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" 43be77196bSCyrill Gorcunov #include "../fdinfo.h" 44272eb014SEric Paris 45272eb014SEric Paris #include <asm/ioctls.h> 46272eb014SEric Paris 47272eb014SEric Paris /* these are configurable via /proc/sys/fs/inotify/ */ 48272eb014SEric Paris static int inotify_max_user_instances __read_mostly; 49272eb014SEric Paris static int inotify_max_queued_events __read_mostly; 500a24887aSH Hartley Sweeten static int inotify_max_user_watches __read_mostly; 5163c882a0SEric Paris 5263c882a0SEric Paris static struct kmem_cache *inotify_inode_mark_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 60*92f778ddSJoe Perches struct 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 */ 1008c1934c8SEric 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 1267053aee2SJan Kara static int round_event_name_len(struct fsnotify_event *fsn_event) 127e9fe6904SJan Kara { 1287053aee2SJan Kara struct inotify_event_info *event; 1297053aee2SJan Kara 1307053aee2SJan Kara event = INOTIFY_E(fsn_event); 131e9fe6904SJan Kara if (!event->name_len) 132e9fe6904SJan Kara return 0; 133e9fe6904SJan Kara return roundup(event->name_len + 1, sizeof(struct inotify_event)); 134e9fe6904SJan Kara } 135e9fe6904SJan Kara 1363632dee2SVegard Nossum /* 1373632dee2SVegard Nossum * Get an inotify_kernel_event if one exists and is small 1383632dee2SVegard Nossum * enough to fit in "count". Return an error pointer if 1393632dee2SVegard Nossum * not large enough. 1403632dee2SVegard Nossum * 14163c882a0SEric Paris * Called with the group->notification_mutex held. 1423632dee2SVegard Nossum */ 14363c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group, 1443632dee2SVegard Nossum size_t count) 1453632dee2SVegard Nossum { 1463632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 14763c882a0SEric Paris struct fsnotify_event *event; 1483632dee2SVegard Nossum 14963c882a0SEric Paris if (fsnotify_notify_queue_is_empty(group)) 1503632dee2SVegard Nossum return NULL; 1513632dee2SVegard Nossum 15263c882a0SEric Paris event = fsnotify_peek_notify_event(group); 15363c882a0SEric Paris 1545ba08e2eSEric Paris pr_debug("%s: group=%p event=%p\n", __func__, group, event); 1555ba08e2eSEric Paris 156e9fe6904SJan Kara event_size += round_event_name_len(event); 1573632dee2SVegard Nossum if (event_size > count) 1583632dee2SVegard Nossum return ERR_PTR(-EINVAL); 1593632dee2SVegard Nossum 16063c882a0SEric Paris /* held the notification_mutex the whole time, so this is the 16163c882a0SEric Paris * same event we peeked above */ 16263c882a0SEric Paris fsnotify_remove_notify_event(group); 16363c882a0SEric Paris 16463c882a0SEric Paris return event; 1653632dee2SVegard Nossum } 1663632dee2SVegard Nossum 1673632dee2SVegard Nossum /* 1683632dee2SVegard Nossum * Copy an event to user space, returning how much we copied. 1693632dee2SVegard Nossum * 1703632dee2SVegard Nossum * We already checked that the event size is smaller than the 1713632dee2SVegard Nossum * buffer we had in "get_one_event()" above. 1723632dee2SVegard Nossum */ 17363c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group, 1747053aee2SJan Kara struct fsnotify_event *fsn_event, 1753632dee2SVegard Nossum char __user *buf) 1763632dee2SVegard Nossum { 17763c882a0SEric Paris struct inotify_event inotify_event; 1787053aee2SJan Kara struct inotify_event_info *event; 1793632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 180e9fe6904SJan Kara size_t name_len; 181e9fe6904SJan Kara size_t pad_name_len; 1823632dee2SVegard Nossum 1837053aee2SJan Kara pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event); 1845ba08e2eSEric Paris 1857053aee2SJan Kara event = INOTIFY_E(fsn_event); 186e9fe6904SJan Kara name_len = event->name_len; 187b962e731SBrian Rogers /* 188e9fe6904SJan Kara * round up name length so it is a multiple of event_size 1890db501bdSEric W. Biederman * plus an extra byte for the terminating '\0'. 1900db501bdSEric W. Biederman */ 1917053aee2SJan Kara pad_name_len = round_event_name_len(fsn_event); 192e9fe6904SJan Kara inotify_event.len = pad_name_len; 1937053aee2SJan Kara inotify_event.mask = inotify_mask_to_arg(fsn_event->mask); 1947053aee2SJan Kara inotify_event.wd = event->wd; 19563c882a0SEric Paris inotify_event.cookie = event->sync_cookie; 19663c882a0SEric Paris 19763c882a0SEric Paris /* send the main event */ 19863c882a0SEric Paris if (copy_to_user(buf, &inotify_event, event_size)) 1993632dee2SVegard Nossum return -EFAULT; 2003632dee2SVegard Nossum 2013632dee2SVegard Nossum buf += event_size; 2023632dee2SVegard Nossum 20363c882a0SEric Paris /* 20463c882a0SEric Paris * fsnotify only stores the pathname, so here we have to send the pathname 20563c882a0SEric Paris * and then pad that pathname out to a multiple of sizeof(inotify_event) 206e9fe6904SJan Kara * with zeros. 20763c882a0SEric Paris */ 208e9fe6904SJan Kara if (pad_name_len) { 20963c882a0SEric Paris /* copy the path name */ 2107053aee2SJan Kara if (copy_to_user(buf, event->name, name_len)) 2113632dee2SVegard Nossum return -EFAULT; 212e9fe6904SJan Kara buf += name_len; 2133632dee2SVegard Nossum 2140db501bdSEric W. Biederman /* fill userspace with 0's */ 215e9fe6904SJan Kara if (clear_user(buf, pad_name_len - name_len)) 21663c882a0SEric Paris return -EFAULT; 217e9fe6904SJan Kara event_size += pad_name_len; 2183632dee2SVegard Nossum } 21963c882a0SEric Paris 2203632dee2SVegard Nossum return event_size; 2213632dee2SVegard Nossum } 2223632dee2SVegard Nossum 223272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf, 224272eb014SEric Paris size_t count, loff_t *pos) 225272eb014SEric Paris { 22663c882a0SEric Paris struct fsnotify_group *group; 22763c882a0SEric Paris struct fsnotify_event *kevent; 228272eb014SEric Paris char __user *start; 229272eb014SEric Paris int ret; 230272eb014SEric Paris DEFINE_WAIT(wait); 231272eb014SEric Paris 232272eb014SEric Paris start = buf; 23363c882a0SEric Paris group = file->private_data; 234272eb014SEric Paris 235272eb014SEric Paris while (1) { 23663c882a0SEric Paris prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE); 237272eb014SEric Paris 23863c882a0SEric Paris mutex_lock(&group->notification_mutex); 23963c882a0SEric Paris kevent = get_one_event(group, count); 24063c882a0SEric Paris mutex_unlock(&group->notification_mutex); 241272eb014SEric Paris 2425ba08e2eSEric Paris pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent); 2435ba08e2eSEric Paris 2443632dee2SVegard Nossum if (kevent) { 2453632dee2SVegard Nossum ret = PTR_ERR(kevent); 2463632dee2SVegard Nossum if (IS_ERR(kevent)) 247272eb014SEric Paris break; 24863c882a0SEric Paris ret = copy_event_to_user(group, kevent, buf); 2497053aee2SJan Kara fsnotify_destroy_event(group, kevent); 2503632dee2SVegard Nossum if (ret < 0) 2513632dee2SVegard Nossum break; 2523632dee2SVegard Nossum buf += ret; 2533632dee2SVegard Nossum count -= ret; 2543632dee2SVegard Nossum continue; 255272eb014SEric Paris } 256272eb014SEric Paris 2573632dee2SVegard Nossum ret = -EAGAIN; 2583632dee2SVegard Nossum if (file->f_flags & O_NONBLOCK) 259272eb014SEric Paris break; 2601ca39ab9SEric Paris ret = -ERESTARTSYS; 2613632dee2SVegard Nossum if (signal_pending(current)) 2623632dee2SVegard Nossum break; 2633632dee2SVegard Nossum 2643632dee2SVegard Nossum if (start != buf) 2653632dee2SVegard Nossum break; 266272eb014SEric Paris 267272eb014SEric Paris schedule(); 268272eb014SEric Paris } 269272eb014SEric Paris 27063c882a0SEric Paris finish_wait(&group->notification_waitq, &wait); 2713632dee2SVegard Nossum if (start != buf && ret != -EFAULT) 272272eb014SEric Paris ret = buf - start; 273272eb014SEric Paris return ret; 274272eb014SEric Paris } 275272eb014SEric Paris 276272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file) 277272eb014SEric Paris { 27863c882a0SEric Paris struct fsnotify_group *group = file->private_data; 279272eb014SEric Paris 2805ba08e2eSEric Paris pr_debug("%s: group=%p\n", __func__, group); 2815ba08e2eSEric Paris 28263c882a0SEric Paris /* free this group, matching get was inotify_init->fsnotify_obtain_group */ 283d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 284272eb014SEric Paris 285272eb014SEric Paris return 0; 286272eb014SEric Paris } 287272eb014SEric Paris 288272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd, 289272eb014SEric Paris unsigned long arg) 290272eb014SEric Paris { 29163c882a0SEric Paris struct fsnotify_group *group; 2927053aee2SJan Kara struct fsnotify_event *fsn_event; 293272eb014SEric Paris void __user *p; 294272eb014SEric Paris int ret = -ENOTTY; 29563c882a0SEric Paris size_t send_len = 0; 296272eb014SEric Paris 29763c882a0SEric Paris group = file->private_data; 298272eb014SEric Paris p = (void __user *) arg; 299272eb014SEric Paris 3005ba08e2eSEric Paris pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd); 3015ba08e2eSEric Paris 302272eb014SEric Paris switch (cmd) { 303272eb014SEric Paris case FIONREAD: 30463c882a0SEric Paris mutex_lock(&group->notification_mutex); 3057053aee2SJan Kara list_for_each_entry(fsn_event, &group->notification_list, 3067053aee2SJan Kara list) { 30763c882a0SEric Paris send_len += sizeof(struct inotify_event); 3087053aee2SJan Kara send_len += round_event_name_len(fsn_event); 30963c882a0SEric Paris } 31063c882a0SEric Paris mutex_unlock(&group->notification_mutex); 31163c882a0SEric Paris ret = put_user(send_len, (int __user *) p); 312272eb014SEric Paris break; 313272eb014SEric Paris } 314272eb014SEric Paris 315272eb014SEric Paris return ret; 316272eb014SEric Paris } 317272eb014SEric Paris 318272eb014SEric Paris static const struct file_operations inotify_fops = { 319be77196bSCyrill Gorcunov .show_fdinfo = inotify_show_fdinfo, 320272eb014SEric Paris .poll = inotify_poll, 321272eb014SEric Paris .read = inotify_read, 3220a6b6bd5SEric Paris .fasync = fsnotify_fasync, 323272eb014SEric Paris .release = inotify_release, 324272eb014SEric Paris .unlocked_ioctl = inotify_ioctl, 325272eb014SEric Paris .compat_ioctl = inotify_ioctl, 3266038f373SArnd Bergmann .llseek = noop_llseek, 327272eb014SEric Paris }; 328272eb014SEric Paris 329272eb014SEric Paris 33063c882a0SEric Paris /* 33163c882a0SEric Paris * find_inode - resolve a user-given path to a specific inode 33263c882a0SEric Paris */ 33363c882a0SEric Paris static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags) 33463c882a0SEric Paris { 33563c882a0SEric Paris int error; 33663c882a0SEric Paris 33763c882a0SEric Paris error = user_path_at(AT_FDCWD, dirname, flags, path); 33863c882a0SEric Paris if (error) 33963c882a0SEric Paris return error; 34063c882a0SEric Paris /* you can only watch an inode if you have read permissions on it */ 34163c882a0SEric Paris error = inode_permission(path->dentry->d_inode, MAY_READ); 34263c882a0SEric Paris if (error) 34363c882a0SEric Paris path_put(path); 34463c882a0SEric Paris return error; 34563c882a0SEric Paris } 34663c882a0SEric Paris 347b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock, 348000285deSEric Paris struct inotify_inode_mark *i_mark) 349b7ba8371SEric Paris { 350b7ba8371SEric Paris int ret; 351b7ba8371SEric Paris 3524542da63STejun Heo idr_preload(GFP_KERNEL); 353b7ba8371SEric Paris spin_lock(idr_lock); 3544542da63STejun Heo 355a66c04b4SJeff Layton ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT); 3564542da63STejun Heo if (ret >= 0) { 357b7ba8371SEric Paris /* we added the mark to the idr, take a reference */ 3584542da63STejun Heo i_mark->wd = ret; 359000285deSEric Paris fsnotify_get_mark(&i_mark->fsn_mark); 3607050c488SEric Paris } 361b7ba8371SEric Paris 3624542da63STejun Heo spin_unlock(idr_lock); 3634542da63STejun Heo idr_preload_end(); 3644542da63STejun Heo return ret < 0 ? ret : 0; 365b7ba8371SEric Paris } 366b7ba8371SEric Paris 367000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group, 368b7ba8371SEric Paris int wd) 369b7ba8371SEric Paris { 370b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 371b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 372000285deSEric Paris struct inotify_inode_mark *i_mark; 373b7ba8371SEric Paris 374b7ba8371SEric Paris assert_spin_locked(idr_lock); 375b7ba8371SEric Paris 376000285deSEric Paris i_mark = idr_find(idr, wd); 377000285deSEric Paris if (i_mark) { 378000285deSEric Paris struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark; 379b7ba8371SEric Paris 380000285deSEric Paris fsnotify_get_mark(fsn_mark); 381b7ba8371SEric Paris /* One ref for being in the idr, one ref we just took */ 382000285deSEric Paris BUG_ON(atomic_read(&fsn_mark->refcnt) < 2); 383b7ba8371SEric Paris } 384b7ba8371SEric Paris 385000285deSEric Paris return i_mark; 386b7ba8371SEric Paris } 387b7ba8371SEric Paris 388000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group, 389b7ba8371SEric Paris int wd) 390b7ba8371SEric Paris { 391000285deSEric Paris struct inotify_inode_mark *i_mark; 392b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 393b7ba8371SEric Paris 394b7ba8371SEric Paris spin_lock(idr_lock); 395000285deSEric Paris i_mark = inotify_idr_find_locked(group, wd); 396b7ba8371SEric Paris spin_unlock(idr_lock); 397b7ba8371SEric Paris 398000285deSEric Paris return i_mark; 399b7ba8371SEric Paris } 400b7ba8371SEric Paris 401b7ba8371SEric Paris static void do_inotify_remove_from_idr(struct fsnotify_group *group, 402000285deSEric Paris struct inotify_inode_mark *i_mark) 403b7ba8371SEric Paris { 404b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 405b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 406000285deSEric Paris int wd = i_mark->wd; 407b7ba8371SEric Paris 408b7ba8371SEric Paris assert_spin_locked(idr_lock); 409b7ba8371SEric Paris 410b7ba8371SEric Paris idr_remove(idr, wd); 411b7ba8371SEric Paris 412b7ba8371SEric Paris /* removed from the idr, drop that ref */ 413000285deSEric Paris fsnotify_put_mark(&i_mark->fsn_mark); 414b7ba8371SEric Paris } 415b7ba8371SEric Paris 416dead537dSEric Paris /* 417dead537dSEric Paris * Remove the mark from the idr (if present) and drop the reference 418dead537dSEric Paris * on the mark because it was in the idr. 419dead537dSEric Paris */ 4207e790dd5SEric Paris static void inotify_remove_from_idr(struct fsnotify_group *group, 421000285deSEric Paris struct inotify_inode_mark *i_mark) 4227e790dd5SEric Paris { 423b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 424000285deSEric Paris struct inotify_inode_mark *found_i_mark = NULL; 425dead537dSEric Paris int wd; 4267e790dd5SEric Paris 427b7ba8371SEric Paris spin_lock(idr_lock); 428000285deSEric Paris wd = i_mark->wd; 429dead537dSEric Paris 430b7ba8371SEric Paris /* 431000285deSEric Paris * does this i_mark think it is in the idr? we shouldn't get called 432b7ba8371SEric Paris * if it wasn't.... 433b7ba8371SEric Paris */ 434b7ba8371SEric Paris if (wd == -1) { 435000285deSEric Paris WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p" 436000285deSEric Paris " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd, 437000285deSEric Paris i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode); 438dead537dSEric Paris goto out; 4397e790dd5SEric Paris } 440dead537dSEric Paris 441b7ba8371SEric Paris /* Lets look in the idr to see if we find it */ 442000285deSEric Paris found_i_mark = inotify_idr_find_locked(group, wd); 443000285deSEric Paris if (unlikely(!found_i_mark)) { 444000285deSEric Paris WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p" 445000285deSEric Paris " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd, 446000285deSEric Paris i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode); 447b7ba8371SEric Paris goto out; 448b7ba8371SEric Paris } 449dead537dSEric Paris 450b7ba8371SEric Paris /* 451000285deSEric Paris * We found an mark in the idr at the right wd, but it's 452000285deSEric Paris * not the mark we were told to remove. eparis seriously 453b7ba8371SEric Paris * fucked up somewhere. 454b7ba8371SEric Paris */ 455000285deSEric Paris if (unlikely(found_i_mark != i_mark)) { 456000285deSEric Paris WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p " 457000285deSEric Paris "mark->inode=%p found_i_mark=%p found_i_mark->wd=%d " 458000285deSEric Paris "found_i_mark->group=%p found_i_mark->inode=%p\n", 459000285deSEric Paris __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group, 460000285deSEric Paris i_mark->fsn_mark.i.inode, found_i_mark, found_i_mark->wd, 461000285deSEric Paris found_i_mark->fsn_mark.group, 462000285deSEric Paris found_i_mark->fsn_mark.i.inode); 463b7ba8371SEric Paris goto out; 464b7ba8371SEric Paris } 465dead537dSEric Paris 466b7ba8371SEric Paris /* 467b7ba8371SEric Paris * One ref for being in the idr 468b7ba8371SEric Paris * one ref held by the caller trying to kill us 469b7ba8371SEric Paris * one ref grabbed by inotify_idr_find 470b7ba8371SEric Paris */ 471000285deSEric Paris if (unlikely(atomic_read(&i_mark->fsn_mark.refcnt) < 3)) { 472000285deSEric Paris printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p" 473000285deSEric Paris " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd, 474000285deSEric Paris i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode); 475b7ba8371SEric Paris /* we can't really recover with bad ref cnting.. */ 476b7ba8371SEric Paris BUG(); 477b7ba8371SEric Paris } 478b7ba8371SEric Paris 479000285deSEric Paris do_inotify_remove_from_idr(group, i_mark); 480dead537dSEric Paris out: 481b7ba8371SEric Paris /* match the ref taken by inotify_idr_find_locked() */ 482000285deSEric Paris if (found_i_mark) 483000285deSEric Paris fsnotify_put_mark(&found_i_mark->fsn_mark); 484000285deSEric Paris i_mark->wd = -1; 485b7ba8371SEric Paris spin_unlock(idr_lock); 486dead537dSEric Paris } 487dead537dSEric Paris 48863c882a0SEric Paris /* 489dead537dSEric Paris * Send IN_IGNORED for this wd, remove this wd from the idr. 49063c882a0SEric Paris */ 491000285deSEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark, 492528da3e9SEric Paris struct fsnotify_group *group) 49363c882a0SEric Paris { 494000285deSEric Paris struct inotify_inode_mark *i_mark; 4957053aee2SJan Kara 4967053aee2SJan Kara /* Queue ignore event for the watch */ 4977053aee2SJan Kara inotify_handle_event(group, NULL, fsn_mark, NULL, FS_IN_IGNORED, 49845a22f4cSJan Kara NULL, FSNOTIFY_EVENT_NONE, NULL, 0); 49963c882a0SEric Paris 5008b99c3ccSLino Sanfilippo i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 501000285deSEric Paris /* remove this mark from the idr */ 502000285deSEric Paris inotify_remove_from_idr(group, i_mark); 50363c882a0SEric Paris 5045549f7cdSEric Paris atomic_dec(&group->inotify_data.user->inotify_watches); 50563c882a0SEric Paris } 50663c882a0SEric Paris 50763c882a0SEric Paris /* ding dong the mark is dead */ 508000285deSEric Paris static void inotify_free_mark(struct fsnotify_mark *fsn_mark) 50963c882a0SEric Paris { 510000285deSEric Paris struct inotify_inode_mark *i_mark; 51131ddd326SEric Paris 512000285deSEric Paris i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 51363c882a0SEric Paris 514000285deSEric Paris kmem_cache_free(inotify_inode_mark_cachep, i_mark); 51563c882a0SEric Paris } 51663c882a0SEric Paris 51752cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group, 51852cef755SEric Paris struct inode *inode, 51952cef755SEric Paris u32 arg) 52063c882a0SEric Paris { 521000285deSEric Paris struct fsnotify_mark *fsn_mark; 522000285deSEric Paris struct inotify_inode_mark *i_mark; 52363c882a0SEric Paris __u32 old_mask, new_mask; 52452cef755SEric Paris __u32 mask; 52552cef755SEric Paris int add = (arg & IN_MASK_ADD); 52652cef755SEric Paris int ret; 52763c882a0SEric Paris 52863c882a0SEric Paris mask = inotify_arg_to_mask(arg); 52963c882a0SEric Paris 5305444e298SEric Paris fsn_mark = fsnotify_find_inode_mark(group, inode); 531000285deSEric Paris if (!fsn_mark) 53252cef755SEric Paris return -ENOENT; 53352cef755SEric Paris 534000285deSEric Paris i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 53575fe2b26SEric Paris 536000285deSEric Paris spin_lock(&fsn_mark->lock); 53763c882a0SEric Paris 538000285deSEric Paris old_mask = fsn_mark->mask; 53990b1e7a5SEric Paris if (add) 54090b1e7a5SEric Paris fsnotify_set_mark_mask_locked(fsn_mark, (fsn_mark->mask | mask)); 54190b1e7a5SEric Paris else 54290b1e7a5SEric Paris fsnotify_set_mark_mask_locked(fsn_mark, mask); 543000285deSEric Paris new_mask = fsn_mark->mask; 54463c882a0SEric Paris 545000285deSEric Paris spin_unlock(&fsn_mark->lock); 54663c882a0SEric Paris 54763c882a0SEric Paris if (old_mask != new_mask) { 54863c882a0SEric Paris /* more bits in old than in new? */ 54963c882a0SEric Paris int dropped = (old_mask & ~new_mask); 550000285deSEric Paris /* more bits in this fsn_mark than the inode's mask? */ 55163c882a0SEric Paris int do_inode = (new_mask & ~inode->i_fsnotify_mask); 55263c882a0SEric Paris 553000285deSEric Paris /* update the inode with this new fsn_mark */ 55463c882a0SEric Paris if (dropped || do_inode) 55563c882a0SEric Paris fsnotify_recalc_inode_mask(inode); 55663c882a0SEric Paris 55763c882a0SEric Paris } 55863c882a0SEric Paris 55952cef755SEric Paris /* return the wd */ 560000285deSEric Paris ret = i_mark->wd; 56152cef755SEric Paris 562d0775441SEric Paris /* match the get from fsnotify_find_mark() */ 563000285deSEric Paris fsnotify_put_mark(fsn_mark); 56475fe2b26SEric Paris 56552cef755SEric Paris return ret; 56663c882a0SEric Paris } 5677e790dd5SEric Paris 56852cef755SEric Paris static int inotify_new_watch(struct fsnotify_group *group, 56952cef755SEric Paris struct inode *inode, 57052cef755SEric Paris u32 arg) 57152cef755SEric Paris { 572000285deSEric Paris struct inotify_inode_mark *tmp_i_mark; 57352cef755SEric Paris __u32 mask; 57452cef755SEric Paris int ret; 575b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 576b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 57752cef755SEric Paris 57852cef755SEric Paris mask = inotify_arg_to_mask(arg); 57952cef755SEric Paris 580000285deSEric Paris tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); 581000285deSEric Paris if (unlikely(!tmp_i_mark)) 58252cef755SEric Paris return -ENOMEM; 58352cef755SEric Paris 584000285deSEric Paris fsnotify_init_mark(&tmp_i_mark->fsn_mark, inotify_free_mark); 585000285deSEric Paris tmp_i_mark->fsn_mark.mask = mask; 586000285deSEric Paris tmp_i_mark->wd = -1; 58752cef755SEric Paris 58852cef755SEric Paris ret = -ENOSPC; 58952cef755SEric Paris if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches) 59052cef755SEric Paris goto out_err; 591b7ba8371SEric Paris 592a66c04b4SJeff Layton ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark); 593b7ba8371SEric Paris if (ret) 59452cef755SEric Paris goto out_err; 59552cef755SEric Paris 59652cef755SEric Paris /* we are on the idr, now get on the inode */ 597e1e5a9f8SLino Sanfilippo ret = fsnotify_add_mark_locked(&tmp_i_mark->fsn_mark, group, inode, 598e1e5a9f8SLino Sanfilippo NULL, 0); 59952cef755SEric Paris if (ret) { 60052cef755SEric Paris /* we failed to get on the inode, get off the idr */ 601000285deSEric Paris inotify_remove_from_idr(group, tmp_i_mark); 60252cef755SEric Paris goto out_err; 60352cef755SEric Paris } 60452cef755SEric Paris 60552cef755SEric Paris /* increment the number of watches the user has */ 60652cef755SEric Paris atomic_inc(&group->inotify_data.user->inotify_watches); 60752cef755SEric Paris 608000285deSEric Paris /* return the watch descriptor for this new mark */ 609000285deSEric Paris ret = tmp_i_mark->wd; 61052cef755SEric Paris 61152cef755SEric Paris out_err: 612000285deSEric Paris /* match the ref from fsnotify_init_mark() */ 613000285deSEric Paris fsnotify_put_mark(&tmp_i_mark->fsn_mark); 61452cef755SEric Paris 61552cef755SEric Paris return ret; 61652cef755SEric Paris } 61752cef755SEric Paris 61852cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg) 61952cef755SEric Paris { 62052cef755SEric Paris int ret = 0; 62152cef755SEric Paris 622e1e5a9f8SLino Sanfilippo mutex_lock(&group->mark_mutex); 62352cef755SEric Paris /* try to update and existing watch with the new arg */ 62452cef755SEric Paris ret = inotify_update_existing_watch(group, inode, arg); 62552cef755SEric Paris /* no mark present, try to add a new one */ 62652cef755SEric Paris if (ret == -ENOENT) 62752cef755SEric Paris ret = inotify_new_watch(group, inode, arg); 628e1e5a9f8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 62952cef755SEric Paris 63063c882a0SEric Paris return ret; 63163c882a0SEric Paris } 63263c882a0SEric Paris 633d0de4dc5SEric Paris static struct fsnotify_group *inotify_new_group(unsigned int max_events) 63463c882a0SEric Paris { 63563c882a0SEric Paris struct fsnotify_group *group; 636ff57cd58SJan Kara struct inotify_event_info *oevent; 63763c882a0SEric Paris 6380d2e2a1dSEric Paris group = fsnotify_alloc_group(&inotify_fsnotify_ops); 63963c882a0SEric Paris if (IS_ERR(group)) 64063c882a0SEric Paris return group; 64163c882a0SEric Paris 642ff57cd58SJan Kara oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL); 643ff57cd58SJan Kara if (unlikely(!oevent)) { 644ff57cd58SJan Kara fsnotify_destroy_group(group); 645ff57cd58SJan Kara return ERR_PTR(-ENOMEM); 646ff57cd58SJan Kara } 647ff57cd58SJan Kara group->overflow_event = &oevent->fse; 648ff57cd58SJan Kara fsnotify_init_event(group->overflow_event, NULL, FS_Q_OVERFLOW); 649ff57cd58SJan Kara oevent->wd = -1; 650ff57cd58SJan Kara oevent->sync_cookie = 0; 651ff57cd58SJan Kara oevent->name_len = 0; 652ff57cd58SJan Kara 65363c882a0SEric Paris group->max_events = max_events; 65463c882a0SEric Paris 65563c882a0SEric Paris spin_lock_init(&group->inotify_data.idr_lock); 65663c882a0SEric Paris idr_init(&group->inotify_data.idr); 657d0de4dc5SEric Paris group->inotify_data.user = get_current_user(); 658d0de4dc5SEric Paris 659d0de4dc5SEric Paris if (atomic_inc_return(&group->inotify_data.user->inotify_devs) > 660d0de4dc5SEric Paris inotify_max_user_instances) { 661d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 662d0de4dc5SEric Paris return ERR_PTR(-EMFILE); 663d0de4dc5SEric Paris } 66463c882a0SEric Paris 66563c882a0SEric Paris return group; 66663c882a0SEric Paris } 66763c882a0SEric Paris 66863c882a0SEric Paris 66963c882a0SEric Paris /* inotify syscalls */ 670938bb9f5SHeiko Carstens SYSCALL_DEFINE1(inotify_init1, int, flags) 671272eb014SEric Paris { 67263c882a0SEric Paris struct fsnotify_group *group; 673c44dcc56SAl Viro int ret; 674272eb014SEric Paris 675272eb014SEric Paris /* Check the IN_* constants for consistency. */ 676272eb014SEric Paris BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC); 677272eb014SEric Paris BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK); 678272eb014SEric Paris 679272eb014SEric Paris if (flags & ~(IN_CLOEXEC | IN_NONBLOCK)) 680272eb014SEric Paris return -EINVAL; 681272eb014SEric Paris 68263c882a0SEric Paris /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */ 683d0de4dc5SEric Paris group = inotify_new_group(inotify_max_queued_events); 684d0de4dc5SEric Paris if (IS_ERR(group)) 685d0de4dc5SEric Paris return PTR_ERR(group); 686825f9692SAl Viro 687c44dcc56SAl Viro ret = anon_inode_getfd("inotify", &inotify_fops, group, 688c44dcc56SAl Viro O_RDONLY | flags); 689d0de4dc5SEric Paris if (ret < 0) 690d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 691d0de4dc5SEric Paris 692272eb014SEric Paris return ret; 693272eb014SEric Paris } 694272eb014SEric Paris 695938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init) 696272eb014SEric Paris { 697272eb014SEric Paris return sys_inotify_init1(0); 698272eb014SEric Paris } 699272eb014SEric Paris 7002e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, 7012e4d0924SHeiko Carstens u32, mask) 702272eb014SEric Paris { 70363c882a0SEric Paris struct fsnotify_group *group; 704272eb014SEric Paris struct inode *inode; 705272eb014SEric Paris struct path path; 7062903ff01SAl Viro struct fd f; 7072903ff01SAl Viro int ret; 708272eb014SEric Paris unsigned flags = 0; 709272eb014SEric Paris 71004df32faSZhao Hongjiang /* don't allow invalid bits: we don't want flags set */ 71104df32faSZhao Hongjiang if (unlikely(!(mask & ALL_INOTIFY_BITS))) 71204df32faSZhao Hongjiang return -EINVAL; 71304df32faSZhao Hongjiang 7142903ff01SAl Viro f = fdget(fd); 7152903ff01SAl Viro if (unlikely(!f.file)) 716272eb014SEric Paris return -EBADF; 717272eb014SEric Paris 718272eb014SEric Paris /* verify that this is indeed an inotify instance */ 7192903ff01SAl Viro if (unlikely(f.file->f_op != &inotify_fops)) { 720272eb014SEric Paris ret = -EINVAL; 721272eb014SEric Paris goto fput_and_out; 722272eb014SEric Paris } 723272eb014SEric Paris 724272eb014SEric Paris if (!(mask & IN_DONT_FOLLOW)) 725272eb014SEric Paris flags |= LOOKUP_FOLLOW; 726272eb014SEric Paris if (mask & IN_ONLYDIR) 727272eb014SEric Paris flags |= LOOKUP_DIRECTORY; 728272eb014SEric Paris 72963c882a0SEric Paris ret = inotify_find_inode(pathname, &path, flags); 73063c882a0SEric Paris if (ret) 731272eb014SEric Paris goto fput_and_out; 732272eb014SEric Paris 73363c882a0SEric Paris /* inode held in place by reference to path; group by fget on fd */ 734272eb014SEric Paris inode = path.dentry->d_inode; 7352903ff01SAl Viro group = f.file->private_data; 736272eb014SEric Paris 73763c882a0SEric Paris /* create/update an inode mark */ 73863c882a0SEric Paris ret = inotify_update_watch(group, inode, mask); 739272eb014SEric Paris path_put(&path); 740272eb014SEric Paris fput_and_out: 7412903ff01SAl Viro fdput(f); 742272eb014SEric Paris return ret; 743272eb014SEric Paris } 744272eb014SEric Paris 7452e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd) 746272eb014SEric Paris { 74763c882a0SEric Paris struct fsnotify_group *group; 748000285deSEric Paris struct inotify_inode_mark *i_mark; 7492903ff01SAl Viro struct fd f; 7502903ff01SAl Viro int ret = 0; 751272eb014SEric Paris 7522903ff01SAl Viro f = fdget(fd); 7532903ff01SAl Viro if (unlikely(!f.file)) 754272eb014SEric Paris return -EBADF; 755272eb014SEric Paris 756272eb014SEric Paris /* verify that this is indeed an inotify instance */ 757272eb014SEric Paris ret = -EINVAL; 7582903ff01SAl Viro if (unlikely(f.file->f_op != &inotify_fops)) 759272eb014SEric Paris goto out; 760272eb014SEric Paris 7612903ff01SAl Viro group = f.file->private_data; 762272eb014SEric Paris 76363c882a0SEric Paris ret = -EINVAL; 764000285deSEric Paris i_mark = inotify_idr_find(group, wd); 765000285deSEric Paris if (unlikely(!i_mark)) 76663c882a0SEric Paris goto out; 76763c882a0SEric Paris 768b7ba8371SEric Paris ret = 0; 769b7ba8371SEric Paris 770e2a29943SLino Sanfilippo fsnotify_destroy_mark(&i_mark->fsn_mark, group); 771b7ba8371SEric Paris 772b7ba8371SEric Paris /* match ref taken by inotify_idr_find */ 773000285deSEric Paris fsnotify_put_mark(&i_mark->fsn_mark); 774272eb014SEric Paris 775272eb014SEric Paris out: 7762903ff01SAl Viro fdput(f); 777272eb014SEric Paris return ret; 778272eb014SEric Paris } 779272eb014SEric Paris 780272eb014SEric Paris /* 781ae0e47f0SJustin P. Mattock * inotify_user_setup - Our initialization function. Note that we cannot return 782272eb014SEric Paris * error because we have compiled-in VFS hooks. So an (unlikely) failure here 783272eb014SEric Paris * must result in panic(). 784272eb014SEric Paris */ 785272eb014SEric Paris static int __init inotify_user_setup(void) 786272eb014SEric Paris { 787f874e1acSEric Paris BUILD_BUG_ON(IN_ACCESS != FS_ACCESS); 788f874e1acSEric Paris BUILD_BUG_ON(IN_MODIFY != FS_MODIFY); 789f874e1acSEric Paris BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB); 790f874e1acSEric Paris BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE); 791f874e1acSEric Paris BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); 792f874e1acSEric Paris BUILD_BUG_ON(IN_OPEN != FS_OPEN); 793f874e1acSEric Paris BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM); 794f874e1acSEric Paris BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO); 795f874e1acSEric Paris BUILD_BUG_ON(IN_CREATE != FS_CREATE); 796f874e1acSEric Paris BUILD_BUG_ON(IN_DELETE != FS_DELETE); 797f874e1acSEric Paris BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF); 798f874e1acSEric Paris BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF); 799f874e1acSEric Paris BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT); 800f874e1acSEric Paris BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW); 801f874e1acSEric Paris BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED); 802f874e1acSEric Paris BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK); 803b29866aaSEric Paris BUILD_BUG_ON(IN_ISDIR != FS_ISDIR); 804f874e1acSEric Paris BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT); 805f874e1acSEric Paris 806f874e1acSEric Paris BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21); 807f874e1acSEric Paris 808000285deSEric Paris inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC); 80963c882a0SEric Paris 810272eb014SEric Paris inotify_max_queued_events = 16384; 811272eb014SEric Paris inotify_max_user_instances = 128; 812272eb014SEric Paris inotify_max_user_watches = 8192; 813272eb014SEric Paris 814272eb014SEric Paris return 0; 815272eb014SEric Paris } 816272eb014SEric Paris module_init(inotify_user_setup); 817