1272eb014SEric Paris /* 2272eb014SEric Paris * fs/inotify_user.c - inotify support for userspace 3272eb014SEric Paris * 4272eb014SEric Paris * Authors: 5272eb014SEric Paris * John McCutchan <ttb@tentacle.dhs.org> 6272eb014SEric Paris * Robert Love <rml@novell.com> 7272eb014SEric Paris * 8272eb014SEric Paris * Copyright (C) 2005 John McCutchan 9272eb014SEric Paris * Copyright 2006 Hewlett-Packard Development Company, L.P. 10272eb014SEric Paris * 1163c882a0SEric Paris * Copyright (C) 2009 Eric Paris <Red Hat Inc> 1263c882a0SEric Paris * inotify was largely rewriten to make use of the fsnotify infrastructure 1363c882a0SEric Paris * 14272eb014SEric Paris * This program is free software; you can redistribute it and/or modify it 15272eb014SEric Paris * under the terms of the GNU General Public License as published by the 16272eb014SEric Paris * Free Software Foundation; either version 2, or (at your option) any 17272eb014SEric Paris * later version. 18272eb014SEric Paris * 19272eb014SEric Paris * This program is distributed in the hope that it will be useful, but 20272eb014SEric Paris * WITHOUT ANY WARRANTY; without even the implied warranty of 21272eb014SEric Paris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22272eb014SEric Paris * General Public License for more details. 23272eb014SEric Paris */ 24272eb014SEric Paris 25272eb014SEric Paris #include <linux/file.h> 2663c882a0SEric Paris #include <linux/fs.h> /* struct inode */ 2763c882a0SEric Paris #include <linux/fsnotify_backend.h> 2863c882a0SEric Paris #include <linux/idr.h> 2963c882a0SEric Paris #include <linux/init.h> /* module_init */ 30272eb014SEric Paris #include <linux/inotify.h> 3163c882a0SEric Paris #include <linux/kernel.h> /* roundup() */ 3263c882a0SEric Paris #include <linux/namei.h> /* LOOKUP_FOLLOW */ 3363c882a0SEric Paris #include <linux/sched.h> /* struct user */ 3463c882a0SEric Paris #include <linux/slab.h> /* struct kmem_cache */ 35272eb014SEric Paris #include <linux/syscalls.h> 3663c882a0SEric Paris #include <linux/types.h> 37c44dcc56SAl Viro #include <linux/anon_inodes.h> 3863c882a0SEric Paris #include <linux/uaccess.h> 3963c882a0SEric Paris #include <linux/poll.h> 4063c882a0SEric Paris #include <linux/wait.h> 4163c882a0SEric Paris 4263c882a0SEric Paris #include "inotify.h" 43272eb014SEric Paris 44272eb014SEric Paris #include <asm/ioctls.h> 45272eb014SEric Paris 46272eb014SEric Paris /* these are configurable via /proc/sys/fs/inotify/ */ 47272eb014SEric Paris static int inotify_max_user_instances __read_mostly; 48272eb014SEric Paris static int inotify_max_queued_events __read_mostly; 490a24887aSH Hartley Sweeten static int inotify_max_user_watches __read_mostly; 5063c882a0SEric Paris 5163c882a0SEric Paris static struct kmem_cache *inotify_inode_mark_cachep __read_mostly; 5263c882a0SEric Paris struct kmem_cache *event_priv_cachep __read_mostly; 53272eb014SEric Paris 54272eb014SEric Paris #ifdef CONFIG_SYSCTL 55272eb014SEric Paris 56272eb014SEric Paris #include <linux/sysctl.h> 57272eb014SEric Paris 58272eb014SEric Paris static int zero; 59272eb014SEric Paris 60272eb014SEric Paris ctl_table inotify_table[] = { 61272eb014SEric Paris { 62272eb014SEric Paris .procname = "max_user_instances", 63272eb014SEric Paris .data = &inotify_max_user_instances, 64272eb014SEric Paris .maxlen = sizeof(int), 65272eb014SEric Paris .mode = 0644, 666d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 67272eb014SEric Paris .extra1 = &zero, 68272eb014SEric Paris }, 69272eb014SEric Paris { 70272eb014SEric Paris .procname = "max_user_watches", 71272eb014SEric Paris .data = &inotify_max_user_watches, 72272eb014SEric Paris .maxlen = sizeof(int), 73272eb014SEric Paris .mode = 0644, 746d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 75272eb014SEric Paris .extra1 = &zero, 76272eb014SEric Paris }, 77272eb014SEric Paris { 78272eb014SEric Paris .procname = "max_queued_events", 79272eb014SEric Paris .data = &inotify_max_queued_events, 80272eb014SEric Paris .maxlen = sizeof(int), 81272eb014SEric Paris .mode = 0644, 826d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 83272eb014SEric Paris .extra1 = &zero 84272eb014SEric Paris }, 85ab09203eSEric W. Biederman { } 86272eb014SEric Paris }; 87272eb014SEric Paris #endif /* CONFIG_SYSCTL */ 88272eb014SEric Paris 8963c882a0SEric Paris static inline __u32 inotify_arg_to_mask(u32 arg) 90272eb014SEric Paris { 9163c882a0SEric Paris __u32 mask; 9263c882a0SEric Paris 93611da04fSEric Paris /* 94611da04fSEric Paris * everything should accept their own ignored, cares about children, 95611da04fSEric Paris * and should receive events when the inode is unmounted 96611da04fSEric Paris */ 97611da04fSEric Paris mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT); 9863c882a0SEric Paris 9963c882a0SEric Paris /* mask off the flags used to open the fd */ 100*8c1934c8SEric Paris mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK)); 10163c882a0SEric Paris 10263c882a0SEric Paris return mask; 103272eb014SEric Paris } 104272eb014SEric Paris 10563c882a0SEric Paris static inline u32 inotify_mask_to_arg(__u32 mask) 106272eb014SEric Paris { 10763c882a0SEric Paris return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED | 10863c882a0SEric Paris IN_Q_OVERFLOW); 109272eb014SEric Paris } 110272eb014SEric Paris 11163c882a0SEric Paris /* intofiy userspace file descriptor functions */ 112272eb014SEric Paris static unsigned int inotify_poll(struct file *file, poll_table *wait) 113272eb014SEric Paris { 11463c882a0SEric Paris struct fsnotify_group *group = file->private_data; 115272eb014SEric Paris int ret = 0; 116272eb014SEric Paris 11763c882a0SEric Paris poll_wait(file, &group->notification_waitq, wait); 11863c882a0SEric Paris mutex_lock(&group->notification_mutex); 11963c882a0SEric Paris if (!fsnotify_notify_queue_is_empty(group)) 120272eb014SEric Paris ret = POLLIN | POLLRDNORM; 12163c882a0SEric Paris mutex_unlock(&group->notification_mutex); 122272eb014SEric Paris 123272eb014SEric Paris return ret; 124272eb014SEric Paris } 125272eb014SEric Paris 1263632dee2SVegard Nossum /* 1273632dee2SVegard Nossum * Get an inotify_kernel_event if one exists and is small 1283632dee2SVegard Nossum * enough to fit in "count". Return an error pointer if 1293632dee2SVegard Nossum * not large enough. 1303632dee2SVegard Nossum * 13163c882a0SEric Paris * Called with the group->notification_mutex held. 1323632dee2SVegard Nossum */ 13363c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group, 1343632dee2SVegard Nossum size_t count) 1353632dee2SVegard Nossum { 1363632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 13763c882a0SEric Paris struct fsnotify_event *event; 1383632dee2SVegard Nossum 13963c882a0SEric Paris if (fsnotify_notify_queue_is_empty(group)) 1403632dee2SVegard Nossum return NULL; 1413632dee2SVegard Nossum 14263c882a0SEric Paris event = fsnotify_peek_notify_event(group); 14363c882a0SEric Paris 14483cb10f0SEric Paris if (event->name_len) 14583cb10f0SEric Paris event_size += roundup(event->name_len + 1, event_size); 1463632dee2SVegard Nossum 1473632dee2SVegard Nossum if (event_size > count) 1483632dee2SVegard Nossum return ERR_PTR(-EINVAL); 1493632dee2SVegard Nossum 15063c882a0SEric Paris /* held the notification_mutex the whole time, so this is the 15163c882a0SEric Paris * same event we peeked above */ 15263c882a0SEric Paris fsnotify_remove_notify_event(group); 15363c882a0SEric Paris 15463c882a0SEric Paris return event; 1553632dee2SVegard Nossum } 1563632dee2SVegard Nossum 1573632dee2SVegard Nossum /* 1583632dee2SVegard Nossum * Copy an event to user space, returning how much we copied. 1593632dee2SVegard Nossum * 1603632dee2SVegard Nossum * We already checked that the event size is smaller than the 1613632dee2SVegard Nossum * buffer we had in "get_one_event()" above. 1623632dee2SVegard Nossum */ 16363c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group, 16463c882a0SEric Paris struct fsnotify_event *event, 1653632dee2SVegard Nossum char __user *buf) 1663632dee2SVegard Nossum { 16763c882a0SEric Paris struct inotify_event inotify_event; 16863c882a0SEric Paris struct fsnotify_event_private_data *fsn_priv; 16963c882a0SEric Paris struct inotify_event_private_data *priv; 1703632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 171b962e731SBrian Rogers size_t name_len = 0; 1723632dee2SVegard Nossum 17363c882a0SEric Paris /* we get the inotify watch descriptor from the event private data */ 17463c882a0SEric Paris spin_lock(&event->lock); 17563c882a0SEric Paris fsn_priv = fsnotify_remove_priv_from_event(group, event); 17663c882a0SEric Paris spin_unlock(&event->lock); 17763c882a0SEric Paris 17863c882a0SEric Paris if (!fsn_priv) 17963c882a0SEric Paris inotify_event.wd = -1; 18063c882a0SEric Paris else { 18163c882a0SEric Paris priv = container_of(fsn_priv, struct inotify_event_private_data, 18263c882a0SEric Paris fsnotify_event_priv_data); 18363c882a0SEric Paris inotify_event.wd = priv->wd; 18463c882a0SEric Paris inotify_free_event_priv(fsn_priv); 18563c882a0SEric Paris } 18663c882a0SEric Paris 187b962e731SBrian Rogers /* 188b962e731SBrian Rogers * round up event->name_len so it is a multiple of event_size 1890db501bdSEric W. Biederman * plus an extra byte for the terminating '\0'. 1900db501bdSEric W. Biederman */ 191b962e731SBrian Rogers if (event->name_len) 1920db501bdSEric W. Biederman name_len = roundup(event->name_len + 1, event_size); 19363c882a0SEric Paris inotify_event.len = name_len; 19463c882a0SEric Paris 19563c882a0SEric Paris inotify_event.mask = inotify_mask_to_arg(event->mask); 19663c882a0SEric Paris inotify_event.cookie = event->sync_cookie; 19763c882a0SEric Paris 19863c882a0SEric Paris /* send the main event */ 19963c882a0SEric Paris if (copy_to_user(buf, &inotify_event, event_size)) 2003632dee2SVegard Nossum return -EFAULT; 2013632dee2SVegard Nossum 2023632dee2SVegard Nossum buf += event_size; 2033632dee2SVegard Nossum 20463c882a0SEric Paris /* 20563c882a0SEric Paris * fsnotify only stores the pathname, so here we have to send the pathname 20663c882a0SEric Paris * and then pad that pathname out to a multiple of sizeof(inotify_event) 20763c882a0SEric Paris * with zeros. I get my zeros from the nul_inotify_event. 20863c882a0SEric Paris */ 20963c882a0SEric Paris if (name_len) { 21063c882a0SEric Paris unsigned int len_to_zero = name_len - event->name_len; 21163c882a0SEric Paris /* copy the path name */ 21263c882a0SEric Paris if (copy_to_user(buf, event->file_name, event->name_len)) 2133632dee2SVegard Nossum return -EFAULT; 21463c882a0SEric Paris buf += event->name_len; 2153632dee2SVegard Nossum 2160db501bdSEric W. Biederman /* fill userspace with 0's */ 2170db501bdSEric W. Biederman if (clear_user(buf, len_to_zero)) 21863c882a0SEric Paris return -EFAULT; 21963c882a0SEric Paris buf += len_to_zero; 22063c882a0SEric Paris event_size += name_len; 2213632dee2SVegard Nossum } 22263c882a0SEric Paris 2233632dee2SVegard Nossum return event_size; 2243632dee2SVegard Nossum } 2253632dee2SVegard Nossum 226272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf, 227272eb014SEric Paris size_t count, loff_t *pos) 228272eb014SEric Paris { 22963c882a0SEric Paris struct fsnotify_group *group; 23063c882a0SEric Paris struct fsnotify_event *kevent; 231272eb014SEric Paris char __user *start; 232272eb014SEric Paris int ret; 233272eb014SEric Paris DEFINE_WAIT(wait); 234272eb014SEric Paris 235272eb014SEric Paris start = buf; 23663c882a0SEric Paris group = file->private_data; 237272eb014SEric Paris 238272eb014SEric Paris while (1) { 23963c882a0SEric Paris prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE); 240272eb014SEric Paris 24163c882a0SEric Paris mutex_lock(&group->notification_mutex); 24263c882a0SEric Paris kevent = get_one_event(group, count); 24363c882a0SEric Paris mutex_unlock(&group->notification_mutex); 244272eb014SEric Paris 2453632dee2SVegard Nossum if (kevent) { 2463632dee2SVegard Nossum ret = PTR_ERR(kevent); 2473632dee2SVegard Nossum if (IS_ERR(kevent)) 248272eb014SEric Paris break; 24963c882a0SEric Paris ret = copy_event_to_user(group, kevent, buf); 25063c882a0SEric Paris fsnotify_put_event(kevent); 2513632dee2SVegard Nossum if (ret < 0) 2523632dee2SVegard Nossum break; 2533632dee2SVegard Nossum buf += ret; 2543632dee2SVegard Nossum count -= ret; 2553632dee2SVegard Nossum continue; 256272eb014SEric Paris } 257272eb014SEric Paris 2583632dee2SVegard Nossum ret = -EAGAIN; 2593632dee2SVegard Nossum if (file->f_flags & O_NONBLOCK) 260272eb014SEric Paris break; 2613632dee2SVegard Nossum ret = -EINTR; 2623632dee2SVegard Nossum if (signal_pending(current)) 2633632dee2SVegard Nossum break; 2643632dee2SVegard Nossum 2653632dee2SVegard Nossum if (start != buf) 2663632dee2SVegard Nossum break; 267272eb014SEric Paris 268272eb014SEric Paris schedule(); 269272eb014SEric Paris } 270272eb014SEric Paris 27163c882a0SEric Paris finish_wait(&group->notification_waitq, &wait); 2723632dee2SVegard Nossum if (start != buf && ret != -EFAULT) 273272eb014SEric Paris ret = buf - start; 274272eb014SEric Paris return ret; 275272eb014SEric Paris } 276272eb014SEric Paris 277272eb014SEric Paris static int inotify_fasync(int fd, struct file *file, int on) 278272eb014SEric Paris { 27963c882a0SEric Paris struct fsnotify_group *group = file->private_data; 280272eb014SEric Paris 28163c882a0SEric Paris return fasync_helper(fd, file, on, &group->inotify_data.fa) >= 0 ? 0 : -EIO; 282272eb014SEric Paris } 283272eb014SEric Paris 284272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file) 285272eb014SEric Paris { 28663c882a0SEric Paris struct fsnotify_group *group = file->private_data; 287bdae997fSKeith Packard struct user_struct *user = group->inotify_data.user; 288272eb014SEric Paris 28963c882a0SEric Paris fsnotify_clear_marks_by_group(group); 290272eb014SEric Paris 29163c882a0SEric Paris /* free this group, matching get was inotify_init->fsnotify_obtain_group */ 29263c882a0SEric Paris fsnotify_put_group(group); 293272eb014SEric Paris 294bdae997fSKeith Packard atomic_dec(&user->inotify_devs); 295bdae997fSKeith Packard 296272eb014SEric Paris return 0; 297272eb014SEric Paris } 298272eb014SEric Paris 299272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd, 300272eb014SEric Paris unsigned long arg) 301272eb014SEric Paris { 30263c882a0SEric Paris struct fsnotify_group *group; 30363c882a0SEric Paris struct fsnotify_event_holder *holder; 30463c882a0SEric Paris struct fsnotify_event *event; 305272eb014SEric Paris void __user *p; 306272eb014SEric Paris int ret = -ENOTTY; 30763c882a0SEric Paris size_t send_len = 0; 308272eb014SEric Paris 30963c882a0SEric Paris group = file->private_data; 310272eb014SEric Paris p = (void __user *) arg; 311272eb014SEric Paris 312272eb014SEric Paris switch (cmd) { 313272eb014SEric Paris case FIONREAD: 31463c882a0SEric Paris mutex_lock(&group->notification_mutex); 31563c882a0SEric Paris list_for_each_entry(holder, &group->notification_list, event_list) { 31663c882a0SEric Paris event = holder->event; 31763c882a0SEric Paris send_len += sizeof(struct inotify_event); 31883cb10f0SEric Paris if (event->name_len) 31983cb10f0SEric Paris send_len += roundup(event->name_len + 1, 32063c882a0SEric Paris sizeof(struct inotify_event)); 32163c882a0SEric Paris } 32263c882a0SEric Paris mutex_unlock(&group->notification_mutex); 32363c882a0SEric Paris ret = put_user(send_len, (int __user *) p); 324272eb014SEric Paris break; 325272eb014SEric Paris } 326272eb014SEric Paris 327272eb014SEric Paris return ret; 328272eb014SEric Paris } 329272eb014SEric Paris 330272eb014SEric Paris static const struct file_operations inotify_fops = { 331272eb014SEric Paris .poll = inotify_poll, 332272eb014SEric Paris .read = inotify_read, 333272eb014SEric Paris .fasync = inotify_fasync, 334272eb014SEric Paris .release = inotify_release, 335272eb014SEric Paris .unlocked_ioctl = inotify_ioctl, 336272eb014SEric Paris .compat_ioctl = inotify_ioctl, 337272eb014SEric Paris }; 338272eb014SEric Paris 339272eb014SEric Paris 34063c882a0SEric Paris /* 34163c882a0SEric Paris * find_inode - resolve a user-given path to a specific inode 34263c882a0SEric Paris */ 34363c882a0SEric Paris static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags) 34463c882a0SEric Paris { 34563c882a0SEric Paris int error; 34663c882a0SEric Paris 34763c882a0SEric Paris error = user_path_at(AT_FDCWD, dirname, flags, path); 34863c882a0SEric Paris if (error) 34963c882a0SEric Paris return error; 35063c882a0SEric Paris /* you can only watch an inode if you have read permissions on it */ 35163c882a0SEric Paris error = inode_permission(path->dentry->d_inode, MAY_READ); 35263c882a0SEric Paris if (error) 35363c882a0SEric Paris path_put(path); 35463c882a0SEric Paris return error; 35563c882a0SEric Paris } 35663c882a0SEric Paris 357b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock, 3587050c488SEric Paris int *last_wd, 359000285deSEric Paris struct inotify_inode_mark *i_mark) 360b7ba8371SEric Paris { 361b7ba8371SEric Paris int ret; 362b7ba8371SEric Paris 363b7ba8371SEric Paris do { 364b7ba8371SEric Paris if (unlikely(!idr_pre_get(idr, GFP_KERNEL))) 365b7ba8371SEric Paris return -ENOMEM; 366b7ba8371SEric Paris 367b7ba8371SEric Paris spin_lock(idr_lock); 368000285deSEric Paris ret = idr_get_new_above(idr, i_mark, *last_wd + 1, 369000285deSEric Paris &i_mark->wd); 370b7ba8371SEric Paris /* we added the mark to the idr, take a reference */ 3717050c488SEric Paris if (!ret) { 372000285deSEric Paris *last_wd = i_mark->wd; 373000285deSEric Paris fsnotify_get_mark(&i_mark->fsn_mark); 3747050c488SEric Paris } 375b7ba8371SEric Paris spin_unlock(idr_lock); 376b7ba8371SEric Paris } while (ret == -EAGAIN); 377b7ba8371SEric Paris 378b7ba8371SEric Paris return ret; 379b7ba8371SEric Paris } 380b7ba8371SEric Paris 381000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group, 382b7ba8371SEric Paris int wd) 383b7ba8371SEric Paris { 384b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 385b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 386000285deSEric Paris struct inotify_inode_mark *i_mark; 387b7ba8371SEric Paris 388b7ba8371SEric Paris assert_spin_locked(idr_lock); 389b7ba8371SEric Paris 390000285deSEric Paris i_mark = idr_find(idr, wd); 391000285deSEric Paris if (i_mark) { 392000285deSEric Paris struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark; 393b7ba8371SEric Paris 394000285deSEric Paris fsnotify_get_mark(fsn_mark); 395b7ba8371SEric Paris /* One ref for being in the idr, one ref we just took */ 396000285deSEric Paris BUG_ON(atomic_read(&fsn_mark->refcnt) < 2); 397b7ba8371SEric Paris } 398b7ba8371SEric Paris 399000285deSEric Paris return i_mark; 400b7ba8371SEric Paris } 401b7ba8371SEric Paris 402000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group, 403b7ba8371SEric Paris int wd) 404b7ba8371SEric Paris { 405000285deSEric Paris struct inotify_inode_mark *i_mark; 406b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 407b7ba8371SEric Paris 408b7ba8371SEric Paris spin_lock(idr_lock); 409000285deSEric Paris i_mark = inotify_idr_find_locked(group, wd); 410b7ba8371SEric Paris spin_unlock(idr_lock); 411b7ba8371SEric Paris 412000285deSEric Paris return i_mark; 413b7ba8371SEric Paris } 414b7ba8371SEric Paris 415b7ba8371SEric Paris static void do_inotify_remove_from_idr(struct fsnotify_group *group, 416000285deSEric Paris struct inotify_inode_mark *i_mark) 417b7ba8371SEric Paris { 418b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 419b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 420000285deSEric Paris int wd = i_mark->wd; 421b7ba8371SEric Paris 422b7ba8371SEric Paris assert_spin_locked(idr_lock); 423b7ba8371SEric Paris 424b7ba8371SEric Paris idr_remove(idr, wd); 425b7ba8371SEric Paris 426b7ba8371SEric Paris /* removed from the idr, drop that ref */ 427000285deSEric Paris fsnotify_put_mark(&i_mark->fsn_mark); 428b7ba8371SEric Paris } 429b7ba8371SEric Paris 430dead537dSEric Paris /* 431dead537dSEric Paris * Remove the mark from the idr (if present) and drop the reference 432dead537dSEric Paris * on the mark because it was in the idr. 433dead537dSEric Paris */ 4347e790dd5SEric Paris static void inotify_remove_from_idr(struct fsnotify_group *group, 435000285deSEric Paris struct inotify_inode_mark *i_mark) 4367e790dd5SEric Paris { 437b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 438000285deSEric Paris struct inotify_inode_mark *found_i_mark = NULL; 439dead537dSEric Paris int wd; 4407e790dd5SEric Paris 441b7ba8371SEric Paris spin_lock(idr_lock); 442000285deSEric Paris wd = i_mark->wd; 443dead537dSEric Paris 444b7ba8371SEric Paris /* 445000285deSEric Paris * does this i_mark think it is in the idr? we shouldn't get called 446b7ba8371SEric Paris * if it wasn't.... 447b7ba8371SEric Paris */ 448b7ba8371SEric Paris if (wd == -1) { 449000285deSEric Paris WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p" 450000285deSEric Paris " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd, 451000285deSEric Paris i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode); 452dead537dSEric Paris goto out; 4537e790dd5SEric Paris } 454dead537dSEric Paris 455b7ba8371SEric Paris /* Lets look in the idr to see if we find it */ 456000285deSEric Paris found_i_mark = inotify_idr_find_locked(group, wd); 457000285deSEric Paris if (unlikely(!found_i_mark)) { 458000285deSEric Paris WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p" 459000285deSEric Paris " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd, 460000285deSEric Paris i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode); 461b7ba8371SEric Paris goto out; 462b7ba8371SEric Paris } 463dead537dSEric Paris 464b7ba8371SEric Paris /* 465000285deSEric Paris * We found an mark in the idr at the right wd, but it's 466000285deSEric Paris * not the mark we were told to remove. eparis seriously 467b7ba8371SEric Paris * fucked up somewhere. 468b7ba8371SEric Paris */ 469000285deSEric Paris if (unlikely(found_i_mark != i_mark)) { 470000285deSEric Paris WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p " 471000285deSEric Paris "mark->inode=%p found_i_mark=%p found_i_mark->wd=%d " 472000285deSEric Paris "found_i_mark->group=%p found_i_mark->inode=%p\n", 473000285deSEric Paris __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group, 474000285deSEric Paris i_mark->fsn_mark.i.inode, found_i_mark, found_i_mark->wd, 475000285deSEric Paris found_i_mark->fsn_mark.group, 476000285deSEric Paris found_i_mark->fsn_mark.i.inode); 477b7ba8371SEric Paris goto out; 478b7ba8371SEric Paris } 479dead537dSEric Paris 480b7ba8371SEric Paris /* 481b7ba8371SEric Paris * One ref for being in the idr 482b7ba8371SEric Paris * one ref held by the caller trying to kill us 483b7ba8371SEric Paris * one ref grabbed by inotify_idr_find 484b7ba8371SEric Paris */ 485000285deSEric Paris if (unlikely(atomic_read(&i_mark->fsn_mark.refcnt) < 3)) { 486000285deSEric Paris printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p" 487000285deSEric Paris " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd, 488000285deSEric Paris i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode); 489b7ba8371SEric Paris /* we can't really recover with bad ref cnting.. */ 490b7ba8371SEric Paris BUG(); 491b7ba8371SEric Paris } 492b7ba8371SEric Paris 493000285deSEric Paris do_inotify_remove_from_idr(group, i_mark); 494dead537dSEric Paris out: 495b7ba8371SEric Paris /* match the ref taken by inotify_idr_find_locked() */ 496000285deSEric Paris if (found_i_mark) 497000285deSEric Paris fsnotify_put_mark(&found_i_mark->fsn_mark); 498000285deSEric Paris i_mark->wd = -1; 499b7ba8371SEric Paris spin_unlock(idr_lock); 500dead537dSEric Paris } 501dead537dSEric Paris 50263c882a0SEric Paris /* 503dead537dSEric Paris * Send IN_IGNORED for this wd, remove this wd from the idr. 50463c882a0SEric Paris */ 505000285deSEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark, 506528da3e9SEric Paris struct fsnotify_group *group) 50763c882a0SEric Paris { 508000285deSEric Paris struct inotify_inode_mark *i_mark; 509f44aebccSEric Paris struct fsnotify_event *ignored_event; 51063c882a0SEric Paris struct inotify_event_private_data *event_priv; 51163c882a0SEric Paris struct fsnotify_event_private_data *fsn_event_priv; 512eef3a116SEric Paris int ret; 51363c882a0SEric Paris 514f44aebccSEric Paris ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL, 515f44aebccSEric Paris FSNOTIFY_EVENT_NONE, NULL, 0, 516f44aebccSEric Paris GFP_NOFS); 517f44aebccSEric Paris if (!ignored_event) 518f44aebccSEric Paris return; 519f44aebccSEric Paris 520000285deSEric Paris i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 52163c882a0SEric Paris 522f44aebccSEric Paris event_priv = kmem_cache_alloc(event_priv_cachep, GFP_NOFS); 52363c882a0SEric Paris if (unlikely(!event_priv)) 52463c882a0SEric Paris goto skip_send_ignore; 52563c882a0SEric Paris 52663c882a0SEric Paris fsn_event_priv = &event_priv->fsnotify_event_priv_data; 52763c882a0SEric Paris 52863c882a0SEric Paris fsn_event_priv->group = group; 529000285deSEric Paris event_priv->wd = i_mark->wd; 53063c882a0SEric Paris 5316e5f77b3SEric Paris ret = fsnotify_add_notify_event(group, ignored_event, fsn_event_priv, NULL, NULL); 532eef3a116SEric Paris if (ret) 53363c882a0SEric Paris inotify_free_event_priv(fsn_event_priv); 53463c882a0SEric Paris 53563c882a0SEric Paris skip_send_ignore: 53663c882a0SEric Paris 537f44aebccSEric Paris /* matches the reference taken when the event was created */ 538f44aebccSEric Paris fsnotify_put_event(ignored_event); 539f44aebccSEric Paris 540000285deSEric Paris /* remove this mark from the idr */ 541000285deSEric Paris inotify_remove_from_idr(group, i_mark); 54263c882a0SEric Paris 5435549f7cdSEric Paris atomic_dec(&group->inotify_data.user->inotify_watches); 54463c882a0SEric Paris } 54563c882a0SEric Paris 54663c882a0SEric Paris /* ding dong the mark is dead */ 547000285deSEric Paris static void inotify_free_mark(struct fsnotify_mark *fsn_mark) 54863c882a0SEric Paris { 549000285deSEric Paris struct inotify_inode_mark *i_mark; 55031ddd326SEric Paris 551000285deSEric Paris i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 55263c882a0SEric Paris 553000285deSEric Paris kmem_cache_free(inotify_inode_mark_cachep, i_mark); 55463c882a0SEric Paris } 55563c882a0SEric Paris 55652cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group, 55752cef755SEric Paris struct inode *inode, 55852cef755SEric Paris u32 arg) 55963c882a0SEric Paris { 560000285deSEric Paris struct fsnotify_mark *fsn_mark; 561000285deSEric Paris struct inotify_inode_mark *i_mark; 56263c882a0SEric Paris __u32 old_mask, new_mask; 56352cef755SEric Paris __u32 mask; 56452cef755SEric Paris int add = (arg & IN_MASK_ADD); 56552cef755SEric Paris int ret; 56663c882a0SEric Paris 56763c882a0SEric Paris /* don't allow invalid bits: we don't want flags set */ 56863c882a0SEric Paris mask = inotify_arg_to_mask(arg); 56963c882a0SEric Paris if (unlikely(!mask)) 57063c882a0SEric Paris return -EINVAL; 57163c882a0SEric Paris 5725444e298SEric Paris fsn_mark = fsnotify_find_inode_mark(group, inode); 573000285deSEric Paris if (!fsn_mark) 57452cef755SEric Paris return -ENOENT; 57552cef755SEric Paris 576000285deSEric Paris i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 57775fe2b26SEric Paris 578000285deSEric Paris spin_lock(&fsn_mark->lock); 57963c882a0SEric Paris 580000285deSEric Paris old_mask = fsn_mark->mask; 58190b1e7a5SEric Paris if (add) 58290b1e7a5SEric Paris fsnotify_set_mark_mask_locked(fsn_mark, (fsn_mark->mask | mask)); 58390b1e7a5SEric Paris else 58490b1e7a5SEric Paris fsnotify_set_mark_mask_locked(fsn_mark, mask); 585000285deSEric Paris new_mask = fsn_mark->mask; 58663c882a0SEric Paris 587000285deSEric Paris spin_unlock(&fsn_mark->lock); 58863c882a0SEric Paris 58963c882a0SEric Paris if (old_mask != new_mask) { 59063c882a0SEric Paris /* more bits in old than in new? */ 59163c882a0SEric Paris int dropped = (old_mask & ~new_mask); 592000285deSEric Paris /* more bits in this fsn_mark than the inode's mask? */ 59363c882a0SEric Paris int do_inode = (new_mask & ~inode->i_fsnotify_mask); 594000285deSEric Paris /* more bits in this fsn_mark than the group? */ 59563c882a0SEric Paris int do_group = (new_mask & ~group->mask); 59663c882a0SEric Paris 597000285deSEric Paris /* update the inode with this new fsn_mark */ 59863c882a0SEric Paris if (dropped || do_inode) 59963c882a0SEric Paris fsnotify_recalc_inode_mask(inode); 60063c882a0SEric Paris 60163c882a0SEric Paris /* update the group mask with the new mask */ 60263c882a0SEric Paris if (dropped || do_group) 60363c882a0SEric Paris fsnotify_recalc_group_mask(group); 60463c882a0SEric Paris } 60563c882a0SEric Paris 60652cef755SEric Paris /* return the wd */ 607000285deSEric Paris ret = i_mark->wd; 60852cef755SEric Paris 609d0775441SEric Paris /* match the get from fsnotify_find_mark() */ 610000285deSEric Paris fsnotify_put_mark(fsn_mark); 61175fe2b26SEric Paris 61252cef755SEric Paris return ret; 61363c882a0SEric Paris } 6147e790dd5SEric Paris 61552cef755SEric Paris static int inotify_new_watch(struct fsnotify_group *group, 61652cef755SEric Paris struct inode *inode, 61752cef755SEric Paris u32 arg) 61852cef755SEric Paris { 619000285deSEric Paris struct inotify_inode_mark *tmp_i_mark; 62052cef755SEric Paris __u32 mask; 62152cef755SEric Paris int ret; 622b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 623b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 62452cef755SEric Paris 62552cef755SEric Paris /* don't allow invalid bits: we don't want flags set */ 62652cef755SEric Paris mask = inotify_arg_to_mask(arg); 62752cef755SEric Paris if (unlikely(!mask)) 62852cef755SEric Paris return -EINVAL; 62952cef755SEric Paris 630000285deSEric Paris tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); 631000285deSEric Paris if (unlikely(!tmp_i_mark)) 63252cef755SEric Paris return -ENOMEM; 63352cef755SEric Paris 634000285deSEric Paris fsnotify_init_mark(&tmp_i_mark->fsn_mark, inotify_free_mark); 635000285deSEric Paris tmp_i_mark->fsn_mark.mask = mask; 636000285deSEric Paris tmp_i_mark->wd = -1; 63752cef755SEric Paris 63852cef755SEric Paris ret = -ENOSPC; 63952cef755SEric Paris if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches) 64052cef755SEric Paris goto out_err; 641b7ba8371SEric Paris 6427050c488SEric Paris ret = inotify_add_to_idr(idr, idr_lock, &group->inotify_data.last_wd, 643000285deSEric Paris tmp_i_mark); 644b7ba8371SEric Paris if (ret) 64552cef755SEric Paris goto out_err; 64652cef755SEric Paris 64752cef755SEric Paris /* we are on the idr, now get on the inode */ 6485444e298SEric Paris ret = fsnotify_add_mark(&tmp_i_mark->fsn_mark, group, inode, NULL, 0); 64952cef755SEric Paris if (ret) { 65052cef755SEric Paris /* we failed to get on the inode, get off the idr */ 651000285deSEric Paris inotify_remove_from_idr(group, tmp_i_mark); 65252cef755SEric Paris goto out_err; 65352cef755SEric Paris } 65452cef755SEric Paris 65552cef755SEric Paris /* increment the number of watches the user has */ 65652cef755SEric Paris atomic_inc(&group->inotify_data.user->inotify_watches); 65752cef755SEric Paris 658000285deSEric Paris /* return the watch descriptor for this new mark */ 659000285deSEric Paris ret = tmp_i_mark->wd; 66052cef755SEric Paris 661750a8870SEric Paris /* if this mark added a new event update the group mask */ 662750a8870SEric Paris if (mask & ~group->mask) 663750a8870SEric Paris fsnotify_recalc_group_mask(group); 664750a8870SEric Paris 66552cef755SEric Paris out_err: 666000285deSEric Paris /* match the ref from fsnotify_init_mark() */ 667000285deSEric Paris fsnotify_put_mark(&tmp_i_mark->fsn_mark); 66852cef755SEric Paris 66952cef755SEric Paris return ret; 67052cef755SEric Paris } 67152cef755SEric Paris 67252cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg) 67352cef755SEric Paris { 67452cef755SEric Paris int ret = 0; 67552cef755SEric Paris 67652cef755SEric Paris retry: 67752cef755SEric Paris /* try to update and existing watch with the new arg */ 67852cef755SEric Paris ret = inotify_update_existing_watch(group, inode, arg); 67952cef755SEric Paris /* no mark present, try to add a new one */ 68052cef755SEric Paris if (ret == -ENOENT) 68152cef755SEric Paris ret = inotify_new_watch(group, inode, arg); 68252cef755SEric Paris /* 68352cef755SEric Paris * inotify_new_watch could race with another thread which did an 68452cef755SEric Paris * inotify_new_watch between the update_existing and the add watch 68552cef755SEric Paris * here, go back and try to update an existing mark again. 68652cef755SEric Paris */ 68752cef755SEric Paris if (ret == -EEXIST) 68852cef755SEric Paris goto retry; 68952cef755SEric Paris 69063c882a0SEric Paris return ret; 69163c882a0SEric Paris } 69263c882a0SEric Paris 69363c882a0SEric Paris static struct fsnotify_group *inotify_new_group(struct user_struct *user, unsigned int max_events) 69463c882a0SEric Paris { 69563c882a0SEric Paris struct fsnotify_group *group; 69663c882a0SEric Paris 6970d2e2a1dSEric Paris group = fsnotify_alloc_group(&inotify_fsnotify_ops); 69863c882a0SEric Paris if (IS_ERR(group)) 69963c882a0SEric Paris return group; 70063c882a0SEric Paris 70163c882a0SEric Paris group->max_events = max_events; 70263c882a0SEric Paris 70363c882a0SEric Paris spin_lock_init(&group->inotify_data.idr_lock); 70463c882a0SEric Paris idr_init(&group->inotify_data.idr); 7059e572cc9SEric Paris group->inotify_data.last_wd = 0; 70663c882a0SEric Paris group->inotify_data.user = user; 70763c882a0SEric Paris group->inotify_data.fa = NULL; 70863c882a0SEric Paris 70963c882a0SEric Paris return group; 71063c882a0SEric Paris } 71163c882a0SEric Paris 71263c882a0SEric Paris 71363c882a0SEric Paris /* inotify syscalls */ 714938bb9f5SHeiko Carstens SYSCALL_DEFINE1(inotify_init1, int, flags) 715272eb014SEric Paris { 71663c882a0SEric Paris struct fsnotify_group *group; 717272eb014SEric Paris struct user_struct *user; 718c44dcc56SAl Viro int ret; 719272eb014SEric Paris 720272eb014SEric Paris /* Check the IN_* constants for consistency. */ 721272eb014SEric Paris BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC); 722272eb014SEric Paris BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK); 723272eb014SEric Paris 724272eb014SEric Paris if (flags & ~(IN_CLOEXEC | IN_NONBLOCK)) 725272eb014SEric Paris return -EINVAL; 726272eb014SEric Paris 727272eb014SEric Paris user = get_current_user(); 728272eb014SEric Paris if (unlikely(atomic_read(&user->inotify_devs) >= 729272eb014SEric Paris inotify_max_user_instances)) { 730272eb014SEric Paris ret = -EMFILE; 731272eb014SEric Paris goto out_free_uid; 732272eb014SEric Paris } 733272eb014SEric Paris 73463c882a0SEric Paris /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */ 73563c882a0SEric Paris group = inotify_new_group(user, inotify_max_queued_events); 73663c882a0SEric Paris if (IS_ERR(group)) { 73763c882a0SEric Paris ret = PTR_ERR(group); 738272eb014SEric Paris goto out_free_uid; 739272eb014SEric Paris } 740272eb014SEric Paris 741825f9692SAl Viro atomic_inc(&user->inotify_devs); 742825f9692SAl Viro 743c44dcc56SAl Viro ret = anon_inode_getfd("inotify", &inotify_fops, group, 744c44dcc56SAl Viro O_RDONLY | flags); 745c44dcc56SAl Viro if (ret >= 0) 746c44dcc56SAl Viro return ret; 747825f9692SAl Viro 748825f9692SAl Viro atomic_dec(&user->inotify_devs); 749272eb014SEric Paris out_free_uid: 750272eb014SEric Paris free_uid(user); 751272eb014SEric Paris return ret; 752272eb014SEric Paris } 753272eb014SEric Paris 754938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init) 755272eb014SEric Paris { 756272eb014SEric Paris return sys_inotify_init1(0); 757272eb014SEric Paris } 758272eb014SEric Paris 7592e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, 7602e4d0924SHeiko Carstens u32, mask) 761272eb014SEric Paris { 76263c882a0SEric Paris struct fsnotify_group *group; 763272eb014SEric Paris struct inode *inode; 764272eb014SEric Paris struct path path; 765272eb014SEric Paris struct file *filp; 766272eb014SEric Paris int ret, fput_needed; 767272eb014SEric Paris unsigned flags = 0; 768272eb014SEric Paris 769272eb014SEric Paris filp = fget_light(fd, &fput_needed); 770272eb014SEric Paris if (unlikely(!filp)) 771272eb014SEric Paris return -EBADF; 772272eb014SEric Paris 773272eb014SEric Paris /* verify that this is indeed an inotify instance */ 774272eb014SEric Paris if (unlikely(filp->f_op != &inotify_fops)) { 775272eb014SEric Paris ret = -EINVAL; 776272eb014SEric Paris goto fput_and_out; 777272eb014SEric Paris } 778272eb014SEric Paris 779272eb014SEric Paris if (!(mask & IN_DONT_FOLLOW)) 780272eb014SEric Paris flags |= LOOKUP_FOLLOW; 781272eb014SEric Paris if (mask & IN_ONLYDIR) 782272eb014SEric Paris flags |= LOOKUP_DIRECTORY; 783272eb014SEric Paris 78463c882a0SEric Paris ret = inotify_find_inode(pathname, &path, flags); 78563c882a0SEric Paris if (ret) 786272eb014SEric Paris goto fput_and_out; 787272eb014SEric Paris 78863c882a0SEric Paris /* inode held in place by reference to path; group by fget on fd */ 789272eb014SEric Paris inode = path.dentry->d_inode; 79063c882a0SEric Paris group = filp->private_data; 791272eb014SEric Paris 79263c882a0SEric Paris /* create/update an inode mark */ 79363c882a0SEric Paris ret = inotify_update_watch(group, inode, mask); 794272eb014SEric Paris path_put(&path); 795272eb014SEric Paris fput_and_out: 796272eb014SEric Paris fput_light(filp, fput_needed); 797272eb014SEric Paris return ret; 798272eb014SEric Paris } 799272eb014SEric Paris 8002e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd) 801272eb014SEric Paris { 80263c882a0SEric Paris struct fsnotify_group *group; 803000285deSEric Paris struct inotify_inode_mark *i_mark; 804272eb014SEric Paris struct file *filp; 80563c882a0SEric Paris int ret = 0, fput_needed; 806272eb014SEric Paris 807272eb014SEric Paris filp = fget_light(fd, &fput_needed); 808272eb014SEric Paris if (unlikely(!filp)) 809272eb014SEric Paris return -EBADF; 810272eb014SEric Paris 811272eb014SEric Paris /* verify that this is indeed an inotify instance */ 812272eb014SEric Paris ret = -EINVAL; 813b7ba8371SEric Paris if (unlikely(filp->f_op != &inotify_fops)) 814272eb014SEric Paris goto out; 815272eb014SEric Paris 81663c882a0SEric Paris group = filp->private_data; 817272eb014SEric Paris 81863c882a0SEric Paris ret = -EINVAL; 819000285deSEric Paris i_mark = inotify_idr_find(group, wd); 820000285deSEric Paris if (unlikely(!i_mark)) 82163c882a0SEric Paris goto out; 82263c882a0SEric Paris 823b7ba8371SEric Paris ret = 0; 824b7ba8371SEric Paris 825000285deSEric Paris fsnotify_destroy_mark(&i_mark->fsn_mark); 826b7ba8371SEric Paris 827b7ba8371SEric Paris /* match ref taken by inotify_idr_find */ 828000285deSEric Paris fsnotify_put_mark(&i_mark->fsn_mark); 829272eb014SEric Paris 830272eb014SEric Paris out: 831272eb014SEric Paris fput_light(filp, fput_needed); 832272eb014SEric Paris return ret; 833272eb014SEric Paris } 834272eb014SEric Paris 835272eb014SEric Paris /* 836272eb014SEric Paris * inotify_user_setup - Our initialization function. Note that we cannnot return 837272eb014SEric Paris * error because we have compiled-in VFS hooks. So an (unlikely) failure here 838272eb014SEric Paris * must result in panic(). 839272eb014SEric Paris */ 840272eb014SEric Paris static int __init inotify_user_setup(void) 841272eb014SEric Paris { 842000285deSEric Paris inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC); 84363c882a0SEric Paris event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC); 84463c882a0SEric Paris 845272eb014SEric Paris inotify_max_queued_events = 16384; 846272eb014SEric Paris inotify_max_user_instances = 128; 847272eb014SEric Paris inotify_max_user_watches = 8192; 848272eb014SEric Paris 849272eb014SEric Paris return 0; 850272eb014SEric Paris } 851272eb014SEric Paris module_init(inotify_user_setup); 852