From patchwork Tue Apr 21 06:49:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 1273991 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=J+QgF/bq; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 495vMY1019z9sSK for ; Tue, 21 Apr 2020 16:50:16 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id DC4BA3C29B7 for ; Tue, 21 Apr 2020 08:50:13 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [217.194.8.2]) by picard.linux.it (Postfix) with ESMTP id 59AC33C2994 for ; Tue, 21 Apr 2020 08:50:12 +0200 (CEST) Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id F32F7600EA0 for ; Tue, 21 Apr 2020 08:50:11 +0200 (CEST) Received: by mail-wr1-x444.google.com with SMTP id s10so5980456wrr.0 for ; Mon, 20 Apr 2020 23:50:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Y8WZ76d/ZzoqUnjeRyaGKZ3m3YW2udWjlvXOXDdAOZ0=; b=J+QgF/bqla9cvsKzSQQ7PS5buIby02is0HCONBXevtr90GZ4++ROl8aoMk3npLwg3d J0MIg2IfJFE3T83EzigWSXiCuPxN464Y5QBpd1TzWI3nI9A5EWx94NF+OXPjGINQm7js ABnJLSLymJxLs+KjaWbcxNAqYTBo8vb2hXumfhC+m3LXS2YpBU8v/ZIQtatVvHB+QgEM FdE3g92hqMj1Br07cwcMzZC9/4OYwVxF6TnkgT/QrvZUcOhZLBJYJqgzWrWTmExFXYLx ner7NRSkD9EyW1OEV2TOhqCRu9qj3JzkfRZG8xSH8TGd6i4pWCNK3G19CHrMFydK5tUy 3D7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Y8WZ76d/ZzoqUnjeRyaGKZ3m3YW2udWjlvXOXDdAOZ0=; b=qjE+oQxxLF5JTTfcsl5iact7xOAmlOYXZWU+yzc2SnkZZH2pteaxYnjP6El8H8RJ3H rze9j0fUjnvBFmzNeA9DlKhLTo+0lzpzrIzV+y4FMgvhbHoVxtddkLJEOT2LMqktv9Bn PqcvBGeyfwf59/F7SpJDk1djF/k3Q0EO1oDxe++gChdNYDff2xd50IlT/VhxDikRXKMm 94k5LL5fuXivR9ss+E7PqOJksAhV/R3kse/Ru9VmeqoHJrBpM778BP2NZZXtqzd15ivb 84JzbVmIZKiBa0bgML6NoUH/qxp5FGd+8q/BHeEZY9yWYscaXUAV5MRyaexoKl2/gyjn S8yQ== X-Gm-Message-State: AGi0PuZ4LYMwbXwRRos9NvXJBoW5TQkKtU3E0FjdrFzHigmATgunXP+6 2+jOgk2BNg6vgr5XcF9xngo= X-Google-Smtp-Source: APiQypKQHGFsZ98UKFXq0XgaTpF5vx6RFTIuj6n0ePsHFoADheJ3jSyu9dzPmlshWxICSKIfipz9nQ== X-Received: by 2002:a5d:6a04:: with SMTP id m4mr24064141wru.326.1587451811578; Mon, 20 Apr 2020 23:50:11 -0700 (PDT) Received: from localhost.localdomain ([141.226.12.123]) by smtp.gmail.com with ESMTPSA id h16sm2698093wrw.36.2020.04.20.23.50.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2020 23:50:11 -0700 (PDT) From: Amir Goldstein To: Cyril Hrubis Date: Tue, 21 Apr 2020 09:49:59 +0300 Message-Id: <20200421065002.12417-2-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200421065002.12417-1-amir73il@gmail.com> References: <20200421065002.12417-1-amir73il@gmail.com> X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH 1/4] syscalls/fanotify09: Check merging of events on directories X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Matthew Bobrowski , Jan Kara , ltp@lists.linux.it MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" In a setup of mount mark and directory inode mark the FAN_ONDIR flag set on one mark should not imply that all events in the other mark mask are expected on directories as well. Add a regression test case for commit 55bf882c7f13: fanotify: fix merging marks masks with FAN_ONDIR Signed-off-by: Amir Goldstein Reviewed-by: Matthew Bobrowski --- .../kernel/syscalls/fanotify/fanotify09.c | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/testcases/kernel/syscalls/fanotify/fanotify09.c b/testcases/kernel/syscalls/fanotify/fanotify09.c index 0f6a9e864..68a4e5081 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify09.c +++ b/testcases/kernel/syscalls/fanotify/fanotify09.c @@ -15,6 +15,10 @@ * Test case #2 is a regression test for commit b469e7e47c8a: * * fanotify: fix handling of events on child sub-directory + * + * Test case #3 is a regression test for commit 55bf882c7f13: + * + * fanotify: fix merging marks masks with FAN_ONDIR */ #define _GNU_SOURCE #include "config.h" @@ -57,16 +61,25 @@ static int mount_created; static struct tcase { const char *tname; unsigned int ondir; + const char *testdir; int nevents; } tcases[] = { { "Events on children with both inode and mount marks", 0, + DIR_NAME, 1, }, { "Events on children and subdirs with both inode and mount marks", FAN_ONDIR, + DIR_NAME, + 2, + }, + { + "Events on files and dirs with both inode and mount marks", + FAN_ONDIR, + ".", 2, }, }; @@ -125,6 +138,20 @@ static void cleanup_fanotify_groups(void) } } +static void event_res(int ttype, int group, + struct fanotify_event_metadata *event) +{ + int len; + sprintf(symlnk, "/proc/self/fd/%d", event->fd); + len = readlink(symlnk, fdpath, sizeof(fdpath)); + if (len < 0) + len = 0; + fdpath[len] = 0; + tst_res(ttype, "group %d got event: mask %llx pid=%u fd=%d path=%s", + group, (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, fdpath); +} + static void verify_event(int group, struct fanotify_event_metadata *event, uint32_t expect) { @@ -139,15 +166,7 @@ static void verify_event(int group, struct fanotify_event_metadata *event, (unsigned long long)event->mask, (unsigned)event->pid, (unsigned)getpid(), event->fd); } else { - int len; - sprintf(symlnk, "/proc/self/fd/%d", event->fd); - len = readlink(symlnk, fdpath, sizeof(fdpath)); - if (len < 0) - len = 0; - fdpath[len] = 0; - tst_res(TPASS, "group %d got event: mask %llx pid=%u fd=%d path=%s", - group, (unsigned long long)event->mask, - (unsigned)event->pid, event->fd, fdpath); + event_res(TPASS, group, event); } } @@ -167,9 +186,9 @@ static void test_fanotify(unsigned int n) */ SAFE_FILE_PRINTF(fname, "1"); /* - * generate FAN_CLOSE_NOWRITE event on a child subdir. + * generate FAN_CLOSE_NOWRITE event on a testdir (subdir or ".") */ - dirfd = SAFE_OPEN(DIR_NAME, O_RDONLY); + dirfd = SAFE_OPEN(tc->testdir, O_RDONLY); if (dirfd >= 0) SAFE_CLOSE(dirfd); @@ -210,13 +229,12 @@ static void test_fanotify(unsigned int n) /* * Then verify the rest of the groups did not get the MODIFY event and - * did not get the FAN_CLOSE_NOWRITE event on subdir. + * did not get the FAN_CLOSE_NOWRITE event on testdir. */ for (i = 1; i < NUM_GROUPS; i++) { ret = read(fd_notify[i], event_buf, FAN_EVENT_METADATA_LEN); if (ret > 0) { - tst_res(TFAIL, "group %d got event", i); - verify_event(i, event, FAN_CLOSE_NOWRITE); + event_res(TFAIL, i, event); if (event->fd != FAN_NOFD) SAFE_CLOSE(event->fd); continue; From patchwork Tue Apr 21 06:50:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 1273993 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=AGVp8ojo; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 495vMv2HMtz9sSK for ; Tue, 21 Apr 2020 16:50:35 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id C17F93C2999 for ; Tue, 21 Apr 2020 08:50:32 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [217.194.8.2]) by picard.linux.it (Postfix) with ESMTP id 98C6D3C29B3 for ; Tue, 21 Apr 2020 08:50:13 +0200 (CEST) Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 3234A600EA0 for ; Tue, 21 Apr 2020 08:50:13 +0200 (CEST) Received: by mail-wr1-x442.google.com with SMTP id k13so13927693wrw.7 for ; Mon, 20 Apr 2020 23:50:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Nc9N6WE9csRzBQKfMz2lT3zMXF5sdnWyyEY5X7hmii4=; b=AGVp8ojoI0QYvPV+mFcRVDNrajINalsmeRRdeNR5tGqrHRQE8vzfd3e1Fb5BxW6Nrs JqgPIB3Om3A5vZgDoRiS9UUcI7F/7Hmb6fzMnXXkEV2w8FZnPp4JsszO2ZAMmSdKUz47 NTpD0VdHMvV8XGUu+j5DqtaqFnjn5AgR5b2bWYfnptbDiY/urez5wUXfo0B0r/MDysgG GHopuPQe166mJUlDo2VMOEec5bQEqevpmeEl7DWNWJAf2pwx5yuJGjaLspfm81GH+2tZ B2WB/V+f3dEvrvnuOo5Dy5SX0PBa+vBAEfyF4XrxLlRBlakHsnOSkxbtdtmcK4+B2Fgr jgPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Nc9N6WE9csRzBQKfMz2lT3zMXF5sdnWyyEY5X7hmii4=; b=PIQV4tPVfCW0PYNELL+qqtcxSih+6Uxsd6kBip4E7w/RiFK4TiHuK6I+25y+/d0hit aMR1M2MMh7WagvG5izsVdxgrm+ynMLN2XrsEFOqOqerK90ptvR1NJky2/DPn63zQUPiJ 4KVVQU97C+d3vEBwPfdGUKg4vARYhQkgu/Xu2nR3i/vKUxmdbMbf59YVWPA4z4qdTa2s dy7O9Ps2Sr4AWyYmq4MNJI/a6eQgWlC/6UNcoPlR3pDcc0uFOVIujRan0K69PafyVARK Q02xLA06IjddJdBAt30nqmAVswY2Mgoihl9Eoivglg3ETqIHj7jXd0p8yOTJIVcsXM6j T3og== X-Gm-Message-State: AGi0PuZu4/FLYRtodQvry7SMWyhIM8M+4VNNDRdgGMIku//R8eqCSCmX ihf2djLqSwLN7MloDHG3LGU= X-Google-Smtp-Source: APiQypJwgf3UlsjscNOlzFyv/3UwsgzwkOQpySvSHCNwci7ZcSsd2QDq3a6LowtFET4oCAHdAZl5OA== X-Received: by 2002:adf:fad0:: with SMTP id a16mr24869010wrs.149.1587451812783; Mon, 20 Apr 2020 23:50:12 -0700 (PDT) Received: from localhost.localdomain ([141.226.12.123]) by smtp.gmail.com with ESMTPSA id h16sm2698093wrw.36.2020.04.20.23.50.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2020 23:50:12 -0700 (PDT) From: Amir Goldstein To: Cyril Hrubis Date: Tue, 21 Apr 2020 09:50:00 +0300 Message-Id: <20200421065002.12417-3-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200421065002.12417-1-amir73il@gmail.com> References: <20200421065002.12417-1-amir73il@gmail.com> X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH 2/4] syscalls/fanotify15: Minor corrections X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Matthew Bobrowski , Jan Kara , ltp@lists.linux.it MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" - Fix calculation of events buffer size - Read file events and dir events in two batches - Generate FAN_MODIFY event explicitly with truncate() operation instead of FAN_ATTRIB event implicitly with create() operation - FAN_MODIFY and FAN_DELETE_SELF may or may not be merged Signed-off-by: Amir Goldstein Reviewed-by: Petr Vorel Reviewed-by: Cyril Hrubis Reviewed-by: Matthew Bobrowski --- .../kernel/syscalls/fanotify/fanotify15.c | 58 ++++++++++++++----- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/testcases/kernel/syscalls/fanotify/fanotify15.c b/testcases/kernel/syscalls/fanotify/fanotify15.c index e0d513025..454441bfe 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify15.c +++ b/testcases/kernel/syscalls/fanotify/fanotify15.c @@ -25,8 +25,14 @@ #if defined(HAVE_SYS_FANOTIFY_H) #include -#define BUF_SIZE 256 -#define EVENT_MAX 256 +#define EVENT_MAX 10 + +/* Size of the event structure, not including file handle */ +#define EVENT_SIZE (sizeof(struct fanotify_event_metadata) + \ + sizeof(struct fanotify_event_info_fid)) +/* Double events buffer size to account for file handles */ +#define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE * 2) + #define MOUNT_POINT "mntpoint" #define TEST_DIR MOUNT_POINT"/test_dir" @@ -44,7 +50,7 @@ struct event_t { }; static int fanotify_fd; -static char events_buf[BUF_SIZE]; +static char events_buf[EVENT_BUF_LEN]; static struct event_t event_set[EVENT_MAX]; static void do_test(void) @@ -55,23 +61,24 @@ static void do_test(void) struct fanotify_event_metadata *metadata; struct fanotify_event_info_fid *event_fid; + if (fanotify_mark(fanotify_fd, FAN_MARK_ADD | FAN_MARK_FILESYSTEM, - FAN_CREATE | FAN_DELETE | FAN_ATTRIB | - FAN_MOVED_FROM | FAN_MOVED_TO | - FAN_DELETE_SELF | FAN_ONDIR, + FAN_CREATE | FAN_DELETE | FAN_MOVE | + FAN_MODIFY | FAN_DELETE_SELF | FAN_ONDIR, AT_FDCWD, TEST_DIR) == -1) { if (errno == ENODEV) tst_brk(TCONF, "FAN_REPORT_FID not supported on %s " "filesystem", tst_device->fs_type); tst_brk(TBROK | TERRNO, - "fanotify_mark(%d, FAN_MARK_ADD, FAN_CREATE | " - "FAN_DELETE | FAN_MOVED_FROM | FAN_MOVED_TO | " - "FAN_DELETE_SELF | FAN_ONDIR, AT_FDCWD, %s) failed", + "fanotify_mark(%d, FAN_MARK_ADD | FAN_MARK_FILESYSTEM, " + "FAN_CREATE | FAN_DELETE | FAN_MOVE | " + "FAN_MODIFY | FAN_DELETE_SELF | FAN_ONDIR, " + "AT_FDCWD, %s) failed", fanotify_fd, TEST_DIR); } - /* Generate a sequence of events */ + /* All dirent events on testdir are merged */ event_set[count].mask = FAN_CREATE | FAN_MOVED_FROM | FAN_MOVED_TO | \ FAN_DELETE; event_set[count].handle.handle_bytes = MAX_HANDLE_SZ; @@ -82,9 +89,22 @@ static void do_test(void) fd = SAFE_CREAT(FILE1, 0644); SAFE_CLOSE(fd); + /* + * Event on child file is not merged with dirent events. + */ + event_set[count].mask = FAN_MODIFY; + event_set[count].handle.handle_bytes = MAX_HANDLE_SZ; + fanotify_get_fid(FILE1, &event_set[count].fsid, + &event_set[count].handle); + count++; + + SAFE_TRUNCATE(FILE1, 1); SAFE_RENAME(FILE1, FILE2); - event_set[count].mask = FAN_ATTRIB | FAN_DELETE_SELF; + /* + * FAN_DELETE_SELF may be merged with FAN_MODIFY event above. + */ + event_set[count].mask = FAN_DELETE_SELF; event_set[count].handle.handle_bytes = MAX_HANDLE_SZ; fanotify_get_fid(FILE2, &event_set[count].fsid, &event_set[count].handle); @@ -92,6 +112,9 @@ static void do_test(void) SAFE_UNLINK(FILE2); + /* Read file events from the event queue */ + len = SAFE_READ(0, fanotify_fd, events_buf, EVENT_BUF_LEN); + /* * Generate a sequence of events on a directory. Subsequent events * are merged, so it's required that we set FAN_ONDIR once in @@ -118,13 +141,12 @@ static void do_test(void) SAFE_RMDIR(DIR2); - /* Read events from the event queue */ - len = SAFE_READ(0, fanotify_fd, events_buf, BUF_SIZE); + /* Read dir events from the event queue */ + len += SAFE_READ(0, fanotify_fd, events_buf + len, EVENT_BUF_LEN - len); /* Process each event in buffer */ for (i = 0, metadata = (struct fanotify_event_metadata *) events_buf; - FAN_EVENT_OK(metadata, len); - metadata = FAN_EVENT_NEXT(metadata,len), i++) { + FAN_EVENT_OK(metadata, len); i++) { event_fid = (struct fanotify_event_info_fid *) (metadata + 1); event_file_handle = (struct file_handle *) event_fid->handle; @@ -141,7 +163,7 @@ static void do_test(void) "Received unexpected file descriptor %d in " "event. Expected to get FAN_NOFD(%d)", metadata->fd, FAN_NOFD); - } else if (metadata->mask != event_set[i].mask) { + } else if (!(metadata->mask & event_set[i].mask)) { tst_res(TFAIL, "Got event: mask=%llx (expected %llx) " "pid=%u fd=%d", @@ -197,6 +219,10 @@ static void do_test(void) *(unsigned long *) event_file_handle->f_handle); } + metadata->mask &= ~event_set[i].mask; + /* No events left in current mask? Go for next event */ + if (metadata->mask == 0) + metadata = FAN_EVENT_NEXT(metadata, len); } for (; i < count; i++) From patchwork Tue Apr 21 06:50:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 1273994 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=kIcweVz/; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 495vN72cGDz9sSK for ; Tue, 21 Apr 2020 16:50:47 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 26AD03C29C4 for ; Tue, 21 Apr 2020 08:50:42 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [217.194.8.2]) by picard.linux.it (Postfix) with ESMTP id E70393C29CA for ; Tue, 21 Apr 2020 08:50:14 +0200 (CEST) Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 80C13600CE6 for ; Tue, 21 Apr 2020 08:50:14 +0200 (CEST) Received: by mail-wm1-x341.google.com with SMTP id y24so2329218wma.4 for ; Mon, 20 Apr 2020 23:50:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Vizo/7h+DNIo+oYns++2GWC3GgmSkwVcEsnXQ7O1hko=; b=kIcweVz/hi12KgeoUdMzJJ1uaGWGQdGqA9jRzZ7bKsRnRX5YRwwVxclvSajpLykPBz d42pp/x6+FmcLUPhu3GO52+GzmcxMOukPozA4lwYS0NE+Hjv1y7xNq0sE3MrC6y1ZqDZ hQlnVaK+FsQVbdgd2eC3CN4tSzQSgeXYYeyZz5VmcayG924IArpem6f/AllWGKXpkdcM KFKt/A43gzNoxVH7rP5sg7HSzJRG38T5vsWHdqcERrOdm8G7KFfqHXvyEqj/uUcSGfot niW/zVIAV7GZ22pgOflSR4/F1XUlBpAcUCAjg4FnGUosJrCZVF6chVfF1EnProiSbckj 8xaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Vizo/7h+DNIo+oYns++2GWC3GgmSkwVcEsnXQ7O1hko=; b=I28m/kqpZVbnSc93zmhIVeNXoHh6Tv57kX0fg5w2B0zkX+B7MXE7y8XteNwQSpDLzm gOBeKNjFtzfBorzIaJFNgLcg7pkrBEUNfUHc/tV0rI4++XU36UmrzuqMvGOfY+4ZQwSS JBSWqMjwrdIX13YJkZGHzv5BgkMpxdw7NhBHfnRD27dko3M9pA1VI7Tgwn91l7rWVmmG w2DuGxwitiXIugaJWRMh8mo1EajNafscryY2wL1sI9fSsJ+BTC4C0IJ7YkHmYcRXO7lT tDXVJVzlxqIZawcwwANMk1mpnaIot8YQwbh1TOA/bBBIWRjdDclFW9sfNvifk+3n72Hc nAbA== X-Gm-Message-State: AGi0PuauArFjN4T1PpX6/eTBLnJcoAra/1rnBYZN3HLP5zphGg5unGXG FAuSyBcIRjnYxxE/1wcOHm4= X-Google-Smtp-Source: APiQypKFeOJ4ZL+D/u5VXorbPFk3uUYLofnjibuNB/+wjv1+WkcEiugPTCbOsFAYV2QCg3fYG8Vgyw== X-Received: by 2002:a1c:3985:: with SMTP id g127mr3339770wma.102.1587451814049; Mon, 20 Apr 2020 23:50:14 -0700 (PDT) Received: from localhost.localdomain ([141.226.12.123]) by smtp.gmail.com with ESMTPSA id h16sm2698093wrw.36.2020.04.20.23.50.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2020 23:50:13 -0700 (PDT) From: Amir Goldstein To: Cyril Hrubis Date: Tue, 21 Apr 2020 09:50:01 +0300 Message-Id: <20200421065002.12417-4-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200421065002.12417-1-amir73il@gmail.com> References: <20200421065002.12417-1-amir73il@gmail.com> X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH 3/4] syscalls/fanotify15: Add a test case for inode marks X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Matthew Bobrowski , Jan Kara , ltp@lists.linux.it MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Test reporting events with fid also with recusrive inode marks: - Test events "on self" (FAN_DELETE_SELF) on file and dir - Test events "on child" (FAN_MODIFY) on file With recursive inode marks, verify that the FAN_MODIFY event reported to parent "on child" is merged with the FAN_MODIFY event reported to child. The new test case is a regression test for commit f367a62a7cad: fanotify: merge duplicate events on parent and child Signed-off-by: Amir Goldstein Reviewed-by: Petr Vorel Reviewed-by: Cyril Hrubis Reviewed-by: Matthew Bobrowski --- .../kernel/syscalls/fanotify/fanotify15.c | 76 +++++++++++++++++-- 1 file changed, 69 insertions(+), 7 deletions(-) diff --git a/testcases/kernel/syscalls/fanotify/fanotify15.c b/testcases/kernel/syscalls/fanotify/fanotify15.c index 454441bfe..bb1069139 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify15.c +++ b/testcases/kernel/syscalls/fanotify/fanotify15.c @@ -9,6 +9,10 @@ * Test file that has been purposely designed to verify * FAN_REPORT_FID functionality while using newly defined dirent * events. + * + * Test case #1 is a regression test for commit f367a62a7cad: + * + * fanotify: merge duplicate events on parent and child */ #define _GNU_SOURCE #include "config.h" @@ -53,29 +57,51 @@ static int fanotify_fd; static char events_buf[EVENT_BUF_LEN]; static struct event_t event_set[EVENT_MAX]; -static void do_test(void) +static struct test_case_t { + struct fanotify_mark_type mark; + unsigned long mask; +} test_cases[] = { + { + /* Watch filesystem including events "on self" */ + INIT_FANOTIFY_MARK_TYPE(FILESYSTEM), + FAN_DELETE_SELF, + }, + { + /* Watch directory including events "on children" */ + INIT_FANOTIFY_MARK_TYPE(INODE), + FAN_EVENT_ON_CHILD, + }, +}; + +static void do_test(unsigned int number) { int i, fd, len, count = 0; struct file_handle *event_file_handle; struct fanotify_event_metadata *metadata; struct fanotify_event_info_fid *event_fid; + struct test_case_t *tc = &test_cases[number]; + struct fanotify_mark_type *mark = &tc->mark; + tst_res(TINFO, + "Test #%d: FAN_REPORT_FID with mark type: %s", + number, mark->name); - if (fanotify_mark(fanotify_fd, FAN_MARK_ADD | FAN_MARK_FILESYSTEM, + + if (fanotify_mark(fanotify_fd, FAN_MARK_ADD | mark->flag, tc->mask | FAN_CREATE | FAN_DELETE | FAN_MOVE | - FAN_MODIFY | FAN_DELETE_SELF | FAN_ONDIR, + FAN_MODIFY | FAN_ONDIR, AT_FDCWD, TEST_DIR) == -1) { if (errno == ENODEV) tst_brk(TCONF, "FAN_REPORT_FID not supported on %s " "filesystem", tst_device->fs_type); tst_brk(TBROK | TERRNO, - "fanotify_mark(%d, FAN_MARK_ADD | FAN_MARK_FILESYSTEM, " + "fanotify_mark(%d, FAN_MARK_ADD | %s, " "FAN_CREATE | FAN_DELETE | FAN_MOVE | " - "FAN_MODIFY | FAN_DELETE_SELF | FAN_ONDIR, " + "FAN_MODIFY | FAN_ONDIR, " "AT_FDCWD, %s) failed", - fanotify_fd, TEST_DIR); + fanotify_fd, mark->name, TEST_DIR); } /* All dirent events on testdir are merged */ @@ -89,8 +115,21 @@ static void do_test(void) fd = SAFE_CREAT(FILE1, 0644); SAFE_CLOSE(fd); + /* Recursive watch file for events "on self" */ + if (mark->flag == FAN_MARK_INODE && + fanotify_mark(fanotify_fd, FAN_MARK_ADD | mark->flag, + FAN_MODIFY | FAN_DELETE_SELF, + AT_FDCWD, FILE1) == -1) { + tst_brk(TBROK | TERRNO, + "fanotify_mark(%d, FAN_MARK_ADD | %s, " + "FAN_DELETE_SELF, AT_FDCWD, %s) failed", + fanotify_fd, mark->name, FILE1); + } + /* * Event on child file is not merged with dirent events. + * FAN_MODIFY event reported on file mark should be merged with the + * FAN_MODIFY event reported on parent directory watch. */ event_set[count].mask = FAN_MODIFY; event_set[count].handle.handle_bytes = MAX_HANDLE_SZ; @@ -131,6 +170,17 @@ static void do_test(void) SAFE_MKDIR(DIR1, 0755); + /* Recursive watch subdir for events "on self" */ + if (mark->flag == FAN_MARK_INODE && + fanotify_mark(fanotify_fd, FAN_MARK_ADD | mark->flag, + FAN_DELETE_SELF | FAN_ONDIR, + AT_FDCWD, DIR1) == -1) { + tst_brk(TBROK | TERRNO, + "fanotify_mark(%d, FAN_MARK_ADD | %s," + "FAN_DELETE_SELF | FAN_ONDIR, AT_FDCWD, %s) failed", + fanotify_fd, mark->name, DIR1); + } + SAFE_RENAME(DIR1, DIR2); event_set[count].mask = FAN_ONDIR | FAN_DELETE_SELF; @@ -144,6 +194,17 @@ static void do_test(void) /* Read dir events from the event queue */ len += SAFE_READ(0, fanotify_fd, events_buf + len, EVENT_BUF_LEN - len); + /* + * Cleanup the mark + */ + if (fanotify_mark(fanotify_fd, FAN_MARK_FLUSH | mark->flag, 0, + AT_FDCWD, TEST_DIR) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_FLUSH | %s, 0," + "AT_FDCWD, '"TEST_DIR"') failed", + fanotify_fd, mark->name); + } + /* Process each event in buffer */ for (i = 0, metadata = (struct fanotify_event_metadata *) events_buf; FAN_EVENT_OK(metadata, len); i++) { @@ -262,7 +323,8 @@ static struct tst_test test = { .mount_device = 1, .mntpoint = MOUNT_POINT, .all_filesystems = 1, - .test_all = do_test, + .test = do_test, + .tcnt = ARRAY_SIZE(test_cases), .setup = do_setup, .cleanup = do_cleanup }; From patchwork Tue Apr 21 06:50:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 1273995 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=ItcFsJfz; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 495vNL0jr5z9sSM for ; Tue, 21 Apr 2020 16:50:58 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 7147A3C62A2 for ; Tue, 21 Apr 2020 08:50:55 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [IPv6:2001:4b78:1:20::5]) by picard.linux.it (Postfix) with ESMTP id 552F73C6292 for ; Tue, 21 Apr 2020 08:50:16 +0200 (CEST) Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-5.smtp.seeweb.it (Postfix) with ESMTPS id 67B47600C3A for ; Tue, 21 Apr 2020 08:50:15 +0200 (CEST) Received: by mail-wm1-x343.google.com with SMTP id v4so1721426wme.1 for ; Mon, 20 Apr 2020 23:50:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KQgAWnE899XxWpHV5FFsjI8ENQ7nGZG/ntRi1z55eZM=; b=ItcFsJfzOjg0DAUxVCmoldIbBzox0Hy5fOikzqQ8D8Z5beYGUE4MWaU+yDkHcgnOHG ZNx0DPJzOidrFptI8U9CJavPUoA/z9ecQy7ss9RJSUlnirrTwMuhLn8SVM9XU+n1ui9M DFW13l6ZoGFlbhW9O74aXn7eg90cHXsMBxCGQR8+tjS5sweBtAFUEeKzrhY3K8jrgUge Px2LsRxLq2BFpgW+cTHeDIomeK8psE4bZZt81rG9PtzIc3q/UTudzzHQeTSXoiuxEePe C1V6M7UwEuioz5j50vSencmHBOGt727VnlQyzlHqaFtqNzu+1h5+VhOFNhCAJdgaFU6e 6oAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KQgAWnE899XxWpHV5FFsjI8ENQ7nGZG/ntRi1z55eZM=; b=sFhmlBMGOUzVCPw9wxiPKwF1Y3GZtHyUXjofnJJ66uIzG9AGIDMh5QfuwtSZw5Msve qtql0hx/3cTaBLSx4N7PA7WhgOu6vKW9ZoJb/jJpfr/R2RfqzH+KJ9Qxv/xlVSO706GU nKzbd/7i9wxS2wddvYU++clPlQdeoru8wmV7wmxEXTt6Cmmaa4h0vJx1UqaG59rrluIL x6nEMhJp187eu4GvgbmC4o6D5oi7Aiufpj3YiATdi8CqEfdqtXI2clSd97ajFYmf9AsJ Mo7LWtNB+VBHQaI2VKfvpKWqFFQJJK9KjdxxcmMCfcawPWXh/M4P6UiyMSLKwKd9G4q0 +ncA== X-Gm-Message-State: AGi0PuZA5Yg8YGweXlFJoNqwrqcRz3cn7w691E23bXmt4earj3TuwAia Ml9npBnCh4kANrYdqJqByXUcrgvj X-Google-Smtp-Source: APiQypIjrlpJbLWmkiLE2yXaQn/Sg182M2nJI/pF4/TEQBAkrKPKIDxUlZ2enTrOhq18fRKhzqP+RA== X-Received: by 2002:a1c:ba09:: with SMTP id k9mr3188241wmf.176.1587451815375; Mon, 20 Apr 2020 23:50:15 -0700 (PDT) Received: from localhost.localdomain ([141.226.12.123]) by smtp.gmail.com with ESMTPSA id h16sm2698093wrw.36.2020.04.20.23.50.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2020 23:50:14 -0700 (PDT) From: Amir Goldstein To: Cyril Hrubis Date: Tue, 21 Apr 2020 09:50:02 +0300 Message-Id: <20200421065002.12417-5-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200421065002.12417-1-amir73il@gmail.com> References: <20200421065002.12417-1-amir73il@gmail.com> X-Virus-Scanned: clamav-milter 0.99.2 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-5.smtp.seeweb.it Subject: [LTP] [PATCH 4/4] syscalls/fanotify: New test for FAN_MODIFY_DIR X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Matthew Bobrowski , Jan Kara , ltp@lists.linux.it MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" - Watch dir modify events with name info - Watch self delete events w/o name info - Test inode and filesystem marks - Check getting events for all file and dir names Signed-off-by: Amir Goldstein Reviewed-by: Petr Vorel Reviewed-by: Matthew Bobrowski --- runtest/syscalls | 1 + testcases/kernel/syscalls/fanotify/.gitignore | 1 + testcases/kernel/syscalls/fanotify/fanotify.h | 15 +- .../kernel/syscalls/fanotify/fanotify16.c | 441 ++++++++++++++++++ 4 files changed, 455 insertions(+), 3 deletions(-) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify16.c diff --git a/runtest/syscalls b/runtest/syscalls index 9bb72beb2..4c25c9cb9 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -571,6 +571,7 @@ fanotify12 fanotify12 fanotify13 fanotify13 fanotify14 fanotify14 fanotify15 fanotify15 +fanotify16 fanotify16 ioperm01 ioperm01 ioperm02 ioperm02 diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore index 68e4cc7aa..e7cf224fd 100644 --- a/testcases/kernel/syscalls/fanotify/.gitignore +++ b/testcases/kernel/syscalls/fanotify/.gitignore @@ -13,4 +13,5 @@ /fanotify13 /fanotify14 /fanotify15 +/fanotify16 /fanotify_child diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h index 6da7e765c..a05f4a372 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify.h +++ b/testcases/kernel/syscalls/fanotify/fanotify.h @@ -41,6 +41,9 @@ static long fanotify_mark(int fd, unsigned int flags, uint64_t mask, #ifndef FAN_REPORT_TID #define FAN_REPORT_TID 0x00000100 #endif +#ifndef FAN_REPORT_FID +#define FAN_REPORT_FID 0x00000200 +#endif #ifndef FAN_MARK_INODE #define FAN_MARK_INODE 0 @@ -79,9 +82,8 @@ static long fanotify_mark(int fd, unsigned int flags, uint64_t mask, #ifndef FAN_OPEN_EXEC_PERM #define FAN_OPEN_EXEC_PERM 0x00040000 #endif - -#ifndef FAN_REPORT_FID -#define FAN_REPORT_FID 0x00000200 +#ifndef FAN_DIR_MODIFY +#define FAN_DIR_MODIFY 0x00080000 #endif /* @@ -106,6 +108,13 @@ typedef struct { #define __kernel_fsid_t lapi_fsid_t #endif /* __kernel_fsid_t */ +#ifndef FAN_EVENT_INFO_TYPE_FID +#define FAN_EVENT_INFO_TYPE_FID 1 +#endif +#ifndef FAN_EVENT_INFO_TYPE_DFID_NAME +#define FAN_EVENT_INFO_TYPE_DFID_NAME 2 +#endif + #ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_HEADER struct fanotify_event_info_header { uint8_t info_type; diff --git a/testcases/kernel/syscalls/fanotify/fanotify16.c b/testcases/kernel/syscalls/fanotify/fanotify16.c new file mode 100644 index 000000000..0ec151841 --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify16.c @@ -0,0 +1,441 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2020 CTERA Networks. All Rights Reserved. + * + * Started by Amir Goldstein + * + * DESCRIPTION + * Check FAN_DIR_MODIFY events with name info + */ +#define _GNU_SOURCE +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include "tst_test.h" +#include "fanotify.h" + +#if defined(HAVE_SYS_FANOTIFY_H) +#include +#include + +#define EVENT_MAX 10 + +/* Size of the event structure, not including file handle */ +#define EVENT_SIZE (sizeof(struct fanotify_event_metadata) + \ + sizeof(struct fanotify_event_info_fid)) +/* Tripple events buffer size to account for file handles and names */ +#define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE * 3) + + +#define BUF_SIZE 256 + +static char fname1[BUF_SIZE], fname2[BUF_SIZE]; +static char dname1[BUF_SIZE], dname2[BUF_SIZE]; +static int fd_notify; + +struct fid_t { + __kernel_fsid_t fsid; + struct file_handle handle; + char buf[MAX_HANDLE_SZ]; +}; + +struct event_t { + unsigned long long mask; + struct fid_t *fid; + char name[BUF_SIZE]; +}; + +static struct event_t event_set[EVENT_MAX]; + +static char event_buf[EVENT_BUF_LEN]; + +#define DIR_NAME1 "test_dir1" +#define DIR_NAME2 "test_dir2" +#define FILE_NAME1 "test_file1" +#define FILE_NAME2 "test_file2" +#define MOUNT_PATH "fs_mnt" + +static struct test_case_t { + struct fanotify_mark_type mark; + unsigned long mask; + struct fanotify_mark_type sub_mark; + unsigned long sub_mask; +} test_cases[] = { + { + /* Filesystem watch for dir modify and delete self events */ + INIT_FANOTIFY_MARK_TYPE(FILESYSTEM), + FAN_DIR_MODIFY | FAN_DELETE_SELF | FAN_ONDIR, + {}, + 0, + }, + { + /* Recursive watches for dir modify events */ + INIT_FANOTIFY_MARK_TYPE(INODE), + FAN_DIR_MODIFY, + /* Watches for delete self event on subdir */ + INIT_FANOTIFY_MARK_TYPE(INODE), + FAN_DIR_MODIFY | FAN_DELETE_SELF | FAN_ONDIR, + }, +}; + +void save_fid(const char *path, struct fid_t *fid) +{ + int *fh = (int *)(fid->handle.f_handle); + int *fsid = fid->fsid.val; + + fh[0] = fh[1] = fh[2] = 0; + fid->handle.handle_bytes = MAX_HANDLE_SZ; + fanotify_get_fid(path, &fid->fsid, &fid->handle); + + tst_res(TINFO, + "fid(%s) = %x.%x.%x.%x.%x...", + path, fsid[0], fsid[1], fh[0], fh[1], fh[2]); +} + +static void do_test(unsigned int number) +{ + int len = 0, i = 0, test_num = 0; + int tst_count = 0; + int fd; + struct test_case_t *tc = &test_cases[number]; + struct fanotify_mark_type *mark = &tc->mark; + struct fanotify_mark_type *sub_mark = &tc->sub_mark; + struct fid_t root_fid, dir_fid, file_fid; + + tst_res(TINFO, + "Test #%d: FAN_REPORT_FID with mark type: %s", + number, mark->name); + + + fd_notify = fanotify_init(FAN_REPORT_FID, 0); + if (fd_notify == -1) { + if (errno == EINVAL) { + tst_brk(TCONF, + "FAN_REPORT_FID not supported by kernel"); + return; + } + tst_brk(TBROK | TERRNO, + "fanotify_init(FAN_REPORT_FID, 0) failed"); + } + + /* + * Watch dir modify events with name in filesystem/dir + */ + if (fanotify_mark(fd_notify, FAN_MARK_ADD | mark->flag, tc->mask, + AT_FDCWD, MOUNT_PATH) < 0) { + if (errno == EINVAL) { + tst_brk(TCONF, + "FAN_DIR_MODIFY not supported by kernel"); + return; + } + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD | %s, " + "FAN_DIR_MODIFY, AT_FDCWD, '"MOUNT_PATH"') " + "failed", fd_notify, mark->name); + } + + /* Save the mount root fid */ + save_fid(MOUNT_PATH, &root_fid); + + /* + * Create subdir and watch open events "on children" with name. + */ + if (mkdir(dname1, 0755) < 0) { + tst_brk(TBROK | TERRNO, + "mkdir('"DIR_NAME1"', 0755) failed"); + } + + /* Save the subdir fid */ + save_fid(dname1, &dir_fid); + + if (tc->sub_mask && + fanotify_mark(fd_notify, FAN_MARK_ADD | sub_mark->flag, tc->sub_mask, + AT_FDCWD, dname1) < 0) { + tst_brk(TBROK | TERRNO, + "fanotify_mark (%d, FAN_MARK_ADD | %s, " + "FAN_DIR_MODIFY | FAN_DELETE_SELF | FAN_ONDIR, " + "AT_FDCWD, '%s') " + "failed", fd_notify, sub_mark->name, dname1); + } + + event_set[tst_count].mask = FAN_DIR_MODIFY; + event_set[tst_count].fid = &root_fid; + strcpy(event_set[tst_count].name, DIR_NAME1); + tst_count++; + + /* Generate modify events "on child" */ + if ((fd = creat(fname1, 0755)) == -1) { + tst_brk(TBROK | TERRNO, + "creat(\"%s\", 755) failed", FILE_NAME1); + } + + /* Save the file fid */ + save_fid(fname1, &file_fid); + + SAFE_WRITE(1, fd, "1", 1); + + if (rename(fname1, fname2) == -1) { + tst_brk(TBROK | TERRNO, + "rename(%s, %s) failed", + FILE_NAME1, FILE_NAME2); + } + + if (close(fd) == -1) { + tst_brk(TBROK | TERRNO, + "close(%s) failed", FILE_NAME2); + } + + /* Generate delete events with fname2 */ + if (unlink(fname2) == -1) { + tst_brk(TBROK | TERRNO, + "unlink(%s) failed", FILE_NAME2); + } + + /* Read events on files in subdir */ + len += SAFE_READ(0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); + + /* + * FAN_DIR_MODIFY events with the same name are merged. + */ + event_set[tst_count].mask = FAN_DIR_MODIFY; + event_set[tst_count].fid = &dir_fid; + strcpy(event_set[tst_count].name, FILE_NAME1); + tst_count++; + event_set[tst_count].mask = FAN_DIR_MODIFY; + event_set[tst_count].fid = &dir_fid; + strcpy(event_set[tst_count].name, FILE_NAME2); + tst_count++; + /* + * Directory watch does not get self events on children. + * Filesystem watch gets self event w/o name info. + */ + if (mark->flag == FAN_MARK_FILESYSTEM) { + event_set[tst_count].mask = FAN_DELETE_SELF; + event_set[tst_count].fid = &file_fid; + strcpy(event_set[tst_count].name, ""); + tst_count++; + } + + if (rename(dname1, dname2) == -1) { + tst_brk(TBROK | TERRNO, + "rename(%s, %s) failed", + DIR_NAME1, DIR_NAME2); + } + + if (rmdir(dname2) == -1) { + tst_brk(TBROK | TERRNO, + "rmdir(%s) failed", DIR_NAME2); + } + + /* Read more events on dirs */ + len += SAFE_READ(0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); + + event_set[tst_count].mask = FAN_DIR_MODIFY; + event_set[tst_count].fid = &root_fid; + strcpy(event_set[tst_count].name, DIR_NAME1); + tst_count++; + event_set[tst_count].mask = FAN_DIR_MODIFY; + event_set[tst_count].fid = &root_fid; + strcpy(event_set[tst_count].name, DIR_NAME2); + tst_count++; + /* + * Directory watch gets self event on itself w/o name info. + */ + event_set[tst_count].mask = FAN_DELETE_SELF | FAN_ONDIR; + strcpy(event_set[tst_count].name, ""); + event_set[tst_count].fid = &dir_fid; + tst_count++; + + /* + * Cleanup the marks + */ + SAFE_CLOSE(fd_notify); + fd_notify = -1; + + while (i < len) { + struct event_t *expected = &event_set[test_num]; + struct fanotify_event_metadata *event; + struct fanotify_event_info_fid *event_fid; + struct file_handle *file_handle; + unsigned int fhlen; + const char *filename; + int namelen, info_type; + + event = (struct fanotify_event_metadata *)&event_buf[i]; + event_fid = (struct fanotify_event_info_fid *)(event + 1); + file_handle = (struct file_handle *)event_fid->handle; + fhlen = file_handle->handle_bytes; + filename = (char *)file_handle->f_handle + fhlen; + namelen = ((char *)event + event->event_len) - filename; + /* End of event could have name, zero padding, both or none */ + if (namelen > 0) { + namelen = strlen(filename); + } else { + filename = ""; + namelen = 0; + } + if (expected->name[0]) { + info_type = FAN_EVENT_INFO_TYPE_DFID_NAME; + } else { + info_type = FAN_EVENT_INFO_TYPE_FID; + } + if (test_num >= tst_count) { + tst_res(TFAIL, + "got unnecessary event: mask=%llx " + "pid=%u fd=%d name='%s' " + "len=%d info_type=%d info_len=%d fh_len=%d", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, filename, + event->event_len, event_fid->hdr.info_type, + event_fid->hdr.len, fhlen); + } else if (!fhlen || namelen < 0) { + tst_res(TFAIL, + "got event without fid: mask=%llx pid=%u fd=%d, " + "len=%d info_type=%d info_len=%d fh_len=%d", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, + event->event_len, event_fid->hdr.info_type, + event_fid->hdr.len, fhlen); + } else if (event->mask != expected->mask) { + tst_res(TFAIL, + "got event: mask=%llx (expected %llx) " + "pid=%u fd=%d name='%s' " + "len=%d info_type=%d info_len=%d fh_len=%d", + (unsigned long long)event->mask, expected->mask, + (unsigned)event->pid, event->fd, filename, + event->event_len, event_fid->hdr.info_type, + event_fid->hdr.len, fhlen); + } else if (info_type != event_fid->hdr.info_type) { + tst_res(TFAIL, + "got event: mask=%llx pid=%u fd=%d, " + "len=%d info_type=%d expected(%d) info_len=%d fh_len=%d", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, + event->event_len, event_fid->hdr.info_type, + info_type, event_fid->hdr.len, fhlen); + } else if (fhlen != expected->fid->handle.handle_bytes) { + tst_res(TFAIL, + "got event: mask=%llx pid=%u fd=%d name='%s' " + "len=%d info_type=%d info_len=%d fh_len=%d expected(%d)" + "fh_type=%d", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, filename, + event->event_len, info_type, + event_fid->hdr.len, fhlen, + expected->fid->handle.handle_bytes, + file_handle->handle_type); + } else if (file_handle->handle_type != + expected->fid->handle.handle_type) { + tst_res(TFAIL, + "got event: mask=%llx pid=%u fd=%d name='%s' " + "len=%d info_type=%d info_len=%d fh_len=%d " + "fh_type=%d expected(%x)", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, filename, + event->event_len, info_type, + event_fid->hdr.len, fhlen, + file_handle->handle_type, + expected->fid->handle.handle_type); + } else if (memcmp(file_handle->f_handle, + expected->fid->handle.f_handle, fhlen)) { + tst_res(TFAIL, + "got event: mask=%llx pid=%u fd=%d name='%s' " + "len=%d info_type=%d info_len=%d fh_len=%d " + "fh_type=%d unexpected file handle (%x...)", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, filename, + event->event_len, info_type, + event_fid->hdr.len, fhlen, + file_handle->handle_type, + *(int *)(file_handle->f_handle)); + } else if (memcmp(&event_fid->fsid, &expected->fid->fsid, + sizeof(event_fid->fsid)) != 0) { + tst_res(TFAIL, + "got event: mask=%llx pid=%u fd=%d name='%s' " + "len=%d info_type=%d info_len=%d fh_len=%d " + "fsid=%x.%x (expected %x.%x)", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, filename, + event->event_len, info_type, + event_fid->hdr.len, fhlen, + event_fid->fsid.val[0], event_fid->fsid.val[1], + expected->fid->fsid.val[0], + expected->fid->fsid.val[1]); + } else if (strcmp(expected->name, filename)) { + tst_res(TFAIL, + "got event: mask=%llx " + "pid=%u fd=%d name='%s' expected('%s') " + "len=%d info_type=%d info_len=%d fh_len=%d", + (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, + filename, expected->name, + event->event_len, event_fid->hdr.info_type, + event_fid->hdr.len, fhlen); + } else if (event->pid != getpid()) { + tst_res(TFAIL, + "got event: mask=%llx pid=%u " + "(expected %u) fd=%d name='%s' " + "len=%d info_type=%d info_len=%d fh_len=%d", + (unsigned long long)event->mask, + (unsigned)event->pid, + (unsigned)getpid(), + event->fd, filename, + event->event_len, event_fid->hdr.info_type, + event_fid->hdr.len, fhlen); + } else { + tst_res(TPASS, + "got event #%d: mask=%llx pid=%u fd=%d name='%s' " + "len=%d info_type=%d info_len=%d fh_len=%d", + test_num, (unsigned long long)event->mask, + (unsigned)event->pid, event->fd, filename, + event->event_len, event_fid->hdr.info_type, + event_fid->hdr.len, fhlen); + } + i += event->event_len; + if (event->fd > 0) + SAFE_CLOSE(event->fd); + test_num++; + } + for (; test_num < tst_count; test_num++) { + tst_res(TFAIL, "didn't get event: mask=%llx, name='%s'", + event_set[test_num].mask, event_set[test_num].name); + + } +} + +static void setup(void) +{ + sprintf(dname1, "%s/%s", MOUNT_PATH, DIR_NAME1); + sprintf(dname2, "%s/%s", MOUNT_PATH, DIR_NAME2); + sprintf(fname1, "%s/%s", dname1, FILE_NAME1); + sprintf(fname2, "%s/%s", dname1, FILE_NAME2); +} + +static void cleanup(void) +{ + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); +} + +static struct tst_test test = { + .test = do_test, + .tcnt = ARRAY_SIZE(test_cases), + .setup = setup, + .cleanup = cleanup, + .mount_device = 1, + .mntpoint = MOUNT_PATH, + .all_filesystems = 1, + .needs_tmpdir = 1, + .needs_root = 1 +}; + +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); +#endif