inotify.c 19 KB

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