[PATCH] inotify (1/5): split kernel API from userspace support
[deliverable/linux.git] / fs / inotify.c
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 * Kernel API added by: Amy Griffis <amy.griffis@hp.com>
9 *
10 * Copyright (C) 2005 John McCutchan
11 * Copyright 2006 Hewlett-Packard Development Company, L.P.
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2, or (at your option) any
16 * later version.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 */
23
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/spinlock.h>
27 #include <linux/idr.h>
28 #include <linux/slab.h>
29 #include <linux/fs.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/writeback.h>
33 #include <linux/inotify.h>
34
35 static atomic_t inotify_cookie;
36
37 /*
38 * Lock ordering:
39 *
40 * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
41 * iprune_mutex (synchronize shrink_icache_memory())
42 * inode_lock (protects the super_block->s_inodes list)
43 * inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)
44 * inotify_handle->mutex (protects inotify_handle and watches->h_list)
45 *
46 * The inode->inotify_mutex and inotify_handle->mutex and held during execution
47 * of a caller's event handler. Thus, the caller must not hold any locks
48 * taken in their event handler while calling any of the published inotify
49 * interfaces.
50 */
51
52 /*
53 * Lifetimes of the three main data structures--inotify_handle, inode, and
54 * inotify_watch--are managed by reference count.
55 *
56 * inotify_handle: Lifetime is from inotify_init() to inotify_destroy().
57 * Additional references can bump the count via get_inotify_handle() and drop
58 * the count via put_inotify_handle().
59 *
60 * inotify_watch: for inotify's purposes, lifetime is from inotify_add_watch()
61 * to remove_watch_no_event(). Additional references can bump the count via
62 * get_inotify_watch() and drop the count via put_inotify_watch(). The caller
63 * is reponsible for the final put after receiving IN_IGNORED, or when using
64 * IN_ONESHOT after receiving the first event. Inotify does the final put if
65 * inotify_destroy() is called.
66 *
67 * inode: Pinned so long as the inode is associated with a watch, from
68 * inotify_add_watch() to the final put_inotify_watch().
69 */
70
71 /*
72 * struct inotify_handle - represents an inotify instance
73 *
74 * This structure is protected by the mutex 'mutex'.
75 */
76 struct inotify_handle {
77 struct idr idr; /* idr mapping wd -> watch */
78 struct mutex mutex; /* protects this bad boy */
79 struct list_head watches; /* list of watches */
80 atomic_t count; /* reference count */
81 u32 last_wd; /* the last wd allocated */
82 const struct inotify_operations *in_ops; /* inotify caller operations */
83 };
84
85 static inline void get_inotify_handle(struct inotify_handle *ih)
86 {
87 atomic_inc(&ih->count);
88 }
89
90 static inline void put_inotify_handle(struct inotify_handle *ih)
91 {
92 if (atomic_dec_and_test(&ih->count)) {
93 idr_destroy(&ih->idr);
94 kfree(ih);
95 }
96 }
97
98 /**
99 * get_inotify_watch - grab a reference to an inotify_watch
100 * @watch: watch to grab
101 */
102 void get_inotify_watch(struct inotify_watch *watch)
103 {
104 atomic_inc(&watch->count);
105 }
106 EXPORT_SYMBOL_GPL(get_inotify_watch);
107
108 /**
109 * put_inotify_watch - decrements the ref count on a given watch. cleans up
110 * watch references if the count reaches zero. inotify_watch is freed by
111 * inotify callers via the destroy_watch() op.
112 * @watch: watch to release
113 */
114 void put_inotify_watch(struct inotify_watch *watch)
115 {
116 if (atomic_dec_and_test(&watch->count)) {
117 struct inotify_handle *ih = watch->ih;
118
119 iput(watch->inode);
120 ih->in_ops->destroy_watch(watch);
121 put_inotify_handle(ih);
122 }
123 }
124 EXPORT_SYMBOL_GPL(put_inotify_watch);
125
126 /*
127 * inotify_handle_get_wd - returns the next WD for use by the given handle
128 *
129 * Callers must hold ih->mutex. This function can sleep.
130 */
131 static int inotify_handle_get_wd(struct inotify_handle *ih,
132 struct inotify_watch *watch)
133 {
134 int ret;
135
136 do {
137 if (unlikely(!idr_pre_get(&ih->idr, GFP_KERNEL)))
138 return -ENOSPC;
139 ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd);
140 } while (ret == -EAGAIN);
141
142 if (likely(!ret))
143 ih->last_wd = watch->wd;
144
145 return ret;
146 }
147
148 /*
149 * inotify_inode_watched - returns nonzero if there are watches on this inode
150 * and zero otherwise. We call this lockless, we do not care if we race.
151 */
152 static inline int inotify_inode_watched(struct inode *inode)
153 {
154 return !list_empty(&inode->inotify_watches);
155 }
156
157 /*
158 * Get child dentry flag into synch with parent inode.
159 * Flag should always be clear for negative dentrys.
160 */
161 static void set_dentry_child_flags(struct inode *inode, int watched)
162 {
163 struct dentry *alias;
164
165 spin_lock(&dcache_lock);
166 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
167 struct dentry *child;
168
169 list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
170 if (!child->d_inode) {
171 WARN_ON(child->d_flags & DCACHE_INOTIFY_PARENT_WATCHED);
172 continue;
173 }
174 spin_lock(&child->d_lock);
175 if (watched) {
176 WARN_ON(child->d_flags &
177 DCACHE_INOTIFY_PARENT_WATCHED);
178 child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
179 } else {
180 WARN_ON(!(child->d_flags &
181 DCACHE_INOTIFY_PARENT_WATCHED));
182 child->d_flags&=~DCACHE_INOTIFY_PARENT_WATCHED;
183 }
184 spin_unlock(&child->d_lock);
185 }
186 }
187 spin_unlock(&dcache_lock);
188 }
189
190 /*
191 * inotify_find_handle - find the watch associated with the given inode and
192 * handle
193 *
194 * Callers must hold inode->inotify_mutex.
195 */
196 static struct inotify_watch *inode_find_handle(struct inode *inode,
197 struct inotify_handle *ih)
198 {
199 struct inotify_watch *watch;
200
201 list_for_each_entry(watch, &inode->inotify_watches, i_list) {
202 if (watch->ih == ih)
203 return watch;
204 }
205
206 return NULL;
207 }
208
209 /*
210 * remove_watch_no_event - remove_watch() without the IN_IGNORED event.
211 *
212 * Callers must hold both inode->inotify_mutex and ih->mutex.
213 */
214 static void remove_watch_no_event(struct inotify_watch *watch,
215 struct inotify_handle *ih)
216 {
217 list_del(&watch->i_list);
218 list_del(&watch->h_list);
219
220 if (!inotify_inode_watched(watch->inode))
221 set_dentry_child_flags(watch->inode, 0);
222
223 idr_remove(&ih->idr, watch->wd);
224 }
225
226 /*
227 * remove_watch - Remove a watch from both the handle and the inode. Sends
228 * the IN_IGNORED event signifying that the inode is no longer watched.
229 *
230 * Callers must hold both inode->inotify_mutex and ih->mutex.
231 */
232 static void remove_watch(struct inotify_watch *watch, struct inotify_handle *ih)
233 {
234 remove_watch_no_event(watch, ih);
235 ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL);
236 }
237
238 /* Kernel API for producing events */
239
240 /*
241 * inotify_d_instantiate - instantiate dcache entry for inode
242 */
243 void inotify_d_instantiate(struct dentry *entry, struct inode *inode)
244 {
245 struct dentry *parent;
246
247 if (!inode)
248 return;
249
250 WARN_ON(entry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED);
251 spin_lock(&entry->d_lock);
252 parent = entry->d_parent;
253 if (parent->d_inode && inotify_inode_watched(parent->d_inode))
254 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
255 spin_unlock(&entry->d_lock);
256 }
257
258 /*
259 * inotify_d_move - dcache entry has been moved
260 */
261 void inotify_d_move(struct dentry *entry)
262 {
263 struct dentry *parent;
264
265 parent = entry->d_parent;
266 if (inotify_inode_watched(parent->d_inode))
267 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
268 else
269 entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
270 }
271
272 /**
273 * inotify_inode_queue_event - queue an event to all watches on this inode
274 * @inode: inode event is originating from
275 * @mask: event mask describing this event
276 * @cookie: cookie for synchronization, or zero
277 * @name: filename, if any
278 */
279 void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
280 const char *name)
281 {
282 struct inotify_watch *watch, *next;
283
284 if (!inotify_inode_watched(inode))
285 return;
286
287 mutex_lock(&inode->inotify_mutex);
288 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
289 u32 watch_mask = watch->mask;
290 if (watch_mask & mask) {
291 struct inotify_handle *ih= watch->ih;
292 mutex_lock(&ih->mutex);
293 if (watch_mask & IN_ONESHOT)
294 remove_watch_no_event(watch, ih);
295 ih->in_ops->handle_event(watch, watch->wd, mask, cookie, name);
296 mutex_unlock(&ih->mutex);
297 }
298 }
299 mutex_unlock(&inode->inotify_mutex);
300 }
301 EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
302
303 /**
304 * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
305 * @dentry: the dentry in question, we queue against this dentry's parent
306 * @mask: event mask describing this event
307 * @cookie: cookie for synchronization, or zero
308 * @name: filename, if any
309 */
310 void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
311 u32 cookie, const char *name)
312 {
313 struct dentry *parent;
314 struct inode *inode;
315
316 if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
317 return;
318
319 spin_lock(&dentry->d_lock);
320 parent = dentry->d_parent;
321 inode = parent->d_inode;
322
323 if (inotify_inode_watched(inode)) {
324 dget(parent);
325 spin_unlock(&dentry->d_lock);
326 inotify_inode_queue_event(inode, mask, cookie, name);
327 dput(parent);
328 } else
329 spin_unlock(&dentry->d_lock);
330 }
331 EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
332
333 /**
334 * inotify_get_cookie - return a unique cookie for use in synchronizing events.
335 */
336 u32 inotify_get_cookie(void)
337 {
338 return atomic_inc_return(&inotify_cookie);
339 }
340 EXPORT_SYMBOL_GPL(inotify_get_cookie);
341
342 /**
343 * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
344 * @list: list of inodes being unmounted (sb->s_inodes)
345 *
346 * Called with inode_lock held, protecting the unmounting super block's list
347 * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
348 * We temporarily drop inode_lock, however, and CAN block.
349 */
350 void inotify_unmount_inodes(struct list_head *list)
351 {
352 struct inode *inode, *next_i, *need_iput = NULL;
353
354 list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
355 struct inotify_watch *watch, *next_w;
356 struct inode *need_iput_tmp;
357 struct list_head *watches;
358
359 /*
360 * If i_count is zero, the inode cannot have any watches and
361 * doing an __iget/iput with MS_ACTIVE clear would actually
362 * evict all inodes with zero i_count from icache which is
363 * unnecessarily violent and may in fact be illegal to do.
364 */
365 if (!atomic_read(&inode->i_count))
366 continue;
367
368 /*
369 * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or
370 * I_WILL_FREE which is fine because by that point the inode
371 * cannot have any associated watches.
372 */
373 if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))
374 continue;
375
376 need_iput_tmp = need_iput;
377 need_iput = NULL;
378 /* In case the remove_watch() drops a reference. */
379 if (inode != need_iput_tmp)
380 __iget(inode);
381 else
382 need_iput_tmp = NULL;
383 /* In case the dropping of a reference would nuke next_i. */
384 if ((&next_i->i_sb_list != list) &&
385 atomic_read(&next_i->i_count) &&
386 !(next_i->i_state & (I_CLEAR | I_FREEING |
387 I_WILL_FREE))) {
388 __iget(next_i);
389 need_iput = next_i;
390 }
391
392 /*
393 * We can safely drop inode_lock here because we hold
394 * references on both inode and next_i. Also no new inodes
395 * will be added since the umount has begun. Finally,
396 * iprune_mutex keeps shrink_icache_memory() away.
397 */
398 spin_unlock(&inode_lock);
399
400 if (need_iput_tmp)
401 iput(need_iput_tmp);
402
403 /* for each watch, send IN_UNMOUNT and then remove it */
404 mutex_lock(&inode->inotify_mutex);
405 watches = &inode->inotify_watches;
406 list_for_each_entry_safe(watch, next_w, watches, i_list) {
407 struct inotify_handle *ih= watch->ih;
408 mutex_lock(&ih->mutex);
409 ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
410 NULL);
411 remove_watch(watch, ih);
412 mutex_unlock(&ih->mutex);
413 }
414 mutex_unlock(&inode->inotify_mutex);
415 iput(inode);
416
417 spin_lock(&inode_lock);
418 }
419 }
420 EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
421
422 /**
423 * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
424 * @inode: inode that is about to be removed
425 */
426 void inotify_inode_is_dead(struct inode *inode)
427 {
428 struct inotify_watch *watch, *next;
429
430 mutex_lock(&inode->inotify_mutex);
431 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
432 struct inotify_handle *ih = watch->ih;
433 mutex_lock(&ih->mutex);
434 remove_watch(watch, ih);
435 mutex_unlock(&ih->mutex);
436 }
437 mutex_unlock(&inode->inotify_mutex);
438 }
439 EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
440
441 /* Kernel Consumer API */
442
443 /**
444 * inotify_init - allocate and initialize an inotify instance
445 * @ops: caller's inotify operations
446 */
447 struct inotify_handle *inotify_init(const struct inotify_operations *ops)
448 {
449 struct inotify_handle *ih;
450
451 ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
452 if (unlikely(!ih))
453 return ERR_PTR(-ENOMEM);
454
455 idr_init(&ih->idr);
456 INIT_LIST_HEAD(&ih->watches);
457 mutex_init(&ih->mutex);
458 ih->last_wd = 0;
459 ih->in_ops = ops;
460 atomic_set(&ih->count, 0);
461 get_inotify_handle(ih);
462
463 return ih;
464 }
465 EXPORT_SYMBOL_GPL(inotify_init);
466
467 /**
468 * inotify_destroy - clean up and destroy an inotify instance
469 * @ih: inotify handle
470 */
471 void inotify_destroy(struct inotify_handle *ih)
472 {
473 /*
474 * Destroy all of the watches for this handle. Unfortunately, not very
475 * pretty. We cannot do a simple iteration over the list, because we
476 * do not know the inode until we iterate to the watch. But we need to
477 * hold inode->inotify_mutex before ih->mutex. The following works.
478 */
479 while (1) {
480 struct inotify_watch *watch;
481 struct list_head *watches;
482 struct inode *inode;
483
484 mutex_lock(&ih->mutex);
485 watches = &ih->watches;
486 if (list_empty(watches)) {
487 mutex_unlock(&ih->mutex);
488 break;
489 }
490 watch = list_entry(watches->next, struct inotify_watch, h_list);
491 get_inotify_watch(watch);
492 mutex_unlock(&ih->mutex);
493
494 inode = watch->inode;
495 mutex_lock(&inode->inotify_mutex);
496 mutex_lock(&ih->mutex);
497
498 /* make sure we didn't race with another list removal */
499 if (likely(idr_find(&ih->idr, watch->wd))) {
500 remove_watch_no_event(watch, ih);
501 put_inotify_watch(watch);
502 }
503
504 mutex_unlock(&ih->mutex);
505 mutex_unlock(&inode->inotify_mutex);
506 put_inotify_watch(watch);
507 }
508
509 /* free this handle: the put matching the get in inotify_init() */
510 put_inotify_handle(ih);
511 }
512 EXPORT_SYMBOL_GPL(inotify_destroy);
513
514 /**
515 * inotify_find_update_watch - find and update the mask of an existing watch
516 * @ih: inotify handle
517 * @inode: inode's watch to update
518 * @mask: mask of events to watch
519 *
520 * Caller must pin given inode (via nameidata).
521 */
522 s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
523 u32 mask)
524 {
525 struct inotify_watch *old;
526 int mask_add = 0;
527 int ret;
528
529 if (mask & IN_MASK_ADD)
530 mask_add = 1;
531
532 /* don't allow invalid bits: we don't want flags set */
533 mask &= IN_ALL_EVENTS | IN_ONESHOT;
534 if (unlikely(!mask))
535 return -EINVAL;
536
537 mutex_lock(&inode->inotify_mutex);
538 mutex_lock(&ih->mutex);
539
540 /*
541 * Handle the case of re-adding a watch on an (inode,ih) pair that we
542 * are already watching. We just update the mask and return its wd.
543 */
544 old = inode_find_handle(inode, ih);
545 if (unlikely(!old)) {
546 ret = -ENOENT;
547 goto out;
548 }
549
550 if (mask_add)
551 old->mask |= mask;
552 else
553 old->mask = mask;
554 ret = old->wd;
555 out:
556 mutex_unlock(&ih->mutex);
557 mutex_unlock(&inode->inotify_mutex);
558 return ret;
559 }
560 EXPORT_SYMBOL_GPL(inotify_find_update_watch);
561
562 /**
563 * inotify_add_watch - add a watch to an inotify instance
564 * @ih: inotify handle
565 * @watch: caller allocated watch structure
566 * @inode: inode to watch
567 * @mask: mask of events to watch
568 *
569 * Caller must pin given inode (via nameidata).
570 * Caller must ensure it only calls inotify_add_watch() once per watch.
571 * Calls inotify_handle_get_wd() so may sleep.
572 */
573 s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,
574 struct inode *inode, u32 mask)
575 {
576 int ret = 0;
577
578 /* don't allow invalid bits: we don't want flags set */
579 mask &= IN_ALL_EVENTS | IN_ONESHOT;
580 if (unlikely(!mask))
581 return -EINVAL;
582 watch->mask = mask;
583
584 mutex_lock(&inode->inotify_mutex);
585 mutex_lock(&ih->mutex);
586
587 /* Initialize a new watch */
588 ret = inotify_handle_get_wd(ih, watch);
589 if (unlikely(ret))
590 goto out;
591 ret = watch->wd;
592
593 atomic_set(&watch->count, 0);
594 INIT_LIST_HEAD(&watch->h_list);
595 INIT_LIST_HEAD(&watch->i_list);
596
597 /* save a reference to handle and bump the count to make it official */
598 get_inotify_handle(ih);
599 watch->ih = ih;
600
601 /*
602 * Save a reference to the inode and bump the ref count to make it
603 * official. We hold a reference to nameidata, which makes this safe.
604 */
605 watch->inode = igrab(inode);
606
607 get_inotify_watch(watch); /* initial get */
608
609 if (!inotify_inode_watched(inode))
610 set_dentry_child_flags(inode, 1);
611
612 /* Add the watch to the handle's and the inode's list */
613 list_add(&watch->h_list, &ih->watches);
614 list_add(&watch->i_list, &inode->inotify_watches);
615 out:
616 mutex_unlock(&ih->mutex);
617 mutex_unlock(&inode->inotify_mutex);
618 return ret;
619 }
620 EXPORT_SYMBOL_GPL(inotify_add_watch);
621
622 /**
623 * inotify_rm_wd - remove a watch from an inotify instance
624 * @ih: inotify handle
625 * @wd: watch descriptor to remove
626 *
627 * Can sleep.
628 */
629 int inotify_rm_wd(struct inotify_handle *ih, u32 wd)
630 {
631 struct inotify_watch *watch;
632 struct inode *inode;
633
634 mutex_lock(&ih->mutex);
635 watch = idr_find(&ih->idr, wd);
636 if (unlikely(!watch)) {
637 mutex_unlock(&ih->mutex);
638 return -EINVAL;
639 }
640 get_inotify_watch(watch);
641 inode = watch->inode;
642 mutex_unlock(&ih->mutex);
643
644 mutex_lock(&inode->inotify_mutex);
645 mutex_lock(&ih->mutex);
646
647 /* make sure that we did not race */
648 if (likely(idr_find(&ih->idr, wd) == watch))
649 remove_watch(watch, ih);
650
651 mutex_unlock(&ih->mutex);
652 mutex_unlock(&inode->inotify_mutex);
653 put_inotify_watch(watch);
654
655 return 0;
656 }
657 EXPORT_SYMBOL_GPL(inotify_rm_wd);
658
659 /*
660 * inotify_setup - core initialization function
661 */
662 static int __init inotify_setup(void)
663 {
664 atomic_set(&inotify_cookie, 0);
665
666 return 0;
667 }
668
669 module_init(inotify_setup);
This page took 0.045722 seconds and 5 git commands to generate.