xref: /linux/fs/notify/inotify/inotify_user.c (revision d46eb14b735b11927d4bdc2d1854c311af19de6d)
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 */
33174cd4b1SIngo Molnar #include <linux/sched/signal.h>
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>
41*d46eb14bSShakeel Butt #include <linux/memcontrol.h>
4263c882a0SEric Paris 
4363c882a0SEric Paris #include "inotify.h"
44be77196bSCyrill Gorcunov #include "../fdinfo.h"
45272eb014SEric Paris 
46272eb014SEric Paris #include <asm/ioctls.h>
47272eb014SEric Paris 
481cce1eeaSNikolay Borisov /* configurable via /proc/sys/fs/inotify/ */
49272eb014SEric Paris static int inotify_max_queued_events __read_mostly;
5063c882a0SEric Paris 
51054c636eSJan Kara struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
52272eb014SEric Paris 
53272eb014SEric Paris #ifdef CONFIG_SYSCTL
54272eb014SEric Paris 
55272eb014SEric Paris #include <linux/sysctl.h>
56272eb014SEric Paris 
57272eb014SEric Paris static int zero;
58272eb014SEric Paris 
5992f778ddSJoe Perches struct ctl_table inotify_table[] = {
60272eb014SEric Paris 	{
61272eb014SEric Paris 		.procname	= "max_user_instances",
621cce1eeaSNikolay Borisov 		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
63272eb014SEric Paris 		.maxlen		= sizeof(int),
64272eb014SEric Paris 		.mode		= 0644,
656d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
66272eb014SEric Paris 		.extra1		= &zero,
67272eb014SEric Paris 	},
68272eb014SEric Paris 	{
69272eb014SEric Paris 		.procname	= "max_user_watches",
701cce1eeaSNikolay Borisov 		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
71272eb014SEric Paris 		.maxlen		= sizeof(int),
72272eb014SEric Paris 		.mode		= 0644,
736d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
74272eb014SEric Paris 		.extra1		= &zero,
75272eb014SEric Paris 	},
76272eb014SEric Paris 	{
77272eb014SEric Paris 		.procname	= "max_queued_events",
78272eb014SEric Paris 		.data		= &inotify_max_queued_events,
79272eb014SEric Paris 		.maxlen		= sizeof(int),
80272eb014SEric Paris 		.mode		= 0644,
816d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
82272eb014SEric Paris 		.extra1		= &zero
83272eb014SEric Paris 	},
84ab09203eSEric W. Biederman 	{ }
85272eb014SEric Paris };
86272eb014SEric Paris #endif /* CONFIG_SYSCTL */
87272eb014SEric Paris 
8863c882a0SEric Paris static inline __u32 inotify_arg_to_mask(u32 arg)
89272eb014SEric Paris {
9063c882a0SEric Paris 	__u32 mask;
9163c882a0SEric Paris 
92611da04fSEric Paris 	/*
93611da04fSEric Paris 	 * everything should accept their own ignored, cares about children,
94611da04fSEric Paris 	 * and should receive events when the inode is unmounted
95611da04fSEric Paris 	 */
96611da04fSEric Paris 	mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
9763c882a0SEric Paris 
9863c882a0SEric Paris 	/* mask off the flags used to open the fd */
998c1934c8SEric Paris 	mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
10063c882a0SEric Paris 
10163c882a0SEric Paris 	return mask;
102272eb014SEric Paris }
103272eb014SEric Paris 
10463c882a0SEric Paris static inline u32 inotify_mask_to_arg(__u32 mask)
105272eb014SEric Paris {
10663c882a0SEric Paris 	return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
10763c882a0SEric Paris 		       IN_Q_OVERFLOW);
108272eb014SEric Paris }
109272eb014SEric Paris 
11063c882a0SEric Paris /* intofiy userspace file descriptor functions */
111076ccb76SAl Viro static __poll_t inotify_poll(struct file *file, poll_table *wait)
112272eb014SEric Paris {
11363c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
114076ccb76SAl Viro 	__poll_t ret = 0;
115272eb014SEric Paris 
11663c882a0SEric Paris 	poll_wait(file, &group->notification_waitq, wait);
117c21dbe20SJan Kara 	spin_lock(&group->notification_lock);
11863c882a0SEric Paris 	if (!fsnotify_notify_queue_is_empty(group))
119a9a08845SLinus Torvalds 		ret = EPOLLIN | EPOLLRDNORM;
120c21dbe20SJan Kara 	spin_unlock(&group->notification_lock);
121272eb014SEric Paris 
122272eb014SEric Paris 	return ret;
123272eb014SEric Paris }
124272eb014SEric Paris 
1257053aee2SJan Kara static int round_event_name_len(struct fsnotify_event *fsn_event)
126e9fe6904SJan Kara {
1277053aee2SJan Kara 	struct inotify_event_info *event;
1287053aee2SJan Kara 
1297053aee2SJan Kara 	event = INOTIFY_E(fsn_event);
130e9fe6904SJan Kara 	if (!event->name_len)
131e9fe6904SJan Kara 		return 0;
132e9fe6904SJan Kara 	return roundup(event->name_len + 1, sizeof(struct inotify_event));
133e9fe6904SJan Kara }
134e9fe6904SJan Kara 
1353632dee2SVegard Nossum /*
1363632dee2SVegard Nossum  * Get an inotify_kernel_event if one exists and is small
1373632dee2SVegard Nossum  * enough to fit in "count". Return an error pointer if
1383632dee2SVegard Nossum  * not large enough.
1393632dee2SVegard Nossum  *
140c21dbe20SJan Kara  * Called with the group->notification_lock held.
1413632dee2SVegard Nossum  */
14263c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
1433632dee2SVegard Nossum 					    size_t count)
1443632dee2SVegard Nossum {
1453632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
14663c882a0SEric Paris 	struct fsnotify_event *event;
1473632dee2SVegard Nossum 
14863c882a0SEric Paris 	if (fsnotify_notify_queue_is_empty(group))
1493632dee2SVegard Nossum 		return NULL;
1503632dee2SVegard Nossum 
1518ba8fa91SJan Kara 	event = fsnotify_peek_first_event(group);
15263c882a0SEric Paris 
1535ba08e2eSEric Paris 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
1545ba08e2eSEric Paris 
155e9fe6904SJan Kara 	event_size += round_event_name_len(event);
1563632dee2SVegard Nossum 	if (event_size > count)
1573632dee2SVegard Nossum 		return ERR_PTR(-EINVAL);
1583632dee2SVegard Nossum 
159c21dbe20SJan Kara 	/* held the notification_lock the whole time, so this is the
16063c882a0SEric Paris 	 * same event we peeked above */
1618ba8fa91SJan Kara 	fsnotify_remove_first_event(group);
16263c882a0SEric Paris 
16363c882a0SEric Paris 	return event;
1643632dee2SVegard Nossum }
1653632dee2SVegard Nossum 
1663632dee2SVegard Nossum /*
1673632dee2SVegard Nossum  * Copy an event to user space, returning how much we copied.
1683632dee2SVegard Nossum  *
1693632dee2SVegard Nossum  * We already checked that the event size is smaller than the
1703632dee2SVegard Nossum  * buffer we had in "get_one_event()" above.
1713632dee2SVegard Nossum  */
17263c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group,
1737053aee2SJan Kara 				  struct fsnotify_event *fsn_event,
1743632dee2SVegard Nossum 				  char __user *buf)
1753632dee2SVegard Nossum {
17663c882a0SEric Paris 	struct inotify_event inotify_event;
1777053aee2SJan Kara 	struct inotify_event_info *event;
1783632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
179e9fe6904SJan Kara 	size_t name_len;
180e9fe6904SJan Kara 	size_t pad_name_len;
1813632dee2SVegard Nossum 
1827053aee2SJan Kara 	pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
1835ba08e2eSEric Paris 
1847053aee2SJan Kara 	event = INOTIFY_E(fsn_event);
185e9fe6904SJan Kara 	name_len = event->name_len;
186b962e731SBrian Rogers 	/*
187e9fe6904SJan Kara 	 * round up name length so it is a multiple of event_size
1880db501bdSEric W. Biederman 	 * plus an extra byte for the terminating '\0'.
1890db501bdSEric W. Biederman 	 */
1907053aee2SJan Kara 	pad_name_len = round_event_name_len(fsn_event);
191e9fe6904SJan Kara 	inotify_event.len = pad_name_len;
1927053aee2SJan Kara 	inotify_event.mask = inotify_mask_to_arg(fsn_event->mask);
1937053aee2SJan Kara 	inotify_event.wd = event->wd;
19463c882a0SEric Paris 	inotify_event.cookie = event->sync_cookie;
19563c882a0SEric Paris 
19663c882a0SEric Paris 	/* send the main event */
19763c882a0SEric Paris 	if (copy_to_user(buf, &inotify_event, event_size))
1983632dee2SVegard Nossum 		return -EFAULT;
1993632dee2SVegard Nossum 
2003632dee2SVegard Nossum 	buf += event_size;
2013632dee2SVegard Nossum 
20263c882a0SEric Paris 	/*
20363c882a0SEric Paris 	 * fsnotify only stores the pathname, so here we have to send the pathname
20463c882a0SEric Paris 	 * and then pad that pathname out to a multiple of sizeof(inotify_event)
205e9fe6904SJan Kara 	 * with zeros.
20663c882a0SEric Paris 	 */
207e9fe6904SJan Kara 	if (pad_name_len) {
20863c882a0SEric Paris 		/* copy the path name */
2097053aee2SJan Kara 		if (copy_to_user(buf, event->name, name_len))
2103632dee2SVegard Nossum 			return -EFAULT;
211e9fe6904SJan Kara 		buf += name_len;
2123632dee2SVegard Nossum 
2130db501bdSEric W. Biederman 		/* fill userspace with 0's */
214e9fe6904SJan Kara 		if (clear_user(buf, pad_name_len - name_len))
21563c882a0SEric Paris 			return -EFAULT;
216e9fe6904SJan Kara 		event_size += pad_name_len;
2173632dee2SVegard Nossum 	}
21863c882a0SEric Paris 
2193632dee2SVegard Nossum 	return event_size;
2203632dee2SVegard Nossum }
2213632dee2SVegard Nossum 
222272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf,
223272eb014SEric Paris 			    size_t count, loff_t *pos)
224272eb014SEric Paris {
22563c882a0SEric Paris 	struct fsnotify_group *group;
22663c882a0SEric Paris 	struct fsnotify_event *kevent;
227272eb014SEric Paris 	char __user *start;
228272eb014SEric Paris 	int ret;
229e23738a7SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
230272eb014SEric Paris 
231272eb014SEric Paris 	start = buf;
23263c882a0SEric Paris 	group = file->private_data;
233272eb014SEric Paris 
234e23738a7SPeter Zijlstra 	add_wait_queue(&group->notification_waitq, &wait);
235272eb014SEric Paris 	while (1) {
236c21dbe20SJan Kara 		spin_lock(&group->notification_lock);
23763c882a0SEric Paris 		kevent = get_one_event(group, count);
238c21dbe20SJan Kara 		spin_unlock(&group->notification_lock);
239272eb014SEric Paris 
2405ba08e2eSEric Paris 		pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
2415ba08e2eSEric Paris 
2423632dee2SVegard Nossum 		if (kevent) {
2433632dee2SVegard Nossum 			ret = PTR_ERR(kevent);
2443632dee2SVegard Nossum 			if (IS_ERR(kevent))
245272eb014SEric Paris 				break;
24663c882a0SEric Paris 			ret = copy_event_to_user(group, kevent, buf);
2477053aee2SJan Kara 			fsnotify_destroy_event(group, kevent);
2483632dee2SVegard Nossum 			if (ret < 0)
2493632dee2SVegard Nossum 				break;
2503632dee2SVegard Nossum 			buf += ret;
2513632dee2SVegard Nossum 			count -= ret;
2523632dee2SVegard Nossum 			continue;
253272eb014SEric Paris 		}
254272eb014SEric Paris 
2553632dee2SVegard Nossum 		ret = -EAGAIN;
2563632dee2SVegard Nossum 		if (file->f_flags & O_NONBLOCK)
257272eb014SEric Paris 			break;
2581ca39ab9SEric Paris 		ret = -ERESTARTSYS;
2593632dee2SVegard Nossum 		if (signal_pending(current))
2603632dee2SVegard Nossum 			break;
2613632dee2SVegard Nossum 
2623632dee2SVegard Nossum 		if (start != buf)
2633632dee2SVegard Nossum 			break;
264272eb014SEric Paris 
265e23738a7SPeter Zijlstra 		wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
266272eb014SEric Paris 	}
267e23738a7SPeter Zijlstra 	remove_wait_queue(&group->notification_waitq, &wait);
268272eb014SEric Paris 
2693632dee2SVegard Nossum 	if (start != buf && ret != -EFAULT)
270272eb014SEric Paris 		ret = buf - start;
271272eb014SEric Paris 	return ret;
272272eb014SEric Paris }
273272eb014SEric Paris 
274272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file)
275272eb014SEric Paris {
27663c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
277272eb014SEric Paris 
2785ba08e2eSEric Paris 	pr_debug("%s: group=%p\n", __func__, group);
2795ba08e2eSEric Paris 
28063c882a0SEric Paris 	/* free this group, matching get was inotify_init->fsnotify_obtain_group */
281d8153d4dSLino Sanfilippo 	fsnotify_destroy_group(group);
282272eb014SEric Paris 
283272eb014SEric Paris 	return 0;
284272eb014SEric Paris }
285272eb014SEric Paris 
286272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd,
287272eb014SEric Paris 			  unsigned long arg)
288272eb014SEric Paris {
28963c882a0SEric Paris 	struct fsnotify_group *group;
2907053aee2SJan Kara 	struct fsnotify_event *fsn_event;
291272eb014SEric Paris 	void __user *p;
292272eb014SEric Paris 	int ret = -ENOTTY;
29363c882a0SEric Paris 	size_t send_len = 0;
294272eb014SEric Paris 
29563c882a0SEric Paris 	group = file->private_data;
296272eb014SEric Paris 	p = (void __user *) arg;
297272eb014SEric Paris 
2985ba08e2eSEric Paris 	pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
2995ba08e2eSEric Paris 
300272eb014SEric Paris 	switch (cmd) {
301272eb014SEric Paris 	case FIONREAD:
302c21dbe20SJan Kara 		spin_lock(&group->notification_lock);
3037053aee2SJan Kara 		list_for_each_entry(fsn_event, &group->notification_list,
3047053aee2SJan Kara 				    list) {
30563c882a0SEric Paris 			send_len += sizeof(struct inotify_event);
3067053aee2SJan Kara 			send_len += round_event_name_len(fsn_event);
30763c882a0SEric Paris 		}
308c21dbe20SJan Kara 		spin_unlock(&group->notification_lock);
30963c882a0SEric Paris 		ret = put_user(send_len, (int __user *) p);
310272eb014SEric Paris 		break;
311e1603b6eSKirill Tkhai #ifdef CONFIG_CHECKPOINT_RESTORE
312e1603b6eSKirill Tkhai 	case INOTIFY_IOC_SETNEXTWD:
313e1603b6eSKirill Tkhai 		ret = -EINVAL;
314e1603b6eSKirill Tkhai 		if (arg >= 1 && arg <= INT_MAX) {
315e1603b6eSKirill Tkhai 			struct inotify_group_private_data *data;
316e1603b6eSKirill Tkhai 
317e1603b6eSKirill Tkhai 			data = &group->inotify_data;
318e1603b6eSKirill Tkhai 			spin_lock(&data->idr_lock);
319e1603b6eSKirill Tkhai 			idr_set_cursor(&data->idr, (unsigned int)arg);
320e1603b6eSKirill Tkhai 			spin_unlock(&data->idr_lock);
321e1603b6eSKirill Tkhai 			ret = 0;
322e1603b6eSKirill Tkhai 		}
323e1603b6eSKirill Tkhai 		break;
324e1603b6eSKirill Tkhai #endif /* CONFIG_CHECKPOINT_RESTORE */
325272eb014SEric Paris 	}
326272eb014SEric Paris 
327272eb014SEric Paris 	return ret;
328272eb014SEric Paris }
329272eb014SEric Paris 
330272eb014SEric Paris static const struct file_operations inotify_fops = {
331be77196bSCyrill Gorcunov 	.show_fdinfo	= inotify_show_fdinfo,
332272eb014SEric Paris 	.poll		= inotify_poll,
333272eb014SEric Paris 	.read		= inotify_read,
3340a6b6bd5SEric Paris 	.fasync		= fsnotify_fasync,
335272eb014SEric Paris 	.release	= inotify_release,
336272eb014SEric Paris 	.unlocked_ioctl	= inotify_ioctl,
337272eb014SEric Paris 	.compat_ioctl	= inotify_ioctl,
3386038f373SArnd Bergmann 	.llseek		= noop_llseek,
339272eb014SEric Paris };
340272eb014SEric Paris 
341272eb014SEric Paris 
34263c882a0SEric Paris /*
34363c882a0SEric Paris  * find_inode - resolve a user-given path to a specific inode
34463c882a0SEric Paris  */
34563c882a0SEric Paris static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
34663c882a0SEric Paris {
34763c882a0SEric Paris 	int error;
34863c882a0SEric Paris 
34963c882a0SEric Paris 	error = user_path_at(AT_FDCWD, dirname, flags, path);
35063c882a0SEric Paris 	if (error)
35163c882a0SEric Paris 		return error;
35263c882a0SEric Paris 	/* you can only watch an inode if you have read permissions on it */
35363c882a0SEric Paris 	error = inode_permission(path->dentry->d_inode, MAY_READ);
35463c882a0SEric Paris 	if (error)
35563c882a0SEric Paris 		path_put(path);
35663c882a0SEric Paris 	return error;
35763c882a0SEric Paris }
35863c882a0SEric Paris 
359b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
360000285deSEric Paris 			      struct inotify_inode_mark *i_mark)
361b7ba8371SEric Paris {
362b7ba8371SEric Paris 	int ret;
363b7ba8371SEric Paris 
3644542da63STejun Heo 	idr_preload(GFP_KERNEL);
365b7ba8371SEric Paris 	spin_lock(idr_lock);
3664542da63STejun Heo 
367a66c04b4SJeff Layton 	ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
3684542da63STejun Heo 	if (ret >= 0) {
369b7ba8371SEric Paris 		/* we added the mark to the idr, take a reference */
3704542da63STejun Heo 		i_mark->wd = ret;
371000285deSEric Paris 		fsnotify_get_mark(&i_mark->fsn_mark);
3727050c488SEric Paris 	}
373b7ba8371SEric Paris 
3744542da63STejun Heo 	spin_unlock(idr_lock);
3754542da63STejun Heo 	idr_preload_end();
3764542da63STejun Heo 	return ret < 0 ? ret : 0;
377b7ba8371SEric Paris }
378b7ba8371SEric Paris 
379000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
380b7ba8371SEric Paris 								int wd)
381b7ba8371SEric Paris {
382b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
383b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
384000285deSEric Paris 	struct inotify_inode_mark *i_mark;
385b7ba8371SEric Paris 
386b7ba8371SEric Paris 	assert_spin_locked(idr_lock);
387b7ba8371SEric Paris 
388000285deSEric Paris 	i_mark = idr_find(idr, wd);
389000285deSEric Paris 	if (i_mark) {
390000285deSEric Paris 		struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
391b7ba8371SEric Paris 
392000285deSEric Paris 		fsnotify_get_mark(fsn_mark);
393b7ba8371SEric Paris 		/* One ref for being in the idr, one ref we just took */
394ab97f873SElena Reshetova 		BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
395b7ba8371SEric Paris 	}
396b7ba8371SEric Paris 
397000285deSEric Paris 	return i_mark;
398b7ba8371SEric Paris }
399b7ba8371SEric Paris 
400000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
401b7ba8371SEric Paris 							 int wd)
402b7ba8371SEric Paris {
403000285deSEric Paris 	struct inotify_inode_mark *i_mark;
404b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
405b7ba8371SEric Paris 
406b7ba8371SEric Paris 	spin_lock(idr_lock);
407000285deSEric Paris 	i_mark = inotify_idr_find_locked(group, wd);
408b7ba8371SEric Paris 	spin_unlock(idr_lock);
409b7ba8371SEric Paris 
410000285deSEric Paris 	return i_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 {
420e7253760SJan Kara 	struct idr *idr = &group->inotify_data.idr;
421b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
422000285deSEric Paris 	struct inotify_inode_mark *found_i_mark = NULL;
423dead537dSEric Paris 	int wd;
4247e790dd5SEric Paris 
425b7ba8371SEric Paris 	spin_lock(idr_lock);
426000285deSEric Paris 	wd = i_mark->wd;
427dead537dSEric Paris 
428b7ba8371SEric Paris 	/*
429000285deSEric Paris 	 * does this i_mark think it is in the idr?  we shouldn't get called
430b7ba8371SEric Paris 	 * if it wasn't....
431b7ba8371SEric Paris 	 */
432b7ba8371SEric Paris 	if (wd == -1) {
43325c829afSJan Kara 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
43425c829afSJan Kara 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
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)) {
44125c829afSJan Kara 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
44225c829afSJan Kara 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
443b7ba8371SEric Paris 		goto out;
444b7ba8371SEric Paris 	}
445dead537dSEric Paris 
446b7ba8371SEric Paris 	/*
447000285deSEric Paris 	 * We found an mark in the idr at the right wd, but it's
448000285deSEric Paris 	 * not the mark we were told to remove.  eparis seriously
449b7ba8371SEric Paris 	 * fucked up somewhere.
450b7ba8371SEric Paris 	 */
451000285deSEric Paris 	if (unlikely(found_i_mark != i_mark)) {
452000285deSEric Paris 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
45325c829afSJan Kara 			"found_i_mark=%p found_i_mark->wd=%d "
45425c829afSJan Kara 			"found_i_mark->group=%p\n", __func__, i_mark,
45525c829afSJan Kara 			i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
45625c829afSJan Kara 			found_i_mark->wd, found_i_mark->fsn_mark.group);
457b7ba8371SEric Paris 		goto out;
458b7ba8371SEric Paris 	}
459dead537dSEric Paris 
460b7ba8371SEric Paris 	/*
461b7ba8371SEric Paris 	 * One ref for being in the idr
462b7ba8371SEric Paris 	 * one ref grabbed by inotify_idr_find
463b7ba8371SEric Paris 	 */
464ab97f873SElena Reshetova 	if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
46525c829afSJan Kara 		printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
46625c829afSJan Kara 			 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
467b7ba8371SEric Paris 		/* we can't really recover with bad ref cnting.. */
468b7ba8371SEric Paris 		BUG();
469b7ba8371SEric Paris 	}
470b7ba8371SEric Paris 
471e7253760SJan Kara 	idr_remove(idr, wd);
472e7253760SJan Kara 	/* Removed from the idr, drop that ref. */
473e7253760SJan Kara 	fsnotify_put_mark(&i_mark->fsn_mark);
474dead537dSEric Paris out:
475e7253760SJan Kara 	i_mark->wd = -1;
476e7253760SJan Kara 	spin_unlock(idr_lock);
477b7ba8371SEric Paris 	/* match the ref taken by inotify_idr_find_locked() */
478000285deSEric Paris 	if (found_i_mark)
479000285deSEric Paris 		fsnotify_put_mark(&found_i_mark->fsn_mark);
480dead537dSEric Paris }
481dead537dSEric Paris 
48263c882a0SEric Paris /*
483dead537dSEric Paris  * Send IN_IGNORED for this wd, remove this wd from the idr.
48463c882a0SEric Paris  */
485000285deSEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
486528da3e9SEric Paris 				    struct fsnotify_group *group)
48763c882a0SEric Paris {
488000285deSEric Paris 	struct inotify_inode_mark *i_mark;
48947d9c7ccSAmir Goldstein 	struct fsnotify_iter_info iter_info = { };
49047d9c7ccSAmir Goldstein 
49147d9c7ccSAmir Goldstein 	fsnotify_iter_set_report_type_mark(&iter_info, FSNOTIFY_OBJ_TYPE_INODE,
49247d9c7ccSAmir Goldstein 					   fsn_mark);
4937053aee2SJan Kara 
4947053aee2SJan Kara 	/* Queue ignore event for the watch */
4955b0457adSAmir Goldstein 	inotify_handle_event(group, NULL, FS_IN_IGNORED, NULL,
4965b0457adSAmir Goldstein 			     FSNOTIFY_EVENT_NONE, NULL, 0, &iter_info);
49763c882a0SEric Paris 
4988b99c3ccSLino Sanfilippo 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
499000285deSEric Paris 	/* remove this mark from the idr */
500000285deSEric Paris 	inotify_remove_from_idr(group, i_mark);
50163c882a0SEric Paris 
5021cce1eeaSNikolay Borisov 	dec_inotify_watches(group->inotify_data.ucounts);
50363c882a0SEric Paris }
50463c882a0SEric Paris 
50552cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group,
50652cef755SEric Paris 					 struct inode *inode,
50752cef755SEric Paris 					 u32 arg)
50863c882a0SEric Paris {
509000285deSEric Paris 	struct fsnotify_mark *fsn_mark;
510000285deSEric Paris 	struct inotify_inode_mark *i_mark;
51163c882a0SEric Paris 	__u32 old_mask, new_mask;
51252cef755SEric Paris 	__u32 mask;
51352cef755SEric Paris 	int add = (arg & IN_MASK_ADD);
51452cef755SEric Paris 	int ret;
51563c882a0SEric Paris 
51663c882a0SEric Paris 	mask = inotify_arg_to_mask(arg);
51763c882a0SEric Paris 
518b1362edfSJan Kara 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
519000285deSEric Paris 	if (!fsn_mark)
52052cef755SEric Paris 		return -ENOENT;
52152cef755SEric Paris 
522000285deSEric Paris 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
52375fe2b26SEric Paris 
524000285deSEric Paris 	spin_lock(&fsn_mark->lock);
525000285deSEric Paris 	old_mask = fsn_mark->mask;
52690b1e7a5SEric Paris 	if (add)
52766d2b81bSJan Kara 		fsn_mark->mask |= mask;
52890b1e7a5SEric Paris 	else
52966d2b81bSJan Kara 		fsn_mark->mask = mask;
530000285deSEric Paris 	new_mask = fsn_mark->mask;
531000285deSEric Paris 	spin_unlock(&fsn_mark->lock);
53263c882a0SEric Paris 
53363c882a0SEric Paris 	if (old_mask != new_mask) {
53463c882a0SEric Paris 		/* more bits in old than in new? */
53563c882a0SEric Paris 		int dropped = (old_mask & ~new_mask);
536000285deSEric Paris 		/* more bits in this fsn_mark than the inode's mask? */
53763c882a0SEric Paris 		int do_inode = (new_mask & ~inode->i_fsnotify_mask);
53863c882a0SEric Paris 
539000285deSEric Paris 		/* update the inode with this new fsn_mark */
54063c882a0SEric Paris 		if (dropped || do_inode)
5418920d273SJan Kara 			fsnotify_recalc_mask(inode->i_fsnotify_marks);
54263c882a0SEric Paris 
54363c882a0SEric Paris 	}
54463c882a0SEric Paris 
54552cef755SEric Paris 	/* return the wd */
546000285deSEric Paris 	ret = i_mark->wd;
54752cef755SEric Paris 
548d0775441SEric Paris 	/* match the get from fsnotify_find_mark() */
549000285deSEric Paris 	fsnotify_put_mark(fsn_mark);
55075fe2b26SEric Paris 
55152cef755SEric Paris 	return ret;
55263c882a0SEric Paris }
5537e790dd5SEric Paris 
55452cef755SEric Paris static int inotify_new_watch(struct fsnotify_group *group,
55552cef755SEric Paris 			     struct inode *inode,
55652cef755SEric Paris 			     u32 arg)
55752cef755SEric Paris {
558000285deSEric Paris 	struct inotify_inode_mark *tmp_i_mark;
55952cef755SEric Paris 	__u32 mask;
56052cef755SEric Paris 	int ret;
561b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
562b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
56352cef755SEric Paris 
56452cef755SEric Paris 	mask = inotify_arg_to_mask(arg);
56552cef755SEric Paris 
566000285deSEric Paris 	tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
567000285deSEric Paris 	if (unlikely(!tmp_i_mark))
56852cef755SEric Paris 		return -ENOMEM;
56952cef755SEric Paris 
570054c636eSJan Kara 	fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
571000285deSEric Paris 	tmp_i_mark->fsn_mark.mask = mask;
572000285deSEric Paris 	tmp_i_mark->wd = -1;
57352cef755SEric Paris 
574a66c04b4SJeff Layton 	ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
575b7ba8371SEric Paris 	if (ret)
57652cef755SEric Paris 		goto out_err;
57752cef755SEric Paris 
5781cce1eeaSNikolay Borisov 	/* increment the number of watches the user has */
5791cce1eeaSNikolay Borisov 	if (!inc_inotify_watches(group->inotify_data.ucounts)) {
5801cce1eeaSNikolay Borisov 		inotify_remove_from_idr(group, tmp_i_mark);
5811cce1eeaSNikolay Borisov 		ret = -ENOSPC;
5821cce1eeaSNikolay Borisov 		goto out_err;
5831cce1eeaSNikolay Borisov 	}
5841cce1eeaSNikolay Borisov 
58552cef755SEric Paris 	/* we are on the idr, now get on the inode */
586b249f5beSAmir Goldstein 	ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
58752cef755SEric Paris 	if (ret) {
58852cef755SEric Paris 		/* we failed to get on the inode, get off the idr */
589000285deSEric Paris 		inotify_remove_from_idr(group, tmp_i_mark);
59052cef755SEric Paris 		goto out_err;
59152cef755SEric Paris 	}
59252cef755SEric Paris 
59352cef755SEric Paris 
594000285deSEric Paris 	/* return the watch descriptor for this new mark */
595000285deSEric Paris 	ret = tmp_i_mark->wd;
59652cef755SEric Paris 
59752cef755SEric Paris out_err:
598000285deSEric Paris 	/* match the ref from fsnotify_init_mark() */
599000285deSEric Paris 	fsnotify_put_mark(&tmp_i_mark->fsn_mark);
60052cef755SEric Paris 
60152cef755SEric Paris 	return ret;
60252cef755SEric Paris }
60352cef755SEric Paris 
60452cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
60552cef755SEric Paris {
60652cef755SEric Paris 	int ret = 0;
60752cef755SEric Paris 
608e1e5a9f8SLino Sanfilippo 	mutex_lock(&group->mark_mutex);
60952cef755SEric Paris 	/* try to update and existing watch with the new arg */
61052cef755SEric Paris 	ret = inotify_update_existing_watch(group, inode, arg);
61152cef755SEric Paris 	/* no mark present, try to add a new one */
61252cef755SEric Paris 	if (ret == -ENOENT)
61352cef755SEric Paris 		ret = inotify_new_watch(group, inode, arg);
614e1e5a9f8SLino Sanfilippo 	mutex_unlock(&group->mark_mutex);
61552cef755SEric Paris 
61663c882a0SEric Paris 	return ret;
61763c882a0SEric Paris }
61863c882a0SEric Paris 
619d0de4dc5SEric Paris static struct fsnotify_group *inotify_new_group(unsigned int max_events)
62063c882a0SEric Paris {
62163c882a0SEric Paris 	struct fsnotify_group *group;
622ff57cd58SJan Kara 	struct inotify_event_info *oevent;
62363c882a0SEric Paris 
6240d2e2a1dSEric Paris 	group = fsnotify_alloc_group(&inotify_fsnotify_ops);
62563c882a0SEric Paris 	if (IS_ERR(group))
62663c882a0SEric Paris 		return group;
62763c882a0SEric Paris 
628ff57cd58SJan Kara 	oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
629ff57cd58SJan Kara 	if (unlikely(!oevent)) {
630ff57cd58SJan Kara 		fsnotify_destroy_group(group);
631ff57cd58SJan Kara 		return ERR_PTR(-ENOMEM);
632ff57cd58SJan Kara 	}
633ff57cd58SJan Kara 	group->overflow_event = &oevent->fse;
634ff57cd58SJan Kara 	fsnotify_init_event(group->overflow_event, NULL, FS_Q_OVERFLOW);
635ff57cd58SJan Kara 	oevent->wd = -1;
636ff57cd58SJan Kara 	oevent->sync_cookie = 0;
637ff57cd58SJan Kara 	oevent->name_len = 0;
638ff57cd58SJan Kara 
63963c882a0SEric Paris 	group->max_events = max_events;
640*d46eb14bSShakeel Butt 	group->memcg = get_mem_cgroup_from_mm(current->mm);
64163c882a0SEric Paris 
64263c882a0SEric Paris 	spin_lock_init(&group->inotify_data.idr_lock);
64363c882a0SEric Paris 	idr_init(&group->inotify_data.idr);
6441cce1eeaSNikolay Borisov 	group->inotify_data.ucounts = inc_ucount(current_user_ns(),
6451cce1eeaSNikolay Borisov 						 current_euid(),
6461cce1eeaSNikolay Borisov 						 UCOUNT_INOTIFY_INSTANCES);
647d0de4dc5SEric Paris 
6481cce1eeaSNikolay Borisov 	if (!group->inotify_data.ucounts) {
649d8153d4dSLino Sanfilippo 		fsnotify_destroy_group(group);
650d0de4dc5SEric Paris 		return ERR_PTR(-EMFILE);
651d0de4dc5SEric Paris 	}
65263c882a0SEric Paris 
65363c882a0SEric Paris 	return group;
65463c882a0SEric Paris }
65563c882a0SEric Paris 
65663c882a0SEric Paris 
65763c882a0SEric Paris /* inotify syscalls */
658d0d89d1eSDominik Brodowski static int do_inotify_init(int flags)
659272eb014SEric Paris {
66063c882a0SEric Paris 	struct fsnotify_group *group;
661c44dcc56SAl Viro 	int ret;
662272eb014SEric Paris 
663272eb014SEric Paris 	/* Check the IN_* constants for consistency.  */
664272eb014SEric Paris 	BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
665272eb014SEric Paris 	BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
666272eb014SEric Paris 
667272eb014SEric Paris 	if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
668272eb014SEric Paris 		return -EINVAL;
669272eb014SEric Paris 
67063c882a0SEric Paris 	/* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
671d0de4dc5SEric Paris 	group = inotify_new_group(inotify_max_queued_events);
672d0de4dc5SEric Paris 	if (IS_ERR(group))
673d0de4dc5SEric Paris 		return PTR_ERR(group);
674825f9692SAl Viro 
675c44dcc56SAl Viro 	ret = anon_inode_getfd("inotify", &inotify_fops, group,
676c44dcc56SAl Viro 				  O_RDONLY | flags);
677d0de4dc5SEric Paris 	if (ret < 0)
678d8153d4dSLino Sanfilippo 		fsnotify_destroy_group(group);
679d0de4dc5SEric Paris 
680272eb014SEric Paris 	return ret;
681272eb014SEric Paris }
682272eb014SEric Paris 
683d0d89d1eSDominik Brodowski SYSCALL_DEFINE1(inotify_init1, int, flags)
684d0d89d1eSDominik Brodowski {
685d0d89d1eSDominik Brodowski 	return do_inotify_init(flags);
686d0d89d1eSDominik Brodowski }
687d0d89d1eSDominik Brodowski 
688938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init)
689272eb014SEric Paris {
690d0d89d1eSDominik Brodowski 	return do_inotify_init(0);
691272eb014SEric Paris }
692272eb014SEric Paris 
6932e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
6942e4d0924SHeiko Carstens 		u32, mask)
695272eb014SEric Paris {
69663c882a0SEric Paris 	struct fsnotify_group *group;
697272eb014SEric Paris 	struct inode *inode;
698272eb014SEric Paris 	struct path path;
6992903ff01SAl Viro 	struct fd f;
7002903ff01SAl Viro 	int ret;
701272eb014SEric Paris 	unsigned flags = 0;
702272eb014SEric Paris 
703d30e2c05SDave Hansen 	/*
704d30e2c05SDave Hansen 	 * We share a lot of code with fs/dnotify.  We also share
705d30e2c05SDave Hansen 	 * the bit layout between inotify's IN_* and the fsnotify
706d30e2c05SDave Hansen 	 * FS_*.  This check ensures that only the inotify IN_*
707d30e2c05SDave Hansen 	 * bits get passed in and set in watches/events.
708d30e2c05SDave Hansen 	 */
709d30e2c05SDave Hansen 	if (unlikely(mask & ~ALL_INOTIFY_BITS))
710d30e2c05SDave Hansen 		return -EINVAL;
711d30e2c05SDave Hansen 	/*
712d30e2c05SDave Hansen 	 * Require at least one valid bit set in the mask.
713d30e2c05SDave Hansen 	 * Without _something_ set, we would have no events to
714d30e2c05SDave Hansen 	 * watch for.
715d30e2c05SDave Hansen 	 */
71604df32faSZhao Hongjiang 	if (unlikely(!(mask & ALL_INOTIFY_BITS)))
71704df32faSZhao Hongjiang 		return -EINVAL;
71804df32faSZhao Hongjiang 
7192903ff01SAl Viro 	f = fdget(fd);
7202903ff01SAl Viro 	if (unlikely(!f.file))
721272eb014SEric Paris 		return -EBADF;
722272eb014SEric Paris 
723272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
7242903ff01SAl Viro 	if (unlikely(f.file->f_op != &inotify_fops)) {
725272eb014SEric Paris 		ret = -EINVAL;
726272eb014SEric Paris 		goto fput_and_out;
727272eb014SEric Paris 	}
728272eb014SEric Paris 
729272eb014SEric Paris 	if (!(mask & IN_DONT_FOLLOW))
730272eb014SEric Paris 		flags |= LOOKUP_FOLLOW;
731272eb014SEric Paris 	if (mask & IN_ONLYDIR)
732272eb014SEric Paris 		flags |= LOOKUP_DIRECTORY;
733272eb014SEric Paris 
73463c882a0SEric Paris 	ret = inotify_find_inode(pathname, &path, flags);
73563c882a0SEric Paris 	if (ret)
736272eb014SEric Paris 		goto fput_and_out;
737272eb014SEric Paris 
73863c882a0SEric Paris 	/* inode held in place by reference to path; group by fget on fd */
739272eb014SEric Paris 	inode = path.dentry->d_inode;
7402903ff01SAl Viro 	group = f.file->private_data;
741272eb014SEric Paris 
74263c882a0SEric Paris 	/* create/update an inode mark */
74363c882a0SEric Paris 	ret = inotify_update_watch(group, inode, mask);
744272eb014SEric Paris 	path_put(&path);
745272eb014SEric Paris fput_and_out:
7462903ff01SAl Viro 	fdput(f);
747272eb014SEric Paris 	return ret;
748272eb014SEric Paris }
749272eb014SEric Paris 
7502e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
751272eb014SEric Paris {
75263c882a0SEric Paris 	struct fsnotify_group *group;
753000285deSEric Paris 	struct inotify_inode_mark *i_mark;
7542903ff01SAl Viro 	struct fd f;
7552903ff01SAl Viro 	int ret = 0;
756272eb014SEric Paris 
7572903ff01SAl Viro 	f = fdget(fd);
7582903ff01SAl Viro 	if (unlikely(!f.file))
759272eb014SEric Paris 		return -EBADF;
760272eb014SEric Paris 
761272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
762272eb014SEric Paris 	ret = -EINVAL;
7632903ff01SAl Viro 	if (unlikely(f.file->f_op != &inotify_fops))
764272eb014SEric Paris 		goto out;
765272eb014SEric Paris 
7662903ff01SAl Viro 	group = f.file->private_data;
767272eb014SEric Paris 
76863c882a0SEric Paris 	ret = -EINVAL;
769000285deSEric Paris 	i_mark = inotify_idr_find(group, wd);
770000285deSEric Paris 	if (unlikely(!i_mark))
77163c882a0SEric Paris 		goto out;
77263c882a0SEric Paris 
773b7ba8371SEric Paris 	ret = 0;
774b7ba8371SEric Paris 
775e2a29943SLino Sanfilippo 	fsnotify_destroy_mark(&i_mark->fsn_mark, group);
776b7ba8371SEric Paris 
777b7ba8371SEric Paris 	/* match ref taken by inotify_idr_find */
778000285deSEric Paris 	fsnotify_put_mark(&i_mark->fsn_mark);
779272eb014SEric Paris 
780272eb014SEric Paris out:
7812903ff01SAl Viro 	fdput(f);
782272eb014SEric Paris 	return ret;
783272eb014SEric Paris }
784272eb014SEric Paris 
785272eb014SEric Paris /*
786ae0e47f0SJustin P. Mattock  * inotify_user_setup - Our initialization function.  Note that we cannot return
787272eb014SEric Paris  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
788272eb014SEric Paris  * must result in panic().
789272eb014SEric Paris  */
790272eb014SEric Paris static int __init inotify_user_setup(void)
791272eb014SEric Paris {
792f874e1acSEric Paris 	BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
793f874e1acSEric Paris 	BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
794f874e1acSEric Paris 	BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
795f874e1acSEric Paris 	BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
796f874e1acSEric Paris 	BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
797f874e1acSEric Paris 	BUILD_BUG_ON(IN_OPEN != FS_OPEN);
798f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
799f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
800f874e1acSEric Paris 	BUILD_BUG_ON(IN_CREATE != FS_CREATE);
801f874e1acSEric Paris 	BUILD_BUG_ON(IN_DELETE != FS_DELETE);
802f874e1acSEric Paris 	BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
803f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
804f874e1acSEric Paris 	BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
805f874e1acSEric Paris 	BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
806f874e1acSEric Paris 	BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
807f874e1acSEric Paris 	BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
808b29866aaSEric Paris 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
809f874e1acSEric Paris 	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
810f874e1acSEric Paris 
811f874e1acSEric Paris 	BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
812f874e1acSEric Paris 
813*d46eb14bSShakeel Butt 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
814*d46eb14bSShakeel Butt 					       SLAB_PANIC|SLAB_ACCOUNT);
81563c882a0SEric Paris 
816272eb014SEric Paris 	inotify_max_queued_events = 16384;
8171cce1eeaSNikolay Borisov 	init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
8181cce1eeaSNikolay Borisov 	init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
819272eb014SEric Paris 
820272eb014SEric Paris 	return 0;
821272eb014SEric Paris }
822c013d5a4SPaul Gortmaker fs_initcall(inotify_user_setup);
823