2 * fs/inotify_user.c - inotify support for userspace
5 * John McCutchan <ttb@tentacle.dhs.org>
6 * Robert Love <rml@novell.com>
8 * Copyright (C) 2005 John McCutchan
9 * Copyright 2006 Hewlett-Packard Development Company, L.P.
11 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
12 * inotify was largely rewriten to make use of the fsnotify infrastructure
14 * 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
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.
25 #include <linux/file.h>
26 #include <linux/fs.h> /* struct inode */
27 #include <linux/fsnotify_backend.h>
28 #include <linux/idr.h>
29 #include <linux/init.h> /* module_init */
30 #include <linux/inotify.h>
31 #include <linux/kernel.h> /* roundup() */
32 #include <linux/namei.h> /* LOOKUP_FOLLOW */
33 #include <linux/sched.h> /* struct user */
34 #include <linux/slab.h> /* struct kmem_cache */
35 #include <linux/syscalls.h>
36 #include <linux/types.h>
37 #include <linux/anon_inodes.h>
38 #include <linux/uaccess.h>
39 #include <linux/poll.h>
40 #include <linux/wait.h>
43 #include "../fdinfo.h"
45 #include <asm/ioctls.h>
47 /* these are configurable via /proc/sys/fs/inotify/ */
48 static int inotify_max_user_instances __read_mostly;
49 static int inotify_max_queued_events __read_mostly;
50 static int inotify_max_user_watches __read_mostly;
52 static struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
53 struct kmem_cache *event_priv_cachep __read_mostly;
57 #include <linux/sysctl.h>
61 ctl_table inotify_table[] = {
63 .procname = "max_user_instances",
64 .data = &inotify_max_user_instances,
65 .maxlen = sizeof(int),
67 .proc_handler = proc_dointvec_minmax,
71 .procname = "max_user_watches",
72 .data = &inotify_max_user_watches,
73 .maxlen = sizeof(int),
75 .proc_handler = proc_dointvec_minmax,
79 .procname = "max_queued_events",
80 .data = &inotify_max_queued_events,
81 .maxlen = sizeof(int),
83 .proc_handler = proc_dointvec_minmax,
88 #endif /* CONFIG_SYSCTL */
90 static inline __u32 inotify_arg_to_mask(u32 arg)
95 * everything should accept their own ignored, cares about children,
96 * and should receive events when the inode is unmounted
98 mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
100 /* mask off the flags used to open the fd */
101 mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
106 static inline u32 inotify_mask_to_arg(__u32 mask)
108 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
112 /* intofiy userspace file descriptor functions */
113 static unsigned int inotify_poll(struct file *file, poll_table *wait)
115 struct fsnotify_group *group = file->private_data;
118 poll_wait(file, &group->notification_waitq, wait);
119 mutex_lock(&group->notification_mutex);
120 if (!fsnotify_notify_queue_is_empty(group))
121 ret = POLLIN | POLLRDNORM;
122 mutex_unlock(&group->notification_mutex);
127 static int round_event_name_len(struct fsnotify_event *event)
129 if (!event->name_len)
131 return roundup(event->name_len + 1, sizeof(struct inotify_event));
135 * Get an inotify_kernel_event if one exists and is small
136 * enough to fit in "count". Return an error pointer if
139 * Called with the group->notification_mutex held.
141 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
144 size_t event_size = sizeof(struct inotify_event);
145 struct fsnotify_event *event;
147 if (fsnotify_notify_queue_is_empty(group))
150 event = fsnotify_peek_notify_event(group);
152 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
154 event_size += round_event_name_len(event);
155 if (event_size > count)
156 return ERR_PTR(-EINVAL);
158 /* held the notification_mutex the whole time, so this is the
159 * same event we peeked above */
160 fsnotify_remove_notify_event(group);
166 * Copy an event to user space, returning how much we copied.
168 * We already checked that the event size is smaller than the
169 * buffer we had in "get_one_event()" above.
171 static ssize_t copy_event_to_user(struct fsnotify_group *group,
172 struct fsnotify_event *event,
175 struct inotify_event inotify_event;
176 struct fsnotify_event_private_data *fsn_priv;
177 struct inotify_event_private_data *priv;
178 size_t event_size = sizeof(struct inotify_event);
182 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
184 /* we get the inotify watch descriptor from the event private data */
185 spin_lock(&event->lock);
186 fsn_priv = fsnotify_remove_priv_from_event(group, event);
187 spin_unlock(&event->lock);
190 inotify_event.wd = -1;
192 priv = container_of(fsn_priv, struct inotify_event_private_data,
193 fsnotify_event_priv_data);
194 inotify_event.wd = priv->wd;
195 inotify_free_event_priv(fsn_priv);
198 name_len = event->name_len;
200 * round up name length so it is a multiple of event_size
201 * plus an extra byte for the terminating '\0'.
203 pad_name_len = round_event_name_len(event);
204 inotify_event.len = pad_name_len;
205 inotify_event.mask = inotify_mask_to_arg(event->mask);
206 inotify_event.cookie = event->sync_cookie;
208 /* send the main event */
209 if (copy_to_user(buf, &inotify_event, event_size))
215 * fsnotify only stores the pathname, so here we have to send the pathname
216 * and then pad that pathname out to a multiple of sizeof(inotify_event)
220 /* copy the path name */
221 if (copy_to_user(buf, event->file_name, name_len))
225 /* fill userspace with 0's */
226 if (clear_user(buf, pad_name_len - name_len))
228 event_size += pad_name_len;
234 static ssize_t inotify_read(struct file *file, char __user *buf,
235 size_t count, loff_t *pos)
237 struct fsnotify_group *group;
238 struct fsnotify_event *kevent;
244 group = file->private_data;
247 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
249 mutex_lock(&group->notification_mutex);
250 kevent = get_one_event(group, count);
251 mutex_unlock(&group->notification_mutex);
253 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
256 ret = PTR_ERR(kevent);
259 ret = copy_event_to_user(group, kevent, buf);
260 fsnotify_put_event(kevent);
269 if (file->f_flags & O_NONBLOCK)
272 if (signal_pending(current))
281 finish_wait(&group->notification_waitq, &wait);
282 if (start != buf && ret != -EFAULT)
287 static int inotify_release(struct inode *ignored, struct file *file)
289 struct fsnotify_group *group = file->private_data;
291 pr_debug("%s: group=%p\n", __func__, group);
293 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
294 fsnotify_destroy_group(group);
299 static long inotify_ioctl(struct file *file, unsigned int cmd,
302 struct fsnotify_group *group;
303 struct fsnotify_event_holder *holder;
304 struct fsnotify_event *event;
309 group = file->private_data;
310 p = (void __user *) arg;
312 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
316 mutex_lock(&group->notification_mutex);
317 list_for_each_entry(holder, &group->notification_list, event_list) {
318 event = holder->event;
319 send_len += sizeof(struct inotify_event);
320 send_len += round_event_name_len(event);
322 mutex_unlock(&group->notification_mutex);
323 ret = put_user(send_len, (int __user *) p);
330 static const struct file_operations inotify_fops = {
331 .show_fdinfo = inotify_show_fdinfo,
332 .poll = inotify_poll,
333 .read = inotify_read,
334 .fasync = fsnotify_fasync,
335 .release = inotify_release,
336 .unlocked_ioctl = inotify_ioctl,
337 .compat_ioctl = inotify_ioctl,
338 .llseek = noop_llseek,
343 * find_inode - resolve a user-given path to a specific inode
345 static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
349 error = user_path_at(AT_FDCWD, dirname, flags, path);
352 /* you can only watch an inode if you have read permissions on it */
353 error = inode_permission(path->dentry->d_inode, MAY_READ);
359 static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
360 struct inotify_inode_mark *i_mark)
364 idr_preload(GFP_KERNEL);
367 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
369 /* we added the mark to the idr, take a reference */
371 fsnotify_get_mark(&i_mark->fsn_mark);
374 spin_unlock(idr_lock);
376 return ret < 0 ? ret : 0;
379 static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
382 struct idr *idr = &group->inotify_data.idr;
383 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
384 struct inotify_inode_mark *i_mark;
386 assert_spin_locked(idr_lock);
388 i_mark = idr_find(idr, wd);
390 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
392 fsnotify_get_mark(fsn_mark);
393 /* One ref for being in the idr, one ref we just took */
394 BUG_ON(atomic_read(&fsn_mark->refcnt) < 2);
400 static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
403 struct inotify_inode_mark *i_mark;
404 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
407 i_mark = inotify_idr_find_locked(group, wd);
408 spin_unlock(idr_lock);
413 static void do_inotify_remove_from_idr(struct fsnotify_group *group,
414 struct inotify_inode_mark *i_mark)
416 struct idr *idr = &group->inotify_data.idr;
417 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
420 assert_spin_locked(idr_lock);
424 /* removed from the idr, drop that ref */
425 fsnotify_put_mark(&i_mark->fsn_mark);
429 * Remove the mark from the idr (if present) and drop the reference
430 * on the mark because it was in the idr.
432 static void inotify_remove_from_idr(struct fsnotify_group *group,
433 struct inotify_inode_mark *i_mark)
435 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
436 struct inotify_inode_mark *found_i_mark = NULL;
443 * does this i_mark think it is in the idr? we shouldn't get called
447 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
448 " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
449 i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
453 /* Lets look in the idr to see if we find it */
454 found_i_mark = inotify_idr_find_locked(group, wd);
455 if (unlikely(!found_i_mark)) {
456 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
457 " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
458 i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
463 * We found an mark in the idr at the right wd, but it's
464 * not the mark we were told to remove. eparis seriously
465 * fucked up somewhere.
467 if (unlikely(found_i_mark != i_mark)) {
468 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
469 "mark->inode=%p found_i_mark=%p found_i_mark->wd=%d "
470 "found_i_mark->group=%p found_i_mark->inode=%p\n",
471 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group,
472 i_mark->fsn_mark.i.inode, found_i_mark, found_i_mark->wd,
473 found_i_mark->fsn_mark.group,
474 found_i_mark->fsn_mark.i.inode);
479 * One ref for being in the idr
480 * one ref held by the caller trying to kill us
481 * one ref grabbed by inotify_idr_find
483 if (unlikely(atomic_read(&i_mark->fsn_mark.refcnt) < 3)) {
484 printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
485 " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
486 i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
487 /* we can't really recover with bad ref cnting.. */
491 do_inotify_remove_from_idr(group, i_mark);
493 /* match the ref taken by inotify_idr_find_locked() */
495 fsnotify_put_mark(&found_i_mark->fsn_mark);
497 spin_unlock(idr_lock);
501 * Send IN_IGNORED for this wd, remove this wd from the idr.
503 void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
504 struct fsnotify_group *group)
506 struct inotify_inode_mark *i_mark;
507 struct fsnotify_event *ignored_event, *notify_event;
508 struct inotify_event_private_data *event_priv;
509 struct fsnotify_event_private_data *fsn_event_priv;
512 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
514 ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL,
515 FSNOTIFY_EVENT_NONE, NULL, 0,
518 goto skip_send_ignore;
520 event_priv = kmem_cache_alloc(event_priv_cachep, GFP_NOFS);
521 if (unlikely(!event_priv))
522 goto skip_send_ignore;
524 fsn_event_priv = &event_priv->fsnotify_event_priv_data;
526 fsnotify_get_group(group);
527 fsn_event_priv->group = group;
528 event_priv->wd = i_mark->wd;
530 notify_event = fsnotify_add_notify_event(group, ignored_event, fsn_event_priv, NULL);
532 if (IS_ERR(notify_event))
533 ret = PTR_ERR(notify_event);
535 fsnotify_put_event(notify_event);
536 inotify_free_event_priv(fsn_event_priv);
540 /* matches the reference taken when the event was created */
542 fsnotify_put_event(ignored_event);
544 /* remove this mark from the idr */
545 inotify_remove_from_idr(group, i_mark);
547 atomic_dec(&group->inotify_data.user->inotify_watches);
550 /* ding dong the mark is dead */
551 static void inotify_free_mark(struct fsnotify_mark *fsn_mark)
553 struct inotify_inode_mark *i_mark;
555 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
557 kmem_cache_free(inotify_inode_mark_cachep, i_mark);
560 static int inotify_update_existing_watch(struct fsnotify_group *group,
564 struct fsnotify_mark *fsn_mark;
565 struct inotify_inode_mark *i_mark;
566 __u32 old_mask, new_mask;
568 int add = (arg & IN_MASK_ADD);
571 mask = inotify_arg_to_mask(arg);
573 fsn_mark = fsnotify_find_inode_mark(group, inode);
577 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
579 spin_lock(&fsn_mark->lock);
581 old_mask = fsn_mark->mask;
583 fsnotify_set_mark_mask_locked(fsn_mark, (fsn_mark->mask | mask));
585 fsnotify_set_mark_mask_locked(fsn_mark, mask);
586 new_mask = fsn_mark->mask;
588 spin_unlock(&fsn_mark->lock);
590 if (old_mask != new_mask) {
591 /* more bits in old than in new? */
592 int dropped = (old_mask & ~new_mask);
593 /* more bits in this fsn_mark than the inode's mask? */
594 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
596 /* update the inode with this new fsn_mark */
597 if (dropped || do_inode)
598 fsnotify_recalc_inode_mask(inode);
605 /* match the get from fsnotify_find_mark() */
606 fsnotify_put_mark(fsn_mark);
611 static int inotify_new_watch(struct fsnotify_group *group,
615 struct inotify_inode_mark *tmp_i_mark;
618 struct idr *idr = &group->inotify_data.idr;
619 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
621 mask = inotify_arg_to_mask(arg);
623 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
624 if (unlikely(!tmp_i_mark))
627 fsnotify_init_mark(&tmp_i_mark->fsn_mark, inotify_free_mark);
628 tmp_i_mark->fsn_mark.mask = mask;
632 if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
635 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
639 /* we are on the idr, now get on the inode */
640 ret = fsnotify_add_mark_locked(&tmp_i_mark->fsn_mark, group, inode,
643 /* we failed to get on the inode, get off the idr */
644 inotify_remove_from_idr(group, tmp_i_mark);
648 /* increment the number of watches the user has */
649 atomic_inc(&group->inotify_data.user->inotify_watches);
651 /* return the watch descriptor for this new mark */
652 ret = tmp_i_mark->wd;
655 /* match the ref from fsnotify_init_mark() */
656 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
661 static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
665 mutex_lock(&group->mark_mutex);
666 /* try to update and existing watch with the new arg */
667 ret = inotify_update_existing_watch(group, inode, arg);
668 /* no mark present, try to add a new one */
670 ret = inotify_new_watch(group, inode, arg);
671 mutex_unlock(&group->mark_mutex);
676 static struct fsnotify_group *inotify_new_group(unsigned int max_events)
678 struct fsnotify_group *group;
680 group = fsnotify_alloc_group(&inotify_fsnotify_ops);
684 group->max_events = max_events;
686 spin_lock_init(&group->inotify_data.idr_lock);
687 idr_init(&group->inotify_data.idr);
688 group->inotify_data.user = get_current_user();
690 if (atomic_inc_return(&group->inotify_data.user->inotify_devs) >
691 inotify_max_user_instances) {
692 fsnotify_destroy_group(group);
693 return ERR_PTR(-EMFILE);
700 /* inotify syscalls */
701 SYSCALL_DEFINE1(inotify_init1, int, flags)
703 struct fsnotify_group *group;
706 /* Check the IN_* constants for consistency. */
707 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
708 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
710 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
713 /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
714 group = inotify_new_group(inotify_max_queued_events);
716 return PTR_ERR(group);
718 ret = anon_inode_getfd("inotify", &inotify_fops, group,
721 fsnotify_destroy_group(group);
726 SYSCALL_DEFINE0(inotify_init)
728 return sys_inotify_init1(0);
731 SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
734 struct fsnotify_group *group;
741 /* don't allow invalid bits: we don't want flags set */
742 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
746 if (unlikely(!f.file))
749 /* verify that this is indeed an inotify instance */
750 if (unlikely(f.file->f_op != &inotify_fops)) {
755 if (!(mask & IN_DONT_FOLLOW))
756 flags |= LOOKUP_FOLLOW;
757 if (mask & IN_ONLYDIR)
758 flags |= LOOKUP_DIRECTORY;
760 ret = inotify_find_inode(pathname, &path, flags);
764 /* inode held in place by reference to path; group by fget on fd */
765 inode = path.dentry->d_inode;
766 group = f.file->private_data;
768 /* create/update an inode mark */
769 ret = inotify_update_watch(group, inode, mask);
776 SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
778 struct fsnotify_group *group;
779 struct inotify_inode_mark *i_mark;
784 if (unlikely(!f.file))
787 /* verify that this is indeed an inotify instance */
789 if (unlikely(f.file->f_op != &inotify_fops))
792 group = f.file->private_data;
795 i_mark = inotify_idr_find(group, wd);
796 if (unlikely(!i_mark))
801 fsnotify_destroy_mark(&i_mark->fsn_mark, group);
803 /* match ref taken by inotify_idr_find */
804 fsnotify_put_mark(&i_mark->fsn_mark);
812 * inotify_user_setup - Our initialization function. Note that we cannot return
813 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
814 * must result in panic().
816 static int __init inotify_user_setup(void)
818 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
819 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
820 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
821 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
822 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
823 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
824 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
825 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
826 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
827 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
828 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
829 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
830 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
831 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
832 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
833 BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
834 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
835 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
837 BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
839 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
840 event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC);
842 inotify_max_queued_events = 16384;
843 inotify_max_user_instances = 128;
844 inotify_max_user_watches = 8192;
848 module_init(inotify_user_setup);