mark.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 group->recnt and mark->refcnt tell how many "things" in the kernel
  23. * currently are referencing the objects. Both kind of objects typically will
  24. * live inside the kernel with a refcnt of 2, one for its creation and one for
  25. * the reference a group and a mark hold to each other.
  26. * If you are holding the appropriate locks, you can take a reference and the
  27. * object itself is guaranteed to survive until the reference is dropped.
  28. *
  29. * LOCKING:
  30. * There are 3 locks involved with fsnotify inode marks and they MUST be taken
  31. * in order as follows:
  32. *
  33. * group->mark_mutex
  34. * mark->lock
  35. * inode->i_lock
  36. *
  37. * group->mark_mutex protects the marks_list anchored inside a given group and
  38. * each mark is hooked via the g_list. It also protects the groups private
  39. * data (i.e group limits).
  40. * mark->lock protects the marks attributes like its masks and flags.
  41. * Furthermore it protects the access to a reference of the group that the mark
  42. * is assigned to as well as the access to a reference of the inode/vfsmount
  43. * that is being watched by the mark.
  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; we walk i_free_list
  67. * and before we destroy the mark we make sure that we dont race with a
  68. * concurrent destroy_group by getting a ref to the marks group and taking the
  69. * groups mutex.
  70. * Very similarly for freeing by group, except we use free_g_list.
  71. *
  72. * This has the very interesting property of being able to run concurrently with
  73. * any (or all) other directions.
  74. */
  75. #include <linux/fs.h>
  76. #include <linux/init.h>
  77. #include <linux/kernel.h>
  78. #include <linux/kthread.h>
  79. #include <linux/module.h>
  80. #include <linux/mutex.h>
  81. #include <linux/slab.h>
  82. #include <linux/spinlock.h>
  83. #include <linux/srcu.h>
  84. #include <linux/atomic.h>
  85. #include <linux/fsnotify_backend.h>
  86. #include "fsnotify.h"
  87. struct srcu_struct fsnotify_mark_srcu;
  88. static DEFINE_SPINLOCK(destroy_lock);
  89. static LIST_HEAD(destroy_list);
  90. static DECLARE_WAIT_QUEUE_HEAD(destroy_waitq);
  91. void fsnotify_get_mark(struct fsnotify_mark *mark)
  92. {
  93. atomic_inc(&mark->refcnt);
  94. }
  95. void fsnotify_put_mark(struct fsnotify_mark *mark)
  96. {
  97. if (atomic_dec_and_test(&mark->refcnt)) {
  98. if (mark->group)
  99. fsnotify_put_group(mark->group);
  100. mark->free_mark(mark);
  101. }
  102. }
  103. /* Calculate mask of events for a list of marks */
  104. u32 fsnotify_recalc_mask(struct hlist_head *head)
  105. {
  106. u32 new_mask = 0;
  107. struct fsnotify_mark *mark;
  108. hlist_for_each_entry(mark, head, obj_list)
  109. new_mask |= mark->mask;
  110. return new_mask;
  111. }
  112. /*
  113. * Any time a mark is getting freed we end up here.
  114. * The caller had better be holding a reference to this mark so we don't actually
  115. * do the final put under the mark->lock
  116. */
  117. void fsnotify_destroy_mark_locked(struct fsnotify_mark *mark,
  118. struct fsnotify_group *group)
  119. {
  120. struct inode *inode = NULL;
  121. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  122. spin_lock(&mark->lock);
  123. /* something else already called this function on this mark */
  124. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
  125. spin_unlock(&mark->lock);
  126. return;
  127. }
  128. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  129. if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
  130. inode = mark->inode;
  131. fsnotify_destroy_inode_mark(mark);
  132. } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
  133. fsnotify_destroy_vfsmount_mark(mark);
  134. else
  135. BUG();
  136. list_del_init(&mark->g_list);
  137. spin_unlock(&mark->lock);
  138. if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
  139. iput(inode);
  140. /* release lock temporarily */
  141. mutex_unlock(&group->mark_mutex);
  142. spin_lock(&destroy_lock);
  143. list_add(&mark->g_list, &destroy_list);
  144. spin_unlock(&destroy_lock);
  145. wake_up(&destroy_waitq);
  146. /*
  147. * We don't necessarily have a ref on mark from caller so the above destroy
  148. * may have actually freed it, unless this group provides a 'freeing_mark'
  149. * function which must be holding a reference.
  150. */
  151. /*
  152. * Some groups like to know that marks are being freed. This is a
  153. * callback to the group function to let it know that this mark
  154. * is being freed.
  155. */
  156. if (group->ops->freeing_mark)
  157. group->ops->freeing_mark(mark, group);
  158. /*
  159. * __fsnotify_update_child_dentry_flags(inode);
  160. *
  161. * I really want to call that, but we can't, we have no idea if the inode
  162. * still exists the second we drop the mark->lock.
  163. *
  164. * The next time an event arrive to this inode from one of it's children
  165. * __fsnotify_parent will see that the inode doesn't care about it's
  166. * children and will update all of these flags then. So really this
  167. * is just a lazy update (and could be a perf win...)
  168. */
  169. atomic_dec(&group->num_marks);
  170. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  171. }
  172. void fsnotify_destroy_mark(struct fsnotify_mark *mark,
  173. struct fsnotify_group *group)
  174. {
  175. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  176. fsnotify_destroy_mark_locked(mark, group);
  177. mutex_unlock(&group->mark_mutex);
  178. }
  179. /*
  180. * Destroy all marks in the given list. The marks must be already detached from
  181. * the original inode / vfsmount.
  182. */
  183. void fsnotify_destroy_marks(struct list_head *to_free)
  184. {
  185. struct fsnotify_mark *mark, *lmark;
  186. struct fsnotify_group *group;
  187. list_for_each_entry_safe(mark, lmark, to_free, free_list) {
  188. spin_lock(&mark->lock);
  189. fsnotify_get_group(mark->group);
  190. group = mark->group;
  191. spin_unlock(&mark->lock);
  192. fsnotify_destroy_mark(mark, group);
  193. fsnotify_put_mark(mark);
  194. fsnotify_put_group(group);
  195. }
  196. }
  197. void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
  198. {
  199. assert_spin_locked(&mark->lock);
  200. mark->mask = mask;
  201. if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
  202. fsnotify_set_inode_mark_mask_locked(mark, mask);
  203. }
  204. void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
  205. {
  206. assert_spin_locked(&mark->lock);
  207. mark->ignored_mask = mask;
  208. }
  209. /*
  210. * Sorting function for lists of fsnotify marks.
  211. *
  212. * Fanotify supports different notification classes (reflected as priority of
  213. * notification group). Events shall be passed to notification groups in
  214. * decreasing priority order. To achieve this marks in notification lists for
  215. * inodes and vfsmounts are sorted so that priorities of corresponding groups
  216. * are descending.
  217. *
  218. * Furthermore correct handling of the ignore mask requires processing inode
  219. * and vfsmount marks of each group together. Using the group address as
  220. * further sort criterion provides a unique sorting order and thus we can
  221. * merge inode and vfsmount lists of marks in linear time and find groups
  222. * present in both lists.
  223. *
  224. * A return value of 1 signifies that b has priority over a.
  225. * A return value of 0 signifies that the two marks have to be handled together.
  226. * A return value of -1 signifies that a has priority over b.
  227. */
  228. int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
  229. {
  230. if (a == b)
  231. return 0;
  232. if (!a)
  233. return 1;
  234. if (!b)
  235. return -1;
  236. if (a->priority < b->priority)
  237. return 1;
  238. if (a->priority > b->priority)
  239. return -1;
  240. if (a < b)
  241. return 1;
  242. return -1;
  243. }
  244. /* Add mark into proper place in given list of marks */
  245. int fsnotify_add_mark_list(struct hlist_head *head, struct fsnotify_mark *mark,
  246. int allow_dups)
  247. {
  248. struct fsnotify_mark *lmark, *last = NULL;
  249. int cmp;
  250. /* is mark the first mark? */
  251. if (hlist_empty(head)) {
  252. hlist_add_head_rcu(&mark->obj_list, head);
  253. return 0;
  254. }
  255. /* should mark be in the middle of the current list? */
  256. hlist_for_each_entry(lmark, head, obj_list) {
  257. last = lmark;
  258. if ((lmark->group == mark->group) && !allow_dups)
  259. return -EEXIST;
  260. cmp = fsnotify_compare_groups(lmark->group, mark->group);
  261. if (cmp >= 0) {
  262. hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
  263. return 0;
  264. }
  265. }
  266. BUG_ON(last == NULL);
  267. /* mark should be the last entry. last is the current last entry */
  268. hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
  269. return 0;
  270. }
  271. /*
  272. * Attach an initialized mark to a given group and fs object.
  273. * These marks may be used for the fsnotify backend to determine which
  274. * event types should be delivered to which group.
  275. */
  276. int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
  277. struct fsnotify_group *group, struct inode *inode,
  278. struct vfsmount *mnt, int allow_dups)
  279. {
  280. int ret = 0;
  281. BUG_ON(inode && mnt);
  282. BUG_ON(!inode && !mnt);
  283. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  284. /*
  285. * LOCKING ORDER!!!!
  286. * group->mark_mutex
  287. * mark->lock
  288. * inode->i_lock
  289. */
  290. spin_lock(&mark->lock);
  291. mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE;
  292. fsnotify_get_group(group);
  293. mark->group = group;
  294. list_add(&mark->g_list, &group->marks_list);
  295. atomic_inc(&group->num_marks);
  296. fsnotify_get_mark(mark); /* for i_list and g_list */
  297. if (inode) {
  298. ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
  299. if (ret)
  300. goto err;
  301. } else if (mnt) {
  302. ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
  303. if (ret)
  304. goto err;
  305. } else {
  306. BUG();
  307. }
  308. /* this will pin the object if appropriate */
  309. fsnotify_set_mark_mask_locked(mark, mark->mask);
  310. spin_unlock(&mark->lock);
  311. if (inode)
  312. __fsnotify_update_child_dentry_flags(inode);
  313. return ret;
  314. err:
  315. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  316. list_del_init(&mark->g_list);
  317. fsnotify_put_group(group);
  318. mark->group = NULL;
  319. atomic_dec(&group->num_marks);
  320. spin_unlock(&mark->lock);
  321. spin_lock(&destroy_lock);
  322. list_add(&mark->g_list, &destroy_list);
  323. spin_unlock(&destroy_lock);
  324. wake_up(&destroy_waitq);
  325. return ret;
  326. }
  327. int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
  328. struct inode *inode, struct vfsmount *mnt, int allow_dups)
  329. {
  330. int ret;
  331. mutex_lock(&group->mark_mutex);
  332. ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
  333. mutex_unlock(&group->mark_mutex);
  334. return ret;
  335. }
  336. /*
  337. * Given a list of marks, find the mark associated with given group. If found
  338. * take a reference to that mark and return it, else return NULL.
  339. */
  340. struct fsnotify_mark *fsnotify_find_mark(struct hlist_head *head,
  341. struct fsnotify_group *group)
  342. {
  343. struct fsnotify_mark *mark;
  344. hlist_for_each_entry(mark, head, obj_list) {
  345. if (mark->group == group) {
  346. fsnotify_get_mark(mark);
  347. return mark;
  348. }
  349. }
  350. return NULL;
  351. }
  352. /*
  353. * clear any marks in a group in which mark->flags & flags is true
  354. */
  355. void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
  356. unsigned int flags)
  357. {
  358. struct fsnotify_mark *lmark, *mark;
  359. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  360. list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
  361. if (mark->flags & flags) {
  362. fsnotify_get_mark(mark);
  363. fsnotify_destroy_mark_locked(mark, group);
  364. fsnotify_put_mark(mark);
  365. }
  366. }
  367. mutex_unlock(&group->mark_mutex);
  368. }
  369. /*
  370. * Given a group, destroy all of the marks associated with that group.
  371. */
  372. void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
  373. {
  374. fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1);
  375. }
  376. void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old)
  377. {
  378. assert_spin_locked(&old->lock);
  379. new->inode = old->inode;
  380. new->mnt = old->mnt;
  381. if (old->group)
  382. fsnotify_get_group(old->group);
  383. new->group = old->group;
  384. new->mask = old->mask;
  385. new->free_mark = old->free_mark;
  386. }
  387. /*
  388. * Nothing fancy, just initialize lists and locks and counters.
  389. */
  390. void fsnotify_init_mark(struct fsnotify_mark *mark,
  391. void (*free_mark)(struct fsnotify_mark *mark))
  392. {
  393. memset(mark, 0, sizeof(*mark));
  394. spin_lock_init(&mark->lock);
  395. atomic_set(&mark->refcnt, 1);
  396. mark->free_mark = free_mark;
  397. }
  398. static int fsnotify_mark_destroy(void *ignored)
  399. {
  400. struct fsnotify_mark *mark, *next;
  401. struct list_head private_destroy_list;
  402. for (;;) {
  403. spin_lock(&destroy_lock);
  404. /* exchange the list head */
  405. list_replace_init(&destroy_list, &private_destroy_list);
  406. spin_unlock(&destroy_lock);
  407. synchronize_srcu(&fsnotify_mark_srcu);
  408. list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
  409. list_del_init(&mark->g_list);
  410. fsnotify_put_mark(mark);
  411. }
  412. wait_event_interruptible(destroy_waitq, !list_empty(&destroy_list));
  413. }
  414. return 0;
  415. }
  416. static int __init fsnotify_mark_init(void)
  417. {
  418. struct task_struct *thread;
  419. thread = kthread_run(fsnotify_mark_destroy, NULL,
  420. "fsnotify_mark");
  421. if (IS_ERR(thread))
  422. panic("unable to start fsnotify mark destruction thread.");
  423. return 0;
  424. }
  425. device_initcall(fsnotify_mark_init);