xref: /linux/fs/notify/inotify/inotify_user.c (revision 1a2620a99803ad660edc5d22fd9c66cce91ceb1c)
13e0a4e85SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2272eb014SEric Paris /*
3272eb014SEric Paris  * fs/inotify_user.c - inotify support for userspace
4272eb014SEric Paris  *
5272eb014SEric Paris  * Authors:
6272eb014SEric Paris  *	John McCutchan	<ttb@tentacle.dhs.org>
7272eb014SEric Paris  *	Robert Love	<rml@novell.com>
8272eb014SEric Paris  *
9272eb014SEric Paris  * Copyright (C) 2005 John McCutchan
10272eb014SEric Paris  * Copyright 2006 Hewlett-Packard Development Company, L.P.
11272eb014SEric Paris  *
1263c882a0SEric Paris  * Copyright (C) 2009 Eric Paris <Red Hat Inc>
1363c882a0SEric Paris  * inotify was largely rewriten to make use of the fsnotify infrastructure
14272eb014SEric Paris  */
15272eb014SEric Paris 
16272eb014SEric Paris #include <linux/file.h>
1763c882a0SEric Paris #include <linux/fs.h> /* struct inode */
1863c882a0SEric Paris #include <linux/fsnotify_backend.h>
1963c882a0SEric Paris #include <linux/idr.h>
20c013d5a4SPaul Gortmaker #include <linux/init.h> /* fs_initcall */
21272eb014SEric Paris #include <linux/inotify.h>
2263c882a0SEric Paris #include <linux/kernel.h> /* roundup() */
2363c882a0SEric Paris #include <linux/namei.h> /* LOOKUP_FOLLOW */
24174cd4b1SIngo Molnar #include <linux/sched/signal.h>
2563c882a0SEric Paris #include <linux/slab.h> /* struct kmem_cache */
26272eb014SEric Paris #include <linux/syscalls.h>
2763c882a0SEric Paris #include <linux/types.h>
28c44dcc56SAl Viro #include <linux/anon_inodes.h>
2963c882a0SEric Paris #include <linux/uaccess.h>
3063c882a0SEric Paris #include <linux/poll.h>
3163c882a0SEric Paris #include <linux/wait.h>
32d46eb14bSShakeel Butt #include <linux/memcontrol.h>
33ac5656d8SAaron Goidel #include <linux/security.h>
3463c882a0SEric Paris 
3563c882a0SEric Paris #include "inotify.h"
36be77196bSCyrill Gorcunov #include "../fdinfo.h"
37272eb014SEric Paris 
38272eb014SEric Paris #include <asm/ioctls.h>
39272eb014SEric Paris 
4092890123SWaiman Long /*
4192890123SWaiman Long  * An inotify watch requires allocating an inotify_inode_mark structure as
4292890123SWaiman Long  * well as pinning the watched inode. Doubling the size of a VFS inode
4392890123SWaiman Long  * should be more than enough to cover the additional filesystem inode
4492890123SWaiman Long  * size increase.
4592890123SWaiman Long  */
4692890123SWaiman Long #define INOTIFY_WATCH_COST	(sizeof(struct inotify_inode_mark) + \
4792890123SWaiman Long 				 2 * sizeof(struct inode))
4892890123SWaiman Long 
491cce1eeaSNikolay Borisov /* configurable via /proc/sys/fs/inotify/ */
50272eb014SEric Paris static int inotify_max_queued_events __read_mostly;
5163c882a0SEric Paris 
52054c636eSJan Kara struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
53272eb014SEric Paris 
54272eb014SEric Paris #ifdef CONFIG_SYSCTL
55272eb014SEric Paris 
56272eb014SEric Paris #include <linux/sysctl.h>
57272eb014SEric Paris 
5892f778ddSJoe Perches struct ctl_table inotify_table[] = {
59272eb014SEric Paris 	{
60272eb014SEric Paris 		.procname	= "max_user_instances",
611cce1eeaSNikolay 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,
65eec4844fSMatteo Croce 		.extra1		= SYSCTL_ZERO,
66272eb014SEric Paris 	},
67272eb014SEric Paris 	{
68272eb014SEric Paris 		.procname	= "max_user_watches",
691cce1eeaSNikolay 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,
73eec4844fSMatteo Croce 		.extra1		= SYSCTL_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,
81eec4844fSMatteo Croce 		.extra1		= SYSCTL_ZERO
82272eb014SEric Paris 	},
83ab09203eSEric W. Biederman 	{ }
84272eb014SEric Paris };
85272eb014SEric Paris #endif /* CONFIG_SYSCTL */
86272eb014SEric Paris 
87957f7b47SAmir Goldstein static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
88272eb014SEric Paris {
8963c882a0SEric Paris 	__u32 mask;
9063c882a0SEric Paris 
91611da04fSEric Paris 	/*
92957f7b47SAmir Goldstein 	 * Everything should accept their own ignored and should receive events
93957f7b47SAmir Goldstein 	 * when the inode is unmounted.  All directories care about children.
94611da04fSEric Paris 	 */
95957f7b47SAmir Goldstein 	mask = (FS_IN_IGNORED | FS_UNMOUNT);
96957f7b47SAmir Goldstein 	if (S_ISDIR(inode->i_mode))
97957f7b47SAmir Goldstein 		mask |= FS_EVENT_ON_CHILD;
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 */
112076ccb76SAl Viro static __poll_t inotify_poll(struct file *file, poll_table *wait)
113272eb014SEric Paris {
11463c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
115076ccb76SAl Viro 	__poll_t ret = 0;
116272eb014SEric Paris 
11763c882a0SEric Paris 	poll_wait(file, &group->notification_waitq, wait);
118c21dbe20SJan Kara 	spin_lock(&group->notification_lock);
11963c882a0SEric Paris 	if (!fsnotify_notify_queue_is_empty(group))
120a9a08845SLinus Torvalds 		ret = EPOLLIN | EPOLLRDNORM;
121c21dbe20SJan Kara 	spin_unlock(&group->notification_lock);
122272eb014SEric Paris 
123272eb014SEric Paris 	return ret;
124272eb014SEric Paris }
125272eb014SEric Paris 
1267053aee2SJan Kara static int round_event_name_len(struct fsnotify_event *fsn_event)
127e9fe6904SJan Kara {
1287053aee2SJan Kara 	struct inotify_event_info *event;
1297053aee2SJan Kara 
1307053aee2SJan Kara 	event = INOTIFY_E(fsn_event);
131e9fe6904SJan Kara 	if (!event->name_len)
132e9fe6904SJan Kara 		return 0;
133e9fe6904SJan Kara 	return roundup(event->name_len + 1, sizeof(struct inotify_event));
134e9fe6904SJan Kara }
135e9fe6904SJan Kara 
1363632dee2SVegard Nossum /*
1373632dee2SVegard Nossum  * Get an inotify_kernel_event if one exists and is small
1383632dee2SVegard Nossum  * enough to fit in "count". Return an error pointer if
1393632dee2SVegard Nossum  * not large enough.
1403632dee2SVegard Nossum  *
141c21dbe20SJan Kara  * Called with the group->notification_lock held.
1423632dee2SVegard Nossum  */
14363c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
1443632dee2SVegard Nossum 					    size_t count)
1453632dee2SVegard Nossum {
1463632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
14763c882a0SEric Paris 	struct fsnotify_event *event;
1483632dee2SVegard Nossum 
14963c882a0SEric Paris 	if (fsnotify_notify_queue_is_empty(group))
1503632dee2SVegard Nossum 		return NULL;
1513632dee2SVegard Nossum 
1528ba8fa91SJan Kara 	event = fsnotify_peek_first_event(group);
15363c882a0SEric Paris 
1545ba08e2eSEric Paris 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
1555ba08e2eSEric Paris 
156e9fe6904SJan Kara 	event_size += round_event_name_len(event);
1573632dee2SVegard Nossum 	if (event_size > count)
1583632dee2SVegard Nossum 		return ERR_PTR(-EINVAL);
1593632dee2SVegard Nossum 
160c21dbe20SJan Kara 	/* held the notification_lock the whole time, so this is the
16163c882a0SEric Paris 	 * same event we peeked above */
1628ba8fa91SJan Kara 	fsnotify_remove_first_event(group);
16363c882a0SEric Paris 
16463c882a0SEric Paris 	return event;
1653632dee2SVegard Nossum }
1663632dee2SVegard Nossum 
1673632dee2SVegard Nossum /*
1683632dee2SVegard Nossum  * Copy an event to user space, returning how much we copied.
1693632dee2SVegard Nossum  *
1703632dee2SVegard Nossum  * We already checked that the event size is smaller than the
1713632dee2SVegard Nossum  * buffer we had in "get_one_event()" above.
1723632dee2SVegard Nossum  */
17363c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group,
1747053aee2SJan Kara 				  struct fsnotify_event *fsn_event,
1753632dee2SVegard Nossum 				  char __user *buf)
1763632dee2SVegard Nossum {
17763c882a0SEric Paris 	struct inotify_event inotify_event;
1787053aee2SJan Kara 	struct inotify_event_info *event;
1793632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
180e9fe6904SJan Kara 	size_t name_len;
181e9fe6904SJan Kara 	size_t pad_name_len;
1823632dee2SVegard Nossum 
1837053aee2SJan Kara 	pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
1845ba08e2eSEric Paris 
1857053aee2SJan Kara 	event = INOTIFY_E(fsn_event);
186e9fe6904SJan Kara 	name_len = event->name_len;
187b962e731SBrian Rogers 	/*
188e9fe6904SJan Kara 	 * round up name length so it is a multiple of event_size
1890db501bdSEric W. Biederman 	 * plus an extra byte for the terminating '\0'.
1900db501bdSEric W. Biederman 	 */
1917053aee2SJan Kara 	pad_name_len = round_event_name_len(fsn_event);
192e9fe6904SJan Kara 	inotify_event.len = pad_name_len;
193a0a92d26SAmir Goldstein 	inotify_event.mask = inotify_mask_to_arg(event->mask);
1947053aee2SJan Kara 	inotify_event.wd = event->wd;
19563c882a0SEric Paris 	inotify_event.cookie = event->sync_cookie;
19663c882a0SEric Paris 
19763c882a0SEric Paris 	/* send the main event */
19863c882a0SEric Paris 	if (copy_to_user(buf, &inotify_event, event_size))
1993632dee2SVegard Nossum 		return -EFAULT;
2003632dee2SVegard Nossum 
2013632dee2SVegard Nossum 	buf += event_size;
2023632dee2SVegard Nossum 
20363c882a0SEric Paris 	/*
20463c882a0SEric Paris 	 * fsnotify only stores the pathname, so here we have to send the pathname
20563c882a0SEric Paris 	 * and then pad that pathname out to a multiple of sizeof(inotify_event)
206e9fe6904SJan Kara 	 * with zeros.
20763c882a0SEric Paris 	 */
208e9fe6904SJan Kara 	if (pad_name_len) {
20963c882a0SEric Paris 		/* copy the path name */
2107053aee2SJan Kara 		if (copy_to_user(buf, event->name, name_len))
2113632dee2SVegard Nossum 			return -EFAULT;
212e9fe6904SJan Kara 		buf += name_len;
2133632dee2SVegard Nossum 
2140db501bdSEric W. Biederman 		/* fill userspace with 0's */
215e9fe6904SJan Kara 		if (clear_user(buf, pad_name_len - name_len))
21663c882a0SEric Paris 			return -EFAULT;
217e9fe6904SJan Kara 		event_size += pad_name_len;
2183632dee2SVegard Nossum 	}
21963c882a0SEric Paris 
2203632dee2SVegard Nossum 	return event_size;
2213632dee2SVegard Nossum }
2223632dee2SVegard Nossum 
223272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf,
224272eb014SEric Paris 			    size_t count, loff_t *pos)
225272eb014SEric Paris {
22663c882a0SEric Paris 	struct fsnotify_group *group;
22763c882a0SEric Paris 	struct fsnotify_event *kevent;
228272eb014SEric Paris 	char __user *start;
229272eb014SEric Paris 	int ret;
230e23738a7SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
231272eb014SEric Paris 
232272eb014SEric Paris 	start = buf;
23363c882a0SEric Paris 	group = file->private_data;
234272eb014SEric Paris 
235e23738a7SPeter Zijlstra 	add_wait_queue(&group->notification_waitq, &wait);
236272eb014SEric Paris 	while (1) {
237c21dbe20SJan Kara 		spin_lock(&group->notification_lock);
23863c882a0SEric Paris 		kevent = get_one_event(group, count);
239c21dbe20SJan Kara 		spin_unlock(&group->notification_lock);
240272eb014SEric Paris 
2415ba08e2eSEric Paris 		pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
2425ba08e2eSEric Paris 
2433632dee2SVegard Nossum 		if (kevent) {
2443632dee2SVegard Nossum 			ret = PTR_ERR(kevent);
2453632dee2SVegard Nossum 			if (IS_ERR(kevent))
246272eb014SEric Paris 				break;
24763c882a0SEric Paris 			ret = copy_event_to_user(group, kevent, buf);
2487053aee2SJan Kara 			fsnotify_destroy_event(group, kevent);
2493632dee2SVegard Nossum 			if (ret < 0)
2503632dee2SVegard Nossum 				break;
2513632dee2SVegard Nossum 			buf += ret;
2523632dee2SVegard Nossum 			count -= ret;
2533632dee2SVegard Nossum 			continue;
254272eb014SEric Paris 		}
255272eb014SEric Paris 
2563632dee2SVegard Nossum 		ret = -EAGAIN;
2573632dee2SVegard Nossum 		if (file->f_flags & O_NONBLOCK)
258272eb014SEric Paris 			break;
2591ca39ab9SEric Paris 		ret = -ERESTARTSYS;
2603632dee2SVegard Nossum 		if (signal_pending(current))
2613632dee2SVegard Nossum 			break;
2623632dee2SVegard Nossum 
2633632dee2SVegard Nossum 		if (start != buf)
2643632dee2SVegard Nossum 			break;
265272eb014SEric Paris 
266e23738a7SPeter Zijlstra 		wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
267272eb014SEric Paris 	}
268e23738a7SPeter Zijlstra 	remove_wait_queue(&group->notification_waitq, &wait);
269272eb014SEric Paris 
2703632dee2SVegard Nossum 	if (start != buf && ret != -EFAULT)
271272eb014SEric Paris 		ret = buf - start;
272272eb014SEric Paris 	return ret;
273272eb014SEric Paris }
274272eb014SEric Paris 
275272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file)
276272eb014SEric Paris {
27763c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
278272eb014SEric Paris 
2795ba08e2eSEric Paris 	pr_debug("%s: group=%p\n", __func__, group);
2805ba08e2eSEric Paris 
28163c882a0SEric Paris 	/* free this group, matching get was inotify_init->fsnotify_obtain_group */
282d8153d4dSLino Sanfilippo 	fsnotify_destroy_group(group);
283272eb014SEric Paris 
284272eb014SEric Paris 	return 0;
285272eb014SEric Paris }
286272eb014SEric Paris 
287272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd,
288272eb014SEric Paris 			  unsigned long arg)
289272eb014SEric Paris {
29063c882a0SEric Paris 	struct fsnotify_group *group;
2917053aee2SJan Kara 	struct fsnotify_event *fsn_event;
292272eb014SEric Paris 	void __user *p;
293272eb014SEric Paris 	int ret = -ENOTTY;
29463c882a0SEric Paris 	size_t send_len = 0;
295272eb014SEric Paris 
29663c882a0SEric Paris 	group = file->private_data;
297272eb014SEric Paris 	p = (void __user *) arg;
298272eb014SEric Paris 
2995ba08e2eSEric Paris 	pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
3005ba08e2eSEric Paris 
301272eb014SEric Paris 	switch (cmd) {
302272eb014SEric Paris 	case FIONREAD:
303c21dbe20SJan Kara 		spin_lock(&group->notification_lock);
3047053aee2SJan Kara 		list_for_each_entry(fsn_event, &group->notification_list,
3057053aee2SJan Kara 				    list) {
30663c882a0SEric Paris 			send_len += sizeof(struct inotify_event);
3077053aee2SJan Kara 			send_len += round_event_name_len(fsn_event);
30863c882a0SEric Paris 		}
309c21dbe20SJan Kara 		spin_unlock(&group->notification_lock);
31063c882a0SEric Paris 		ret = put_user(send_len, (int __user *) p);
311272eb014SEric Paris 		break;
312e1603b6eSKirill Tkhai #ifdef CONFIG_CHECKPOINT_RESTORE
313e1603b6eSKirill Tkhai 	case INOTIFY_IOC_SETNEXTWD:
314e1603b6eSKirill Tkhai 		ret = -EINVAL;
315e1603b6eSKirill Tkhai 		if (arg >= 1 && arg <= INT_MAX) {
316e1603b6eSKirill Tkhai 			struct inotify_group_private_data *data;
317e1603b6eSKirill Tkhai 
318e1603b6eSKirill Tkhai 			data = &group->inotify_data;
319e1603b6eSKirill Tkhai 			spin_lock(&data->idr_lock);
320e1603b6eSKirill Tkhai 			idr_set_cursor(&data->idr, (unsigned int)arg);
321e1603b6eSKirill Tkhai 			spin_unlock(&data->idr_lock);
322e1603b6eSKirill Tkhai 			ret = 0;
323e1603b6eSKirill Tkhai 		}
324e1603b6eSKirill Tkhai 		break;
325e1603b6eSKirill Tkhai #endif /* CONFIG_CHECKPOINT_RESTORE */
326272eb014SEric Paris 	}
327272eb014SEric Paris 
328272eb014SEric Paris 	return ret;
329272eb014SEric Paris }
330272eb014SEric Paris 
331272eb014SEric Paris static const struct file_operations inotify_fops = {
332be77196bSCyrill Gorcunov 	.show_fdinfo	= inotify_show_fdinfo,
333272eb014SEric Paris 	.poll		= inotify_poll,
334272eb014SEric Paris 	.read		= inotify_read,
3350a6b6bd5SEric Paris 	.fasync		= fsnotify_fasync,
336272eb014SEric Paris 	.release	= inotify_release,
337272eb014SEric Paris 	.unlocked_ioctl	= inotify_ioctl,
338272eb014SEric Paris 	.compat_ioctl	= inotify_ioctl,
3396038f373SArnd Bergmann 	.llseek		= noop_llseek,
340272eb014SEric Paris };
341272eb014SEric Paris 
342272eb014SEric Paris 
34363c882a0SEric Paris /*
34463c882a0SEric Paris  * find_inode - resolve a user-given path to a specific inode
34563c882a0SEric Paris  */
346ac5656d8SAaron Goidel static int inotify_find_inode(const char __user *dirname, struct path *path,
347ac5656d8SAaron Goidel 						unsigned int flags, __u64 mask)
34863c882a0SEric Paris {
34963c882a0SEric Paris 	int error;
35063c882a0SEric Paris 
35163c882a0SEric Paris 	error = user_path_at(AT_FDCWD, dirname, flags, path);
35263c882a0SEric Paris 	if (error)
35363c882a0SEric Paris 		return error;
35463c882a0SEric Paris 	/* you can only watch an inode if you have read permissions on it */
35563c882a0SEric Paris 	error = inode_permission(path->dentry->d_inode, MAY_READ);
356ac5656d8SAaron Goidel 	if (error) {
357ac5656d8SAaron Goidel 		path_put(path);
358ac5656d8SAaron Goidel 		return error;
359ac5656d8SAaron Goidel 	}
360ac5656d8SAaron Goidel 	error = security_path_notify(path, mask,
361ac5656d8SAaron Goidel 				FSNOTIFY_OBJ_TYPE_INODE);
36263c882a0SEric Paris 	if (error)
36363c882a0SEric Paris 		path_put(path);
364ac5656d8SAaron Goidel 
36563c882a0SEric Paris 	return error;
36663c882a0SEric Paris }
36763c882a0SEric Paris 
368b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
369000285deSEric Paris 			      struct inotify_inode_mark *i_mark)
370b7ba8371SEric Paris {
371b7ba8371SEric Paris 	int ret;
372b7ba8371SEric Paris 
3734542da63STejun Heo 	idr_preload(GFP_KERNEL);
374b7ba8371SEric Paris 	spin_lock(idr_lock);
3754542da63STejun Heo 
376a66c04b4SJeff Layton 	ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
3774542da63STejun Heo 	if (ret >= 0) {
378b7ba8371SEric Paris 		/* we added the mark to the idr, take a reference */
3794542da63STejun Heo 		i_mark->wd = ret;
380000285deSEric Paris 		fsnotify_get_mark(&i_mark->fsn_mark);
3817050c488SEric Paris 	}
382b7ba8371SEric Paris 
3834542da63STejun Heo 	spin_unlock(idr_lock);
3844542da63STejun Heo 	idr_preload_end();
3854542da63STejun Heo 	return ret < 0 ? ret : 0;
386b7ba8371SEric Paris }
387b7ba8371SEric Paris 
388000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
389b7ba8371SEric Paris 								int wd)
390b7ba8371SEric Paris {
391b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
392b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
393000285deSEric Paris 	struct inotify_inode_mark *i_mark;
394b7ba8371SEric Paris 
395b7ba8371SEric Paris 	assert_spin_locked(idr_lock);
396b7ba8371SEric Paris 
397000285deSEric Paris 	i_mark = idr_find(idr, wd);
398000285deSEric Paris 	if (i_mark) {
399000285deSEric Paris 		struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
400b7ba8371SEric Paris 
401000285deSEric Paris 		fsnotify_get_mark(fsn_mark);
402b7ba8371SEric Paris 		/* One ref for being in the idr, one ref we just took */
403ab97f873SElena Reshetova 		BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
404b7ba8371SEric Paris 	}
405b7ba8371SEric Paris 
406000285deSEric Paris 	return i_mark;
407b7ba8371SEric Paris }
408b7ba8371SEric Paris 
409000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
410b7ba8371SEric Paris 							 int wd)
411b7ba8371SEric Paris {
412000285deSEric Paris 	struct inotify_inode_mark *i_mark;
413b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
414b7ba8371SEric Paris 
415b7ba8371SEric Paris 	spin_lock(idr_lock);
416000285deSEric Paris 	i_mark = inotify_idr_find_locked(group, wd);
417b7ba8371SEric Paris 	spin_unlock(idr_lock);
418b7ba8371SEric Paris 
419000285deSEric Paris 	return i_mark;
420b7ba8371SEric Paris }
421b7ba8371SEric Paris 
422dead537dSEric Paris /*
423dead537dSEric Paris  * Remove the mark from the idr (if present) and drop the reference
424dead537dSEric Paris  * on the mark because it was in the idr.
425dead537dSEric Paris  */
4267e790dd5SEric Paris static void inotify_remove_from_idr(struct fsnotify_group *group,
427000285deSEric Paris 				    struct inotify_inode_mark *i_mark)
4287e790dd5SEric Paris {
429e7253760SJan Kara 	struct idr *idr = &group->inotify_data.idr;
430b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
431000285deSEric Paris 	struct inotify_inode_mark *found_i_mark = NULL;
432dead537dSEric Paris 	int wd;
4337e790dd5SEric Paris 
434b7ba8371SEric Paris 	spin_lock(idr_lock);
435000285deSEric Paris 	wd = i_mark->wd;
436dead537dSEric Paris 
437b7ba8371SEric Paris 	/*
438000285deSEric Paris 	 * does this i_mark think it is in the idr?  we shouldn't get called
439b7ba8371SEric Paris 	 * if it wasn't....
440b7ba8371SEric Paris 	 */
441b7ba8371SEric Paris 	if (wd == -1) {
44225c829afSJan Kara 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
44325c829afSJan Kara 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
444dead537dSEric Paris 		goto out;
4457e790dd5SEric Paris 	}
446dead537dSEric Paris 
447b7ba8371SEric Paris 	/* Lets look in the idr to see if we find it */
448000285deSEric Paris 	found_i_mark = inotify_idr_find_locked(group, wd);
449000285deSEric Paris 	if (unlikely(!found_i_mark)) {
45025c829afSJan Kara 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
45125c829afSJan Kara 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
452b7ba8371SEric Paris 		goto out;
453b7ba8371SEric Paris 	}
454dead537dSEric Paris 
455b7ba8371SEric Paris 	/*
456000285deSEric Paris 	 * We found an mark in the idr at the right wd, but it's
457000285deSEric Paris 	 * not the mark we were told to remove.  eparis seriously
458b7ba8371SEric Paris 	 * fucked up somewhere.
459b7ba8371SEric Paris 	 */
460000285deSEric Paris 	if (unlikely(found_i_mark != i_mark)) {
461000285deSEric Paris 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
46225c829afSJan Kara 			"found_i_mark=%p found_i_mark->wd=%d "
46325c829afSJan Kara 			"found_i_mark->group=%p\n", __func__, i_mark,
46425c829afSJan Kara 			i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
46525c829afSJan Kara 			found_i_mark->wd, found_i_mark->fsn_mark.group);
466b7ba8371SEric Paris 		goto out;
467b7ba8371SEric Paris 	}
468dead537dSEric Paris 
469b7ba8371SEric Paris 	/*
470b7ba8371SEric Paris 	 * One ref for being in the idr
471b7ba8371SEric Paris 	 * one ref grabbed by inotify_idr_find
472b7ba8371SEric Paris 	 */
473ab97f873SElena Reshetova 	if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
47425c829afSJan Kara 		printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
47525c829afSJan Kara 			 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
476b7ba8371SEric Paris 		/* we can't really recover with bad ref cnting.. */
477b7ba8371SEric Paris 		BUG();
478b7ba8371SEric Paris 	}
479b7ba8371SEric Paris 
480e7253760SJan Kara 	idr_remove(idr, wd);
481e7253760SJan Kara 	/* Removed from the idr, drop that ref. */
482e7253760SJan Kara 	fsnotify_put_mark(&i_mark->fsn_mark);
483dead537dSEric Paris out:
484e7253760SJan Kara 	i_mark->wd = -1;
485e7253760SJan Kara 	spin_unlock(idr_lock);
486b7ba8371SEric Paris 	/* match the ref taken by inotify_idr_find_locked() */
487000285deSEric Paris 	if (found_i_mark)
488000285deSEric Paris 		fsnotify_put_mark(&found_i_mark->fsn_mark);
489dead537dSEric Paris }
490dead537dSEric Paris 
49163c882a0SEric Paris /*
492dead537dSEric Paris  * Send IN_IGNORED for this wd, remove this wd from the idr.
49363c882a0SEric Paris  */
494000285deSEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
495528da3e9SEric Paris 				    struct fsnotify_group *group)
49663c882a0SEric Paris {
497000285deSEric Paris 	struct inotify_inode_mark *i_mark;
4987053aee2SJan Kara 
4997053aee2SJan Kara 	/* Queue ignore event for the watch */
500*1a2620a9SAmir Goldstein 	inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
501*1a2620a9SAmir Goldstein 				   0);
50263c882a0SEric Paris 
5038b99c3ccSLino Sanfilippo 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
504000285deSEric Paris 	/* remove this mark from the idr */
505000285deSEric Paris 	inotify_remove_from_idr(group, i_mark);
50663c882a0SEric Paris 
5071cce1eeaSNikolay Borisov 	dec_inotify_watches(group->inotify_data.ucounts);
50863c882a0SEric Paris }
50963c882a0SEric Paris 
51052cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group,
51152cef755SEric Paris 					 struct inode *inode,
51252cef755SEric Paris 					 u32 arg)
51363c882a0SEric Paris {
514000285deSEric Paris 	struct fsnotify_mark *fsn_mark;
515000285deSEric Paris 	struct inotify_inode_mark *i_mark;
51663c882a0SEric Paris 	__u32 old_mask, new_mask;
51752cef755SEric Paris 	__u32 mask;
51852cef755SEric Paris 	int add = (arg & IN_MASK_ADD);
5194d97f7d5SHenry Wilson 	int create = (arg & IN_MASK_CREATE);
52052cef755SEric Paris 	int ret;
52163c882a0SEric Paris 
522957f7b47SAmir Goldstein 	mask = inotify_arg_to_mask(inode, arg);
52363c882a0SEric Paris 
524b1362edfSJan Kara 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
525000285deSEric Paris 	if (!fsn_mark)
52652cef755SEric Paris 		return -ENOENT;
52762c9d267SZhangXiaoxu 	else if (create) {
52862c9d267SZhangXiaoxu 		ret = -EEXIST;
52962c9d267SZhangXiaoxu 		goto out;
53062c9d267SZhangXiaoxu 	}
53152cef755SEric Paris 
532000285deSEric Paris 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
53375fe2b26SEric Paris 
534000285deSEric Paris 	spin_lock(&fsn_mark->lock);
535000285deSEric Paris 	old_mask = fsn_mark->mask;
53690b1e7a5SEric Paris 	if (add)
53766d2b81bSJan Kara 		fsn_mark->mask |= mask;
53890b1e7a5SEric Paris 	else
53966d2b81bSJan Kara 		fsn_mark->mask = mask;
540000285deSEric Paris 	new_mask = fsn_mark->mask;
541000285deSEric Paris 	spin_unlock(&fsn_mark->lock);
54263c882a0SEric Paris 
54363c882a0SEric Paris 	if (old_mask != new_mask) {
54463c882a0SEric Paris 		/* more bits in old than in new? */
54563c882a0SEric Paris 		int dropped = (old_mask & ~new_mask);
546000285deSEric Paris 		/* more bits in this fsn_mark than the inode's mask? */
54763c882a0SEric Paris 		int do_inode = (new_mask & ~inode->i_fsnotify_mask);
54863c882a0SEric Paris 
549000285deSEric Paris 		/* update the inode with this new fsn_mark */
55063c882a0SEric Paris 		if (dropped || do_inode)
5518920d273SJan Kara 			fsnotify_recalc_mask(inode->i_fsnotify_marks);
55263c882a0SEric Paris 
55363c882a0SEric Paris 	}
55463c882a0SEric Paris 
55552cef755SEric Paris 	/* return the wd */
556000285deSEric Paris 	ret = i_mark->wd;
55752cef755SEric Paris 
55862c9d267SZhangXiaoxu out:
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 
575957f7b47SAmir Goldstein 	mask = inotify_arg_to_mask(inode, 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 
581054c636eSJan Kara 	fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
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 
5891cce1eeaSNikolay Borisov 	/* increment the number of watches the user has */
5901cce1eeaSNikolay Borisov 	if (!inc_inotify_watches(group->inotify_data.ucounts)) {
5911cce1eeaSNikolay Borisov 		inotify_remove_from_idr(group, tmp_i_mark);
5921cce1eeaSNikolay Borisov 		ret = -ENOSPC;
5931cce1eeaSNikolay Borisov 		goto out_err;
5941cce1eeaSNikolay Borisov 	}
5951cce1eeaSNikolay Borisov 
59652cef755SEric Paris 	/* we are on the idr, now get on the inode */
597b249f5beSAmir Goldstein 	ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
59852cef755SEric Paris 	if (ret) {
59952cef755SEric Paris 		/* we failed to get on the inode, get off the idr */
600000285deSEric Paris 		inotify_remove_from_idr(group, tmp_i_mark);
60152cef755SEric Paris 		goto out_err;
60252cef755SEric Paris 	}
60352cef755SEric Paris 
60452cef755SEric Paris 
605000285deSEric Paris 	/* return the watch descriptor for this new mark */
606000285deSEric Paris 	ret = tmp_i_mark->wd;
60752cef755SEric Paris 
60852cef755SEric Paris out_err:
609000285deSEric Paris 	/* match the ref from fsnotify_init_mark() */
610000285deSEric Paris 	fsnotify_put_mark(&tmp_i_mark->fsn_mark);
61152cef755SEric Paris 
61252cef755SEric Paris 	return ret;
61352cef755SEric Paris }
61452cef755SEric Paris 
61552cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
61652cef755SEric Paris {
61752cef755SEric Paris 	int ret = 0;
61852cef755SEric Paris 
619e1e5a9f8SLino Sanfilippo 	mutex_lock(&group->mark_mutex);
62052cef755SEric Paris 	/* try to update and existing watch with the new arg */
62152cef755SEric Paris 	ret = inotify_update_existing_watch(group, inode, arg);
62252cef755SEric Paris 	/* no mark present, try to add a new one */
62352cef755SEric Paris 	if (ret == -ENOENT)
62452cef755SEric Paris 		ret = inotify_new_watch(group, inode, arg);
625e1e5a9f8SLino Sanfilippo 	mutex_unlock(&group->mark_mutex);
62652cef755SEric Paris 
62763c882a0SEric Paris 	return ret;
62863c882a0SEric Paris }
62963c882a0SEric Paris 
630d0de4dc5SEric Paris static struct fsnotify_group *inotify_new_group(unsigned int max_events)
63163c882a0SEric Paris {
63263c882a0SEric Paris 	struct fsnotify_group *group;
633ff57cd58SJan Kara 	struct inotify_event_info *oevent;
63463c882a0SEric Paris 
6350d2e2a1dSEric Paris 	group = fsnotify_alloc_group(&inotify_fsnotify_ops);
63663c882a0SEric Paris 	if (IS_ERR(group))
63763c882a0SEric Paris 		return group;
63863c882a0SEric Paris 
639ff57cd58SJan Kara 	oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
640ff57cd58SJan Kara 	if (unlikely(!oevent)) {
641ff57cd58SJan Kara 		fsnotify_destroy_group(group);
642ff57cd58SJan Kara 		return ERR_PTR(-ENOMEM);
643ff57cd58SJan Kara 	}
644ff57cd58SJan Kara 	group->overflow_event = &oevent->fse;
645dfc2d259SAmir Goldstein 	fsnotify_init_event(group->overflow_event, 0);
646a0a92d26SAmir Goldstein 	oevent->mask = 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;
652d46eb14bSShakeel Butt 	group->memcg = get_mem_cgroup_from_mm(current->mm);
65363c882a0SEric Paris 
65463c882a0SEric Paris 	spin_lock_init(&group->inotify_data.idr_lock);
65563c882a0SEric Paris 	idr_init(&group->inotify_data.idr);
6561cce1eeaSNikolay Borisov 	group->inotify_data.ucounts = inc_ucount(current_user_ns(),
6571cce1eeaSNikolay Borisov 						 current_euid(),
6581cce1eeaSNikolay Borisov 						 UCOUNT_INOTIFY_INSTANCES);
659d0de4dc5SEric Paris 
6601cce1eeaSNikolay Borisov 	if (!group->inotify_data.ucounts) {
661d8153d4dSLino Sanfilippo 		fsnotify_destroy_group(group);
662d0de4dc5SEric Paris 		return ERR_PTR(-EMFILE);
663d0de4dc5SEric Paris 	}
66463c882a0SEric Paris 
66563c882a0SEric Paris 	return group;
66663c882a0SEric Paris }
66763c882a0SEric Paris 
66863c882a0SEric Paris 
66963c882a0SEric Paris /* inotify syscalls */
670d0d89d1eSDominik Brodowski static int do_inotify_init(int flags)
671272eb014SEric Paris {
67263c882a0SEric Paris 	struct fsnotify_group *group;
673c44dcc56SAl Viro 	int ret;
674272eb014SEric Paris 
675272eb014SEric Paris 	/* Check the IN_* constants for consistency.  */
676272eb014SEric Paris 	BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
677272eb014SEric Paris 	BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
678272eb014SEric Paris 
679272eb014SEric Paris 	if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
680272eb014SEric Paris 		return -EINVAL;
681272eb014SEric Paris 
68263c882a0SEric Paris 	/* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
683d0de4dc5SEric Paris 	group = inotify_new_group(inotify_max_queued_events);
684d0de4dc5SEric Paris 	if (IS_ERR(group))
685d0de4dc5SEric Paris 		return PTR_ERR(group);
686825f9692SAl Viro 
687c44dcc56SAl Viro 	ret = anon_inode_getfd("inotify", &inotify_fops, group,
688c44dcc56SAl Viro 				  O_RDONLY | flags);
689d0de4dc5SEric Paris 	if (ret < 0)
690d8153d4dSLino Sanfilippo 		fsnotify_destroy_group(group);
691d0de4dc5SEric Paris 
692272eb014SEric Paris 	return ret;
693272eb014SEric Paris }
694272eb014SEric Paris 
695d0d89d1eSDominik Brodowski SYSCALL_DEFINE1(inotify_init1, int, flags)
696d0d89d1eSDominik Brodowski {
697d0d89d1eSDominik Brodowski 	return do_inotify_init(flags);
698d0d89d1eSDominik Brodowski }
699d0d89d1eSDominik Brodowski 
700938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init)
701272eb014SEric Paris {
702d0d89d1eSDominik Brodowski 	return do_inotify_init(0);
703272eb014SEric Paris }
704272eb014SEric Paris 
7052e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
7062e4d0924SHeiko Carstens 		u32, mask)
707272eb014SEric Paris {
70863c882a0SEric Paris 	struct fsnotify_group *group;
709272eb014SEric Paris 	struct inode *inode;
710272eb014SEric Paris 	struct path path;
7112903ff01SAl Viro 	struct fd f;
7122903ff01SAl Viro 	int ret;
713272eb014SEric Paris 	unsigned flags = 0;
714272eb014SEric Paris 
715d30e2c05SDave Hansen 	/*
716d30e2c05SDave Hansen 	 * We share a lot of code with fs/dnotify.  We also share
717d30e2c05SDave Hansen 	 * the bit layout between inotify's IN_* and the fsnotify
718d30e2c05SDave Hansen 	 * FS_*.  This check ensures that only the inotify IN_*
719d30e2c05SDave Hansen 	 * bits get passed in and set in watches/events.
720d30e2c05SDave Hansen 	 */
721d30e2c05SDave Hansen 	if (unlikely(mask & ~ALL_INOTIFY_BITS))
722d30e2c05SDave Hansen 		return -EINVAL;
723d30e2c05SDave Hansen 	/*
724d30e2c05SDave Hansen 	 * Require at least one valid bit set in the mask.
725d30e2c05SDave Hansen 	 * Without _something_ set, we would have no events to
726d30e2c05SDave Hansen 	 * watch for.
727d30e2c05SDave Hansen 	 */
72804df32faSZhao Hongjiang 	if (unlikely(!(mask & ALL_INOTIFY_BITS)))
72904df32faSZhao Hongjiang 		return -EINVAL;
73004df32faSZhao Hongjiang 
7312903ff01SAl Viro 	f = fdget(fd);
7322903ff01SAl Viro 	if (unlikely(!f.file))
733272eb014SEric Paris 		return -EBADF;
734272eb014SEric Paris 
7354d97f7d5SHenry Wilson 	/* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
736125892edSTetsuo Handa 	if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
737125892edSTetsuo Handa 		ret = -EINVAL;
738125892edSTetsuo Handa 		goto fput_and_out;
739125892edSTetsuo Handa 	}
7404d97f7d5SHenry Wilson 
741272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
7422903ff01SAl Viro 	if (unlikely(f.file->f_op != &inotify_fops)) {
743272eb014SEric Paris 		ret = -EINVAL;
744272eb014SEric Paris 		goto fput_and_out;
745272eb014SEric Paris 	}
746272eb014SEric Paris 
747272eb014SEric Paris 	if (!(mask & IN_DONT_FOLLOW))
748272eb014SEric Paris 		flags |= LOOKUP_FOLLOW;
749272eb014SEric Paris 	if (mask & IN_ONLYDIR)
750272eb014SEric Paris 		flags |= LOOKUP_DIRECTORY;
751272eb014SEric Paris 
752ac5656d8SAaron Goidel 	ret = inotify_find_inode(pathname, &path, flags,
753ac5656d8SAaron Goidel 			(mask & IN_ALL_EVENTS));
75463c882a0SEric Paris 	if (ret)
755272eb014SEric Paris 		goto fput_and_out;
756272eb014SEric Paris 
75763c882a0SEric Paris 	/* inode held in place by reference to path; group by fget on fd */
758272eb014SEric Paris 	inode = path.dentry->d_inode;
7592903ff01SAl Viro 	group = f.file->private_data;
760272eb014SEric Paris 
76163c882a0SEric Paris 	/* create/update an inode mark */
76263c882a0SEric Paris 	ret = inotify_update_watch(group, inode, mask);
763272eb014SEric Paris 	path_put(&path);
764272eb014SEric Paris fput_and_out:
7652903ff01SAl Viro 	fdput(f);
766272eb014SEric Paris 	return ret;
767272eb014SEric Paris }
768272eb014SEric Paris 
7692e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
770272eb014SEric Paris {
77163c882a0SEric Paris 	struct fsnotify_group *group;
772000285deSEric Paris 	struct inotify_inode_mark *i_mark;
7732903ff01SAl Viro 	struct fd f;
7747b26aa24Syoungjun 	int ret = -EINVAL;
775272eb014SEric Paris 
7762903ff01SAl Viro 	f = fdget(fd);
7772903ff01SAl Viro 	if (unlikely(!f.file))
778272eb014SEric Paris 		return -EBADF;
779272eb014SEric Paris 
780272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
7812903ff01SAl Viro 	if (unlikely(f.file->f_op != &inotify_fops))
782272eb014SEric Paris 		goto out;
783272eb014SEric Paris 
7842903ff01SAl Viro 	group = f.file->private_data;
785272eb014SEric Paris 
786000285deSEric Paris 	i_mark = inotify_idr_find(group, wd);
787000285deSEric Paris 	if (unlikely(!i_mark))
78863c882a0SEric Paris 		goto out;
78963c882a0SEric Paris 
790b7ba8371SEric Paris 	ret = 0;
791b7ba8371SEric Paris 
792e2a29943SLino Sanfilippo 	fsnotify_destroy_mark(&i_mark->fsn_mark, group);
793b7ba8371SEric Paris 
794b7ba8371SEric Paris 	/* match ref taken by inotify_idr_find */
795000285deSEric Paris 	fsnotify_put_mark(&i_mark->fsn_mark);
796272eb014SEric Paris 
797272eb014SEric Paris out:
7982903ff01SAl Viro 	fdput(f);
799272eb014SEric Paris 	return ret;
800272eb014SEric Paris }
801272eb014SEric Paris 
802272eb014SEric Paris /*
803ae0e47f0SJustin P. Mattock  * inotify_user_setup - Our initialization function.  Note that we cannot return
804272eb014SEric Paris  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
805272eb014SEric Paris  * must result in panic().
806272eb014SEric Paris  */
807272eb014SEric Paris static int __init inotify_user_setup(void)
808272eb014SEric Paris {
80992890123SWaiman Long 	unsigned long watches_max;
81092890123SWaiman Long 	struct sysinfo si;
81192890123SWaiman Long 
81292890123SWaiman Long 	si_meminfo(&si);
81392890123SWaiman Long 	/*
81492890123SWaiman Long 	 * Allow up to 1% of addressable memory to be allocated for inotify
81592890123SWaiman Long 	 * watches (per user) limited to the range [8192, 1048576].
81692890123SWaiman Long 	 */
81792890123SWaiman Long 	watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
81892890123SWaiman Long 			INOTIFY_WATCH_COST;
81992890123SWaiman Long 	watches_max = clamp(watches_max, 8192UL, 1048576UL);
82092890123SWaiman Long 
821f874e1acSEric Paris 	BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
822f874e1acSEric Paris 	BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
823f874e1acSEric Paris 	BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
824f874e1acSEric Paris 	BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
825f874e1acSEric Paris 	BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
826f874e1acSEric Paris 	BUILD_BUG_ON(IN_OPEN != FS_OPEN);
827f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
828f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
829f874e1acSEric Paris 	BUILD_BUG_ON(IN_CREATE != FS_CREATE);
830f874e1acSEric Paris 	BUILD_BUG_ON(IN_DELETE != FS_DELETE);
831f874e1acSEric Paris 	BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
832f874e1acSEric Paris 	BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
833f874e1acSEric Paris 	BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
834f874e1acSEric Paris 	BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
835f874e1acSEric Paris 	BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
836f874e1acSEric Paris 	BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
837b29866aaSEric Paris 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
838f874e1acSEric Paris 	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
839f874e1acSEric Paris 
840a39f7ec4SAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
841f874e1acSEric Paris 
842d46eb14bSShakeel Butt 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
843d46eb14bSShakeel Butt 					       SLAB_PANIC|SLAB_ACCOUNT);
84463c882a0SEric Paris 
845272eb014SEric Paris 	inotify_max_queued_events = 16384;
8461cce1eeaSNikolay Borisov 	init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
84792890123SWaiman Long 	init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
848272eb014SEric Paris 
849272eb014SEric Paris 	return 0;
850272eb014SEric Paris }
851c013d5a4SPaul Gortmaker fs_initcall(inotify_user_setup);
852