blob: 8f17719842ec924eb0efbd728e1e6e1d19417d27 [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>
41
42#include "inotify.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080043#include "../fdinfo.h"
Amy Griffis2d9048e2006-06-01 13:10:59 -070044
45#include <asm/ioctls.h>
46
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020047/* configurable via /proc/sys/fs/inotify/ */
Harvey Harrison3c828e42008-02-14 19:31:21 -080048static int inotify_max_queued_events __read_mostly;
Eric Paris63c882a2009-05-21 17:02:01 -040049
Jan Kara054c6362016-12-21 18:06:12 +010050struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
Amy Griffis2d9048e2006-06-01 13:10:59 -070051
Amy Griffis2d9048e2006-06-01 13:10:59 -070052#ifdef CONFIG_SYSCTL
53
54#include <linux/sysctl.h>
55
56static int zero;
57
Joe Perches92f778d2014-06-06 14:38:04 -070058struct ctl_table inotify_table[] = {
Amy Griffis2d9048e2006-06-01 13:10:59 -070059 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070060 .procname = "max_user_instances",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020061 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
Amy Griffis2d9048e2006-06-01 13:10:59 -070062 .maxlen = sizeof(int),
63 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080064 .proc_handler = proc_dointvec_minmax,
Amy Griffis2d9048e2006-06-01 13:10:59 -070065 .extra1 = &zero,
66 },
67 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070068 .procname = "max_user_watches",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020069 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
Amy Griffis2d9048e2006-06-01 13:10:59 -070070 .maxlen = sizeof(int),
71 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080072 .proc_handler = proc_dointvec_minmax,
Amy Griffis2d9048e2006-06-01 13:10:59 -070073 .extra1 = &zero,
74 },
75 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070076 .procname = "max_queued_events",
77 .data = &inotify_max_queued_events,
78 .maxlen = sizeof(int),
79 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080080 .proc_handler = proc_dointvec_minmax,
Amy Griffis2d9048e2006-06-01 13:10:59 -070081 .extra1 = &zero
82 },
Eric W. Biedermanab092032009-11-05 14:25:10 -080083 { }
Amy Griffis2d9048e2006-06-01 13:10:59 -070084};
85#endif /* CONFIG_SYSCTL */
86
Eric Paris63c882a2009-05-21 17:02:01 -040087static inline __u32 inotify_arg_to_mask(u32 arg)
Amy Griffis2d9048e2006-06-01 13:10:59 -070088{
Eric Paris63c882a2009-05-21 17:02:01 -040089 __u32 mask;
90
Eric Paris611da042010-07-28 10:18:37 -040091 /*
92 * everything should accept their own ignored, cares about children,
93 * and should receive events when the inode is unmounted
94 */
95 mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
Eric Paris63c882a2009-05-21 17:02:01 -040096
97 /* mask off the flags used to open the fd */
Eric Paris8c1934c2010-07-28 10:18:37 -040098 mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
Eric Paris63c882a2009-05-21 17:02:01 -040099
100 return mask;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700101}
102
Eric Paris63c882a2009-05-21 17:02:01 -0400103static inline u32 inotify_mask_to_arg(__u32 mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700104{
Eric Paris63c882a2009-05-21 17:02:01 -0400105 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
106 IN_Q_OVERFLOW);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700107}
108
Eric Paris63c882a2009-05-21 17:02:01 -0400109/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400110static __poll_t inotify_poll(struct file *file, poll_table *wait)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700111{
Eric Paris63c882a2009-05-21 17:02:01 -0400112 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400113 __poll_t ret = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700114
Eric Paris63c882a2009-05-21 17:02:01 -0400115 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700116 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400117 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800118 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700119 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700120
121 return ret;
122}
123
Jan Kara7053aee2014-01-21 15:48:14 -0800124static int round_event_name_len(struct fsnotify_event *fsn_event)
Jan Karae9fe6902014-01-21 15:48:13 -0800125{
Jan Kara7053aee2014-01-21 15:48:14 -0800126 struct inotify_event_info *event;
127
128 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800129 if (!event->name_len)
130 return 0;
131 return roundup(event->name_len + 1, sizeof(struct inotify_event));
132}
133
Vegard Nossum3632dee2009-01-22 15:29:45 +0100134/*
135 * Get an inotify_kernel_event if one exists and is small
136 * enough to fit in "count". Return an error pointer if
137 * not large enough.
138 *
Jan Karac21dbe22016-10-07 16:56:52 -0700139 * Called with the group->notification_lock held.
Vegard Nossum3632dee2009-01-22 15:29:45 +0100140 */
Eric Paris63c882a2009-05-21 17:02:01 -0400141static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
142 size_t count)
Vegard Nossum3632dee2009-01-22 15:29:45 +0100143{
144 size_t event_size = sizeof(struct inotify_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400145 struct fsnotify_event *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100146
Eric Paris63c882a2009-05-21 17:02:01 -0400147 if (fsnotify_notify_queue_is_empty(group))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100148 return NULL;
149
Jan Kara8ba8fa912014-08-06 16:03:26 -0700150 event = fsnotify_peek_first_event(group);
Eric Paris63c882a2009-05-21 17:02:01 -0400151
Eric Paris5ba08e22010-07-28 10:18:37 -0400152 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
153
Jan Karae9fe6902014-01-21 15:48:13 -0800154 event_size += round_event_name_len(event);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100155 if (event_size > count)
156 return ERR_PTR(-EINVAL);
157
Jan Karac21dbe22016-10-07 16:56:52 -0700158 /* held the notification_lock the whole time, so this is the
Eric Paris63c882a2009-05-21 17:02:01 -0400159 * same event we peeked above */
Jan Kara8ba8fa912014-08-06 16:03:26 -0700160 fsnotify_remove_first_event(group);
Eric Paris63c882a2009-05-21 17:02:01 -0400161
162 return event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100163}
164
165/*
166 * Copy an event to user space, returning how much we copied.
167 *
168 * We already checked that the event size is smaller than the
169 * buffer we had in "get_one_event()" above.
170 */
Eric Paris63c882a2009-05-21 17:02:01 -0400171static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800172 struct fsnotify_event *fsn_event,
Vegard Nossum3632dee2009-01-22 15:29:45 +0100173 char __user *buf)
174{
Eric Paris63c882a2009-05-21 17:02:01 -0400175 struct inotify_event inotify_event;
Jan Kara7053aee2014-01-21 15:48:14 -0800176 struct inotify_event_info *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100177 size_t event_size = sizeof(struct inotify_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800178 size_t name_len;
179 size_t pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100180
Jan Kara7053aee2014-01-21 15:48:14 -0800181 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
Eric Paris5ba08e22010-07-28 10:18:37 -0400182
Jan Kara7053aee2014-01-21 15:48:14 -0800183 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800184 name_len = event->name_len;
Brian Rogersb962e732009-08-28 10:00:05 -0400185 /*
Jan Karae9fe6902014-01-21 15:48:13 -0800186 * round up name length so it is a multiple of event_size
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700187 * plus an extra byte for the terminating '\0'.
188 */
Jan Kara7053aee2014-01-21 15:48:14 -0800189 pad_name_len = round_event_name_len(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800190 inotify_event.len = pad_name_len;
Jan Kara7053aee2014-01-21 15:48:14 -0800191 inotify_event.mask = inotify_mask_to_arg(fsn_event->mask);
192 inotify_event.wd = event->wd;
Eric Paris63c882a2009-05-21 17:02:01 -0400193 inotify_event.cookie = event->sync_cookie;
194
195 /* send the main event */
196 if (copy_to_user(buf, &inotify_event, event_size))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100197 return -EFAULT;
198
Eric Paris63c882a2009-05-21 17:02:01 -0400199 buf += event_size;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100200
Eric Paris63c882a2009-05-21 17:02:01 -0400201 /*
202 * fsnotify only stores the pathname, so here we have to send the pathname
203 * and then pad that pathname out to a multiple of sizeof(inotify_event)
Jan Karae9fe6902014-01-21 15:48:13 -0800204 * with zeros.
Eric Paris63c882a2009-05-21 17:02:01 -0400205 */
Jan Karae9fe6902014-01-21 15:48:13 -0800206 if (pad_name_len) {
Eric Paris63c882a2009-05-21 17:02:01 -0400207 /* copy the path name */
Jan Kara7053aee2014-01-21 15:48:14 -0800208 if (copy_to_user(buf, event->name, name_len))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100209 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800210 buf += name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100211
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700212 /* fill userspace with 0's */
Jan Karae9fe6902014-01-21 15:48:13 -0800213 if (clear_user(buf, pad_name_len - name_len))
Eric Paris63c882a2009-05-21 17:02:01 -0400214 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800215 event_size += pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100216 }
Eric Paris63c882a2009-05-21 17:02:01 -0400217
Vegard Nossum3632dee2009-01-22 15:29:45 +0100218 return event_size;
219}
220
Amy Griffis2d9048e2006-06-01 13:10:59 -0700221static ssize_t inotify_read(struct file *file, char __user *buf,
222 size_t count, loff_t *pos)
223{
Eric Paris63c882a2009-05-21 17:02:01 -0400224 struct fsnotify_group *group;
225 struct fsnotify_event *kevent;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700226 char __user *start;
227 int ret;
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200228 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700229
230 start = buf;
Eric Paris63c882a2009-05-21 17:02:01 -0400231 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700232
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200233 add_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700234 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700235 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400236 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700237 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700238
Eric Paris5ba08e22010-07-28 10:18:37 -0400239 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
240
Vegard Nossum3632dee2009-01-22 15:29:45 +0100241 if (kevent) {
242 ret = PTR_ERR(kevent);
243 if (IS_ERR(kevent))
244 break;
Eric Paris63c882a2009-05-21 17:02:01 -0400245 ret = copy_event_to_user(group, kevent, buf);
Jan Kara7053aee2014-01-21 15:48:14 -0800246 fsnotify_destroy_event(group, kevent);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100247 if (ret < 0)
248 break;
249 buf += ret;
250 count -= ret;
251 continue;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700252 }
253
Vegard Nossum3632dee2009-01-22 15:29:45 +0100254 ret = -EAGAIN;
255 if (file->f_flags & O_NONBLOCK)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700256 break;
Eric Paris1ca39ab2012-03-26 13:07:59 -0400257 ret = -ERESTARTSYS;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100258 if (signal_pending(current))
259 break;
260
261 if (start != buf)
262 break;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700263
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200264 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700265 }
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200266 remove_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700267
Vegard Nossum3632dee2009-01-22 15:29:45 +0100268 if (start != buf && ret != -EFAULT)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700269 ret = buf - start;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700270 return ret;
271}
272
273static int inotify_release(struct inode *ignored, struct file *file)
274{
Eric Paris63c882a2009-05-21 17:02:01 -0400275 struct fsnotify_group *group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700276
Eric Paris5ba08e22010-07-28 10:18:37 -0400277 pr_debug("%s: group=%p\n", __func__, group);
278
Eric Paris63c882a2009-05-21 17:02:01 -0400279 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200280 fsnotify_destroy_group(group);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700281
282 return 0;
283}
284
285static long inotify_ioctl(struct file *file, unsigned int cmd,
286 unsigned long arg)
287{
Eric Paris63c882a2009-05-21 17:02:01 -0400288 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800289 struct fsnotify_event *fsn_event;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700290 void __user *p;
291 int ret = -ENOTTY;
Eric Paris63c882a2009-05-21 17:02:01 -0400292 size_t send_len = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700293
Eric Paris63c882a2009-05-21 17:02:01 -0400294 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700295 p = (void __user *) arg;
296
Eric Paris5ba08e22010-07-28 10:18:37 -0400297 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
298
Amy Griffis2d9048e2006-06-01 13:10:59 -0700299 switch (cmd) {
300 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700301 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800302 list_for_each_entry(fsn_event, &group->notification_list,
303 list) {
Eric Paris63c882a2009-05-21 17:02:01 -0400304 send_len += sizeof(struct inotify_event);
Jan Kara7053aee2014-01-21 15:48:14 -0800305 send_len += round_event_name_len(fsn_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400306 }
Jan Karac21dbe22016-10-07 16:56:52 -0700307 spin_unlock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400308 ret = put_user(send_len, (int __user *) p);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700309 break;
Kirill Tkhaie1603b62018-02-09 18:04:54 +0300310#ifdef CONFIG_CHECKPOINT_RESTORE
311 case INOTIFY_IOC_SETNEXTWD:
312 ret = -EINVAL;
313 if (arg >= 1 && arg <= INT_MAX) {
314 struct inotify_group_private_data *data;
315
316 data = &group->inotify_data;
317 spin_lock(&data->idr_lock);
318 idr_set_cursor(&data->idr, (unsigned int)arg);
319 spin_unlock(&data->idr_lock);
320 ret = 0;
321 }
322 break;
323#endif /* CONFIG_CHECKPOINT_RESTORE */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700324 }
325
326 return ret;
327}
328
329static const struct file_operations inotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800330 .show_fdinfo = inotify_show_fdinfo,
Eric Paris63c882a2009-05-21 17:02:01 -0400331 .poll = inotify_poll,
332 .read = inotify_read,
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400333 .fasync = fsnotify_fasync,
Eric Paris63c882a2009-05-21 17:02:01 -0400334 .release = inotify_release,
335 .unlocked_ioctl = inotify_ioctl,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700336 .compat_ioctl = inotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200337 .llseek = noop_llseek,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700338};
339
Amy Griffis2d9048e2006-06-01 13:10:59 -0700340
Eric Paris63c882a2009-05-21 17:02:01 -0400341/*
342 * find_inode - resolve a user-given path to a specific inode
343 */
344static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
345{
346 int error;
347
348 error = user_path_at(AT_FDCWD, dirname, flags, path);
349 if (error)
350 return error;
351 /* you can only watch an inode if you have read permissions on it */
352 error = inode_permission(path->dentry->d_inode, MAY_READ);
353 if (error)
354 path_put(path);
355 return error;
356}
357
Eric Parisb7ba8372009-12-17 20:12:04 -0500358static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
Eric Paris000285d2009-12-17 21:24:24 -0500359 struct inotify_inode_mark *i_mark)
Eric Parisb7ba8372009-12-17 20:12:04 -0500360{
361 int ret;
362
Tejun Heo4542da62013-02-27 17:04:50 -0800363 idr_preload(GFP_KERNEL);
364 spin_lock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500365
Jeff Laytona66c04b2013-04-29 16:21:21 -0700366 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
Tejun Heo4542da62013-02-27 17:04:50 -0800367 if (ret >= 0) {
Eric Parisb7ba8372009-12-17 20:12:04 -0500368 /* we added the mark to the idr, take a reference */
Tejun Heo4542da62013-02-27 17:04:50 -0800369 i_mark->wd = ret;
Tejun Heo4542da62013-02-27 17:04:50 -0800370 fsnotify_get_mark(&i_mark->fsn_mark);
371 }
Eric Parisb7ba8372009-12-17 20:12:04 -0500372
Tejun Heo4542da62013-02-27 17:04:50 -0800373 spin_unlock(idr_lock);
374 idr_preload_end();
375 return ret < 0 ? ret : 0;
Eric Parisb7ba8372009-12-17 20:12:04 -0500376}
377
Eric Paris000285d2009-12-17 21:24:24 -0500378static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500379 int wd)
380{
381 struct idr *idr = &group->inotify_data.idr;
382 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500383 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500384
385 assert_spin_locked(idr_lock);
386
Eric Paris000285d2009-12-17 21:24:24 -0500387 i_mark = idr_find(idr, wd);
388 if (i_mark) {
389 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500390
Eric Paris000285d2009-12-17 21:24:24 -0500391 fsnotify_get_mark(fsn_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500392 /* One ref for being in the idr, one ref we just took */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300393 BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
Eric Parisb7ba8372009-12-17 20:12:04 -0500394 }
395
Eric Paris000285d2009-12-17 21:24:24 -0500396 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500397}
398
Eric Paris000285d2009-12-17 21:24:24 -0500399static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500400 int wd)
401{
Eric Paris000285d2009-12-17 21:24:24 -0500402 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500403 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
404
405 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500406 i_mark = inotify_idr_find_locked(group, wd);
Eric Parisb7ba8372009-12-17 20:12:04 -0500407 spin_unlock(idr_lock);
408
Eric Paris000285d2009-12-17 21:24:24 -0500409 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500410}
411
Eric Parisdead5372009-08-24 16:03:35 -0400412/*
413 * Remove the mark from the idr (if present) and drop the reference
414 * on the mark because it was in the idr.
415 */
Eric Paris7e790dd2009-07-07 10:28:24 -0400416static void inotify_remove_from_idr(struct fsnotify_group *group,
Eric Paris000285d2009-12-17 21:24:24 -0500417 struct inotify_inode_mark *i_mark)
Eric Paris7e790dd2009-07-07 10:28:24 -0400418{
Jan Karae7253762016-12-21 11:50:39 +0100419 struct idr *idr = &group->inotify_data.idr;
Eric Parisb7ba8372009-12-17 20:12:04 -0500420 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500421 struct inotify_inode_mark *found_i_mark = NULL;
Eric Parisdead5372009-08-24 16:03:35 -0400422 int wd;
Eric Paris7e790dd2009-07-07 10:28:24 -0400423
Eric Parisb7ba8372009-12-17 20:12:04 -0500424 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500425 wd = i_mark->wd;
Eric Parisdead5372009-08-24 16:03:35 -0400426
Eric Parisb7ba8372009-12-17 20:12:04 -0500427 /*
Eric Paris000285d2009-12-17 21:24:24 -0500428 * does this i_mark think it is in the idr? we shouldn't get called
Eric Parisb7ba8372009-12-17 20:12:04 -0500429 * if it wasn't....
430 */
431 if (wd == -1) {
Jan Kara25c829a2016-12-09 09:38:55 +0100432 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
433 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisdead5372009-08-24 16:03:35 -0400434 goto out;
435 }
436
Eric Parisb7ba8372009-12-17 20:12:04 -0500437 /* Lets look in the idr to see if we find it */
Eric Paris000285d2009-12-17 21:24:24 -0500438 found_i_mark = inotify_idr_find_locked(group, wd);
439 if (unlikely(!found_i_mark)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100440 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
441 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500442 goto out;
443 }
Eric Parisdead5372009-08-24 16:03:35 -0400444
Eric Parisb7ba8372009-12-17 20:12:04 -0500445 /*
Eric Paris000285d2009-12-17 21:24:24 -0500446 * We found an mark in the idr at the right wd, but it's
447 * not the mark we were told to remove. eparis seriously
Eric Parisb7ba8372009-12-17 20:12:04 -0500448 * fucked up somewhere.
449 */
Eric Paris000285d2009-12-17 21:24:24 -0500450 if (unlikely(found_i_mark != i_mark)) {
451 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
Jan Kara25c829a2016-12-09 09:38:55 +0100452 "found_i_mark=%p found_i_mark->wd=%d "
453 "found_i_mark->group=%p\n", __func__, i_mark,
454 i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
455 found_i_mark->wd, found_i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500456 goto out;
457 }
Eric Parisdead5372009-08-24 16:03:35 -0400458
Eric Parisb7ba8372009-12-17 20:12:04 -0500459 /*
460 * One ref for being in the idr
Eric Parisb7ba8372009-12-17 20:12:04 -0500461 * one ref grabbed by inotify_idr_find
462 */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300463 if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100464 printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
465 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500466 /* we can't really recover with bad ref cnting.. */
467 BUG();
468 }
469
Jan Karae7253762016-12-21 11:50:39 +0100470 idr_remove(idr, wd);
471 /* Removed from the idr, drop that ref. */
472 fsnotify_put_mark(&i_mark->fsn_mark);
Eric Parisdead5372009-08-24 16:03:35 -0400473out:
Jan Karae7253762016-12-21 11:50:39 +0100474 i_mark->wd = -1;
475 spin_unlock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500476 /* match the ref taken by inotify_idr_find_locked() */
Eric Paris000285d2009-12-17 21:24:24 -0500477 if (found_i_mark)
478 fsnotify_put_mark(&found_i_mark->fsn_mark);
Eric Paris7e790dd2009-07-07 10:28:24 -0400479}
Eric Parisdead5372009-08-24 16:03:35 -0400480
Eric Paris63c882a2009-05-21 17:02:01 -0400481/*
Eric Parisdead5372009-08-24 16:03:35 -0400482 * Send IN_IGNORED for this wd, remove this wd from the idr.
Eric Paris63c882a2009-05-21 17:02:01 -0400483 */
Eric Paris000285d2009-12-17 21:24:24 -0500484void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
Eric Paris528da3e2009-06-12 16:04:26 -0400485 struct fsnotify_group *group)
Eric Paris63c882a2009-05-21 17:02:01 -0400486{
Eric Paris000285d2009-12-17 21:24:24 -0500487 struct inotify_inode_mark *i_mark;
Jan Kara7053aee2014-01-21 15:48:14 -0800488
489 /* Queue ignore event for the watch */
490 inotify_handle_event(group, NULL, fsn_mark, NULL, FS_IN_IGNORED,
Jan Kara9385a842016-11-10 17:51:50 +0100491 NULL, FSNOTIFY_EVENT_NONE, NULL, 0, NULL);
Eric Paris63c882a2009-05-21 17:02:01 -0400492
Lino Sanfilippo8b99c3c2012-03-24 23:44:19 +0100493 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
Eric Paris000285d2009-12-17 21:24:24 -0500494 /* remove this mark from the idr */
495 inotify_remove_from_idr(group, i_mark);
Eric Paris63c882a2009-05-21 17:02:01 -0400496
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200497 dec_inotify_watches(group->inotify_data.ucounts);
Eric Paris63c882a2009-05-21 17:02:01 -0400498}
499
Eric Paris52cef752009-08-24 16:03:35 -0400500static int inotify_update_existing_watch(struct fsnotify_group *group,
501 struct inode *inode,
502 u32 arg)
Eric Paris63c882a2009-05-21 17:02:01 -0400503{
Eric Paris000285d2009-12-17 21:24:24 -0500504 struct fsnotify_mark *fsn_mark;
505 struct inotify_inode_mark *i_mark;
Eric Paris63c882a2009-05-21 17:02:01 -0400506 __u32 old_mask, new_mask;
Eric Paris52cef752009-08-24 16:03:35 -0400507 __u32 mask;
508 int add = (arg & IN_MASK_ADD);
509 int ret;
Eric Paris63c882a2009-05-21 17:02:01 -0400510
Eric Paris63c882a2009-05-21 17:02:01 -0400511 mask = inotify_arg_to_mask(arg);
Eric Paris63c882a2009-05-21 17:02:01 -0400512
Jan Karab1362ed2016-12-21 16:28:45 +0100513 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Eric Paris000285d2009-12-17 21:24:24 -0500514 if (!fsn_mark)
Eric Paris52cef752009-08-24 16:03:35 -0400515 return -ENOENT;
Eric Paris63c882a2009-05-21 17:02:01 -0400516
Eric Paris000285d2009-12-17 21:24:24 -0500517 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
Eric Paris75fe2b262009-07-07 10:28:23 -0400518
Eric Paris000285d2009-12-17 21:24:24 -0500519 spin_lock(&fsn_mark->lock);
Eric Paris000285d2009-12-17 21:24:24 -0500520 old_mask = fsn_mark->mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500521 if (add)
Jan Kara66d2b812016-12-21 16:03:59 +0100522 fsn_mark->mask |= mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500523 else
Jan Kara66d2b812016-12-21 16:03:59 +0100524 fsn_mark->mask = mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500525 new_mask = fsn_mark->mask;
Eric Paris000285d2009-12-17 21:24:24 -0500526 spin_unlock(&fsn_mark->lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400527
528 if (old_mask != new_mask) {
529 /* more bits in old than in new? */
530 int dropped = (old_mask & ~new_mask);
Eric Paris000285d2009-12-17 21:24:24 -0500531 /* more bits in this fsn_mark than the inode's mask? */
Eric Paris63c882a2009-05-21 17:02:01 -0400532 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
Eric Paris63c882a2009-05-21 17:02:01 -0400533
Eric Paris000285d2009-12-17 21:24:24 -0500534 /* update the inode with this new fsn_mark */
Eric Paris63c882a2009-05-21 17:02:01 -0400535 if (dropped || do_inode)
Jan Kara8920d272016-12-21 16:13:54 +0100536 fsnotify_recalc_mask(inode->i_fsnotify_marks);
Eric Paris63c882a2009-05-21 17:02:01 -0400537
Eric Paris63c882a2009-05-21 17:02:01 -0400538 }
539
Eric Paris52cef752009-08-24 16:03:35 -0400540 /* return the wd */
Eric Paris000285d2009-12-17 21:24:24 -0500541 ret = i_mark->wd;
Eric Paris52cef752009-08-24 16:03:35 -0400542
Eric Parisd0775442009-12-17 21:24:24 -0500543 /* match the get from fsnotify_find_mark() */
Eric Paris000285d2009-12-17 21:24:24 -0500544 fsnotify_put_mark(fsn_mark);
Eric Paris75fe2b262009-07-07 10:28:23 -0400545
Eric Paris52cef752009-08-24 16:03:35 -0400546 return ret;
547}
548
549static int inotify_new_watch(struct fsnotify_group *group,
550 struct inode *inode,
551 u32 arg)
552{
Eric Paris000285d2009-12-17 21:24:24 -0500553 struct inotify_inode_mark *tmp_i_mark;
Eric Paris52cef752009-08-24 16:03:35 -0400554 __u32 mask;
555 int ret;
Eric Parisb7ba8372009-12-17 20:12:04 -0500556 struct idr *idr = &group->inotify_data.idr;
557 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris52cef752009-08-24 16:03:35 -0400558
Eric Paris52cef752009-08-24 16:03:35 -0400559 mask = inotify_arg_to_mask(arg);
Eric Paris52cef752009-08-24 16:03:35 -0400560
Eric Paris000285d2009-12-17 21:24:24 -0500561 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
562 if (unlikely(!tmp_i_mark))
Eric Paris52cef752009-08-24 16:03:35 -0400563 return -ENOMEM;
564
Jan Kara054c6362016-12-21 18:06:12 +0100565 fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
Eric Paris000285d2009-12-17 21:24:24 -0500566 tmp_i_mark->fsn_mark.mask = mask;
567 tmp_i_mark->wd = -1;
Eric Paris52cef752009-08-24 16:03:35 -0400568
Jeff Laytona66c04b2013-04-29 16:21:21 -0700569 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500570 if (ret)
Eric Paris52cef752009-08-24 16:03:35 -0400571 goto out_err;
572
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200573 /* increment the number of watches the user has */
574 if (!inc_inotify_watches(group->inotify_data.ucounts)) {
575 inotify_remove_from_idr(group, tmp_i_mark);
576 ret = -ENOSPC;
577 goto out_err;
578 }
579
Eric Paris52cef752009-08-24 16:03:35 -0400580 /* we are on the idr, now get on the inode */
Jan Kara7b129322016-12-21 18:32:48 +0100581 ret = fsnotify_add_mark_locked(&tmp_i_mark->fsn_mark, inode, NULL, 0);
Eric Paris52cef752009-08-24 16:03:35 -0400582 if (ret) {
583 /* we failed to get on the inode, get off the idr */
Eric Paris000285d2009-12-17 21:24:24 -0500584 inotify_remove_from_idr(group, tmp_i_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400585 goto out_err;
586 }
587
Eric Paris52cef752009-08-24 16:03:35 -0400588
Eric Paris000285d2009-12-17 21:24:24 -0500589 /* return the watch descriptor for this new mark */
590 ret = tmp_i_mark->wd;
Eric Paris52cef752009-08-24 16:03:35 -0400591
Eric Paris52cef752009-08-24 16:03:35 -0400592out_err:
Eric Paris000285d2009-12-17 21:24:24 -0500593 /* match the ref from fsnotify_init_mark() */
594 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400595
596 return ret;
597}
598
599static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
600{
601 int ret = 0;
602
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700603 mutex_lock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400604 /* try to update and existing watch with the new arg */
605 ret = inotify_update_existing_watch(group, inode, arg);
606 /* no mark present, try to add a new one */
607 if (ret == -ENOENT)
608 ret = inotify_new_watch(group, inode, arg);
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700609 mutex_unlock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400610
Eric Paris63c882a2009-05-21 17:02:01 -0400611 return ret;
612}
613
Eric Parisd0de4dc2011-04-05 17:20:50 -0400614static struct fsnotify_group *inotify_new_group(unsigned int max_events)
Eric Paris63c882a2009-05-21 17:02:01 -0400615{
616 struct fsnotify_group *group;
Jan Karaff57cd52014-02-21 19:14:11 +0100617 struct inotify_event_info *oevent;
Eric Paris63c882a2009-05-21 17:02:01 -0400618
Eric Paris0d2e2a12009-12-17 21:24:22 -0500619 group = fsnotify_alloc_group(&inotify_fsnotify_ops);
Eric Paris63c882a2009-05-21 17:02:01 -0400620 if (IS_ERR(group))
621 return group;
622
Jan Karaff57cd52014-02-21 19:14:11 +0100623 oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
624 if (unlikely(!oevent)) {
625 fsnotify_destroy_group(group);
626 return ERR_PTR(-ENOMEM);
627 }
628 group->overflow_event = &oevent->fse;
629 fsnotify_init_event(group->overflow_event, NULL, FS_Q_OVERFLOW);
630 oevent->wd = -1;
631 oevent->sync_cookie = 0;
632 oevent->name_len = 0;
633
Eric Paris63c882a2009-05-21 17:02:01 -0400634 group->max_events = max_events;
635
636 spin_lock_init(&group->inotify_data.idr_lock);
637 idr_init(&group->inotify_data.idr);
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200638 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
639 current_euid(),
640 UCOUNT_INOTIFY_INSTANCES);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400641
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200642 if (!group->inotify_data.ucounts) {
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200643 fsnotify_destroy_group(group);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400644 return ERR_PTR(-EMFILE);
645 }
Eric Paris63c882a2009-05-21 17:02:01 -0400646
647 return group;
648}
649
650
651/* inotify syscalls */
Heiko Carstens938bb9f2009-01-14 14:14:30 +0100652SYSCALL_DEFINE1(inotify_init1, int, flags)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700653{
Eric Paris63c882a2009-05-21 17:02:01 -0400654 struct fsnotify_group *group;
Al Viroc44dcc52010-02-11 02:24:46 -0500655 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700656
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700657 /* Check the IN_* constants for consistency. */
658 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
659 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
660
Ulrich Drepper510df2d2008-07-23 21:29:41 -0700661 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
Ulrich Drepper40065532008-07-23 21:29:32 -0700662 return -EINVAL;
663
Eric Paris63c882a2009-05-21 17:02:01 -0400664 /* 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 -0400665 group = inotify_new_group(inotify_max_queued_events);
666 if (IS_ERR(group))
667 return PTR_ERR(group);
Al Viro825f9692009-08-05 18:35:21 +0400668
Al Viroc44dcc52010-02-11 02:24:46 -0500669 ret = anon_inode_getfd("inotify", &inotify_fops, group,
670 O_RDONLY | flags);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400671 if (ret < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200672 fsnotify_destroy_group(group);
Al Viro825f9692009-08-05 18:35:21 +0400673
Amy Griffis2d9048e2006-06-01 13:10:59 -0700674 return ret;
675}
676
Heiko Carstens938bb9f2009-01-14 14:14:30 +0100677SYSCALL_DEFINE0(inotify_init)
Ulrich Drepper40065532008-07-23 21:29:32 -0700678{
679 return sys_inotify_init1(0);
680}
681
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100682SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
683 u32, mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700684{
Eric Paris63c882a2009-05-21 17:02:01 -0400685 struct fsnotify_group *group;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700686 struct inode *inode;
Al Viro2d8f3032008-07-22 09:59:21 -0400687 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -0400688 struct fd f;
689 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700690 unsigned flags = 0;
691
Dave Hansend30e2c02015-11-05 18:43:46 -0800692 /*
693 * We share a lot of code with fs/dnotify. We also share
694 * the bit layout between inotify's IN_* and the fsnotify
695 * FS_*. This check ensures that only the inotify IN_*
696 * bits get passed in and set in watches/events.
697 */
698 if (unlikely(mask & ~ALL_INOTIFY_BITS))
699 return -EINVAL;
700 /*
701 * Require at least one valid bit set in the mask.
702 * Without _something_ set, we would have no events to
703 * watch for.
704 */
Zhao Hongjiang04df32f2013-04-30 15:26:46 -0700705 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
706 return -EINVAL;
707
Al Viro2903ff02012-08-28 12:52:22 -0400708 f = fdget(fd);
709 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700710 return -EBADF;
711
712 /* verify that this is indeed an inotify instance */
Al Viro2903ff02012-08-28 12:52:22 -0400713 if (unlikely(f.file->f_op != &inotify_fops)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700714 ret = -EINVAL;
715 goto fput_and_out;
716 }
717
718 if (!(mask & IN_DONT_FOLLOW))
719 flags |= LOOKUP_FOLLOW;
720 if (mask & IN_ONLYDIR)
721 flags |= LOOKUP_DIRECTORY;
722
Eric Paris63c882a2009-05-21 17:02:01 -0400723 ret = inotify_find_inode(pathname, &path, flags);
724 if (ret)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700725 goto fput_and_out;
726
Eric Paris63c882a2009-05-21 17:02:01 -0400727 /* inode held in place by reference to path; group by fget on fd */
Al Viro2d8f3032008-07-22 09:59:21 -0400728 inode = path.dentry->d_inode;
Al Viro2903ff02012-08-28 12:52:22 -0400729 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700730
Eric Paris63c882a2009-05-21 17:02:01 -0400731 /* create/update an inode mark */
732 ret = inotify_update_watch(group, inode, mask);
Al Viro2d8f3032008-07-22 09:59:21 -0400733 path_put(&path);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700734fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400735 fdput(f);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700736 return ret;
737}
738
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100739SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700740{
Eric Paris63c882a2009-05-21 17:02:01 -0400741 struct fsnotify_group *group;
Eric Paris000285d2009-12-17 21:24:24 -0500742 struct inotify_inode_mark *i_mark;
Al Viro2903ff02012-08-28 12:52:22 -0400743 struct fd f;
744 int ret = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700745
Al Viro2903ff02012-08-28 12:52:22 -0400746 f = fdget(fd);
747 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700748 return -EBADF;
749
750 /* verify that this is indeed an inotify instance */
Eric Parisb7ba8372009-12-17 20:12:04 -0500751 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400752 if (unlikely(f.file->f_op != &inotify_fops))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700753 goto out;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700754
Al Viro2903ff02012-08-28 12:52:22 -0400755 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700756
Eric Parisb7ba8372009-12-17 20:12:04 -0500757 ret = -EINVAL;
Eric Paris000285d2009-12-17 21:24:24 -0500758 i_mark = inotify_idr_find(group, wd);
759 if (unlikely(!i_mark))
Eric Paris63c882a2009-05-21 17:02:01 -0400760 goto out;
Eric Paris63c882a2009-05-21 17:02:01 -0400761
Eric Parisb7ba8372009-12-17 20:12:04 -0500762 ret = 0;
763
Lino Sanfilippoe2a29942011-06-14 17:29:51 +0200764 fsnotify_destroy_mark(&i_mark->fsn_mark, group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500765
766 /* match ref taken by inotify_idr_find */
Eric Paris000285d2009-12-17 21:24:24 -0500767 fsnotify_put_mark(&i_mark->fsn_mark);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700768
769out:
Al Viro2903ff02012-08-28 12:52:22 -0400770 fdput(f);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700771 return ret;
772}
773
Amy Griffis2d9048e2006-06-01 13:10:59 -0700774/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100775 * inotify_user_setup - Our initialization function. Note that we cannot return
Amy Griffis2d9048e2006-06-01 13:10:59 -0700776 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
777 * must result in panic().
778 */
779static int __init inotify_user_setup(void)
780{
Eric Parisf874e1a2010-07-28 10:18:37 -0400781 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
782 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
783 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
784 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
785 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
786 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
787 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
788 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
789 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
790 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
791 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
792 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
793 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
794 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
795 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
796 BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
Eric Parisb29866a2010-10-28 17:21:58 -0400797 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
Eric Parisf874e1a2010-07-28 10:18:37 -0400798 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
799
800 BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
801
Eric Paris000285d2009-12-17 21:24:24 -0500802 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
Eric Paris63c882a2009-05-21 17:02:01 -0400803
Amy Griffis2d9048e2006-06-01 13:10:59 -0700804 inotify_max_queued_events = 16384;
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200805 init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
806 init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700807
Amy Griffis2d9048e2006-06-01 13:10:59 -0700808 return 0;
809}
Paul Gortmakerc013d5a2015-05-01 20:08:20 -0400810fs_initcall(inotify_user_setup);