inotify.c 26 KB

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