xref: /OK3568_Linux_fs/kernel/fs/notify/dnotify/dnotify.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Directory notifications for Linux.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2000,2001,2002 Stephen Rothwell
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2009 Eric Paris <Red Hat Inc>
8*4882a593Smuzhiyun  * dnotify was largly rewritten to use the new fsnotify infrastructure
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun #include <linux/fs.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/sched.h>
13*4882a593Smuzhiyun #include <linux/sched/signal.h>
14*4882a593Smuzhiyun #include <linux/dnotify.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <linux/security.h>
17*4882a593Smuzhiyun #include <linux/spinlock.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/fdtable.h>
20*4882a593Smuzhiyun #include <linux/fsnotify_backend.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun int dir_notify_enable __read_mostly = 1;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun static struct kmem_cache *dnotify_struct_cache __read_mostly;
25*4882a593Smuzhiyun static struct kmem_cache *dnotify_mark_cache __read_mostly;
26*4882a593Smuzhiyun static struct fsnotify_group *dnotify_group __read_mostly;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  * dnotify will attach one of these to each inode (i_fsnotify_marks) which
30*4882a593Smuzhiyun  * is being watched by dnotify.  If multiple userspace applications are watching
31*4882a593Smuzhiyun  * the same directory with dnotify their information is chained in dn
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun struct dnotify_mark {
34*4882a593Smuzhiyun 	struct fsnotify_mark fsn_mark;
35*4882a593Smuzhiyun 	struct dnotify_struct *dn;
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun  * When a process starts or stops watching an inode the set of events which
40*4882a593Smuzhiyun  * dnotify cares about for that inode may change.  This function runs the
41*4882a593Smuzhiyun  * list of everything receiving dnotify events about this directory and calculates
42*4882a593Smuzhiyun  * the set of all those events.  After it updates what dnotify is interested in
43*4882a593Smuzhiyun  * it calls the fsnotify function so it can update the set of all events relevant
44*4882a593Smuzhiyun  * to this inode.
45*4882a593Smuzhiyun  */
dnotify_recalc_inode_mask(struct fsnotify_mark * fsn_mark)46*4882a593Smuzhiyun static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	__u32 new_mask = 0;
49*4882a593Smuzhiyun 	struct dnotify_struct *dn;
50*4882a593Smuzhiyun 	struct dnotify_mark *dn_mark  = container_of(fsn_mark,
51*4882a593Smuzhiyun 						     struct dnotify_mark,
52*4882a593Smuzhiyun 						     fsn_mark);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	assert_spin_locked(&fsn_mark->lock);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	for (dn = dn_mark->dn; dn != NULL; dn = dn->dn_next)
57*4882a593Smuzhiyun 		new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT);
58*4882a593Smuzhiyun 	if (fsn_mark->mask == new_mask)
59*4882a593Smuzhiyun 		return;
60*4882a593Smuzhiyun 	fsn_mark->mask = new_mask;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	fsnotify_recalc_mask(fsn_mark->connector);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun  * Mains fsnotify call where events are delivered to dnotify.
67*4882a593Smuzhiyun  * Find the dnotify mark on the relevant inode, run the list of dnotify structs
68*4882a593Smuzhiyun  * on that mark and determine which of them has expressed interest in receiving
69*4882a593Smuzhiyun  * events of this type.  When found send the correct process and signal and
70*4882a593Smuzhiyun  * destroy the dnotify struct if it was not registered to receive multiple
71*4882a593Smuzhiyun  * events.
72*4882a593Smuzhiyun  */
dnotify_handle_event(struct fsnotify_mark * inode_mark,u32 mask,struct inode * inode,struct inode * dir,const struct qstr * name,u32 cookie)73*4882a593Smuzhiyun static int dnotify_handle_event(struct fsnotify_mark *inode_mark, u32 mask,
74*4882a593Smuzhiyun 				struct inode *inode, struct inode *dir,
75*4882a593Smuzhiyun 				const struct qstr *name, u32 cookie)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct dnotify_mark *dn_mark;
78*4882a593Smuzhiyun 	struct dnotify_struct *dn;
79*4882a593Smuzhiyun 	struct dnotify_struct **prev;
80*4882a593Smuzhiyun 	struct fown_struct *fown;
81*4882a593Smuzhiyun 	__u32 test_mask = mask & ~FS_EVENT_ON_CHILD;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	/* not a dir, dnotify doesn't care */
84*4882a593Smuzhiyun 	if (!dir && !(mask & FS_ISDIR))
85*4882a593Smuzhiyun 		return 0;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	dn_mark = container_of(inode_mark, struct dnotify_mark, fsn_mark);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	spin_lock(&inode_mark->lock);
90*4882a593Smuzhiyun 	prev = &dn_mark->dn;
91*4882a593Smuzhiyun 	while ((dn = *prev) != NULL) {
92*4882a593Smuzhiyun 		if ((dn->dn_mask & test_mask) == 0) {
93*4882a593Smuzhiyun 			prev = &dn->dn_next;
94*4882a593Smuzhiyun 			continue;
95*4882a593Smuzhiyun 		}
96*4882a593Smuzhiyun 		fown = &dn->dn_filp->f_owner;
97*4882a593Smuzhiyun 		send_sigio(fown, dn->dn_fd, POLL_MSG);
98*4882a593Smuzhiyun 		if (dn->dn_mask & FS_DN_MULTISHOT)
99*4882a593Smuzhiyun 			prev = &dn->dn_next;
100*4882a593Smuzhiyun 		else {
101*4882a593Smuzhiyun 			*prev = dn->dn_next;
102*4882a593Smuzhiyun 			kmem_cache_free(dnotify_struct_cache, dn);
103*4882a593Smuzhiyun 			dnotify_recalc_inode_mask(inode_mark);
104*4882a593Smuzhiyun 		}
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	spin_unlock(&inode_mark->lock);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	return 0;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
dnotify_free_mark(struct fsnotify_mark * fsn_mark)112*4882a593Smuzhiyun static void dnotify_free_mark(struct fsnotify_mark *fsn_mark)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	struct dnotify_mark *dn_mark = container_of(fsn_mark,
115*4882a593Smuzhiyun 						    struct dnotify_mark,
116*4882a593Smuzhiyun 						    fsn_mark);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	BUG_ON(dn_mark->dn);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	kmem_cache_free(dnotify_mark_cache, dn_mark);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun static const struct fsnotify_ops dnotify_fsnotify_ops = {
124*4882a593Smuzhiyun 	.handle_inode_event = dnotify_handle_event,
125*4882a593Smuzhiyun 	.free_mark = dnotify_free_mark,
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun  * Called every time a file is closed.  Looks first for a dnotify mark on the
130*4882a593Smuzhiyun  * inode.  If one is found run all of the ->dn structures attached to that
131*4882a593Smuzhiyun  * mark for one relevant to this process closing the file and remove that
132*4882a593Smuzhiyun  * dnotify_struct.  If that was the last dnotify_struct also remove the
133*4882a593Smuzhiyun  * fsnotify_mark.
134*4882a593Smuzhiyun  */
dnotify_flush(struct file * filp,fl_owner_t id)135*4882a593Smuzhiyun void dnotify_flush(struct file *filp, fl_owner_t id)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	struct fsnotify_mark *fsn_mark;
138*4882a593Smuzhiyun 	struct dnotify_mark *dn_mark;
139*4882a593Smuzhiyun 	struct dnotify_struct *dn;
140*4882a593Smuzhiyun 	struct dnotify_struct **prev;
141*4882a593Smuzhiyun 	struct inode *inode;
142*4882a593Smuzhiyun 	bool free = false;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	inode = file_inode(filp);
145*4882a593Smuzhiyun 	if (!S_ISDIR(inode->i_mode))
146*4882a593Smuzhiyun 		return;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, dnotify_group);
149*4882a593Smuzhiyun 	if (!fsn_mark)
150*4882a593Smuzhiyun 		return;
151*4882a593Smuzhiyun 	dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	mutex_lock(&dnotify_group->mark_mutex);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	spin_lock(&fsn_mark->lock);
156*4882a593Smuzhiyun 	prev = &dn_mark->dn;
157*4882a593Smuzhiyun 	while ((dn = *prev) != NULL) {
158*4882a593Smuzhiyun 		if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
159*4882a593Smuzhiyun 			*prev = dn->dn_next;
160*4882a593Smuzhiyun 			kmem_cache_free(dnotify_struct_cache, dn);
161*4882a593Smuzhiyun 			dnotify_recalc_inode_mask(fsn_mark);
162*4882a593Smuzhiyun 			break;
163*4882a593Smuzhiyun 		}
164*4882a593Smuzhiyun 		prev = &dn->dn_next;
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	spin_unlock(&fsn_mark->lock);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/* nothing else could have found us thanks to the dnotify_groups
170*4882a593Smuzhiyun 	   mark_mutex */
171*4882a593Smuzhiyun 	if (dn_mark->dn == NULL) {
172*4882a593Smuzhiyun 		fsnotify_detach_mark(fsn_mark);
173*4882a593Smuzhiyun 		free = true;
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	mutex_unlock(&dnotify_group->mark_mutex);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	if (free)
179*4882a593Smuzhiyun 		fsnotify_free_mark(fsn_mark);
180*4882a593Smuzhiyun 	fsnotify_put_mark(fsn_mark);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /* this conversion is done only at watch creation */
convert_arg(unsigned long arg)184*4882a593Smuzhiyun static __u32 convert_arg(unsigned long arg)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	__u32 new_mask = FS_EVENT_ON_CHILD;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (arg & DN_MULTISHOT)
189*4882a593Smuzhiyun 		new_mask |= FS_DN_MULTISHOT;
190*4882a593Smuzhiyun 	if (arg & DN_DELETE)
191*4882a593Smuzhiyun 		new_mask |= (FS_DELETE | FS_MOVED_FROM);
192*4882a593Smuzhiyun 	if (arg & DN_MODIFY)
193*4882a593Smuzhiyun 		new_mask |= FS_MODIFY;
194*4882a593Smuzhiyun 	if (arg & DN_ACCESS)
195*4882a593Smuzhiyun 		new_mask |= FS_ACCESS;
196*4882a593Smuzhiyun 	if (arg & DN_ATTRIB)
197*4882a593Smuzhiyun 		new_mask |= FS_ATTRIB;
198*4882a593Smuzhiyun 	if (arg & DN_RENAME)
199*4882a593Smuzhiyun 		new_mask |= FS_DN_RENAME;
200*4882a593Smuzhiyun 	if (arg & DN_CREATE)
201*4882a593Smuzhiyun 		new_mask |= (FS_CREATE | FS_MOVED_TO);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	return new_mask;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun /*
207*4882a593Smuzhiyun  * If multiple processes watch the same inode with dnotify there is only one
208*4882a593Smuzhiyun  * dnotify mark in inode->i_fsnotify_marks but we chain a dnotify_struct
209*4882a593Smuzhiyun  * onto that mark.  This function either attaches the new dnotify_struct onto
210*4882a593Smuzhiyun  * that list, or it |= the mask onto an existing dnofiy_struct.
211*4882a593Smuzhiyun  */
attach_dn(struct dnotify_struct * dn,struct dnotify_mark * dn_mark,fl_owner_t id,int fd,struct file * filp,__u32 mask)212*4882a593Smuzhiyun static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark *dn_mark,
213*4882a593Smuzhiyun 		     fl_owner_t id, int fd, struct file *filp, __u32 mask)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun 	struct dnotify_struct *odn;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	odn = dn_mark->dn;
218*4882a593Smuzhiyun 	while (odn != NULL) {
219*4882a593Smuzhiyun 		/* adding more events to existing dnofiy_struct? */
220*4882a593Smuzhiyun 		if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
221*4882a593Smuzhiyun 			odn->dn_fd = fd;
222*4882a593Smuzhiyun 			odn->dn_mask |= mask;
223*4882a593Smuzhiyun 			return -EEXIST;
224*4882a593Smuzhiyun 		}
225*4882a593Smuzhiyun 		odn = odn->dn_next;
226*4882a593Smuzhiyun 	}
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	dn->dn_mask = mask;
229*4882a593Smuzhiyun 	dn->dn_fd = fd;
230*4882a593Smuzhiyun 	dn->dn_filp = filp;
231*4882a593Smuzhiyun 	dn->dn_owner = id;
232*4882a593Smuzhiyun 	dn->dn_next = dn_mark->dn;
233*4882a593Smuzhiyun 	dn_mark->dn = dn;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	return 0;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun /*
239*4882a593Smuzhiyun  * When a process calls fcntl to attach a dnotify watch to a directory it ends
240*4882a593Smuzhiyun  * up here.  Allocate both a mark for fsnotify to add and a dnotify_struct to be
241*4882a593Smuzhiyun  * attached to the fsnotify_mark.
242*4882a593Smuzhiyun  */
fcntl_dirnotify(int fd,struct file * filp,unsigned long arg)243*4882a593Smuzhiyun int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	struct dnotify_mark *new_dn_mark, *dn_mark;
246*4882a593Smuzhiyun 	struct fsnotify_mark *new_fsn_mark, *fsn_mark;
247*4882a593Smuzhiyun 	struct dnotify_struct *dn;
248*4882a593Smuzhiyun 	struct inode *inode;
249*4882a593Smuzhiyun 	fl_owner_t id = current->files;
250*4882a593Smuzhiyun 	struct file *f;
251*4882a593Smuzhiyun 	int destroy = 0, error = 0;
252*4882a593Smuzhiyun 	__u32 mask;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	/* we use these to tell if we need to kfree */
255*4882a593Smuzhiyun 	new_fsn_mark = NULL;
256*4882a593Smuzhiyun 	dn = NULL;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	if (!dir_notify_enable) {
259*4882a593Smuzhiyun 		error = -EINVAL;
260*4882a593Smuzhiyun 		goto out_err;
261*4882a593Smuzhiyun 	}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	/* a 0 mask means we are explicitly removing the watch */
264*4882a593Smuzhiyun 	if ((arg & ~DN_MULTISHOT) == 0) {
265*4882a593Smuzhiyun 		dnotify_flush(filp, id);
266*4882a593Smuzhiyun 		error = 0;
267*4882a593Smuzhiyun 		goto out_err;
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/* dnotify only works on directories */
271*4882a593Smuzhiyun 	inode = file_inode(filp);
272*4882a593Smuzhiyun 	if (!S_ISDIR(inode->i_mode)) {
273*4882a593Smuzhiyun 		error = -ENOTDIR;
274*4882a593Smuzhiyun 		goto out_err;
275*4882a593Smuzhiyun 	}
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	/*
278*4882a593Smuzhiyun 	 * convert the userspace DN_* "arg" to the internal FS_*
279*4882a593Smuzhiyun 	 * defined in fsnotify
280*4882a593Smuzhiyun 	 */
281*4882a593Smuzhiyun 	mask = convert_arg(arg);
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	error = security_path_notify(&filp->f_path, mask,
284*4882a593Smuzhiyun 			FSNOTIFY_OBJ_TYPE_INODE);
285*4882a593Smuzhiyun 	if (error)
286*4882a593Smuzhiyun 		goto out_err;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	/* expect most fcntl to add new rather than augment old */
289*4882a593Smuzhiyun 	dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL);
290*4882a593Smuzhiyun 	if (!dn) {
291*4882a593Smuzhiyun 		error = -ENOMEM;
292*4882a593Smuzhiyun 		goto out_err;
293*4882a593Smuzhiyun 	}
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/* new fsnotify mark, we expect most fcntl calls to add a new mark */
296*4882a593Smuzhiyun 	new_dn_mark = kmem_cache_alloc(dnotify_mark_cache, GFP_KERNEL);
297*4882a593Smuzhiyun 	if (!new_dn_mark) {
298*4882a593Smuzhiyun 		error = -ENOMEM;
299*4882a593Smuzhiyun 		goto out_err;
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	/* set up the new_fsn_mark and new_dn_mark */
303*4882a593Smuzhiyun 	new_fsn_mark = &new_dn_mark->fsn_mark;
304*4882a593Smuzhiyun 	fsnotify_init_mark(new_fsn_mark, dnotify_group);
305*4882a593Smuzhiyun 	new_fsn_mark->mask = mask;
306*4882a593Smuzhiyun 	new_dn_mark->dn = NULL;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	/* this is needed to prevent the fcntl/close race described below */
309*4882a593Smuzhiyun 	mutex_lock(&dnotify_group->mark_mutex);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	/* add the new_fsn_mark or find an old one. */
312*4882a593Smuzhiyun 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, dnotify_group);
313*4882a593Smuzhiyun 	if (fsn_mark) {
314*4882a593Smuzhiyun 		dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
315*4882a593Smuzhiyun 		spin_lock(&fsn_mark->lock);
316*4882a593Smuzhiyun 	} else {
317*4882a593Smuzhiyun 		error = fsnotify_add_inode_mark_locked(new_fsn_mark, inode, 0);
318*4882a593Smuzhiyun 		if (error) {
319*4882a593Smuzhiyun 			mutex_unlock(&dnotify_group->mark_mutex);
320*4882a593Smuzhiyun 			goto out_err;
321*4882a593Smuzhiyun 		}
322*4882a593Smuzhiyun 		spin_lock(&new_fsn_mark->lock);
323*4882a593Smuzhiyun 		fsn_mark = new_fsn_mark;
324*4882a593Smuzhiyun 		dn_mark = new_dn_mark;
325*4882a593Smuzhiyun 		/* we used new_fsn_mark, so don't free it */
326*4882a593Smuzhiyun 		new_fsn_mark = NULL;
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	rcu_read_lock();
330*4882a593Smuzhiyun 	f = fcheck(fd);
331*4882a593Smuzhiyun 	rcu_read_unlock();
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	/* if (f != filp) means that we lost a race and another task/thread
334*4882a593Smuzhiyun 	 * actually closed the fd we are still playing with before we grabbed
335*4882a593Smuzhiyun 	 * the dnotify_groups mark_mutex and fsn_mark->lock.  Since closing the
336*4882a593Smuzhiyun 	 * fd is the only time we clean up the marks we need to get our mark
337*4882a593Smuzhiyun 	 * off the list. */
338*4882a593Smuzhiyun 	if (f != filp) {
339*4882a593Smuzhiyun 		/* if we added ourselves, shoot ourselves, it's possible that
340*4882a593Smuzhiyun 		 * the flush actually did shoot this fsn_mark.  That's fine too
341*4882a593Smuzhiyun 		 * since multiple calls to destroy_mark is perfectly safe, if
342*4882a593Smuzhiyun 		 * we found a dn_mark already attached to the inode, just sod
343*4882a593Smuzhiyun 		 * off silently as the flush at close time dealt with it.
344*4882a593Smuzhiyun 		 */
345*4882a593Smuzhiyun 		if (dn_mark == new_dn_mark)
346*4882a593Smuzhiyun 			destroy = 1;
347*4882a593Smuzhiyun 		error = 0;
348*4882a593Smuzhiyun 		goto out;
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	__f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	error = attach_dn(dn, dn_mark, id, fd, filp, mask);
354*4882a593Smuzhiyun 	/* !error means that we attached the dn to the dn_mark, so don't free it */
355*4882a593Smuzhiyun 	if (!error)
356*4882a593Smuzhiyun 		dn = NULL;
357*4882a593Smuzhiyun 	/* -EEXIST means that we didn't add this new dn and used an old one.
358*4882a593Smuzhiyun 	 * that isn't an error (and the unused dn should be freed) */
359*4882a593Smuzhiyun 	else if (error == -EEXIST)
360*4882a593Smuzhiyun 		error = 0;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	dnotify_recalc_inode_mask(fsn_mark);
363*4882a593Smuzhiyun out:
364*4882a593Smuzhiyun 	spin_unlock(&fsn_mark->lock);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	if (destroy)
367*4882a593Smuzhiyun 		fsnotify_detach_mark(fsn_mark);
368*4882a593Smuzhiyun 	mutex_unlock(&dnotify_group->mark_mutex);
369*4882a593Smuzhiyun 	if (destroy)
370*4882a593Smuzhiyun 		fsnotify_free_mark(fsn_mark);
371*4882a593Smuzhiyun 	fsnotify_put_mark(fsn_mark);
372*4882a593Smuzhiyun out_err:
373*4882a593Smuzhiyun 	if (new_fsn_mark)
374*4882a593Smuzhiyun 		fsnotify_put_mark(new_fsn_mark);
375*4882a593Smuzhiyun 	if (dn)
376*4882a593Smuzhiyun 		kmem_cache_free(dnotify_struct_cache, dn);
377*4882a593Smuzhiyun 	return error;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
dnotify_init(void)380*4882a593Smuzhiyun static int __init dnotify_init(void)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	dnotify_struct_cache = KMEM_CACHE(dnotify_struct,
383*4882a593Smuzhiyun 					  SLAB_PANIC|SLAB_ACCOUNT);
384*4882a593Smuzhiyun 	dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC|SLAB_ACCOUNT);
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops);
387*4882a593Smuzhiyun 	if (IS_ERR(dnotify_group))
388*4882a593Smuzhiyun 		panic("unable to allocate fsnotify group for dnotify\n");
389*4882a593Smuzhiyun 	return 0;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun module_init(dnotify_init)
393