inotify is for lovers Documentation/dnotify.txt | 8 drivers/char/Kconfig | 13 drivers/char/Makefile | 1 drivers/char/inotify.c | 968 +++++++++++++++++ fs/Kconfig | 12 fs/Makefile | 13 fs/attr.c | 31 fs/dnotify.c | 5 fs/file_table.c | 6 fs/inode.c | 3 fs/namei.c | 26 fs/namei.c.orig | 2536 ++++++++++++++++++++++++++++++++++++++++++++++ fs/open.c | 7 fs/read_write.c | 17 fs/super.c | 2 include/linux/dnotify.h | 46 include/linux/fs.h | 9 include/linux/inotify.h | 151 ++ kernel/sysctl.c | 8 19 files changed, 3831 insertions(+), 31 deletions(-) diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/Documentation/dnotify.txt linux-inotify/Documentation/dnotify.txt --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/Documentation/dnotify.txt 2004-04-03 22:37:23.000000000 -0500 +++ linux-inotify/Documentation/dnotify.txt 2004-10-05 16:15:07.000000000 -0400 @@ -54,6 +54,14 @@ Also, files that are unlinked, will still cause notifications in the last directory that they were linked to. +Configuration +------------- + +Dnotify is controlled via the CONFIG_DNOTIFY configuration option. When +disabled, fcntl(fd, F_NOTIFY, ...) will return -EINVAL. + +Dnotify is deprecated in favor of inotify (CONFIG_INOTIFY). + Example ------- diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/drivers/char/inotify.c linux-inotify/drivers/char/inotify.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/drivers/char/inotify.c 1969-12-31 19:00:00.000000000 -0500 +++ linux-inotify/drivers/char/inotify.c 2004-10-06 14:10:52.000000000 -0400 @@ -0,0 +1,968 @@ +/* + * Inode based directory notifications for Linux. + * + * Copyright (C) 2004 John McCutchan + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + */ + +/* TODO: + * unmount events don't get sent if filesystem is mounted in two places + * MOVED_TO/MOVED_FROM filename is wrong + * dynamically allocate event filename + * need a way to connect MOVED_TO/MOVED_FROM events in user space + * use slab cache for inotify_inode_data structures + * watcher -> watch in the release notes + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_INOTIFY_DEVS 8 /* max open inotify devices */ +#define MAX_INOTIFY_DEV_WATCHES 8192 /* max total watches */ +#define MAX_INOTIFY_QUEUED_EVENTS 256 /* max events queued on the dev */ + +static atomic_t watch_count; +static kmem_cache_t *watch_cachep; +static kmem_cache_t *event_cachep; + +/* For debugging */ +static int inotify_debug_flags; +#define iprintk(f, str...) if (inotify_debug_flags & f) printk (KERN_ALERT str) + +/* + * struct inotify_device - represents an open instance of an inotify device + * + * For each inotify device, we need to keep track of the events queued on it, + * a list of the inodes that we are watching, and so on. + * + * 'bitmask' holds one bit for each possible watch descriptor: a set bit + * implies that the given WD is valid, unset implies it is not. + * + * This structure is protected by 'lock'. Lock ordering: + * + * inode->i_lock + * dev->lock + * dev->wait->lock + * + * FIXME: Look at replacing i_lock with i_sem. + */ +struct inotify_device { + DECLARE_BITMAP(bitmask, MAX_INOTIFY_DEV_WATCHES); + wait_queue_head_t wait; + struct list_head events; + struct list_head watches; + spinlock_t lock; + unsigned int event_count; + unsigned int nr_watches; +}; + +struct inotify_watch { + __s32 wd; /* watch descriptor */ + __u32 mask; + struct inode * inode; + struct inotify_device * dev; + struct list_head d_list; /* device list */ + struct list_head i_list; /* inode list */ + struct list_head u_list; /* unmount list */ +}; +#define inotify_watch_d_list(pos) list_entry((pos), struct inotify_watch, d_list) +#define inotify_watch_i_list(pos) list_entry((pos), struct inotify_watch, i_list) +#define inotify_watch_u_list(pos) list_entry((pos), struct inotify_watch, u_list) + +/* + * A list of these is attached to each instance of the driver + * when the drivers read() gets called, this list is walked and + * all events that can fit in the buffer get delivered + */ +struct inotify_kernel_event { + struct list_head list; + struct inotify_event event; +}; + +/* + * find_inode - resolve a user-given path to a specific inode and iget() it + */ +static struct inode * find_inode(const char __user *dirname) +{ + struct inode *inode; + struct nameidata nd; + int error; + + error = __user_walk(dirname, LOOKUP_FOLLOW, &nd, NULL); + if (error) { + iprintk(INOTIFY_DEBUG_INODE, "could not find inode\n"); + inode = ERR_PTR(error); + goto out; + } + + inode = nd.dentry->d_inode; + __iget(inode); + iprintk(INOTIFY_DEBUG_INODE, "ref'd inode\n"); + path_release(&nd); +out: + return inode; +} + +static void unref_inode(struct inode *inode) +{ + iprintk(INOTIFY_DEBUG_INODE, "unref'd inode\n"); + iput(inode); +} + +struct inotify_kernel_event *kernel_event(int wd, __u32 mask, + const char *filename) +{ + struct inotify_kernel_event *kevent; + + kevent = kmem_cache_alloc(event_cachep, GFP_ATOMIC); + if (!kevent) { + iprintk(INOTIFY_DEBUG_ALLOC, + "failed to alloc kevent (%d,%d)\n", wd, mask); + goto out; + } + + iprintk(INOTIFY_DEBUG_ALLOC, "alloced kevent %p (%d,%d)\n", + kevent, wd, mask); + + kevent->event.wd = wd; + kevent->event.mask = mask; + INIT_LIST_HEAD(&kevent->list); + + if (filename) { + iprintk(INOTIFY_DEBUG_FILEN, + "filename for event was %p %s\n", filename, filename); + strncpy(kevent->event.filename, filename, + INOTIFY_FILENAME_MAX); + kevent->event.filename[INOTIFY_FILENAME_MAX-1] = '\0'; + iprintk(INOTIFY_DEBUG_FILEN, + "filename after copying %s\n", kevent->event.filename); + } else { + iprintk(INOTIFY_DEBUG_FILEN, "no filename for event\n"); + kevent->event.filename[0] = '\0'; + } + + +out: + return kevent; +} + +void delete_kernel_event(struct inotify_kernel_event *kevent) +{ + if (!kevent) + return; + iprintk(INOTIFY_DEBUG_ALLOC, "free'd kevent %p\n", kevent); + kmem_cache_free(event_cachep, kevent); +} + +#define list_to_inotify_kernel_event(pos) list_entry((pos), struct inotify_kernel_event, list) +#define inotify_dev_get_event(dev) (list_to_inotify_kernel_event(dev->events.next)) +#define inotify_dev_has_events(dev) (!list_empty(&dev->events)) + +/* Does this events mask get sent to the watch ? */ +#define event_and(event_mask,watches_mask) ((event_mask == IN_UNMOUNT) || \ + (event_mask == IN_IGNORED) || \ + (event_mask & watches_mask)) + +/* + * inotify_dev_queue_event - add a new event to the given device + * + * Caller must hold dev->lock. + */ +static void inotify_dev_queue_event(struct inotify_device *dev, + struct inotify_watch *watch, + __u32 mask, const char *filename) +{ + struct inotify_kernel_event *kevent, *last; + + /* + * Check if the new event is a duplicate of the last event queued. + */ + last = inotify_dev_get_event(dev); + if (dev->event_count && last->event.mask == mask && + last->event.wd == watch->wd) { + /* Check if the filenames match */ + if (!filename && last->event.filename[0] == '\0') + return; + if (filename && !strcmp(last->event.filename, filename)) + return; + } + + /* + * the queue has already overflowed and we have already sent the + * Q_OVERFLOW event + */ + if (dev->event_count > MAX_INOTIFY_QUEUED_EVENTS) { + iprintk(INOTIFY_DEBUG_EVENTS, + "event queue for %p overflowed\n", dev); + return; + } + + /* the queue has just overflowed and we need to notify user space */ + if (dev->event_count == MAX_INOTIFY_QUEUED_EVENTS) { + dev->event_count++; + kevent = kernel_event(-1, IN_Q_OVERFLOW, NULL); + iprintk(INOTIFY_DEBUG_EVENTS, "sending IN_Q_OVERFLOW to %p\n", + dev); + goto add_event_to_queue; + } + + if (!event_and(mask, watch->inode->inotify_data->watch_mask) || + !event_and(mask, watch->mask)) + return; + + dev->event_count++; + kevent = kernel_event(watch->wd, mask, filename); + +add_event_to_queue: + if (!kevent) { + iprintk(INOTIFY_DEBUG_EVENTS, "failed to queue event for %p" + " -- could not allocate kevent\n", dev); + dev->event_count--; + return; + } + + /* queue the event and wake up anyone waiting */ + list_add_tail(&kevent->list, &dev->events); + iprintk(INOTIFY_DEBUG_EVENTS, + "queued event %x for %p\n", kevent->event.mask, dev); + wake_up_interruptible(&dev->wait); +} + +/* + * inotify_dev_event_dequeue - destroy an event on the given device + * + * Caller must hold dev->lock. + */ +static void inotify_dev_event_dequeue(struct inotify_device *dev) +{ + struct inotify_kernel_event *kevent; + + if (!inotify_dev_has_events(dev)) + return; + + kevent = inotify_dev_get_event(dev); + list_del(&kevent->list); + dev->event_count--; + delete_kernel_event(kevent); + + iprintk(INOTIFY_DEBUG_EVENTS, "dequeued event on %p\n", dev); +} + +/* + * inotify_dev_get_wd - returns the next WD for use by the given dev + * + * Caller must hold dev->lock before calling. + */ +static int inotify_dev_get_wd(struct inotify_device *dev) +{ + int wd; + + if (!dev || dev->nr_watches == MAX_INOTIFY_DEV_WATCHES) + return -1; + + dev->nr_watches++; + wd = find_first_zero_bit(dev->bitmask, MAX_INOTIFY_DEV_WATCHES); + set_bit(wd, dev->bitmask); + + return wd; +} + +/* + * inotify_dev_put_wd - release the given WD on the given device + * + * Caller must hold dev->lock. + */ +static int inotify_dev_put_wd(struct inotify_device *dev, int wd) +{ + if (!dev || wd < 0) + return -1; + + dev->nr_watches--; + clear_bit(wd, dev->bitmask); + + return 0; +} + +/* + * create_watch - creates a watch on the given device. + * + * Grabs dev->lock, so the caller must not hold it. + */ +static struct inotify_watch *create_watch(struct inotify_device *dev, + __u32 mask, struct inode *inode) +{ + struct inotify_watch *watch; + + watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL); + if (!watch) { + iprintk(INOTIFY_DEBUG_ALLOC, + "failed to allocate watch (%p,%d)\n", inode, mask); + return NULL; + } + + watch->mask = mask; + watch->inode = inode; + watch->dev = dev; + INIT_LIST_HEAD(&watch->d_list); + INIT_LIST_HEAD(&watch->i_list); + INIT_LIST_HEAD(&watch->u_list); + + spin_lock(&dev->lock); + watch->wd = inotify_dev_get_wd(dev); + spin_unlock(&dev->lock); + + if (watch->wd < 0) { + iprintk(INOTIFY_DEBUG_ERRORS, + "Could not get wd for watch %p\n", watch); + iprintk(INOTIFY_DEBUG_ALLOC, "free'd watch %p\n", watch); + kmem_cache_free(watch_cachep, watch); + return NULL; + } + + return watch; +} + +/* + * delete_watch - removes the given 'watch' from the given 'dev' + * + * Caller must hold dev->lock. + */ +static void delete_watch(struct inotify_device *dev, + struct inotify_watch *watch) +{ + inotify_dev_put_wd(dev, watch->wd); + iprintk(INOTIFY_DEBUG_ALLOC, "free'd watch %p\n", watch); + kmem_cache_free(watch_cachep, watch); +} + +/* + * inotify_find_dev - find the watch associated with the given inode and dev + * + * Caller must hold dev->lock. + */ +static struct inotify_watch *inode_find_dev(struct inode *inode, + struct inotify_device *dev) +{ + struct inotify_watch *watch; + + if (!inode->inotify_data) + return NULL; + + list_for_each_entry(watch, &inode->inotify_data->watches, i_list) { + if (watch->dev == dev) + return watch; + } + + return NULL; +} + +static struct inotify_watch *dev_find_wd(struct inotify_device *dev, int wd) +{ + struct inotify_watch *watch; + + list_for_each_entry(watch, &dev->watches, d_list) { + if (watch->wd == wd) + return watch; + } + + return NULL; +} + +static int inotify_dev_is_watching_inode(struct inotify_device *dev, + struct inode *inode) +{ + struct inotify_watch *watch; + + list_for_each_entry(watch, &dev->watches, d_list) { + if (watch->inode == inode) + return 1; + } + + return 0; +} + +/* + * inotify_dev_add_watcher - add the given watcher to the given device instance + * + * Caller must hold dev->lock. + */ +static int inotify_dev_add_watch(struct inotify_device *dev, + struct inotify_watch *watch) +{ + if (!dev || !watch) + return -EINVAL; + + if (dev_find_wd (dev, watch->wd)) + return -EINVAL; + + if (dev->nr_watches == MAX_INOTIFY_DEV_WATCHES) + return -ENOSPC; + + list_add(&watch->d_list, &dev->watches); + return 0; +} + +/* + * inotify_dev_rm_watch - remove the given watch from the given device + * + * Caller must hold dev->lock because we call inotify_dev_queue_event(). + */ +static int inotify_dev_rm_watch(struct inotify_device *dev, + struct inotify_watch *watch) +{ + if (!watch) + return -EINVAL; + + inotify_dev_queue_event(dev, watch, IN_IGNORED, NULL); + list_del(&watch->d_list); + + return 0; +} + +void inode_update_watch_mask(struct inode *inode) +{ + struct inotify_watch *watch; + __u32 new_mask; + + if (!inode->inotify_data) + return; + + new_mask = 0; + list_for_each_entry(watch, &inode->inotify_data->watches, i_list) + new_mask |= watch->mask; + + inode->inotify_data->watch_mask = new_mask; +} + +/* + * inode_add_watch - add a watch to the given inode + * + * Callers must hold dev->lock, because we call inode_find_dev(). + */ +static int inode_add_watch(struct inode *inode, + struct inotify_watch *watch) +{ + if (!inode || !watch || inode_find_dev(inode, watch->dev)) + return -EINVAL; + + /* + * This inode doesn't have an inotify_data structure attached to it + */ + if (!inode->inotify_data) { + inode->inotify_data = kmalloc(sizeof(struct inotify_inode_data), + GFP_ATOMIC); + INIT_LIST_HEAD(&inode->inotify_data->watches); + inode->inotify_data->watch_mask = 0; + inode->inotify_data->watch_count = 0; + } + list_add(&watch->i_list, &inode->inotify_data->watches); + inode->inotify_data->watch_count++; + inode_update_watch_mask(inode); + + return 0; +} + +static int inode_rm_watch(struct inode *inode, + struct inotify_watch *watch) +{ + if (!inode || !watch || !inode->inotify_data) + return -EINVAL; + + list_del(&watch->i_list); + inode->inotify_data->watch_count--; + + if (!inode->inotify_data->watch_count) { + kfree(inode->inotify_data); + inode->inotify_data = NULL; + } + + inode_update_watch_mask(inode); + + return 0; +} + +/* Kernel API */ + +void inotify_inode_queue_event(struct inode *inode, __u32 mask, + const char *filename) +{ + struct inotify_watch *watch; + + if (!inode->inotify_data) + return; + + spin_lock(&inode->i_lock); + + list_for_each_entry(watch, &inode->inotify_data->watches, i_list) { + spin_lock(&watch->dev->lock); + inotify_dev_queue_event(watch->dev, watch, mask, filename); + spin_unlock(&watch->dev->lock); + } + + spin_unlock(&inode->i_lock); +} +EXPORT_SYMBOL_GPL(inotify_inode_queue_event); + +void inotify_dentry_parent_queue_event(struct dentry *dentry, __u32 mask, + const char *filename) +{ + struct dentry *parent; + + parent = dget_parent(dentry); + inotify_inode_queue_event(parent->d_inode, mask, filename); + dput(parent); +} +EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event); + +static void ignore_helper(struct inotify_watch *watch, int event) +{ + struct inotify_device *dev; + struct inode *inode; + + inode = watch->inode; + dev = watch->dev; + + spin_lock(&inode->i_lock); + spin_lock(&dev->lock); + + if (event) + inotify_dev_queue_event(dev, watch, event, NULL); + + inode_rm_watch(inode, watch); + inotify_dev_rm_watch(watch->dev, watch); + list_del(&watch->u_list); + + delete_watch(dev, watch); + spin_unlock(&dev->lock); + spin_unlock(&inode->i_lock); + + unref_inode(inode); +} + +static void process_umount_list(struct list_head *umount) +{ + struct inotify_watch *watch, *next; + + list_for_each_entry_safe(watch, next, umount, u_list) + ignore_helper(watch, IN_UNMOUNT); +} + +/* + * build_umount_list - build a list of watches affected by an unmount. + * + * Caller must hold inode_lock. + */ +static void build_umount_list(struct list_head *head, struct super_block *sb, + struct list_head *umount) +{ + struct inode *inode; + + list_for_each_entry(inode, head, i_list) { + struct inotify_watch *watch; + + if (inode->i_sb != sb) + continue; + + if (!inode->inotify_data) + continue; + + spin_lock(&inode->i_lock); + + list_for_each_entry(watch, &inode->inotify_data->watches, + i_list) + list_add(&watch->u_list, umount); + + spin_unlock(&inode->i_lock); + } +} + +void inotify_super_block_umount(struct super_block *sb) +{ + struct list_head umount; + + INIT_LIST_HEAD(&umount); + + spin_lock(&inode_lock); + build_umount_list(&inode_in_use, sb, &umount); + spin_unlock(&inode_lock); + + process_umount_list(&umount); +} +EXPORT_SYMBOL_GPL(inotify_super_block_umount); + +/* + * inotify_inode_is_dead - an inode has been deleted, cleanup any watches + * + * FIXME: Callers need to always hold inode->i_lock. + */ +void inotify_inode_is_dead(struct inode *inode) +{ + struct inotify_watch *watch, *next; + struct inotify_inode_data *data; + + data = inode->inotify_data; + if (!data) + return; + + list_for_each_entry_safe(watch, next, &data->watches, i_list) + ignore_helper(watch, 0); +} +EXPORT_SYMBOL_GPL(inotify_inode_is_dead); + +/* The driver interface is implemented below */ + +static unsigned int inotify_poll(struct file *file, poll_table *wait) +{ + struct inotify_device *dev; + + dev = file->private_data; + + poll_wait(file, &dev->wait, wait); + + if (inotify_dev_has_events(dev)) + return POLLIN | POLLRDNORM; + + return 0; +} + +static ssize_t inotify_read(struct file *file, char __user *buf, + size_t count, loff_t *pos) +{ + size_t event_size; + struct inotify_device *dev; + char *start; + DECLARE_WAITQUEUE(wait, current); + + start = buf; + dev = file->private_data; + + /* We only hand out full inotify events */ + event_size = sizeof(struct inotify_event); + if (count < event_size) + return -EINVAL; + + while(1) { + int has_events; + + spin_lock(&dev->lock); + has_events = inotify_dev_has_events(dev); + spin_unlock(&dev->lock); + if (has_events) + break; + + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + + if (signal_pending(current)) + return -ERESTARTSYS; + + add_wait_queue(&dev->wait, &wait); + set_current_state(TASK_INTERRUPTIBLE); + + schedule(); + + set_current_state(TASK_RUNNING); + remove_wait_queue(&dev->wait, &wait); + } + + while (count >= event_size) { + struct inotify_kernel_event *kevent; + + spin_lock(&dev->lock); + if (!inotify_dev_has_events(dev)) { + spin_unlock(&dev->lock); + break; + } + kevent = inotify_dev_get_event(dev); + spin_unlock(&dev->lock); + if (copy_to_user(buf, &kevent->event, event_size)) + return -EFAULT; + + spin_lock(&dev->lock); + inotify_dev_event_dequeue(dev); + spin_unlock(&dev->lock); + count -= event_size; + buf += event_size; + } + + return buf - start; +} + +static int inotify_open(struct inode *inode, struct file *file) +{ + struct inotify_device *dev; + + if (atomic_read(&watch_count) == MAX_INOTIFY_DEVS) + return -ENODEV; + + atomic_inc(&watch_count); + + dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL); + if (!dev) + return -ENOMEM; + + bitmap_clear(dev->bitmask, MAX_INOTIFY_DEV_WATCHES); + + INIT_LIST_HEAD(&dev->events); + INIT_LIST_HEAD(&dev->watches); + init_waitqueue_head(&dev->wait); + + dev->event_count = 0; + dev->nr_watches = 0; + dev->lock = SPIN_LOCK_UNLOCKED; + + file->private_data = dev; + + printk(KERN_ALERT "inotify device opened\n"); + + return 0; +} + +/* + * inotify_release_all_watches - destroy all watches on a given device + */ +static void inotify_release_all_watches(struct inotify_device *dev) +{ + struct inotify_watch *watch,*next; + + list_for_each_entry_safe(watch, next, &dev->watches, d_list) + ignore_helper(watch, 0); +} + +/* + * inotify_release_all_events - destroy all of the events on a given device + */ +static void inotify_release_all_events(struct inotify_device *dev) +{ + spin_lock(&dev->lock); + while (inotify_dev_has_events(dev)) + inotify_dev_event_dequeue(dev); + spin_unlock(&dev->lock); +} + +static int inotify_release(struct inode *inode, struct file *file) +{ + struct inotify_device *dev; + + dev = file->private_data; + inotify_release_all_watches(dev); + inotify_release_all_events(dev); + kfree(dev); + + printk(KERN_ALERT "inotify device released\n"); + + atomic_dec(&watch_count); + return 0; +} + +static int inotify_watch(struct inotify_device *dev, + struct inotify_watch_request *request) +{ + struct inode *inode; + struct inotify_watch *watch; + + inode = find_inode(request->dirname); + if (IS_ERR(inode)) + return PTR_ERR(inode); + + if (!S_ISDIR(inode->i_mode)) + iprintk(INOTIFY_DEBUG_ERRORS, "watching file\n"); + + spin_lock(&inode->i_lock); + spin_lock(&dev->lock); + + /* + * This handles the case of re-adding a directory we are already + * watching, we just update the mask and return 0 + */ + if (inotify_dev_is_watching_inode(dev, inode)) { + struct inotify_watch *owatch; /* the old watch */ + + iprintk(INOTIFY_DEBUG_ERRORS, + "adjusting event mask for inode %p\n", inode); + + owatch = inode_find_dev(inode, dev); + owatch->mask = request->mask; + inode_update_watch_mask(inode); + spin_unlock(&dev->lock); + spin_unlock(&inode->i_lock); + unref_inode(inode); + + return 0; + } + + spin_unlock(&dev->lock); + spin_unlock(&inode->i_lock); + + watch = create_watch(dev, request->mask, inode); + if (!watch) { + unref_inode(inode); + return -ENOSPC; + } + + spin_lock(&inode->i_lock); + spin_lock(&dev->lock); + + /* We can't add anymore watches to this device */ + if (inotify_dev_add_watch(dev, watch) == -ENOSPC) { + iprintk(INOTIFY_DEBUG_ERRORS, + "can't add watch dev is full\n"); + delete_watch(dev, watch); + spin_unlock(&dev->lock); + spin_unlock(&inode->i_lock); + unref_inode(inode); + return -ENOSPC; + } + + inode_add_watch(inode, watch); + + spin_unlock(&dev->lock); + spin_unlock(&inode->i_lock); + + return watch->wd; +} + +static int inotify_ignore(struct inotify_device *dev, int wd) +{ + struct inotify_watch *watch; + + watch = dev_find_wd(dev, wd); + if (!watch) + return -EINVAL; + ignore_helper(watch, 0); + + return 0; +} + +static void inotify_print_stats(struct inotify_device *dev) +{ + int sizeof_inotify_watch; + int sizeof_inotify_device; + int sizeof_inotify_kernel_event; + + sizeof_inotify_watch = sizeof (struct inotify_watch); + sizeof_inotify_device = sizeof (struct inotify_device); + sizeof_inotify_kernel_event = sizeof (struct inotify_kernel_event); + + printk(KERN_ALERT "GLOBAL INOTIFY STATS\n"); + printk(KERN_ALERT "watch_count = %d\n", atomic_read(&watch_count)); + + printk(KERN_ALERT "sizeof(struct inotify_watch) = %d\n", + sizeof_inotify_watch); + printk(KERN_ALERT "sizeof(struct inotify_device) = %d\n", + sizeof_inotify_device); + printk(KERN_ALERT "sizeof(struct inotify_kernel_event) = %d\n", + sizeof_inotify_kernel_event); + + spin_lock(&dev->lock); + + printk(KERN_ALERT "inotify device: %p\n", dev); + printk(KERN_ALERT "inotify event_count: %u\n", dev->event_count); + printk(KERN_ALERT "inotify watch_count: %d\n", dev->nr_watches); + + spin_unlock(&dev->lock); +} + +/* + * inotify_ioctl() - our device file's ioctl method + * + * The VFS serializes all of our calls via the BKL and we rely on that. We + * could, alternatively, grab dev->lock. Right now lower levels grab that + * where needed. + */ +static int inotify_ioctl(struct inode *ip, struct file *fp, + unsigned int cmd, unsigned long arg) +{ + struct inotify_device *dev; + struct inotify_watch_request request; + void __user *p; + int wd; + + dev = fp->private_data; + p = (void __user *) arg; + + switch (cmd) { + case INOTIFY_WATCH: + iprintk(INOTIFY_DEBUG_ERRORS, "INOTIFY_WATCH ioctl\n"); + if (copy_from_user(&request, p, sizeof (request))) + return -EFAULT; + return inotify_watch(dev, &request); + case INOTIFY_IGNORE: + iprintk(INOTIFY_DEBUG_ERRORS, "INOTIFY_IGNORE ioctl\n"); + if (copy_from_user(&wd, p, sizeof (wd))) + return -EFAULT; + return inotify_ignore(dev, wd); + case INOTIFY_STATS: + iprintk(INOTIFY_DEBUG_ERRORS, "INOTIFY_STATS ioctl\n"); + inotify_print_stats(dev); + return 0; + case INOTIFY_SETDEBUG: + iprintk(INOTIFY_DEBUG_ERRORS, "INOTIFY_SETDEBUG ioctl\n"); + if (copy_from_user(&inotify_debug_flags, p, sizeof (int))) + return -EFAULT; + return 0; + default: + return -ENOTTY; + } +} + +static struct file_operations inotify_fops = { + .owner = THIS_MODULE, + .poll = inotify_poll, + .read = inotify_read, + .open = inotify_open, + .release = inotify_release, + .ioctl = inotify_ioctl, +}; + +struct miscdevice inotify_device = { + .minor = MISC_DYNAMIC_MINOR, + .name = "inotify", + .fops = &inotify_fops, +}; + +static int __init inotify_init(void) +{ + int ret; + + ret = misc_register(&inotify_device); + if (ret) + goto out; + + inotify_debug_flags = INOTIFY_DEBUG_NONE; + + watch_cachep = kmem_cache_create("inotify_watch_cache", + sizeof(struct inotify_watch), 0, 0, + NULL, NULL); + + event_cachep = kmem_cache_create("inotify_event_cache", + sizeof(struct inotify_kernel_event), 0, + 0, NULL, NULL); + + printk(KERN_INFO "inotify init: minor=%d\n", inotify_device.minor); +out: + return ret; +} + +module_init(inotify_init); diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/drivers/char/Kconfig linux-inotify/drivers/char/Kconfig --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/drivers/char/Kconfig 2004-09-27 03:41:14.000000000 -0400 +++ linux-inotify/drivers/char/Kconfig 2004-10-05 16:15:07.000000000 -0400 @@ -65,6 +65,19 @@ config ECC tristate "ECC memory monitoring" +config INOTIFY + bool "Inotify file change notification support" + default y + ---help--- + Say Y here to enable inotify support and the /dev/inotify character + device. Inotify is a file change notification system and a + replacement for dnotify. Inotify fixes numerous shortcomings in + dnotify and introduces several new features. It allows monitoring + of both files and directories via a single open fd. Multiple file + events are supported. + + If unsure, say Y. + config SERIAL_NONSTANDARD bool "Non-standard serial port support" ---help--- diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/drivers/char/Makefile linux-inotify/drivers/char/Makefile --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/drivers/char/Makefile 2004-09-27 03:41:14.000000000 -0400 +++ linux-inotify/drivers/char/Makefile 2004-10-05 16:16:46.000000000 -0400 @@ -11,6 +11,7 @@ obj-$(CONFIG_VT) += vt_ioctl.o vc_screen.o consolemap.o \ consolemap_deftbl.o selection.o keyboard.o +obj-$(CONFIG_INOTIFY) += inotify.o obj-$(CONFIG_ECC) += ecc.o obj-$(CONFIG_HW_CONSOLE) += vt.o defkeymap.o obj-$(CONFIG_MAGIC_SYSRQ) += sysrq.o diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/attr.c linux-inotify/fs/attr.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/attr.c 2004-09-27 03:41:57.000000000 -0400 +++ linux-inotify/fs/attr.c 2004-10-05 16:15:07.000000000 -0400 @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -128,6 +129,28 @@ return dn_mask; } +static int setattr_mask_inotify(unsigned int ia_valid) +{ + unsigned long dn_mask = 0; + + if (ia_valid & ATTR_UID) + dn_mask |= IN_ATTRIB; + if (ia_valid & ATTR_GID) + dn_mask |= IN_ATTRIB; + if (ia_valid & ATTR_SIZE) + dn_mask |= IN_MODIFY; + /* both times implies a utime(s) call */ + if ((ia_valid & (ATTR_ATIME|ATTR_MTIME)) == (ATTR_ATIME|ATTR_MTIME)) + dn_mask |= IN_ATTRIB; + else if (ia_valid & ATTR_ATIME) + dn_mask |= IN_ACCESS; + else if (ia_valid & ATTR_MTIME) + dn_mask |= IN_MODIFY; + if (ia_valid & ATTR_MODE) + dn_mask |= IN_ATTRIB; + return dn_mask; +} + int notify_change(struct dentry * dentry, struct iattr * attr) { struct inode *inode = dentry->d_inode; @@ -185,8 +208,16 @@ } if (!error) { unsigned long dn_mask = setattr_mask(ia_valid); + unsigned long in_mask = setattr_mask_inotify(ia_valid); + if (dn_mask) dnotify_parent(dentry, dn_mask); + if (in_mask) { + inotify_inode_queue_event(dentry->d_inode, in_mask, + NULL); + inotify_dentry_parent_queue_event(dentry, in_mask, + dentry->d_name.name); + } } return error; } diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/dnotify.c linux-inotify/fs/dnotify.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/dnotify.c 2004-04-03 22:36:55.000000000 -0500 +++ linux-inotify/fs/dnotify.c 2004-10-05 16:16:19.000000000 -0400 @@ -13,6 +13,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. */ + #include #include #include @@ -21,8 +22,6 @@ #include #include -int dir_notify_enable = 1; - static rwlock_t dn_lock = RW_LOCK_UNLOCKED; static kmem_cache_t *dn_cache; @@ -73,8 +72,6 @@ dnotify_flush(filp, id); return 0; } - if (!dir_notify_enable) - return -EINVAL; inode = filp->f_dentry->d_inode; if (!S_ISDIR(inode->i_mode)) return -ENOTDIR; diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/file_table.c linux-inotify/fs/file_table.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/file_table.c 2004-04-03 22:36:16.000000000 -0500 +++ linux-inotify/fs/file_table.c 2004-10-06 13:43:00.000000000 -0400 @@ -16,6 +16,7 @@ #include #include #include +#include /* sysctl tunables... */ struct files_stat_struct files_stat = { @@ -168,6 +169,11 @@ struct dentry *dentry = file->f_dentry; struct vfsmount *mnt = file->f_vfsmnt; struct inode *inode = dentry->d_inode; + u32 mask; + + mask = (file->f_mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE; + inotify_dentry_parent_queue_event(dentry, mask, dentry->d_name.name); + inotify_inode_queue_event(inode, mask, NULL); /* * The function eventpoll_release() should be the first called diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/inode.c linux-inotify/fs/inode.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/inode.c 2004-09-27 03:41:52.000000000 -0400 +++ linux-inotify/fs/inode.c 2004-10-05 16:15:07.000000000 -0400 @@ -113,6 +113,9 @@ if (inode) { struct address_space * const mapping = &inode->i_data; +#ifdef CONFIG_INOTIFY + inode->inotify_data = NULL; +#endif inode->i_sb = sb; inode->i_blkbits = sb->s_blocksize_bits; inode->i_flags = 0; diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/Kconfig linux-inotify/fs/Kconfig --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/Kconfig 2004-09-27 03:41:53.000000000 -0400 +++ linux-inotify/fs/Kconfig 2004-10-05 16:15:07.000000000 -0400 @@ -562,6 +562,18 @@ depends on XFS_QUOTA || QUOTA default y +config DNOTIFY + bool "Dnotify support" + default y + help + Dnotify is a directory-based per-fd file change notification system + that uses signals to communicate events to user-space. It has + been replaced by inotify (see CONFIG_INOTIFY), which solves many of + the shortcomings of dnotify and adds new features, but some + applications may still rely on dnotify. + + Because of this, if unsure, say Y. + config AUTOFS_FS tristate "Kernel automounter support" help diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/Makefile linux-inotify/fs/Makefile --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/Makefile 2004-09-27 03:41:51.000000000 -0400 +++ linux-inotify/fs/Makefile 2004-10-05 16:15:07.000000000 -0400 @@ -5,12 +5,11 @@ # Rewritten to use lists instead of if-statements. # -obj-y := open.o read_write.o file_table.o buffer.o \ - bio.o super.o block_dev.o char_dev.o stat.o exec.o pipe.o \ - namei.o fcntl.o ioctl.o readdir.o select.o fifo.o locks.o \ - dcache.o inode.o attr.o bad_inode.o file.o dnotify.o \ - filesystems.o namespace.o seq_file.o xattr.o libfs.o \ - fs-writeback.o mpage.o direct-io.o aio.o +obj-y := open.o read_write.o file_table.o buffer.o bio.o super.o \ + block_dev.o char_dev.o stat.o exec.o pipe.o namei.o fcntl.o \ + ioctl.o readdir.o select.o fifo.o locks.o dcache.o inode.o \ + attr.o bad_inode.o file.o filesystems.o namespace.o aio.o \ + seq_file.o xattr.o libfs.o fs-writeback.o mpage.o direct-io.o \ obj-$(CONFIG_EPOLL) += eventpoll.o obj-$(CONFIG_COMPAT) += compat.o @@ -38,6 +37,8 @@ obj-$(CONFIG_QFMT_V2) += quota_v2.o obj-$(CONFIG_QUOTACTL) += quota.o +obj-$(CONFIG_DNOTIFY) += dnotify.o + obj-$(CONFIG_PROC_FS) += proc/ obj-y += partitions/ obj-y += sysfs/ diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/namei.c linux-inotify/fs/namei.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/namei.c 2004-09-27 03:41:58.000000000 -0400 +++ linux-inotify/fs/namei.c 2004-10-06 15:47:50.849359112 -0400 @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -1232,6 +1233,7 @@ error = dir->i_op->create(dir, dentry, mode, nd); if (!error) { inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_FILE, dentry->d_name.name); security_inode_post_create(dir, dentry, mode); } return error; @@ -1543,6 +1545,7 @@ error = dir->i_op->mknod(dir, dentry, mode, dev); if (!error) { inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_FILE, dentry->d_name.name); security_inode_post_mknod(dir, dentry, mode, dev); } return error; @@ -1631,6 +1634,7 @@ error = dir->i_op->mkdir(dir, dentry, mode); if (!error) { inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_SUBDIR, dentry->d_name.name); security_inode_post_mkdir(dir,dentry, mode); } return error; @@ -1741,6 +1745,9 @@ up(&dentry->d_inode->i_sem); if (!error) { inode_dir_notify(dir, DN_DELETE); + inotify_inode_queue_event(dir, IN_DELETE_SUBDIR, dentry->d_name.name); + inotify_inode_queue_event(dentry->d_inode, IN_DELETE_SELF, NULL); + inotify_inode_is_dead (dentry->d_inode); d_delete(dentry); } dput(dentry); @@ -1827,8 +1834,11 @@ /* We don't d_delete() NFS sillyrenamed files--they still exist. */ if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { - d_delete(dentry); inode_dir_notify(dir, DN_DELETE); + inotify_inode_queue_event(dir, IN_DELETE_FILE, dentry->d_name.name); + inotify_inode_queue_event(dentry->d_inode, IN_DELETE_SELF, NULL); + inotify_inode_is_dead (dentry->d_inode); + d_delete(dentry); } return error; } @@ -1918,6 +1928,7 @@ error = dir->i_op->symlink(dir, dentry, oldname); if (!error) { inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_FILE, dentry->d_name.name); security_inode_post_symlink(dir, dentry, oldname); } return error; @@ -2006,6 +2017,7 @@ up(&old_dentry->d_inode->i_sem); if (!error) { inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_FILE, new_dentry->d_name.name); security_inode_post_link(old_dentry, dir, new_dentry); } return error; @@ -2184,6 +2196,7 @@ { int error; int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); + char *old_name; if (old_dentry->d_inode == new_dentry->d_inode) return 0; @@ -2205,18 +2218,25 @@ DQUOT_INIT(old_dir); DQUOT_INIT(new_dir); + old_name = inotify_oldname_init(old_dentry); + if (is_dir) error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); else error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); if (!error) { - if (old_dir == new_dir) + if (old_dir == new_dir) { inode_dir_notify(old_dir, DN_RENAME); - else { + } else { inode_dir_notify(old_dir, DN_DELETE); inode_dir_notify(new_dir, DN_CREATE); } + inotify_inode_queue_event(old_dir, IN_MOVED_FROM, old_name); + inotify_inode_queue_event(new_dir, IN_MOVED_TO, + new_dentry->d_name.name); } + inotify_oldname_free(old_name); + return error; } diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/namei.c.orig linux-inotify/fs/namei.c.orig --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/namei.c.orig 1969-12-31 19:00:00.000000000 -0500 +++ linux-inotify/fs/namei.c.orig 2004-10-05 16:15:07.000000000 -0400 @@ -0,0 +1,2536 @@ +/* + * linux/fs/namei.c + * + * Copyright (C) 1991, 1992 Linus Torvalds + */ + +/* + * Some corrections by tytso. + */ + +/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname + * lookup logic. + */ +/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE]) + +/* [Feb-1997 T. Schoebel-Theuer] + * Fundamental changes in the pathname lookup mechanisms (namei) + * were necessary because of omirr. The reason is that omirr needs + * to know the _real_ pathname, not the user-supplied one, in case + * of symlinks (and also when transname replacements occur). + * + * The new code replaces the old recursive symlink resolution with + * an iterative one (in case of non-nested symlink chains). It does + * this with calls to _follow_link(). + * As a side effect, dir_namei(), _namei() and follow_link() are now + * replaced with a single function lookup_dentry() that can handle all + * the special cases of the former code. + * + * With the new dcache, the pathname is stored at each inode, at least as + * long as the refcount of the inode is positive. As a side effect, the + * size of the dcache depends on the inode cache and thus is dynamic. + * + * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink + * resolution to correspond with current state of the code. + * + * Note that the symlink resolution is not *completely* iterative. + * There is still a significant amount of tail- and mid- recursion in + * the algorithm. Also, note that _readlink() is not used in + * lookup_dentry(): lookup_dentry() on the result of _readlink() + * may return different results than _follow_link(). Many virtual + * filesystems (including /proc) exhibit this behavior. + */ + +/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: + * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL + * and the name already exists in form of a symlink, try to create the new + * name indicated by the symlink. The old code always complained that the + * name already exists, due to not following the symlink even if its target + * is nonexistent. The new semantics affects also mknod() and link() when + * the name is a symlink pointing to a non-existant name. + * + * I don't know which semantics is the right one, since I have no access + * to standards. But I found by trial that HP-UX 9.0 has the full "new" + * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the + * "old" one. Personally, I think the new semantics is much more logical. + * Note that "ln old new" where "new" is a symlink pointing to a non-existing + * file does succeed in both HP-UX and SunOs, but not in Solaris + * and in the old Linux semantics. + */ + +/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink + * semantics. See the comments in "open_namei" and "do_link" below. + * + * [10-Sep-98 Alan Modra] Another symlink change. + */ + +/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: + * inside the path - always follow. + * in the last component in creation/removal/renaming - never follow. + * if LOOKUP_FOLLOW passed - follow. + * if the pathname has trailing slashes - follow. + * otherwise - don't follow. + * (applied in that order). + * + * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT + * restored for 2.4. This is the last surviving part of old 4.2BSD bug. + * During the 2.4 we need to fix the userland stuff depending on it - + * hopefully we will be able to get rid of that wart in 2.5. So far only + * XEmacs seems to be relying on it... + */ +/* + * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) + * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives + * any extra contention... + */ + +/* In order to reduce some races, while at the same time doing additional + * checking and hopefully speeding things up, we copy filenames to the + * kernel data space before using them.. + * + * POSIX.1 2.4: an empty pathname is invalid (ENOENT). + * PATH_MAX includes the nul terminator --RR. + */ +static inline int do_getname(const char __user *filename, char *page) +{ + int retval; + unsigned long len = PATH_MAX; + + if ((unsigned long) filename >= TASK_SIZE) { + if (!segment_eq(get_fs(), KERNEL_DS)) + return -EFAULT; + } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX) + len = TASK_SIZE - (unsigned long) filename; + + retval = strncpy_from_user((char *)page, filename, len); + if (retval > 0) { + if (retval < len) + return 0; + return -ENAMETOOLONG; + } else if (!retval) + retval = -ENOENT; + return retval; +} + +char * getname(const char __user * filename) +{ + char *tmp, *result; + + result = ERR_PTR(-ENOMEM); + tmp = __getname(); + if (tmp) { + int retval = do_getname(filename, tmp); + + result = tmp; + if (retval < 0) { + putname(tmp); + result = ERR_PTR(retval); + } + } + return result; +} + +/* + * vfs_permission() + * + * is used to check for read/write/execute permissions on a file. + * We use "fsuid" for this, letting us set arbitrary permissions + * for filesystem access without changing the "normal" uids which + * are used for other things.. + */ +int vfs_permission(struct inode * inode, int mask) +{ + umode_t mode = inode->i_mode; + + if (mask & MAY_WRITE) { + /* + * Nobody gets write access to a read-only fs. + */ + if (IS_RDONLY(inode) && + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) + return -EROFS; + + /* + * Nobody gets write access to an immutable file. + */ + if (IS_IMMUTABLE(inode)) + return -EACCES; + } + + if (current->fsuid == inode->i_uid) + mode >>= 6; + else if (in_group_p(inode->i_gid)) + mode >>= 3; + + /* + * If the DACs are ok we don't need any capability check. + */ + if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)) + return 0; + + /* + * Read/write DACs are always overridable. + * Executable DACs are overridable if at least one exec bit is set. + */ + if (!(mask & MAY_EXEC) || + (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode)) + if (capable(CAP_DAC_OVERRIDE)) + return 0; + + /* + * Searching includes executable on directories, else just read. + */ + if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) + if (capable(CAP_DAC_READ_SEARCH)) + return 0; + + return -EACCES; +} + +int permission(struct inode * inode,int mask, struct nameidata *nd) +{ + int retval; + int submask; + + /* Ordinary permission routines do not understand MAY_APPEND. */ + submask = mask & ~MAY_APPEND; + + if (inode->i_op && inode->i_op->permission) + retval = inode->i_op->permission(inode, submask, nd); + else + retval = vfs_permission(inode, submask); + if (retval) + return retval; + + return security_inode_permission(inode, mask, nd); +} + +/* + * get_write_access() gets write permission for a file. + * put_write_access() releases this write permission. + * This is used for regular files. + * We cannot support write (and maybe mmap read-write shared) accesses and + * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode + * can have the following values: + * 0: no writers, no VM_DENYWRITE mappings + * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist + * > 0: (i_writecount) users are writing to the file. + * + * Normally we operate on that counter with atomic_{inc,dec} and it's safe + * except for the cases where we don't hold i_writecount yet. Then we need to + * use {get,deny}_write_access() - these functions check the sign and refuse + * to do the change if sign is wrong. Exclusion between them is provided by + * the inode->i_lock spinlock. + */ + +int get_write_access(struct inode * inode) +{ + spin_lock(&inode->i_lock); + if (atomic_read(&inode->i_writecount) < 0) { + spin_unlock(&inode->i_lock); + return -ETXTBSY; + } + atomic_inc(&inode->i_writecount); + spin_unlock(&inode->i_lock); + + return 0; +} + +int deny_write_access(struct file * file) +{ + struct inode *inode = file->f_dentry->d_inode; + + spin_lock(&inode->i_lock); + if (atomic_read(&inode->i_writecount) > 0) { + spin_unlock(&inode->i_lock); + return -ETXTBSY; + } + atomic_dec(&inode->i_writecount); + spin_unlock(&inode->i_lock); + + return 0; +} + +void intent_release(struct lookup_intent *it) +{ + if (!it) + return; + if (it->it_magic != INTENT_MAGIC) + return; + if (it->it_op_release) + it->it_op_release(it); +} + +int deny_write_access_to_inode(struct inode * inode) +{ + spin_lock(&inode->i_lock); + if (atomic_read(&inode->i_writecount) > 0) { + spin_unlock(&inode->i_lock); + return -ETXTBSY; + } + atomic_dec(&inode->i_writecount); + spin_unlock(&inode->i_lock); + return 0; +} + +void path_release(struct nameidata *nd) +{ + intent_release(&nd->intent); + dput(nd->dentry); + mntput(nd->mnt); +} + +/* + * Internal lookup() using the new generic dcache. + * SMP-safe + */ +static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd) +{ + struct dentry * dentry = __d_lookup(parent, name); + + /* lockess __d_lookup may fail due to concurrent d_move() + * in some unrelated directory, so try with d_lookup + */ + if (!dentry) + dentry = d_lookup(parent, name); + + if (dentry && dentry->d_op && dentry->d_op->d_revalidate) { + if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) { + dput(dentry); + dentry = NULL; + } + } + return dentry; +} + +/* + * Short-cut version of permission(), for calling by + * path_walk(), when dcache lock is held. Combines parts + * of permission() and vfs_permission(), and tests ONLY for + * MAY_EXEC permission. + * + * If appropriate, check DAC only. If not appropriate, or + * short-cut DAC fails, then call permission() to do more + * complete permission check. + */ +static inline int exec_permission_lite(struct inode *inode, + struct nameidata *nd) +{ + umode_t mode = inode->i_mode; + + if ((inode->i_op && inode->i_op->permission)) + return -EAGAIN; + + if (current->fsuid == inode->i_uid) + mode >>= 6; + else if (in_group_p(inode->i_gid)) + mode >>= 3; + + if (mode & MAY_EXEC) + goto ok; + + if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE)) + goto ok; + + if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH)) + goto ok; + + return -EACCES; +ok: + return security_inode_permission(inode, MAY_EXEC, nd); +} + +/* + * This is called when everything else fails, and we actually have + * to go to the low-level filesystem to find out what we should do.. + * + * We get the directory semaphore, and after getting that we also + * make sure that nobody added the entry to the dcache in the meantime.. + * SMP-safe + */ +static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd) +{ + struct dentry * result; + struct inode *dir = parent->d_inode; + int counter = 0; + +again: + counter++; + down(&dir->i_sem); + /* + * First re-do the cached lookup just in case it was created + * while we waited for the directory semaphore.. + * + * FIXME! This could use version numbering or similar to + * avoid unnecessary cache lookups. + * + * The "dcache_lock" is purely to protect the RCU list walker + * from concurrent renames at this point (we mustn't get false + * negatives from the RCU list walk here, unlike the optimistic + * fast walk). + * + * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup + */ + result = d_lookup(parent, name); + if (!result) { + struct dentry * dentry = d_alloc(parent, name); + result = ERR_PTR(-ENOMEM); + if (dentry) { + result = dir->i_op->lookup(dir, dentry, nd); + if (result) + dput(dentry); + else + result = dentry; + } + up(&dir->i_sem); + return result; + } + + /* + * Uhhuh! Nasty case: the cache was re-populated while + * we waited on the semaphore. Need to revalidate. + */ + up(&dir->i_sem); + if (result->d_op && result->d_op->d_revalidate) { + if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) { + dput(result); + if (counter > 10) + result = ERR_PTR(-ESTALE); + if (!IS_ERR(result)) + goto again; + } + } + return result; +} + +/* + * This limits recursive symlink follows to 8, while + * limiting consecutive symlinks to 40. + * + * Without that kind of total limit, nasty chains of consecutive + * symlinks can cause almost arbitrarily long lookups. + */ +static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd) +{ + int err = -ELOOP; + if (current->link_count >= 8) + goto loop; + if (current->total_link_count >= 40) + goto loop; + cond_resched(); + err = security_inode_follow_link(dentry, nd); + if (err) + goto loop; + current->link_count++; + current->total_link_count++; + touch_atime(nd->mnt, dentry); + err = dentry->d_inode->i_op->follow_link(dentry, nd); + current->link_count--; + return err; +loop: + path_release(nd); + return err; +} + +int follow_up(struct vfsmount **mnt, struct dentry **dentry) +{ + struct vfsmount *parent; + struct dentry *mountpoint; + spin_lock(&vfsmount_lock); + parent=(*mnt)->mnt_parent; + if (parent == *mnt) { + spin_unlock(&vfsmount_lock); + return 0; + } + mntget(parent); + mountpoint=dget((*mnt)->mnt_mountpoint); + spin_unlock(&vfsmount_lock); + dput(*dentry); + *dentry = mountpoint; + mntput(*mnt); + *mnt = parent; + return 1; +} + +/* no need for dcache_lock, as serialization is taken care in + * namespace.c + */ +static int follow_mount(struct vfsmount **mnt, struct dentry **dentry) +{ + int res = 0; + while (d_mountpoint(*dentry)) { + struct vfsmount *mounted = lookup_mnt(*mnt, *dentry); + if (!mounted) + break; + mntput(*mnt); + *mnt = mounted; + dput(*dentry); + *dentry = dget(mounted->mnt_root); + res = 1; + } + return res; +} + +/* no need for dcache_lock, as serialization is taken care in + * namespace.c + */ +static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry) +{ + struct vfsmount *mounted; + + mounted = lookup_mnt(*mnt, *dentry); + if (mounted) { + mntput(*mnt); + *mnt = mounted; + dput(*dentry); + *dentry = dget(mounted->mnt_root); + return 1; + } + return 0; +} + +int follow_down(struct vfsmount **mnt, struct dentry **dentry) +{ + return __follow_down(mnt,dentry); +} + +static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry) +{ + while(1) { + struct vfsmount *parent; + struct dentry *old = *dentry; + + read_lock(¤t->fs->lock); + if (*dentry == current->fs->root && + *mnt == current->fs->rootmnt) { + read_unlock(¤t->fs->lock); + break; + } + read_unlock(¤t->fs->lock); + spin_lock(&dcache_lock); + if (*dentry != (*mnt)->mnt_root) { + *dentry = dget((*dentry)->d_parent); + spin_unlock(&dcache_lock); + dput(old); + break; + } + spin_unlock(&dcache_lock); + spin_lock(&vfsmount_lock); + parent = (*mnt)->mnt_parent; + if (parent == *mnt) { + spin_unlock(&vfsmount_lock); + break; + } + mntget(parent); + *dentry = dget((*mnt)->mnt_mountpoint); + spin_unlock(&vfsmount_lock); + dput(old); + mntput(*mnt); + *mnt = parent; + } + follow_mount(mnt, dentry); +} + +struct path { + struct vfsmount *mnt; + struct dentry *dentry; +}; + +/* + * It's more convoluted than I'd like it to be, but... it's still fairly + * small and for now I'd prefer to have fast path as straight as possible. + * It _is_ time-critical. + */ +static int do_lookup(struct nameidata *nd, struct qstr *name, + struct path *path) +{ + struct vfsmount *mnt = nd->mnt; + struct dentry *dentry = __d_lookup(nd->dentry, name); + + if (!dentry) + goto need_lookup; + if (dentry->d_op && dentry->d_op->d_revalidate) + goto need_revalidate; +done: + path->mnt = mnt; + path->dentry = dentry; + return 0; + +need_lookup: + dentry = real_lookup(nd->dentry, name, nd); + if (IS_ERR(dentry)) + goto fail; + goto done; + +need_revalidate: + if (dentry->d_op->d_revalidate(dentry, nd)) + goto done; + if (d_invalidate(dentry)) + goto done; + dput(dentry); + goto need_lookup; + +fail: + return PTR_ERR(dentry); +} + +static int revalidate_special(struct nameidata *nd) +{ + struct dentry *dentry = nd->dentry; + int err, counter = 0; + + revalidate_again: + if (!dentry->d_op || !dentry->d_op->d_revalidate) + return 0; + if (!dentry->d_op->d_revalidate(dentry, nd)) { + struct dentry *new; + if ((err = permission(dentry->d_parent->d_inode, MAY_EXEC,nd))) + return err; + new = real_lookup(dentry->d_parent, &dentry->d_name, nd); + if (IS_ERR(new)) + return PTR_ERR(new); + d_invalidate(dentry); + dput(dentry); + nd->dentry = dentry = new; + counter++; + if (counter < 10) + goto revalidate_again; + printk("excessive revalidate_it loops\n"); + return -ESTALE; + } + return 0; +} + +/* + * Name resolution. + * + * This is the basic name resolution function, turning a pathname + * into the final dentry. + * + * We expect 'base' to be positive and a directory. + */ +int fastcall link_path_walk(const char * name, struct nameidata *nd) +{ + struct path next; + struct inode *inode; + int err; + unsigned int lookup_flags = nd->flags; + + while (*name=='/') + name++; + if (!*name) + goto return_reval; + + inode = nd->dentry->d_inode; + if (current->link_count) + lookup_flags = LOOKUP_FOLLOW; + + /* At this point we know we have a real path component. */ + for(;;) { + unsigned long hash; + struct qstr this; + unsigned int c; + + err = exec_permission_lite(inode, nd); + if (err == -EAGAIN) { + err = permission(inode, MAY_EXEC, nd); + } + if (err) + break; + + this.name = name; + c = *(const unsigned char *)name; + + hash = init_name_hash(); + do { + name++; + hash = partial_name_hash(c, hash); + c = *(const unsigned char *)name; + } while (c && (c != '/')); + this.len = name - (const char *) this.name; + this.hash = end_name_hash(hash); + + /* remove trailing slashes? */ + if (!c) + goto last_component; + while (*++name == '/'); + if (!*name) + goto last_with_slashes; + + /* + * "." and ".." are special - ".." especially so because it has + * to be able to know about the current root directory and + * parent relationships. + */ + if (this.name[0] == '.') switch (this.len) { + default: + break; + case 2: + if (this.name[1] != '.') + break; + follow_dotdot(&nd->mnt, &nd->dentry); + inode = nd->dentry->d_inode; + /* fallthrough */ + case 1: + continue; + } + /* + * See if the low-level filesystem might want + * to use its own hash.. + */ + if (nd->dentry->d_op && nd->dentry->d_op->d_hash) { + err = nd->dentry->d_op->d_hash(nd->dentry, &this); + if (err < 0) + break; + } + nd->flags |= LOOKUP_CONTINUE; + /* This does the actual lookups.. */ + err = do_lookup(nd, &this, &next); + if (err) + break; + /* Check mountpoints.. */ + follow_mount(&next.mnt, &next.dentry); + + err = -ENOENT; + inode = next.dentry->d_inode; + if (!inode) + goto out_dput; + err = -ENOTDIR; + if (!inode->i_op) + goto out_dput; + + if (inode->i_op->follow_link) { + mntget(next.mnt); + nd->flags |= LOOKUP_LINK_NOTLAST; + err = do_follow_link(next.dentry, nd); + nd->flags &= ~LOOKUP_LINK_NOTLAST; + dput(next.dentry); + mntput(next.mnt); + if (err) + goto return_err; + err = -ENOENT; + inode = nd->dentry->d_inode; + if (!inode) + break; + err = -ENOTDIR; + if (!inode->i_op) + break; + } else { + dput(nd->dentry); + nd->mnt = next.mnt; + nd->dentry = next.dentry; + } + err = -ENOTDIR; + if (!inode->i_op->lookup) + break; + continue; + /* here ends the main loop */ + +last_with_slashes: + lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; +last_component: + nd->flags &= ~LOOKUP_CONTINUE; + if (lookup_flags & LOOKUP_PARENT) + goto lookup_parent; + if (this.name[0] == '.') switch (this.len) { + default: + break; + case 2: + if (this.name[1] != '.') + break; + follow_dotdot(&nd->mnt, &nd->dentry); + inode = nd->dentry->d_inode; + /* fallthrough */ + case 1: + nd->flags |= LOOKUP_LAST; + err = revalidate_special(nd); + nd->flags &= ~LOOKUP_LAST; + if (!nd->dentry->d_inode) + err = -ENOENT; + if (err) + goto return_err; + goto return_reval; + } + + if (nd->dentry->d_op && nd->dentry->d_op->d_hash) { + err = nd->dentry->d_op->d_hash(nd->dentry, &this); + if (err < 0) + break; + } + nd->flags |= LOOKUP_LAST; + err = do_lookup(nd, &this, &next); + nd->flags &= ~LOOKUP_LAST; + if (err) + break; + follow_mount(&next.mnt, &next.dentry); + inode = next.dentry->d_inode; + if ((lookup_flags & LOOKUP_FOLLOW) + && inode && inode->i_op && inode->i_op->follow_link) { + mntget(next.mnt); + err = do_follow_link(next.dentry, nd); + dput(next.dentry); + mntput(next.mnt); + if (err) + goto return_err; + inode = nd->dentry->d_inode; + } else { + dput(nd->dentry); + nd->mnt = next.mnt; + nd->dentry = next.dentry; + } + err = -ENOENT; + if (!inode) + break; + if (lookup_flags & LOOKUP_DIRECTORY) { + err = -ENOTDIR; + if (!inode->i_op || !inode->i_op->lookup) + break; + } + goto return_base; +lookup_parent: + nd->last = this; + nd->last_type = LAST_NORM; + if (this.name[0] != '.') + goto return_base; + if (this.len == 1) + nd->last_type = LAST_DOT; + else if (this.len == 2 && this.name[1] == '.') + nd->last_type = LAST_DOTDOT; + else + goto return_base; +return_reval: + /* + * We bypassed the ordinary revalidation routines. + * We may need to check the cached dentry for staleness. + */ + if (nd->dentry && nd->dentry->d_sb && + (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { + err = -ESTALE; + /* Note: we do not d_invalidate() */ + if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd)) + break; + } +return_base: + return 0; +out_dput: + dput(next.dentry); + break; + } + path_release(nd); +return_err: + return err; +} + +int fastcall path_walk(const char * name, struct nameidata *nd) +{ + current->total_link_count = 0; + return link_path_walk(name, nd); +} + +/* SMP-safe */ +/* returns 1 if everything is done */ +static int __emul_lookup_dentry(const char *name, struct nameidata *nd) +{ + if (path_walk(name, nd)) + return 0; /* something went wrong... */ + + if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) { + struct nameidata nd_root; + /* + * NAME was not found in alternate root or it's a directory. Try to find + * it in the normal root: + */ + nd_root.last_type = LAST_ROOT; + nd_root.flags = nd->flags; + memcpy(&nd_root.intent, &nd->intent, sizeof(nd_root.intent)); + read_lock(¤t->fs->lock); + nd_root.mnt = mntget(current->fs->rootmnt); + nd_root.dentry = dget(current->fs->root); + read_unlock(¤t->fs->lock); + if (path_walk(name, &nd_root)) + return 1; + if (nd_root.dentry->d_inode) { + path_release(nd); + nd->dentry = nd_root.dentry; + nd->mnt = nd_root.mnt; + nd->last = nd_root.last; + return 1; + } + path_release(&nd_root); + } + return 1; +} + +void set_fs_altroot(void) +{ + char *emul = __emul_prefix(); + struct nameidata nd; + struct vfsmount *mnt = NULL, *oldmnt; + struct dentry *dentry = NULL, *olddentry; + int err; + + if (!emul) + goto set_it; + err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd); + if (!err) { + mnt = nd.mnt; + dentry = nd.dentry; + } +set_it: + write_lock(¤t->fs->lock); + oldmnt = current->fs->altrootmnt; + olddentry = current->fs->altroot; + current->fs->altrootmnt = mnt; + current->fs->altroot = dentry; + write_unlock(¤t->fs->lock); + if (olddentry) { + dput(olddentry); + mntput(oldmnt); + } +} + +/* SMP-safe */ +static inline int +walk_init_root(const char *name, struct nameidata *nd) +{ + read_lock(¤t->fs->lock); + if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) { + nd->mnt = mntget(current->fs->altrootmnt); + nd->dentry = dget(current->fs->altroot); + read_unlock(¤t->fs->lock); + if (__emul_lookup_dentry(name,nd)) + return 0; + read_lock(¤t->fs->lock); + } + nd->mnt = mntget(current->fs->rootmnt); + nd->dentry = dget(current->fs->root); + read_unlock(¤t->fs->lock); + return 1; +} + +int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd) +{ + nd->last_type = LAST_ROOT; /* if there are only slashes... */ + nd->flags = flags; + + read_lock(¤t->fs->lock); + if (*name=='/') { + if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) { + nd->mnt = mntget(current->fs->altrootmnt); + nd->dentry = dget(current->fs->altroot); + read_unlock(¤t->fs->lock); + if (__emul_lookup_dentry(name,nd)) + return 0; + read_lock(¤t->fs->lock); + } + nd->mnt = mntget(current->fs->rootmnt); + nd->dentry = dget(current->fs->root); + } + else{ + nd->mnt = mntget(current->fs->pwdmnt); + nd->dentry = dget(current->fs->pwd); + } + read_unlock(¤t->fs->lock); + current->total_link_count = 0; + return link_path_walk(name, nd); +} + +/* + * Restricted form of lookup. Doesn't follow links, single-component only, + * needs parent already locked. Doesn't follow mounts. + * SMP-safe. + */ +static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd) +{ + struct dentry * dentry; + struct inode *inode; + int err; + + inode = base->d_inode; + err = permission(inode, MAY_EXEC, nd); + dentry = ERR_PTR(err); + if (err) + goto out; + + /* + * See if the low-level filesystem might want + * to use its own hash.. + */ + if (base->d_op && base->d_op->d_hash) { + err = base->d_op->d_hash(base, name); + dentry = ERR_PTR(err); + if (err < 0) + goto out; + } + + dentry = cached_lookup(base, name, nd); + if (!dentry) { + struct dentry *new = d_alloc(base, name); + dentry = ERR_PTR(-ENOMEM); + if (!new) + goto out; + dentry = inode->i_op->lookup(inode, new, nd); + if (!dentry) + dentry = new; + else + dput(new); + } +out: + return dentry; +} + +struct dentry * lookup_hash(struct qstr *name, struct dentry * base) +{ + return __lookup_hash(name, base, NULL); +} + +/* SMP-safe */ +struct dentry * lookup_one_len_it(const char * name, struct dentry * base, int len, struct nameidata *nd) +{ + unsigned long hash; + struct qstr this; + unsigned int c; + + this.name = name; + this.len = len; + if (!len) + goto access; + + hash = init_name_hash(); + while (len--) { + c = *(const unsigned char *)name++; + if (c == '/' || c == '\0') + goto access; + hash = partial_name_hash(c, hash); + } + this.hash = end_name_hash(hash); + + return __lookup_hash(&this, base, nd); +access: + return ERR_PTR(-EACCES); +} + +struct dentry * lookup_one_len(const char * name, struct dentry * base, int len) +{ + return lookup_one_len_it(name, base, len, NULL); +} + +/* + * namei() + * + * is used by most simple commands to get the inode of a specified name. + * Open, link etc use their own routines, but this is enough for things + * like 'chmod' etc. + * + * namei exists in two versions: namei/lnamei. The only difference is + * that namei follows links, while lnamei does not. + * SMP-safe + */ +int fastcall __user_walk_it(const char __user *name, unsigned flags, + struct nameidata *nd, const char **pname) +{ + char *tmp = getname(name); + int err = PTR_ERR(tmp); + + if (!IS_ERR(tmp)) { + err = path_lookup(tmp, flags, nd); + if (!pname) + putname(tmp); + } + if (pname) + *pname = tmp; + return err; +} + +int fastcall __user_walk(const char __user *name, unsigned flags, + struct nameidata *nd, const char **pname) +{ + intent_init(&nd->intent, IT_LOOKUP); + return __user_walk_it(name, flags, nd, pname); +} + +/* + * It's inline, so penalty for filesystems that don't use sticky bit is + * minimal. + */ +static inline int check_sticky(struct inode *dir, struct inode *inode) +{ + if (!(dir->i_mode & S_ISVTX)) + return 0; + if (inode->i_uid == current->fsuid) + return 0; + if (dir->i_uid == current->fsuid) + return 0; + return !capable(CAP_FOWNER); +} + +/* + * Check whether we can remove a link victim from directory dir, check + * whether the type of victim is right. + * 1. We can't do it if dir is read-only (done in permission()) + * 2. We should have write and exec permissions on dir + * 3. We can't remove anything from append-only dir + * 4. We can't do anything with immutable dir (done in permission()) + * 5. If the sticky bit on dir is set we should either + * a. be owner of dir, or + * b. be owner of victim, or + * c. have CAP_FOWNER capability + * 6. If the victim is append-only or immutable we can't do antyhing with + * links pointing to it. + * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. + * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. + * 9. We can't remove a root or mountpoint. + * 10. We don't allow removal of NFS sillyrenamed files; it's handled by + * nfs_async_unlink(). + */ +static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir) +{ + int error; + if (!victim->d_inode || victim->d_parent->d_inode != dir) + return -ENOENT; + error = permission(dir,MAY_WRITE | MAY_EXEC, NULL); + if (error) + return error; + if (IS_APPEND(dir)) + return -EPERM; + if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| + IS_IMMUTABLE(victim->d_inode)) + return -EPERM; + if (isdir) { + if (!S_ISDIR(victim->d_inode->i_mode)) + return -ENOTDIR; + if (IS_ROOT(victim)) + return -EBUSY; + } else if (S_ISDIR(victim->d_inode->i_mode)) + return -EISDIR; + if (IS_DEADDIR(dir)) + return -ENOENT; + if (victim->d_flags & DCACHE_NFSFS_RENAMED) + return -EBUSY; + return 0; +} + +/* Check whether we can create an object with dentry child in directory + * dir. + * 1. We can't do it if child already exists (open has special treatment for + * this case, but since we are inlined it's OK) + * 2. We can't do it if dir is read-only (done in permission()) + * 3. We should have write and exec permissions on dir + * 4. We can't do it if dir is immutable (done in permission()) + */ +static inline int may_create(struct inode *dir, struct dentry *child, + struct nameidata *nd) +{ + if (child->d_inode) + return -EEXIST; + if (IS_DEADDIR(dir)) + return -ENOENT; + return permission(dir,MAY_WRITE | MAY_EXEC, nd); +} + +/* + * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security + * reasons. + * + * O_DIRECTORY translates into forcing a directory lookup. + */ +static inline int lookup_flags(unsigned int f) +{ + unsigned long retval = LOOKUP_FOLLOW; + + if (f & O_NOFOLLOW) + retval &= ~LOOKUP_FOLLOW; + + if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) + retval &= ~LOOKUP_FOLLOW; + + if (f & O_DIRECTORY) + retval |= LOOKUP_DIRECTORY; + + return retval; +} + +/* + * p1 and p2 should be directories on the same fs. + */ +struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) +{ + struct dentry *p; + + if (p1 == p2) { + down(&p1->d_inode->i_sem); + return NULL; + } + + down(&p1->d_inode->i_sb->s_vfs_rename_sem); + + for (p = p1; p->d_parent != p; p = p->d_parent) { + if (p->d_parent == p2) { + down(&p2->d_inode->i_sem); + down(&p1->d_inode->i_sem); + return p; + } + } + + for (p = p2; p->d_parent != p; p = p->d_parent) { + if (p->d_parent == p1) { + down(&p1->d_inode->i_sem); + down(&p2->d_inode->i_sem); + return p; + } + } + + down(&p1->d_inode->i_sem); + down(&p2->d_inode->i_sem); + return NULL; +} + +void unlock_rename(struct dentry *p1, struct dentry *p2) +{ + up(&p1->d_inode->i_sem); + if (p1 != p2) { + up(&p2->d_inode->i_sem); + up(&p1->d_inode->i_sb->s_vfs_rename_sem); + } +} + +int vfs_create(struct inode *dir, struct dentry *dentry, int mode, + struct nameidata *nd) +{ + int error = may_create(dir, dentry, nd); + + if (error) + return error; + + if (!dir->i_op || !dir->i_op->create) + return -EACCES; /* shouldn't it be ENOSYS? */ + mode &= S_IALLUGO; + mode |= S_IFREG; + error = security_inode_create(dir, dentry, mode); + if (error) + return error; + DQUOT_INIT(dir); + error = dir->i_op->create(dir, dentry, mode, nd); + if (!error) { + inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_FILE, dentry->d_name.name); + security_inode_post_create(dir, dentry, mode); + } + return error; +} + +int may_open(struct nameidata *nd, int acc_mode, int flag) +{ + struct dentry *dentry = nd->dentry; + struct inode *inode = dentry->d_inode; + int error; + + if (!inode) + return -ENOENT; + + if (S_ISLNK(inode->i_mode)) + return -ELOOP; + + if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE)) + return -EISDIR; + + error = permission(inode, acc_mode, nd); + if (error) + return error; + + /* + * FIFO's, sockets and device files are special: they don't + * actually live on the filesystem itself, and as such you + * can write to them even if the filesystem is read-only. + */ + if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { + flag &= ~O_TRUNC; + } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) { + if (nd->mnt->mnt_flags & MNT_NODEV) + return -EACCES; + + flag &= ~O_TRUNC; + } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE)) + return -EROFS; + /* + * An append-only file must be opened in append mode for writing. + */ + if (IS_APPEND(inode)) { + if ((flag & FMODE_WRITE) && !(flag & O_APPEND)) + return -EPERM; + if (flag & O_TRUNC) + return -EPERM; + } + + /* O_NOATIME can only be set by the owner or superuser */ + if (flag & O_NOATIME) + if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER)) + return -EPERM; + + /* + * Ensure there are no outstanding leases on the file. + */ + error = break_lease(inode, flag); + if (error) + return error; + + if (flag & O_TRUNC) { + error = get_write_access(inode); + if (error) + return error; + + /* + * Refuse to truncate files with mandatory locks held on them. + */ + error = locks_verify_locked(inode); + if (!error) { + DQUOT_INIT(inode); + + error = do_truncate(dentry, 0, 1); + } + put_write_access(inode); + if (error) + return error; + } else + if (flag & FMODE_WRITE) + DQUOT_INIT(inode); + + return 0; +} + +/* + * open_namei() + * + * namei for open - this is in fact almost the whole open-routine. + * + * Note that the low bits of "flag" aren't the same as in the open + * system call - they are 00 - no permissions needed + * 01 - read permission needed + * 10 - write permission needed + * 11 - read/write permissions needed + * which is a lot more logical, and also allows the "no perm" needed + * for symlinks (where the permissions are checked later). + * SMP-safe + */ +int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd) +{ + int acc_mode, error = 0; + struct dentry *dentry; + struct dentry *dir; + int count = 0; + + acc_mode = ACC_MODE(flag); + + /* Allow the LSM permission hook to distinguish append + access from general write access. */ + if (flag & O_APPEND) + acc_mode |= MAY_APPEND; + + /* Fill in the open() intent data */ + nd->intent.it_flags = flag; + nd->intent.it_create_mode = mode; + + /* + * The simplest case - just a plain lookup. + */ + if (!(flag & O_CREAT)) { + error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd); + if (error) + return error; + goto ok; + } + + /* + * Create - we need to know the parent. + */ + nd->intent.it_op |= IT_CREAT; + error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd); + if (error) + return error; + + /* + * We have the parent and last component. First of all, check + * that we are not asked to creat(2) an obvious directory - that + * will not do. + */ + error = -EISDIR; + if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len]) + goto exit; + + dir = nd->dentry; + nd->flags &= ~LOOKUP_PARENT; + down(&dir->d_inode->i_sem); + nd->flags |= LOOKUP_LAST; + dentry = __lookup_hash(&nd->last, nd->dentry, nd); + nd->flags &= ~LOOKUP_LAST; + +do_last: + error = PTR_ERR(dentry); + if (IS_ERR(dentry)) { + up(&dir->d_inode->i_sem); + goto exit; + } + + /* Negative dentry, just create the file */ + if (!dentry->d_inode) { + if (!IS_POSIXACL(dir->d_inode)) + mode &= ~current->fs->umask; + error = vfs_create(dir->d_inode, dentry, mode, nd); + up(&dir->d_inode->i_sem); + dput(nd->dentry); + nd->dentry = dentry; + if (error) + goto exit; + /* Don't check for write permission, don't truncate */ + acc_mode = 0; + flag &= ~O_TRUNC; + goto ok; + } + + /* + * It already exists. + */ + up(&dir->d_inode->i_sem); + + error = -EEXIST; + if (flag & O_EXCL) + goto exit_dput; + + if (d_mountpoint(dentry)) { + error = -ELOOP; + if (flag & O_NOFOLLOW) + goto exit_dput; + while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry)); + } + error = -ENOENT; + if (!dentry->d_inode) + goto exit_dput; + if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link) + goto do_link; + + dput(nd->dentry); + nd->dentry = dentry; + error = -EISDIR; + if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) + goto exit; +ok: + error = may_open(nd, acc_mode, flag); + if (error) + goto exit; + return 0; + +exit_dput: + dput(dentry); +exit: + path_release(nd); + return error; + +do_link: + error = -ELOOP; + if (flag & O_NOFOLLOW) + goto exit_dput; + /* + * This is subtle. Instead of calling do_follow_link() we do the + * thing by hands. The reason is that this way we have zero link_count + * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT. + * After that we have the parent and last component, i.e. + * we are in the same situation as after the first path_walk(). + * Well, almost - if the last component is normal we get its copy + * stored in nd->last.name and we will have to putname() it when we + * are done. Procfs-like symlinks just set LAST_BIND. + */ + nd->flags |= LOOKUP_PARENT; + error = security_inode_follow_link(dentry, nd); + if (error) + goto exit_dput; + touch_atime(nd->mnt, dentry); + error = dentry->d_inode->i_op->follow_link(dentry, nd); + dput(dentry); + if (error) + return error; + nd->flags &= ~LOOKUP_PARENT; + if (nd->last_type == LAST_BIND) { + dentry = nd->dentry; + goto ok; + } + error = -EISDIR; + if (nd->last_type != LAST_NORM) + goto exit; + if (nd->last.name[nd->last.len]) { + putname(nd->last.name); + goto exit; + } + error = -ELOOP; + if (count++==32) { + putname(nd->last.name); + goto exit; + } + dir = nd->dentry; + down(&dir->d_inode->i_sem); + nd->flags |= LOOKUP_LAST; + dentry = __lookup_hash(&nd->last, nd->dentry, nd); + nd->flags &= ~LOOKUP_LAST; + putname(nd->last.name); + goto do_last; +} + +/** + * lookup_create - lookup a dentry, creating it if it doesn't exist + * @nd: nameidata info + * @is_dir: directory flag + * + * Simple function to lookup and return a dentry and create it + * if it doesn't exist. Is SMP-safe. + */ +struct dentry *lookup_create(struct nameidata *nd, int is_dir) +{ + struct dentry *dentry; + + down(&nd->dentry->d_inode->i_sem); + dentry = ERR_PTR(-EEXIST); + if (nd->last_type != LAST_NORM) + goto fail; + nd->flags &= ~LOOKUP_PARENT; + dentry = lookup_hash(&nd->last, nd->dentry); + if (IS_ERR(dentry)) + goto fail; + if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode) + goto enoent; + return dentry; +enoent: + dput(dentry); + dentry = ERR_PTR(-ENOENT); +fail: + return dentry; +} + +int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) +{ + int error = may_create(dir, dentry, NULL); + + if (error) + return error; + + if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) + return -EPERM; + + if (!dir->i_op || !dir->i_op->mknod) + return -EPERM; + + error = security_inode_mknod(dir, dentry, mode, dev); + if (error) + return error; + + DQUOT_INIT(dir); + error = dir->i_op->mknod(dir, dentry, mode, dev); + if (!error) { + inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_FILE, dentry->d_name.name); + security_inode_post_mknod(dir, dentry, mode, dev); + } + return error; +} + +asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev) +{ + int error = 0; + char * tmp; + struct dentry * dentry; + struct nameidata nd; + intent_init(&nd.intent, IT_LOOKUP); + + if (S_ISDIR(mode)) + return -EPERM; + tmp = getname(filename); + if (IS_ERR(tmp)) + return PTR_ERR(tmp); + + FSHOOK_BEGIN(mknod, error, .path = tmp, .mode = mode, .dev = dev) + + error = path_lookup(tmp, LOOKUP_PARENT, &nd); + if (error) + goto out; + + if (nd.dentry->d_inode->i_op->mknod_raw) { + struct inode_operations *op = nd.dentry->d_inode->i_op; + error = op->mknod_raw(&nd, mode, dev); + /* the file system wants to use normal vfs path now */ + if (error != -EOPNOTSUPP) + goto out2; + } + + dentry = lookup_create(&nd, 0); + error = PTR_ERR(dentry); + + if (!IS_POSIXACL(nd.dentry->d_inode)) + mode &= ~current->fs->umask; + if (!IS_ERR(dentry)) { + switch (mode & S_IFMT) { + case 0: case S_IFREG: + error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd); + break; + case S_IFCHR: case S_IFBLK: + error = vfs_mknod(nd.dentry->d_inode,dentry,mode, + new_decode_dev(dev)); + break; + case S_IFIFO: case S_IFSOCK: + error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0); + break; + case S_IFDIR: + error = -EPERM; + break; + default: + error = -EINVAL; + } + dput(dentry); + } + up(&nd.dentry->d_inode->i_sem); +out2: + path_release(&nd); +out: + putname(tmp); + + FSHOOK_END(mknod, error) + + return error; +} + +int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) +{ + int error = may_create(dir, dentry, NULL); + + if (error) + return error; + + if (!dir->i_op || !dir->i_op->mkdir) + return -EPERM; + + mode &= (S_IRWXUGO|S_ISVTX); + error = security_inode_mkdir(dir, dentry, mode); + if (error) + return error; + + DQUOT_INIT(dir); + error = dir->i_op->mkdir(dir, dentry, mode); + if (!error) { + inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_SUBDIR, dentry->d_name.name); + security_inode_post_mkdir(dir,dentry, mode); + } + return error; +} + +asmlinkage long sys_mkdir(const char __user * pathname, int mode) +{ + int error = 0; + char * tmp; + + tmp = getname(pathname); + error = PTR_ERR(tmp); + if (!IS_ERR(tmp)) { + + FSHOOK_BEGIN(mkdir, error, .dirname = tmp, .mode = mode) + + struct dentry *dentry; + struct nameidata nd; + intent_init(&nd.intent, IT_LOOKUP); + + error = path_lookup(tmp, LOOKUP_PARENT, &nd); + if (error) + goto out; + if (nd.dentry->d_inode->i_op->mkdir_raw) { + struct inode_operations *op = nd.dentry->d_inode->i_op; + error = op->mkdir_raw(&nd, mode); + /* the file system wants to use normal vfs path now */ + if (error != -EOPNOTSUPP) + goto out2; + } + dentry = lookup_create(&nd, 1); + error = PTR_ERR(dentry); + if (!IS_ERR(dentry)) { + if (!IS_POSIXACL(nd.dentry->d_inode)) + mode &= ~current->fs->umask; + error = vfs_mkdir(nd.dentry->d_inode, dentry, mode); + dput(dentry); + } + up(&nd.dentry->d_inode->i_sem); +out2: + path_release(&nd); +out: + + FSHOOK_END(mkdir, error) + + putname(tmp); + } + + return error; +} + +/* + * We try to drop the dentry early: we should have + * a usage count of 2 if we're the only user of this + * dentry, and if that is true (possibly after pruning + * the dcache), then we drop the dentry now. + * + * A low-level filesystem can, if it choses, legally + * do a + * + * if (!d_unhashed(dentry)) + * return -EBUSY; + * + * if it cannot handle the case of removing a directory + * that is still in use by something else.. + */ +static void d_unhash(struct dentry *dentry) +{ + dget(dentry); + spin_lock(&dcache_lock); + switch (atomic_read(&dentry->d_count)) { + default: + spin_unlock(&dcache_lock); + shrink_dcache_parent(dentry); + spin_lock(&dcache_lock); + if (atomic_read(&dentry->d_count) != 2) + break; + case 2: + __d_drop(dentry); + } + spin_unlock(&dcache_lock); +} + +int vfs_rmdir(struct inode *dir, struct dentry *dentry) +{ + int error = may_delete(dir, dentry, 1); + + if (error) + return error; + + if (!dir->i_op || !dir->i_op->rmdir) + return -EPERM; + + DQUOT_INIT(dir); + + down(&dentry->d_inode->i_sem); + d_unhash(dentry); + if (d_mountpoint(dentry)) + error = -EBUSY; + else { + error = security_inode_rmdir(dir, dentry); + if (!error) { + error = dir->i_op->rmdir(dir, dentry); + if (!error) + dentry->d_inode->i_flags |= S_DEAD; + } + } + up(&dentry->d_inode->i_sem); + if (!error) { + inode_dir_notify(dir, DN_DELETE); + inotify_inode_queue_event(dir, IN_DELETE_SUBDIR, dentry->d_name.name); + inotify_inode_queue_event(dentry->d_inode, IN_DELETE_SELF, NULL); + inotify_inode_is_dead (dentry->d_inode); + d_delete(dentry); + } + dput(dentry); + + return error; +} + +asmlinkage long sys_rmdir(const char __user * pathname) +{ + int error = 0; + char * name; + struct dentry *dentry; + struct nameidata nd; + intent_init(&nd.intent, IT_LOOKUP); + + name = getname(pathname); + if(IS_ERR(name)) + return PTR_ERR(name); + + FSHOOK_BEGIN(rmdir, error, .dirname = name) + + error = path_lookup(name, LOOKUP_PARENT, &nd); + if (error) + goto exit; + + switch(nd.last_type) { + case LAST_DOTDOT: + error = -ENOTEMPTY; + goto exit1; + case LAST_DOT: + error = -EINVAL; + goto exit1; + case LAST_ROOT: + error = -EBUSY; + goto exit1; + } + if (nd.dentry->d_inode->i_op->rmdir_raw) { + struct inode_operations *op = nd.dentry->d_inode->i_op; + + error = op->rmdir_raw(&nd); + /* the file system wants to use normal vfs path now */ + if (error != -EOPNOTSUPP) + goto exit1; + } + down(&nd.dentry->d_inode->i_sem); + dentry = lookup_hash(&nd.last, nd.dentry); + error = PTR_ERR(dentry); + if (!IS_ERR(dentry)) { + error = vfs_rmdir(nd.dentry->d_inode, dentry); + dput(dentry); + } + up(&nd.dentry->d_inode->i_sem); +exit1: + path_release(&nd); +exit: + + FSHOOK_END(rmdir, error) + + putname(name); + return error; +} + +int vfs_unlink(struct inode *dir, struct dentry *dentry) +{ + int error = may_delete(dir, dentry, 0); + + if (error) + return error; + + if (!dir->i_op || !dir->i_op->unlink) + return -EPERM; + + DQUOT_INIT(dir); + + down(&dentry->d_inode->i_sem); + if (d_mountpoint(dentry)) + error = -EBUSY; + else { + error = security_inode_unlink(dir, dentry); + if (!error) + error = dir->i_op->unlink(dir, dentry); + } + up(&dentry->d_inode->i_sem); + + /* We don't d_delete() NFS sillyrenamed files--they still exist. */ + if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { + inode_dir_notify(dir, DN_DELETE); + inotify_inode_queue_event(dir, IN_DELETE_FILE, dentry->d_name.name); + inotify_inode_queue_event(dentry->d_inode, IN_DELETE_SELF, NULL); + inotify_inode_is_dead (dentry->d_inode); + d_delete(dentry); + } + return error; +} + +/* + * Make sure that the actual truncation of the file will occur outside its + * directory's i_sem. Truncate can take a long time if there is a lot of + * writeout happening, and we don't want to prevent access to the directory + * while waiting on the I/O. + */ +asmlinkage long sys_unlink(const char __user * pathname) +{ + int error = 0; + char * name; + struct dentry *dentry; + struct nameidata nd; + struct inode *inode = NULL; + intent_init(&nd.intent, IT_LOOKUP); + + name = getname(pathname); + if(IS_ERR(name)) + return PTR_ERR(name); + + FSHOOK_BEGIN(unlink, error, .filename = name) + + error = path_lookup(name, LOOKUP_PARENT, &nd); + if (error) + goto exit; + error = -EISDIR; + if (nd.last_type != LAST_NORM) + goto exit1; + if (nd.dentry->d_inode->i_op->unlink_raw) { + struct inode_operations *op = nd.dentry->d_inode->i_op; + error = op->unlink_raw(&nd); + /* the file system wants to use normal vfs path now */ + if (error != -EOPNOTSUPP) + goto exit1; + } + down(&nd.dentry->d_inode->i_sem); + dentry = lookup_hash(&nd.last, nd.dentry); + error = PTR_ERR(dentry); + if (!IS_ERR(dentry)) { + /* Why not before? Because we want correct error value */ + if (nd.last.name[nd.last.len]) + goto slashes; + inode = dentry->d_inode; + if (inode) + atomic_inc(&inode->i_count); + error = vfs_unlink(nd.dentry->d_inode, dentry); + exit2: + dput(dentry); + } + up(&nd.dentry->d_inode->i_sem); +exit1: + path_release(&nd); +exit: + + FSHOOK_END(unlink, error) + + putname(name); + + if (inode) + iput(inode); /* truncate the inode here */ + return error; + +slashes: + error = !dentry->d_inode ? -ENOENT : + S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; + goto exit2; +} + +int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) +{ + int error = may_create(dir, dentry, NULL); + + if (error) + return error; + + if (!dir->i_op || !dir->i_op->symlink) + return -EPERM; + + error = security_inode_symlink(dir, dentry, oldname); + if (error) + return error; + + DQUOT_INIT(dir); + error = dir->i_op->symlink(dir, dentry, oldname); + if (!error) { + inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_FILE, dentry->d_name.name); + security_inode_post_symlink(dir, dentry, oldname); + } + return error; +} + +asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname) +{ + int error = 0; + char * from; + char * to; + + from = getname(oldname); + if(IS_ERR(from)) + return PTR_ERR(from); + to = getname(newname); + error = PTR_ERR(to); + if (!IS_ERR(to)) { + + FSHOOK_BEGIN(symlink, error, .oldpath = from, .newpath = to) + + struct dentry *dentry; + struct nameidata nd; + intent_init(&nd.intent, IT_LOOKUP); + + error = path_lookup(to, LOOKUP_PARENT, &nd); + if (error) + goto out; + if (nd.dentry->d_inode->i_op->symlink_raw) { + struct inode_operations *op = nd.dentry->d_inode->i_op; + error = op->symlink_raw(&nd, from); + /* the file system wants to use normal vfs path now */ + if (error != -EOPNOTSUPP) + goto out2; + } + dentry = lookup_create(&nd, 0); + error = PTR_ERR(dentry); + if (!IS_ERR(dentry)) { + error = vfs_symlink(nd.dentry->d_inode, dentry, from); + dput(dentry); + } + up(&nd.dentry->d_inode->i_sem); +out2: + path_release(&nd); +out: + + FSHOOK_END(symlink, error) + + putname(to); + } + putname(from); + return error; +} + +int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) +{ + struct inode *inode = old_dentry->d_inode; + int error; + + if (!inode) + return -ENOENT; + + error = may_create(dir, new_dentry, NULL); + if (error) + return error; + + if (dir->i_sb != inode->i_sb) + return -EXDEV; + + /* + * A link to an append-only or immutable file cannot be created. + */ + if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) + return -EPERM; + if (!dir->i_op || !dir->i_op->link) + return -EPERM; + if (S_ISDIR(old_dentry->d_inode->i_mode)) + return -EPERM; + + error = security_inode_link(old_dentry, dir, new_dentry); + if (error) + return error; + + down(&old_dentry->d_inode->i_sem); + DQUOT_INIT(dir); + error = dir->i_op->link(old_dentry, dir, new_dentry); + up(&old_dentry->d_inode->i_sem); + if (!error) { + inode_dir_notify(dir, DN_CREATE); + inotify_inode_queue_event(dir, IN_CREATE_FILE, new_dentry->d_name.name); + security_inode_post_link(old_dentry, dir, new_dentry); + } + return error; +} + +/* + * Hardlinks are often used in delicate situations. We avoid + * security-related surprises by not following symlinks on the + * newname. --KAB + * + * We don't follow them on the oldname either to be compatible + * with linux 2.0, and to avoid hard-linking to directories + * and other special files. --ADM + */ +asmlinkage long sys_link(const char __user * oldname, const char __user * newname) +{ + struct dentry *new_dentry; + struct nameidata nd, old_nd; + int error; + char * to; + intent_init(&nd.intent, IT_LOOKUP); + intent_init(&old_nd.intent, IT_LOOKUP); + + to = getname(newname); + if (IS_ERR(to)) + return PTR_ERR(to); + + FSHOOK_BEGIN_USER_PATH_WALK_LINK(link, + error, + oldname, + old_nd, + oldpath, + .newpath = to) + + error = path_lookup(to, LOOKUP_PARENT, &nd); + if (error) + goto out; + error = -EXDEV; + if (old_nd.mnt != nd.mnt) + goto out_release; + if (nd.dentry->d_inode->i_op->link_raw) { + struct inode_operations *op = nd.dentry->d_inode->i_op; + error = op->link_raw(&old_nd, &nd); + /* the file system wants to use normal vfs path now */ + if (error != -EOPNOTSUPP) + goto out_release; + } + new_dentry = lookup_create(&nd, 0); + error = PTR_ERR(new_dentry); + if (!IS_ERR(new_dentry)) { + error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry); + dput(new_dentry); + } + up(&nd.dentry->d_inode->i_sem); +out_release: + path_release(&nd); +out: + path_release(&old_nd); + + FSHOOK_END_USER_WALK(link, error, oldpath) + + putname(to); + + return error; +} + +/* + * The worst of all namespace operations - renaming directory. "Perverted" + * doesn't even start to describe it. Somebody in UCB had a heck of a trip... + * Problems: + * a) we can get into loop creation. Check is done in is_subdir(). + * b) race potential - two innocent renames can create a loop together. + * That's where 4.4 screws up. Current fix: serialization on + * sb->s_vfs_rename_sem. We might be more accurate, but that's another + * story. + * c) we have to lock _three_ objects - parents and victim (if it exists). + * And that - after we got ->i_sem on parents (until then we don't know + * whether the target exists). Solution: try to be smart with locking + * order for inodes. We rely on the fact that tree topology may change + * only under ->s_vfs_rename_sem _and_ that parent of the object we + * move will be locked. Thus we can rank directories by the tree + * (ancestors first) and rank all non-directories after them. + * That works since everybody except rename does "lock parent, lookup, + * lock child" and rename is under ->s_vfs_rename_sem. + * HOWEVER, it relies on the assumption that any object with ->lookup() + * has no more than 1 dentry. If "hybrid" objects will ever appear, + * we'd better make sure that there's no link(2) for them. + * d) some filesystems don't support opened-but-unlinked directories, + * either because of layout or because they are not ready to deal with + * all cases correctly. The latter will be fixed (taking this sort of + * stuff into VFS), but the former is not going away. Solution: the same + * trick as in rmdir(). + * e) conversion from fhandle to dentry may come in the wrong moment - when + * we are removing the target. Solution: we will have to grab ->i_sem + * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on + * ->i_sem on parents, which works but leads to some truely excessive + * locking]. + */ +int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, + struct inode *new_dir, struct dentry *new_dentry) +{ + int error = 0; + struct inode *target; + + /* + * If we are going to change the parent - check write permissions, + * we'll need to flip '..'. + */ + if (new_dir != old_dir) { + error = permission(old_dentry->d_inode, MAY_WRITE, NULL); + if (error) + return error; + } + + error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); + if (error) + return error; + + target = new_dentry->d_inode; + if (target) { + down(&target->i_sem); + d_unhash(new_dentry); + } + if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) + error = -EBUSY; + else + error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); + if (target) { + if (!error) + target->i_flags |= S_DEAD; + up(&target->i_sem); + if (d_unhashed(new_dentry)) + d_rehash(new_dentry); + dput(new_dentry); + } + if (!error) { + d_move(old_dentry,new_dentry); + security_inode_post_rename(old_dir, old_dentry, + new_dir, new_dentry); + } + return error; +} + +int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, + struct inode *new_dir, struct dentry *new_dentry) +{ + struct inode *target; + int error; + + error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); + if (error) + return error; + + dget(new_dentry); + target = new_dentry->d_inode; + if (target) + down(&target->i_sem); + if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) + error = -EBUSY; + else + error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); + if (!error) { + /* The following d_move() should become unconditional */ + if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME)) + d_move(old_dentry, new_dentry); + security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry); + } + if (target) + up(&target->i_sem); + dput(new_dentry); + return error; +} + +int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, + struct inode *new_dir, struct dentry *new_dentry) +{ + int error; + int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); + + if (old_dentry->d_inode == new_dentry->d_inode) + return 0; + + error = may_delete(old_dir, old_dentry, is_dir); + if (error) + return error; + + if (!new_dentry->d_inode) + error = may_create(new_dir, new_dentry, NULL); + else + error = may_delete(new_dir, new_dentry, is_dir); + if (error) + return error; + + if (!old_dir->i_op || !old_dir->i_op->rename) + return -EPERM; + + DQUOT_INIT(old_dir); + DQUOT_INIT(new_dir); + + if (is_dir) + error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); + else + error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); + if (!error) { + if (old_dir == new_dir) { + inode_dir_notify(old_dir, DN_RENAME); + } else { + inode_dir_notify(old_dir, DN_DELETE); + inode_dir_notify(new_dir, DN_CREATE); + } + + inotify_inode_queue_event (old_dir, IN_MOVED_FROM, old_dentry->d_name.name); + inotify_inode_queue_event (new_dir, IN_MOVED_TO, new_dentry->d_name.name); + } + return error; +} + +static inline int do_rename(const char * oldname, const char * newname) +{ + int error = 0; + struct dentry * old_dir, * new_dir; + struct dentry * old_dentry, *new_dentry; + struct dentry * trap; + struct nameidata oldnd, newnd; + intent_init(&oldnd.intent, IT_LOOKUP); + intent_init(&newnd.intent, IT_LOOKUP); + + error = path_lookup(oldname, LOOKUP_PARENT, &oldnd); + if (error) + goto exit; + + error = path_lookup(newname, LOOKUP_PARENT, &newnd); + if (error) + goto exit1; + + error = -EXDEV; + if (oldnd.mnt != newnd.mnt) + goto exit2; + + old_dir = oldnd.dentry; + error = -EBUSY; + if (oldnd.last_type != LAST_NORM) + goto exit2; + + new_dir = newnd.dentry; + if (newnd.last_type != LAST_NORM) + goto exit2; + + if (old_dir->d_inode->i_op->rename_raw) { + error = old_dir->d_inode->i_op->rename_raw(&oldnd, &newnd); + /* the file system wants to use normal vfs path now */ + if (error != -EOPNOTSUPP) + goto exit2; + } + + trap = lock_rename(new_dir, old_dir); + + old_dentry = lookup_hash(&oldnd.last, old_dir); + error = PTR_ERR(old_dentry); + if (IS_ERR(old_dentry)) + goto exit3; + /* source must exist */ + error = -ENOENT; + if (!old_dentry->d_inode) + goto exit4; + /* unless the source is a directory trailing slashes give -ENOTDIR */ + if (!S_ISDIR(old_dentry->d_inode->i_mode)) { + error = -ENOTDIR; + if (oldnd.last.name[oldnd.last.len]) + goto exit4; + if (newnd.last.name[newnd.last.len]) + goto exit4; + } + /* source should not be ancestor of target */ + error = -EINVAL; + if (old_dentry == trap) + goto exit4; + new_dentry = lookup_hash(&newnd.last, new_dir); + error = PTR_ERR(new_dentry); + if (IS_ERR(new_dentry)) + goto exit4; + /* target should not be an ancestor of source */ + error = -ENOTEMPTY; + if (new_dentry == trap) + goto exit5; + + error = vfs_rename(old_dir->d_inode, old_dentry, new_dir->d_inode, new_dentry); +exit5: + dput(new_dentry); +exit4: + dput(old_dentry); +exit3: + unlock_rename(new_dir, old_dir); +exit2: + path_release(&newnd); +exit1: + path_release(&oldnd); +exit: + return error; +} + +asmlinkage long sys_rename(const char __user * oldname, const char __user * newname) +{ + int error; + char * from; + char * to; + + from = getname(oldname); + if(IS_ERR(from)) + return PTR_ERR(from); + to = getname(newname); + error = PTR_ERR(to); + if (!IS_ERR(to)) { + + FSHOOK_BEGIN(rename, error, .oldpath = from, .newpath = to) + + error = do_rename(from,to); + + FSHOOK_END(rename, error) + + putname(to); + } + putname(from); + return error; +} + +int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) +{ + int len; + + len = PTR_ERR(link); + if (IS_ERR(link)) + goto out; + + len = strlen(link); + if (len > (unsigned) buflen) + len = buflen; + if (copy_to_user(buffer, link, len)) + len = -EFAULT; +out: + return len; +} + +static inline int +__vfs_follow_link(struct nameidata *nd, const char *link) +{ + int res = 0; + struct lookup_intent it = nd->intent; + char *name; + + if (IS_ERR(link)) + goto fail; + + if (*link == '/') { + path_release(nd); + if (!walk_init_root(link, nd)) + /* weird __emul_prefix() stuff did it */ + goto out; + } + + intent_init(&nd->intent, it.it_op); + nd->intent.it_flags = it.it_flags; + nd->intent.it_create_mode = it.it_create_mode; + res = link_path_walk(link, nd); +out: + if (current->link_count || res || nd->last_type!=LAST_NORM) + return res; + /* + * If it is an iterative symlinks resolution in open_namei() we + * have to copy the last component. And all that crap because of + * bloody create() on broken symlinks. Furrfu... + */ + name = __getname(); + if (unlikely(!name)) { + path_release(nd); + return -ENOMEM; + } + strcpy(name, nd->last.name); + nd->last.name = name; + return 0; +fail: + path_release(nd); + return PTR_ERR(link); +} + +int vfs_follow_link(struct nameidata *nd, const char *link) +{ + return __vfs_follow_link(nd, link); +} + +/* get the link contents into pagecache */ +static char *page_getlink(struct dentry * dentry, struct page **ppage) +{ + struct page * page; + struct address_space *mapping = dentry->d_inode->i_mapping; + page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage, + NULL); + if (IS_ERR(page)) + goto sync_fail; + wait_on_page_locked(page); + if (!PageUptodate(page)) + goto async_fail; + *ppage = page; + return kmap(page); + +async_fail: + page_cache_release(page); + return ERR_PTR(-EIO); + +sync_fail: + return (char*)page; +} + +int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) +{ + struct page *page = NULL; + char *s = page_getlink(dentry, &page); + int res = vfs_readlink(dentry,buffer,buflen,s); + if (page) { + kunmap(page); + page_cache_release(page); + } + return res; +} + +int page_follow_link(struct dentry *dentry, struct nameidata *nd) +{ + struct page *page = NULL; + char *s = page_getlink(dentry, &page); + int res = __vfs_follow_link(nd, s); + if (page) { + kunmap(page); + page_cache_release(page); + } + return res; +} + +int page_symlink(struct inode *inode, const char *symname, int len) +{ + struct address_space *mapping = inode->i_mapping; + struct page *page = grab_cache_page(mapping, 0); + int err = -ENOMEM; + char *kaddr; + + if (!page) + goto fail; + err = mapping->a_ops->prepare_write(NULL, page, 0, len-1); + if (err) + goto fail_map; + kaddr = kmap_atomic(page, KM_USER0); + memcpy(kaddr, symname, len-1); + kunmap_atomic(kaddr, KM_USER0); + mapping->a_ops->commit_write(NULL, page, 0, len-1); + /* + * Notice that we are _not_ going to block here - end of page is + * unmapped, so this will only try to map the rest of page, see + * that it is unmapped (typically even will not look into inode - + * ->i_size will be enough for everything) and zero it out. + * OTOH it's obviously correct and should make the page up-to-date. + */ + if (!PageUptodate(page)) { + err = mapping->a_ops->readpage(NULL, page); + wait_on_page_locked(page); + } else { + unlock_page(page); + } + page_cache_release(page); + if (err < 0) + goto fail; + mark_inode_dirty(inode); + return 0; +fail_map: + unlock_page(page); + page_cache_release(page); +fail: + return err; +} + +struct inode_operations page_symlink_inode_operations = { + .readlink = page_readlink, + .follow_link = page_follow_link, +}; + +EXPORT_SYMBOL(__user_walk); +EXPORT_SYMBOL(follow_down); +EXPORT_SYMBOL(follow_up); +EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ +EXPORT_SYMBOL(getname); +EXPORT_SYMBOL(lock_rename); +EXPORT_SYMBOL(lookup_create); +EXPORT_SYMBOL(lookup_hash); +EXPORT_SYMBOL(lookup_one_len); +EXPORT_SYMBOL(page_follow_link); +EXPORT_SYMBOL(page_readlink); +EXPORT_SYMBOL(page_symlink); +EXPORT_SYMBOL(page_symlink_inode_operations); +EXPORT_SYMBOL(path_lookup); +EXPORT_SYMBOL(path_release); +EXPORT_SYMBOL(path_walk); +EXPORT_SYMBOL(permission); +EXPORT_SYMBOL(unlock_rename); +EXPORT_SYMBOL(vfs_create); +EXPORT_SYMBOL(vfs_follow_link); +EXPORT_SYMBOL(vfs_link); +EXPORT_SYMBOL(vfs_mkdir); +EXPORT_SYMBOL(vfs_mknod); +EXPORT_SYMBOL(vfs_permission); +EXPORT_SYMBOL(vfs_readlink); +EXPORT_SYMBOL(vfs_rename); +EXPORT_SYMBOL(vfs_rmdir); +EXPORT_SYMBOL(vfs_symlink); +EXPORT_SYMBOL(vfs_unlink); +#ifdef CONFIG_DPROBES_MODULE +extern int deny_write_access_to_inode(struct inode *); +EXPORT_SYMBOL(deny_write_access_to_inode); +#endif diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/open.c linux-inotify/fs/open.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/open.c 2004-09-27 03:41:57.000000000 -0400 +++ linux-inotify/fs/open.c 2004-10-06 13:41:17.000000000 -0400 @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -1066,7 +1067,11 @@ if (!IS_ERR(f)) { TRIG_EVENT(open_hook, fd, f->f_dentry->d_name.len, - f->f_dentry->d_name.name); + f->f_dentry->d_name.name); + inotify_inode_queue_event(f->f_dentry->d_inode, + IN_OPEN, NULL); + inotify_dentry_parent_queue_event(f->f_dentry, + IN_OPEN, f->f_dentry->d_name.name); fd_install(fd, f); } else { put_unused_fd(fd); diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/read_write.c linux-inotify/fs/read_write.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/read_write.c 2004-09-27 03:41:54.000000000 -0400 +++ linux-inotify/fs/read_write.c 2004-10-05 16:15:07.000000000 -0400 @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -237,8 +238,11 @@ ret = file->f_op->read(file, buf, count, pos); else ret = do_sync_read(file, buf, count, pos); - if (ret > 0) + if (ret > 0) { dnotify_parent(file->f_dentry, DN_ACCESS); + inotify_dentry_parent_queue_event(file->f_dentry, IN_ACCESS, file->f_dentry->d_name.name); + inotify_inode_queue_event (file->f_dentry->d_inode, IN_ACCESS, NULL); + } } } @@ -281,8 +285,11 @@ ret = file->f_op->write(file, buf, count, pos); else ret = do_sync_write(file, buf, count, pos); - if (ret > 0) + if (ret > 0) { dnotify_parent(file->f_dentry, DN_MODIFY); + inotify_dentry_parent_queue_event(file->f_dentry, IN_MODIFY, file->f_dentry->d_name.name); + inotify_inode_queue_event (file->f_dentry->d_inode, IN_MODIFY, NULL); + } } } @@ -526,9 +533,13 @@ out: if (iov != iovstack) kfree(iov); - if ((ret + (type == READ)) > 0) + if ((ret + (type == READ)) > 0) { dnotify_parent(file->f_dentry, (type == READ) ? DN_ACCESS : DN_MODIFY); + inotify_dentry_parent_queue_event(file->f_dentry, + (type == READ) ? IN_ACCESS : IN_MODIFY, file->f_dentry->d_name.name); + inotify_inode_queue_event (file->f_dentry->d_inode, (type == READ) ? IN_ACCESS : IN_MODIFY, NULL); + } return ret; } diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/super.c linux-inotify/fs/super.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/fs/super.c 2004-09-27 03:41:54.000000000 -0400 +++ linux-inotify/fs/super.c 2004-10-05 16:15:07.000000000 -0400 @@ -36,6 +36,7 @@ #include /* for the emergency remount stuff */ #include #include +#include void get_filesystem(struct file_system_type *fs); @@ -204,6 +205,7 @@ if (root) { sb->s_root = NULL; + inotify_super_block_umount (sb); shrink_dcache_parent(root); shrink_dcache_anon(&sb->s_anon); dput(root); diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/include/linux/dnotify.h linux-inotify/include/linux/dnotify.h --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/include/linux/dnotify.h 2004-04-03 22:36:14.000000000 -0500 +++ linux-inotify/include/linux/dnotify.h 2004-10-05 16:15:07.000000000 -0400 @@ -1,3 +1,5 @@ +#ifndef _LINUX_DNOTIFY_H +#define _LINUX_DNOTIFY_H /* * Directory notification for Linux * @@ -8,20 +10,54 @@ struct dnotify_struct { struct dnotify_struct * dn_next; - unsigned long dn_mask; /* Events to be notified - see linux/fcntl.h */ + unsigned long dn_mask; int dn_fd; struct file * dn_filp; fl_owner_t dn_owner; }; +#ifdef __KERNEL__ + +#include + +#ifdef CONFIG_DNOTIFY + extern void __inode_dir_notify(struct inode *, unsigned long); -extern void dnotify_flush(struct file *filp, fl_owner_t id); +extern void dnotify_flush(struct file *, fl_owner_t); extern int fcntl_dirnotify(int, struct file *, unsigned long); -void dnotify_parent(struct dentry *dentry, unsigned long event); +extern void dnotify_parent(struct dentry *, unsigned long); static inline void inode_dir_notify(struct inode *inode, unsigned long event) { - if ((inode)->i_dnotify_mask & (event)) + if (inode->i_dnotify_mask & (event)) __inode_dir_notify(inode, event); } + +#else + +static inline void __inode_dir_notify(struct inode *inode, unsigned long event) +{ +} + +static inline void dnotify_flush(struct file *filp, fl_owner_t id) +{ +} + +static inline int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) +{ + return -EINVAL; +} + +static inline void dnotify_parent(struct dentry *dentry, unsigned long event) +{ +} + +static inline void inode_dir_notify(struct inode *inode, unsigned long event) +{ +} + +#endif /* CONFIG_DNOTIFY */ + +#endif /* __KERNEL __ */ + +#endif /* _LINUX_DNOTIFY_H */ diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/include/linux/fs.h linux-inotify/include/linux/fs.h --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/include/linux/fs.h 2004-09-27 03:41:58.000000000 -0400 +++ linux-inotify/include/linux/fs.h 2004-10-05 16:15:07.000000000 -0400 @@ -29,6 +29,7 @@ struct kstatfs; struct vm_area_struct; struct vfsmount; +struct inotify_inode_data; /* * It's silly to have NR_OPEN bigger than NR_FILE, but you can change @@ -63,7 +64,7 @@ }; extern struct inodes_stat_t inodes_stat; -extern int leases_enable, dir_notify_enable, lease_break_time; +extern int leases_enable, lease_break_time; #define NR_FILE 8192 /* this can well be larger on a larger system */ #define NR_RESERVED_FILES 10 /* reserved for root */ @@ -428,8 +429,14 @@ int i_cindex; void *i_filterdata; +#ifdef CONFIG_DNOTIFY unsigned long i_dnotify_mask; /* Directory notify events */ struct dnotify_struct *i_dnotify; /* for directory notifications */ +#endif + +#ifdef CONFIG_INOTIFY + struct inotify_inode_data *inotify_data; +#endif unsigned long i_state; unsigned long dirtied_when; /* jiffies of first dirtying */ diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/include/linux/inotify.h linux-inotify/include/linux/inotify.h --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/include/linux/inotify.h 1969-12-31 19:00:00.000000000 -0500 +++ linux-inotify/include/linux/inotify.h 2004-10-06 15:46:50.786490056 -0400 @@ -0,0 +1,151 @@ +/* + * Inode based directory notification for Linux + * + * Copyright (C) 2004 John McCutchan + */ + +#ifndef _LINUX_INOTIFY_H +#define _LINUX_INOTIFY_H + +#include +#include + +/* this size could limit things, since technically we could need PATH_MAX */ +#define INOTIFY_FILENAME_MAX 256 + +/* + * struct inotify_event - structure read from the inotify device for each event + * + * When you are watching a directory, you will receive the filename for events + * such as IN_CREATE, IN_DELETE, IN_OPEN, IN_CLOSE, ... + * + * Note: When reading from the device you must provide a buffer that is a + * multiple of sizeof(struct inotify_event) + */ +struct inotify_event { + __s32 wd; + __u32 mask; + __u32 cookie; + char filename[INOTIFY_FILENAME_MAX]; +}; + +/* + * struct inotify_watch_request - represents a watch request + * + * Pass to the inotify device via the INOTIFY_WATCH ioctl + */ +struct inotify_watch_request { + char *dirname; /* directory name */ + __u32 mask; /* event mask */ +}; + +/* the following are legal, implemented events */ +#define IN_ACCESS 0x00000001 /* File was accessed */ +#define IN_MODIFY 0x00000002 /* File was modified */ +#define IN_ATTRIB 0x00000004 /* File changed attributes */ +#define IN_CLOSE_WRITE 0x00000008 /* Writtable file was closed */ +#define IN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file closed */ +#define IN_OPEN 0x00000020 /* File was opened */ +#define IN_MOVED_FROM 0x00000040 /* File was moved from X */ +#define IN_MOVED_TO 0x00000080 /* File was moved to Y */ +#define IN_DELETE_SUBDIR 0x00000100 /* Subdir was deleted */ +#define IN_DELETE_FILE 0x00000200 /* Subfile was deleted */ +#define IN_CREATE_SUBDIR 0x00000400 /* Subdir was created */ +#define IN_CREATE_FILE 0x00000800 /* Subfile was created */ +#define IN_DELETE_SELF 0x00001000 /* Self was deleted */ +#define IN_UNMOUNT 0x00002000 /* Backing fs was unmounted */ +#define IN_Q_OVERFLOW 0x00004000 /* Event queued overflowed */ +#define IN_IGNORED 0x00008000 /* File was ignored */ + +/* special flags */ +#define IN_ALL_EVENTS 0xffffffff /* All the events */ +#define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) + +#define INOTIFY_IOCTL_MAGIC 'Q' +#define INOTIFY_IOCTL_MAXNR 4 + +#define INOTIFY_WATCH _IOR(INOTIFY_IOCTL_MAGIC, 1, struct inotify_watch_request) +#define INOTIFY_IGNORE _IOR(INOTIFY_IOCTL_MAGIC, 2, int) +#define INOTIFY_STATS _IOR(INOTIFY_IOCTL_MAGIC, 3, int) +#define INOTIFY_SETDEBUG _IOR(INOTIFY_IOCTL_MAGIC, 4, int) + +#define INOTIFY_DEBUG_NONE 0x00000000 +#define INOTIFY_DEBUG_ALLOC 0x00000001 +#define INOTIFY_DEBUG_EVENTS 0x00000002 +#define INOTIFY_DEBUG_INODE 0x00000004 +#define INOTIFY_DEBUG_ERRORS 0x00000008 +#define INOTIFY_DEBUG_FILEN 0x00000010 +#define INOTIFY_DEBUG_ALL 0xffffffff + +#ifdef __KERNEL__ + +#include +#include +#include + +struct inotify_inode_data { + struct list_head watches; + __u32 watch_mask; + int watch_count; +}; + +#ifdef CONFIG_INOTIFY + +extern void inotify_inode_queue_event(struct inode *, __u32, const char *); +extern void inotify_dentry_parent_queue_event(struct dentry *, __u32, + const char *); +extern void inotify_super_block_umount(struct super_block *); +extern void inotify_inode_is_dead(struct inode *); + +/* this could be kstrdup if only we could add that to lib/string.c */ +static inline char * inotify_oldname_init(struct dentry *old_dentry) +{ + char *old_name; + + old_name = kmalloc(strlen(old_dentry->d_name.name) + 1, GFP_KERNEL); + if (old_name) + strcpy(old_name, old_dentry->d_name.name); + return old_name; +} + +static inline void inotify_oldname_free(const char *old_name) +{ + kfree(old_name); +} + +#else + +static inline void inotify_inode_queue_event(struct inode *inode, + __u32 mask, + const char *filename) +{ +} + +static inline void inotify_dentry_parent_queue_event(struct dentry *dentry, + __u32 mask, + const char *filename) +{ +} + +static inline void inotify_super_block_umount(struct super_block *sb) +{ +} + +static inline void inotify_inode_is_dead(struct inode *inode) +{ +} + +static inline char * inotify_oldname_init(struct dentry *old_dentry) +{ + return NULL; +} + +static inline void inotify_oldname_free(const char *old_name) +{ +} + +#endif /* CONFIG_INOTIFY */ + +#endif /* __KERNEL __ */ + +#endif /* _LINUX_INOTIFY_H */ diff -urN linux-2.6.5-SLES9_GA_BRANCH_200409261010299/kernel/sysctl.c linux-inotify/kernel/sysctl.c --- linux-2.6.5-SLES9_GA_BRANCH_200409261010299/kernel/sysctl.c 2004-09-27 03:41:57.000000000 -0400 +++ linux-inotify/kernel/sysctl.c 2004-10-05 16:15:07.000000000 -0400 @@ -961,14 +961,6 @@ .proc_handler = &proc_dointvec, }, { - .ctl_name = FS_DIR_NOTIFY, - .procname = "dir-notify-enable", - .data = &dir_notify_enable, - .maxlen = sizeof(int), - .mode = 0644, - .proc_handler = &proc_dointvec, - }, - { .ctl_name = FS_LEASE_TIME, .procname = "lease-break-time", .data = &lease_break_time,