xref: /linux/fs/notify/inotify/inotify_user.c (revision 1cce1eea0aff51201753fcaca421df825b0813b6)
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>
29c013d5a4SPaul Gortmaker #include <linux/init.h> /* fs_initcall */
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"
43be77196bSCyrill Gorcunov #include "../fdinfo.h"
44272eb014SEric Paris 
45272eb014SEric Paris #include <asm/ioctls.h>
46272eb014SEric Paris 
47*1cce1eeaSNikolay Borisov /* configurable via /proc/sys/fs/inotify/ */
48272eb014SEric Paris static int inotify_max_queued_events __read_mostly;
4963c882a0SEric Paris 
5063c882a0SEric Paris static struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
51272eb014SEric Paris 
52272eb014SEric Paris #ifdef CONFIG_SYSCTL
53272eb014SEric Paris 
54272eb014SEric Paris #include <linux/sysctl.h>
55272eb014SEric Paris 
56272eb014SEric Paris static int zero;
57272eb014SEric Paris 
5892f778ddSJoe Perches struct ctl_table inotify_table[] = {
59272eb014SEric Paris 	{
60272eb014SEric Paris 		.procname	= "max_user_instances",
61*1cce1eeaSNikolay Borisov 		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
62272eb014SEric Paris 		.maxlen		= sizeof(int),
63272eb014SEric Paris 		.mode		= 0644,
646d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
65272eb014SEric Paris 		.extra1		= &zero,
66272eb014SEric Paris 	},
67272eb014SEric Paris 	{
68272eb014SEric Paris 		.procname	= "max_user_watches",
69*1cce1eeaSNikolay Borisov 		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
70272eb014SEric Paris 		.maxlen		= sizeof(int),
71272eb014SEric Paris 		.mode		= 0644,
726d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
73272eb014SEric Paris 		.extra1		= &zero,
74272eb014SEric Paris 	},
75272eb014SEric Paris 	{
76272eb014SEric Paris 		.procname	= "max_queued_events",
77272eb014SEric Paris 		.data		= &inotify_max_queued_events,
78272eb014SEric Paris 		.maxlen		= sizeof(int),
79272eb014SEric Paris 		.mode		= 0644,
806d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
81272eb014SEric Paris 		.extra1		= &zero
82272eb014SEric Paris 	},
83ab09203eSEric W. Biederman 	{ }
84272eb014SEric Paris };
85272eb014SEric Paris #endif /* CONFIG_SYSCTL */
86272eb014SEric Paris 
8763c882a0SEric Paris static inline __u32 inotify_arg_to_mask(u32 arg)
88272eb014SEric Paris {
8963c882a0SEric Paris 	__u32 mask;
9063c882a0SEric Paris 
91611da04fSEric Paris 	/*
92611da04fSEric Paris 	 * everything should accept their own ignored, cares about children,
93611da04fSEric Paris 	 * and should receive events when the inode is unmounted
94611da04fSEric Paris 	 */
95611da04fSEric Paris 	mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
9663c882a0SEric Paris 
9763c882a0SEric Paris 	/* mask off the flags used to open the fd */
988c1934c8SEric Paris 	mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
9963c882a0SEric Paris 
10063c882a0SEric Paris 	return mask;
101272eb014SEric Paris }
102272eb014SEric Paris 
10363c882a0SEric Paris static inline u32 inotify_mask_to_arg(__u32 mask)
104272eb014SEric Paris {
10563c882a0SEric Paris 	return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
10663c882a0SEric Paris 		       IN_Q_OVERFLOW);
107272eb014SEric Paris }
108272eb014SEric Paris 
10963c882a0SEric Paris /* intofiy userspace file descriptor functions */
110272eb014SEric Paris static unsigned int inotify_poll(struct file *file, poll_table *wait)
111272eb014SEric Paris {
11263c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
113272eb014SEric Paris 	int ret = 0;
114272eb014SEric Paris 
11563c882a0SEric Paris 	poll_wait(file, &group->notification_waitq, wait);
116c21dbe20SJan Kara 	spin_lock(&group->notification_lock);
11763c882a0SEric Paris 	if (!fsnotify_notify_queue_is_empty(group))
118272eb014SEric Paris 		ret = POLLIN | POLLRDNORM;
119c21dbe20SJan Kara 	spin_unlock(&group->notification_lock);
120272eb014SEric Paris 
121272eb014SEric Paris 	return ret;
122272eb014SEric Paris }
123272eb014SEric Paris 
1247053aee2SJan Kara static int round_event_name_len(struct fsnotify_event *fsn_event)
125e9fe6904SJan Kara {
1267053aee2SJan Kara 	struct inotify_event_info *event;
1277053aee2SJan Kara 
1287053aee2SJan Kara 	event = INOTIFY_E(fsn_event);
129e9fe6904SJan Kara 	if (!event->name_len)
130e9fe6904SJan Kara 		return 0;
131e9fe6904SJan Kara 	return roundup(event->name_len + 1, sizeof(struct inotify_event));
132e9fe6904SJan Kara }
133e9fe6904SJan Kara 
1343632dee2SVegard Nossum /*
1353632dee2SVegard Nossum  * Get an inotify_kernel_event if one exists and is small
1363632dee2SVegard Nossum  * enough to fit in "count". Return an error pointer if
1373632dee2SVegard Nossum  * not large enough.
1383632dee2SVegard Nossum  *
139c21dbe20SJan Kara  * Called with the group->notification_lock held.
1403632dee2SVegard Nossum  */
14163c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
1423632dee2SVegard Nossum 					    size_t count)
1433632dee2SVegard Nossum {
1443632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
14563c882a0SEric Paris 	struct fsnotify_event *event;
1463632dee2SVegard Nossum 
14763c882a0SEric Paris 	if (fsnotify_notify_queue_is_empty(group))
1483632dee2SVegard Nossum 		return NULL;
1493632dee2SVegard Nossum 
1508ba8fa91SJan Kara 	event = fsnotify_peek_first_event(group);
15163c882a0SEric Paris 
1525ba08e2eSEric Paris 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
1535ba08e2eSEric Paris 
154e9fe6904SJan Kara 	event_size += round_event_name_len(event);
1553632dee2SVegard Nossum 	if (event_size > count)
1563632dee2SVegard Nossum 		return ERR_PTR(-EINVAL);
1573632dee2SVegard Nossum 
158c21dbe20SJan Kara 	/* held the notification_lock the whole time, so this is the
15963c882a0SEric Paris 	 * same event we peeked above */
1608ba8fa91SJan Kara 	fsnotify_remove_first_event(group);
16163c882a0SEric Paris 
16263c882a0SEric Paris 	return event;
1633632dee2SVegard Nossum }
1643632dee2SVegard Nossum 
1653632dee2SVegard Nossum /*
1663632dee2SVegard Nossum  * Copy an event to user space, returning how much we copied.
1673632dee2SVegard Nossum  *
1683632dee2SVegard Nossum  * We already checked that the event size is smaller than the
1693632dee2SVegard Nossum  * buffer we had in "get_one_event()" above.
1703632dee2SVegard Nossum  */
17163c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group,
1727053aee2SJan Kara 				  struct fsnotify_event *fsn_event,
1733632dee2SVegard Nossum 				  char __user *buf)
1743632dee2SVegard Nossum {
17563c882a0SEric Paris 	struct inotify_event inotify_event;
1767053aee2SJan Kara 	struct inotify_event_info *event;
1773632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
178e9fe6904SJan Kara 	size_t name_len;
179e9fe6904SJan Kara 	size_t pad_name_len;
1803632dee2SVegard Nossum 
1817053aee2SJan Kara 	pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
1825ba08e2eSEric Paris 
1837053aee2SJan Kara 	event = INOTIFY_E(fsn_event);
184e9fe6904SJan Kara 	name_len = event->name_len;
185b962e731SBrian Rogers 	/*
186e9fe6904SJan Kara 	 * round up name length so it is a multiple of event_size
1870db501bdSEric W. Biederman 	 * plus an extra byte for the terminating '\0'.
1880db501bdSEric W. Biederman 	 */
1897053aee2SJan Kara 	pad_name_len = round_event_name_len(fsn_event);
190e9fe6904SJan Kara 	inotify_event.len = pad_name_len;
1917053aee2SJan Kara 	inotify_event.mask = inotify_mask_to_arg(fsn_event->mask);
1927053aee2SJan Kara 	inotify_event.wd = event->wd;
19363c882a0SEric Paris 	inotify_event.cookie = event->sync_cookie;
19463c882a0SEric Paris 
19563c882a0SEric Paris 	/* send the main event */
19663c882a0SEric Paris 	if (copy_to_user(buf, &inotify_event, event_size))
1973632dee2SVegard Nossum 		return -EFAULT;
1983632dee2SVegard Nossum 
1993632dee2SVegard Nossum 	buf += event_size;
2003632dee2SVegard Nossum 
20163c882a0SEric Paris 	/*
20263c882a0SEric Paris 	 * fsnotify only stores the pathname, so here we have to send the pathname
20363c882a0SEric Paris 	 * and then pad that pathname out to a multiple of sizeof(inotify_event)
204e9fe6904SJan Kara 	 * with zeros.
20563c882a0SEric Paris 	 */
206e9fe6904SJan Kara 	if (pad_name_len) {
20763c882a0SEric Paris 		/* copy the path name */
2087053aee2SJan Kara 		if (copy_to_user(buf, event->name, name_len))
2093632dee2SVegard Nossum 			return -EFAULT;
210e9fe6904SJan Kara 		buf += name_len;
2113632dee2SVegard Nossum 
2120db501bdSEric W. Biederman 		/* fill userspace with 0's */
213e9fe6904SJan Kara 		if (clear_user(buf, pad_name_len - name_len))
21463c882a0SEric Paris 			return -EFAULT;
215e9fe6904SJan Kara 		event_size += pad_name_len;
2163632dee2SVegard Nossum 	}
21763c882a0SEric Paris 
2183632dee2SVegard Nossum 	return event_size;
2193632dee2SVegard Nossum }
2203632dee2SVegard Nossum 
221272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf,
222272eb014SEric Paris 			    size_t count, loff_t *pos)
223272eb014SEric Paris {
22463c882a0SEric Paris 	struct fsnotify_group *group;
22563c882a0SEric Paris 	struct fsnotify_event *kevent;
226272eb014SEric Paris 	char __user *start;
227272eb014SEric Paris 	int ret;
228e23738a7SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
229272eb014SEric Paris 
230272eb014SEric Paris 	start = buf;
23163c882a0SEric Paris 	group = file->private_data;
232272eb014SEric Paris 
233e23738a7SPeter Zijlstra 	add_wait_queue(&group->notification_waitq, &wait);
234272eb014SEric Paris 	while (1) {
235c21dbe20SJan Kara 		spin_lock(&group->notification_lock);
23663c882a0SEric Paris 		kevent = get_one_event(group, count);
237c21dbe20SJan Kara 		spin_unlock(&group->notification_lock);
238272eb014SEric Paris 
2395ba08e2eSEric Paris 		pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
2405ba08e2eSEric Paris 
2413632dee2SVegard Nossum 		if (kevent) {
2423632dee2SVegard Nossum 			ret = PTR_ERR(kevent);
2433632dee2SVegard Nossum 			if (IS_ERR(kevent))
244272eb014SEric Paris 				break;
24563c882a0SEric Paris 			ret = copy_event_to_user(group, kevent, buf);
2467053aee2SJan Kara 			fsnotify_destroy_event(group, kevent);
2473632dee2SVegard Nossum 			if (ret < 0)
2483632dee2SVegard Nossum 				break;
2493632dee2SVegard Nossum 			buf += ret;
2503632dee2SVegard Nossum 			count -= ret;
2513632dee2SVegard Nossum 			continue;
252272eb014SEric Paris 		}
253272eb014SEric Paris 
2543632dee2SVegard Nossum 		ret = -EAGAIN;
2553632dee2SVegard Nossum 		if (file->f_flags & O_NONBLOCK)
256272eb014SEric Paris 			break;
2571ca39ab9SEric Paris 		ret = -ERESTARTSYS;
2583632dee2SVegard Nossum 		if (signal_pending(current))
2593632dee2SVegard Nossum 			break;
2603632dee2SVegard Nossum 
2613632dee2SVegard Nossum 		if (start != buf)
2623632dee2SVegard Nossum 			break;
263272eb014SEric Paris 
264e23738a7SPeter Zijlstra 		wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
265272eb014SEric Paris 	}
266e23738a7SPeter Zijlstra 	remove_wait_queue(&group->notification_waitq, &wait);
267272eb014SEric Paris 
2683632dee2SVegard Nossum 	if (start != buf && ret != -EFAULT)
269272eb014SEric Paris 		ret = buf - start;
270272eb014SEric Paris 	return ret;
271272eb014SEric Paris }
272272eb014SEric Paris 
273272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file)
274272eb014SEric Paris {
27563c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
276272eb014SEric Paris 
2775ba08e2eSEric Paris 	pr_debug("%s: group=%p\n", __func__, group);
2785ba08e2eSEric Paris 
27963c882a0SEric Paris 	/* free this group, matching get was inotify_init->fsnotify_obtain_group */
280d8153d4dSLino Sanfilippo 	fsnotify_destroy_group(group);
281272eb014SEric Paris 
282272eb014SEric Paris 	return 0;
283272eb014SEric Paris }
284272eb014SEric Paris 
285272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd,
286272eb014SEric Paris 			  unsigned long arg)
287272eb014SEric Paris {
28863c882a0SEric Paris 	struct fsnotify_group *group;
2897053aee2SJan Kara 	struct fsnotify_event *fsn_event;
290272eb014SEric Paris 	void __user *p;
291272eb014SEric Paris 	int ret = -ENOTTY;
29263c882a0SEric Paris 	size_t send_len = 0;
293272eb014SEric Paris 
29463c882a0SEric Paris 	group = file->private_data;
295272eb014SEric Paris 	p = (void __user *) arg;
296272eb014SEric Paris 
2975ba08e2eSEric Paris 	pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
2985ba08e2eSEric Paris 
299272eb014SEric Paris 	switch (cmd) {
300272eb014SEric Paris 	case FIONREAD:
301c21dbe20SJan Kara 		spin_lock(&group->notification_lock);
3027053aee2SJan Kara 		list_for_each_entry(fsn_event, &group->notification_list,
3037053aee2SJan Kara 				    list) {
30463c882a0SEric Paris 			send_len += sizeof(struct inotify_event);
3057053aee2SJan Kara 			send_len += round_event_name_len(fsn_event);
30663c882a0SEric Paris 		}
307c21dbe20SJan Kara 		spin_unlock(&group->notification_lock);
30863c882a0SEric Paris 		ret = put_user(send_len, (int __user *) p);
309272eb014SEric Paris 		break;
310272eb014SEric Paris 	}
311272eb014SEric Paris 
312272eb014SEric Paris 	return ret;
313272eb014SEric Paris }
314272eb014SEric Paris 
315272eb014SEric Paris static const struct file_operations inotify_fops = {
316be77196bSCyrill Gorcunov 	.show_fdinfo	= inotify_show_fdinfo,
317272eb014SEric Paris 	.poll		= inotify_poll,
318272eb014SEric Paris 	.read		= inotify_read,
3190a6b6bd5SEric Paris 	.fasync		= fsnotify_fasync,
320272eb014SEric Paris 	.release	= inotify_release,
321272eb014SEric Paris 	.unlocked_ioctl	= inotify_ioctl,
322272eb014SEric Paris 	.compat_ioctl	= inotify_ioctl,
3236038f373SArnd Bergmann 	.llseek		= noop_llseek,
324272eb014SEric Paris };
325272eb014SEric Paris 
326272eb014SEric Paris 
32763c882a0SEric Paris /*
32863c882a0SEric Paris  * find_inode - resolve a user-given path to a specific inode
32963c882a0SEric Paris  */
33063c882a0SEric Paris static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
33163c882a0SEric Paris {
33263c882a0SEric Paris 	int error;
33363c882a0SEric Paris 
33463c882a0SEric Paris 	error = user_path_at(AT_FDCWD, dirname, flags, path);
33563c882a0SEric Paris 	if (error)
33663c882a0SEric Paris 		return error;
33763c882a0SEric Paris 	/* you can only watch an inode if you have read permissions on it */
33863c882a0SEric Paris 	error = inode_permission(path->dentry->d_inode, MAY_READ);
33963c882a0SEric Paris 	if (error)
34063c882a0SEric Paris 		path_put(path);
34163c882a0SEric Paris 	return error;
34263c882a0SEric Paris }
34363c882a0SEric Paris 
344b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
345000285deSEric Paris 			      struct inotify_inode_mark *i_mark)
346b7ba8371SEric Paris {
347b7ba8371SEric Paris 	int ret;
348b7ba8371SEric Paris 
3494542da63STejun Heo 	idr_preload(GFP_KERNEL);
350b7ba8371SEric Paris 	spin_lock(idr_lock);
3514542da63STejun Heo 
352a66c04b4SJeff Layton 	ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
3534542da63STejun Heo 	if (ret >= 0) {
354b7ba8371SEric Paris 		/* we added the mark to the idr, take a reference */
3554542da63STejun Heo 		i_mark->wd = ret;
356000285deSEric Paris 		fsnotify_get_mark(&i_mark->fsn_mark);
3577050c488SEric Paris 	}
358b7ba8371SEric Paris 
3594542da63STejun Heo 	spin_unlock(idr_lock);
3604542da63STejun Heo 	idr_preload_end();
3614542da63STejun Heo 	return ret < 0 ? ret : 0;
362b7ba8371SEric Paris }
363b7ba8371SEric Paris 
364000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
365b7ba8371SEric Paris 								int wd)
366b7ba8371SEric Paris {
367b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
368b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
369000285deSEric Paris 	struct inotify_inode_mark *i_mark;
370b7ba8371SEric Paris 
371b7ba8371SEric Paris 	assert_spin_locked(idr_lock);
372b7ba8371SEric Paris 
373000285deSEric Paris 	i_mark = idr_find(idr, wd);
374000285deSEric Paris 	if (i_mark) {
375000285deSEric Paris 		struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
376b7ba8371SEric Paris 
377000285deSEric Paris 		fsnotify_get_mark(fsn_mark);
378b7ba8371SEric Paris 		/* One ref for being in the idr, one ref we just took */
379000285deSEric Paris 		BUG_ON(atomic_read(&fsn_mark->refcnt) < 2);
380b7ba8371SEric Paris 	}
381b7ba8371SEric Paris 
382000285deSEric Paris 	return i_mark;
383b7ba8371SEric Paris }
384b7ba8371SEric Paris 
385000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
386b7ba8371SEric Paris 							 int wd)
387b7ba8371SEric Paris {
388000285deSEric Paris 	struct inotify_inode_mark *i_mark;
389b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
390b7ba8371SEric Paris 
391b7ba8371SEric Paris 	spin_lock(idr_lock);
392000285deSEric Paris 	i_mark = inotify_idr_find_locked(group, wd);
393b7ba8371SEric Paris 	spin_unlock(idr_lock);
394b7ba8371SEric Paris 
395000285deSEric Paris 	return i_mark;
396b7ba8371SEric Paris }
397b7ba8371SEric Paris 
398b7ba8371SEric Paris static void do_inotify_remove_from_idr(struct fsnotify_group *group,
399000285deSEric Paris 				       struct inotify_inode_mark *i_mark)
400b7ba8371SEric Paris {
401b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
402b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
403000285deSEric Paris 	int wd = i_mark->wd;
404b7ba8371SEric Paris 
405b7ba8371SEric Paris 	assert_spin_locked(idr_lock);
406b7ba8371SEric Paris 
407b7ba8371SEric Paris 	idr_remove(idr, wd);
408b7ba8371SEric Paris 
409b7ba8371SEric Paris 	/* removed from the idr, drop that ref */
410000285deSEric Paris 	fsnotify_put_mark(&i_mark->fsn_mark);
411b7ba8371SEric Paris }
412b7ba8371SEric Paris 
413dead537dSEric Paris /*
414dead537dSEric Paris  * Remove the mark from the idr (if present) and drop the reference
415dead537dSEric Paris  * on the mark because it was in the idr.
416dead537dSEric Paris  */
4177e790dd5SEric Paris static void inotify_remove_from_idr(struct fsnotify_group *group,
418000285deSEric Paris 				    struct inotify_inode_mark *i_mark)
4197e790dd5SEric Paris {
420b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
421000285deSEric Paris 	struct inotify_inode_mark *found_i_mark = NULL;
422dead537dSEric Paris 	int wd;
4237e790dd5SEric Paris 
424b7ba8371SEric Paris 	spin_lock(idr_lock);
425000285deSEric Paris 	wd = i_mark->wd;
426dead537dSEric Paris 
427b7ba8371SEric Paris 	/*
428000285deSEric Paris 	 * does this i_mark think it is in the idr?  we shouldn't get called
429b7ba8371SEric Paris 	 * if it wasn't....
430b7ba8371SEric Paris 	 */
431b7ba8371SEric Paris 	if (wd == -1) {
432000285deSEric Paris 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
433000285deSEric Paris 			" i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
4340809ab69SJan Kara 			i_mark->fsn_mark.group, i_mark->fsn_mark.inode);
435dead537dSEric Paris 		goto out;
4367e790dd5SEric Paris 	}
437dead537dSEric Paris 
438b7ba8371SEric Paris 	/* Lets look in the idr to see if we find it */
439000285deSEric Paris 	found_i_mark = inotify_idr_find_locked(group, wd);
440000285deSEric Paris 	if (unlikely(!found_i_mark)) {
441000285deSEric Paris 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
442000285deSEric Paris 			" i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
4430809ab69SJan Kara 			i_mark->fsn_mark.group, i_mark->fsn_mark.inode);
444b7ba8371SEric Paris 		goto out;
445b7ba8371SEric Paris 	}
446dead537dSEric Paris 
447b7ba8371SEric Paris 	/*
448000285deSEric Paris 	 * We found an mark in the idr at the right wd, but it's
449000285deSEric Paris 	 * not the mark we were told to remove.  eparis seriously
450b7ba8371SEric Paris 	 * fucked up somewhere.
451b7ba8371SEric Paris 	 */
452000285deSEric Paris 	if (unlikely(found_i_mark != i_mark)) {
453000285deSEric Paris 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
454000285deSEric Paris 			"mark->inode=%p found_i_mark=%p found_i_mark->wd=%d "
455000285deSEric Paris 			"found_i_mark->group=%p found_i_mark->inode=%p\n",
456000285deSEric Paris 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group,
4570809ab69SJan Kara 			i_mark->fsn_mark.inode, found_i_mark, found_i_mark->wd,
458000285deSEric Paris 			found_i_mark->fsn_mark.group,
4590809ab69SJan Kara 			found_i_mark->fsn_mark.inode);
460b7ba8371SEric Paris 		goto out;
461b7ba8371SEric Paris 	}
462dead537dSEric Paris 
463b7ba8371SEric Paris 	/*
464b7ba8371SEric Paris 	 * One ref for being in the idr
465b7ba8371SEric Paris 	 * one ref held by the caller trying to kill us
466b7ba8371SEric Paris 	 * one ref grabbed by inotify_idr_find
467b7ba8371SEric Paris 	 */
468000285deSEric Paris 	if (unlikely(atomic_read(&i_mark->fsn_mark.refcnt) < 3)) {
469000285deSEric Paris 		printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
470000285deSEric Paris 			" i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
4710809ab69SJan Kara 			i_mark->fsn_mark.group, i_mark->fsn_mark.inode);
472b7ba8371SEric Paris 		/* we can't really recover with bad ref cnting.. */
473b7ba8371SEric Paris 		BUG();
474b7ba8371SEric Paris 	}
475b7ba8371SEric Paris 
476000285deSEric Paris 	do_inotify_remove_from_idr(group, i_mark);
477dead537dSEric Paris out:
478b7ba8371SEric Paris 	/* match the ref taken by inotify_idr_find_locked() */
479000285deSEric Paris 	if (found_i_mark)
480000285deSEric Paris 		fsnotify_put_mark(&found_i_mark->fsn_mark);
481000285deSEric Paris 	i_mark->wd = -1;
482b7ba8371SEric Paris 	spin_unlock(idr_lock);
483dead537dSEric Paris }
484dead537dSEric Paris 
48563c882a0SEric Paris /*
486dead537dSEric Paris  * Send IN_IGNORED for this wd, remove this wd from the idr.
48763c882a0SEric Paris  */
488000285deSEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
489528da3e9SEric Paris 				    struct fsnotify_group *group)
49063c882a0SEric Paris {
491000285deSEric Paris 	struct inotify_inode_mark *i_mark;
4927053aee2SJan Kara 
4937053aee2SJan Kara 	/* Queue ignore event for the watch */
4947053aee2SJan Kara 	inotify_handle_event(group, NULL, fsn_mark, NULL, FS_IN_IGNORED,
49545a22f4cSJan Kara 			     NULL, FSNOTIFY_EVENT_NONE, NULL, 0);
49663c882a0SEric Paris 
4978b99c3ccSLino Sanfilippo 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
498000285deSEric Paris 	/* remove this mark from the idr */
499000285deSEric Paris 	inotify_remove_from_idr(group, i_mark);
50063c882a0SEric Paris 
501*1cce1eeaSNikolay Borisov 	dec_inotify_watches(group->inotify_data.ucounts);
50263c882a0SEric Paris }
50363c882a0SEric Paris 
50463c882a0SEric Paris /* ding dong the mark is dead */
505000285deSEric Paris static void inotify_free_mark(struct fsnotify_mark *fsn_mark)
50663c882a0SEric Paris {
507000285deSEric Paris 	struct inotify_inode_mark *i_mark;
50831ddd326SEric Paris 
509000285deSEric Paris 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
51063c882a0SEric Paris 
511000285deSEric Paris 	kmem_cache_free(inotify_inode_mark_cachep, i_mark);
51263c882a0SEric Paris }
51363c882a0SEric Paris 
51452cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group,
51552cef755SEric Paris 					 struct inode *inode,
51652cef755SEric Paris 					 u32 arg)
51763c882a0SEric Paris {
518000285deSEric Paris 	struct fsnotify_mark *fsn_mark;
519000285deSEric Paris 	struct inotify_inode_mark *i_mark;
52063c882a0SEric Paris 	__u32 old_mask, new_mask;
52152cef755SEric Paris 	__u32 mask;
52252cef755SEric Paris 	int add = (arg & IN_MASK_ADD);
52352cef755SEric Paris 	int ret;
52463c882a0SEric Paris 
52563c882a0SEric Paris 	mask = inotify_arg_to_mask(arg);
52663c882a0SEric Paris 
5275444e298SEric Paris 	fsn_mark = fsnotify_find_inode_mark(group, inode);
528000285deSEric Paris 	if (!fsn_mark)
52952cef755SEric Paris 		return -ENOENT;
53052cef755SEric Paris 
531000285deSEric Paris 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
53275fe2b26SEric Paris 
533000285deSEric Paris 	spin_lock(&fsn_mark->lock);
53463c882a0SEric Paris 
535000285deSEric Paris 	old_mask = fsn_mark->mask;
53690b1e7a5SEric Paris 	if (add)
53790b1e7a5SEric Paris 		fsnotify_set_mark_mask_locked(fsn_mark, (fsn_mark->mask | mask));
53890b1e7a5SEric Paris 	else
53990b1e7a5SEric Paris 		fsnotify_set_mark_mask_locked(fsn_mark, mask);
540000285deSEric Paris 	new_mask = fsn_mark->mask;
54163c882a0SEric Paris 
542000285deSEric Paris 	spin_unlock(&fsn_mark->lock);
54363c882a0SEric Paris 
54463c882a0SEric Paris 	if (old_mask != new_mask) {
54563c882a0SEric Paris 		/* more bits in old than in new? */
54663c882a0SEric Paris 		int dropped = (old_mask & ~new_mask);
547000285deSEric Paris 		/* more bits in this fsn_mark than the inode's mask? */
54863c882a0SEric Paris 		int do_inode = (new_mask & ~inode->i_fsnotify_mask);
54963c882a0SEric Paris 
550000285deSEric Paris 		/* update the inode with this new fsn_mark */
55163c882a0SEric Paris 		if (dropped || do_inode)
55263c882a0SEric Paris 			fsnotify_recalc_inode_mask(inode);
55363c882a0SEric Paris 
55463c882a0SEric Paris 	}
55563c882a0SEric Paris 
55652cef755SEric Paris 	/* return the wd */
557000285deSEric Paris 	ret = i_mark->wd;
55852cef755SEric Paris 
559d0775441SEric Paris 	/* match the get from fsnotify_find_mark() */
560000285deSEric Paris 	fsnotify_put_mark(fsn_mark);
56175fe2b26SEric Paris 
56252cef755SEric Paris 	return ret;
56363c882a0SEric Paris }
5647e790dd5SEric Paris 
56552cef755SEric Paris static int inotify_new_watch(struct fsnotify_group *group,
56652cef755SEric Paris 			     struct inode *inode,
56752cef755SEric Paris 			     u32 arg)
56852cef755SEric Paris {
569000285deSEric Paris 	struct inotify_inode_mark *tmp_i_mark;
57052cef755SEric Paris 	__u32 mask;
57152cef755SEric Paris 	int ret;
572b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
573b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
57452cef755SEric Paris 
57552cef755SEric Paris 	mask = inotify_arg_to_mask(arg);
57652cef755SEric Paris 
577000285deSEric Paris 	tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
578000285deSEric Paris 	if (unlikely(!tmp_i_mark))
57952cef755SEric Paris 		return -ENOMEM;
58052cef755SEric Paris 
581000285deSEric Paris 	fsnotify_init_mark(&tmp_i_mark->fsn_mark, inotify_free_mark);
582000285deSEric Paris 	tmp_i_mark->fsn_mark.mask = mask;
583000285deSEric Paris 	tmp_i_mark->wd = -1;
58452cef755SEric Paris 
585a66c04b4SJeff Layton 	ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
586b7ba8371SEric Paris 	if (ret)
58752cef755SEric Paris 		goto out_err;
58852cef755SEric Paris 
589*1cce1eeaSNikolay Borisov 	/* increment the number of watches the user has */
590*1cce1eeaSNikolay Borisov 	if (!inc_inotify_watches(group->inotify_data.ucounts)) {
591*1cce1eeaSNikolay Borisov 		inotify_remove_from_idr(group, tmp_i_mark);
592*1cce1eeaSNikolay Borisov 		ret = -ENOSPC;
593*1cce1eeaSNikolay Borisov 		goto out_err;
594*1cce1eeaSNikolay Borisov 	}
595*1cce1eeaSNikolay Borisov 
59652cef755SEric Paris 	/* we are on the idr, now get on the inode */
597e1e5a9f8SLino Sanfilippo 	ret = fsnotify_add_mark_locked(&tmp_i_mark->fsn_mark, group, inode,
598e1e5a9f8SLino Sanfilippo 				       NULL, 0);
59952cef755SEric Paris 	if (ret) {
60052cef755SEric Paris 		/* we failed to get on the inode, get off the idr */
601000285deSEric Paris 		inotify_remove_from_idr(group, tmp_i_mark);
60252cef755SEric Paris 		goto out_err;
60352cef755SEric Paris 	}
60452cef755SEric Paris 
60552cef755SEric Paris 
606000285deSEric Paris 	/* return the watch descriptor for this new mark */
607000285deSEric Paris 	ret = tmp_i_mark->wd;
60852cef755SEric Paris 
60952cef755SEric Paris out_err:
610000285deSEric Paris 	/* match the ref from fsnotify_init_mark() */
611000285deSEric Paris 	fsnotify_put_mark(&tmp_i_mark->fsn_mark);
61252cef755SEric Paris 
61352cef755SEric Paris 	return ret;
61452cef755SEric Paris }
61552cef755SEric Paris 
61652cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
61752cef755SEric Paris {
61852cef755SEric Paris 	int ret = 0;
61952cef755SEric Paris 
620e1e5a9f8SLino Sanfilippo 	mutex_lock(&group->mark_mutex);
62152cef755SEric Paris 	/* try to update and existing watch with the new arg */
62252cef755SEric Paris 	ret = inotify_update_existing_watch(group, inode, arg);
62352cef755SEric Paris 	/* no mark present, try to add a new one */
62452cef755SEric Paris 	if (ret == -ENOENT)
62552cef755SEric Paris 		ret = inotify_new_watch(group, inode, arg);
626e1e5a9f8SLino Sanfilippo 	mutex_unlock(&group->mark_mutex);
62752cef755SEric Paris 
62863c882a0SEric Paris 	return ret;
62963c882a0SEric Paris }
63063c882a0SEric Paris 
631d0de4dc5SEric Paris static struct fsnotify_group *inotify_new_group(unsigned int max_events)
63263c882a0SEric Paris {
63363c882a0SEric Paris 	struct fsnotify_group *group;
634ff57cd58SJan Kara 	struct inotify_event_info *oevent;
63563c882a0SEric Paris 
6360d2e2a1dSEric Paris 	group = fsnotify_alloc_group(&inotify_fsnotify_ops);
63763c882a0SEric Paris 	if (IS_ERR(group))
63863c882a0SEric Paris 		return group;
63963c882a0SEric Paris 
640ff57cd58SJan Kara 	oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
641ff57cd58SJan Kara 	if (unlikely(!oevent)) {
642ff57cd58SJan Kara 		fsnotify_destroy_group(group);
643ff57cd58SJan Kara 		return ERR_PTR(-ENOMEM);
644ff57cd58SJan Kara 	}
645ff57cd58SJan Kara 	group->overflow_event = &oevent->fse;
646ff57cd58SJan Kara 	fsnotify_init_event(group->overflow_event, NULL, FS_Q_OVERFLOW);
647ff57cd58SJan Kara 	oevent->wd = -1;
648ff57cd58SJan Kara 	oevent->sync_cookie = 0;
649ff57cd58SJan Kara 	oevent->name_len = 0;
650ff57cd58SJan Kara 
65163c882a0SEric Paris 	group->max_events = max_events;
65263c882a0SEric Paris 
65363c882a0SEric Paris 	spin_lock_init(&group->inotify_data.idr_lock);
65463c882a0SEric Paris 	idr_init(&group->inotify_data.idr);
655*1cce1eeaSNikolay Borisov 	group->inotify_data.ucounts = inc_ucount(current_user_ns(),
656*1cce1eeaSNikolay Borisov 						 current_euid(),
657*1cce1eeaSNikolay Borisov 						 UCOUNT_INOTIFY_INSTANCES);
658d0de4dc5SEric Paris 
659*1cce1eeaSNikolay Borisov 	if (!group->inotify_data.ucounts) {
660d8153d4dSLino Sanfilippo 		fsnotify_destroy_group(group);
661d0de4dc5SEric Paris 		return ERR_PTR(-EMFILE);
662d0de4dc5SEric Paris 	}
66363c882a0SEric Paris 
66463c882a0SEric Paris 	return group;
66563c882a0SEric Paris }
66663c882a0SEric Paris 
66763c882a0SEric Paris 
66863c882a0SEric Paris /* inotify syscalls */
669938bb9f5SHeiko Carstens SYSCALL_DEFINE1(inotify_init1, int, flags)
670272eb014SEric Paris {
67163c882a0SEric Paris 	struct fsnotify_group *group;
672c44dcc56SAl Viro 	int ret;
673272eb014SEric Paris 
674272eb014SEric Paris 	/* Check the IN_* constants for consistency.  */
675272eb014SEric Paris 	BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
676272eb014SEric Paris 	BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
677272eb014SEric Paris 
678272eb014SEric Paris 	if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
679272eb014SEric Paris 		return -EINVAL;
680272eb014SEric Paris 
68163c882a0SEric Paris 	/* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
682d0de4dc5SEric Paris 	group = inotify_new_group(inotify_max_queued_events);
683d0de4dc5SEric Paris 	if (IS_ERR(group))
684d0de4dc5SEric Paris 		return PTR_ERR(group);
685825f9692SAl Viro 
686c44dcc56SAl Viro 	ret = anon_inode_getfd("inotify", &inotify_fops, group,
687c44dcc56SAl Viro 				  O_RDONLY | flags);
688d0de4dc5SEric Paris 	if (ret < 0)
689d8153d4dSLino Sanfilippo 		fsnotify_destroy_group(group);
690d0de4dc5SEric Paris 
691272eb014SEric Paris 	return ret;
692272eb014SEric Paris }
693272eb014SEric Paris 
694938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init)
695272eb014SEric Paris {
696272eb014SEric Paris 	return sys_inotify_init1(0);
697272eb014SEric Paris }
698272eb014SEric Paris 
6992e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
7002e4d0924SHeiko Carstens 		u32, mask)
701272eb014SEric Paris {
70263c882a0SEric Paris 	struct fsnotify_group *group;
703272eb014SEric Paris 	struct inode *inode;
704272eb014SEric Paris 	struct path path;
7052903ff01SAl Viro 	struct fd f;
7062903ff01SAl Viro 	int ret;
707272eb014SEric Paris 	unsigned flags = 0;
708272eb014SEric Paris 
709d30e2c05SDave Hansen 	/*
710d30e2c05SDave Hansen 	 * We share a lot of code with fs/dnotify.  We also share
711d30e2c05SDave Hansen 	 * the bit layout between inotify's IN_* and the fsnotify
712d30e2c05SDave Hansen 	 * FS_*.  This check ensures that only the inotify IN_*
713d30e2c05SDave Hansen 	 * bits get passed in and set in watches/events.
714d30e2c05SDave Hansen 	 */
715d30e2c05SDave Hansen 	if (unlikely(mask & ~ALL_INOTIFY_BITS))
716d30e2c05SDave Hansen 		return -EINVAL;
717d30e2c05SDave Hansen 	/*
718d30e2c05SDave Hansen 	 * Require at least one valid bit set in the mask.
719d30e2c05SDave Hansen 	 * Without _something_ set, we would have no events to
720d30e2c05SDave Hansen 	 * watch for.
721d30e2c05SDave Hansen 	 */
72204df32faSZhao Hongjiang 	if (unlikely(!(mask & ALL_INOTIFY_BITS)))
72304df32faSZhao Hongjiang 		return -EINVAL;
72404df32faSZhao Hongjiang 
7252903ff01SAl Viro 	f = fdget(fd);
7262903ff01SAl Viro 	if (unlikely(!f.file))
727272eb014SEric Paris 		return -EBADF;
728272eb014SEric Paris 
729272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
7302903ff01SAl Viro 	if (unlikely(f.file->f_op != &inotify_fops)) {
731272eb014SEric Paris 		ret = -EINVAL;
732272eb014SEric Paris 		goto fput_and_out;
733272eb014SEric Paris 	}
734272eb014SEric Paris 
735272eb014SEric Paris 	if (!(mask & IN_DONT_FOLLOW))
736272eb014SEric Paris 		flags |= LOOKUP_FOLLOW;
737272eb014SEric Paris 	if (mask & IN_ONLYDIR)
738272eb014SEric Paris 		flags |= LOOKUP_DIRECTORY;
739272eb014SEric Paris 
74063c882a0SEric Paris 	ret = inotify_find_inode(pathname, &path, flags);
74163c882a0SEric Paris 	if (ret)
742272eb014SEric Paris 		goto fput_and_out;
743272eb014SEric Paris 
74463c882a0SEric Paris 	/* inode held in place by reference to path; group by fget on fd */
745272eb014SEric Paris 	inode = path.dentry->d_inode;
7462903ff01SAl Viro 	group = f.file->private_data;
747272eb014SEric Paris 
74863c882a0SEric Paris 	/* create/update an inode mark */
74963c882a0SEric Paris 	ret = inotify_update_watch(group, inode, mask);
750272eb014SEric Paris 	path_put(&path);
751272eb014SEric Paris fput_and_out:
7522903ff01SAl Viro 	fdput(f);
753272eb014SEric Paris 	return ret;
754272eb014SEric Paris }
755272eb014SEric Paris 
7562e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
757272eb014SEric Paris {
75863c882a0SEric Paris 	struct fsnotify_group *group;
759000285deSEric Paris 	struct inotify_inode_mark *i_mark;
7602903ff01SAl Viro 	struct fd f;
7612903ff01SAl Viro 	int ret = 0;
762272eb014SEric Paris 
7632903ff01SAl Viro 	f = fdget(fd);
7642903ff01SAl Viro 	if (unlikely(!f.file))
765272eb014SEric Paris 		return -EBADF;
766272eb014SEric Paris 
767272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
768272eb014SEric Paris 	ret = -EINVAL;
7692903ff01SAl Viro 	if (unlikely(f.file->f_op != &inotify_fops))
770272eb014SEric Paris 		goto out;
771272eb014SEric Paris 
7722903ff01SAl Viro 	group = f.file->private_data;
773272eb014SEric Paris 
77463c882a0SEric Paris 	ret = -EINVAL;
775000285deSEric Paris 	i_mark = inotify_idr_find(group, wd);
776000285deSEric Paris 	if (unlikely(!i_mark))
77763c882a0SEric Paris 		goto out;
77863c882a0SEric Paris 
779b7ba8371SEric Paris 	ret = 0;
780b7ba8371SEric Paris 
781e2a29943SLino Sanfilippo 	fsnotify_destroy_mark(&i_mark->fsn_mark, group);
782b7ba8371SEric Paris 
783b7ba8371SEric Paris 	/* match ref taken by inotify_idr_find */
784000285deSEric Paris 	fsnotify_put_mark(&i_mark->fsn_mark);
785272eb014SEric Paris 
786272eb014SEric Paris out:
7872903ff01SAl Viro 	fdput(f);
788272eb014SEric Paris 	return ret;
789272eb014SEric Paris }
790272eb014SEric Paris 
791272eb014SEric Paris /*
792ae0e47f0SJustin P. Mattock  * inotify_user_setup - Our initialization function.  Note that we cannot return
793272eb014SEric Paris  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
794272eb014SEric Paris  * must result in panic().
795272eb014SEric Paris  */
796272eb014SEric Paris static int __init inotify_user_setup(void)
797272eb014SEric Paris {
798f874e1acSEric Paris 	BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
799f874e1acSEric Paris 	BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
800f874e1acSEric Paris 	BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
801f874e1acSEric Paris 	BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
802f874e1acSEric Paris 	BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
803f874e1acSEric Paris 	BUILD_BUG_ON(IN_OPEN != FS_OPEN);
804f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
805f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
806f874e1acSEric Paris 	BUILD_BUG_ON(IN_CREATE != FS_CREATE);
807f874e1acSEric Paris 	BUILD_BUG_ON(IN_DELETE != FS_DELETE);
808f874e1acSEric Paris 	BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
809f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
810f874e1acSEric Paris 	BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
811f874e1acSEric Paris 	BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
812f874e1acSEric Paris 	BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
813f874e1acSEric Paris 	BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
814b29866aaSEric Paris 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
815f874e1acSEric Paris 	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
816f874e1acSEric Paris 
817f874e1acSEric Paris 	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
818f874e1acSEric Paris 
819000285deSEric Paris 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
82063c882a0SEric Paris 
821272eb014SEric Paris 	inotify_max_queued_events = 16384;
822*1cce1eeaSNikolay Borisov 	init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
823*1cce1eeaSNikolay Borisov 	init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
824272eb014SEric Paris 
825272eb014SEric Paris 	return 0;
826272eb014SEric Paris }
827c013d5a4SPaul Gortmaker fs_initcall(inotify_user_setup);
828