xref: /linux/fs/notify/inotify/inotify_user.c (revision 2903ff019b346ab8d36ebbf54853c3aaf6590608)
1272eb014SEric Paris /*
2272eb014SEric Paris  * fs/inotify_user.c - inotify support for userspace
3272eb014SEric Paris  *
4272eb014SEric Paris  * Authors:
5272eb014SEric Paris  *	John McCutchan	<ttb@tentacle.dhs.org>
6272eb014SEric Paris  *	Robert Love	<rml@novell.com>
7272eb014SEric Paris  *
8272eb014SEric Paris  * Copyright (C) 2005 John McCutchan
9272eb014SEric Paris  * Copyright 2006 Hewlett-Packard Development Company, L.P.
10272eb014SEric Paris  *
1163c882a0SEric Paris  * Copyright (C) 2009 Eric Paris <Red Hat Inc>
1263c882a0SEric Paris  * inotify was largely rewriten to make use of the fsnotify infrastructure
1363c882a0SEric Paris  *
14272eb014SEric Paris  * This program is free software; you can redistribute it and/or modify it
15272eb014SEric Paris  * under the terms of the GNU General Public License as published by the
16272eb014SEric Paris  * Free Software Foundation; either version 2, or (at your option) any
17272eb014SEric Paris  * later version.
18272eb014SEric Paris  *
19272eb014SEric Paris  * This program is distributed in the hope that it will be useful, but
20272eb014SEric Paris  * WITHOUT ANY WARRANTY; without even the implied warranty of
21272eb014SEric Paris  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22272eb014SEric Paris  * General Public License for more details.
23272eb014SEric Paris  */
24272eb014SEric Paris 
25272eb014SEric Paris #include <linux/file.h>
2663c882a0SEric Paris #include <linux/fs.h> /* struct inode */
2763c882a0SEric Paris #include <linux/fsnotify_backend.h>
2863c882a0SEric Paris #include <linux/idr.h>
2963c882a0SEric Paris #include <linux/init.h> /* module_init */
30272eb014SEric Paris #include <linux/inotify.h>
3163c882a0SEric Paris #include <linux/kernel.h> /* roundup() */
3263c882a0SEric Paris #include <linux/namei.h> /* LOOKUP_FOLLOW */
3363c882a0SEric Paris #include <linux/sched.h> /* struct user */
3463c882a0SEric Paris #include <linux/slab.h> /* struct kmem_cache */
35272eb014SEric Paris #include <linux/syscalls.h>
3663c882a0SEric Paris #include <linux/types.h>
37c44dcc56SAl Viro #include <linux/anon_inodes.h>
3863c882a0SEric Paris #include <linux/uaccess.h>
3963c882a0SEric Paris #include <linux/poll.h>
4063c882a0SEric Paris #include <linux/wait.h>
4163c882a0SEric Paris 
4263c882a0SEric Paris #include "inotify.h"
43272eb014SEric Paris 
44272eb014SEric Paris #include <asm/ioctls.h>
45272eb014SEric Paris 
46272eb014SEric Paris /* these are configurable via /proc/sys/fs/inotify/ */
47272eb014SEric Paris static int inotify_max_user_instances __read_mostly;
48272eb014SEric Paris static int inotify_max_queued_events __read_mostly;
490a24887aSH Hartley Sweeten static int inotify_max_user_watches __read_mostly;
5063c882a0SEric Paris 
5163c882a0SEric Paris static struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
5263c882a0SEric Paris struct kmem_cache *event_priv_cachep __read_mostly;
53272eb014SEric Paris 
54272eb014SEric Paris #ifdef CONFIG_SYSCTL
55272eb014SEric Paris 
56272eb014SEric Paris #include <linux/sysctl.h>
57272eb014SEric Paris 
58272eb014SEric Paris static int zero;
59272eb014SEric Paris 
60272eb014SEric Paris ctl_table inotify_table[] = {
61272eb014SEric Paris 	{
62272eb014SEric Paris 		.procname	= "max_user_instances",
63272eb014SEric Paris 		.data		= &inotify_max_user_instances,
64272eb014SEric Paris 		.maxlen		= sizeof(int),
65272eb014SEric Paris 		.mode		= 0644,
666d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
67272eb014SEric Paris 		.extra1		= &zero,
68272eb014SEric Paris 	},
69272eb014SEric Paris 	{
70272eb014SEric Paris 		.procname	= "max_user_watches",
71272eb014SEric Paris 		.data		= &inotify_max_user_watches,
72272eb014SEric Paris 		.maxlen		= sizeof(int),
73272eb014SEric Paris 		.mode		= 0644,
746d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
75272eb014SEric Paris 		.extra1		= &zero,
76272eb014SEric Paris 	},
77272eb014SEric Paris 	{
78272eb014SEric Paris 		.procname	= "max_queued_events",
79272eb014SEric Paris 		.data		= &inotify_max_queued_events,
80272eb014SEric Paris 		.maxlen		= sizeof(int),
81272eb014SEric Paris 		.mode		= 0644,
826d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
83272eb014SEric Paris 		.extra1		= &zero
84272eb014SEric Paris 	},
85ab09203eSEric W. Biederman 	{ }
86272eb014SEric Paris };
87272eb014SEric Paris #endif /* CONFIG_SYSCTL */
88272eb014SEric Paris 
8963c882a0SEric Paris static inline __u32 inotify_arg_to_mask(u32 arg)
90272eb014SEric Paris {
9163c882a0SEric Paris 	__u32 mask;
9263c882a0SEric Paris 
93611da04fSEric Paris 	/*
94611da04fSEric Paris 	 * everything should accept their own ignored, cares about children,
95611da04fSEric Paris 	 * and should receive events when the inode is unmounted
96611da04fSEric Paris 	 */
97611da04fSEric Paris 	mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
9863c882a0SEric Paris 
9963c882a0SEric Paris 	/* mask off the flags used to open the fd */
1008c1934c8SEric Paris 	mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
10163c882a0SEric Paris 
10263c882a0SEric Paris 	return mask;
103272eb014SEric Paris }
104272eb014SEric Paris 
10563c882a0SEric Paris static inline u32 inotify_mask_to_arg(__u32 mask)
106272eb014SEric Paris {
10763c882a0SEric Paris 	return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
10863c882a0SEric Paris 		       IN_Q_OVERFLOW);
109272eb014SEric Paris }
110272eb014SEric Paris 
11163c882a0SEric Paris /* intofiy userspace file descriptor functions */
112272eb014SEric Paris static unsigned int inotify_poll(struct file *file, poll_table *wait)
113272eb014SEric Paris {
11463c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
115272eb014SEric Paris 	int ret = 0;
116272eb014SEric Paris 
11763c882a0SEric Paris 	poll_wait(file, &group->notification_waitq, wait);
11863c882a0SEric Paris 	mutex_lock(&group->notification_mutex);
11963c882a0SEric Paris 	if (!fsnotify_notify_queue_is_empty(group))
120272eb014SEric Paris 		ret = POLLIN | POLLRDNORM;
12163c882a0SEric Paris 	mutex_unlock(&group->notification_mutex);
122272eb014SEric Paris 
123272eb014SEric Paris 	return ret;
124272eb014SEric Paris }
125272eb014SEric Paris 
1263632dee2SVegard Nossum /*
1273632dee2SVegard Nossum  * Get an inotify_kernel_event if one exists and is small
1283632dee2SVegard Nossum  * enough to fit in "count". Return an error pointer if
1293632dee2SVegard Nossum  * not large enough.
1303632dee2SVegard Nossum  *
13163c882a0SEric Paris  * Called with the group->notification_mutex held.
1323632dee2SVegard Nossum  */
13363c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
1343632dee2SVegard Nossum 					    size_t count)
1353632dee2SVegard Nossum {
1363632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
13763c882a0SEric Paris 	struct fsnotify_event *event;
1383632dee2SVegard Nossum 
13963c882a0SEric Paris 	if (fsnotify_notify_queue_is_empty(group))
1403632dee2SVegard Nossum 		return NULL;
1413632dee2SVegard Nossum 
14263c882a0SEric Paris 	event = fsnotify_peek_notify_event(group);
14363c882a0SEric Paris 
1445ba08e2eSEric Paris 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
1455ba08e2eSEric Paris 
14683cb10f0SEric Paris 	if (event->name_len)
14783cb10f0SEric Paris 		event_size += roundup(event->name_len + 1, event_size);
1483632dee2SVegard Nossum 
1493632dee2SVegard Nossum 	if (event_size > count)
1503632dee2SVegard Nossum 		return ERR_PTR(-EINVAL);
1513632dee2SVegard Nossum 
15263c882a0SEric Paris 	/* held the notification_mutex the whole time, so this is the
15363c882a0SEric Paris 	 * same event we peeked above */
15463c882a0SEric Paris 	fsnotify_remove_notify_event(group);
15563c882a0SEric Paris 
15663c882a0SEric Paris 	return event;
1573632dee2SVegard Nossum }
1583632dee2SVegard Nossum 
1593632dee2SVegard Nossum /*
1603632dee2SVegard Nossum  * Copy an event to user space, returning how much we copied.
1613632dee2SVegard Nossum  *
1623632dee2SVegard Nossum  * We already checked that the event size is smaller than the
1633632dee2SVegard Nossum  * buffer we had in "get_one_event()" above.
1643632dee2SVegard Nossum  */
16563c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group,
16663c882a0SEric Paris 				  struct fsnotify_event *event,
1673632dee2SVegard Nossum 				  char __user *buf)
1683632dee2SVegard Nossum {
16963c882a0SEric Paris 	struct inotify_event inotify_event;
17063c882a0SEric Paris 	struct fsnotify_event_private_data *fsn_priv;
17163c882a0SEric Paris 	struct inotify_event_private_data *priv;
1723632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
173b962e731SBrian Rogers 	size_t name_len = 0;
1743632dee2SVegard Nossum 
1755ba08e2eSEric Paris 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
1765ba08e2eSEric Paris 
17763c882a0SEric Paris 	/* we get the inotify watch descriptor from the event private data */
17863c882a0SEric Paris 	spin_lock(&event->lock);
17963c882a0SEric Paris 	fsn_priv = fsnotify_remove_priv_from_event(group, event);
18063c882a0SEric Paris 	spin_unlock(&event->lock);
18163c882a0SEric Paris 
18263c882a0SEric Paris 	if (!fsn_priv)
18363c882a0SEric Paris 		inotify_event.wd = -1;
18463c882a0SEric Paris 	else {
18563c882a0SEric Paris 		priv = container_of(fsn_priv, struct inotify_event_private_data,
18663c882a0SEric Paris 				    fsnotify_event_priv_data);
18763c882a0SEric Paris 		inotify_event.wd = priv->wd;
18863c882a0SEric Paris 		inotify_free_event_priv(fsn_priv);
18963c882a0SEric Paris 	}
19063c882a0SEric Paris 
191b962e731SBrian Rogers 	/*
192b962e731SBrian Rogers 	 * round up event->name_len so it is a multiple of event_size
1930db501bdSEric W. Biederman 	 * plus an extra byte for the terminating '\0'.
1940db501bdSEric W. Biederman 	 */
195b962e731SBrian Rogers 	if (event->name_len)
1960db501bdSEric W. Biederman 		name_len = roundup(event->name_len + 1, event_size);
19763c882a0SEric Paris 	inotify_event.len = name_len;
19863c882a0SEric Paris 
19963c882a0SEric Paris 	inotify_event.mask = inotify_mask_to_arg(event->mask);
20063c882a0SEric Paris 	inotify_event.cookie = event->sync_cookie;
20163c882a0SEric Paris 
20263c882a0SEric Paris 	/* send the main event */
20363c882a0SEric Paris 	if (copy_to_user(buf, &inotify_event, event_size))
2043632dee2SVegard Nossum 		return -EFAULT;
2053632dee2SVegard Nossum 
2063632dee2SVegard Nossum 	buf += event_size;
2073632dee2SVegard Nossum 
20863c882a0SEric Paris 	/*
20963c882a0SEric Paris 	 * fsnotify only stores the pathname, so here we have to send the pathname
21063c882a0SEric Paris 	 * and then pad that pathname out to a multiple of sizeof(inotify_event)
21163c882a0SEric Paris 	 * with zeros.  I get my zeros from the nul_inotify_event.
21263c882a0SEric Paris 	 */
21363c882a0SEric Paris 	if (name_len) {
21463c882a0SEric Paris 		unsigned int len_to_zero = name_len - event->name_len;
21563c882a0SEric Paris 		/* copy the path name */
21663c882a0SEric Paris 		if (copy_to_user(buf, event->file_name, event->name_len))
2173632dee2SVegard Nossum 			return -EFAULT;
21863c882a0SEric Paris 		buf += event->name_len;
2193632dee2SVegard Nossum 
2200db501bdSEric W. Biederman 		/* fill userspace with 0's */
2210db501bdSEric W. Biederman 		if (clear_user(buf, len_to_zero))
22263c882a0SEric Paris 			return -EFAULT;
22363c882a0SEric Paris 		buf += len_to_zero;
22463c882a0SEric Paris 		event_size += name_len;
2253632dee2SVegard Nossum 	}
22663c882a0SEric Paris 
2273632dee2SVegard Nossum 	return event_size;
2283632dee2SVegard Nossum }
2293632dee2SVegard Nossum 
230272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf,
231272eb014SEric Paris 			    size_t count, loff_t *pos)
232272eb014SEric Paris {
23363c882a0SEric Paris 	struct fsnotify_group *group;
23463c882a0SEric Paris 	struct fsnotify_event *kevent;
235272eb014SEric Paris 	char __user *start;
236272eb014SEric Paris 	int ret;
237272eb014SEric Paris 	DEFINE_WAIT(wait);
238272eb014SEric Paris 
239272eb014SEric Paris 	start = buf;
24063c882a0SEric Paris 	group = file->private_data;
241272eb014SEric Paris 
242272eb014SEric Paris 	while (1) {
24363c882a0SEric Paris 		prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
244272eb014SEric Paris 
24563c882a0SEric Paris 		mutex_lock(&group->notification_mutex);
24663c882a0SEric Paris 		kevent = get_one_event(group, count);
24763c882a0SEric Paris 		mutex_unlock(&group->notification_mutex);
248272eb014SEric Paris 
2495ba08e2eSEric Paris 		pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
2505ba08e2eSEric Paris 
2513632dee2SVegard Nossum 		if (kevent) {
2523632dee2SVegard Nossum 			ret = PTR_ERR(kevent);
2533632dee2SVegard Nossum 			if (IS_ERR(kevent))
254272eb014SEric Paris 				break;
25563c882a0SEric Paris 			ret = copy_event_to_user(group, kevent, buf);
25663c882a0SEric Paris 			fsnotify_put_event(kevent);
2573632dee2SVegard Nossum 			if (ret < 0)
2583632dee2SVegard Nossum 				break;
2593632dee2SVegard Nossum 			buf += ret;
2603632dee2SVegard Nossum 			count -= ret;
2613632dee2SVegard Nossum 			continue;
262272eb014SEric Paris 		}
263272eb014SEric Paris 
2643632dee2SVegard Nossum 		ret = -EAGAIN;
2653632dee2SVegard Nossum 		if (file->f_flags & O_NONBLOCK)
266272eb014SEric Paris 			break;
2673632dee2SVegard Nossum 		ret = -EINTR;
2683632dee2SVegard Nossum 		if (signal_pending(current))
2693632dee2SVegard Nossum 			break;
2703632dee2SVegard Nossum 
2713632dee2SVegard Nossum 		if (start != buf)
2723632dee2SVegard Nossum 			break;
273272eb014SEric Paris 
274272eb014SEric Paris 		schedule();
275272eb014SEric Paris 	}
276272eb014SEric Paris 
27763c882a0SEric Paris 	finish_wait(&group->notification_waitq, &wait);
2783632dee2SVegard Nossum 	if (start != buf && ret != -EFAULT)
279272eb014SEric Paris 		ret = buf - start;
280272eb014SEric Paris 	return ret;
281272eb014SEric Paris }
282272eb014SEric Paris 
283272eb014SEric Paris static int inotify_fasync(int fd, struct file *file, int on)
284272eb014SEric Paris {
28563c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
286272eb014SEric Paris 
28763c882a0SEric Paris 	return fasync_helper(fd, file, on, &group->inotify_data.fa) >= 0 ? 0 : -EIO;
288272eb014SEric Paris }
289272eb014SEric Paris 
290272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file)
291272eb014SEric Paris {
29263c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
293272eb014SEric Paris 
2945ba08e2eSEric Paris 	pr_debug("%s: group=%p\n", __func__, group);
2955ba08e2eSEric Paris 
29663c882a0SEric Paris 	fsnotify_clear_marks_by_group(group);
297272eb014SEric Paris 
29863c882a0SEric Paris 	/* free this group, matching get was inotify_init->fsnotify_obtain_group */
29963c882a0SEric Paris 	fsnotify_put_group(group);
300272eb014SEric Paris 
301272eb014SEric Paris 	return 0;
302272eb014SEric Paris }
303272eb014SEric Paris 
304272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd,
305272eb014SEric Paris 			  unsigned long arg)
306272eb014SEric Paris {
30763c882a0SEric Paris 	struct fsnotify_group *group;
30863c882a0SEric Paris 	struct fsnotify_event_holder *holder;
30963c882a0SEric Paris 	struct fsnotify_event *event;
310272eb014SEric Paris 	void __user *p;
311272eb014SEric Paris 	int ret = -ENOTTY;
31263c882a0SEric Paris 	size_t send_len = 0;
313272eb014SEric Paris 
31463c882a0SEric Paris 	group = file->private_data;
315272eb014SEric Paris 	p = (void __user *) arg;
316272eb014SEric Paris 
3175ba08e2eSEric Paris 	pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
3185ba08e2eSEric Paris 
319272eb014SEric Paris 	switch (cmd) {
320272eb014SEric Paris 	case FIONREAD:
32163c882a0SEric Paris 		mutex_lock(&group->notification_mutex);
32263c882a0SEric Paris 		list_for_each_entry(holder, &group->notification_list, event_list) {
32363c882a0SEric Paris 			event = holder->event;
32463c882a0SEric Paris 			send_len += sizeof(struct inotify_event);
32583cb10f0SEric Paris 			if (event->name_len)
32683cb10f0SEric Paris 				send_len += roundup(event->name_len + 1,
32763c882a0SEric Paris 						sizeof(struct inotify_event));
32863c882a0SEric Paris 		}
32963c882a0SEric Paris 		mutex_unlock(&group->notification_mutex);
33063c882a0SEric Paris 		ret = put_user(send_len, (int __user *) p);
331272eb014SEric Paris 		break;
332272eb014SEric Paris 	}
333272eb014SEric Paris 
334272eb014SEric Paris 	return ret;
335272eb014SEric Paris }
336272eb014SEric Paris 
337272eb014SEric Paris static const struct file_operations inotify_fops = {
338272eb014SEric Paris 	.poll		= inotify_poll,
339272eb014SEric Paris 	.read		= inotify_read,
340272eb014SEric Paris 	.fasync		= inotify_fasync,
341272eb014SEric Paris 	.release	= inotify_release,
342272eb014SEric Paris 	.unlocked_ioctl	= inotify_ioctl,
343272eb014SEric Paris 	.compat_ioctl	= inotify_ioctl,
3446038f373SArnd Bergmann 	.llseek		= noop_llseek,
345272eb014SEric Paris };
346272eb014SEric Paris 
347272eb014SEric Paris 
34863c882a0SEric Paris /*
34963c882a0SEric Paris  * find_inode - resolve a user-given path to a specific inode
35063c882a0SEric Paris  */
35163c882a0SEric Paris static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
35263c882a0SEric Paris {
35363c882a0SEric Paris 	int error;
35463c882a0SEric Paris 
35563c882a0SEric Paris 	error = user_path_at(AT_FDCWD, dirname, flags, path);
35663c882a0SEric Paris 	if (error)
35763c882a0SEric Paris 		return error;
35863c882a0SEric Paris 	/* you can only watch an inode if you have read permissions on it */
35963c882a0SEric Paris 	error = inode_permission(path->dentry->d_inode, MAY_READ);
36063c882a0SEric Paris 	if (error)
36163c882a0SEric Paris 		path_put(path);
36263c882a0SEric Paris 	return error;
36363c882a0SEric Paris }
36463c882a0SEric Paris 
365b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
3667050c488SEric Paris 			      int *last_wd,
367000285deSEric Paris 			      struct inotify_inode_mark *i_mark)
368b7ba8371SEric Paris {
369b7ba8371SEric Paris 	int ret;
370b7ba8371SEric Paris 
371b7ba8371SEric Paris 	do {
372b7ba8371SEric Paris 		if (unlikely(!idr_pre_get(idr, GFP_KERNEL)))
373b7ba8371SEric Paris 			return -ENOMEM;
374b7ba8371SEric Paris 
375b7ba8371SEric Paris 		spin_lock(idr_lock);
376000285deSEric Paris 		ret = idr_get_new_above(idr, i_mark, *last_wd + 1,
377000285deSEric Paris 					&i_mark->wd);
378b7ba8371SEric Paris 		/* we added the mark to the idr, take a reference */
3797050c488SEric Paris 		if (!ret) {
380000285deSEric Paris 			*last_wd = i_mark->wd;
381000285deSEric Paris 			fsnotify_get_mark(&i_mark->fsn_mark);
3827050c488SEric Paris 		}
383b7ba8371SEric Paris 		spin_unlock(idr_lock);
384b7ba8371SEric Paris 	} while (ret == -EAGAIN);
385b7ba8371SEric Paris 
386b7ba8371SEric Paris 	return ret;
387b7ba8371SEric Paris }
388b7ba8371SEric Paris 
389000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
390b7ba8371SEric Paris 								int wd)
391b7ba8371SEric Paris {
392b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
393b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
394000285deSEric Paris 	struct inotify_inode_mark *i_mark;
395b7ba8371SEric Paris 
396b7ba8371SEric Paris 	assert_spin_locked(idr_lock);
397b7ba8371SEric Paris 
398000285deSEric Paris 	i_mark = idr_find(idr, wd);
399000285deSEric Paris 	if (i_mark) {
400000285deSEric Paris 		struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
401b7ba8371SEric Paris 
402000285deSEric Paris 		fsnotify_get_mark(fsn_mark);
403b7ba8371SEric Paris 		/* One ref for being in the idr, one ref we just took */
404000285deSEric Paris 		BUG_ON(atomic_read(&fsn_mark->refcnt) < 2);
405b7ba8371SEric Paris 	}
406b7ba8371SEric Paris 
407000285deSEric Paris 	return i_mark;
408b7ba8371SEric Paris }
409b7ba8371SEric Paris 
410000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
411b7ba8371SEric Paris 							 int wd)
412b7ba8371SEric Paris {
413000285deSEric Paris 	struct inotify_inode_mark *i_mark;
414b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
415b7ba8371SEric Paris 
416b7ba8371SEric Paris 	spin_lock(idr_lock);
417000285deSEric Paris 	i_mark = inotify_idr_find_locked(group, wd);
418b7ba8371SEric Paris 	spin_unlock(idr_lock);
419b7ba8371SEric Paris 
420000285deSEric Paris 	return i_mark;
421b7ba8371SEric Paris }
422b7ba8371SEric Paris 
423b7ba8371SEric Paris static void do_inotify_remove_from_idr(struct fsnotify_group *group,
424000285deSEric Paris 				       struct inotify_inode_mark *i_mark)
425b7ba8371SEric Paris {
426b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
427b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
428000285deSEric Paris 	int wd = i_mark->wd;
429b7ba8371SEric Paris 
430b7ba8371SEric Paris 	assert_spin_locked(idr_lock);
431b7ba8371SEric Paris 
432b7ba8371SEric Paris 	idr_remove(idr, wd);
433b7ba8371SEric Paris 
434b7ba8371SEric Paris 	/* removed from the idr, drop that ref */
435000285deSEric Paris 	fsnotify_put_mark(&i_mark->fsn_mark);
436b7ba8371SEric Paris }
437b7ba8371SEric Paris 
438dead537dSEric Paris /*
439dead537dSEric Paris  * Remove the mark from the idr (if present) and drop the reference
440dead537dSEric Paris  * on the mark because it was in the idr.
441dead537dSEric Paris  */
4427e790dd5SEric Paris static void inotify_remove_from_idr(struct fsnotify_group *group,
443000285deSEric Paris 				    struct inotify_inode_mark *i_mark)
4447e790dd5SEric Paris {
445b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
446000285deSEric Paris 	struct inotify_inode_mark *found_i_mark = NULL;
447dead537dSEric Paris 	int wd;
4487e790dd5SEric Paris 
449b7ba8371SEric Paris 	spin_lock(idr_lock);
450000285deSEric Paris 	wd = i_mark->wd;
451dead537dSEric Paris 
452b7ba8371SEric Paris 	/*
453000285deSEric Paris 	 * does this i_mark think it is in the idr?  we shouldn't get called
454b7ba8371SEric Paris 	 * if it wasn't....
455b7ba8371SEric Paris 	 */
456b7ba8371SEric Paris 	if (wd == -1) {
457000285deSEric Paris 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
458000285deSEric Paris 			" i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
459000285deSEric Paris 			i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
460dead537dSEric Paris 		goto out;
4617e790dd5SEric Paris 	}
462dead537dSEric Paris 
463b7ba8371SEric Paris 	/* Lets look in the idr to see if we find it */
464000285deSEric Paris 	found_i_mark = inotify_idr_find_locked(group, wd);
465000285deSEric Paris 	if (unlikely(!found_i_mark)) {
466000285deSEric Paris 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
467000285deSEric Paris 			" i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
468000285deSEric Paris 			i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
469b7ba8371SEric Paris 		goto out;
470b7ba8371SEric Paris 	}
471dead537dSEric Paris 
472b7ba8371SEric Paris 	/*
473000285deSEric Paris 	 * We found an mark in the idr at the right wd, but it's
474000285deSEric Paris 	 * not the mark we were told to remove.  eparis seriously
475b7ba8371SEric Paris 	 * fucked up somewhere.
476b7ba8371SEric Paris 	 */
477000285deSEric Paris 	if (unlikely(found_i_mark != i_mark)) {
478000285deSEric Paris 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
479000285deSEric Paris 			"mark->inode=%p found_i_mark=%p found_i_mark->wd=%d "
480000285deSEric Paris 			"found_i_mark->group=%p found_i_mark->inode=%p\n",
481000285deSEric Paris 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group,
482000285deSEric Paris 			i_mark->fsn_mark.i.inode, found_i_mark, found_i_mark->wd,
483000285deSEric Paris 			found_i_mark->fsn_mark.group,
484000285deSEric Paris 			found_i_mark->fsn_mark.i.inode);
485b7ba8371SEric Paris 		goto out;
486b7ba8371SEric Paris 	}
487dead537dSEric Paris 
488b7ba8371SEric Paris 	/*
489b7ba8371SEric Paris 	 * One ref for being in the idr
490b7ba8371SEric Paris 	 * one ref held by the caller trying to kill us
491b7ba8371SEric Paris 	 * one ref grabbed by inotify_idr_find
492b7ba8371SEric Paris 	 */
493000285deSEric Paris 	if (unlikely(atomic_read(&i_mark->fsn_mark.refcnt) < 3)) {
494000285deSEric Paris 		printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
495000285deSEric Paris 			" i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
496000285deSEric Paris 			i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
497b7ba8371SEric Paris 		/* we can't really recover with bad ref cnting.. */
498b7ba8371SEric Paris 		BUG();
499b7ba8371SEric Paris 	}
500b7ba8371SEric Paris 
501000285deSEric Paris 	do_inotify_remove_from_idr(group, i_mark);
502dead537dSEric Paris out:
503b7ba8371SEric Paris 	/* match the ref taken by inotify_idr_find_locked() */
504000285deSEric Paris 	if (found_i_mark)
505000285deSEric Paris 		fsnotify_put_mark(&found_i_mark->fsn_mark);
506000285deSEric Paris 	i_mark->wd = -1;
507b7ba8371SEric Paris 	spin_unlock(idr_lock);
508dead537dSEric Paris }
509dead537dSEric Paris 
51063c882a0SEric Paris /*
511dead537dSEric Paris  * Send IN_IGNORED for this wd, remove this wd from the idr.
51263c882a0SEric Paris  */
513000285deSEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
514528da3e9SEric Paris 				    struct fsnotify_group *group)
51563c882a0SEric Paris {
516000285deSEric Paris 	struct inotify_inode_mark *i_mark;
517f70ab54cSEric Paris 	struct fsnotify_event *ignored_event, *notify_event;
51863c882a0SEric Paris 	struct inotify_event_private_data *event_priv;
51963c882a0SEric Paris 	struct fsnotify_event_private_data *fsn_event_priv;
520eef3a116SEric Paris 	int ret;
52163c882a0SEric Paris 
522f44aebccSEric Paris 	ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL,
523f44aebccSEric Paris 					      FSNOTIFY_EVENT_NONE, NULL, 0,
524f44aebccSEric Paris 					      GFP_NOFS);
525f44aebccSEric Paris 	if (!ignored_event)
526f44aebccSEric Paris 		return;
527f44aebccSEric Paris 
528000285deSEric Paris 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
52963c882a0SEric Paris 
530f44aebccSEric Paris 	event_priv = kmem_cache_alloc(event_priv_cachep, GFP_NOFS);
53163c882a0SEric Paris 	if (unlikely(!event_priv))
53263c882a0SEric Paris 		goto skip_send_ignore;
53363c882a0SEric Paris 
53463c882a0SEric Paris 	fsn_event_priv = &event_priv->fsnotify_event_priv_data;
53563c882a0SEric Paris 
53663c882a0SEric Paris 	fsn_event_priv->group = group;
537000285deSEric Paris 	event_priv->wd = i_mark->wd;
53863c882a0SEric Paris 
539f70ab54cSEric Paris 	notify_event = fsnotify_add_notify_event(group, ignored_event, fsn_event_priv, NULL);
540f70ab54cSEric Paris 	if (notify_event) {
541f70ab54cSEric Paris 		if (IS_ERR(notify_event))
542f70ab54cSEric Paris 			ret = PTR_ERR(notify_event);
543f70ab54cSEric Paris 		else
544f70ab54cSEric Paris 			fsnotify_put_event(notify_event);
54563c882a0SEric Paris 		inotify_free_event_priv(fsn_event_priv);
546f70ab54cSEric Paris 	}
54763c882a0SEric Paris 
54863c882a0SEric Paris skip_send_ignore:
54963c882a0SEric Paris 
550f44aebccSEric Paris 	/* matches the reference taken when the event was created */
551f44aebccSEric Paris 	fsnotify_put_event(ignored_event);
552f44aebccSEric Paris 
553000285deSEric Paris 	/* remove this mark from the idr */
554000285deSEric Paris 	inotify_remove_from_idr(group, i_mark);
55563c882a0SEric Paris 
5565549f7cdSEric Paris 	atomic_dec(&group->inotify_data.user->inotify_watches);
55763c882a0SEric Paris }
55863c882a0SEric Paris 
55963c882a0SEric Paris /* ding dong the mark is dead */
560000285deSEric Paris static void inotify_free_mark(struct fsnotify_mark *fsn_mark)
56163c882a0SEric Paris {
562000285deSEric Paris 	struct inotify_inode_mark *i_mark;
56331ddd326SEric Paris 
564000285deSEric Paris 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
56563c882a0SEric Paris 
566000285deSEric Paris 	kmem_cache_free(inotify_inode_mark_cachep, i_mark);
56763c882a0SEric Paris }
56863c882a0SEric Paris 
56952cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group,
57052cef755SEric Paris 					 struct inode *inode,
57152cef755SEric Paris 					 u32 arg)
57263c882a0SEric Paris {
573000285deSEric Paris 	struct fsnotify_mark *fsn_mark;
574000285deSEric Paris 	struct inotify_inode_mark *i_mark;
57563c882a0SEric Paris 	__u32 old_mask, new_mask;
57652cef755SEric Paris 	__u32 mask;
57752cef755SEric Paris 	int add = (arg & IN_MASK_ADD);
57852cef755SEric Paris 	int ret;
57963c882a0SEric Paris 
58063c882a0SEric Paris 	/* don't allow invalid bits: we don't want flags set */
58163c882a0SEric Paris 	mask = inotify_arg_to_mask(arg);
58244b350fcSJerome Marchand 	if (unlikely(!(mask & IN_ALL_EVENTS)))
58363c882a0SEric Paris 		return -EINVAL;
58463c882a0SEric Paris 
5855444e298SEric Paris 	fsn_mark = fsnotify_find_inode_mark(group, inode);
586000285deSEric Paris 	if (!fsn_mark)
58752cef755SEric Paris 		return -ENOENT;
58852cef755SEric Paris 
589000285deSEric Paris 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
59075fe2b26SEric Paris 
591000285deSEric Paris 	spin_lock(&fsn_mark->lock);
59263c882a0SEric Paris 
593000285deSEric Paris 	old_mask = fsn_mark->mask;
59490b1e7a5SEric Paris 	if (add)
59590b1e7a5SEric Paris 		fsnotify_set_mark_mask_locked(fsn_mark, (fsn_mark->mask | mask));
59690b1e7a5SEric Paris 	else
59790b1e7a5SEric Paris 		fsnotify_set_mark_mask_locked(fsn_mark, mask);
598000285deSEric Paris 	new_mask = fsn_mark->mask;
59963c882a0SEric Paris 
600000285deSEric Paris 	spin_unlock(&fsn_mark->lock);
60163c882a0SEric Paris 
60263c882a0SEric Paris 	if (old_mask != new_mask) {
60363c882a0SEric Paris 		/* more bits in old than in new? */
60463c882a0SEric Paris 		int dropped = (old_mask & ~new_mask);
605000285deSEric Paris 		/* more bits in this fsn_mark than the inode's mask? */
60663c882a0SEric Paris 		int do_inode = (new_mask & ~inode->i_fsnotify_mask);
60763c882a0SEric Paris 
608000285deSEric Paris 		/* update the inode with this new fsn_mark */
60963c882a0SEric Paris 		if (dropped || do_inode)
61063c882a0SEric Paris 			fsnotify_recalc_inode_mask(inode);
61163c882a0SEric Paris 
61263c882a0SEric Paris 	}
61363c882a0SEric Paris 
61452cef755SEric Paris 	/* return the wd */
615000285deSEric Paris 	ret = i_mark->wd;
61652cef755SEric Paris 
617d0775441SEric Paris 	/* match the get from fsnotify_find_mark() */
618000285deSEric Paris 	fsnotify_put_mark(fsn_mark);
61975fe2b26SEric Paris 
62052cef755SEric Paris 	return ret;
62163c882a0SEric Paris }
6227e790dd5SEric Paris 
62352cef755SEric Paris static int inotify_new_watch(struct fsnotify_group *group,
62452cef755SEric Paris 			     struct inode *inode,
62552cef755SEric Paris 			     u32 arg)
62652cef755SEric Paris {
627000285deSEric Paris 	struct inotify_inode_mark *tmp_i_mark;
62852cef755SEric Paris 	__u32 mask;
62952cef755SEric Paris 	int ret;
630b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
631b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
63252cef755SEric Paris 
63352cef755SEric Paris 	/* don't allow invalid bits: we don't want flags set */
63452cef755SEric Paris 	mask = inotify_arg_to_mask(arg);
63544b350fcSJerome Marchand 	if (unlikely(!(mask & IN_ALL_EVENTS)))
63652cef755SEric Paris 		return -EINVAL;
63752cef755SEric Paris 
638000285deSEric Paris 	tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
639000285deSEric Paris 	if (unlikely(!tmp_i_mark))
64052cef755SEric Paris 		return -ENOMEM;
64152cef755SEric Paris 
642000285deSEric Paris 	fsnotify_init_mark(&tmp_i_mark->fsn_mark, inotify_free_mark);
643000285deSEric Paris 	tmp_i_mark->fsn_mark.mask = mask;
644000285deSEric Paris 	tmp_i_mark->wd = -1;
64552cef755SEric Paris 
64652cef755SEric Paris 	ret = -ENOSPC;
64752cef755SEric Paris 	if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
64852cef755SEric Paris 		goto out_err;
649b7ba8371SEric Paris 
6507050c488SEric Paris 	ret = inotify_add_to_idr(idr, idr_lock, &group->inotify_data.last_wd,
651000285deSEric Paris 				 tmp_i_mark);
652b7ba8371SEric Paris 	if (ret)
65352cef755SEric Paris 		goto out_err;
65452cef755SEric Paris 
65552cef755SEric Paris 	/* we are on the idr, now get on the inode */
6565444e298SEric Paris 	ret = fsnotify_add_mark(&tmp_i_mark->fsn_mark, group, inode, NULL, 0);
65752cef755SEric Paris 	if (ret) {
65852cef755SEric Paris 		/* we failed to get on the inode, get off the idr */
659000285deSEric Paris 		inotify_remove_from_idr(group, tmp_i_mark);
66052cef755SEric Paris 		goto out_err;
66152cef755SEric Paris 	}
66252cef755SEric Paris 
66352cef755SEric Paris 	/* increment the number of watches the user has */
66452cef755SEric Paris 	atomic_inc(&group->inotify_data.user->inotify_watches);
66552cef755SEric Paris 
666000285deSEric Paris 	/* return the watch descriptor for this new mark */
667000285deSEric Paris 	ret = tmp_i_mark->wd;
66852cef755SEric Paris 
66952cef755SEric Paris out_err:
670000285deSEric Paris 	/* match the ref from fsnotify_init_mark() */
671000285deSEric Paris 	fsnotify_put_mark(&tmp_i_mark->fsn_mark);
67252cef755SEric Paris 
67352cef755SEric Paris 	return ret;
67452cef755SEric Paris }
67552cef755SEric Paris 
67652cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
67752cef755SEric Paris {
67852cef755SEric Paris 	int ret = 0;
67952cef755SEric Paris 
68052cef755SEric Paris retry:
68152cef755SEric Paris 	/* try to update and existing watch with the new arg */
68252cef755SEric Paris 	ret = inotify_update_existing_watch(group, inode, arg);
68352cef755SEric Paris 	/* no mark present, try to add a new one */
68452cef755SEric Paris 	if (ret == -ENOENT)
68552cef755SEric Paris 		ret = inotify_new_watch(group, inode, arg);
68652cef755SEric Paris 	/*
68752cef755SEric Paris 	 * inotify_new_watch could race with another thread which did an
68852cef755SEric Paris 	 * inotify_new_watch between the update_existing and the add watch
68952cef755SEric Paris 	 * here, go back and try to update an existing mark again.
69052cef755SEric Paris 	 */
69152cef755SEric Paris 	if (ret == -EEXIST)
69252cef755SEric Paris 		goto retry;
69352cef755SEric Paris 
69463c882a0SEric Paris 	return ret;
69563c882a0SEric Paris }
69663c882a0SEric Paris 
697d0de4dc5SEric Paris static struct fsnotify_group *inotify_new_group(unsigned int max_events)
69863c882a0SEric Paris {
69963c882a0SEric Paris 	struct fsnotify_group *group;
70063c882a0SEric Paris 
7010d2e2a1dSEric Paris 	group = fsnotify_alloc_group(&inotify_fsnotify_ops);
70263c882a0SEric Paris 	if (IS_ERR(group))
70363c882a0SEric Paris 		return group;
70463c882a0SEric Paris 
70563c882a0SEric Paris 	group->max_events = max_events;
70663c882a0SEric Paris 
70763c882a0SEric Paris 	spin_lock_init(&group->inotify_data.idr_lock);
70863c882a0SEric Paris 	idr_init(&group->inotify_data.idr);
7099e572cc9SEric Paris 	group->inotify_data.last_wd = 0;
71063c882a0SEric Paris 	group->inotify_data.fa = NULL;
711d0de4dc5SEric Paris 	group->inotify_data.user = get_current_user();
712d0de4dc5SEric Paris 
713d0de4dc5SEric Paris 	if (atomic_inc_return(&group->inotify_data.user->inotify_devs) >
714d0de4dc5SEric Paris 	    inotify_max_user_instances) {
715d0de4dc5SEric Paris 		fsnotify_put_group(group);
716d0de4dc5SEric Paris 		return ERR_PTR(-EMFILE);
717d0de4dc5SEric Paris 	}
71863c882a0SEric Paris 
71963c882a0SEric Paris 	return group;
72063c882a0SEric Paris }
72163c882a0SEric Paris 
72263c882a0SEric Paris 
72363c882a0SEric Paris /* inotify syscalls */
724938bb9f5SHeiko Carstens SYSCALL_DEFINE1(inotify_init1, int, flags)
725272eb014SEric Paris {
72663c882a0SEric Paris 	struct fsnotify_group *group;
727c44dcc56SAl Viro 	int ret;
728272eb014SEric Paris 
729272eb014SEric Paris 	/* Check the IN_* constants for consistency.  */
730272eb014SEric Paris 	BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
731272eb014SEric Paris 	BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
732272eb014SEric Paris 
733272eb014SEric Paris 	if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
734272eb014SEric Paris 		return -EINVAL;
735272eb014SEric Paris 
73663c882a0SEric Paris 	/* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
737d0de4dc5SEric Paris 	group = inotify_new_group(inotify_max_queued_events);
738d0de4dc5SEric Paris 	if (IS_ERR(group))
739d0de4dc5SEric Paris 		return PTR_ERR(group);
740825f9692SAl Viro 
741c44dcc56SAl Viro 	ret = anon_inode_getfd("inotify", &inotify_fops, group,
742c44dcc56SAl Viro 				  O_RDONLY | flags);
743d0de4dc5SEric Paris 	if (ret < 0)
744a2ae4cc9SEric Paris 		fsnotify_put_group(group);
745d0de4dc5SEric Paris 
746272eb014SEric Paris 	return ret;
747272eb014SEric Paris }
748272eb014SEric Paris 
749938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init)
750272eb014SEric Paris {
751272eb014SEric Paris 	return sys_inotify_init1(0);
752272eb014SEric Paris }
753272eb014SEric Paris 
7542e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
7552e4d0924SHeiko Carstens 		u32, mask)
756272eb014SEric Paris {
75763c882a0SEric Paris 	struct fsnotify_group *group;
758272eb014SEric Paris 	struct inode *inode;
759272eb014SEric Paris 	struct path path;
760*2903ff01SAl Viro 	struct fd f;
761*2903ff01SAl Viro 	int ret;
762272eb014SEric Paris 	unsigned flags = 0;
763272eb014SEric Paris 
764*2903ff01SAl Viro 	f = fdget(fd);
765*2903ff01SAl Viro 	if (unlikely(!f.file))
766272eb014SEric Paris 		return -EBADF;
767272eb014SEric Paris 
768272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
769*2903ff01SAl Viro 	if (unlikely(f.file->f_op != &inotify_fops)) {
770272eb014SEric Paris 		ret = -EINVAL;
771272eb014SEric Paris 		goto fput_and_out;
772272eb014SEric Paris 	}
773272eb014SEric Paris 
774272eb014SEric Paris 	if (!(mask & IN_DONT_FOLLOW))
775272eb014SEric Paris 		flags |= LOOKUP_FOLLOW;
776272eb014SEric Paris 	if (mask & IN_ONLYDIR)
777272eb014SEric Paris 		flags |= LOOKUP_DIRECTORY;
778272eb014SEric Paris 
77963c882a0SEric Paris 	ret = inotify_find_inode(pathname, &path, flags);
78063c882a0SEric Paris 	if (ret)
781272eb014SEric Paris 		goto fput_and_out;
782272eb014SEric Paris 
78363c882a0SEric Paris 	/* inode held in place by reference to path; group by fget on fd */
784272eb014SEric Paris 	inode = path.dentry->d_inode;
785*2903ff01SAl Viro 	group = f.file->private_data;
786272eb014SEric Paris 
78763c882a0SEric Paris 	/* create/update an inode mark */
78863c882a0SEric Paris 	ret = inotify_update_watch(group, inode, mask);
789272eb014SEric Paris 	path_put(&path);
790272eb014SEric Paris fput_and_out:
791*2903ff01SAl Viro 	fdput(f);
792272eb014SEric Paris 	return ret;
793272eb014SEric Paris }
794272eb014SEric Paris 
7952e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
796272eb014SEric Paris {
79763c882a0SEric Paris 	struct fsnotify_group *group;
798000285deSEric Paris 	struct inotify_inode_mark *i_mark;
799*2903ff01SAl Viro 	struct fd f;
800*2903ff01SAl Viro 	int ret = 0;
801272eb014SEric Paris 
802*2903ff01SAl Viro 	f = fdget(fd);
803*2903ff01SAl Viro 	if (unlikely(!f.file))
804272eb014SEric Paris 		return -EBADF;
805272eb014SEric Paris 
806272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
807272eb014SEric Paris 	ret = -EINVAL;
808*2903ff01SAl Viro 	if (unlikely(f.file->f_op != &inotify_fops))
809272eb014SEric Paris 		goto out;
810272eb014SEric Paris 
811*2903ff01SAl Viro 	group = f.file->private_data;
812272eb014SEric Paris 
81363c882a0SEric Paris 	ret = -EINVAL;
814000285deSEric Paris 	i_mark = inotify_idr_find(group, wd);
815000285deSEric Paris 	if (unlikely(!i_mark))
81663c882a0SEric Paris 		goto out;
81763c882a0SEric Paris 
818b7ba8371SEric Paris 	ret = 0;
819b7ba8371SEric Paris 
820000285deSEric Paris 	fsnotify_destroy_mark(&i_mark->fsn_mark);
821b7ba8371SEric Paris 
822b7ba8371SEric Paris 	/* match ref taken by inotify_idr_find */
823000285deSEric Paris 	fsnotify_put_mark(&i_mark->fsn_mark);
824272eb014SEric Paris 
825272eb014SEric Paris out:
826*2903ff01SAl Viro 	fdput(f);
827272eb014SEric Paris 	return ret;
828272eb014SEric Paris }
829272eb014SEric Paris 
830272eb014SEric Paris /*
831ae0e47f0SJustin P. Mattock  * inotify_user_setup - Our initialization function.  Note that we cannot return
832272eb014SEric Paris  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
833272eb014SEric Paris  * must result in panic().
834272eb014SEric Paris  */
835272eb014SEric Paris static int __init inotify_user_setup(void)
836272eb014SEric Paris {
837f874e1acSEric Paris 	BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
838f874e1acSEric Paris 	BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
839f874e1acSEric Paris 	BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
840f874e1acSEric Paris 	BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
841f874e1acSEric Paris 	BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
842f874e1acSEric Paris 	BUILD_BUG_ON(IN_OPEN != FS_OPEN);
843f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
844f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
845f874e1acSEric Paris 	BUILD_BUG_ON(IN_CREATE != FS_CREATE);
846f874e1acSEric Paris 	BUILD_BUG_ON(IN_DELETE != FS_DELETE);
847f874e1acSEric Paris 	BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
848f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
849f874e1acSEric Paris 	BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
850f874e1acSEric Paris 	BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
851f874e1acSEric Paris 	BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
852f874e1acSEric Paris 	BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
853b29866aaSEric Paris 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
854f874e1acSEric Paris 	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
855f874e1acSEric Paris 
856f874e1acSEric Paris 	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
857f874e1acSEric Paris 
858000285deSEric Paris 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
85963c882a0SEric Paris 	event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC);
86063c882a0SEric Paris 
861272eb014SEric Paris 	inotify_max_queued_events = 16384;
862272eb014SEric Paris 	inotify_max_user_instances = 128;
863272eb014SEric Paris 	inotify_max_user_watches = 8192;
864272eb014SEric Paris 
865272eb014SEric Paris 	return 0;
866272eb014SEric Paris }
867272eb014SEric Paris module_init(inotify_user_setup);
868