mark.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. void fsnotify_get_mark(struct fsnotify_mark *mark)
  89. {
  90. atomic_inc(&mark->refcnt);
  91. }
  92. void fsnotify_put_mark(struct fsnotify_mark *mark)
  93. {
  94. if (atomic_dec_and_test(&mark->refcnt)) {
  95. if (mark->group)
  96. fsnotify_put_group(mark->group);
  97. mark->free_mark(mark);
  98. }
  99. }
  100. /* Calculate mask of events for a list of marks */
  101. u32 fsnotify_recalc_mask(struct hlist_head *head)
  102. {
  103. u32 new_mask = 0;
  104. struct fsnotify_mark *mark;
  105. hlist_for_each_entry(mark, head, obj_list)
  106. new_mask |= mark->mask;
  107. return new_mask;
  108. }
  109. /*
  110. * Remove mark from inode / vfsmount list, group list, drop inode reference
  111. * if we got one.
  112. *
  113. * Must be called with group->mark_mutex held.
  114. */
  115. void fsnotify_detach_mark(struct fsnotify_mark *mark)
  116. {
  117. struct inode *inode = NULL;
  118. struct fsnotify_group *group = mark->group;
  119. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  120. spin_lock(&mark->lock);
  121. /* something else already called this function on this mark */
  122. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
  123. spin_unlock(&mark->lock);
  124. return;
  125. }
  126. mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
  127. if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
  128. inode = mark->inode;
  129. fsnotify_destroy_inode_mark(mark);
  130. } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
  131. fsnotify_destroy_vfsmount_mark(mark);
  132. else
  133. BUG();
  134. /*
  135. * Note that we didn't update flags telling whether inode cares about
  136. * what's happening with children. We update these flags from
  137. * __fsnotify_parent() lazily when next event happens on one of our
  138. * children.
  139. */
  140. list_del_init(&mark->g_list);
  141. spin_unlock(&mark->lock);
  142. if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
  143. iput(inode);
  144. atomic_dec(&group->num_marks);
  145. }
  146. static void
  147. fsnotify_mark_free_rcu(struct rcu_head *rcu)
  148. {
  149. struct fsnotify_mark *mark;
  150. mark = container_of(rcu, struct fsnotify_mark, g_rcu);
  151. fsnotify_put_mark(mark);
  152. }
  153. /*
  154. * Free fsnotify mark. The freeing is actually happening from a call_srcu
  155. * callback. Caller must have a reference to the mark or be protected by
  156. * fsnotify_mark_srcu.
  157. */
  158. void fsnotify_free_mark(struct fsnotify_mark *mark)
  159. {
  160. struct fsnotify_group *group = mark->group;
  161. spin_lock(&mark->lock);
  162. /* something else already called this function on this mark */
  163. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
  164. spin_unlock(&mark->lock);
  165. return;
  166. }
  167. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  168. spin_unlock(&mark->lock);
  169. call_srcu(&fsnotify_mark_srcu, &mark->g_rcu, fsnotify_mark_free_rcu);
  170. /*
  171. * Some groups like to know that marks are being freed. This is a
  172. * callback to the group function to let it know that this mark
  173. * is being freed.
  174. */
  175. if (group->ops->freeing_mark)
  176. group->ops->freeing_mark(mark, group);
  177. }
  178. void fsnotify_destroy_mark(struct fsnotify_mark *mark,
  179. struct fsnotify_group *group)
  180. {
  181. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  182. fsnotify_detach_mark(mark);
  183. mutex_unlock(&group->mark_mutex);
  184. fsnotify_free_mark(mark);
  185. }
  186. void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock)
  187. {
  188. struct fsnotify_mark *mark;
  189. while (1) {
  190. /*
  191. * We have to be careful since we can race with e.g.
  192. * fsnotify_clear_marks_by_group() and once we drop 'lock',
  193. * mark can get removed from the obj_list and destroyed. But
  194. * we are holding mark reference so mark cannot be freed and
  195. * calling fsnotify_destroy_mark() more than once is fine.
  196. */
  197. spin_lock(lock);
  198. if (hlist_empty(head)) {
  199. spin_unlock(lock);
  200. break;
  201. }
  202. mark = hlist_entry(head->first, struct fsnotify_mark, obj_list);
  203. /*
  204. * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
  205. * since inode / mount is going away anyway. So just remove
  206. * mark from the list.
  207. */
  208. hlist_del_init_rcu(&mark->obj_list);
  209. fsnotify_get_mark(mark);
  210. spin_unlock(lock);
  211. fsnotify_destroy_mark(mark, mark->group);
  212. fsnotify_put_mark(mark);
  213. }
  214. }
  215. void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
  216. {
  217. assert_spin_locked(&mark->lock);
  218. mark->mask = mask;
  219. if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
  220. fsnotify_set_inode_mark_mask_locked(mark, mask);
  221. }
  222. void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
  223. {
  224. assert_spin_locked(&mark->lock);
  225. mark->ignored_mask = mask;
  226. }
  227. /*
  228. * Sorting function for lists of fsnotify marks.
  229. *
  230. * Fanotify supports different notification classes (reflected as priority of
  231. * notification group). Events shall be passed to notification groups in
  232. * decreasing priority order. To achieve this marks in notification lists for
  233. * inodes and vfsmounts are sorted so that priorities of corresponding groups
  234. * are descending.
  235. *
  236. * Furthermore correct handling of the ignore mask requires processing inode
  237. * and vfsmount marks of each group together. Using the group address as
  238. * further sort criterion provides a unique sorting order and thus we can
  239. * merge inode and vfsmount lists of marks in linear time and find groups
  240. * present in both lists.
  241. *
  242. * A return value of 1 signifies that b has priority over a.
  243. * A return value of 0 signifies that the two marks have to be handled together.
  244. * A return value of -1 signifies that a has priority over b.
  245. */
  246. int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
  247. {
  248. if (a == b)
  249. return 0;
  250. if (!a)
  251. return 1;
  252. if (!b)
  253. return -1;
  254. if (a->priority < b->priority)
  255. return 1;
  256. if (a->priority > b->priority)
  257. return -1;
  258. if (a < b)
  259. return 1;
  260. return -1;
  261. }
  262. /* Add mark into proper place in given list of marks */
  263. int fsnotify_add_mark_list(struct hlist_head *head, struct fsnotify_mark *mark,
  264. int allow_dups)
  265. {
  266. struct fsnotify_mark *lmark, *last = NULL;
  267. int cmp;
  268. /* is mark the first mark? */
  269. if (hlist_empty(head)) {
  270. hlist_add_head_rcu(&mark->obj_list, head);
  271. return 0;
  272. }
  273. /* should mark be in the middle of the current list? */
  274. hlist_for_each_entry(lmark, head, obj_list) {
  275. last = lmark;
  276. if ((lmark->group == mark->group) && !allow_dups)
  277. return -EEXIST;
  278. cmp = fsnotify_compare_groups(lmark->group, mark->group);
  279. if (cmp >= 0) {
  280. hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
  281. return 0;
  282. }
  283. }
  284. BUG_ON(last == NULL);
  285. /* mark should be the last entry. last is the current last entry */
  286. hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
  287. return 0;
  288. }
  289. /*
  290. * Attach an initialized mark to a given group and fs object.
  291. * These marks may be used for the fsnotify backend to determine which
  292. * event types should be delivered to which group.
  293. */
  294. int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
  295. struct fsnotify_group *group, struct inode *inode,
  296. struct vfsmount *mnt, int allow_dups)
  297. {
  298. int ret = 0;
  299. BUG_ON(inode && mnt);
  300. BUG_ON(!inode && !mnt);
  301. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  302. /*
  303. * LOCKING ORDER!!!!
  304. * group->mark_mutex
  305. * mark->lock
  306. * inode->i_lock
  307. */
  308. spin_lock(&mark->lock);
  309. mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
  310. fsnotify_get_group(group);
  311. mark->group = group;
  312. list_add(&mark->g_list, &group->marks_list);
  313. atomic_inc(&group->num_marks);
  314. fsnotify_get_mark(mark); /* for i_list and g_list */
  315. if (inode) {
  316. ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
  317. if (ret)
  318. goto err;
  319. } else if (mnt) {
  320. ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
  321. if (ret)
  322. goto err;
  323. } else {
  324. BUG();
  325. }
  326. /* this will pin the object if appropriate */
  327. fsnotify_set_mark_mask_locked(mark, mark->mask);
  328. spin_unlock(&mark->lock);
  329. if (inode)
  330. __fsnotify_update_child_dentry_flags(inode);
  331. return ret;
  332. err:
  333. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  334. list_del_init(&mark->g_list);
  335. fsnotify_put_group(group);
  336. mark->group = NULL;
  337. atomic_dec(&group->num_marks);
  338. spin_unlock(&mark->lock);
  339. call_srcu(&fsnotify_mark_srcu, &mark->g_rcu, fsnotify_mark_free_rcu);
  340. return ret;
  341. }
  342. int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
  343. struct inode *inode, struct vfsmount *mnt, int allow_dups)
  344. {
  345. int ret;
  346. mutex_lock(&group->mark_mutex);
  347. ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
  348. mutex_unlock(&group->mark_mutex);
  349. return ret;
  350. }
  351. /*
  352. * Given a list of marks, find the mark associated with given group. If found
  353. * take a reference to that mark and return it, else return NULL.
  354. */
  355. struct fsnotify_mark *fsnotify_find_mark(struct hlist_head *head,
  356. struct fsnotify_group *group)
  357. {
  358. struct fsnotify_mark *mark;
  359. hlist_for_each_entry(mark, head, obj_list) {
  360. if (mark->group == group) {
  361. fsnotify_get_mark(mark);
  362. return mark;
  363. }
  364. }
  365. return NULL;
  366. }
  367. /*
  368. * clear any marks in a group in which mark->flags & flags is true
  369. */
  370. void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
  371. unsigned int flags)
  372. {
  373. struct fsnotify_mark *lmark, *mark;
  374. LIST_HEAD(to_free);
  375. /*
  376. * We have to be really careful here. Anytime we drop mark_mutex, e.g.
  377. * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
  378. * to_free list so we have to use mark_mutex even when accessing that
  379. * list. And freeing mark requires us to drop mark_mutex. So we can
  380. * reliably free only the first mark in the list. That's why we first
  381. * move marks to free to to_free list in one go and then free marks in
  382. * to_free list one by one.
  383. */
  384. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  385. list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
  386. if (mark->flags & flags)
  387. list_move(&mark->g_list, &to_free);
  388. }
  389. mutex_unlock(&group->mark_mutex);
  390. while (1) {
  391. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  392. if (list_empty(&to_free)) {
  393. mutex_unlock(&group->mark_mutex);
  394. break;
  395. }
  396. mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
  397. fsnotify_get_mark(mark);
  398. fsnotify_detach_mark(mark);
  399. mutex_unlock(&group->mark_mutex);
  400. fsnotify_free_mark(mark);
  401. fsnotify_put_mark(mark);
  402. }
  403. }
  404. /*
  405. * Given a group, destroy all of the marks associated with that group.
  406. */
  407. void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
  408. {
  409. fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1);
  410. }
  411. void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old)
  412. {
  413. assert_spin_locked(&old->lock);
  414. new->inode = old->inode;
  415. new->mnt = old->mnt;
  416. if (old->group)
  417. fsnotify_get_group(old->group);
  418. new->group = old->group;
  419. new->mask = old->mask;
  420. new->free_mark = old->free_mark;
  421. }
  422. /*
  423. * Nothing fancy, just initialize lists and locks and counters.
  424. */
  425. void fsnotify_init_mark(struct fsnotify_mark *mark,
  426. void (*free_mark)(struct fsnotify_mark *mark))
  427. {
  428. memset(mark, 0, sizeof(*mark));
  429. spin_lock_init(&mark->lock);
  430. atomic_set(&mark->refcnt, 1);
  431. mark->free_mark = free_mark;
  432. }