Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp3871581pxy; Mon, 26 Apr 2021 11:45:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9bgPIrBbWEY7KkOa03tfC7rMiv30A5D6BalRQYuolBDiZxTj1aZGbFopgEwczYyB5mA93 X-Received: by 2002:a17:906:90b:: with SMTP id i11mr20103158ejd.168.1619462731411; Mon, 26 Apr 2021 11:45:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619462731; cv=none; d=google.com; s=arc-20160816; b=SvUYbxVZBi4de7ZXz12A4fUSVCo2TH9Mkw2rxyT0bRvwTS5Q9jVaqsGRBtraKE9m1o 9GSl59JieKn5mt/DxPHSxtcrjbQNU+eujSDK5U3Z17z/jswbCIw/KJZ6ifVgejRXQON8 DjQvTdlCJOXRJKuFfpK7CVB33kjprkCFfaSAVOsonmGOXbdI1txLFN81v3PSOOLOoVrm MmCqYx6BQpWYLcaeF1b/h0aqYm+UOQDxQxBCe3ulV3oBTZAbKPDDz2t8VzcM8iENhP77 /29Q3YJS6NQb7BamyS2S/d0edWHPF2+NVOq+uG+asbpuYZ2oEDPj04ipEE6/fMsFEY8a OQ4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Y3r8xDuSNrBJK2dsyI3gnP0NPKZ254CGt98bbcqcSRg=; b=RWzF7t0TEwp/YsaPiiDGYFqf6h58u8S7LYKouVCzAR2rq8iUcUAYtkCByVPiT0dUEf 83+WL8o5XZaBC7cTWvJ22f2PgIk+sNZZ87uixJPC8KVPd9XsA0oIh1TYBGZzw+xTgUCQ Pvg+cfwXC6hVUv/qTCzemxFAUBAOZmcUFXiKVe/IvsYdQnPNSb6Piat6G2EiA52gW3Q/ oBjTx3wLfUbF9gFK/60RiG9UbqrPmW6gs9PePCGk5oNWWENIn1l/+UoLfJX2YsPNSP6T yQ10gUn6wXr17NUROAn0FCphBePWKCVXZWqKNmNOmx4cZ4cJT/IgJdqC/9GQo/ZP9arQ AxYQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k3si555160eds.434.2021.04.26.11.45.08; Mon, 26 Apr 2021 11:45:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238579AbhDZSpr (ORCPT + 99 others); Mon, 26 Apr 2021 14:45:47 -0400 Received: from bhuna.collabora.co.uk ([46.235.227.227]:47610 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239508AbhDZSn7 (ORCPT ); Mon, 26 Apr 2021 14:43:59 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id A0D821F422D6 From: Gabriel Krisman Bertazi To: amir73il@gmail.com, tytso@mit.edu, djwong@kernel.org Cc: david@fromorbit.com, jack@suse.com, dhowells@redhat.com, khazhy@google.com, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, Gabriel Krisman Bertazi , kernel@collabora.com Subject: [PATCH RFC 14/15] samples: Add fs error monitoring example Date: Mon, 26 Apr 2021 14:42:00 -0400 Message-Id: <20210426184201.4177978-15-krisman@collabora.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210426184201.4177978-1-krisman@collabora.com> References: <20210426184201.4177978-1-krisman@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Introduce an example of a FAN_ERROR fanotify user to track filesystem errors. Signed-off-by: Gabriel Krisman Bertazi --- samples/Kconfig | 7 ++ samples/Makefile | 1 + samples/fanotify/Makefile | 3 + samples/fanotify/fs-monitor.c | 135 ++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 samples/fanotify/Makefile create mode 100644 samples/fanotify/fs-monitor.c diff --git a/samples/Kconfig b/samples/Kconfig index e76cdfc50e25..a2968338517f 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -120,6 +120,13 @@ config SAMPLE_CONNECTOR with it. See also Documentation/driver-api/connector.rst +config SAMPLE_FANOTIFY_ERROR + bool "Build fanotify error monitoring sample" + depends on FANOTIFY + help + When enabled, this builds an example code that uses the FAN_ERROR + fanotify mechanism to monitor filesystem errors. + config SAMPLE_HIDRAW bool "hidraw sample" depends on CC_CAN_LINK && HEADERS_INSTALL diff --git a/samples/Makefile b/samples/Makefile index c3392a595e4b..93e2d64bc9a7 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -5,6 +5,7 @@ subdir-$(CONFIG_SAMPLE_AUXDISPLAY) += auxdisplay subdir-$(CONFIG_SAMPLE_ANDROID_BINDERFS) += binderfs obj-$(CONFIG_SAMPLE_CONFIGFS) += configfs/ obj-$(CONFIG_SAMPLE_CONNECTOR) += connector/ +obj-$(CONFIG_SAMPLE_FANOTIFY_ERROR) += fanotify/ subdir-$(CONFIG_SAMPLE_HIDRAW) += hidraw obj-$(CONFIG_SAMPLE_HW_BREAKPOINT) += hw_breakpoint/ obj-$(CONFIG_SAMPLE_KDB) += kdb/ diff --git a/samples/fanotify/Makefile b/samples/fanotify/Makefile new file mode 100644 index 000000000000..b3d5cc826e6f --- /dev/null +++ b/samples/fanotify/Makefile @@ -0,0 +1,3 @@ +userprogs-always-y += fs-monitor + +userccflags += -I usr/include diff --git a/samples/fanotify/fs-monitor.c b/samples/fanotify/fs-monitor.c new file mode 100644 index 000000000000..cdece8344c20 --- /dev/null +++ b/samples/fanotify/fs-monitor.c @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2021, Collabora Ltd. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef FAN_ERROR + +#define FAN_ERROR 0x00100000 +#define FAN_PREALLOC_QUEUE 0x00000080 + +#define FAN_EVENT_INFO_TYPE_LOCATION 4 +#define FAN_EVENT_INFO_TYPE_ERROR 5 +#define FAN_EVENT_INFO_TYPE_FSDATA 6 + +struct fanotify_event_info_error { + struct fanotify_event_info_header hdr; + int version; + int error; + long long unsigned fsid; +}; + +struct fanotify_event_info_location { + struct fanotify_event_info_header hdr; + int line; + char function[0]; +}; + +struct fanotify_event_info_fsdata { + struct fanotify_event_info_header hdr; + char data[0]; +}; + +struct ext4_error_inode_report { + unsigned long long inode; + unsigned long long block; + char desc[40]; +}; +#endif + +static void handle_notifications(char *buffer, int len) +{ + struct fanotify_event_metadata *metadata; + struct fanotify_event_info_header *hdr = 0; + char *off, *next; + + for (metadata = (struct fanotify_event_metadata *) buffer; + FAN_EVENT_OK(metadata, len); metadata = FAN_EVENT_NEXT(metadata, len)) { + next = (char*)metadata + metadata->event_len; + if (!(metadata->mask == FAN_ERROR)) { + printf("unexpected FAN MARK: %llx\n", metadata->mask); + continue; + } + if (metadata->fd != FAN_NOFD) { + printf("bizar fd != FAN_NOFD\n"); + continue;; + } + + printf("FAN_ERROR found len=%d\n", metadata->event_len); + + for (off = (char*)(metadata+1); off < next; off = off + hdr->len) { + hdr = (struct fanotify_event_info_header*)(off); + + if (hdr->info_type == FAN_EVENT_INFO_TYPE_ERROR) { + struct fanotify_event_info_error *error = + (struct fanotify_event_info_error*) hdr; + + printf(" Generic Error Record: len=%d\n", hdr->len); + printf(" version: %d\n", error->version); + printf(" error: %d\n", error->error); + printf(" fsid: %llx\n", error->fsid); + + } else if(hdr->info_type == FAN_EVENT_INFO_TYPE_LOCATION) { + struct fanotify_event_info_location *loc = + (struct fanotify_event_info_location*) hdr; + + printf(" Location Record Size = %d\n", loc->hdr.len); + printf(" loc=%s:%d\n", loc->function, loc->line); + + } else if(hdr->info_type == FAN_EVENT_INFO_TYPE_FSDATA) { + struct fanotify_event_info_fsdata *data = + (struct fanotify_event_info_fsdata *)hdr; + struct ext4_error_inode_report *fsdata = + (struct ext4_error_inode_report*) ((char*)data->data); + + printf(" Fsdata Record: len=%d\n", hdr->len); + printf(" inode=%llu\n", fsdata->inode); + if (fsdata->block != -1L) + printf(" block=%llu\n", fsdata->block); + printf(" desc=%s\n", fsdata->desc); + } + } + } +} + +int main(int argc, char **argv) +{ + int fd; + char buffer[BUFSIZ]; + + if (argc < 2) { + printf("Missing path argument\n"); + return 1; + } + + fd = fanotify_init(FAN_CLASS_NOTIF|FAN_PREALLOC_QUEUE, O_RDONLY); + if (fd < 0) + errx(1, "fanotify_init"); + + if (fanotify_mark(fd, FAN_MARK_ADD|FAN_MARK_FILESYSTEM, + FAN_ERROR, AT_FDCWD, argv[1])) { + errx(1, "fanotify_mark"); + } + + while (1) { + int n = read(fd, buffer, BUFSIZ); + if (n < 0) + errx(1, "read"); + + handle_notifications(buffer, n); + } + + return 0; +} -- 2.31.0