fsnotify: tree-watching support
Subsystems (3)
PATCH 1/3 fsnotify: tree-watching support
Add tree-watching support to fsnotify.
See http://patchwork.kernel.org/patch/79171/ and http://patchwork.kernel.org/patch/79172/ for other patches.
--- fs/debugfs/inode.c | 8 + fs/namei.c | 12 +- fs/notify/fsnotify.c | 189 +++++++++++++++++++++++++++++++-------- fs/notify/fsnotify.h | 1 fs/notify/inode_mark.c | 46 ++++++++- include/linux/dcache.h | 3 include/linux/fsnotify.h | 55 +++++++---- include/linux/fsnotify_backend.h | 51 +++++++--- 8 files changed, 282 insertions(+), 83 deletions(-)
fs/notify/fsnotify.c +151 -38
@@ -76,55 +76,165 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode)spin_unlock (&dcache_lock); } -/* Notify this dentry's parent about a child's events. */ -void __fsnotify_parent (struct dentry *dentry,__u32 mask) +/* +* Does the work when updating descents of a dentry +*/ +static void fsnotify_update_descents (struct dentry *dentry,bool watched) { -struct dentry *parent; -struct inode *p_inode; -bool send = false; -bool should_update_children = false; +struct dentry *child; + +list_for_each_entry (child, &dentry->d_subdirs, d_u.d_child) { +if (!child->d_inode) +continue ; + +spin_lock (&child->d_lock); + +if (watched) + child->d_flags |=DCACHE_FSNOTIFY_ANCESTOR_WATCHED ; +else + child->d_flags &= ~DCACHE_FSNOTIFY_ANCESTOR_WATCHED ; + +if (!fsnotify_inode_watches_descents (child->d_inode)) { +fsnotify_update_descents (child, watched); + } + +spin_unlock (&child->d_lock); + } +} -if (!(dentry->d_flags &DCACHE_FSNOTIFY_PARENT_WATCHED )) +/* do something equivalent to __fsnotify_update_child_dentry_flags, but for a tree watch */ +void __fsnotify_update_descents (struct dentry *dentry,bool lock) +{ +bool should_be =fsnotify_inode_watches_descents (dentry->d_inode) + || (dentry->d_flags &DCACHE_FSNOTIFY_ANCESTOR_WATCHED ); +bool is; + +/* any child of this dentry */ + struct dentry *onechild = list_entry(dentry->d_subdirs.next, struct dentry, d_u.d_child); + +if (onechild != dentry) + is = onechild->d_flags &DCACHE_FSNOTIFY_ANCESTOR_WATCHED ; +else +return ;/* no child dentries here */ + +if (should_be == is)return ; -spin_lock (&dentry->d_lock); - parent = dentry->d_parent; - p_inode = parent->d_inode; +if (lock) +spin_lock (&dentry->d_lock); + +fsnotify_update_descents (dentry, should_be); + +if (lock) +spin_unlock (&dentry->d_lock); +} + +/* Notify this dentry's ancestors about a child's events. */ +void __fsnotify_ancestors (struct dentry *dentry,__u32 mask) +{ +if (dentry->d_flags &DCACHE_FSNOTIFY_PARENT_WATCHED ) { +struct dentry *parent; +struct inode *p_inode; +bool should_update_children = false; +bool send = false; + +spin_lock (&dentry->d_lock); + + parent = dentry->d_parent; + p_inode = parent->d_inode; -if (fsnotify_inode_watches_children (p_inode)) { -if (p_inode->i_fsnotify_mask & mask) { +if (fsnotify_inode_watches_children (p_inode)) { +if (p_inode->i_fsnotify_mask & mask) { +dget (parent); + send = true; + } + }else { +/* +* The parent doesn't care about events on its children but +* at least one child thought it did. We need to run all the +* children and update their d_flags to let them know p_inode +* doesn't care about them any more. +*/ dget (parent); - send = true; + should_update_children = true; } - }else { -/* -* The parent doesn't care about events on it's children but -* at least one child thought it did. We need to run all the -* children and update their d_flags to let them know p_inode -* doesn't care about them any more. -*/ -dget (parent); - should_update_children = true; - } -spin_unlock (&dentry->d_lock); +spin_unlock (&dentry->d_lock); -if (send) { -/* we are notifying a parent so come up with the new mask which -* specifies these are events which came from a child. */ - mask |=FS_EVENT_ON_CHILD ; +if (send) { +/* we are notifying a parent so come up with the new mask which +* specifies these are events which came from a child. */ +fsnotify (p_inode, mask |FS_EVENT_ON_TREE , dentry->d_inode, +FSNOTIFY_EVENT_INODE , dentry->d_name.name,0 ); +dput (parent); + } -fsnotify (p_inode, mask, dentry->d_inode,FSNOTIFY_EVENT_INODE , - dentry->d_name.name,0 ); -dput (parent); +if (unlikely (should_update_children)) { +__fsnotify_update_child_dentry_flags (p_inode); +dput (parent); + } } -if (unlikely (should_update_children)) { -__fsnotify_update_child_dentry_flags (p_inode); -dput (parent); +fsnotify_far_ancestors (dentry->d_parent, dentry->d_name.name, dentry->d_name.len, mask); +} +EXPORT_SYMBOL_GPL (__fsnotify_ancestors); + +/** +* fsnotify_far_ancestors - notify tree-watching ancestors +* @dentry: The dentry the walkup should start with +* @file_name: The string that should be appended to this dentries' path +* @file_len: The length of this string +* @mask: The events' mask +*/ +void fsnotify_far_ancestors (struct dentry *dentry,const unsigned char *file_name,int file_len,__u32 mask) +{ +if (dentry->d_flags &DCACHE_FSNOTIFY_ANCESTOR_WATCHED ) { +char path[PATH_MAX ]; +char *pathptr = path +PATH_MAX -1 - file_len; +struct dentry *test_dentry, *next_update =NULL ; +struct inode *d_inode, *test_inode; + +strcpy (pathptr, file_name); + +spin_lock (&dentry->d_lock); + d_inode = dentry->d_inode; + + test_dentry = dentry; + test_inode = d_inode; +do { +dget (test_dentry); +if (fsnotify_inode_watches_descents (test_inode)) { +if (test_inode->i_fsnotify_mask & mask) { +dget (dentry); +spin_unlock (&dentry->d_lock); +inotify_inode_queue_event (test_inode, mask,0 , pathptr, + d_inode); +fsnotify (test_inode, mask |FS_EVENT_ON_DESCENT , d_inode, +FSNOTIFY_EVENT_INODE , pathptr,0 ); +spin_lock (&dentry->d_lock); +dput (dentry); + } + }else { + next_update = test_dentry; + } + + pathptr -= test_dentry->d_name.len +1 ; +strcpy (pathptr, test_dentry->d_name.name); + pathptr[test_dentry->d_name.len] ='/' ; +dput (test_dentry); + + test_dentry = test_dentry->d_parent; + test_inode = test_dentry->d_inode; + }while (test_dentry->d_flags &DCACHE_FSNOTIFY_ANCESTOR_WATCHED || +fsnotify_inode_watches_descents (test_inode)); + +spin_unlock (&dentry->d_lock); + +if (next_update) +__fsnotify_update_descents (next_update, true); } } -EXPORT_SYMBOL_GPL (__fsnotify_parent); +EXPORT_SYMBOL_GPL (fsnotify_far_ancestors);/* * This is the main call to fsnotify. The VFS calls into hook specific functions
@@ -137,8 +247,8 @@ void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is, conststruct fsnotify_group *group;struct fsnotify_event *event =NULL ;int idx; -/* global tests shouldn't care about events on child only the specific event */ -__u32 test_mask = (mask & ~FS_EVENT_ON_CHILD ); +/* global tests shouldn't care about events on child/descent only the specific event */ +__u32 test_mask = mask & ~FS_EVENT_ON_TREE ;if (list_empty (&fsnotify_groups))return ;
@@ -155,7 +265,10 @@ void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is, const*/ idx =srcu_read_lock (&fsnotify_grp_srcu);list_for_each_entry_rcu (group, &fsnotify_groups, group_list) { -if (test_mask & group->mask) { +bool send = test_mask & group->mask; + send = send && (!(mask &FS_EVENT_ON_TREE ) || + (mask & group->mask) &FS_EVENT_ON_TREE ); +if (send) {if (!group->ops->should_send_event (group, to_tell, mask))continue ;if (!event) {
fs/debugfs/inode.c +5 -3
@@ -472,6 +472,7 @@ struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,int error;struct dentry *dentry =NULL , *trap;const char *old_name; +int old_len; trap =lock_rename (new_dir, old_dir);/* Source or destination directories don't exist? */
@@ -487,6 +488,7 @@ struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, goto exit; old_name =fsnotify_oldname_init (old_dentry->d_name.name); + old_len = old_dentry->d_name.len; error =simple_rename (old_dir->d_inode, old_dentry, new_dir->d_inode, dentry);
@@ -495,9 +497,9 @@ struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, goto exit; }d_move (old_dentry, dentry); -fsnotify_move (old_dir->d_inode, new_dir->d_inode, old_name, - old_dentry->d_name.name,S_ISDIR (old_dentry->d_inode->i_mode), -NULL , old_dentry); +fsnotify_move (old_dir->d_inode, old_name, old_len, + new_dir->d_inode, old_dentry->d_name.name, old_dentry->d_name.len, +S_ISDIR (old_dentry->d_inode->i_mode),NULL , old_dentry);fsnotify_oldname_free (old_name);unlock_rename (new_dir, old_dir);dput (dentry);
fs/namei.c +8 -4
@@ -2635,8 +2635,9 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,int is_dir =S_ISDIR (old_dentry->d_inode->i_mode);const char *old_name; +int old_len;if (old_dentry->d_inode == new_dentry->d_inode) -return 0 ; - +return false; + error =may_delete (old_dir, old_dentry, is_dir);if (error)
@@ -2656,6 +2657,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,vfs_dq_init (new_dir); old_name =fsnotify_oldname_init (old_dentry->d_name.name); + old_len = old_dentry->d_name.len;if (is_dir) error =vfs_rename_dir (old_dir,old_dentry,new_dir,new_dentry);
@@ -2663,8 +2665,10 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, error =vfs_rename_other (old_dir,old_dentry,new_dir,new_dentry);if (!error) {const char *new_name = old_dentry->d_name.name; -fsnotify_move (old_dir, new_dir, old_name, new_name, is_dir, - new_dentry->d_inode, old_dentry); +int new_len = old_dentry->d_name.len; +fsnotify_move (old_dir, old_name, old_len, + new_dir, new_name, new_len, + is_dir, new_dentry->d_inode, old_dentry); }fsnotify_oldname_free (old_name);
fs/notify/fsnotify.h +1 -0
@@ -26,6 +26,7 @@ extern void fsnotify_clear_marks_by_inode(struct inode *inode);* about events that happen to its children. */ extern void __fsnotify_update_child_dentry_flags (struct inode *inode); +/* same here for tree watches */ /* allocate and destroy and event holder to attach events to notification/access queues */ extern struct fsnotify_event_holder *fsnotify_alloc_event_holder (void );
fs/notify/inode_mark.c +40 -6
89 lines not on this page · raw message
include/linux/dcache.h +2 -1
@@ -184,7 +184,8 @@ d_iput: no no no yes#define DCACHE_COOKIE 0x0040/* For use by dcookie subsystem */ -#define DCACHE_FSNOTIFY_PARENT_WATCHED 0x0080/* Parent inode is watched by some fsnotify listener */ +#define DCACHE_FSNOTIFY_PARENT_WATCHED 0x0080/* Parent inode is watched by some fsnotify listener */ +#define DCACHE_FSNOTIFY_ANCESTOR_WATCHED 0x0100/* Ancestor inode is watched by some tree-watching fsnotify listener */ extern spinlock_t dcache_lock;extern seqlock_t rename_lock;
include/linux/fsnotify.h +38 -17
155 lines not on this page · raw message
include/linux/fsnotify_backend.h +37 -14
94 lines not on this page · raw message
Signature · 1 line
14:46:13 up 5:25, 5 users, load average: 0.34, 0.29, 0.29
Quoted 42 lines · Joris Dolderer
Add tree-watching support to fsnotify.
See http://patchwork.kernel.org/patch/79171/ and http://patchwork.kernel.org/patch/79172/ for other patches.Signed-off-by Joris Dolderer <[email protected]>--- fs/debugfs/inode.c | 8 + fs/namei.c | 12 +- fs/notify/fsnotify.c | 189 +++++++++++++++++++++++++++++++-------- fs/notify/fsnotify.h | 1 fs/notify/inode_mark.c | 46 ++++++++- include/linux/dcache.h | 3 include/linux/fsnotify.h | 55 +++++++---- include/linux/fsnotify_backend.h | 51 +++++++--- 8 files changed, 282 insertions(+), 83 deletions(-)fs/notify/fsnotify.c +14
-6@@ -76,55 +76,165 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode)spin_unlock (&dcache_lock); } -/* Notify this dentry's parent about a child's events. */ -void __fsnotify_parent (struct dentry *dentry,__u32 mask) +/* +* Does the work when updating descents of a dentry +*/ +static void fsnotify_update_descents (struct dentry *dentry,bool watched) { -struct dentry *parent; -struct inode *p_inode; -bool send = false; -bool should_update_children = false; +struct dentry *child; + +list_for_each_entry (child, &dentry->d_subdirs, d_u.d_child) { +if (!child->d_inode) +continue ; + +spin_lock (&child->d_lock); + +if (watched) + child->d_flags |=DCACHE_FSNOTIFY_ANCESTOR_WATCHED ;
fs/notify/fsnotify.c void __fsnotify_update_child_dentry_flags(struct inode *inode) · line 103
Quoted 10 lines
fs/notify/fsnotify.c +10
-0+else + child->d_flags &= ~DCACHE_FSNOTIFY_ANCESTOR_WATCHED ; + +if (!fsnotify_inode_watches_descents (child->d_inode)) { +fsnotify_update_descents (child, watched); + } + +spin_unlock (&child->d_lock); + } +}
I haven't tested the code at all, just started to even peek at it and only made it this far.....
Did you run this with lockdep enabled? Turn it on, understand and fix anything it complains about.
Any kind of recursion, especially that which is not tail recursion is highly frowned upon.
What happens when you do
mkdir /tmp/subdir
ln -s ../ /tmp/subdir/upone
and then put a recursive watch on /tmp?
I'm betting deadlock for 1 of 2 reasons, either you
A) try to take the dentry->d_lock on subdir twice (deadlock)
B) run indefinitely around the /tmp -> /tmp/subdir -> /tmp/subdir/upone -> /tmp/subidr -> /tmp/subdir/upone -> ... loop.
Maybe I'm missing something, but both of these make this a non-starter....
Maybe symlinks don't break like that, but I'm betting I could do A with bind mounts if not B.....
-Eric