]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/notify/inotify/inotify.c
get rid of S_BIAS
[net-next-2.6.git] / fs / notify / inotify / inotify.c
CommitLineData
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 *
2d9048e2
AG
8 * Kernel API added by: Amy Griffis <amy.griffis@hp.com>
9 *
0eeca283 10 * Copyright (C) 2005 John McCutchan
2d9048e2 11 * Copyright 2006 Hewlett-Packard Development Company, L.P.
0eeca283
RL
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>
0eeca283
RL
26#include <linux/spinlock.h>
27#include <linux/idr.h>
28#include <linux/slab.h>
29#include <linux/fs.h>
914e2637 30#include <linux/sched.h>
0eeca283
RL
31#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/writeback.h>
34#include <linux/inotify.h>
90586523 35#include <linux/fsnotify_backend.h>
0eeca283
RL
36
37static atomic_t inotify_cookie;
38
0eeca283
RL
39/*
40 * Lock ordering:
41 *
42 * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
f24075bd 43 * iprune_mutex (synchronize shrink_icache_memory())
0eeca283 44 * inode_lock (protects the super_block->s_inodes list)
d4f9af9d 45 * inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)
2d9048e2
AG
46 * inotify_handle->mutex (protects inotify_handle and watches->h_list)
47 *
48 * The inode->inotify_mutex and inotify_handle->mutex and held during execution
49 * of a caller's event handler. Thus, the caller must not hold any locks
50 * taken in their event handler while calling any of the published inotify
51 * interfaces.
0eeca283
RL
52 */
53
54/*
2d9048e2 55 * Lifetimes of the three main data structures--inotify_handle, inode, and
0eeca283
RL
56 * inotify_watch--are managed by reference count.
57 *
2d9048e2
AG
58 * inotify_handle: Lifetime is from inotify_init() to inotify_destroy().
59 * Additional references can bump the count via get_inotify_handle() and drop
60 * the count via put_inotify_handle().
0eeca283 61 *
2d9048e2
AG
62 * inotify_watch: for inotify's purposes, lifetime is from inotify_add_watch()
63 * to remove_watch_no_event(). Additional references can bump the count via
64 * get_inotify_watch() and drop the count via put_inotify_watch(). The caller
65 * is reponsible for the final put after receiving IN_IGNORED, or when using
66 * IN_ONESHOT after receiving the first event. Inotify does the final put if
67 * inotify_destroy() is called.
0eeca283
RL
68 *
69 * inode: Pinned so long as the inode is associated with a watch, from
2d9048e2 70 * inotify_add_watch() to the final put_inotify_watch().
0eeca283
RL
71 */
72
73/*
2d9048e2 74 * struct inotify_handle - represents an inotify instance
0eeca283 75 *
d4f9af9d 76 * This structure is protected by the mutex 'mutex'.
0eeca283 77 */
2d9048e2 78struct inotify_handle {
0eeca283 79 struct idr idr; /* idr mapping wd -> watch */
d4f9af9d 80 struct mutex mutex; /* protects this bad boy */
0eeca283
RL
81 struct list_head watches; /* list of watches */
82 atomic_t count; /* reference count */
b9c55d29 83 u32 last_wd; /* the last wd allocated */
2d9048e2 84 const struct inotify_operations *in_ops; /* inotify caller operations */
0eeca283
RL
85};
86
2d9048e2 87static inline void get_inotify_handle(struct inotify_handle *ih)
0eeca283 88{
2d9048e2 89 atomic_inc(&ih->count);
0eeca283
RL
90}
91
2d9048e2 92static inline void put_inotify_handle(struct inotify_handle *ih)
0eeca283 93{
2d9048e2
AG
94 if (atomic_dec_and_test(&ih->count)) {
95 idr_destroy(&ih->idr);
96 kfree(ih);
0eeca283
RL
97 }
98}
99
2d9048e2
AG
100/**
101 * get_inotify_watch - grab a reference to an inotify_watch
102 * @watch: watch to grab
103 */
104void get_inotify_watch(struct inotify_watch *watch)
0eeca283
RL
105{
106 atomic_inc(&watch->count);
107}
2d9048e2 108EXPORT_SYMBOL_GPL(get_inotify_watch);
0eeca283 109
8f7b0ba1
AV
110int pin_inotify_watch(struct inotify_watch *watch)
111{
112 struct super_block *sb = watch->inode->i_sb;
b20bd1a5 113 if (atomic_inc_not_zero(&sb->s_active)) {
8f7b0ba1
AV
114 atomic_inc(&watch->count);
115 return 1;
116 }
8f7b0ba1
AV
117 return 0;
118}
119
2d9048e2 120/**
0eeca283 121 * put_inotify_watch - decrements the ref count on a given watch. cleans up
2d9048e2
AG
122 * watch references if the count reaches zero. inotify_watch is freed by
123 * inotify callers via the destroy_watch() op.
124 * @watch: watch to release
0eeca283 125 */
2d9048e2 126void put_inotify_watch(struct inotify_watch *watch)
0eeca283
RL
127{
128 if (atomic_dec_and_test(&watch->count)) {
2d9048e2 129 struct inotify_handle *ih = watch->ih;
0eeca283 130
2d9048e2
AG
131 iput(watch->inode);
132 ih->in_ops->destroy_watch(watch);
133 put_inotify_handle(ih);
0eeca283
RL
134 }
135}
2d9048e2 136EXPORT_SYMBOL_GPL(put_inotify_watch);
0eeca283 137
8f7b0ba1
AV
138void unpin_inotify_watch(struct inotify_watch *watch)
139{
140 struct super_block *sb = watch->inode->i_sb;
141 put_inotify_watch(watch);
142 deactivate_super(sb);
143}
144
0eeca283 145/*
2d9048e2 146 * inotify_handle_get_wd - returns the next WD for use by the given handle
0eeca283 147 *
2d9048e2 148 * Callers must hold ih->mutex. This function can sleep.
0eeca283 149 */
2d9048e2
AG
150static int inotify_handle_get_wd(struct inotify_handle *ih,
151 struct inotify_watch *watch)
0eeca283
RL
152{
153 int ret;
154
155 do {
f04b30de 156 if (unlikely(!idr_pre_get(&ih->idr, GFP_NOFS)))
0eeca283 157 return -ENOSPC;
2d9048e2 158 ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd);
0eeca283
RL
159 } while (ret == -EAGAIN);
160
2d9048e2
AG
161 if (likely(!ret))
162 ih->last_wd = watch->wd;
0eeca283 163
2d9048e2 164 return ret;
0eeca283
RL
165}
166
c32ccd87
NP
167/*
168 * inotify_inode_watched - returns nonzero if there are watches on this inode
169 * and zero otherwise. We call this lockless, we do not care if we race.
170 */
171static inline int inotify_inode_watched(struct inode *inode)
172{
173 return !list_empty(&inode->inotify_watches);
174}
175
176/*
177 * Get child dentry flag into synch with parent inode.
178 * Flag should always be clear for negative dentrys.
179 */
180static void set_dentry_child_flags(struct inode *inode, int watched)
181{
182 struct dentry *alias;
183
184 spin_lock(&dcache_lock);
185 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
186 struct dentry *child;
187
188 list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
0d71bd59 189 if (!child->d_inode)
c32ccd87 190 continue;
0d71bd59 191
c32ccd87 192 spin_lock(&child->d_lock);
0d71bd59 193 if (watched)
c32ccd87 194 child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
0d71bd59
NP
195 else
196 child->d_flags &=~DCACHE_INOTIFY_PARENT_WATCHED;
c32ccd87
NP
197 spin_unlock(&child->d_lock);
198 }
199 }
200 spin_unlock(&dcache_lock);
201}
202
0eeca283 203/*
2d9048e2
AG
204 * inotify_find_handle - find the watch associated with the given inode and
205 * handle
0eeca283 206 *
d4f9af9d 207 * Callers must hold inode->inotify_mutex.
0eeca283 208 */
2d9048e2
AG
209static struct inotify_watch *inode_find_handle(struct inode *inode,
210 struct inotify_handle *ih)
0eeca283
RL
211{
212 struct inotify_watch *watch;
213
214 list_for_each_entry(watch, &inode->inotify_watches, i_list) {
2d9048e2 215 if (watch->ih == ih)
0eeca283
RL
216 return watch;
217 }
218
219 return NULL;
220}
221
222/*
3ca10067 223 * remove_watch_no_event - remove watch without the IN_IGNORED event.
2d9048e2
AG
224 *
225 * Callers must hold both inode->inotify_mutex and ih->mutex.
0eeca283
RL
226 */
227static void remove_watch_no_event(struct inotify_watch *watch,
2d9048e2 228 struct inotify_handle *ih)
0eeca283
RL
229{
230 list_del(&watch->i_list);
2d9048e2 231 list_del(&watch->h_list);
0eeca283 232
c32ccd87
NP
233 if (!inotify_inode_watched(watch->inode))
234 set_dentry_child_flags(watch->inode, 0);
235
2d9048e2 236 idr_remove(&ih->idr, watch->wd);
0eeca283
RL
237}
238
3ca10067
AG
239/**
240 * inotify_remove_watch_locked - Remove a watch from both the handle and the
241 * inode. Sends the IN_IGNORED event signifying that the inode is no longer
242 * watched. May be invoked from a caller's event handler.
243 * @ih: inotify handle associated with watch
244 * @watch: watch to remove
0eeca283 245 *
2d9048e2 246 * Callers must hold both inode->inotify_mutex and ih->mutex.
0eeca283 247 */
3ca10067
AG
248void inotify_remove_watch_locked(struct inotify_handle *ih,
249 struct inotify_watch *watch)
0eeca283 250{
2d9048e2 251 remove_watch_no_event(watch, ih);
7c297722 252 ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL);
0eeca283 253}
3ca10067 254EXPORT_SYMBOL_GPL(inotify_remove_watch_locked);
0eeca283 255
2d9048e2 256/* Kernel API for producing events */
c32ccd87 257
0eeca283 258/*
c32ccd87 259 * inotify_d_instantiate - instantiate dcache entry for inode
0eeca283 260 */
c32ccd87 261void inotify_d_instantiate(struct dentry *entry, struct inode *inode)
0eeca283 262{
c32ccd87
NP
263 struct dentry *parent;
264
265 if (!inode)
266 return;
267
c32ccd87
NP
268 spin_lock(&entry->d_lock);
269 parent = entry->d_parent;
091e881d 270 if (parent->d_inode && inotify_inode_watched(parent->d_inode))
c32ccd87
NP
271 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
272 spin_unlock(&entry->d_lock);
0eeca283
RL
273}
274
c32ccd87
NP
275/*
276 * inotify_d_move - dcache entry has been moved
277 */
278void inotify_d_move(struct dentry *entry)
279{
280 struct dentry *parent;
281
282 parent = entry->d_parent;
283 if (inotify_inode_watched(parent->d_inode))
284 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
285 else
286 entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
287}
0eeca283
RL
288
289/**
290 * inotify_inode_queue_event - queue an event to all watches on this inode
291 * @inode: inode event is originating from
292 * @mask: event mask describing this event
293 * @cookie: cookie for synchronization, or zero
294 * @name: filename, if any
7c297722 295 * @n_inode: inode associated with name
0eeca283
RL
296 */
297void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
7c297722 298 const char *name, struct inode *n_inode)
0eeca283
RL
299{
300 struct inotify_watch *watch, *next;
301
302 if (!inotify_inode_watched(inode))
303 return;
304
d4f9af9d 305 mutex_lock(&inode->inotify_mutex);
0eeca283
RL
306 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
307 u32 watch_mask = watch->mask;
308 if (watch_mask & mask) {
2d9048e2
AG
309 struct inotify_handle *ih= watch->ih;
310 mutex_lock(&ih->mutex);
0eeca283 311 if (watch_mask & IN_ONESHOT)
2d9048e2 312 remove_watch_no_event(watch, ih);
7c297722
AG
313 ih->in_ops->handle_event(watch, watch->wd, mask, cookie,
314 name, n_inode);
2d9048e2 315 mutex_unlock(&ih->mutex);
0eeca283
RL
316 }
317 }
d4f9af9d 318 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
319}
320EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
321
322/**
323 * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
324 * @dentry: the dentry in question, we queue against this dentry's parent
325 * @mask: event mask describing this event
326 * @cookie: cookie for synchronization, or zero
327 * @name: filename, if any
328 */
329void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
330 u32 cookie, const char *name)
331{
332 struct dentry *parent;
333 struct inode *inode;
334
c32ccd87 335 if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
820249ba
JM
336 return;
337
0eeca283
RL
338 spin_lock(&dentry->d_lock);
339 parent = dentry->d_parent;
340 inode = parent->d_inode;
341
342 if (inotify_inode_watched(inode)) {
343 dget(parent);
344 spin_unlock(&dentry->d_lock);
7c297722
AG
345 inotify_inode_queue_event(inode, mask, cookie, name,
346 dentry->d_inode);
0eeca283
RL
347 dput(parent);
348 } else
349 spin_unlock(&dentry->d_lock);
350}
351EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
352
353/**
354 * inotify_get_cookie - return a unique cookie for use in synchronizing events.
355 */
356u32 inotify_get_cookie(void)
357{
358 return atomic_inc_return(&inotify_cookie);
359}
360EXPORT_SYMBOL_GPL(inotify_get_cookie);
361
362/**
363 * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
364 * @list: list of inodes being unmounted (sb->s_inodes)
365 *
366 * Called with inode_lock held, protecting the unmounting super block's list
f24075bd 367 * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
0eeca283
RL
368 * We temporarily drop inode_lock, however, and CAN block.
369 */
370void inotify_unmount_inodes(struct list_head *list)
371{
372 struct inode *inode, *next_i, *need_iput = NULL;
373
374 list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
375 struct inotify_watch *watch, *next_w;
376 struct inode *need_iput_tmp;
377 struct list_head *watches;
378
aabb8fdb
NP
379 /*
380 * We cannot __iget() an inode in state I_CLEAR, I_FREEING,
381 * I_WILL_FREE, or I_NEW which is fine because by that point
382 * the inode cannot have any associated watches.
383 */
384 if (inode->i_state & (I_CLEAR|I_FREEING|I_WILL_FREE|I_NEW))
385 continue;
386
0eeca283
RL
387 /*
388 * If i_count is zero, the inode cannot have any watches and
389 * doing an __iget/iput with MS_ACTIVE clear would actually
390 * evict all inodes with zero i_count from icache which is
391 * unnecessarily violent and may in fact be illegal to do.
392 */
393 if (!atomic_read(&inode->i_count))
394 continue;
395
0eeca283
RL
396 need_iput_tmp = need_iput;
397 need_iput = NULL;
3ca10067 398 /* In case inotify_remove_watch_locked() drops a reference. */
0eeca283
RL
399 if (inode != need_iput_tmp)
400 __iget(inode);
401 else
402 need_iput_tmp = NULL;
403 /* In case the dropping of a reference would nuke next_i. */
404 if ((&next_i->i_sb_list != list) &&
405 atomic_read(&next_i->i_count) &&
406 !(next_i->i_state & (I_CLEAR | I_FREEING |
407 I_WILL_FREE))) {
408 __iget(next_i);
409 need_iput = next_i;
410 }
411
412 /*
413 * We can safely drop inode_lock here because we hold
414 * references on both inode and next_i. Also no new inodes
415 * will be added since the umount has begun. Finally,
f24075bd 416 * iprune_mutex keeps shrink_icache_memory() away.
0eeca283
RL
417 */
418 spin_unlock(&inode_lock);
419
420 if (need_iput_tmp)
421 iput(need_iput_tmp);
422
423 /* for each watch, send IN_UNMOUNT and then remove it */
d4f9af9d 424 mutex_lock(&inode->inotify_mutex);
0eeca283
RL
425 watches = &inode->inotify_watches;
426 list_for_each_entry_safe(watch, next_w, watches, i_list) {
2d9048e2 427 struct inotify_handle *ih= watch->ih;
6ee5a399 428 get_inotify_watch(watch);
2d9048e2
AG
429 mutex_lock(&ih->mutex);
430 ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
7c297722 431 NULL, NULL);
3ca10067 432 inotify_remove_watch_locked(ih, watch);
2d9048e2 433 mutex_unlock(&ih->mutex);
6ee5a399 434 put_inotify_watch(watch);
0eeca283 435 }
d4f9af9d 436 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
437 iput(inode);
438
439 spin_lock(&inode_lock);
440 }
441}
442EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
443
444/**
445 * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
446 * @inode: inode that is about to be removed
447 */
448void inotify_inode_is_dead(struct inode *inode)
449{
450 struct inotify_watch *watch, *next;
451
d4f9af9d 452 mutex_lock(&inode->inotify_mutex);
0eeca283 453 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
2d9048e2
AG
454 struct inotify_handle *ih = watch->ih;
455 mutex_lock(&ih->mutex);
3ca10067 456 inotify_remove_watch_locked(ih, watch);
2d9048e2 457 mutex_unlock(&ih->mutex);
0eeca283 458 }
d4f9af9d 459 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
460}
461EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
462
2d9048e2 463/* Kernel Consumer API */
0eeca283 464
2d9048e2
AG
465/**
466 * inotify_init - allocate and initialize an inotify instance
467 * @ops: caller's inotify operations
468 */
469struct inotify_handle *inotify_init(const struct inotify_operations *ops)
0eeca283 470{
2d9048e2 471 struct inotify_handle *ih;
0eeca283 472
2d9048e2
AG
473 ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
474 if (unlikely(!ih))
475 return ERR_PTR(-ENOMEM);
0eeca283 476
2d9048e2
AG
477 idr_init(&ih->idr);
478 INIT_LIST_HEAD(&ih->watches);
479 mutex_init(&ih->mutex);
480 ih->last_wd = 0;
481 ih->in_ops = ops;
482 atomic_set(&ih->count, 0);
483 get_inotify_handle(ih);
0eeca283 484
2d9048e2 485 return ih;
0eeca283 486}
2d9048e2 487EXPORT_SYMBOL_GPL(inotify_init);
0eeca283 488
a9dc971d
AG
489/**
490 * inotify_init_watch - initialize an inotify watch
491 * @watch: watch to initialize
492 */
493void inotify_init_watch(struct inotify_watch *watch)
494{
495 INIT_LIST_HEAD(&watch->h_list);
496 INIT_LIST_HEAD(&watch->i_list);
497 atomic_set(&watch->count, 0);
498 get_inotify_watch(watch); /* initial get */
499}
500EXPORT_SYMBOL_GPL(inotify_init_watch);
501
8f7b0ba1
AV
502/*
503 * Watch removals suck violently. To kick the watch out we need (in this
504 * order) inode->inotify_mutex and ih->mutex. That's fine if we have
505 * a hold on inode; however, for all other cases we need to make damn sure
506 * we don't race with umount. We can *NOT* just grab a reference to a
507 * watch - inotify_unmount_inodes() will happily sail past it and we'll end
508 * with reference to inode potentially outliving its superblock. Ideally
509 * we just want to grab an active reference to superblock if we can; that
510 * will make sure we won't go into inotify_umount_inodes() until we are
511 * done. Cleanup is just deactivate_super(). However, that leaves a messy
512 * case - what if we *are* racing with umount() and active references to
513 * superblock can't be acquired anymore? We can bump ->s_count, grab
514 * ->s_umount, which will almost certainly wait until the superblock is shut
515 * down and the watch in question is pining for fjords. That's fine, but
516 * there is a problem - we might have hit the window between ->s_active
b20bd1a5
AV
517 * getting to 0 (i.e. the moment when superblock is past the point of no return
518 * and is heading for shutdown) and the moment when deactivate_super() acquires
519 * ->s_umount. We could just do drop_super() yield() and retry, but that's
520 * rather antisocial and this stuff is luser-triggerable. OTOH, having grabbed
521 * ->s_umount and having found that we'd got there first (i.e. that ->s_root is
522 * non-NULL) we know that we won't race with inotify_umount_inodes(). So we
523 * could grab a reference to watch and do the rest as above, just with
524 * drop_super() instead of deactivate_super(), right? Wrong. We had to drop
525 * ih->mutex before we could grab ->s_umount. So the watch could've been gone
526 * already.
8f7b0ba1
AV
527 *
528 * That still can be dealt with - we need to save watch->wd, do idr_find()
529 * and compare its result with our pointer. If they match, we either have
530 * the damn thing still alive or we'd lost not one but two races at once,
531 * the watch had been killed and a new one got created with the same ->wd
532 * at the same address. That couldn't have happened in inotify_destroy(),
533 * but inotify_rm_wd() could run into that. Still, "new one got created"
534 * is not a problem - we have every right to kill it or leave it alone,
535 * whatever's more convenient.
536 *
537 * So we can use idr_find(...) == watch && watch->inode->i_sb == sb as
538 * "grab it and kill it" check. If it's been our original watch, we are
539 * fine, if it's a newcomer - nevermind, just pretend that we'd won the
540 * race and kill the fscker anyway; we are safe since we know that its
541 * superblock won't be going away.
542 *
543 * And yes, this is far beyond mere "not very pretty"; so's the entire
544 * concept of inotify to start with.
545 */
546
547/**
548 * pin_to_kill - pin the watch down for removal
549 * @ih: inotify handle
550 * @watch: watch to kill
551 *
552 * Called with ih->mutex held, drops it. Possible return values:
553 * 0 - nothing to do, it has died
554 * 1 - remove it, drop the reference and deactivate_super()
555 * 2 - remove it, drop the reference and drop_super(); we tried hard to avoid
556 * that variant, since it involved a lot of PITA, but that's the best that
557 * could've been done.
558 */
559static int pin_to_kill(struct inotify_handle *ih, struct inotify_watch *watch)
560{
561 struct super_block *sb = watch->inode->i_sb;
562 s32 wd = watch->wd;
563
b20bd1a5 564 if (atomic_inc_not_zero(&sb->s_active)) {
8f7b0ba1
AV
565 get_inotify_watch(watch);
566 mutex_unlock(&ih->mutex);
567 return 1; /* the best outcome */
568 }
b20bd1a5 569 spin_lock(&sb_lock);
8f7b0ba1
AV
570 sb->s_count++;
571 spin_unlock(&sb_lock);
572 mutex_unlock(&ih->mutex); /* can't grab ->s_umount under it */
573 down_read(&sb->s_umount);
574 if (likely(!sb->s_root)) {
575 /* fs is already shut down; the watch is dead */
576 drop_super(sb);
577 return 0;
578 }
579 /* raced with the final deactivate_super() */
580 mutex_lock(&ih->mutex);
581 if (idr_find(&ih->idr, wd) != watch || watch->inode->i_sb != sb) {
582 /* the watch is dead */
583 mutex_unlock(&ih->mutex);
584 drop_super(sb);
585 return 0;
586 }
587 /* still alive or freed and reused with the same sb and wd; kill */
588 get_inotify_watch(watch);
589 mutex_unlock(&ih->mutex);
590 return 2;
591}
592
593static void unpin_and_kill(struct inotify_watch *watch, int how)
594{
595 struct super_block *sb = watch->inode->i_sb;
596 put_inotify_watch(watch);
597 switch (how) {
598 case 1:
599 deactivate_super(sb);
600 break;
601 case 2:
602 drop_super(sb);
603 }
604}
605
2d9048e2
AG
606/**
607 * inotify_destroy - clean up and destroy an inotify instance
608 * @ih: inotify handle
609 */
610void inotify_destroy(struct inotify_handle *ih)
0eeca283 611{
0eeca283 612 /*
2d9048e2 613 * Destroy all of the watches for this handle. Unfortunately, not very
0eeca283
RL
614 * pretty. We cannot do a simple iteration over the list, because we
615 * do not know the inode until we iterate to the watch. But we need to
2d9048e2 616 * hold inode->inotify_mutex before ih->mutex. The following works.
8f7b0ba1
AV
617 *
618 * AV: it had to become even uglier to start working ;-/
0eeca283
RL
619 */
620 while (1) {
621 struct inotify_watch *watch;
622 struct list_head *watches;
8f7b0ba1 623 struct super_block *sb;
0eeca283 624 struct inode *inode;
8f7b0ba1 625 int how;
0eeca283 626
2d9048e2
AG
627 mutex_lock(&ih->mutex);
628 watches = &ih->watches;
0eeca283 629 if (list_empty(watches)) {
2d9048e2 630 mutex_unlock(&ih->mutex);
0eeca283
RL
631 break;
632 }
b5e61818 633 watch = list_first_entry(watches, struct inotify_watch, h_list);
8f7b0ba1
AV
634 sb = watch->inode->i_sb;
635 how = pin_to_kill(ih, watch);
636 if (!how)
637 continue;
0eeca283
RL
638
639 inode = watch->inode;
d4f9af9d 640 mutex_lock(&inode->inotify_mutex);
2d9048e2 641 mutex_lock(&ih->mutex);
66055a4e
AG
642
643 /* make sure we didn't race with another list removal */
2d9048e2
AG
644 if (likely(idr_find(&ih->idr, watch->wd))) {
645 remove_watch_no_event(watch, ih);
646 put_inotify_watch(watch);
647 }
66055a4e 648
2d9048e2 649 mutex_unlock(&ih->mutex);
d4f9af9d 650 mutex_unlock(&inode->inotify_mutex);
8f7b0ba1 651 unpin_and_kill(watch, how);
0eeca283
RL
652 }
653
2d9048e2
AG
654 /* free this handle: the put matching the get in inotify_init() */
655 put_inotify_handle(ih);
0eeca283 656}
2d9048e2 657EXPORT_SYMBOL_GPL(inotify_destroy);
0eeca283 658
a9dc971d
AG
659/**
660 * inotify_find_watch - find an existing watch for an (ih,inode) pair
661 * @ih: inotify handle
662 * @inode: inode to watch
663 * @watchp: pointer to existing inotify_watch
664 *
665 * Caller must pin given inode (via nameidata).
666 */
667s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,
668 struct inotify_watch **watchp)
669{
670 struct inotify_watch *old;
671 int ret = -ENOENT;
672
673 mutex_lock(&inode->inotify_mutex);
674 mutex_lock(&ih->mutex);
675
676 old = inode_find_handle(inode, ih);
677 if (unlikely(old)) {
678 get_inotify_watch(old); /* caller must put watch */
679 *watchp = old;
680 ret = old->wd;
681 }
682
683 mutex_unlock(&ih->mutex);
684 mutex_unlock(&inode->inotify_mutex);
685
686 return ret;
687}
688EXPORT_SYMBOL_GPL(inotify_find_watch);
689
2d9048e2
AG
690/**
691 * inotify_find_update_watch - find and update the mask of an existing watch
692 * @ih: inotify handle
693 * @inode: inode's watch to update
694 * @mask: mask of events to watch
0eeca283 695 *
2d9048e2 696 * Caller must pin given inode (via nameidata).
0eeca283 697 */
2d9048e2
AG
698s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
699 u32 mask)
0eeca283 700{
2d9048e2
AG
701 struct inotify_watch *old;
702 int mask_add = 0;
703 int ret;
0eeca283 704
2d9048e2
AG
705 if (mask & IN_MASK_ADD)
706 mask_add = 1;
707
708 /* don't allow invalid bits: we don't want flags set */
709 mask &= IN_ALL_EVENTS | IN_ONESHOT;
710 if (unlikely(!mask))
0eeca283 711 return -EINVAL;
0eeca283 712
d4f9af9d 713 mutex_lock(&inode->inotify_mutex);
2d9048e2 714 mutex_lock(&ih->mutex);
0eeca283 715
2d9048e2
AG
716 /*
717 * Handle the case of re-adding a watch on an (inode,ih) pair that we
718 * are already watching. We just update the mask and return its wd.
719 */
720 old = inode_find_handle(inode, ih);
721 if (unlikely(!old)) {
722 ret = -ENOENT;
723 goto out;
0eeca283
RL
724 }
725
2d9048e2
AG
726 if (mask_add)
727 old->mask |= mask;
728 else
729 old->mask = mask;
730 ret = old->wd;
731out:
732 mutex_unlock(&ih->mutex);
733 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
734 return ret;
735}
2d9048e2 736EXPORT_SYMBOL_GPL(inotify_find_update_watch);
0eeca283 737
2d9048e2
AG
738/**
739 * inotify_add_watch - add a watch to an inotify instance
740 * @ih: inotify handle
741 * @watch: caller allocated watch structure
742 * @inode: inode to watch
743 * @mask: mask of events to watch
744 *
745 * Caller must pin given inode (via nameidata).
746 * Caller must ensure it only calls inotify_add_watch() once per watch.
747 * Calls inotify_handle_get_wd() so may sleep.
748 */
749s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,
750 struct inode *inode, u32 mask)
0eeca283 751{
2d9048e2 752 int ret = 0;
d599e36a 753 int newly_watched;
0eeca283 754
2d9048e2
AG
755 /* don't allow invalid bits: we don't want flags set */
756 mask &= IN_ALL_EVENTS | IN_ONESHOT;
757 if (unlikely(!mask))
758 return -EINVAL;
759 watch->mask = mask;
783bc29b 760
2d9048e2
AG
761 mutex_lock(&inode->inotify_mutex);
762 mutex_lock(&ih->mutex);
8140a500 763
2d9048e2
AG
764 /* Initialize a new watch */
765 ret = inotify_handle_get_wd(ih, watch);
b680716e 766 if (unlikely(ret))
2d9048e2
AG
767 goto out;
768 ret = watch->wd;
0eeca283 769
2d9048e2
AG
770 /* save a reference to handle and bump the count to make it official */
771 get_inotify_handle(ih);
772 watch->ih = ih;
0eeca283
RL
773
774 /*
2d9048e2
AG
775 * Save a reference to the inode and bump the ref count to make it
776 * official. We hold a reference to nameidata, which makes this safe.
0eeca283 777 */
2d9048e2 778 watch->inode = igrab(inode);
0eeca283 779
2d9048e2 780 /* Add the watch to the handle's and the inode's list */
d599e36a 781 newly_watched = !inotify_inode_watched(inode);
2d9048e2 782 list_add(&watch->h_list, &ih->watches);
0eeca283 783 list_add(&watch->i_list, &inode->inotify_watches);
d599e36a
NP
784 /*
785 * Set child flags _after_ adding the watch, so there is no race
786 * windows where newly instantiated children could miss their parent's
787 * watched flag.
788 */
789 if (newly_watched)
790 set_dentry_child_flags(inode, 1);
791
0eeca283 792out:
2d9048e2 793 mutex_unlock(&ih->mutex);
d4f9af9d 794 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
795 return ret;
796}
2d9048e2 797EXPORT_SYMBOL_GPL(inotify_add_watch);
0eeca283 798
b9efe8a2
AV
799/**
800 * inotify_clone_watch - put the watch next to existing one
801 * @old: already installed watch
802 * @new: new watch
803 *
804 * Caller must hold the inotify_mutex of inode we are dealing with;
805 * it is expected to remove the old watch before unlocking the inode.
806 */
807s32 inotify_clone_watch(struct inotify_watch *old, struct inotify_watch *new)
808{
809 struct inotify_handle *ih = old->ih;
810 int ret = 0;
811
812 new->mask = old->mask;
813 new->ih = ih;
814
815 mutex_lock(&ih->mutex);
816
817 /* Initialize a new watch */
818 ret = inotify_handle_get_wd(ih, new);
819 if (unlikely(ret))
820 goto out;
821 ret = new->wd;
822
823 get_inotify_handle(ih);
824
825 new->inode = igrab(old->inode);
826
827 list_add(&new->h_list, &ih->watches);
828 list_add(&new->i_list, &old->inode->inotify_watches);
829out:
830 mutex_unlock(&ih->mutex);
831 return ret;
832}
833
455434d4
AV
834void inotify_evict_watch(struct inotify_watch *watch)
835{
836 get_inotify_watch(watch);
837 mutex_lock(&watch->ih->mutex);
838 inotify_remove_watch_locked(watch->ih, watch);
839 mutex_unlock(&watch->ih->mutex);
840}
841
2d9048e2
AG
842/**
843 * inotify_rm_wd - remove a watch from an inotify instance
844 * @ih: inotify handle
845 * @wd: watch descriptor to remove
846 *
847 * Can sleep.
848 */
849int inotify_rm_wd(struct inotify_handle *ih, u32 wd)
0eeca283 850{
2d9048e2 851 struct inotify_watch *watch;
8f7b0ba1 852 struct super_block *sb;
2d9048e2 853 struct inode *inode;
8f7b0ba1 854 int how;
783bc29b 855
2d9048e2
AG
856 mutex_lock(&ih->mutex);
857 watch = idr_find(&ih->idr, wd);
858 if (unlikely(!watch)) {
859 mutex_unlock(&ih->mutex);
860 return -EINVAL;
783bc29b 861 }
8f7b0ba1
AV
862 sb = watch->inode->i_sb;
863 how = pin_to_kill(ih, watch);
864 if (!how)
865 return 0;
866
2d9048e2 867 inode = watch->inode;
783bc29b 868
2d9048e2
AG
869 mutex_lock(&inode->inotify_mutex);
870 mutex_lock(&ih->mutex);
9a556e89 871
2d9048e2
AG
872 /* make sure that we did not race */
873 if (likely(idr_find(&ih->idr, wd) == watch))
3ca10067 874 inotify_remove_watch_locked(ih, watch);
0eeca283 875
2d9048e2
AG
876 mutex_unlock(&ih->mutex);
877 mutex_unlock(&inode->inotify_mutex);
8f7b0ba1 878 unpin_and_kill(watch, how);
0eeca283 879
2d9048e2
AG
880 return 0;
881}
882EXPORT_SYMBOL_GPL(inotify_rm_wd);
0eeca283 883
a9dc971d
AG
884/**
885 * inotify_rm_watch - remove a watch from an inotify instance
886 * @ih: inotify handle
887 * @watch: watch to remove
888 *
889 * Can sleep.
890 */
891int inotify_rm_watch(struct inotify_handle *ih,
892 struct inotify_watch *watch)
893{
894 return inotify_rm_wd(ih, watch->wd);
895}
896EXPORT_SYMBOL_GPL(inotify_rm_watch);
897
0eeca283 898/*
2d9048e2 899 * inotify_setup - core initialization function
0eeca283 900 */
b680716e 901static int __init inotify_setup(void)
0eeca283 902{
90586523
EP
903 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
904 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
905 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
906 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
907 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
908 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
909 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
910 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
911 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
912 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
913 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
914 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
915 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
916
917 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
918 BUILD_BUG_ON(IN_ISDIR != FS_IN_ISDIR);
919 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
920 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
921
0eeca283
RL
922 atomic_set(&inotify_cookie, 0);
923
0eeca283
RL
924 return 0;
925}
926
b680716e 927module_init(inotify_setup);