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> 29c013d5a4SPaul Gortmaker #include <linux/init.h> /* fs_initcall */ 30272eb014SEric Paris #include <linux/inotify.h> 3163c882a0SEric Paris #include <linux/kernel.h> /* roundup() */ 3263c882a0SEric Paris #include <linux/namei.h> /* LOOKUP_FOLLOW */ 33174cd4b1SIngo Molnar #include <linux/sched/signal.h> 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 471cce1eeaSNikolay Borisov /* configurable via /proc/sys/fs/inotify/ */ 48272eb014SEric Paris static int inotify_max_queued_events __read_mostly; 4963c882a0SEric Paris 50054c636eSJan Kara struct kmem_cache *inotify_inode_mark_cachep __read_mostly; 51272eb014SEric Paris 52272eb014SEric Paris #ifdef CONFIG_SYSCTL 53272eb014SEric Paris 54272eb014SEric Paris #include <linux/sysctl.h> 55272eb014SEric Paris 56272eb014SEric Paris static int zero; 57272eb014SEric Paris 5892f778ddSJoe Perches struct ctl_table inotify_table[] = { 59272eb014SEric Paris { 60272eb014SEric Paris .procname = "max_user_instances", 611cce1eeaSNikolay Borisov .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES], 62272eb014SEric Paris .maxlen = sizeof(int), 63272eb014SEric Paris .mode = 0644, 646d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 65272eb014SEric Paris .extra1 = &zero, 66272eb014SEric Paris }, 67272eb014SEric Paris { 68272eb014SEric Paris .procname = "max_user_watches", 691cce1eeaSNikolay Borisov .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES], 70272eb014SEric Paris .maxlen = sizeof(int), 71272eb014SEric Paris .mode = 0644, 726d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 73272eb014SEric Paris .extra1 = &zero, 74272eb014SEric Paris }, 75272eb014SEric Paris { 76272eb014SEric Paris .procname = "max_queued_events", 77272eb014SEric Paris .data = &inotify_max_queued_events, 78272eb014SEric Paris .maxlen = sizeof(int), 79272eb014SEric Paris .mode = 0644, 806d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 81272eb014SEric Paris .extra1 = &zero 82272eb014SEric Paris }, 83ab09203eSEric W. Biederman { } 84272eb014SEric Paris }; 85272eb014SEric Paris #endif /* CONFIG_SYSCTL */ 86272eb014SEric Paris 8763c882a0SEric Paris static inline __u32 inotify_arg_to_mask(u32 arg) 88272eb014SEric Paris { 8963c882a0SEric Paris __u32 mask; 9063c882a0SEric Paris 91611da04fSEric Paris /* 92611da04fSEric Paris * everything should accept their own ignored, cares about children, 93611da04fSEric Paris * and should receive events when the inode is unmounted 94611da04fSEric Paris */ 95611da04fSEric Paris mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT); 9663c882a0SEric Paris 9763c882a0SEric Paris /* mask off the flags used to open the fd */ 988c1934c8SEric Paris mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK)); 9963c882a0SEric Paris 10063c882a0SEric Paris return mask; 101272eb014SEric Paris } 102272eb014SEric Paris 10363c882a0SEric Paris static inline u32 inotify_mask_to_arg(__u32 mask) 104272eb014SEric Paris { 10563c882a0SEric Paris return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED | 10663c882a0SEric Paris IN_Q_OVERFLOW); 107272eb014SEric Paris } 108272eb014SEric Paris 10963c882a0SEric Paris /* intofiy userspace file descriptor functions */ 110272eb014SEric Paris static unsigned int inotify_poll(struct file *file, poll_table *wait) 111272eb014SEric Paris { 11263c882a0SEric Paris struct fsnotify_group *group = file->private_data; 113272eb014SEric Paris int ret = 0; 114272eb014SEric Paris 11563c882a0SEric Paris poll_wait(file, &group->notification_waitq, wait); 116c21dbe20SJan Kara spin_lock(&group->notification_lock); 11763c882a0SEric Paris if (!fsnotify_notify_queue_is_empty(group)) 118272eb014SEric Paris ret = POLLIN | POLLRDNORM; 119c21dbe20SJan Kara spin_unlock(&group->notification_lock); 120272eb014SEric Paris 121272eb014SEric Paris return ret; 122272eb014SEric Paris } 123272eb014SEric Paris 1247053aee2SJan Kara static int round_event_name_len(struct fsnotify_event *fsn_event) 125e9fe6904SJan Kara { 1267053aee2SJan Kara struct inotify_event_info *event; 1277053aee2SJan Kara 1287053aee2SJan Kara event = INOTIFY_E(fsn_event); 129e9fe6904SJan Kara if (!event->name_len) 130e9fe6904SJan Kara return 0; 131e9fe6904SJan Kara return roundup(event->name_len + 1, sizeof(struct inotify_event)); 132e9fe6904SJan Kara } 133e9fe6904SJan Kara 1343632dee2SVegard Nossum /* 1353632dee2SVegard Nossum * Get an inotify_kernel_event if one exists and is small 1363632dee2SVegard Nossum * enough to fit in "count". Return an error pointer if 1373632dee2SVegard Nossum * not large enough. 1383632dee2SVegard Nossum * 139c21dbe20SJan Kara * Called with the group->notification_lock held. 1403632dee2SVegard Nossum */ 14163c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group, 1423632dee2SVegard Nossum size_t count) 1433632dee2SVegard Nossum { 1443632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 14563c882a0SEric Paris struct fsnotify_event *event; 1463632dee2SVegard Nossum 14763c882a0SEric Paris if (fsnotify_notify_queue_is_empty(group)) 1483632dee2SVegard Nossum return NULL; 1493632dee2SVegard Nossum 1508ba8fa91SJan Kara event = fsnotify_peek_first_event(group); 15163c882a0SEric Paris 1525ba08e2eSEric Paris pr_debug("%s: group=%p event=%p\n", __func__, group, event); 1535ba08e2eSEric Paris 154e9fe6904SJan Kara event_size += round_event_name_len(event); 1553632dee2SVegard Nossum if (event_size > count) 1563632dee2SVegard Nossum return ERR_PTR(-EINVAL); 1573632dee2SVegard Nossum 158c21dbe20SJan Kara /* held the notification_lock the whole time, so this is the 15963c882a0SEric Paris * same event we peeked above */ 1608ba8fa91SJan Kara fsnotify_remove_first_event(group); 16163c882a0SEric Paris 16263c882a0SEric Paris return event; 1633632dee2SVegard Nossum } 1643632dee2SVegard Nossum 1653632dee2SVegard Nossum /* 1663632dee2SVegard Nossum * Copy an event to user space, returning how much we copied. 1673632dee2SVegard Nossum * 1683632dee2SVegard Nossum * We already checked that the event size is smaller than the 1693632dee2SVegard Nossum * buffer we had in "get_one_event()" above. 1703632dee2SVegard Nossum */ 17163c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group, 1727053aee2SJan Kara struct fsnotify_event *fsn_event, 1733632dee2SVegard Nossum char __user *buf) 1743632dee2SVegard Nossum { 17563c882a0SEric Paris struct inotify_event inotify_event; 1767053aee2SJan Kara struct inotify_event_info *event; 1773632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 178e9fe6904SJan Kara size_t name_len; 179e9fe6904SJan Kara size_t pad_name_len; 1803632dee2SVegard Nossum 1817053aee2SJan Kara pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event); 1825ba08e2eSEric Paris 1837053aee2SJan Kara event = INOTIFY_E(fsn_event); 184e9fe6904SJan Kara name_len = event->name_len; 185b962e731SBrian Rogers /* 186e9fe6904SJan Kara * round up name length so it is a multiple of event_size 1870db501bdSEric W. Biederman * plus an extra byte for the terminating '\0'. 1880db501bdSEric W. Biederman */ 1897053aee2SJan Kara pad_name_len = round_event_name_len(fsn_event); 190e9fe6904SJan Kara inotify_event.len = pad_name_len; 1917053aee2SJan Kara inotify_event.mask = inotify_mask_to_arg(fsn_event->mask); 1927053aee2SJan Kara inotify_event.wd = event->wd; 19363c882a0SEric Paris inotify_event.cookie = event->sync_cookie; 19463c882a0SEric Paris 19563c882a0SEric Paris /* send the main event */ 19663c882a0SEric Paris if (copy_to_user(buf, &inotify_event, event_size)) 1973632dee2SVegard Nossum return -EFAULT; 1983632dee2SVegard Nossum 1993632dee2SVegard Nossum buf += event_size; 2003632dee2SVegard Nossum 20163c882a0SEric Paris /* 20263c882a0SEric Paris * fsnotify only stores the pathname, so here we have to send the pathname 20363c882a0SEric Paris * and then pad that pathname out to a multiple of sizeof(inotify_event) 204e9fe6904SJan Kara * with zeros. 20563c882a0SEric Paris */ 206e9fe6904SJan Kara if (pad_name_len) { 20763c882a0SEric Paris /* copy the path name */ 2087053aee2SJan Kara if (copy_to_user(buf, event->name, name_len)) 2093632dee2SVegard Nossum return -EFAULT; 210e9fe6904SJan Kara buf += name_len; 2113632dee2SVegard Nossum 2120db501bdSEric W. Biederman /* fill userspace with 0's */ 213e9fe6904SJan Kara if (clear_user(buf, pad_name_len - name_len)) 21463c882a0SEric Paris return -EFAULT; 215e9fe6904SJan Kara event_size += pad_name_len; 2163632dee2SVegard Nossum } 21763c882a0SEric Paris 2183632dee2SVegard Nossum return event_size; 2193632dee2SVegard Nossum } 2203632dee2SVegard Nossum 221272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf, 222272eb014SEric Paris size_t count, loff_t *pos) 223272eb014SEric Paris { 22463c882a0SEric Paris struct fsnotify_group *group; 22563c882a0SEric Paris struct fsnotify_event *kevent; 226272eb014SEric Paris char __user *start; 227272eb014SEric Paris int ret; 228e23738a7SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 229272eb014SEric Paris 230272eb014SEric Paris start = buf; 23163c882a0SEric Paris group = file->private_data; 232272eb014SEric Paris 233e23738a7SPeter Zijlstra add_wait_queue(&group->notification_waitq, &wait); 234272eb014SEric Paris while (1) { 235c21dbe20SJan Kara spin_lock(&group->notification_lock); 23663c882a0SEric Paris kevent = get_one_event(group, count); 237c21dbe20SJan Kara spin_unlock(&group->notification_lock); 238272eb014SEric Paris 2395ba08e2eSEric Paris pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent); 2405ba08e2eSEric Paris 2413632dee2SVegard Nossum if (kevent) { 2423632dee2SVegard Nossum ret = PTR_ERR(kevent); 2433632dee2SVegard Nossum if (IS_ERR(kevent)) 244272eb014SEric Paris break; 24563c882a0SEric Paris ret = copy_event_to_user(group, kevent, buf); 2467053aee2SJan Kara fsnotify_destroy_event(group, kevent); 2473632dee2SVegard Nossum if (ret < 0) 2483632dee2SVegard Nossum break; 2493632dee2SVegard Nossum buf += ret; 2503632dee2SVegard Nossum count -= ret; 2513632dee2SVegard Nossum continue; 252272eb014SEric Paris } 253272eb014SEric Paris 2543632dee2SVegard Nossum ret = -EAGAIN; 2553632dee2SVegard Nossum if (file->f_flags & O_NONBLOCK) 256272eb014SEric Paris break; 2571ca39ab9SEric Paris ret = -ERESTARTSYS; 2583632dee2SVegard Nossum if (signal_pending(current)) 2593632dee2SVegard Nossum break; 2603632dee2SVegard Nossum 2613632dee2SVegard Nossum if (start != buf) 2623632dee2SVegard Nossum break; 263272eb014SEric Paris 264e23738a7SPeter Zijlstra wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 265272eb014SEric Paris } 266e23738a7SPeter Zijlstra remove_wait_queue(&group->notification_waitq, &wait); 267272eb014SEric Paris 2683632dee2SVegard Nossum if (start != buf && ret != -EFAULT) 269272eb014SEric Paris ret = buf - start; 270272eb014SEric Paris return ret; 271272eb014SEric Paris } 272272eb014SEric Paris 273272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file) 274272eb014SEric Paris { 27563c882a0SEric Paris struct fsnotify_group *group = file->private_data; 276272eb014SEric Paris 2775ba08e2eSEric Paris pr_debug("%s: group=%p\n", __func__, group); 2785ba08e2eSEric Paris 27963c882a0SEric Paris /* free this group, matching get was inotify_init->fsnotify_obtain_group */ 280d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 281272eb014SEric Paris 282272eb014SEric Paris return 0; 283272eb014SEric Paris } 284272eb014SEric Paris 285272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd, 286272eb014SEric Paris unsigned long arg) 287272eb014SEric Paris { 28863c882a0SEric Paris struct fsnotify_group *group; 2897053aee2SJan Kara struct fsnotify_event *fsn_event; 290272eb014SEric Paris void __user *p; 291272eb014SEric Paris int ret = -ENOTTY; 29263c882a0SEric Paris size_t send_len = 0; 293272eb014SEric Paris 29463c882a0SEric Paris group = file->private_data; 295272eb014SEric Paris p = (void __user *) arg; 296272eb014SEric Paris 2975ba08e2eSEric Paris pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd); 2985ba08e2eSEric Paris 299272eb014SEric Paris switch (cmd) { 300272eb014SEric Paris case FIONREAD: 301c21dbe20SJan Kara spin_lock(&group->notification_lock); 3027053aee2SJan Kara list_for_each_entry(fsn_event, &group->notification_list, 3037053aee2SJan Kara list) { 30463c882a0SEric Paris send_len += sizeof(struct inotify_event); 3057053aee2SJan Kara send_len += round_event_name_len(fsn_event); 30663c882a0SEric Paris } 307c21dbe20SJan Kara spin_unlock(&group->notification_lock); 30863c882a0SEric Paris ret = put_user(send_len, (int __user *) p); 309272eb014SEric Paris break; 310272eb014SEric Paris } 311272eb014SEric Paris 312272eb014SEric Paris return ret; 313272eb014SEric Paris } 314272eb014SEric Paris 315272eb014SEric Paris static const struct file_operations inotify_fops = { 316be77196bSCyrill Gorcunov .show_fdinfo = inotify_show_fdinfo, 317272eb014SEric Paris .poll = inotify_poll, 318272eb014SEric Paris .read = inotify_read, 3190a6b6bd5SEric Paris .fasync = fsnotify_fasync, 320272eb014SEric Paris .release = inotify_release, 321272eb014SEric Paris .unlocked_ioctl = inotify_ioctl, 322272eb014SEric Paris .compat_ioctl = inotify_ioctl, 3236038f373SArnd Bergmann .llseek = noop_llseek, 324272eb014SEric Paris }; 325272eb014SEric Paris 326272eb014SEric Paris 32763c882a0SEric Paris /* 32863c882a0SEric Paris * find_inode - resolve a user-given path to a specific inode 32963c882a0SEric Paris */ 33063c882a0SEric Paris static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags) 33163c882a0SEric Paris { 33263c882a0SEric Paris int error; 33363c882a0SEric Paris 33463c882a0SEric Paris error = user_path_at(AT_FDCWD, dirname, flags, path); 33563c882a0SEric Paris if (error) 33663c882a0SEric Paris return error; 33763c882a0SEric Paris /* you can only watch an inode if you have read permissions on it */ 33863c882a0SEric Paris error = inode_permission(path->dentry->d_inode, MAY_READ); 33963c882a0SEric Paris if (error) 34063c882a0SEric Paris path_put(path); 34163c882a0SEric Paris return error; 34263c882a0SEric Paris } 34363c882a0SEric Paris 344b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock, 345000285deSEric Paris struct inotify_inode_mark *i_mark) 346b7ba8371SEric Paris { 347b7ba8371SEric Paris int ret; 348b7ba8371SEric Paris 3494542da63STejun Heo idr_preload(GFP_KERNEL); 350b7ba8371SEric Paris spin_lock(idr_lock); 3514542da63STejun Heo 352a66c04b4SJeff Layton ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT); 3534542da63STejun Heo if (ret >= 0) { 354b7ba8371SEric Paris /* we added the mark to the idr, take a reference */ 3554542da63STejun Heo i_mark->wd = ret; 356000285deSEric Paris fsnotify_get_mark(&i_mark->fsn_mark); 3577050c488SEric Paris } 358b7ba8371SEric Paris 3594542da63STejun Heo spin_unlock(idr_lock); 3604542da63STejun Heo idr_preload_end(); 3614542da63STejun Heo return ret < 0 ? ret : 0; 362b7ba8371SEric Paris } 363b7ba8371SEric Paris 364000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group, 365b7ba8371SEric Paris int wd) 366b7ba8371SEric Paris { 367b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 368b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 369000285deSEric Paris struct inotify_inode_mark *i_mark; 370b7ba8371SEric Paris 371b7ba8371SEric Paris assert_spin_locked(idr_lock); 372b7ba8371SEric Paris 373000285deSEric Paris i_mark = idr_find(idr, wd); 374000285deSEric Paris if (i_mark) { 375000285deSEric Paris struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark; 376b7ba8371SEric Paris 377000285deSEric Paris fsnotify_get_mark(fsn_mark); 378b7ba8371SEric Paris /* One ref for being in the idr, one ref we just took */ 379*ab97f873SElena Reshetova BUG_ON(refcount_read(&fsn_mark->refcnt) < 2); 380b7ba8371SEric Paris } 381b7ba8371SEric Paris 382000285deSEric Paris return i_mark; 383b7ba8371SEric Paris } 384b7ba8371SEric Paris 385000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group, 386b7ba8371SEric Paris int wd) 387b7ba8371SEric Paris { 388000285deSEric Paris struct inotify_inode_mark *i_mark; 389b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 390b7ba8371SEric Paris 391b7ba8371SEric Paris spin_lock(idr_lock); 392000285deSEric Paris i_mark = inotify_idr_find_locked(group, wd); 393b7ba8371SEric Paris spin_unlock(idr_lock); 394b7ba8371SEric Paris 395000285deSEric Paris return i_mark; 396b7ba8371SEric Paris } 397b7ba8371SEric Paris 398dead537dSEric Paris /* 399dead537dSEric Paris * Remove the mark from the idr (if present) and drop the reference 400dead537dSEric Paris * on the mark because it was in the idr. 401dead537dSEric Paris */ 4027e790dd5SEric Paris static void inotify_remove_from_idr(struct fsnotify_group *group, 403000285deSEric Paris struct inotify_inode_mark *i_mark) 4047e790dd5SEric Paris { 405e7253760SJan Kara struct idr *idr = &group->inotify_data.idr; 406b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 407000285deSEric Paris struct inotify_inode_mark *found_i_mark = NULL; 408dead537dSEric Paris int wd; 4097e790dd5SEric Paris 410b7ba8371SEric Paris spin_lock(idr_lock); 411000285deSEric Paris wd = i_mark->wd; 412dead537dSEric Paris 413b7ba8371SEric Paris /* 414000285deSEric Paris * does this i_mark think it is in the idr? we shouldn't get called 415b7ba8371SEric Paris * if it wasn't.... 416b7ba8371SEric Paris */ 417b7ba8371SEric Paris if (wd == -1) { 41825c829afSJan Kara WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", 41925c829afSJan Kara __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); 420dead537dSEric Paris goto out; 4217e790dd5SEric Paris } 422dead537dSEric Paris 423b7ba8371SEric Paris /* Lets look in the idr to see if we find it */ 424000285deSEric Paris found_i_mark = inotify_idr_find_locked(group, wd); 425000285deSEric Paris if (unlikely(!found_i_mark)) { 42625c829afSJan Kara WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", 42725c829afSJan Kara __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); 428b7ba8371SEric Paris goto out; 429b7ba8371SEric Paris } 430dead537dSEric Paris 431b7ba8371SEric Paris /* 432000285deSEric Paris * We found an mark in the idr at the right wd, but it's 433000285deSEric Paris * not the mark we were told to remove. eparis seriously 434b7ba8371SEric Paris * fucked up somewhere. 435b7ba8371SEric Paris */ 436000285deSEric Paris if (unlikely(found_i_mark != i_mark)) { 437000285deSEric Paris WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p " 43825c829afSJan Kara "found_i_mark=%p found_i_mark->wd=%d " 43925c829afSJan Kara "found_i_mark->group=%p\n", __func__, i_mark, 44025c829afSJan Kara i_mark->wd, i_mark->fsn_mark.group, found_i_mark, 44125c829afSJan Kara found_i_mark->wd, found_i_mark->fsn_mark.group); 442b7ba8371SEric Paris goto out; 443b7ba8371SEric Paris } 444dead537dSEric Paris 445b7ba8371SEric Paris /* 446b7ba8371SEric Paris * One ref for being in the idr 447b7ba8371SEric Paris * one ref grabbed by inotify_idr_find 448b7ba8371SEric Paris */ 449*ab97f873SElena Reshetova if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) { 45025c829afSJan Kara printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", 45125c829afSJan Kara __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); 452b7ba8371SEric Paris /* we can't really recover with bad ref cnting.. */ 453b7ba8371SEric Paris BUG(); 454b7ba8371SEric Paris } 455b7ba8371SEric Paris 456e7253760SJan Kara idr_remove(idr, wd); 457e7253760SJan Kara /* Removed from the idr, drop that ref. */ 458e7253760SJan Kara fsnotify_put_mark(&i_mark->fsn_mark); 459dead537dSEric Paris out: 460e7253760SJan Kara i_mark->wd = -1; 461e7253760SJan Kara spin_unlock(idr_lock); 462b7ba8371SEric Paris /* match the ref taken by inotify_idr_find_locked() */ 463000285deSEric Paris if (found_i_mark) 464000285deSEric Paris fsnotify_put_mark(&found_i_mark->fsn_mark); 465dead537dSEric Paris } 466dead537dSEric Paris 46763c882a0SEric Paris /* 468dead537dSEric Paris * Send IN_IGNORED for this wd, remove this wd from the idr. 46963c882a0SEric Paris */ 470000285deSEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark, 471528da3e9SEric Paris struct fsnotify_group *group) 47263c882a0SEric Paris { 473000285deSEric Paris struct inotify_inode_mark *i_mark; 4747053aee2SJan Kara 4757053aee2SJan Kara /* Queue ignore event for the watch */ 4767053aee2SJan Kara inotify_handle_event(group, NULL, fsn_mark, NULL, FS_IN_IGNORED, 4779385a84dSJan Kara NULL, FSNOTIFY_EVENT_NONE, NULL, 0, NULL); 47863c882a0SEric Paris 4798b99c3ccSLino Sanfilippo i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 480000285deSEric Paris /* remove this mark from the idr */ 481000285deSEric Paris inotify_remove_from_idr(group, i_mark); 48263c882a0SEric Paris 4831cce1eeaSNikolay Borisov dec_inotify_watches(group->inotify_data.ucounts); 48463c882a0SEric Paris } 48563c882a0SEric Paris 48652cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group, 48752cef755SEric Paris struct inode *inode, 48852cef755SEric Paris u32 arg) 48963c882a0SEric Paris { 490000285deSEric Paris struct fsnotify_mark *fsn_mark; 491000285deSEric Paris struct inotify_inode_mark *i_mark; 49263c882a0SEric Paris __u32 old_mask, new_mask; 49352cef755SEric Paris __u32 mask; 49452cef755SEric Paris int add = (arg & IN_MASK_ADD); 49552cef755SEric Paris int ret; 49663c882a0SEric Paris 49763c882a0SEric Paris mask = inotify_arg_to_mask(arg); 49863c882a0SEric Paris 499b1362edfSJan Kara fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group); 500000285deSEric Paris if (!fsn_mark) 50152cef755SEric Paris return -ENOENT; 50252cef755SEric Paris 503000285deSEric Paris i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 50475fe2b26SEric Paris 505000285deSEric Paris spin_lock(&fsn_mark->lock); 506000285deSEric Paris old_mask = fsn_mark->mask; 50790b1e7a5SEric Paris if (add) 50866d2b81bSJan Kara fsn_mark->mask |= mask; 50990b1e7a5SEric Paris else 51066d2b81bSJan Kara fsn_mark->mask = mask; 511000285deSEric Paris new_mask = fsn_mark->mask; 512000285deSEric Paris spin_unlock(&fsn_mark->lock); 51363c882a0SEric Paris 51463c882a0SEric Paris if (old_mask != new_mask) { 51563c882a0SEric Paris /* more bits in old than in new? */ 51663c882a0SEric Paris int dropped = (old_mask & ~new_mask); 517000285deSEric Paris /* more bits in this fsn_mark than the inode's mask? */ 51863c882a0SEric Paris int do_inode = (new_mask & ~inode->i_fsnotify_mask); 51963c882a0SEric Paris 520000285deSEric Paris /* update the inode with this new fsn_mark */ 52163c882a0SEric Paris if (dropped || do_inode) 5228920d273SJan Kara fsnotify_recalc_mask(inode->i_fsnotify_marks); 52363c882a0SEric Paris 52463c882a0SEric Paris } 52563c882a0SEric Paris 52652cef755SEric Paris /* return the wd */ 527000285deSEric Paris ret = i_mark->wd; 52852cef755SEric Paris 529d0775441SEric Paris /* match the get from fsnotify_find_mark() */ 530000285deSEric Paris fsnotify_put_mark(fsn_mark); 53175fe2b26SEric Paris 53252cef755SEric Paris return ret; 53363c882a0SEric Paris } 5347e790dd5SEric Paris 53552cef755SEric Paris static int inotify_new_watch(struct fsnotify_group *group, 53652cef755SEric Paris struct inode *inode, 53752cef755SEric Paris u32 arg) 53852cef755SEric Paris { 539000285deSEric Paris struct inotify_inode_mark *tmp_i_mark; 54052cef755SEric Paris __u32 mask; 54152cef755SEric Paris int ret; 542b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 543b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 54452cef755SEric Paris 54552cef755SEric Paris mask = inotify_arg_to_mask(arg); 54652cef755SEric Paris 547000285deSEric Paris tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); 548000285deSEric Paris if (unlikely(!tmp_i_mark)) 54952cef755SEric Paris return -ENOMEM; 55052cef755SEric Paris 551054c636eSJan Kara fsnotify_init_mark(&tmp_i_mark->fsn_mark, group); 552000285deSEric Paris tmp_i_mark->fsn_mark.mask = mask; 553000285deSEric Paris tmp_i_mark->wd = -1; 55452cef755SEric Paris 555a66c04b4SJeff Layton ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark); 556b7ba8371SEric Paris if (ret) 55752cef755SEric Paris goto out_err; 55852cef755SEric Paris 5591cce1eeaSNikolay Borisov /* increment the number of watches the user has */ 5601cce1eeaSNikolay Borisov if (!inc_inotify_watches(group->inotify_data.ucounts)) { 5611cce1eeaSNikolay Borisov inotify_remove_from_idr(group, tmp_i_mark); 5621cce1eeaSNikolay Borisov ret = -ENOSPC; 5631cce1eeaSNikolay Borisov goto out_err; 5641cce1eeaSNikolay Borisov } 5651cce1eeaSNikolay Borisov 56652cef755SEric Paris /* we are on the idr, now get on the inode */ 5677b129323SJan Kara ret = fsnotify_add_mark_locked(&tmp_i_mark->fsn_mark, inode, NULL, 0); 56852cef755SEric Paris if (ret) { 56952cef755SEric Paris /* we failed to get on the inode, get off the idr */ 570000285deSEric Paris inotify_remove_from_idr(group, tmp_i_mark); 57152cef755SEric Paris goto out_err; 57252cef755SEric Paris } 57352cef755SEric Paris 57452cef755SEric Paris 575000285deSEric Paris /* return the watch descriptor for this new mark */ 576000285deSEric Paris ret = tmp_i_mark->wd; 57752cef755SEric Paris 57852cef755SEric Paris out_err: 579000285deSEric Paris /* match the ref from fsnotify_init_mark() */ 580000285deSEric Paris fsnotify_put_mark(&tmp_i_mark->fsn_mark); 58152cef755SEric Paris 58252cef755SEric Paris return ret; 58352cef755SEric Paris } 58452cef755SEric Paris 58552cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg) 58652cef755SEric Paris { 58752cef755SEric Paris int ret = 0; 58852cef755SEric Paris 589e1e5a9f8SLino Sanfilippo mutex_lock(&group->mark_mutex); 59052cef755SEric Paris /* try to update and existing watch with the new arg */ 59152cef755SEric Paris ret = inotify_update_existing_watch(group, inode, arg); 59252cef755SEric Paris /* no mark present, try to add a new one */ 59352cef755SEric Paris if (ret == -ENOENT) 59452cef755SEric Paris ret = inotify_new_watch(group, inode, arg); 595e1e5a9f8SLino Sanfilippo mutex_unlock(&group->mark_mutex); 59652cef755SEric Paris 59763c882a0SEric Paris return ret; 59863c882a0SEric Paris } 59963c882a0SEric Paris 600d0de4dc5SEric Paris static struct fsnotify_group *inotify_new_group(unsigned int max_events) 60163c882a0SEric Paris { 60263c882a0SEric Paris struct fsnotify_group *group; 603ff57cd58SJan Kara struct inotify_event_info *oevent; 60463c882a0SEric Paris 6050d2e2a1dSEric Paris group = fsnotify_alloc_group(&inotify_fsnotify_ops); 60663c882a0SEric Paris if (IS_ERR(group)) 60763c882a0SEric Paris return group; 60863c882a0SEric Paris 609ff57cd58SJan Kara oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL); 610ff57cd58SJan Kara if (unlikely(!oevent)) { 611ff57cd58SJan Kara fsnotify_destroy_group(group); 612ff57cd58SJan Kara return ERR_PTR(-ENOMEM); 613ff57cd58SJan Kara } 614ff57cd58SJan Kara group->overflow_event = &oevent->fse; 615ff57cd58SJan Kara fsnotify_init_event(group->overflow_event, NULL, FS_Q_OVERFLOW); 616ff57cd58SJan Kara oevent->wd = -1; 617ff57cd58SJan Kara oevent->sync_cookie = 0; 618ff57cd58SJan Kara oevent->name_len = 0; 619ff57cd58SJan Kara 62063c882a0SEric Paris group->max_events = max_events; 62163c882a0SEric Paris 62263c882a0SEric Paris spin_lock_init(&group->inotify_data.idr_lock); 62363c882a0SEric Paris idr_init(&group->inotify_data.idr); 6241cce1eeaSNikolay Borisov group->inotify_data.ucounts = inc_ucount(current_user_ns(), 6251cce1eeaSNikolay Borisov current_euid(), 6261cce1eeaSNikolay Borisov UCOUNT_INOTIFY_INSTANCES); 627d0de4dc5SEric Paris 6281cce1eeaSNikolay Borisov if (!group->inotify_data.ucounts) { 629d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 630d0de4dc5SEric Paris return ERR_PTR(-EMFILE); 631d0de4dc5SEric Paris } 63263c882a0SEric Paris 63363c882a0SEric Paris return group; 63463c882a0SEric Paris } 63563c882a0SEric Paris 63663c882a0SEric Paris 63763c882a0SEric Paris /* inotify syscalls */ 638938bb9f5SHeiko Carstens SYSCALL_DEFINE1(inotify_init1, int, flags) 639272eb014SEric Paris { 64063c882a0SEric Paris struct fsnotify_group *group; 641c44dcc56SAl Viro int ret; 642272eb014SEric Paris 643272eb014SEric Paris /* Check the IN_* constants for consistency. */ 644272eb014SEric Paris BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC); 645272eb014SEric Paris BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK); 646272eb014SEric Paris 647272eb014SEric Paris if (flags & ~(IN_CLOEXEC | IN_NONBLOCK)) 648272eb014SEric Paris return -EINVAL; 649272eb014SEric Paris 65063c882a0SEric Paris /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */ 651d0de4dc5SEric Paris group = inotify_new_group(inotify_max_queued_events); 652d0de4dc5SEric Paris if (IS_ERR(group)) 653d0de4dc5SEric Paris return PTR_ERR(group); 654825f9692SAl Viro 655c44dcc56SAl Viro ret = anon_inode_getfd("inotify", &inotify_fops, group, 656c44dcc56SAl Viro O_RDONLY | flags); 657d0de4dc5SEric Paris if (ret < 0) 658d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 659d0de4dc5SEric Paris 660272eb014SEric Paris return ret; 661272eb014SEric Paris } 662272eb014SEric Paris 663938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init) 664272eb014SEric Paris { 665272eb014SEric Paris return sys_inotify_init1(0); 666272eb014SEric Paris } 667272eb014SEric Paris 6682e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, 6692e4d0924SHeiko Carstens u32, mask) 670272eb014SEric Paris { 67163c882a0SEric Paris struct fsnotify_group *group; 672272eb014SEric Paris struct inode *inode; 673272eb014SEric Paris struct path path; 6742903ff01SAl Viro struct fd f; 6752903ff01SAl Viro int ret; 676272eb014SEric Paris unsigned flags = 0; 677272eb014SEric Paris 678d30e2c05SDave Hansen /* 679d30e2c05SDave Hansen * We share a lot of code with fs/dnotify. We also share 680d30e2c05SDave Hansen * the bit layout between inotify's IN_* and the fsnotify 681d30e2c05SDave Hansen * FS_*. This check ensures that only the inotify IN_* 682d30e2c05SDave Hansen * bits get passed in and set in watches/events. 683d30e2c05SDave Hansen */ 684d30e2c05SDave Hansen if (unlikely(mask & ~ALL_INOTIFY_BITS)) 685d30e2c05SDave Hansen return -EINVAL; 686d30e2c05SDave Hansen /* 687d30e2c05SDave Hansen * Require at least one valid bit set in the mask. 688d30e2c05SDave Hansen * Without _something_ set, we would have no events to 689d30e2c05SDave Hansen * watch for. 690d30e2c05SDave Hansen */ 69104df32faSZhao Hongjiang if (unlikely(!(mask & ALL_INOTIFY_BITS))) 69204df32faSZhao Hongjiang return -EINVAL; 69304df32faSZhao Hongjiang 6942903ff01SAl Viro f = fdget(fd); 6952903ff01SAl Viro if (unlikely(!f.file)) 696272eb014SEric Paris return -EBADF; 697272eb014SEric Paris 698272eb014SEric Paris /* verify that this is indeed an inotify instance */ 6992903ff01SAl Viro if (unlikely(f.file->f_op != &inotify_fops)) { 700272eb014SEric Paris ret = -EINVAL; 701272eb014SEric Paris goto fput_and_out; 702272eb014SEric Paris } 703272eb014SEric Paris 704272eb014SEric Paris if (!(mask & IN_DONT_FOLLOW)) 705272eb014SEric Paris flags |= LOOKUP_FOLLOW; 706272eb014SEric Paris if (mask & IN_ONLYDIR) 707272eb014SEric Paris flags |= LOOKUP_DIRECTORY; 708272eb014SEric Paris 70963c882a0SEric Paris ret = inotify_find_inode(pathname, &path, flags); 71063c882a0SEric Paris if (ret) 711272eb014SEric Paris goto fput_and_out; 712272eb014SEric Paris 71363c882a0SEric Paris /* inode held in place by reference to path; group by fget on fd */ 714272eb014SEric Paris inode = path.dentry->d_inode; 7152903ff01SAl Viro group = f.file->private_data; 716272eb014SEric Paris 71763c882a0SEric Paris /* create/update an inode mark */ 71863c882a0SEric Paris ret = inotify_update_watch(group, inode, mask); 719272eb014SEric Paris path_put(&path); 720272eb014SEric Paris fput_and_out: 7212903ff01SAl Viro fdput(f); 722272eb014SEric Paris return ret; 723272eb014SEric Paris } 724272eb014SEric Paris 7252e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd) 726272eb014SEric Paris { 72763c882a0SEric Paris struct fsnotify_group *group; 728000285deSEric Paris struct inotify_inode_mark *i_mark; 7292903ff01SAl Viro struct fd f; 7302903ff01SAl Viro int ret = 0; 731272eb014SEric Paris 7322903ff01SAl Viro f = fdget(fd); 7332903ff01SAl Viro if (unlikely(!f.file)) 734272eb014SEric Paris return -EBADF; 735272eb014SEric Paris 736272eb014SEric Paris /* verify that this is indeed an inotify instance */ 737272eb014SEric Paris ret = -EINVAL; 7382903ff01SAl Viro if (unlikely(f.file->f_op != &inotify_fops)) 739272eb014SEric Paris goto out; 740272eb014SEric Paris 7412903ff01SAl Viro group = f.file->private_data; 742272eb014SEric Paris 74363c882a0SEric Paris ret = -EINVAL; 744000285deSEric Paris i_mark = inotify_idr_find(group, wd); 745000285deSEric Paris if (unlikely(!i_mark)) 74663c882a0SEric Paris goto out; 74763c882a0SEric Paris 748b7ba8371SEric Paris ret = 0; 749b7ba8371SEric Paris 750e2a29943SLino Sanfilippo fsnotify_destroy_mark(&i_mark->fsn_mark, group); 751b7ba8371SEric Paris 752b7ba8371SEric Paris /* match ref taken by inotify_idr_find */ 753000285deSEric Paris fsnotify_put_mark(&i_mark->fsn_mark); 754272eb014SEric Paris 755272eb014SEric Paris out: 7562903ff01SAl Viro fdput(f); 757272eb014SEric Paris return ret; 758272eb014SEric Paris } 759272eb014SEric Paris 760272eb014SEric Paris /* 761ae0e47f0SJustin P. Mattock * inotify_user_setup - Our initialization function. Note that we cannot return 762272eb014SEric Paris * error because we have compiled-in VFS hooks. So an (unlikely) failure here 763272eb014SEric Paris * must result in panic(). 764272eb014SEric Paris */ 765272eb014SEric Paris static int __init inotify_user_setup(void) 766272eb014SEric Paris { 767f874e1acSEric Paris BUILD_BUG_ON(IN_ACCESS != FS_ACCESS); 768f874e1acSEric Paris BUILD_BUG_ON(IN_MODIFY != FS_MODIFY); 769f874e1acSEric Paris BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB); 770f874e1acSEric Paris BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE); 771f874e1acSEric Paris BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); 772f874e1acSEric Paris BUILD_BUG_ON(IN_OPEN != FS_OPEN); 773f874e1acSEric Paris BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM); 774f874e1acSEric Paris BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO); 775f874e1acSEric Paris BUILD_BUG_ON(IN_CREATE != FS_CREATE); 776f874e1acSEric Paris BUILD_BUG_ON(IN_DELETE != FS_DELETE); 777f874e1acSEric Paris BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF); 778f874e1acSEric Paris BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF); 779f874e1acSEric Paris BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT); 780f874e1acSEric Paris BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW); 781f874e1acSEric Paris BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED); 782f874e1acSEric Paris BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK); 783b29866aaSEric Paris BUILD_BUG_ON(IN_ISDIR != FS_ISDIR); 784f874e1acSEric Paris BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT); 785f874e1acSEric Paris 786f874e1acSEric Paris BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21); 787f874e1acSEric Paris 788000285deSEric Paris inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC); 78963c882a0SEric Paris 790272eb014SEric Paris inotify_max_queued_events = 16384; 7911cce1eeaSNikolay Borisov init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128; 7921cce1eeaSNikolay Borisov init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192; 793272eb014SEric Paris 794272eb014SEric Paris return 0; 795272eb014SEric Paris } 796c013d5a4SPaul Gortmaker fs_initcall(inotify_user_setup); 797