inotify.c 20 KB

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