inotify.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. WARN_ON(child->d_flags & DCACHE_INOTIFY_PARENT_WATCHED);
  155. continue;
  156. }
  157. spin_lock(&child->d_lock);
  158. if (watched) {
  159. WARN_ON(child->d_flags &
  160. DCACHE_INOTIFY_PARENT_WATCHED);
  161. child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
  162. } else {
  163. WARN_ON(!(child->d_flags &
  164. DCACHE_INOTIFY_PARENT_WATCHED));
  165. child->d_flags&=~DCACHE_INOTIFY_PARENT_WATCHED;
  166. }
  167. spin_unlock(&child->d_lock);
  168. }
  169. }
  170. spin_unlock(&dcache_lock);
  171. }
  172. /*
  173. * inotify_find_handle - find the watch associated with the given inode and
  174. * handle
  175. *
  176. * Callers must hold inode->inotify_mutex.
  177. */
  178. static struct inotify_watch *inode_find_handle(struct inode *inode,
  179. struct inotify_handle *ih)
  180. {
  181. struct inotify_watch *watch;
  182. list_for_each_entry(watch, &inode->inotify_watches, i_list) {
  183. if (watch->ih == ih)
  184. return watch;
  185. }
  186. return NULL;
  187. }
  188. /*
  189. * remove_watch_no_event - remove watch without the IN_IGNORED event.
  190. *
  191. * Callers must hold both inode->inotify_mutex and ih->mutex.
  192. */
  193. static void remove_watch_no_event(struct inotify_watch *watch,
  194. struct inotify_handle *ih)
  195. {
  196. list_del(&watch->i_list);
  197. list_del(&watch->h_list);
  198. if (!inotify_inode_watched(watch->inode))
  199. set_dentry_child_flags(watch->inode, 0);
  200. idr_remove(&ih->idr, watch->wd);
  201. }
  202. /**
  203. * inotify_remove_watch_locked - Remove a watch from both the handle and the
  204. * inode. Sends the IN_IGNORED event signifying that the inode is no longer
  205. * watched. May be invoked from a caller's event handler.
  206. * @ih: inotify handle associated with watch
  207. * @watch: watch to remove
  208. *
  209. * Callers must hold both inode->inotify_mutex and ih->mutex.
  210. */
  211. void inotify_remove_watch_locked(struct inotify_handle *ih,
  212. struct inotify_watch *watch)
  213. {
  214. remove_watch_no_event(watch, ih);
  215. ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL);
  216. }
  217. EXPORT_SYMBOL_GPL(inotify_remove_watch_locked);
  218. /* Kernel API for producing events */
  219. /*
  220. * inotify_d_instantiate - instantiate dcache entry for inode
  221. */
  222. void inotify_d_instantiate(struct dentry *entry, struct inode *inode)
  223. {
  224. struct dentry *parent;
  225. if (!inode)
  226. return;
  227. WARN_ON(entry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED);
  228. spin_lock(&entry->d_lock);
  229. parent = entry->d_parent;
  230. if (parent->d_inode && inotify_inode_watched(parent->d_inode))
  231. entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
  232. spin_unlock(&entry->d_lock);
  233. }
  234. /*
  235. * inotify_d_move - dcache entry has been moved
  236. */
  237. void inotify_d_move(struct dentry *entry)
  238. {
  239. struct dentry *parent;
  240. parent = entry->d_parent;
  241. if (inotify_inode_watched(parent->d_inode))
  242. entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
  243. else
  244. entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
  245. }
  246. /**
  247. * inotify_inode_queue_event - queue an event to all watches on this inode
  248. * @inode: inode event is originating from
  249. * @mask: event mask describing this event
  250. * @cookie: cookie for synchronization, or zero
  251. * @name: filename, if any
  252. * @n_inode: inode associated with name
  253. */
  254. void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
  255. const char *name, struct inode *n_inode)
  256. {
  257. struct inotify_watch *watch, *next;
  258. if (!inotify_inode_watched(inode))
  259. return;
  260. mutex_lock(&inode->inotify_mutex);
  261. list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
  262. u32 watch_mask = watch->mask;
  263. if (watch_mask & mask) {
  264. struct inotify_handle *ih= watch->ih;
  265. mutex_lock(&ih->mutex);
  266. if (watch_mask & IN_ONESHOT)
  267. remove_watch_no_event(watch, ih);
  268. ih->in_ops->handle_event(watch, watch->wd, mask, cookie,
  269. name, n_inode);
  270. mutex_unlock(&ih->mutex);
  271. }
  272. }
  273. mutex_unlock(&inode->inotify_mutex);
  274. }
  275. EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
  276. /**
  277. * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
  278. * @dentry: the dentry in question, we queue against this dentry's parent
  279. * @mask: event mask describing this event
  280. * @cookie: cookie for synchronization, or zero
  281. * @name: filename, if any
  282. */
  283. void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
  284. u32 cookie, const char *name)
  285. {
  286. struct dentry *parent;
  287. struct inode *inode;
  288. if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
  289. return;
  290. spin_lock(&dentry->d_lock);
  291. parent = dentry->d_parent;
  292. inode = parent->d_inode;
  293. if (inotify_inode_watched(inode)) {
  294. dget(parent);
  295. spin_unlock(&dentry->d_lock);
  296. inotify_inode_queue_event(inode, mask, cookie, name,
  297. dentry->d_inode);
  298. dput(parent);
  299. } else
  300. spin_unlock(&dentry->d_lock);
  301. }
  302. EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
  303. /**
  304. * inotify_get_cookie - return a unique cookie for use in synchronizing events.
  305. */
  306. u32 inotify_get_cookie(void)
  307. {
  308. return atomic_inc_return(&inotify_cookie);
  309. }
  310. EXPORT_SYMBOL_GPL(inotify_get_cookie);
  311. /**
  312. * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  313. * @list: list of inodes being unmounted (sb->s_inodes)
  314. *
  315. * Called with inode_lock held, protecting the unmounting super block's list
  316. * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
  317. * We temporarily drop inode_lock, however, and CAN block.
  318. */
  319. void inotify_unmount_inodes(struct list_head *list)
  320. {
  321. struct inode *inode, *next_i, *need_iput = NULL;
  322. list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
  323. struct inotify_watch *watch, *next_w;
  324. struct inode *need_iput_tmp;
  325. struct list_head *watches;
  326. /*
  327. * If i_count is zero, the inode cannot have any watches and
  328. * doing an __iget/iput with MS_ACTIVE clear would actually
  329. * evict all inodes with zero i_count from icache which is
  330. * unnecessarily violent and may in fact be illegal to do.
  331. */
  332. if (!atomic_read(&inode->i_count))
  333. continue;
  334. /*
  335. * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or
  336. * I_WILL_FREE which is fine because by that point the inode
  337. * cannot have any associated watches.
  338. */
  339. if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))
  340. continue;
  341. need_iput_tmp = need_iput;
  342. need_iput = NULL;
  343. /* In case inotify_remove_watch_locked() drops a reference. */
  344. if (inode != need_iput_tmp)
  345. __iget(inode);
  346. else
  347. need_iput_tmp = NULL;
  348. /* In case the dropping of a reference would nuke next_i. */
  349. if ((&next_i->i_sb_list != list) &&
  350. atomic_read(&next_i->i_count) &&
  351. !(next_i->i_state & (I_CLEAR | I_FREEING |
  352. I_WILL_FREE))) {
  353. __iget(next_i);
  354. need_iput = next_i;
  355. }
  356. /*
  357. * We can safely drop inode_lock here because we hold
  358. * references on both inode and next_i. Also no new inodes
  359. * will be added since the umount has begun. Finally,
  360. * iprune_mutex keeps shrink_icache_memory() away.
  361. */
  362. spin_unlock(&inode_lock);
  363. if (need_iput_tmp)
  364. iput(need_iput_tmp);
  365. /* for each watch, send IN_UNMOUNT and then remove it */
  366. mutex_lock(&inode->inotify_mutex);
  367. watches = &inode->inotify_watches;
  368. list_for_each_entry_safe(watch, next_w, watches, i_list) {
  369. struct inotify_handle *ih= watch->ih;
  370. mutex_lock(&ih->mutex);
  371. ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
  372. NULL, NULL);
  373. inotify_remove_watch_locked(ih, watch);
  374. mutex_unlock(&ih->mutex);
  375. }
  376. mutex_unlock(&inode->inotify_mutex);
  377. iput(inode);
  378. spin_lock(&inode_lock);
  379. }
  380. }
  381. EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
  382. /**
  383. * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
  384. * @inode: inode that is about to be removed
  385. */
  386. void inotify_inode_is_dead(struct inode *inode)
  387. {
  388. struct inotify_watch *watch, *next;
  389. mutex_lock(&inode->inotify_mutex);
  390. list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
  391. struct inotify_handle *ih = watch->ih;
  392. mutex_lock(&ih->mutex);
  393. inotify_remove_watch_locked(ih, watch);
  394. mutex_unlock(&ih->mutex);
  395. }
  396. mutex_unlock(&inode->inotify_mutex);
  397. }
  398. EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
  399. /* Kernel Consumer API */
  400. /**
  401. * inotify_init - allocate and initialize an inotify instance
  402. * @ops: caller's inotify operations
  403. */
  404. struct inotify_handle *inotify_init(const struct inotify_operations *ops)
  405. {
  406. struct inotify_handle *ih;
  407. ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
  408. if (unlikely(!ih))
  409. return ERR_PTR(-ENOMEM);
  410. idr_init(&ih->idr);
  411. INIT_LIST_HEAD(&ih->watches);
  412. mutex_init(&ih->mutex);
  413. ih->last_wd = 0;
  414. ih->in_ops = ops;
  415. atomic_set(&ih->count, 0);
  416. get_inotify_handle(ih);
  417. return ih;
  418. }
  419. EXPORT_SYMBOL_GPL(inotify_init);
  420. /**
  421. * inotify_init_watch - initialize an inotify watch
  422. * @watch: watch to initialize
  423. */
  424. void inotify_init_watch(struct inotify_watch *watch)
  425. {
  426. INIT_LIST_HEAD(&watch->h_list);
  427. INIT_LIST_HEAD(&watch->i_list);
  428. atomic_set(&watch->count, 0);
  429. get_inotify_watch(watch); /* initial get */
  430. }
  431. EXPORT_SYMBOL_GPL(inotify_init_watch);
  432. /**
  433. * inotify_destroy - clean up and destroy an inotify instance
  434. * @ih: inotify handle
  435. */
  436. void inotify_destroy(struct inotify_handle *ih)
  437. {
  438. /*
  439. * Destroy all of the watches for this handle. Unfortunately, not very
  440. * pretty. We cannot do a simple iteration over the list, because we
  441. * do not know the inode until we iterate to the watch. But we need to
  442. * hold inode->inotify_mutex before ih->mutex. The following works.
  443. */
  444. while (1) {
  445. struct inotify_watch *watch;
  446. struct list_head *watches;
  447. struct inode *inode;
  448. mutex_lock(&ih->mutex);
  449. watches = &ih->watches;
  450. if (list_empty(watches)) {
  451. mutex_unlock(&ih->mutex);
  452. break;
  453. }
  454. watch = list_entry(watches->next, struct inotify_watch, h_list);
  455. get_inotify_watch(watch);
  456. mutex_unlock(&ih->mutex);
  457. inode = watch->inode;
  458. mutex_lock(&inode->inotify_mutex);
  459. mutex_lock(&ih->mutex);
  460. /* make sure we didn't race with another list removal */
  461. if (likely(idr_find(&ih->idr, watch->wd))) {
  462. remove_watch_no_event(watch, ih);
  463. put_inotify_watch(watch);
  464. }
  465. mutex_unlock(&ih->mutex);
  466. mutex_unlock(&inode->inotify_mutex);
  467. put_inotify_watch(watch);
  468. }
  469. /* free this handle: the put matching the get in inotify_init() */
  470. put_inotify_handle(ih);
  471. }
  472. EXPORT_SYMBOL_GPL(inotify_destroy);
  473. /**
  474. * inotify_find_watch - find an existing watch for an (ih,inode) pair
  475. * @ih: inotify handle
  476. * @inode: inode to watch
  477. * @watchp: pointer to existing inotify_watch
  478. *
  479. * Caller must pin given inode (via nameidata).
  480. */
  481. s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,
  482. struct inotify_watch **watchp)
  483. {
  484. struct inotify_watch *old;
  485. int ret = -ENOENT;
  486. mutex_lock(&inode->inotify_mutex);
  487. mutex_lock(&ih->mutex);
  488. old = inode_find_handle(inode, ih);
  489. if (unlikely(old)) {
  490. get_inotify_watch(old); /* caller must put watch */
  491. *watchp = old;
  492. ret = old->wd;
  493. }
  494. mutex_unlock(&ih->mutex);
  495. mutex_unlock(&inode->inotify_mutex);
  496. return ret;
  497. }
  498. EXPORT_SYMBOL_GPL(inotify_find_watch);
  499. /**
  500. * inotify_find_update_watch - find and update the mask of an existing watch
  501. * @ih: inotify handle
  502. * @inode: inode's watch to update
  503. * @mask: mask of events to watch
  504. *
  505. * Caller must pin given inode (via nameidata).
  506. */
  507. s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
  508. u32 mask)
  509. {
  510. struct inotify_watch *old;
  511. int mask_add = 0;
  512. int ret;
  513. if (mask & IN_MASK_ADD)
  514. mask_add = 1;
  515. /* don't allow invalid bits: we don't want flags set */
  516. mask &= IN_ALL_EVENTS | IN_ONESHOT;
  517. if (unlikely(!mask))
  518. return -EINVAL;
  519. mutex_lock(&inode->inotify_mutex);
  520. mutex_lock(&ih->mutex);
  521. /*
  522. * Handle the case of re-adding a watch on an (inode,ih) pair that we
  523. * are already watching. We just update the mask and return its wd.
  524. */
  525. old = inode_find_handle(inode, ih);
  526. if (unlikely(!old)) {
  527. ret = -ENOENT;
  528. goto out;
  529. }
  530. if (mask_add)
  531. old->mask |= mask;
  532. else
  533. old->mask = mask;
  534. ret = old->wd;
  535. out:
  536. mutex_unlock(&ih->mutex);
  537. mutex_unlock(&inode->inotify_mutex);
  538. return ret;
  539. }
  540. EXPORT_SYMBOL_GPL(inotify_find_update_watch);
  541. /**
  542. * inotify_add_watch - add a watch to an inotify instance
  543. * @ih: inotify handle
  544. * @watch: caller allocated watch structure
  545. * @inode: inode to watch
  546. * @mask: mask of events to watch
  547. *
  548. * Caller must pin given inode (via nameidata).
  549. * Caller must ensure it only calls inotify_add_watch() once per watch.
  550. * Calls inotify_handle_get_wd() so may sleep.
  551. */
  552. s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,
  553. struct inode *inode, u32 mask)
  554. {
  555. int ret = 0;
  556. /* don't allow invalid bits: we don't want flags set */
  557. mask &= IN_ALL_EVENTS | IN_ONESHOT;
  558. if (unlikely(!mask))
  559. return -EINVAL;
  560. watch->mask = mask;
  561. mutex_lock(&inode->inotify_mutex);
  562. mutex_lock(&ih->mutex);
  563. /* Initialize a new watch */
  564. ret = inotify_handle_get_wd(ih, watch);
  565. if (unlikely(ret))
  566. goto out;
  567. ret = watch->wd;
  568. /* save a reference to handle and bump the count to make it official */
  569. get_inotify_handle(ih);
  570. watch->ih = ih;
  571. /*
  572. * Save a reference to the inode and bump the ref count to make it
  573. * official. We hold a reference to nameidata, which makes this safe.
  574. */
  575. watch->inode = igrab(inode);
  576. if (!inotify_inode_watched(inode))
  577. set_dentry_child_flags(inode, 1);
  578. /* Add the watch to the handle's and the inode's list */
  579. list_add(&watch->h_list, &ih->watches);
  580. list_add(&watch->i_list, &inode->inotify_watches);
  581. out:
  582. mutex_unlock(&ih->mutex);
  583. mutex_unlock(&inode->inotify_mutex);
  584. return ret;
  585. }
  586. EXPORT_SYMBOL_GPL(inotify_add_watch);
  587. /**
  588. * inotify_rm_wd - remove a watch from an inotify instance
  589. * @ih: inotify handle
  590. * @wd: watch descriptor to remove
  591. *
  592. * Can sleep.
  593. */
  594. int inotify_rm_wd(struct inotify_handle *ih, u32 wd)
  595. {
  596. struct inotify_watch *watch;
  597. struct inode *inode;
  598. mutex_lock(&ih->mutex);
  599. watch = idr_find(&ih->idr, wd);
  600. if (unlikely(!watch)) {
  601. mutex_unlock(&ih->mutex);
  602. return -EINVAL;
  603. }
  604. get_inotify_watch(watch);
  605. inode = watch->inode;
  606. mutex_unlock(&ih->mutex);
  607. mutex_lock(&inode->inotify_mutex);
  608. mutex_lock(&ih->mutex);
  609. /* make sure that we did not race */
  610. if (likely(idr_find(&ih->idr, wd) == watch))
  611. inotify_remove_watch_locked(ih, watch);
  612. mutex_unlock(&ih->mutex);
  613. mutex_unlock(&inode->inotify_mutex);
  614. put_inotify_watch(watch);
  615. return 0;
  616. }
  617. EXPORT_SYMBOL_GPL(inotify_rm_wd);
  618. /**
  619. * inotify_rm_watch - remove a watch from an inotify instance
  620. * @ih: inotify handle
  621. * @watch: watch to remove
  622. *
  623. * Can sleep.
  624. */
  625. int inotify_rm_watch(struct inotify_handle *ih,
  626. struct inotify_watch *watch)
  627. {
  628. return inotify_rm_wd(ih, watch->wd);
  629. }
  630. EXPORT_SYMBOL_GPL(inotify_rm_watch);
  631. /*
  632. * inotify_setup - core initialization function
  633. */
  634. static int __init inotify_setup(void)
  635. {
  636. atomic_set(&inotify_cookie, 0);
  637. return 0;
  638. }
  639. module_init(inotify_setup);