Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * fs/inotify_user.c - inotify support for userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *	John McCutchan	<ttb@tentacle.dhs.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	Robert Love	<rml@novell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 2005 John McCutchan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Copyright 2006 Hewlett-Packard Development Company, L.P.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Copyright (C) 2009 Eric Paris <Red Hat Inc>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * inotify was largely rewriten to make use of the fsnotify infrastructure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/fs.h> /* struct inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/fsnotify_backend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/init.h> /* fs_initcall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/inotify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/kernel.h> /* roundup() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/namei.h> /* LOOKUP_FOLLOW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/slab.h> /* struct kmem_cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/anon_inodes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/memcontrol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include "inotify.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include "../fdinfo.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <asm/ioctls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /* configurable via /proc/sys/fs/inotify/ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static int inotify_max_queued_events __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) struct ctl_table inotify_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		.procname	= "max_user_instances",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		.maxlen		= sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		.mode		= 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		.proc_handler	= proc_dointvec_minmax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		.extra1		= SYSCTL_ZERO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		.procname	= "max_user_watches",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		.maxlen		= sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		.mode		= 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		.proc_handler	= proc_dointvec_minmax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		.extra1		= SYSCTL_ZERO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		.procname	= "max_queued_events",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		.data		= &inotify_max_queued_events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		.maxlen		= sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		.mode		= 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		.proc_handler	= proc_dointvec_minmax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		.extra1		= SYSCTL_ZERO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #endif /* CONFIG_SYSCTL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	__u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 * Everything should accept their own ignored and should receive events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	 * when the inode is unmounted.  All directories care about children.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	mask = (FS_IN_IGNORED | FS_UNMOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		mask |= FS_EVENT_ON_CHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	/* mask off the flags used to open the fd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static inline u32 inotify_mask_to_arg(__u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		       IN_Q_OVERFLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* intofiy userspace file descriptor functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static __poll_t inotify_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct fsnotify_group *group = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	__poll_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	poll_wait(file, &group->notification_waitq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (!fsnotify_notify_queue_is_empty(group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		ret = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int round_event_name_len(struct fsnotify_event *fsn_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct inotify_event_info *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	event = INOTIFY_E(fsn_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (!event->name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return roundup(event->name_len + 1, sizeof(struct inotify_event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * Get an inotify_kernel_event if one exists and is small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  * enough to fit in "count". Return an error pointer if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  * not large enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * Called with the group->notification_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 					    size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	size_t event_size = sizeof(struct inotify_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct fsnotify_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (fsnotify_notify_queue_is_empty(group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	event = fsnotify_peek_first_event(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	event_size += round_event_name_len(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (event_size > count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	/* held the notification_lock the whole time, so this is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	 * same event we peeked above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	fsnotify_remove_first_event(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * Copy an event to user space, returning how much we copied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * We already checked that the event size is smaller than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * buffer we had in "get_one_event()" above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static ssize_t copy_event_to_user(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 				  struct fsnotify_event *fsn_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				  char __user *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct inotify_event inotify_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct inotify_event_info *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	size_t event_size = sizeof(struct inotify_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	size_t name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	size_t pad_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	event = INOTIFY_E(fsn_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	name_len = event->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * round up name length so it is a multiple of event_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 * plus an extra byte for the terminating '\0'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	pad_name_len = round_event_name_len(fsn_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	inotify_event.len = pad_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	inotify_event.mask = inotify_mask_to_arg(event->mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	inotify_event.wd = event->wd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	inotify_event.cookie = event->sync_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	/* send the main event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (copy_to_user(buf, &inotify_event, event_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	buf += event_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	 * fsnotify only stores the pathname, so here we have to send the pathname
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	 * and then pad that pathname out to a multiple of sizeof(inotify_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	 * with zeros.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (pad_name_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		/* copy the path name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (copy_to_user(buf, event->name, name_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		buf += name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		/* fill userspace with 0's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		if (clear_user(buf, pad_name_len - name_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		event_size += pad_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return event_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static ssize_t inotify_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			    size_t count, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct fsnotify_event *kevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	char __user *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	start = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	group = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	add_wait_queue(&group->notification_waitq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		kevent = get_one_event(group, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		if (kevent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			ret = PTR_ERR(kevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			if (IS_ERR(kevent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			ret = copy_event_to_user(group, kevent, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			fsnotify_destroy_event(group, kevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			buf += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			count -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (file->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		if (start != buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	remove_wait_queue(&group->notification_waitq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (start != buf && ret != -EFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		ret = buf - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static int inotify_release(struct inode *ignored, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	struct fsnotify_group *group = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	pr_debug("%s: group=%p\n", __func__, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	/* free this group, matching get was inotify_init->fsnotify_obtain_group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	fsnotify_destroy_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static long inotify_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			  unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct fsnotify_event *fsn_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	void __user *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	int ret = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	size_t send_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	group = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	p = (void __user *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	case FIONREAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		list_for_each_entry(fsn_event, &group->notification_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				    list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			send_len += sizeof(struct inotify_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			send_len += round_event_name_len(fsn_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		ret = put_user(send_len, (int __user *) p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) #ifdef CONFIG_CHECKPOINT_RESTORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	case INOTIFY_IOC_SETNEXTWD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		if (arg >= 1 && arg <= INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			struct inotify_group_private_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			data = &group->inotify_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			spin_lock(&data->idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			idr_set_cursor(&data->idr, (unsigned int)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			spin_unlock(&data->idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) #endif /* CONFIG_CHECKPOINT_RESTORE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static const struct file_operations inotify_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	.show_fdinfo	= inotify_show_fdinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	.poll		= inotify_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	.read		= inotify_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	.fasync		= fsnotify_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	.release	= inotify_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	.unlocked_ioctl	= inotify_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	.compat_ioctl	= inotify_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	.llseek		= noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  * find_inode - resolve a user-given path to a specific inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static int inotify_find_inode(const char __user *dirname, struct path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 						unsigned int flags, __u64 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	error = user_path_at(AT_FDCWD, dirname, flags, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	/* you can only watch an inode if you have read permissions on it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	error = inode_permission(path->dentry->d_inode, MAY_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		path_put(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	error = security_path_notify(path, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				FSNOTIFY_OBJ_TYPE_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		path_put(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			      struct inotify_inode_mark *i_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	idr_preload(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	spin_lock(idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		/* we added the mark to the idr, take a reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		i_mark->wd = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		fsnotify_get_mark(&i_mark->fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	spin_unlock(idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	return ret < 0 ? ret : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 								int wd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	struct idr *idr = &group->inotify_data.idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct inotify_inode_mark *i_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	assert_spin_locked(idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	i_mark = idr_find(idr, wd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (i_mark) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		fsnotify_get_mark(fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		/* One ref for being in the idr, one ref we just took */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	return i_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 							 int wd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	struct inotify_inode_mark *i_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	spin_lock(idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	i_mark = inotify_idr_find_locked(group, wd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	spin_unlock(idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return i_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)  * Remove the mark from the idr (if present) and drop the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)  * on the mark because it was in the idr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static void inotify_remove_from_idr(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 				    struct inotify_inode_mark *i_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	struct idr *idr = &group->inotify_data.idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct inotify_inode_mark *found_i_mark = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	int wd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	spin_lock(idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	wd = i_mark->wd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	 * does this i_mark think it is in the idr?  we shouldn't get called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	 * if it wasn't....
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (wd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	/* Lets look in the idr to see if we find it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	found_i_mark = inotify_idr_find_locked(group, wd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (unlikely(!found_i_mark)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	 * We found an mark in the idr at the right wd, but it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	 * not the mark we were told to remove.  eparis seriously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	 * fucked up somewhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (unlikely(found_i_mark != i_mark)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			"found_i_mark=%p found_i_mark->wd=%d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			"found_i_mark->group=%p\n", __func__, i_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			found_i_mark->wd, found_i_mark->fsn_mark.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	 * One ref for being in the idr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	 * one ref grabbed by inotify_idr_find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		/* we can't really recover with bad ref cnting.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	idr_remove(idr, wd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	/* Removed from the idr, drop that ref. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	fsnotify_put_mark(&i_mark->fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	i_mark->wd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	spin_unlock(idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	/* match the ref taken by inotify_idr_find_locked() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (found_i_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		fsnotify_put_mark(&found_i_mark->fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)  * Send IN_IGNORED for this wd, remove this wd from the idr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 				    struct fsnotify_group *group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	struct inotify_inode_mark *i_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	/* Queue ignore event for the watch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 				   0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	/* remove this mark from the idr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	inotify_remove_from_idr(group, i_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	dec_inotify_watches(group->inotify_data.ucounts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static int inotify_update_existing_watch(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 					 struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 					 u32 arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	struct fsnotify_mark *fsn_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	struct inotify_inode_mark *i_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	__u32 old_mask, new_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	__u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	int add = (arg & IN_MASK_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	int create = (arg & IN_MASK_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	mask = inotify_arg_to_mask(inode, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (!fsn_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	else if (create) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	spin_lock(&fsn_mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	old_mask = fsn_mark->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if (add)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		fsn_mark->mask |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		fsn_mark->mask = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	new_mask = fsn_mark->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	spin_unlock(&fsn_mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	if (old_mask != new_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		/* more bits in old than in new? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		int dropped = (old_mask & ~new_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		/* more bits in this fsn_mark than the inode's mask? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		int do_inode = (new_mask & ~inode->i_fsnotify_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		/* update the inode with this new fsn_mark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		if (dropped || do_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 			fsnotify_recalc_mask(inode->i_fsnotify_marks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	/* return the wd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	ret = i_mark->wd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	/* match the get from fsnotify_find_mark() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	fsnotify_put_mark(fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) static int inotify_new_watch(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 			     struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			     u32 arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	struct inotify_inode_mark *tmp_i_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	__u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	struct idr *idr = &group->inotify_data.idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	mask = inotify_arg_to_mask(inode, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	if (unlikely(!tmp_i_mark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	tmp_i_mark->fsn_mark.mask = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	tmp_i_mark->wd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	/* increment the number of watches the user has */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	if (!inc_inotify_watches(group->inotify_data.ucounts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		inotify_remove_from_idr(group, tmp_i_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	/* we are on the idr, now get on the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		/* we failed to get on the inode, get off the idr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		inotify_remove_from_idr(group, tmp_i_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	/* return the watch descriptor for this new mark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	ret = tmp_i_mark->wd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	/* match the ref from fsnotify_init_mark() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	fsnotify_put_mark(&tmp_i_mark->fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	mutex_lock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	/* try to update and existing watch with the new arg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	ret = inotify_update_existing_watch(group, inode, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	/* no mark present, try to add a new one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		ret = inotify_new_watch(group, inode, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static struct fsnotify_group *inotify_new_group(unsigned int max_events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	struct inotify_event_info *oevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	group = fsnotify_alloc_group(&inotify_fsnotify_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	if (IS_ERR(group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		return group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	if (unlikely(!oevent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		fsnotify_destroy_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	group->overflow_event = &oevent->fse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	fsnotify_init_event(group->overflow_event, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	oevent->mask = FS_Q_OVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	oevent->wd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	oevent->sync_cookie = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	oevent->name_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	group->max_events = max_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	group->memcg = get_mem_cgroup_from_mm(current->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	spin_lock_init(&group->inotify_data.idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	idr_init(&group->inotify_data.idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	group->inotify_data.ucounts = inc_ucount(current_user_ns(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 						 current_euid(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 						 UCOUNT_INOTIFY_INSTANCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	if (!group->inotify_data.ucounts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		fsnotify_destroy_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		return ERR_PTR(-EMFILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	return group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /* inotify syscalls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) static int do_inotify_init(int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	/* Check the IN_* constants for consistency.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	/* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	group = inotify_new_group(inotify_max_queued_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	if (IS_ERR(group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		return PTR_ERR(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	ret = anon_inode_getfd("inotify", &inotify_fops, group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 				  O_RDONLY | flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		fsnotify_destroy_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) SYSCALL_DEFINE1(inotify_init1, int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	return do_inotify_init(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) SYSCALL_DEFINE0(inotify_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	return do_inotify_init(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		u32, mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	struct path alteredpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	struct path *canonical_path = &path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	unsigned flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	 * We share a lot of code with fs/dnotify.  We also share
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	 * the bit layout between inotify's IN_* and the fsnotify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	 * FS_*.  This check ensures that only the inotify IN_*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	 * bits get passed in and set in watches/events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	if (unlikely(mask & ~ALL_INOTIFY_BITS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	 * Require at least one valid bit set in the mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	 * Without _something_ set, we would have no events to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	 * watch for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	if (unlikely(!(mask & ALL_INOTIFY_BITS)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	f = fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	if (unlikely(!f.file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	/* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		goto fput_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	/* verify that this is indeed an inotify instance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	if (unlikely(f.file->f_op != &inotify_fops)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		goto fput_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	if (!(mask & IN_DONT_FOLLOW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		flags |= LOOKUP_FOLLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	if (mask & IN_ONLYDIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		flags |= LOOKUP_DIRECTORY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	ret = inotify_find_inode(pathname, &path, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 			(mask & IN_ALL_EVENTS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		goto fput_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	/* support stacked filesystems */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	if (path.dentry && path.dentry->d_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		if (path.dentry->d_op->d_canonical_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 			path.dentry->d_op->d_canonical_path(&path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 							    &alteredpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 			canonical_path = &alteredpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 			path_put(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	/* inode held in place by reference to path; group by fget on fd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	inode = canonical_path->dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	group = f.file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	/* create/update an inode mark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	ret = inotify_update_watch(group, inode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	path_put(canonical_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) fput_and_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	struct inotify_inode_mark *i_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	f = fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	if (unlikely(!f.file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	/* verify that this is indeed an inotify instance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	if (unlikely(f.file->f_op != &inotify_fops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	group = f.file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	i_mark = inotify_idr_find(group, wd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	if (unlikely(!i_mark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	fsnotify_destroy_mark(&i_mark->fsn_mark, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	/* match ref taken by inotify_idr_find */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	fsnotify_put_mark(&i_mark->fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)  * inotify_user_setup - Our initialization function.  Note that we cannot return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)  * must result in panic().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) static int __init inotify_user_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	BUILD_BUG_ON(IN_OPEN != FS_OPEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	BUILD_BUG_ON(IN_CREATE != FS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	BUILD_BUG_ON(IN_DELETE != FS_DELETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 					       SLAB_PANIC|SLAB_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	inotify_max_queued_events = 16384;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) fs_initcall(inotify_user_setup);