mark.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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_workfn(struct work_struct *work);
  92. static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
  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. * Prepare mark for freeing and add it to the list of marks prepared for
  153. * freeing. The actual freeing must happen after SRCU period ends and the
  154. * caller is responsible for this.
  155. *
  156. * The function returns true if the mark was added to the list of marks for
  157. * freeing. The function returns false if someone else has already called
  158. * __fsnotify_free_mark() for the mark.
  159. */
  160. static bool __fsnotify_free_mark(struct fsnotify_mark *mark)
  161. {
  162. struct fsnotify_group *group = mark->group;
  163. spin_lock(&mark->lock);
  164. /* something else already called this function on this mark */
  165. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
  166. spin_unlock(&mark->lock);
  167. return false;
  168. }
  169. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  170. spin_unlock(&mark->lock);
  171. /*
  172. * Some groups like to know that marks are being freed. This is a
  173. * callback to the group function to let it know that this mark
  174. * is being freed.
  175. */
  176. if (group->ops->freeing_mark)
  177. group->ops->freeing_mark(mark, group);
  178. spin_lock(&destroy_lock);
  179. list_add(&mark->g_list, &destroy_list);
  180. spin_unlock(&destroy_lock);
  181. return true;
  182. }
  183. /*
  184. * Free fsnotify mark. The freeing is actually happening from a workqueue which
  185. * first waits for srcu period end. Caller must have a reference to the mark
  186. * or be protected by fsnotify_mark_srcu.
  187. */
  188. void fsnotify_free_mark(struct fsnotify_mark *mark)
  189. {
  190. if (__fsnotify_free_mark(mark)) {
  191. queue_delayed_work(system_unbound_wq, &reaper_work,
  192. FSNOTIFY_REAPER_DELAY);
  193. }
  194. }
  195. void fsnotify_destroy_mark(struct fsnotify_mark *mark,
  196. struct fsnotify_group *group)
  197. {
  198. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  199. fsnotify_detach_mark(mark);
  200. mutex_unlock(&group->mark_mutex);
  201. fsnotify_free_mark(mark);
  202. }
  203. void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock)
  204. {
  205. struct fsnotify_mark *mark;
  206. while (1) {
  207. /*
  208. * We have to be careful since we can race with e.g.
  209. * fsnotify_clear_marks_by_group() and once we drop 'lock',
  210. * mark can get removed from the obj_list and destroyed. But
  211. * we are holding mark reference so mark cannot be freed and
  212. * calling fsnotify_destroy_mark() more than once is fine.
  213. */
  214. spin_lock(lock);
  215. if (hlist_empty(head)) {
  216. spin_unlock(lock);
  217. break;
  218. }
  219. mark = hlist_entry(head->first, struct fsnotify_mark, obj_list);
  220. /*
  221. * We don't update i_fsnotify_mask / mnt_fsnotify_mask here
  222. * since inode / mount is going away anyway. So just remove
  223. * mark from the list.
  224. */
  225. hlist_del_init_rcu(&mark->obj_list);
  226. fsnotify_get_mark(mark);
  227. spin_unlock(lock);
  228. fsnotify_destroy_mark(mark, mark->group);
  229. fsnotify_put_mark(mark);
  230. }
  231. }
  232. void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
  233. {
  234. assert_spin_locked(&mark->lock);
  235. mark->mask = mask;
  236. if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
  237. fsnotify_set_inode_mark_mask_locked(mark, mask);
  238. }
  239. void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
  240. {
  241. assert_spin_locked(&mark->lock);
  242. mark->ignored_mask = mask;
  243. }
  244. /*
  245. * Sorting function for lists of fsnotify marks.
  246. *
  247. * Fanotify supports different notification classes (reflected as priority of
  248. * notification group). Events shall be passed to notification groups in
  249. * decreasing priority order. To achieve this marks in notification lists for
  250. * inodes and vfsmounts are sorted so that priorities of corresponding groups
  251. * are descending.
  252. *
  253. * Furthermore correct handling of the ignore mask requires processing inode
  254. * and vfsmount marks of each group together. Using the group address as
  255. * further sort criterion provides a unique sorting order and thus we can
  256. * merge inode and vfsmount lists of marks in linear time and find groups
  257. * present in both lists.
  258. *
  259. * A return value of 1 signifies that b has priority over a.
  260. * A return value of 0 signifies that the two marks have to be handled together.
  261. * A return value of -1 signifies that a has priority over b.
  262. */
  263. int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
  264. {
  265. if (a == b)
  266. return 0;
  267. if (!a)
  268. return 1;
  269. if (!b)
  270. return -1;
  271. if (a->priority < b->priority)
  272. return 1;
  273. if (a->priority > b->priority)
  274. return -1;
  275. if (a < b)
  276. return 1;
  277. return -1;
  278. }
  279. /* Add mark into proper place in given list of marks */
  280. int fsnotify_add_mark_list(struct hlist_head *head, struct fsnotify_mark *mark,
  281. int allow_dups)
  282. {
  283. struct fsnotify_mark *lmark, *last = NULL;
  284. int cmp;
  285. /* is mark the first mark? */
  286. if (hlist_empty(head)) {
  287. hlist_add_head_rcu(&mark->obj_list, head);
  288. return 0;
  289. }
  290. /* should mark be in the middle of the current list? */
  291. hlist_for_each_entry(lmark, head, obj_list) {
  292. last = lmark;
  293. if ((lmark->group == mark->group) && !allow_dups)
  294. return -EEXIST;
  295. cmp = fsnotify_compare_groups(lmark->group, mark->group);
  296. if (cmp >= 0) {
  297. hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
  298. return 0;
  299. }
  300. }
  301. BUG_ON(last == NULL);
  302. /* mark should be the last entry. last is the current last entry */
  303. hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
  304. return 0;
  305. }
  306. /*
  307. * Attach an initialized mark to a given group and fs object.
  308. * These marks may be used for the fsnotify backend to determine which
  309. * event types should be delivered to which group.
  310. */
  311. int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
  312. struct fsnotify_group *group, struct inode *inode,
  313. struct vfsmount *mnt, int allow_dups)
  314. {
  315. int ret = 0;
  316. BUG_ON(inode && mnt);
  317. BUG_ON(!inode && !mnt);
  318. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  319. /*
  320. * LOCKING ORDER!!!!
  321. * group->mark_mutex
  322. * mark->lock
  323. * inode->i_lock
  324. */
  325. spin_lock(&mark->lock);
  326. mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
  327. fsnotify_get_group(group);
  328. mark->group = group;
  329. list_add(&mark->g_list, &group->marks_list);
  330. atomic_inc(&group->num_marks);
  331. fsnotify_get_mark(mark); /* for i_list and g_list */
  332. if (inode) {
  333. ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
  334. if (ret)
  335. goto err;
  336. } else if (mnt) {
  337. ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
  338. if (ret)
  339. goto err;
  340. } else {
  341. BUG();
  342. }
  343. /* this will pin the object if appropriate */
  344. fsnotify_set_mark_mask_locked(mark, mark->mask);
  345. spin_unlock(&mark->lock);
  346. if (inode)
  347. __fsnotify_update_child_dentry_flags(inode);
  348. return ret;
  349. err:
  350. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  351. list_del_init(&mark->g_list);
  352. fsnotify_put_group(group);
  353. mark->group = NULL;
  354. atomic_dec(&group->num_marks);
  355. spin_unlock(&mark->lock);
  356. spin_lock(&destroy_lock);
  357. list_add(&mark->g_list, &destroy_list);
  358. spin_unlock(&destroy_lock);
  359. queue_delayed_work(system_unbound_wq, &reaper_work,
  360. FSNOTIFY_REAPER_DELAY);
  361. return ret;
  362. }
  363. int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
  364. struct inode *inode, struct vfsmount *mnt, int allow_dups)
  365. {
  366. int ret;
  367. mutex_lock(&group->mark_mutex);
  368. ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups);
  369. mutex_unlock(&group->mark_mutex);
  370. return ret;
  371. }
  372. /*
  373. * Given a list of marks, find the mark associated with given group. If found
  374. * take a reference to that mark and return it, else return NULL.
  375. */
  376. struct fsnotify_mark *fsnotify_find_mark(struct hlist_head *head,
  377. struct fsnotify_group *group)
  378. {
  379. struct fsnotify_mark *mark;
  380. hlist_for_each_entry(mark, head, obj_list) {
  381. if (mark->group == group) {
  382. fsnotify_get_mark(mark);
  383. return mark;
  384. }
  385. }
  386. return NULL;
  387. }
  388. /*
  389. * clear any marks in a group in which mark->flags & flags is true
  390. */
  391. void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
  392. unsigned int flags)
  393. {
  394. struct fsnotify_mark *lmark, *mark;
  395. LIST_HEAD(to_free);
  396. /*
  397. * We have to be really careful here. Anytime we drop mark_mutex, e.g.
  398. * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
  399. * to_free list so we have to use mark_mutex even when accessing that
  400. * list. And freeing mark requires us to drop mark_mutex. So we can
  401. * reliably free only the first mark in the list. That's why we first
  402. * move marks to free to to_free list in one go and then free marks in
  403. * to_free list one by one.
  404. */
  405. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  406. list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
  407. if (mark->flags & flags)
  408. list_move(&mark->g_list, &to_free);
  409. }
  410. mutex_unlock(&group->mark_mutex);
  411. while (1) {
  412. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  413. if (list_empty(&to_free)) {
  414. mutex_unlock(&group->mark_mutex);
  415. break;
  416. }
  417. mark = list_first_entry(&to_free, struct fsnotify_mark, g_list);
  418. fsnotify_get_mark(mark);
  419. fsnotify_detach_mark(mark);
  420. mutex_unlock(&group->mark_mutex);
  421. fsnotify_free_mark(mark);
  422. fsnotify_put_mark(mark);
  423. }
  424. }
  425. /*
  426. * Given a group, prepare for freeing all the marks associated with that group.
  427. * The marks are attached to the list of marks prepared for destruction, the
  428. * caller is responsible for freeing marks in that list after SRCU period has
  429. * ended.
  430. */
  431. void fsnotify_detach_group_marks(struct fsnotify_group *group)
  432. {
  433. struct fsnotify_mark *mark;
  434. while (1) {
  435. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  436. if (list_empty(&group->marks_list)) {
  437. mutex_unlock(&group->mark_mutex);
  438. break;
  439. }
  440. mark = list_first_entry(&group->marks_list,
  441. struct fsnotify_mark, g_list);
  442. fsnotify_get_mark(mark);
  443. fsnotify_detach_mark(mark);
  444. mutex_unlock(&group->mark_mutex);
  445. __fsnotify_free_mark(mark);
  446. fsnotify_put_mark(mark);
  447. }
  448. }
  449. void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old)
  450. {
  451. assert_spin_locked(&old->lock);
  452. new->inode = old->inode;
  453. new->mnt = old->mnt;
  454. if (old->group)
  455. fsnotify_get_group(old->group);
  456. new->group = old->group;
  457. new->mask = old->mask;
  458. new->free_mark = old->free_mark;
  459. }
  460. /*
  461. * Nothing fancy, just initialize lists and locks and counters.
  462. */
  463. void fsnotify_init_mark(struct fsnotify_mark *mark,
  464. void (*free_mark)(struct fsnotify_mark *mark))
  465. {
  466. memset(mark, 0, sizeof(*mark));
  467. spin_lock_init(&mark->lock);
  468. atomic_set(&mark->refcnt, 1);
  469. mark->free_mark = free_mark;
  470. }
  471. /*
  472. * Destroy all marks in destroy_list, waits for SRCU period to finish before
  473. * actually freeing marks.
  474. */
  475. void fsnotify_mark_destroy_list(void)
  476. {
  477. struct fsnotify_mark *mark, *next;
  478. struct list_head private_destroy_list;
  479. spin_lock(&destroy_lock);
  480. /* exchange the list head */
  481. list_replace_init(&destroy_list, &private_destroy_list);
  482. spin_unlock(&destroy_lock);
  483. synchronize_srcu(&fsnotify_mark_srcu);
  484. list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
  485. list_del_init(&mark->g_list);
  486. fsnotify_put_mark(mark);
  487. }
  488. }
  489. static void fsnotify_mark_destroy_workfn(struct work_struct *work)
  490. {
  491. fsnotify_mark_destroy_list();
  492. }