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