inotify.c 23 KB

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