Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp582691pxb; Tue, 19 Oct 2021 08:49:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz9VYp1LD/aA+6LGu6JNT6IqhFnEBL5MXd6vDxkSMCz2Gh/n6IbuNJka4eZrkRBsND35Yxr X-Received: by 2002:aa7:9d0b:0:b0:44c:62a6:8679 with SMTP id k11-20020aa79d0b000000b0044c62a68679mr593339pfp.0.1634658590881; Tue, 19 Oct 2021 08:49:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634658590; cv=none; d=google.com; s=arc-20160816; b=NRl/OKPyDeWf2fR22hXTTxl1pumAz+ZeQy19zADkHVfe6PXVSrutkDMKNRX5AQUVCv gx+TGNjzUJzU1yq5tVwAwcPSQ8LCM7YbXzvV+vltpSn7udSlRobF4Rprko1LWWbDuSKk m479utRVO4eggb6TNsi1is4fLkOLILuxAz1C5zs2GowtSGl2epEwTLY7B2l5ODnidH2/ 67N4o4WT1cOJNab+eZwAT5Bphww9KLaaezlbnMFJdCbp6BKVULv80NDkx8I+WT9Mm7FT 8B0LX6w7lCH8FuB0VBYNjwIhKnHsmwv1++eOAMTVYXhdyM/7oazegZphjcCZtEXK46ft 9Ekw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:dkim-signature; bh=AuFXMWgPNu/ob1w45v2bwC1eTYwhpFz8+Ou6HJ1sINc=; b=pQYkoR+18Skw1pS14xbRUG7elpQVtlJ1n87v00nchizk1H2eWK1I81OCJ+4j9PWuiL HkoobtDzqVjGjqAsUjRCojTTakDXcXpRe5+LaNZaZtEu6SkdpQLNaAaWHt+YCxJmO2x8 6pKn3VApeOuVYtQTG+9Jl8e9q1jdeY3/rxo8cjTbh3UqcKv7ePUrd/Ltt95rwQf5q6E6 tbM3ggRrQL7j7l93CGK+xnC/nJoUWOfL2O8siORSjR5lvt9HDQ6YyzK4agB+smsRiBHp UXRF5ED+1orP2nq2Yr4g82fJ0wlo8U9/Hm0L5wAFlzZNyohsY0uFkKXONhOrOS+RuDkU nnOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=jI8mBT9N; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e6si10081374pfv.355.2021.10.19.08.49.37; Tue, 19 Oct 2021 08:49:50 -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; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=jI8mBT9N; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230127AbhJSPvi (ORCPT + 99 others); Tue, 19 Oct 2021 11:51:38 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]:57622 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229803AbhJSPvh (ORCPT ); Tue, 19 Oct 2021 11:51:37 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id DFBC2219D0; Tue, 19 Oct 2021 15:49:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1634658563; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AuFXMWgPNu/ob1w45v2bwC1eTYwhpFz8+Ou6HJ1sINc=; b=jI8mBT9NN3CJ98e8TRrBe6+W4IX9QJHhQVVEKnl3en/oUgwXJ1BtjxKiiFkWwgj9VIseiX T6qtord8tZ7xDsvRJGxPWnmadejJJpLBcmyDH1ASV21xRAuFOoTHhuO2Q+aN+0HjYC0TeU RoIu5cPOyMzZsNgD6DpIFmTLpD/TZLc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1634658563; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AuFXMWgPNu/ob1w45v2bwC1eTYwhpFz8+Ou6HJ1sINc=; b=+hXA1ubFurqb0bbuOQkaDlB7NFuBhX15D2Rqr0TyPRZda1n6LcfRhIwECdaOHILJn/cbn7 7km0z2ZAp5KnwzBw== Received: from quack2.suse.cz (unknown [10.100.200.198]) by relay2.suse.de (Postfix) with ESMTP id C3672A3B85; Tue, 19 Oct 2021 15:49:23 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id A80B01E0983; Tue, 19 Oct 2021 17:49:20 +0200 (CEST) Date: Tue, 19 Oct 2021 17:49:20 +0200 From: Jan Kara To: Gabriel Krisman Bertazi Cc: jack@suse.com, amir73il@gmail.com, djwong@kernel.org, tytso@mit.edu, david@fromorbit.com, dhowells@redhat.com, khazhy@google.com, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-api@vger.kernel.org, kernel@collabora.com Subject: Re: [PATCH v8 31/32] samples: Add fs error monitoring example Message-ID: <20211019154920.GS3255@quack2.suse.cz> References: <20211019000015.1666608-1-krisman@collabora.com> <20211019000015.1666608-32-krisman@collabora.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211019000015.1666608-32-krisman@collabora.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Mon 18-10-21 21:00:14, Gabriel Krisman Bertazi wrote: > Introduce an example of a FAN_FS_ERROR fanotify user to track filesystem > errors. > > Reviewed-by: Amir Goldstein > Signed-off-by: Gabriel Krisman Bertazi Looks good. Feel free to add: Reviewed-by: Jan Kara Honza > --- > Changes since v4: > - Protect file_handle defines with ifdef guards > > Changes since v1: > - minor fixes > --- > samples/Kconfig | 9 +++ > samples/Makefile | 1 + > samples/fanotify/Makefile | 5 ++ > samples/fanotify/fs-monitor.c | 142 ++++++++++++++++++++++++++++++++++ > 4 files changed, 157 insertions(+) > create mode 100644 samples/fanotify/Makefile > create mode 100644 samples/fanotify/fs-monitor.c > > diff --git a/samples/Kconfig b/samples/Kconfig > index b0503ef058d3..88353b8eac0b 100644 > --- a/samples/Kconfig > +++ b/samples/Kconfig > @@ -120,6 +120,15 @@ 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_FS_ERROR fanotify mechanism to monitor filesystem > + errors. > + See also Documentation/admin-guide/filesystem-monitoring.rst. > + > config SAMPLE_HIDRAW > bool "hidraw sample" > depends on CC_CAN_LINK && HEADERS_INSTALL > diff --git a/samples/Makefile b/samples/Makefile > index 087e0988ccc5..931a81847c48 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..e20db1bdde3b > --- /dev/null > +++ b/samples/fanotify/Makefile > @@ -0,0 +1,5 @@ > +# SPDX-License-Identifier: GPL-2.0-only > +userprogs-always-y += fs-monitor > + > +userccflags += -I usr/include -Wall > + > diff --git a/samples/fanotify/fs-monitor.c b/samples/fanotify/fs-monitor.c > new file mode 100644 > index 000000000000..a0e44cd31e6f > --- /dev/null > +++ b/samples/fanotify/fs-monitor.c > @@ -0,0 +1,142 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright 2021, Collabora Ltd. > + */ > + > +#define _GNU_SOURCE > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#ifndef FAN_FS_ERROR > +#define FAN_FS_ERROR 0x00008000 > +#define FAN_EVENT_INFO_TYPE_ERROR 5 > + > +struct fanotify_event_info_error { > + struct fanotify_event_info_header hdr; > + __s32 error; > + __u32 error_count; > +}; > +#endif > + > +#ifndef FILEID_INO32_GEN > +#define FILEID_INO32_GEN 1 > +#endif > + > +#ifndef FILEID_INVALID > +#define FILEID_INVALID 0xff > +#endif > + > +static void print_fh(struct file_handle *fh) > +{ > + int i; > + uint32_t *h = (uint32_t *) fh->f_handle; > + > + printf("\tfh: "); > + for (i = 0; i < fh->handle_bytes; i++) > + printf("%hhx", fh->f_handle[i]); > + printf("\n"); > + > + printf("\tdecoded fh: "); > + if (fh->handle_type == FILEID_INO32_GEN) > + printf("inode=%u gen=%u\n", h[0], h[1]); > + else if (fh->handle_type == FILEID_INVALID && !fh->handle_bytes) > + printf("Type %d (Superblock error)\n", fh->handle_type); > + else > + printf("Type %d (Unknown)\n", fh->handle_type); > + > +} > + > +static void handle_notifications(char *buffer, int len) > +{ > + struct fanotify_event_metadata *event = > + (struct fanotify_event_metadata *) buffer; > + struct fanotify_event_info_header *info; > + struct fanotify_event_info_error *err; > + struct fanotify_event_info_fid *fid; > + int off; > + > + for (; FAN_EVENT_OK(event, len); event = FAN_EVENT_NEXT(event, len)) { > + > + if (event->mask != FAN_FS_ERROR) { > + printf("unexpected FAN MARK: %llx\n", event->mask); > + goto next_event; > + } > + > + if (event->fd != FAN_NOFD) { > + printf("Unexpected fd (!= FAN_NOFD)\n"); > + goto next_event; > + } > + > + printf("FAN_FS_ERROR (len=%d)\n", event->event_len); > + > + for (off = sizeof(*event) ; off < event->event_len; > + off += info->len) { > + info = (struct fanotify_event_info_header *) > + ((char *) event + off); > + > + switch (info->info_type) { > + case FAN_EVENT_INFO_TYPE_ERROR: > + err = (struct fanotify_event_info_error *) info; > + > + printf("\tGeneric Error Record: len=%d\n", > + err->hdr.len); > + printf("\terror: %d\n", err->error); > + printf("\terror_count: %d\n", err->error_count); > + break; > + > + case FAN_EVENT_INFO_TYPE_FID: > + fid = (struct fanotify_event_info_fid *) info; > + > + printf("\tfsid: %x%x\n", > + fid->fsid.val[0], fid->fsid.val[1]); > + print_fh((struct file_handle *) &fid->handle); > + break; > + > + default: > + printf("\tUnknown info type=%d len=%d:\n", > + info->info_type, info->len); > + } > + } > +next_event: > + printf("---\n\n"); > + } > +} > + > +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_REPORT_FID, O_RDONLY); > + if (fd < 0) > + errx(1, "fanotify_init"); > + > + if (fanotify_mark(fd, FAN_MARK_ADD|FAN_MARK_FILESYSTEM, > + FAN_FS_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.33.0 > -- Jan Kara SUSE Labs, CR