mark.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
  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. static struct kmem_cache *fsnotify_mark_connector_cachep;
  76. static struct kmem_cache *fsnotify_inode_mark_connector_cachep;
  77. static DEFINE_SPINLOCK(destroy_lock);
  78. static LIST_HEAD(destroy_list);
  79. static struct fsnotify_mark_connector *connector_destroy_list;
  80. static void fsnotify_mark_destroy_workfn(struct work_struct *work);
  81. static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
  82. static void fsnotify_connector_destroy_workfn(struct work_struct *work);
  83. static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
  84. void fsnotify_get_mark(struct fsnotify_mark *mark)
  85. {
  86. WARN_ON_ONCE(!refcount_read(&mark->refcnt));
  87. refcount_inc(&mark->refcnt);
  88. }
  89. static fsnotify_connp_t *fsnotify_object_connp(void *obj,
  90. enum fsnotify_obj_type obj_type)
  91. {
  92. switch (obj_type) {
  93. case FSNOTIFY_OBJ_TYPE_INODE:
  94. return &((struct inode *)obj)->i_fsnotify_marks;
  95. case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
  96. return &real_mount(obj)->mnt_fsnotify_marks;
  97. case FSNOTIFY_OBJ_TYPE_SB:
  98. return fsnotify_sb_marks(obj);
  99. case FSNOTIFY_OBJ_TYPE_MNTNS:
  100. return &((struct mnt_namespace *)obj)->n_fsnotify_marks;
  101. default:
  102. return NULL;
  103. }
  104. }
  105. static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn)
  106. {
  107. if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
  108. return &fsnotify_conn_inode(conn)->i_fsnotify_mask;
  109. else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
  110. return &fsnotify_conn_mount(conn)->mnt_fsnotify_mask;
  111. else if (conn->type == FSNOTIFY_OBJ_TYPE_SB)
  112. return &fsnotify_conn_sb(conn)->s_fsnotify_mask;
  113. else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS)
  114. return &fsnotify_conn_mntns(conn)->n_fsnotify_mask;
  115. return NULL;
  116. }
  117. __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn)
  118. {
  119. if (WARN_ON(!fsnotify_valid_obj_type(conn->type)))
  120. return 0;
  121. return READ_ONCE(*fsnotify_conn_mask_p(conn));
  122. }
  123. static void fsnotify_get_sb_watched_objects(struct super_block *sb)
  124. {
  125. atomic_long_inc(fsnotify_sb_watched_objects(sb));
  126. }
  127. static void fsnotify_put_sb_watched_objects(struct super_block *sb)
  128. {
  129. atomic_long_t *watched_objects = fsnotify_sb_watched_objects(sb);
  130. /* the superblock can go away after this decrement */
  131. if (atomic_long_dec_and_test(watched_objects))
  132. wake_up_var(watched_objects);
  133. }
  134. static void fsnotify_get_inode_ref(struct inode *inode)
  135. {
  136. ihold(inode);
  137. fsnotify_get_sb_watched_objects(inode->i_sb);
  138. }
  139. static void fsnotify_put_inode_ref(struct inode *inode)
  140. {
  141. /* read ->i_sb before the inode can go away */
  142. struct super_block *sb = inode->i_sb;
  143. iput(inode);
  144. fsnotify_put_sb_watched_objects(sb);
  145. }
  146. /*
  147. * Grab or drop watched objects reference depending on whether the connector
  148. * is attached and has any marks attached.
  149. */
  150. static void fsnotify_update_sb_watchers(struct super_block *sb,
  151. struct fsnotify_mark_connector *conn)
  152. {
  153. struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
  154. bool is_watched = conn->flags & FSNOTIFY_CONN_FLAG_IS_WATCHED;
  155. struct fsnotify_mark *first_mark = NULL;
  156. unsigned int highest_prio = 0;
  157. if (conn->obj)
  158. first_mark = hlist_entry_safe(conn->list.first,
  159. struct fsnotify_mark, obj_list);
  160. if (first_mark)
  161. highest_prio = first_mark->group->priority;
  162. if (WARN_ON(highest_prio >= __FSNOTIFY_PRIO_NUM))
  163. highest_prio = 0;
  164. /*
  165. * If the highest priority of group watching this object is prio,
  166. * then watched object has a reference on counters [0..prio].
  167. * Update priority >= 1 watched objects counters.
  168. */
  169. for (unsigned int p = conn->prio + 1; p <= highest_prio; p++)
  170. atomic_long_inc(&sbinfo->watched_objects[p]);
  171. for (unsigned int p = conn->prio; p > highest_prio; p--)
  172. atomic_long_dec(&sbinfo->watched_objects[p]);
  173. conn->prio = highest_prio;
  174. /* Update priority >= 0 (a.k.a total) watched objects counter */
  175. BUILD_BUG_ON(FSNOTIFY_PRIO_NORMAL != 0);
  176. if (first_mark && !is_watched) {
  177. conn->flags |= FSNOTIFY_CONN_FLAG_IS_WATCHED;
  178. fsnotify_get_sb_watched_objects(sb);
  179. } else if (!first_mark && is_watched) {
  180. conn->flags &= ~FSNOTIFY_CONN_FLAG_IS_WATCHED;
  181. fsnotify_put_sb_watched_objects(sb);
  182. }
  183. }
  184. /*
  185. * Grab or drop inode reference for the connector if needed.
  186. *
  187. * When it's time to drop the reference, we only clear the HAS_IREF flag and
  188. * return the inode object. fsnotify_drop_object() will be resonsible for doing
  189. * iput() outside of spinlocks. This happens when last mark that wanted iref is
  190. * detached.
  191. */
  192. static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn,
  193. bool want_iref)
  194. {
  195. bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF;
  196. struct inode *inode = NULL;
  197. if (conn->type != FSNOTIFY_OBJ_TYPE_INODE ||
  198. want_iref == has_iref)
  199. return NULL;
  200. if (want_iref) {
  201. /* Pin inode if any mark wants inode refcount held */
  202. fsnotify_get_inode_ref(fsnotify_conn_inode(conn));
  203. conn->flags |= FSNOTIFY_CONN_FLAG_HAS_IREF;
  204. } else {
  205. /* Unpin inode after detach of last mark that wanted iref */
  206. inode = fsnotify_conn_inode(conn);
  207. conn->flags &= ~FSNOTIFY_CONN_FLAG_HAS_IREF;
  208. }
  209. return inode;
  210. }
  211. static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
  212. {
  213. u32 new_mask = 0;
  214. bool want_iref = false;
  215. struct fsnotify_mark *mark;
  216. assert_spin_locked(&conn->lock);
  217. /* We can get detached connector here when inode is getting unlinked. */
  218. if (!fsnotify_valid_obj_type(conn->type))
  219. return NULL;
  220. hlist_for_each_entry(mark, &conn->list, obj_list) {
  221. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED))
  222. continue;
  223. new_mask |= fsnotify_calc_mask(mark);
  224. if (conn->type == FSNOTIFY_OBJ_TYPE_INODE &&
  225. !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
  226. want_iref = true;
  227. }
  228. /*
  229. * We use WRITE_ONCE() to prevent silly compiler optimizations from
  230. * confusing readers not holding conn->lock with partial updates.
  231. */
  232. WRITE_ONCE(*fsnotify_conn_mask_p(conn), new_mask);
  233. return fsnotify_update_iref(conn, want_iref);
  234. }
  235. static bool fsnotify_conn_watches_children(
  236. struct fsnotify_mark_connector *conn)
  237. {
  238. if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
  239. return false;
  240. return fsnotify_inode_watches_children(fsnotify_conn_inode(conn));
  241. }
  242. static void fsnotify_conn_set_children_dentry_flags(
  243. struct fsnotify_mark_connector *conn)
  244. {
  245. if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
  246. return;
  247. fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn));
  248. }
  249. /*
  250. * Calculate mask of events for a list of marks. The caller must make sure
  251. * connector and connector->obj cannot disappear under us. Callers achieve
  252. * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
  253. * list.
  254. */
  255. void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
  256. {
  257. bool update_children;
  258. if (!conn)
  259. return;
  260. spin_lock(&conn->lock);
  261. update_children = !fsnotify_conn_watches_children(conn);
  262. __fsnotify_recalc_mask(conn);
  263. update_children &= fsnotify_conn_watches_children(conn);
  264. spin_unlock(&conn->lock);
  265. /*
  266. * Set children's PARENT_WATCHED flags only if parent started watching.
  267. * When parent stops watching, we clear false positive PARENT_WATCHED
  268. * flags lazily in __fsnotify_parent().
  269. */
  270. if (update_children)
  271. fsnotify_conn_set_children_dentry_flags(conn);
  272. }
  273. /* Free all connectors queued for freeing once SRCU period ends */
  274. static void fsnotify_connector_destroy_workfn(struct work_struct *work)
  275. {
  276. struct fsnotify_mark_connector *conn, *free;
  277. spin_lock(&destroy_lock);
  278. conn = connector_destroy_list;
  279. connector_destroy_list = NULL;
  280. spin_unlock(&destroy_lock);
  281. synchronize_srcu(&fsnotify_mark_srcu);
  282. while (conn) {
  283. free = conn;
  284. conn = conn->destroy_next;
  285. kfree(free);
  286. }
  287. }
  288. static void fsnotify_untrack_connector(struct fsnotify_mark_connector *conn);
  289. static void *fsnotify_detach_connector_from_object(
  290. struct fsnotify_mark_connector *conn,
  291. unsigned int *type)
  292. {
  293. fsnotify_connp_t *connp = fsnotify_object_connp(conn->obj, conn->type);
  294. struct super_block *sb = fsnotify_connector_sb(conn);
  295. struct inode *inode = NULL;
  296. *type = conn->type;
  297. if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED)
  298. return NULL;
  299. if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
  300. inode = fsnotify_conn_inode(conn);
  301. inode->i_fsnotify_mask = 0;
  302. fsnotify_untrack_connector(conn);
  303. /* Unpin inode when detaching from connector */
  304. if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF))
  305. inode = NULL;
  306. } else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
  307. fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
  308. } else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
  309. fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
  310. } else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS) {
  311. fsnotify_conn_mntns(conn)->n_fsnotify_mask = 0;
  312. }
  313. rcu_assign_pointer(*connp, NULL);
  314. conn->obj = NULL;
  315. conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
  316. if (sb)
  317. fsnotify_update_sb_watchers(sb, conn);
  318. return inode;
  319. }
  320. static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
  321. {
  322. struct fsnotify_group *group = mark->group;
  323. if (WARN_ON_ONCE(!group))
  324. return;
  325. group->ops->free_mark(mark);
  326. fsnotify_put_group(group);
  327. }
  328. /* Drop object reference originally held by a connector */
  329. static void fsnotify_drop_object(unsigned int type, void *objp)
  330. {
  331. if (!objp)
  332. return;
  333. /* Currently only inode references are passed to be dropped */
  334. if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE))
  335. return;
  336. fsnotify_put_inode_ref(objp);
  337. }
  338. void fsnotify_put_mark(struct fsnotify_mark *mark)
  339. {
  340. struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector);
  341. void *objp = NULL;
  342. unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED;
  343. bool free_conn = false;
  344. /* Catch marks that were actually never attached to object */
  345. if (!conn) {
  346. if (refcount_dec_and_test(&mark->refcnt))
  347. fsnotify_final_mark_destroy(mark);
  348. return;
  349. }
  350. /*
  351. * We have to be careful so that traversals of obj_list under lock can
  352. * safely grab mark reference.
  353. */
  354. if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock))
  355. return;
  356. hlist_del_init_rcu(&mark->obj_list);
  357. if (hlist_empty(&conn->list)) {
  358. objp = fsnotify_detach_connector_from_object(conn, &type);
  359. free_conn = true;
  360. } else {
  361. struct super_block *sb = fsnotify_connector_sb(conn);
  362. /* Update watched objects after detaching mark */
  363. if (sb)
  364. fsnotify_update_sb_watchers(sb, conn);
  365. objp = __fsnotify_recalc_mask(conn);
  366. type = conn->type;
  367. }
  368. WRITE_ONCE(mark->connector, NULL);
  369. spin_unlock(&conn->lock);
  370. fsnotify_drop_object(type, objp);
  371. if (free_conn) {
  372. spin_lock(&destroy_lock);
  373. conn->destroy_next = connector_destroy_list;
  374. connector_destroy_list = conn;
  375. spin_unlock(&destroy_lock);
  376. queue_work(system_dfl_wq, &connector_reaper_work);
  377. }
  378. /*
  379. * Note that we didn't update flags telling whether inode cares about
  380. * what's happening with children. We update these flags from
  381. * __fsnotify_parent() lazily when next event happens on one of our
  382. * children.
  383. */
  384. spin_lock(&destroy_lock);
  385. list_add(&mark->g_list, &destroy_list);
  386. spin_unlock(&destroy_lock);
  387. queue_delayed_work(system_dfl_wq, &reaper_work,
  388. FSNOTIFY_REAPER_DELAY);
  389. }
  390. EXPORT_SYMBOL_GPL(fsnotify_put_mark);
  391. /*
  392. * Get mark reference when we found the mark via lockless traversal of object
  393. * list. Mark can be already removed from the list by now and on its way to be
  394. * destroyed once SRCU period ends.
  395. *
  396. * Also pin the group so it doesn't disappear under us.
  397. */
  398. static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
  399. {
  400. if (!mark)
  401. return true;
  402. if (refcount_inc_not_zero(&mark->refcnt)) {
  403. spin_lock(&mark->lock);
  404. if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) {
  405. /* mark is attached, group is still alive then */
  406. atomic_inc(&mark->group->user_waits);
  407. spin_unlock(&mark->lock);
  408. return true;
  409. }
  410. spin_unlock(&mark->lock);
  411. fsnotify_put_mark(mark);
  412. }
  413. return false;
  414. }
  415. /*
  416. * Puts marks and wakes up group destruction if necessary.
  417. *
  418. * Pairs with fsnotify_get_mark_safe()
  419. */
  420. static void fsnotify_put_mark_wake(struct fsnotify_mark *mark)
  421. {
  422. if (mark) {
  423. struct fsnotify_group *group = mark->group;
  424. fsnotify_put_mark(mark);
  425. /*
  426. * We abuse notification_waitq on group shutdown for waiting for
  427. * all marks pinned when waiting for userspace.
  428. */
  429. if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
  430. wake_up(&group->notification_waitq);
  431. }
  432. }
  433. bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)
  434. __releases(&fsnotify_mark_srcu)
  435. {
  436. int type;
  437. fsnotify_foreach_iter_type(type) {
  438. /* This can fail if mark is being removed */
  439. if (!fsnotify_get_mark_safe(iter_info->marks[type])) {
  440. __release(&fsnotify_mark_srcu);
  441. goto fail;
  442. }
  443. }
  444. /*
  445. * Now that both marks are pinned by refcount in the inode / vfsmount
  446. * lists, we can drop SRCU lock, and safely resume the list iteration
  447. * once userspace returns.
  448. */
  449. srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx);
  450. return true;
  451. fail:
  452. for (type--; type >= 0; type--)
  453. fsnotify_put_mark_wake(iter_info->marks[type]);
  454. return false;
  455. }
  456. void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info)
  457. __acquires(&fsnotify_mark_srcu)
  458. {
  459. int type;
  460. iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
  461. fsnotify_foreach_iter_type(type)
  462. fsnotify_put_mark_wake(iter_info->marks[type]);
  463. }
  464. /*
  465. * Mark mark as detached, remove it from group list. Mark still stays in object
  466. * list until its last reference is dropped. Note that we rely on mark being
  467. * removed from group list before corresponding reference to it is dropped. In
  468. * particular we rely on mark->connector being valid while we hold
  469. * group->mark_mutex if we found the mark through g_list.
  470. *
  471. * Must be called with group->mark_mutex held. The caller must either hold
  472. * reference to the mark or be protected by fsnotify_mark_srcu.
  473. */
  474. void fsnotify_detach_mark(struct fsnotify_mark *mark)
  475. {
  476. fsnotify_group_assert_locked(mark->group);
  477. WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) &&
  478. refcount_read(&mark->refcnt) < 1 +
  479. !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
  480. spin_lock(&mark->lock);
  481. /* something else already called this function on this mark */
  482. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
  483. spin_unlock(&mark->lock);
  484. return;
  485. }
  486. mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
  487. list_del_init(&mark->g_list);
  488. spin_unlock(&mark->lock);
  489. /* Drop mark reference acquired in fsnotify_add_mark_locked() */
  490. fsnotify_put_mark(mark);
  491. }
  492. /*
  493. * Free fsnotify mark. The mark is actually only marked as being freed. The
  494. * freeing is actually happening only once last reference to the mark is
  495. * dropped from a workqueue which first waits for srcu period end.
  496. *
  497. * Caller must have a reference to the mark or be protected by
  498. * fsnotify_mark_srcu.
  499. */
  500. void fsnotify_free_mark(struct fsnotify_mark *mark)
  501. {
  502. struct fsnotify_group *group = mark->group;
  503. spin_lock(&mark->lock);
  504. /* something else already called this function on this mark */
  505. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
  506. spin_unlock(&mark->lock);
  507. return;
  508. }
  509. mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
  510. spin_unlock(&mark->lock);
  511. /*
  512. * Some groups like to know that marks are being freed. This is a
  513. * callback to the group function to let it know that this mark
  514. * is being freed.
  515. */
  516. if (group->ops->freeing_mark)
  517. group->ops->freeing_mark(mark, group);
  518. }
  519. void fsnotify_destroy_mark(struct fsnotify_mark *mark,
  520. struct fsnotify_group *group)
  521. {
  522. fsnotify_group_lock(group);
  523. fsnotify_detach_mark(mark);
  524. fsnotify_group_unlock(group);
  525. fsnotify_free_mark(mark);
  526. }
  527. EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
  528. /*
  529. * Sorting function for lists of fsnotify marks.
  530. *
  531. * Fanotify supports different notification classes (reflected as priority of
  532. * notification group). Events shall be passed to notification groups in
  533. * decreasing priority order. To achieve this marks in notification lists for
  534. * inodes and vfsmounts are sorted so that priorities of corresponding groups
  535. * are descending.
  536. *
  537. * Furthermore correct handling of the ignore mask requires processing inode
  538. * and vfsmount marks of each group together. Using the group address as
  539. * further sort criterion provides a unique sorting order and thus we can
  540. * merge inode and vfsmount lists of marks in linear time and find groups
  541. * present in both lists.
  542. *
  543. * A return value of 1 signifies that b has priority over a.
  544. * A return value of 0 signifies that the two marks have to be handled together.
  545. * A return value of -1 signifies that a has priority over b.
  546. */
  547. int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
  548. {
  549. if (a == b)
  550. return 0;
  551. if (!a)
  552. return 1;
  553. if (!b)
  554. return -1;
  555. if (a->priority < b->priority)
  556. return 1;
  557. if (a->priority > b->priority)
  558. return -1;
  559. if (a < b)
  560. return 1;
  561. return -1;
  562. }
  563. static int fsnotify_attach_info_to_sb(struct super_block *sb)
  564. {
  565. struct fsnotify_sb_info *sbinfo;
  566. /* sb info is freed on fsnotify_sb_delete() */
  567. sbinfo = kzalloc_obj(*sbinfo);
  568. if (!sbinfo)
  569. return -ENOMEM;
  570. INIT_LIST_HEAD(&sbinfo->inode_conn_list);
  571. spin_lock_init(&sbinfo->list_lock);
  572. /*
  573. * cmpxchg() provides the barrier so that callers of fsnotify_sb_info()
  574. * will observe an initialized structure
  575. */
  576. if (cmpxchg(&sb->s_fsnotify_info, NULL, sbinfo)) {
  577. /* Someone else created sbinfo for us */
  578. kfree(sbinfo);
  579. }
  580. return 0;
  581. }
  582. struct fsnotify_inode_mark_connector {
  583. struct fsnotify_mark_connector common;
  584. struct list_head conns_list;
  585. };
  586. static struct inode *fsnotify_get_living_inode(struct fsnotify_sb_info *sbinfo)
  587. {
  588. struct fsnotify_inode_mark_connector *iconn;
  589. struct inode *inode;
  590. spin_lock(&sbinfo->list_lock);
  591. /* Find the first non-evicting inode */
  592. list_for_each_entry(iconn, &sbinfo->inode_conn_list, conns_list) {
  593. /* All connectors on the list are still attached to an inode */
  594. inode = iconn->common.obj;
  595. /*
  596. * For connectors without FSNOTIFY_CONN_FLAG_HAS_IREF
  597. * (evictable marks) corresponding inode may well have 0
  598. * refcount and can be undergoing eviction. OTOH list_lock
  599. * protects us from the connector getting detached and inode
  600. * freed. So we can poke around the inode safely.
  601. */
  602. spin_lock(&inode->i_lock);
  603. if (likely(
  604. !(inode_state_read(inode) & (I_FREEING | I_WILL_FREE)))) {
  605. __iget(inode);
  606. spin_unlock(&inode->i_lock);
  607. spin_unlock(&sbinfo->list_lock);
  608. return inode;
  609. }
  610. spin_unlock(&inode->i_lock);
  611. }
  612. spin_unlock(&sbinfo->list_lock);
  613. return NULL;
  614. }
  615. /**
  616. * fsnotify_unmount_inodes - an sb is unmounting. Handle any watched inodes.
  617. * @sbinfo: fsnotify info for superblock being unmounted.
  618. *
  619. * Walk all inode connectors for the superblock and free all associated marks.
  620. */
  621. void fsnotify_unmount_inodes(struct fsnotify_sb_info *sbinfo)
  622. {
  623. struct inode *inode;
  624. while ((inode = fsnotify_get_living_inode(sbinfo))) {
  625. fsnotify_inode(inode, FS_UNMOUNT);
  626. fsnotify_clear_marks_by_inode(inode);
  627. iput(inode);
  628. cond_resched();
  629. }
  630. }
  631. static void fsnotify_init_connector(struct fsnotify_mark_connector *conn,
  632. void *obj, unsigned int obj_type)
  633. {
  634. spin_lock_init(&conn->lock);
  635. INIT_HLIST_HEAD(&conn->list);
  636. conn->flags = 0;
  637. conn->prio = 0;
  638. conn->type = obj_type;
  639. conn->obj = obj;
  640. }
  641. static struct fsnotify_mark_connector *
  642. fsnotify_alloc_inode_connector(struct inode *inode)
  643. {
  644. struct fsnotify_inode_mark_connector *iconn;
  645. struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(inode->i_sb);
  646. iconn = kmem_cache_alloc(fsnotify_inode_mark_connector_cachep,
  647. GFP_KERNEL);
  648. if (!iconn)
  649. return NULL;
  650. fsnotify_init_connector(&iconn->common, inode, FSNOTIFY_OBJ_TYPE_INODE);
  651. spin_lock(&sbinfo->list_lock);
  652. list_add(&iconn->conns_list, &sbinfo->inode_conn_list);
  653. spin_unlock(&sbinfo->list_lock);
  654. return &iconn->common;
  655. }
  656. static void fsnotify_untrack_connector(struct fsnotify_mark_connector *conn)
  657. {
  658. struct fsnotify_inode_mark_connector *iconn;
  659. struct fsnotify_sb_info *sbinfo;
  660. if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
  661. return;
  662. iconn = container_of(conn, struct fsnotify_inode_mark_connector, common);
  663. sbinfo = fsnotify_sb_info(fsnotify_conn_inode(conn)->i_sb);
  664. spin_lock(&sbinfo->list_lock);
  665. list_del(&iconn->conns_list);
  666. spin_unlock(&sbinfo->list_lock);
  667. }
  668. static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
  669. void *obj, unsigned int obj_type)
  670. {
  671. struct fsnotify_mark_connector *conn;
  672. if (obj_type == FSNOTIFY_OBJ_TYPE_INODE) {
  673. struct inode *inode = obj;
  674. conn = fsnotify_alloc_inode_connector(inode);
  675. } else {
  676. conn = kmem_cache_alloc(fsnotify_mark_connector_cachep,
  677. GFP_KERNEL);
  678. if (conn)
  679. fsnotify_init_connector(conn, obj, obj_type);
  680. }
  681. if (!conn)
  682. return -ENOMEM;
  683. /*
  684. * cmpxchg() provides the barrier so that readers of *connp can see
  685. * only initialized structure
  686. */
  687. if (cmpxchg(connp, NULL, conn)) {
  688. /* Someone else created list structure for us */
  689. fsnotify_untrack_connector(conn);
  690. kfree(conn);
  691. }
  692. return 0;
  693. }
  694. /*
  695. * Get mark connector, make sure it is alive and return with its lock held.
  696. * This is for users that get connector pointer from inode or mount. Users that
  697. * hold reference to a mark on the list may directly lock connector->lock as
  698. * they are sure list cannot go away under them.
  699. */
  700. static struct fsnotify_mark_connector *fsnotify_grab_connector(
  701. fsnotify_connp_t *connp)
  702. {
  703. struct fsnotify_mark_connector *conn;
  704. int idx;
  705. idx = srcu_read_lock(&fsnotify_mark_srcu);
  706. conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
  707. if (!conn)
  708. goto out;
  709. spin_lock(&conn->lock);
  710. if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
  711. spin_unlock(&conn->lock);
  712. srcu_read_unlock(&fsnotify_mark_srcu, idx);
  713. return NULL;
  714. }
  715. out:
  716. srcu_read_unlock(&fsnotify_mark_srcu, idx);
  717. return conn;
  718. }
  719. /*
  720. * Add mark into proper place in given list of marks. These marks may be used
  721. * for the fsnotify backend to determine which event types should be delivered
  722. * to which group and for which inodes. These marks are ordered according to
  723. * priority, highest number first, and then by the group's location in memory.
  724. */
  725. static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj,
  726. unsigned int obj_type, int add_flags)
  727. {
  728. struct super_block *sb = fsnotify_object_sb(obj, obj_type);
  729. struct fsnotify_mark *lmark, *last = NULL;
  730. struct fsnotify_mark_connector *conn;
  731. fsnotify_connp_t *connp;
  732. int cmp;
  733. int err = 0;
  734. if (WARN_ON(!fsnotify_valid_obj_type(obj_type)))
  735. return -EINVAL;
  736. /*
  737. * Attach the sb info before attaching a connector to any object on sb.
  738. * The sb info will remain attached as long as sb lives.
  739. */
  740. if (sb && !fsnotify_sb_info(sb)) {
  741. err = fsnotify_attach_info_to_sb(sb);
  742. if (err)
  743. return err;
  744. }
  745. connp = fsnotify_object_connp(obj, obj_type);
  746. restart:
  747. spin_lock(&mark->lock);
  748. conn = fsnotify_grab_connector(connp);
  749. if (!conn) {
  750. spin_unlock(&mark->lock);
  751. err = fsnotify_attach_connector_to_object(connp, obj, obj_type);
  752. if (err)
  753. return err;
  754. goto restart;
  755. }
  756. /* is mark the first mark? */
  757. if (hlist_empty(&conn->list)) {
  758. hlist_add_head_rcu(&mark->obj_list, &conn->list);
  759. goto added;
  760. }
  761. /* should mark be in the middle of the current list? */
  762. hlist_for_each_entry(lmark, &conn->list, obj_list) {
  763. last = lmark;
  764. if ((lmark->group == mark->group) &&
  765. (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
  766. !(mark->group->flags & FSNOTIFY_GROUP_DUPS)) {
  767. err = -EEXIST;
  768. goto out_err;
  769. }
  770. cmp = fsnotify_compare_groups(lmark->group, mark->group);
  771. if (cmp >= 0) {
  772. hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
  773. goto added;
  774. }
  775. }
  776. BUG_ON(last == NULL);
  777. /* mark should be the last entry. last is the current last entry */
  778. hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
  779. added:
  780. if (sb)
  781. fsnotify_update_sb_watchers(sb, conn);
  782. /*
  783. * Since connector is attached to object using cmpxchg() we are
  784. * guaranteed that connector initialization is fully visible by anyone
  785. * seeing mark->connector set.
  786. */
  787. WRITE_ONCE(mark->connector, conn);
  788. out_err:
  789. spin_unlock(&conn->lock);
  790. spin_unlock(&mark->lock);
  791. return err;
  792. }
  793. /*
  794. * Attach an initialized mark to a given group and fs object.
  795. * These marks may be used for the fsnotify backend to determine which
  796. * event types should be delivered to which group.
  797. */
  798. int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
  799. void *obj, unsigned int obj_type,
  800. int add_flags)
  801. {
  802. struct fsnotify_group *group = mark->group;
  803. int ret = 0;
  804. fsnotify_group_assert_locked(group);
  805. /*
  806. * LOCKING ORDER!!!!
  807. * group->mark_mutex
  808. * mark->lock
  809. * mark->connector->lock
  810. */
  811. spin_lock(&mark->lock);
  812. mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
  813. list_add(&mark->g_list, &group->marks_list);
  814. fsnotify_get_mark(mark); /* for g_list */
  815. spin_unlock(&mark->lock);
  816. ret = fsnotify_add_mark_list(mark, obj, obj_type, add_flags);
  817. if (ret)
  818. goto err;
  819. fsnotify_recalc_mask(mark->connector);
  820. return ret;
  821. err:
  822. spin_lock(&mark->lock);
  823. mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
  824. FSNOTIFY_MARK_FLAG_ATTACHED);
  825. list_del_init(&mark->g_list);
  826. spin_unlock(&mark->lock);
  827. fsnotify_put_mark(mark);
  828. return ret;
  829. }
  830. int fsnotify_add_mark(struct fsnotify_mark *mark, void *obj,
  831. unsigned int obj_type, int add_flags)
  832. {
  833. int ret;
  834. struct fsnotify_group *group = mark->group;
  835. fsnotify_group_lock(group);
  836. ret = fsnotify_add_mark_locked(mark, obj, obj_type, add_flags);
  837. fsnotify_group_unlock(group);
  838. return ret;
  839. }
  840. EXPORT_SYMBOL_GPL(fsnotify_add_mark);
  841. /*
  842. * Given a list of marks, find the mark associated with given group. If found
  843. * take a reference to that mark and return it, else return NULL.
  844. */
  845. struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type,
  846. struct fsnotify_group *group)
  847. {
  848. fsnotify_connp_t *connp = fsnotify_object_connp(obj, obj_type);
  849. struct fsnotify_mark_connector *conn;
  850. struct fsnotify_mark *mark;
  851. if (!connp)
  852. return NULL;
  853. conn = fsnotify_grab_connector(connp);
  854. if (!conn)
  855. return NULL;
  856. hlist_for_each_entry(mark, &conn->list, obj_list) {
  857. if (mark->group == group &&
  858. (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
  859. fsnotify_get_mark(mark);
  860. spin_unlock(&conn->lock);
  861. return mark;
  862. }
  863. }
  864. spin_unlock(&conn->lock);
  865. return NULL;
  866. }
  867. EXPORT_SYMBOL_GPL(fsnotify_find_mark);
  868. /* Clear any marks in a group with given type mask */
  869. void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
  870. unsigned int obj_type)
  871. {
  872. struct fsnotify_mark *lmark, *mark;
  873. LIST_HEAD(to_free);
  874. struct list_head *head = &to_free;
  875. /* Skip selection step if we want to clear all marks. */
  876. if (obj_type == FSNOTIFY_OBJ_TYPE_ANY) {
  877. head = &group->marks_list;
  878. goto clear;
  879. }
  880. /*
  881. * We have to be really careful here. Anytime we drop mark_mutex, e.g.
  882. * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
  883. * to_free list so we have to use mark_mutex even when accessing that
  884. * list. And freeing mark requires us to drop mark_mutex. So we can
  885. * reliably free only the first mark in the list. That's why we first
  886. * move marks to free to to_free list in one go and then free marks in
  887. * to_free list one by one.
  888. */
  889. fsnotify_group_lock(group);
  890. list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
  891. if (mark->connector->type == obj_type)
  892. list_move(&mark->g_list, &to_free);
  893. }
  894. fsnotify_group_unlock(group);
  895. clear:
  896. while (1) {
  897. fsnotify_group_lock(group);
  898. if (list_empty(head)) {
  899. fsnotify_group_unlock(group);
  900. break;
  901. }
  902. mark = list_first_entry(head, struct fsnotify_mark, g_list);
  903. fsnotify_get_mark(mark);
  904. fsnotify_detach_mark(mark);
  905. fsnotify_group_unlock(group);
  906. fsnotify_free_mark(mark);
  907. fsnotify_put_mark(mark);
  908. }
  909. }
  910. /* Destroy all marks attached to an object via connector */
  911. void fsnotify_destroy_marks(fsnotify_connp_t *connp)
  912. {
  913. struct fsnotify_mark_connector *conn;
  914. struct fsnotify_mark *mark, *old_mark = NULL;
  915. void *objp;
  916. unsigned int type;
  917. conn = fsnotify_grab_connector(connp);
  918. if (!conn)
  919. return;
  920. /*
  921. * We have to be careful since we can race with e.g.
  922. * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
  923. * list can get modified. However we are holding mark reference and
  924. * thus our mark cannot be removed from obj_list so we can continue
  925. * iteration after regaining conn->lock.
  926. */
  927. hlist_for_each_entry(mark, &conn->list, obj_list) {
  928. fsnotify_get_mark(mark);
  929. spin_unlock(&conn->lock);
  930. if (old_mark)
  931. fsnotify_put_mark(old_mark);
  932. old_mark = mark;
  933. fsnotify_destroy_mark(mark, mark->group);
  934. spin_lock(&conn->lock);
  935. }
  936. /*
  937. * Detach list from object now so that we don't pin inode until all
  938. * mark references get dropped. It would lead to strange results such
  939. * as delaying inode deletion or blocking unmount.
  940. */
  941. objp = fsnotify_detach_connector_from_object(conn, &type);
  942. spin_unlock(&conn->lock);
  943. if (old_mark)
  944. fsnotify_put_mark(old_mark);
  945. fsnotify_drop_object(type, objp);
  946. }
  947. /*
  948. * Nothing fancy, just initialize lists and locks and counters.
  949. */
  950. void fsnotify_init_mark(struct fsnotify_mark *mark,
  951. struct fsnotify_group *group)
  952. {
  953. memset(mark, 0, sizeof(*mark));
  954. spin_lock_init(&mark->lock);
  955. refcount_set(&mark->refcnt, 1);
  956. fsnotify_get_group(group);
  957. mark->group = group;
  958. WRITE_ONCE(mark->connector, NULL);
  959. }
  960. EXPORT_SYMBOL_GPL(fsnotify_init_mark);
  961. /*
  962. * Destroy all marks in destroy_list, waits for SRCU period to finish before
  963. * actually freeing marks.
  964. */
  965. static void fsnotify_mark_destroy_workfn(struct work_struct *work)
  966. {
  967. struct fsnotify_mark *mark, *next;
  968. struct list_head private_destroy_list;
  969. spin_lock(&destroy_lock);
  970. /* exchange the list head */
  971. list_replace_init(&destroy_list, &private_destroy_list);
  972. spin_unlock(&destroy_lock);
  973. synchronize_srcu(&fsnotify_mark_srcu);
  974. list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
  975. list_del_init(&mark->g_list);
  976. fsnotify_final_mark_destroy(mark);
  977. }
  978. }
  979. /* Wait for all marks queued for destruction to be actually destroyed */
  980. void fsnotify_wait_marks_destroyed(void)
  981. {
  982. flush_delayed_work(&reaper_work);
  983. }
  984. EXPORT_SYMBOL_GPL(fsnotify_wait_marks_destroyed);
  985. __init void fsnotify_init_connector_caches(void)
  986. {
  987. fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
  988. SLAB_PANIC);
  989. fsnotify_inode_mark_connector_cachep = KMEM_CACHE(
  990. fsnotify_inode_mark_connector,
  991. SLAB_PANIC);
  992. }