mark.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2008 Red Hat, Inc., Eric Paris <[email protected]>
  4. */
  5. /*
  6. * fsnotify inode mark locking/lifetime/and refcnting
  7. *
  8. * REFCNT:
  9. * The group->recnt and mark->refcnt tell how many "things" in the kernel
  10. * currently are referencing the objects. Both kind of objects typically will
  11. * live inside the kernel with a refcnt of 2, one for its creation and one for
  12. * the reference a group and a mark hold to each other.
  13. * If you are holding the appropriate locks, you can take a reference and the
  14. * object itself is guaranteed to survive until the reference is dropped.
  15. *
  16. * LOCKING:
  17. * There are 3 locks involved with fsnotify inode marks and they MUST be taken
  18. * in order as follows:
  19. *
  20. * group->mark_mutex
  21. * mark->lock
  22. * mark->connector->lock
  23. *
  24. * group->mark_mutex protects the marks_list anchored inside a given group and
  25. * each mark is hooked via the g_list. It also protects the groups private
  26. * data (i.e group limits).
  27. * mark->lock protects the marks attributes like its masks and flags.
  28. * Furthermore it protects the access to a reference of the group that the mark
  29. * is assigned to as well as the access to a reference of the inode/vfsmount
  30. * that is being watched by the mark.
  31. *
  32. * mark->connector->lock protects the list of marks anchored inside an
  33. * inode / vfsmount and each mark is hooked via the i_list.
  34. *
  35. * A list of notification marks relating to inode / mnt is contained in
  36. * fsnotify_mark_connector. That structure is alive as long as there are any
  37. * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets
  38. * detached from fsnotify_mark_connector when last reference to the mark is
  39. * dropped. Thus having mark reference is enough to protect mark->connector
  40. * pointer and to make sure fsnotify_mark_connector cannot disappear. Also
  41. * because we remove mark from g_list before dropping mark reference associated
  42. * with that, any mark found through g_list is guaranteed to have
  43. * mark->connector set until we drop group->mark_mutex.
  44. *
  45. * LIFETIME:
  46. * Inode marks survive between when they are added to an inode and when their
  47. * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
  48. *
  49. * The inode mark can be cleared for a number of different reasons including:
  50. * - The inode is unlinked for the last time. (fsnotify_inode_remove)
  51. * - The inode is being evicted from cache. (fsnotify_inode_delete)
  52. * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes)
  53. * - Something explicitly requests that it be removed. (fsnotify_destroy_mark)
  54. * - The fsnotify_group associated with the mark is going away and all such marks
  55. * need to be cleaned up. (fsnotify_clear_marks_by_group)
  56. *
  57. * This has the very interesting property of being able to run concurrently with
  58. * any (or all) other directions.
  59. */
  60. #include <linux/fs.h>
  61. #include <linux/init.h>
  62. #include <linux/kernel.h>
  63. #include <linux/kthread.h>
  64. #include <linux/module.h>
  65. #include <linux/mutex.h>
  66. #include <linux/slab.h>
  67. #include <linux/spinlock.h>
  68. #include <linux/srcu.h>
  69. #include <linux/ratelimit.h>
  70. #include <linux/atomic.h>
  71. #include <linux/fsnotify_backend.h>
  72. #include "fsnotify.h"
  73. #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */
  74. struct srcu_struct fsnotify_mark_srcu;
  75. struct kmem_cache *fsnotify_mark_connector_cachep;
  76. static DEFINE_SPINLOCK(destroy_lock);
  77. static LIST_HEAD(destroy_list);
  78. static struct fsnotify_mark_connector *connector_destroy_list;
  79. static void fsnotify_mark_destroy_workfn(struct work_struct *work);
  80. static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
  81. static void fsnotify_connector_destroy_workfn(struct work_struct *work);
  82. static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
  83. void fsnotify_get_mark(struct fsnotify_mark *mark)
  84. {
  85. WARN_ON_ONCE(!refcount_read(&mark->refcnt));
  86. refcount_inc(&mark->refcnt);
  87. }
  88. static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn)
  89. {
  90. if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
  91. return &fsnotify_conn_inode(conn)->i_fsnotify_mask;
  92. else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
  93. return &fsnotify_conn_mount(conn)->mnt_fsnotify_mask;
  94. else if (conn->type == FSNOTIFY_OBJ_TYPE_SB)
  95. return &fsnotify_conn_sb(conn)->s_fsnotify_mask;
  96. return NULL;
  97. }
  98. __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn)
  99. {
  100. if (WARN_ON(!fsnotify_valid_obj_type(conn->type)))
  101. return 0;
  102. return *fsnotify_conn_mask_p(conn);
  103. }
  104. static void fsnotify_get_inode_ref(struct inode *inode)
  105. {
  106. ihold(inode);
  107. atomic_long_inc(&inode->i_sb->s_fsnotify_connectors);
  108. }
  109. /*
  110. * Grab or drop inode reference for the connector if needed.
  111. *
  112. * When it's time to drop the reference, we only clear the HAS_IREF flag and
  113. * return the inode object. fsnotify_drop_object() will be resonsible for doing
  114. * iput() outside of spinlocks. This happens when last mark that wanted iref is
  115. * detached.
  116. */
  117. static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn,
  118. bool want_iref)
  119. {
  120. bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF;
  121. struct inode *inode = NULL;
  122. if (conn->type != FSNOTIFY_OBJ_TYPE_INODE ||
  123. want_iref == has_iref)
  124. return NULL;
  125. if (want_iref) {
  126. /* Pin inode if any mark wants inode refcount held */
  127. fsnotify_get_inode_ref(fsnotify_conn_inode(conn));
  128. conn->flags |= FSNOTIFY_CONN_FLAG_HAS_IREF;
  129. } else {
  130. /* Unpin inode after detach of last mark that wanted iref */
  131. inode = fsnotify_conn_inode(conn);
  132. conn->flags &= ~FSNOTIFY_CONN_FLAG_HAS_IREF;
  133. }
  134. return inode;
  135. }
  136. static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
  137. {
  138. u32 new_mask = 0;
  139. bool want_iref = false;
  140. struct fsnotify_mark *mark;
  141. assert_spin_locked(&conn->lock);
  142. /* We can get detached connector here when inode is getting unlinked. */
  143. if (!fsnotify_valid_obj_type(conn->type))
  144. return NULL;
  145. hlist_for_each_entry(mark, &conn->list, obj_list) {
  146. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED))
  147. continue;
  148. new_mask |= fsnotify_calc_mask(mark);
  149. if (conn->type == FSNOTIFY_OBJ_TYPE_INODE &&
  150. !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
  151. want_iref = true;
  152. }
  153. *fsnotify_conn_mask_p(conn) = new_mask;
  154. return fsnotify_update_iref(conn, want_iref);
  155. }
  156. /*
  157. * Calculate mask of events for a list of marks. The caller must make sure
  158. * connector and connector->obj cannot disappear under us. Callers achieve
  159. * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
  160. * list.
  161. */
  162. void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
  163. {
  164. if (!conn)
  165. return;
  166. spin_lock(&conn->lock);
  167. __fsnotify_recalc_mask(conn);
  168. spin_unlock(&conn->lock);
  169. if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
  170. __fsnotify_update_child_dentry_flags(
  171. fsnotify_conn_inode(conn));
  172. }
  173. /* Free all connectors queued for freeing once SRCU period ends */
  174. static void fsnotify_connector_destroy_workfn(struct work_struct *work)
  175. {
  176. struct fsnotify_mark_connector *conn, *free;
  177. spin_lock(&destroy_lock);
  178. conn = connector_destroy_list;
  179. connector_destroy_list = NULL;
  180. spin_unlock(&destroy_lock);
  181. synchronize_srcu(&fsnotify_mark_srcu);
  182. while (conn) {
  183. free = conn;
  184. conn = conn->destroy_next;
  185. kmem_cache_free(fsnotify_mark_connector_cachep, free);
  186. }
  187. }
  188. static void fsnotify_put_inode_ref(struct inode *inode)
  189. {
  190. struct super_block *sb = inode->i_sb;
  191. iput(inode);
  192. if (atomic_long_dec_and_test(&sb->s_fsnotify_connectors))
  193. wake_up_var(&sb->s_fsnotify_connectors);
  194. }
  195. static void fsnotify_get_sb_connectors(struct fsnotify_mark_connector *conn)
  196. {
  197. struct super_block *sb = fsnotify_connector_sb(conn);
  198. if (sb)
  199. atomic_long_inc(&sb->s_fsnotify_connectors);
  200. }
  201. static void fsnotify_put_sb_connectors(struct fsnotify_mark_connector *conn)
  202. {
  203. struct super_block *sb = fsnotify_connector_sb(conn);
  204. if (sb && atomic_long_dec_and_test(&sb->s_fsnotify_connectors))
  205. wake_up_var(&sb->s_fsnotify_connectors);
  206. }
  207. static void *fsnotify_detach_connector_from_object(
  208. struct fsnotify_mark_connector *conn,
  209. unsigned int *type)
  210. {
  211. struct inode *inode = NULL;
  212. *type = conn->type;
  213. if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED)
  214. return NULL;
  215. if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
  216. inode = fsnotify_conn_inode(conn);
  217. inode->i_fsnotify_mask = 0;
  218. /* Unpin inode when detaching from connector */
  219. if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF))
  220. inode = NULL;
  221. } else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
  222. fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
  223. } else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
  224. fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
  225. }
  226. fsnotify_put_sb_connectors(conn);
  227. rcu_assign_pointer(*(conn->obj), NULL);
  228. conn->obj = NULL;
  229. conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
  230. return inode;
  231. }
  232. static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
  233. {
  234. struct fsnotify_group *group = mark->group;
  235. if (WARN_ON_ONCE(!group))
  236. return;
  237. group->ops->free_mark(mark);
  238. fsnotify_put_group(group);
  239. }
  240. /* Drop object reference originally held by a connector */
  241. static void fsnotify_drop_object(unsigned int type, void *objp)
  242. {
  243. if (!objp)
  244. return;
  245. /* Currently only inode references are passed to be dropped */
  246. if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE))
  247. return;
  248. fsnotify_put_inode_ref(objp);
  249. }
  250. void fsnotify_put_mark(struct fsnotify_mark *mark)
  251. {
  252. struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector);
  253. void *objp = NULL;
  254. unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED;
  255. bool free_conn = false;
  256. /* Catch marks that were actually never attached to object */
  257. if (!conn) {
  258. if (refcount_dec_and_test(&mark->refcnt))
  259. fsnotify_final_mark_destroy(mark);
  260. return;
  261. }
  262. /*
  263. * We have to be careful so that traversals of obj_list under lock can
  264. * safely grab mark reference.
  265. */
  266. if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock))
  267. return;
  268. hlist_del_init_rcu(&mark->obj_list);
  269. if (hlist_empty(&conn->list)) {
  270. objp = fsnotify_detach_connector_from_object(conn, &type);
  271. free_conn = true;
  272. } else {
  273. objp = __fsnotify_recalc_mask(conn);
  274. type = conn->type;
  275. }
  276. WRITE_ONCE(mark->connector, NULL);
  277. spin_unlock(&conn->lock);
  278. fsnotify_drop_object(type, objp);
  279. if (free_conn) {
  280. spin_lock(&destroy_lock);
  281. conn->destroy_next = connector_destroy_list;
  282. connector_destroy_list = conn;
  283. spin_unlock(&destroy_lock);
  284. queue_work(system_unbound_wq, &connector_reaper_work);
  285. }
  286. /*
  287. * Note that we didn't update flags telling whether inode cares about
  288. * what's happening with children. We update these flags from
  289. * __fsnotify_parent() lazily when next event happens on one of our
  290. * children.
  291. */
  292. spin_lock(&destroy_lock);
  293. list_add(&mark->g_list, &destroy_list);
  294. spin_unlock(&destroy_lock);
  295. queue_delayed_work(system_unbound_wq, &reaper_work,
  296. FSNOTIFY_REAPER_DELAY);
  297. }
  298. EXPORT_SYMBOL_GPL(fsnotify_put_mark);
  299. /*
  300. * Get mark reference when we found the mark via lockless traversal of object
  301. * list. Mark can be already removed from the list by now and on its way to be
  302. * destroyed once SRCU period ends.
  303. *
  304. * Also pin the group so it doesn't disappear under us.
  305. */
  306. static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
  307. {
  308. if (!mark)
  309. return true;
  310. if (refcount_inc_not_zero(&mark->refcnt)) {
  311. spin_lock(&mark->lock);
  312. if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) {
  313. /* mark is attached, group is still alive then */
  314. atomic_inc(&mark->group->user_waits);
  315. spin_unlock(&mark->lock);
  316. return true;
  317. }
  318. spin_unlock(&mark->lock);
  319. fsnotify_put_mark(mark);
  320. }
  321. return false;
  322. }
  323. /*
  324. * Puts marks and wakes up group destruction if necessary.
  325. *
  326. * Pairs with fsnotify_get_mark_safe()
  327. */
  328. static void fsnotify_put_mark_wake(struct fsnotify_mark *mark)
  329. {
  330. if (mark) {
  331. struct fsnotify_group *group = mark->group;
  332. fsnotify_put_mark(mark);
  333. /*
  334. * We abuse notification_waitq on group shutdown for waiting for
  335. * all marks pinned when waiting for userspace.
  336. */
  337. if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
  338. wake_up(&group->notification_waitq);
  339. }
  340. }
  341. bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)
  342. __releases(&fsnotify_mark_srcu)
  343. {
  344. int type;
  345. fsnotify_foreach_iter_type(type) {
  346. /* This can fail if mark is being removed */
  347. if (!fsnotify_get_mark_safe(iter_info->marks[type])) {
  348. __release(&fsnotify_mark_srcu);
  349. goto fail;
  350. }
  351. }
  352. /*
  353. * Now that both marks are pinned by refcount in the inode / vfsmount
  354. * lists, we can drop SRCU lock, and safely resume the list iteration
  355. * once userspace returns.
  356. */
  357. srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx);
  358. return true;
  359. fail:
  360. for (type--; type >= 0; type--)
  361. fsnotify_put_mark_wake(iter_info->marks[type]);
  362. return false;
  363. }
  364. void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info)
  365. __acquires(&fsnotify_mark_srcu)
  366. {
  367. int type;
  368. iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
  369. fsnotify_foreach_iter_type(type)
  370. fsnotify_put_mark_wake(iter_info->marks[type]);
  371. }
  372. /*
  373. * Mark mark as detached, remove it from group list. Mark still stays in object
  374. * list until its last reference is dropped. Note that we rely on mark being
  375. * removed from group list before corresponding reference to it is dropped. In
  376. * particular we rely on mark->connector being valid while we hold
  377. * group->mark_mutex if we found the mark through g_list.
  378. *
  379. * Must be called with group->mark_mutex held. The caller must either hold
  380. * reference to the mark or be protected by fsnotify_mark_srcu.
  381. */
  382. void fsnotify_detach_mark(struct fsnotify_mark *mark)
  383. {
  384. fsnotify_group_assert_locked(mark->group);
  385. WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) &&
  386. refcount_read(&mark->refcnt) < 1 +
  387. !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
  388. spin_lock(&mark->lock);
  389. /* something else already called this function on this mark */
  390. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
  391. spin_unlock(&mark->lock);
  392. return;
  393. }
  394. mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
  395. list_del_init(&mark->g_list);
  396. spin_unlock(&mark->lock);
  397. /* Drop mark reference acquired in fsnotify_add_mark_locked() */
  398. fsnotify_put_mark(mark);
  399. }
  400. /*
  401. * Free fsnotify mark. The mark is actually only marked as being freed. The
  402. * freeing is actually happening only once last reference to the mark is
  403. * dropped from a workqueue which first waits for srcu period end.
  404. *
  405. * Caller must have a reference to the mark or be protected by
  406. * fsnotify_mark_srcu.
  407. */
  408. void fsnotify_free_mark(struct fsnotify_mark *mark)
  409. {
  410. struct fsnotify_group *group = mark->group;
  411. spin_lock(&mark->lock);
  412. /* something else already called this function on this mark */
  413. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
  414. spin_unlock(&mark->lock);
  415. return;
  416. }
  417. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  418. spin_unlock(&mark->lock);
  419. /*
  420. * Some groups like to know that marks are being freed. This is a
  421. * callback to the group function to let it know that this mark
  422. * is being freed.
  423. */
  424. if (group->ops->freeing_mark)
  425. group->ops->freeing_mark(mark, group);
  426. }
  427. void fsnotify_destroy_mark(struct fsnotify_mark *mark,
  428. struct fsnotify_group *group)
  429. {
  430. fsnotify_group_lock(group);
  431. fsnotify_detach_mark(mark);
  432. fsnotify_group_unlock(group);
  433. fsnotify_free_mark(mark);
  434. }
  435. EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
  436. /*
  437. * Sorting function for lists of fsnotify marks.
  438. *
  439. * Fanotify supports different notification classes (reflected as priority of
  440. * notification group). Events shall be passed to notification groups in
  441. * decreasing priority order. To achieve this marks in notification lists for
  442. * inodes and vfsmounts are sorted so that priorities of corresponding groups
  443. * are descending.
  444. *
  445. * Furthermore correct handling of the ignore mask requires processing inode
  446. * and vfsmount marks of each group together. Using the group address as
  447. * further sort criterion provides a unique sorting order and thus we can
  448. * merge inode and vfsmount lists of marks in linear time and find groups
  449. * present in both lists.
  450. *
  451. * A return value of 1 signifies that b has priority over a.
  452. * A return value of 0 signifies that the two marks have to be handled together.
  453. * A return value of -1 signifies that a has priority over b.
  454. */
  455. int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
  456. {
  457. if (a == b)
  458. return 0;
  459. if (!a)
  460. return 1;
  461. if (!b)
  462. return -1;
  463. if (a->priority < b->priority)
  464. return 1;
  465. if (a->priority > b->priority)
  466. return -1;
  467. if (a < b)
  468. return 1;
  469. return -1;
  470. }
  471. static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
  472. unsigned int obj_type,
  473. __kernel_fsid_t *fsid)
  474. {
  475. struct fsnotify_mark_connector *conn;
  476. conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
  477. if (!conn)
  478. return -ENOMEM;
  479. spin_lock_init(&conn->lock);
  480. INIT_HLIST_HEAD(&conn->list);
  481. conn->flags = 0;
  482. conn->type = obj_type;
  483. conn->obj = connp;
  484. /* Cache fsid of filesystem containing the object */
  485. if (fsid) {
  486. conn->fsid = *fsid;
  487. conn->flags = FSNOTIFY_CONN_FLAG_HAS_FSID;
  488. } else {
  489. conn->fsid.val[0] = conn->fsid.val[1] = 0;
  490. conn->flags = 0;
  491. }
  492. fsnotify_get_sb_connectors(conn);
  493. /*
  494. * cmpxchg() provides the barrier so that readers of *connp can see
  495. * only initialized structure
  496. */
  497. if (cmpxchg(connp, NULL, conn)) {
  498. /* Someone else created list structure for us */
  499. fsnotify_put_sb_connectors(conn);
  500. kmem_cache_free(fsnotify_mark_connector_cachep, conn);
  501. }
  502. return 0;
  503. }
  504. /*
  505. * Get mark connector, make sure it is alive and return with its lock held.
  506. * This is for users that get connector pointer from inode or mount. Users that
  507. * hold reference to a mark on the list may directly lock connector->lock as
  508. * they are sure list cannot go away under them.
  509. */
  510. static struct fsnotify_mark_connector *fsnotify_grab_connector(
  511. fsnotify_connp_t *connp)
  512. {
  513. struct fsnotify_mark_connector *conn;
  514. int idx;
  515. idx = srcu_read_lock(&fsnotify_mark_srcu);
  516. conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
  517. if (!conn)
  518. goto out;
  519. spin_lock(&conn->lock);
  520. if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
  521. spin_unlock(&conn->lock);
  522. srcu_read_unlock(&fsnotify_mark_srcu, idx);
  523. return NULL;
  524. }
  525. out:
  526. srcu_read_unlock(&fsnotify_mark_srcu, idx);
  527. return conn;
  528. }
  529. /*
  530. * Add mark into proper place in given list of marks. These marks may be used
  531. * for the fsnotify backend to determine which event types should be delivered
  532. * to which group and for which inodes. These marks are ordered according to
  533. * priority, highest number first, and then by the group's location in memory.
  534. */
  535. static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
  536. fsnotify_connp_t *connp,
  537. unsigned int obj_type,
  538. int add_flags, __kernel_fsid_t *fsid)
  539. {
  540. struct fsnotify_mark *lmark, *last = NULL;
  541. struct fsnotify_mark_connector *conn;
  542. int cmp;
  543. int err = 0;
  544. if (WARN_ON(!fsnotify_valid_obj_type(obj_type)))
  545. return -EINVAL;
  546. /* Backend is expected to check for zero fsid (e.g. tmpfs) */
  547. if (fsid && WARN_ON_ONCE(!fsid->val[0] && !fsid->val[1]))
  548. return -ENODEV;
  549. restart:
  550. spin_lock(&mark->lock);
  551. conn = fsnotify_grab_connector(connp);
  552. if (!conn) {
  553. spin_unlock(&mark->lock);
  554. err = fsnotify_attach_connector_to_object(connp, obj_type,
  555. fsid);
  556. if (err)
  557. return err;
  558. goto restart;
  559. } else if (fsid && !(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID)) {
  560. conn->fsid = *fsid;
  561. /* Pairs with smp_rmb() in fanotify_get_fsid() */
  562. smp_wmb();
  563. conn->flags |= FSNOTIFY_CONN_FLAG_HAS_FSID;
  564. } else if (fsid && (conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID) &&
  565. (fsid->val[0] != conn->fsid.val[0] ||
  566. fsid->val[1] != conn->fsid.val[1])) {
  567. /*
  568. * Backend is expected to check for non uniform fsid
  569. * (e.g. btrfs), but maybe we missed something?
  570. * Only allow setting conn->fsid once to non zero fsid.
  571. * inotify and non-fid fanotify groups do not set nor test
  572. * conn->fsid.
  573. */
  574. pr_warn_ratelimited("%s: fsid mismatch on object of type %u: "
  575. "%x.%x != %x.%x\n", __func__, conn->type,
  576. fsid->val[0], fsid->val[1],
  577. conn->fsid.val[0], conn->fsid.val[1]);
  578. err = -EXDEV;
  579. goto out_err;
  580. }
  581. /* is mark the first mark? */
  582. if (hlist_empty(&conn->list)) {
  583. hlist_add_head_rcu(&mark->obj_list, &conn->list);
  584. goto added;
  585. }
  586. /* should mark be in the middle of the current list? */
  587. hlist_for_each_entry(lmark, &conn->list, obj_list) {
  588. last = lmark;
  589. if ((lmark->group == mark->group) &&
  590. (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
  591. !(mark->group->flags & FSNOTIFY_GROUP_DUPS)) {
  592. err = -EEXIST;
  593. goto out_err;
  594. }
  595. cmp = fsnotify_compare_groups(lmark->group, mark->group);
  596. if (cmp >= 0) {
  597. hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
  598. goto added;
  599. }
  600. }
  601. BUG_ON(last == NULL);
  602. /* mark should be the last entry. last is the current last entry */
  603. hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
  604. added:
  605. /*
  606. * Since connector is attached to object using cmpxchg() we are
  607. * guaranteed that connector initialization is fully visible by anyone
  608. * seeing mark->connector set.
  609. */
  610. WRITE_ONCE(mark->connector, conn);
  611. out_err:
  612. spin_unlock(&conn->lock);
  613. spin_unlock(&mark->lock);
  614. return err;
  615. }
  616. /*
  617. * Attach an initialized mark to a given group and fs object.
  618. * These marks may be used for the fsnotify backend to determine which
  619. * event types should be delivered to which group.
  620. */
  621. int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
  622. fsnotify_connp_t *connp, unsigned int obj_type,
  623. int add_flags, __kernel_fsid_t *fsid)
  624. {
  625. struct fsnotify_group *group = mark->group;
  626. int ret = 0;
  627. fsnotify_group_assert_locked(group);
  628. /*
  629. * LOCKING ORDER!!!!
  630. * group->mark_mutex
  631. * mark->lock
  632. * mark->connector->lock
  633. */
  634. spin_lock(&mark->lock);
  635. mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
  636. list_add(&mark->g_list, &group->marks_list);
  637. fsnotify_get_mark(mark); /* for g_list */
  638. spin_unlock(&mark->lock);
  639. ret = fsnotify_add_mark_list(mark, connp, obj_type, add_flags, fsid);
  640. if (ret)
  641. goto err;
  642. fsnotify_recalc_mask(mark->connector);
  643. return ret;
  644. err:
  645. spin_lock(&mark->lock);
  646. mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
  647. FSNOTIFY_MARK_FLAG_ATTACHED);
  648. list_del_init(&mark->g_list);
  649. spin_unlock(&mark->lock);
  650. fsnotify_put_mark(mark);
  651. return ret;
  652. }
  653. int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
  654. unsigned int obj_type, int add_flags,
  655. __kernel_fsid_t *fsid)
  656. {
  657. int ret;
  658. struct fsnotify_group *group = mark->group;
  659. fsnotify_group_lock(group);
  660. ret = fsnotify_add_mark_locked(mark, connp, obj_type, add_flags, fsid);
  661. fsnotify_group_unlock(group);
  662. return ret;
  663. }
  664. EXPORT_SYMBOL_GPL(fsnotify_add_mark);
  665. /*
  666. * Given a list of marks, find the mark associated with given group. If found
  667. * take a reference to that mark and return it, else return NULL.
  668. */
  669. struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
  670. struct fsnotify_group *group)
  671. {
  672. struct fsnotify_mark_connector *conn;
  673. struct fsnotify_mark *mark;
  674. conn = fsnotify_grab_connector(connp);
  675. if (!conn)
  676. return NULL;
  677. hlist_for_each_entry(mark, &conn->list, obj_list) {
  678. if (mark->group == group &&
  679. (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
  680. fsnotify_get_mark(mark);
  681. spin_unlock(&conn->lock);
  682. return mark;
  683. }
  684. }
  685. spin_unlock(&conn->lock);
  686. return NULL;
  687. }
  688. EXPORT_SYMBOL_GPL(fsnotify_find_mark);
  689. /* Clear any marks in a group with given type mask */
  690. void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
  691. unsigned int obj_type)
  692. {
  693. struct fsnotify_mark *lmark, *mark;
  694. LIST_HEAD(to_free);
  695. struct list_head *head = &to_free;
  696. /* Skip selection step if we want to clear all marks. */
  697. if (obj_type == FSNOTIFY_OBJ_TYPE_ANY) {
  698. head = &group->marks_list;
  699. goto clear;
  700. }
  701. /*
  702. * We have to be really careful here. Anytime we drop mark_mutex, e.g.
  703. * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
  704. * to_free list so we have to use mark_mutex even when accessing that
  705. * list. And freeing mark requires us to drop mark_mutex. So we can
  706. * reliably free only the first mark in the list. That's why we first
  707. * move marks to free to to_free list in one go and then free marks in
  708. * to_free list one by one.
  709. */
  710. fsnotify_group_lock(group);
  711. list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
  712. if (mark->connector->type == obj_type)
  713. list_move(&mark->g_list, &to_free);
  714. }
  715. fsnotify_group_unlock(group);
  716. clear:
  717. while (1) {
  718. fsnotify_group_lock(group);
  719. if (list_empty(head)) {
  720. fsnotify_group_unlock(group);
  721. break;
  722. }
  723. mark = list_first_entry(head, struct fsnotify_mark, g_list);
  724. fsnotify_get_mark(mark);
  725. fsnotify_detach_mark(mark);
  726. fsnotify_group_unlock(group);
  727. fsnotify_free_mark(mark);
  728. fsnotify_put_mark(mark);
  729. }
  730. }
  731. /* Destroy all marks attached to an object via connector */
  732. void fsnotify_destroy_marks(fsnotify_connp_t *connp)
  733. {
  734. struct fsnotify_mark_connector *conn;
  735. struct fsnotify_mark *mark, *old_mark = NULL;
  736. void *objp;
  737. unsigned int type;
  738. conn = fsnotify_grab_connector(connp);
  739. if (!conn)
  740. return;
  741. /*
  742. * We have to be careful since we can race with e.g.
  743. * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
  744. * list can get modified. However we are holding mark reference and
  745. * thus our mark cannot be removed from obj_list so we can continue
  746. * iteration after regaining conn->lock.
  747. */
  748. hlist_for_each_entry(mark, &conn->list, obj_list) {
  749. fsnotify_get_mark(mark);
  750. spin_unlock(&conn->lock);
  751. if (old_mark)
  752. fsnotify_put_mark(old_mark);
  753. old_mark = mark;
  754. fsnotify_destroy_mark(mark, mark->group);
  755. spin_lock(&conn->lock);
  756. }
  757. /*
  758. * Detach list from object now so that we don't pin inode until all
  759. * mark references get dropped. It would lead to strange results such
  760. * as delaying inode deletion or blocking unmount.
  761. */
  762. objp = fsnotify_detach_connector_from_object(conn, &type);
  763. spin_unlock(&conn->lock);
  764. if (old_mark)
  765. fsnotify_put_mark(old_mark);
  766. fsnotify_drop_object(type, objp);
  767. }
  768. /*
  769. * Nothing fancy, just initialize lists and locks and counters.
  770. */
  771. void fsnotify_init_mark(struct fsnotify_mark *mark,
  772. struct fsnotify_group *group)
  773. {
  774. memset(mark, 0, sizeof(*mark));
  775. spin_lock_init(&mark->lock);
  776. refcount_set(&mark->refcnt, 1);
  777. fsnotify_get_group(group);
  778. mark->group = group;
  779. WRITE_ONCE(mark->connector, NULL);
  780. }
  781. EXPORT_SYMBOL_GPL(fsnotify_init_mark);
  782. /*
  783. * Destroy all marks in destroy_list, waits for SRCU period to finish before
  784. * actually freeing marks.
  785. */
  786. static void fsnotify_mark_destroy_workfn(struct work_struct *work)
  787. {
  788. struct fsnotify_mark *mark, *next;
  789. struct list_head private_destroy_list;
  790. spin_lock(&destroy_lock);
  791. /* exchange the list head */
  792. list_replace_init(&destroy_list, &private_destroy_list);
  793. spin_unlock(&destroy_lock);
  794. synchronize_srcu(&fsnotify_mark_srcu);
  795. list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
  796. list_del_init(&mark->g_list);
  797. fsnotify_final_mark_destroy(mark);
  798. }
  799. }
  800. /* Wait for all marks queued for destruction to be actually destroyed */
  801. void fsnotify_wait_marks_destroyed(void)
  802. {
  803. flush_delayed_work(&reaper_work);
  804. }
  805. EXPORT_SYMBOL_GPL(fsnotify_wait_marks_destroyed);