mark.c 24 KB

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