mark.c 24 KB

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