blob: e2901fbb9f76c97a7abe1607f5aa51f53a842966 [file] [log] [blame]
Amy Griffis2d9048e2006-06-01 13:10:59 -07001/*
2 * fs/inotify_user.c - inotify support for userspace
3 *
4 * Authors:
5 * John McCutchan <ttb@tentacle.dhs.org>
6 * Robert Love <rml@novell.com>
7 *
8 * Copyright (C) 2005 John McCutchan
9 * Copyright 2006 Hewlett-Packard Development Company, L.P.
10 *
Eric Paris63c882a2009-05-21 17:02:01 -040011 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
12 * inotify was largely rewriten to make use of the fsnotify infrastructure
13 *
Amy Griffis2d9048e2006-06-01 13:10:59 -070014 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2, or (at your option) any
17 * later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 */
24
Amy Griffis2d9048e2006-06-01 13:10:59 -070025#include <linux/file.h>
Eric Paris63c882a2009-05-21 17:02:01 -040026#include <linux/fs.h> /* struct inode */
27#include <linux/fsnotify_backend.h>
28#include <linux/idr.h>
Paul Gortmakerc013d5a2015-05-01 20:08:20 -040029#include <linux/init.h> /* fs_initcall */
Amy Griffis2d9048e2006-06-01 13:10:59 -070030#include <linux/inotify.h>
Eric Paris63c882a2009-05-21 17:02:01 -040031#include <linux/kernel.h> /* roundup() */
Eric Paris63c882a2009-05-21 17:02:01 -040032#include <linux/namei.h> /* LOOKUP_FOLLOW */
Ingo Molnar174cd4b2017-02-02 19:15:33 +010033#include <linux/sched/signal.h>
Eric Paris63c882a2009-05-21 17:02:01 -040034#include <linux/slab.h> /* struct kmem_cache */
Amy Griffis2d9048e2006-06-01 13:10:59 -070035#include <linux/syscalls.h>
Eric Paris63c882a2009-05-21 17:02:01 -040036#include <linux/types.h>
Al Viroc44dcc52010-02-11 02:24:46 -050037#include <linux/anon_inodes.h>
Eric Paris63c882a2009-05-21 17:02:01 -040038#include <linux/uaccess.h>
39#include <linux/poll.h>
40#include <linux/wait.h>
Shakeel Buttd46eb14b2018-08-17 15:46:39 -070041#include <linux/memcontrol.h>
Eric Paris63c882a2009-05-21 17:02:01 -040042
43#include "inotify.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080044#include "../fdinfo.h"
Amy Griffis2d9048e2006-06-01 13:10:59 -070045
46#include <asm/ioctls.h>
47
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020048/* configurable via /proc/sys/fs/inotify/ */
Harvey Harrison3c828e42008-02-14 19:31:21 -080049static int inotify_max_queued_events __read_mostly;
Eric Paris63c882a2009-05-21 17:02:01 -040050
Jan Kara054c6362016-12-21 18:06:12 +010051struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
Amy Griffis2d9048e2006-06-01 13:10:59 -070052
Amy Griffis2d9048e2006-06-01 13:10:59 -070053#ifdef CONFIG_SYSCTL
54
55#include <linux/sysctl.h>
56
57static int zero;
58
Joe Perches92f778d2014-06-06 14:38:04 -070059struct ctl_table inotify_table[] = {
Amy Griffis2d9048e2006-06-01 13:10:59 -070060 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070061 .procname = "max_user_instances",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020062 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
Amy Griffis2d9048e2006-06-01 13:10:59 -070063 .maxlen = sizeof(int),
64 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080065 .proc_handler = proc_dointvec_minmax,
Amy Griffis2d9048e2006-06-01 13:10:59 -070066 .extra1 = &zero,
67 },
68 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070069 .procname = "max_user_watches",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020070 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
Amy Griffis2d9048e2006-06-01 13:10:59 -070071 .maxlen = sizeof(int),
72 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080073 .proc_handler = proc_dointvec_minmax,
Amy Griffis2d9048e2006-06-01 13:10:59 -070074 .extra1 = &zero,
75 },
76 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070077 .procname = "max_queued_events",
78 .data = &inotify_max_queued_events,
79 .maxlen = sizeof(int),
80 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080081 .proc_handler = proc_dointvec_minmax,
Amy Griffis2d9048e2006-06-01 13:10:59 -070082 .extra1 = &zero
83 },
Eric W. Biedermanab092032009-11-05 14:25:10 -080084 { }
Amy Griffis2d9048e2006-06-01 13:10:59 -070085};
86#endif /* CONFIG_SYSCTL */
87
Eric Paris63c882a2009-05-21 17:02:01 -040088static inline __u32 inotify_arg_to_mask(u32 arg)
Amy Griffis2d9048e2006-06-01 13:10:59 -070089{
Eric Paris63c882a2009-05-21 17:02:01 -040090 __u32 mask;
91
Eric Paris611da042010-07-28 10:18:37 -040092 /*
93 * everything should accept their own ignored, cares about children,
94 * and should receive events when the inode is unmounted
95 */
96 mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
Eric Paris63c882a2009-05-21 17:02:01 -040097
98 /* mask off the flags used to open the fd */
Eric Paris8c1934c2010-07-28 10:18:37 -040099 mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
Eric Paris63c882a2009-05-21 17:02:01 -0400100
101 return mask;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700102}
103
Eric Paris63c882a2009-05-21 17:02:01 -0400104static inline u32 inotify_mask_to_arg(__u32 mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700105{
Eric Paris63c882a2009-05-21 17:02:01 -0400106 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
107 IN_Q_OVERFLOW);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700108}
109
Eric Paris63c882a2009-05-21 17:02:01 -0400110/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400111static __poll_t inotify_poll(struct file *file, poll_table *wait)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700112{
Eric Paris63c882a2009-05-21 17:02:01 -0400113 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400114 __poll_t ret = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700115
Eric Paris63c882a2009-05-21 17:02:01 -0400116 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700117 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400118 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800119 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700120 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700121
122 return ret;
123}
124
Jan Kara7053aee2014-01-21 15:48:14 -0800125static int round_event_name_len(struct fsnotify_event *fsn_event)
Jan Karae9fe6902014-01-21 15:48:13 -0800126{
Jan Kara7053aee2014-01-21 15:48:14 -0800127 struct inotify_event_info *event;
128
129 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800130 if (!event->name_len)
131 return 0;
132 return roundup(event->name_len + 1, sizeof(struct inotify_event));
133}
134
Vegard Nossum3632dee2009-01-22 15:29:45 +0100135/*
136 * Get an inotify_kernel_event if one exists and is small
137 * enough to fit in "count". Return an error pointer if
138 * not large enough.
139 *
Jan Karac21dbe22016-10-07 16:56:52 -0700140 * Called with the group->notification_lock held.
Vegard Nossum3632dee2009-01-22 15:29:45 +0100141 */
Eric Paris63c882a2009-05-21 17:02:01 -0400142static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
143 size_t count)
Vegard Nossum3632dee2009-01-22 15:29:45 +0100144{
145 size_t event_size = sizeof(struct inotify_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400146 struct fsnotify_event *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100147
Eric Paris63c882a2009-05-21 17:02:01 -0400148 if (fsnotify_notify_queue_is_empty(group))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100149 return NULL;
150
Jan Kara8ba8fa912014-08-06 16:03:26 -0700151 event = fsnotify_peek_first_event(group);
Eric Paris63c882a2009-05-21 17:02:01 -0400152
Eric Paris5ba08e22010-07-28 10:18:37 -0400153 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
154
Jan Karae9fe6902014-01-21 15:48:13 -0800155 event_size += round_event_name_len(event);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100156 if (event_size > count)
157 return ERR_PTR(-EINVAL);
158
Jan Karac21dbe22016-10-07 16:56:52 -0700159 /* held the notification_lock the whole time, so this is the
Eric Paris63c882a2009-05-21 17:02:01 -0400160 * same event we peeked above */
Jan Kara8ba8fa912014-08-06 16:03:26 -0700161 fsnotify_remove_first_event(group);
Eric Paris63c882a2009-05-21 17:02:01 -0400162
163 return event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100164}
165
166/*
167 * Copy an event to user space, returning how much we copied.
168 *
169 * We already checked that the event size is smaller than the
170 * buffer we had in "get_one_event()" above.
171 */
Eric Paris63c882a2009-05-21 17:02:01 -0400172static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800173 struct fsnotify_event *fsn_event,
Vegard Nossum3632dee2009-01-22 15:29:45 +0100174 char __user *buf)
175{
Eric Paris63c882a2009-05-21 17:02:01 -0400176 struct inotify_event inotify_event;
Jan Kara7053aee2014-01-21 15:48:14 -0800177 struct inotify_event_info *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100178 size_t event_size = sizeof(struct inotify_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800179 size_t name_len;
180 size_t pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100181
Jan Kara7053aee2014-01-21 15:48:14 -0800182 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
Eric Paris5ba08e22010-07-28 10:18:37 -0400183
Jan Kara7053aee2014-01-21 15:48:14 -0800184 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800185 name_len = event->name_len;
Brian Rogersb962e732009-08-28 10:00:05 -0400186 /*
Jan Karae9fe6902014-01-21 15:48:13 -0800187 * round up name length so it is a multiple of event_size
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700188 * plus an extra byte for the terminating '\0'.
189 */
Jan Kara7053aee2014-01-21 15:48:14 -0800190 pad_name_len = round_event_name_len(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800191 inotify_event.len = pad_name_len;
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200192 inotify_event.mask = inotify_mask_to_arg(event->mask);
Jan Kara7053aee2014-01-21 15:48:14 -0800193 inotify_event.wd = event->wd;
Eric Paris63c882a2009-05-21 17:02:01 -0400194 inotify_event.cookie = event->sync_cookie;
195
196 /* send the main event */
197 if (copy_to_user(buf, &inotify_event, event_size))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100198 return -EFAULT;
199
Eric Paris63c882a2009-05-21 17:02:01 -0400200 buf += event_size;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100201
Eric Paris63c882a2009-05-21 17:02:01 -0400202 /*
203 * fsnotify only stores the pathname, so here we have to send the pathname
204 * and then pad that pathname out to a multiple of sizeof(inotify_event)
Jan Karae9fe6902014-01-21 15:48:13 -0800205 * with zeros.
Eric Paris63c882a2009-05-21 17:02:01 -0400206 */
Jan Karae9fe6902014-01-21 15:48:13 -0800207 if (pad_name_len) {
Eric Paris63c882a2009-05-21 17:02:01 -0400208 /* copy the path name */
Jan Kara7053aee2014-01-21 15:48:14 -0800209 if (copy_to_user(buf, event->name, name_len))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100210 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800211 buf += name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100212
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700213 /* fill userspace with 0's */
Jan Karae9fe6902014-01-21 15:48:13 -0800214 if (clear_user(buf, pad_name_len - name_len))
Eric Paris63c882a2009-05-21 17:02:01 -0400215 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800216 event_size += pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100217 }
Eric Paris63c882a2009-05-21 17:02:01 -0400218
Vegard Nossum3632dee2009-01-22 15:29:45 +0100219 return event_size;
220}
221
Amy Griffis2d9048e2006-06-01 13:10:59 -0700222static ssize_t inotify_read(struct file *file, char __user *buf,
223 size_t count, loff_t *pos)
224{
Eric Paris63c882a2009-05-21 17:02:01 -0400225 struct fsnotify_group *group;
226 struct fsnotify_event *kevent;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700227 char __user *start;
228 int ret;
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200229 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700230
231 start = buf;
Eric Paris63c882a2009-05-21 17:02:01 -0400232 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700233
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200234 add_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700235 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700236 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400237 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700238 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700239
Eric Paris5ba08e22010-07-28 10:18:37 -0400240 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
241
Vegard Nossum3632dee2009-01-22 15:29:45 +0100242 if (kevent) {
243 ret = PTR_ERR(kevent);
244 if (IS_ERR(kevent))
245 break;
Eric Paris63c882a2009-05-21 17:02:01 -0400246 ret = copy_event_to_user(group, kevent, buf);
Jan Kara7053aee2014-01-21 15:48:14 -0800247 fsnotify_destroy_event(group, kevent);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100248 if (ret < 0)
249 break;
250 buf += ret;
251 count -= ret;
252 continue;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700253 }
254
Vegard Nossum3632dee2009-01-22 15:29:45 +0100255 ret = -EAGAIN;
256 if (file->f_flags & O_NONBLOCK)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700257 break;
Eric Paris1ca39ab2012-03-26 13:07:59 -0400258 ret = -ERESTARTSYS;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100259 if (signal_pending(current))
260 break;
261
262 if (start != buf)
263 break;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700264
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200265 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700266 }
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200267 remove_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700268
Vegard Nossum3632dee2009-01-22 15:29:45 +0100269 if (start != buf && ret != -EFAULT)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700270 ret = buf - start;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700271 return ret;
272}
273
274static int inotify_release(struct inode *ignored, struct file *file)
275{
Eric Paris63c882a2009-05-21 17:02:01 -0400276 struct fsnotify_group *group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700277
Eric Paris5ba08e22010-07-28 10:18:37 -0400278 pr_debug("%s: group=%p\n", __func__, group);
279
Eric Paris63c882a2009-05-21 17:02:01 -0400280 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200281 fsnotify_destroy_group(group);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700282
283 return 0;
284}
285
286static long inotify_ioctl(struct file *file, unsigned int cmd,
287 unsigned long arg)
288{
Eric Paris63c882a2009-05-21 17:02:01 -0400289 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800290 struct fsnotify_event *fsn_event;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700291 void __user *p;
292 int ret = -ENOTTY;
Eric Paris63c882a2009-05-21 17:02:01 -0400293 size_t send_len = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700294
Eric Paris63c882a2009-05-21 17:02:01 -0400295 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700296 p = (void __user *) arg;
297
Eric Paris5ba08e22010-07-28 10:18:37 -0400298 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
299
Amy Griffis2d9048e2006-06-01 13:10:59 -0700300 switch (cmd) {
301 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700302 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800303 list_for_each_entry(fsn_event, &group->notification_list,
304 list) {
Eric Paris63c882a2009-05-21 17:02:01 -0400305 send_len += sizeof(struct inotify_event);
Jan Kara7053aee2014-01-21 15:48:14 -0800306 send_len += round_event_name_len(fsn_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400307 }
Jan Karac21dbe22016-10-07 16:56:52 -0700308 spin_unlock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400309 ret = put_user(send_len, (int __user *) p);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700310 break;
Kirill Tkhaie1603b62018-02-09 18:04:54 +0300311#ifdef CONFIG_CHECKPOINT_RESTORE
312 case INOTIFY_IOC_SETNEXTWD:
313 ret = -EINVAL;
314 if (arg >= 1 && arg <= INT_MAX) {
315 struct inotify_group_private_data *data;
316
317 data = &group->inotify_data;
318 spin_lock(&data->idr_lock);
319 idr_set_cursor(&data->idr, (unsigned int)arg);
320 spin_unlock(&data->idr_lock);
321 ret = 0;
322 }
323 break;
324#endif /* CONFIG_CHECKPOINT_RESTORE */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700325 }
326
327 return ret;
328}
329
330static const struct file_operations inotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800331 .show_fdinfo = inotify_show_fdinfo,
Eric Paris63c882a2009-05-21 17:02:01 -0400332 .poll = inotify_poll,
333 .read = inotify_read,
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400334 .fasync = fsnotify_fasync,
Eric Paris63c882a2009-05-21 17:02:01 -0400335 .release = inotify_release,
336 .unlocked_ioctl = inotify_ioctl,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700337 .compat_ioctl = inotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200338 .llseek = noop_llseek,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700339};
340
Amy Griffis2d9048e2006-06-01 13:10:59 -0700341
Eric Paris63c882a2009-05-21 17:02:01 -0400342/*
343 * find_inode - resolve a user-given path to a specific inode
344 */
345static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
346{
347 int error;
348
349 error = user_path_at(AT_FDCWD, dirname, flags, path);
350 if (error)
351 return error;
352 /* you can only watch an inode if you have read permissions on it */
353 error = inode_permission(path->dentry->d_inode, MAY_READ);
354 if (error)
355 path_put(path);
356 return error;
357}
358
Eric Parisb7ba8372009-12-17 20:12:04 -0500359static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
Eric Paris000285d2009-12-17 21:24:24 -0500360 struct inotify_inode_mark *i_mark)
Eric Parisb7ba8372009-12-17 20:12:04 -0500361{
362 int ret;
363
Tejun Heo4542da62013-02-27 17:04:50 -0800364 idr_preload(GFP_KERNEL);
365 spin_lock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500366
Jeff Laytona66c04b2013-04-29 16:21:21 -0700367 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
Tejun Heo4542da62013-02-27 17:04:50 -0800368 if (ret >= 0) {
Eric Parisb7ba8372009-12-17 20:12:04 -0500369 /* we added the mark to the idr, take a reference */
Tejun Heo4542da62013-02-27 17:04:50 -0800370 i_mark->wd = ret;
Tejun Heo4542da62013-02-27 17:04:50 -0800371 fsnotify_get_mark(&i_mark->fsn_mark);
372 }
Eric Parisb7ba8372009-12-17 20:12:04 -0500373
Tejun Heo4542da62013-02-27 17:04:50 -0800374 spin_unlock(idr_lock);
375 idr_preload_end();
376 return ret < 0 ? ret : 0;
Eric Parisb7ba8372009-12-17 20:12:04 -0500377}
378
Eric Paris000285d2009-12-17 21:24:24 -0500379static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500380 int wd)
381{
382 struct idr *idr = &group->inotify_data.idr;
383 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500384 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500385
386 assert_spin_locked(idr_lock);
387
Eric Paris000285d2009-12-17 21:24:24 -0500388 i_mark = idr_find(idr, wd);
389 if (i_mark) {
390 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500391
Eric Paris000285d2009-12-17 21:24:24 -0500392 fsnotify_get_mark(fsn_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500393 /* One ref for being in the idr, one ref we just took */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300394 BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
Eric Parisb7ba8372009-12-17 20:12:04 -0500395 }
396
Eric Paris000285d2009-12-17 21:24:24 -0500397 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500398}
399
Eric Paris000285d2009-12-17 21:24:24 -0500400static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500401 int wd)
402{
Eric Paris000285d2009-12-17 21:24:24 -0500403 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500404 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
405
406 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500407 i_mark = inotify_idr_find_locked(group, wd);
Eric Parisb7ba8372009-12-17 20:12:04 -0500408 spin_unlock(idr_lock);
409
Eric Paris000285d2009-12-17 21:24:24 -0500410 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500411}
412
Eric Parisdead5372009-08-24 16:03:35 -0400413/*
414 * Remove the mark from the idr (if present) and drop the reference
415 * on the mark because it was in the idr.
416 */
Eric Paris7e790dd2009-07-07 10:28:24 -0400417static void inotify_remove_from_idr(struct fsnotify_group *group,
Eric Paris000285d2009-12-17 21:24:24 -0500418 struct inotify_inode_mark *i_mark)
Eric Paris7e790dd2009-07-07 10:28:24 -0400419{
Jan Karae7253762016-12-21 11:50:39 +0100420 struct idr *idr = &group->inotify_data.idr;
Eric Parisb7ba8372009-12-17 20:12:04 -0500421 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500422 struct inotify_inode_mark *found_i_mark = NULL;
Eric Parisdead5372009-08-24 16:03:35 -0400423 int wd;
Eric Paris7e790dd2009-07-07 10:28:24 -0400424
Eric Parisb7ba8372009-12-17 20:12:04 -0500425 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500426 wd = i_mark->wd;
Eric Parisdead5372009-08-24 16:03:35 -0400427
Eric Parisb7ba8372009-12-17 20:12:04 -0500428 /*
Eric Paris000285d2009-12-17 21:24:24 -0500429 * does this i_mark think it is in the idr? we shouldn't get called
Eric Parisb7ba8372009-12-17 20:12:04 -0500430 * if it wasn't....
431 */
432 if (wd == -1) {
Jan Kara25c829a2016-12-09 09:38:55 +0100433 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
434 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisdead5372009-08-24 16:03:35 -0400435 goto out;
436 }
437
Eric Parisb7ba8372009-12-17 20:12:04 -0500438 /* Lets look in the idr to see if we find it */
Eric Paris000285d2009-12-17 21:24:24 -0500439 found_i_mark = inotify_idr_find_locked(group, wd);
440 if (unlikely(!found_i_mark)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100441 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
442 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500443 goto out;
444 }
Eric Parisdead5372009-08-24 16:03:35 -0400445
Eric Parisb7ba8372009-12-17 20:12:04 -0500446 /*
Eric Paris000285d2009-12-17 21:24:24 -0500447 * We found an mark in the idr at the right wd, but it's
448 * not the mark we were told to remove. eparis seriously
Eric Parisb7ba8372009-12-17 20:12:04 -0500449 * fucked up somewhere.
450 */
Eric Paris000285d2009-12-17 21:24:24 -0500451 if (unlikely(found_i_mark != i_mark)) {
452 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
Jan Kara25c829a2016-12-09 09:38:55 +0100453 "found_i_mark=%p found_i_mark->wd=%d "
454 "found_i_mark->group=%p\n", __func__, i_mark,
455 i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
456 found_i_mark->wd, found_i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500457 goto out;
458 }
Eric Parisdead5372009-08-24 16:03:35 -0400459
Eric Parisb7ba8372009-12-17 20:12:04 -0500460 /*
461 * One ref for being in the idr
Eric Parisb7ba8372009-12-17 20:12:04 -0500462 * one ref grabbed by inotify_idr_find
463 */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300464 if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100465 printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
466 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500467 /* we can't really recover with bad ref cnting.. */
468 BUG();
469 }
470
Jan Karae7253762016-12-21 11:50:39 +0100471 idr_remove(idr, wd);
472 /* Removed from the idr, drop that ref. */
473 fsnotify_put_mark(&i_mark->fsn_mark);
Eric Parisdead5372009-08-24 16:03:35 -0400474out:
Jan Karae7253762016-12-21 11:50:39 +0100475 i_mark->wd = -1;
476 spin_unlock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500477 /* match the ref taken by inotify_idr_find_locked() */
Eric Paris000285d2009-12-17 21:24:24 -0500478 if (found_i_mark)
479 fsnotify_put_mark(&found_i_mark->fsn_mark);
Eric Paris7e790dd2009-07-07 10:28:24 -0400480}
Eric Parisdead5372009-08-24 16:03:35 -0400481
Eric Paris63c882a2009-05-21 17:02:01 -0400482/*
Eric Parisdead5372009-08-24 16:03:35 -0400483 * Send IN_IGNORED for this wd, remove this wd from the idr.
Eric Paris63c882a2009-05-21 17:02:01 -0400484 */
Eric Paris000285d2009-12-17 21:24:24 -0500485void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
Eric Paris528da3e2009-06-12 16:04:26 -0400486 struct fsnotify_group *group)
Eric Paris63c882a2009-05-21 17:02:01 -0400487{
Eric Paris000285d2009-12-17 21:24:24 -0500488 struct inotify_inode_mark *i_mark;
Amir Goldstein47d9c7c2018-04-20 16:10:52 -0700489 struct fsnotify_iter_info iter_info = { };
490
491 fsnotify_iter_set_report_type_mark(&iter_info, FSNOTIFY_OBJ_TYPE_INODE,
492 fsn_mark);
Jan Kara7053aee2014-01-21 15:48:14 -0800493
494 /* Queue ignore event for the watch */
Amir Goldstein5b0457a2018-04-20 16:10:50 -0700495 inotify_handle_event(group, NULL, FS_IN_IGNORED, NULL,
496 FSNOTIFY_EVENT_NONE, NULL, 0, &iter_info);
Eric Paris63c882a2009-05-21 17:02:01 -0400497
Lino Sanfilippo8b99c3c2012-03-24 23:44:19 +0100498 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
Eric Paris000285d2009-12-17 21:24:24 -0500499 /* remove this mark from the idr */
500 inotify_remove_from_idr(group, i_mark);
Eric Paris63c882a2009-05-21 17:02:01 -0400501
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200502 dec_inotify_watches(group->inotify_data.ucounts);
Eric Paris63c882a2009-05-21 17:02:01 -0400503}
504
Eric Paris52cef752009-08-24 16:03:35 -0400505static int inotify_update_existing_watch(struct fsnotify_group *group,
506 struct inode *inode,
507 u32 arg)
Eric Paris63c882a2009-05-21 17:02:01 -0400508{
Eric Paris000285d2009-12-17 21:24:24 -0500509 struct fsnotify_mark *fsn_mark;
510 struct inotify_inode_mark *i_mark;
Eric Paris63c882a2009-05-21 17:02:01 -0400511 __u32 old_mask, new_mask;
Eric Paris52cef752009-08-24 16:03:35 -0400512 __u32 mask;
513 int add = (arg & IN_MASK_ADD);
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000514 int create = (arg & IN_MASK_CREATE);
Eric Paris52cef752009-08-24 16:03:35 -0400515 int ret;
Eric Paris63c882a2009-05-21 17:02:01 -0400516
Eric Paris63c882a2009-05-21 17:02:01 -0400517 mask = inotify_arg_to_mask(arg);
Eric Paris63c882a2009-05-21 17:02:01 -0400518
Jan Karab1362ed2016-12-21 16:28:45 +0100519 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Eric Paris000285d2009-12-17 21:24:24 -0500520 if (!fsn_mark)
Eric Paris52cef752009-08-24 16:03:35 -0400521 return -ENOENT;
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000522 else if (create)
523 return -EEXIST;
Eric Paris63c882a2009-05-21 17:02:01 -0400524
Eric Paris000285d2009-12-17 21:24:24 -0500525 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
Eric Paris75fe2b262009-07-07 10:28:23 -0400526
Eric Paris000285d2009-12-17 21:24:24 -0500527 spin_lock(&fsn_mark->lock);
Eric Paris000285d2009-12-17 21:24:24 -0500528 old_mask = fsn_mark->mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500529 if (add)
Jan Kara66d2b812016-12-21 16:03:59 +0100530 fsn_mark->mask |= mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500531 else
Jan Kara66d2b812016-12-21 16:03:59 +0100532 fsn_mark->mask = mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500533 new_mask = fsn_mark->mask;
Eric Paris000285d2009-12-17 21:24:24 -0500534 spin_unlock(&fsn_mark->lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400535
536 if (old_mask != new_mask) {
537 /* more bits in old than in new? */
538 int dropped = (old_mask & ~new_mask);
Eric Paris000285d2009-12-17 21:24:24 -0500539 /* more bits in this fsn_mark than the inode's mask? */
Eric Paris63c882a2009-05-21 17:02:01 -0400540 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
Eric Paris63c882a2009-05-21 17:02:01 -0400541
Eric Paris000285d2009-12-17 21:24:24 -0500542 /* update the inode with this new fsn_mark */
Eric Paris63c882a2009-05-21 17:02:01 -0400543 if (dropped || do_inode)
Jan Kara8920d272016-12-21 16:13:54 +0100544 fsnotify_recalc_mask(inode->i_fsnotify_marks);
Eric Paris63c882a2009-05-21 17:02:01 -0400545
Eric Paris63c882a2009-05-21 17:02:01 -0400546 }
547
Eric Paris52cef752009-08-24 16:03:35 -0400548 /* return the wd */
Eric Paris000285d2009-12-17 21:24:24 -0500549 ret = i_mark->wd;
Eric Paris52cef752009-08-24 16:03:35 -0400550
Eric Parisd0775442009-12-17 21:24:24 -0500551 /* match the get from fsnotify_find_mark() */
Eric Paris000285d2009-12-17 21:24:24 -0500552 fsnotify_put_mark(fsn_mark);
Eric Paris75fe2b262009-07-07 10:28:23 -0400553
Eric Paris52cef752009-08-24 16:03:35 -0400554 return ret;
555}
556
557static int inotify_new_watch(struct fsnotify_group *group,
558 struct inode *inode,
559 u32 arg)
560{
Eric Paris000285d2009-12-17 21:24:24 -0500561 struct inotify_inode_mark *tmp_i_mark;
Eric Paris52cef752009-08-24 16:03:35 -0400562 __u32 mask;
563 int ret;
Eric Parisb7ba8372009-12-17 20:12:04 -0500564 struct idr *idr = &group->inotify_data.idr;
565 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris52cef752009-08-24 16:03:35 -0400566
Eric Paris52cef752009-08-24 16:03:35 -0400567 mask = inotify_arg_to_mask(arg);
Eric Paris52cef752009-08-24 16:03:35 -0400568
Eric Paris000285d2009-12-17 21:24:24 -0500569 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
570 if (unlikely(!tmp_i_mark))
Eric Paris52cef752009-08-24 16:03:35 -0400571 return -ENOMEM;
572
Jan Kara054c6362016-12-21 18:06:12 +0100573 fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
Eric Paris000285d2009-12-17 21:24:24 -0500574 tmp_i_mark->fsn_mark.mask = mask;
575 tmp_i_mark->wd = -1;
Eric Paris52cef752009-08-24 16:03:35 -0400576
Jeff Laytona66c04b2013-04-29 16:21:21 -0700577 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500578 if (ret)
Eric Paris52cef752009-08-24 16:03:35 -0400579 goto out_err;
580
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200581 /* increment the number of watches the user has */
582 if (!inc_inotify_watches(group->inotify_data.ucounts)) {
583 inotify_remove_from_idr(group, tmp_i_mark);
584 ret = -ENOSPC;
585 goto out_err;
586 }
587
Eric Paris52cef752009-08-24 16:03:35 -0400588 /* we are on the idr, now get on the inode */
Amir Goldsteinb249f5b2018-04-20 16:10:55 -0700589 ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
Eric Paris52cef752009-08-24 16:03:35 -0400590 if (ret) {
591 /* we failed to get on the inode, get off the idr */
Eric Paris000285d2009-12-17 21:24:24 -0500592 inotify_remove_from_idr(group, tmp_i_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400593 goto out_err;
594 }
595
Eric Paris52cef752009-08-24 16:03:35 -0400596
Eric Paris000285d2009-12-17 21:24:24 -0500597 /* return the watch descriptor for this new mark */
598 ret = tmp_i_mark->wd;
Eric Paris52cef752009-08-24 16:03:35 -0400599
Eric Paris52cef752009-08-24 16:03:35 -0400600out_err:
Eric Paris000285d2009-12-17 21:24:24 -0500601 /* match the ref from fsnotify_init_mark() */
602 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400603
604 return ret;
605}
606
607static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
608{
609 int ret = 0;
610
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700611 mutex_lock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400612 /* try to update and existing watch with the new arg */
613 ret = inotify_update_existing_watch(group, inode, arg);
614 /* no mark present, try to add a new one */
615 if (ret == -ENOENT)
616 ret = inotify_new_watch(group, inode, arg);
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700617 mutex_unlock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400618
Eric Paris63c882a2009-05-21 17:02:01 -0400619 return ret;
620}
621
Eric Parisd0de4dc2011-04-05 17:20:50 -0400622static struct fsnotify_group *inotify_new_group(unsigned int max_events)
Eric Paris63c882a2009-05-21 17:02:01 -0400623{
624 struct fsnotify_group *group;
Jan Karaff57cd52014-02-21 19:14:11 +0100625 struct inotify_event_info *oevent;
Eric Paris63c882a2009-05-21 17:02:01 -0400626
Eric Paris0d2e2a12009-12-17 21:24:22 -0500627 group = fsnotify_alloc_group(&inotify_fsnotify_ops);
Eric Paris63c882a2009-05-21 17:02:01 -0400628 if (IS_ERR(group))
629 return group;
630
Jan Karaff57cd52014-02-21 19:14:11 +0100631 oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
632 if (unlikely(!oevent)) {
633 fsnotify_destroy_group(group);
634 return ERR_PTR(-ENOMEM);
635 }
636 group->overflow_event = &oevent->fse;
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200637 fsnotify_init_event(group->overflow_event, NULL);
638 oevent->mask = FS_Q_OVERFLOW;
Jan Karaff57cd52014-02-21 19:14:11 +0100639 oevent->wd = -1;
640 oevent->sync_cookie = 0;
641 oevent->name_len = 0;
642
Eric Paris63c882a2009-05-21 17:02:01 -0400643 group->max_events = max_events;
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700644 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris63c882a2009-05-21 17:02:01 -0400645
646 spin_lock_init(&group->inotify_data.idr_lock);
647 idr_init(&group->inotify_data.idr);
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200648 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
649 current_euid(),
650 UCOUNT_INOTIFY_INSTANCES);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400651
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200652 if (!group->inotify_data.ucounts) {
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200653 fsnotify_destroy_group(group);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400654 return ERR_PTR(-EMFILE);
655 }
Eric Paris63c882a2009-05-21 17:02:01 -0400656
657 return group;
658}
659
660
661/* inotify syscalls */
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100662static int do_inotify_init(int flags)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700663{
Eric Paris63c882a2009-05-21 17:02:01 -0400664 struct fsnotify_group *group;
Al Viroc44dcc52010-02-11 02:24:46 -0500665 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700666
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700667 /* Check the IN_* constants for consistency. */
668 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
669 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
670
Ulrich Drepper510df2d2008-07-23 21:29:41 -0700671 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
Ulrich Drepper40065532008-07-23 21:29:32 -0700672 return -EINVAL;
673
Eric Paris63c882a2009-05-21 17:02:01 -0400674 /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
Eric Parisd0de4dc2011-04-05 17:20:50 -0400675 group = inotify_new_group(inotify_max_queued_events);
676 if (IS_ERR(group))
677 return PTR_ERR(group);
Al Viro825f9692009-08-05 18:35:21 +0400678
Al Viroc44dcc52010-02-11 02:24:46 -0500679 ret = anon_inode_getfd("inotify", &inotify_fops, group,
680 O_RDONLY | flags);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400681 if (ret < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200682 fsnotify_destroy_group(group);
Al Viro825f9692009-08-05 18:35:21 +0400683
Amy Griffis2d9048e2006-06-01 13:10:59 -0700684 return ret;
685}
686
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100687SYSCALL_DEFINE1(inotify_init1, int, flags)
688{
689 return do_inotify_init(flags);
690}
691
Heiko Carstens938bb9f2009-01-14 14:14:30 +0100692SYSCALL_DEFINE0(inotify_init)
Ulrich Drepper40065532008-07-23 21:29:32 -0700693{
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100694 return do_inotify_init(0);
Ulrich Drepper40065532008-07-23 21:29:32 -0700695}
696
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100697SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
698 u32, mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700699{
Eric Paris63c882a2009-05-21 17:02:01 -0400700 struct fsnotify_group *group;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700701 struct inode *inode;
Al Viro2d8f3032008-07-22 09:59:21 -0400702 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -0400703 struct fd f;
704 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700705 unsigned flags = 0;
706
Dave Hansend30e2c02015-11-05 18:43:46 -0800707 /*
708 * We share a lot of code with fs/dnotify. We also share
709 * the bit layout between inotify's IN_* and the fsnotify
710 * FS_*. This check ensures that only the inotify IN_*
711 * bits get passed in and set in watches/events.
712 */
713 if (unlikely(mask & ~ALL_INOTIFY_BITS))
714 return -EINVAL;
715 /*
716 * Require at least one valid bit set in the mask.
717 * Without _something_ set, we would have no events to
718 * watch for.
719 */
Zhao Hongjiang04df32f2013-04-30 15:26:46 -0700720 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
721 return -EINVAL;
722
Al Viro2903ff02012-08-28 12:52:22 -0400723 f = fdget(fd);
724 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700725 return -EBADF;
726
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000727 /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
Tetsuo Handa125892e2019-01-01 18:54:26 +0900728 if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
729 ret = -EINVAL;
730 goto fput_and_out;
731 }
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000732
Amy Griffis2d9048e2006-06-01 13:10:59 -0700733 /* verify that this is indeed an inotify instance */
Al Viro2903ff02012-08-28 12:52:22 -0400734 if (unlikely(f.file->f_op != &inotify_fops)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700735 ret = -EINVAL;
736 goto fput_and_out;
737 }
738
739 if (!(mask & IN_DONT_FOLLOW))
740 flags |= LOOKUP_FOLLOW;
741 if (mask & IN_ONLYDIR)
742 flags |= LOOKUP_DIRECTORY;
743
Eric Paris63c882a2009-05-21 17:02:01 -0400744 ret = inotify_find_inode(pathname, &path, flags);
745 if (ret)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700746 goto fput_and_out;
747
Eric Paris63c882a2009-05-21 17:02:01 -0400748 /* inode held in place by reference to path; group by fget on fd */
Al Viro2d8f3032008-07-22 09:59:21 -0400749 inode = path.dentry->d_inode;
Al Viro2903ff02012-08-28 12:52:22 -0400750 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700751
Eric Paris63c882a2009-05-21 17:02:01 -0400752 /* create/update an inode mark */
753 ret = inotify_update_watch(group, inode, mask);
Al Viro2d8f3032008-07-22 09:59:21 -0400754 path_put(&path);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700755fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400756 fdput(f);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700757 return ret;
758}
759
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100760SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700761{
Eric Paris63c882a2009-05-21 17:02:01 -0400762 struct fsnotify_group *group;
Eric Paris000285d2009-12-17 21:24:24 -0500763 struct inotify_inode_mark *i_mark;
Al Viro2903ff02012-08-28 12:52:22 -0400764 struct fd f;
765 int ret = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700766
Al Viro2903ff02012-08-28 12:52:22 -0400767 f = fdget(fd);
768 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700769 return -EBADF;
770
771 /* verify that this is indeed an inotify instance */
Eric Parisb7ba8372009-12-17 20:12:04 -0500772 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400773 if (unlikely(f.file->f_op != &inotify_fops))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700774 goto out;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700775
Al Viro2903ff02012-08-28 12:52:22 -0400776 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700777
Eric Parisb7ba8372009-12-17 20:12:04 -0500778 ret = -EINVAL;
Eric Paris000285d2009-12-17 21:24:24 -0500779 i_mark = inotify_idr_find(group, wd);
780 if (unlikely(!i_mark))
Eric Paris63c882a2009-05-21 17:02:01 -0400781 goto out;
Eric Paris63c882a2009-05-21 17:02:01 -0400782
Eric Parisb7ba8372009-12-17 20:12:04 -0500783 ret = 0;
784
Lino Sanfilippoe2a29942011-06-14 17:29:51 +0200785 fsnotify_destroy_mark(&i_mark->fsn_mark, group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500786
787 /* match ref taken by inotify_idr_find */
Eric Paris000285d2009-12-17 21:24:24 -0500788 fsnotify_put_mark(&i_mark->fsn_mark);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700789
790out:
Al Viro2903ff02012-08-28 12:52:22 -0400791 fdput(f);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700792 return ret;
793}
794
Amy Griffis2d9048e2006-06-01 13:10:59 -0700795/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100796 * inotify_user_setup - Our initialization function. Note that we cannot return
Amy Griffis2d9048e2006-06-01 13:10:59 -0700797 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
798 * must result in panic().
799 */
800static int __init inotify_user_setup(void)
801{
Eric Parisf874e1a2010-07-28 10:18:37 -0400802 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
803 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
804 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
805 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
806 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
807 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
808 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
809 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
810 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
811 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
812 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
813 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
814 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
815 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
816 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
817 BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
Eric Parisb29866a2010-10-28 17:21:58 -0400818 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
Eric Parisf874e1a2010-07-28 10:18:37 -0400819 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
820
Amir Goldsteina39f7ec2018-10-04 00:25:36 +0300821 BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
Eric Parisf874e1a2010-07-28 10:18:37 -0400822
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700823 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
824 SLAB_PANIC|SLAB_ACCOUNT);
Eric Paris63c882a2009-05-21 17:02:01 -0400825
Amy Griffis2d9048e2006-06-01 13:10:59 -0700826 inotify_max_queued_events = 16384;
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200827 init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
828 init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700829
Amy Griffis2d9048e2006-06-01 13:10:59 -0700830 return 0;
831}
Paul Gortmakerc013d5a2015-05-01 20:08:20 -0400832fs_initcall(inotify_user_setup);