mark.c 24 KB

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