ecryptfs: propagate key errors up at mount time
[linux-2.6/mini2440.git] / fs / inotify_user.c
blobfe79c25d95dc9c630649aec8485b784bbb34d49c
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>
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 static int inotify_max_user_instances __read_mostly;
45 static int inotify_max_user_watches __read_mostly;
46 static int inotify_max_queued_events __read_mostly;
49 * Lock ordering:
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 struct fasync_struct *fa; /* async notification */
83 unsigned int queue_size; /* size of the queue (bytes) */
84 unsigned int event_count; /* number of pending events */
85 unsigned int max_events; /* maximum number of events */
89 * struct inotify_kernel_event - An inotify event, originating from a watch and
90 * queued for user-space. A list of these is attached to each instance of the
91 * device. In read(), this list is walked and all events that can fit in the
92 * buffer are returned.
94 * Protected by dev->ev_mutex of the device in which we are queued.
96 struct inotify_kernel_event {
97 struct inotify_event event; /* the user-space event */
98 struct list_head list; /* entry in inotify_device's list */
99 char *name; /* filename, if any */
103 * struct inotify_user_watch - our version of an inotify_watch, we add
104 * a reference to the associated inotify_device.
106 struct inotify_user_watch {
107 struct inotify_device *dev; /* associated device */
108 struct inotify_watch wdata; /* inotify watch data */
111 #ifdef CONFIG_SYSCTL
113 #include <linux/sysctl.h>
115 static int zero;
117 ctl_table inotify_table[] = {
119 .ctl_name = INOTIFY_MAX_USER_INSTANCES,
120 .procname = "max_user_instances",
121 .data = &inotify_max_user_instances,
122 .maxlen = sizeof(int),
123 .mode = 0644,
124 .proc_handler = &proc_dointvec_minmax,
125 .strategy = &sysctl_intvec,
126 .extra1 = &zero,
129 .ctl_name = INOTIFY_MAX_USER_WATCHES,
130 .procname = "max_user_watches",
131 .data = &inotify_max_user_watches,
132 .maxlen = sizeof(int),
133 .mode = 0644,
134 .proc_handler = &proc_dointvec_minmax,
135 .strategy = &sysctl_intvec,
136 .extra1 = &zero,
139 .ctl_name = INOTIFY_MAX_QUEUED_EVENTS,
140 .procname = "max_queued_events",
141 .data = &inotify_max_queued_events,
142 .maxlen = sizeof(int),
143 .mode = 0644,
144 .proc_handler = &proc_dointvec_minmax,
145 .strategy = &sysctl_intvec,
146 .extra1 = &zero
148 { .ctl_name = 0 }
150 #endif /* CONFIG_SYSCTL */
152 static inline void get_inotify_dev(struct inotify_device *dev)
154 atomic_inc(&dev->count);
157 static inline void put_inotify_dev(struct inotify_device *dev)
159 if (atomic_dec_and_test(&dev->count)) {
160 atomic_dec(&dev->user->inotify_devs);
161 free_uid(dev->user);
162 kfree(dev);
167 * free_inotify_user_watch - cleans up the watch and its references
169 static void free_inotify_user_watch(struct inotify_watch *w)
171 struct inotify_user_watch *watch;
172 struct inotify_device *dev;
174 watch = container_of(w, struct inotify_user_watch, wdata);
175 dev = watch->dev;
177 atomic_dec(&dev->user->inotify_watches);
178 put_inotify_dev(dev);
179 kmem_cache_free(watch_cachep, watch);
183 * kernel_event - create a new kernel event with the given parameters
185 * This function can sleep.
187 static struct inotify_kernel_event * kernel_event(s32 wd, u32 mask, u32 cookie,
188 const char *name)
190 struct inotify_kernel_event *kevent;
192 kevent = kmem_cache_alloc(event_cachep, GFP_NOFS);
193 if (unlikely(!kevent))
194 return NULL;
196 /* we hand this out to user-space, so zero it just in case */
197 memset(&kevent->event, 0, sizeof(struct inotify_event));
199 kevent->event.wd = wd;
200 kevent->event.mask = mask;
201 kevent->event.cookie = cookie;
203 INIT_LIST_HEAD(&kevent->list);
205 if (name) {
206 size_t len, rem, event_size = sizeof(struct inotify_event);
209 * We need to pad the filename so as to properly align an
210 * array of inotify_event structures. Because the structure is
211 * small and the common case is a small filename, we just round
212 * up to the next multiple of the structure's sizeof. This is
213 * simple and safe for all architectures.
215 len = strlen(name) + 1;
216 rem = event_size - len;
217 if (len > event_size) {
218 rem = event_size - (len % event_size);
219 if (len % event_size == 0)
220 rem = 0;
223 kevent->name = kmalloc(len + rem, GFP_KERNEL);
224 if (unlikely(!kevent->name)) {
225 kmem_cache_free(event_cachep, kevent);
226 return NULL;
228 memcpy(kevent->name, name, len);
229 if (rem)
230 memset(kevent->name + len, 0, rem);
231 kevent->event.len = len + rem;
232 } else {
233 kevent->event.len = 0;
234 kevent->name = NULL;
237 return kevent;
241 * inotify_dev_get_event - return the next event in the given dev's queue
243 * Caller must hold dev->ev_mutex.
245 static inline struct inotify_kernel_event *
246 inotify_dev_get_event(struct inotify_device *dev)
248 return list_entry(dev->events.next, struct inotify_kernel_event, list);
252 * inotify_dev_get_last_event - return the last event in the given dev's queue
254 * Caller must hold dev->ev_mutex.
256 static inline struct inotify_kernel_event *
257 inotify_dev_get_last_event(struct inotify_device *dev)
259 if (list_empty(&dev->events))
260 return NULL;
261 return list_entry(dev->events.prev, struct inotify_kernel_event, list);
265 * inotify_dev_queue_event - event handler registered with core inotify, adds
266 * a new event to the given device
268 * Can sleep (calls kernel_event()).
270 static void inotify_dev_queue_event(struct inotify_watch *w, u32 wd, u32 mask,
271 u32 cookie, const char *name,
272 struct inode *ignored)
274 struct inotify_user_watch *watch;
275 struct inotify_device *dev;
276 struct inotify_kernel_event *kevent, *last;
278 watch = container_of(w, struct inotify_user_watch, wdata);
279 dev = watch->dev;
281 mutex_lock(&dev->ev_mutex);
283 /* we can safely put the watch as we don't reference it while
284 * generating the event
286 if (mask & IN_IGNORED || w->mask & IN_ONESHOT)
287 put_inotify_watch(w); /* final put */
289 /* coalescing: drop this event if it is a dupe of the previous */
290 last = inotify_dev_get_last_event(dev);
291 if (last && last->event.mask == mask && last->event.wd == wd &&
292 last->event.cookie == cookie) {
293 const char *lastname = last->name;
295 if (!name && !lastname)
296 goto out;
297 if (name && lastname && !strcmp(lastname, name))
298 goto out;
301 /* the queue overflowed and we already sent the Q_OVERFLOW event */
302 if (unlikely(dev->event_count > dev->max_events))
303 goto out;
305 /* if the queue overflows, we need to notify user space */
306 if (unlikely(dev->event_count == dev->max_events))
307 kevent = kernel_event(-1, IN_Q_OVERFLOW, cookie, NULL);
308 else
309 kevent = kernel_event(wd, mask, cookie, name);
311 if (unlikely(!kevent))
312 goto out;
314 /* queue the event and wake up anyone waiting */
315 dev->event_count++;
316 dev->queue_size += sizeof(struct inotify_event) + kevent->event.len;
317 list_add_tail(&kevent->list, &dev->events);
318 wake_up_interruptible(&dev->wq);
319 kill_fasync(&dev->fa, SIGIO, POLL_IN);
321 out:
322 mutex_unlock(&dev->ev_mutex);
326 * remove_kevent - cleans up and ultimately frees the given kevent
328 * Caller must hold dev->ev_mutex.
330 static void remove_kevent(struct inotify_device *dev,
331 struct inotify_kernel_event *kevent)
333 list_del(&kevent->list);
335 dev->event_count--;
336 dev->queue_size -= sizeof(struct inotify_event) + kevent->event.len;
338 kfree(kevent->name);
339 kmem_cache_free(event_cachep, kevent);
343 * inotify_dev_event_dequeue - destroy an event on the given device
345 * Caller must hold dev->ev_mutex.
347 static void inotify_dev_event_dequeue(struct inotify_device *dev)
349 if (!list_empty(&dev->events)) {
350 struct inotify_kernel_event *kevent;
351 kevent = inotify_dev_get_event(dev);
352 remove_kevent(dev, kevent);
357 * find_inode - resolve a user-given path to a specific inode and return a nd
359 static int find_inode(const char __user *dirname, struct nameidata *nd,
360 unsigned flags)
362 int error;
364 error = __user_walk(dirname, flags, nd);
365 if (error)
366 return error;
367 /* you can only watch an inode if you have read permissions on it */
368 error = vfs_permission(nd, MAY_READ);
369 if (error)
370 path_put(&nd->path);
371 return error;
375 * create_watch - creates a watch on the given device.
377 * Callers must hold dev->up_mutex.
379 static int create_watch(struct inotify_device *dev, struct inode *inode,
380 u32 mask)
382 struct inotify_user_watch *watch;
383 int ret;
385 if (atomic_read(&dev->user->inotify_watches) >=
386 inotify_max_user_watches)
387 return -ENOSPC;
389 watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL);
390 if (unlikely(!watch))
391 return -ENOMEM;
393 /* save a reference to device and bump the count to make it official */
394 get_inotify_dev(dev);
395 watch->dev = dev;
397 atomic_inc(&dev->user->inotify_watches);
399 inotify_init_watch(&watch->wdata);
400 ret = inotify_add_watch(dev->ih, &watch->wdata, inode, mask);
401 if (ret < 0)
402 free_inotify_user_watch(&watch->wdata);
404 return ret;
407 /* Device Interface */
409 static unsigned int inotify_poll(struct file *file, poll_table *wait)
411 struct inotify_device *dev = file->private_data;
412 int ret = 0;
414 poll_wait(file, &dev->wq, wait);
415 mutex_lock(&dev->ev_mutex);
416 if (!list_empty(&dev->events))
417 ret = POLLIN | POLLRDNORM;
418 mutex_unlock(&dev->ev_mutex);
420 return ret;
423 static ssize_t inotify_read(struct file *file, char __user *buf,
424 size_t count, loff_t *pos)
426 size_t event_size = sizeof (struct inotify_event);
427 struct inotify_device *dev;
428 char __user *start;
429 int ret;
430 DEFINE_WAIT(wait);
432 start = buf;
433 dev = file->private_data;
435 while (1) {
436 int events;
438 prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE);
440 mutex_lock(&dev->ev_mutex);
441 events = !list_empty(&dev->events);
442 mutex_unlock(&dev->ev_mutex);
443 if (events) {
444 ret = 0;
445 break;
448 if (file->f_flags & O_NONBLOCK) {
449 ret = -EAGAIN;
450 break;
453 if (signal_pending(current)) {
454 ret = -EINTR;
455 break;
458 schedule();
461 finish_wait(&dev->wq, &wait);
462 if (ret)
463 return ret;
465 mutex_lock(&dev->ev_mutex);
466 while (1) {
467 struct inotify_kernel_event *kevent;
469 ret = buf - start;
470 if (list_empty(&dev->events))
471 break;
473 kevent = inotify_dev_get_event(dev);
474 if (event_size + kevent->event.len > count) {
475 if (ret == 0 && count > 0) {
477 * could not get a single event because we
478 * didn't have enough buffer space.
480 ret = -EINVAL;
482 break;
485 if (copy_to_user(buf, &kevent->event, event_size)) {
486 ret = -EFAULT;
487 break;
489 buf += event_size;
490 count -= event_size;
492 if (kevent->name) {
493 if (copy_to_user(buf, kevent->name, kevent->event.len)){
494 ret = -EFAULT;
495 break;
497 buf += kevent->event.len;
498 count -= kevent->event.len;
501 remove_kevent(dev, kevent);
503 mutex_unlock(&dev->ev_mutex);
505 return ret;
508 static int inotify_fasync(int fd, struct file *file, int on)
510 struct inotify_device *dev = file->private_data;
512 return fasync_helper(fd, file, on, &dev->fa) >= 0 ? 0 : -EIO;
515 static int inotify_release(struct inode *ignored, struct file *file)
517 struct inotify_device *dev = file->private_data;
519 inotify_destroy(dev->ih);
521 /* destroy all of the events on this device */
522 mutex_lock(&dev->ev_mutex);
523 while (!list_empty(&dev->events))
524 inotify_dev_event_dequeue(dev);
525 mutex_unlock(&dev->ev_mutex);
527 if (file->f_flags & FASYNC)
528 inotify_fasync(-1, file, 0);
530 /* free this device: the put matching the get in inotify_init() */
531 put_inotify_dev(dev);
533 return 0;
536 static long inotify_ioctl(struct file *file, unsigned int cmd,
537 unsigned long arg)
539 struct inotify_device *dev;
540 void __user *p;
541 int ret = -ENOTTY;
543 dev = file->private_data;
544 p = (void __user *) arg;
546 switch (cmd) {
547 case FIONREAD:
548 ret = put_user(dev->queue_size, (int __user *) p);
549 break;
552 return ret;
555 static const struct file_operations inotify_fops = {
556 .poll = inotify_poll,
557 .read = inotify_read,
558 .fasync = inotify_fasync,
559 .release = inotify_release,
560 .unlocked_ioctl = inotify_ioctl,
561 .compat_ioctl = inotify_ioctl,
564 static const struct inotify_operations inotify_user_ops = {
565 .handle_event = inotify_dev_queue_event,
566 .destroy_watch = free_inotify_user_watch,
569 asmlinkage long sys_inotify_init1(int flags)
571 struct inotify_device *dev;
572 struct inotify_handle *ih;
573 struct user_struct *user;
574 struct file *filp;
575 int fd, ret;
577 /* Check the IN_* constants for consistency. */
578 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
579 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
581 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
582 return -EINVAL;
584 fd = get_unused_fd_flags(flags & O_CLOEXEC);
585 if (fd < 0)
586 return fd;
588 filp = get_empty_filp();
589 if (!filp) {
590 ret = -ENFILE;
591 goto out_put_fd;
594 user = get_uid(current->user);
595 if (unlikely(atomic_read(&user->inotify_devs) >=
596 inotify_max_user_instances)) {
597 ret = -EMFILE;
598 goto out_free_uid;
601 dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL);
602 if (unlikely(!dev)) {
603 ret = -ENOMEM;
604 goto out_free_uid;
607 ih = inotify_init(&inotify_user_ops);
608 if (IS_ERR(ih)) {
609 ret = PTR_ERR(ih);
610 goto out_free_dev;
612 dev->ih = ih;
613 dev->fa = NULL;
615 filp->f_op = &inotify_fops;
616 filp->f_path.mnt = mntget(inotify_mnt);
617 filp->f_path.dentry = dget(inotify_mnt->mnt_root);
618 filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
619 filp->f_mode = FMODE_READ;
620 filp->f_flags = O_RDONLY | (flags & O_NONBLOCK);
621 filp->private_data = dev;
623 INIT_LIST_HEAD(&dev->events);
624 init_waitqueue_head(&dev->wq);
625 mutex_init(&dev->ev_mutex);
626 mutex_init(&dev->up_mutex);
627 dev->event_count = 0;
628 dev->queue_size = 0;
629 dev->max_events = inotify_max_queued_events;
630 dev->user = user;
631 atomic_set(&dev->count, 0);
633 get_inotify_dev(dev);
634 atomic_inc(&user->inotify_devs);
635 fd_install(fd, filp);
637 return fd;
638 out_free_dev:
639 kfree(dev);
640 out_free_uid:
641 free_uid(user);
642 put_filp(filp);
643 out_put_fd:
644 put_unused_fd(fd);
645 return ret;
648 asmlinkage long sys_inotify_init(void)
650 return sys_inotify_init1(0);
653 asmlinkage long sys_inotify_add_watch(int fd, const char __user *path, u32 mask)
655 struct inode *inode;
656 struct inotify_device *dev;
657 struct nameidata nd;
658 struct file *filp;
659 int ret, fput_needed;
660 unsigned flags = 0;
662 filp = fget_light(fd, &fput_needed);
663 if (unlikely(!filp))
664 return -EBADF;
666 /* verify that this is indeed an inotify instance */
667 if (unlikely(filp->f_op != &inotify_fops)) {
668 ret = -EINVAL;
669 goto fput_and_out;
672 if (!(mask & IN_DONT_FOLLOW))
673 flags |= LOOKUP_FOLLOW;
674 if (mask & IN_ONLYDIR)
675 flags |= LOOKUP_DIRECTORY;
677 ret = find_inode(path, &nd, flags);
678 if (unlikely(ret))
679 goto fput_and_out;
681 /* inode held in place by reference to nd; dev by fget on fd */
682 inode = nd.path.dentry->d_inode;
683 dev = filp->private_data;
685 mutex_lock(&dev->up_mutex);
686 ret = inotify_find_update_watch(dev->ih, inode, mask);
687 if (ret == -ENOENT)
688 ret = create_watch(dev, inode, mask);
689 mutex_unlock(&dev->up_mutex);
691 path_put(&nd.path);
692 fput_and_out:
693 fput_light(filp, fput_needed);
694 return ret;
697 asmlinkage long sys_inotify_rm_watch(int fd, u32 wd)
699 struct file *filp;
700 struct inotify_device *dev;
701 int ret, fput_needed;
703 filp = fget_light(fd, &fput_needed);
704 if (unlikely(!filp))
705 return -EBADF;
707 /* verify that this is indeed an inotify instance */
708 if (unlikely(filp->f_op != &inotify_fops)) {
709 ret = -EINVAL;
710 goto out;
713 dev = filp->private_data;
715 /* we free our watch data when we get IN_IGNORED */
716 ret = inotify_rm_wd(dev->ih, wd);
718 out:
719 fput_light(filp, fput_needed);
720 return ret;
723 static int
724 inotify_get_sb(struct file_system_type *fs_type, int flags,
725 const char *dev_name, void *data, struct vfsmount *mnt)
727 return get_sb_pseudo(fs_type, "inotify", NULL,
728 INOTIFYFS_SUPER_MAGIC, mnt);
731 static struct file_system_type inotify_fs_type = {
732 .name = "inotifyfs",
733 .get_sb = inotify_get_sb,
734 .kill_sb = kill_anon_super,
738 * inotify_user_setup - Our initialization function. Note that we cannnot return
739 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
740 * must result in panic().
742 static int __init inotify_user_setup(void)
744 int ret;
746 ret = register_filesystem(&inotify_fs_type);
747 if (unlikely(ret))
748 panic("inotify: register_filesystem returned %d!\n", ret);
750 inotify_mnt = kern_mount(&inotify_fs_type);
751 if (IS_ERR(inotify_mnt))
752 panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt));
754 inotify_max_queued_events = 16384;
755 inotify_max_user_instances = 128;
756 inotify_max_user_watches = 8192;
758 watch_cachep = kmem_cache_create("inotify_watch_cache",
759 sizeof(struct inotify_user_watch),
760 0, SLAB_PANIC, NULL);
761 event_cachep = kmem_cache_create("inotify_event_cache",
762 sizeof(struct inotify_kernel_event),
763 0, SLAB_PANIC, NULL);
765 return 0;
768 module_init(inotify_user_setup);