Commit 497b0c5a authored by Amir Goldstein's avatar Amir Goldstein Committed by Jan Kara

fsnotify: send event to parent and child with single callback

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

Link: https://lore.kernel.org/r/20200716084230.30611-15-amir73il@gmail.comSigned-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
parent c8f3446c
...@@ -883,6 +883,7 @@ static void kernfs_notify_workfn(struct work_struct *work) ...@@ -883,6 +883,7 @@ static void kernfs_notify_workfn(struct work_struct *work)
list_for_each_entry(info, &kernfs_root(kn)->supers, node) { list_for_each_entry(info, &kernfs_root(kn)->supers, node) {
struct kernfs_node *parent; struct kernfs_node *parent;
struct inode *p_inode = NULL;
struct inode *inode; struct inode *inode;
struct qstr name; struct qstr name;
...@@ -899,8 +900,6 @@ static void kernfs_notify_workfn(struct work_struct *work) ...@@ -899,8 +900,6 @@ static void kernfs_notify_workfn(struct work_struct *work)
name = (struct qstr)QSTR_INIT(kn->name, strlen(kn->name)); name = (struct qstr)QSTR_INIT(kn->name, strlen(kn->name));
parent = kernfs_get_parent(kn); parent = kernfs_get_parent(kn);
if (parent) { if (parent) {
struct inode *p_inode;
p_inode = ilookup(info->sb, kernfs_ino(parent)); p_inode = ilookup(info->sb, kernfs_ino(parent));
if (p_inode) { if (p_inode) {
fsnotify(p_inode, FS_MODIFY | FS_EVENT_ON_CHILD, fsnotify(p_inode, FS_MODIFY | FS_EVENT_ON_CHILD,
...@@ -911,8 +910,11 @@ static void kernfs_notify_workfn(struct work_struct *work) ...@@ -911,8 +910,11 @@ static void kernfs_notify_workfn(struct work_struct *work)
kernfs_put(parent); kernfs_put(parent);
} }
if (!p_inode) {
fsnotify(inode, FS_MODIFY, inode, FSNOTIFY_EVENT_INODE, fsnotify(inode, FS_MODIFY, inode, FSNOTIFY_EVENT_INODE,
NULL, 0); NULL, 0);
}
iput(inode); iput(inode);
} }
......
...@@ -261,12 +261,12 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, ...@@ -261,12 +261,12 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
continue; continue;
/* /*
* If the event is for a child and this mark doesn't care about * If the event is for a child and this mark is on a parent not
* events on a child, don't send it! * watching children, don't send it!
*/ */
if (event_mask & FS_EVENT_ON_CHILD && if (event_mask & FS_EVENT_ON_CHILD &&
(type != FSNOTIFY_OBJ_TYPE_INODE || type == FSNOTIFY_OBJ_TYPE_INODE &&
!(mark->mask & FS_EVENT_ON_CHILD))) !(mark->mask & FS_EVENT_ON_CHILD))
continue; continue;
marks_mask |= mark->mask; marks_mask |= mark->mask;
......
...@@ -145,17 +145,21 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode) ...@@ -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 * Notify this dentry's parent about a child's events with child name info
* if parent is watching. * 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 __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
int data_type) int data_type)
{ {
struct inode *inode = d_inode(dentry);
struct dentry *parent; struct dentry *parent;
struct inode *p_inode; struct inode *p_inode;
struct name_snapshot name;
struct qstr *file_name = NULL;
int ret = 0; int ret = 0;
parent = NULL;
if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED)) if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
goto notify_child; goto notify;
parent = dget_parent(dentry); parent = dget_parent(dentry);
p_inode = parent->d_inode; p_inode = parent->d_inode;
...@@ -163,25 +167,24 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data, ...@@ -163,25 +167,24 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
if (unlikely(!fsnotify_inode_watches_children(p_inode))) { if (unlikely(!fsnotify_inode_watches_children(p_inode))) {
__fsnotify_update_child_dentry_flags(p_inode); __fsnotify_update_child_dentry_flags(p_inode);
} else if (p_inode->i_fsnotify_mask & mask & ALL_FSNOTIFY_EVENTS) { } 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));
/* /* Notify both parent and child with child name info */
* We are notifying a parent, so set a flag in mask to inform inode = p_inode;
* backend that event has information about a child entry.
*/
take_dentry_name_snapshot(&name, dentry); take_dentry_name_snapshot(&name, dentry);
ret = fsnotify(p_inode, mask | FS_EVENT_ON_CHILD, data, file_name = &name.name;
data_type, &name.name, 0); mask |= FS_EVENT_ON_CHILD;
release_dentry_name_snapshot(&name);
} }
notify:
ret = fsnotify(inode, mask, data, data_type, file_name, 0);
if (file_name)
release_dentry_name_snapshot(&name);
dput(parent); dput(parent);
if (ret)
return ret; return ret;
notify_child:
return fsnotify(d_inode(dentry), mask, data, data_type, NULL, 0);
} }
EXPORT_SYMBOL_GPL(__fsnotify_parent); EXPORT_SYMBOL_GPL(__fsnotify_parent);
...@@ -322,12 +325,16 @@ int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_type, ...@@ -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 super_block *sb = to_tell->i_sb;
struct inode *dir = file_name ? to_tell : NULL; struct inode *dir = file_name ? to_tell : NULL;
struct mount *mnt = NULL; struct mount *mnt = NULL;
struct inode *child = NULL;
int ret = 0; int ret = 0;
__u32 test_mask, marks_mask; __u32 test_mask, marks_mask;
if (path) if (path)
mnt = real_mount(path->mnt); 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 * Optimization: srcu_read_lock() has a memory barrier which can
* be expensive. It protects walking the *_fsnotify_marks lists. * 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, ...@@ -336,21 +343,20 @@ int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_type,
* need SRCU to keep them "alive". * need SRCU to keep them "alive".
*/ */
if (!to_tell->i_fsnotify_marks && !sb->s_fsnotify_marks && 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; return 0;
/* An event "on child" is not intended for a mount/sb mark */ marks_mask = to_tell->i_fsnotify_mask | sb->s_fsnotify_mask;
marks_mask = to_tell->i_fsnotify_mask;
if (!(mask & FS_EVENT_ON_CHILD)) {
marks_mask |= sb->s_fsnotify_mask;
if (mnt) if (mnt)
marks_mask |= mnt->mnt_fsnotify_mask; 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 * 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 * otherwise return if none of the marks care about this type of event.
* this type of event.
*/ */
test_mask = (mask & ALL_FSNOTIFY_EVENTS); test_mask = (mask & ALL_FSNOTIFY_EVENTS);
if (!(mask & FS_MODIFY) && !(test_mask & marks_mask)) 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, ...@@ -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] = iter_info.marks[FSNOTIFY_OBJ_TYPE_VFSMOUNT] =
fsnotify_first_mark(&mnt->mnt_fsnotify_marks); 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 * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment