mark.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. * mark->connector->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. * mark->connector->lock protects the list of marks anchored inside an
  46. * inode / vfsmount and each mark is hooked via the i_list.
  47. *
  48. * A list of notification marks relating to inode / mnt is contained in
  49. * fsnotify_mark_connector. That structure is alive as long as there are any
  50. * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets
  51. * detached from fsnotify_mark_connector when last reference to the mark is
  52. * dropped. Thus having mark reference is enough to protect mark->connector
  53. * pointer and to make sure fsnotify_mark_connector cannot disappear. Also
  54. * because we remove mark from g_list before dropping mark reference associated
  55. * with that, any mark found through g_list is guaranteed to have
  56. * mark->connector set until we drop group->mark_mutex.
  57. *
  58. * LIFETIME:
  59. * Inode marks survive between when they are added to an inode and when their
  60. * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
  61. *
  62. * The inode mark can be cleared for a number of different reasons including:
  63. * - The inode is unlinked for the last time. (fsnotify_inode_remove)
  64. * - The inode is being evicted from cache. (fsnotify_inode_delete)
  65. * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes)
  66. * - Something explicitly requests that it be removed. (fsnotify_destroy_mark)
  67. * - The fsnotify_group associated with the mark is going away and all such marks
  68. * need to be cleaned up. (fsnotify_clear_marks_by_group)
  69. *
  70. * This has the very interesting property of being able to run concurrently with
  71. * any (or all) other directions.
  72. */
  73. #include <linux/fs.h>
  74. #include <linux/init.h>
  75. #include <linux/kernel.h>
  76. #include <linux/kthread.h>
  77. #include <linux/module.h>
  78. #include <linux/mutex.h>
  79. #include <linux/slab.h>
  80. #include <linux/spinlock.h>
  81. #include <linux/srcu.h>
  82. #include <linux/atomic.h>
  83. #include <linux/fsnotify_backend.h>
  84. #include "fsnotify.h"
  85. #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */
  86. struct srcu_struct fsnotify_mark_srcu;
  87. struct kmem_cache *fsnotify_mark_connector_cachep;
  88. static DEFINE_SPINLOCK(destroy_lock);
  89. static LIST_HEAD(destroy_list);
  90. static struct fsnotify_mark_connector *connector_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. static void fsnotify_connector_destroy_workfn(struct work_struct *work);
  94. static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
  95. void fsnotify_get_mark(struct fsnotify_mark *mark)
  96. {
  97. WARN_ON_ONCE(!atomic_read(&mark->refcnt));
  98. atomic_inc(&mark->refcnt);
  99. }
  100. /*
  101. * Get mark reference when we found the mark via lockless traversal of object
  102. * list. Mark can be already removed from the list by now and on its way to be
  103. * destroyed once SRCU period ends.
  104. */
  105. static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
  106. {
  107. return atomic_inc_not_zero(&mark->refcnt);
  108. }
  109. static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
  110. {
  111. u32 new_mask = 0;
  112. struct fsnotify_mark *mark;
  113. assert_spin_locked(&conn->lock);
  114. hlist_for_each_entry(mark, &conn->list, obj_list) {
  115. if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)
  116. new_mask |= mark->mask;
  117. }
  118. if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
  119. conn->inode->i_fsnotify_mask = new_mask;
  120. else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
  121. real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask;
  122. }
  123. /*
  124. * Calculate mask of events for a list of marks. The caller must make sure
  125. * connector and connector->inode cannot disappear under us. Callers achieve
  126. * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
  127. * list.
  128. */
  129. void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
  130. {
  131. if (!conn)
  132. return;
  133. spin_lock(&conn->lock);
  134. __fsnotify_recalc_mask(conn);
  135. spin_unlock(&conn->lock);
  136. if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
  137. __fsnotify_update_child_dentry_flags(conn->inode);
  138. }
  139. /* Free all connectors queued for freeing once SRCU period ends */
  140. static void fsnotify_connector_destroy_workfn(struct work_struct *work)
  141. {
  142. struct fsnotify_mark_connector *conn, *free;
  143. spin_lock(&destroy_lock);
  144. conn = connector_destroy_list;
  145. connector_destroy_list = NULL;
  146. spin_unlock(&destroy_lock);
  147. synchronize_srcu(&fsnotify_mark_srcu);
  148. while (conn) {
  149. free = conn;
  150. conn = conn->destroy_next;
  151. kmem_cache_free(fsnotify_mark_connector_cachep, free);
  152. }
  153. }
  154. static struct inode *fsnotify_detach_connector_from_object(
  155. struct fsnotify_mark_connector *conn)
  156. {
  157. struct inode *inode = NULL;
  158. if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) {
  159. inode = conn->inode;
  160. rcu_assign_pointer(inode->i_fsnotify_marks, NULL);
  161. inode->i_fsnotify_mask = 0;
  162. conn->inode = NULL;
  163. conn->flags &= ~FSNOTIFY_OBJ_TYPE_INODE;
  164. } else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
  165. rcu_assign_pointer(real_mount(conn->mnt)->mnt_fsnotify_marks,
  166. NULL);
  167. real_mount(conn->mnt)->mnt_fsnotify_mask = 0;
  168. conn->mnt = NULL;
  169. conn->flags &= ~FSNOTIFY_OBJ_TYPE_VFSMOUNT;
  170. }
  171. return inode;
  172. }
  173. static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
  174. {
  175. struct fsnotify_group *group = mark->group;
  176. if (WARN_ON_ONCE(!group))
  177. return;
  178. group->ops->free_mark(mark);
  179. fsnotify_put_group(group);
  180. }
  181. void fsnotify_put_mark(struct fsnotify_mark *mark)
  182. {
  183. struct fsnotify_mark_connector *conn;
  184. struct inode *inode = NULL;
  185. bool free_conn = false;
  186. /* Catch marks that were actually never attached to object */
  187. if (!mark->connector) {
  188. if (atomic_dec_and_test(&mark->refcnt))
  189. fsnotify_final_mark_destroy(mark);
  190. return;
  191. }
  192. /*
  193. * We have to be careful so that traversals of obj_list under lock can
  194. * safely grab mark reference.
  195. */
  196. if (!atomic_dec_and_lock(&mark->refcnt, &mark->connector->lock))
  197. return;
  198. conn = mark->connector;
  199. hlist_del_init_rcu(&mark->obj_list);
  200. if (hlist_empty(&conn->list)) {
  201. inode = fsnotify_detach_connector_from_object(conn);
  202. free_conn = true;
  203. } else {
  204. __fsnotify_recalc_mask(conn);
  205. }
  206. mark->connector = NULL;
  207. spin_unlock(&conn->lock);
  208. iput(inode);
  209. if (free_conn) {
  210. spin_lock(&destroy_lock);
  211. conn->destroy_next = connector_destroy_list;
  212. connector_destroy_list = conn;
  213. spin_unlock(&destroy_lock);
  214. queue_work(system_unbound_wq, &connector_reaper_work);
  215. }
  216. /*
  217. * Note that we didn't update flags telling whether inode cares about
  218. * what's happening with children. We update these flags from
  219. * __fsnotify_parent() lazily when next event happens on one of our
  220. * children.
  221. */
  222. spin_lock(&destroy_lock);
  223. list_add(&mark->g_list, &destroy_list);
  224. spin_unlock(&destroy_lock);
  225. queue_delayed_work(system_unbound_wq, &reaper_work,
  226. FSNOTIFY_REAPER_DELAY);
  227. }
  228. bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)
  229. {
  230. struct fsnotify_group *group;
  231. if (WARN_ON_ONCE(!iter_info->inode_mark && !iter_info->vfsmount_mark))
  232. return false;
  233. if (iter_info->inode_mark)
  234. group = iter_info->inode_mark->group;
  235. else
  236. group = iter_info->vfsmount_mark->group;
  237. /*
  238. * Since acquisition of mark reference is an atomic op as well, we can
  239. * be sure this inc is seen before any effect of refcount increment.
  240. */
  241. atomic_inc(&group->user_waits);
  242. if (iter_info->inode_mark) {
  243. /* This can fail if mark is being removed */
  244. if (!fsnotify_get_mark_safe(iter_info->inode_mark))
  245. goto out_wait;
  246. }
  247. if (iter_info->vfsmount_mark) {
  248. if (!fsnotify_get_mark_safe(iter_info->vfsmount_mark))
  249. goto out_inode;
  250. }
  251. /*
  252. * Now that both marks are pinned by refcount in the inode / vfsmount
  253. * lists, we can drop SRCU lock, and safely resume the list iteration
  254. * once userspace returns.
  255. */
  256. srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx);
  257. return true;
  258. out_inode:
  259. if (iter_info->inode_mark)
  260. fsnotify_put_mark(iter_info->inode_mark);
  261. out_wait:
  262. if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
  263. wake_up(&group->notification_waitq);
  264. return false;
  265. }
  266. void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info)
  267. {
  268. struct fsnotify_group *group = NULL;
  269. iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
  270. if (iter_info->inode_mark) {
  271. group = iter_info->inode_mark->group;
  272. fsnotify_put_mark(iter_info->inode_mark);
  273. }
  274. if (iter_info->vfsmount_mark) {
  275. group = iter_info->vfsmount_mark->group;
  276. fsnotify_put_mark(iter_info->vfsmount_mark);
  277. }
  278. /*
  279. * We abuse notification_waitq on group shutdown for waiting for all
  280. * marks pinned when waiting for userspace.
  281. */
  282. if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
  283. wake_up(&group->notification_waitq);
  284. }
  285. /*
  286. * Mark mark as detached, remove it from group list. Mark still stays in object
  287. * list until its last reference is dropped. Note that we rely on mark being
  288. * removed from group list before corresponding reference to it is dropped. In
  289. * particular we rely on mark->connector being valid while we hold
  290. * group->mark_mutex if we found the mark through g_list.
  291. *
  292. * Must be called with group->mark_mutex held. The caller must either hold
  293. * reference to the mark or be protected by fsnotify_mark_srcu.
  294. */
  295. void fsnotify_detach_mark(struct fsnotify_mark *mark)
  296. {
  297. struct fsnotify_group *group = mark->group;
  298. WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
  299. WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) &&
  300. atomic_read(&mark->refcnt) < 1 +
  301. !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
  302. spin_lock(&mark->lock);
  303. /* something else already called this function on this mark */
  304. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
  305. spin_unlock(&mark->lock);
  306. return;
  307. }
  308. mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
  309. list_del_init(&mark->g_list);
  310. spin_unlock(&mark->lock);
  311. atomic_dec(&group->num_marks);
  312. /* Drop mark reference acquired in fsnotify_add_mark_locked() */
  313. fsnotify_put_mark(mark);
  314. }
  315. /*
  316. * Free fsnotify mark. The mark is actually only marked as being freed. The
  317. * freeing is actually happening only once last reference to the mark is
  318. * dropped from a workqueue which first waits for srcu period end.
  319. *
  320. * Caller must have a reference to the mark or be protected by
  321. * fsnotify_mark_srcu.
  322. */
  323. void fsnotify_free_mark(struct fsnotify_mark *mark)
  324. {
  325. struct fsnotify_group *group = mark->group;
  326. spin_lock(&mark->lock);
  327. /* something else already called this function on this mark */
  328. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
  329. spin_unlock(&mark->lock);
  330. return;
  331. }
  332. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  333. spin_unlock(&mark->lock);
  334. /*
  335. * Some groups like to know that marks are being freed. This is a
  336. * callback to the group function to let it know that this mark
  337. * is being freed.
  338. */
  339. if (group->ops->freeing_mark)
  340. group->ops->freeing_mark(mark, group);
  341. }
  342. void fsnotify_destroy_mark(struct fsnotify_mark *mark,
  343. struct fsnotify_group *group)
  344. {
  345. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  346. fsnotify_detach_mark(mark);
  347. mutex_unlock(&group->mark_mutex);
  348. fsnotify_free_mark(mark);
  349. }
  350. /*
  351. * Sorting function for lists of fsnotify marks.
  352. *
  353. * Fanotify supports different notification classes (reflected as priority of
  354. * notification group). Events shall be passed to notification groups in
  355. * decreasing priority order. To achieve this marks in notification lists for
  356. * inodes and vfsmounts are sorted so that priorities of corresponding groups
  357. * are descending.
  358. *
  359. * Furthermore correct handling of the ignore mask requires processing inode
  360. * and vfsmount marks of each group together. Using the group address as
  361. * further sort criterion provides a unique sorting order and thus we can
  362. * merge inode and vfsmount lists of marks in linear time and find groups
  363. * present in both lists.
  364. *
  365. * A return value of 1 signifies that b has priority over a.
  366. * A return value of 0 signifies that the two marks have to be handled together.
  367. * A return value of -1 signifies that a has priority over b.
  368. */
  369. int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
  370. {
  371. if (a == b)
  372. return 0;
  373. if (!a)
  374. return 1;
  375. if (!b)
  376. return -1;
  377. if (a->priority < b->priority)
  378. return 1;
  379. if (a->priority > b->priority)
  380. return -1;
  381. if (a < b)
  382. return 1;
  383. return -1;
  384. }
  385. static int fsnotify_attach_connector_to_object(
  386. struct fsnotify_mark_connector __rcu **connp,
  387. struct inode *inode,
  388. struct vfsmount *mnt)
  389. {
  390. struct fsnotify_mark_connector *conn;
  391. conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
  392. if (!conn)
  393. return -ENOMEM;
  394. spin_lock_init(&conn->lock);
  395. INIT_HLIST_HEAD(&conn->list);
  396. if (inode) {
  397. conn->flags = FSNOTIFY_OBJ_TYPE_INODE;
  398. conn->inode = igrab(inode);
  399. } else {
  400. conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
  401. conn->mnt = mnt;
  402. }
  403. /*
  404. * cmpxchg() provides the barrier so that readers of *connp can see
  405. * only initialized structure
  406. */
  407. if (cmpxchg(connp, NULL, conn)) {
  408. /* Someone else created list structure for us */
  409. if (inode)
  410. iput(inode);
  411. kmem_cache_free(fsnotify_mark_connector_cachep, conn);
  412. }
  413. return 0;
  414. }
  415. /*
  416. * Get mark connector, make sure it is alive and return with its lock held.
  417. * This is for users that get connector pointer from inode or mount. Users that
  418. * hold reference to a mark on the list may directly lock connector->lock as
  419. * they are sure list cannot go away under them.
  420. */
  421. static struct fsnotify_mark_connector *fsnotify_grab_connector(
  422. struct fsnotify_mark_connector __rcu **connp)
  423. {
  424. struct fsnotify_mark_connector *conn;
  425. int idx;
  426. idx = srcu_read_lock(&fsnotify_mark_srcu);
  427. conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
  428. if (!conn)
  429. goto out;
  430. spin_lock(&conn->lock);
  431. if (!(conn->flags & (FSNOTIFY_OBJ_TYPE_INODE |
  432. FSNOTIFY_OBJ_TYPE_VFSMOUNT))) {
  433. spin_unlock(&conn->lock);
  434. srcu_read_unlock(&fsnotify_mark_srcu, idx);
  435. return NULL;
  436. }
  437. out:
  438. srcu_read_unlock(&fsnotify_mark_srcu, idx);
  439. return conn;
  440. }
  441. /*
  442. * Add mark into proper place in given list of marks. These marks may be used
  443. * for the fsnotify backend to determine which event types should be delivered
  444. * to which group and for which inodes. These marks are ordered according to
  445. * priority, highest number first, and then by the group's location in memory.
  446. */
  447. static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
  448. struct inode *inode, struct vfsmount *mnt,
  449. int allow_dups)
  450. {
  451. struct fsnotify_mark *lmark, *last = NULL;
  452. struct fsnotify_mark_connector *conn;
  453. struct fsnotify_mark_connector __rcu **connp;
  454. int cmp;
  455. int err = 0;
  456. if (WARN_ON(!inode && !mnt))
  457. return -EINVAL;
  458. if (inode)
  459. connp = &inode->i_fsnotify_marks;
  460. else
  461. connp = &real_mount(mnt)->mnt_fsnotify_marks;
  462. restart:
  463. spin_lock(&mark->lock);
  464. conn = fsnotify_grab_connector(connp);
  465. if (!conn) {
  466. spin_unlock(&mark->lock);
  467. err = fsnotify_attach_connector_to_object(connp, inode, mnt);
  468. if (err)
  469. return err;
  470. goto restart;
  471. }
  472. /* is mark the first mark? */
  473. if (hlist_empty(&conn->list)) {
  474. hlist_add_head_rcu(&mark->obj_list, &conn->list);
  475. goto added;
  476. }
  477. /* should mark be in the middle of the current list? */
  478. hlist_for_each_entry(lmark, &conn->list, obj_list) {
  479. last = lmark;
  480. if ((lmark->group == mark->group) &&
  481. (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
  482. !allow_dups) {
  483. err = -EEXIST;
  484. goto out_err;
  485. }
  486. cmp = fsnotify_compare_groups(lmark->group, mark->group);
  487. if (cmp >= 0) {
  488. hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
  489. goto added;
  490. }
  491. }
  492. BUG_ON(last == NULL);
  493. /* mark should be the last entry. last is the current last entry */
  494. hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
  495. added:
  496. mark->connector = conn;
  497. out_err:
  498. spin_unlock(&conn->lock);
  499. spin_unlock(&mark->lock);
  500. return err;
  501. }
  502. /*
  503. * Attach an initialized mark to a given group and fs object.
  504. * These marks may be used for the fsnotify backend to determine which
  505. * event types should be delivered to which group.
  506. */
  507. int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
  508. struct vfsmount *mnt, int allow_dups)
  509. {
  510. struct fsnotify_group *group = mark->group;
  511. int ret = 0;
  512. BUG_ON(inode && mnt);
  513. BUG_ON(!inode && !mnt);
  514. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  515. /*
  516. * LOCKING ORDER!!!!
  517. * group->mark_mutex
  518. * mark->lock
  519. * mark->connector->lock
  520. */
  521. spin_lock(&mark->lock);
  522. mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
  523. list_add(&mark->g_list, &group->marks_list);
  524. atomic_inc(&group->num_marks);
  525. fsnotify_get_mark(mark); /* for g_list */
  526. spin_unlock(&mark->lock);
  527. ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups);
  528. if (ret)
  529. goto err;
  530. if (mark->mask)
  531. fsnotify_recalc_mask(mark->connector);
  532. return ret;
  533. err:
  534. mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
  535. FSNOTIFY_MARK_FLAG_ATTACHED);
  536. list_del_init(&mark->g_list);
  537. atomic_dec(&group->num_marks);
  538. fsnotify_put_mark(mark);
  539. return ret;
  540. }
  541. int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
  542. struct vfsmount *mnt, int allow_dups)
  543. {
  544. int ret;
  545. struct fsnotify_group *group = mark->group;
  546. mutex_lock(&group->mark_mutex);
  547. ret = fsnotify_add_mark_locked(mark, inode, mnt, allow_dups);
  548. mutex_unlock(&group->mark_mutex);
  549. return ret;
  550. }
  551. /*
  552. * Given a list of marks, find the mark associated with given group. If found
  553. * take a reference to that mark and return it, else return NULL.
  554. */
  555. struct fsnotify_mark *fsnotify_find_mark(
  556. struct fsnotify_mark_connector __rcu **connp,
  557. struct fsnotify_group *group)
  558. {
  559. struct fsnotify_mark_connector *conn;
  560. struct fsnotify_mark *mark;
  561. conn = fsnotify_grab_connector(connp);
  562. if (!conn)
  563. return NULL;
  564. hlist_for_each_entry(mark, &conn->list, obj_list) {
  565. if (mark->group == group &&
  566. (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
  567. fsnotify_get_mark(mark);
  568. spin_unlock(&conn->lock);
  569. return mark;
  570. }
  571. }
  572. spin_unlock(&conn->lock);
  573. return NULL;
  574. }
  575. /* Clear any marks in a group with given type */
  576. void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
  577. unsigned int type)
  578. {
  579. struct fsnotify_mark *lmark, *mark;
  580. LIST_HEAD(to_free);
  581. struct list_head *head = &to_free;
  582. /* Skip selection step if we want to clear all marks. */
  583. if (type == FSNOTIFY_OBJ_ALL_TYPES) {
  584. head = &group->marks_list;
  585. goto clear;
  586. }
  587. /*
  588. * We have to be really careful here. Anytime we drop mark_mutex, e.g.
  589. * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
  590. * to_free list so we have to use mark_mutex even when accessing that
  591. * list. And freeing mark requires us to drop mark_mutex. So we can
  592. * reliably free only the first mark in the list. That's why we first
  593. * move marks to free to to_free list in one go and then free marks in
  594. * to_free list one by one.
  595. */
  596. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  597. list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
  598. if (mark->connector->flags & type)
  599. list_move(&mark->g_list, &to_free);
  600. }
  601. mutex_unlock(&group->mark_mutex);
  602. clear:
  603. while (1) {
  604. mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
  605. if (list_empty(head)) {
  606. mutex_unlock(&group->mark_mutex);
  607. break;
  608. }
  609. mark = list_first_entry(head, struct fsnotify_mark, g_list);
  610. fsnotify_get_mark(mark);
  611. fsnotify_detach_mark(mark);
  612. mutex_unlock(&group->mark_mutex);
  613. fsnotify_free_mark(mark);
  614. fsnotify_put_mark(mark);
  615. }
  616. }
  617. /* Destroy all marks attached to inode / vfsmount */
  618. void fsnotify_destroy_marks(struct fsnotify_mark_connector __rcu **connp)
  619. {
  620. struct fsnotify_mark_connector *conn;
  621. struct fsnotify_mark *mark, *old_mark = NULL;
  622. struct inode *inode;
  623. conn = fsnotify_grab_connector(connp);
  624. if (!conn)
  625. return;
  626. /*
  627. * We have to be careful since we can race with e.g.
  628. * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
  629. * list can get modified. However we are holding mark reference and
  630. * thus our mark cannot be removed from obj_list so we can continue
  631. * iteration after regaining conn->lock.
  632. */
  633. hlist_for_each_entry(mark, &conn->list, obj_list) {
  634. fsnotify_get_mark(mark);
  635. spin_unlock(&conn->lock);
  636. if (old_mark)
  637. fsnotify_put_mark(old_mark);
  638. old_mark = mark;
  639. fsnotify_destroy_mark(mark, mark->group);
  640. spin_lock(&conn->lock);
  641. }
  642. /*
  643. * Detach list from object now so that we don't pin inode until all
  644. * mark references get dropped. It would lead to strange results such
  645. * as delaying inode deletion or blocking unmount.
  646. */
  647. inode = fsnotify_detach_connector_from_object(conn);
  648. spin_unlock(&conn->lock);
  649. if (old_mark)
  650. fsnotify_put_mark(old_mark);
  651. iput(inode);
  652. }
  653. /*
  654. * Nothing fancy, just initialize lists and locks and counters.
  655. */
  656. void fsnotify_init_mark(struct fsnotify_mark *mark,
  657. struct fsnotify_group *group)
  658. {
  659. memset(mark, 0, sizeof(*mark));
  660. spin_lock_init(&mark->lock);
  661. atomic_set(&mark->refcnt, 1);
  662. fsnotify_get_group(group);
  663. mark->group = group;
  664. }
  665. /*
  666. * Destroy all marks in destroy_list, waits for SRCU period to finish before
  667. * actually freeing marks.
  668. */
  669. static void fsnotify_mark_destroy_workfn(struct work_struct *work)
  670. {
  671. struct fsnotify_mark *mark, *next;
  672. struct list_head private_destroy_list;
  673. spin_lock(&destroy_lock);
  674. /* exchange the list head */
  675. list_replace_init(&destroy_list, &private_destroy_list);
  676. spin_unlock(&destroy_lock);
  677. synchronize_srcu(&fsnotify_mark_srcu);
  678. list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
  679. list_del_init(&mark->g_list);
  680. fsnotify_final_mark_destroy(mark);
  681. }
  682. }
  683. /* Wait for all marks queued for destruction to be actually destroyed */
  684. void fsnotify_wait_marks_destroyed(void)
  685. {
  686. flush_delayed_work(&reaper_work);
  687. }