aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/notify/fsnotify.c91
-rw-r--r--fs/notify/fsnotify.h5
-rw-r--r--fs/notify/inode_mark.c17
-rw-r--r--include/linux/dcache.h4
-rw-r--r--include/linux/fsnotify.h34
-rw-r--r--include/linux/fsnotify_backend.h64
6 files changed, 205 insertions, 10 deletions
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index d5654629c659..7fc760067a62 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -35,6 +35,97 @@ void __fsnotify_inode_delete(struct inode *inode)
35EXPORT_SYMBOL_GPL(__fsnotify_inode_delete); 35EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
36 36
37/* 37/*
38 * Given an inode, first check if we care what happens to our children. Inotify
39 * and dnotify both tell their parents about events. If we care about any event
40 * on a child we run all of our children and set a dentry flag saying that the
41 * parent cares. Thus when an event happens on a child it can quickly tell if
42 * if there is a need to find a parent and send the event to the parent.
43 */
44void __fsnotify_update_child_dentry_flags(struct inode *inode)
45{
46 struct dentry *alias;
47 int watched;
48
49 if (!S_ISDIR(inode->i_mode))
50 return;
51
52 /* determine if the children should tell inode about their events */
53 watched = fsnotify_inode_watches_children(inode);
54
55 spin_lock(&dcache_lock);
56 /* run all of the dentries associated with this inode. Since this is a
57 * directory, there damn well better only be one item on this list */
58 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
59 struct dentry *child;
60
61 /* run all of the children of the original inode and fix their
62 * d_flags to indicate parental interest (their parent is the
63 * original inode) */
64 list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
65 if (!child->d_inode)
66 continue;
67
68 spin_lock(&child->d_lock);
69 if (watched)
70 child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
71 else
72 child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
73 spin_unlock(&child->d_lock);
74 }
75 }
76 spin_unlock(&dcache_lock);
77}
78
79/* Notify this dentry's parent about a child's events. */
80void __fsnotify_parent(struct dentry *dentry, __u32 mask)
81{
82 struct dentry *parent;
83 struct inode *p_inode;
84 bool send = false;
85 bool should_update_children = false;
86
87 if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
88 return;
89
90 spin_lock(&dentry->d_lock);
91 parent = dentry->d_parent;
92 p_inode = parent->d_inode;
93
94 if (fsnotify_inode_watches_children(p_inode)) {
95 if (p_inode->i_fsnotify_mask & mask) {
96 dget(parent);
97 send = true;
98 }
99 } else {
100 /*
101 * The parent doesn't care about events on it's children but
102 * at least one child thought it did. We need to run all the
103 * children and update their d_flags to let them know p_inode
104 * doesn't care about them any more.
105 */
106 dget(parent);
107 should_update_children = true;
108 }
109
110 spin_unlock(&dentry->d_lock);
111
112 if (send) {
113 /* we are notifying a parent so come up with the new mask which
114 * specifies these are events which came from a child. */
115 mask |= FS_EVENT_ON_CHILD;
116
117 fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE);
118 dput(parent);
119 }
120
121 if (unlikely(should_update_children)) {
122 __fsnotify_update_child_dentry_flags(p_inode);
123 dput(parent);
124 }
125}
126EXPORT_SYMBOL_GPL(__fsnotify_parent);
127
128/*
38 * This is the main call to fsnotify. The VFS calls into hook specific functions 129 * This is the main call to fsnotify. The VFS calls into hook specific functions
39 * in linux/fsnotify.h. Those functions then in turn call here. Here will call 130 * in linux/fsnotify.h. Those functions then in turn call here. Here will call
40 * out to all of the registered fsnotify_group. Those groups can then use the 131 * out to all of the registered fsnotify_group. Those groups can then use the
diff --git a/fs/notify/fsnotify.h b/fs/notify/fsnotify.h
index 8ebcbe893c91..83b8ec0a8ec2 100644
--- a/fs/notify/fsnotify.h
+++ b/fs/notify/fsnotify.h
@@ -17,4 +17,9 @@ extern __u32 fsnotify_mask;
17extern void fsnotify_final_destroy_group(struct fsnotify_group *group); 17extern void fsnotify_final_destroy_group(struct fsnotify_group *group);
18/* run the list of all marks associated with inode and flag them to be freed */ 18/* run the list of all marks associated with inode and flag them to be freed */
19extern void fsnotify_clear_marks_by_inode(struct inode *inode); 19extern void fsnotify_clear_marks_by_inode(struct inode *inode);
20/*
21 * update the dentry->d_flags of all of inode's children to indicate if inode cares
22 * about events that happen to its children.
23 */
24extern void __fsnotify_update_child_dentry_flags(struct inode *inode);
20#endif /* __FS_NOTIFY_FSNOTIFY_H_ */ 25#endif /* __FS_NOTIFY_FSNOTIFY_H_ */
diff --git a/fs/notify/inode_mark.c b/fs/notify/inode_mark.c
index cdc154146974..a39534845b28 100644
--- a/fs/notify/inode_mark.c
+++ b/fs/notify/inode_mark.c
@@ -131,6 +131,8 @@ void fsnotify_recalc_inode_mask(struct inode *inode)
131 spin_lock(&inode->i_lock); 131 spin_lock(&inode->i_lock);
132 fsnotify_recalc_inode_mask_locked(inode); 132 fsnotify_recalc_inode_mask_locked(inode);
133 spin_unlock(&inode->i_lock); 133 spin_unlock(&inode->i_lock);
134
135 __fsnotify_update_child_dentry_flags(inode);
134} 136}
135 137
136/* 138/*
@@ -190,6 +192,19 @@ void fsnotify_destroy_mark_by_entry(struct fsnotify_mark_entry *entry)
190 group->ops->freeing_mark(entry, group); 192 group->ops->freeing_mark(entry, group);
191 193
192 /* 194 /*
195 * __fsnotify_update_child_dentry_flags(inode);
196 *
197 * I really want to call that, but we can't, we have no idea if the inode
198 * still exists the second we drop the entry->lock.
199 *
200 * The next time an event arrive to this inode from one of it's children
201 * __fsnotify_parent will see that the inode doesn't care about it's
202 * children and will update all of these flags then. So really this
203 * is just a lazy update (and could be a perf win...)
204 */
205
206
207 /*
193 * it's possible that this group tried to destroy itself, but this 208 * it's possible that this group tried to destroy itself, but this
194 * this mark was simultaneously being freed by inode. If that's the 209 * this mark was simultaneously being freed by inode. If that's the
195 * case, we finish freeing the group here. 210 * case, we finish freeing the group here.
@@ -323,6 +338,8 @@ int fsnotify_add_mark(struct fsnotify_mark_entry *entry,
323 if (lentry) { 338 if (lentry) {
324 ret = -EEXIST; 339 ret = -EEXIST;
325 fsnotify_put_mark(lentry); 340 fsnotify_put_mark(lentry);
341 } else {
342 __fsnotify_update_child_dentry_flags(inode);
326 } 343 }
327 344
328 return ret; 345 return ret;
diff --git a/include/linux/dcache.h b/include/linux/dcache.h
index 15156364d196..97978004338d 100644
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -180,10 +180,12 @@ d_iput: no no no yes
180#define DCACHE_REFERENCED 0x0008 /* Recently used, don't discard. */ 180#define DCACHE_REFERENCED 0x0008 /* Recently used, don't discard. */
181#define DCACHE_UNHASHED 0x0010 181#define DCACHE_UNHASHED 0x0010
182 182
183#define DCACHE_INOTIFY_PARENT_WATCHED 0x0020 /* Parent inode is watched */ 183#define DCACHE_INOTIFY_PARENT_WATCHED 0x0020 /* Parent inode is watched by inotify */
184 184
185#define DCACHE_COOKIE 0x0040 /* For use by dcookie subsystem */ 185#define DCACHE_COOKIE 0x0040 /* For use by dcookie subsystem */
186 186
187#define DCACHE_FSNOTIFY_PARENT_WATCHED 0x0080 /* Parent inode is watched by some fsnotify listener */
188
187extern spinlock_t dcache_lock; 189extern spinlock_t dcache_lock;
188extern seqlock_t rename_lock; 190extern seqlock_t rename_lock;
189 191
diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h
index 3856eb6e5973..6a662ed0bc8a 100644
--- a/include/linux/fsnotify.h
+++ b/include/linux/fsnotify.h
@@ -23,15 +23,31 @@
23static inline void fsnotify_d_instantiate(struct dentry *entry, 23static inline void fsnotify_d_instantiate(struct dentry *entry,
24 struct inode *inode) 24 struct inode *inode)
25{ 25{
26 __fsnotify_d_instantiate(entry, inode);
27
26 inotify_d_instantiate(entry, inode); 28 inotify_d_instantiate(entry, inode);
27} 29}
28 30
31/* Notify this dentry's parent about a child's events. */
32static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)
33{
34 __fsnotify_parent(dentry, mask);
35
36 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
37}
38
29/* 39/*
30 * fsnotify_d_move - entry has been moved 40 * fsnotify_d_move - entry has been moved
31 * Called with dcache_lock and entry->d_lock held. 41 * Called with dcache_lock and entry->d_lock held.
32 */ 42 */
33static inline void fsnotify_d_move(struct dentry *entry) 43static inline void fsnotify_d_move(struct dentry *entry)
34{ 44{
45 /*
46 * On move we need to update entry->d_flags to indicate if the new parent
47 * cares about events from this entry.
48 */
49 __fsnotify_update_dcache_flags(entry);
50
35 inotify_d_move(entry); 51 inotify_d_move(entry);
36} 52}
37 53
@@ -117,7 +133,8 @@ static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
117 if (isdir) 133 if (isdir)
118 mask |= FS_IN_ISDIR; 134 mask |= FS_IN_ISDIR;
119 dnotify_parent(dentry, DN_DELETE); 135 dnotify_parent(dentry, DN_DELETE);
120 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name); 136
137 fsnotify_parent(dentry, mask);
121} 138}
122 139
123/* 140/*
@@ -188,9 +205,9 @@ static inline void fsnotify_access(struct dentry *dentry)
188 mask |= FS_IN_ISDIR; 205 mask |= FS_IN_ISDIR;
189 206
190 dnotify_parent(dentry, DN_ACCESS); 207 dnotify_parent(dentry, DN_ACCESS);
191 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
192 inotify_inode_queue_event(inode, mask, 0, NULL, NULL); 208 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
193 209
210 fsnotify_parent(dentry, mask);
194 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE); 211 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
195} 212}
196 213
@@ -206,9 +223,9 @@ static inline void fsnotify_modify(struct dentry *dentry)
206 mask |= FS_IN_ISDIR; 223 mask |= FS_IN_ISDIR;
207 224
208 dnotify_parent(dentry, DN_MODIFY); 225 dnotify_parent(dentry, DN_MODIFY);
209 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
210 inotify_inode_queue_event(inode, mask, 0, NULL, NULL); 226 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
211 227
228 fsnotify_parent(dentry, mask);
212 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE); 229 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
213} 230}
214 231
@@ -223,9 +240,9 @@ static inline void fsnotify_open(struct dentry *dentry)
223 if (S_ISDIR(inode->i_mode)) 240 if (S_ISDIR(inode->i_mode))
224 mask |= FS_IN_ISDIR; 241 mask |= FS_IN_ISDIR;
225 242
226 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
227 inotify_inode_queue_event(inode, mask, 0, NULL, NULL); 243 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
228 244
245 fsnotify_parent(dentry, mask);
229 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE); 246 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
230} 247}
231 248
@@ -236,16 +253,15 @@ static inline void fsnotify_close(struct file *file)
236{ 253{
237 struct dentry *dentry = file->f_path.dentry; 254 struct dentry *dentry = file->f_path.dentry;
238 struct inode *inode = dentry->d_inode; 255 struct inode *inode = dentry->d_inode;
239 const char *name = dentry->d_name.name;
240 fmode_t mode = file->f_mode; 256 fmode_t mode = file->f_mode;
241 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE; 257 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
242 258
243 if (S_ISDIR(inode->i_mode)) 259 if (S_ISDIR(inode->i_mode))
244 mask |= FS_IN_ISDIR; 260 mask |= FS_IN_ISDIR;
245 261
246 inotify_dentry_parent_queue_event(dentry, mask, 0, name);
247 inotify_inode_queue_event(inode, mask, 0, NULL, NULL); 262 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
248 263
264 fsnotify_parent(dentry, mask);
249 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE); 265 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE);
250} 266}
251 267
@@ -260,9 +276,9 @@ static inline void fsnotify_xattr(struct dentry *dentry)
260 if (S_ISDIR(inode->i_mode)) 276 if (S_ISDIR(inode->i_mode))
261 mask |= FS_IN_ISDIR; 277 mask |= FS_IN_ISDIR;
262 278
263 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
264 inotify_inode_queue_event(inode, mask, 0, NULL, NULL); 279 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
265 280
281 fsnotify_parent(dentry, mask);
266 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE); 282 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
267} 283}
268 284
@@ -311,8 +327,8 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
311 if (S_ISDIR(inode->i_mode)) 327 if (S_ISDIR(inode->i_mode))
312 in_mask |= FS_IN_ISDIR; 328 in_mask |= FS_IN_ISDIR;
313 inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL); 329 inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
314 inotify_dentry_parent_queue_event(dentry, in_mask, 0, 330
315 dentry->d_name.name); 331 fsnotify_parent(dentry, in_mask);
316 fsnotify(inode, in_mask, inode, FSNOTIFY_EVENT_INODE); 332 fsnotify(inode, in_mask, inode, FSNOTIFY_EVENT_INODE);
317 } 333 }
318} 334}
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index cad5c4d75c1d..13d2dd570049 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -46,6 +46,17 @@
46#define FS_DN_RENAME 0x10000000 /* file renamed */ 46#define FS_DN_RENAME 0x10000000 /* file renamed */
47#define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */ 47#define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */
48 48
49/* This inode cares about things that happen to its children. Always set for
50 * dnotify and inotify. */
51#define FS_EVENT_ON_CHILD 0x08000000
52
53/* This is a list of all events that may get sent to a parernt based on fs event
54 * happening to inodes inside that directory */
55#define FS_EVENTS_POSS_ON_CHILD (FS_ACCESS | FS_MODIFY | FS_ATTRIB |\
56 FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | FS_OPEN |\
57 FS_MOVED_FROM | FS_MOVED_TO | FS_CREATE |\
58 FS_DELETE)
59
49struct fsnotify_group; 60struct fsnotify_group;
50struct fsnotify_event; 61struct fsnotify_event;
51struct fsnotify_mark_entry; 62struct fsnotify_mark_entry;
@@ -183,8 +194,52 @@ struct fsnotify_mark_entry {
183 194
184/* main fsnotify call to send events */ 195/* main fsnotify call to send events */
185extern void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is); 196extern void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is);
197extern void __fsnotify_parent(struct dentry *dentry, __u32 mask);
186extern void __fsnotify_inode_delete(struct inode *inode); 198extern void __fsnotify_inode_delete(struct inode *inode);
187 199
200static inline int fsnotify_inode_watches_children(struct inode *inode)
201{
202 /* FS_EVENT_ON_CHILD is set if the inode may care */
203 if (!(inode->i_fsnotify_mask & FS_EVENT_ON_CHILD))
204 return 0;
205 /* this inode might care about child events, does it care about the
206 * specific set of events that can happen on a child? */
207 return inode->i_fsnotify_mask & FS_EVENTS_POSS_ON_CHILD;
208}
209
210/*
211 * Update the dentry with a flag indicating the interest of its parent to receive
212 * filesystem events when those events happens to this dentry->d_inode.
213 */
214static inline void __fsnotify_update_dcache_flags(struct dentry *dentry)
215{
216 struct dentry *parent;
217
218 assert_spin_locked(&dcache_lock);
219 assert_spin_locked(&dentry->d_lock);
220
221 parent = dentry->d_parent;
222 if (fsnotify_inode_watches_children(parent->d_inode))
223 dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
224 else
225 dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
226}
227
228/*
229 * fsnotify_d_instantiate - instantiate a dentry for inode
230 * Called with dcache_lock held.
231 */
232static inline void __fsnotify_d_instantiate(struct dentry *dentry, struct inode *inode)
233{
234 if (!inode)
235 return;
236
237 assert_spin_locked(&dcache_lock);
238
239 spin_lock(&dentry->d_lock);
240 __fsnotify_update_dcache_flags(dentry);
241 spin_unlock(&dentry->d_lock);
242}
188 243
189/* called from fsnotify listeners, such as fanotify or dnotify */ 244/* called from fsnotify listeners, such as fanotify or dnotify */
190 245
@@ -230,9 +285,18 @@ extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32
230static inline void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is) 285static inline void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is)
231{} 286{}
232 287
288static inline void __fsnotify_parent(struct dentry *dentry, __u32 mask)
289{}
290
233static inline void __fsnotify_inode_delete(struct inode *inode) 291static inline void __fsnotify_inode_delete(struct inode *inode)
234{} 292{}
235 293
294static inline void __fsnotify_update_dcache_flags(struct dentry *dentry)
295{}
296
297static inline void __fsnotify_d_instantiate(struct dentry *dentry, struct inode *inode)
298{}
299
236#endif /* CONFIG_FSNOTIFY */ 300#endif /* CONFIG_FSNOTIFY */
237 301
238#endif /* __KERNEL __ */ 302#endif /* __KERNEL __ */