mark.c 23 KB

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