[PATCH] inotify (2/5): add name's inode to event handler
[deliverable/linux.git] / fs / 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>
0eeca283
RL
30#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/writeback.h>
33#include <linux/inotify.h>
0eeca283
RL
34
35static atomic_t inotify_cookie;
36
0eeca283
RL
37/*
38 * Lock ordering:
39 *
40 * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
f24075bd 41 * iprune_mutex (synchronize shrink_icache_memory())
0eeca283 42 * inode_lock (protects the super_block->s_inodes list)
d4f9af9d 43 * inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)
2d9048e2
AG
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.
0eeca283
RL
50 */
51
52/*
2d9048e2 53 * Lifetimes of the three main data structures--inotify_handle, inode, and
0eeca283
RL
54 * inotify_watch--are managed by reference count.
55 *
2d9048e2
AG
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().
0eeca283 59 *
2d9048e2
AG
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.
0eeca283
RL
66 *
67 * inode: Pinned so long as the inode is associated with a watch, from
2d9048e2 68 * inotify_add_watch() to the final put_inotify_watch().
0eeca283
RL
69 */
70
71/*
2d9048e2 72 * struct inotify_handle - represents an inotify instance
0eeca283 73 *
d4f9af9d 74 * This structure is protected by the mutex 'mutex'.
0eeca283 75 */
2d9048e2 76struct inotify_handle {
0eeca283 77 struct idr idr; /* idr mapping wd -> watch */
d4f9af9d 78 struct mutex mutex; /* protects this bad boy */
0eeca283
RL
79 struct list_head watches; /* list of watches */
80 atomic_t count; /* reference count */
b9c55d29 81 u32 last_wd; /* the last wd allocated */
2d9048e2 82 const struct inotify_operations *in_ops; /* inotify caller operations */
0eeca283
RL
83};
84
2d9048e2 85static inline void get_inotify_handle(struct inotify_handle *ih)
0eeca283 86{
2d9048e2 87 atomic_inc(&ih->count);
0eeca283
RL
88}
89
2d9048e2 90static inline void put_inotify_handle(struct inotify_handle *ih)
0eeca283 91{
2d9048e2
AG
92 if (atomic_dec_and_test(&ih->count)) {
93 idr_destroy(&ih->idr);
94 kfree(ih);
0eeca283
RL
95 }
96}
97
2d9048e2
AG
98/**
99 * get_inotify_watch - grab a reference to an inotify_watch
100 * @watch: watch to grab
101 */
102void get_inotify_watch(struct inotify_watch *watch)
0eeca283
RL
103{
104 atomic_inc(&watch->count);
105}
2d9048e2 106EXPORT_SYMBOL_GPL(get_inotify_watch);
0eeca283 107
2d9048e2 108/**
0eeca283 109 * put_inotify_watch - decrements the ref count on a given watch. cleans up
2d9048e2
AG
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
0eeca283 113 */
2d9048e2 114void put_inotify_watch(struct inotify_watch *watch)
0eeca283
RL
115{
116 if (atomic_dec_and_test(&watch->count)) {
2d9048e2 117 struct inotify_handle *ih = watch->ih;
0eeca283 118
2d9048e2
AG
119 iput(watch->inode);
120 ih->in_ops->destroy_watch(watch);
121 put_inotify_handle(ih);
0eeca283
RL
122 }
123}
2d9048e2 124EXPORT_SYMBOL_GPL(put_inotify_watch);
0eeca283
RL
125
126/*
2d9048e2 127 * inotify_handle_get_wd - returns the next WD for use by the given handle
0eeca283 128 *
2d9048e2 129 * Callers must hold ih->mutex. This function can sleep.
0eeca283 130 */
2d9048e2
AG
131static int inotify_handle_get_wd(struct inotify_handle *ih,
132 struct inotify_watch *watch)
0eeca283
RL
133{
134 int ret;
135
136 do {
2d9048e2 137 if (unlikely(!idr_pre_get(&ih->idr, GFP_KERNEL)))
0eeca283 138 return -ENOSPC;
2d9048e2 139 ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd);
0eeca283
RL
140 } while (ret == -EAGAIN);
141
2d9048e2
AG
142 if (likely(!ret))
143 ih->last_wd = watch->wd;
0eeca283 144
2d9048e2 145 return ret;
0eeca283
RL
146}
147
c32ccd87
NP
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 */
152static 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 */
161static 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
0eeca283 190/*
2d9048e2
AG
191 * inotify_find_handle - find the watch associated with the given inode and
192 * handle
0eeca283 193 *
d4f9af9d 194 * Callers must hold inode->inotify_mutex.
0eeca283 195 */
2d9048e2
AG
196static struct inotify_watch *inode_find_handle(struct inode *inode,
197 struct inotify_handle *ih)
0eeca283
RL
198{
199 struct inotify_watch *watch;
200
201 list_for_each_entry(watch, &inode->inotify_watches, i_list) {
2d9048e2 202 if (watch->ih == ih)
0eeca283
RL
203 return watch;
204 }
205
206 return NULL;
207}
208
209/*
210 * remove_watch_no_event - remove_watch() without the IN_IGNORED event.
2d9048e2
AG
211 *
212 * Callers must hold both inode->inotify_mutex and ih->mutex.
0eeca283
RL
213 */
214static void remove_watch_no_event(struct inotify_watch *watch,
2d9048e2 215 struct inotify_handle *ih)
0eeca283
RL
216{
217 list_del(&watch->i_list);
2d9048e2 218 list_del(&watch->h_list);
0eeca283 219
c32ccd87
NP
220 if (!inotify_inode_watched(watch->inode))
221 set_dentry_child_flags(watch->inode, 0);
222
2d9048e2 223 idr_remove(&ih->idr, watch->wd);
0eeca283
RL
224}
225
226/*
2d9048e2
AG
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.
0eeca283 229 *
2d9048e2 230 * Callers must hold both inode->inotify_mutex and ih->mutex.
0eeca283 231 */
2d9048e2 232static void remove_watch(struct inotify_watch *watch, struct inotify_handle *ih)
0eeca283 233{
2d9048e2 234 remove_watch_no_event(watch, ih);
7c297722 235 ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL);
0eeca283
RL
236}
237
2d9048e2 238/* Kernel API for producing events */
c32ccd87 239
0eeca283 240/*
c32ccd87 241 * inotify_d_instantiate - instantiate dcache entry for inode
0eeca283 242 */
c32ccd87 243void inotify_d_instantiate(struct dentry *entry, struct inode *inode)
0eeca283 244{
c32ccd87
NP
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;
091e881d 253 if (parent->d_inode && inotify_inode_watched(parent->d_inode))
c32ccd87
NP
254 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
255 spin_unlock(&entry->d_lock);
0eeca283
RL
256}
257
c32ccd87
NP
258/*
259 * inotify_d_move - dcache entry has been moved
260 */
261void 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}
0eeca283
RL
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
7c297722 278 * @n_inode: inode associated with name
0eeca283
RL
279 */
280void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
7c297722 281 const char *name, struct inode *n_inode)
0eeca283
RL
282{
283 struct inotify_watch *watch, *next;
284
285 if (!inotify_inode_watched(inode))
286 return;
287
d4f9af9d 288 mutex_lock(&inode->inotify_mutex);
0eeca283
RL
289 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
290 u32 watch_mask = watch->mask;
291 if (watch_mask & mask) {
2d9048e2
AG
292 struct inotify_handle *ih= watch->ih;
293 mutex_lock(&ih->mutex);
0eeca283 294 if (watch_mask & IN_ONESHOT)
2d9048e2 295 remove_watch_no_event(watch, ih);
7c297722
AG
296 ih->in_ops->handle_event(watch, watch->wd, mask, cookie,
297 name, n_inode);
2d9048e2 298 mutex_unlock(&ih->mutex);
0eeca283
RL
299 }
300 }
d4f9af9d 301 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
302}
303EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
304
305/**
306 * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
307 * @dentry: the dentry in question, we queue against this dentry's parent
308 * @mask: event mask describing this event
309 * @cookie: cookie for synchronization, or zero
310 * @name: filename, if any
311 */
312void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
313 u32 cookie, const char *name)
314{
315 struct dentry *parent;
316 struct inode *inode;
317
c32ccd87 318 if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
820249ba
JM
319 return;
320
0eeca283
RL
321 spin_lock(&dentry->d_lock);
322 parent = dentry->d_parent;
323 inode = parent->d_inode;
324
325 if (inotify_inode_watched(inode)) {
326 dget(parent);
327 spin_unlock(&dentry->d_lock);
7c297722
AG
328 inotify_inode_queue_event(inode, mask, cookie, name,
329 dentry->d_inode);
0eeca283
RL
330 dput(parent);
331 } else
332 spin_unlock(&dentry->d_lock);
333}
334EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
335
336/**
337 * inotify_get_cookie - return a unique cookie for use in synchronizing events.
338 */
339u32 inotify_get_cookie(void)
340{
341 return atomic_inc_return(&inotify_cookie);
342}
343EXPORT_SYMBOL_GPL(inotify_get_cookie);
344
345/**
346 * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
347 * @list: list of inodes being unmounted (sb->s_inodes)
348 *
349 * Called with inode_lock held, protecting the unmounting super block's list
f24075bd 350 * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
0eeca283
RL
351 * We temporarily drop inode_lock, however, and CAN block.
352 */
353void inotify_unmount_inodes(struct list_head *list)
354{
355 struct inode *inode, *next_i, *need_iput = NULL;
356
357 list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
358 struct inotify_watch *watch, *next_w;
359 struct inode *need_iput_tmp;
360 struct list_head *watches;
361
362 /*
363 * If i_count is zero, the inode cannot have any watches and
364 * doing an __iget/iput with MS_ACTIVE clear would actually
365 * evict all inodes with zero i_count from icache which is
366 * unnecessarily violent and may in fact be illegal to do.
367 */
368 if (!atomic_read(&inode->i_count))
369 continue;
370
371 /*
372 * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or
373 * I_WILL_FREE which is fine because by that point the inode
374 * cannot have any associated watches.
375 */
376 if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))
377 continue;
378
379 need_iput_tmp = need_iput;
380 need_iput = NULL;
381 /* In case the remove_watch() drops a reference. */
382 if (inode != need_iput_tmp)
383 __iget(inode);
384 else
385 need_iput_tmp = NULL;
386 /* In case the dropping of a reference would nuke next_i. */
387 if ((&next_i->i_sb_list != list) &&
388 atomic_read(&next_i->i_count) &&
389 !(next_i->i_state & (I_CLEAR | I_FREEING |
390 I_WILL_FREE))) {
391 __iget(next_i);
392 need_iput = next_i;
393 }
394
395 /*
396 * We can safely drop inode_lock here because we hold
397 * references on both inode and next_i. Also no new inodes
398 * will be added since the umount has begun. Finally,
f24075bd 399 * iprune_mutex keeps shrink_icache_memory() away.
0eeca283
RL
400 */
401 spin_unlock(&inode_lock);
402
403 if (need_iput_tmp)
404 iput(need_iput_tmp);
405
406 /* for each watch, send IN_UNMOUNT and then remove it */
d4f9af9d 407 mutex_lock(&inode->inotify_mutex);
0eeca283
RL
408 watches = &inode->inotify_watches;
409 list_for_each_entry_safe(watch, next_w, watches, i_list) {
2d9048e2
AG
410 struct inotify_handle *ih= watch->ih;
411 mutex_lock(&ih->mutex);
412 ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
7c297722 413 NULL, NULL);
2d9048e2
AG
414 remove_watch(watch, ih);
415 mutex_unlock(&ih->mutex);
0eeca283 416 }
d4f9af9d 417 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
418 iput(inode);
419
420 spin_lock(&inode_lock);
421 }
422}
423EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
424
425/**
426 * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
427 * @inode: inode that is about to be removed
428 */
429void inotify_inode_is_dead(struct inode *inode)
430{
431 struct inotify_watch *watch, *next;
432
d4f9af9d 433 mutex_lock(&inode->inotify_mutex);
0eeca283 434 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
2d9048e2
AG
435 struct inotify_handle *ih = watch->ih;
436 mutex_lock(&ih->mutex);
437 remove_watch(watch, ih);
438 mutex_unlock(&ih->mutex);
0eeca283 439 }
d4f9af9d 440 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
441}
442EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
443
2d9048e2 444/* Kernel Consumer API */
0eeca283 445
2d9048e2
AG
446/**
447 * inotify_init - allocate and initialize an inotify instance
448 * @ops: caller's inotify operations
449 */
450struct inotify_handle *inotify_init(const struct inotify_operations *ops)
0eeca283 451{
2d9048e2 452 struct inotify_handle *ih;
0eeca283 453
2d9048e2
AG
454 ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
455 if (unlikely(!ih))
456 return ERR_PTR(-ENOMEM);
0eeca283 457
2d9048e2
AG
458 idr_init(&ih->idr);
459 INIT_LIST_HEAD(&ih->watches);
460 mutex_init(&ih->mutex);
461 ih->last_wd = 0;
462 ih->in_ops = ops;
463 atomic_set(&ih->count, 0);
464 get_inotify_handle(ih);
0eeca283 465
2d9048e2 466 return ih;
0eeca283 467}
2d9048e2 468EXPORT_SYMBOL_GPL(inotify_init);
0eeca283 469
2d9048e2
AG
470/**
471 * inotify_destroy - clean up and destroy an inotify instance
472 * @ih: inotify handle
473 */
474void inotify_destroy(struct inotify_handle *ih)
0eeca283 475{
0eeca283 476 /*
2d9048e2 477 * Destroy all of the watches for this handle. Unfortunately, not very
0eeca283
RL
478 * pretty. We cannot do a simple iteration over the list, because we
479 * do not know the inode until we iterate to the watch. But we need to
2d9048e2 480 * hold inode->inotify_mutex before ih->mutex. The following works.
0eeca283
RL
481 */
482 while (1) {
483 struct inotify_watch *watch;
484 struct list_head *watches;
485 struct inode *inode;
486
2d9048e2
AG
487 mutex_lock(&ih->mutex);
488 watches = &ih->watches;
0eeca283 489 if (list_empty(watches)) {
2d9048e2 490 mutex_unlock(&ih->mutex);
0eeca283
RL
491 break;
492 }
2d9048e2 493 watch = list_entry(watches->next, struct inotify_watch, h_list);
0eeca283 494 get_inotify_watch(watch);
2d9048e2 495 mutex_unlock(&ih->mutex);
0eeca283
RL
496
497 inode = watch->inode;
d4f9af9d 498 mutex_lock(&inode->inotify_mutex);
2d9048e2 499 mutex_lock(&ih->mutex);
66055a4e
AG
500
501 /* make sure we didn't race with another list removal */
2d9048e2
AG
502 if (likely(idr_find(&ih->idr, watch->wd))) {
503 remove_watch_no_event(watch, ih);
504 put_inotify_watch(watch);
505 }
66055a4e 506
2d9048e2 507 mutex_unlock(&ih->mutex);
d4f9af9d 508 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
509 put_inotify_watch(watch);
510 }
511
2d9048e2
AG
512 /* free this handle: the put matching the get in inotify_init() */
513 put_inotify_handle(ih);
0eeca283 514}
2d9048e2 515EXPORT_SYMBOL_GPL(inotify_destroy);
0eeca283 516
2d9048e2
AG
517/**
518 * inotify_find_update_watch - find and update the mask of an existing watch
519 * @ih: inotify handle
520 * @inode: inode's watch to update
521 * @mask: mask of events to watch
0eeca283 522 *
2d9048e2 523 * Caller must pin given inode (via nameidata).
0eeca283 524 */
2d9048e2
AG
525s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
526 u32 mask)
0eeca283 527{
2d9048e2
AG
528 struct inotify_watch *old;
529 int mask_add = 0;
530 int ret;
0eeca283 531
2d9048e2
AG
532 if (mask & IN_MASK_ADD)
533 mask_add = 1;
534
535 /* don't allow invalid bits: we don't want flags set */
536 mask &= IN_ALL_EVENTS | IN_ONESHOT;
537 if (unlikely(!mask))
0eeca283 538 return -EINVAL;
0eeca283 539
d4f9af9d 540 mutex_lock(&inode->inotify_mutex);
2d9048e2 541 mutex_lock(&ih->mutex);
0eeca283 542
2d9048e2
AG
543 /*
544 * Handle the case of re-adding a watch on an (inode,ih) pair that we
545 * are already watching. We just update the mask and return its wd.
546 */
547 old = inode_find_handle(inode, ih);
548 if (unlikely(!old)) {
549 ret = -ENOENT;
550 goto out;
0eeca283
RL
551 }
552
2d9048e2
AG
553 if (mask_add)
554 old->mask |= mask;
555 else
556 old->mask = mask;
557 ret = old->wd;
558out:
559 mutex_unlock(&ih->mutex);
560 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
561 return ret;
562}
2d9048e2 563EXPORT_SYMBOL_GPL(inotify_find_update_watch);
0eeca283 564
2d9048e2
AG
565/**
566 * inotify_add_watch - add a watch to an inotify instance
567 * @ih: inotify handle
568 * @watch: caller allocated watch structure
569 * @inode: inode to watch
570 * @mask: mask of events to watch
571 *
572 * Caller must pin given inode (via nameidata).
573 * Caller must ensure it only calls inotify_add_watch() once per watch.
574 * Calls inotify_handle_get_wd() so may sleep.
575 */
576s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,
577 struct inode *inode, u32 mask)
0eeca283 578{
2d9048e2 579 int ret = 0;
0eeca283 580
2d9048e2
AG
581 /* don't allow invalid bits: we don't want flags set */
582 mask &= IN_ALL_EVENTS | IN_ONESHOT;
583 if (unlikely(!mask))
584 return -EINVAL;
585 watch->mask = mask;
783bc29b 586
2d9048e2
AG
587 mutex_lock(&inode->inotify_mutex);
588 mutex_lock(&ih->mutex);
8140a500 589
2d9048e2
AG
590 /* Initialize a new watch */
591 ret = inotify_handle_get_wd(ih, watch);
b680716e 592 if (unlikely(ret))
2d9048e2
AG
593 goto out;
594 ret = watch->wd;
0eeca283 595
2d9048e2
AG
596 atomic_set(&watch->count, 0);
597 INIT_LIST_HEAD(&watch->h_list);
598 INIT_LIST_HEAD(&watch->i_list);
7ea6040b 599
2d9048e2
AG
600 /* save a reference to handle and bump the count to make it official */
601 get_inotify_handle(ih);
602 watch->ih = ih;
0eeca283
RL
603
604 /*
2d9048e2
AG
605 * Save a reference to the inode and bump the ref count to make it
606 * official. We hold a reference to nameidata, which makes this safe.
0eeca283 607 */
2d9048e2 608 watch->inode = igrab(inode);
0eeca283 609
2d9048e2 610 get_inotify_watch(watch); /* initial get */
0eeca283 611
c32ccd87
NP
612 if (!inotify_inode_watched(inode))
613 set_dentry_child_flags(inode, 1);
614
2d9048e2
AG
615 /* Add the watch to the handle's and the inode's list */
616 list_add(&watch->h_list, &ih->watches);
0eeca283 617 list_add(&watch->i_list, &inode->inotify_watches);
0eeca283 618out:
2d9048e2 619 mutex_unlock(&ih->mutex);
d4f9af9d 620 mutex_unlock(&inode->inotify_mutex);
0eeca283
RL
621 return ret;
622}
2d9048e2 623EXPORT_SYMBOL_GPL(inotify_add_watch);
0eeca283 624
2d9048e2
AG
625/**
626 * inotify_rm_wd - remove a watch from an inotify instance
627 * @ih: inotify handle
628 * @wd: watch descriptor to remove
629 *
630 * Can sleep.
631 */
632int inotify_rm_wd(struct inotify_handle *ih, u32 wd)
0eeca283 633{
2d9048e2
AG
634 struct inotify_watch *watch;
635 struct inode *inode;
783bc29b 636
2d9048e2
AG
637 mutex_lock(&ih->mutex);
638 watch = idr_find(&ih->idr, wd);
639 if (unlikely(!watch)) {
640 mutex_unlock(&ih->mutex);
641 return -EINVAL;
783bc29b 642 }
2d9048e2
AG
643 get_inotify_watch(watch);
644 inode = watch->inode;
645 mutex_unlock(&ih->mutex);
783bc29b 646
2d9048e2
AG
647 mutex_lock(&inode->inotify_mutex);
648 mutex_lock(&ih->mutex);
9a556e89 649
2d9048e2
AG
650 /* make sure that we did not race */
651 if (likely(idr_find(&ih->idr, wd) == watch))
652 remove_watch(watch, ih);
0eeca283 653
2d9048e2
AG
654 mutex_unlock(&ih->mutex);
655 mutex_unlock(&inode->inotify_mutex);
656 put_inotify_watch(watch);
0eeca283 657
2d9048e2
AG
658 return 0;
659}
660EXPORT_SYMBOL_GPL(inotify_rm_wd);
0eeca283
RL
661
662/*
2d9048e2 663 * inotify_setup - core initialization function
0eeca283 664 */
b680716e 665static int __init inotify_setup(void)
0eeca283 666{
0eeca283
RL
667 atomic_set(&inotify_cookie, 0);
668
0eeca283
RL
669 return 0;
670}
671
b680716e 672module_init(inotify_setup);
This page took 0.157373 seconds and 5 git commands to generate.