inotify.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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_NOFS)))
  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. * We cannot __iget() an inode in state I_CLEAR, I_FREEING,
  339. * I_WILL_FREE, or I_NEW which is fine because by that point
  340. * the inode cannot have any associated watches.
  341. */
  342. if (inode->i_state & (I_CLEAR|I_FREEING|I_WILL_FREE|I_NEW))
  343. continue;
  344. /*
  345. * If i_count is zero, the inode cannot have any watches and
  346. * doing an __iget/iput with MS_ACTIVE clear would actually
  347. * evict all inodes with zero i_count from icache which is
  348. * unnecessarily violent and may in fact be illegal to do.
  349. */
  350. if (!atomic_read(&inode->i_count))
  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. get_inotify_watch(watch);
  382. mutex_lock(&ih->mutex);
  383. ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
  384. NULL, NULL);
  385. inotify_remove_watch_locked(ih, watch);
  386. mutex_unlock(&ih->mutex);
  387. put_inotify_watch(watch);
  388. }
  389. mutex_unlock(&inode->inotify_mutex);
  390. iput(inode);
  391. spin_lock(&inode_lock);
  392. }
  393. }
  394. EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
  395. /**
  396. * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
  397. * @inode: inode that is about to be removed
  398. */
  399. void inotify_inode_is_dead(struct inode *inode)
  400. {
  401. struct inotify_watch *watch, *next;
  402. mutex_lock(&inode->inotify_mutex);
  403. list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
  404. struct inotify_handle *ih = watch->ih;
  405. mutex_lock(&ih->mutex);
  406. inotify_remove_watch_locked(ih, watch);
  407. mutex_unlock(&ih->mutex);
  408. }
  409. mutex_unlock(&inode->inotify_mutex);
  410. }
  411. EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
  412. /* Kernel Consumer API */
  413. /**
  414. * inotify_init - allocate and initialize an inotify instance
  415. * @ops: caller's inotify operations
  416. */
  417. struct inotify_handle *inotify_init(const struct inotify_operations *ops)
  418. {
  419. struct inotify_handle *ih;
  420. ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
  421. if (unlikely(!ih))
  422. return ERR_PTR(-ENOMEM);
  423. idr_init(&ih->idr);
  424. INIT_LIST_HEAD(&ih->watches);
  425. mutex_init(&ih->mutex);
  426. ih->last_wd = 0;
  427. ih->in_ops = ops;
  428. atomic_set(&ih->count, 0);
  429. get_inotify_handle(ih);
  430. return ih;
  431. }
  432. EXPORT_SYMBOL_GPL(inotify_init);
  433. /**
  434. * inotify_init_watch - initialize an inotify watch
  435. * @watch: watch to initialize
  436. */
  437. void inotify_init_watch(struct inotify_watch *watch)
  438. {
  439. INIT_LIST_HEAD(&watch->h_list);
  440. INIT_LIST_HEAD(&watch->i_list);
  441. atomic_set(&watch->count, 0);
  442. get_inotify_watch(watch); /* initial get */
  443. }
  444. EXPORT_SYMBOL_GPL(inotify_init_watch);
  445. /*
  446. * Watch removals suck violently. To kick the watch out we need (in this
  447. * order) inode->inotify_mutex and ih->mutex. That's fine if we have
  448. * a hold on inode; however, for all other cases we need to make damn sure
  449. * we don't race with umount. We can *NOT* just grab a reference to a
  450. * watch - inotify_unmount_inodes() will happily sail past it and we'll end
  451. * with reference to inode potentially outliving its superblock. Ideally
  452. * we just want to grab an active reference to superblock if we can; that
  453. * will make sure we won't go into inotify_umount_inodes() until we are
  454. * done. Cleanup is just deactivate_super(). However, that leaves a messy
  455. * case - what if we *are* racing with umount() and active references to
  456. * superblock can't be acquired anymore? We can bump ->s_count, grab
  457. * ->s_umount, which will almost certainly wait until the superblock is shut
  458. * down and the watch in question is pining for fjords. That's fine, but
  459. * there is a problem - we might have hit the window between ->s_active
  460. * getting to 0 / ->s_count - below S_BIAS (i.e. the moment when superblock
  461. * is past the point of no return and is heading for shutdown) and the
  462. * moment when deactivate_super() acquires ->s_umount. We could just do
  463. * drop_super() yield() and retry, but that's rather antisocial and this
  464. * stuff is luser-triggerable. OTOH, having grabbed ->s_umount and having
  465. * found that we'd got there first (i.e. that ->s_root is non-NULL) we know
  466. * that we won't race with inotify_umount_inodes(). So we could grab a
  467. * reference to watch and do the rest as above, just with drop_super() instead
  468. * of deactivate_super(), right? Wrong. We had to drop ih->mutex before we
  469. * could grab ->s_umount. So the watch could've been gone already.
  470. *
  471. * That still can be dealt with - we need to save watch->wd, do idr_find()
  472. * and compare its result with our pointer. If they match, we either have
  473. * the damn thing still alive or we'd lost not one but two races at once,
  474. * the watch had been killed and a new one got created with the same ->wd
  475. * at the same address. That couldn't have happened in inotify_destroy(),
  476. * but inotify_rm_wd() could run into that. Still, "new one got created"
  477. * is not a problem - we have every right to kill it or leave it alone,
  478. * whatever's more convenient.
  479. *
  480. * So we can use idr_find(...) == watch && watch->inode->i_sb == sb as
  481. * "grab it and kill it" check. If it's been our original watch, we are
  482. * fine, if it's a newcomer - nevermind, just pretend that we'd won the
  483. * race and kill the fscker anyway; we are safe since we know that its
  484. * superblock won't be going away.
  485. *
  486. * And yes, this is far beyond mere "not very pretty"; so's the entire
  487. * concept of inotify to start with.
  488. */
  489. /**
  490. * pin_to_kill - pin the watch down for removal
  491. * @ih: inotify handle
  492. * @watch: watch to kill
  493. *
  494. * Called with ih->mutex held, drops it. Possible return values:
  495. * 0 - nothing to do, it has died
  496. * 1 - remove it, drop the reference and deactivate_super()
  497. * 2 - remove it, drop the reference and drop_super(); we tried hard to avoid
  498. * that variant, since it involved a lot of PITA, but that's the best that
  499. * could've been done.
  500. */
  501. static int pin_to_kill(struct inotify_handle *ih, struct inotify_watch *watch)
  502. {
  503. struct super_block *sb = watch->inode->i_sb;
  504. s32 wd = watch->wd;
  505. spin_lock(&sb_lock);
  506. if (sb->s_count >= S_BIAS) {
  507. atomic_inc(&sb->s_active);
  508. spin_unlock(&sb_lock);
  509. get_inotify_watch(watch);
  510. mutex_unlock(&ih->mutex);
  511. return 1; /* the best outcome */
  512. }
  513. sb->s_count++;
  514. spin_unlock(&sb_lock);
  515. mutex_unlock(&ih->mutex); /* can't grab ->s_umount under it */
  516. down_read(&sb->s_umount);
  517. if (likely(!sb->s_root)) {
  518. /* fs is already shut down; the watch is dead */
  519. drop_super(sb);
  520. return 0;
  521. }
  522. /* raced with the final deactivate_super() */
  523. mutex_lock(&ih->mutex);
  524. if (idr_find(&ih->idr, wd) != watch || watch->inode->i_sb != sb) {
  525. /* the watch is dead */
  526. mutex_unlock(&ih->mutex);
  527. drop_super(sb);
  528. return 0;
  529. }
  530. /* still alive or freed and reused with the same sb and wd; kill */
  531. get_inotify_watch(watch);
  532. mutex_unlock(&ih->mutex);
  533. return 2;
  534. }
  535. static void unpin_and_kill(struct inotify_watch *watch, int how)
  536. {
  537. struct super_block *sb = watch->inode->i_sb;
  538. put_inotify_watch(watch);
  539. switch (how) {
  540. case 1:
  541. deactivate_super(sb);
  542. break;
  543. case 2:
  544. drop_super(sb);
  545. }
  546. }
  547. /**
  548. * inotify_destroy - clean up and destroy an inotify instance
  549. * @ih: inotify handle
  550. */
  551. void inotify_destroy(struct inotify_handle *ih)
  552. {
  553. /*
  554. * Destroy all of the watches for this handle. Unfortunately, not very
  555. * pretty. We cannot do a simple iteration over the list, because we
  556. * do not know the inode until we iterate to the watch. But we need to
  557. * hold inode->inotify_mutex before ih->mutex. The following works.
  558. *
  559. * AV: it had to become even uglier to start working ;-/
  560. */
  561. while (1) {
  562. struct inotify_watch *watch;
  563. struct list_head *watches;
  564. struct super_block *sb;
  565. struct inode *inode;
  566. int how;
  567. mutex_lock(&ih->mutex);
  568. watches = &ih->watches;
  569. if (list_empty(watches)) {
  570. mutex_unlock(&ih->mutex);
  571. break;
  572. }
  573. watch = list_first_entry(watches, struct inotify_watch, h_list);
  574. sb = watch->inode->i_sb;
  575. how = pin_to_kill(ih, watch);
  576. if (!how)
  577. continue;
  578. inode = watch->inode;
  579. mutex_lock(&inode->inotify_mutex);
  580. mutex_lock(&ih->mutex);
  581. /* make sure we didn't race with another list removal */
  582. if (likely(idr_find(&ih->idr, watch->wd))) {
  583. remove_watch_no_event(watch, ih);
  584. put_inotify_watch(watch);
  585. }
  586. mutex_unlock(&ih->mutex);
  587. mutex_unlock(&inode->inotify_mutex);
  588. unpin_and_kill(watch, how);
  589. }
  590. /* free this handle: the put matching the get in inotify_init() */
  591. put_inotify_handle(ih);
  592. }
  593. EXPORT_SYMBOL_GPL(inotify_destroy);
  594. /**
  595. * inotify_find_watch - find an existing watch for an (ih,inode) pair
  596. * @ih: inotify handle
  597. * @inode: inode to watch
  598. * @watchp: pointer to existing inotify_watch
  599. *
  600. * Caller must pin given inode (via nameidata).
  601. */
  602. s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,
  603. struct inotify_watch **watchp)
  604. {
  605. struct inotify_watch *old;
  606. int ret = -ENOENT;
  607. mutex_lock(&inode->inotify_mutex);
  608. mutex_lock(&ih->mutex);
  609. old = inode_find_handle(inode, ih);
  610. if (unlikely(old)) {
  611. get_inotify_watch(old); /* caller must put watch */
  612. *watchp = old;
  613. ret = old->wd;
  614. }
  615. mutex_unlock(&ih->mutex);
  616. mutex_unlock(&inode->inotify_mutex);
  617. return ret;
  618. }
  619. EXPORT_SYMBOL_GPL(inotify_find_watch);
  620. /**
  621. * inotify_find_update_watch - find and update the mask of an existing watch
  622. * @ih: inotify handle
  623. * @inode: inode's watch to update
  624. * @mask: mask of events to watch
  625. *
  626. * Caller must pin given inode (via nameidata).
  627. */
  628. s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
  629. u32 mask)
  630. {
  631. struct inotify_watch *old;
  632. int mask_add = 0;
  633. int ret;
  634. if (mask & IN_MASK_ADD)
  635. mask_add = 1;
  636. /* don't allow invalid bits: we don't want flags set */
  637. mask &= IN_ALL_EVENTS | IN_ONESHOT;
  638. if (unlikely(!mask))
  639. return -EINVAL;
  640. mutex_lock(&inode->inotify_mutex);
  641. mutex_lock(&ih->mutex);
  642. /*
  643. * Handle the case of re-adding a watch on an (inode,ih) pair that we
  644. * are already watching. We just update the mask and return its wd.
  645. */
  646. old = inode_find_handle(inode, ih);
  647. if (unlikely(!old)) {
  648. ret = -ENOENT;
  649. goto out;
  650. }
  651. if (mask_add)
  652. old->mask |= mask;
  653. else
  654. old->mask = mask;
  655. ret = old->wd;
  656. out:
  657. mutex_unlock(&ih->mutex);
  658. mutex_unlock(&inode->inotify_mutex);
  659. return ret;
  660. }
  661. EXPORT_SYMBOL_GPL(inotify_find_update_watch);
  662. /**
  663. * inotify_add_watch - add a watch to an inotify instance
  664. * @ih: inotify handle
  665. * @watch: caller allocated watch structure
  666. * @inode: inode to watch
  667. * @mask: mask of events to watch
  668. *
  669. * Caller must pin given inode (via nameidata).
  670. * Caller must ensure it only calls inotify_add_watch() once per watch.
  671. * Calls inotify_handle_get_wd() so may sleep.
  672. */
  673. s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,
  674. struct inode *inode, u32 mask)
  675. {
  676. int ret = 0;
  677. int newly_watched;
  678. /* don't allow invalid bits: we don't want flags set */
  679. mask &= IN_ALL_EVENTS | IN_ONESHOT;
  680. if (unlikely(!mask))
  681. return -EINVAL;
  682. watch->mask = mask;
  683. mutex_lock(&inode->inotify_mutex);
  684. mutex_lock(&ih->mutex);
  685. /* Initialize a new watch */
  686. ret = inotify_handle_get_wd(ih, watch);
  687. if (unlikely(ret))
  688. goto out;
  689. ret = watch->wd;
  690. /* save a reference to handle and bump the count to make it official */
  691. get_inotify_handle(ih);
  692. watch->ih = ih;
  693. /*
  694. * Save a reference to the inode and bump the ref count to make it
  695. * official. We hold a reference to nameidata, which makes this safe.
  696. */
  697. watch->inode = igrab(inode);
  698. /* Add the watch to the handle's and the inode's list */
  699. newly_watched = !inotify_inode_watched(inode);
  700. list_add(&watch->h_list, &ih->watches);
  701. list_add(&watch->i_list, &inode->inotify_watches);
  702. /*
  703. * Set child flags _after_ adding the watch, so there is no race
  704. * windows where newly instantiated children could miss their parent's
  705. * watched flag.
  706. */
  707. if (newly_watched)
  708. set_dentry_child_flags(inode, 1);
  709. out:
  710. mutex_unlock(&ih->mutex);
  711. mutex_unlock(&inode->inotify_mutex);
  712. return ret;
  713. }
  714. EXPORT_SYMBOL_GPL(inotify_add_watch);
  715. /**
  716. * inotify_clone_watch - put the watch next to existing one
  717. * @old: already installed watch
  718. * @new: new watch
  719. *
  720. * Caller must hold the inotify_mutex of inode we are dealing with;
  721. * it is expected to remove the old watch before unlocking the inode.
  722. */
  723. s32 inotify_clone_watch(struct inotify_watch *old, struct inotify_watch *new)
  724. {
  725. struct inotify_handle *ih = old->ih;
  726. int ret = 0;
  727. new->mask = old->mask;
  728. new->ih = ih;
  729. mutex_lock(&ih->mutex);
  730. /* Initialize a new watch */
  731. ret = inotify_handle_get_wd(ih, new);
  732. if (unlikely(ret))
  733. goto out;
  734. ret = new->wd;
  735. get_inotify_handle(ih);
  736. new->inode = igrab(old->inode);
  737. list_add(&new->h_list, &ih->watches);
  738. list_add(&new->i_list, &old->inode->inotify_watches);
  739. out:
  740. mutex_unlock(&ih->mutex);
  741. return ret;
  742. }
  743. void inotify_evict_watch(struct inotify_watch *watch)
  744. {
  745. get_inotify_watch(watch);
  746. mutex_lock(&watch->ih->mutex);
  747. inotify_remove_watch_locked(watch->ih, watch);
  748. mutex_unlock(&watch->ih->mutex);
  749. }
  750. /**
  751. * inotify_rm_wd - remove a watch from an inotify instance
  752. * @ih: inotify handle
  753. * @wd: watch descriptor to remove
  754. *
  755. * Can sleep.
  756. */
  757. int inotify_rm_wd(struct inotify_handle *ih, u32 wd)
  758. {
  759. struct inotify_watch *watch;
  760. struct super_block *sb;
  761. struct inode *inode;
  762. int how;
  763. mutex_lock(&ih->mutex);
  764. watch = idr_find(&ih->idr, wd);
  765. if (unlikely(!watch)) {
  766. mutex_unlock(&ih->mutex);
  767. return -EINVAL;
  768. }
  769. sb = watch->inode->i_sb;
  770. how = pin_to_kill(ih, watch);
  771. if (!how)
  772. return 0;
  773. inode = watch->inode;
  774. mutex_lock(&inode->inotify_mutex);
  775. mutex_lock(&ih->mutex);
  776. /* make sure that we did not race */
  777. if (likely(idr_find(&ih->idr, wd) == watch))
  778. inotify_remove_watch_locked(ih, watch);
  779. mutex_unlock(&ih->mutex);
  780. mutex_unlock(&inode->inotify_mutex);
  781. unpin_and_kill(watch, how);
  782. return 0;
  783. }
  784. EXPORT_SYMBOL_GPL(inotify_rm_wd);
  785. /**
  786. * inotify_rm_watch - remove a watch from an inotify instance
  787. * @ih: inotify handle
  788. * @watch: watch to remove
  789. *
  790. * Can sleep.
  791. */
  792. int inotify_rm_watch(struct inotify_handle *ih,
  793. struct inotify_watch *watch)
  794. {
  795. return inotify_rm_wd(ih, watch->wd);
  796. }
  797. EXPORT_SYMBOL_GPL(inotify_rm_watch);
  798. /*
  799. * inotify_setup - core initialization function
  800. */
  801. static int __init inotify_setup(void)
  802. {
  803. atomic_set(&inotify_cookie, 0);
  804. return 0;
  805. }
  806. module_init(inotify_setup);