inotify.c 26 KB

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