blob: 5385d5817dd9764ec15892d9b17dd31435ff4c9e [file] [log] [blame]
Thomas Gleixner3e0a4e82019-05-23 11:14:55 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Amy Griffis2d9048e2006-06-01 13:10:59 -07002/*
3 * fs/inotify_user.c - inotify support for userspace
4 *
5 * Authors:
6 * John McCutchan <ttb@tentacle.dhs.org>
7 * Robert Love <rml@novell.com>
8 *
9 * Copyright (C) 2005 John McCutchan
10 * Copyright 2006 Hewlett-Packard Development Company, L.P.
11 *
Eric Paris63c882a2009-05-21 17:02:01 -040012 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
13 * inotify was largely rewriten to make use of the fsnotify infrastructure
Amy Griffis2d9048e2006-06-01 13:10:59 -070014 */
15
Amy Griffis2d9048e2006-06-01 13:10:59 -070016#include <linux/file.h>
Eric Paris63c882a2009-05-21 17:02:01 -040017#include <linux/fs.h> /* struct inode */
18#include <linux/fsnotify_backend.h>
19#include <linux/idr.h>
Paul Gortmakerc013d5a2015-05-01 20:08:20 -040020#include <linux/init.h> /* fs_initcall */
Amy Griffis2d9048e2006-06-01 13:10:59 -070021#include <linux/inotify.h>
Eric Paris63c882a2009-05-21 17:02:01 -040022#include <linux/kernel.h> /* roundup() */
Eric Paris63c882a2009-05-21 17:02:01 -040023#include <linux/namei.h> /* LOOKUP_FOLLOW */
Ingo Molnar174cd4b2017-02-02 19:15:33 +010024#include <linux/sched/signal.h>
Eric Paris63c882a2009-05-21 17:02:01 -040025#include <linux/slab.h> /* struct kmem_cache */
Amy Griffis2d9048e2006-06-01 13:10:59 -070026#include <linux/syscalls.h>
Eric Paris63c882a2009-05-21 17:02:01 -040027#include <linux/types.h>
Al Viroc44dcc52010-02-11 02:24:46 -050028#include <linux/anon_inodes.h>
Eric Paris63c882a2009-05-21 17:02:01 -040029#include <linux/uaccess.h>
30#include <linux/poll.h>
31#include <linux/wait.h>
Shakeel Buttd46eb14b2018-08-17 15:46:39 -070032#include <linux/memcontrol.h>
Aaron Goidelac5656d2019-08-12 11:20:00 -040033#include <linux/security.h>
Eric Paris63c882a2009-05-21 17:02:01 -040034
35#include "inotify.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080036#include "../fdinfo.h"
Amy Griffis2d9048e2006-06-01 13:10:59 -070037
38#include <asm/ioctls.h>
39
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020040/* configurable via /proc/sys/fs/inotify/ */
Harvey Harrison3c828e42008-02-14 19:31:21 -080041static int inotify_max_queued_events __read_mostly;
Eric Paris63c882a2009-05-21 17:02:01 -040042
Jan Kara054c6362016-12-21 18:06:12 +010043struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
Amy Griffis2d9048e2006-06-01 13:10:59 -070044
Amy Griffis2d9048e2006-06-01 13:10:59 -070045#ifdef CONFIG_SYSCTL
46
47#include <linux/sysctl.h>
48
Joe Perches92f778d2014-06-06 14:38:04 -070049struct ctl_table inotify_table[] = {
Amy Griffis2d9048e2006-06-01 13:10:59 -070050 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070051 .procname = "max_user_instances",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020052 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
Amy Griffis2d9048e2006-06-01 13:10:59 -070053 .maxlen = sizeof(int),
54 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080055 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -070056 .extra1 = SYSCTL_ZERO,
Amy Griffis2d9048e2006-06-01 13:10:59 -070057 },
58 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070059 .procname = "max_user_watches",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020060 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
Amy Griffis2d9048e2006-06-01 13:10:59 -070061 .maxlen = sizeof(int),
62 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080063 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -070064 .extra1 = SYSCTL_ZERO,
Amy Griffis2d9048e2006-06-01 13:10:59 -070065 },
66 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070067 .procname = "max_queued_events",
68 .data = &inotify_max_queued_events,
69 .maxlen = sizeof(int),
70 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080071 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -070072 .extra1 = SYSCTL_ZERO
Amy Griffis2d9048e2006-06-01 13:10:59 -070073 },
Eric W. Biedermanab092032009-11-05 14:25:10 -080074 { }
Amy Griffis2d9048e2006-06-01 13:10:59 -070075};
76#endif /* CONFIG_SYSCTL */
77
Eric Paris63c882a2009-05-21 17:02:01 -040078static inline __u32 inotify_arg_to_mask(u32 arg)
Amy Griffis2d9048e2006-06-01 13:10:59 -070079{
Eric Paris63c882a2009-05-21 17:02:01 -040080 __u32 mask;
81
Eric Paris611da042010-07-28 10:18:37 -040082 /*
83 * everything should accept their own ignored, cares about children,
84 * and should receive events when the inode is unmounted
85 */
86 mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
Eric Paris63c882a2009-05-21 17:02:01 -040087
88 /* mask off the flags used to open the fd */
Eric Paris8c1934c2010-07-28 10:18:37 -040089 mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
Eric Paris63c882a2009-05-21 17:02:01 -040090
91 return mask;
Amy Griffis2d9048e2006-06-01 13:10:59 -070092}
93
Eric Paris63c882a2009-05-21 17:02:01 -040094static inline u32 inotify_mask_to_arg(__u32 mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -070095{
Eric Paris63c882a2009-05-21 17:02:01 -040096 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
97 IN_Q_OVERFLOW);
Amy Griffis2d9048e2006-06-01 13:10:59 -070098}
99
Eric Paris63c882a2009-05-21 17:02:01 -0400100/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400101static __poll_t inotify_poll(struct file *file, poll_table *wait)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700102{
Eric Paris63c882a2009-05-21 17:02:01 -0400103 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400104 __poll_t ret = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700105
Eric Paris63c882a2009-05-21 17:02:01 -0400106 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700107 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400108 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800109 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700110 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700111
112 return ret;
113}
114
Jan Kara7053aee2014-01-21 15:48:14 -0800115static int round_event_name_len(struct fsnotify_event *fsn_event)
Jan Karae9fe6902014-01-21 15:48:13 -0800116{
Jan Kara7053aee2014-01-21 15:48:14 -0800117 struct inotify_event_info *event;
118
119 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800120 if (!event->name_len)
121 return 0;
122 return roundup(event->name_len + 1, sizeof(struct inotify_event));
123}
124
Vegard Nossum3632dee2009-01-22 15:29:45 +0100125/*
126 * Get an inotify_kernel_event if one exists and is small
127 * enough to fit in "count". Return an error pointer if
128 * not large enough.
129 *
Jan Karac21dbe22016-10-07 16:56:52 -0700130 * Called with the group->notification_lock held.
Vegard Nossum3632dee2009-01-22 15:29:45 +0100131 */
Eric Paris63c882a2009-05-21 17:02:01 -0400132static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
133 size_t count)
Vegard Nossum3632dee2009-01-22 15:29:45 +0100134{
135 size_t event_size = sizeof(struct inotify_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400136 struct fsnotify_event *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100137
Eric Paris63c882a2009-05-21 17:02:01 -0400138 if (fsnotify_notify_queue_is_empty(group))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100139 return NULL;
140
Jan Kara8ba8fa912014-08-06 16:03:26 -0700141 event = fsnotify_peek_first_event(group);
Eric Paris63c882a2009-05-21 17:02:01 -0400142
Eric Paris5ba08e22010-07-28 10:18:37 -0400143 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
144
Jan Karae9fe6902014-01-21 15:48:13 -0800145 event_size += round_event_name_len(event);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100146 if (event_size > count)
147 return ERR_PTR(-EINVAL);
148
Jan Karac21dbe22016-10-07 16:56:52 -0700149 /* held the notification_lock the whole time, so this is the
Eric Paris63c882a2009-05-21 17:02:01 -0400150 * same event we peeked above */
Jan Kara8ba8fa912014-08-06 16:03:26 -0700151 fsnotify_remove_first_event(group);
Eric Paris63c882a2009-05-21 17:02:01 -0400152
153 return event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100154}
155
156/*
157 * Copy an event to user space, returning how much we copied.
158 *
159 * We already checked that the event size is smaller than the
160 * buffer we had in "get_one_event()" above.
161 */
Eric Paris63c882a2009-05-21 17:02:01 -0400162static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800163 struct fsnotify_event *fsn_event,
Vegard Nossum3632dee2009-01-22 15:29:45 +0100164 char __user *buf)
165{
Eric Paris63c882a2009-05-21 17:02:01 -0400166 struct inotify_event inotify_event;
Jan Kara7053aee2014-01-21 15:48:14 -0800167 struct inotify_event_info *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100168 size_t event_size = sizeof(struct inotify_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800169 size_t name_len;
170 size_t pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100171
Jan Kara7053aee2014-01-21 15:48:14 -0800172 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
Eric Paris5ba08e22010-07-28 10:18:37 -0400173
Jan Kara7053aee2014-01-21 15:48:14 -0800174 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800175 name_len = event->name_len;
Brian Rogersb962e732009-08-28 10:00:05 -0400176 /*
Jan Karae9fe6902014-01-21 15:48:13 -0800177 * round up name length so it is a multiple of event_size
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700178 * plus an extra byte for the terminating '\0'.
179 */
Jan Kara7053aee2014-01-21 15:48:14 -0800180 pad_name_len = round_event_name_len(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800181 inotify_event.len = pad_name_len;
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200182 inotify_event.mask = inotify_mask_to_arg(event->mask);
Jan Kara7053aee2014-01-21 15:48:14 -0800183 inotify_event.wd = event->wd;
Eric Paris63c882a2009-05-21 17:02:01 -0400184 inotify_event.cookie = event->sync_cookie;
185
186 /* send the main event */
187 if (copy_to_user(buf, &inotify_event, event_size))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100188 return -EFAULT;
189
Eric Paris63c882a2009-05-21 17:02:01 -0400190 buf += event_size;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100191
Eric Paris63c882a2009-05-21 17:02:01 -0400192 /*
193 * fsnotify only stores the pathname, so here we have to send the pathname
194 * and then pad that pathname out to a multiple of sizeof(inotify_event)
Jan Karae9fe6902014-01-21 15:48:13 -0800195 * with zeros.
Eric Paris63c882a2009-05-21 17:02:01 -0400196 */
Jan Karae9fe6902014-01-21 15:48:13 -0800197 if (pad_name_len) {
Eric Paris63c882a2009-05-21 17:02:01 -0400198 /* copy the path name */
Jan Kara7053aee2014-01-21 15:48:14 -0800199 if (copy_to_user(buf, event->name, name_len))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100200 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800201 buf += name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100202
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700203 /* fill userspace with 0's */
Jan Karae9fe6902014-01-21 15:48:13 -0800204 if (clear_user(buf, pad_name_len - name_len))
Eric Paris63c882a2009-05-21 17:02:01 -0400205 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800206 event_size += pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100207 }
Eric Paris63c882a2009-05-21 17:02:01 -0400208
Vegard Nossum3632dee2009-01-22 15:29:45 +0100209 return event_size;
210}
211
Amy Griffis2d9048e2006-06-01 13:10:59 -0700212static ssize_t inotify_read(struct file *file, char __user *buf,
213 size_t count, loff_t *pos)
214{
Eric Paris63c882a2009-05-21 17:02:01 -0400215 struct fsnotify_group *group;
216 struct fsnotify_event *kevent;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700217 char __user *start;
218 int ret;
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200219 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700220
221 start = buf;
Eric Paris63c882a2009-05-21 17:02:01 -0400222 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700223
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200224 add_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700225 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700226 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400227 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700228 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700229
Eric Paris5ba08e22010-07-28 10:18:37 -0400230 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
231
Vegard Nossum3632dee2009-01-22 15:29:45 +0100232 if (kevent) {
233 ret = PTR_ERR(kevent);
234 if (IS_ERR(kevent))
235 break;
Eric Paris63c882a2009-05-21 17:02:01 -0400236 ret = copy_event_to_user(group, kevent, buf);
Jan Kara7053aee2014-01-21 15:48:14 -0800237 fsnotify_destroy_event(group, kevent);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100238 if (ret < 0)
239 break;
240 buf += ret;
241 count -= ret;
242 continue;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700243 }
244
Vegard Nossum3632dee2009-01-22 15:29:45 +0100245 ret = -EAGAIN;
246 if (file->f_flags & O_NONBLOCK)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700247 break;
Eric Paris1ca39ab2012-03-26 13:07:59 -0400248 ret = -ERESTARTSYS;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100249 if (signal_pending(current))
250 break;
251
252 if (start != buf)
253 break;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700254
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200255 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700256 }
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200257 remove_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700258
Vegard Nossum3632dee2009-01-22 15:29:45 +0100259 if (start != buf && ret != -EFAULT)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700260 ret = buf - start;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700261 return ret;
262}
263
264static int inotify_release(struct inode *ignored, struct file *file)
265{
Eric Paris63c882a2009-05-21 17:02:01 -0400266 struct fsnotify_group *group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700267
Eric Paris5ba08e22010-07-28 10:18:37 -0400268 pr_debug("%s: group=%p\n", __func__, group);
269
Eric Paris63c882a2009-05-21 17:02:01 -0400270 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200271 fsnotify_destroy_group(group);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700272
273 return 0;
274}
275
276static long inotify_ioctl(struct file *file, unsigned int cmd,
277 unsigned long arg)
278{
Eric Paris63c882a2009-05-21 17:02:01 -0400279 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800280 struct fsnotify_event *fsn_event;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700281 void __user *p;
282 int ret = -ENOTTY;
Eric Paris63c882a2009-05-21 17:02:01 -0400283 size_t send_len = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700284
Eric Paris63c882a2009-05-21 17:02:01 -0400285 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700286 p = (void __user *) arg;
287
Eric Paris5ba08e22010-07-28 10:18:37 -0400288 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
289
Amy Griffis2d9048e2006-06-01 13:10:59 -0700290 switch (cmd) {
291 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700292 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800293 list_for_each_entry(fsn_event, &group->notification_list,
294 list) {
Eric Paris63c882a2009-05-21 17:02:01 -0400295 send_len += sizeof(struct inotify_event);
Jan Kara7053aee2014-01-21 15:48:14 -0800296 send_len += round_event_name_len(fsn_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400297 }
Jan Karac21dbe22016-10-07 16:56:52 -0700298 spin_unlock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400299 ret = put_user(send_len, (int __user *) p);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700300 break;
Kirill Tkhaie1603b62018-02-09 18:04:54 +0300301#ifdef CONFIG_CHECKPOINT_RESTORE
302 case INOTIFY_IOC_SETNEXTWD:
303 ret = -EINVAL;
304 if (arg >= 1 && arg <= INT_MAX) {
305 struct inotify_group_private_data *data;
306
307 data = &group->inotify_data;
308 spin_lock(&data->idr_lock);
309 idr_set_cursor(&data->idr, (unsigned int)arg);
310 spin_unlock(&data->idr_lock);
311 ret = 0;
312 }
313 break;
314#endif /* CONFIG_CHECKPOINT_RESTORE */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700315 }
316
317 return ret;
318}
319
320static const struct file_operations inotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800321 .show_fdinfo = inotify_show_fdinfo,
Eric Paris63c882a2009-05-21 17:02:01 -0400322 .poll = inotify_poll,
323 .read = inotify_read,
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400324 .fasync = fsnotify_fasync,
Eric Paris63c882a2009-05-21 17:02:01 -0400325 .release = inotify_release,
326 .unlocked_ioctl = inotify_ioctl,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700327 .compat_ioctl = inotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200328 .llseek = noop_llseek,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700329};
330
Amy Griffis2d9048e2006-06-01 13:10:59 -0700331
Eric Paris63c882a2009-05-21 17:02:01 -0400332/*
333 * find_inode - resolve a user-given path to a specific inode
334 */
Aaron Goidelac5656d2019-08-12 11:20:00 -0400335static int inotify_find_inode(const char __user *dirname, struct path *path,
336 unsigned int flags, __u64 mask)
Eric Paris63c882a2009-05-21 17:02:01 -0400337{
338 int error;
339
340 error = user_path_at(AT_FDCWD, dirname, flags, path);
341 if (error)
342 return error;
343 /* you can only watch an inode if you have read permissions on it */
344 error = inode_permission(path->dentry->d_inode, MAY_READ);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400345 if (error) {
346 path_put(path);
347 return error;
348 }
349 error = security_path_notify(path, mask,
350 FSNOTIFY_OBJ_TYPE_INODE);
Eric Paris63c882a2009-05-21 17:02:01 -0400351 if (error)
352 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400353
Eric Paris63c882a2009-05-21 17:02:01 -0400354 return error;
355}
356
Eric Parisb7ba8372009-12-17 20:12:04 -0500357static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
Eric Paris000285d2009-12-17 21:24:24 -0500358 struct inotify_inode_mark *i_mark)
Eric Parisb7ba8372009-12-17 20:12:04 -0500359{
360 int ret;
361
Tejun Heo4542da62013-02-27 17:04:50 -0800362 idr_preload(GFP_KERNEL);
363 spin_lock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500364
Jeff Laytona66c04b2013-04-29 16:21:21 -0700365 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
Tejun Heo4542da62013-02-27 17:04:50 -0800366 if (ret >= 0) {
Eric Parisb7ba8372009-12-17 20:12:04 -0500367 /* we added the mark to the idr, take a reference */
Tejun Heo4542da62013-02-27 17:04:50 -0800368 i_mark->wd = ret;
Tejun Heo4542da62013-02-27 17:04:50 -0800369 fsnotify_get_mark(&i_mark->fsn_mark);
370 }
Eric Parisb7ba8372009-12-17 20:12:04 -0500371
Tejun Heo4542da62013-02-27 17:04:50 -0800372 spin_unlock(idr_lock);
373 idr_preload_end();
374 return ret < 0 ? ret : 0;
Eric Parisb7ba8372009-12-17 20:12:04 -0500375}
376
Eric Paris000285d2009-12-17 21:24:24 -0500377static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500378 int wd)
379{
380 struct idr *idr = &group->inotify_data.idr;
381 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500382 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500383
384 assert_spin_locked(idr_lock);
385
Eric Paris000285d2009-12-17 21:24:24 -0500386 i_mark = idr_find(idr, wd);
387 if (i_mark) {
388 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500389
Eric Paris000285d2009-12-17 21:24:24 -0500390 fsnotify_get_mark(fsn_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500391 /* One ref for being in the idr, one ref we just took */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300392 BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
Eric Parisb7ba8372009-12-17 20:12:04 -0500393 }
394
Eric Paris000285d2009-12-17 21:24:24 -0500395 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500396}
397
Eric Paris000285d2009-12-17 21:24:24 -0500398static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500399 int wd)
400{
Eric Paris000285d2009-12-17 21:24:24 -0500401 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500402 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
403
404 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500405 i_mark = inotify_idr_find_locked(group, wd);
Eric Parisb7ba8372009-12-17 20:12:04 -0500406 spin_unlock(idr_lock);
407
Eric Paris000285d2009-12-17 21:24:24 -0500408 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500409}
410
Eric Parisdead5372009-08-24 16:03:35 -0400411/*
412 * Remove the mark from the idr (if present) and drop the reference
413 * on the mark because it was in the idr.
414 */
Eric Paris7e790dd2009-07-07 10:28:24 -0400415static void inotify_remove_from_idr(struct fsnotify_group *group,
Eric Paris000285d2009-12-17 21:24:24 -0500416 struct inotify_inode_mark *i_mark)
Eric Paris7e790dd2009-07-07 10:28:24 -0400417{
Jan Karae7253762016-12-21 11:50:39 +0100418 struct idr *idr = &group->inotify_data.idr;
Eric Parisb7ba8372009-12-17 20:12:04 -0500419 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500420 struct inotify_inode_mark *found_i_mark = NULL;
Eric Parisdead5372009-08-24 16:03:35 -0400421 int wd;
Eric Paris7e790dd2009-07-07 10:28:24 -0400422
Eric Parisb7ba8372009-12-17 20:12:04 -0500423 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500424 wd = i_mark->wd;
Eric Parisdead5372009-08-24 16:03:35 -0400425
Eric Parisb7ba8372009-12-17 20:12:04 -0500426 /*
Eric Paris000285d2009-12-17 21:24:24 -0500427 * does this i_mark think it is in the idr? we shouldn't get called
Eric Parisb7ba8372009-12-17 20:12:04 -0500428 * if it wasn't....
429 */
430 if (wd == -1) {
Jan Kara25c829a2016-12-09 09:38:55 +0100431 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
432 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisdead5372009-08-24 16:03:35 -0400433 goto out;
434 }
435
Eric Parisb7ba8372009-12-17 20:12:04 -0500436 /* Lets look in the idr to see if we find it */
Eric Paris000285d2009-12-17 21:24:24 -0500437 found_i_mark = inotify_idr_find_locked(group, wd);
438 if (unlikely(!found_i_mark)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100439 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
440 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500441 goto out;
442 }
Eric Parisdead5372009-08-24 16:03:35 -0400443
Eric Parisb7ba8372009-12-17 20:12:04 -0500444 /*
Eric Paris000285d2009-12-17 21:24:24 -0500445 * We found an mark in the idr at the right wd, but it's
446 * not the mark we were told to remove. eparis seriously
Eric Parisb7ba8372009-12-17 20:12:04 -0500447 * fucked up somewhere.
448 */
Eric Paris000285d2009-12-17 21:24:24 -0500449 if (unlikely(found_i_mark != i_mark)) {
450 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
Jan Kara25c829a2016-12-09 09:38:55 +0100451 "found_i_mark=%p found_i_mark->wd=%d "
452 "found_i_mark->group=%p\n", __func__, i_mark,
453 i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
454 found_i_mark->wd, found_i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500455 goto out;
456 }
Eric Parisdead5372009-08-24 16:03:35 -0400457
Eric Parisb7ba8372009-12-17 20:12:04 -0500458 /*
459 * One ref for being in the idr
Eric Parisb7ba8372009-12-17 20:12:04 -0500460 * one ref grabbed by inotify_idr_find
461 */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300462 if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100463 printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
464 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500465 /* we can't really recover with bad ref cnting.. */
466 BUG();
467 }
468
Jan Karae7253762016-12-21 11:50:39 +0100469 idr_remove(idr, wd);
470 /* Removed from the idr, drop that ref. */
471 fsnotify_put_mark(&i_mark->fsn_mark);
Eric Parisdead5372009-08-24 16:03:35 -0400472out:
Jan Karae7253762016-12-21 11:50:39 +0100473 i_mark->wd = -1;
474 spin_unlock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500475 /* match the ref taken by inotify_idr_find_locked() */
Eric Paris000285d2009-12-17 21:24:24 -0500476 if (found_i_mark)
477 fsnotify_put_mark(&found_i_mark->fsn_mark);
Eric Paris7e790dd2009-07-07 10:28:24 -0400478}
Eric Parisdead5372009-08-24 16:03:35 -0400479
Eric Paris63c882a2009-05-21 17:02:01 -0400480/*
Eric Parisdead5372009-08-24 16:03:35 -0400481 * Send IN_IGNORED for this wd, remove this wd from the idr.
Eric Paris63c882a2009-05-21 17:02:01 -0400482 */
Eric Paris000285d2009-12-17 21:24:24 -0500483void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
Eric Paris528da3e2009-06-12 16:04:26 -0400484 struct fsnotify_group *group)
Eric Paris63c882a2009-05-21 17:02:01 -0400485{
Eric Paris000285d2009-12-17 21:24:24 -0500486 struct inotify_inode_mark *i_mark;
Amir Goldstein47d9c7c2018-04-20 16:10:52 -0700487 struct fsnotify_iter_info iter_info = { };
488
489 fsnotify_iter_set_report_type_mark(&iter_info, FSNOTIFY_OBJ_TYPE_INODE,
490 fsn_mark);
Jan Kara7053aee2014-01-21 15:48:14 -0800491
492 /* Queue ignore event for the watch */
Amir Goldsteinb54cecf2020-06-07 12:10:40 +0300493 inotify_handle_event(group, FS_IN_IGNORED, NULL, FSNOTIFY_EVENT_NONE,
494 NULL, NULL, 0, &iter_info);
Eric Paris63c882a2009-05-21 17:02:01 -0400495
Lino Sanfilippo8b99c3c2012-03-24 23:44:19 +0100496 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
Eric Paris000285d2009-12-17 21:24:24 -0500497 /* remove this mark from the idr */
498 inotify_remove_from_idr(group, i_mark);
Eric Paris63c882a2009-05-21 17:02:01 -0400499
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200500 dec_inotify_watches(group->inotify_data.ucounts);
Eric Paris63c882a2009-05-21 17:02:01 -0400501}
502
Eric Paris52cef752009-08-24 16:03:35 -0400503static int inotify_update_existing_watch(struct fsnotify_group *group,
504 struct inode *inode,
505 u32 arg)
Eric Paris63c882a2009-05-21 17:02:01 -0400506{
Eric Paris000285d2009-12-17 21:24:24 -0500507 struct fsnotify_mark *fsn_mark;
508 struct inotify_inode_mark *i_mark;
Eric Paris63c882a2009-05-21 17:02:01 -0400509 __u32 old_mask, new_mask;
Eric Paris52cef752009-08-24 16:03:35 -0400510 __u32 mask;
511 int add = (arg & IN_MASK_ADD);
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000512 int create = (arg & IN_MASK_CREATE);
Eric Paris52cef752009-08-24 16:03:35 -0400513 int ret;
Eric Paris63c882a2009-05-21 17:02:01 -0400514
Eric Paris63c882a2009-05-21 17:02:01 -0400515 mask = inotify_arg_to_mask(arg);
Eric Paris63c882a2009-05-21 17:02:01 -0400516
Jan Karab1362ed2016-12-21 16:28:45 +0100517 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Eric Paris000285d2009-12-17 21:24:24 -0500518 if (!fsn_mark)
Eric Paris52cef752009-08-24 16:03:35 -0400519 return -ENOENT;
ZhangXiaoxu62c9d262019-03-02 09:17:32 +0800520 else if (create) {
521 ret = -EEXIST;
522 goto out;
523 }
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
ZhangXiaoxu62c9d262019-03-02 09:17:32 +0800551out:
Eric Parisd0775442009-12-17 21:24:24 -0500552 /* match the get from fsnotify_find_mark() */
Eric Paris000285d2009-12-17 21:24:24 -0500553 fsnotify_put_mark(fsn_mark);
Eric Paris75fe2b262009-07-07 10:28:23 -0400554
Eric Paris52cef752009-08-24 16:03:35 -0400555 return ret;
556}
557
558static int inotify_new_watch(struct fsnotify_group *group,
559 struct inode *inode,
560 u32 arg)
561{
Eric Paris000285d2009-12-17 21:24:24 -0500562 struct inotify_inode_mark *tmp_i_mark;
Eric Paris52cef752009-08-24 16:03:35 -0400563 __u32 mask;
564 int ret;
Eric Parisb7ba8372009-12-17 20:12:04 -0500565 struct idr *idr = &group->inotify_data.idr;
566 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris52cef752009-08-24 16:03:35 -0400567
Eric Paris52cef752009-08-24 16:03:35 -0400568 mask = inotify_arg_to_mask(arg);
Eric Paris52cef752009-08-24 16:03:35 -0400569
Eric Paris000285d2009-12-17 21:24:24 -0500570 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
571 if (unlikely(!tmp_i_mark))
Eric Paris52cef752009-08-24 16:03:35 -0400572 return -ENOMEM;
573
Jan Kara054c6362016-12-21 18:06:12 +0100574 fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
Eric Paris000285d2009-12-17 21:24:24 -0500575 tmp_i_mark->fsn_mark.mask = mask;
576 tmp_i_mark->wd = -1;
Eric Paris52cef752009-08-24 16:03:35 -0400577
Jeff Laytona66c04b2013-04-29 16:21:21 -0700578 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500579 if (ret)
Eric Paris52cef752009-08-24 16:03:35 -0400580 goto out_err;
581
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200582 /* increment the number of watches the user has */
583 if (!inc_inotify_watches(group->inotify_data.ucounts)) {
584 inotify_remove_from_idr(group, tmp_i_mark);
585 ret = -ENOSPC;
586 goto out_err;
587 }
588
Eric Paris52cef752009-08-24 16:03:35 -0400589 /* we are on the idr, now get on the inode */
Amir Goldsteinb249f5b2018-04-20 16:10:55 -0700590 ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
Eric Paris52cef752009-08-24 16:03:35 -0400591 if (ret) {
592 /* we failed to get on the inode, get off the idr */
Eric Paris000285d2009-12-17 21:24:24 -0500593 inotify_remove_from_idr(group, tmp_i_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400594 goto out_err;
595 }
596
Eric Paris52cef752009-08-24 16:03:35 -0400597
Eric Paris000285d2009-12-17 21:24:24 -0500598 /* return the watch descriptor for this new mark */
599 ret = tmp_i_mark->wd;
Eric Paris52cef752009-08-24 16:03:35 -0400600
Eric Paris52cef752009-08-24 16:03:35 -0400601out_err:
Eric Paris000285d2009-12-17 21:24:24 -0500602 /* match the ref from fsnotify_init_mark() */
603 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400604
605 return ret;
606}
607
608static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
609{
610 int ret = 0;
611
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700612 mutex_lock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400613 /* try to update and existing watch with the new arg */
614 ret = inotify_update_existing_watch(group, inode, arg);
615 /* no mark present, try to add a new one */
616 if (ret == -ENOENT)
617 ret = inotify_new_watch(group, inode, arg);
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700618 mutex_unlock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400619
Eric Paris63c882a2009-05-21 17:02:01 -0400620 return ret;
621}
622
Eric Parisd0de4dc2011-04-05 17:20:50 -0400623static struct fsnotify_group *inotify_new_group(unsigned int max_events)
Eric Paris63c882a2009-05-21 17:02:01 -0400624{
625 struct fsnotify_group *group;
Jan Karaff57cd52014-02-21 19:14:11 +0100626 struct inotify_event_info *oevent;
Eric Paris63c882a2009-05-21 17:02:01 -0400627
Eric Paris0d2e2a12009-12-17 21:24:22 -0500628 group = fsnotify_alloc_group(&inotify_fsnotify_ops);
Eric Paris63c882a2009-05-21 17:02:01 -0400629 if (IS_ERR(group))
630 return group;
631
Jan Karaff57cd52014-02-21 19:14:11 +0100632 oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
633 if (unlikely(!oevent)) {
634 fsnotify_destroy_group(group);
635 return ERR_PTR(-ENOMEM);
636 }
637 group->overflow_event = &oevent->fse;
Amir Goldsteindfc2d252020-03-19 17:10:15 +0200638 fsnotify_init_event(group->overflow_event, 0);
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200639 oevent->mask = FS_Q_OVERFLOW;
Jan Karaff57cd52014-02-21 19:14:11 +0100640 oevent->wd = -1;
641 oevent->sync_cookie = 0;
642 oevent->name_len = 0;
643
Eric Paris63c882a2009-05-21 17:02:01 -0400644 group->max_events = max_events;
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700645 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris63c882a2009-05-21 17:02:01 -0400646
647 spin_lock_init(&group->inotify_data.idr_lock);
648 idr_init(&group->inotify_data.idr);
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200649 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
650 current_euid(),
651 UCOUNT_INOTIFY_INSTANCES);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400652
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200653 if (!group->inotify_data.ucounts) {
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200654 fsnotify_destroy_group(group);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400655 return ERR_PTR(-EMFILE);
656 }
Eric Paris63c882a2009-05-21 17:02:01 -0400657
658 return group;
659}
660
661
662/* inotify syscalls */
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100663static int do_inotify_init(int flags)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700664{
Eric Paris63c882a2009-05-21 17:02:01 -0400665 struct fsnotify_group *group;
Al Viroc44dcc52010-02-11 02:24:46 -0500666 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700667
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700668 /* Check the IN_* constants for consistency. */
669 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
670 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
671
Ulrich Drepper510df2d2008-07-23 21:29:41 -0700672 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
Ulrich Drepper40065532008-07-23 21:29:32 -0700673 return -EINVAL;
674
Eric Paris63c882a2009-05-21 17:02:01 -0400675 /* 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 -0400676 group = inotify_new_group(inotify_max_queued_events);
677 if (IS_ERR(group))
678 return PTR_ERR(group);
Al Viro825f9692009-08-05 18:35:21 +0400679
Al Viroc44dcc52010-02-11 02:24:46 -0500680 ret = anon_inode_getfd("inotify", &inotify_fops, group,
681 O_RDONLY | flags);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400682 if (ret < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200683 fsnotify_destroy_group(group);
Al Viro825f9692009-08-05 18:35:21 +0400684
Amy Griffis2d9048e2006-06-01 13:10:59 -0700685 return ret;
686}
687
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100688SYSCALL_DEFINE1(inotify_init1, int, flags)
689{
690 return do_inotify_init(flags);
691}
692
Heiko Carstens938bb9f2009-01-14 14:14:30 +0100693SYSCALL_DEFINE0(inotify_init)
Ulrich Drepper40065532008-07-23 21:29:32 -0700694{
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100695 return do_inotify_init(0);
Ulrich Drepper40065532008-07-23 21:29:32 -0700696}
697
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100698SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
699 u32, mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700700{
Eric Paris63c882a2009-05-21 17:02:01 -0400701 struct fsnotify_group *group;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700702 struct inode *inode;
Al Viro2d8f3032008-07-22 09:59:21 -0400703 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -0400704 struct fd f;
705 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700706 unsigned flags = 0;
707
Dave Hansend30e2c02015-11-05 18:43:46 -0800708 /*
709 * We share a lot of code with fs/dnotify. We also share
710 * the bit layout between inotify's IN_* and the fsnotify
711 * FS_*. This check ensures that only the inotify IN_*
712 * bits get passed in and set in watches/events.
713 */
714 if (unlikely(mask & ~ALL_INOTIFY_BITS))
715 return -EINVAL;
716 /*
717 * Require at least one valid bit set in the mask.
718 * Without _something_ set, we would have no events to
719 * watch for.
720 */
Zhao Hongjiang04df32f2013-04-30 15:26:46 -0700721 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
722 return -EINVAL;
723
Al Viro2903ff02012-08-28 12:52:22 -0400724 f = fdget(fd);
725 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700726 return -EBADF;
727
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000728 /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
Tetsuo Handa125892e2019-01-01 18:54:26 +0900729 if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
730 ret = -EINVAL;
731 goto fput_and_out;
732 }
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000733
Amy Griffis2d9048e2006-06-01 13:10:59 -0700734 /* verify that this is indeed an inotify instance */
Al Viro2903ff02012-08-28 12:52:22 -0400735 if (unlikely(f.file->f_op != &inotify_fops)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700736 ret = -EINVAL;
737 goto fput_and_out;
738 }
739
740 if (!(mask & IN_DONT_FOLLOW))
741 flags |= LOOKUP_FOLLOW;
742 if (mask & IN_ONLYDIR)
743 flags |= LOOKUP_DIRECTORY;
744
Aaron Goidelac5656d2019-08-12 11:20:00 -0400745 ret = inotify_find_inode(pathname, &path, flags,
746 (mask & IN_ALL_EVENTS));
Eric Paris63c882a2009-05-21 17:02:01 -0400747 if (ret)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700748 goto fput_and_out;
749
Eric Paris63c882a2009-05-21 17:02:01 -0400750 /* inode held in place by reference to path; group by fget on fd */
Al Viro2d8f3032008-07-22 09:59:21 -0400751 inode = path.dentry->d_inode;
Al Viro2903ff02012-08-28 12:52:22 -0400752 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700753
Eric Paris63c882a2009-05-21 17:02:01 -0400754 /* create/update an inode mark */
755 ret = inotify_update_watch(group, inode, mask);
Al Viro2d8f3032008-07-22 09:59:21 -0400756 path_put(&path);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700757fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400758 fdput(f);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700759 return ret;
760}
761
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100762SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700763{
Eric Paris63c882a2009-05-21 17:02:01 -0400764 struct fsnotify_group *group;
Eric Paris000285d2009-12-17 21:24:24 -0500765 struct inotify_inode_mark *i_mark;
Al Viro2903ff02012-08-28 12:52:22 -0400766 struct fd f;
youngjun7b26aa22020-04-26 07:33:16 -0700767 int ret = -EINVAL;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700768
Al Viro2903ff02012-08-28 12:52:22 -0400769 f = fdget(fd);
770 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700771 return -EBADF;
772
773 /* verify that this is indeed an inotify instance */
Al Viro2903ff02012-08-28 12:52:22 -0400774 if (unlikely(f.file->f_op != &inotify_fops))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700775 goto out;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700776
Al Viro2903ff02012-08-28 12:52:22 -0400777 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700778
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);