Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Directory notifications for Linux. | |
3 | * | |
4 | * Copyright (C) 2000,2001,2002 Stephen Rothwell | |
5 | * | |
3c5119c0 EP |
6 | * Copyright (C) 2009 Eric Paris <Red Hat Inc> |
7 | * dnotify was largly rewritten to use the new fsnotify infrastructure | |
8 | * | |
1da177e4 LT |
9 | * This program is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU General Public License as published by the | |
11 | * Free Software Foundation; either version 2, or (at your option) any | |
12 | * later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | */ | |
19 | #include <linux/fs.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/sched.h> | |
22 | #include <linux/dnotify.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/spinlock.h> | |
25 | #include <linux/slab.h> | |
9f3acc31 | 26 | #include <linux/fdtable.h> |
3c5119c0 | 27 | #include <linux/fsnotify_backend.h> |
1da177e4 | 28 | |
fa3536cc | 29 | int dir_notify_enable __read_mostly = 1; |
1da177e4 | 30 | |
3c5119c0 EP |
31 | static struct kmem_cache *dnotify_struct_cache __read_mostly; |
32 | static struct kmem_cache *dnotify_mark_entry_cache __read_mostly; | |
33 | static struct fsnotify_group *dnotify_group __read_mostly; | |
34 | static DEFINE_MUTEX(dnotify_mark_mutex); | |
35 | ||
36 | /* | |
37 | * dnotify will attach one of these to each inode (i_fsnotify_mark_entries) which | |
38 | * is being watched by dnotify. If multiple userspace applications are watching | |
39 | * the same directory with dnotify their information is chained in dn | |
40 | */ | |
41 | struct dnotify_mark_entry { | |
42 | struct fsnotify_mark_entry fsn_entry; | |
43 | struct dnotify_struct *dn; | |
44 | }; | |
1da177e4 | 45 | |
3c5119c0 EP |
46 | /* |
47 | * When a process starts or stops watching an inode the set of events which | |
48 | * dnotify cares about for that inode may change. This function runs the | |
49 | * list of everything receiving dnotify events about this directory and calculates | |
50 | * the set of all those events. After it updates what dnotify is interested in | |
51 | * it calls the fsnotify function so it can update the set of all events relevant | |
52 | * to this inode. | |
53 | */ | |
54 | static void dnotify_recalc_inode_mask(struct fsnotify_mark_entry *entry) | |
1da177e4 | 55 | { |
3c5119c0 | 56 | __u32 new_mask, old_mask; |
1da177e4 | 57 | struct dnotify_struct *dn; |
3c5119c0 EP |
58 | struct dnotify_mark_entry *dnentry = container_of(entry, |
59 | struct dnotify_mark_entry, | |
60 | fsn_entry); | |
61 | ||
62 | assert_spin_locked(&entry->lock); | |
1da177e4 | 63 | |
3c5119c0 | 64 | old_mask = entry->mask; |
1da177e4 | 65 | new_mask = 0; |
3c5119c0 EP |
66 | for (dn = dnentry->dn; dn != NULL; dn = dn->dn_next) |
67 | new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT); | |
68 | entry->mask = new_mask; | |
69 | ||
70 | if (old_mask == new_mask) | |
71 | return; | |
72 | ||
73 | if (entry->inode) | |
74 | fsnotify_recalc_inode_mask(entry->inode); | |
1da177e4 LT |
75 | } |
76 | ||
3c5119c0 EP |
77 | /* |
78 | * Mains fsnotify call where events are delivered to dnotify. | |
79 | * Find the dnotify mark on the relevant inode, run the list of dnotify structs | |
80 | * on that mark and determine which of them has expressed interest in receiving | |
81 | * events of this type. When found send the correct process and signal and | |
82 | * destroy the dnotify struct if it was not registered to receive multiple | |
83 | * events. | |
84 | */ | |
85 | static int dnotify_handle_event(struct fsnotify_group *group, | |
86 | struct fsnotify_event *event) | |
87 | { | |
88 | struct fsnotify_mark_entry *entry = NULL; | |
89 | struct dnotify_mark_entry *dnentry; | |
90 | struct inode *to_tell; | |
91 | struct dnotify_struct *dn; | |
92 | struct dnotify_struct **prev; | |
93 | struct fown_struct *fown; | |
94552684 | 94 | __u32 test_mask = event->mask & ~FS_EVENT_ON_CHILD; |
3c5119c0 EP |
95 | |
96 | to_tell = event->to_tell; | |
97 | ||
98 | spin_lock(&to_tell->i_lock); | |
99 | entry = fsnotify_find_mark_entry(group, to_tell); | |
100 | spin_unlock(&to_tell->i_lock); | |
101 | ||
102 | /* unlikely since we alreay passed dnotify_should_send_event() */ | |
103 | if (unlikely(!entry)) | |
104 | return 0; | |
105 | dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry); | |
106 | ||
107 | spin_lock(&entry->lock); | |
108 | prev = &dnentry->dn; | |
109 | while ((dn = *prev) != NULL) { | |
94552684 | 110 | if ((dn->dn_mask & test_mask) == 0) { |
3c5119c0 EP |
111 | prev = &dn->dn_next; |
112 | continue; | |
113 | } | |
114 | fown = &dn->dn_filp->f_owner; | |
115 | send_sigio(fown, dn->dn_fd, POLL_MSG); | |
116 | if (dn->dn_mask & FS_DN_MULTISHOT) | |
117 | prev = &dn->dn_next; | |
118 | else { | |
119 | *prev = dn->dn_next; | |
120 | kmem_cache_free(dnotify_struct_cache, dn); | |
121 | dnotify_recalc_inode_mask(entry); | |
122 | } | |
123 | } | |
124 | ||
125 | spin_unlock(&entry->lock); | |
126 | fsnotify_put_mark(entry); | |
127 | ||
128 | return 0; | |
129 | } | |
130 | ||
131 | /* | |
132 | * Given an inode and mask determine if dnotify would be interested in sending | |
133 | * userspace notification for that pair. | |
134 | */ | |
135 | static bool dnotify_should_send_event(struct fsnotify_group *group, | |
3a9fb89f EP |
136 | struct inode *inode, struct vfsmount *mnt, |
137 | __u32 mask, void *data, int data_type) | |
3c5119c0 EP |
138 | { |
139 | struct fsnotify_mark_entry *entry; | |
140 | bool send; | |
141 | ||
142 | /* !dir_notify_enable should never get here, don't waste time checking | |
143 | if (!dir_notify_enable) | |
144 | return 0; */ | |
145 | ||
146 | /* not a dir, dnotify doesn't care */ | |
147 | if (!S_ISDIR(inode->i_mode)) | |
148 | return false; | |
149 | ||
150 | spin_lock(&inode->i_lock); | |
151 | entry = fsnotify_find_mark_entry(group, inode); | |
152 | spin_unlock(&inode->i_lock); | |
153 | ||
154 | /* no mark means no dnotify watch */ | |
155 | if (!entry) | |
156 | return false; | |
157 | ||
e42e2773 | 158 | mask = (mask & ~FS_EVENT_ON_CHILD); |
5ac697b7 | 159 | send = (mask & entry->mask); |
ce61856b | 160 | |
3c5119c0 EP |
161 | fsnotify_put_mark(entry); /* matches fsnotify_find_mark_entry */ |
162 | ||
163 | return send; | |
164 | } | |
165 | ||
3c5119c0 EP |
166 | static void dnotify_free_mark(struct fsnotify_mark_entry *entry) |
167 | { | |
168 | struct dnotify_mark_entry *dnentry = container_of(entry, | |
169 | struct dnotify_mark_entry, | |
170 | fsn_entry); | |
171 | ||
172 | BUG_ON(dnentry->dn); | |
173 | ||
174 | kmem_cache_free(dnotify_mark_entry_cache, dnentry); | |
175 | } | |
176 | ||
177 | static struct fsnotify_ops dnotify_fsnotify_ops = { | |
178 | .handle_event = dnotify_handle_event, | |
179 | .should_send_event = dnotify_should_send_event, | |
180 | .free_group_priv = NULL, | |
a092ee20 | 181 | .freeing_mark = NULL, |
e4aff117 | 182 | .free_event_priv = NULL, |
3c5119c0 EP |
183 | }; |
184 | ||
185 | /* | |
186 | * Called every time a file is closed. Looks first for a dnotify mark on the | |
187 | * inode. If one is found run all of the ->dn entries attached to that | |
188 | * mark for one relevant to this process closing the file and remove that | |
189 | * dnotify_struct. If that was the last dnotify_struct also remove the | |
190 | * fsnotify_mark_entry. | |
191 | */ | |
1da177e4 LT |
192 | void dnotify_flush(struct file *filp, fl_owner_t id) |
193 | { | |
3c5119c0 EP |
194 | struct fsnotify_mark_entry *entry; |
195 | struct dnotify_mark_entry *dnentry; | |
1da177e4 LT |
196 | struct dnotify_struct *dn; |
197 | struct dnotify_struct **prev; | |
198 | struct inode *inode; | |
199 | ||
0f7fc9e4 | 200 | inode = filp->f_path.dentry->d_inode; |
1da177e4 LT |
201 | if (!S_ISDIR(inode->i_mode)) |
202 | return; | |
3c5119c0 | 203 | |
1da177e4 | 204 | spin_lock(&inode->i_lock); |
3c5119c0 EP |
205 | entry = fsnotify_find_mark_entry(dnotify_group, inode); |
206 | spin_unlock(&inode->i_lock); | |
207 | if (!entry) | |
208 | return; | |
209 | dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry); | |
210 | ||
211 | mutex_lock(&dnotify_mark_mutex); | |
212 | ||
213 | spin_lock(&entry->lock); | |
214 | prev = &dnentry->dn; | |
1da177e4 LT |
215 | while ((dn = *prev) != NULL) { |
216 | if ((dn->dn_owner == id) && (dn->dn_filp == filp)) { | |
217 | *prev = dn->dn_next; | |
3c5119c0 EP |
218 | kmem_cache_free(dnotify_struct_cache, dn); |
219 | dnotify_recalc_inode_mask(entry); | |
1da177e4 LT |
220 | break; |
221 | } | |
222 | prev = &dn->dn_next; | |
223 | } | |
3c5119c0 EP |
224 | |
225 | spin_unlock(&entry->lock); | |
226 | ||
227 | /* nothing else could have found us thanks to the dnotify_mark_mutex */ | |
228 | if (dnentry->dn == NULL) | |
229 | fsnotify_destroy_mark_by_entry(entry); | |
230 | ||
231 | fsnotify_recalc_group_mask(dnotify_group); | |
232 | ||
233 | mutex_unlock(&dnotify_mark_mutex); | |
234 | ||
235 | fsnotify_put_mark(entry); | |
236 | } | |
237 | ||
238 | /* this conversion is done only at watch creation */ | |
239 | static __u32 convert_arg(unsigned long arg) | |
240 | { | |
241 | __u32 new_mask = FS_EVENT_ON_CHILD; | |
242 | ||
243 | if (arg & DN_MULTISHOT) | |
244 | new_mask |= FS_DN_MULTISHOT; | |
245 | if (arg & DN_DELETE) | |
246 | new_mask |= (FS_DELETE | FS_MOVED_FROM); | |
247 | if (arg & DN_MODIFY) | |
248 | new_mask |= FS_MODIFY; | |
249 | if (arg & DN_ACCESS) | |
250 | new_mask |= FS_ACCESS; | |
251 | if (arg & DN_ATTRIB) | |
252 | new_mask |= FS_ATTRIB; | |
253 | if (arg & DN_RENAME) | |
254 | new_mask |= FS_DN_RENAME; | |
255 | if (arg & DN_CREATE) | |
256 | new_mask |= (FS_CREATE | FS_MOVED_TO); | |
257 | ||
258 | return new_mask; | |
1da177e4 LT |
259 | } |
260 | ||
3c5119c0 EP |
261 | /* |
262 | * If multiple processes watch the same inode with dnotify there is only one | |
263 | * dnotify mark in inode->i_fsnotify_mark_entries but we chain a dnotify_struct | |
264 | * onto that mark. This function either attaches the new dnotify_struct onto | |
265 | * that list, or it |= the mask onto an existing dnofiy_struct. | |
266 | */ | |
267 | static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark_entry *dnentry, | |
268 | fl_owner_t id, int fd, struct file *filp, __u32 mask) | |
269 | { | |
270 | struct dnotify_struct *odn; | |
271 | ||
272 | odn = dnentry->dn; | |
273 | while (odn != NULL) { | |
274 | /* adding more events to existing dnofiy_struct? */ | |
275 | if ((odn->dn_owner == id) && (odn->dn_filp == filp)) { | |
276 | odn->dn_fd = fd; | |
277 | odn->dn_mask |= mask; | |
278 | return -EEXIST; | |
279 | } | |
280 | odn = odn->dn_next; | |
281 | } | |
282 | ||
283 | dn->dn_mask = mask; | |
284 | dn->dn_fd = fd; | |
285 | dn->dn_filp = filp; | |
286 | dn->dn_owner = id; | |
287 | dn->dn_next = dnentry->dn; | |
288 | dnentry->dn = dn; | |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
293 | /* | |
294 | * When a process calls fcntl to attach a dnotify watch to a directory it ends | |
295 | * up here. Allocate both a mark for fsnotify to add and a dnotify_struct to be | |
296 | * attached to the fsnotify_mark. | |
297 | */ | |
1da177e4 LT |
298 | int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) |
299 | { | |
3c5119c0 EP |
300 | struct dnotify_mark_entry *new_dnentry, *dnentry; |
301 | struct fsnotify_mark_entry *new_entry, *entry; | |
1da177e4 | 302 | struct dnotify_struct *dn; |
1da177e4 LT |
303 | struct inode *inode; |
304 | fl_owner_t id = current->files; | |
214b7049 | 305 | struct file *f; |
3c5119c0 EP |
306 | int destroy = 0, error = 0; |
307 | __u32 mask; | |
308 | ||
309 | /* we use these to tell if we need to kfree */ | |
310 | new_entry = NULL; | |
311 | dn = NULL; | |
1da177e4 | 312 | |
3c5119c0 EP |
313 | if (!dir_notify_enable) { |
314 | error = -EINVAL; | |
315 | goto out_err; | |
316 | } | |
317 | ||
318 | /* a 0 mask means we are explicitly removing the watch */ | |
1da177e4 LT |
319 | if ((arg & ~DN_MULTISHOT) == 0) { |
320 | dnotify_flush(filp, id); | |
3c5119c0 EP |
321 | error = 0; |
322 | goto out_err; | |
1da177e4 | 323 | } |
3c5119c0 EP |
324 | |
325 | /* dnotify only works on directories */ | |
0f7fc9e4 | 326 | inode = filp->f_path.dentry->d_inode; |
3c5119c0 EP |
327 | if (!S_ISDIR(inode->i_mode)) { |
328 | error = -ENOTDIR; | |
329 | goto out_err; | |
1da177e4 LT |
330 | } |
331 | ||
3c5119c0 EP |
332 | /* expect most fcntl to add new rather than augment old */ |
333 | dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL); | |
334 | if (!dn) { | |
335 | error = -ENOMEM; | |
336 | goto out_err; | |
337 | } | |
214b7049 | 338 | |
3c5119c0 EP |
339 | /* new fsnotify mark, we expect most fcntl calls to add a new mark */ |
340 | new_dnentry = kmem_cache_alloc(dnotify_mark_entry_cache, GFP_KERNEL); | |
341 | if (!new_dnentry) { | |
342 | error = -ENOMEM; | |
343 | goto out_err; | |
344 | } | |
1da177e4 | 345 | |
3c5119c0 EP |
346 | /* convert the userspace DN_* "arg" to the internal FS_* defines in fsnotify */ |
347 | mask = convert_arg(arg); | |
1da177e4 | 348 | |
3c5119c0 EP |
349 | /* set up the new_entry and new_dnentry */ |
350 | new_entry = &new_dnentry->fsn_entry; | |
351 | fsnotify_init_mark(new_entry, dnotify_free_mark); | |
352 | new_entry->mask = mask; | |
353 | new_dnentry->dn = NULL; | |
1da177e4 | 354 | |
3c5119c0 EP |
355 | /* this is needed to prevent the fcntl/close race described below */ |
356 | mutex_lock(&dnotify_mark_mutex); | |
1da177e4 | 357 | |
3c5119c0 | 358 | /* add the new_entry or find an old one. */ |
1da177e4 | 359 | spin_lock(&inode->i_lock); |
3c5119c0 | 360 | entry = fsnotify_find_mark_entry(dnotify_group, inode); |
1da177e4 | 361 | spin_unlock(&inode->i_lock); |
3c5119c0 EP |
362 | if (entry) { |
363 | dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry); | |
364 | spin_lock(&entry->lock); | |
365 | } else { | |
40554c3d | 366 | fsnotify_add_mark(new_entry, dnotify_group, inode, 0); |
3c5119c0 EP |
367 | spin_lock(&new_entry->lock); |
368 | entry = new_entry; | |
369 | dnentry = new_dnentry; | |
370 | /* we used new_entry, so don't free it */ | |
371 | new_entry = NULL; | |
372 | } | |
1da177e4 | 373 | |
3c5119c0 EP |
374 | rcu_read_lock(); |
375 | f = fcheck(fd); | |
376 | rcu_read_unlock(); | |
1da177e4 | 377 | |
3c5119c0 EP |
378 | /* if (f != filp) means that we lost a race and another task/thread |
379 | * actually closed the fd we are still playing with before we grabbed | |
380 | * the dnotify_mark_mutex and entry->lock. Since closing the fd is the | |
381 | * only time we clean up the mark entries we need to get our mark off | |
382 | * the list. */ | |
383 | if (f != filp) { | |
384 | /* if we added ourselves, shoot ourselves, it's possible that | |
385 | * the flush actually did shoot this entry. That's fine too | |
386 | * since multiple calls to destroy_mark is perfectly safe, if | |
387 | * we found a dnentry already attached to the inode, just sod | |
388 | * off silently as the flush at close time dealt with it. | |
389 | */ | |
390 | if (dnentry == new_dnentry) | |
391 | destroy = 1; | |
392 | goto out; | |
393 | } | |
1da177e4 | 394 | |
3c5119c0 EP |
395 | error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); |
396 | if (error) { | |
397 | /* if we added, we must shoot */ | |
398 | if (dnentry == new_dnentry) | |
399 | destroy = 1; | |
400 | goto out; | |
1da177e4 | 401 | } |
3c5119c0 EP |
402 | |
403 | error = attach_dn(dn, dnentry, id, fd, filp, mask); | |
404 | /* !error means that we attached the dn to the dnentry, so don't free it */ | |
405 | if (!error) | |
406 | dn = NULL; | |
407 | /* -EEXIST means that we didn't add this new dn and used an old one. | |
408 | * that isn't an error (and the unused dn should be freed) */ | |
409 | else if (error == -EEXIST) | |
410 | error = 0; | |
411 | ||
412 | dnotify_recalc_inode_mask(entry); | |
413 | out: | |
414 | spin_unlock(&entry->lock); | |
415 | ||
416 | if (destroy) | |
417 | fsnotify_destroy_mark_by_entry(entry); | |
418 | ||
419 | fsnotify_recalc_group_mask(dnotify_group); | |
420 | ||
421 | mutex_unlock(&dnotify_mark_mutex); | |
422 | fsnotify_put_mark(entry); | |
423 | out_err: | |
424 | if (new_entry) | |
425 | fsnotify_put_mark(new_entry); | |
426 | if (dn) | |
427 | kmem_cache_free(dnotify_struct_cache, dn); | |
428 | return error; | |
1da177e4 | 429 | } |
1da177e4 LT |
430 | |
431 | static int __init dnotify_init(void) | |
432 | { | |
3c5119c0 EP |
433 | dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC); |
434 | dnotify_mark_entry_cache = KMEM_CACHE(dnotify_mark_entry, SLAB_PANIC); | |
435 | ||
0d2e2a1d | 436 | dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops); |
3c5119c0 EP |
437 | if (IS_ERR(dnotify_group)) |
438 | panic("unable to allocate fsnotify group for dnotify\n"); | |
1da177e4 LT |
439 | return 0; |
440 | } | |
441 | ||
442 | module_init(dnotify_init) |