1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * fs/inotify_user.c - inotify support for userspace
6 * John McCutchan <ttb@tentacle.dhs.org>
7 * Robert Love <rml@novell.com>
9 * Copyright (C) 2005 John McCutchan
10 * Copyright 2006 Hewlett-Packard Development Company, L.P.
12 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
13 * inotify was largely rewriten to make use of the fsnotify infrastructure
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>
35 #include "../fdinfo.h"
37 #include <asm/ioctls.h>
39 /* configurable via /proc/sys/fs/inotify/ */
40 static int inotify_max_queued_events __read_mostly;
42 struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
46 #include <linux/sysctl.h>
48 struct ctl_table inotify_table[] = {
50 .procname = "max_user_instances",
51 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
52 .maxlen = sizeof(int),
54 .proc_handler = proc_dointvec_minmax,
55 .extra1 = SYSCTL_ZERO,
58 .procname = "max_user_watches",
59 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
60 .maxlen = sizeof(int),
62 .proc_handler = proc_dointvec_minmax,
63 .extra1 = SYSCTL_ZERO,
66 .procname = "max_queued_events",
67 .data = &inotify_max_queued_events,
68 .maxlen = sizeof(int),
70 .proc_handler = proc_dointvec_minmax,
75 #endif /* CONFIG_SYSCTL */
77 static inline __u32 inotify_arg_to_mask(u32 arg)
82 * everything should accept their own ignored, cares about children,
83 * and should receive events when the inode is unmounted
85 mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
87 /* mask off the flags used to open the fd */
88 mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
93 static inline u32 inotify_mask_to_arg(__u32 mask)
95 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
99 /* intofiy userspace file descriptor functions */
100 static __poll_t inotify_poll(struct file *file, poll_table *wait)
102 struct fsnotify_group *group = file->private_data;
105 poll_wait(file, &group->notification_waitq, wait);
106 spin_lock(&group->notification_lock);
107 if (!fsnotify_notify_queue_is_empty(group))
108 ret = EPOLLIN | EPOLLRDNORM;
109 spin_unlock(&group->notification_lock);
114 static int round_event_name_len(struct fsnotify_event *fsn_event)
116 struct inotify_event_info *event;
118 event = INOTIFY_E(fsn_event);
119 if (!event->name_len)
121 return roundup(event->name_len + 1, sizeof(struct inotify_event));
125 * Get an inotify_kernel_event if one exists and is small
126 * enough to fit in "count". Return an error pointer if
129 * Called with the group->notification_lock held.
131 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
134 size_t event_size = sizeof(struct inotify_event);
135 struct fsnotify_event *event;
137 if (fsnotify_notify_queue_is_empty(group))
140 event = fsnotify_peek_first_event(group);
142 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
144 event_size += round_event_name_len(event);
145 if (event_size > count)
146 return ERR_PTR(-EINVAL);
148 /* held the notification_lock the whole time, so this is the
149 * same event we peeked above */
150 fsnotify_remove_first_event(group);
156 * Copy an event to user space, returning how much we copied.
158 * We already checked that the event size is smaller than the
159 * buffer we had in "get_one_event()" above.
161 static ssize_t copy_event_to_user(struct fsnotify_group *group,
162 struct fsnotify_event *fsn_event,
165 struct inotify_event inotify_event;
166 struct inotify_event_info *event;
167 size_t event_size = sizeof(struct inotify_event);
171 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
173 event = INOTIFY_E(fsn_event);
174 name_len = event->name_len;
176 * round up name length so it is a multiple of event_size
177 * plus an extra byte for the terminating '\0'.
179 pad_name_len = round_event_name_len(fsn_event);
180 inotify_event.len = pad_name_len;
181 inotify_event.mask = inotify_mask_to_arg(event->mask);
182 inotify_event.wd = event->wd;
183 inotify_event.cookie = event->sync_cookie;
185 /* send the main event */
186 if (copy_to_user(buf, &inotify_event, event_size))
192 * fsnotify only stores the pathname, so here we have to send the pathname
193 * and then pad that pathname out to a multiple of sizeof(inotify_event)
197 /* copy the path name */
198 if (copy_to_user(buf, event->name, name_len))
202 /* fill userspace with 0's */
203 if (clear_user(buf, pad_name_len - name_len))
205 event_size += pad_name_len;
211 static ssize_t inotify_read(struct file *file, char __user *buf,
212 size_t count, loff_t *pos)
214 struct fsnotify_group *group;
215 struct fsnotify_event *kevent;
218 DEFINE_WAIT_FUNC(wait, woken_wake_function);
221 group = file->private_data;
223 add_wait_queue(&group->notification_waitq, &wait);
225 spin_lock(&group->notification_lock);
226 kevent = get_one_event(group, count);
227 spin_unlock(&group->notification_lock);
229 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
232 ret = PTR_ERR(kevent);
235 ret = copy_event_to_user(group, kevent, buf);
236 fsnotify_destroy_event(group, kevent);
245 if (file->f_flags & O_NONBLOCK)
248 if (signal_pending(current))
254 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
256 remove_wait_queue(&group->notification_waitq, &wait);
258 if (start != buf && ret != -EFAULT)
263 static int inotify_release(struct inode *ignored, struct file *file)
265 struct fsnotify_group *group = file->private_data;
267 pr_debug("%s: group=%p\n", __func__, group);
269 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
270 fsnotify_destroy_group(group);
275 static long inotify_ioctl(struct file *file, unsigned int cmd,
278 struct fsnotify_group *group;
279 struct fsnotify_event *fsn_event;
284 group = file->private_data;
285 p = (void __user *) arg;
287 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
291 spin_lock(&group->notification_lock);
292 list_for_each_entry(fsn_event, &group->notification_list,
294 send_len += sizeof(struct inotify_event);
295 send_len += round_event_name_len(fsn_event);
297 spin_unlock(&group->notification_lock);
298 ret = put_user(send_len, (int __user *) p);
300 #ifdef CONFIG_CHECKPOINT_RESTORE
301 case INOTIFY_IOC_SETNEXTWD:
303 if (arg >= 1 && arg <= INT_MAX) {
304 struct inotify_group_private_data *data;
306 data = &group->inotify_data;
307 spin_lock(&data->idr_lock);
308 idr_set_cursor(&data->idr, (unsigned int)arg);
309 spin_unlock(&data->idr_lock);
313 #endif /* CONFIG_CHECKPOINT_RESTORE */
319 static const struct file_operations inotify_fops = {
320 .show_fdinfo = inotify_show_fdinfo,
321 .poll = inotify_poll,
322 .read = inotify_read,
323 .fasync = fsnotify_fasync,
324 .release = inotify_release,
325 .unlocked_ioctl = inotify_ioctl,
326 .compat_ioctl = inotify_ioctl,
327 .llseek = noop_llseek,
332 * find_inode - resolve a user-given path to a specific inode
334 static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
338 error = user_path_at(AT_FDCWD, dirname, flags, path);
341 /* you can only watch an inode if you have read permissions on it */
342 error = inode_permission(path->dentry->d_inode, MAY_READ);
348 static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
349 struct inotify_inode_mark *i_mark)
353 idr_preload(GFP_KERNEL);
356 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
358 /* we added the mark to the idr, take a reference */
360 fsnotify_get_mark(&i_mark->fsn_mark);
363 spin_unlock(idr_lock);
365 return ret < 0 ? ret : 0;
368 static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
371 struct idr *idr = &group->inotify_data.idr;
372 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
373 struct inotify_inode_mark *i_mark;
375 assert_spin_locked(idr_lock);
377 i_mark = idr_find(idr, wd);
379 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
381 fsnotify_get_mark(fsn_mark);
382 /* One ref for being in the idr, one ref we just took */
383 BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
389 static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
392 struct inotify_inode_mark *i_mark;
393 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
396 i_mark = inotify_idr_find_locked(group, wd);
397 spin_unlock(idr_lock);
403 * Remove the mark from the idr (if present) and drop the reference
404 * on the mark because it was in the idr.
406 static void inotify_remove_from_idr(struct fsnotify_group *group,
407 struct inotify_inode_mark *i_mark)
409 struct idr *idr = &group->inotify_data.idr;
410 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
411 struct inotify_inode_mark *found_i_mark = NULL;
418 * does this i_mark think it is in the idr? we shouldn't get called
422 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
423 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
427 /* Lets look in the idr to see if we find it */
428 found_i_mark = inotify_idr_find_locked(group, wd);
429 if (unlikely(!found_i_mark)) {
430 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
431 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
436 * We found an mark in the idr at the right wd, but it's
437 * not the mark we were told to remove. eparis seriously
438 * fucked up somewhere.
440 if (unlikely(found_i_mark != i_mark)) {
441 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
442 "found_i_mark=%p found_i_mark->wd=%d "
443 "found_i_mark->group=%p\n", __func__, i_mark,
444 i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
445 found_i_mark->wd, found_i_mark->fsn_mark.group);
450 * One ref for being in the idr
451 * one ref grabbed by inotify_idr_find
453 if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
454 printk(KERN_ERR "%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 /* we can't really recover with bad ref cnting.. */
461 /* Removed from the idr, drop that ref. */
462 fsnotify_put_mark(&i_mark->fsn_mark);
465 spin_unlock(idr_lock);
466 /* match the ref taken by inotify_idr_find_locked() */
468 fsnotify_put_mark(&found_i_mark->fsn_mark);
472 * Send IN_IGNORED for this wd, remove this wd from the idr.
474 void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
475 struct fsnotify_group *group)
477 struct inotify_inode_mark *i_mark;
478 struct fsnotify_iter_info iter_info = { };
480 fsnotify_iter_set_report_type_mark(&iter_info, FSNOTIFY_OBJ_TYPE_INODE,
483 /* Queue ignore event for the watch */
484 inotify_handle_event(group, NULL, FS_IN_IGNORED, NULL,
485 FSNOTIFY_EVENT_NONE, NULL, 0, &iter_info);
487 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
488 /* remove this mark from the idr */
489 inotify_remove_from_idr(group, i_mark);
491 dec_inotify_watches(group->inotify_data.ucounts);
494 static int inotify_update_existing_watch(struct fsnotify_group *group,
498 struct fsnotify_mark *fsn_mark;
499 struct inotify_inode_mark *i_mark;
500 __u32 old_mask, new_mask;
502 int add = (arg & IN_MASK_ADD);
503 int create = (arg & IN_MASK_CREATE);
506 mask = inotify_arg_to_mask(arg);
508 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
516 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
518 spin_lock(&fsn_mark->lock);
519 old_mask = fsn_mark->mask;
521 fsn_mark->mask |= mask;
523 fsn_mark->mask = mask;
524 new_mask = fsn_mark->mask;
525 spin_unlock(&fsn_mark->lock);
527 if (old_mask != new_mask) {
528 /* more bits in old than in new? */
529 int dropped = (old_mask & ~new_mask);
530 /* more bits in this fsn_mark than the inode's mask? */
531 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
533 /* update the inode with this new fsn_mark */
534 if (dropped || do_inode)
535 fsnotify_recalc_mask(inode->i_fsnotify_marks);
543 /* match the get from fsnotify_find_mark() */
544 fsnotify_put_mark(fsn_mark);
549 static int inotify_new_watch(struct fsnotify_group *group,
553 struct inotify_inode_mark *tmp_i_mark;
556 struct idr *idr = &group->inotify_data.idr;
557 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
559 mask = inotify_arg_to_mask(arg);
561 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
562 if (unlikely(!tmp_i_mark))
565 fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
566 tmp_i_mark->fsn_mark.mask = mask;
569 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
573 /* 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);
580 /* we are on the idr, now get on the inode */
581 ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
583 /* we failed to get on the inode, get off the idr */
584 inotify_remove_from_idr(group, tmp_i_mark);
589 /* return the watch descriptor for this new mark */
590 ret = tmp_i_mark->wd;
593 /* match the ref from fsnotify_init_mark() */
594 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
599 static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
603 mutex_lock(&group->mark_mutex);
604 /* 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 */
608 ret = inotify_new_watch(group, inode, arg);
609 mutex_unlock(&group->mark_mutex);
614 static struct fsnotify_group *inotify_new_group(unsigned int max_events)
616 struct fsnotify_group *group;
617 struct inotify_event_info *oevent;
619 group = fsnotify_alloc_group(&inotify_fsnotify_ops);
623 oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
624 if (unlikely(!oevent)) {
625 fsnotify_destroy_group(group);
626 return ERR_PTR(-ENOMEM);
628 group->overflow_event = &oevent->fse;
629 fsnotify_init_event(group->overflow_event, NULL);
630 oevent->mask = FS_Q_OVERFLOW;
632 oevent->sync_cookie = 0;
633 oevent->name_len = 0;
635 group->max_events = max_events;
636 group->memcg = get_mem_cgroup_from_mm(current->mm);
638 spin_lock_init(&group->inotify_data.idr_lock);
639 idr_init(&group->inotify_data.idr);
640 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
642 UCOUNT_INOTIFY_INSTANCES);
644 if (!group->inotify_data.ucounts) {
645 fsnotify_destroy_group(group);
646 return ERR_PTR(-EMFILE);
653 /* inotify syscalls */
654 static int do_inotify_init(int flags)
656 struct fsnotify_group *group;
659 /* Check the IN_* constants for consistency. */
660 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
661 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
663 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
666 /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
667 group = inotify_new_group(inotify_max_queued_events);
669 return PTR_ERR(group);
671 ret = anon_inode_getfd("inotify", &inotify_fops, group,
674 fsnotify_destroy_group(group);
679 SYSCALL_DEFINE1(inotify_init1, int, flags)
681 return do_inotify_init(flags);
684 SYSCALL_DEFINE0(inotify_init)
686 return do_inotify_init(0);
689 SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
692 struct fsnotify_group *group;
700 * We share a lot of code with fs/dnotify. We also share
701 * the bit layout between inotify's IN_* and the fsnotify
702 * FS_*. This check ensures that only the inotify IN_*
703 * bits get passed in and set in watches/events.
705 if (unlikely(mask & ~ALL_INOTIFY_BITS))
708 * Require at least one valid bit set in the mask.
709 * Without _something_ set, we would have no events to
712 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
716 if (unlikely(!f.file))
719 /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
720 if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
725 /* verify that this is indeed an inotify instance */
726 if (unlikely(f.file->f_op != &inotify_fops)) {
731 if (!(mask & IN_DONT_FOLLOW))
732 flags |= LOOKUP_FOLLOW;
733 if (mask & IN_ONLYDIR)
734 flags |= LOOKUP_DIRECTORY;
736 ret = inotify_find_inode(pathname, &path, flags);
740 /* inode held in place by reference to path; group by fget on fd */
741 inode = path.dentry->d_inode;
742 group = f.file->private_data;
744 /* create/update an inode mark */
745 ret = inotify_update_watch(group, inode, mask);
752 SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
754 struct fsnotify_group *group;
755 struct inotify_inode_mark *i_mark;
760 if (unlikely(!f.file))
763 /* verify that this is indeed an inotify instance */
765 if (unlikely(f.file->f_op != &inotify_fops))
768 group = f.file->private_data;
771 i_mark = inotify_idr_find(group, wd);
772 if (unlikely(!i_mark))
777 fsnotify_destroy_mark(&i_mark->fsn_mark, group);
779 /* match ref taken by inotify_idr_find */
780 fsnotify_put_mark(&i_mark->fsn_mark);
788 * inotify_user_setup - Our initialization function. Note that we cannot return
789 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
790 * must result in panic().
792 static int __init inotify_user_setup(void)
794 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
795 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
796 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
797 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
798 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
799 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
800 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
801 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
802 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
803 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
804 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
805 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
806 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
807 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
808 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
809 BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
810 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
811 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
813 BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
815 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
816 SLAB_PANIC|SLAB_ACCOUNT);
818 inotify_max_queued_events = 16384;
819 init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
820 init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
824 fs_initcall(inotify_user_setup);