From patchwork Mon Nov 5 10:07:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 992980 X-Patchwork-Delegate: petr.vorel@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) 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.b="VCjMIDGi"; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42pSzH2ks1z9sCV for ; Mon, 5 Nov 2018 21:07:39 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id C22B53E783B for ; Mon, 5 Nov 2018 11:07:35 +0100 (CET) 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 [IPv6:2001:4b78:1:20::2]) by picard.linux.it (Postfix) with ESMTP id 88B493E780E for ; Mon, 5 Nov 2018 11:07:33 +0100 (CET) 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 DAE296008E7 for ; Mon, 5 Nov 2018 11:07:32 +0100 (CET) Received: by mail-wr1-x442.google.com with SMTP id y3-v6so8476331wrh.10 for ; Mon, 05 Nov 2018 02:07:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=i+yZXwAyT1CpOKkKu9lXCTL8XykfJmWQtx3Q6Hf46Lk=; b=VCjMIDGi2XmYbu3JAZiNF1jxGeivv6RbWAZS5/1O7Ea3MQnDIDEwdckV70a4ZGofQ6 w9BXiQ5Xi17/+bFvYXckOgIfp/Ze4O7YZ15hGxoXIKYeYssZt4Cagube8h+/VfK5aN/s apMdWlqkLLfMFv3KRyYSQaklL0WT4ZpNAEfcni3xBkq4QgTnLYyb3mB9ED9wm5sFDHHh pVv+zfzUBD8GfnfaWtRCxueXGQv3Do1EowKa188KbOzSJj5PqvMifOVGXWxhAuYLAIbM kxZTfGKJB93tyzrEeCEE6I3AEHCwuFs556SeDns0Xs+Alf0If+lDlhy1AkWWtKDV39sc HcZg== 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; bh=i+yZXwAyT1CpOKkKu9lXCTL8XykfJmWQtx3Q6Hf46Lk=; b=kagFe0qraPGlojiVaXKR6eHlxcVPAEw1oLlVagjnnS++r4TzdHbyDBkx7GiGMDbqlU Dma38GVbdXvp1jNKypk/mSYGYV7xh/MBmpW//dBK3XIJVMhfDUYVxKNINQRp0+Aij9qf a2gGpOlanumEZPNblyhHL056hYfCSozXJyZqV5Md5mUreQTy11h2e1F1xwckq0Wesn7N MIIK8ryLWs5Nj0PpvG9kW4bSEMRB1sInaHY3dFtGn22Ch3NxsrV7nUOy//tlzmjNGFmz 4xwkD0qmMltEqqjqfOJ4ktyAMr/k11tLHOc7Cv8eov3DSiAdiup5H6Z3v1tMKDyK2CmW rDmQ== X-Gm-Message-State: AGRZ1gJdFhH2oY5yAUB5fEP0uqFMIVs0HvRdCj+6tlNwfOgADjmShkvW fwbhiehXWf0e1GnYD/o6VYw= X-Google-Smtp-Source: AJdET5cLAknpPAjldLyJcfHmtx50NPZps7TKN9EZ0clfKTOVO4rvC7Wj5MUVE/EZSbH5bQG+CtJ0DQ== X-Received: by 2002:adf:f4c5:: with SMTP id h5-v6mr18678289wrp.21.1541412452257; Mon, 05 Nov 2018 02:07:32 -0800 (PST) Received: from localhost.localdomain (bzq-166-168-31-246.red.bezeqint.net. [31.168.166.246]) by smtp.gmail.com with ESMTPSA id 140-v6sm31417808wmx.34.2018.11.05.02.07.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Nov 2018 02:07:31 -0800 (PST) From: Amir Goldstein To: Jan Kara Date: Mon, 5 Nov 2018 12:07:22 +0200 Message-Id: <20181105100722.1812-1-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 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_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Cc: ltp@lists.linux.it Subject: [LTP] [PATCH] fanotify09: check merging of events on child subdir X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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 on children mount mark should not be getting double events "on child" and "on self" for the same action. Signed-off-by: Amir Goldstein --- Jan, FYI, this patch improves fanotify09 to cover the bug fixed by kernel patch "fanotify: fix handling of events on child sub-directory". Note that this patch is based on a cleanup patch available at [1]. Thanks, Amir. [1] https://github.com/amir73il/ltp/commits/fanotify_cleanup .../kernel/syscalls/fanotify/fanotify09.c | 104 ++++++++++++------ 1 file changed, 68 insertions(+), 36 deletions(-) diff --git a/testcases/kernel/syscalls/fanotify/fanotify09.c b/testcases/kernel/syscalls/fanotify/fanotify09.c index 511ccc0e0..ef948c8a9 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify09.c +++ b/testcases/kernel/syscalls/fanotify/fanotify09.c @@ -39,16 +39,20 @@ #define BUF_SIZE 256 static char fname[BUF_SIZE]; +static char symlnk[BUF_SIZE]; +static char fdpath[BUF_SIZE]; static int fd_notify[NUM_GROUPS]; static char event_buf[EVENT_BUF_LEN]; #define MOUNT_NAME "mntpoint" +#define DIR_NAME "testdir" static int mount_created; static void create_fanotify_groups(void) { unsigned int i, onchild; + const char *onchildstr; int ret; for (i = 0; i < NUM_GROUPS; i++) { @@ -56,34 +60,39 @@ static void create_fanotify_groups(void) FAN_NONBLOCK, O_RDONLY); - /* Add mount mark for each group without MODIFY event */ - ret = fanotify_mark(fd_notify[i], - FAN_MARK_ADD | FAN_MARK_MOUNT, - FAN_CLOSE_NOWRITE, + onchild = i == 0 ? FAN_EVENT_ON_CHILD | FAN_ONDIR : 0; + onchildstr = i == 0 ? " | FAN_EVENT_ON_CHILD | FAN_ONDIR" : ""; + + /* + * Add mount mark for each group without MODIFY event. + * The first group only gets events on directories - the request + * for events on child for mount mark is ignored. + */ + ret = fanotify_mark(fd_notify[i], FAN_MARK_ADD | FAN_MARK_MOUNT, + FAN_CLOSE_NOWRITE | onchild, AT_FDCWD, "."); if (ret < 0) { tst_brk(TBROK | TERRNO, "fanotify_mark(%d, FAN_MARK_ADD | " - "FAN_MARK_MOUNT, FAN_MODIFY, AT_FDCWD," - " '.') failed", fd_notify[i]); + "FAN_MARK_MOUNT, FAN_CLOSE_NOWRITE%s" + ", AT_FDCWD, '.') failed", + fd_notify[i], onchildstr); } /* - * Add inode mark on parent for each group with MODIFY - * event, but only one group requests events on child. + * Add inode mark on parent for each group with MODIFY event, + * but only one group requests events on child (and subdirs). * The one mark with FAN_EVENT_ON_CHILD is needed for * setting the DCACHE_FSNOTIFY_PARENT_WATCHED dentry * flag. */ - onchild = (i == 0) ? FAN_EVENT_ON_CHILD : 0; - ret = fanotify_mark(fd_notify[i], - FAN_MARK_ADD, - FAN_MODIFY | onchild, AT_FDCWD, "."); + ret = fanotify_mark(fd_notify[i], FAN_MARK_ADD, + FAN_MODIFY | onchild, + AT_FDCWD, "."); if (ret < 0) { tst_brk(TBROK | TERRNO, "fanotify_mark(%d, FAN_MARK_ADD, " "FAN_MODIFY%s, AT_FDCWD, '.') failed", - fd_notify[i], - onchild ? " | FAN_EVENT_ON_CHILD" : ""); + fd_notify[i], onchildstr); } } } @@ -98,12 +107,13 @@ static void cleanup_fanotify_groups(void) } } -static void verify_event(int group, struct fanotify_event_metadata *event) +static void verify_event(int group, struct fanotify_event_metadata *event, + __u32 expect) { - if (event->mask != FAN_MODIFY) { + if (event->mask != expect) { tst_res(TFAIL, "group %d got event: mask %llx (expected %llx) " "pid=%u fd=%d", group, (unsigned long long)event->mask, - (unsigned long long)FAN_MODIFY, + (unsigned long long)expect, (unsigned)event->pid, event->fd); } else if (event->pid != getpid()) { tst_res(TFAIL, "group %d got event: mask %llx pid=%u " @@ -111,17 +121,23 @@ static void verify_event(int group, struct fanotify_event_metadata *event) (unsigned long long)event->mask, (unsigned)event->pid, (unsigned)getpid(), event->fd); } else { - tst_res(TPASS, "group %d got event: mask %llx pid=%u fd=%d", + 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); + (unsigned)event->pid, event->fd, fdpath); } } void test01(void) { - int ret; + int ret, dirfd; unsigned int i; - struct fanotify_event_metadata *event; + struct fanotify_event_metadata *event, *ev; create_fanotify_groups(); @@ -129,8 +145,17 @@ void test01(void) * generate MODIFY event and no FAN_CLOSE_NOWRITE event. */ SAFE_FILE_PRINTF(fname, "1"); + /* + * generate FAN_CLOSE_NOWRITE event on a child subdir. + */ + dirfd = SAFE_OPEN(DIR_NAME, O_RDONLY); + if (dirfd >= 0) + SAFE_CLOSE(dirfd); - /* First verify the first group got the MODIFY event */ + /* + * First verify the first group got the file MODIFY event and got just + * one FAN_CLOSE_NOWRITE event. + */ ret = read(fd_notify[0], event_buf, EVENT_BUF_LEN); if (ret < 0) { if (errno == EAGAIN) { @@ -140,26 +165,32 @@ void test01(void) "reading fanotify events failed"); } } - if (ret < (int)FAN_EVENT_METADATA_LEN) { + if (ret < 2 * (int)FAN_EVENT_METADATA_LEN) { tst_brk(TBROK, - "short read when reading fanotify " - "events (%d < %d)", ret, - (int)EVENT_BUF_LEN); + "short read when reading fanotify events (%d < %d)", + ret, 2 * (int)FAN_EVENT_METADATA_LEN); } event = (struct fanotify_event_metadata *)event_buf; - if (ret > (int)event->event_len) { - tst_res(TFAIL, "first group got more than one " - "event (%d > %d)", ret, - event->event_len); - } else { - verify_event(0, event); + verify_event(0, event, FAN_MODIFY); + verify_event(0, event + 1, FAN_CLOSE_NOWRITE); + if (ret > 2 * (int)FAN_EVENT_METADATA_LEN) { + tst_res(TFAIL, + "first group got more than two events (%d > %d)", + ret, 2 * (int)FAN_EVENT_METADATA_LEN); + } + /* Close all file descriptors of read events */ + for (ev = event; ret >= (int)FAN_EVENT_METADATA_LEN; ev++) { + if (ev->fd != FAN_NOFD) + SAFE_CLOSE(ev->fd); + ret -= (int)FAN_EVENT_METADATA_LEN; } - if (event->fd != FAN_NOFD) - SAFE_CLOSE(event->fd); - /* Then verify the rest of the groups did not get the MODIFY event */ + /* + * Then verify the rest of the groups did not get the MODIFY event and + * did not get the FAN_CLOSE_NOWRITE event on subdir. + */ for (i = 1; i < NUM_GROUPS; i++) { - ret = read(fd_notify[i], event_buf, EVENT_BUF_LEN); + ret = read(fd_notify[i], event_buf, FAN_EVENT_METADATA_LEN); if (ret > 0) { tst_res(TFAIL, "group %d got event", i); if (event->fd != FAN_NOFD) @@ -187,6 +218,7 @@ static void setup(void) SAFE_MOUNT(MOUNT_NAME, MOUNT_NAME, "none", MS_BIND, NULL); mount_created = 1; SAFE_CHDIR(MOUNT_NAME); + SAFE_MKDIR(DIR_NAME, 0755); sprintf(fname, "tfile_%d", getpid()); SAFE_FILE_PRINTF(fname, "1");