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