inotify.c 25 KB

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