fsnotify.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
  4. */
  5. #include <linux/dcache.h>
  6. #include <linux/fs.h>
  7. #include <linux/gfp.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/mount.h>
  11. #include <linux/srcu.h>
  12. #include <linux/fsnotify_backend.h>
  13. #include "fsnotify.h"
  14. /*
  15. * Clear all of the marks on an inode when it is being evicted from core
  16. */
  17. void __fsnotify_inode_delete(struct inode *inode)
  18. {
  19. fsnotify_clear_marks_by_inode(inode);
  20. }
  21. EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
  22. void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
  23. {
  24. fsnotify_clear_marks_by_mount(mnt);
  25. }
  26. /**
  27. * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  28. * @sb: superblock being unmounted.
  29. *
  30. * Called during unmount with no locks held, so needs to be safe against
  31. * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
  32. */
  33. static void fsnotify_unmount_inodes(struct super_block *sb)
  34. {
  35. struct inode *inode, *iput_inode = NULL;
  36. spin_lock(&sb->s_inode_list_lock);
  37. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  38. /*
  39. * We cannot __iget() an inode in state I_FREEING,
  40. * I_WILL_FREE, or I_NEW which is fine because by that point
  41. * the inode cannot have any associated watches.
  42. */
  43. spin_lock(&inode->i_lock);
  44. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
  45. spin_unlock(&inode->i_lock);
  46. continue;
  47. }
  48. /*
  49. * If i_count is zero, the inode cannot have any watches and
  50. * doing an __iget/iput with SB_ACTIVE clear would actually
  51. * evict all inodes with zero i_count from icache which is
  52. * unnecessarily violent and may in fact be illegal to do.
  53. * However, we should have been called /after/ evict_inodes
  54. * removed all zero refcount inodes, in any case. Test to
  55. * be sure.
  56. */
  57. if (!atomic_read(&inode->i_count)) {
  58. spin_unlock(&inode->i_lock);
  59. continue;
  60. }
  61. __iget(inode);
  62. spin_unlock(&inode->i_lock);
  63. spin_unlock(&sb->s_inode_list_lock);
  64. if (iput_inode)
  65. iput(iput_inode);
  66. /* for each watch, send FS_UNMOUNT and then remove it */
  67. fsnotify_inode(inode, FS_UNMOUNT);
  68. fsnotify_inode_delete(inode);
  69. iput_inode = inode;
  70. cond_resched();
  71. spin_lock(&sb->s_inode_list_lock);
  72. }
  73. spin_unlock(&sb->s_inode_list_lock);
  74. if (iput_inode)
  75. iput(iput_inode);
  76. /* Wait for outstanding inode references from connectors */
  77. wait_var_event(&sb->s_fsnotify_inode_refs,
  78. !atomic_long_read(&sb->s_fsnotify_inode_refs));
  79. }
  80. void fsnotify_sb_delete(struct super_block *sb)
  81. {
  82. fsnotify_unmount_inodes(sb);
  83. fsnotify_clear_marks_by_sb(sb);
  84. }
  85. /*
  86. * Given an inode, first check if we care what happens to our children. Inotify
  87. * and dnotify both tell their parents about events. If we care about any event
  88. * on a child we run all of our children and set a dentry flag saying that the
  89. * parent cares. Thus when an event happens on a child it can quickly tell if
  90. * if there is a need to find a parent and send the event to the parent.
  91. */
  92. void __fsnotify_update_child_dentry_flags(struct inode *inode)
  93. {
  94. struct dentry *alias;
  95. int watched;
  96. if (!S_ISDIR(inode->i_mode))
  97. return;
  98. /* determine if the children should tell inode about their events */
  99. watched = fsnotify_inode_watches_children(inode);
  100. spin_lock(&inode->i_lock);
  101. /* run all of the dentries associated with this inode. Since this is a
  102. * directory, there damn well better only be one item on this list */
  103. hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
  104. struct dentry *child;
  105. /* run all of the children of the original inode and fix their
  106. * d_flags to indicate parental interest (their parent is the
  107. * original inode) */
  108. spin_lock(&alias->d_lock);
  109. list_for_each_entry(child, &alias->d_subdirs, d_child) {
  110. if (!child->d_inode)
  111. continue;
  112. spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
  113. if (watched)
  114. child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
  115. else
  116. child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
  117. spin_unlock(&child->d_lock);
  118. }
  119. spin_unlock(&alias->d_lock);
  120. }
  121. spin_unlock(&inode->i_lock);
  122. }
  123. /* Are inode/sb/mount interested in parent and name info with this event? */
  124. static bool fsnotify_event_needs_parent(struct inode *inode, struct mount *mnt,
  125. __u32 mask)
  126. {
  127. __u32 marks_mask = 0;
  128. /* We only send parent/name to inode/sb/mount for events on non-dir */
  129. if (mask & FS_ISDIR)
  130. return false;
  131. /*
  132. * All events that are possible on child can also may be reported with
  133. * parent/name info to inode/sb/mount. Otherwise, a watching parent
  134. * could result in events reported with unexpected name info to sb/mount.
  135. */
  136. BUILD_BUG_ON(FS_EVENTS_POSS_ON_CHILD & ~FS_EVENTS_POSS_TO_PARENT);
  137. /* Did either inode/sb/mount subscribe for events with parent/name? */
  138. marks_mask |= fsnotify_parent_needed_mask(inode->i_fsnotify_mask);
  139. marks_mask |= fsnotify_parent_needed_mask(inode->i_sb->s_fsnotify_mask);
  140. if (mnt)
  141. marks_mask |= fsnotify_parent_needed_mask(mnt->mnt_fsnotify_mask);
  142. /* Did they subscribe for this event with parent/name info? */
  143. return mask & marks_mask;
  144. }
  145. /*
  146. * Notify this dentry's parent about a child's events with child name info
  147. * if parent is watching or if inode/sb/mount are interested in events with
  148. * parent and name info.
  149. *
  150. * Notify only the child without name info if parent is not watching and
  151. * inode/sb/mount are not interested in events with parent and name info.
  152. */
  153. int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
  154. int data_type)
  155. {
  156. const struct path *path = fsnotify_data_path(data, data_type);
  157. struct mount *mnt = path ? real_mount(path->mnt) : NULL;
  158. struct inode *inode = d_inode(dentry);
  159. struct dentry *parent;
  160. bool parent_watched = dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED;
  161. bool parent_needed, parent_interested;
  162. __u32 p_mask;
  163. struct inode *p_inode = NULL;
  164. struct name_snapshot name;
  165. struct qstr *file_name = NULL;
  166. int ret = 0;
  167. /*
  168. * Do inode/sb/mount care about parent and name info on non-dir?
  169. * Do they care about any event at all?
  170. */
  171. if (!inode->i_fsnotify_marks && !inode->i_sb->s_fsnotify_marks &&
  172. (!mnt || !mnt->mnt_fsnotify_marks) && !parent_watched)
  173. return 0;
  174. parent = NULL;
  175. parent_needed = fsnotify_event_needs_parent(inode, mnt, mask);
  176. if (!parent_watched && !parent_needed)
  177. goto notify;
  178. /* Does parent inode care about events on children? */
  179. parent = dget_parent(dentry);
  180. p_inode = parent->d_inode;
  181. p_mask = fsnotify_inode_watches_children(p_inode);
  182. if (unlikely(parent_watched && !p_mask))
  183. __fsnotify_update_child_dentry_flags(p_inode);
  184. /*
  185. * Include parent/name in notification either if some notification
  186. * groups require parent info or the parent is interested in this event.
  187. */
  188. parent_interested = mask & p_mask & ALL_FSNOTIFY_EVENTS;
  189. if (parent_needed || parent_interested) {
  190. /* When notifying parent, child should be passed as data */
  191. WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type));
  192. /* Notify both parent and child with child name info */
  193. take_dentry_name_snapshot(&name, dentry);
  194. file_name = &name.name;
  195. if (parent_interested)
  196. mask |= FS_EVENT_ON_CHILD;
  197. }
  198. notify:
  199. ret = fsnotify(mask, data, data_type, p_inode, file_name, inode, 0);
  200. if (file_name)
  201. release_dentry_name_snapshot(&name);
  202. dput(parent);
  203. return ret;
  204. }
  205. EXPORT_SYMBOL_GPL(__fsnotify_parent);
  206. static int fsnotify_handle_inode_event(struct fsnotify_group *group,
  207. struct fsnotify_mark *inode_mark,
  208. u32 mask, const void *data, int data_type,
  209. struct inode *dir, const struct qstr *name,
  210. u32 cookie)
  211. {
  212. const struct path *path = fsnotify_data_path(data, data_type);
  213. struct inode *inode = fsnotify_data_inode(data, data_type);
  214. const struct fsnotify_ops *ops = group->ops;
  215. if (WARN_ON_ONCE(!ops->handle_inode_event))
  216. return 0;
  217. if ((inode_mark->mask & FS_EXCL_UNLINK) &&
  218. path && d_unlinked(path->dentry))
  219. return 0;
  220. /* Check interest of this mark in case event was sent with two marks */
  221. if (!(mask & inode_mark->mask & ALL_FSNOTIFY_EVENTS))
  222. return 0;
  223. return ops->handle_inode_event(inode_mark, mask, inode, dir, name, cookie);
  224. }
  225. static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
  226. const void *data, int data_type,
  227. struct inode *dir, const struct qstr *name,
  228. u32 cookie, struct fsnotify_iter_info *iter_info)
  229. {
  230. struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
  231. struct fsnotify_mark *parent_mark = fsnotify_iter_parent_mark(iter_info);
  232. int ret;
  233. if (WARN_ON_ONCE(fsnotify_iter_sb_mark(iter_info)) ||
  234. WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info)))
  235. return 0;
  236. if (parent_mark) {
  237. /*
  238. * parent_mark indicates that the parent inode is watching
  239. * children and interested in this event, which is an event
  240. * possible on child. But is *this mark* watching children and
  241. * interested in this event?
  242. */
  243. if (parent_mark->mask & FS_EVENT_ON_CHILD) {
  244. ret = fsnotify_handle_inode_event(group, parent_mark, mask,
  245. data, data_type, dir, name, 0);
  246. if (ret)
  247. return ret;
  248. }
  249. if (!inode_mark)
  250. return 0;
  251. }
  252. if (mask & FS_EVENT_ON_CHILD) {
  253. /*
  254. * Some events can be sent on both parent dir and child marks
  255. * (e.g. FS_ATTRIB). If both parent dir and child are
  256. * watching, report the event once to parent dir with name (if
  257. * interested) and once to child without name (if interested).
  258. * The child watcher is expecting an event without a file name
  259. * and without the FS_EVENT_ON_CHILD flag.
  260. */
  261. mask &= ~FS_EVENT_ON_CHILD;
  262. dir = NULL;
  263. name = NULL;
  264. }
  265. return fsnotify_handle_inode_event(group, inode_mark, mask, data, data_type,
  266. dir, name, cookie);
  267. }
  268. static int send_to_group(__u32 mask, const void *data, int data_type,
  269. struct inode *dir, const struct qstr *file_name,
  270. u32 cookie, struct fsnotify_iter_info *iter_info)
  271. {
  272. struct fsnotify_group *group = NULL;
  273. __u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
  274. __u32 marks_mask = 0;
  275. __u32 marks_ignored_mask = 0;
  276. struct fsnotify_mark *mark;
  277. int type;
  278. if (WARN_ON(!iter_info->report_mask))
  279. return 0;
  280. /* clear ignored on inode modification */
  281. if (mask & FS_MODIFY) {
  282. fsnotify_foreach_obj_type(type) {
  283. if (!fsnotify_iter_should_report_type(iter_info, type))
  284. continue;
  285. mark = iter_info->marks[type];
  286. if (mark &&
  287. !(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
  288. mark->ignored_mask = 0;
  289. }
  290. }
  291. fsnotify_foreach_obj_type(type) {
  292. if (!fsnotify_iter_should_report_type(iter_info, type))
  293. continue;
  294. mark = iter_info->marks[type];
  295. /* does the object mark tell us to do something? */
  296. if (mark) {
  297. group = mark->group;
  298. marks_mask |= mark->mask;
  299. marks_ignored_mask |= mark->ignored_mask;
  300. }
  301. }
  302. pr_debug("%s: group=%p mask=%x marks_mask=%x marks_ignored_mask=%x data=%p data_type=%d dir=%p cookie=%d\n",
  303. __func__, group, mask, marks_mask, marks_ignored_mask,
  304. data, data_type, dir, cookie);
  305. if (!(test_mask & marks_mask & ~marks_ignored_mask))
  306. return 0;
  307. if (group->ops->handle_event) {
  308. return group->ops->handle_event(group, mask, data, data_type, dir,
  309. file_name, cookie, iter_info);
  310. }
  311. return fsnotify_handle_event(group, mask, data, data_type, dir,
  312. file_name, cookie, iter_info);
  313. }
  314. static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp)
  315. {
  316. struct fsnotify_mark_connector *conn;
  317. struct hlist_node *node = NULL;
  318. conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
  319. if (conn)
  320. node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu);
  321. return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
  322. }
  323. static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
  324. {
  325. struct hlist_node *node = NULL;
  326. if (mark)
  327. node = srcu_dereference(mark->obj_list.next,
  328. &fsnotify_mark_srcu);
  329. return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
  330. }
  331. /*
  332. * iter_info is a multi head priority queue of marks.
  333. * Pick a subset of marks from queue heads, all with the
  334. * same group and set the report_mask for selected subset.
  335. * Returns the report_mask of the selected subset.
  336. */
  337. static unsigned int fsnotify_iter_select_report_types(
  338. struct fsnotify_iter_info *iter_info)
  339. {
  340. struct fsnotify_group *max_prio_group = NULL;
  341. struct fsnotify_mark *mark;
  342. int type;
  343. /* Choose max prio group among groups of all queue heads */
  344. fsnotify_foreach_obj_type(type) {
  345. mark = iter_info->marks[type];
  346. if (mark &&
  347. fsnotify_compare_groups(max_prio_group, mark->group) > 0)
  348. max_prio_group = mark->group;
  349. }
  350. if (!max_prio_group)
  351. return 0;
  352. /* Set the report mask for marks from same group as max prio group */
  353. iter_info->report_mask = 0;
  354. fsnotify_foreach_obj_type(type) {
  355. mark = iter_info->marks[type];
  356. if (mark &&
  357. fsnotify_compare_groups(max_prio_group, mark->group) == 0)
  358. fsnotify_iter_set_report_type(iter_info, type);
  359. }
  360. return iter_info->report_mask;
  361. }
  362. /*
  363. * Pop from iter_info multi head queue, the marks that were iterated in the
  364. * current iteration step.
  365. */
  366. static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
  367. {
  368. int type;
  369. fsnotify_foreach_obj_type(type) {
  370. if (fsnotify_iter_should_report_type(iter_info, type))
  371. iter_info->marks[type] =
  372. fsnotify_next_mark(iter_info->marks[type]);
  373. }
  374. }
  375. /*
  376. * fsnotify - This is the main call to fsnotify.
  377. *
  378. * The VFS calls into hook specific functions in linux/fsnotify.h.
  379. * Those functions then in turn call here. Here will call out to all of the
  380. * registered fsnotify_group. Those groups can then use the notification event
  381. * in whatever means they feel necessary.
  382. *
  383. * @mask: event type and flags
  384. * @data: object that event happened on
  385. * @data_type: type of object for fanotify_data_XXX() accessors
  386. * @dir: optional directory associated with event -
  387. * if @file_name is not NULL, this is the directory that
  388. * @file_name is relative to
  389. * @file_name: optional file name associated with event
  390. * @inode: optional inode associated with event -
  391. * either @dir or @inode must be non-NULL.
  392. * if both are non-NULL event may be reported to both.
  393. * @cookie: inotify rename cookie
  394. */
  395. int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
  396. const struct qstr *file_name, struct inode *inode, u32 cookie)
  397. {
  398. const struct path *path = fsnotify_data_path(data, data_type);
  399. struct fsnotify_iter_info iter_info = {};
  400. struct super_block *sb;
  401. struct mount *mnt = NULL;
  402. struct inode *parent = NULL;
  403. int ret = 0;
  404. __u32 test_mask, marks_mask;
  405. if (path)
  406. mnt = real_mount(path->mnt);
  407. if (!inode) {
  408. /* Dirent event - report on TYPE_INODE to dir */
  409. inode = dir;
  410. } else if (mask & FS_EVENT_ON_CHILD) {
  411. /*
  412. * Event on child - report on TYPE_PARENT to dir if it is
  413. * watching children and on TYPE_INODE to child.
  414. */
  415. parent = dir;
  416. }
  417. sb = inode->i_sb;
  418. /*
  419. * Optimization: srcu_read_lock() has a memory barrier which can
  420. * be expensive. It protects walking the *_fsnotify_marks lists.
  421. * However, if we do not walk the lists, we do not have to do
  422. * SRCU because we have no references to any objects and do not
  423. * need SRCU to keep them "alive".
  424. */
  425. if (!sb->s_fsnotify_marks &&
  426. (!mnt || !mnt->mnt_fsnotify_marks) &&
  427. (!inode || !inode->i_fsnotify_marks) &&
  428. (!parent || !parent->i_fsnotify_marks))
  429. return 0;
  430. marks_mask = sb->s_fsnotify_mask;
  431. if (mnt)
  432. marks_mask |= mnt->mnt_fsnotify_mask;
  433. if (inode)
  434. marks_mask |= inode->i_fsnotify_mask;
  435. if (parent)
  436. marks_mask |= parent->i_fsnotify_mask;
  437. /*
  438. * if this is a modify event we may need to clear the ignored masks
  439. * otherwise return if none of the marks care about this type of event.
  440. */
  441. test_mask = (mask & ALL_FSNOTIFY_EVENTS);
  442. if (!(mask & FS_MODIFY) && !(test_mask & marks_mask))
  443. return 0;
  444. iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
  445. iter_info.marks[FSNOTIFY_OBJ_TYPE_SB] =
  446. fsnotify_first_mark(&sb->s_fsnotify_marks);
  447. if (mnt) {
  448. iter_info.marks[FSNOTIFY_OBJ_TYPE_VFSMOUNT] =
  449. fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
  450. }
  451. if (inode) {
  452. iter_info.marks[FSNOTIFY_OBJ_TYPE_INODE] =
  453. fsnotify_first_mark(&inode->i_fsnotify_marks);
  454. }
  455. if (parent) {
  456. iter_info.marks[FSNOTIFY_OBJ_TYPE_PARENT] =
  457. fsnotify_first_mark(&parent->i_fsnotify_marks);
  458. }
  459. /*
  460. * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
  461. * ignore masks are properly reflected for mount/sb mark notifications.
  462. * That's why this traversal is so complicated...
  463. */
  464. while (fsnotify_iter_select_report_types(&iter_info)) {
  465. ret = send_to_group(mask, data, data_type, dir, file_name,
  466. cookie, &iter_info);
  467. if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
  468. goto out;
  469. fsnotify_iter_next(&iter_info);
  470. }
  471. ret = 0;
  472. out:
  473. srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx);
  474. return ret;
  475. }
  476. EXPORT_SYMBOL_GPL(fsnotify);
  477. static __init int fsnotify_init(void)
  478. {
  479. int ret;
  480. BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25);
  481. ret = init_srcu_struct(&fsnotify_mark_srcu);
  482. if (ret)
  483. panic("initializing fsnotify_mark_srcu");
  484. fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
  485. SLAB_PANIC);
  486. return 0;
  487. }
  488. core_initcall(fsnotify_init);