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 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
26 #include <linux/file.h>
27 #include <linux/mount.h>
28 #include <linux/namei.h>
29 #include <linux/poll.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/inotify.h>
33 #include <linux/syscalls.h>
34 #include <linux/magic.h>
36 #include <asm/ioctls.h>
38 static struct kmem_cache
*watch_cachep __read_mostly
;
39 static struct kmem_cache
*event_cachep __read_mostly
;
41 static struct vfsmount
*inotify_mnt __read_mostly
;
43 /* these are configurable via /proc/sys/fs/inotify/ */
44 int inotify_max_user_instances __read_mostly
;
45 int inotify_max_user_watches __read_mostly
;
46 int inotify_max_queued_events __read_mostly
;
51 * inotify_dev->up_mutex (ensures we don't re-add the same watch)
52 * inode->inotify_mutex (protects inode's watch list)
53 * inotify_handle->mutex (protects inotify_handle's watch list)
54 * inotify_dev->ev_mutex (protects device's event queue)
58 * Lifetimes of the main data structures:
60 * inotify_device: Lifetime is managed by reference count, from
61 * sys_inotify_init() until release. Additional references can bump the count
62 * via get_inotify_dev() and drop the count via put_inotify_dev().
64 * inotify_user_watch: Lifetime is from create_watch() to the receipt of an
65 * IN_IGNORED event from inotify, or when using IN_ONESHOT, to receipt of the
66 * first event, or to inotify_destroy().
70 * struct inotify_device - represents an inotify instance
72 * This structure is protected by the mutex 'mutex'.
74 struct inotify_device
{
75 wait_queue_head_t wq
; /* wait queue for i/o */
76 struct mutex ev_mutex
; /* protects event queue */
77 struct mutex up_mutex
; /* synchronizes watch updates */
78 struct list_head events
; /* list of queued events */
79 atomic_t count
; /* reference count */
80 struct user_struct
*user
; /* user who opened this dev */
81 struct inotify_handle
*ih
; /* inotify handle */
82 unsigned int queue_size
; /* size of the queue (bytes) */
83 unsigned int event_count
; /* number of pending events */
84 unsigned int max_events
; /* maximum number of events */
88 * struct inotify_kernel_event - An inotify event, originating from a watch and
89 * queued for user-space. A list of these is attached to each instance of the
90 * device. In read(), this list is walked and all events that can fit in the
91 * buffer are returned.
93 * Protected by dev->ev_mutex of the device in which we are queued.
95 struct inotify_kernel_event
{
96 struct inotify_event event
; /* the user-space event */
97 struct list_head list
; /* entry in inotify_device's list */
98 char *name
; /* filename, if any */
102 * struct inotify_user_watch - our version of an inotify_watch, we add
103 * a reference to the associated inotify_device.
105 struct inotify_user_watch
{
106 struct inotify_device
*dev
; /* associated device */
107 struct inotify_watch wdata
; /* inotify watch data */
112 #include <linux/sysctl.h>
116 ctl_table inotify_table
[] = {
118 .ctl_name
= INOTIFY_MAX_USER_INSTANCES
,
119 .procname
= "max_user_instances",
120 .data
= &inotify_max_user_instances
,
121 .maxlen
= sizeof(int),
123 .proc_handler
= &proc_dointvec_minmax
,
124 .strategy
= &sysctl_intvec
,
128 .ctl_name
= INOTIFY_MAX_USER_WATCHES
,
129 .procname
= "max_user_watches",
130 .data
= &inotify_max_user_watches
,
131 .maxlen
= sizeof(int),
133 .proc_handler
= &proc_dointvec_minmax
,
134 .strategy
= &sysctl_intvec
,
138 .ctl_name
= INOTIFY_MAX_QUEUED_EVENTS
,
139 .procname
= "max_queued_events",
140 .data
= &inotify_max_queued_events
,
141 .maxlen
= sizeof(int),
143 .proc_handler
= &proc_dointvec_minmax
,
144 .strategy
= &sysctl_intvec
,
149 #endif /* CONFIG_SYSCTL */
151 static inline void get_inotify_dev(struct inotify_device
*dev
)
153 atomic_inc(&dev
->count
);
156 static inline void put_inotify_dev(struct inotify_device
*dev
)
158 if (atomic_dec_and_test(&dev
->count
)) {
159 atomic_dec(&dev
->user
->inotify_devs
);
166 * free_inotify_user_watch - cleans up the watch and its references
168 static void free_inotify_user_watch(struct inotify_watch
*w
)
170 struct inotify_user_watch
*watch
;
171 struct inotify_device
*dev
;
173 watch
= container_of(w
, struct inotify_user_watch
, wdata
);
176 atomic_dec(&dev
->user
->inotify_watches
);
177 put_inotify_dev(dev
);
178 kmem_cache_free(watch_cachep
, watch
);
182 * kernel_event - create a new kernel event with the given parameters
184 * This function can sleep.
186 static struct inotify_kernel_event
* kernel_event(s32 wd
, u32 mask
, u32 cookie
,
189 struct inotify_kernel_event
*kevent
;
191 kevent
= kmem_cache_alloc(event_cachep
, GFP_NOFS
);
192 if (unlikely(!kevent
))
195 /* we hand this out to user-space, so zero it just in case */
196 memset(&kevent
->event
, 0, sizeof(struct inotify_event
));
198 kevent
->event
.wd
= wd
;
199 kevent
->event
.mask
= mask
;
200 kevent
->event
.cookie
= cookie
;
202 INIT_LIST_HEAD(&kevent
->list
);
205 size_t len
, rem
, event_size
= sizeof(struct inotify_event
);
208 * We need to pad the filename so as to properly align an
209 * array of inotify_event structures. Because the structure is
210 * small and the common case is a small filename, we just round
211 * up to the next multiple of the structure's sizeof. This is
212 * simple and safe for all architectures.
214 len
= strlen(name
) + 1;
215 rem
= event_size
- len
;
216 if (len
> event_size
) {
217 rem
= event_size
- (len
% event_size
);
218 if (len
% event_size
== 0)
222 kevent
->name
= kmalloc(len
+ rem
, GFP_KERNEL
);
223 if (unlikely(!kevent
->name
)) {
224 kmem_cache_free(event_cachep
, kevent
);
227 memcpy(kevent
->name
, name
, len
);
229 memset(kevent
->name
+ len
, 0, rem
);
230 kevent
->event
.len
= len
+ rem
;
232 kevent
->event
.len
= 0;
240 * inotify_dev_get_event - return the next event in the given dev's queue
242 * Caller must hold dev->ev_mutex.
244 static inline struct inotify_kernel_event
*
245 inotify_dev_get_event(struct inotify_device
*dev
)
247 return list_entry(dev
->events
.next
, struct inotify_kernel_event
, list
);
251 * inotify_dev_get_last_event - return the last event in the given dev's queue
253 * Caller must hold dev->ev_mutex.
255 static inline struct inotify_kernel_event
*
256 inotify_dev_get_last_event(struct inotify_device
*dev
)
258 if (list_empty(&dev
->events
))
260 return list_entry(dev
->events
.prev
, struct inotify_kernel_event
, list
);
264 * inotify_dev_queue_event - event handler registered with core inotify, adds
265 * a new event to the given device
267 * Can sleep (calls kernel_event()).
269 static void inotify_dev_queue_event(struct inotify_watch
*w
, u32 wd
, u32 mask
,
270 u32 cookie
, const char *name
,
271 struct inode
*ignored
)
273 struct inotify_user_watch
*watch
;
274 struct inotify_device
*dev
;
275 struct inotify_kernel_event
*kevent
, *last
;
277 watch
= container_of(w
, struct inotify_user_watch
, wdata
);
280 mutex_lock(&dev
->ev_mutex
);
282 /* we can safely put the watch as we don't reference it while
283 * generating the event
285 if (mask
& IN_IGNORED
|| mask
& IN_ONESHOT
)
286 put_inotify_watch(w
); /* final put */
288 /* coalescing: drop this event if it is a dupe of the previous */
289 last
= inotify_dev_get_last_event(dev
);
290 if (last
&& last
->event
.mask
== mask
&& last
->event
.wd
== wd
&&
291 last
->event
.cookie
== cookie
) {
292 const char *lastname
= last
->name
;
294 if (!name
&& !lastname
)
296 if (name
&& lastname
&& !strcmp(lastname
, name
))
300 /* the queue overflowed and we already sent the Q_OVERFLOW event */
301 if (unlikely(dev
->event_count
> dev
->max_events
))
304 /* if the queue overflows, we need to notify user space */
305 if (unlikely(dev
->event_count
== dev
->max_events
))
306 kevent
= kernel_event(-1, IN_Q_OVERFLOW
, cookie
, NULL
);
308 kevent
= kernel_event(wd
, mask
, cookie
, name
);
310 if (unlikely(!kevent
))
313 /* queue the event and wake up anyone waiting */
315 dev
->queue_size
+= sizeof(struct inotify_event
) + kevent
->event
.len
;
316 list_add_tail(&kevent
->list
, &dev
->events
);
317 wake_up_interruptible(&dev
->wq
);
320 mutex_unlock(&dev
->ev_mutex
);
324 * remove_kevent - cleans up and ultimately frees the given kevent
326 * Caller must hold dev->ev_mutex.
328 static void remove_kevent(struct inotify_device
*dev
,
329 struct inotify_kernel_event
*kevent
)
331 list_del(&kevent
->list
);
334 dev
->queue_size
-= sizeof(struct inotify_event
) + kevent
->event
.len
;
337 kmem_cache_free(event_cachep
, kevent
);
341 * inotify_dev_event_dequeue - destroy an event on the given device
343 * Caller must hold dev->ev_mutex.
345 static void inotify_dev_event_dequeue(struct inotify_device
*dev
)
347 if (!list_empty(&dev
->events
)) {
348 struct inotify_kernel_event
*kevent
;
349 kevent
= inotify_dev_get_event(dev
);
350 remove_kevent(dev
, kevent
);
355 * find_inode - resolve a user-given path to a specific inode and return a nd
357 static int find_inode(const char __user
*dirname
, struct nameidata
*nd
,
362 error
= __user_walk(dirname
, flags
, nd
);
365 /* you can only watch an inode if you have read permissions on it */
366 error
= vfs_permission(nd
, MAY_READ
);
373 * create_watch - creates a watch on the given device.
375 * Callers must hold dev->up_mutex.
377 static int create_watch(struct inotify_device
*dev
, struct inode
*inode
,
380 struct inotify_user_watch
*watch
;
383 if (atomic_read(&dev
->user
->inotify_watches
) >=
384 inotify_max_user_watches
)
387 watch
= kmem_cache_alloc(watch_cachep
, GFP_KERNEL
);
388 if (unlikely(!watch
))
391 /* save a reference to device and bump the count to make it official */
392 get_inotify_dev(dev
);
395 atomic_inc(&dev
->user
->inotify_watches
);
397 inotify_init_watch(&watch
->wdata
);
398 ret
= inotify_add_watch(dev
->ih
, &watch
->wdata
, inode
, mask
);
400 free_inotify_user_watch(&watch
->wdata
);
405 /* Device Interface */
407 static unsigned int inotify_poll(struct file
*file
, poll_table
*wait
)
409 struct inotify_device
*dev
= file
->private_data
;
412 poll_wait(file
, &dev
->wq
, wait
);
413 mutex_lock(&dev
->ev_mutex
);
414 if (!list_empty(&dev
->events
))
415 ret
= POLLIN
| POLLRDNORM
;
416 mutex_unlock(&dev
->ev_mutex
);
421 static ssize_t
inotify_read(struct file
*file
, char __user
*buf
,
422 size_t count
, loff_t
*pos
)
424 size_t event_size
= sizeof (struct inotify_event
);
425 struct inotify_device
*dev
;
431 dev
= file
->private_data
;
436 prepare_to_wait(&dev
->wq
, &wait
, TASK_INTERRUPTIBLE
);
438 mutex_lock(&dev
->ev_mutex
);
439 events
= !list_empty(&dev
->events
);
440 mutex_unlock(&dev
->ev_mutex
);
446 if (file
->f_flags
& O_NONBLOCK
) {
451 if (signal_pending(current
)) {
459 finish_wait(&dev
->wq
, &wait
);
463 mutex_lock(&dev
->ev_mutex
);
465 struct inotify_kernel_event
*kevent
;
468 if (list_empty(&dev
->events
))
471 kevent
= inotify_dev_get_event(dev
);
472 if (event_size
+ kevent
->event
.len
> count
) {
473 if (ret
== 0 && count
> 0) {
475 * could not get a single event because we
476 * didn't have enough buffer space.
483 if (copy_to_user(buf
, &kevent
->event
, event_size
)) {
491 if (copy_to_user(buf
, kevent
->name
, kevent
->event
.len
)){
495 buf
+= kevent
->event
.len
;
496 count
-= kevent
->event
.len
;
499 remove_kevent(dev
, kevent
);
501 mutex_unlock(&dev
->ev_mutex
);
506 static int inotify_release(struct inode
*ignored
, struct file
*file
)
508 struct inotify_device
*dev
= file
->private_data
;
510 inotify_destroy(dev
->ih
);
512 /* destroy all of the events on this device */
513 mutex_lock(&dev
->ev_mutex
);
514 while (!list_empty(&dev
->events
))
515 inotify_dev_event_dequeue(dev
);
516 mutex_unlock(&dev
->ev_mutex
);
518 /* free this device: the put matching the get in inotify_init() */
519 put_inotify_dev(dev
);
524 static long inotify_ioctl(struct file
*file
, unsigned int cmd
,
527 struct inotify_device
*dev
;
531 dev
= file
->private_data
;
532 p
= (void __user
*) arg
;
536 ret
= put_user(dev
->queue_size
, (int __user
*) p
);
543 static const struct file_operations inotify_fops
= {
544 .poll
= inotify_poll
,
545 .read
= inotify_read
,
546 .release
= inotify_release
,
547 .unlocked_ioctl
= inotify_ioctl
,
548 .compat_ioctl
= inotify_ioctl
,
551 static const struct inotify_operations inotify_user_ops
= {
552 .handle_event
= inotify_dev_queue_event
,
553 .destroy_watch
= free_inotify_user_watch
,
556 asmlinkage
long sys_inotify_init(void)
558 struct inotify_device
*dev
;
559 struct inotify_handle
*ih
;
560 struct user_struct
*user
;
564 fd
= get_unused_fd();
568 filp
= get_empty_filp();
574 user
= get_uid(current
->user
);
575 if (unlikely(atomic_read(&user
->inotify_devs
) >=
576 inotify_max_user_instances
)) {
581 dev
= kmalloc(sizeof(struct inotify_device
), GFP_KERNEL
);
582 if (unlikely(!dev
)) {
587 ih
= inotify_init(&inotify_user_ops
);
588 if (unlikely(IS_ERR(ih
))) {
594 filp
->f_op
= &inotify_fops
;
595 filp
->f_path
.mnt
= mntget(inotify_mnt
);
596 filp
->f_path
.dentry
= dget(inotify_mnt
->mnt_root
);
597 filp
->f_mapping
= filp
->f_path
.dentry
->d_inode
->i_mapping
;
598 filp
->f_mode
= FMODE_READ
;
599 filp
->f_flags
= O_RDONLY
;
600 filp
->private_data
= dev
;
602 INIT_LIST_HEAD(&dev
->events
);
603 init_waitqueue_head(&dev
->wq
);
604 mutex_init(&dev
->ev_mutex
);
605 mutex_init(&dev
->up_mutex
);
606 dev
->event_count
= 0;
608 dev
->max_events
= inotify_max_queued_events
;
610 atomic_set(&dev
->count
, 0);
612 get_inotify_dev(dev
);
613 atomic_inc(&user
->inotify_devs
);
614 fd_install(fd
, filp
);
627 asmlinkage
long sys_inotify_add_watch(int fd
, const char __user
*path
, u32 mask
)
630 struct inotify_device
*dev
;
633 int ret
, fput_needed
;
636 filp
= fget_light(fd
, &fput_needed
);
640 /* verify that this is indeed an inotify instance */
641 if (unlikely(filp
->f_op
!= &inotify_fops
)) {
646 if (!(mask
& IN_DONT_FOLLOW
))
647 flags
|= LOOKUP_FOLLOW
;
648 if (mask
& IN_ONLYDIR
)
649 flags
|= LOOKUP_DIRECTORY
;
651 ret
= find_inode(path
, &nd
, flags
);
655 /* inode held in place by reference to nd; dev by fget on fd */
656 inode
= nd
.dentry
->d_inode
;
657 dev
= filp
->private_data
;
659 mutex_lock(&dev
->up_mutex
);
660 ret
= inotify_find_update_watch(dev
->ih
, inode
, mask
);
662 ret
= create_watch(dev
, inode
, mask
);
663 mutex_unlock(&dev
->up_mutex
);
667 fput_light(filp
, fput_needed
);
671 asmlinkage
long sys_inotify_rm_watch(int fd
, u32 wd
)
674 struct inotify_device
*dev
;
675 int ret
, fput_needed
;
677 filp
= fget_light(fd
, &fput_needed
);
681 /* verify that this is indeed an inotify instance */
682 if (unlikely(filp
->f_op
!= &inotify_fops
)) {
687 dev
= filp
->private_data
;
689 /* we free our watch data when we get IN_IGNORED */
690 ret
= inotify_rm_wd(dev
->ih
, wd
);
693 fput_light(filp
, fput_needed
);
698 inotify_get_sb(struct file_system_type
*fs_type
, int flags
,
699 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
701 return get_sb_pseudo(fs_type
, "inotify", NULL
,
702 INOTIFYFS_SUPER_MAGIC
, mnt
);
705 static struct file_system_type inotify_fs_type
= {
707 .get_sb
= inotify_get_sb
,
708 .kill_sb
= kill_anon_super
,
712 * inotify_user_setup - Our initialization function. Note that we cannnot return
713 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
714 * must result in panic().
716 static int __init
inotify_user_setup(void)
720 ret
= register_filesystem(&inotify_fs_type
);
722 panic("inotify: register_filesystem returned %d!\n", ret
);
724 inotify_mnt
= kern_mount(&inotify_fs_type
);
725 if (IS_ERR(inotify_mnt
))
726 panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt
));
728 inotify_max_queued_events
= 16384;
729 inotify_max_user_instances
= 128;
730 inotify_max_user_watches
= 8192;
732 watch_cachep
= kmem_cache_create("inotify_watch_cache",
733 sizeof(struct inotify_user_watch
),
734 0, SLAB_PANIC
, NULL
);
735 event_cachep
= kmem_cache_create("inotify_event_cache",
736 sizeof(struct inotify_kernel_event
),
737 0, SLAB_PANIC
, NULL
);
742 module_init(inotify_user_setup
);