CONFIG_SYSFS_DEPRECATED
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / inotify_user.c
blob017cb0f134d6aa600f5c37296b6c786bacb6cf7c
1 /*
2 * fs/inotify_user.c - inotify support for userspace
4 * Authors:
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
14 * later version.
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>
25 #include <linux/fs.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>
35 #include <asm/ioctls.h>
37 static kmem_cache_t *watch_cachep __read_mostly;
38 static kmem_cache_t *event_cachep __read_mostly;
40 static struct vfsmount *inotify_mnt __read_mostly;
42 /* these are configurable via /proc/sys/fs/inotify/ */
43 int inotify_max_user_instances __read_mostly;
44 int inotify_max_user_watches __read_mostly;
45 int inotify_max_queued_events __read_mostly;
48 * Lock ordering:
50 * inotify_dev->up_mutex (ensures we don't re-add the same watch)
51 * inode->inotify_mutex (protects inode's watch list)
52 * inotify_handle->mutex (protects inotify_handle's watch list)
53 * inotify_dev->ev_mutex (protects device's event queue)
57 * Lifetimes of the main data structures:
59 * inotify_device: Lifetime is managed by reference count, from
60 * sys_inotify_init() until release. Additional references can bump the count
61 * via get_inotify_dev() and drop the count via put_inotify_dev().
63 * inotify_user_watch: Lifetime is from create_watch() to the receipt of an
64 * IN_IGNORED event from inotify, or when using IN_ONESHOT, to receipt of the
65 * first event, or to inotify_destroy().
69 * struct inotify_device - represents an inotify instance
71 * This structure is protected by the mutex 'mutex'.
73 struct inotify_device {
74 wait_queue_head_t wq; /* wait queue for i/o */
75 struct mutex ev_mutex; /* protects event queue */
76 struct mutex up_mutex; /* synchronizes watch updates */
77 struct list_head events; /* list of queued events */
78 atomic_t count; /* reference count */
79 struct user_struct *user; /* user who opened this dev */
80 struct inotify_handle *ih; /* inotify handle */
81 unsigned int queue_size; /* size of the queue (bytes) */
82 unsigned int event_count; /* number of pending events */
83 unsigned int max_events; /* maximum number of events */
87 * struct inotify_kernel_event - An inotify event, originating from a watch and
88 * queued for user-space. A list of these is attached to each instance of the
89 * device. In read(), this list is walked and all events that can fit in the
90 * buffer are returned.
92 * Protected by dev->ev_mutex of the device in which we are queued.
94 struct inotify_kernel_event {
95 struct inotify_event event; /* the user-space event */
96 struct list_head list; /* entry in inotify_device's list */
97 char *name; /* filename, if any */
101 * struct inotify_user_watch - our version of an inotify_watch, we add
102 * a reference to the associated inotify_device.
104 struct inotify_user_watch {
105 struct inotify_device *dev; /* associated device */
106 struct inotify_watch wdata; /* inotify watch data */
109 #ifdef CONFIG_SYSCTL
111 #include <linux/sysctl.h>
113 static int zero;
115 ctl_table inotify_table[] = {
117 .ctl_name = INOTIFY_MAX_USER_INSTANCES,
118 .procname = "max_user_instances",
119 .data = &inotify_max_user_instances,
120 .maxlen = sizeof(int),
121 .mode = 0644,
122 .proc_handler = &proc_dointvec_minmax,
123 .strategy = &sysctl_intvec,
124 .extra1 = &zero,
127 .ctl_name = INOTIFY_MAX_USER_WATCHES,
128 .procname = "max_user_watches",
129 .data = &inotify_max_user_watches,
130 .maxlen = sizeof(int),
131 .mode = 0644,
132 .proc_handler = &proc_dointvec_minmax,
133 .strategy = &sysctl_intvec,
134 .extra1 = &zero,
137 .ctl_name = INOTIFY_MAX_QUEUED_EVENTS,
138 .procname = "max_queued_events",
139 .data = &inotify_max_queued_events,
140 .maxlen = sizeof(int),
141 .mode = 0644,
142 .proc_handler = &proc_dointvec_minmax,
143 .strategy = &sysctl_intvec,
144 .extra1 = &zero
146 { .ctl_name = 0 }
148 #endif /* CONFIG_SYSCTL */
150 static inline void get_inotify_dev(struct inotify_device *dev)
152 atomic_inc(&dev->count);
155 static inline void put_inotify_dev(struct inotify_device *dev)
157 if (atomic_dec_and_test(&dev->count)) {
158 atomic_dec(&dev->user->inotify_devs);
159 free_uid(dev->user);
160 kfree(dev);
165 * free_inotify_user_watch - cleans up the watch and its references
167 static void free_inotify_user_watch(struct inotify_watch *w)
169 struct inotify_user_watch *watch;
170 struct inotify_device *dev;
172 watch = container_of(w, struct inotify_user_watch, wdata);
173 dev = watch->dev;
175 atomic_dec(&dev->user->inotify_watches);
176 put_inotify_dev(dev);
177 kmem_cache_free(watch_cachep, watch);
181 * kernel_event - create a new kernel event with the given parameters
183 * This function can sleep.
185 static struct inotify_kernel_event * kernel_event(s32 wd, u32 mask, u32 cookie,
186 const char *name)
188 struct inotify_kernel_event *kevent;
190 kevent = kmem_cache_alloc(event_cachep, GFP_NOFS);
191 if (unlikely(!kevent))
192 return NULL;
194 /* we hand this out to user-space, so zero it just in case */
195 memset(&kevent->event, 0, sizeof(struct inotify_event));
197 kevent->event.wd = wd;
198 kevent->event.mask = mask;
199 kevent->event.cookie = cookie;
201 INIT_LIST_HEAD(&kevent->list);
203 if (name) {
204 size_t len, rem, event_size = sizeof(struct inotify_event);
207 * We need to pad the filename so as to properly align an
208 * array of inotify_event structures. Because the structure is
209 * small and the common case is a small filename, we just round
210 * up to the next multiple of the structure's sizeof. This is
211 * simple and safe for all architectures.
213 len = strlen(name) + 1;
214 rem = event_size - len;
215 if (len > event_size) {
216 rem = event_size - (len % event_size);
217 if (len % event_size == 0)
218 rem = 0;
221 kevent->name = kmalloc(len + rem, GFP_KERNEL);
222 if (unlikely(!kevent->name)) {
223 kmem_cache_free(event_cachep, kevent);
224 return NULL;
226 memcpy(kevent->name, name, len);
227 if (rem)
228 memset(kevent->name + len, 0, rem);
229 kevent->event.len = len + rem;
230 } else {
231 kevent->event.len = 0;
232 kevent->name = NULL;
235 return kevent;
239 * inotify_dev_get_event - return the next event in the given dev's queue
241 * Caller must hold dev->ev_mutex.
243 static inline struct inotify_kernel_event *
244 inotify_dev_get_event(struct inotify_device *dev)
246 return list_entry(dev->events.next, struct inotify_kernel_event, list);
250 * inotify_dev_queue_event - event handler registered with core inotify, adds
251 * a new event to the given device
253 * Can sleep (calls kernel_event()).
255 static void inotify_dev_queue_event(struct inotify_watch *w, u32 wd, u32 mask,
256 u32 cookie, const char *name,
257 struct inode *ignored)
259 struct inotify_user_watch *watch;
260 struct inotify_device *dev;
261 struct inotify_kernel_event *kevent, *last;
263 watch = container_of(w, struct inotify_user_watch, wdata);
264 dev = watch->dev;
266 mutex_lock(&dev->ev_mutex);
268 /* we can safely put the watch as we don't reference it while
269 * generating the event
271 if (mask & IN_IGNORED || mask & IN_ONESHOT)
272 put_inotify_watch(w); /* final put */
274 /* coalescing: drop this event if it is a dupe of the previous */
275 last = inotify_dev_get_event(dev);
276 if (last && last->event.mask == mask && last->event.wd == wd &&
277 last->event.cookie == cookie) {
278 const char *lastname = last->name;
280 if (!name && !lastname)
281 goto out;
282 if (name && lastname && !strcmp(lastname, name))
283 goto out;
286 /* the queue overflowed and we already sent the Q_OVERFLOW event */
287 if (unlikely(dev->event_count > dev->max_events))
288 goto out;
290 /* if the queue overflows, we need to notify user space */
291 if (unlikely(dev->event_count == dev->max_events))
292 kevent = kernel_event(-1, IN_Q_OVERFLOW, cookie, NULL);
293 else
294 kevent = kernel_event(wd, mask, cookie, name);
296 if (unlikely(!kevent))
297 goto out;
299 /* queue the event and wake up anyone waiting */
300 dev->event_count++;
301 dev->queue_size += sizeof(struct inotify_event) + kevent->event.len;
302 list_add_tail(&kevent->list, &dev->events);
303 wake_up_interruptible(&dev->wq);
305 out:
306 mutex_unlock(&dev->ev_mutex);
310 * remove_kevent - cleans up and ultimately frees the given kevent
312 * Caller must hold dev->ev_mutex.
314 static void remove_kevent(struct inotify_device *dev,
315 struct inotify_kernel_event *kevent)
317 list_del(&kevent->list);
319 dev->event_count--;
320 dev->queue_size -= sizeof(struct inotify_event) + kevent->event.len;
322 kfree(kevent->name);
323 kmem_cache_free(event_cachep, kevent);
327 * inotify_dev_event_dequeue - destroy an event on the given device
329 * Caller must hold dev->ev_mutex.
331 static void inotify_dev_event_dequeue(struct inotify_device *dev)
333 if (!list_empty(&dev->events)) {
334 struct inotify_kernel_event *kevent;
335 kevent = inotify_dev_get_event(dev);
336 remove_kevent(dev, kevent);
341 * find_inode - resolve a user-given path to a specific inode and return a nd
343 static int find_inode(const char __user *dirname, struct nameidata *nd,
344 unsigned flags)
346 int error;
348 error = __user_walk(dirname, flags, nd);
349 if (error)
350 return error;
351 /* you can only watch an inode if you have read permissions on it */
352 error = vfs_permission(nd, MAY_READ);
353 if (error)
354 path_release(nd);
355 return error;
359 * create_watch - creates a watch on the given device.
361 * Callers must hold dev->up_mutex.
363 static int create_watch(struct inotify_device *dev, struct inode *inode,
364 u32 mask)
366 struct inotify_user_watch *watch;
367 int ret;
369 if (atomic_read(&dev->user->inotify_watches) >=
370 inotify_max_user_watches)
371 return -ENOSPC;
373 watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL);
374 if (unlikely(!watch))
375 return -ENOMEM;
377 /* save a reference to device and bump the count to make it official */
378 get_inotify_dev(dev);
379 watch->dev = dev;
381 atomic_inc(&dev->user->inotify_watches);
383 inotify_init_watch(&watch->wdata);
384 ret = inotify_add_watch(dev->ih, &watch->wdata, inode, mask);
385 if (ret < 0)
386 free_inotify_user_watch(&watch->wdata);
388 return ret;
391 /* Device Interface */
393 static unsigned int inotify_poll(struct file *file, poll_table *wait)
395 struct inotify_device *dev = file->private_data;
396 int ret = 0;
398 poll_wait(file, &dev->wq, wait);
399 mutex_lock(&dev->ev_mutex);
400 if (!list_empty(&dev->events))
401 ret = POLLIN | POLLRDNORM;
402 mutex_unlock(&dev->ev_mutex);
404 return ret;
407 static ssize_t inotify_read(struct file *file, char __user *buf,
408 size_t count, loff_t *pos)
410 size_t event_size = sizeof (struct inotify_event);
411 struct inotify_device *dev;
412 char __user *start;
413 int ret;
414 DEFINE_WAIT(wait);
416 start = buf;
417 dev = file->private_data;
419 while (1) {
420 int events;
422 prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE);
424 mutex_lock(&dev->ev_mutex);
425 events = !list_empty(&dev->events);
426 mutex_unlock(&dev->ev_mutex);
427 if (events) {
428 ret = 0;
429 break;
432 if (file->f_flags & O_NONBLOCK) {
433 ret = -EAGAIN;
434 break;
437 if (signal_pending(current)) {
438 ret = -EINTR;
439 break;
442 schedule();
445 finish_wait(&dev->wq, &wait);
446 if (ret)
447 return ret;
449 mutex_lock(&dev->ev_mutex);
450 while (1) {
451 struct inotify_kernel_event *kevent;
453 ret = buf - start;
454 if (list_empty(&dev->events))
455 break;
457 kevent = inotify_dev_get_event(dev);
458 if (event_size + kevent->event.len > count)
459 break;
461 if (copy_to_user(buf, &kevent->event, event_size)) {
462 ret = -EFAULT;
463 break;
465 buf += event_size;
466 count -= event_size;
468 if (kevent->name) {
469 if (copy_to_user(buf, kevent->name, kevent->event.len)){
470 ret = -EFAULT;
471 break;
473 buf += kevent->event.len;
474 count -= kevent->event.len;
477 remove_kevent(dev, kevent);
479 mutex_unlock(&dev->ev_mutex);
481 return ret;
484 static int inotify_release(struct inode *ignored, struct file *file)
486 struct inotify_device *dev = file->private_data;
488 inotify_destroy(dev->ih);
490 /* destroy all of the events on this device */
491 mutex_lock(&dev->ev_mutex);
492 while (!list_empty(&dev->events))
493 inotify_dev_event_dequeue(dev);
494 mutex_unlock(&dev->ev_mutex);
496 /* free this device: the put matching the get in inotify_init() */
497 put_inotify_dev(dev);
499 return 0;
502 static long inotify_ioctl(struct file *file, unsigned int cmd,
503 unsigned long arg)
505 struct inotify_device *dev;
506 void __user *p;
507 int ret = -ENOTTY;
509 dev = file->private_data;
510 p = (void __user *) arg;
512 switch (cmd) {
513 case FIONREAD:
514 ret = put_user(dev->queue_size, (int __user *) p);
515 break;
518 return ret;
521 static const struct file_operations inotify_fops = {
522 .poll = inotify_poll,
523 .read = inotify_read,
524 .release = inotify_release,
525 .unlocked_ioctl = inotify_ioctl,
526 .compat_ioctl = inotify_ioctl,
529 static const struct inotify_operations inotify_user_ops = {
530 .handle_event = inotify_dev_queue_event,
531 .destroy_watch = free_inotify_user_watch,
534 asmlinkage long sys_inotify_init(void)
536 struct inotify_device *dev;
537 struct inotify_handle *ih;
538 struct user_struct *user;
539 struct file *filp;
540 int fd, ret;
542 fd = get_unused_fd();
543 if (fd < 0)
544 return fd;
546 filp = get_empty_filp();
547 if (!filp) {
548 ret = -ENFILE;
549 goto out_put_fd;
552 user = get_uid(current->user);
553 if (unlikely(atomic_read(&user->inotify_devs) >=
554 inotify_max_user_instances)) {
555 ret = -EMFILE;
556 goto out_free_uid;
559 dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL);
560 if (unlikely(!dev)) {
561 ret = -ENOMEM;
562 goto out_free_uid;
565 ih = inotify_init(&inotify_user_ops);
566 if (unlikely(IS_ERR(ih))) {
567 ret = PTR_ERR(ih);
568 goto out_free_dev;
570 dev->ih = ih;
572 filp->f_op = &inotify_fops;
573 filp->f_vfsmnt = mntget(inotify_mnt);
574 filp->f_dentry = dget(inotify_mnt->mnt_root);
575 filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
576 filp->f_mode = FMODE_READ;
577 filp->f_flags = O_RDONLY;
578 filp->private_data = dev;
580 INIT_LIST_HEAD(&dev->events);
581 init_waitqueue_head(&dev->wq);
582 mutex_init(&dev->ev_mutex);
583 mutex_init(&dev->up_mutex);
584 dev->event_count = 0;
585 dev->queue_size = 0;
586 dev->max_events = inotify_max_queued_events;
587 dev->user = user;
588 atomic_set(&dev->count, 0);
590 get_inotify_dev(dev);
591 atomic_inc(&user->inotify_devs);
592 fd_install(fd, filp);
594 return fd;
595 out_free_dev:
596 kfree(dev);
597 out_free_uid:
598 free_uid(user);
599 put_filp(filp);
600 out_put_fd:
601 put_unused_fd(fd);
602 return ret;
605 asmlinkage long sys_inotify_add_watch(int fd, const char __user *path, u32 mask)
607 struct inode *inode;
608 struct inotify_device *dev;
609 struct nameidata nd;
610 struct file *filp;
611 int ret, fput_needed;
612 unsigned flags = 0;
614 filp = fget_light(fd, &fput_needed);
615 if (unlikely(!filp))
616 return -EBADF;
618 /* verify that this is indeed an inotify instance */
619 if (unlikely(filp->f_op != &inotify_fops)) {
620 ret = -EINVAL;
621 goto fput_and_out;
624 if (!(mask & IN_DONT_FOLLOW))
625 flags |= LOOKUP_FOLLOW;
626 if (mask & IN_ONLYDIR)
627 flags |= LOOKUP_DIRECTORY;
629 ret = find_inode(path, &nd, flags);
630 if (unlikely(ret))
631 goto fput_and_out;
633 /* inode held in place by reference to nd; dev by fget on fd */
634 inode = nd.dentry->d_inode;
635 dev = filp->private_data;
637 mutex_lock(&dev->up_mutex);
638 ret = inotify_find_update_watch(dev->ih, inode, mask);
639 if (ret == -ENOENT)
640 ret = create_watch(dev, inode, mask);
641 mutex_unlock(&dev->up_mutex);
643 path_release(&nd);
644 fput_and_out:
645 fput_light(filp, fput_needed);
646 return ret;
649 asmlinkage long sys_inotify_rm_watch(int fd, u32 wd)
651 struct file *filp;
652 struct inotify_device *dev;
653 int ret, fput_needed;
655 filp = fget_light(fd, &fput_needed);
656 if (unlikely(!filp))
657 return -EBADF;
659 /* verify that this is indeed an inotify instance */
660 if (unlikely(filp->f_op != &inotify_fops)) {
661 ret = -EINVAL;
662 goto out;
665 dev = filp->private_data;
667 /* we free our watch data when we get IN_IGNORED */
668 ret = inotify_rm_wd(dev->ih, wd);
670 out:
671 fput_light(filp, fput_needed);
672 return ret;
675 static int
676 inotify_get_sb(struct file_system_type *fs_type, int flags,
677 const char *dev_name, void *data, struct vfsmount *mnt)
679 return get_sb_pseudo(fs_type, "inotify", NULL, 0xBAD1DEA, mnt);
682 static struct file_system_type inotify_fs_type = {
683 .name = "inotifyfs",
684 .get_sb = inotify_get_sb,
685 .kill_sb = kill_anon_super,
689 * inotify_user_setup - Our initialization function. Note that we cannnot return
690 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
691 * must result in panic().
693 static int __init inotify_user_setup(void)
695 int ret;
697 ret = register_filesystem(&inotify_fs_type);
698 if (unlikely(ret))
699 panic("inotify: register_filesystem returned %d!\n", ret);
701 inotify_mnt = kern_mount(&inotify_fs_type);
702 if (IS_ERR(inotify_mnt))
703 panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt));
705 inotify_max_queued_events = 16384;
706 inotify_max_user_instances = 128;
707 inotify_max_user_watches = 8192;
709 watch_cachep = kmem_cache_create("inotify_watch_cache",
710 sizeof(struct inotify_user_watch),
711 0, SLAB_PANIC, NULL, NULL);
712 event_cachep = kmem_cache_create("inotify_event_cache",
713 sizeof(struct inotify_kernel_event),
714 0, SLAB_PANIC, NULL, NULL);
716 return 0;
719 module_init(inotify_user_setup);