inotify.c 18 KB

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