Skip to content

Commit c28f7e5

Browse files
committed
fsnotify: parent event notification
inotify and dnotify both use a similar parent notification mechanism. We add a generic parent notification mechanism to fsnotify for both of these to use. This new machanism also adds the dentry flag optimization which exists for inotify to dnotify. Signed-off-by: Eric Paris <eparis@redhat.com> Acked-by: Al Viro <viro@zeniv.linux.org.uk> Cc: Christoph Hellwig <hch@lst.de>
1 parent 3be25f4 commit c28f7e5

6 files changed

Lines changed: 205 additions & 10 deletions

File tree

‎fs/notify/fsnotify.c‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,97 @@ void __fsnotify_inode_delete(struct inode *inode)
3434
}
3535
EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
3636

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+
*/
44+
void __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. */
80+
void __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+
}
126+
EXPORT_SYMBOL_GPL(__fsnotify_parent);
127+
37128
/*
38129
* This is the main call to fsnotify. The VFS calls into hook specific functions
39130
* in linux/fsnotify.h. Those functions then in turn call here. Here will call

‎fs/notify/fsnotify.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ extern __u32 fsnotify_mask;
1717
extern void fsnotify_final_destroy_group(struct fsnotify_group *group);
1818
/* run the list of all marks associated with inode and flag them to be freed */
1919
extern 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+
*/
24+
extern void __fsnotify_update_child_dentry_flags(struct inode *inode);
2025
#endif /* __FS_NOTIFY_FSNOTIFY_H_ */

‎fs/notify/inode_mark.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ void fsnotify_recalc_inode_mask(struct inode *inode)
131131
spin_lock(&inode->i_lock);
132132
fsnotify_recalc_inode_mask_locked(inode);
133133
spin_unlock(&inode->i_lock);
134+
135+
__fsnotify_update_child_dentry_flags(inode);
134136
}
135137

136138
/*
@@ -189,6 +191,19 @@ void fsnotify_destroy_mark_by_entry(struct fsnotify_mark_entry *entry)
189191
*/
190192
group->ops->freeing_mark(entry, group);
191193

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+
192207
/*
193208
* it's possible that this group tried to destroy itself, but this
194209
* this mark was simultaneously being freed by inode. If that's the
@@ -323,6 +338,8 @@ int fsnotify_add_mark(struct fsnotify_mark_entry *entry,
323338
if (lentry) {
324339
ret = -EEXIST;
325340
fsnotify_put_mark(lentry);
341+
} else {
342+
__fsnotify_update_child_dentry_flags(inode);
326343
}
327344

328345
return ret;

‎include/linux/dcache.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,12 @@ d_iput: no no no yes
180180
#define DCACHE_REFERENCED 0x0008 /* Recently used, don't discard. */
181181
#define DCACHE_UNHASHED 0x0010
182182

183-
#define DCACHE_INOTIFY_PARENT_WATCHED 0x0020 /* Parent inode is watched */
183+
#define DCACHE_INOTIFY_PARENT_WATCHED 0x0020 /* Parent inode is watched by inotify */
184184

185185
#define DCACHE_COOKIE 0x0040 /* For use by dcookie subsystem */
186186

187+
#define DCACHE_FSNOTIFY_PARENT_WATCHED 0x0080 /* Parent inode is watched by some fsnotify listener */
188+
187189
extern spinlock_t dcache_lock;
188190
extern seqlock_t rename_lock;
189191

‎include/linux/fsnotify.h‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,31 @@
2323
static inline void fsnotify_d_instantiate(struct dentry *entry,
2424
struct inode *inode)
2525
{
26+
__fsnotify_d_instantiate(entry, inode);
27+
2628
inotify_d_instantiate(entry, inode);
2729
}
2830

31+
/* Notify this dentry's parent about a child's events. */
32+
static 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+
2939
/*
3040
* fsnotify_d_move - entry has been moved
3141
* Called with dcache_lock and entry->d_lock held.
3242
*/
3343
static inline void fsnotify_d_move(struct dentry *entry)
3444
{
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+
3551
inotify_d_move(entry);
3652
}
3753

@@ -117,7 +133,8 @@ static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
117133
if (isdir)
118134
mask |= FS_IN_ISDIR;
119135
dnotify_parent(dentry, DN_DELETE);
120-
inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
136+
137+
fsnotify_parent(dentry, mask);
121138
}
122139

123140
/*
@@ -188,9 +205,9 @@ static inline void fsnotify_access(struct dentry *dentry)
188205
mask |= FS_IN_ISDIR;
189206

190207
dnotify_parent(dentry, DN_ACCESS);
191-
inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
192208
inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
193209

210+
fsnotify_parent(dentry, mask);
194211
fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
195212
}
196213

@@ -206,9 +223,9 @@ static inline void fsnotify_modify(struct dentry *dentry)
206223
mask |= FS_IN_ISDIR;
207224

208225
dnotify_parent(dentry, DN_MODIFY);
209-
inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
210226
inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
211227

228+
fsnotify_parent(dentry, mask);
212229
fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
213230
}
214231

@@ -223,9 +240,9 @@ static inline void fsnotify_open(struct dentry *dentry)
223240
if (S_ISDIR(inode->i_mode))
224241
mask |= FS_IN_ISDIR;
225242

226-
inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
227243
inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
228244

245+
fsnotify_parent(dentry, mask);
229246
fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
230247
}
231248

@@ -236,16 +253,15 @@ static inline void fsnotify_close(struct file *file)
236253
{
237254
struct dentry *dentry = file->f_path.dentry;
238255
struct inode *inode = dentry->d_inode;
239-
const char *name = dentry->d_name.name;
240256
fmode_t mode = file->f_mode;
241257
__u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
242258

243259
if (S_ISDIR(inode->i_mode))
244260
mask |= FS_IN_ISDIR;
245261

246-
inotify_dentry_parent_queue_event(dentry, mask, 0, name);
247262
inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
248263

264+
fsnotify_parent(dentry, mask);
249265
fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE);
250266
}
251267

@@ -260,9 +276,9 @@ static inline void fsnotify_xattr(struct dentry *dentry)
260276
if (S_ISDIR(inode->i_mode))
261277
mask |= FS_IN_ISDIR;
262278

263-
inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
264279
inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
265280

281+
fsnotify_parent(dentry, mask);
266282
fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
267283
}
268284

@@ -311,8 +327,8 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
311327
if (S_ISDIR(inode->i_mode))
312328
in_mask |= FS_IN_ISDIR;
313329
inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
314-
inotify_dentry_parent_queue_event(dentry, in_mask, 0,
315-
dentry->d_name.name);
330+
331+
fsnotify_parent(dentry, in_mask);
316332
fsnotify(inode, in_mask, inode, FSNOTIFY_EVENT_INODE);
317333
}
318334
}

‎include/linux/fsnotify_backend.h‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@
4646
#define FS_DN_RENAME 0x10000000 /* file renamed */
4747
#define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */
4848

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+
4960
struct fsnotify_group;
5061
struct fsnotify_event;
5162
struct fsnotify_mark_entry;
@@ -183,8 +194,52 @@ struct fsnotify_mark_entry {
183194

184195
/* main fsnotify call to send events */
185196
extern void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is);
197+
extern void __fsnotify_parent(struct dentry *dentry, __u32 mask);
186198
extern void __fsnotify_inode_delete(struct inode *inode);
187199

200+
static 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+
*/
214+
static 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+
*/
232+
static 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+
}
188243

189244
/* called from fsnotify listeners, such as fanotify or dnotify */
190245

@@ -230,9 +285,18 @@ extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32
230285
static inline void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is)
231286
{}
232287

288+
static inline void __fsnotify_parent(struct dentry *dentry, __u32 mask)
289+
{}
290+
233291
static inline void __fsnotify_inode_delete(struct inode *inode)
234292
{}
235293

294+
static inline void __fsnotify_update_dcache_flags(struct dentry *dentry)
295+
{}
296+
297+
static inline void __fsnotify_d_instantiate(struct dentry *dentry, struct inode *inode)
298+
{}
299+
236300
#endif /* CONFIG_FSNOTIFY */
237301

238302
#endif /* __KERNEL __ */

0 commit comments

Comments
 (0)