inode_mark.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; see the file COPYING. If not, write to
  16. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*
  19. * fsnotify inode mark locking/lifetime/and refcnting
  20. *
  21. * REFCNT:
  22. * The mark->refcnt tells how many "things" in the kernel currently are
  23. * referencing this object. The object typically will live inside the kernel
  24. * with a refcnt of 2, one for each list it is on (i_list, g_list). Any task
  25. * which can find this object holding the appropriete locks, can take a reference
  26. * and the object itself is guarenteed to survive until the reference is dropped.
  27. *
  28. * LOCKING:
  29. * There are 3 spinlocks involved with fsnotify inode marks and they MUST
  30. * be taken in order as follows:
  31. *
  32. * entry->lock
  33. * group->mark_lock
  34. * inode->i_lock
  35. *
  36. * entry->lock protects 2 things, entry->group and entry->inode. You must hold
  37. * that lock to dereference either of these things (they could be NULL even with
  38. * the lock)
  39. *
  40. * group->mark_lock protects the mark_entries list anchored inside a given group
  41. * and each entry is hooked via the g_list. It also sorta protects the
  42. * free_g_list, which when used is anchored by a private list on the stack of the
  43. * task which held the group->mark_lock.
  44. *
  45. * inode->i_lock protects the i_fsnotify_mark_entries list anchored inside a
  46. * given inode and each entry is hooked via the i_list. (and sorta the
  47. * free_i_list)
  48. *
  49. *
  50. * LIFETIME:
  51. * Inode marks survive between when they are added to an inode and when their
  52. * refcnt==0.
  53. *
  54. * The inode mark can be cleared for a number of different reasons including:
  55. * - The inode is unlinked for the last time. (fsnotify_inode_remove)
  56. * - The inode is being evicted from cache. (fsnotify_inode_delete)
  57. * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes)
  58. * - Something explicitly requests that it be removed. (fsnotify_destroy_mark_by_entry)
  59. * - The fsnotify_group associated with the mark is going away and all such marks
  60. * need to be cleaned up. (fsnotify_clear_marks_by_group)
  61. *
  62. * Worst case we are given an inode and need to clean up all the marks on that
  63. * inode. We take i_lock and walk the i_fsnotify_mark_entries safely. For each
  64. * mark on the list we take a reference (so the mark can't disappear under us).
  65. * We remove that mark form the inode's list of marks and we add this mark to a
  66. * private list anchored on the stack using i_free_list; At this point we no
  67. * longer fear anything finding the mark using the inode's list of marks.
  68. *
  69. * We can safely and locklessly run the private list on the stack of everything
  70. * we just unattached from the original inode. For each mark on the private list
  71. * we grab the mark-> and can thus dereference mark->group and mark->inode. If
  72. * we see the group and inode are not NULL we take those locks. Now holding all
  73. * 3 locks we can completely remove the mark from other tasks finding it in the
  74. * future. Remember, 10 things might already be referencing this mark, but they
  75. * better be holding a ref. We drop our reference we took before we unhooked it
  76. * from the inode. When the ref hits 0 we can free the mark.
  77. *
  78. * Very similarly for freeing by group, except we use free_g_list.
  79. *
  80. * This has the very interesting property of being able to run concurrently with
  81. * any (or all) other directions.
  82. */
  83. #include <linux/fs.h>
  84. #include <linux/init.h>
  85. #include <linux/kernel.h>
  86. #include <linux/module.h>
  87. #include <linux/mutex.h>
  88. #include <linux/slab.h>
  89. #include <linux/spinlock.h>
  90. #include <linux/writeback.h> /* for inode_lock */
  91. #include <asm/atomic.h>
  92. #include <linux/fsnotify_backend.h>
  93. #include "fsnotify.h"
  94. void fsnotify_get_mark(struct fsnotify_mark_entry *entry)
  95. {
  96. atomic_inc(&entry->refcnt);
  97. }
  98. void fsnotify_put_mark(struct fsnotify_mark_entry *entry)
  99. {
  100. if (atomic_dec_and_test(&entry->refcnt))
  101. entry->free_mark(entry);
  102. }
  103. /*
  104. * Recalculate the mask of events relevant to a given inode locked.
  105. */
  106. static void fsnotify_recalc_inode_mask_locked(struct inode *inode)
  107. {
  108. struct fsnotify_mark_entry *entry;
  109. struct hlist_node *pos;
  110. __u32 new_mask = 0;
  111. assert_spin_locked(&inode->i_lock);
  112. hlist_for_each_entry(entry, pos, &inode->i_fsnotify_mark_entries, i_list)
  113. new_mask |= entry->mask;
  114. inode->i_fsnotify_mask = new_mask;
  115. }
  116. /*
  117. * Recalculate the inode->i_fsnotify_mask, or the mask of all FS_* event types
  118. * any notifier is interested in hearing for this inode.
  119. */
  120. void fsnotify_recalc_inode_mask(struct inode *inode)
  121. {
  122. spin_lock(&inode->i_lock);
  123. fsnotify_recalc_inode_mask_locked(inode);
  124. spin_unlock(&inode->i_lock);
  125. __fsnotify_update_child_dentry_flags(inode);
  126. }
  127. /*
  128. * Any time a mark is getting freed we end up here.
  129. * The caller had better be holding a reference to this mark so we don't actually
  130. * do the final put under the entry->lock
  131. */
  132. void fsnotify_destroy_mark_by_entry(struct fsnotify_mark_entry *entry)
  133. {
  134. struct fsnotify_group *group;
  135. struct inode *inode;
  136. spin_lock(&entry->lock);
  137. group = entry->group;
  138. inode = entry->inode;
  139. BUG_ON(group && !inode);
  140. BUG_ON(!group && inode);
  141. /* if !group something else already marked this to die */
  142. if (!group) {
  143. spin_unlock(&entry->lock);
  144. return;
  145. }
  146. /* 1 from caller and 1 for being on i_list/g_list */
  147. BUG_ON(atomic_read(&entry->refcnt) < 2);
  148. spin_lock(&group->mark_lock);
  149. spin_lock(&inode->i_lock);
  150. hlist_del_init(&entry->i_list);
  151. entry->inode = NULL;
  152. list_del_init(&entry->g_list);
  153. entry->group = NULL;
  154. fsnotify_put_mark(entry); /* for i_list and g_list */
  155. /*
  156. * this mark is now off the inode->i_fsnotify_mark_entries list and we
  157. * hold the inode->i_lock, so this is the perfect time to update the
  158. * inode->i_fsnotify_mask
  159. */
  160. fsnotify_recalc_inode_mask_locked(inode);
  161. spin_unlock(&inode->i_lock);
  162. spin_unlock(&group->mark_lock);
  163. spin_unlock(&entry->lock);
  164. /*
  165. * Some groups like to know that marks are being freed. This is a
  166. * callback to the group function to let it know that this entry
  167. * is being freed.
  168. */
  169. if (group->ops->freeing_mark)
  170. group->ops->freeing_mark(entry, group);
  171. /*
  172. * __fsnotify_update_child_dentry_flags(inode);
  173. *
  174. * I really want to call that, but we can't, we have no idea if the inode
  175. * still exists the second we drop the entry->lock.
  176. *
  177. * The next time an event arrive to this inode from one of it's children
  178. * __fsnotify_parent will see that the inode doesn't care about it's
  179. * children and will update all of these flags then. So really this
  180. * is just a lazy update (and could be a perf win...)
  181. */
  182. iput(inode);
  183. /*
  184. * it's possible that this group tried to destroy itself, but this
  185. * this mark was simultaneously being freed by inode. If that's the
  186. * case, we finish freeing the group here.
  187. */
  188. if (unlikely(atomic_dec_and_test(&group->num_marks)))
  189. fsnotify_final_destroy_group(group);
  190. }
  191. /*
  192. * Given a group, destroy all of the marks associated with that group.
  193. */
  194. void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
  195. {
  196. struct fsnotify_mark_entry *lentry, *entry;
  197. LIST_HEAD(free_list);
  198. spin_lock(&group->mark_lock);
  199. list_for_each_entry_safe(entry, lentry, &group->mark_entries, g_list) {
  200. list_add(&entry->free_g_list, &free_list);
  201. list_del_init(&entry->g_list);
  202. fsnotify_get_mark(entry);
  203. }
  204. spin_unlock(&group->mark_lock);
  205. list_for_each_entry_safe(entry, lentry, &free_list, free_g_list) {
  206. fsnotify_destroy_mark_by_entry(entry);
  207. fsnotify_put_mark(entry);
  208. }
  209. }
  210. /*
  211. * Given an inode, destroy all of the marks associated with that inode.
  212. */
  213. void fsnotify_clear_marks_by_inode(struct inode *inode)
  214. {
  215. struct fsnotify_mark_entry *entry, *lentry;
  216. struct hlist_node *pos, *n;
  217. LIST_HEAD(free_list);
  218. spin_lock(&inode->i_lock);
  219. hlist_for_each_entry_safe(entry, pos, n, &inode->i_fsnotify_mark_entries, i_list) {
  220. list_add(&entry->free_i_list, &free_list);
  221. hlist_del_init(&entry->i_list);
  222. fsnotify_get_mark(entry);
  223. }
  224. spin_unlock(&inode->i_lock);
  225. list_for_each_entry_safe(entry, lentry, &free_list, free_i_list) {
  226. fsnotify_destroy_mark_by_entry(entry);
  227. fsnotify_put_mark(entry);
  228. }
  229. }
  230. /*
  231. * given a group and inode, find the mark associated with that combination.
  232. * if found take a reference to that mark and return it, else return NULL
  233. */
  234. struct fsnotify_mark_entry *fsnotify_find_mark_entry(struct fsnotify_group *group,
  235. struct inode *inode)
  236. {
  237. struct fsnotify_mark_entry *entry;
  238. struct hlist_node *pos;
  239. assert_spin_locked(&inode->i_lock);
  240. hlist_for_each_entry(entry, pos, &inode->i_fsnotify_mark_entries, i_list) {
  241. if (entry->group == group) {
  242. fsnotify_get_mark(entry);
  243. return entry;
  244. }
  245. }
  246. return NULL;
  247. }
  248. /*
  249. * Nothing fancy, just initialize lists and locks and counters.
  250. */
  251. void fsnotify_init_mark(struct fsnotify_mark_entry *entry,
  252. void (*free_mark)(struct fsnotify_mark_entry *entry))
  253. {
  254. spin_lock_init(&entry->lock);
  255. atomic_set(&entry->refcnt, 1);
  256. INIT_HLIST_NODE(&entry->i_list);
  257. entry->group = NULL;
  258. entry->mask = 0;
  259. entry->inode = NULL;
  260. entry->free_mark = free_mark;
  261. }
  262. /*
  263. * Attach an initialized mark entry to a given group and inode.
  264. * These marks may be used for the fsnotify backend to determine which
  265. * event types should be delivered to which group and for which inodes.
  266. */
  267. int fsnotify_add_mark(struct fsnotify_mark_entry *entry,
  268. struct fsnotify_group *group, struct inode *inode)
  269. {
  270. struct fsnotify_mark_entry *lentry;
  271. int ret = 0;
  272. inode = igrab(inode);
  273. if (unlikely(!inode))
  274. return -EINVAL;
  275. /*
  276. * LOCKING ORDER!!!!
  277. * entry->lock
  278. * group->mark_lock
  279. * inode->i_lock
  280. */
  281. spin_lock(&entry->lock);
  282. spin_lock(&group->mark_lock);
  283. spin_lock(&inode->i_lock);
  284. entry->group = group;
  285. entry->inode = inode;
  286. lentry = fsnotify_find_mark_entry(group, inode);
  287. if (!lentry) {
  288. hlist_add_head(&entry->i_list, &inode->i_fsnotify_mark_entries);
  289. list_add(&entry->g_list, &group->mark_entries);
  290. fsnotify_get_mark(entry); /* for i_list and g_list */
  291. atomic_inc(&group->num_marks);
  292. fsnotify_recalc_inode_mask_locked(inode);
  293. }
  294. spin_unlock(&inode->i_lock);
  295. spin_unlock(&group->mark_lock);
  296. spin_unlock(&entry->lock);
  297. if (lentry) {
  298. ret = -EEXIST;
  299. iput(inode);
  300. fsnotify_put_mark(lentry);
  301. } else {
  302. __fsnotify_update_child_dentry_flags(inode);
  303. }
  304. return ret;
  305. }
  306. /**
  307. * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  308. * @list: list of inodes being unmounted (sb->s_inodes)
  309. *
  310. * Called with inode_lock held, protecting the unmounting super block's list
  311. * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
  312. * We temporarily drop inode_lock, however, and CAN block.
  313. */
  314. void fsnotify_unmount_inodes(struct list_head *list)
  315. {
  316. struct inode *inode, *next_i, *need_iput = NULL;
  317. list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
  318. struct inode *need_iput_tmp;
  319. /*
  320. * We cannot __iget() an inode in state I_CLEAR, I_FREEING,
  321. * I_WILL_FREE, or I_NEW which is fine because by that point
  322. * the inode cannot have any associated watches.
  323. */
  324. if (inode->i_state & (I_CLEAR|I_FREEING|I_WILL_FREE|I_NEW))
  325. continue;
  326. /*
  327. * If i_count is zero, the inode cannot have any watches and
  328. * doing an __iget/iput with MS_ACTIVE clear would actually
  329. * evict all inodes with zero i_count from icache which is
  330. * unnecessarily violent and may in fact be illegal to do.
  331. */
  332. if (!atomic_read(&inode->i_count))
  333. continue;
  334. need_iput_tmp = need_iput;
  335. need_iput = NULL;
  336. /* In case fsnotify_inode_delete() drops a reference. */
  337. if (inode != need_iput_tmp)
  338. __iget(inode);
  339. else
  340. need_iput_tmp = NULL;
  341. /* In case the dropping of a reference would nuke next_i. */
  342. if ((&next_i->i_sb_list != list) &&
  343. atomic_read(&next_i->i_count) &&
  344. !(next_i->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))) {
  345. __iget(next_i);
  346. need_iput = next_i;
  347. }
  348. /*
  349. * We can safely drop inode_lock here because we hold
  350. * references on both inode and next_i. Also no new inodes
  351. * will be added since the umount has begun. Finally,
  352. * iprune_mutex keeps shrink_icache_memory() away.
  353. */
  354. spin_unlock(&inode_lock);
  355. if (need_iput_tmp)
  356. iput(need_iput_tmp);
  357. /* for each watch, send FS_UNMOUNT and then remove it */
  358. fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  359. fsnotify_inode_delete(inode);
  360. iput(inode);
  361. spin_lock(&inode_lock);
  362. }
  363. }