Commit | Line | Data |
---|---|---|
0eeca283 RL |
1 | /* |
2 | * fs/inotify.c - inode-based file event notifications | |
3 | * | |
4 | * Authors: | |
5 | * John McCutchan <ttb@tentacle.dhs.org> | |
6 | * Robert Love <rml@novell.com> | |
7 | * | |
8 | * Copyright (C) 2005 John McCutchan | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2, or (at your option) any | |
13 | * later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, but | |
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | */ | |
20 | ||
21 | #include <linux/module.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/sched.h> | |
24 | #include <linux/spinlock.h> | |
25 | #include <linux/idr.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/fs.h> | |
28 | #include <linux/file.h> | |
29 | #include <linux/mount.h> | |
30 | #include <linux/namei.h> | |
31 | #include <linux/poll.h> | |
0eeca283 RL |
32 | #include <linux/init.h> |
33 | #include <linux/list.h> | |
34 | #include <linux/writeback.h> | |
35 | #include <linux/inotify.h> | |
36 | ||
37 | #include <asm/ioctls.h> | |
38 | ||
39 | static atomic_t inotify_cookie; | |
40 | ||
41 | static kmem_cache_t *watch_cachep; | |
42 | static kmem_cache_t *event_cachep; | |
43 | ||
44 | static struct vfsmount *inotify_mnt; | |
45 | ||
0399cb08 RL |
46 | /* these are configurable via /proc/sys/fs/inotify/ */ |
47 | int inotify_max_user_instances; | |
0eeca283 RL |
48 | int inotify_max_user_watches; |
49 | int inotify_max_queued_events; | |
50 | ||
51 | /* | |
52 | * Lock ordering: | |
53 | * | |
54 | * dentry->d_lock (used to keep d_move() away from dentry->d_parent) | |
55 | * iprune_sem (synchronize shrink_icache_memory()) | |
56 | * inode_lock (protects the super_block->s_inodes list) | |
57 | * inode->inotify_sem (protects inode->inotify_watches and watches->i_list) | |
58 | * inotify_dev->sem (protects inotify_device and watches->d_list) | |
59 | */ | |
60 | ||
61 | /* | |
62 | * Lifetimes of the three main data structures--inotify_device, inode, and | |
63 | * inotify_watch--are managed by reference count. | |
64 | * | |
b680716e RL |
65 | * inotify_device: Lifetime is from inotify_init() until release. Additional |
66 | * references can bump the count via get_inotify_dev() and drop the count via | |
0eeca283 RL |
67 | * put_inotify_dev(). |
68 | * | |
69 | * inotify_watch: Lifetime is from create_watch() to destory_watch(). | |
70 | * Additional references can bump the count via get_inotify_watch() and drop | |
71 | * the count via put_inotify_watch(). | |
72 | * | |
73 | * inode: Pinned so long as the inode is associated with a watch, from | |
74 | * create_watch() to put_inotify_watch(). | |
75 | */ | |
76 | ||
77 | /* | |
b680716e | 78 | * struct inotify_device - represents an inotify instance |
0eeca283 RL |
79 | * |
80 | * This structure is protected by the semaphore 'sem'. | |
81 | */ | |
82 | struct inotify_device { | |
83 | wait_queue_head_t wq; /* wait queue for i/o */ | |
84 | struct idr idr; /* idr mapping wd -> watch */ | |
85 | struct semaphore sem; /* protects this bad boy */ | |
86 | struct list_head events; /* list of queued events */ | |
87 | struct list_head watches; /* list of watches */ | |
88 | atomic_t count; /* reference count */ | |
89 | struct user_struct *user; /* user who opened this dev */ | |
90 | unsigned int queue_size; /* size of the queue (bytes) */ | |
91 | unsigned int event_count; /* number of pending events */ | |
92 | unsigned int max_events; /* maximum number of events */ | |
b9c55d29 | 93 | u32 last_wd; /* the last wd allocated */ |
0eeca283 RL |
94 | }; |
95 | ||
96 | /* | |
97 | * struct inotify_kernel_event - An inotify event, originating from a watch and | |
98 | * queued for user-space. A list of these is attached to each instance of the | |
99 | * device. In read(), this list is walked and all events that can fit in the | |
100 | * buffer are returned. | |
101 | * | |
102 | * Protected by dev->sem of the device in which we are queued. | |
103 | */ | |
104 | struct inotify_kernel_event { | |
105 | struct inotify_event event; /* the user-space event */ | |
106 | struct list_head list; /* entry in inotify_device's list */ | |
107 | char *name; /* filename, if any */ | |
108 | }; | |
109 | ||
110 | /* | |
111 | * struct inotify_watch - represents a watch request on a specific inode | |
112 | * | |
113 | * d_list is protected by dev->sem of the associated watch->dev. | |
114 | * i_list and mask are protected by inode->inotify_sem of the associated inode. | |
115 | * dev, inode, and wd are never written to once the watch is created. | |
116 | */ | |
117 | struct inotify_watch { | |
118 | struct list_head d_list; /* entry in inotify_device's list */ | |
119 | struct list_head i_list; /* entry in inode's list */ | |
120 | atomic_t count; /* reference count */ | |
121 | struct inotify_device *dev; /* associated device */ | |
122 | struct inode *inode; /* associated inode */ | |
123 | s32 wd; /* watch descriptor */ | |
124 | u32 mask; /* event mask for this watch */ | |
125 | }; | |
126 | ||
0399cb08 RL |
127 | #ifdef CONFIG_SYSCTL |
128 | ||
129 | #include <linux/sysctl.h> | |
130 | ||
131 | static int zero; | |
132 | ||
133 | ctl_table inotify_table[] = { | |
134 | { | |
135 | .ctl_name = INOTIFY_MAX_USER_INSTANCES, | |
136 | .procname = "max_user_instances", | |
137 | .data = &inotify_max_user_instances, | |
138 | .maxlen = sizeof(int), | |
139 | .mode = 0644, | |
140 | .proc_handler = &proc_dointvec_minmax, | |
141 | .strategy = &sysctl_intvec, | |
142 | .extra1 = &zero, | |
143 | }, | |
144 | { | |
145 | .ctl_name = INOTIFY_MAX_USER_WATCHES, | |
146 | .procname = "max_user_watches", | |
147 | .data = &inotify_max_user_watches, | |
148 | .maxlen = sizeof(int), | |
149 | .mode = 0644, | |
150 | .proc_handler = &proc_dointvec_minmax, | |
151 | .strategy = &sysctl_intvec, | |
152 | .extra1 = &zero, | |
153 | }, | |
154 | { | |
155 | .ctl_name = INOTIFY_MAX_QUEUED_EVENTS, | |
156 | .procname = "max_queued_events", | |
157 | .data = &inotify_max_queued_events, | |
158 | .maxlen = sizeof(int), | |
159 | .mode = 0644, | |
160 | .proc_handler = &proc_dointvec_minmax, | |
161 | .strategy = &sysctl_intvec, | |
162 | .extra1 = &zero | |
163 | }, | |
164 | { .ctl_name = 0 } | |
165 | }; | |
166 | #endif /* CONFIG_SYSCTL */ | |
167 | ||
0eeca283 RL |
168 | static inline void get_inotify_dev(struct inotify_device *dev) |
169 | { | |
170 | atomic_inc(&dev->count); | |
171 | } | |
172 | ||
173 | static inline void put_inotify_dev(struct inotify_device *dev) | |
174 | { | |
175 | if (atomic_dec_and_test(&dev->count)) { | |
176 | atomic_dec(&dev->user->inotify_devs); | |
177 | free_uid(dev->user); | |
178 | kfree(dev); | |
179 | } | |
180 | } | |
181 | ||
182 | static inline void get_inotify_watch(struct inotify_watch *watch) | |
183 | { | |
184 | atomic_inc(&watch->count); | |
185 | } | |
186 | ||
187 | /* | |
188 | * put_inotify_watch - decrements the ref count on a given watch. cleans up | |
189 | * the watch and its references if the count reaches zero. | |
190 | */ | |
191 | static inline void put_inotify_watch(struct inotify_watch *watch) | |
192 | { | |
193 | if (atomic_dec_and_test(&watch->count)) { | |
194 | put_inotify_dev(watch->dev); | |
195 | iput(watch->inode); | |
196 | kmem_cache_free(watch_cachep, watch); | |
197 | } | |
198 | } | |
199 | ||
200 | /* | |
201 | * kernel_event - create a new kernel event with the given parameters | |
202 | * | |
203 | * This function can sleep. | |
204 | */ | |
205 | static struct inotify_kernel_event * kernel_event(s32 wd, u32 mask, u32 cookie, | |
206 | const char *name) | |
207 | { | |
208 | struct inotify_kernel_event *kevent; | |
209 | ||
210 | kevent = kmem_cache_alloc(event_cachep, GFP_KERNEL); | |
211 | if (unlikely(!kevent)) | |
212 | return NULL; | |
213 | ||
214 | /* we hand this out to user-space, so zero it just in case */ | |
215 | memset(&kevent->event, 0, sizeof(struct inotify_event)); | |
216 | ||
217 | kevent->event.wd = wd; | |
218 | kevent->event.mask = mask; | |
219 | kevent->event.cookie = cookie; | |
220 | ||
221 | INIT_LIST_HEAD(&kevent->list); | |
222 | ||
223 | if (name) { | |
224 | size_t len, rem, event_size = sizeof(struct inotify_event); | |
225 | ||
226 | /* | |
227 | * We need to pad the filename so as to properly align an | |
228 | * array of inotify_event structures. Because the structure is | |
229 | * small and the common case is a small filename, we just round | |
230 | * up to the next multiple of the structure's sizeof. This is | |
231 | * simple and safe for all architectures. | |
232 | */ | |
233 | len = strlen(name) + 1; | |
234 | rem = event_size - len; | |
235 | if (len > event_size) { | |
236 | rem = event_size - (len % event_size); | |
237 | if (len % event_size == 0) | |
238 | rem = 0; | |
239 | } | |
240 | ||
241 | kevent->name = kmalloc(len + rem, GFP_KERNEL); | |
242 | if (unlikely(!kevent->name)) { | |
243 | kmem_cache_free(event_cachep, kevent); | |
244 | return NULL; | |
245 | } | |
246 | memcpy(kevent->name, name, len); | |
247 | if (rem) | |
248 | memset(kevent->name + len, 0, rem); | |
249 | kevent->event.len = len + rem; | |
250 | } else { | |
251 | kevent->event.len = 0; | |
252 | kevent->name = NULL; | |
253 | } | |
254 | ||
255 | return kevent; | |
256 | } | |
257 | ||
258 | /* | |
259 | * inotify_dev_get_event - return the next event in the given dev's queue | |
260 | * | |
261 | * Caller must hold dev->sem. | |
262 | */ | |
263 | static inline struct inotify_kernel_event * | |
264 | inotify_dev_get_event(struct inotify_device *dev) | |
265 | { | |
266 | return list_entry(dev->events.next, struct inotify_kernel_event, list); | |
267 | } | |
268 | ||
269 | /* | |
270 | * inotify_dev_queue_event - add a new event to the given device | |
271 | * | |
272 | * Caller must hold dev->sem. Can sleep (calls kernel_event()). | |
273 | */ | |
274 | static void inotify_dev_queue_event(struct inotify_device *dev, | |
275 | struct inotify_watch *watch, u32 mask, | |
276 | u32 cookie, const char *name) | |
277 | { | |
278 | struct inotify_kernel_event *kevent, *last; | |
279 | ||
280 | /* coalescing: drop this event if it is a dupe of the previous */ | |
281 | last = inotify_dev_get_event(dev); | |
282 | if (last && last->event.mask == mask && last->event.wd == watch->wd && | |
283 | last->event.cookie == cookie) { | |
284 | const char *lastname = last->name; | |
285 | ||
286 | if (!name && !lastname) | |
287 | return; | |
288 | if (name && lastname && !strcmp(lastname, name)) | |
289 | return; | |
290 | } | |
291 | ||
292 | /* the queue overflowed and we already sent the Q_OVERFLOW event */ | |
293 | if (unlikely(dev->event_count > dev->max_events)) | |
294 | return; | |
295 | ||
296 | /* if the queue overflows, we need to notify user space */ | |
297 | if (unlikely(dev->event_count == dev->max_events)) | |
298 | kevent = kernel_event(-1, IN_Q_OVERFLOW, cookie, NULL); | |
299 | else | |
300 | kevent = kernel_event(watch->wd, mask, cookie, name); | |
301 | ||
302 | if (unlikely(!kevent)) | |
303 | return; | |
304 | ||
305 | /* queue the event and wake up anyone waiting */ | |
306 | dev->event_count++; | |
307 | dev->queue_size += sizeof(struct inotify_event) + kevent->event.len; | |
308 | list_add_tail(&kevent->list, &dev->events); | |
309 | wake_up_interruptible(&dev->wq); | |
310 | } | |
311 | ||
312 | /* | |
313 | * remove_kevent - cleans up and ultimately frees the given kevent | |
314 | * | |
315 | * Caller must hold dev->sem. | |
316 | */ | |
317 | static void remove_kevent(struct inotify_device *dev, | |
318 | struct inotify_kernel_event *kevent) | |
319 | { | |
320 | list_del(&kevent->list); | |
321 | ||
322 | dev->event_count--; | |
323 | dev->queue_size -= sizeof(struct inotify_event) + kevent->event.len; | |
324 | ||
325 | kfree(kevent->name); | |
326 | kmem_cache_free(event_cachep, kevent); | |
327 | } | |
328 | ||
329 | /* | |
330 | * inotify_dev_event_dequeue - destroy an event on the given device | |
331 | * | |
332 | * Caller must hold dev->sem. | |
333 | */ | |
334 | static void inotify_dev_event_dequeue(struct inotify_device *dev) | |
335 | { | |
336 | if (!list_empty(&dev->events)) { | |
337 | struct inotify_kernel_event *kevent; | |
338 | kevent = inotify_dev_get_event(dev); | |
339 | remove_kevent(dev, kevent); | |
340 | } | |
341 | } | |
342 | ||
343 | /* | |
344 | * inotify_dev_get_wd - returns the next WD for use by the given dev | |
345 | * | |
346 | * Callers must hold dev->sem. This function can sleep. | |
347 | */ | |
348 | static int inotify_dev_get_wd(struct inotify_device *dev, | |
349 | struct inotify_watch *watch) | |
350 | { | |
351 | int ret; | |
352 | ||
353 | do { | |
354 | if (unlikely(!idr_pre_get(&dev->idr, GFP_KERNEL))) | |
355 | return -ENOSPC; | |
7c657f2f | 356 | ret = idr_get_new_above(&dev->idr, watch, dev->last_wd+1, &watch->wd); |
0eeca283 RL |
357 | } while (ret == -EAGAIN); |
358 | ||
359 | return ret; | |
360 | } | |
361 | ||
362 | /* | |
363 | * find_inode - resolve a user-given path to a specific inode and return a nd | |
364 | */ | |
365 | static int find_inode(const char __user *dirname, struct nameidata *nd) | |
366 | { | |
367 | int error; | |
368 | ||
369 | error = __user_walk(dirname, LOOKUP_FOLLOW, nd); | |
370 | if (error) | |
371 | return error; | |
372 | /* you can only watch an inode if you have read permissions on it */ | |
373 | error = permission(nd->dentry->d_inode, MAY_READ, NULL); | |
374 | if (error) | |
b680716e | 375 | path_release(nd); |
0eeca283 RL |
376 | return error; |
377 | } | |
378 | ||
379 | /* | |
380 | * create_watch - creates a watch on the given device. | |
381 | * | |
382 | * Callers must hold dev->sem. Calls inotify_dev_get_wd() so may sleep. | |
383 | * Both 'dev' and 'inode' (by way of nameidata) need to be pinned. | |
384 | */ | |
385 | static struct inotify_watch *create_watch(struct inotify_device *dev, | |
386 | u32 mask, struct inode *inode) | |
387 | { | |
388 | struct inotify_watch *watch; | |
389 | int ret; | |
390 | ||
b680716e RL |
391 | if (atomic_read(&dev->user->inotify_watches) >= |
392 | inotify_max_user_watches) | |
0eeca283 RL |
393 | return ERR_PTR(-ENOSPC); |
394 | ||
395 | watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL); | |
396 | if (unlikely(!watch)) | |
397 | return ERR_PTR(-ENOMEM); | |
398 | ||
399 | ret = inotify_dev_get_wd(dev, watch); | |
400 | if (unlikely(ret)) { | |
401 | kmem_cache_free(watch_cachep, watch); | |
402 | return ERR_PTR(ret); | |
403 | } | |
404 | ||
0bf955ce | 405 | dev->last_wd = watch->wd; |
0eeca283 RL |
406 | watch->mask = mask; |
407 | atomic_set(&watch->count, 0); | |
408 | INIT_LIST_HEAD(&watch->d_list); | |
409 | INIT_LIST_HEAD(&watch->i_list); | |
410 | ||
411 | /* save a reference to device and bump the count to make it official */ | |
412 | get_inotify_dev(dev); | |
413 | watch->dev = dev; | |
414 | ||
415 | /* | |
416 | * Save a reference to the inode and bump the ref count to make it | |
417 | * official. We hold a reference to nameidata, which makes this safe. | |
418 | */ | |
419 | watch->inode = igrab(inode); | |
420 | ||
421 | /* bump our own count, corresponding to our entry in dev->watches */ | |
422 | get_inotify_watch(watch); | |
423 | ||
424 | atomic_inc(&dev->user->inotify_watches); | |
425 | ||
426 | return watch; | |
427 | } | |
428 | ||
429 | /* | |
430 | * inotify_find_dev - find the watch associated with the given inode and dev | |
431 | * | |
432 | * Callers must hold inode->inotify_sem. | |
433 | */ | |
434 | static struct inotify_watch *inode_find_dev(struct inode *inode, | |
435 | struct inotify_device *dev) | |
436 | { | |
437 | struct inotify_watch *watch; | |
438 | ||
439 | list_for_each_entry(watch, &inode->inotify_watches, i_list) { | |
440 | if (watch->dev == dev) | |
441 | return watch; | |
442 | } | |
443 | ||
444 | return NULL; | |
445 | } | |
446 | ||
447 | /* | |
448 | * remove_watch_no_event - remove_watch() without the IN_IGNORED event. | |
449 | */ | |
450 | static void remove_watch_no_event(struct inotify_watch *watch, | |
451 | struct inotify_device *dev) | |
452 | { | |
453 | list_del(&watch->i_list); | |
454 | list_del(&watch->d_list); | |
455 | ||
456 | atomic_dec(&dev->user->inotify_watches); | |
457 | idr_remove(&dev->idr, watch->wd); | |
458 | put_inotify_watch(watch); | |
459 | } | |
460 | ||
461 | /* | |
462 | * remove_watch - Remove a watch from both the device and the inode. Sends | |
463 | * the IN_IGNORED event to the given device signifying that the inode is no | |
464 | * longer watched. | |
465 | * | |
466 | * Callers must hold both inode->inotify_sem and dev->sem. We drop a | |
467 | * reference to the inode before returning. | |
468 | * | |
469 | * The inode is not iput() so as to remain atomic. If the inode needs to be | |
470 | * iput(), the call returns one. Otherwise, it returns zero. | |
471 | */ | |
472 | static void remove_watch(struct inotify_watch *watch,struct inotify_device *dev) | |
473 | { | |
474 | inotify_dev_queue_event(dev, watch, IN_IGNORED, 0, NULL); | |
475 | remove_watch_no_event(watch, dev); | |
476 | } | |
477 | ||
478 | /* | |
479 | * inotify_inode_watched - returns nonzero if there are watches on this inode | |
480 | * and zero otherwise. We call this lockless, we do not care if we race. | |
481 | */ | |
482 | static inline int inotify_inode_watched(struct inode *inode) | |
483 | { | |
484 | return !list_empty(&inode->inotify_watches); | |
485 | } | |
486 | ||
487 | /* Kernel API */ | |
488 | ||
489 | /** | |
490 | * inotify_inode_queue_event - queue an event to all watches on this inode | |
491 | * @inode: inode event is originating from | |
492 | * @mask: event mask describing this event | |
493 | * @cookie: cookie for synchronization, or zero | |
494 | * @name: filename, if any | |
495 | */ | |
496 | void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie, | |
497 | const char *name) | |
498 | { | |
499 | struct inotify_watch *watch, *next; | |
500 | ||
501 | if (!inotify_inode_watched(inode)) | |
502 | return; | |
503 | ||
504 | down(&inode->inotify_sem); | |
505 | list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) { | |
506 | u32 watch_mask = watch->mask; | |
507 | if (watch_mask & mask) { | |
508 | struct inotify_device *dev = watch->dev; | |
509 | get_inotify_watch(watch); | |
510 | down(&dev->sem); | |
511 | inotify_dev_queue_event(dev, watch, mask, cookie, name); | |
512 | if (watch_mask & IN_ONESHOT) | |
513 | remove_watch_no_event(watch, dev); | |
514 | up(&dev->sem); | |
515 | put_inotify_watch(watch); | |
516 | } | |
517 | } | |
518 | up(&inode->inotify_sem); | |
519 | } | |
520 | EXPORT_SYMBOL_GPL(inotify_inode_queue_event); | |
521 | ||
522 | /** | |
523 | * inotify_dentry_parent_queue_event - queue an event to a dentry's parent | |
524 | * @dentry: the dentry in question, we queue against this dentry's parent | |
525 | * @mask: event mask describing this event | |
526 | * @cookie: cookie for synchronization, or zero | |
527 | * @name: filename, if any | |
528 | */ | |
529 | void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask, | |
530 | u32 cookie, const char *name) | |
531 | { | |
532 | struct dentry *parent; | |
533 | struct inode *inode; | |
534 | ||
535 | spin_lock(&dentry->d_lock); | |
536 | parent = dentry->d_parent; | |
537 | inode = parent->d_inode; | |
538 | ||
539 | if (inotify_inode_watched(inode)) { | |
540 | dget(parent); | |
541 | spin_unlock(&dentry->d_lock); | |
542 | inotify_inode_queue_event(inode, mask, cookie, name); | |
543 | dput(parent); | |
544 | } else | |
545 | spin_unlock(&dentry->d_lock); | |
546 | } | |
547 | EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event); | |
548 | ||
549 | /** | |
550 | * inotify_get_cookie - return a unique cookie for use in synchronizing events. | |
551 | */ | |
552 | u32 inotify_get_cookie(void) | |
553 | { | |
554 | return atomic_inc_return(&inotify_cookie); | |
555 | } | |
556 | EXPORT_SYMBOL_GPL(inotify_get_cookie); | |
557 | ||
558 | /** | |
559 | * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes. | |
560 | * @list: list of inodes being unmounted (sb->s_inodes) | |
561 | * | |
562 | * Called with inode_lock held, protecting the unmounting super block's list | |
563 | * of inodes, and with iprune_sem held, keeping shrink_icache_memory() at bay. | |
564 | * We temporarily drop inode_lock, however, and CAN block. | |
565 | */ | |
566 | void inotify_unmount_inodes(struct list_head *list) | |
567 | { | |
568 | struct inode *inode, *next_i, *need_iput = NULL; | |
569 | ||
570 | list_for_each_entry_safe(inode, next_i, list, i_sb_list) { | |
571 | struct inotify_watch *watch, *next_w; | |
572 | struct inode *need_iput_tmp; | |
573 | struct list_head *watches; | |
574 | ||
575 | /* | |
576 | * If i_count is zero, the inode cannot have any watches and | |
577 | * doing an __iget/iput with MS_ACTIVE clear would actually | |
578 | * evict all inodes with zero i_count from icache which is | |
579 | * unnecessarily violent and may in fact be illegal to do. | |
580 | */ | |
581 | if (!atomic_read(&inode->i_count)) | |
582 | continue; | |
583 | ||
584 | /* | |
585 | * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or | |
586 | * I_WILL_FREE which is fine because by that point the inode | |
587 | * cannot have any associated watches. | |
588 | */ | |
589 | if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE)) | |
590 | continue; | |
591 | ||
592 | need_iput_tmp = need_iput; | |
593 | need_iput = NULL; | |
594 | /* In case the remove_watch() drops a reference. */ | |
595 | if (inode != need_iput_tmp) | |
596 | __iget(inode); | |
597 | else | |
598 | need_iput_tmp = NULL; | |
599 | /* In case the dropping of a reference would nuke next_i. */ | |
600 | if ((&next_i->i_sb_list != list) && | |
601 | atomic_read(&next_i->i_count) && | |
602 | !(next_i->i_state & (I_CLEAR | I_FREEING | | |
603 | I_WILL_FREE))) { | |
604 | __iget(next_i); | |
605 | need_iput = next_i; | |
606 | } | |
607 | ||
608 | /* | |
609 | * We can safely drop inode_lock here because we hold | |
610 | * references on both inode and next_i. Also no new inodes | |
611 | * will be added since the umount has begun. Finally, | |
612 | * iprune_sem keeps shrink_icache_memory() away. | |
613 | */ | |
614 | spin_unlock(&inode_lock); | |
615 | ||
616 | if (need_iput_tmp) | |
617 | iput(need_iput_tmp); | |
618 | ||
619 | /* for each watch, send IN_UNMOUNT and then remove it */ | |
620 | down(&inode->inotify_sem); | |
621 | watches = &inode->inotify_watches; | |
622 | list_for_each_entry_safe(watch, next_w, watches, i_list) { | |
623 | struct inotify_device *dev = watch->dev; | |
624 | down(&dev->sem); | |
625 | inotify_dev_queue_event(dev, watch, IN_UNMOUNT,0,NULL); | |
626 | remove_watch(watch, dev); | |
627 | up(&dev->sem); | |
628 | } | |
629 | up(&inode->inotify_sem); | |
630 | iput(inode); | |
631 | ||
632 | spin_lock(&inode_lock); | |
633 | } | |
634 | } | |
635 | EXPORT_SYMBOL_GPL(inotify_unmount_inodes); | |
636 | ||
637 | /** | |
638 | * inotify_inode_is_dead - an inode has been deleted, cleanup any watches | |
639 | * @inode: inode that is about to be removed | |
640 | */ | |
641 | void inotify_inode_is_dead(struct inode *inode) | |
642 | { | |
643 | struct inotify_watch *watch, *next; | |
644 | ||
645 | down(&inode->inotify_sem); | |
646 | list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) { | |
647 | struct inotify_device *dev = watch->dev; | |
648 | down(&dev->sem); | |
649 | remove_watch(watch, dev); | |
650 | up(&dev->sem); | |
651 | } | |
652 | up(&inode->inotify_sem); | |
653 | } | |
654 | EXPORT_SYMBOL_GPL(inotify_inode_is_dead); | |
655 | ||
656 | /* Device Interface */ | |
657 | ||
658 | static unsigned int inotify_poll(struct file *file, poll_table *wait) | |
659 | { | |
660 | struct inotify_device *dev = file->private_data; | |
661 | int ret = 0; | |
662 | ||
663 | poll_wait(file, &dev->wq, wait); | |
664 | down(&dev->sem); | |
665 | if (!list_empty(&dev->events)) | |
666 | ret = POLLIN | POLLRDNORM; | |
667 | up(&dev->sem); | |
668 | ||
669 | return ret; | |
670 | } | |
671 | ||
672 | static ssize_t inotify_read(struct file *file, char __user *buf, | |
673 | size_t count, loff_t *pos) | |
674 | { | |
675 | size_t event_size = sizeof (struct inotify_event); | |
676 | struct inotify_device *dev; | |
677 | char __user *start; | |
678 | int ret; | |
679 | DEFINE_WAIT(wait); | |
680 | ||
681 | start = buf; | |
682 | dev = file->private_data; | |
683 | ||
684 | while (1) { | |
685 | int events; | |
686 | ||
687 | prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE); | |
688 | ||
689 | down(&dev->sem); | |
690 | events = !list_empty(&dev->events); | |
691 | up(&dev->sem); | |
692 | if (events) { | |
693 | ret = 0; | |
694 | break; | |
695 | } | |
696 | ||
697 | if (file->f_flags & O_NONBLOCK) { | |
698 | ret = -EAGAIN; | |
699 | break; | |
700 | } | |
701 | ||
702 | if (signal_pending(current)) { | |
703 | ret = -EINTR; | |
704 | break; | |
705 | } | |
706 | ||
707 | schedule(); | |
708 | } | |
709 | ||
710 | finish_wait(&dev->wq, &wait); | |
711 | if (ret) | |
712 | return ret; | |
713 | ||
714 | down(&dev->sem); | |
715 | while (1) { | |
716 | struct inotify_kernel_event *kevent; | |
717 | ||
718 | ret = buf - start; | |
719 | if (list_empty(&dev->events)) | |
720 | break; | |
721 | ||
722 | kevent = inotify_dev_get_event(dev); | |
723 | if (event_size + kevent->event.len > count) | |
724 | break; | |
725 | ||
726 | if (copy_to_user(buf, &kevent->event, event_size)) { | |
727 | ret = -EFAULT; | |
728 | break; | |
729 | } | |
730 | buf += event_size; | |
731 | count -= event_size; | |
732 | ||
733 | if (kevent->name) { | |
734 | if (copy_to_user(buf, kevent->name, kevent->event.len)){ | |
735 | ret = -EFAULT; | |
736 | break; | |
737 | } | |
738 | buf += kevent->event.len; | |
739 | count -= kevent->event.len; | |
740 | } | |
741 | ||
742 | remove_kevent(dev, kevent); | |
743 | } | |
744 | up(&dev->sem); | |
745 | ||
746 | return ret; | |
747 | } | |
748 | ||
749 | static int inotify_release(struct inode *ignored, struct file *file) | |
750 | { | |
751 | struct inotify_device *dev = file->private_data; | |
752 | ||
753 | /* | |
754 | * Destroy all of the watches on this device. Unfortunately, not very | |
755 | * pretty. We cannot do a simple iteration over the list, because we | |
756 | * do not know the inode until we iterate to the watch. But we need to | |
757 | * hold inode->inotify_sem before dev->sem. The following works. | |
758 | */ | |
759 | while (1) { | |
760 | struct inotify_watch *watch; | |
761 | struct list_head *watches; | |
762 | struct inode *inode; | |
763 | ||
764 | down(&dev->sem); | |
765 | watches = &dev->watches; | |
766 | if (list_empty(watches)) { | |
767 | up(&dev->sem); | |
768 | break; | |
769 | } | |
770 | watch = list_entry(watches->next, struct inotify_watch, d_list); | |
771 | get_inotify_watch(watch); | |
772 | up(&dev->sem); | |
773 | ||
774 | inode = watch->inode; | |
775 | down(&inode->inotify_sem); | |
776 | down(&dev->sem); | |
777 | remove_watch_no_event(watch, dev); | |
778 | up(&dev->sem); | |
779 | up(&inode->inotify_sem); | |
780 | put_inotify_watch(watch); | |
781 | } | |
782 | ||
783 | /* destroy all of the events on this device */ | |
784 | down(&dev->sem); | |
785 | while (!list_empty(&dev->events)) | |
786 | inotify_dev_event_dequeue(dev); | |
787 | up(&dev->sem); | |
788 | ||
b680716e | 789 | /* free this device: the put matching the get in inotify_init() */ |
0eeca283 RL |
790 | put_inotify_dev(dev); |
791 | ||
792 | return 0; | |
793 | } | |
794 | ||
795 | /* | |
b680716e | 796 | * inotify_ignore - remove a given wd from this inotify instance. |
0eeca283 RL |
797 | * |
798 | * Can sleep. | |
799 | */ | |
800 | static int inotify_ignore(struct inotify_device *dev, s32 wd) | |
801 | { | |
802 | struct inotify_watch *watch; | |
803 | struct inode *inode; | |
804 | ||
805 | down(&dev->sem); | |
806 | watch = idr_find(&dev->idr, wd); | |
807 | if (unlikely(!watch)) { | |
808 | up(&dev->sem); | |
809 | return -EINVAL; | |
810 | } | |
811 | get_inotify_watch(watch); | |
812 | inode = watch->inode; | |
813 | up(&dev->sem); | |
814 | ||
815 | down(&inode->inotify_sem); | |
816 | down(&dev->sem); | |
817 | ||
818 | /* make sure that we did not race */ | |
819 | watch = idr_find(&dev->idr, wd); | |
820 | if (likely(watch)) | |
821 | remove_watch(watch, dev); | |
822 | ||
823 | up(&dev->sem); | |
824 | up(&inode->inotify_sem); | |
825 | put_inotify_watch(watch); | |
826 | ||
827 | return 0; | |
828 | } | |
829 | ||
830 | static long inotify_ioctl(struct file *file, unsigned int cmd, | |
831 | unsigned long arg) | |
832 | { | |
833 | struct inotify_device *dev; | |
834 | void __user *p; | |
835 | int ret = -ENOTTY; | |
836 | ||
837 | dev = file->private_data; | |
838 | p = (void __user *) arg; | |
839 | ||
840 | switch (cmd) { | |
841 | case FIONREAD: | |
842 | ret = put_user(dev->queue_size, (int __user *) p); | |
843 | break; | |
844 | } | |
845 | ||
846 | return ret; | |
847 | } | |
848 | ||
849 | static struct file_operations inotify_fops = { | |
850 | .poll = inotify_poll, | |
851 | .read = inotify_read, | |
852 | .release = inotify_release, | |
853 | .unlocked_ioctl = inotify_ioctl, | |
854 | .compat_ioctl = inotify_ioctl, | |
855 | }; | |
856 | ||
857 | asmlinkage long sys_inotify_init(void) | |
858 | { | |
859 | struct inotify_device *dev; | |
860 | struct user_struct *user; | |
b680716e RL |
861 | struct file *filp; |
862 | int fd, ret; | |
0eeca283 RL |
863 | |
864 | fd = get_unused_fd(); | |
b680716e RL |
865 | if (fd < 0) |
866 | return fd; | |
0eeca283 RL |
867 | |
868 | filp = get_empty_filp(); | |
869 | if (!filp) { | |
0eeca283 | 870 | ret = -ENFILE; |
5eb22cbc | 871 | goto out_put_fd; |
0eeca283 | 872 | } |
0eeca283 RL |
873 | |
874 | user = get_uid(current->user); | |
b680716e RL |
875 | if (unlikely(atomic_read(&user->inotify_devs) >= |
876 | inotify_max_user_instances)) { | |
0eeca283 | 877 | ret = -EMFILE; |
5eb22cbc | 878 | goto out_free_uid; |
0eeca283 RL |
879 | } |
880 | ||
881 | dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL); | |
882 | if (unlikely(!dev)) { | |
883 | ret = -ENOMEM; | |
5eb22cbc | 884 | goto out_free_uid; |
0eeca283 RL |
885 | } |
886 | ||
b680716e RL |
887 | filp->f_op = &inotify_fops; |
888 | filp->f_vfsmnt = mntget(inotify_mnt); | |
889 | filp->f_dentry = dget(inotify_mnt->mnt_root); | |
890 | filp->f_mapping = filp->f_dentry->d_inode->i_mapping; | |
891 | filp->f_mode = FMODE_READ; | |
892 | filp->f_flags = O_RDONLY; | |
893 | filp->private_data = dev; | |
894 | ||
0eeca283 RL |
895 | idr_init(&dev->idr); |
896 | INIT_LIST_HEAD(&dev->events); | |
897 | INIT_LIST_HEAD(&dev->watches); | |
898 | init_waitqueue_head(&dev->wq); | |
899 | sema_init(&dev->sem, 1); | |
900 | dev->event_count = 0; | |
901 | dev->queue_size = 0; | |
902 | dev->max_events = inotify_max_queued_events; | |
903 | dev->user = user; | |
b9c55d29 | 904 | dev->last_wd = 0; |
0eeca283 RL |
905 | atomic_set(&dev->count, 0); |
906 | ||
907 | get_inotify_dev(dev); | |
908 | atomic_inc(&user->inotify_devs); | |
b680716e | 909 | fd_install(fd, filp); |
0eeca283 | 910 | |
0eeca283 | 911 | return fd; |
5eb22cbc | 912 | out_free_uid: |
0eeca283 | 913 | free_uid(user); |
5eb22cbc RL |
914 | put_filp(filp); |
915 | out_put_fd: | |
916 | put_unused_fd(fd); | |
0eeca283 RL |
917 | return ret; |
918 | } | |
919 | ||
b680716e | 920 | asmlinkage long sys_inotify_add_watch(int fd, const char __user *path, u32 mask) |
0eeca283 RL |
921 | { |
922 | struct inotify_watch *watch, *old; | |
923 | struct inode *inode; | |
924 | struct inotify_device *dev; | |
925 | struct nameidata nd; | |
926 | struct file *filp; | |
33ea2f52 | 927 | int ret, fput_needed; |
0eeca283 | 928 | |
33ea2f52 RL |
929 | filp = fget_light(fd, &fput_needed); |
930 | if (unlikely(!filp)) | |
0eeca283 RL |
931 | return -EBADF; |
932 | ||
783bc29b RL |
933 | /* verify that this is indeed an inotify instance */ |
934 | if (unlikely(filp->f_op != &inotify_fops)) { | |
935 | ret = -EINVAL; | |
936 | goto fput_and_out; | |
937 | } | |
938 | ||
b680716e RL |
939 | ret = find_inode(path, &nd); |
940 | if (unlikely(ret)) | |
0eeca283 RL |
941 | goto fput_and_out; |
942 | ||
b680716e | 943 | /* inode held in place by reference to nd; dev by fget on fd */ |
0eeca283 | 944 | inode = nd.dentry->d_inode; |
b680716e | 945 | dev = filp->private_data; |
0eeca283 RL |
946 | |
947 | down(&inode->inotify_sem); | |
948 | down(&dev->sem); | |
949 | ||
950 | /* don't let user-space set invalid bits: we don't want flags set */ | |
951 | mask &= IN_ALL_EVENTS; | |
b680716e | 952 | if (unlikely(!mask)) { |
0eeca283 RL |
953 | ret = -EINVAL; |
954 | goto out; | |
955 | } | |
956 | ||
957 | /* | |
958 | * Handle the case of re-adding a watch on an (inode,dev) pair that we | |
959 | * are already watching. We just update the mask and return its wd. | |
960 | */ | |
961 | old = inode_find_dev(inode, dev); | |
962 | if (unlikely(old)) { | |
963 | old->mask = mask; | |
964 | ret = old->wd; | |
965 | goto out; | |
966 | } | |
967 | ||
968 | watch = create_watch(dev, mask, inode); | |
969 | if (unlikely(IS_ERR(watch))) { | |
970 | ret = PTR_ERR(watch); | |
971 | goto out; | |
972 | } | |
973 | ||
974 | /* Add the watch to the device's and the inode's list */ | |
975 | list_add(&watch->d_list, &dev->watches); | |
976 | list_add(&watch->i_list, &inode->inotify_watches); | |
977 | ret = watch->wd; | |
978 | out: | |
0eeca283 RL |
979 | up(&dev->sem); |
980 | up(&inode->inotify_sem); | |
5eb22cbc | 981 | path_release(&nd); |
0eeca283 | 982 | fput_and_out: |
33ea2f52 | 983 | fput_light(filp, fput_needed); |
0eeca283 RL |
984 | return ret; |
985 | } | |
986 | ||
987 | asmlinkage long sys_inotify_rm_watch(int fd, u32 wd) | |
988 | { | |
989 | struct file *filp; | |
990 | struct inotify_device *dev; | |
33ea2f52 | 991 | int ret, fput_needed; |
0eeca283 | 992 | |
33ea2f52 RL |
993 | filp = fget_light(fd, &fput_needed); |
994 | if (unlikely(!filp)) | |
0eeca283 | 995 | return -EBADF; |
783bc29b RL |
996 | |
997 | /* verify that this is indeed an inotify instance */ | |
998 | if (unlikely(filp->f_op != &inotify_fops)) { | |
999 | ret = -EINVAL; | |
1000 | goto out; | |
1001 | } | |
1002 | ||
0eeca283 | 1003 | dev = filp->private_data; |
9a556e89 | 1004 | ret = inotify_ignore(dev, wd); |
9a556e89 | 1005 | |
783bc29b RL |
1006 | out: |
1007 | fput_light(filp, fput_needed); | |
0eeca283 RL |
1008 | return ret; |
1009 | } | |
1010 | ||
1011 | static struct super_block * | |
1012 | inotify_get_sb(struct file_system_type *fs_type, int flags, | |
1013 | const char *dev_name, void *data) | |
1014 | { | |
1015 | return get_sb_pseudo(fs_type, "inotify", NULL, 0xBAD1DEA); | |
1016 | } | |
1017 | ||
1018 | static struct file_system_type inotify_fs_type = { | |
1019 | .name = "inotifyfs", | |
1020 | .get_sb = inotify_get_sb, | |
1021 | .kill_sb = kill_anon_super, | |
1022 | }; | |
1023 | ||
1024 | /* | |
b680716e | 1025 | * inotify_setup - Our initialization function. Note that we cannnot return |
0eeca283 RL |
1026 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
1027 | * must result in panic(). | |
1028 | */ | |
b680716e | 1029 | static int __init inotify_setup(void) |
0eeca283 | 1030 | { |
e5ca844a RL |
1031 | int ret; |
1032 | ||
1033 | ret = register_filesystem(&inotify_fs_type); | |
1034 | if (unlikely(ret)) | |
1035 | panic("inotify: register_filesystem returned %d!\n", ret); | |
1036 | ||
0eeca283 | 1037 | inotify_mnt = kern_mount(&inotify_fs_type); |
89373de7 | 1038 | if (IS_ERR(inotify_mnt)) |
e5ca844a | 1039 | panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt)); |
0eeca283 | 1040 | |
1b2ccf0c RL |
1041 | inotify_max_queued_events = 16384; |
1042 | inotify_max_user_instances = 128; | |
0eeca283 RL |
1043 | inotify_max_user_watches = 8192; |
1044 | ||
1045 | atomic_set(&inotify_cookie, 0); | |
1046 | ||
1047 | watch_cachep = kmem_cache_create("inotify_watch_cache", | |
1048 | sizeof(struct inotify_watch), | |
1049 | 0, SLAB_PANIC, NULL, NULL); | |
1050 | event_cachep = kmem_cache_create("inotify_event_cache", | |
1051 | sizeof(struct inotify_kernel_event), | |
1052 | 0, SLAB_PANIC, NULL, NULL); | |
1053 | ||
0eeca283 RL |
1054 | return 0; |
1055 | } | |
1056 | ||
b680716e | 1057 | module_init(inotify_setup); |