fsnotify.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. void __fsnotify_mntns_delete(struct mnt_namespace *mntns)
  27. {
  28. fsnotify_clear_marks_by_mntns(mntns);
  29. }
  30. void fsnotify_sb_delete(struct super_block *sb)
  31. {
  32. struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
  33. /* Were any marks ever added to any object on this sb? */
  34. if (!sbinfo)
  35. return;
  36. fsnotify_unmount_inodes(sbinfo);
  37. fsnotify_clear_marks_by_sb(sb);
  38. /* Wait for outstanding object references from connectors */
  39. wait_var_event(fsnotify_sb_watched_objects(sb),
  40. !atomic_long_read(fsnotify_sb_watched_objects(sb)));
  41. WARN_ON(fsnotify_sb_has_priority_watchers(sb, FSNOTIFY_PRIO_CONTENT));
  42. WARN_ON(fsnotify_sb_has_priority_watchers(sb,
  43. FSNOTIFY_PRIO_PRE_CONTENT));
  44. }
  45. void fsnotify_sb_free(struct super_block *sb)
  46. {
  47. if (sb->s_fsnotify_info) {
  48. WARN_ON_ONCE(!list_empty(&sb->s_fsnotify_info->inode_conn_list));
  49. kfree(sb->s_fsnotify_info);
  50. }
  51. }
  52. /*
  53. * Given an inode, first check if we care what happens to our children. Inotify
  54. * and dnotify both tell their parents about events. If we care about any event
  55. * on a child we run all of our children and set a dentry flag saying that the
  56. * parent cares. Thus when an event happens on a child it can quickly tell
  57. * if there is a need to find a parent and send the event to the parent.
  58. */
  59. void fsnotify_set_children_dentry_flags(struct inode *inode)
  60. {
  61. struct dentry *alias;
  62. if (!S_ISDIR(inode->i_mode))
  63. return;
  64. spin_lock(&inode->i_lock);
  65. /* run all of the dentries associated with this inode. Since this is a
  66. * directory, there damn well better only be one item on this list */
  67. hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
  68. struct dentry *child;
  69. /* run all of the children of the original inode and fix their
  70. * d_flags to indicate parental interest (their parent is the
  71. * original inode) */
  72. spin_lock(&alias->d_lock);
  73. hlist_for_each_entry(child, &alias->d_children, d_sib) {
  74. if (!child->d_inode)
  75. continue;
  76. spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
  77. child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
  78. spin_unlock(&child->d_lock);
  79. }
  80. spin_unlock(&alias->d_lock);
  81. }
  82. spin_unlock(&inode->i_lock);
  83. }
  84. /*
  85. * Lazily clear false positive PARENT_WATCHED flag for child whose parent had
  86. * stopped watching children.
  87. */
  88. static void fsnotify_clear_child_dentry_flag(struct inode *pinode,
  89. struct dentry *dentry)
  90. {
  91. spin_lock(&dentry->d_lock);
  92. /*
  93. * d_lock is a sufficient barrier to prevent observing a non-watched
  94. * parent state from before the fsnotify_set_children_dentry_flags()
  95. * or fsnotify_update_flags() call that had set PARENT_WATCHED.
  96. */
  97. if (!fsnotify_inode_watches_children(pinode))
  98. dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
  99. spin_unlock(&dentry->d_lock);
  100. }
  101. /* Are inode/sb/mount interested in parent and name info with this event? */
  102. static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask,
  103. __u32 mask)
  104. {
  105. __u32 marks_mask = 0;
  106. /* We only send parent/name to inode/sb/mount for events on non-dir */
  107. if (mask & FS_ISDIR)
  108. return false;
  109. /*
  110. * All events that are possible on child can also may be reported with
  111. * parent/name info to inode/sb/mount. Otherwise, a watching parent
  112. * could result in events reported with unexpected name info to sb/mount.
  113. */
  114. BUILD_BUG_ON(FS_EVENTS_POSS_ON_CHILD & ~FS_EVENTS_POSS_TO_PARENT);
  115. /* Did either inode/sb/mount subscribe for events with parent/name? */
  116. marks_mask |= fsnotify_parent_needed_mask(
  117. READ_ONCE(inode->i_fsnotify_mask));
  118. marks_mask |= fsnotify_parent_needed_mask(
  119. READ_ONCE(inode->i_sb->s_fsnotify_mask));
  120. marks_mask |= fsnotify_parent_needed_mask(mnt_mask);
  121. /* Did they subscribe for this event with parent/name info? */
  122. return mask & marks_mask;
  123. }
  124. /* Are there any inode/mount/sb objects that watch for these events? */
  125. static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
  126. __u32 mask)
  127. {
  128. __u32 marks_mask = READ_ONCE(inode->i_fsnotify_mask) | mnt_mask |
  129. READ_ONCE(inode->i_sb->s_fsnotify_mask);
  130. return mask & marks_mask & ALL_FSNOTIFY_EVENTS;
  131. }
  132. /* Report pre-content event with optional range info */
  133. int fsnotify_pre_content(const struct path *path, const loff_t *ppos,
  134. size_t count)
  135. {
  136. struct file_range range;
  137. /* Report page aligned range only when pos is known */
  138. if (!ppos)
  139. return fsnotify_path(path, FS_PRE_ACCESS);
  140. range.path = path;
  141. range.pos = PAGE_ALIGN_DOWN(*ppos);
  142. range.count = PAGE_ALIGN(*ppos + count) - range.pos;
  143. return fsnotify_parent(path->dentry, FS_PRE_ACCESS, &range,
  144. FSNOTIFY_EVENT_FILE_RANGE);
  145. }
  146. /*
  147. * Notify this dentry's parent about a child's events with child name info
  148. * if parent is watching or if inode/sb/mount are interested in events with
  149. * parent and name info.
  150. *
  151. * Notify only the child without name info if parent is not watching and
  152. * inode/sb/mount are not interested in events with parent and name info.
  153. */
  154. int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
  155. int data_type)
  156. {
  157. const struct path *path = fsnotify_data_path(data, data_type);
  158. __u32 mnt_mask = path ?
  159. READ_ONCE(real_mount(path->mnt)->mnt_fsnotify_mask) : 0;
  160. struct inode *inode = d_inode(dentry);
  161. struct dentry *parent;
  162. bool parent_watched = dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED;
  163. bool parent_needed, parent_interested;
  164. __u32 p_mask;
  165. struct inode *p_inode = NULL;
  166. struct name_snapshot name;
  167. struct qstr *file_name = NULL;
  168. int ret = 0;
  169. /* Optimize the likely case of nobody watching this path */
  170. if (likely(!parent_watched &&
  171. !fsnotify_object_watched(inode, mnt_mask, mask)))
  172. return 0;
  173. parent = NULL;
  174. parent_needed = fsnotify_event_needs_parent(inode, mnt_mask, mask);
  175. if (!parent_watched && !parent_needed)
  176. goto notify;
  177. /* Does parent inode care about events on children? */
  178. parent = dget_parent(dentry);
  179. p_inode = parent->d_inode;
  180. p_mask = fsnotify_inode_watches_children(p_inode);
  181. if (unlikely(parent_watched && !p_mask))
  182. fsnotify_clear_child_dentry_flag(p_inode, dentry);
  183. /*
  184. * Include parent/name in notification either if some notification
  185. * groups require parent info or the parent is interested in this event.
  186. * The parent interest in ACCESS/MODIFY events does not apply to special
  187. * files, where read/write are not on the filesystem of the parent and
  188. * events can provide an undesirable side-channel for information
  189. * exfiltration.
  190. */
  191. parent_interested = mask & p_mask & ALL_FSNOTIFY_EVENTS &&
  192. !(data_type == FSNOTIFY_EVENT_PATH &&
  193. d_is_special(dentry) &&
  194. (mask & (FS_ACCESS | FS_MODIFY)));
  195. if (parent_needed || parent_interested) {
  196. /* When notifying parent, child should be passed as data */
  197. WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type));
  198. /* Notify both parent and child with child name info */
  199. take_dentry_name_snapshot(&name, dentry);
  200. file_name = &name.name;
  201. if (parent_interested)
  202. mask |= FS_EVENT_ON_CHILD;
  203. }
  204. notify:
  205. ret = fsnotify(mask, data, data_type, p_inode, file_name, inode, 0);
  206. if (file_name)
  207. release_dentry_name_snapshot(&name);
  208. dput(parent);
  209. return ret;
  210. }
  211. EXPORT_SYMBOL_GPL(__fsnotify_parent);
  212. static int fsnotify_handle_inode_event(struct fsnotify_group *group,
  213. struct fsnotify_mark *inode_mark,
  214. u32 mask, const void *data, int data_type,
  215. struct inode *dir, const struct qstr *name,
  216. u32 cookie)
  217. {
  218. const struct path *path = fsnotify_data_path(data, data_type);
  219. struct inode *inode = fsnotify_data_inode(data, data_type);
  220. const struct fsnotify_ops *ops = group->ops;
  221. if (WARN_ON_ONCE(!ops->handle_inode_event))
  222. return 0;
  223. if (WARN_ON_ONCE(!inode && !dir))
  224. return 0;
  225. if ((inode_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) &&
  226. path && d_unlinked(path->dentry))
  227. return 0;
  228. /* Check interest of this mark in case event was sent with two marks */
  229. if (!(mask & inode_mark->mask & ALL_FSNOTIFY_EVENTS))
  230. return 0;
  231. return ops->handle_inode_event(inode_mark, mask, inode, dir, name, cookie);
  232. }
  233. static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
  234. const void *data, int data_type,
  235. struct inode *dir, const struct qstr *name,
  236. u32 cookie, struct fsnotify_iter_info *iter_info)
  237. {
  238. struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
  239. struct fsnotify_mark *parent_mark = fsnotify_iter_parent_mark(iter_info);
  240. int ret;
  241. if (WARN_ON_ONCE(fsnotify_iter_sb_mark(iter_info)) ||
  242. WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info)))
  243. return 0;
  244. /*
  245. * For FS_RENAME, 'dir' is old dir and 'data' is new dentry.
  246. * The only ->handle_inode_event() backend that supports FS_RENAME is
  247. * dnotify, where it means file was renamed within same parent.
  248. */
  249. if (mask & FS_RENAME) {
  250. struct dentry *moved = fsnotify_data_dentry(data, data_type);
  251. if (dir != moved->d_parent->d_inode)
  252. return 0;
  253. }
  254. if (parent_mark) {
  255. ret = fsnotify_handle_inode_event(group, parent_mark, mask,
  256. data, data_type, dir, name, 0);
  257. if (ret)
  258. return ret;
  259. }
  260. if (!inode_mark)
  261. return 0;
  262. /*
  263. * Some events can be sent on both parent dir and child marks (e.g.
  264. * FS_ATTRIB). If both parent dir and child are watching, report the
  265. * event once to parent dir with name (if interested) and once to child
  266. * without name (if interested).
  267. *
  268. * In any case regardless whether the parent is watching or not, the
  269. * child watcher is expecting an event without the FS_EVENT_ON_CHILD
  270. * flag. The file name is expected if and only if this is a directory
  271. * event.
  272. */
  273. mask &= ~FS_EVENT_ON_CHILD;
  274. if (!(mask & ALL_FSNOTIFY_DIRENT_EVENTS)) {
  275. dir = NULL;
  276. name = NULL;
  277. }
  278. return fsnotify_handle_inode_event(group, inode_mark, mask, data, data_type,
  279. dir, name, cookie);
  280. }
  281. static int send_to_group(__u32 mask, const void *data, int data_type,
  282. struct inode *dir, const struct qstr *file_name,
  283. u32 cookie, struct fsnotify_iter_info *iter_info)
  284. {
  285. struct fsnotify_group *group = NULL;
  286. __u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
  287. __u32 marks_mask = 0;
  288. __u32 marks_ignore_mask = 0;
  289. bool is_dir = mask & FS_ISDIR;
  290. struct fsnotify_mark *mark;
  291. int type;
  292. if (!iter_info->report_mask)
  293. return 0;
  294. /* clear ignored on inode modification */
  295. if (mask & FS_MODIFY) {
  296. fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
  297. if (!(mark->flags &
  298. FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
  299. mark->ignore_mask = 0;
  300. }
  301. }
  302. /* Are any of the group marks interested in this event? */
  303. fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
  304. group = mark->group;
  305. marks_mask |= mark->mask;
  306. marks_ignore_mask |=
  307. fsnotify_effective_ignore_mask(mark, is_dir, type);
  308. }
  309. pr_debug("%s: group=%p mask=%x marks_mask=%x marks_ignore_mask=%x data=%p data_type=%d dir=%p cookie=%d\n",
  310. __func__, group, mask, marks_mask, marks_ignore_mask,
  311. data, data_type, dir, cookie);
  312. if (!(test_mask & marks_mask & ~marks_ignore_mask))
  313. return 0;
  314. if (group->ops->handle_event) {
  315. return group->ops->handle_event(group, mask, data, data_type, dir,
  316. file_name, cookie, iter_info);
  317. }
  318. return fsnotify_handle_event(group, mask, data, data_type, dir,
  319. file_name, cookie, iter_info);
  320. }
  321. static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector *const *connp)
  322. {
  323. struct fsnotify_mark_connector *conn;
  324. struct hlist_node *node = NULL;
  325. conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
  326. if (conn)
  327. node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu);
  328. return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
  329. }
  330. static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
  331. {
  332. struct hlist_node *node = NULL;
  333. if (mark)
  334. node = srcu_dereference(mark->obj_list.next,
  335. &fsnotify_mark_srcu);
  336. return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
  337. }
  338. /*
  339. * iter_info is a multi head priority queue of marks.
  340. * Pick a subset of marks from queue heads, all with the same group
  341. * and set the report_mask to a subset of the selected marks.
  342. * Returns false if there are no more groups to iterate.
  343. */
  344. static bool fsnotify_iter_select_report_types(
  345. struct fsnotify_iter_info *iter_info)
  346. {
  347. struct fsnotify_group *max_prio_group = NULL;
  348. struct fsnotify_mark *mark;
  349. int type;
  350. /* Choose max prio group among groups of all queue heads */
  351. fsnotify_foreach_iter_type(type) {
  352. mark = iter_info->marks[type];
  353. if (mark &&
  354. fsnotify_compare_groups(max_prio_group, mark->group) > 0)
  355. max_prio_group = mark->group;
  356. }
  357. if (!max_prio_group)
  358. return false;
  359. /* Set the report mask for marks from same group as max prio group */
  360. iter_info->current_group = max_prio_group;
  361. iter_info->report_mask = 0;
  362. fsnotify_foreach_iter_type(type) {
  363. mark = iter_info->marks[type];
  364. if (mark && mark->group == iter_info->current_group) {
  365. /*
  366. * FSNOTIFY_ITER_TYPE_PARENT indicates that this inode
  367. * is watching children and interested in this event,
  368. * which is an event possible on child.
  369. * But is *this mark* watching children?
  370. */
  371. if (type == FSNOTIFY_ITER_TYPE_PARENT &&
  372. !(mark->mask & FS_EVENT_ON_CHILD) &&
  373. !(fsnotify_ignore_mask(mark) & FS_EVENT_ON_CHILD))
  374. continue;
  375. fsnotify_iter_set_report_type(iter_info, type);
  376. }
  377. }
  378. return true;
  379. }
  380. /*
  381. * Pop from iter_info multi head queue, the marks that belong to the group of
  382. * current iteration step.
  383. */
  384. static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
  385. {
  386. struct fsnotify_mark *mark;
  387. int type;
  388. /*
  389. * We cannot use fsnotify_foreach_iter_mark_type() here because we
  390. * may need to advance a mark of type X that belongs to current_group
  391. * but was not selected for reporting.
  392. */
  393. fsnotify_foreach_iter_type(type) {
  394. mark = iter_info->marks[type];
  395. if (mark && mark->group == iter_info->current_group)
  396. iter_info->marks[type] =
  397. fsnotify_next_mark(iter_info->marks[type]);
  398. }
  399. }
  400. /*
  401. * fsnotify - This is the main call to fsnotify.
  402. *
  403. * The VFS calls into hook specific functions in linux/fsnotify.h.
  404. * Those functions then in turn call here. Here will call out to all of the
  405. * registered fsnotify_group. Those groups can then use the notification event
  406. * in whatever means they feel necessary.
  407. *
  408. * @mask: event type and flags
  409. * @data: object that event happened on
  410. * @data_type: type of object for fanotify_data_XXX() accessors
  411. * @dir: optional directory associated with event -
  412. * if @file_name is not NULL, this is the directory that
  413. * @file_name is relative to
  414. * @file_name: optional file name associated with event
  415. * @inode: optional inode associated with event -
  416. * If @dir and @inode are both non-NULL, event may be
  417. * reported to both.
  418. * @cookie: inotify rename cookie
  419. */
  420. int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
  421. const struct qstr *file_name, struct inode *inode, u32 cookie)
  422. {
  423. const struct path *path = fsnotify_data_path(data, data_type);
  424. struct super_block *sb = fsnotify_data_sb(data, data_type);
  425. const struct fsnotify_mnt *mnt_data = fsnotify_data_mnt(data, data_type);
  426. struct fsnotify_sb_info *sbinfo = sb ? fsnotify_sb_info(sb) : NULL;
  427. struct fsnotify_iter_info iter_info = {};
  428. struct mount *mnt = NULL;
  429. struct inode *inode2 = NULL;
  430. struct dentry *moved;
  431. int inode2_type;
  432. int ret = 0;
  433. __u32 test_mask, marks_mask = 0;
  434. if (path)
  435. mnt = real_mount(path->mnt);
  436. if (!inode) {
  437. /* Dirent event - report on TYPE_INODE to dir */
  438. inode = dir;
  439. /* For FS_RENAME, inode is old_dir and inode2 is new_dir */
  440. if (mask & FS_RENAME) {
  441. moved = fsnotify_data_dentry(data, data_type);
  442. inode2 = moved->d_parent->d_inode;
  443. inode2_type = FSNOTIFY_ITER_TYPE_INODE2;
  444. }
  445. } else if (mask & FS_EVENT_ON_CHILD) {
  446. /*
  447. * Event on child - report on TYPE_PARENT to dir if it is
  448. * watching children and on TYPE_INODE to child.
  449. */
  450. inode2 = dir;
  451. inode2_type = FSNOTIFY_ITER_TYPE_PARENT;
  452. }
  453. /*
  454. * Optimization: srcu_read_lock() has a memory barrier which can
  455. * be expensive. It protects walking the *_fsnotify_marks lists.
  456. * However, if we do not walk the lists, we do not have to do
  457. * SRCU because we have no references to any objects and do not
  458. * need SRCU to keep them "alive".
  459. */
  460. if ((!sbinfo || !sbinfo->sb_marks) &&
  461. (!mnt || !mnt->mnt_fsnotify_marks) &&
  462. (!inode || !inode->i_fsnotify_marks) &&
  463. (!inode2 || !inode2->i_fsnotify_marks) &&
  464. (!mnt_data || !mnt_data->ns->n_fsnotify_marks))
  465. return 0;
  466. if (sb)
  467. marks_mask |= READ_ONCE(sb->s_fsnotify_mask);
  468. if (mnt)
  469. marks_mask |= READ_ONCE(mnt->mnt_fsnotify_mask);
  470. if (inode)
  471. marks_mask |= READ_ONCE(inode->i_fsnotify_mask);
  472. if (inode2)
  473. marks_mask |= READ_ONCE(inode2->i_fsnotify_mask);
  474. if (mnt_data)
  475. marks_mask |= READ_ONCE(mnt_data->ns->n_fsnotify_mask);
  476. /*
  477. * If this is a modify event we may need to clear some ignore masks.
  478. * In that case, the object with ignore masks will have the FS_MODIFY
  479. * event in its mask.
  480. * Otherwise, return if none of the marks care about this type of event.
  481. */
  482. test_mask = (mask & ALL_FSNOTIFY_EVENTS);
  483. if (!(test_mask & marks_mask))
  484. return 0;
  485. iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
  486. if (sbinfo) {
  487. iter_info.marks[FSNOTIFY_ITER_TYPE_SB] =
  488. fsnotify_first_mark(&sbinfo->sb_marks);
  489. }
  490. if (mnt) {
  491. iter_info.marks[FSNOTIFY_ITER_TYPE_VFSMOUNT] =
  492. fsnotify_first_mark(&mnt->mnt_fsnotify_marks);
  493. }
  494. if (inode) {
  495. iter_info.marks[FSNOTIFY_ITER_TYPE_INODE] =
  496. fsnotify_first_mark(&inode->i_fsnotify_marks);
  497. }
  498. if (inode2) {
  499. iter_info.marks[inode2_type] =
  500. fsnotify_first_mark(&inode2->i_fsnotify_marks);
  501. }
  502. if (mnt_data) {
  503. iter_info.marks[FSNOTIFY_ITER_TYPE_MNTNS] =
  504. fsnotify_first_mark(&mnt_data->ns->n_fsnotify_marks);
  505. }
  506. /*
  507. * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark
  508. * ignore masks are properly reflected for mount/sb mark notifications.
  509. * That's why this traversal is so complicated...
  510. */
  511. while (fsnotify_iter_select_report_types(&iter_info)) {
  512. ret = send_to_group(mask, data, data_type, dir, file_name,
  513. cookie, &iter_info);
  514. if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
  515. goto out;
  516. fsnotify_iter_next(&iter_info);
  517. }
  518. ret = 0;
  519. out:
  520. srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx);
  521. return ret;
  522. }
  523. EXPORT_SYMBOL_GPL(fsnotify);
  524. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  525. /*
  526. * At open time we check fsnotify_sb_has_priority_watchers(), call the open perm
  527. * hook and set the FMODE_NONOTIFY_ mode bits accordignly.
  528. * Later, fsnotify permission hooks do not check if there are permission event
  529. * watches, but that there were permission event watches at open time.
  530. */
  531. int fsnotify_open_perm_and_set_mode(struct file *file)
  532. {
  533. struct dentry *dentry = file->f_path.dentry, *parent;
  534. struct super_block *sb = dentry->d_sb;
  535. __u32 mnt_mask, p_mask = 0;
  536. /* Is it a file opened by fanotify? */
  537. if (FMODE_FSNOTIFY_NONE(file->f_mode))
  538. return 0;
  539. /*
  540. * Permission events is a super set of pre-content events, so if there
  541. * are no permission event watchers, there are also no pre-content event
  542. * watchers and this is implied from the single FMODE_NONOTIFY_PERM bit.
  543. */
  544. if (likely(!fsnotify_sb_has_priority_watchers(sb,
  545. FSNOTIFY_PRIO_CONTENT))) {
  546. file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM);
  547. return 0;
  548. }
  549. /*
  550. * OK, there are some permission event watchers. Check if anybody is
  551. * watching for permission events on *this* file.
  552. */
  553. mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);
  554. p_mask = fsnotify_object_watched(d_inode(dentry), mnt_mask,
  555. ALL_FSNOTIFY_PERM_EVENTS);
  556. if (dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) {
  557. parent = dget_parent(dentry);
  558. p_mask |= fsnotify_inode_watches_children(d_inode(parent));
  559. dput(parent);
  560. }
  561. /*
  562. * Legacy FAN_ACCESS_PERM events have very high performance overhead,
  563. * so unlikely to be used in the wild. If they are used there will be
  564. * no optimizations at all.
  565. */
  566. if (unlikely(p_mask & FS_ACCESS_PERM)) {
  567. /* Enable all permission and pre-content events */
  568. file_set_fsnotify_mode(file, 0);
  569. goto open_perm;
  570. }
  571. /*
  572. * Pre-content events are only supported on regular files.
  573. * If there are pre-content event watchers and no permission access
  574. * watchers, set FMODE_NONOTIFY | FMODE_NONOTIFY_PERM to indicate that.
  575. * That is the common case with HSM service.
  576. */
  577. if (d_is_reg(dentry) && (p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) {
  578. file_set_fsnotify_mode(file, FMODE_NONOTIFY |
  579. FMODE_NONOTIFY_PERM);
  580. goto open_perm;
  581. }
  582. /* Nobody watching permission and pre-content events on this file */
  583. file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM);
  584. open_perm:
  585. /*
  586. * Send open perm events depending on object masks and regardless of
  587. * FMODE_NONOTIFY_PERM.
  588. */
  589. if (file->f_flags & __FMODE_EXEC && p_mask & FS_OPEN_EXEC_PERM) {
  590. int ret = fsnotify_path(&file->f_path, FS_OPEN_EXEC_PERM);
  591. if (ret)
  592. return ret;
  593. }
  594. if (p_mask & FS_OPEN_PERM)
  595. return fsnotify_path(&file->f_path, FS_OPEN_PERM);
  596. return 0;
  597. }
  598. #endif
  599. void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt)
  600. {
  601. struct fsnotify_mnt data = {
  602. .ns = ns,
  603. .mnt_id = real_mount(mnt)->mnt_id_unique,
  604. };
  605. if (WARN_ON_ONCE(!ns))
  606. return;
  607. /*
  608. * This is an optimization as well as making sure fsnotify_init() has
  609. * been called.
  610. */
  611. if (!ns->n_fsnotify_marks)
  612. return;
  613. fsnotify(mask, &data, FSNOTIFY_EVENT_MNT, NULL, NULL, NULL, 0);
  614. }
  615. static __init int fsnotify_init(void)
  616. {
  617. int ret;
  618. BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 26);
  619. ret = init_srcu_struct(&fsnotify_mark_srcu);
  620. if (ret)
  621. panic("initializing fsnotify_mark_srcu");
  622. fsnotify_init_connector_caches();
  623. return 0;
  624. }
  625. core_initcall(fsnotify_init);