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 255950cc0d2SAmir Goldstein if ((inode_mark->mask & FS_EXCL_UNLINK) && 256950cc0d2SAmir Goldstein path && d_unlinked(path->dentry)) 257950cc0d2SAmir Goldstein return 0; 258950cc0d2SAmir Goldstein 259fecc4559SAmir Goldstein /* Check interest of this mark in case event was sent with two marks */ 260fecc4559SAmir Goldstein if (!(mask & inode_mark->mask & ALL_FSNOTIFY_EVENTS)) 261fecc4559SAmir Goldstein return 0; 262fecc4559SAmir Goldstein 263950cc0d2SAmir Goldstein return ops->handle_inode_event(inode_mark, mask, inode, dir, name, cookie); 264950cc0d2SAmir Goldstein } 265950cc0d2SAmir Goldstein 266b9a1b977SAmir Goldstein static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask, 267b9a1b977SAmir Goldstein const void *data, int data_type, 268b9a1b977SAmir Goldstein struct inode *dir, const struct qstr *name, 269b9a1b977SAmir Goldstein u32 cookie, struct fsnotify_iter_info *iter_info) 270b9a1b977SAmir Goldstein { 271b9a1b977SAmir Goldstein struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info); 272fecc4559SAmir Goldstein struct fsnotify_mark *parent_mark = fsnotify_iter_parent_mark(iter_info); 273b9a1b977SAmir Goldstein int ret; 274b9a1b977SAmir Goldstein 275b9a1b977SAmir Goldstein if (WARN_ON_ONCE(fsnotify_iter_sb_mark(iter_info)) || 276b9a1b977SAmir Goldstein WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info))) 277b9a1b977SAmir Goldstein return 0; 278b9a1b977SAmir Goldstein 279fecc4559SAmir Goldstein if (parent_mark) { 280b9a1b977SAmir Goldstein /* 281fecc4559SAmir Goldstein * parent_mark indicates that the parent inode is watching 282fecc4559SAmir Goldstein * children and interested in this event, which is an event 283fecc4559SAmir Goldstein * possible on child. But is *this mark* watching children and 284fecc4559SAmir Goldstein * interested in this event? 285b9a1b977SAmir Goldstein */ 286fecc4559SAmir Goldstein if (parent_mark->mask & FS_EVENT_ON_CHILD) { 287fecc4559SAmir Goldstein ret = fsnotify_handle_inode_event(group, parent_mark, mask, 288fecc4559SAmir Goldstein data, data_type, dir, name, 0); 289fecc4559SAmir Goldstein if (ret) 290fecc4559SAmir Goldstein return ret; 291fecc4559SAmir Goldstein } 292fecc4559SAmir Goldstein if (!inode_mark) 293fecc4559SAmir Goldstein return 0; 294fecc4559SAmir Goldstein } 295fecc4559SAmir Goldstein 296fecc4559SAmir Goldstein if (mask & FS_EVENT_ON_CHILD) { 297fecc4559SAmir Goldstein /* 298fecc4559SAmir Goldstein * Some events can be sent on both parent dir and child marks 299fecc4559SAmir Goldstein * (e.g. FS_ATTRIB). If both parent dir and child are 300fecc4559SAmir Goldstein * watching, report the event once to parent dir with name (if 301fecc4559SAmir Goldstein * interested) and once to child without name (if interested). 302fecc4559SAmir Goldstein * The child watcher is expecting an event without a file name 303fecc4559SAmir Goldstein * and without the FS_EVENT_ON_CHILD flag. 304fecc4559SAmir Goldstein */ 305fecc4559SAmir Goldstein mask &= ~FS_EVENT_ON_CHILD; 306b9a1b977SAmir Goldstein dir = NULL; 307b9a1b977SAmir Goldstein name = NULL; 308b9a1b977SAmir Goldstein } 309b9a1b977SAmir Goldstein 310fecc4559SAmir Goldstein return fsnotify_handle_inode_event(group, inode_mark, mask, data, data_type, 311950cc0d2SAmir Goldstein dir, name, cookie); 312b9a1b977SAmir Goldstein } 313b9a1b977SAmir Goldstein 314b54cecf5SAmir Goldstein static int send_to_group(__u32 mask, const void *data, int data_type, 315b54cecf5SAmir Goldstein struct inode *dir, const struct qstr *file_name, 316b54cecf5SAmir Goldstein u32 cookie, struct fsnotify_iter_info *iter_info) 3177131485aSEric Paris { 318faa9560aSEric Paris struct fsnotify_group *group = NULL; 319007d1e83SAmir Goldstein __u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS); 32092183a42SAmir Goldstein __u32 marks_mask = 0; 32192183a42SAmir Goldstein __u32 marks_ignored_mask = 0; 3223dca1a74SAmir Goldstein struct fsnotify_mark *mark; 3233dca1a74SAmir Goldstein int type; 324613a807fSEric Paris 3255b0457adSAmir Goldstein if (WARN_ON(!iter_info->report_mask)) 326faa9560aSEric Paris return 0; 3275ba08e2eSEric Paris 328ce8f76fbSEric Paris /* clear ignored on inode modification */ 329ce8f76fbSEric Paris if (mask & FS_MODIFY) { 3303dca1a74SAmir Goldstein fsnotify_foreach_obj_type(type) { 3313dca1a74SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 3323dca1a74SAmir Goldstein continue; 3333dca1a74SAmir Goldstein mark = iter_info->marks[type]; 3343dca1a74SAmir Goldstein if (mark && 3353dca1a74SAmir Goldstein !(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) 3363dca1a74SAmir Goldstein mark->ignored_mask = 0; 3373dca1a74SAmir Goldstein } 338ce8f76fbSEric Paris } 339613a807fSEric Paris 3403dca1a74SAmir Goldstein fsnotify_foreach_obj_type(type) { 3413dca1a74SAmir Goldstein if (!fsnotify_iter_should_report_type(iter_info, type)) 3423dca1a74SAmir Goldstein continue; 3433dca1a74SAmir Goldstein mark = iter_info->marks[type]; 3443dca1a74SAmir Goldstein /* does the object mark tell us to do something? */ 3453dca1a74SAmir Goldstein if (mark) { 3463dca1a74SAmir Goldstein group = mark->group; 3473dca1a74SAmir Goldstein marks_mask |= mark->mask; 3483dca1a74SAmir Goldstein marks_ignored_mask |= mark->ignored_mask; 3493dca1a74SAmir Goldstein } 350ce8f76fbSEric Paris } 351ce8f76fbSEric Paris 352b54cecf5SAmir 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", 353b54cecf5SAmir Goldstein __func__, group, mask, marks_mask, marks_ignored_mask, 354b54cecf5SAmir Goldstein data, data_type, dir, cookie); 355faa9560aSEric Paris 35692183a42SAmir Goldstein if (!(test_mask & marks_mask & ~marks_ignored_mask)) 357613a807fSEric Paris return 0; 358613a807fSEric Paris 359b9a1b977SAmir Goldstein if (group->ops->handle_event) { 360b54cecf5SAmir Goldstein return group->ops->handle_event(group, mask, data, data_type, dir, 3619385a84dSJan Kara file_name, cookie, iter_info); 3627131485aSEric Paris } 3637131485aSEric Paris 364b9a1b977SAmir Goldstein return fsnotify_handle_event(group, mask, data, data_type, dir, 365b9a1b977SAmir Goldstein file_name, cookie, iter_info); 366b9a1b977SAmir Goldstein } 367b9a1b977SAmir Goldstein 3683427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_first_mark(struct fsnotify_mark_connector **connp) 3693427ce71SMiklos Szeredi { 3703427ce71SMiklos Szeredi struct fsnotify_mark_connector *conn; 3713427ce71SMiklos Szeredi struct hlist_node *node = NULL; 3723427ce71SMiklos Szeredi 3733427ce71SMiklos Szeredi conn = srcu_dereference(*connp, &fsnotify_mark_srcu); 3743427ce71SMiklos Szeredi if (conn) 3753427ce71SMiklos Szeredi node = srcu_dereference(conn->list.first, &fsnotify_mark_srcu); 3763427ce71SMiklos Szeredi 3773427ce71SMiklos Szeredi return hlist_entry_safe(node, struct fsnotify_mark, obj_list); 3783427ce71SMiklos Szeredi } 3793427ce71SMiklos Szeredi 3803427ce71SMiklos Szeredi static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark) 3813427ce71SMiklos Szeredi { 3823427ce71SMiklos Szeredi struct hlist_node *node = NULL; 3833427ce71SMiklos Szeredi 3843427ce71SMiklos Szeredi if (mark) 3853427ce71SMiklos Szeredi node = srcu_dereference(mark->obj_list.next, 3863427ce71SMiklos Szeredi &fsnotify_mark_srcu); 3873427ce71SMiklos Szeredi 3883427ce71SMiklos Szeredi return hlist_entry_safe(node, struct fsnotify_mark, obj_list); 3893427ce71SMiklos Szeredi } 3903427ce71SMiklos Szeredi 391c28f7e56SEric Paris /* 392d9a6f30bSAmir Goldstein * iter_info is a multi head priority queue of marks. 393d9a6f30bSAmir Goldstein * Pick a subset of marks from queue heads, all with the 394d9a6f30bSAmir Goldstein * same group and set the report_mask for selected subset. 395d9a6f30bSAmir Goldstein * Returns the report_mask of the selected subset. 396d9a6f30bSAmir Goldstein */ 397d9a6f30bSAmir Goldstein static unsigned int fsnotify_iter_select_report_types( 398d9a6f30bSAmir Goldstein struct fsnotify_iter_info *iter_info) 399d9a6f30bSAmir Goldstein { 40047d9c7ccSAmir Goldstein struct fsnotify_group *max_prio_group = NULL; 40147d9c7ccSAmir Goldstein struct fsnotify_mark *mark; 40247d9c7ccSAmir Goldstein int type; 403d9a6f30bSAmir Goldstein 40447d9c7ccSAmir Goldstein /* Choose max prio group among groups of all queue heads */ 40547d9c7ccSAmir Goldstein fsnotify_foreach_obj_type(type) { 40647d9c7ccSAmir Goldstein mark = iter_info->marks[type]; 40747d9c7ccSAmir Goldstein if (mark && 40847d9c7ccSAmir Goldstein fsnotify_compare_groups(max_prio_group, mark->group) > 0) 40947d9c7ccSAmir Goldstein max_prio_group = mark->group; 410d9a6f30bSAmir Goldstein } 411d9a6f30bSAmir Goldstein 41247d9c7ccSAmir Goldstein if (!max_prio_group) 41347d9c7ccSAmir Goldstein return 0; 41447d9c7ccSAmir Goldstein 41547d9c7ccSAmir Goldstein /* Set the report mask for marks from same group as max prio group */ 416d9a6f30bSAmir Goldstein iter_info->report_mask = 0; 41747d9c7ccSAmir Goldstein fsnotify_foreach_obj_type(type) { 41847d9c7ccSAmir Goldstein mark = iter_info->marks[type]; 41947d9c7ccSAmir Goldstein if (mark && 42047d9c7ccSAmir Goldstein fsnotify_compare_groups(max_prio_group, mark->group) == 0) 42147d9c7ccSAmir Goldstein fsnotify_iter_set_report_type(iter_info, type); 42247d9c7ccSAmir Goldstein } 423d9a6f30bSAmir Goldstein 424d9a6f30bSAmir Goldstein return iter_info->report_mask; 425d9a6f30bSAmir Goldstein } 426d9a6f30bSAmir Goldstein 427d9a6f30bSAmir Goldstein /* 428d9a6f30bSAmir Goldstein * Pop from iter_info multi head queue, the marks that were iterated in the 429d9a6f30bSAmir Goldstein * current iteration step. 430d9a6f30bSAmir Goldstein */ 431d9a6f30bSAmir Goldstein static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) 432d9a6f30bSAmir Goldstein { 43347d9c7ccSAmir Goldstein int type; 434d9a6f30bSAmir Goldstein 43547d9c7ccSAmir Goldstein fsnotify_foreach_obj_type(type) { 43647d9c7ccSAmir Goldstein if (fsnotify_iter_should_report_type(iter_info, type)) 43747d9c7ccSAmir Goldstein iter_info->marks[type] = 43847d9c7ccSAmir Goldstein fsnotify_next_mark(iter_info->marks[type]); 43947d9c7ccSAmir Goldstein } 440d9a6f30bSAmir Goldstein } 441d9a6f30bSAmir Goldstein 442d9a6f30bSAmir Goldstein /* 44340a100d3SAmir Goldstein * fsnotify - This is the main call to fsnotify. 44440a100d3SAmir Goldstein * 44540a100d3SAmir Goldstein * The VFS calls into hook specific functions in linux/fsnotify.h. 44640a100d3SAmir Goldstein * Those functions then in turn call here. Here will call out to all of the 44740a100d3SAmir Goldstein * registered fsnotify_group. Those groups can then use the notification event 44840a100d3SAmir Goldstein * in whatever means they feel necessary. 44940a100d3SAmir Goldstein * 45040a100d3SAmir Goldstein * @mask: event type and flags 45140a100d3SAmir Goldstein * @data: object that event happened on 45240a100d3SAmir Goldstein * @data_type: type of object for fanotify_data_XXX() accessors 45340a100d3SAmir Goldstein * @dir: optional directory associated with event - 45440a100d3SAmir Goldstein * if @file_name is not NULL, this is the directory that 45540a100d3SAmir Goldstein * @file_name is relative to 45640a100d3SAmir Goldstein * @file_name: optional file name associated with event 45740a100d3SAmir Goldstein * @inode: optional inode associated with event - 458*29335033SGabriel Krisman Bertazi * If @dir and @inode are both non-NULL, event may be 459*29335033SGabriel Krisman Bertazi * reported to both. 46040a100d3SAmir Goldstein * @cookie: inotify rename cookie 46190586523SEric Paris */ 46240a100d3SAmir Goldstein int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, 46340a100d3SAmir Goldstein const struct qstr *file_name, struct inode *inode, u32 cookie) 46490586523SEric Paris { 465b54cecf5SAmir Goldstein const struct path *path = fsnotify_data_path(data, data_type); 466*29335033SGabriel Krisman Bertazi struct super_block *sb = fsnotify_data_sb(data, data_type); 4673427ce71SMiklos Szeredi struct fsnotify_iter_info iter_info = {}; 46860f7ed8cSAmir Goldstein struct mount *mnt = NULL; 469fecc4559SAmir Goldstein struct inode *parent = NULL; 4709385a84dSJan Kara int ret = 0; 47171d73410SMel Gorman __u32 test_mask, marks_mask; 47290586523SEric Paris 47371d73410SMel Gorman if (path) 474aa93bdc5SAmir Goldstein mnt = real_mount(path->mnt); 475613a807fSEric Paris 47640a100d3SAmir Goldstein if (!inode) { 47740a100d3SAmir Goldstein /* Dirent event - report on TYPE_INODE to dir */ 47840a100d3SAmir Goldstein inode = dir; 47940a100d3SAmir Goldstein } else if (mask & FS_EVENT_ON_CHILD) { 48040a100d3SAmir Goldstein /* 481fecc4559SAmir Goldstein * Event on child - report on TYPE_PARENT to dir if it is 482fecc4559SAmir Goldstein * watching children and on TYPE_INODE to child. 48340a100d3SAmir Goldstein */ 484fecc4559SAmir Goldstein parent = dir; 48540a100d3SAmir Goldstein } 486497b0c5aSAmir Goldstein 487613a807fSEric Paris /* 4887c49b861SDave Hansen * Optimization: srcu_read_lock() has a memory barrier which can 4897c49b861SDave Hansen * be expensive. It protects walking the *_fsnotify_marks lists. 4907c49b861SDave Hansen * However, if we do not walk the lists, we do not have to do 4917c49b861SDave Hansen * SRCU because we have no references to any objects and do not 4927c49b861SDave Hansen * need SRCU to keep them "alive". 4937c49b861SDave Hansen */ 4949b93f331SAmir Goldstein if (!sb->s_fsnotify_marks && 495497b0c5aSAmir Goldstein (!mnt || !mnt->mnt_fsnotify_marks) && 4969b93f331SAmir Goldstein (!inode || !inode->i_fsnotify_marks) && 497fecc4559SAmir Goldstein (!parent || !parent->i_fsnotify_marks)) 4987c49b861SDave Hansen return 0; 49971d73410SMel Gorman 5009b93f331SAmir Goldstein marks_mask = sb->s_fsnotify_mask; 50171d73410SMel Gorman if (mnt) 50271d73410SMel Gorman marks_mask |= mnt->mnt_fsnotify_mask; 5039b93f331SAmir Goldstein if (inode) 5049b93f331SAmir Goldstein marks_mask |= inode->i_fsnotify_mask; 505fecc4559SAmir Goldstein if (parent) 506fecc4559SAmir Goldstein marks_mask |= parent->i_fsnotify_mask; 507497b0c5aSAmir Goldstein 50871d73410SMel Gorman 5097c49b861SDave Hansen /* 510613a807fSEric Paris * if this is a modify event we may need to clear the ignored masks 511497b0c5aSAmir Goldstein * otherwise return if none of the marks care about this type of event. 512613a807fSEric Paris */ 51371d73410SMel Gorman test_mask = (mask & ALL_FSNOTIFY_EVENTS); 51471d73410SMel Gorman if (!(mask & FS_MODIFY) && !(test_mask & marks_mask)) 515613a807fSEric Paris return 0; 5163a9fb89fSEric Paris 5179385a84dSJan Kara iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu); 5187131485aSEric Paris 51945a9fb37SAmir Goldstein iter_info.marks[FSNOTIFY_OBJ_TYPE_SB] = 52045a9fb37SAmir Goldstein fsnotify_first_mark(&sb->s_fsnotify_marks); 5219bdda4e9SAmir Goldstein if (mnt) { 52247d9c7ccSAmir Goldstein iter_info.marks[FSNOTIFY_OBJ_TYPE_VFSMOUNT] = 5233427ce71SMiklos Szeredi fsnotify_first_mark(&mnt->mnt_fsnotify_marks); 5247131485aSEric Paris } 5259b93f331SAmir Goldstein if (inode) { 5269b93f331SAmir Goldstein iter_info.marks[FSNOTIFY_OBJ_TYPE_INODE] = 5279b93f331SAmir Goldstein fsnotify_first_mark(&inode->i_fsnotify_marks); 5289b93f331SAmir Goldstein } 529fecc4559SAmir Goldstein if (parent) { 530fecc4559SAmir Goldstein iter_info.marks[FSNOTIFY_OBJ_TYPE_PARENT] = 531fecc4559SAmir Goldstein fsnotify_first_mark(&parent->i_fsnotify_marks); 532497b0c5aSAmir Goldstein } 53375c1be48SEric Paris 5348edc6e16SJan Kara /* 53560f7ed8cSAmir Goldstein * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark 53660f7ed8cSAmir Goldstein * ignore masks are properly reflected for mount/sb mark notifications. 5378edc6e16SJan Kara * That's why this traversal is so complicated... 5388edc6e16SJan Kara */ 539d9a6f30bSAmir Goldstein while (fsnotify_iter_select_report_types(&iter_info)) { 540b54cecf5SAmir Goldstein ret = send_to_group(mask, data, data_type, dir, file_name, 541b54cecf5SAmir Goldstein cookie, &iter_info); 54284a5b68eSEric Paris 543ff8bcbd0SEric Paris if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS)) 544ff8bcbd0SEric Paris goto out; 545ff8bcbd0SEric Paris 546d9a6f30bSAmir Goldstein fsnotify_iter_next(&iter_info); 5477131485aSEric Paris } 548ff8bcbd0SEric Paris ret = 0; 549ff8bcbd0SEric Paris out: 5509385a84dSJan Kara srcu_read_unlock(&fsnotify_mark_srcu, iter_info.srcu_idx); 551c4ec54b4SEric Paris 55298b5c10dSJean-Christophe Dubois return ret; 55390586523SEric Paris } 55490586523SEric Paris EXPORT_SYMBOL_GPL(fsnotify); 55590586523SEric Paris 55690586523SEric Paris static __init int fsnotify_init(void) 55790586523SEric Paris { 55875c1be48SEric Paris int ret; 55975c1be48SEric Paris 56008b95c33SAmir Goldstein BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25); 56120dee624SEric Paris 56275c1be48SEric Paris ret = init_srcu_struct(&fsnotify_mark_srcu); 56375c1be48SEric Paris if (ret) 56475c1be48SEric Paris panic("initializing fsnotify_mark_srcu"); 56575c1be48SEric Paris 5669dd813c1SJan Kara fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector, 5679dd813c1SJan Kara SLAB_PANIC); 5689dd813c1SJan Kara 56975c1be48SEric Paris return 0; 57090586523SEric Paris } 57175c1be48SEric Paris core_initcall(fsnotify_init); 572