1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 290586523SEric Paris /* 390586523SEric Paris * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> 490586523SEric Paris */ 590586523SEric Paris 690586523SEric Paris #include <linux/dcache.h> 790586523SEric Paris #include <linux/fs.h> 85a0e3ad6STejun Heo #include <linux/gfp.h> 990586523SEric Paris #include <linux/init.h> 1090586523SEric Paris #include <linux/module.h> 117131485aSEric Paris #include <linux/mount.h> 1290586523SEric Paris #include <linux/srcu.h> 1390586523SEric Paris 1490586523SEric Paris #include <linux/fsnotify_backend.h> 1590586523SEric Paris #include "fsnotify.h" 1690586523SEric Paris 1790586523SEric Paris /* 183be25f49SEric Paris * Clear all of the marks on an inode when it is being evicted from core 193be25f49SEric Paris */ 203be25f49SEric Paris void __fsnotify_inode_delete(struct inode *inode) 213be25f49SEric Paris { 223be25f49SEric Paris fsnotify_clear_marks_by_inode(inode); 233be25f49SEric Paris } 243be25f49SEric Paris EXPORT_SYMBOL_GPL(__fsnotify_inode_delete); 253be25f49SEric Paris 26ca9c726eSAndreas Gruenbacher void __fsnotify_vfsmount_delete(struct vfsmount *mnt) 27ca9c726eSAndreas Gruenbacher { 28ca9c726eSAndreas Gruenbacher fsnotify_clear_marks_by_mount(mnt); 29ca9c726eSAndreas Gruenbacher } 30ca9c726eSAndreas Gruenbacher 31ebb3b47eSJan Kara /** 32ebb3b47eSJan Kara * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes. 33ebb3b47eSJan Kara * @sb: superblock being unmounted. 34ebb3b47eSJan Kara * 35ebb3b47eSJan Kara * Called during unmount with no locks held, so needs to be safe against 36ebb3b47eSJan Kara * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block. 37ebb3b47eSJan Kara */ 381e6cb723SAmir Goldstein static void fsnotify_unmount_inodes(struct super_block *sb) 39ebb3b47eSJan Kara { 40ebb3b47eSJan Kara struct inode *inode, *iput_inode = NULL; 41ebb3b47eSJan Kara 42ebb3b47eSJan Kara spin_lock(&sb->s_inode_list_lock); 43ebb3b47eSJan Kara list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 44ebb3b47eSJan Kara /* 45ebb3b47eSJan Kara * We cannot __iget() an inode in state I_FREEING, 46ebb3b47eSJan Kara * I_WILL_FREE, or I_NEW which is fine because by that point 47ebb3b47eSJan Kara * the inode cannot have any associated watches. 48ebb3b47eSJan Kara */ 49ebb3b47eSJan Kara spin_lock(&inode->i_lock); 50ebb3b47eSJan Kara if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) { 51ebb3b47eSJan Kara spin_unlock(&inode->i_lock); 52ebb3b47eSJan Kara continue; 53ebb3b47eSJan Kara } 54ebb3b47eSJan Kara 55ebb3b47eSJan Kara /* 56ebb3b47eSJan Kara * If i_count is zero, the inode cannot have any watches and 571751e8a6SLinus Torvalds * doing an __iget/iput with SB_ACTIVE clear would actually 58ebb3b47eSJan Kara * evict all inodes with zero i_count from icache which is 59ebb3b47eSJan Kara * unnecessarily violent and may in fact be illegal to do. 601edc8eb2SEric Sandeen * However, we should have been called /after/ evict_inodes 611edc8eb2SEric Sandeen * removed all zero refcount inodes, in any case. Test to 621edc8eb2SEric Sandeen * be sure. 63ebb3b47eSJan Kara */ 64ebb3b47eSJan Kara if (!atomic_read(&inode->i_count)) { 65ebb3b47eSJan Kara spin_unlock(&inode->i_lock); 66ebb3b47eSJan Kara continue; 67ebb3b47eSJan Kara } 68ebb3b47eSJan Kara 69ebb3b47eSJan Kara __iget(inode); 70ebb3b47eSJan Kara spin_unlock(&inode->i_lock); 71ebb3b47eSJan Kara spin_unlock(&sb->s_inode_list_lock); 72ebb3b47eSJan Kara 73ebb3b47eSJan Kara if (iput_inode) 74ebb3b47eSJan Kara iput(iput_inode); 75ebb3b47eSJan Kara 76ebb3b47eSJan Kara /* for each watch, send FS_UNMOUNT and then remove it */ 7782ace1efSAmir Goldstein fsnotify_inode(inode, FS_UNMOUNT); 78ebb3b47eSJan Kara 79ebb3b47eSJan Kara fsnotify_inode_delete(inode); 80ebb3b47eSJan Kara 81ebb3b47eSJan Kara iput_inode = inode; 82ebb3b47eSJan Kara 8304646aebSEric Sandeen cond_resched(); 84ebb3b47eSJan Kara spin_lock(&sb->s_inode_list_lock); 85ebb3b47eSJan Kara } 86ebb3b47eSJan Kara spin_unlock(&sb->s_inode_list_lock); 87ebb3b47eSJan Kara 88ebb3b47eSJan Kara if (iput_inode) 89ebb3b47eSJan Kara iput(iput_inode); 90ebb3b47eSJan Kara } 91ebb3b47eSJan Kara 921e6cb723SAmir Goldstein void fsnotify_sb_delete(struct super_block *sb) 931e6cb723SAmir Goldstein { 941e6cb723SAmir Goldstein fsnotify_unmount_inodes(sb); 951e6cb723SAmir Goldstein fsnotify_clear_marks_by_sb(sb); 96ec44610fSAmir Goldstein /* Wait for outstanding object references from connectors */ 97ec44610fSAmir Goldstein wait_var_event(&sb->s_fsnotify_connectors, 98ec44610fSAmir Goldstein !atomic_long_read(&sb->s_fsnotify_connectors)); 991e6cb723SAmir Goldstein } 1001e6cb723SAmir Goldstein 1013be25f49SEric Paris /* 102c28f7e56SEric Paris * Given an inode, first check if we care what happens to our children. Inotify 103c28f7e56SEric Paris * and dnotify both tell their parents about events. If we care about any event 104c28f7e56SEric Paris * on a child we run all of our children and set a dentry flag saying that the 105c28f7e56SEric Paris * parent cares. Thus when an event happens on a child it can quickly tell if 106c28f7e56SEric Paris * if there is a need to find a parent and send the event to the parent. 107c28f7e56SEric Paris */ 108c28f7e56SEric Paris void __fsnotify_update_child_dentry_flags(struct inode *inode) 109c28f7e56SEric Paris { 110c28f7e56SEric Paris struct dentry *alias; 111c28f7e56SEric Paris int watched; 112c28f7e56SEric Paris 113c28f7e56SEric Paris if (!S_ISDIR(inode->i_mode)) 114c28f7e56SEric Paris return; 115c28f7e56SEric Paris 116c28f7e56SEric Paris /* determine if the children should tell inode about their events */ 117c28f7e56SEric Paris watched = fsnotify_inode_watches_children(inode); 118c28f7e56SEric Paris 119873feea0SNick Piggin spin_lock(&inode->i_lock); 120c28f7e56SEric Paris /* run all of the dentries associated with this inode. Since this is a 121c28f7e56SEric Paris * directory, there damn well better only be one item on this list */ 122946e51f2SAl Viro hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 123c28f7e56SEric Paris struct dentry *child; 124c28f7e56SEric Paris 125c28f7e56SEric Paris /* run all of the children of the original inode and fix their 126c28f7e56SEric Paris * d_flags to indicate parental interest (their parent is the 127c28f7e56SEric Paris * original inode) */ 1282fd6b7f5SNick Piggin spin_lock(&alias->d_lock); 129946e51f2SAl Viro list_for_each_entry(child, &alias->d_subdirs, d_child) { 130c28f7e56SEric Paris if (!child->d_inode) 131c28f7e56SEric Paris continue; 132c28f7e56SEric Paris 1332fd6b7f5SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); 134c28f7e56SEric Paris if (watched) 135c28f7e56SEric Paris child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED; 136c28f7e56SEric Paris else 137c28f7e56SEric Paris child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED; 138c28f7e56SEric Paris spin_unlock(&child->d_lock); 139c28f7e56SEric Paris } 1402fd6b7f5SNick Piggin spin_unlock(&alias->d_lock); 141c28f7e56SEric Paris } 142873feea0SNick Piggin spin_unlock(&inode->i_lock); 143c28f7e56SEric Paris } 144c28f7e56SEric Paris 1459b93f331SAmir Goldstein /* Are inode/sb/mount interested in parent and name info with this event? */ 1469b93f331SAmir Goldstein static bool fsnotify_event_needs_parent(struct inode *inode, struct mount *mnt, 1479b93f331SAmir Goldstein __u32 mask) 1489b93f331SAmir Goldstein { 1499b93f331SAmir Goldstein __u32 marks_mask = 0; 1509b93f331SAmir Goldstein 1519b93f331SAmir Goldstein /* We only send parent/name to inode/sb/mount for events on non-dir */ 1529b93f331SAmir Goldstein if (mask & FS_ISDIR) 1539b93f331SAmir Goldstein return false; 1549b93f331SAmir Goldstein 155fecc4559SAmir Goldstein /* 156fecc4559SAmir Goldstein * All events that are possible on child can also may be reported with 157fecc4559SAmir Goldstein * parent/name info to inode/sb/mount. Otherwise, a watching parent 158fecc4559SAmir Goldstein * could result in events reported with unexpected name info to sb/mount. 159fecc4559SAmir Goldstein */ 160fecc4559SAmir Goldstein BUILD_BUG_ON(FS_EVENTS_POSS_ON_CHILD & ~FS_EVENTS_POSS_TO_PARENT); 161fecc4559SAmir Goldstein 1629b93f331SAmir Goldstein /* Did either inode/sb/mount subscribe for events with parent/name? */ 1639b93f331SAmir Goldstein marks_mask |= fsnotify_parent_needed_mask(inode->i_fsnotify_mask); 1649b93f331SAmir Goldstein marks_mask |= fsnotify_parent_needed_mask(inode->i_sb->s_fsnotify_mask); 1659b93f331SAmir Goldstein if (mnt) 1669b93f331SAmir Goldstein marks_mask |= fsnotify_parent_needed_mask(mnt->mnt_fsnotify_mask); 1679b93f331SAmir Goldstein 1689b93f331SAmir Goldstein /* Did they subscribe for this event with parent/name info? */ 1699b93f331SAmir Goldstein return mask & marks_mask; 1709b93f331SAmir Goldstein } 1719b93f331SAmir Goldstein 172c738fbabSAmir Goldstein /* 173c738fbabSAmir Goldstein * Notify this dentry's parent about a child's events with child name info 1749b93f331SAmir Goldstein * if parent is watching or if inode/sb/mount are interested in events with 1759b93f331SAmir Goldstein * parent and name info. 1769b93f331SAmir Goldstein * 1779b93f331SAmir Goldstein * Notify only the child without name info if parent is not watching and 1789b93f331SAmir Goldstein * inode/sb/mount are not interested in events with parent and name info. 179c738fbabSAmir Goldstein */ 18071d73410SMel Gorman int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data, 181017de65fSAmir Goldstein int data_type) 182c28f7e56SEric Paris { 1839b93f331SAmir Goldstein const struct path *path = fsnotify_data_path(data, data_type); 1849b93f331SAmir Goldstein struct mount *mnt = path ? real_mount(path->mnt) : NULL; 185497b0c5aSAmir Goldstein struct inode *inode = d_inode(dentry); 186c28f7e56SEric Paris struct dentry *parent; 1879b93f331SAmir Goldstein bool parent_watched = dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED; 1887372e79cSAmir Goldstein bool parent_needed, parent_interested; 1899b93f331SAmir Goldstein __u32 p_mask; 19040a100d3SAmir Goldstein struct inode *p_inode = NULL; 191497b0c5aSAmir Goldstein struct name_snapshot name; 192497b0c5aSAmir Goldstein struct qstr *file_name = NULL; 19352420392SEric Paris int ret = 0; 194c28f7e56SEric Paris 1959b93f331SAmir Goldstein /* 1969b93f331SAmir Goldstein * Do inode/sb/mount care about parent and name info on non-dir? 1979b93f331SAmir Goldstein * Do they care about any event at all? 1989b93f331SAmir Goldstein */ 1999b93f331SAmir Goldstein if (!inode->i_fsnotify_marks && !inode->i_sb->s_fsnotify_marks && 2009b93f331SAmir Goldstein (!mnt || !mnt->mnt_fsnotify_marks) && !parent_watched) 2019b93f331SAmir Goldstein return 0; 2029b93f331SAmir Goldstein 203497b0c5aSAmir Goldstein parent = NULL; 2047372e79cSAmir Goldstein parent_needed = fsnotify_event_needs_parent(inode, mnt, mask); 2057372e79cSAmir Goldstein if (!parent_watched && !parent_needed) 206497b0c5aSAmir Goldstein goto notify; 207c28f7e56SEric Paris 2089b93f331SAmir Goldstein /* Does parent inode care about events on children? */ 2094d4eb366SChristoph Hellwig parent = dget_parent(dentry); 210c28f7e56SEric Paris p_inode = parent->d_inode; 2119b93f331SAmir Goldstein p_mask = fsnotify_inode_watches_children(p_inode); 2129b93f331SAmir Goldstein if (unlikely(parent_watched && !p_mask)) 2134d4eb366SChristoph Hellwig __fsnotify_update_child_dentry_flags(p_inode); 2149b93f331SAmir Goldstein 2159b93f331SAmir Goldstein /* 2169b93f331SAmir Goldstein * Include parent/name in notification either if some notification 2177372e79cSAmir Goldstein * groups require parent info or the parent is interested in this event. 2189b93f331SAmir Goldstein */ 2197372e79cSAmir Goldstein parent_interested = mask & p_mask & ALL_FSNOTIFY_EVENTS; 2207372e79cSAmir Goldstein if (parent_needed || parent_interested) { 221497b0c5aSAmir Goldstein /* When notifying parent, child should be passed as data */ 222497b0c5aSAmir Goldstein WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type)); 22349d31c2fSAl Viro 224497b0c5aSAmir Goldstein /* Notify both parent and child with child name info */ 22549d31c2fSAl Viro take_dentry_name_snapshot(&name, dentry); 226497b0c5aSAmir Goldstein file_name = &name.name; 2277372e79cSAmir Goldstein if (parent_interested) 228497b0c5aSAmir Goldstein mask |= FS_EVENT_ON_CHILD; 229c28f7e56SEric Paris } 230c28f7e56SEric Paris 231497b0c5aSAmir Goldstein notify: 23240a100d3SAmir Goldstein ret = fsnotify(mask, data, data_type, p_inode, file_name, inode, 0); 233497b0c5aSAmir Goldstein 234497b0c5aSAmir Goldstein if (file_name) 235497b0c5aSAmir Goldstein release_dentry_name_snapshot(&name); 236c28f7e56SEric Paris dput(parent); 23752420392SEric Paris 23852420392SEric Paris return ret; 239c28f7e56SEric Paris } 24071d73410SMel Gorman EXPORT_SYMBOL_GPL(__fsnotify_parent); 241c28f7e56SEric Paris 242950cc0d2SAmir Goldstein static int fsnotify_handle_inode_event(struct fsnotify_group *group, 243950cc0d2SAmir Goldstein struct fsnotify_mark *inode_mark, 244950cc0d2SAmir Goldstein u32 mask, const void *data, int data_type, 245950cc0d2SAmir Goldstein struct inode *dir, const struct qstr *name, 246950cc0d2SAmir Goldstein u32 cookie) 247950cc0d2SAmir Goldstein { 248950cc0d2SAmir Goldstein const struct path *path = fsnotify_data_path(data, data_type); 249950cc0d2SAmir Goldstein struct inode *inode = fsnotify_data_inode(data, data_type); 250950cc0d2SAmir Goldstein const struct fsnotify_ops *ops = group->ops; 251950cc0d2SAmir Goldstein 252950cc0d2SAmir Goldstein if (WARN_ON_ONCE(!ops->handle_inode_event)) 253950cc0d2SAmir Goldstein return 0; 254950cc0d2SAmir Goldstein 25524dca905SGabriel Krisman Bertazi if (WARN_ON_ONCE(!inode && !dir)) 25624dca905SGabriel Krisman Bertazi return 0; 25724dca905SGabriel Krisman Bertazi 258950cc0d2SAmir Goldstein if ((inode_mark->mask & FS_EXCL_UNLINK) && 259950cc0d2SAmir Goldstein path && d_unlinked(path->dentry)) 260950cc0d2SAmir Goldstein return 0; 261950cc0d2SAmir Goldstein 262fecc4559SAmir Goldstein /* Check interest of this mark in case event was sent with two marks */ 263fecc4559SAmir Goldstein if (!(mask & inode_mark->mask & ALL_FSNOTIFY_EVENTS)) 264fecc4559SAmir Goldstein return 0; 265fecc4559SAmir Goldstein 266950cc0d2SAmir Goldstein return ops->handle_inode_event(inode_mark, mask, inode, dir, name, cookie); 267950cc0d2SAmir Goldstein } 268950cc0d2SAmir Goldstein 269b9a1b977SAmir Goldstein static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask, 270b9a1b977SAmir Goldstein const void *data, int data_type, 271b9a1b977SAmir Goldstein struct inode *dir, const struct qstr *name, 272b9a1b977SAmir Goldstein u32 cookie, struct fsnotify_iter_info *iter_info) 273b9a1b977SAmir Goldstein { 274b9a1b977SAmir Goldstein struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info); 275fecc4559SAmir Goldstein struct fsnotify_mark *parent_mark = fsnotify_iter_parent_mark(iter_info); 276b9a1b977SAmir Goldstein int ret; 277b9a1b977SAmir Goldstein 278b9a1b977SAmir Goldstein if (WARN_ON_ONCE(fsnotify_iter_sb_mark(iter_info)) || 279b9a1b977SAmir Goldstein WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info))) 280b9a1b977SAmir Goldstein return 0; 281b9a1b977SAmir Goldstein 282*e54183faSAmir Goldstein /* 283*e54183faSAmir Goldstein * For FS_RENAME, 'dir' is old dir and 'data' is new dentry. 284*e54183faSAmir Goldstein * The only ->handle_inode_event() backend that supports FS_RENAME is 285*e54183faSAmir Goldstein * dnotify, where it means file was renamed within same parent. 286*e54183faSAmir Goldstein */ 287*e54183faSAmir Goldstein if (mask & FS_RENAME) { 288*e54183faSAmir Goldstein struct dentry *moved = fsnotify_data_dentry(data, data_type); 289*e54183faSAmir Goldstein 290*e54183faSAmir Goldstein if (dir != moved->d_parent->d_inode) 291*e54183faSAmir Goldstein return 0; 292*e54183faSAmir Goldstein } 293*e54183faSAmir Goldstein 294fecc4559SAmir Goldstein if (parent_mark) { 295b9a1b977SAmir Goldstein /* 296fecc4559SAmir Goldstein * parent_mark indicates that the parent inode is watching 297fecc4559SAmir Goldstein * children and interested in this event, which is an event 298fecc4559SAmir Goldstein * possible on child. But is *this mark* watching children and 299fecc4559SAmir Goldstein * interested in this event? 300b9a1b977SAmir Goldstein */ 301fecc4559SAmir Goldstein if (parent_mark->mask & FS_EVENT_ON_CHILD) { 302fecc4559SAmir Goldstein ret = fsnotify_handle_inode_event(group, parent_mark, mask, 303fecc4559SAmir Goldstein data, data_type, dir, name, 0); 304fecc4559SAmir Goldstein if (ret) 305fecc4559SAmir Goldstein return ret; 306fecc4559SAmir Goldstein } 307fecc4559SAmir Goldstein if (!inode_mark) 308fecc4559SAmir Goldstein return 0; 309fecc4559SAmir Goldstein } 310fecc4559SAmir Goldstein 311fecc4559SAmir Goldstein if (mask & FS_EVENT_ON_CHILD) { 312fecc4559SAmir Goldstein /* 313fecc4559SAmir Goldstein * Some events can be sent on both parent dir and child marks 314fecc4559SAmir Goldstein * (e.g. FS_ATTRIB). If both parent dir and child are 315fecc4559SAmir Goldstein * watching, report the event once to parent dir with name (if 316fecc4559SAmir Goldstein * interested) and once to child without name (if interested). 317fecc4559SAmir Goldstein * The child watcher is expecting an event without a file name 318fecc4559SAmir Goldstein * and without the FS_EVENT_ON_CHILD flag. 319fecc4559SAmir Goldstein */ 320fecc4559SAmir Goldstein mask &= ~FS_EVENT_ON_CHILD; 321b9a1b977SAmir Goldstein dir = NULL; 322b9a1b977SAmir Goldstein name = NULL; 323b9a1b977SAmir Goldstein } 324b9a1b977SAmir Goldstein 325fecc4559SAmir Goldstein return fsnotify_handle_inode_event(group, inode_mark, mask, data, data_type, 326950cc0d2SAmir Goldstein dir, name, cookie); 327b9a1b977SAmir Goldstein } 328b9a1b977SAmir Goldstein 329b54cecf5SAmir Goldstein static int send_to_group(__u32 mask, const void *data, int data_type, 330b54cecf5SAmir Goldstein struct inode *dir, const struct qstr *file_name, 331b54cecf5SAmir Goldstein u32 cookie, struct fsnotify_iter_info *iter_info) 3327131485aSEric Paris { 333faa9560aSEric Paris struct fsnotify_group *group = NULL; 334007d1e83SAmir Goldstein __u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS); 33592183a42SAmir Goldstein __u32 marks_mask = 0; 33692183a42SAmir Goldstein __u32 marks_ignored_mask = 0; 3373dca1a74SAmir Goldstein struct fsnotify_mark *mark; 3383dca1a74SAmir Goldstein int type; 339613a807fSEric Paris 3405b0457adSAmir Goldstein if (WARN_ON(!iter_info->report_mask)) 341faa9560aSEric Paris return 0; 3425ba08e2eSEric Paris 343ce8f76fbSEric Paris /* clear ignored on inode modification */ 344ce8f76fbSEric Paris if (mask & FS_MODIFY) { 3451c9007d6SAmir Goldstein fsnotify_foreach_iter_type(type) { 3463dca1a74SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 3473dca1a74SAmir Goldstein continue; 3483dca1a74SAmir Goldstein mark = iter_info->marks[type]; 3493dca1a74SAmir Goldstein if (mark && 3503dca1a74SAmir Goldstein !(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) 3513dca1a74SAmir Goldstein mark->ignored_mask = 0; 3523dca1a74SAmir Goldstein } 353ce8f76fbSEric Paris } 354613a807fSEric Paris 3551c9007d6SAmir Goldstein fsnotify_foreach_iter_type(type) { 3563dca1a74SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 3573dca1a74SAmir Goldstein continue; 3583dca1a74SAmir Goldstein mark = iter_info->marks[type]; 3593dca1a74SAmir Goldstein /* does the object mark tell us to do something? */ 3603dca1a74SAmir Goldstein if (mark) { 3613dca1a74SAmir Goldstein group = mark->group; 3623dca1a74SAmir Goldstein marks_mask |= mark->mask; 3633dca1a74SAmir Goldstein marks_ignored_mask |= mark->ignored_mask; 3643dca1a74SAmir Goldstein } 365ce8f76fbSEric Paris } 366ce8f76fbSEric Paris 367b54cecf5SAmir Goldstein pr_debug("%s: group=%p mask=%x marks_mask=%x marks_ignored_mask=%x data=%p data_type=%d dir=%p cookie=%d\n", 368b54cecf5SAmir Goldstein __func__, group, mask, marks_mask, marks_ignored_mask, 369b54cecf5SAmir Goldstein data, data_type, dir, cookie); 370faa9560aSEric Paris 37192183a42SAmir Goldstein if (!(test_mask & marks_mask & ~marks_ignored_mask)) 372613a807fSEric Paris return 0; 373613a807fSEric Paris 374b9a1b977SAmir Goldstein if (group->ops->handle_event) { 375b54cecf5SAmir Goldstein return group->ops->handle_event(group, mask, data, data_type, dir, 3769385a84dSJan Kara file_name, cookie, iter_info); 3777131485aSEric Paris } 3787131485aSEric Paris 379b9a1b977SAmir Goldstein return fsnotify_handle_event(group, mask, data, data_type, dir, 380b9a1b977SAmir Goldstein file_name, cookie, iter_info); 381b9a1b977SAmir Goldstein } 382b9a1b977SAmir Goldstein 3833427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp) 3843427ce71SMiklos Szeredi { 3853427ce71SMiklos Szeredi struct fsnotify_mark_connector *conn; 3863427ce71SMiklos Szeredi struct hlist_node *node = NULL; 3873427ce71SMiklos Szeredi 3883427ce71SMiklos Szeredi conn = srcu_dereference(*connp, &fsnotify_mark_srcu); 3893427ce71SMiklos Szeredi if (conn) 3903427ce71SMiklos Szeredi node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu); 3913427ce71SMiklos Szeredi 3923427ce71SMiklos Szeredi return hlist_entry_safe(node, struct fsnotify_mark, obj_list); 3933427ce71SMiklos Szeredi } 3943427ce71SMiklos Szeredi 3953427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark) 3963427ce71SMiklos Szeredi { 3973427ce71SMiklos Szeredi struct hlist_node *node = NULL; 3983427ce71SMiklos Szeredi 3993427ce71SMiklos Szeredi if (mark) 4003427ce71SMiklos Szeredi node = srcu_dereference(mark->obj_list.next, 4013427ce71SMiklos Szeredi &fsnotify_mark_srcu); 4023427ce71SMiklos Szeredi 4033427ce71SMiklos Szeredi return hlist_entry_safe(node, struct fsnotify_mark, obj_list); 4043427ce71SMiklos Szeredi } 4053427ce71SMiklos Szeredi 406c28f7e56SEric Paris /* 407d9a6f30bSAmir Goldstein * iter_info is a multi head priority queue of marks. 408d9a6f30bSAmir Goldstein * Pick a subset of marks from queue heads, all with the 409d9a6f30bSAmir Goldstein * same group and set the report_mask for selected subset. 410d9a6f30bSAmir Goldstein * Returns the report_mask of the selected subset. 411d9a6f30bSAmir Goldstein */ 412d9a6f30bSAmir Goldstein static unsigned int fsnotify_iter_select_report_types( 413d9a6f30bSAmir Goldstein struct fsnotify_iter_info *iter_info) 414d9a6f30bSAmir Goldstein { 41547d9c7ccSAmir Goldstein struct fsnotify_group *max_prio_group = NULL; 41647d9c7ccSAmir Goldstein struct fsnotify_mark *mark; 41747d9c7ccSAmir Goldstein int type; 418d9a6f30bSAmir Goldstein 41947d9c7ccSAmir Goldstein /* Choose max prio group among groups of all queue heads */ 4201c9007d6SAmir Goldstein fsnotify_foreach_iter_type(type) { 42147d9c7ccSAmir Goldstein mark = iter_info->marks[type]; 42247d9c7ccSAmir Goldstein if (mark && 42347d9c7ccSAmir Goldstein fsnotify_compare_groups(max_prio_group, mark->group) > 0) 42447d9c7ccSAmir Goldstein max_prio_group = mark->group; 425d9a6f30bSAmir Goldstein } 426d9a6f30bSAmir Goldstein 42747d9c7ccSAmir Goldstein if (!max_prio_group) 42847d9c7ccSAmir Goldstein return 0; 42947d9c7ccSAmir Goldstein 43047d9c7ccSAmir Goldstein /* Set the report mask for marks from same group as max prio group */ 431d9a6f30bSAmir Goldstein iter_info->report_mask = 0; 4321c9007d6SAmir Goldstein fsnotify_foreach_iter_type(type) { 43347d9c7ccSAmir Goldstein mark = iter_info->marks[type]; 43447d9c7ccSAmir Goldstein if (mark && 43547d9c7ccSAmir Goldstein fsnotify_compare_groups(max_prio_group, mark->group) == 0) 43647d9c7ccSAmir Goldstein fsnotify_iter_set_report_type(iter_info, type); 43747d9c7ccSAmir Goldstein } 438d9a6f30bSAmir Goldstein 439d9a6f30bSAmir Goldstein return iter_info->report_mask; 440d9a6f30bSAmir Goldstein } 441d9a6f30bSAmir Goldstein 442d9a6f30bSAmir Goldstein /* 443d9a6f30bSAmir Goldstein * Pop from iter_info multi head queue, the marks that were iterated in the 444d9a6f30bSAmir Goldstein * current iteration step. 445d9a6f30bSAmir Goldstein */ 446d9a6f30bSAmir Goldstein static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) 447d9a6f30bSAmir Goldstein { 44847d9c7ccSAmir Goldstein int type; 449d9a6f30bSAmir Goldstein 4501c9007d6SAmir Goldstein fsnotify_foreach_iter_type(type) { 45147d9c7ccSAmir Goldstein if (fsnotify_iter_should_report_type(iter_info, type)) 45247d9c7ccSAmir Goldstein iter_info->marks[type] = 45347d9c7ccSAmir Goldstein fsnotify_next_mark(iter_info->marks[type]); 45447d9c7ccSAmir Goldstein } 455d9a6f30bSAmir Goldstein } 456d9a6f30bSAmir Goldstein 457d9a6f30bSAmir Goldstein /* 45840a100d3SAmir Goldstein * fsnotify - This is the main call to fsnotify. 45940a100d3SAmir Goldstein * 46040a100d3SAmir Goldstein * The VFS calls into hook specific functions in linux/fsnotify.h. 46140a100d3SAmir Goldstein * Those functions then in turn call here. Here will call out to all of the 46240a100d3SAmir Goldstein * registered fsnotify_group. Those groups can then use the notification event 46340a100d3SAmir Goldstein * in whatever means they feel necessary. 46440a100d3SAmir Goldstein * 46540a100d3SAmir Goldstein * @mask: event type and flags 46640a100d3SAmir Goldstein * @data: object that event happened on 46740a100d3SAmir Goldstein * @data_type: type of object for fanotify_data_XXX() accessors 46840a100d3SAmir Goldstein * @dir: optional directory associated with event - 46940a100d3SAmir Goldstein * if @file_name is not NULL, this is the directory that 47040a100d3SAmir Goldstein * @file_name is relative to 47140a100d3SAmir Goldstein * @file_name: optional file name associated with event 47240a100d3SAmir Goldstein * @inode: optional inode associated with event - 47329335033SGabriel Krisman Bertazi * If @dir and @inode are both non-NULL, event may be 47429335033SGabriel Krisman Bertazi * reported to both. 47540a100d3SAmir Goldstein * @cookie: inotify rename cookie 47690586523SEric Paris */ 47740a100d3SAmir Goldstein int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, 47840a100d3SAmir Goldstein const struct qstr *file_name, struct inode *inode, u32 cookie) 47990586523SEric Paris { 480b54cecf5SAmir Goldstein const struct path *path = fsnotify_data_path(data, data_type); 48129335033SGabriel Krisman Bertazi struct super_block *sb = fsnotify_data_sb(data, data_type); 4823427ce71SMiklos Szeredi struct fsnotify_iter_info iter_info = {}; 48360f7ed8cSAmir Goldstein struct mount *mnt = NULL; 484*e54183faSAmir Goldstein struct inode *inode2 = NULL; 485*e54183faSAmir Goldstein struct dentry *moved; 486*e54183faSAmir Goldstein int inode2_type; 4879385a84dSJan Kara int ret = 0; 48871d73410SMel Gorman __u32 test_mask, marks_mask; 48990586523SEric Paris 49071d73410SMel Gorman if (path) 491aa93bdc5SAmir Goldstein mnt = real_mount(path->mnt); 492613a807fSEric Paris 49340a100d3SAmir Goldstein if (!inode) { 49440a100d3SAmir Goldstein /* Dirent event - report on TYPE_INODE to dir */ 49540a100d3SAmir Goldstein inode = dir; 496*e54183faSAmir Goldstein /* For FS_RENAME, inode is old_dir and inode2 is new_dir */ 497*e54183faSAmir Goldstein if (mask & FS_RENAME) { 498*e54183faSAmir Goldstein moved = fsnotify_data_dentry(data, data_type); 499*e54183faSAmir Goldstein inode2 = moved->d_parent->d_inode; 500*e54183faSAmir Goldstein inode2_type = FSNOTIFY_ITER_TYPE_INODE2; 501*e54183faSAmir Goldstein } 50240a100d3SAmir Goldstein } else if (mask & FS_EVENT_ON_CHILD) { 50340a100d3SAmir Goldstein /* 504fecc4559SAmir Goldstein * Event on child - report on TYPE_PARENT to dir if it is 505fecc4559SAmir Goldstein * watching children and on TYPE_INODE to child. 50640a100d3SAmir Goldstein */ 507*e54183faSAmir Goldstein inode2 = dir; 508*e54183faSAmir Goldstein inode2_type = FSNOTIFY_ITER_TYPE_PARENT; 50940a100d3SAmir Goldstein } 510497b0c5aSAmir Goldstein 511613a807fSEric Paris /* 5127c49b861SDave Hansen * Optimization: srcu_read_lock() has a memory barrier which can 5137c49b861SDave Hansen * be expensive. It protects walking the *_fsnotify_marks lists. 5147c49b861SDave Hansen * However, if we do not walk the lists, we do not have to do 5157c49b861SDave Hansen * SRCU because we have no references to any objects and do not 5167c49b861SDave Hansen * need SRCU to keep them "alive". 5177c49b861SDave Hansen */ 5189b93f331SAmir Goldstein if (!sb->s_fsnotify_marks && 519497b0c5aSAmir Goldstein (!mnt || !mnt->mnt_fsnotify_marks) && 5209b93f331SAmir Goldstein (!inode || !inode->i_fsnotify_marks) && 521*e54183faSAmir Goldstein (!inode2 || !inode2->i_fsnotify_marks)) 5227c49b861SDave Hansen return 0; 52371d73410SMel Gorman 5249b93f331SAmir Goldstein marks_mask = sb->s_fsnotify_mask; 52571d73410SMel Gorman if (mnt) 52671d73410SMel Gorman marks_mask |= mnt->mnt_fsnotify_mask; 5279b93f331SAmir Goldstein if (inode) 5289b93f331SAmir Goldstein marks_mask |= inode->i_fsnotify_mask; 529*e54183faSAmir Goldstein if (inode2) 530*e54183faSAmir Goldstein marks_mask |= inode2->i_fsnotify_mask; 531497b0c5aSAmir Goldstein 53271d73410SMel Gorman 5337c49b861SDave Hansen /* 534613a807fSEric Paris * if this is a modify event we may need to clear the ignored masks 535497b0c5aSAmir Goldstein * otherwise return if none of the marks care about this type of event. 536613a807fSEric Paris */ 53771d73410SMel Gorman test_mask = (mask & ALL_FSNOTIFY_EVENTS); 53871d73410SMel Gorman if (!(mask & FS_MODIFY) && !(test_mask & marks_mask)) 539613a807fSEric Paris return 0; 5403a9fb89fSEric Paris 5419385a84dSJan Kara iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu); 5427131485aSEric Paris 5431c9007d6SAmir Goldstein iter_info.marks[FSNOTIFY_ITER_TYPE_SB] = 54445a9fb37SAmir Goldstein fsnotify_first_mark(&sb->s_fsnotify_marks); 5459bdda4e9SAmir Goldstein if (mnt) { 5461c9007d6SAmir Goldstein iter_info.marks[FSNOTIFY_ITER_TYPE_VFSMOUNT] = 5473427ce71SMiklos Szeredi fsnotify_first_mark(&mnt->mnt_fsnotify_marks); 5487131485aSEric Paris } 5499b93f331SAmir Goldstein if (inode) { 5501c9007d6SAmir Goldstein iter_info.marks[FSNOTIFY_ITER_TYPE_INODE] = 5519b93f331SAmir Goldstein fsnotify_first_mark(&inode->i_fsnotify_marks); 5529b93f331SAmir Goldstein } 553*e54183faSAmir Goldstein if (inode2) { 554*e54183faSAmir Goldstein iter_info.marks[inode2_type] = 555*e54183faSAmir Goldstein fsnotify_first_mark(&inode2->i_fsnotify_marks); 556497b0c5aSAmir Goldstein } 55775c1be48SEric Paris 5588edc6e16SJan Kara /* 55960f7ed8cSAmir Goldstein * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark 56060f7ed8cSAmir Goldstein * ignore masks are properly reflected for mount/sb mark notifications. 5618edc6e16SJan Kara * That's why this traversal is so complicated... 5628edc6e16SJan Kara */ 563d9a6f30bSAmir Goldstein while (fsnotify_iter_select_report_types(&iter_info)) { 564b54cecf5SAmir Goldstein ret = send_to_group(mask, data, data_type, dir, file_name, 565b54cecf5SAmir Goldstein cookie, &iter_info); 56684a5b68eSEric Paris 567ff8bcbd0SEric Paris if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS)) 568ff8bcbd0SEric Paris goto out; 569ff8bcbd0SEric Paris 570d9a6f30bSAmir Goldstein fsnotify_iter_next(&iter_info); 5717131485aSEric Paris } 572ff8bcbd0SEric Paris ret = 0; 573ff8bcbd0SEric Paris out: 5749385a84dSJan Kara srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx); 575c4ec54b4SEric Paris 57698b5c10dSJean-Christophe Dubois return ret; 57790586523SEric Paris } 57890586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify); 57990586523SEric Paris 58090586523SEric Paris static __init int fsnotify_init(void) 58190586523SEric Paris { 58275c1be48SEric Paris int ret; 58375c1be48SEric Paris 58408b95c33SAmir Goldstein BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25); 58520dee624SEric Paris 58675c1be48SEric Paris ret = init_srcu_struct(&fsnotify_mark_srcu); 58775c1be48SEric Paris if (ret) 58875c1be48SEric Paris panic("initializing fsnotify_mark_srcu"); 58975c1be48SEric Paris 5909dd813c1SJan Kara fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector, 5919dd813c1SJan Kara SLAB_PANIC); 5929dd813c1SJan Kara 59375c1be48SEric Paris return 0; 59490586523SEric Paris } 59575c1be48SEric Paris core_initcall(fsnotify_init); 596