linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Amir Goldstein <amir73il@gmail.com>
To: Jan Kara <jack@suse.cz>
Cc: linux-fsdevel@vger.kernel.org
Subject: [PATCH v5 14/22] fsnotify: send event to parent and child with single callback
Date: Thu, 16 Jul 2020 11:42:22 +0300	[thread overview]
Message-ID: <20200716084230.30611-15-amir73il@gmail.com> (raw)
In-Reply-To: <20200716084230.30611-1-amir73il@gmail.com>

Instead of calling fsnotify() twice, once with parent inode and once
with child inode, if event should be sent to parent inode, send it
with both parent and child inodes marks in object type iterator and call
the backend handle_event() callback only once.

The parent inode is assigned to the standard "inode" iterator type and
the child inode is assigned to the special "child" iterator type.

In that case, the bit FS_EVENT_ON_CHILD will be set in the event mask,
the dir argment to handle_event will be the parent inode, the file_name
argument to handle_event is non NULL and refers to the name of the child
and the child inode can be accessed with fsnotify_data_inode().

This will allow fanotify to make decisions based on child or parent's
ignored mask.  For example, when a parent is interested in a specific
event on its children, but a specific child wishes to ignore this event,
the event will not be reported.  This is not what happens with current
code, but according to man page, it is the expected behavior.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
 fs/kernfs/file.c     | 10 +++++---
 fs/notify/fsnotify.c | 60 ++++++++++++++++++++++++++------------------
 2 files changed, 41 insertions(+), 29 deletions(-)

diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index e23b3f62483c..5b1468bc509e 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -883,6 +883,7 @@ static void kernfs_notify_workfn(struct work_struct *work)
 
 	list_for_each_entry(info, &kernfs_root(kn)->supers, node) {
 		struct kernfs_node *parent;
+		struct inode *p_inode = NULL;
 		struct inode *inode;
 		struct qstr name;
 
@@ -899,8 +900,6 @@ static void kernfs_notify_workfn(struct work_struct *work)
 		name = (struct qstr)QSTR_INIT(kn->name, strlen(kn->name));
 		parent = kernfs_get_parent(kn);
 		if (parent) {
-			struct inode *p_inode;
-
 			p_inode = ilookup(info->sb, kernfs_ino(parent));
 			if (p_inode) {
 				fsnotify(p_inode, FS_MODIFY | FS_EVENT_ON_CHILD,
@@ -911,8 +910,11 @@ static void kernfs_notify_workfn(struct work_struct *work)
 			kernfs_put(parent);
 		}
 
-		fsnotify(inode, FS_MODIFY, inode, FSNOTIFY_EVENT_INODE,
-			 NULL, 0);
+		if (!p_inode) {
+			fsnotify(inode, FS_MODIFY, inode, FSNOTIFY_EVENT_INODE,
+				 NULL, 0);
+		}
+
 		iput(inode);
 	}
 
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index 51ada3cfd2ff..7120c675e9a6 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -145,17 +145,21 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode)
 /*
  * Notify this dentry's parent about a child's events with child name info
  * if parent is watching.
- * Notify also the child without name info if child inode is watching.
+ * Notify only the child without name info if parent is not watching.
  */
 int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
 		      int data_type)
 {
+	struct inode *inode = d_inode(dentry);
 	struct dentry *parent;
 	struct inode *p_inode;
+	struct name_snapshot name;
+	struct qstr *file_name = NULL;
 	int ret = 0;
 
+	parent = NULL;
 	if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
-		goto notify_child;
+		goto notify;
 
 	parent = dget_parent(dentry);
 	p_inode = parent->d_inode;
@@ -163,25 +167,24 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
 	if (unlikely(!fsnotify_inode_watches_children(p_inode))) {
 		__fsnotify_update_child_dentry_flags(p_inode);
 	} else if (p_inode->i_fsnotify_mask & mask & ALL_FSNOTIFY_EVENTS) {
-		struct name_snapshot name;
+		/* When notifying parent, child should be passed as data */
+		WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type));
 
-		/*
-		 * We are notifying a parent, so set a flag in mask to inform
-		 * backend that event has information about a child entry.
-		 */
+		/* Notify both parent and child with child name info */
+		inode = p_inode;
 		take_dentry_name_snapshot(&name, dentry);
-		ret = fsnotify(p_inode, mask | FS_EVENT_ON_CHILD, data,
-			       data_type, &name.name, 0);
-		release_dentry_name_snapshot(&name);
+		file_name = &name.name;
+		mask |= FS_EVENT_ON_CHILD;
 	}
 
-	dput(parent);
+notify:
+	ret = fsnotify(inode, mask, data, data_type, file_name, 0);
 
-	if (ret)
-		return ret;
+	if (file_name)
+		release_dentry_name_snapshot(&name);
+	dput(parent);
 
-notify_child:
-	return fsnotify(d_inode(dentry), mask, data, data_type, NULL, 0);
+	return ret;
 }
 EXPORT_SYMBOL_GPL(__fsnotify_parent);
 
@@ -322,12 +325,16 @@ int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_type,
 	struct super_block *sb = to_tell->i_sb;
 	struct inode *dir = S_ISDIR(to_tell->i_mode) ? to_tell : NULL;
 	struct mount *mnt = NULL;
+	struct inode *child = NULL;
 	int ret = 0;
 	__u32 test_mask, marks_mask;
 
 	if (path)
 		mnt = real_mount(path->mnt);
 
+	if (mask & FS_EVENT_ON_CHILD)
+		child = fsnotify_data_inode(data, data_type);
+
 	/*
 	 * Optimization: srcu_read_lock() has a memory barrier which can
 	 * be expensive.  It protects walking the *_fsnotify_marks lists.
@@ -336,21 +343,20 @@ int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_type,
 	 * need SRCU to keep them "alive".
 	 */
 	if (!to_tell->i_fsnotify_marks && !sb->s_fsnotify_marks &&
-	    (!mnt || !mnt->mnt_fsnotify_marks))
+	    (!mnt || !mnt->mnt_fsnotify_marks) &&
+	    (!child || !child->i_fsnotify_marks))
 		return 0;
 
-	/* An event "on child" is not intended for a mount/sb mark */
-	marks_mask = to_tell->i_fsnotify_mask;
-	if (!(mask & FS_EVENT_ON_CHILD)) {
-		marks_mask |= sb->s_fsnotify_mask;
-		if (mnt)
-			marks_mask |= mnt->mnt_fsnotify_mask;
-	}
+	marks_mask = to_tell->i_fsnotify_mask | sb->s_fsnotify_mask;
+	if (mnt)
+		marks_mask |= mnt->mnt_fsnotify_mask;
+	if (child)
+		marks_mask |= child->i_fsnotify_mask;
+
 
 	/*
 	 * if this is a modify event we may need to clear the ignored masks
-	 * otherwise return if neither the inode nor the vfsmount/sb care about
-	 * this type of event.
+	 * otherwise return if none of the marks care about this type of event.
 	 */
 	test_mask = (mask & ALL_FSNOTIFY_EVENTS);
 	if (!(mask & FS_MODIFY) && !(test_mask & marks_mask))
@@ -366,6 +372,10 @@ int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_type,
 		iter_info.marks[FSNOTIFY_OBJ_TYPE_VFSMOUNT] =
 			fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
 	}
+	if (child) {
+		iter_info.marks[FSNOTIFY_OBJ_TYPE_CHILD] =
+			fsnotify_first_mark(&child->i_fsnotify_marks);
+	}
 
 	/*
 	 * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
-- 
2.17.1


  parent reply	other threads:[~2020-07-16  8:43 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-16  8:42 [PATCH v5 00/22] fanotify events with name info Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 01/22] fanotify: generalize the handling of extra event flags Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 02/22] fanotify: generalize merge logic of events on dir Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 03/22] fanotify: distinguish between fid encode error and null fid Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 04/22] fanotify: generalize test for FAN_REPORT_FID Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 05/22] fanotify: mask out special event flags from ignored mask Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 06/22] fanotify: prepare for implicit event flags in mark mask Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 07/22] fanotify: use FAN_EVENT_ON_CHILD as implicit flag on sb/mount/non-dir marks Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 08/22] fsnotify: add object type "child" to object type iterator Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 09/22] fanotify: use struct fanotify_info to parcel the variable size buffer Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 10/22] fanotify: no external fh buffer in fanotify_name_event Amir Goldstein
2020-07-16 12:44   ` Jan Kara
2020-07-16 13:30     ` Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 11/22] dnotify: report both events on parent and child with single callback Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 12/22] inotify: " Amir Goldstein
2020-07-16 12:52   ` Jan Kara
2020-07-16 14:25     ` Amir Goldstein
2020-07-16 15:17       ` Jan Kara
2020-07-16  8:42 ` [PATCH v5 13/22] fanotify: " Amir Goldstein
2020-07-16  8:42 ` Amir Goldstein [this message]
2020-07-16  8:42 ` [PATCH v5 15/22] fsnotify: send event with parent/name info to sb/mount/non-dir marks Amir Goldstein
2020-07-16 17:01   ` Jan Kara
2020-07-16 17:20     ` Amir Goldstein
2020-07-16 17:57       ` Jan Kara
2020-07-16 18:42         ` Amir Goldstein
2020-07-16 22:34           ` Jan Kara
2020-07-17  3:49             ` Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 16/22] fsnotify: remove check that source dentry is positive Amir Goldstein
2020-07-16 13:13   ` Jan Kara
2020-07-16 13:29     ` Jan Kara
2020-07-16 13:54       ` Amir Goldstein
2020-07-16 14:06         ` Jan Kara
2020-07-16  8:42 ` [PATCH v5 17/22] fsnotify: send MOVE_SELF event with parent/name info Amir Goldstein
2020-07-16 13:45   ` Jan Kara
2020-07-16 13:59     ` Amir Goldstein
2020-07-16 14:10       ` Amir Goldstein
2020-07-16 15:57         ` Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 18/22] fanotify: add basic support for FAN_REPORT_DIR_FID Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 19/22] fanotify: report events with parent dir fid to sb/mount/non-dir marks Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 20/22] fanotify: add support for FAN_REPORT_NAME Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 21/22] fanotify: report parent fid + name + child fid Amir Goldstein
2020-07-16 15:59   ` Jan Kara
2020-07-16 16:08     ` Amir Goldstein
2020-07-16  8:42 ` [PATCH v5 22/22] fanotify: report parent fid " Amir Goldstein
2020-07-16 17:13 ` [PATCH v5 00/22] fanotify events with name info Jan Kara

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=20200716084230.30611-15-amir73il@gmail.com \
    --to=amir73il@gmail.com \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --subject='Re: [PATCH v5 14/22] fsnotify: send event to parent and child with single callback' \
    /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

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).