18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * fs/inotify_user.c - inotify support for userspace
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors:
68c2ecf20Sopenharmony_ci *	John McCutchan	<ttb@tentacle.dhs.org>
78c2ecf20Sopenharmony_ci *	Robert Love	<rml@novell.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2005 John McCutchan
108c2ecf20Sopenharmony_ci * Copyright 2006 Hewlett-Packard Development Company, L.P.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Copyright (C) 2009 Eric Paris <Red Hat Inc>
138c2ecf20Sopenharmony_ci * inotify was largely rewriten to make use of the fsnotify infrastructure
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/file.h>
178c2ecf20Sopenharmony_ci#include <linux/fs.h> /* struct inode */
188c2ecf20Sopenharmony_ci#include <linux/fsnotify_backend.h>
198c2ecf20Sopenharmony_ci#include <linux/idr.h>
208c2ecf20Sopenharmony_ci#include <linux/init.h> /* fs_initcall */
218c2ecf20Sopenharmony_ci#include <linux/inotify.h>
228c2ecf20Sopenharmony_ci#include <linux/kernel.h> /* roundup() */
238c2ecf20Sopenharmony_ci#include <linux/namei.h> /* LOOKUP_FOLLOW */
248c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
258c2ecf20Sopenharmony_ci#include <linux/slab.h> /* struct kmem_cache */
268c2ecf20Sopenharmony_ci#include <linux/syscalls.h>
278c2ecf20Sopenharmony_ci#include <linux/types.h>
288c2ecf20Sopenharmony_ci#include <linux/anon_inodes.h>
298c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
308c2ecf20Sopenharmony_ci#include <linux/poll.h>
318c2ecf20Sopenharmony_ci#include <linux/wait.h>
328c2ecf20Sopenharmony_ci#include <linux/memcontrol.h>
338c2ecf20Sopenharmony_ci#include <linux/security.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include "inotify.h"
368c2ecf20Sopenharmony_ci#include "../fdinfo.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include <asm/ioctls.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* configurable via /proc/sys/fs/inotify/ */
418c2ecf20Sopenharmony_cistatic int inotify_max_queued_events __read_mostly;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistruct kmem_cache *inotify_inode_mark_cachep __read_mostly;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSCTL
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#include <linux/sysctl.h>
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct ctl_table inotify_table[] = {
508c2ecf20Sopenharmony_ci	{
518c2ecf20Sopenharmony_ci		.procname	= "max_user_instances",
528c2ecf20Sopenharmony_ci		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
538c2ecf20Sopenharmony_ci		.maxlen		= sizeof(int),
548c2ecf20Sopenharmony_ci		.mode		= 0644,
558c2ecf20Sopenharmony_ci		.proc_handler	= proc_dointvec_minmax,
568c2ecf20Sopenharmony_ci		.extra1		= SYSCTL_ZERO,
578c2ecf20Sopenharmony_ci	},
588c2ecf20Sopenharmony_ci	{
598c2ecf20Sopenharmony_ci		.procname	= "max_user_watches",
608c2ecf20Sopenharmony_ci		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
618c2ecf20Sopenharmony_ci		.maxlen		= sizeof(int),
628c2ecf20Sopenharmony_ci		.mode		= 0644,
638c2ecf20Sopenharmony_ci		.proc_handler	= proc_dointvec_minmax,
648c2ecf20Sopenharmony_ci		.extra1		= SYSCTL_ZERO,
658c2ecf20Sopenharmony_ci	},
668c2ecf20Sopenharmony_ci	{
678c2ecf20Sopenharmony_ci		.procname	= "max_queued_events",
688c2ecf20Sopenharmony_ci		.data		= &inotify_max_queued_events,
698c2ecf20Sopenharmony_ci		.maxlen		= sizeof(int),
708c2ecf20Sopenharmony_ci		.mode		= 0644,
718c2ecf20Sopenharmony_ci		.proc_handler	= proc_dointvec_minmax,
728c2ecf20Sopenharmony_ci		.extra1		= SYSCTL_ZERO
738c2ecf20Sopenharmony_ci	},
748c2ecf20Sopenharmony_ci	{ }
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci#endif /* CONFIG_SYSCTL */
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	__u32 mask;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	/*
838c2ecf20Sopenharmony_ci	 * Everything should accept their own ignored and should receive events
848c2ecf20Sopenharmony_ci	 * when the inode is unmounted.  All directories care about children.
858c2ecf20Sopenharmony_ci	 */
868c2ecf20Sopenharmony_ci	mask = (FS_IN_IGNORED | FS_UNMOUNT);
878c2ecf20Sopenharmony_ci	if (S_ISDIR(inode->i_mode))
888c2ecf20Sopenharmony_ci		mask |= FS_EVENT_ON_CHILD;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/* mask off the flags used to open the fd */
918c2ecf20Sopenharmony_ci	mask |= (arg & INOTIFY_USER_MASK);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	return mask;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic inline u32 inotify_mask_to_arg(__u32 mask)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
998c2ecf20Sopenharmony_ci		       IN_Q_OVERFLOW);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/* intofiy userspace file descriptor functions */
1038c2ecf20Sopenharmony_cistatic __poll_t inotify_poll(struct file *file, poll_table *wait)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct fsnotify_group *group = file->private_data;
1068c2ecf20Sopenharmony_ci	__poll_t ret = 0;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	poll_wait(file, &group->notification_waitq, wait);
1098c2ecf20Sopenharmony_ci	spin_lock(&group->notification_lock);
1108c2ecf20Sopenharmony_ci	if (!fsnotify_notify_queue_is_empty(group))
1118c2ecf20Sopenharmony_ci		ret = EPOLLIN | EPOLLRDNORM;
1128c2ecf20Sopenharmony_ci	spin_unlock(&group->notification_lock);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return ret;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int round_event_name_len(struct fsnotify_event *fsn_event)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct inotify_event_info *event;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	event = INOTIFY_E(fsn_event);
1228c2ecf20Sopenharmony_ci	if (!event->name_len)
1238c2ecf20Sopenharmony_ci		return 0;
1248c2ecf20Sopenharmony_ci	return roundup(event->name_len + 1, sizeof(struct inotify_event));
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * Get an inotify_kernel_event if one exists and is small
1298c2ecf20Sopenharmony_ci * enough to fit in "count". Return an error pointer if
1308c2ecf20Sopenharmony_ci * not large enough.
1318c2ecf20Sopenharmony_ci *
1328c2ecf20Sopenharmony_ci * Called with the group->notification_lock held.
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_cistatic struct fsnotify_event *get_one_event(struct fsnotify_group *group,
1358c2ecf20Sopenharmony_ci					    size_t count)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	size_t event_size = sizeof(struct inotify_event);
1388c2ecf20Sopenharmony_ci	struct fsnotify_event *event;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (fsnotify_notify_queue_is_empty(group))
1418c2ecf20Sopenharmony_ci		return NULL;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	event = fsnotify_peek_first_event(group);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	event_size += round_event_name_len(event);
1488c2ecf20Sopenharmony_ci	if (event_size > count)
1498c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/* held the notification_lock the whole time, so this is the
1528c2ecf20Sopenharmony_ci	 * same event we peeked above */
1538c2ecf20Sopenharmony_ci	fsnotify_remove_first_event(group);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	return event;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/*
1598c2ecf20Sopenharmony_ci * Copy an event to user space, returning how much we copied.
1608c2ecf20Sopenharmony_ci *
1618c2ecf20Sopenharmony_ci * We already checked that the event size is smaller than the
1628c2ecf20Sopenharmony_ci * buffer we had in "get_one_event()" above.
1638c2ecf20Sopenharmony_ci */
1648c2ecf20Sopenharmony_cistatic ssize_t copy_event_to_user(struct fsnotify_group *group,
1658c2ecf20Sopenharmony_ci				  struct fsnotify_event *fsn_event,
1668c2ecf20Sopenharmony_ci				  char __user *buf)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct inotify_event inotify_event;
1698c2ecf20Sopenharmony_ci	struct inotify_event_info *event;
1708c2ecf20Sopenharmony_ci	size_t event_size = sizeof(struct inotify_event);
1718c2ecf20Sopenharmony_ci	size_t name_len;
1728c2ecf20Sopenharmony_ci	size_t pad_name_len;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	event = INOTIFY_E(fsn_event);
1778c2ecf20Sopenharmony_ci	name_len = event->name_len;
1788c2ecf20Sopenharmony_ci	/*
1798c2ecf20Sopenharmony_ci	 * round up name length so it is a multiple of event_size
1808c2ecf20Sopenharmony_ci	 * plus an extra byte for the terminating '\0'.
1818c2ecf20Sopenharmony_ci	 */
1828c2ecf20Sopenharmony_ci	pad_name_len = round_event_name_len(fsn_event);
1838c2ecf20Sopenharmony_ci	inotify_event.len = pad_name_len;
1848c2ecf20Sopenharmony_ci	inotify_event.mask = inotify_mask_to_arg(event->mask);
1858c2ecf20Sopenharmony_ci	inotify_event.wd = event->wd;
1868c2ecf20Sopenharmony_ci	inotify_event.cookie = event->sync_cookie;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/* send the main event */
1898c2ecf20Sopenharmony_ci	if (copy_to_user(buf, &inotify_event, event_size))
1908c2ecf20Sopenharmony_ci		return -EFAULT;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	buf += event_size;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/*
1958c2ecf20Sopenharmony_ci	 * fsnotify only stores the pathname, so here we have to send the pathname
1968c2ecf20Sopenharmony_ci	 * and then pad that pathname out to a multiple of sizeof(inotify_event)
1978c2ecf20Sopenharmony_ci	 * with zeros.
1988c2ecf20Sopenharmony_ci	 */
1998c2ecf20Sopenharmony_ci	if (pad_name_len) {
2008c2ecf20Sopenharmony_ci		/* copy the path name */
2018c2ecf20Sopenharmony_ci		if (copy_to_user(buf, event->name, name_len))
2028c2ecf20Sopenharmony_ci			return -EFAULT;
2038c2ecf20Sopenharmony_ci		buf += name_len;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci		/* fill userspace with 0's */
2068c2ecf20Sopenharmony_ci		if (clear_user(buf, pad_name_len - name_len))
2078c2ecf20Sopenharmony_ci			return -EFAULT;
2088c2ecf20Sopenharmony_ci		event_size += pad_name_len;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	return event_size;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic ssize_t inotify_read(struct file *file, char __user *buf,
2158c2ecf20Sopenharmony_ci			    size_t count, loff_t *pos)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	struct fsnotify_group *group;
2188c2ecf20Sopenharmony_ci	struct fsnotify_event *kevent;
2198c2ecf20Sopenharmony_ci	char __user *start;
2208c2ecf20Sopenharmony_ci	int ret;
2218c2ecf20Sopenharmony_ci	DEFINE_WAIT_FUNC(wait, woken_wake_function);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	start = buf;
2248c2ecf20Sopenharmony_ci	group = file->private_data;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	add_wait_queue(&group->notification_waitq, &wait);
2278c2ecf20Sopenharmony_ci	while (1) {
2288c2ecf20Sopenharmony_ci		spin_lock(&group->notification_lock);
2298c2ecf20Sopenharmony_ci		kevent = get_one_event(group, count);
2308c2ecf20Sopenharmony_ci		spin_unlock(&group->notification_lock);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		if (kevent) {
2358c2ecf20Sopenharmony_ci			ret = PTR_ERR(kevent);
2368c2ecf20Sopenharmony_ci			if (IS_ERR(kevent))
2378c2ecf20Sopenharmony_ci				break;
2388c2ecf20Sopenharmony_ci			ret = copy_event_to_user(group, kevent, buf);
2398c2ecf20Sopenharmony_ci			fsnotify_destroy_event(group, kevent);
2408c2ecf20Sopenharmony_ci			if (ret < 0)
2418c2ecf20Sopenharmony_ci				break;
2428c2ecf20Sopenharmony_ci			buf += ret;
2438c2ecf20Sopenharmony_ci			count -= ret;
2448c2ecf20Sopenharmony_ci			continue;
2458c2ecf20Sopenharmony_ci		}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci		ret = -EAGAIN;
2488c2ecf20Sopenharmony_ci		if (file->f_flags & O_NONBLOCK)
2498c2ecf20Sopenharmony_ci			break;
2508c2ecf20Sopenharmony_ci		ret = -ERESTARTSYS;
2518c2ecf20Sopenharmony_ci		if (signal_pending(current))
2528c2ecf20Sopenharmony_ci			break;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		if (start != buf)
2558c2ecf20Sopenharmony_ci			break;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci		wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2588c2ecf20Sopenharmony_ci	}
2598c2ecf20Sopenharmony_ci	remove_wait_queue(&group->notification_waitq, &wait);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	if (start != buf && ret != -EFAULT)
2628c2ecf20Sopenharmony_ci		ret = buf - start;
2638c2ecf20Sopenharmony_ci	return ret;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int inotify_release(struct inode *ignored, struct file *file)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	struct fsnotify_group *group = file->private_data;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	pr_debug("%s: group=%p\n", __func__, group);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/* free this group, matching get was inotify_init->fsnotify_obtain_group */
2738c2ecf20Sopenharmony_ci	fsnotify_destroy_group(group);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	return 0;
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic long inotify_ioctl(struct file *file, unsigned int cmd,
2798c2ecf20Sopenharmony_ci			  unsigned long arg)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	struct fsnotify_group *group;
2828c2ecf20Sopenharmony_ci	struct fsnotify_event *fsn_event;
2838c2ecf20Sopenharmony_ci	void __user *p;
2848c2ecf20Sopenharmony_ci	int ret = -ENOTTY;
2858c2ecf20Sopenharmony_ci	size_t send_len = 0;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	group = file->private_data;
2888c2ecf20Sopenharmony_ci	p = (void __user *) arg;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	switch (cmd) {
2938c2ecf20Sopenharmony_ci	case FIONREAD:
2948c2ecf20Sopenharmony_ci		spin_lock(&group->notification_lock);
2958c2ecf20Sopenharmony_ci		list_for_each_entry(fsn_event, &group->notification_list,
2968c2ecf20Sopenharmony_ci				    list) {
2978c2ecf20Sopenharmony_ci			send_len += sizeof(struct inotify_event);
2988c2ecf20Sopenharmony_ci			send_len += round_event_name_len(fsn_event);
2998c2ecf20Sopenharmony_ci		}
3008c2ecf20Sopenharmony_ci		spin_unlock(&group->notification_lock);
3018c2ecf20Sopenharmony_ci		ret = put_user(send_len, (int __user *) p);
3028c2ecf20Sopenharmony_ci		break;
3038c2ecf20Sopenharmony_ci#ifdef CONFIG_CHECKPOINT_RESTORE
3048c2ecf20Sopenharmony_ci	case INOTIFY_IOC_SETNEXTWD:
3058c2ecf20Sopenharmony_ci		ret = -EINVAL;
3068c2ecf20Sopenharmony_ci		if (arg >= 1 && arg <= INT_MAX) {
3078c2ecf20Sopenharmony_ci			struct inotify_group_private_data *data;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci			data = &group->inotify_data;
3108c2ecf20Sopenharmony_ci			spin_lock(&data->idr_lock);
3118c2ecf20Sopenharmony_ci			idr_set_cursor(&data->idr, (unsigned int)arg);
3128c2ecf20Sopenharmony_ci			spin_unlock(&data->idr_lock);
3138c2ecf20Sopenharmony_ci			ret = 0;
3148c2ecf20Sopenharmony_ci		}
3158c2ecf20Sopenharmony_ci		break;
3168c2ecf20Sopenharmony_ci#endif /* CONFIG_CHECKPOINT_RESTORE */
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	return ret;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic const struct file_operations inotify_fops = {
3238c2ecf20Sopenharmony_ci	.show_fdinfo	= inotify_show_fdinfo,
3248c2ecf20Sopenharmony_ci	.poll		= inotify_poll,
3258c2ecf20Sopenharmony_ci	.read		= inotify_read,
3268c2ecf20Sopenharmony_ci	.fasync		= fsnotify_fasync,
3278c2ecf20Sopenharmony_ci	.release	= inotify_release,
3288c2ecf20Sopenharmony_ci	.unlocked_ioctl	= inotify_ioctl,
3298c2ecf20Sopenharmony_ci	.compat_ioctl	= inotify_ioctl,
3308c2ecf20Sopenharmony_ci	.llseek		= noop_llseek,
3318c2ecf20Sopenharmony_ci};
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci/*
3358c2ecf20Sopenharmony_ci * find_inode - resolve a user-given path to a specific inode
3368c2ecf20Sopenharmony_ci */
3378c2ecf20Sopenharmony_cistatic int inotify_find_inode(const char __user *dirname, struct path *path,
3388c2ecf20Sopenharmony_ci						unsigned int flags, __u64 mask)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	int error;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	error = user_path_at(AT_FDCWD, dirname, flags, path);
3438c2ecf20Sopenharmony_ci	if (error)
3448c2ecf20Sopenharmony_ci		return error;
3458c2ecf20Sopenharmony_ci	/* you can only watch an inode if you have read permissions on it */
3468c2ecf20Sopenharmony_ci	error = inode_permission(path->dentry->d_inode, MAY_READ);
3478c2ecf20Sopenharmony_ci	if (error) {
3488c2ecf20Sopenharmony_ci		path_put(path);
3498c2ecf20Sopenharmony_ci		return error;
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci	error = security_path_notify(path, mask,
3528c2ecf20Sopenharmony_ci				FSNOTIFY_OBJ_TYPE_INODE);
3538c2ecf20Sopenharmony_ci	if (error)
3548c2ecf20Sopenharmony_ci		path_put(path);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	return error;
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_cistatic int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
3608c2ecf20Sopenharmony_ci			      struct inotify_inode_mark *i_mark)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	int ret;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	idr_preload(GFP_KERNEL);
3658c2ecf20Sopenharmony_ci	spin_lock(idr_lock);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
3688c2ecf20Sopenharmony_ci	if (ret >= 0) {
3698c2ecf20Sopenharmony_ci		/* we added the mark to the idr, take a reference */
3708c2ecf20Sopenharmony_ci		i_mark->wd = ret;
3718c2ecf20Sopenharmony_ci		fsnotify_get_mark(&i_mark->fsn_mark);
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	spin_unlock(idr_lock);
3758c2ecf20Sopenharmony_ci	idr_preload_end();
3768c2ecf20Sopenharmony_ci	return ret < 0 ? ret : 0;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
3808c2ecf20Sopenharmony_ci								int wd)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	struct idr *idr = &group->inotify_data.idr;
3838c2ecf20Sopenharmony_ci	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
3848c2ecf20Sopenharmony_ci	struct inotify_inode_mark *i_mark;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	assert_spin_locked(idr_lock);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	i_mark = idr_find(idr, wd);
3898c2ecf20Sopenharmony_ci	if (i_mark) {
3908c2ecf20Sopenharmony_ci		struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci		fsnotify_get_mark(fsn_mark);
3938c2ecf20Sopenharmony_ci		/* One ref for being in the idr, one ref we just took */
3948c2ecf20Sopenharmony_ci		BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	return i_mark;
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
4018c2ecf20Sopenharmony_ci							 int wd)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	struct inotify_inode_mark *i_mark;
4048c2ecf20Sopenharmony_ci	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	spin_lock(idr_lock);
4078c2ecf20Sopenharmony_ci	i_mark = inotify_idr_find_locked(group, wd);
4088c2ecf20Sopenharmony_ci	spin_unlock(idr_lock);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	return i_mark;
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci/*
4148c2ecf20Sopenharmony_ci * Remove the mark from the idr (if present) and drop the reference
4158c2ecf20Sopenharmony_ci * on the mark because it was in the idr.
4168c2ecf20Sopenharmony_ci */
4178c2ecf20Sopenharmony_cistatic void inotify_remove_from_idr(struct fsnotify_group *group,
4188c2ecf20Sopenharmony_ci				    struct inotify_inode_mark *i_mark)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	struct idr *idr = &group->inotify_data.idr;
4218c2ecf20Sopenharmony_ci	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
4228c2ecf20Sopenharmony_ci	struct inotify_inode_mark *found_i_mark = NULL;
4238c2ecf20Sopenharmony_ci	int wd;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	spin_lock(idr_lock);
4268c2ecf20Sopenharmony_ci	wd = i_mark->wd;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	/*
4298c2ecf20Sopenharmony_ci	 * does this i_mark think it is in the idr?  we shouldn't get called
4308c2ecf20Sopenharmony_ci	 * if it wasn't....
4318c2ecf20Sopenharmony_ci	 */
4328c2ecf20Sopenharmony_ci	if (wd == -1) {
4338c2ecf20Sopenharmony_ci		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
4348c2ecf20Sopenharmony_ci			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
4358c2ecf20Sopenharmony_ci		goto out;
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	/* Lets look in the idr to see if we find it */
4398c2ecf20Sopenharmony_ci	found_i_mark = inotify_idr_find_locked(group, wd);
4408c2ecf20Sopenharmony_ci	if (unlikely(!found_i_mark)) {
4418c2ecf20Sopenharmony_ci		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
4428c2ecf20Sopenharmony_ci			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
4438c2ecf20Sopenharmony_ci		goto out;
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/*
4478c2ecf20Sopenharmony_ci	 * We found an mark in the idr at the right wd, but it's
4488c2ecf20Sopenharmony_ci	 * not the mark we were told to remove.  eparis seriously
4498c2ecf20Sopenharmony_ci	 * fucked up somewhere.
4508c2ecf20Sopenharmony_ci	 */
4518c2ecf20Sopenharmony_ci	if (unlikely(found_i_mark != i_mark)) {
4528c2ecf20Sopenharmony_ci		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
4538c2ecf20Sopenharmony_ci			"found_i_mark=%p found_i_mark->wd=%d "
4548c2ecf20Sopenharmony_ci			"found_i_mark->group=%p\n", __func__, i_mark,
4558c2ecf20Sopenharmony_ci			i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
4568c2ecf20Sopenharmony_ci			found_i_mark->wd, found_i_mark->fsn_mark.group);
4578c2ecf20Sopenharmony_ci		goto out;
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	/*
4618c2ecf20Sopenharmony_ci	 * One ref for being in the idr
4628c2ecf20Sopenharmony_ci	 * one ref grabbed by inotify_idr_find
4638c2ecf20Sopenharmony_ci	 */
4648c2ecf20Sopenharmony_ci	if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
4658c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
4668c2ecf20Sopenharmony_ci			 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
4678c2ecf20Sopenharmony_ci		/* we can't really recover with bad ref cnting.. */
4688c2ecf20Sopenharmony_ci		BUG();
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	idr_remove(idr, wd);
4728c2ecf20Sopenharmony_ci	/* Removed from the idr, drop that ref. */
4738c2ecf20Sopenharmony_ci	fsnotify_put_mark(&i_mark->fsn_mark);
4748c2ecf20Sopenharmony_ciout:
4758c2ecf20Sopenharmony_ci	i_mark->wd = -1;
4768c2ecf20Sopenharmony_ci	spin_unlock(idr_lock);
4778c2ecf20Sopenharmony_ci	/* match the ref taken by inotify_idr_find_locked() */
4788c2ecf20Sopenharmony_ci	if (found_i_mark)
4798c2ecf20Sopenharmony_ci		fsnotify_put_mark(&found_i_mark->fsn_mark);
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci/*
4838c2ecf20Sopenharmony_ci * Send IN_IGNORED for this wd, remove this wd from the idr.
4848c2ecf20Sopenharmony_ci */
4858c2ecf20Sopenharmony_civoid inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
4868c2ecf20Sopenharmony_ci				    struct fsnotify_group *group)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	struct inotify_inode_mark *i_mark;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	/* Queue ignore event for the watch */
4918c2ecf20Sopenharmony_ci	inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
4928c2ecf20Sopenharmony_ci				   0);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
4958c2ecf20Sopenharmony_ci	/* remove this mark from the idr */
4968c2ecf20Sopenharmony_ci	inotify_remove_from_idr(group, i_mark);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	dec_inotify_watches(group->inotify_data.ucounts);
4998c2ecf20Sopenharmony_ci}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_cistatic int inotify_update_existing_watch(struct fsnotify_group *group,
5028c2ecf20Sopenharmony_ci					 struct inode *inode,
5038c2ecf20Sopenharmony_ci					 u32 arg)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	struct fsnotify_mark *fsn_mark;
5068c2ecf20Sopenharmony_ci	struct inotify_inode_mark *i_mark;
5078c2ecf20Sopenharmony_ci	__u32 old_mask, new_mask;
5088c2ecf20Sopenharmony_ci	__u32 mask;
5098c2ecf20Sopenharmony_ci	int add = (arg & IN_MASK_ADD);
5108c2ecf20Sopenharmony_ci	int create = (arg & IN_MASK_CREATE);
5118c2ecf20Sopenharmony_ci	int ret;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	mask = inotify_arg_to_mask(inode, arg);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
5168c2ecf20Sopenharmony_ci	if (!fsn_mark)
5178c2ecf20Sopenharmony_ci		return -ENOENT;
5188c2ecf20Sopenharmony_ci	else if (create) {
5198c2ecf20Sopenharmony_ci		ret = -EEXIST;
5208c2ecf20Sopenharmony_ci		goto out;
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	spin_lock(&fsn_mark->lock);
5268c2ecf20Sopenharmony_ci	old_mask = fsn_mark->mask;
5278c2ecf20Sopenharmony_ci	if (add)
5288c2ecf20Sopenharmony_ci		fsn_mark->mask |= mask;
5298c2ecf20Sopenharmony_ci	else
5308c2ecf20Sopenharmony_ci		fsn_mark->mask = mask;
5318c2ecf20Sopenharmony_ci	new_mask = fsn_mark->mask;
5328c2ecf20Sopenharmony_ci	spin_unlock(&fsn_mark->lock);
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	if (old_mask != new_mask) {
5358c2ecf20Sopenharmony_ci		/* more bits in old than in new? */
5368c2ecf20Sopenharmony_ci		int dropped = (old_mask & ~new_mask);
5378c2ecf20Sopenharmony_ci		/* more bits in this fsn_mark than the inode's mask? */
5388c2ecf20Sopenharmony_ci		int do_inode = (new_mask & ~inode->i_fsnotify_mask);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci		/* update the inode with this new fsn_mark */
5418c2ecf20Sopenharmony_ci		if (dropped || do_inode)
5428c2ecf20Sopenharmony_ci			fsnotify_recalc_mask(inode->i_fsnotify_marks);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	}
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	/* return the wd */
5478c2ecf20Sopenharmony_ci	ret = i_mark->wd;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ciout:
5508c2ecf20Sopenharmony_ci	/* match the get from fsnotify_find_mark() */
5518c2ecf20Sopenharmony_ci	fsnotify_put_mark(fsn_mark);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	return ret;
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_cistatic int inotify_new_watch(struct fsnotify_group *group,
5578c2ecf20Sopenharmony_ci			     struct inode *inode,
5588c2ecf20Sopenharmony_ci			     u32 arg)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	struct inotify_inode_mark *tmp_i_mark;
5618c2ecf20Sopenharmony_ci	__u32 mask;
5628c2ecf20Sopenharmony_ci	int ret;
5638c2ecf20Sopenharmony_ci	struct idr *idr = &group->inotify_data.idr;
5648c2ecf20Sopenharmony_ci	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	mask = inotify_arg_to_mask(inode, arg);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
5698c2ecf20Sopenharmony_ci	if (unlikely(!tmp_i_mark))
5708c2ecf20Sopenharmony_ci		return -ENOMEM;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
5738c2ecf20Sopenharmony_ci	tmp_i_mark->fsn_mark.mask = mask;
5748c2ecf20Sopenharmony_ci	tmp_i_mark->wd = -1;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
5778c2ecf20Sopenharmony_ci	if (ret)
5788c2ecf20Sopenharmony_ci		goto out_err;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	/* increment the number of watches the user has */
5818c2ecf20Sopenharmony_ci	if (!inc_inotify_watches(group->inotify_data.ucounts)) {
5828c2ecf20Sopenharmony_ci		inotify_remove_from_idr(group, tmp_i_mark);
5838c2ecf20Sopenharmony_ci		ret = -ENOSPC;
5848c2ecf20Sopenharmony_ci		goto out_err;
5858c2ecf20Sopenharmony_ci	}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	/* we are on the idr, now get on the inode */
5888c2ecf20Sopenharmony_ci	ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
5898c2ecf20Sopenharmony_ci	if (ret) {
5908c2ecf20Sopenharmony_ci		/* we failed to get on the inode, get off the idr */
5918c2ecf20Sopenharmony_ci		inotify_remove_from_idr(group, tmp_i_mark);
5928c2ecf20Sopenharmony_ci		goto out_err;
5938c2ecf20Sopenharmony_ci	}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	/* return the watch descriptor for this new mark */
5978c2ecf20Sopenharmony_ci	ret = tmp_i_mark->wd;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ciout_err:
6008c2ecf20Sopenharmony_ci	/* match the ref from fsnotify_init_mark() */
6018c2ecf20Sopenharmony_ci	fsnotify_put_mark(&tmp_i_mark->fsn_mark);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	return ret;
6048c2ecf20Sopenharmony_ci}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_cistatic int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
6078c2ecf20Sopenharmony_ci{
6088c2ecf20Sopenharmony_ci	int ret = 0;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	mutex_lock(&group->mark_mutex);
6118c2ecf20Sopenharmony_ci	/* try to update and existing watch with the new arg */
6128c2ecf20Sopenharmony_ci	ret = inotify_update_existing_watch(group, inode, arg);
6138c2ecf20Sopenharmony_ci	/* no mark present, try to add a new one */
6148c2ecf20Sopenharmony_ci	if (ret == -ENOENT)
6158c2ecf20Sopenharmony_ci		ret = inotify_new_watch(group, inode, arg);
6168c2ecf20Sopenharmony_ci	mutex_unlock(&group->mark_mutex);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	return ret;
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_cistatic struct fsnotify_group *inotify_new_group(unsigned int max_events)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct fsnotify_group *group;
6248c2ecf20Sopenharmony_ci	struct inotify_event_info *oevent;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	group = fsnotify_alloc_group(&inotify_fsnotify_ops);
6278c2ecf20Sopenharmony_ci	if (IS_ERR(group))
6288c2ecf20Sopenharmony_ci		return group;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
6318c2ecf20Sopenharmony_ci	if (unlikely(!oevent)) {
6328c2ecf20Sopenharmony_ci		fsnotify_destroy_group(group);
6338c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci	group->overflow_event = &oevent->fse;
6368c2ecf20Sopenharmony_ci	fsnotify_init_event(group->overflow_event, 0);
6378c2ecf20Sopenharmony_ci	oevent->mask = FS_Q_OVERFLOW;
6388c2ecf20Sopenharmony_ci	oevent->wd = -1;
6398c2ecf20Sopenharmony_ci	oevent->sync_cookie = 0;
6408c2ecf20Sopenharmony_ci	oevent->name_len = 0;
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	group->max_events = max_events;
6438c2ecf20Sopenharmony_ci	group->memcg = get_mem_cgroup_from_mm(current->mm);
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	spin_lock_init(&group->inotify_data.idr_lock);
6468c2ecf20Sopenharmony_ci	idr_init(&group->inotify_data.idr);
6478c2ecf20Sopenharmony_ci	group->inotify_data.ucounts = inc_ucount(current_user_ns(),
6488c2ecf20Sopenharmony_ci						 current_euid(),
6498c2ecf20Sopenharmony_ci						 UCOUNT_INOTIFY_INSTANCES);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	if (!group->inotify_data.ucounts) {
6528c2ecf20Sopenharmony_ci		fsnotify_destroy_group(group);
6538c2ecf20Sopenharmony_ci		return ERR_PTR(-EMFILE);
6548c2ecf20Sopenharmony_ci	}
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	return group;
6578c2ecf20Sopenharmony_ci}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci/* inotify syscalls */
6618c2ecf20Sopenharmony_cistatic int do_inotify_init(int flags)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	struct fsnotify_group *group;
6648c2ecf20Sopenharmony_ci	int ret;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	/* Check the IN_* constants for consistency.  */
6678c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
6688c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
6718c2ecf20Sopenharmony_ci		return -EINVAL;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	/* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
6748c2ecf20Sopenharmony_ci	group = inotify_new_group(inotify_max_queued_events);
6758c2ecf20Sopenharmony_ci	if (IS_ERR(group))
6768c2ecf20Sopenharmony_ci		return PTR_ERR(group);
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	ret = anon_inode_getfd("inotify", &inotify_fops, group,
6798c2ecf20Sopenharmony_ci				  O_RDONLY | flags);
6808c2ecf20Sopenharmony_ci	if (ret < 0)
6818c2ecf20Sopenharmony_ci		fsnotify_destroy_group(group);
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	return ret;
6848c2ecf20Sopenharmony_ci}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ciSYSCALL_DEFINE1(inotify_init1, int, flags)
6878c2ecf20Sopenharmony_ci{
6888c2ecf20Sopenharmony_ci	return do_inotify_init(flags);
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ciSYSCALL_DEFINE0(inotify_init)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	return do_inotify_init(0);
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
6978c2ecf20Sopenharmony_ci		u32, mask)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	struct fsnotify_group *group;
7008c2ecf20Sopenharmony_ci	struct inode *inode;
7018c2ecf20Sopenharmony_ci	struct path path;
7028c2ecf20Sopenharmony_ci	struct fd f;
7038c2ecf20Sopenharmony_ci	int ret;
7048c2ecf20Sopenharmony_ci	unsigned flags = 0;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	/*
7078c2ecf20Sopenharmony_ci	 * We share a lot of code with fs/dnotify.  We also share
7088c2ecf20Sopenharmony_ci	 * the bit layout between inotify's IN_* and the fsnotify
7098c2ecf20Sopenharmony_ci	 * FS_*.  This check ensures that only the inotify IN_*
7108c2ecf20Sopenharmony_ci	 * bits get passed in and set in watches/events.
7118c2ecf20Sopenharmony_ci	 */
7128c2ecf20Sopenharmony_ci	if (unlikely(mask & ~ALL_INOTIFY_BITS))
7138c2ecf20Sopenharmony_ci		return -EINVAL;
7148c2ecf20Sopenharmony_ci	/*
7158c2ecf20Sopenharmony_ci	 * Require at least one valid bit set in the mask.
7168c2ecf20Sopenharmony_ci	 * Without _something_ set, we would have no events to
7178c2ecf20Sopenharmony_ci	 * watch for.
7188c2ecf20Sopenharmony_ci	 */
7198c2ecf20Sopenharmony_ci	if (unlikely(!(mask & ALL_INOTIFY_BITS)))
7208c2ecf20Sopenharmony_ci		return -EINVAL;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	f = fdget(fd);
7238c2ecf20Sopenharmony_ci	if (unlikely(!f.file))
7248c2ecf20Sopenharmony_ci		return -EBADF;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	/* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
7278c2ecf20Sopenharmony_ci	if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
7288c2ecf20Sopenharmony_ci		ret = -EINVAL;
7298c2ecf20Sopenharmony_ci		goto fput_and_out;
7308c2ecf20Sopenharmony_ci	}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	/* verify that this is indeed an inotify instance */
7338c2ecf20Sopenharmony_ci	if (unlikely(f.file->f_op != &inotify_fops)) {
7348c2ecf20Sopenharmony_ci		ret = -EINVAL;
7358c2ecf20Sopenharmony_ci		goto fput_and_out;
7368c2ecf20Sopenharmony_ci	}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	if (!(mask & IN_DONT_FOLLOW))
7398c2ecf20Sopenharmony_ci		flags |= LOOKUP_FOLLOW;
7408c2ecf20Sopenharmony_ci	if (mask & IN_ONLYDIR)
7418c2ecf20Sopenharmony_ci		flags |= LOOKUP_DIRECTORY;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	ret = inotify_find_inode(pathname, &path, flags,
7448c2ecf20Sopenharmony_ci			(mask & IN_ALL_EVENTS));
7458c2ecf20Sopenharmony_ci	if (ret)
7468c2ecf20Sopenharmony_ci		goto fput_and_out;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	/* inode held in place by reference to path; group by fget on fd */
7498c2ecf20Sopenharmony_ci	inode = path.dentry->d_inode;
7508c2ecf20Sopenharmony_ci	group = f.file->private_data;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	/* create/update an inode mark */
7538c2ecf20Sopenharmony_ci	ret = inotify_update_watch(group, inode, mask);
7548c2ecf20Sopenharmony_ci	path_put(&path);
7558c2ecf20Sopenharmony_cifput_and_out:
7568c2ecf20Sopenharmony_ci	fdput(f);
7578c2ecf20Sopenharmony_ci	return ret;
7588c2ecf20Sopenharmony_ci}
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
7618c2ecf20Sopenharmony_ci{
7628c2ecf20Sopenharmony_ci	struct fsnotify_group *group;
7638c2ecf20Sopenharmony_ci	struct inotify_inode_mark *i_mark;
7648c2ecf20Sopenharmony_ci	struct fd f;
7658c2ecf20Sopenharmony_ci	int ret = -EINVAL;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	f = fdget(fd);
7688c2ecf20Sopenharmony_ci	if (unlikely(!f.file))
7698c2ecf20Sopenharmony_ci		return -EBADF;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	/* verify that this is indeed an inotify instance */
7728c2ecf20Sopenharmony_ci	if (unlikely(f.file->f_op != &inotify_fops))
7738c2ecf20Sopenharmony_ci		goto out;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	group = f.file->private_data;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	i_mark = inotify_idr_find(group, wd);
7788c2ecf20Sopenharmony_ci	if (unlikely(!i_mark))
7798c2ecf20Sopenharmony_ci		goto out;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	ret = 0;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	fsnotify_destroy_mark(&i_mark->fsn_mark, group);
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	/* match ref taken by inotify_idr_find */
7868c2ecf20Sopenharmony_ci	fsnotify_put_mark(&i_mark->fsn_mark);
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ciout:
7898c2ecf20Sopenharmony_ci	fdput(f);
7908c2ecf20Sopenharmony_ci	return ret;
7918c2ecf20Sopenharmony_ci}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci/*
7948c2ecf20Sopenharmony_ci * inotify_user_setup - Our initialization function.  Note that we cannot return
7958c2ecf20Sopenharmony_ci * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
7968c2ecf20Sopenharmony_ci * must result in panic().
7978c2ecf20Sopenharmony_ci */
7988c2ecf20Sopenharmony_cistatic int __init inotify_user_setup(void)
7998c2ecf20Sopenharmony_ci{
8008c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
8018c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
8028c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
8038c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
8048c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
8058c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_OPEN != FS_OPEN);
8068c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
8078c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
8088c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_CREATE != FS_CREATE);
8098c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_DELETE != FS_DELETE);
8108c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
8118c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
8128c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
8138c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
8148c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
8158c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
8168c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
8178c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
8228c2ecf20Sopenharmony_ci					       SLAB_PANIC|SLAB_ACCOUNT);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	inotify_max_queued_events = 16384;
8258c2ecf20Sopenharmony_ci	init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
8268c2ecf20Sopenharmony_ci	init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	return 0;
8298c2ecf20Sopenharmony_ci}
8308c2ecf20Sopenharmony_cifs_initcall(inotify_user_setup);
831