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