xref: /linux/fs/notify/inotify/inotify_user.c (revision 31ddd3268dcb6c1d70e9930a83be43bf86e4bf17)
1272eb014SEric Paris /*
2272eb014SEric Paris  * fs/inotify_user.c - inotify support for userspace
3272eb014SEric Paris  *
4272eb014SEric Paris  * Authors:
5272eb014SEric Paris  *	John McCutchan	<ttb@tentacle.dhs.org>
6272eb014SEric Paris  *	Robert Love	<rml@novell.com>
7272eb014SEric Paris  *
8272eb014SEric Paris  * Copyright (C) 2005 John McCutchan
9272eb014SEric Paris  * Copyright 2006 Hewlett-Packard Development Company, L.P.
10272eb014SEric Paris  *
1163c882a0SEric Paris  * Copyright (C) 2009 Eric Paris <Red Hat Inc>
1263c882a0SEric Paris  * inotify was largely rewriten to make use of the fsnotify infrastructure
1363c882a0SEric Paris  *
14272eb014SEric Paris  * This program is free software; you can redistribute it and/or modify it
15272eb014SEric Paris  * under the terms of the GNU General Public License as published by the
16272eb014SEric Paris  * Free Software Foundation; either version 2, or (at your option) any
17272eb014SEric Paris  * later version.
18272eb014SEric Paris  *
19272eb014SEric Paris  * This program is distributed in the hope that it will be useful, but
20272eb014SEric Paris  * WITHOUT ANY WARRANTY; without even the implied warranty of
21272eb014SEric Paris  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22272eb014SEric Paris  * General Public License for more details.
23272eb014SEric Paris  */
24272eb014SEric Paris 
25272eb014SEric Paris #include <linux/file.h>
2663c882a0SEric Paris #include <linux/fs.h> /* struct inode */
2763c882a0SEric Paris #include <linux/fsnotify_backend.h>
2863c882a0SEric Paris #include <linux/idr.h>
2963c882a0SEric Paris #include <linux/init.h> /* module_init */
30272eb014SEric Paris #include <linux/inotify.h>
3163c882a0SEric Paris #include <linux/kernel.h> /* roundup() */
3263c882a0SEric Paris #include <linux/namei.h> /* LOOKUP_FOLLOW */
3363c882a0SEric Paris #include <linux/sched.h> /* struct user */
3463c882a0SEric Paris #include <linux/slab.h> /* struct kmem_cache */
35272eb014SEric Paris #include <linux/syscalls.h>
3663c882a0SEric Paris #include <linux/types.h>
37c44dcc56SAl Viro #include <linux/anon_inodes.h>
3863c882a0SEric Paris #include <linux/uaccess.h>
3963c882a0SEric Paris #include <linux/poll.h>
4063c882a0SEric Paris #include <linux/wait.h>
4163c882a0SEric Paris 
4263c882a0SEric Paris #include "inotify.h"
43272eb014SEric Paris 
44272eb014SEric Paris #include <asm/ioctls.h>
45272eb014SEric Paris 
46272eb014SEric Paris /* these are configurable via /proc/sys/fs/inotify/ */
47272eb014SEric Paris static int inotify_max_user_instances __read_mostly;
48272eb014SEric Paris static int inotify_max_queued_events __read_mostly;
4963c882a0SEric Paris int inotify_max_user_watches __read_mostly;
5063c882a0SEric Paris 
5163c882a0SEric Paris static struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
5263c882a0SEric Paris struct kmem_cache *event_priv_cachep __read_mostly;
53272eb014SEric Paris 
54272eb014SEric Paris /*
5563c882a0SEric Paris  * When inotify registers a new group it increments this and uses that
5663c882a0SEric Paris  * value as an offset to set the fsnotify group "name" and priority.
57272eb014SEric Paris  */
5863c882a0SEric Paris static atomic_t inotify_grp_num;
59272eb014SEric Paris 
60272eb014SEric Paris #ifdef CONFIG_SYSCTL
61272eb014SEric Paris 
62272eb014SEric Paris #include <linux/sysctl.h>
63272eb014SEric Paris 
64272eb014SEric Paris static int zero;
65272eb014SEric Paris 
66272eb014SEric Paris ctl_table inotify_table[] = {
67272eb014SEric Paris 	{
68272eb014SEric Paris 		.procname	= "max_user_instances",
69272eb014SEric Paris 		.data		= &inotify_max_user_instances,
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_user_watches",
77272eb014SEric Paris 		.data		= &inotify_max_user_watches,
78272eb014SEric Paris 		.maxlen		= sizeof(int),
79272eb014SEric Paris 		.mode		= 0644,
806d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
81272eb014SEric Paris 		.extra1		= &zero,
82272eb014SEric Paris 	},
83272eb014SEric Paris 	{
84272eb014SEric Paris 		.procname	= "max_queued_events",
85272eb014SEric Paris 		.data		= &inotify_max_queued_events,
86272eb014SEric Paris 		.maxlen		= sizeof(int),
87272eb014SEric Paris 		.mode		= 0644,
886d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
89272eb014SEric Paris 		.extra1		= &zero
90272eb014SEric Paris 	},
91ab09203eSEric W. Biederman 	{ }
92272eb014SEric Paris };
93272eb014SEric Paris #endif /* CONFIG_SYSCTL */
94272eb014SEric Paris 
9563c882a0SEric Paris static inline __u32 inotify_arg_to_mask(u32 arg)
96272eb014SEric Paris {
9763c882a0SEric Paris 	__u32 mask;
9863c882a0SEric Paris 
9963c882a0SEric Paris 	/* everything should accept their own ignored and cares about children */
10063c882a0SEric Paris 	mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD);
10163c882a0SEric Paris 
10263c882a0SEric Paris 	/* mask off the flags used to open the fd */
10363c882a0SEric Paris 	mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT));
10463c882a0SEric Paris 
10563c882a0SEric Paris 	return mask;
106272eb014SEric Paris }
107272eb014SEric Paris 
10863c882a0SEric Paris static inline u32 inotify_mask_to_arg(__u32 mask)
109272eb014SEric Paris {
11063c882a0SEric Paris 	return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
11163c882a0SEric Paris 		       IN_Q_OVERFLOW);
112272eb014SEric Paris }
113272eb014SEric Paris 
11463c882a0SEric Paris /* intofiy userspace file descriptor functions */
115272eb014SEric Paris static unsigned int inotify_poll(struct file *file, poll_table *wait)
116272eb014SEric Paris {
11763c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
118272eb014SEric Paris 	int ret = 0;
119272eb014SEric Paris 
12063c882a0SEric Paris 	poll_wait(file, &group->notification_waitq, wait);
12163c882a0SEric Paris 	mutex_lock(&group->notification_mutex);
12263c882a0SEric Paris 	if (!fsnotify_notify_queue_is_empty(group))
123272eb014SEric Paris 		ret = POLLIN | POLLRDNORM;
12463c882a0SEric Paris 	mutex_unlock(&group->notification_mutex);
125272eb014SEric Paris 
126272eb014SEric Paris 	return ret;
127272eb014SEric Paris }
128272eb014SEric Paris 
1293632dee2SVegard Nossum /*
1303632dee2SVegard Nossum  * Get an inotify_kernel_event if one exists and is small
1313632dee2SVegard Nossum  * enough to fit in "count". Return an error pointer if
1323632dee2SVegard Nossum  * not large enough.
1333632dee2SVegard Nossum  *
13463c882a0SEric Paris  * Called with the group->notification_mutex held.
1353632dee2SVegard Nossum  */
13663c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
1373632dee2SVegard Nossum 					    size_t count)
1383632dee2SVegard Nossum {
1393632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
14063c882a0SEric Paris 	struct fsnotify_event *event;
1413632dee2SVegard Nossum 
14263c882a0SEric Paris 	if (fsnotify_notify_queue_is_empty(group))
1433632dee2SVegard Nossum 		return NULL;
1443632dee2SVegard Nossum 
14563c882a0SEric Paris 	event = fsnotify_peek_notify_event(group);
14663c882a0SEric Paris 
14783cb10f0SEric Paris 	if (event->name_len)
14883cb10f0SEric Paris 		event_size += roundup(event->name_len + 1, event_size);
1493632dee2SVegard Nossum 
1503632dee2SVegard Nossum 	if (event_size > count)
1513632dee2SVegard Nossum 		return ERR_PTR(-EINVAL);
1523632dee2SVegard Nossum 
15363c882a0SEric Paris 	/* held the notification_mutex the whole time, so this is the
15463c882a0SEric Paris 	 * same event we peeked above */
15563c882a0SEric Paris 	fsnotify_remove_notify_event(group);
15663c882a0SEric Paris 
15763c882a0SEric Paris 	return event;
1583632dee2SVegard Nossum }
1593632dee2SVegard Nossum 
1603632dee2SVegard Nossum /*
1613632dee2SVegard Nossum  * Copy an event to user space, returning how much we copied.
1623632dee2SVegard Nossum  *
1633632dee2SVegard Nossum  * We already checked that the event size is smaller than the
1643632dee2SVegard Nossum  * buffer we had in "get_one_event()" above.
1653632dee2SVegard Nossum  */
16663c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group,
16763c882a0SEric Paris 				  struct fsnotify_event *event,
1683632dee2SVegard Nossum 				  char __user *buf)
1693632dee2SVegard Nossum {
17063c882a0SEric Paris 	struct inotify_event inotify_event;
17163c882a0SEric Paris 	struct fsnotify_event_private_data *fsn_priv;
17263c882a0SEric Paris 	struct inotify_event_private_data *priv;
1733632dee2SVegard Nossum 	size_t event_size = sizeof(struct inotify_event);
174b962e731SBrian Rogers 	size_t name_len = 0;
1753632dee2SVegard Nossum 
17663c882a0SEric Paris 	/* we get the inotify watch descriptor from the event private data */
17763c882a0SEric Paris 	spin_lock(&event->lock);
17863c882a0SEric Paris 	fsn_priv = fsnotify_remove_priv_from_event(group, event);
17963c882a0SEric Paris 	spin_unlock(&event->lock);
18063c882a0SEric Paris 
18163c882a0SEric Paris 	if (!fsn_priv)
18263c882a0SEric Paris 		inotify_event.wd = -1;
18363c882a0SEric Paris 	else {
18463c882a0SEric Paris 		priv = container_of(fsn_priv, struct inotify_event_private_data,
18563c882a0SEric Paris 				    fsnotify_event_priv_data);
18663c882a0SEric Paris 		inotify_event.wd = priv->wd;
18763c882a0SEric Paris 		inotify_free_event_priv(fsn_priv);
18863c882a0SEric Paris 	}
18963c882a0SEric Paris 
190b962e731SBrian Rogers 	/*
191b962e731SBrian Rogers 	 * round up event->name_len so it is a multiple of event_size
1920db501bdSEric W. Biederman 	 * plus an extra byte for the terminating '\0'.
1930db501bdSEric W. Biederman 	 */
194b962e731SBrian Rogers 	if (event->name_len)
1950db501bdSEric W. Biederman 		name_len = roundup(event->name_len + 1, event_size);
19663c882a0SEric Paris 	inotify_event.len = name_len;
19763c882a0SEric Paris 
19863c882a0SEric Paris 	inotify_event.mask = inotify_mask_to_arg(event->mask);
19963c882a0SEric Paris 	inotify_event.cookie = event->sync_cookie;
20063c882a0SEric Paris 
20163c882a0SEric Paris 	/* send the main event */
20263c882a0SEric Paris 	if (copy_to_user(buf, &inotify_event, event_size))
2033632dee2SVegard Nossum 		return -EFAULT;
2043632dee2SVegard Nossum 
2053632dee2SVegard Nossum 	buf += event_size;
2063632dee2SVegard Nossum 
20763c882a0SEric Paris 	/*
20863c882a0SEric Paris 	 * fsnotify only stores the pathname, so here we have to send the pathname
20963c882a0SEric Paris 	 * and then pad that pathname out to a multiple of sizeof(inotify_event)
21063c882a0SEric Paris 	 * with zeros.  I get my zeros from the nul_inotify_event.
21163c882a0SEric Paris 	 */
21263c882a0SEric Paris 	if (name_len) {
21363c882a0SEric Paris 		unsigned int len_to_zero = name_len - event->name_len;
21463c882a0SEric Paris 		/* copy the path name */
21563c882a0SEric Paris 		if (copy_to_user(buf, event->file_name, event->name_len))
2163632dee2SVegard Nossum 			return -EFAULT;
21763c882a0SEric Paris 		buf += event->name_len;
2183632dee2SVegard Nossum 
2190db501bdSEric W. Biederman 		/* fill userspace with 0's */
2200db501bdSEric W. Biederman 		if (clear_user(buf, len_to_zero))
22163c882a0SEric Paris 			return -EFAULT;
22263c882a0SEric Paris 		buf += len_to_zero;
22363c882a0SEric Paris 		event_size += name_len;
2243632dee2SVegard Nossum 	}
22563c882a0SEric Paris 
2263632dee2SVegard Nossum 	return event_size;
2273632dee2SVegard Nossum }
2283632dee2SVegard Nossum 
229272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf,
230272eb014SEric Paris 			    size_t count, loff_t *pos)
231272eb014SEric Paris {
23263c882a0SEric Paris 	struct fsnotify_group *group;
23363c882a0SEric Paris 	struct fsnotify_event *kevent;
234272eb014SEric Paris 	char __user *start;
235272eb014SEric Paris 	int ret;
236272eb014SEric Paris 	DEFINE_WAIT(wait);
237272eb014SEric Paris 
238272eb014SEric Paris 	start = buf;
23963c882a0SEric Paris 	group = file->private_data;
240272eb014SEric Paris 
241272eb014SEric Paris 	while (1) {
24263c882a0SEric Paris 		prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
243272eb014SEric Paris 
24463c882a0SEric Paris 		mutex_lock(&group->notification_mutex);
24563c882a0SEric Paris 		kevent = get_one_event(group, count);
24663c882a0SEric Paris 		mutex_unlock(&group->notification_mutex);
247272eb014SEric Paris 
2483632dee2SVegard Nossum 		if (kevent) {
2493632dee2SVegard Nossum 			ret = PTR_ERR(kevent);
2503632dee2SVegard Nossum 			if (IS_ERR(kevent))
251272eb014SEric Paris 				break;
25263c882a0SEric Paris 			ret = copy_event_to_user(group, kevent, buf);
25363c882a0SEric Paris 			fsnotify_put_event(kevent);
2543632dee2SVegard Nossum 			if (ret < 0)
2553632dee2SVegard Nossum 				break;
2563632dee2SVegard Nossum 			buf += ret;
2573632dee2SVegard Nossum 			count -= ret;
2583632dee2SVegard Nossum 			continue;
259272eb014SEric Paris 		}
260272eb014SEric Paris 
2613632dee2SVegard Nossum 		ret = -EAGAIN;
2623632dee2SVegard Nossum 		if (file->f_flags & O_NONBLOCK)
263272eb014SEric Paris 			break;
2643632dee2SVegard Nossum 		ret = -EINTR;
2653632dee2SVegard Nossum 		if (signal_pending(current))
2663632dee2SVegard Nossum 			break;
2673632dee2SVegard Nossum 
2683632dee2SVegard Nossum 		if (start != buf)
2693632dee2SVegard Nossum 			break;
270272eb014SEric Paris 
271272eb014SEric Paris 		schedule();
272272eb014SEric Paris 	}
273272eb014SEric Paris 
27463c882a0SEric Paris 	finish_wait(&group->notification_waitq, &wait);
2753632dee2SVegard Nossum 	if (start != buf && ret != -EFAULT)
276272eb014SEric Paris 		ret = buf - start;
277272eb014SEric Paris 	return ret;
278272eb014SEric Paris }
279272eb014SEric Paris 
280272eb014SEric Paris static int inotify_fasync(int fd, struct file *file, int on)
281272eb014SEric Paris {
28263c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
283272eb014SEric Paris 
28463c882a0SEric Paris 	return fasync_helper(fd, file, on, &group->inotify_data.fa) >= 0 ? 0 : -EIO;
285272eb014SEric Paris }
286272eb014SEric Paris 
287272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file)
288272eb014SEric Paris {
28963c882a0SEric Paris 	struct fsnotify_group *group = file->private_data;
290bdae997fSKeith Packard 	struct user_struct *user = group->inotify_data.user;
291272eb014SEric Paris 
29263c882a0SEric Paris 	fsnotify_clear_marks_by_group(group);
293272eb014SEric Paris 
29463c882a0SEric Paris 	/* free this group, matching get was inotify_init->fsnotify_obtain_group */
29563c882a0SEric Paris 	fsnotify_put_group(group);
296272eb014SEric Paris 
297bdae997fSKeith Packard 	atomic_dec(&user->inotify_devs);
298bdae997fSKeith Packard 
299272eb014SEric Paris 	return 0;
300272eb014SEric Paris }
301272eb014SEric Paris 
302272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd,
303272eb014SEric Paris 			  unsigned long arg)
304272eb014SEric Paris {
30563c882a0SEric Paris 	struct fsnotify_group *group;
30663c882a0SEric Paris 	struct fsnotify_event_holder *holder;
30763c882a0SEric Paris 	struct fsnotify_event *event;
308272eb014SEric Paris 	void __user *p;
309272eb014SEric Paris 	int ret = -ENOTTY;
31063c882a0SEric Paris 	size_t send_len = 0;
311272eb014SEric Paris 
31263c882a0SEric Paris 	group = file->private_data;
313272eb014SEric Paris 	p = (void __user *) arg;
314272eb014SEric Paris 
315272eb014SEric Paris 	switch (cmd) {
316272eb014SEric Paris 	case FIONREAD:
31763c882a0SEric Paris 		mutex_lock(&group->notification_mutex);
31863c882a0SEric Paris 		list_for_each_entry(holder, &group->notification_list, event_list) {
31963c882a0SEric Paris 			event = holder->event;
32063c882a0SEric Paris 			send_len += sizeof(struct inotify_event);
32183cb10f0SEric Paris 			if (event->name_len)
32283cb10f0SEric Paris 				send_len += roundup(event->name_len + 1,
32363c882a0SEric Paris 						sizeof(struct inotify_event));
32463c882a0SEric Paris 		}
32563c882a0SEric Paris 		mutex_unlock(&group->notification_mutex);
32663c882a0SEric Paris 		ret = put_user(send_len, (int __user *) p);
327272eb014SEric Paris 		break;
328272eb014SEric Paris 	}
329272eb014SEric Paris 
330272eb014SEric Paris 	return ret;
331272eb014SEric Paris }
332272eb014SEric Paris 
333272eb014SEric Paris static const struct file_operations inotify_fops = {
334272eb014SEric Paris 	.poll		= inotify_poll,
335272eb014SEric Paris 	.read		= inotify_read,
336272eb014SEric Paris 	.fasync		= inotify_fasync,
337272eb014SEric Paris 	.release	= inotify_release,
338272eb014SEric Paris 	.unlocked_ioctl	= inotify_ioctl,
339272eb014SEric Paris 	.compat_ioctl	= inotify_ioctl,
340272eb014SEric Paris };
341272eb014SEric Paris 
342272eb014SEric Paris 
34363c882a0SEric Paris /*
34463c882a0SEric Paris  * find_inode - resolve a user-given path to a specific inode
34563c882a0SEric Paris  */
34663c882a0SEric Paris static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
34763c882a0SEric Paris {
34863c882a0SEric Paris 	int error;
34963c882a0SEric Paris 
35063c882a0SEric Paris 	error = user_path_at(AT_FDCWD, dirname, flags, path);
35163c882a0SEric Paris 	if (error)
35263c882a0SEric Paris 		return error;
35363c882a0SEric Paris 	/* you can only watch an inode if you have read permissions on it */
35463c882a0SEric Paris 	error = inode_permission(path->dentry->d_inode, MAY_READ);
35563c882a0SEric Paris 	if (error)
35663c882a0SEric Paris 		path_put(path);
35763c882a0SEric Paris 	return error;
35863c882a0SEric Paris }
35963c882a0SEric Paris 
360b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
361b7ba8371SEric Paris 			      int last_wd,
362b7ba8371SEric Paris 			      struct inotify_inode_mark_entry *ientry)
363b7ba8371SEric Paris {
364b7ba8371SEric Paris 	int ret;
365b7ba8371SEric Paris 
366b7ba8371SEric Paris 	do {
367b7ba8371SEric Paris 		if (unlikely(!idr_pre_get(idr, GFP_KERNEL)))
368b7ba8371SEric Paris 			return -ENOMEM;
369b7ba8371SEric Paris 
370b7ba8371SEric Paris 		spin_lock(idr_lock);
371b7ba8371SEric Paris 		ret = idr_get_new_above(idr, ientry, last_wd + 1,
372b7ba8371SEric Paris 					&ientry->wd);
373b7ba8371SEric Paris 		/* we added the mark to the idr, take a reference */
374b7ba8371SEric Paris 		if (!ret)
375b7ba8371SEric Paris 			fsnotify_get_mark(&ientry->fsn_entry);
376b7ba8371SEric Paris 		spin_unlock(idr_lock);
377b7ba8371SEric Paris 	} while (ret == -EAGAIN);
378b7ba8371SEric Paris 
379b7ba8371SEric Paris 	return ret;
380b7ba8371SEric Paris }
381b7ba8371SEric Paris 
382b7ba8371SEric Paris static struct inotify_inode_mark_entry *inotify_idr_find_locked(struct fsnotify_group *group,
383b7ba8371SEric Paris 								int wd)
384b7ba8371SEric Paris {
385b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
386b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
387b7ba8371SEric Paris 	struct inotify_inode_mark_entry *ientry;
388b7ba8371SEric Paris 
389b7ba8371SEric Paris 	assert_spin_locked(idr_lock);
390b7ba8371SEric Paris 
391b7ba8371SEric Paris 	ientry = idr_find(idr, wd);
392b7ba8371SEric Paris 	if (ientry) {
393b7ba8371SEric Paris 		struct fsnotify_mark_entry *fsn_entry = &ientry->fsn_entry;
394b7ba8371SEric Paris 
395b7ba8371SEric Paris 		fsnotify_get_mark(fsn_entry);
396b7ba8371SEric Paris 		/* One ref for being in the idr, one ref we just took */
397b7ba8371SEric Paris 		BUG_ON(atomic_read(&fsn_entry->refcnt) < 2);
398b7ba8371SEric Paris 	}
399b7ba8371SEric Paris 
400b7ba8371SEric Paris 	return ientry;
401b7ba8371SEric Paris }
402b7ba8371SEric Paris 
403b7ba8371SEric Paris static struct inotify_inode_mark_entry *inotify_idr_find(struct fsnotify_group *group,
404b7ba8371SEric Paris 							 int wd)
405b7ba8371SEric Paris {
406b7ba8371SEric Paris 	struct inotify_inode_mark_entry *ientry;
407b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
408b7ba8371SEric Paris 
409b7ba8371SEric Paris 	spin_lock(idr_lock);
410b7ba8371SEric Paris 	ientry = inotify_idr_find_locked(group, wd);
411b7ba8371SEric Paris 	spin_unlock(idr_lock);
412b7ba8371SEric Paris 
413b7ba8371SEric Paris 	return ientry;
414b7ba8371SEric Paris }
415b7ba8371SEric Paris 
416b7ba8371SEric Paris static void do_inotify_remove_from_idr(struct fsnotify_group *group,
417b7ba8371SEric Paris 				       struct inotify_inode_mark_entry *ientry)
418b7ba8371SEric Paris {
419b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
420b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
421b7ba8371SEric Paris 	int wd = ientry->wd;
422b7ba8371SEric Paris 
423b7ba8371SEric Paris 	assert_spin_locked(idr_lock);
424b7ba8371SEric Paris 
425b7ba8371SEric Paris 	idr_remove(idr, wd);
426b7ba8371SEric Paris 
427b7ba8371SEric Paris 	/* removed from the idr, drop that ref */
428b7ba8371SEric Paris 	fsnotify_put_mark(&ientry->fsn_entry);
429b7ba8371SEric Paris }
430b7ba8371SEric Paris 
431dead537dSEric Paris /*
432dead537dSEric Paris  * Remove the mark from the idr (if present) and drop the reference
433dead537dSEric Paris  * on the mark because it was in the idr.
434dead537dSEric Paris  */
4357e790dd5SEric Paris static void inotify_remove_from_idr(struct fsnotify_group *group,
4367e790dd5SEric Paris 				    struct inotify_inode_mark_entry *ientry)
4377e790dd5SEric Paris {
438b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
439b7ba8371SEric Paris 	struct inotify_inode_mark_entry *found_ientry = NULL;
440dead537dSEric Paris 	int wd;
4417e790dd5SEric Paris 
442b7ba8371SEric Paris 	spin_lock(idr_lock);
443dead537dSEric Paris 	wd = ientry->wd;
444dead537dSEric Paris 
445b7ba8371SEric Paris 	/*
446b7ba8371SEric Paris 	 * does this ientry think it is in the idr?  we shouldn't get called
447b7ba8371SEric Paris 	 * if it wasn't....
448b7ba8371SEric Paris 	 */
449b7ba8371SEric Paris 	if (wd == -1) {
450b7ba8371SEric Paris 		printk(KERN_WARNING "%s: ientry=%p ientry->wd=%d ientry->group=%p"
451b7ba8371SEric Paris 			" ientry->inode=%p\n", __func__, ientry, ientry->wd,
452b7ba8371SEric Paris 			ientry->fsn_entry.group, ientry->fsn_entry.inode);
453dead537dSEric Paris 		WARN_ON(1);
454dead537dSEric Paris 		goto out;
4557e790dd5SEric Paris 	}
456dead537dSEric Paris 
457b7ba8371SEric Paris 	/* Lets look in the idr to see if we find it */
458b7ba8371SEric Paris 	found_ientry = inotify_idr_find_locked(group, wd);
459b7ba8371SEric Paris 	if (unlikely(!found_ientry)) {
460b7ba8371SEric Paris 		printk(KERN_WARNING "%s: ientry=%p ientry->wd=%d ientry->group=%p"
461b7ba8371SEric Paris 			" ientry->inode=%p\n", __func__, ientry, ientry->wd,
462b7ba8371SEric Paris 			ientry->fsn_entry.group, ientry->fsn_entry.inode);
463b7ba8371SEric Paris 		WARN_ON(1);
464b7ba8371SEric Paris 		goto out;
465b7ba8371SEric Paris 	}
466dead537dSEric Paris 
467b7ba8371SEric Paris 	/*
468b7ba8371SEric Paris 	 * We found an entry in the idr at the right wd, but it's
469b7ba8371SEric Paris 	 * not the entry we were told to remove.  eparis seriously
470b7ba8371SEric Paris 	 * fucked up somewhere.
471b7ba8371SEric Paris 	 */
472b7ba8371SEric Paris 	if (unlikely(found_ientry != ientry)) {
473b7ba8371SEric Paris 		WARN_ON(1);
474b7ba8371SEric Paris 		printk(KERN_WARNING "%s: ientry=%p ientry->wd=%d ientry->group=%p "
475b7ba8371SEric Paris 			"entry->inode=%p found_ientry=%p found_ientry->wd=%d "
476b7ba8371SEric Paris 			"found_ientry->group=%p found_ientry->inode=%p\n",
477b7ba8371SEric Paris 			__func__, ientry, ientry->wd, ientry->fsn_entry.group,
478b7ba8371SEric Paris 			ientry->fsn_entry.inode, found_ientry, found_ientry->wd,
479b7ba8371SEric Paris 			found_ientry->fsn_entry.group,
480b7ba8371SEric Paris 			found_ientry->fsn_entry.inode);
481b7ba8371SEric Paris 		goto out;
482b7ba8371SEric Paris 	}
483dead537dSEric Paris 
484b7ba8371SEric Paris 	/*
485b7ba8371SEric Paris 	 * One ref for being in the idr
486b7ba8371SEric Paris 	 * one ref held by the caller trying to kill us
487b7ba8371SEric Paris 	 * one ref grabbed by inotify_idr_find
488b7ba8371SEric Paris 	 */
489b7ba8371SEric Paris 	if (unlikely(atomic_read(&ientry->fsn_entry.refcnt) < 3)) {
490b7ba8371SEric Paris 		printk(KERN_WARNING "%s: ientry=%p ientry->wd=%d ientry->group=%p"
491b7ba8371SEric Paris 			" ientry->inode=%p\n", __func__, ientry, ientry->wd,
492b7ba8371SEric Paris 			ientry->fsn_entry.group, ientry->fsn_entry.inode);
493b7ba8371SEric Paris 		/* we can't really recover with bad ref cnting.. */
494b7ba8371SEric Paris 		BUG();
495b7ba8371SEric Paris 	}
496b7ba8371SEric Paris 
497b7ba8371SEric Paris 	do_inotify_remove_from_idr(group, ientry);
498dead537dSEric Paris out:
499b7ba8371SEric Paris 	/* match the ref taken by inotify_idr_find_locked() */
500b7ba8371SEric Paris 	if (found_ientry)
501b7ba8371SEric Paris 		fsnotify_put_mark(&found_ientry->fsn_entry);
502b7ba8371SEric Paris 	ientry->wd = -1;
503b7ba8371SEric Paris 	spin_unlock(idr_lock);
504dead537dSEric Paris }
505dead537dSEric Paris 
50663c882a0SEric Paris /*
507dead537dSEric Paris  * Send IN_IGNORED for this wd, remove this wd from the idr.
50863c882a0SEric Paris  */
509528da3e9SEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry,
510528da3e9SEric Paris 				    struct fsnotify_group *group)
51163c882a0SEric Paris {
51263c882a0SEric Paris 	struct inotify_inode_mark_entry *ientry;
513f44aebccSEric Paris 	struct fsnotify_event *ignored_event;
51463c882a0SEric Paris 	struct inotify_event_private_data *event_priv;
51563c882a0SEric Paris 	struct fsnotify_event_private_data *fsn_event_priv;
516eef3a116SEric Paris 	int ret;
51763c882a0SEric Paris 
518f44aebccSEric Paris 	ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL,
519f44aebccSEric Paris 					      FSNOTIFY_EVENT_NONE, NULL, 0,
520f44aebccSEric Paris 					      GFP_NOFS);
521f44aebccSEric Paris 	if (!ignored_event)
522f44aebccSEric Paris 		return;
523f44aebccSEric Paris 
52463c882a0SEric Paris 	ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
52563c882a0SEric Paris 
526f44aebccSEric Paris 	event_priv = kmem_cache_alloc(event_priv_cachep, GFP_NOFS);
52763c882a0SEric Paris 	if (unlikely(!event_priv))
52863c882a0SEric Paris 		goto skip_send_ignore;
52963c882a0SEric Paris 
53063c882a0SEric Paris 	fsn_event_priv = &event_priv->fsnotify_event_priv_data;
53163c882a0SEric Paris 
53263c882a0SEric Paris 	fsn_event_priv->group = group;
53363c882a0SEric Paris 	event_priv->wd = ientry->wd;
53463c882a0SEric Paris 
535eef3a116SEric Paris 	ret = fsnotify_add_notify_event(group, ignored_event, fsn_event_priv);
536eef3a116SEric Paris 	if (ret)
53763c882a0SEric Paris 		inotify_free_event_priv(fsn_event_priv);
53863c882a0SEric Paris 
53963c882a0SEric Paris skip_send_ignore:
54063c882a0SEric Paris 
541f44aebccSEric Paris 	/* matches the reference taken when the event was created */
542f44aebccSEric Paris 	fsnotify_put_event(ignored_event);
543f44aebccSEric Paris 
54463c882a0SEric Paris 	/* remove this entry from the idr */
5457e790dd5SEric Paris 	inotify_remove_from_idr(group, ientry);
54663c882a0SEric Paris 
5475549f7cdSEric Paris 	atomic_dec(&group->inotify_data.user->inotify_watches);
54863c882a0SEric Paris }
54963c882a0SEric Paris 
55063c882a0SEric Paris /* ding dong the mark is dead */
55163c882a0SEric Paris static void inotify_free_mark(struct fsnotify_mark_entry *entry)
55263c882a0SEric Paris {
553*31ddd326SEric Paris 	struct inotify_inode_mark_entry *ientry;
554*31ddd326SEric Paris 
555*31ddd326SEric Paris 	ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
55663c882a0SEric Paris 
55763c882a0SEric Paris 	kmem_cache_free(inotify_inode_mark_cachep, ientry);
55863c882a0SEric Paris }
55963c882a0SEric Paris 
56052cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group,
56152cef755SEric Paris 					 struct inode *inode,
56252cef755SEric Paris 					 u32 arg)
56363c882a0SEric Paris {
56452cef755SEric Paris 	struct fsnotify_mark_entry *entry;
56563c882a0SEric Paris 	struct inotify_inode_mark_entry *ientry;
56663c882a0SEric Paris 	__u32 old_mask, new_mask;
56752cef755SEric Paris 	__u32 mask;
56852cef755SEric Paris 	int add = (arg & IN_MASK_ADD);
56952cef755SEric Paris 	int ret;
57063c882a0SEric Paris 
57163c882a0SEric Paris 	/* don't allow invalid bits: we don't want flags set */
57263c882a0SEric Paris 	mask = inotify_arg_to_mask(arg);
57363c882a0SEric Paris 	if (unlikely(!mask))
57463c882a0SEric Paris 		return -EINVAL;
57563c882a0SEric Paris 
57663c882a0SEric Paris 	spin_lock(&inode->i_lock);
57763c882a0SEric Paris 	entry = fsnotify_find_mark_entry(group, inode);
57863c882a0SEric Paris 	spin_unlock(&inode->i_lock);
57952cef755SEric Paris 	if (!entry)
58052cef755SEric Paris 		return -ENOENT;
58152cef755SEric Paris 
58263c882a0SEric Paris 	ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
58375fe2b26SEric Paris 
58463c882a0SEric Paris 	spin_lock(&entry->lock);
58563c882a0SEric Paris 
58663c882a0SEric Paris 	old_mask = entry->mask;
58763c882a0SEric Paris 	if (add) {
58863c882a0SEric Paris 		entry->mask |= mask;
58963c882a0SEric Paris 		new_mask = entry->mask;
59063c882a0SEric Paris 	} else {
59163c882a0SEric Paris 		entry->mask = mask;
59263c882a0SEric Paris 		new_mask = entry->mask;
59363c882a0SEric Paris 	}
59463c882a0SEric Paris 
59563c882a0SEric Paris 	spin_unlock(&entry->lock);
59663c882a0SEric Paris 
59763c882a0SEric Paris 	if (old_mask != new_mask) {
59863c882a0SEric Paris 		/* more bits in old than in new? */
59963c882a0SEric Paris 		int dropped = (old_mask & ~new_mask);
60063c882a0SEric Paris 		/* more bits in this entry than the inode's mask? */
60163c882a0SEric Paris 		int do_inode = (new_mask & ~inode->i_fsnotify_mask);
60263c882a0SEric Paris 		/* more bits in this entry than the group? */
60363c882a0SEric Paris 		int do_group = (new_mask & ~group->mask);
60463c882a0SEric Paris 
60563c882a0SEric Paris 		/* update the inode with this new entry */
60663c882a0SEric Paris 		if (dropped || do_inode)
60763c882a0SEric Paris 			fsnotify_recalc_inode_mask(inode);
60863c882a0SEric Paris 
60963c882a0SEric Paris 		/* update the group mask with the new mask */
61063c882a0SEric Paris 		if (dropped || do_group)
61163c882a0SEric Paris 			fsnotify_recalc_group_mask(group);
61263c882a0SEric Paris 	}
61363c882a0SEric Paris 
61452cef755SEric Paris 	/* return the wd */
61552cef755SEric Paris 	ret = ientry->wd;
61652cef755SEric Paris 
61752cef755SEric Paris 	/* match the get from fsnotify_find_mark_entry() */
61875fe2b26SEric Paris 	fsnotify_put_mark(entry);
61975fe2b26SEric Paris 
62052cef755SEric Paris 	return ret;
62163c882a0SEric Paris }
6227e790dd5SEric Paris 
62352cef755SEric Paris static int inotify_new_watch(struct fsnotify_group *group,
62452cef755SEric Paris 			     struct inode *inode,
62552cef755SEric Paris 			     u32 arg)
62652cef755SEric Paris {
62752cef755SEric Paris 	struct inotify_inode_mark_entry *tmp_ientry;
62852cef755SEric Paris 	__u32 mask;
62952cef755SEric Paris 	int ret;
630b7ba8371SEric Paris 	struct idr *idr = &group->inotify_data.idr;
631b7ba8371SEric Paris 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
63252cef755SEric Paris 
63352cef755SEric Paris 	/* don't allow invalid bits: we don't want flags set */
63452cef755SEric Paris 	mask = inotify_arg_to_mask(arg);
63552cef755SEric Paris 	if (unlikely(!mask))
63652cef755SEric Paris 		return -EINVAL;
63752cef755SEric Paris 
63852cef755SEric Paris 	tmp_ientry = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
63952cef755SEric Paris 	if (unlikely(!tmp_ientry))
64052cef755SEric Paris 		return -ENOMEM;
64152cef755SEric Paris 
64252cef755SEric Paris 	fsnotify_init_mark(&tmp_ientry->fsn_entry, inotify_free_mark);
64352cef755SEric Paris 	tmp_ientry->fsn_entry.mask = mask;
64452cef755SEric Paris 	tmp_ientry->wd = -1;
64552cef755SEric Paris 
64652cef755SEric Paris 	ret = -ENOSPC;
64752cef755SEric Paris 	if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
64852cef755SEric Paris 		goto out_err;
649b7ba8371SEric Paris 
650b7ba8371SEric Paris 	ret = inotify_add_to_idr(idr, idr_lock, group->inotify_data.last_wd,
651b7ba8371SEric Paris 				 tmp_ientry);
652b7ba8371SEric Paris 	if (ret)
65352cef755SEric Paris 		goto out_err;
65452cef755SEric Paris 
65552cef755SEric Paris 	/* we are on the idr, now get on the inode */
65640554c3dSEric Paris 	ret = fsnotify_add_mark(&tmp_ientry->fsn_entry, group, inode, 0);
65752cef755SEric Paris 	if (ret) {
65852cef755SEric Paris 		/* we failed to get on the inode, get off the idr */
65952cef755SEric Paris 		inotify_remove_from_idr(group, tmp_ientry);
66052cef755SEric Paris 		goto out_err;
66152cef755SEric Paris 	}
66252cef755SEric Paris 
66352cef755SEric Paris 	/* update the idr hint, who cares about races, it's just a hint */
66452cef755SEric Paris 	group->inotify_data.last_wd = tmp_ientry->wd;
66552cef755SEric Paris 
66652cef755SEric Paris 	/* increment the number of watches the user has */
66752cef755SEric Paris 	atomic_inc(&group->inotify_data.user->inotify_watches);
66852cef755SEric Paris 
66952cef755SEric Paris 	/* return the watch descriptor for this new entry */
67052cef755SEric Paris 	ret = tmp_ientry->wd;
67152cef755SEric Paris 
672750a8870SEric Paris 	/* if this mark added a new event update the group mask */
673750a8870SEric Paris 	if (mask & ~group->mask)
674750a8870SEric Paris 		fsnotify_recalc_group_mask(group);
675750a8870SEric Paris 
67652cef755SEric Paris out_err:
6773dbc6fb6SEric Paris 	/* match the ref from fsnotify_init_markentry() */
6783dbc6fb6SEric Paris 	fsnotify_put_mark(&tmp_ientry->fsn_entry);
67952cef755SEric Paris 
68052cef755SEric Paris 	return ret;
68152cef755SEric Paris }
68252cef755SEric Paris 
68352cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
68452cef755SEric Paris {
68552cef755SEric Paris 	int ret = 0;
68652cef755SEric Paris 
68752cef755SEric Paris retry:
68852cef755SEric Paris 	/* try to update and existing watch with the new arg */
68952cef755SEric Paris 	ret = inotify_update_existing_watch(group, inode, arg);
69052cef755SEric Paris 	/* no mark present, try to add a new one */
69152cef755SEric Paris 	if (ret == -ENOENT)
69252cef755SEric Paris 		ret = inotify_new_watch(group, inode, arg);
69352cef755SEric Paris 	/*
69452cef755SEric Paris 	 * inotify_new_watch could race with another thread which did an
69552cef755SEric Paris 	 * inotify_new_watch between the update_existing and the add watch
69652cef755SEric Paris 	 * here, go back and try to update an existing mark again.
69752cef755SEric Paris 	 */
69852cef755SEric Paris 	if (ret == -EEXIST)
69952cef755SEric Paris 		goto retry;
70052cef755SEric Paris 
70163c882a0SEric Paris 	return ret;
70263c882a0SEric Paris }
70363c882a0SEric Paris 
70463c882a0SEric Paris static struct fsnotify_group *inotify_new_group(struct user_struct *user, unsigned int max_events)
70563c882a0SEric Paris {
70663c882a0SEric Paris 	struct fsnotify_group *group;
70763c882a0SEric Paris 	unsigned int grp_num;
70863c882a0SEric Paris 
70963c882a0SEric Paris 	/* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
71063c882a0SEric Paris 	grp_num = (INOTIFY_GROUP_NUM - atomic_inc_return(&inotify_grp_num));
71163c882a0SEric Paris 	group = fsnotify_obtain_group(grp_num, 0, &inotify_fsnotify_ops);
71263c882a0SEric Paris 	if (IS_ERR(group))
71363c882a0SEric Paris 		return group;
71463c882a0SEric Paris 
71563c882a0SEric Paris 	group->max_events = max_events;
71663c882a0SEric Paris 
71763c882a0SEric Paris 	spin_lock_init(&group->inotify_data.idr_lock);
71863c882a0SEric Paris 	idr_init(&group->inotify_data.idr);
7199e572cc9SEric Paris 	group->inotify_data.last_wd = 0;
72063c882a0SEric Paris 	group->inotify_data.user = user;
72163c882a0SEric Paris 	group->inotify_data.fa = NULL;
72263c882a0SEric Paris 
72363c882a0SEric Paris 	return group;
72463c882a0SEric Paris }
72563c882a0SEric Paris 
72663c882a0SEric Paris 
72763c882a0SEric Paris /* inotify syscalls */
728938bb9f5SHeiko Carstens SYSCALL_DEFINE1(inotify_init1, int, flags)
729272eb014SEric Paris {
73063c882a0SEric Paris 	struct fsnotify_group *group;
731272eb014SEric Paris 	struct user_struct *user;
732c44dcc56SAl Viro 	int ret;
733272eb014SEric Paris 
734272eb014SEric Paris 	/* Check the IN_* constants for consistency.  */
735272eb014SEric Paris 	BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
736272eb014SEric Paris 	BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
737272eb014SEric Paris 
738272eb014SEric Paris 	if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
739272eb014SEric Paris 		return -EINVAL;
740272eb014SEric Paris 
741272eb014SEric Paris 	user = get_current_user();
742272eb014SEric Paris 	if (unlikely(atomic_read(&user->inotify_devs) >=
743272eb014SEric Paris 			inotify_max_user_instances)) {
744272eb014SEric Paris 		ret = -EMFILE;
745272eb014SEric Paris 		goto out_free_uid;
746272eb014SEric Paris 	}
747272eb014SEric Paris 
74863c882a0SEric Paris 	/* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
74963c882a0SEric Paris 	group = inotify_new_group(user, inotify_max_queued_events);
75063c882a0SEric Paris 	if (IS_ERR(group)) {
75163c882a0SEric Paris 		ret = PTR_ERR(group);
752272eb014SEric Paris 		goto out_free_uid;
753272eb014SEric Paris 	}
754272eb014SEric Paris 
755825f9692SAl Viro 	atomic_inc(&user->inotify_devs);
756825f9692SAl Viro 
757c44dcc56SAl Viro 	ret = anon_inode_getfd("inotify", &inotify_fops, group,
758c44dcc56SAl Viro 				  O_RDONLY | flags);
759c44dcc56SAl Viro 	if (ret >= 0)
760c44dcc56SAl Viro 		return ret;
761825f9692SAl Viro 
762825f9692SAl Viro 	atomic_dec(&user->inotify_devs);
763272eb014SEric Paris out_free_uid:
764272eb014SEric Paris 	free_uid(user);
765272eb014SEric Paris 	return ret;
766272eb014SEric Paris }
767272eb014SEric Paris 
768938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init)
769272eb014SEric Paris {
770272eb014SEric Paris 	return sys_inotify_init1(0);
771272eb014SEric Paris }
772272eb014SEric Paris 
7732e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
7742e4d0924SHeiko Carstens 		u32, mask)
775272eb014SEric Paris {
77663c882a0SEric Paris 	struct fsnotify_group *group;
777272eb014SEric Paris 	struct inode *inode;
778272eb014SEric Paris 	struct path path;
779272eb014SEric Paris 	struct file *filp;
780272eb014SEric Paris 	int ret, fput_needed;
781272eb014SEric Paris 	unsigned flags = 0;
782272eb014SEric Paris 
783272eb014SEric Paris 	filp = fget_light(fd, &fput_needed);
784272eb014SEric Paris 	if (unlikely(!filp))
785272eb014SEric Paris 		return -EBADF;
786272eb014SEric Paris 
787272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
788272eb014SEric Paris 	if (unlikely(filp->f_op != &inotify_fops)) {
789272eb014SEric Paris 		ret = -EINVAL;
790272eb014SEric Paris 		goto fput_and_out;
791272eb014SEric Paris 	}
792272eb014SEric Paris 
793272eb014SEric Paris 	if (!(mask & IN_DONT_FOLLOW))
794272eb014SEric Paris 		flags |= LOOKUP_FOLLOW;
795272eb014SEric Paris 	if (mask & IN_ONLYDIR)
796272eb014SEric Paris 		flags |= LOOKUP_DIRECTORY;
797272eb014SEric Paris 
79863c882a0SEric Paris 	ret = inotify_find_inode(pathname, &path, flags);
79963c882a0SEric Paris 	if (ret)
800272eb014SEric Paris 		goto fput_and_out;
801272eb014SEric Paris 
80263c882a0SEric Paris 	/* inode held in place by reference to path; group by fget on fd */
803272eb014SEric Paris 	inode = path.dentry->d_inode;
80463c882a0SEric Paris 	group = filp->private_data;
805272eb014SEric Paris 
80663c882a0SEric Paris 	/* create/update an inode mark */
80763c882a0SEric Paris 	ret = inotify_update_watch(group, inode, mask);
808272eb014SEric Paris 	path_put(&path);
809272eb014SEric Paris fput_and_out:
810272eb014SEric Paris 	fput_light(filp, fput_needed);
811272eb014SEric Paris 	return ret;
812272eb014SEric Paris }
813272eb014SEric Paris 
8142e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
815272eb014SEric Paris {
81663c882a0SEric Paris 	struct fsnotify_group *group;
817b7ba8371SEric Paris 	struct inotify_inode_mark_entry *ientry;
818272eb014SEric Paris 	struct file *filp;
81963c882a0SEric Paris 	int ret = 0, fput_needed;
820272eb014SEric Paris 
821272eb014SEric Paris 	filp = fget_light(fd, &fput_needed);
822272eb014SEric Paris 	if (unlikely(!filp))
823272eb014SEric Paris 		return -EBADF;
824272eb014SEric Paris 
825272eb014SEric Paris 	/* verify that this is indeed an inotify instance */
826272eb014SEric Paris 	ret = -EINVAL;
827b7ba8371SEric Paris 	if (unlikely(filp->f_op != &inotify_fops))
828272eb014SEric Paris 		goto out;
829272eb014SEric Paris 
83063c882a0SEric Paris 	group = filp->private_data;
831272eb014SEric Paris 
83263c882a0SEric Paris 	ret = -EINVAL;
833b7ba8371SEric Paris 	ientry = inotify_idr_find(group, wd);
834b7ba8371SEric Paris 	if (unlikely(!ientry))
83563c882a0SEric Paris 		goto out;
83663c882a0SEric Paris 
837b7ba8371SEric Paris 	ret = 0;
838b7ba8371SEric Paris 
839b7ba8371SEric Paris 	fsnotify_destroy_mark_by_entry(&ientry->fsn_entry);
840b7ba8371SEric Paris 
841b7ba8371SEric Paris 	/* match ref taken by inotify_idr_find */
842b7ba8371SEric Paris 	fsnotify_put_mark(&ientry->fsn_entry);
843272eb014SEric Paris 
844272eb014SEric Paris out:
845272eb014SEric Paris 	fput_light(filp, fput_needed);
846272eb014SEric Paris 	return ret;
847272eb014SEric Paris }
848272eb014SEric Paris 
849272eb014SEric Paris /*
850272eb014SEric Paris  * inotify_user_setup - Our initialization function.  Note that we cannnot return
851272eb014SEric Paris  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
852272eb014SEric Paris  * must result in panic().
853272eb014SEric Paris  */
854272eb014SEric Paris static int __init inotify_user_setup(void)
855272eb014SEric Paris {
85663c882a0SEric Paris 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark_entry, SLAB_PANIC);
85763c882a0SEric Paris 	event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC);
85863c882a0SEric Paris 
859272eb014SEric Paris 	inotify_max_queued_events = 16384;
860272eb014SEric Paris 	inotify_max_user_instances = 128;
861272eb014SEric Paris 	inotify_max_user_watches = 8192;
862272eb014SEric Paris 
863272eb014SEric Paris 	return 0;
864272eb014SEric Paris }
865272eb014SEric Paris module_init(inotify_user_setup);
866