linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Amir Goldstein <amir73il@gmail.com>
To: Gabriel Krisman Bertazi <krisman@collabora.com>
Cc: LTP List <ltp@lists.linux.it>, Jan Kara <jack@suse.com>,
	Ext4 <linux-ext4@vger.kernel.org>,
	Khazhismel Kumykov <khazhy@google.com>,
	kernel@collabora.com
Subject: Re: [PATCH 1/7] syscalls/fanotify20: Introduce helpers for FAN_FS_ERROR test
Date: Tue, 3 Aug 2021 11:30:39 +0300	[thread overview]
Message-ID: <CAOQ4uxjRULAa_+n-6xf--7B=afEVN_7kzJ0H8QQP7ZQwA8Ahig@mail.gmail.com> (raw)
In-Reply-To: <20210802214645.2633028-2-krisman@collabora.com>

On Tue, Aug 3, 2021 at 12:47 AM Gabriel Krisman Bertazi
<krisman@collabora.com> wrote:
>
> fanotify20 is a new test validating the FAN_FS_ERROR file system error
> event.  This adds some basic structure for the next patches.
>
> The strategy for error reporting testing in fanotify20 goes like this:
>
>   - Generate a broken filesystem
>   - Start FAN_FS_ERROR monitoring group
>   - Make the file system  notice the error through ordinary operations
>   - Observe the event generated
>
> Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
> ---
>  testcases/kernel/syscalls/fanotify/.gitignore |   1 +
>  .../kernel/syscalls/fanotify/fanotify20.c     | 135 ++++++++++++++++++
>  2 files changed, 136 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/fanotify/fanotify20.c
>
> diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore
> index 9554b16b196e..c99e6fff76d6 100644
> --- a/testcases/kernel/syscalls/fanotify/.gitignore
> +++ b/testcases/kernel/syscalls/fanotify/.gitignore
> @@ -17,4 +17,5 @@
>  /fanotify17
>  /fanotify18
>  /fanotify19
> +/fanotify20
>  /fanotify_child
> diff --git a/testcases/kernel/syscalls/fanotify/fanotify20.c b/testcases/kernel/syscalls/fanotify/fanotify20.c
> new file mode 100644
> index 000000000000..50531bd99cc9
> --- /dev/null
> +++ b/testcases/kernel/syscalls/fanotify/fanotify20.c
> @@ -0,0 +1,135 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2021 Collabora Ltd.
> + *
> + * Author: Gabriel Krisman Bertazi <gabriel@krisman.be>
> + * Based on previous work by Amir Goldstein <amir73il@gmail.com>
> + */
> +
> +/*\
> + * [Description]
> + * Check fanotify FAN_ERROR_FS events triggered by intentionally
> + * corrupted filesystems:
> + *
> + * - Generate a broken filesystem
> + * - Start FAN_FS_ERROR monitoring group
> + * - Make the file system notice the error through ordinary operations
> + * - Observe the event generated
> + */
> +
> +#define _GNU_SOURCE
> +#include "config.h"
> +
> +#include <stdio.h>
> +#include <sys/stat.h>
> +#include <sys/types.h>
> +#include <errno.h>
> +#include <string.h>
> +#include <sys/mount.h>
> +#include <sys/syscall.h>
> +#include "tst_test.h"
> +#include <sys/fanotify.h>
> +#include <sys/types.h>
> +#include <fcntl.h>
> +
> +#ifdef HAVE_SYS_FANOTIFY_H
> +#include "fanotify.h"
> +
> +#ifndef FAN_FS_ERROR
> +#define FAN_FS_ERROR           0x00008000
> +#endif
> +
> +#define BUF_SIZE 256
> +static char event_buf[BUF_SIZE];
> +int fd_notify;
> +
> +#define MOUNT_PATH "test_mnt"
> +
> +static const struct test_case {
> +       char *name;
> +       void (*trigger_error)(void);
> +       void (*prepare_fs)(void);
> +} testcases[] = {
> +};
> +
> +int check_error_event_metadata(struct fanotify_event_metadata *event)
> +{
> +       int fail = 0;
> +
> +       if (event->mask != FAN_FS_ERROR) {
> +               fail++;
> +               tst_res(TFAIL, "got unexpected event %llx",
> +                       (unsigned long long)event->mask);
> +       }
> +
> +       if (event->fd != FAN_NOFD) {
> +               fail++;
> +               tst_res(TFAIL, "Weird FAN_FD %llx",
> +                       (unsigned long long)event->mask);
> +       }
> +       return fail;
> +}
> +
> +void check_event(char *buf, size_t len, const struct test_case *ex)
> +{
> +       struct fanotify_event_metadata *event =
> +               (struct fanotify_event_metadata *) buf;
> +
> +       if (len < FAN_EVENT_METADATA_LEN)
> +               tst_res(TFAIL, "No event metadata found");
> +
> +       if (check_error_event_metadata(event))
> +               return;
> +
> +       tst_res(TPASS, "Successfully received: %s", ex->name);
> +}
> +
> +static void do_test(unsigned int i)
> +{
> +       const struct test_case *tcase = &testcases[i];
> +       size_t read_len;
> +
> +       tcase->trigger_error();
> +
> +       read_len = SAFE_READ(0, fd_notify, event_buf, BUF_SIZE);
> +
> +       check_event(event_buf, read_len, tcase);
> +}
> +
> +static void setup(void)
> +{
> +       unsigned long i;
> +
> +       for (i = 0; i < ARRAY_SIZE(testcases); i++)
> +               if (testcases[i].prepare_fs)
> +                       testcases[i].prepare_fs();
> +

Why is prepare_fs called up front and not on every test case?

> +       fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF|FAN_REPORT_FID,
> +                                      O_RDONLY);
> +
> +       SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD|FAN_MARK_FILESYSTEM,
> +                          FAN_FS_ERROR, AT_FDCWD, MOUNT_PATH);

This will cause test to fail on old kernels.
You need to start this test with
fanotify_events_supported_by_kernel(FAN_FS_ERROR)
but you cannot use it as is.

Create a macro like
REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS
which calls fanotify_init_flags_err_msg(...fanotify_events_supported_by_kernel())
and pass init flags as argument to fanotify_events_supported_by_kernel()
instead of using hardcoded flags FAN_CLASS_CONTENT.


> +}
> +
> +static void cleanup(void)
> +{
> +       if (fd_notify > 0)
> +               SAFE_CLOSE(fd_notify);
> +}
> +
> +static struct tst_test test = {
> +       .test = do_test,
> +       .tcnt = ARRAY_SIZE(testcases),
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .mount_device = 1,
> +       .mntpoint = MOUNT_PATH,
> +       .all_filesystems = 0,

This is 0 by default

Thanks,
Amir.

  reply	other threads:[~2021-08-03  8:30 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-02 21:46 [PATCH 0/7] Test the new fanotify FAN_FS_ERROR event Gabriel Krisman Bertazi
2021-08-02 21:46 ` [PATCH 1/7] syscalls/fanotify20: Introduce helpers for FAN_FS_ERROR test Gabriel Krisman Bertazi
2021-08-03  8:30   ` Amir Goldstein [this message]
2021-08-02 21:46 ` [PATCH 2/7] syscalls/fanotify20: Validate the generic error info Gabriel Krisman Bertazi
2021-08-03  8:42   ` Amir Goldstein
2021-08-02 21:46 ` [PATCH 3/7] syscalls/fanotify20: Validate incoming FID in FAN_FS_ERROR Gabriel Krisman Bertazi
2021-08-03  8:56   ` Amir Goldstein
2021-08-04  4:54     ` Gabriel Krisman Bertazi
2021-08-04  5:39       ` Amir Goldstein
2021-08-04  7:40         ` Matthew Bobrowski
2021-08-20 10:21         ` [LTP] " Petr Vorel
2021-08-20 21:58           ` Matthew Bobrowski
2021-08-23  9:35             ` Jan Kara
2021-08-23 11:19               ` Matthew Bobrowski
2021-08-23 14:34             ` Gabriel Krisman Bertazi
2021-08-02 21:46 ` [PATCH 4/7] syscalls/fanotify20: Watch event after filesystem abort Gabriel Krisman Bertazi
2021-08-02 21:46 ` [PATCH 5/7] syscalls/fanotify20: Support submission of debugfs commands Gabriel Krisman Bertazi
2021-08-02 21:46 ` [PATCH 6/7] syscalls/fanotify20: Test file event with broken inode Gabriel Krisman Bertazi
2021-08-03  9:04   ` Amir Goldstein
2021-08-03  9:08   ` Amir Goldstein
2021-08-04  4:52     ` Gabriel Krisman Bertazi
2021-08-04  5:27       ` Amir Goldstein
2021-08-05 21:50         ` Gabriel Krisman Bertazi
2021-08-02 21:46 ` [PATCH 7/7] syscalls/fanotify20: Test capture of multiple errors Gabriel Krisman Bertazi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOQ4uxjRULAa_+n-6xf--7B=afEVN_7kzJ0H8QQP7ZQwA8Ahig@mail.gmail.com' \
    --to=amir73il@gmail.com \
    --cc=jack@suse.com \
    --cc=kernel@collabora.com \
    --cc=khazhy@google.com \
    --cc=krisman@collabora.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).