13e0a4e85SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2272eb014SEric Paris /* 3272eb014SEric Paris * fs/inotify_user.c - inotify support for userspace 4272eb014SEric Paris * 5272eb014SEric Paris * Authors: 6272eb014SEric Paris * John McCutchan <ttb@tentacle.dhs.org> 7272eb014SEric Paris * Robert Love <rml@novell.com> 8272eb014SEric Paris * 9272eb014SEric Paris * Copyright (C) 2005 John McCutchan 10272eb014SEric Paris * Copyright 2006 Hewlett-Packard Development Company, L.P. 11272eb014SEric Paris * 1263c882a0SEric Paris * Copyright (C) 2009 Eric Paris <Red Hat Inc> 1363c882a0SEric Paris * inotify was largely rewriten to make use of the fsnotify infrastructure 14272eb014SEric Paris */ 15272eb014SEric Paris 16272eb014SEric Paris #include <linux/file.h> 1763c882a0SEric Paris #include <linux/fs.h> /* struct inode */ 1863c882a0SEric Paris #include <linux/fsnotify_backend.h> 1963c882a0SEric Paris #include <linux/idr.h> 20c013d5a4SPaul Gortmaker #include <linux/init.h> /* fs_initcall */ 21272eb014SEric Paris #include <linux/inotify.h> 2263c882a0SEric Paris #include <linux/kernel.h> /* roundup() */ 2363c882a0SEric Paris #include <linux/namei.h> /* LOOKUP_FOLLOW */ 24174cd4b1SIngo Molnar #include <linux/sched/signal.h> 2563c882a0SEric Paris #include <linux/slab.h> /* struct kmem_cache */ 26272eb014SEric Paris #include <linux/syscalls.h> 2763c882a0SEric Paris #include <linux/types.h> 28c44dcc56SAl Viro #include <linux/anon_inodes.h> 2963c882a0SEric Paris #include <linux/uaccess.h> 3063c882a0SEric Paris #include <linux/poll.h> 3163c882a0SEric Paris #include <linux/wait.h> 32d46eb14bSShakeel Butt #include <linux/memcontrol.h> 33ac5656d8SAaron Goidel #include <linux/security.h> 3463c882a0SEric Paris 3563c882a0SEric Paris #include "inotify.h" 36be77196bSCyrill Gorcunov #include "../fdinfo.h" 37272eb014SEric Paris 38272eb014SEric Paris #include <asm/ioctls.h> 39272eb014SEric Paris 4092890123SWaiman Long /* 4192890123SWaiman Long * An inotify watch requires allocating an inotify_inode_mark structure as 4292890123SWaiman Long * well as pinning the watched inode. Doubling the size of a VFS inode 4392890123SWaiman Long * should be more than enough to cover the additional filesystem inode 4492890123SWaiman Long * size increase. 4592890123SWaiman Long */ 4692890123SWaiman Long #define INOTIFY_WATCH_COST (sizeof(struct inotify_inode_mark) + \ 4792890123SWaiman Long 2 * sizeof(struct inode)) 4892890123SWaiman Long 491cce1eeaSNikolay Borisov /* configurable via /proc/sys/fs/inotify/ */ 50272eb014SEric Paris static int inotify_max_queued_events __read_mostly; 5163c882a0SEric Paris 5268279f9cSAlexey Dobriyan struct kmem_cache *inotify_inode_mark_cachep __ro_after_init; 53272eb014SEric Paris 54272eb014SEric Paris #ifdef CONFIG_SYSCTL 55272eb014SEric Paris 56272eb014SEric Paris #include <linux/sysctl.h> 57272eb014SEric Paris 58f153c224SSven Schnelle static long it_zero = 0; 59f153c224SSven Schnelle static long it_int_max = INT_MAX; 60f153c224SSven Schnelle 617b9ad122SXiaoming Ni static struct ctl_table inotify_table[] = { 62272eb014SEric Paris { 63272eb014SEric Paris .procname = "max_user_instances", 641cce1eeaSNikolay Borisov .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES], 65f153c224SSven Schnelle .maxlen = sizeof(long), 66272eb014SEric Paris .mode = 0644, 67f153c224SSven Schnelle .proc_handler = proc_doulongvec_minmax, 68f153c224SSven Schnelle .extra1 = &it_zero, 69f153c224SSven Schnelle .extra2 = &it_int_max, 70272eb014SEric Paris }, 71272eb014SEric Paris { 72272eb014SEric Paris .procname = "max_user_watches", 731cce1eeaSNikolay Borisov .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES], 74f153c224SSven Schnelle .maxlen = sizeof(long), 75272eb014SEric Paris .mode = 0644, 76f153c224SSven Schnelle .proc_handler = proc_doulongvec_minmax, 77f153c224SSven Schnelle .extra1 = &it_zero, 78f153c224SSven Schnelle .extra2 = &it_int_max, 79272eb014SEric Paris }, 80272eb014SEric Paris { 81272eb014SEric Paris .procname = "max_queued_events", 82272eb014SEric Paris .data = &inotify_max_queued_events, 83272eb014SEric Paris .maxlen = sizeof(int), 84272eb014SEric Paris .mode = 0644, 856d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 86eec4844fSMatteo Croce .extra1 = SYSCTL_ZERO 87272eb014SEric Paris }, 88272eb014SEric Paris }; 897b9ad122SXiaoming Ni 907b9ad122SXiaoming Ni static void __init inotify_sysctls_init(void) 917b9ad122SXiaoming Ni { 927b9ad122SXiaoming Ni register_sysctl("fs/inotify", inotify_table); 937b9ad122SXiaoming Ni } 947b9ad122SXiaoming Ni 957b9ad122SXiaoming Ni #else 967b9ad122SXiaoming Ni #define inotify_sysctls_init() do { } while (0) 97272eb014SEric Paris #endif /* CONFIG_SYSCTL */ 98272eb014SEric Paris 99957f7b47SAmir Goldstein static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg) 100272eb014SEric Paris { 10163c882a0SEric Paris __u32 mask; 10263c882a0SEric Paris 103611da04fSEric Paris /* 104e0462f91SGabriel Krisman Bertazi * Everything should receive events when the inode is unmounted. 105e0462f91SGabriel Krisman Bertazi * All directories care about children. 106611da04fSEric Paris */ 107e0462f91SGabriel Krisman Bertazi mask = (FS_UNMOUNT); 108957f7b47SAmir Goldstein if (S_ISDIR(inode->i_mode)) 109957f7b47SAmir Goldstein mask |= FS_EVENT_ON_CHILD; 11063c882a0SEric Paris 11163c882a0SEric Paris /* mask off the flags used to open the fd */ 112a32e697cSAmir Goldstein mask |= (arg & INOTIFY_USER_MASK); 11363c882a0SEric Paris 11463c882a0SEric Paris return mask; 115272eb014SEric Paris } 116272eb014SEric Paris 11738035c04SAmir Goldstein #define INOTIFY_MARK_FLAGS \ 11838035c04SAmir Goldstein (FSNOTIFY_MARK_FLAG_EXCL_UNLINK | FSNOTIFY_MARK_FLAG_IN_ONESHOT) 11938035c04SAmir Goldstein 12038035c04SAmir Goldstein static inline unsigned int inotify_arg_to_flags(u32 arg) 12138035c04SAmir Goldstein { 12238035c04SAmir Goldstein unsigned int flags = 0; 12338035c04SAmir Goldstein 12438035c04SAmir Goldstein if (arg & IN_EXCL_UNLINK) 12538035c04SAmir Goldstein flags |= FSNOTIFY_MARK_FLAG_EXCL_UNLINK; 12638035c04SAmir Goldstein if (arg & IN_ONESHOT) 12738035c04SAmir Goldstein flags |= FSNOTIFY_MARK_FLAG_IN_ONESHOT; 12838035c04SAmir Goldstein 12938035c04SAmir Goldstein return flags; 13038035c04SAmir Goldstein } 13138035c04SAmir Goldstein 13263c882a0SEric Paris static inline u32 inotify_mask_to_arg(__u32 mask) 133272eb014SEric Paris { 13463c882a0SEric Paris return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED | 13563c882a0SEric Paris IN_Q_OVERFLOW); 136272eb014SEric Paris } 137272eb014SEric Paris 138c05787b4SOliver Ford /* inotify userspace file descriptor functions */ 139076ccb76SAl Viro static __poll_t inotify_poll(struct file *file, poll_table *wait) 140272eb014SEric Paris { 14163c882a0SEric Paris struct fsnotify_group *group = file->private_data; 142076ccb76SAl Viro __poll_t ret = 0; 143272eb014SEric Paris 14463c882a0SEric Paris poll_wait(file, &group->notification_waitq, wait); 145c21dbe20SJan Kara spin_lock(&group->notification_lock); 14663c882a0SEric Paris if (!fsnotify_notify_queue_is_empty(group)) 147a9a08845SLinus Torvalds ret = EPOLLIN | EPOLLRDNORM; 148c21dbe20SJan Kara spin_unlock(&group->notification_lock); 149272eb014SEric Paris 150272eb014SEric Paris return ret; 151272eb014SEric Paris } 152272eb014SEric Paris 1537053aee2SJan Kara static int round_event_name_len(struct fsnotify_event *fsn_event) 154e9fe6904SJan Kara { 1557053aee2SJan Kara struct inotify_event_info *event; 1567053aee2SJan Kara 1577053aee2SJan Kara event = INOTIFY_E(fsn_event); 158e9fe6904SJan Kara if (!event->name_len) 159e9fe6904SJan Kara return 0; 160e9fe6904SJan Kara return roundup(event->name_len + 1, sizeof(struct inotify_event)); 161e9fe6904SJan Kara } 162e9fe6904SJan Kara 1633632dee2SVegard Nossum /* 1643632dee2SVegard Nossum * Get an inotify_kernel_event if one exists and is small 1653632dee2SVegard Nossum * enough to fit in "count". Return an error pointer if 1663632dee2SVegard Nossum * not large enough. 1673632dee2SVegard Nossum * 168c21dbe20SJan Kara * Called with the group->notification_lock held. 1693632dee2SVegard Nossum */ 17063c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group, 1713632dee2SVegard Nossum size_t count) 1723632dee2SVegard Nossum { 1733632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 17463c882a0SEric Paris struct fsnotify_event *event; 1753632dee2SVegard Nossum 1768ba8fa91SJan Kara event = fsnotify_peek_first_event(group); 1776f73171eSAmir Goldstein if (!event) 1786f73171eSAmir Goldstein return NULL; 17963c882a0SEric Paris 1805ba08e2eSEric Paris pr_debug("%s: group=%p event=%p\n", __func__, group, event); 1815ba08e2eSEric Paris 182e9fe6904SJan Kara event_size += round_event_name_len(event); 1833632dee2SVegard Nossum if (event_size > count) 1843632dee2SVegard Nossum return ERR_PTR(-EINVAL); 1853632dee2SVegard Nossum 186c21dbe20SJan Kara /* held the notification_lock the whole time, so this is the 18763c882a0SEric Paris * same event we peeked above */ 1888ba8fa91SJan Kara fsnotify_remove_first_event(group); 18963c882a0SEric Paris 19063c882a0SEric Paris return event; 1913632dee2SVegard Nossum } 1923632dee2SVegard Nossum 1933632dee2SVegard Nossum /* 1943632dee2SVegard Nossum * Copy an event to user space, returning how much we copied. 1953632dee2SVegard Nossum * 1963632dee2SVegard Nossum * We already checked that the event size is smaller than the 1973632dee2SVegard Nossum * buffer we had in "get_one_event()" above. 1983632dee2SVegard Nossum */ 19963c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group, 2007053aee2SJan Kara struct fsnotify_event *fsn_event, 2013632dee2SVegard Nossum char __user *buf) 2023632dee2SVegard Nossum { 20363c882a0SEric Paris struct inotify_event inotify_event; 2047053aee2SJan Kara struct inotify_event_info *event; 2053632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 206e9fe6904SJan Kara size_t name_len; 207e9fe6904SJan Kara size_t pad_name_len; 2083632dee2SVegard Nossum 2097053aee2SJan Kara pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event); 2105ba08e2eSEric Paris 2117053aee2SJan Kara event = INOTIFY_E(fsn_event); 212e9fe6904SJan Kara name_len = event->name_len; 213b962e731SBrian Rogers /* 214e9fe6904SJan Kara * round up name length so it is a multiple of event_size 2150db501bdSEric W. Biederman * plus an extra byte for the terminating '\0'. 2160db501bdSEric W. Biederman */ 2177053aee2SJan Kara pad_name_len = round_event_name_len(fsn_event); 218e9fe6904SJan Kara inotify_event.len = pad_name_len; 219a0a92d26SAmir Goldstein inotify_event.mask = inotify_mask_to_arg(event->mask); 2207053aee2SJan Kara inotify_event.wd = event->wd; 22163c882a0SEric Paris inotify_event.cookie = event->sync_cookie; 22263c882a0SEric Paris 22363c882a0SEric Paris /* send the main event */ 22463c882a0SEric Paris if (copy_to_user(buf, &inotify_event, event_size)) 2253632dee2SVegard Nossum return -EFAULT; 2263632dee2SVegard Nossum 2273632dee2SVegard Nossum buf += event_size; 2283632dee2SVegard Nossum 22963c882a0SEric Paris /* 23063c882a0SEric Paris * fsnotify only stores the pathname, so here we have to send the pathname 23163c882a0SEric Paris * and then pad that pathname out to a multiple of sizeof(inotify_event) 232e9fe6904SJan Kara * with zeros. 23363c882a0SEric Paris */ 234e9fe6904SJan Kara if (pad_name_len) { 23563c882a0SEric Paris /* copy the path name */ 2367053aee2SJan Kara if (copy_to_user(buf, event->name, name_len)) 2373632dee2SVegard Nossum return -EFAULT; 238e9fe6904SJan Kara buf += name_len; 2393632dee2SVegard Nossum 2400db501bdSEric W. Biederman /* fill userspace with 0's */ 241e9fe6904SJan Kara if (clear_user(buf, pad_name_len - name_len)) 24263c882a0SEric Paris return -EFAULT; 243e9fe6904SJan Kara event_size += pad_name_len; 2443632dee2SVegard Nossum } 24563c882a0SEric Paris 2463632dee2SVegard Nossum return event_size; 2473632dee2SVegard Nossum } 2483632dee2SVegard Nossum 249272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf, 250272eb014SEric Paris size_t count, loff_t *pos) 251272eb014SEric Paris { 25263c882a0SEric Paris struct fsnotify_group *group; 25363c882a0SEric Paris struct fsnotify_event *kevent; 254272eb014SEric Paris char __user *start; 255272eb014SEric Paris int ret; 256e23738a7SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 257272eb014SEric Paris 258272eb014SEric Paris start = buf; 25963c882a0SEric Paris group = file->private_data; 260272eb014SEric Paris 261e23738a7SPeter Zijlstra add_wait_queue(&group->notification_waitq, &wait); 262272eb014SEric Paris while (1) { 263c21dbe20SJan Kara spin_lock(&group->notification_lock); 26463c882a0SEric Paris kevent = get_one_event(group, count); 265c21dbe20SJan Kara spin_unlock(&group->notification_lock); 266272eb014SEric Paris 2675ba08e2eSEric Paris pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent); 2685ba08e2eSEric Paris 2693632dee2SVegard Nossum if (kevent) { 2703632dee2SVegard Nossum ret = PTR_ERR(kevent); 2713632dee2SVegard Nossum if (IS_ERR(kevent)) 272272eb014SEric Paris break; 27363c882a0SEric Paris ret = copy_event_to_user(group, kevent, buf); 2747053aee2SJan Kara fsnotify_destroy_event(group, kevent); 2753632dee2SVegard Nossum if (ret < 0) 2763632dee2SVegard Nossum break; 2773632dee2SVegard Nossum buf += ret; 2783632dee2SVegard Nossum count -= ret; 2793632dee2SVegard Nossum continue; 280272eb014SEric Paris } 281272eb014SEric Paris 2823632dee2SVegard Nossum ret = -EAGAIN; 2833632dee2SVegard Nossum if (file->f_flags & O_NONBLOCK) 284272eb014SEric Paris break; 2851ca39ab9SEric Paris ret = -ERESTARTSYS; 2863632dee2SVegard Nossum if (signal_pending(current)) 2873632dee2SVegard Nossum break; 2883632dee2SVegard Nossum 2893632dee2SVegard Nossum if (start != buf) 2903632dee2SVegard Nossum break; 291272eb014SEric Paris 292e23738a7SPeter Zijlstra wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 293272eb014SEric Paris } 294e23738a7SPeter Zijlstra remove_wait_queue(&group->notification_waitq, &wait); 295272eb014SEric Paris 2963632dee2SVegard Nossum if (start != buf && ret != -EFAULT) 297272eb014SEric Paris ret = buf - start; 298272eb014SEric Paris return ret; 299272eb014SEric Paris } 300272eb014SEric Paris 301272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file) 302272eb014SEric Paris { 30363c882a0SEric Paris struct fsnotify_group *group = file->private_data; 304272eb014SEric Paris 3055ba08e2eSEric Paris pr_debug("%s: group=%p\n", __func__, group); 3065ba08e2eSEric Paris 30763c882a0SEric Paris /* free this group, matching get was inotify_init->fsnotify_obtain_group */ 308d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 309272eb014SEric Paris 310272eb014SEric Paris return 0; 311272eb014SEric Paris } 312272eb014SEric Paris 313272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd, 314272eb014SEric Paris unsigned long arg) 315272eb014SEric Paris { 31663c882a0SEric Paris struct fsnotify_group *group; 3177053aee2SJan Kara struct fsnotify_event *fsn_event; 318272eb014SEric Paris void __user *p; 319272eb014SEric Paris int ret = -ENOTTY; 32063c882a0SEric Paris size_t send_len = 0; 321272eb014SEric Paris 32263c882a0SEric Paris group = file->private_data; 323272eb014SEric Paris p = (void __user *) arg; 324272eb014SEric Paris 3255ba08e2eSEric Paris pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd); 3265ba08e2eSEric Paris 327272eb014SEric Paris switch (cmd) { 328272eb014SEric Paris case FIONREAD: 329c21dbe20SJan Kara spin_lock(&group->notification_lock); 3307053aee2SJan Kara list_for_each_entry(fsn_event, &group->notification_list, 3317053aee2SJan Kara list) { 33263c882a0SEric Paris send_len += sizeof(struct inotify_event); 3337053aee2SJan Kara send_len += round_event_name_len(fsn_event); 33463c882a0SEric Paris } 335c21dbe20SJan Kara spin_unlock(&group->notification_lock); 33663c882a0SEric Paris ret = put_user(send_len, (int __user *) p); 337272eb014SEric Paris break; 338e1603b6eSKirill Tkhai #ifdef CONFIG_CHECKPOINT_RESTORE 339e1603b6eSKirill Tkhai case INOTIFY_IOC_SETNEXTWD: 340e1603b6eSKirill Tkhai ret = -EINVAL; 341e1603b6eSKirill Tkhai if (arg >= 1 && arg <= INT_MAX) { 342e1603b6eSKirill Tkhai struct inotify_group_private_data *data; 343e1603b6eSKirill Tkhai 344e1603b6eSKirill Tkhai data = &group->inotify_data; 345e1603b6eSKirill Tkhai spin_lock(&data->idr_lock); 346e1603b6eSKirill Tkhai idr_set_cursor(&data->idr, (unsigned int)arg); 347e1603b6eSKirill Tkhai spin_unlock(&data->idr_lock); 348e1603b6eSKirill Tkhai ret = 0; 349e1603b6eSKirill Tkhai } 350e1603b6eSKirill Tkhai break; 351e1603b6eSKirill Tkhai #endif /* CONFIG_CHECKPOINT_RESTORE */ 352272eb014SEric Paris } 353272eb014SEric Paris 354272eb014SEric Paris return ret; 355272eb014SEric Paris } 356272eb014SEric Paris 357272eb014SEric Paris static const struct file_operations inotify_fops = { 358be77196bSCyrill Gorcunov .show_fdinfo = inotify_show_fdinfo, 359272eb014SEric Paris .poll = inotify_poll, 360272eb014SEric Paris .read = inotify_read, 3610a6b6bd5SEric Paris .fasync = fsnotify_fasync, 362272eb014SEric Paris .release = inotify_release, 363272eb014SEric Paris .unlocked_ioctl = inotify_ioctl, 364272eb014SEric Paris .compat_ioctl = inotify_ioctl, 3656038f373SArnd Bergmann .llseek = noop_llseek, 366272eb014SEric Paris }; 367272eb014SEric Paris 368272eb014SEric Paris 36963c882a0SEric Paris /* 37063c882a0SEric Paris * find_inode - resolve a user-given path to a specific inode 37163c882a0SEric Paris */ 372ac5656d8SAaron Goidel static int inotify_find_inode(const char __user *dirname, struct path *path, 373ac5656d8SAaron Goidel unsigned int flags, __u64 mask) 37463c882a0SEric Paris { 37563c882a0SEric Paris int error; 37663c882a0SEric Paris 37763c882a0SEric Paris error = user_path_at(AT_FDCWD, dirname, flags, path); 37863c882a0SEric Paris if (error) 37963c882a0SEric Paris return error; 38063c882a0SEric Paris /* you can only watch an inode if you have read permissions on it */ 38102f92b38SChristian Brauner error = path_permission(path, MAY_READ); 382ac5656d8SAaron Goidel if (error) { 383ac5656d8SAaron Goidel path_put(path); 384ac5656d8SAaron Goidel return error; 385ac5656d8SAaron Goidel } 386ac5656d8SAaron Goidel error = security_path_notify(path, mask, 387ac5656d8SAaron Goidel FSNOTIFY_OBJ_TYPE_INODE); 38863c882a0SEric Paris if (error) 38963c882a0SEric Paris path_put(path); 390ac5656d8SAaron Goidel 39163c882a0SEric Paris return error; 39263c882a0SEric Paris } 39363c882a0SEric Paris 394b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock, 395000285deSEric Paris struct inotify_inode_mark *i_mark) 396b7ba8371SEric Paris { 397b7ba8371SEric Paris int ret; 398b7ba8371SEric Paris 3994542da63STejun Heo idr_preload(GFP_KERNEL); 400b7ba8371SEric Paris spin_lock(idr_lock); 4014542da63STejun Heo 402a66c04b4SJeff Layton ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT); 4034542da63STejun Heo if (ret >= 0) { 404b7ba8371SEric Paris /* we added the mark to the idr, take a reference */ 4054542da63STejun Heo i_mark->wd = ret; 406000285deSEric Paris fsnotify_get_mark(&i_mark->fsn_mark); 4077050c488SEric Paris } 408b7ba8371SEric Paris 4094542da63STejun Heo spin_unlock(idr_lock); 4104542da63STejun Heo idr_preload_end(); 4114542da63STejun Heo return ret < 0 ? ret : 0; 412b7ba8371SEric Paris } 413b7ba8371SEric Paris 414000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group, 415b7ba8371SEric Paris int wd) 416b7ba8371SEric Paris { 417b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 418b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 419000285deSEric Paris struct inotify_inode_mark *i_mark; 420b7ba8371SEric Paris 421b7ba8371SEric Paris assert_spin_locked(idr_lock); 422b7ba8371SEric Paris 423000285deSEric Paris i_mark = idr_find(idr, wd); 424000285deSEric Paris if (i_mark) { 425000285deSEric Paris struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark; 426b7ba8371SEric Paris 427000285deSEric Paris fsnotify_get_mark(fsn_mark); 428b7ba8371SEric Paris /* One ref for being in the idr, one ref we just took */ 429ab97f873SElena Reshetova BUG_ON(refcount_read(&fsn_mark->refcnt) < 2); 430b7ba8371SEric Paris } 431b7ba8371SEric Paris 432000285deSEric Paris return i_mark; 433b7ba8371SEric Paris } 434b7ba8371SEric Paris 435000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group, 436b7ba8371SEric Paris int wd) 437b7ba8371SEric Paris { 438000285deSEric Paris struct inotify_inode_mark *i_mark; 439b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 440b7ba8371SEric Paris 441b7ba8371SEric Paris spin_lock(idr_lock); 442000285deSEric Paris i_mark = inotify_idr_find_locked(group, wd); 443b7ba8371SEric Paris spin_unlock(idr_lock); 444b7ba8371SEric Paris 445000285deSEric Paris return i_mark; 446b7ba8371SEric Paris } 447b7ba8371SEric Paris 448dead537dSEric Paris /* 449dead537dSEric Paris * Remove the mark from the idr (if present) and drop the reference 450dead537dSEric Paris * on the mark because it was in the idr. 451dead537dSEric Paris */ 4527e790dd5SEric Paris static void inotify_remove_from_idr(struct fsnotify_group *group, 453000285deSEric Paris struct inotify_inode_mark *i_mark) 4547e790dd5SEric Paris { 455e7253760SJan Kara struct idr *idr = &group->inotify_data.idr; 456b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 457000285deSEric Paris struct inotify_inode_mark *found_i_mark = NULL; 458dead537dSEric Paris int wd; 4597e790dd5SEric Paris 460b7ba8371SEric Paris spin_lock(idr_lock); 461000285deSEric Paris wd = i_mark->wd; 462dead537dSEric Paris 463b7ba8371SEric Paris /* 464000285deSEric Paris * does this i_mark think it is in the idr? we shouldn't get called 465b7ba8371SEric Paris * if it wasn't.... 466b7ba8371SEric Paris */ 467b7ba8371SEric Paris if (wd == -1) { 46825c829afSJan Kara WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", 46925c829afSJan Kara __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); 470dead537dSEric Paris goto out; 4717e790dd5SEric Paris } 472dead537dSEric Paris 473b7ba8371SEric Paris /* Lets look in the idr to see if we find it */ 474000285deSEric Paris found_i_mark = inotify_idr_find_locked(group, wd); 475000285deSEric Paris if (unlikely(!found_i_mark)) { 47625c829afSJan Kara WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", 47725c829afSJan Kara __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); 478b7ba8371SEric Paris goto out; 479b7ba8371SEric Paris } 480dead537dSEric Paris 481b7ba8371SEric Paris /* 482000285deSEric Paris * We found an mark in the idr at the right wd, but it's 483000285deSEric Paris * not the mark we were told to remove. eparis seriously 484b7ba8371SEric Paris * fucked up somewhere. 485b7ba8371SEric Paris */ 486000285deSEric Paris if (unlikely(found_i_mark != i_mark)) { 487000285deSEric Paris WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p " 48825c829afSJan Kara "found_i_mark=%p found_i_mark->wd=%d " 48925c829afSJan Kara "found_i_mark->group=%p\n", __func__, i_mark, 49025c829afSJan Kara i_mark->wd, i_mark->fsn_mark.group, found_i_mark, 49125c829afSJan Kara found_i_mark->wd, found_i_mark->fsn_mark.group); 492b7ba8371SEric Paris goto out; 493b7ba8371SEric Paris } 494dead537dSEric Paris 495b7ba8371SEric Paris /* 496b7ba8371SEric Paris * One ref for being in the idr 497b7ba8371SEric Paris * one ref grabbed by inotify_idr_find 498b7ba8371SEric Paris */ 499ab97f873SElena Reshetova if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) { 50025c829afSJan Kara printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", 50125c829afSJan Kara __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); 502b7ba8371SEric Paris /* we can't really recover with bad ref cnting.. */ 503b7ba8371SEric Paris BUG(); 504b7ba8371SEric Paris } 505b7ba8371SEric Paris 506e7253760SJan Kara idr_remove(idr, wd); 507e7253760SJan Kara /* Removed from the idr, drop that ref. */ 508e7253760SJan Kara fsnotify_put_mark(&i_mark->fsn_mark); 509dead537dSEric Paris out: 510e7253760SJan Kara i_mark->wd = -1; 511e7253760SJan Kara spin_unlock(idr_lock); 512b7ba8371SEric Paris /* match the ref taken by inotify_idr_find_locked() */ 513000285deSEric Paris if (found_i_mark) 514000285deSEric Paris fsnotify_put_mark(&found_i_mark->fsn_mark); 515dead537dSEric Paris } 516dead537dSEric Paris 51763c882a0SEric Paris /* 518dead537dSEric Paris * Send IN_IGNORED for this wd, remove this wd from the idr. 51963c882a0SEric Paris */ 520000285deSEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark, 521528da3e9SEric Paris struct fsnotify_group *group) 52263c882a0SEric Paris { 523000285deSEric Paris struct inotify_inode_mark *i_mark; 5247053aee2SJan Kara 5257053aee2SJan Kara /* Queue ignore event for the watch */ 5261a2620a9SAmir Goldstein inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL, 5271a2620a9SAmir Goldstein 0); 52863c882a0SEric Paris 5298b99c3ccSLino Sanfilippo i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 530000285deSEric Paris /* remove this mark from the idr */ 531000285deSEric Paris inotify_remove_from_idr(group, i_mark); 53263c882a0SEric Paris 5331cce1eeaSNikolay Borisov dec_inotify_watches(group->inotify_data.ucounts); 53463c882a0SEric Paris } 53563c882a0SEric Paris 53652cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group, 53752cef755SEric Paris struct inode *inode, 53852cef755SEric Paris u32 arg) 53963c882a0SEric Paris { 540000285deSEric Paris struct fsnotify_mark *fsn_mark; 541000285deSEric Paris struct inotify_inode_mark *i_mark; 54263c882a0SEric Paris __u32 old_mask, new_mask; 54338035c04SAmir Goldstein int replace = !(arg & IN_MASK_ADD); 5444d97f7d5SHenry Wilson int create = (arg & IN_MASK_CREATE); 54552cef755SEric Paris int ret; 54663c882a0SEric Paris 547*230d97d3SAmir Goldstein fsn_mark = fsnotify_find_inode_mark(inode, group); 548000285deSEric Paris if (!fsn_mark) 54952cef755SEric Paris return -ENOENT; 55062c9d267SZhangXiaoxu else if (create) { 55162c9d267SZhangXiaoxu ret = -EEXIST; 55262c9d267SZhangXiaoxu goto out; 55362c9d267SZhangXiaoxu } 55452cef755SEric Paris 555000285deSEric Paris i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 55675fe2b26SEric Paris 557000285deSEric Paris spin_lock(&fsn_mark->lock); 558000285deSEric Paris old_mask = fsn_mark->mask; 55938035c04SAmir Goldstein if (replace) { 56038035c04SAmir Goldstein fsn_mark->mask = 0; 56138035c04SAmir Goldstein fsn_mark->flags &= ~INOTIFY_MARK_FLAGS; 56238035c04SAmir Goldstein } 56338035c04SAmir Goldstein fsn_mark->mask |= inotify_arg_to_mask(inode, arg); 56438035c04SAmir Goldstein fsn_mark->flags |= inotify_arg_to_flags(arg); 565000285deSEric Paris new_mask = fsn_mark->mask; 566000285deSEric Paris spin_unlock(&fsn_mark->lock); 56763c882a0SEric Paris 56863c882a0SEric Paris if (old_mask != new_mask) { 56963c882a0SEric Paris /* more bits in old than in new? */ 57063c882a0SEric Paris int dropped = (old_mask & ~new_mask); 571000285deSEric Paris /* more bits in this fsn_mark than the inode's mask? */ 57263c882a0SEric Paris int do_inode = (new_mask & ~inode->i_fsnotify_mask); 57363c882a0SEric Paris 574000285deSEric Paris /* update the inode with this new fsn_mark */ 57563c882a0SEric Paris if (dropped || do_inode) 5768920d273SJan Kara fsnotify_recalc_mask(inode->i_fsnotify_marks); 57763c882a0SEric Paris 57863c882a0SEric Paris } 57963c882a0SEric Paris 58052cef755SEric Paris /* return the wd */ 581000285deSEric Paris ret = i_mark->wd; 58252cef755SEric Paris 58362c9d267SZhangXiaoxu out: 584d0775441SEric Paris /* match the get from fsnotify_find_mark() */ 585000285deSEric Paris fsnotify_put_mark(fsn_mark); 58675fe2b26SEric Paris 58752cef755SEric Paris return ret; 58863c882a0SEric Paris } 5897e790dd5SEric Paris 59052cef755SEric Paris static int inotify_new_watch(struct fsnotify_group *group, 59152cef755SEric Paris struct inode *inode, 59252cef755SEric Paris u32 arg) 59352cef755SEric Paris { 594000285deSEric Paris struct inotify_inode_mark *tmp_i_mark; 59552cef755SEric Paris int ret; 596b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 597b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 59852cef755SEric Paris 599000285deSEric Paris tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); 600000285deSEric Paris if (unlikely(!tmp_i_mark)) 60152cef755SEric Paris return -ENOMEM; 60252cef755SEric Paris 603054c636eSJan Kara fsnotify_init_mark(&tmp_i_mark->fsn_mark, group); 60438035c04SAmir Goldstein tmp_i_mark->fsn_mark.mask = inotify_arg_to_mask(inode, arg); 60538035c04SAmir Goldstein tmp_i_mark->fsn_mark.flags = inotify_arg_to_flags(arg); 606000285deSEric Paris tmp_i_mark->wd = -1; 60752cef755SEric Paris 608a66c04b4SJeff Layton ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark); 609b7ba8371SEric Paris if (ret) 61052cef755SEric Paris goto out_err; 61152cef755SEric Paris 6121cce1eeaSNikolay Borisov /* increment the number of watches the user has */ 6131cce1eeaSNikolay Borisov if (!inc_inotify_watches(group->inotify_data.ucounts)) { 6141cce1eeaSNikolay Borisov inotify_remove_from_idr(group, tmp_i_mark); 6151cce1eeaSNikolay Borisov ret = -ENOSPC; 6161cce1eeaSNikolay Borisov goto out_err; 6171cce1eeaSNikolay Borisov } 6181cce1eeaSNikolay Borisov 61952cef755SEric Paris /* we are on the idr, now get on the inode */ 620b249f5beSAmir Goldstein ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0); 62152cef755SEric Paris if (ret) { 62252cef755SEric Paris /* we failed to get on the inode, get off the idr */ 623000285deSEric Paris inotify_remove_from_idr(group, tmp_i_mark); 62452cef755SEric Paris goto out_err; 62552cef755SEric Paris } 62652cef755SEric Paris 62752cef755SEric Paris 628000285deSEric Paris /* return the watch descriptor for this new mark */ 629000285deSEric Paris ret = tmp_i_mark->wd; 63052cef755SEric Paris 63152cef755SEric Paris out_err: 632000285deSEric Paris /* match the ref from fsnotify_init_mark() */ 633000285deSEric Paris fsnotify_put_mark(&tmp_i_mark->fsn_mark); 63452cef755SEric Paris 63552cef755SEric Paris return ret; 63652cef755SEric Paris } 63752cef755SEric Paris 63852cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg) 63952cef755SEric Paris { 64052cef755SEric Paris int ret = 0; 64152cef755SEric Paris 642642054b8SAmir Goldstein fsnotify_group_lock(group); 64352cef755SEric Paris /* try to update and existing watch with the new arg */ 64452cef755SEric Paris ret = inotify_update_existing_watch(group, inode, arg); 64552cef755SEric Paris /* no mark present, try to add a new one */ 64652cef755SEric Paris if (ret == -ENOENT) 64752cef755SEric Paris ret = inotify_new_watch(group, inode, arg); 648642054b8SAmir Goldstein fsnotify_group_unlock(group); 64952cef755SEric Paris 65063c882a0SEric Paris return ret; 65163c882a0SEric Paris } 65263c882a0SEric Paris 653d0de4dc5SEric Paris static struct fsnotify_group *inotify_new_group(unsigned int max_events) 65463c882a0SEric Paris { 65563c882a0SEric Paris struct fsnotify_group *group; 656ff57cd58SJan Kara struct inotify_event_info *oevent; 65763c882a0SEric Paris 658867a448dSAmir Goldstein group = fsnotify_alloc_group(&inotify_fsnotify_ops, 659867a448dSAmir Goldstein FSNOTIFY_GROUP_USER); 66063c882a0SEric Paris if (IS_ERR(group)) 66163c882a0SEric Paris return group; 66263c882a0SEric Paris 663ac7b79fdSShakeel Butt oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL_ACCOUNT); 664ff57cd58SJan Kara if (unlikely(!oevent)) { 665ff57cd58SJan Kara fsnotify_destroy_group(group); 666ff57cd58SJan Kara return ERR_PTR(-ENOMEM); 667ff57cd58SJan Kara } 668ff57cd58SJan Kara group->overflow_event = &oevent->fse; 6698988f11aSAmir Goldstein fsnotify_init_event(group->overflow_event); 670a0a92d26SAmir Goldstein oevent->mask = FS_Q_OVERFLOW; 671ff57cd58SJan Kara oevent->wd = -1; 672ff57cd58SJan Kara oevent->sync_cookie = 0; 673ff57cd58SJan Kara oevent->name_len = 0; 674ff57cd58SJan Kara 67563c882a0SEric Paris group->max_events = max_events; 676d46eb14bSShakeel Butt group->memcg = get_mem_cgroup_from_mm(current->mm); 67763c882a0SEric Paris 67863c882a0SEric Paris spin_lock_init(&group->inotify_data.idr_lock); 67963c882a0SEric Paris idr_init(&group->inotify_data.idr); 6801cce1eeaSNikolay Borisov group->inotify_data.ucounts = inc_ucount(current_user_ns(), 6811cce1eeaSNikolay Borisov current_euid(), 6821cce1eeaSNikolay Borisov UCOUNT_INOTIFY_INSTANCES); 683d0de4dc5SEric Paris 6841cce1eeaSNikolay Borisov if (!group->inotify_data.ucounts) { 685d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 686d0de4dc5SEric Paris return ERR_PTR(-EMFILE); 687d0de4dc5SEric Paris } 68863c882a0SEric Paris 68963c882a0SEric Paris return group; 69063c882a0SEric Paris } 69163c882a0SEric Paris 69263c882a0SEric Paris 69363c882a0SEric Paris /* inotify syscalls */ 694d0d89d1eSDominik Brodowski static int do_inotify_init(int flags) 695272eb014SEric Paris { 69663c882a0SEric Paris struct fsnotify_group *group; 697c44dcc56SAl Viro int ret; 698272eb014SEric Paris 699272eb014SEric Paris /* Check the IN_* constants for consistency. */ 700272eb014SEric Paris BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC); 701272eb014SEric Paris BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK); 702272eb014SEric Paris 703272eb014SEric Paris if (flags & ~(IN_CLOEXEC | IN_NONBLOCK)) 704272eb014SEric Paris return -EINVAL; 705272eb014SEric Paris 70663c882a0SEric Paris /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */ 707d0de4dc5SEric Paris group = inotify_new_group(inotify_max_queued_events); 708d0de4dc5SEric Paris if (IS_ERR(group)) 709d0de4dc5SEric Paris return PTR_ERR(group); 710825f9692SAl Viro 711c44dcc56SAl Viro ret = anon_inode_getfd("inotify", &inotify_fops, group, 712c44dcc56SAl Viro O_RDONLY | flags); 713d0de4dc5SEric Paris if (ret < 0) 714d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 715d0de4dc5SEric Paris 716272eb014SEric Paris return ret; 717272eb014SEric Paris } 718272eb014SEric Paris 719d0d89d1eSDominik Brodowski SYSCALL_DEFINE1(inotify_init1, int, flags) 720d0d89d1eSDominik Brodowski { 721d0d89d1eSDominik Brodowski return do_inotify_init(flags); 722d0d89d1eSDominik Brodowski } 723d0d89d1eSDominik Brodowski 724938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init) 725272eb014SEric Paris { 726d0d89d1eSDominik Brodowski return do_inotify_init(0); 727272eb014SEric Paris } 728272eb014SEric Paris 7292e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, 7302e4d0924SHeiko Carstens u32, mask) 731272eb014SEric Paris { 73263c882a0SEric Paris struct fsnotify_group *group; 733272eb014SEric Paris struct inode *inode; 734272eb014SEric Paris struct path path; 7352903ff01SAl Viro struct fd f; 7362903ff01SAl Viro int ret; 737272eb014SEric Paris unsigned flags = 0; 738272eb014SEric Paris 739d30e2c05SDave Hansen /* 740d30e2c05SDave Hansen * We share a lot of code with fs/dnotify. We also share 741d30e2c05SDave Hansen * the bit layout between inotify's IN_* and the fsnotify 742d30e2c05SDave Hansen * FS_*. This check ensures that only the inotify IN_* 743d30e2c05SDave Hansen * bits get passed in and set in watches/events. 744d30e2c05SDave Hansen */ 745d30e2c05SDave Hansen if (unlikely(mask & ~ALL_INOTIFY_BITS)) 746d30e2c05SDave Hansen return -EINVAL; 747d30e2c05SDave Hansen /* 748d30e2c05SDave Hansen * Require at least one valid bit set in the mask. 749d30e2c05SDave Hansen * Without _something_ set, we would have no events to 750d30e2c05SDave Hansen * watch for. 751d30e2c05SDave Hansen */ 75204df32faSZhao Hongjiang if (unlikely(!(mask & ALL_INOTIFY_BITS))) 75304df32faSZhao Hongjiang return -EINVAL; 75404df32faSZhao Hongjiang 7552903ff01SAl Viro f = fdget(fd); 7562903ff01SAl Viro if (unlikely(!f.file)) 757272eb014SEric Paris return -EBADF; 758272eb014SEric Paris 7594d97f7d5SHenry Wilson /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */ 760125892edSTetsuo Handa if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) { 761125892edSTetsuo Handa ret = -EINVAL; 762125892edSTetsuo Handa goto fput_and_out; 763125892edSTetsuo Handa } 7644d97f7d5SHenry Wilson 765272eb014SEric Paris /* verify that this is indeed an inotify instance */ 7662903ff01SAl Viro if (unlikely(f.file->f_op != &inotify_fops)) { 767272eb014SEric Paris ret = -EINVAL; 768272eb014SEric Paris goto fput_and_out; 769272eb014SEric Paris } 770272eb014SEric Paris 771272eb014SEric Paris if (!(mask & IN_DONT_FOLLOW)) 772272eb014SEric Paris flags |= LOOKUP_FOLLOW; 773272eb014SEric Paris if (mask & IN_ONLYDIR) 774272eb014SEric Paris flags |= LOOKUP_DIRECTORY; 775272eb014SEric Paris 776ac5656d8SAaron Goidel ret = inotify_find_inode(pathname, &path, flags, 777ac5656d8SAaron Goidel (mask & IN_ALL_EVENTS)); 77863c882a0SEric Paris if (ret) 779272eb014SEric Paris goto fput_and_out; 780272eb014SEric Paris 78163c882a0SEric Paris /* inode held in place by reference to path; group by fget on fd */ 782272eb014SEric Paris inode = path.dentry->d_inode; 7832903ff01SAl Viro group = f.file->private_data; 784272eb014SEric Paris 78563c882a0SEric Paris /* create/update an inode mark */ 78663c882a0SEric Paris ret = inotify_update_watch(group, inode, mask); 787272eb014SEric Paris path_put(&path); 788272eb014SEric Paris fput_and_out: 7892903ff01SAl Viro fdput(f); 790272eb014SEric Paris return ret; 791272eb014SEric Paris } 792272eb014SEric Paris 7932e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd) 794272eb014SEric Paris { 79563c882a0SEric Paris struct fsnotify_group *group; 796000285deSEric Paris struct inotify_inode_mark *i_mark; 7972903ff01SAl Viro struct fd f; 7987b26aa24Syoungjun int ret = -EINVAL; 799272eb014SEric Paris 8002903ff01SAl Viro f = fdget(fd); 8012903ff01SAl Viro if (unlikely(!f.file)) 802272eb014SEric Paris return -EBADF; 803272eb014SEric Paris 804272eb014SEric Paris /* verify that this is indeed an inotify instance */ 8052903ff01SAl Viro if (unlikely(f.file->f_op != &inotify_fops)) 806272eb014SEric Paris goto out; 807272eb014SEric Paris 8082903ff01SAl Viro group = f.file->private_data; 809272eb014SEric Paris 810000285deSEric Paris i_mark = inotify_idr_find(group, wd); 811000285deSEric Paris if (unlikely(!i_mark)) 81263c882a0SEric Paris goto out; 81363c882a0SEric Paris 814b7ba8371SEric Paris ret = 0; 815b7ba8371SEric Paris 816e2a29943SLino Sanfilippo fsnotify_destroy_mark(&i_mark->fsn_mark, group); 817b7ba8371SEric Paris 818b7ba8371SEric Paris /* match ref taken by inotify_idr_find */ 819000285deSEric Paris fsnotify_put_mark(&i_mark->fsn_mark); 820272eb014SEric Paris 821272eb014SEric Paris out: 8222903ff01SAl Viro fdput(f); 823272eb014SEric Paris return ret; 824272eb014SEric Paris } 825272eb014SEric Paris 826272eb014SEric Paris /* 827ae0e47f0SJustin P. Mattock * inotify_user_setup - Our initialization function. Note that we cannot return 828272eb014SEric Paris * error because we have compiled-in VFS hooks. So an (unlikely) failure here 829272eb014SEric Paris * must result in panic(). 830272eb014SEric Paris */ 831272eb014SEric Paris static int __init inotify_user_setup(void) 832272eb014SEric Paris { 83392890123SWaiman Long unsigned long watches_max; 83492890123SWaiman Long struct sysinfo si; 83592890123SWaiman Long 83692890123SWaiman Long si_meminfo(&si); 83792890123SWaiman Long /* 83892890123SWaiman Long * Allow up to 1% of addressable memory to be allocated for inotify 83992890123SWaiman Long * watches (per user) limited to the range [8192, 1048576]. 84092890123SWaiman Long */ 84192890123SWaiman Long watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) / 84292890123SWaiman Long INOTIFY_WATCH_COST; 84392890123SWaiman Long watches_max = clamp(watches_max, 8192UL, 1048576UL); 84492890123SWaiman Long 845f874e1acSEric Paris BUILD_BUG_ON(IN_ACCESS != FS_ACCESS); 846f874e1acSEric Paris BUILD_BUG_ON(IN_MODIFY != FS_MODIFY); 847f874e1acSEric Paris BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB); 848f874e1acSEric Paris BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE); 849f874e1acSEric Paris BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); 850f874e1acSEric Paris BUILD_BUG_ON(IN_OPEN != FS_OPEN); 851f874e1acSEric Paris BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM); 852f874e1acSEric Paris BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO); 853f874e1acSEric Paris BUILD_BUG_ON(IN_CREATE != FS_CREATE); 854f874e1acSEric Paris BUILD_BUG_ON(IN_DELETE != FS_DELETE); 855f874e1acSEric Paris BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF); 856f874e1acSEric Paris BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF); 857f874e1acSEric Paris BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT); 858f874e1acSEric Paris BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW); 859f874e1acSEric Paris BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED); 860b29866aaSEric Paris BUILD_BUG_ON(IN_ISDIR != FS_ISDIR); 861f874e1acSEric Paris 862a39f7ec4SAmir Goldstein BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22); 863f874e1acSEric Paris 864d46eb14bSShakeel Butt inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, 865d46eb14bSShakeel Butt SLAB_PANIC|SLAB_ACCOUNT); 86663c882a0SEric Paris 867272eb014SEric Paris inotify_max_queued_events = 16384; 8681cce1eeaSNikolay Borisov init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128; 86992890123SWaiman Long init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max; 8707b9ad122SXiaoming Ni inotify_sysctls_init(); 871272eb014SEric Paris 872272eb014SEric Paris return 0; 873272eb014SEric Paris } 874c013d5a4SPaul Gortmaker fs_initcall(inotify_user_setup); 875