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