fsnotify.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. iput(iput_inode);
  65. /* for each watch, send FS_UNMOUNT and then remove it */
  66. fsnotify_inode(inode, FS_UNMOUNT);
  67. fsnotify_inode_delete(inode);
  68. iput_inode = inode;
  69. cond_resched();
  70. spin_lock(&sb->s_inode_list_lock);
  71. }
  72. spin_unlock(&sb->s_inode_list_lock);
  73. iput(iput_inode);
  74. }
  75. void fsnotify_sb_delete(struct super_block *sb)
  76. {
  77. struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
  78. /* Were any marks ever added to any object on this sb? */
  79. if (!sbinfo)
  80. return;
  81. fsnotify_unmount_inodes(sb);
  82. fsnotify_clear_marks_by_sb(sb);
  83. /* Wait for outstanding object references from connectors */
  84. wait_var_event(fsnotify_sb_watched_objects(sb),
  85. !atomic_long_read(fsnotify_sb_watched_objects(sb)));
  86. WARN_ON(fsnotify_sb_has_priority_watchers(sb, FSNOTIFY_PRIO_CONTENT));
  87. WARN_ON(fsnotify_sb_has_priority_watchers(sb,
  88. FSNOTIFY_PRIO_PRE_CONTENT));
  89. }
  90. void fsnotify_sb_free(struct super_block *sb)
  91. {
  92. kfree(sb->s_fsnotify_info);
  93. }
  94. /*
  95. * Given an inode, first check if we care what happens to our children. Inotify
  96. * and dnotify both tell their parents about events. If we care about any event
  97. * on a child we run all of our children and set a dentry flag saying that the
  98. * parent cares. Thus when an event happens on a child it can quickly tell
  99. * if there is a need to find a parent and send the event to the parent.
  100. */
  101. void fsnotify_set_children_dentry_flags(struct inode *inode)
  102. {
  103. struct dentry *alias;
  104. if (!S_ISDIR(inode->i_mode))
  105. return;
  106. spin_lock(&inode->i_lock);
  107. /* run all of the dentries associated with this inode. Since this is a
  108. * directory, there damn well better only be one item on this list */
  109. hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
  110. struct dentry *child;
  111. /* run all of the children of the original inode and fix their
  112. * d_flags to indicate parental interest (their parent is the
  113. * original inode) */
  114. spin_lock(&alias->d_lock);
  115. hlist_for_each_entry(child, &alias->d_children, d_sib) {
  116. if (!child->d_inode)
  117. continue;
  118. spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
  119. child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
  120. spin_unlock(&child->d_lock);
  121. }
  122. spin_unlock(&alias->d_lock);
  123. }
  124. spin_unlock(&inode->i_lock);
  125. }
  126. /*
  127. * Lazily clear false positive PARENT_WATCHED flag for child whose parent had
  128. * stopped watching children.
  129. */
  130. static void fsnotify_clear_child_dentry_flag(struct inode *pinode,
  131. struct dentry *dentry)
  132. {
  133. spin_lock(&dentry->d_lock);
  134. /*
  135. * d_lock is a sufficient barrier to prevent observing a non-watched
  136. * parent state from before the fsnotify_set_children_dentry_flags()
  137. * or fsnotify_update_flags() call that had set PARENT_WATCHED.
  138. */
  139. if (!fsnotify_inode_watches_children(pinode))
  140. dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
  141. spin_unlock(&dentry->d_lock);
  142. }
  143. /* Are inode/sb/mount interested in parent and name info with this event? */
  144. static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask,
  145. __u32 mask)
  146. {
  147. __u32 marks_mask = 0;
  148. /* We only send parent/name to inode/sb/mount for events on non-dir */
  149. if (mask & FS_ISDIR)
  150. return false;
  151. /*
  152. * All events that are possible on child can also may be reported with
  153. * parent/name info to inode/sb/mount. Otherwise, a watching parent
  154. * could result in events reported with unexpected name info to sb/mount.
  155. */
  156. BUILD_BUG_ON(FS_EVENTS_POSS_ON_CHILD & ~FS_EVENTS_POSS_TO_PARENT);
  157. /* Did either inode/sb/mount subscribe for events with parent/name? */
  158. marks_mask |= fsnotify_parent_needed_mask(
  159. READ_ONCE(inode->i_fsnotify_mask));
  160. marks_mask |= fsnotify_parent_needed_mask(
  161. READ_ONCE(inode->i_sb->s_fsnotify_mask));
  162. marks_mask |= fsnotify_parent_needed_mask(mnt_mask);
  163. /* Did they subscribe for this event with parent/name info? */
  164. return mask & marks_mask;
  165. }
  166. /* Are there any inode/mount/sb objects that are interested in this event? */
  167. static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
  168. __u32 mask)
  169. {
  170. __u32 marks_mask = READ_ONCE(inode->i_fsnotify_mask) | mnt_mask |
  171. READ_ONCE(inode->i_sb->s_fsnotify_mask);
  172. return mask & marks_mask & ALL_FSNOTIFY_EVENTS;
  173. }
  174. /*
  175. * Notify this dentry's parent about a child's events with child name info
  176. * if parent is watching or if inode/sb/mount are interested in events with
  177. * parent and name info.
  178. *
  179. * Notify only the child without name info if parent is not watching and
  180. * inode/sb/mount are not interested in events with parent and name info.
  181. */
  182. int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
  183. int data_type)
  184. {
  185. const struct path *path = fsnotify_data_path(data, data_type);
  186. __u32 mnt_mask = path ?
  187. READ_ONCE(real_mount(path->mnt)->mnt_fsnotify_mask) : 0;
  188. struct inode *inode = d_inode(dentry);
  189. struct dentry *parent;
  190. bool parent_watched = dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED;
  191. bool parent_needed, parent_interested;
  192. __u32 p_mask;
  193. struct inode *p_inode = NULL;
  194. struct name_snapshot name;
  195. struct qstr *file_name = NULL;
  196. int ret = 0;
  197. /* Optimize the likely case of nobody watching this path */
  198. if (likely(!parent_watched &&
  199. !fsnotify_object_watched(inode, mnt_mask, mask)))
  200. return 0;
  201. parent = NULL;
  202. parent_needed = fsnotify_event_needs_parent(inode, mnt_mask, mask);
  203. if (!parent_watched && !parent_needed)
  204. goto notify;
  205. /* Does parent inode care about events on children? */
  206. parent = dget_parent(dentry);
  207. p_inode = parent->d_inode;
  208. p_mask = fsnotify_inode_watches_children(p_inode);
  209. if (unlikely(parent_watched && !p_mask))
  210. fsnotify_clear_child_dentry_flag(p_inode, dentry);
  211. /*
  212. * Include parent/name in notification either if some notification
  213. * groups require parent info or the parent is interested in this event.
  214. */
  215. parent_interested = mask & p_mask & ALL_FSNOTIFY_EVENTS;
  216. if (parent_needed || parent_interested) {
  217. /* When notifying parent, child should be passed as data */
  218. WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type));
  219. /* Notify both parent and child with child name info */
  220. take_dentry_name_snapshot(&name, dentry);
  221. file_name = &name.name;
  222. if (parent_interested)
  223. mask |= FS_EVENT_ON_CHILD;
  224. }
  225. notify:
  226. ret = fsnotify(mask, data, data_type, p_inode, file_name, inode, 0);
  227. if (file_name)
  228. release_dentry_name_snapshot(&name);
  229. dput(parent);
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(__fsnotify_parent);
  233. static int fsnotify_handle_inode_event(struct fsnotify_group *group,
  234. struct fsnotify_mark *inode_mark,
  235. u32 mask, const void *data, int data_type,
  236. struct inode *dir, const struct qstr *name,
  237. u32 cookie)
  238. {
  239. const struct path *path = fsnotify_data_path(data, data_type);
  240. struct inode *inode = fsnotify_data_inode(data, data_type);
  241. const struct fsnotify_ops *ops = group->ops;
  242. if (WARN_ON_ONCE(!ops->handle_inode_event))
  243. return 0;
  244. if (WARN_ON_ONCE(!inode && !dir))
  245. return 0;
  246. if ((inode_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) &&
  247. path && d_unlinked(path->dentry))
  248. return 0;
  249. /* Check interest of this mark in case event was sent with two marks */
  250. if (!(mask & inode_mark->mask & ALL_FSNOTIFY_EVENTS))
  251. return 0;
  252. return ops->handle_inode_event(inode_mark, mask, inode, dir, name, cookie);
  253. }
  254. static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
  255. const void *data, int data_type,
  256. struct inode *dir, const struct qstr *name,
  257. u32 cookie, struct fsnotify_iter_info *iter_info)
  258. {
  259. struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
  260. struct fsnotify_mark *parent_mark = fsnotify_iter_parent_mark(iter_info);
  261. int ret;
  262. if (WARN_ON_ONCE(fsnotify_iter_sb_mark(iter_info)) ||
  263. WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info)))
  264. return 0;
  265. /*
  266. * For FS_RENAME, 'dir' is old dir and 'data' is new dentry.
  267. * The only ->handle_inode_event() backend that supports FS_RENAME is
  268. * dnotify, where it means file was renamed within same parent.
  269. */
  270. if (mask & FS_RENAME) {
  271. struct dentry *moved = fsnotify_data_dentry(data, data_type);
  272. if (dir != moved->d_parent->d_inode)
  273. return 0;
  274. }
  275. if (parent_mark) {
  276. ret = fsnotify_handle_inode_event(group, parent_mark, mask,
  277. data, data_type, dir, name, 0);
  278. if (ret)
  279. return ret;
  280. }
  281. if (!inode_mark)
  282. return 0;
  283. /*
  284. * Some events can be sent on both parent dir and child marks (e.g.
  285. * FS_ATTRIB). If both parent dir and child are watching, report the
  286. * event once to parent dir with name (if interested) and once to child
  287. * without name (if interested).
  288. *
  289. * In any case regardless whether the parent is watching or not, the
  290. * child watcher is expecting an event without the FS_EVENT_ON_CHILD
  291. * flag. The file name is expected if and only if this is a directory
  292. * event.
  293. */
  294. mask &= ~FS_EVENT_ON_CHILD;
  295. if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS)) {
  296. dir = NULL;
  297. name = NULL;
  298. }
  299. return fsnotify_handle_inode_event(group, inode_mark, mask, data, data_type,
  300. dir, name, cookie);
  301. }
  302. static int send_to_group(__u32 mask, const void *data, int data_type,
  303. struct inode *dir, const struct qstr *file_name,
  304. u32 cookie, struct fsnotify_iter_info *iter_info)
  305. {
  306. struct fsnotify_group *group = NULL;
  307. __u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
  308. __u32 marks_mask = 0;
  309. __u32 marks_ignore_mask = 0;
  310. bool is_dir = mask & FS_ISDIR;
  311. struct fsnotify_mark *mark;
  312. int type;
  313. if (!iter_info->report_mask)
  314. return 0;
  315. /* clear ignored on inode modification */
  316. if (mask & FS_MODIFY) {
  317. fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
  318. if (!(mark->flags &
  319. FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
  320. mark->ignore_mask = 0;
  321. }
  322. }
  323. /* Are any of the group marks interested in this event? */
  324. fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
  325. group = mark->group;
  326. marks_mask |= mark->mask;
  327. marks_ignore_mask |=
  328. fsnotify_effective_ignore_mask(mark, is_dir, type);
  329. }
  330. pr_debug("%s: group=%p mask=%x marks_mask=%x marks_ignore_mask=%x data=%p data_type=%d dir=%p cookie=%d\n",
  331. __func__, group, mask, marks_mask, marks_ignore_mask,
  332. data, data_type, dir, cookie);
  333. if (!(test_mask & marks_mask & ~marks_ignore_mask))
  334. return 0;
  335. if (group->ops->handle_event) {
  336. return group->ops->handle_event(group, mask, data, data_type, dir,
  337. file_name, cookie, iter_info);
  338. }
  339. return fsnotify_handle_event(group, mask, data, data_type, dir,
  340. file_name, cookie, iter_info);
  341. }
  342. static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp)
  343. {
  344. struct fsnotify_mark_connector *conn;
  345. struct hlist_node *node = NULL;
  346. conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
  347. if (conn)
  348. node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu);
  349. return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
  350. }
  351. static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
  352. {
  353. struct hlist_node *node = NULL;
  354. if (mark)
  355. node = srcu_dereference(mark->obj_list.next,
  356. &fsnotify_mark_srcu);
  357. return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
  358. }
  359. /*
  360. * iter_info is a multi head priority queue of marks.
  361. * Pick a subset of marks from queue heads, all with the same group
  362. * and set the report_mask to a subset of the selected marks.
  363. * Returns false if there are no more groups to iterate.
  364. */
  365. static bool fsnotify_iter_select_report_types(
  366. struct fsnotify_iter_info *iter_info)
  367. {
  368. struct fsnotify_group *max_prio_group = NULL;
  369. struct fsnotify_mark *mark;
  370. int type;
  371. /* Choose max prio group among groups of all queue heads */
  372. fsnotify_foreach_iter_type(type) {
  373. mark = iter_info->marks[type];
  374. if (mark &&
  375. fsnotify_compare_groups(max_prio_group, mark->group) > 0)
  376. max_prio_group = mark->group;
  377. }
  378. if (!max_prio_group)
  379. return false;
  380. /* Set the report mask for marks from same group as max prio group */
  381. iter_info->current_group = max_prio_group;
  382. iter_info->report_mask = 0;
  383. fsnotify_foreach_iter_type(type) {
  384. mark = iter_info->marks[type];
  385. if (mark && mark->group == iter_info->current_group) {
  386. /*
  387. * FSNOTIFY_ITER_TYPE_PARENT indicates that this inode
  388. * is watching children and interested in this event,
  389. * which is an event possible on child.
  390. * But is *this mark* watching children?
  391. */
  392. if (type == FSNOTIFY_ITER_TYPE_PARENT &&
  393. !(mark->mask & FS_EVENT_ON_CHILD) &&
  394. !(fsnotify_ignore_mask(mark) & FS_EVENT_ON_CHILD))
  395. continue;
  396. fsnotify_iter_set_report_type(iter_info, type);
  397. }
  398. }
  399. return true;
  400. }
  401. /*
  402. * Pop from iter_info multi head queue, the marks that belong to the group of
  403. * current iteration step.
  404. */
  405. static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
  406. {
  407. struct fsnotify_mark *mark;
  408. int type;
  409. /*
  410. * We cannot use fsnotify_foreach_iter_mark_type() here because we
  411. * may need to advance a mark of type X that belongs to current_group
  412. * but was not selected for reporting.
  413. */
  414. fsnotify_foreach_iter_type(type) {
  415. mark = iter_info->marks[type];
  416. if (mark && mark->group == iter_info->current_group)
  417. iter_info->marks[type] =
  418. fsnotify_next_mark(iter_info->marks[type]);
  419. }
  420. }
  421. /*
  422. * fsnotify - This is the main call to fsnotify.
  423. *
  424. * The VFS calls into hook specific functions in linux/fsnotify.h.
  425. * Those functions then in turn call here. Here will call out to all of the
  426. * registered fsnotify_group. Those groups can then use the notification event
  427. * in whatever means they feel necessary.
  428. *
  429. * @mask: event type and flags
  430. * @data: object that event happened on
  431. * @data_type: type of object for fanotify_data_XXX() accessors
  432. * @dir: optional directory associated with event -
  433. * if @file_name is not NULL, this is the directory that
  434. * @file_name is relative to
  435. * @file_name: optional file name associated with event
  436. * @inode: optional inode associated with event -
  437. * If @dir and @inode are both non-NULL, event may be
  438. * reported to both.
  439. * @cookie: inotify rename cookie
  440. */
  441. int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
  442. const struct qstr *file_name, struct inode *inode, u32 cookie)
  443. {
  444. const struct path *path = fsnotify_data_path(data, data_type);
  445. struct super_block *sb = fsnotify_data_sb(data, data_type);
  446. struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
  447. struct fsnotify_iter_info iter_info = {};
  448. struct mount *mnt = NULL;
  449. struct inode *inode2 = NULL;
  450. struct dentry *moved;
  451. int inode2_type;
  452. int ret = 0;
  453. __u32 test_mask, marks_mask;
  454. if (path)
  455. mnt = real_mount(path->mnt);
  456. if (!inode) {
  457. /* Dirent event - report on TYPE_INODE to dir */
  458. inode = dir;
  459. /* For FS_RENAME, inode is old_dir and inode2 is new_dir */
  460. if (mask & FS_RENAME) {
  461. moved = fsnotify_data_dentry(data, data_type);
  462. inode2 = moved->d_parent->d_inode;
  463. inode2_type = FSNOTIFY_ITER_TYPE_INODE2;
  464. }
  465. } else if (mask & FS_EVENT_ON_CHILD) {
  466. /*
  467. * Event on child - report on TYPE_PARENT to dir if it is
  468. * watching children and on TYPE_INODE to child.
  469. */
  470. inode2 = dir;
  471. inode2_type = FSNOTIFY_ITER_TYPE_PARENT;
  472. }
  473. /*
  474. * Optimization: srcu_read_lock() has a memory barrier which can
  475. * be expensive. It protects walking the *_fsnotify_marks lists.
  476. * However, if we do not walk the lists, we do not have to do
  477. * SRCU because we have no references to any objects and do not
  478. * need SRCU to keep them "alive".
  479. */
  480. if ((!sbinfo || !sbinfo->sb_marks) &&
  481. (!mnt || !mnt->mnt_fsnotify_marks) &&
  482. (!inode || !inode->i_fsnotify_marks) &&
  483. (!inode2 || !inode2->i_fsnotify_marks))
  484. return 0;
  485. marks_mask = READ_ONCE(sb->s_fsnotify_mask);
  486. if (mnt)
  487. marks_mask |= READ_ONCE(mnt->mnt_fsnotify_mask);
  488. if (inode)
  489. marks_mask |= READ_ONCE(inode->i_fsnotify_mask);
  490. if (inode2)
  491. marks_mask |= READ_ONCE(inode2->i_fsnotify_mask);
  492. /*
  493. * If this is a modify event we may need to clear some ignore masks.
  494. * In that case, the object with ignore masks will have the FS_MODIFY
  495. * event in its mask.
  496. * Otherwise, return if none of the marks care about this type of event.
  497. */
  498. test_mask = (mask & ALL_FSNOTIFY_EVENTS);
  499. if (!(test_mask & marks_mask))
  500. return 0;
  501. iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
  502. if (sbinfo) {
  503. iter_info.marks[FSNOTIFY_ITER_TYPE_SB] =
  504. fsnotify_first_mark(&sbinfo->sb_marks);
  505. }
  506. if (mnt) {
  507. iter_info.marks[FSNOTIFY_ITER_TYPE_VFSMOUNT] =
  508. fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
  509. }
  510. if (inode) {
  511. iter_info.marks[FSNOTIFY_ITER_TYPE_INODE] =
  512. fsnotify_first_mark(&inode->i_fsnotify_marks);
  513. }
  514. if (inode2) {
  515. iter_info.marks[inode2_type] =
  516. fsnotify_first_mark(&inode2->i_fsnotify_marks);
  517. }
  518. /*
  519. * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
  520. * ignore masks are properly reflected for mount/sb mark notifications.
  521. * That's why this traversal is so complicated...
  522. */
  523. while (fsnotify_iter_select_report_types(&iter_info)) {
  524. ret = send_to_group(mask, data, data_type, dir, file_name,
  525. cookie, &iter_info);
  526. if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
  527. goto out;
  528. fsnotify_iter_next(&iter_info);
  529. }
  530. ret = 0;
  531. out:
  532. srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx);
  533. return ret;
  534. }
  535. EXPORT_SYMBOL_GPL(fsnotify);
  536. static __init int fsnotify_init(void)
  537. {
  538. int ret;
  539. BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23);
  540. ret = init_srcu_struct(&fsnotify_mark_srcu);
  541. if (ret)
  542. panic("initializing fsnotify_mark_srcu");
  543. fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
  544. SLAB_PANIC);
  545. return 0;
  546. }
  547. core_initcall(fsnotify_init);