mark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 guaranteed 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. * mark->lock
  33. * group->mark_lock
  34. * inode->i_lock
  35. *
  36. * mark->lock protects 2 things, mark->group and mark->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 marks_list anchored inside a given group
  41. * and each mark 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_marks list anchored inside a
  46. * given inode and each mark 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)
  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_marks 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/kthread.h>
  87. #include <linux/module.h>
  88. #include <linux/mutex.h>
  89. #include <linux/slab.h>
  90. #include <linux/spinlock.h>
  91. #include <linux/srcu.h>
  92. #include <linux/atomic.h>
  93. #include <linux/fsnotify_backend.h>
  94. #include "fsnotify.h"
  95. struct srcu_struct fsnotify_mark_srcu;
  96. static DEFINE_SPINLOCK(destroy_lock);
  97. static LIST_HEAD(destroy_list);
  98. static DECLARE_WAIT_QUEUE_HEAD(destroy_waitq);
  99. void fsnotify_get_mark(struct fsnotify_mark *mark)
  100. {
  101. atomic_inc(&mark->refcnt);
  102. }
  103. void fsnotify_put_mark(struct fsnotify_mark *mark)
  104. {
  105. if (atomic_dec_and_test(&mark->refcnt))
  106. mark->free_mark(mark);
  107. }
  108. /*
  109. * Any time a mark is getting freed we end up here.
  110. * The caller had better be holding a reference to this mark so we don't actually
  111. * do the final put under the mark->lock
  112. */
  113. void fsnotify_destroy_mark(struct fsnotify_mark *mark)
  114. {
  115. struct fsnotify_group *group;
  116. struct inode *inode = NULL;
  117. spin_lock(&mark->lock);
  118. group = mark->group;
  119. /* something else already called this function on this mark */
  120. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
  121. spin_unlock(&mark->lock);
  122. return;
  123. }
  124. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  125. spin_lock(&group->mark_lock);
  126. if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
  127. inode = mark->i.inode;
  128. fsnotify_destroy_inode_mark(mark);
  129. } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
  130. fsnotify_destroy_vfsmount_mark(mark);
  131. else
  132. BUG();
  133. list_del_init(&mark->g_list);
  134. spin_unlock(&group->mark_lock);
  135. spin_unlock(&mark->lock);
  136. spin_lock(&destroy_lock);
  137. list_add(&mark->destroy_list, &destroy_list);
  138. spin_unlock(&destroy_lock);
  139. wake_up(&destroy_waitq);
  140. /*
  141. * Some groups like to know that marks are being freed. This is a
  142. * callback to the group function to let it know that this mark
  143. * is being freed.
  144. */
  145. if (group->ops->freeing_mark)
  146. group->ops->freeing_mark(mark, group);
  147. /*
  148. * __fsnotify_update_child_dentry_flags(inode);
  149. *
  150. * I really want to call that, but we can't, we have no idea if the inode
  151. * still exists the second we drop the mark->lock.
  152. *
  153. * The next time an event arrive to this inode from one of it's children
  154. * __fsnotify_parent will see that the inode doesn't care about it's
  155. * children and will update all of these flags then. So really this
  156. * is just a lazy update (and could be a perf win...)
  157. */
  158. if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
  159. iput(inode);
  160. /*
  161. * We don't necessarily have a ref on mark from caller so the above iput
  162. * may have already destroyed it. Don't touch from now on.
  163. */
  164. /*
  165. * it's possible that this group tried to destroy itself, but this
  166. * this mark was simultaneously being freed by inode. If that's the
  167. * case, we finish freeing the group here.
  168. */
  169. if (unlikely(atomic_dec_and_test(&group->num_marks)))
  170. fsnotify_final_destroy_group(group);
  171. }
  172. void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
  173. {
  174. assert_spin_locked(&mark->lock);
  175. mark->mask = mask;
  176. if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
  177. fsnotify_set_inode_mark_mask_locked(mark, mask);
  178. }
  179. void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
  180. {
  181. assert_spin_locked(&mark->lock);
  182. mark->ignored_mask = mask;
  183. }
  184. /*
  185. * Attach an initialized mark to a given group and fs object.
  186. * These marks may be used for the fsnotify backend to determine which
  187. * event types should be delivered to which group.
  188. */
  189. int fsnotify_add_mark(struct fsnotify_mark *mark,
  190. struct fsnotify_group *group, struct inode *inode,
  191. struct vfsmount *mnt, int allow_dups)
  192. {
  193. int ret = 0;
  194. BUG_ON(inode && mnt);
  195. BUG_ON(!inode && !mnt);
  196. /*
  197. * LOCKING ORDER!!!!
  198. * mark->lock
  199. * group->mark_lock
  200. * inode->i_lock
  201. */
  202. spin_lock(&mark->lock);
  203. spin_lock(&group->mark_lock);
  204. mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE;
  205. mark->group = group;
  206. list_add(&mark->g_list, &group->marks_list);
  207. atomic_inc(&group->num_marks);
  208. fsnotify_get_mark(mark); /* for i_list and g_list */
  209. if (inode) {
  210. ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
  211. if (ret)
  212. goto err;
  213. } else if (mnt) {
  214. ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
  215. if (ret)
  216. goto err;
  217. } else {
  218. BUG();
  219. }
  220. spin_unlock(&group->mark_lock);
  221. /* this will pin the object if appropriate */
  222. fsnotify_set_mark_mask_locked(mark, mark->mask);
  223. spin_unlock(&mark->lock);
  224. if (inode)
  225. __fsnotify_update_child_dentry_flags(inode);
  226. return ret;
  227. err:
  228. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  229. list_del_init(&mark->g_list);
  230. mark->group = NULL;
  231. atomic_dec(&group->num_marks);
  232. spin_unlock(&group->mark_lock);
  233. spin_unlock(&mark->lock);
  234. spin_lock(&destroy_lock);
  235. list_add(&mark->destroy_list, &destroy_list);
  236. spin_unlock(&destroy_lock);
  237. wake_up(&destroy_waitq);
  238. return ret;
  239. }
  240. /*
  241. * clear any marks in a group in which mark->flags & flags is true
  242. */
  243. void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
  244. unsigned int flags)
  245. {
  246. struct fsnotify_mark *lmark, *mark;
  247. LIST_HEAD(free_list);
  248. spin_lock(&group->mark_lock);
  249. list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
  250. if (mark->flags & flags) {
  251. list_add(&mark->free_g_list, &free_list);
  252. list_del_init(&mark->g_list);
  253. fsnotify_get_mark(mark);
  254. }
  255. }
  256. spin_unlock(&group->mark_lock);
  257. list_for_each_entry_safe(mark, lmark, &free_list, free_g_list) {
  258. fsnotify_destroy_mark(mark);
  259. fsnotify_put_mark(mark);
  260. }
  261. }
  262. /*
  263. * Given a group, destroy all of the marks associated with that group.
  264. */
  265. void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
  266. {
  267. fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1);
  268. }
  269. void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old)
  270. {
  271. assert_spin_locked(&old->lock);
  272. new->i.inode = old->i.inode;
  273. new->m.mnt = old->m.mnt;
  274. new->group = old->group;
  275. new->mask = old->mask;
  276. new->free_mark = old->free_mark;
  277. }
  278. /*
  279. * Nothing fancy, just initialize lists and locks and counters.
  280. */
  281. void fsnotify_init_mark(struct fsnotify_mark *mark,
  282. void (*free_mark)(struct fsnotify_mark *mark))
  283. {
  284. memset(mark, 0, sizeof(*mark));
  285. spin_lock_init(&mark->lock);
  286. atomic_set(&mark->refcnt, 1);
  287. mark->free_mark = free_mark;
  288. }
  289. static int fsnotify_mark_destroy(void *ignored)
  290. {
  291. struct fsnotify_mark *mark, *next;
  292. LIST_HEAD(private_destroy_list);
  293. for (;;) {
  294. spin_lock(&destroy_lock);
  295. /* exchange the list head */
  296. list_replace_init(&destroy_list, &private_destroy_list);
  297. spin_unlock(&destroy_lock);
  298. synchronize_srcu(&fsnotify_mark_srcu);
  299. list_for_each_entry_safe(mark, next, &private_destroy_list, destroy_list) {
  300. list_del_init(&mark->destroy_list);
  301. fsnotify_put_mark(mark);
  302. }
  303. wait_event_interruptible(destroy_waitq, !list_empty(&destroy_list));
  304. }
  305. return 0;
  306. }
  307. static int __init fsnotify_mark_init(void)
  308. {
  309. struct task_struct *thread;
  310. thread = kthread_run(fsnotify_mark_destroy, NULL,
  311. "fsnotify_mark");
  312. if (IS_ERR(thread))
  313. panic("unable to start fsnotify mark destruction thread.");
  314. return 0;
  315. }
  316. device_initcall(fsnotify_mark_init);