Prepare for fanotify name events
Subsystems (5)
Amir Goldstein
reviewercover Prepare for fanotify name events
Jan,
Fanotify name event patches are ready at [1].
I am still updating the tests and the demo.
In the mean while, I wanted to post these few prep patches including two bug fixes, so perhaps you will be able to process them in time for v5.6-rc1.
Patches 1-4 are just cleanup and minor re-factoring in prep for the name event patches.
Patches 5-6 are fixes for minor bug that I found during the work. I improved ltp tests fanotify09 and fanotify15 to cover these bugs [2].
I did not mark those patches for stable, because backporting is not trivial and the bugs are really minor. For the same reason, I did not bother to provide bug fix patches that are not dependent on the cleanup patches, although that might have been possible to do.
Thanks,
Amir.
[1] https://github.com/amir73il/linux/commits/fanotify_name
[2] https://github.com/amir73il/ltp/commits/fsnotify-fixes
Amir Goldstein (6):
fsnotify: tidy up FS_ and FAN_ constants
fsnotify: pass dentry instead of inode for events possible on child
fsnotify: simplify arguments passing to fsnotify_parent()
fsnotify: replace inode pointer with tag
fanotify: merge duplicate events on parent and child
fanotify: fix merging marks masks with FAN_ONDIR
fs/notify/fanotify/fanotify.c | 36 ++++++++++++++++++++-------- fs/notify/fsnotify.c | 21 ++++++++-------- fs/notify/inotify/inotify_fsnotify.c | 2 +- include/linux/fsnotify.h | 26 ++++++++------------ include/linux/fsnotify_backend.h | 34 +++++++++++++------------- include/uapi/linux/fanotify.h | 4 ++-- kernel/audit_fsnotify.c | 5 +++- kernel/audit_watch.c | 5 +++- 8 files changed, 73 insertions(+), 60 deletions(-)
Signature · 1 line
2.17.1
Amir Goldstein
reviewerPATCH 1/6 fsnotify: tidy up FS_ and FAN_ constants
Order by value, so the free value ranges are easier to find.
--- include/linux/fsnotify_backend.h | 11 +++++------ include/uapi/linux/fanotify.h | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-)
include/linux/fsnotify_backend.h +5 -6
@@ -49,16 +49,15 @@#define FS_OPEN_EXEC_PERM 0x00040000/* open/exec event in a permission hook */ #define FS_EXCL_UNLINK 0x04000000/* do not send events if object is unlinked */ -#define FS_ISDIR 0x40000000/* event occurred against dir */ -#define FS_IN_ONESHOT 0x80000000/* only send event once */ - -#define FS_DN_RENAME 0x10000000/* file renamed */ -#define FS_DN_MULTISHOT 0x20000000/* dnotify multishot */ -/* This inode cares about things that happen to its children. Always set for * dnotify and inotify. */ #define FS_EVENT_ON_CHILD 0x08000000 +#define FS_DN_RENAME 0x10000000/* file renamed */ +#define FS_DN_MULTISHOT 0x20000000/* dnotify multishot */ +#define FS_ISDIR 0x40000000/* event occurred against dir */ +#define FS_IN_ONESHOT 0x80000000/* only send event once */ +#define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)/*
include/uapi/linux/fanotify.h +2 -2
@@ -25,9 +25,9 @@#define FAN_ACCESS_PERM 0x00020000/* File accessed in perm check */ #define FAN_OPEN_EXEC_PERM 0x00040000/* File open/exec in perm check */ -#define FAN_ONDIR 0x40000000/* event occurred against dir */ +#define FAN_EVENT_ON_CHILD 0x08000000/* Interested in child events */ -#define FAN_EVENT_ON_CHILD 0x08000000/* interested in child events */ +#define FAN_ONDIR 0x40000000/* Event occurred against dir */ /* helper events */ #define FAN_CLOSE (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE)/* close */
Signature · 1 line
2.17.1
Amir Goldstein
reviewerPATCH 2/6 fsnotify: pass dentry instead of inode for events possible on child
In reply to Amir GoldsteinMost events that can be reported to watching parent pass FSNOTIFY_EVENT_PATH as event data, except for FS_ARRTIB and FS_MODIFY as a result of truncate.
Define a new data type to pass for event - FSNOTIFY_EVENT_DENTRY and use it to pass the dentry instead of it's ->d_inode for those events.
Add a helper fsnotify_dentry(), similar to fsnotify_path() to report those events to child and parent.
Soon, we are going to use the dentry data type to report events with name info in fanotify backend.
--- fs/notify/fanotify/fanotify.c | 2 ++ fs/notify/fsnotify.c | 4 ++-- include/linux/fsnotify.h | 16 ++++++++++------ include/linux/fsnotify_backend.h | 3 ++- kernel/audit_fsnotify.c | 5 ++++- kernel/audit_watch.c | 5 ++++- 6 files changed, 24 insertions(+), 11 deletions(-)
fs/notify/fanotify/fanotify.c +2 -0
@@ -271,6 +271,8 @@ static struct inode *fanotify_fid_inode(struct inode *to_tell, u32 event_mask,return to_tell; else if (data_type == FSNOTIFY_EVENT_INODE) return (struct inode *)data; + else if (data_type == FSNOTIFY_EVENT_DENTRY) + return d_inode(data); else if (data_type == FSNOTIFY_EVENT_PATH) return d_inode(((struct path *)data)->dentry);return NULL ;
fs/notify/fsnotify.c +2 -2
@@ -172,8 +172,8 @@ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask ret =fsnotify (p_inode, mask, path,FSNOTIFY_EVENT_PATH , &name.name,0 ); else - ret =fsnotify (p_inode, mask, dentry->d_inode,FSNOTIFY_EVENT_INODE , - &name.name,0 ); + ret =fsnotify (p_inode, mask, dentry, +FSNOTIFY_EVENT_DENTRY , &name.name,0 );release_dentry_name_snapshot (&name); }
include/linux/fsnotify.h +10 -6
@@ -41,9 +41,15 @@ static inline int fsnotify_parent(const struct path *path, }/* -* Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when -* an event is on a path. +* Simple wrappers to consolidate calls fsnotify_parent()/fsnotify() when +* an event is on a path/dentry. */ +static inline void fsnotify_dentry (struct dentry *dentry,__u32 mask) +{ +fsnotify_parent (NULL , dentry, mask); +fsnotify (d_inode (dentry), mask, dentry,FSNOTIFY_EVENT_DENTRY ,NULL ,0 ); +} +static inline int fsnotify_path (struct inode *inode,const struct path *path,__u32 mask) {
@@ -301,8 +307,7 @@ static inline void fsnotify_xattr(struct dentry *dentry)if (S_ISDIR (inode->i_mode)) mask |=FS_ISDIR ; -fsnotify_parent (NULL , dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); +fsnotify_dentry (dentry, mask); }/*
@@ -336,8 +341,7 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)if (S_ISDIR (inode->i_mode)) mask |=FS_ISDIR ; -fsnotify_parent (NULL , dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); +fsnotify_dentry (dentry, mask); } }
include/linux/fsnotify_backend.h +2 -1
@@ -212,10 +212,11 @@ struct fsnotify_group { }; }; -/* when calling fsnotify tell it if the data is a path or inode */ +/* when calling fsnotify tell it if the data is a path or inode or dentry */ #define FSNOTIFY_EVENT_NONE 0#define FSNOTIFY_EVENT_PATH 1#define FSNOTIFY_EVENT_INODE 2 +#define FSNOTIFY_EVENT_DENTRY 3enum fsnotify_obj_type {FSNOTIFY_OBJ_TYPE_INODE ,
kernel/audit_fsnotify.c +4 -1
@@ -168,7 +168,10 @@ static int audit_mark_handle_event(struct fsnotify_group *group,switch (data_type) {case (FSNOTIFY_EVENT_PATH ): - inode = ((const struct path *)data)->dentry->d_inode; + inode =d_inode (((const struct path *)data)->dentry); +break ; +case (FSNOTIFY_EVENT_DENTRY ): + inode =d_inode (data);break ;case (FSNOTIFY_EVENT_INODE ): inode = (const struct inode *)data;
kernel/audit_watch.c +4 -1
@@ -482,7 +482,10 @@ static int audit_watch_handle_event(struct fsnotify_group *group,switch (data_type) {case (FSNOTIFY_EVENT_PATH ): - inode =d_backing_inode (((const struct path *)data)->dentry); + inode =d_inode (((const struct path *)data)->dentry); +break ; +case (FSNOTIFY_EVENT_DENTRY ): + inode =d_inode (data);break ;case (FSNOTIFY_EVENT_INODE ): inode = (const struct inode *)data;
Signature · 1 line
2.17.1
Amir Goldstein
reviewerPATCH 3/6 fsnotify: simplify arguments passing to fsnotify_parent()
In reply to Amir GoldsteinInstead of passing both dentry and path and having to figure out which one to use, use the data/data_type convention to simplify the code.
--- fs/notify/fsnotify.c | 21 ++++++++++----------- include/linux/fsnotify.h | 14 ++------------ include/linux/fsnotify_backend.h | 12 ++++++------ 3 files changed, 18 insertions(+), 29 deletions(-)
fs/notify/fsnotify.c +10 -11
@@ -143,14 +143,18 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode) }/* Notify this dentry's parent about a child's events. */ -int __fsnotify_parent (const struct path *path,struct dentry *dentry,__u32 mask) +int fsnotify_parent (__u32 mask,const void *data,int data_type) { -struct dentry *parent; +struct dentry *parent, *dentry;struct inode *p_inode;int ret =0 ; -if (!dentry) - dentry = path->dentry; +if (data_type ==FSNOTIFY_EVENT_DENTRY ) + dentry = (struct dentry *)data; +else if (data_type ==FSNOTIFY_EVENT_PATH ) + dentry = ((struct path *)data)->dentry; +else +return 0 ;if (!(dentry->d_flags &DCACHE_FSNOTIFY_PARENT_WATCHED ))return 0 ;
@@ -168,12 +172,7 @@ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask mask |=FS_EVENT_ON_CHILD ;take_dentry_name_snapshot (&name, dentry); -if (path) - ret =fsnotify (p_inode, mask, path,FSNOTIFY_EVENT_PATH , - &name.name,0 ); -else - ret =fsnotify (p_inode, mask, dentry, -FSNOTIFY_EVENT_DENTRY , &name.name,0 ); + ret =fsnotify (p_inode, mask, data, data_type, &name.name,0 );release_dentry_name_snapshot (&name); }
@@ -181,7 +180,7 @@ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 maskreturn ret; } -EXPORT_SYMBOL_GPL (__fsnotify_parent); +EXPORT_SYMBOL_GPL (fsnotify_parent); static int send_to_group(struct inode *to_tell, __u32 mask, const void *data,
include/linux/fsnotify.h +2 -12
@@ -30,30 +30,20 @@ static inline int fsnotify_dirent(struct inode *dir, struct dentry *dentry, &dentry->d_name, 0); } -/* Notify this dentry's parent about a child's events. */ -static inline int fsnotify_parent (const struct path *path, -struct dentry *dentry,__u32 mask) -{ -if (!dentry) - dentry = path->dentry; - -return __fsnotify_parent (path, dentry, mask); -} -/* * Simple wrappers to consolidate calls fsnotify_parent()/fsnotify() when * an event is on a path/dentry. */ static inline void fsnotify_dentry (struct dentry *dentry,__u32 mask) { -fsnotify_parent (NULL , dentry, mask); +fsnotify_parent (mask, dentry,FSNOTIFY_EVENT_DENTRY );fsnotify (d_inode (dentry), mask, dentry,FSNOTIFY_EVENT_DENTRY ,NULL ,0 ); }static inline int fsnotify_path (struct inode *inode,const struct path *path,__u32 mask) { -int ret =fsnotify_parent (path,NULL , mask); +int ret =fsnotify_parent (mask, path,FSNOTIFY_EVENT_PATH );if (ret)return ret;
include/linux/fsnotify_backend.h +6 -6
@@ -351,9 +351,9 @@ struct fsnotify_mark {/* called from the vfs helpers */ /* main fsnotify call to send events */ -extern int fsnotify (struct inode *to_tell,__u32 mask,const void *data,int data_is, -const struct qstr *name,u32 cookie); -extern int __fsnotify_parent (const struct path *path,struct dentry *dentry,__u32 mask); +extern int fsnotify (struct inode *to_tell,__u32 mask,const void *data, +int data_type,const struct qstr *name,u32 cookie); +extern int fsnotify_parent (__u32 mask,const void *data,int data_type);extern void __fsnotify_inode_delete (struct inode *inode);extern void __fsnotify_vfsmount_delete (struct vfsmount *mnt);extern void fsnotify_sb_delete (struct super_block *sb);
@@ -508,13 +508,13 @@ static inline void fsnotify_init_event(struct fsnotify_event *event,#else -static inline int fsnotify (struct inode *to_tell,__u32 mask,const void *data,int data_is, -const struct qstr *name,u32 cookie) +static inline int fsnotify (struct inode *to_tell,__u32 mask,const void *data, +int data_type,const struct qstr *name,u32 cookie) {return 0 ; } -static inline int __fsnotify_parent (const struct path *path,struct dentry *dentry,__u32 mask) +static inline int fsnotify_parent (__u32 mask,const void *data,int data_type) {return 0 ; }
Signature · 1 line
2.17.1
Amir Goldstein
reviewerPATCH 4/6 fsnotify: replace inode pointer with tag
In reply to Amir GoldsteinThe event inode field is used only for comparison in queue merges and cannot be dereferenced after handle_event(), because it does not hold a refcount on the inode.
Replace it with an abstract tag do to the same thing. We are going to set this tag for values other than inode pointer in fanotify.
--- fs/notify/fanotify/fanotify.c | 2 +- fs/notify/inotify/inotify_fsnotify.c | 2 +- include/linux/fsnotify_backend.h | 8 +++----- 3 files changed, 5 insertions(+), 7 deletions(-)
fs/notify/fanotify/fanotify.c +1 -1
@@ -26,7 +26,7 @@ static bool should_merge(struct fsnotify_event *old_fsn, old =FANOTIFY_E (old_fsn); new =FANOTIFY_E (new_fsn); -if (old_fsn->inode != new_fsn->inode || old->pid != new->pid || +if (old_fsn->tag != new_fsn->tag || old->pid != new->pid || old->fh_type != new->fh_type || old->fh_len != new->fh_len)return false;
fs/notify/inotify/inotify_fsnotify.c +1 -1
@@ -39,7 +39,7 @@ static bool event_compare(struct fsnotify_event *old_fsn,if (old->mask &FS_IN_IGNORED )return false;if ((old->mask == new->mask) && - (old_fsn->inode == new_fsn->inode) && + (old_fsn->tag == new_fsn->tag) && (old->name_len == new->name_len) && (!old->name_len || !strcmp (old->name, new->name)))return true;
include/linux/fsnotify_backend.h +3 -5
@@ -132,8 +132,7 @@ struct fsnotify_ops {*/ struct fsnotify_event {struct list_head list; -/* inode may ONLY be dereferenced during handle_event(). */ -struct inode *inode;/* either the inode the event happened to or its parent */ +unsigned long tag;/* identifier for queue merges */ };/*
@@ -499,11 +498,10 @@ extern void fsnotify_put_mark(struct fsnotify_mark *mark);extern void fsnotify_finish_user_wait (struct fsnotify_iter_info *iter_info);extern bool fsnotify_prepare_user_wait (struct fsnotify_iter_info *iter_info); -static inline void fsnotify_init_event (struct fsnotify_event *event, -struct inode *inode) +static inline void fsnotify_init_event (struct fsnotify_event *event,void *tag) {INIT_LIST_HEAD (&event->list); - event->inode = inode; + event->tag = (unsigned long )tag; }#else
Signature · 1 line
2.17.1
Amir Goldstein
reviewerPATCH 5/6 fanotify: merge duplicate events on parent and child
In reply to Amir GoldsteinWith inotify, when a watch is set on a directory and on its child, an event on the child is reported twice, once with wd of the parent watch and once with wd of the child watch without the filename.
With fanotify, when a watch is set on a directory and on its child, an event on the child is reported twice, but it has the exact same information - either an open file descriptor of the child or an encoded fid of the child.
The reason that the two identical events are not merged is because the tag used for merging events in the queue is the child inode in one event and parent inode in the other.
For events with path or dentry data, use the dentry instead of inode as the tag for event merging, so that the event reported on parent will be merged with the event reported on the child.
--- fs/notify/fanotify/fanotify.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
fs/notify/fanotify/fanotify.c +17 -6
@@ -265,17 +265,21 @@ static int fanotify_encode_fid(struct fanotify_event *event,* FS_CREATE reports the modified dir inode and not the created inode. */ static struct inode *fanotify_fid_inode (struct inode *to_tell,u32 event_mask, -const void *data,int data_type) +const void *data,int data_type, +struct dentry **pdentry) {if (event_mask &ALL_FSNOTIFY_DIRENT_EVENTS )return to_tell;else if (data_type ==FSNOTIFY_EVENT_INODE )return (struct inode *)data;else if (data_type ==FSNOTIFY_EVENT_DENTRY ) -return d_inode (data); + *pdentry = (struct dentry *)data;else if (data_type ==FSNOTIFY_EVENT_PATH ) -return d_inode (((struct path *)data)->dentry); -return NULL ; + *pdentry = ((struct path *)data)->dentry; +else +return NULL ; + +return d_inode (*pdentry); } struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
@@ -285,7 +289,9 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, {struct fanotify_event *event =NULL ;gfp_t gfp =GFP_KERNEL_ACCOUNT ; -struct inode *id =fanotify_fid_inode (inode, mask, data, data_type); +struct dentry *dentry =NULL ; +struct inode *id =fanotify_fid_inode (inode, mask, data, data_type, + &dentry);/* * For queues with unlimited length lost events are not expected and
@@ -316,7 +322,12 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,if (!event) goto out; init:__maybe_unused -fsnotify_init_event (&event->fse, inode); +/* +* Use the dentry instead of inode as tag for event queue, so event +* reported on parent is merged with event reported on child when both +* directory and child watches exist. +*/ +fsnotify_init_event (&event->fse, (void *)dentry ?: inode); event->mask = mask;if (FAN_GROUP_FLAG (group,FAN_REPORT_TID )) event->pid =get_pid (task_pid (current));
Signature · 1 line
2.17.1
Amir Goldstein
reviewerPATCH 6/6 fanotify: fix merging marks masks with FAN_ONDIR
In reply to Amir GoldsteinChange the logic of FAN_ONDIR in two ways that are similar to the logic of FAN_EVENT_ON_CHILD, that was fixed in commit 54a307ba8d3c ("fanotify:
fix logic of events on child"):
1. The flag is meaningless in ignore mask
2. The flag refers only to events in the mask of the mark where it is set
This is what the fanotify_mark.2 man page says about FAN_ONDIR:
"Without this flag, only events for files are created." It doesn't say anything about setting this flag in ignore mask to stop getting events on directories nor can I think of any setup where this capability would be useful.
Currently, when marks masks are merged, the FAN_ONDIR flag set in one mark affects the events that are set in another mark's mask and this behavior causes unexpected results. For example, a user adds a mark on a directory with mask FAN_ATTRIB | FAN_ONDIR and a mount mark with mask FAN_OPEN (without FAN_ONDIR). An opendir() of that directory (which is inside that mount) generates a FAN_OPEN event even though neither of the marks requested to get open events on directories.
--- fs/notify/fanotify/fanotify.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
fs/notify/fanotify/fanotify.c +7 -4
@@ -171,6 +171,13 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,if (!fsnotify_iter_should_report_type (iter_info, type))continue ; mark = iter_info->marks[type]; +/* +* If the event is on dir and this mark doesn't care about +* events on dir, don't send it! +*/ +if (event_mask &FS_ISDIR && !(mark->mask &FS_ISDIR )) +continue ; +/* * If the event is for a child and this mark doesn't care about * events on a child, don't send it!
@@ -203,10 +210,6 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, user_mask &= ~FAN_ONDIR ; } -if (event_mask &FS_ISDIR && - !(marks_mask &FS_ISDIR & ~marks_ignored_mask)) -return 0 ; -return test_mask & user_mask; }
Signature · 1 line
2.17.1