mark.c 15 KB

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