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 52054c636eSJan Kara 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 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 }, 88ab09203eSEric W. Biederman { } 89272eb014SEric Paris }; 907b9ad122SXiaoming Ni 917b9ad122SXiaoming Ni static void __init inotify_sysctls_init(void) 927b9ad122SXiaoming Ni { 937b9ad122SXiaoming Ni register_sysctl("fs/inotify", inotify_table); 947b9ad122SXiaoming Ni } 957b9ad122SXiaoming Ni 967b9ad122SXiaoming Ni #else 977b9ad122SXiaoming Ni #define inotify_sysctls_init() do { } while (0) 98272eb014SEric Paris #endif /* CONFIG_SYSCTL */ 99272eb014SEric Paris 100957f7b47SAmir Goldstein static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg) 101272eb014SEric Paris { 10263c882a0SEric Paris __u32 mask; 10363c882a0SEric Paris 104611da04fSEric Paris /* 105e0462f91SGabriel Krisman Bertazi * Everything should receive events when the inode is unmounted. 106e0462f91SGabriel Krisman Bertazi * All directories care about children. 107611da04fSEric Paris */ 108e0462f91SGabriel Krisman Bertazi mask = (FS_UNMOUNT); 109957f7b47SAmir Goldstein if (S_ISDIR(inode->i_mode)) 110957f7b47SAmir Goldstein mask |= FS_EVENT_ON_CHILD; 11163c882a0SEric Paris 11263c882a0SEric Paris /* mask off the flags used to open the fd */ 113a32e697cSAmir Goldstein mask |= (arg & INOTIFY_USER_MASK); 11463c882a0SEric Paris 11563c882a0SEric Paris return mask; 116272eb014SEric Paris } 117272eb014SEric Paris 11838035c04SAmir Goldstein #define INOTIFY_MARK_FLAGS \ 11938035c04SAmir Goldstein (FSNOTIFY_MARK_FLAG_EXCL_UNLINK | FSNOTIFY_MARK_FLAG_IN_ONESHOT) 12038035c04SAmir Goldstein 12138035c04SAmir Goldstein static inline unsigned int inotify_arg_to_flags(u32 arg) 12238035c04SAmir Goldstein { 12338035c04SAmir Goldstein unsigned int flags = 0; 12438035c04SAmir Goldstein 12538035c04SAmir Goldstein if (arg & IN_EXCL_UNLINK) 12638035c04SAmir Goldstein flags |= FSNOTIFY_MARK_FLAG_EXCL_UNLINK; 12738035c04SAmir Goldstein if (arg & IN_ONESHOT) 12838035c04SAmir Goldstein flags |= FSNOTIFY_MARK_FLAG_IN_ONESHOT; 12938035c04SAmir Goldstein 13038035c04SAmir Goldstein return flags; 13138035c04SAmir Goldstein } 13238035c04SAmir Goldstein 13363c882a0SEric Paris static inline u32 inotify_mask_to_arg(__u32 mask) 134272eb014SEric Paris { 13563c882a0SEric Paris return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED | 13663c882a0SEric Paris IN_Q_OVERFLOW); 137272eb014SEric Paris } 138272eb014SEric Paris 139*c05787b4SOliver Ford /* inotify userspace file descriptor functions */ 140076ccb76SAl Viro static __poll_t inotify_poll(struct file *file, poll_table *wait) 141272eb014SEric Paris { 14263c882a0SEric Paris struct fsnotify_group *group = file->private_data; 143076ccb76SAl Viro __poll_t ret = 0; 144272eb014SEric Paris 14563c882a0SEric Paris poll_wait(file, &group->notification_waitq, wait); 146c21dbe20SJan Kara spin_lock(&group->notification_lock); 14763c882a0SEric Paris if (!fsnotify_notify_queue_is_empty(group)) 148a9a08845SLinus Torvalds ret = EPOLLIN | EPOLLRDNORM; 149c21dbe20SJan Kara spin_unlock(&group->notification_lock); 150272eb014SEric Paris 151272eb014SEric Paris return ret; 152272eb014SEric Paris } 153272eb014SEric Paris 1547053aee2SJan Kara static int round_event_name_len(struct fsnotify_event *fsn_event) 155e9fe6904SJan Kara { 1567053aee2SJan Kara struct inotify_event_info *event; 1577053aee2SJan Kara 1587053aee2SJan Kara event = INOTIFY_E(fsn_event); 159e9fe6904SJan Kara if (!event->name_len) 160e9fe6904SJan Kara return 0; 161e9fe6904SJan Kara return roundup(event->name_len + 1, sizeof(struct inotify_event)); 162e9fe6904SJan Kara } 163e9fe6904SJan Kara 1643632dee2SVegard Nossum /* 1653632dee2SVegard Nossum * Get an inotify_kernel_event if one exists and is small 1663632dee2SVegard Nossum * enough to fit in "count". Return an error pointer if 1673632dee2SVegard Nossum * not large enough. 1683632dee2SVegard Nossum * 169c21dbe20SJan Kara * Called with the group->notification_lock held. 1703632dee2SVegard Nossum */ 17163c882a0SEric Paris static struct fsnotify_event *get_one_event(struct fsnotify_group *group, 1723632dee2SVegard Nossum size_t count) 1733632dee2SVegard Nossum { 1743632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 17563c882a0SEric Paris struct fsnotify_event *event; 1763632dee2SVegard Nossum 1778ba8fa91SJan Kara event = fsnotify_peek_first_event(group); 1786f73171eSAmir Goldstein if (!event) 1796f73171eSAmir Goldstein return NULL; 18063c882a0SEric Paris 1815ba08e2eSEric Paris pr_debug("%s: group=%p event=%p\n", __func__, group, event); 1825ba08e2eSEric Paris 183e9fe6904SJan Kara event_size += round_event_name_len(event); 1843632dee2SVegard Nossum if (event_size > count) 1853632dee2SVegard Nossum return ERR_PTR(-EINVAL); 1863632dee2SVegard Nossum 187c21dbe20SJan Kara /* held the notification_lock the whole time, so this is the 18863c882a0SEric Paris * same event we peeked above */ 1898ba8fa91SJan Kara fsnotify_remove_first_event(group); 19063c882a0SEric Paris 19163c882a0SEric Paris return event; 1923632dee2SVegard Nossum } 1933632dee2SVegard Nossum 1943632dee2SVegard Nossum /* 1953632dee2SVegard Nossum * Copy an event to user space, returning how much we copied. 1963632dee2SVegard Nossum * 1973632dee2SVegard Nossum * We already checked that the event size is smaller than the 1983632dee2SVegard Nossum * buffer we had in "get_one_event()" above. 1993632dee2SVegard Nossum */ 20063c882a0SEric Paris static ssize_t copy_event_to_user(struct fsnotify_group *group, 2017053aee2SJan Kara struct fsnotify_event *fsn_event, 2023632dee2SVegard Nossum char __user *buf) 2033632dee2SVegard Nossum { 20463c882a0SEric Paris struct inotify_event inotify_event; 2057053aee2SJan Kara struct inotify_event_info *event; 2063632dee2SVegard Nossum size_t event_size = sizeof(struct inotify_event); 207e9fe6904SJan Kara size_t name_len; 208e9fe6904SJan Kara size_t pad_name_len; 2093632dee2SVegard Nossum 2107053aee2SJan Kara pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event); 2115ba08e2eSEric Paris 2127053aee2SJan Kara event = INOTIFY_E(fsn_event); 213e9fe6904SJan Kara name_len = event->name_len; 214b962e731SBrian Rogers /* 215e9fe6904SJan Kara * round up name length so it is a multiple of event_size 2160db501bdSEric W. Biederman * plus an extra byte for the terminating '\0'. 2170db501bdSEric W. Biederman */ 2187053aee2SJan Kara pad_name_len = round_event_name_len(fsn_event); 219e9fe6904SJan Kara inotify_event.len = pad_name_len; 220a0a92d26SAmir Goldstein inotify_event.mask = inotify_mask_to_arg(event->mask); 2217053aee2SJan Kara inotify_event.wd = event->wd; 22263c882a0SEric Paris inotify_event.cookie = event->sync_cookie; 22363c882a0SEric Paris 22463c882a0SEric Paris /* send the main event */ 22563c882a0SEric Paris if (copy_to_user(buf, &inotify_event, event_size)) 2263632dee2SVegard Nossum return -EFAULT; 2273632dee2SVegard Nossum 2283632dee2SVegard Nossum buf += event_size; 2293632dee2SVegard Nossum 23063c882a0SEric Paris /* 23163c882a0SEric Paris * fsnotify only stores the pathname, so here we have to send the pathname 23263c882a0SEric Paris * and then pad that pathname out to a multiple of sizeof(inotify_event) 233e9fe6904SJan Kara * with zeros. 23463c882a0SEric Paris */ 235e9fe6904SJan Kara if (pad_name_len) { 23663c882a0SEric Paris /* copy the path name */ 2377053aee2SJan Kara if (copy_to_user(buf, event->name, name_len)) 2383632dee2SVegard Nossum return -EFAULT; 239e9fe6904SJan Kara buf += name_len; 2403632dee2SVegard Nossum 2410db501bdSEric W. Biederman /* fill userspace with 0's */ 242e9fe6904SJan Kara if (clear_user(buf, pad_name_len - name_len)) 24363c882a0SEric Paris return -EFAULT; 244e9fe6904SJan Kara event_size += pad_name_len; 2453632dee2SVegard Nossum } 24663c882a0SEric Paris 2473632dee2SVegard Nossum return event_size; 2483632dee2SVegard Nossum } 2493632dee2SVegard Nossum 250272eb014SEric Paris static ssize_t inotify_read(struct file *file, char __user *buf, 251272eb014SEric Paris size_t count, loff_t *pos) 252272eb014SEric Paris { 25363c882a0SEric Paris struct fsnotify_group *group; 25463c882a0SEric Paris struct fsnotify_event *kevent; 255272eb014SEric Paris char __user *start; 256272eb014SEric Paris int ret; 257e23738a7SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 258272eb014SEric Paris 259272eb014SEric Paris start = buf; 26063c882a0SEric Paris group = file->private_data; 261272eb014SEric Paris 262e23738a7SPeter Zijlstra add_wait_queue(&group->notification_waitq, &wait); 263272eb014SEric Paris while (1) { 264c21dbe20SJan Kara spin_lock(&group->notification_lock); 26563c882a0SEric Paris kevent = get_one_event(group, count); 266c21dbe20SJan Kara spin_unlock(&group->notification_lock); 267272eb014SEric Paris 2685ba08e2eSEric Paris pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent); 2695ba08e2eSEric Paris 2703632dee2SVegard Nossum if (kevent) { 2713632dee2SVegard Nossum ret = PTR_ERR(kevent); 2723632dee2SVegard Nossum if (IS_ERR(kevent)) 273272eb014SEric Paris break; 27463c882a0SEric Paris ret = copy_event_to_user(group, kevent, buf); 2757053aee2SJan Kara fsnotify_destroy_event(group, kevent); 2763632dee2SVegard Nossum if (ret < 0) 2773632dee2SVegard Nossum break; 2783632dee2SVegard Nossum buf += ret; 2793632dee2SVegard Nossum count -= ret; 2803632dee2SVegard Nossum continue; 281272eb014SEric Paris } 282272eb014SEric Paris 2833632dee2SVegard Nossum ret = -EAGAIN; 2843632dee2SVegard Nossum if (file->f_flags & O_NONBLOCK) 285272eb014SEric Paris break; 2861ca39ab9SEric Paris ret = -ERESTARTSYS; 2873632dee2SVegard Nossum if (signal_pending(current)) 2883632dee2SVegard Nossum break; 2893632dee2SVegard Nossum 2903632dee2SVegard Nossum if (start != buf) 2913632dee2SVegard Nossum break; 292272eb014SEric Paris 293e23738a7SPeter Zijlstra wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 294272eb014SEric Paris } 295e23738a7SPeter Zijlstra remove_wait_queue(&group->notification_waitq, &wait); 296272eb014SEric Paris 2973632dee2SVegard Nossum if (start != buf && ret != -EFAULT) 298272eb014SEric Paris ret = buf - start; 299272eb014SEric Paris return ret; 300272eb014SEric Paris } 301272eb014SEric Paris 302272eb014SEric Paris static int inotify_release(struct inode *ignored, struct file *file) 303272eb014SEric Paris { 30463c882a0SEric Paris struct fsnotify_group *group = file->private_data; 305272eb014SEric Paris 3065ba08e2eSEric Paris pr_debug("%s: group=%p\n", __func__, group); 3075ba08e2eSEric Paris 30863c882a0SEric Paris /* free this group, matching get was inotify_init->fsnotify_obtain_group */ 309d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 310272eb014SEric Paris 311272eb014SEric Paris return 0; 312272eb014SEric Paris } 313272eb014SEric Paris 314272eb014SEric Paris static long inotify_ioctl(struct file *file, unsigned int cmd, 315272eb014SEric Paris unsigned long arg) 316272eb014SEric Paris { 31763c882a0SEric Paris struct fsnotify_group *group; 3187053aee2SJan Kara struct fsnotify_event *fsn_event; 319272eb014SEric Paris void __user *p; 320272eb014SEric Paris int ret = -ENOTTY; 32163c882a0SEric Paris size_t send_len = 0; 322272eb014SEric Paris 32363c882a0SEric Paris group = file->private_data; 324272eb014SEric Paris p = (void __user *) arg; 325272eb014SEric Paris 3265ba08e2eSEric Paris pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd); 3275ba08e2eSEric Paris 328272eb014SEric Paris switch (cmd) { 329272eb014SEric Paris case FIONREAD: 330c21dbe20SJan Kara spin_lock(&group->notification_lock); 3317053aee2SJan Kara list_for_each_entry(fsn_event, &group->notification_list, 3327053aee2SJan Kara list) { 33363c882a0SEric Paris send_len += sizeof(struct inotify_event); 3347053aee2SJan Kara send_len += round_event_name_len(fsn_event); 33563c882a0SEric Paris } 336c21dbe20SJan Kara spin_unlock(&group->notification_lock); 33763c882a0SEric Paris ret = put_user(send_len, (int __user *) p); 338272eb014SEric Paris break; 339e1603b6eSKirill Tkhai #ifdef CONFIG_CHECKPOINT_RESTORE 340e1603b6eSKirill Tkhai case INOTIFY_IOC_SETNEXTWD: 341e1603b6eSKirill Tkhai ret = -EINVAL; 342e1603b6eSKirill Tkhai if (arg >= 1 && arg <= INT_MAX) { 343e1603b6eSKirill Tkhai struct inotify_group_private_data *data; 344e1603b6eSKirill Tkhai 345e1603b6eSKirill Tkhai data = &group->inotify_data; 346e1603b6eSKirill Tkhai spin_lock(&data->idr_lock); 347e1603b6eSKirill Tkhai idr_set_cursor(&data->idr, (unsigned int)arg); 348e1603b6eSKirill Tkhai spin_unlock(&data->idr_lock); 349e1603b6eSKirill Tkhai ret = 0; 350e1603b6eSKirill Tkhai } 351e1603b6eSKirill Tkhai break; 352e1603b6eSKirill Tkhai #endif /* CONFIG_CHECKPOINT_RESTORE */ 353272eb014SEric Paris } 354272eb014SEric Paris 355272eb014SEric Paris return ret; 356272eb014SEric Paris } 357272eb014SEric Paris 358272eb014SEric Paris static const struct file_operations inotify_fops = { 359be77196bSCyrill Gorcunov .show_fdinfo = inotify_show_fdinfo, 360272eb014SEric Paris .poll = inotify_poll, 361272eb014SEric Paris .read = inotify_read, 3620a6b6bd5SEric Paris .fasync = fsnotify_fasync, 363272eb014SEric Paris .release = inotify_release, 364272eb014SEric Paris .unlocked_ioctl = inotify_ioctl, 365272eb014SEric Paris .compat_ioctl = inotify_ioctl, 3666038f373SArnd Bergmann .llseek = noop_llseek, 367272eb014SEric Paris }; 368272eb014SEric Paris 369272eb014SEric Paris 37063c882a0SEric Paris /* 37163c882a0SEric Paris * find_inode - resolve a user-given path to a specific inode 37263c882a0SEric Paris */ 373ac5656d8SAaron Goidel static int inotify_find_inode(const char __user *dirname, struct path *path, 374ac5656d8SAaron Goidel unsigned int flags, __u64 mask) 37563c882a0SEric Paris { 37663c882a0SEric Paris int error; 37763c882a0SEric Paris 37863c882a0SEric Paris error = user_path_at(AT_FDCWD, dirname, flags, path); 37963c882a0SEric Paris if (error) 38063c882a0SEric Paris return error; 38163c882a0SEric Paris /* you can only watch an inode if you have read permissions on it */ 38202f92b38SChristian Brauner error = path_permission(path, MAY_READ); 383ac5656d8SAaron Goidel if (error) { 384ac5656d8SAaron Goidel path_put(path); 385ac5656d8SAaron Goidel return error; 386ac5656d8SAaron Goidel } 387ac5656d8SAaron Goidel error = security_path_notify(path, mask, 388ac5656d8SAaron Goidel FSNOTIFY_OBJ_TYPE_INODE); 38963c882a0SEric Paris if (error) 39063c882a0SEric Paris path_put(path); 391ac5656d8SAaron Goidel 39263c882a0SEric Paris return error; 39363c882a0SEric Paris } 39463c882a0SEric Paris 395b7ba8371SEric Paris static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock, 396000285deSEric Paris struct inotify_inode_mark *i_mark) 397b7ba8371SEric Paris { 398b7ba8371SEric Paris int ret; 399b7ba8371SEric Paris 4004542da63STejun Heo idr_preload(GFP_KERNEL); 401b7ba8371SEric Paris spin_lock(idr_lock); 4024542da63STejun Heo 403a66c04b4SJeff Layton ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT); 4044542da63STejun Heo if (ret >= 0) { 405b7ba8371SEric Paris /* we added the mark to the idr, take a reference */ 4064542da63STejun Heo i_mark->wd = ret; 407000285deSEric Paris fsnotify_get_mark(&i_mark->fsn_mark); 4087050c488SEric Paris } 409b7ba8371SEric Paris 4104542da63STejun Heo spin_unlock(idr_lock); 4114542da63STejun Heo idr_preload_end(); 4124542da63STejun Heo return ret < 0 ? ret : 0; 413b7ba8371SEric Paris } 414b7ba8371SEric Paris 415000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group, 416b7ba8371SEric Paris int wd) 417b7ba8371SEric Paris { 418b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 419b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 420000285deSEric Paris struct inotify_inode_mark *i_mark; 421b7ba8371SEric Paris 422b7ba8371SEric Paris assert_spin_locked(idr_lock); 423b7ba8371SEric Paris 424000285deSEric Paris i_mark = idr_find(idr, wd); 425000285deSEric Paris if (i_mark) { 426000285deSEric Paris struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark; 427b7ba8371SEric Paris 428000285deSEric Paris fsnotify_get_mark(fsn_mark); 429b7ba8371SEric Paris /* One ref for being in the idr, one ref we just took */ 430ab97f873SElena Reshetova BUG_ON(refcount_read(&fsn_mark->refcnt) < 2); 431b7ba8371SEric Paris } 432b7ba8371SEric Paris 433000285deSEric Paris return i_mark; 434b7ba8371SEric Paris } 435b7ba8371SEric Paris 436000285deSEric Paris static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group, 437b7ba8371SEric Paris int wd) 438b7ba8371SEric Paris { 439000285deSEric Paris struct inotify_inode_mark *i_mark; 440b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 441b7ba8371SEric Paris 442b7ba8371SEric Paris spin_lock(idr_lock); 443000285deSEric Paris i_mark = inotify_idr_find_locked(group, wd); 444b7ba8371SEric Paris spin_unlock(idr_lock); 445b7ba8371SEric Paris 446000285deSEric Paris return i_mark; 447b7ba8371SEric Paris } 448b7ba8371SEric Paris 449dead537dSEric Paris /* 450dead537dSEric Paris * Remove the mark from the idr (if present) and drop the reference 451dead537dSEric Paris * on the mark because it was in the idr. 452dead537dSEric Paris */ 4537e790dd5SEric Paris static void inotify_remove_from_idr(struct fsnotify_group *group, 454000285deSEric Paris struct inotify_inode_mark *i_mark) 4557e790dd5SEric Paris { 456e7253760SJan Kara struct idr *idr = &group->inotify_data.idr; 457b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 458000285deSEric Paris struct inotify_inode_mark *found_i_mark = NULL; 459dead537dSEric Paris int wd; 4607e790dd5SEric Paris 461b7ba8371SEric Paris spin_lock(idr_lock); 462000285deSEric Paris wd = i_mark->wd; 463dead537dSEric Paris 464b7ba8371SEric Paris /* 465000285deSEric Paris * does this i_mark think it is in the idr? we shouldn't get called 466b7ba8371SEric Paris * if it wasn't.... 467b7ba8371SEric Paris */ 468b7ba8371SEric Paris if (wd == -1) { 46925c829afSJan Kara WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", 47025c829afSJan Kara __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); 471dead537dSEric Paris goto out; 4727e790dd5SEric Paris } 473dead537dSEric Paris 474b7ba8371SEric Paris /* Lets look in the idr to see if we find it */ 475000285deSEric Paris found_i_mark = inotify_idr_find_locked(group, wd); 476000285deSEric Paris if (unlikely(!found_i_mark)) { 47725c829afSJan Kara WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", 47825c829afSJan Kara __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); 479b7ba8371SEric Paris goto out; 480b7ba8371SEric Paris } 481dead537dSEric Paris 482b7ba8371SEric Paris /* 483000285deSEric Paris * We found an mark in the idr at the right wd, but it's 484000285deSEric Paris * not the mark we were told to remove. eparis seriously 485b7ba8371SEric Paris * fucked up somewhere. 486b7ba8371SEric Paris */ 487000285deSEric Paris if (unlikely(found_i_mark != i_mark)) { 488000285deSEric Paris WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p " 48925c829afSJan Kara "found_i_mark=%p found_i_mark->wd=%d " 49025c829afSJan Kara "found_i_mark->group=%p\n", __func__, i_mark, 49125c829afSJan Kara i_mark->wd, i_mark->fsn_mark.group, found_i_mark, 49225c829afSJan Kara found_i_mark->wd, found_i_mark->fsn_mark.group); 493b7ba8371SEric Paris goto out; 494b7ba8371SEric Paris } 495dead537dSEric Paris 496b7ba8371SEric Paris /* 497b7ba8371SEric Paris * One ref for being in the idr 498b7ba8371SEric Paris * one ref grabbed by inotify_idr_find 499b7ba8371SEric Paris */ 500ab97f873SElena Reshetova if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) { 50125c829afSJan Kara printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", 50225c829afSJan Kara __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); 503b7ba8371SEric Paris /* we can't really recover with bad ref cnting.. */ 504b7ba8371SEric Paris BUG(); 505b7ba8371SEric Paris } 506b7ba8371SEric Paris 507e7253760SJan Kara idr_remove(idr, wd); 508e7253760SJan Kara /* Removed from the idr, drop that ref. */ 509e7253760SJan Kara fsnotify_put_mark(&i_mark->fsn_mark); 510dead537dSEric Paris out: 511e7253760SJan Kara i_mark->wd = -1; 512e7253760SJan Kara spin_unlock(idr_lock); 513b7ba8371SEric Paris /* match the ref taken by inotify_idr_find_locked() */ 514000285deSEric Paris if (found_i_mark) 515000285deSEric Paris fsnotify_put_mark(&found_i_mark->fsn_mark); 516dead537dSEric Paris } 517dead537dSEric Paris 51863c882a0SEric Paris /* 519dead537dSEric Paris * Send IN_IGNORED for this wd, remove this wd from the idr. 52063c882a0SEric Paris */ 521000285deSEric Paris void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark, 522528da3e9SEric Paris struct fsnotify_group *group) 52363c882a0SEric Paris { 524000285deSEric Paris struct inotify_inode_mark *i_mark; 5257053aee2SJan Kara 5267053aee2SJan Kara /* Queue ignore event for the watch */ 5271a2620a9SAmir Goldstein inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL, 5281a2620a9SAmir Goldstein 0); 52963c882a0SEric Paris 5308b99c3ccSLino Sanfilippo i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 531000285deSEric Paris /* remove this mark from the idr */ 532000285deSEric Paris inotify_remove_from_idr(group, i_mark); 53363c882a0SEric Paris 5341cce1eeaSNikolay Borisov dec_inotify_watches(group->inotify_data.ucounts); 53563c882a0SEric Paris } 53663c882a0SEric Paris 53752cef755SEric Paris static int inotify_update_existing_watch(struct fsnotify_group *group, 53852cef755SEric Paris struct inode *inode, 53952cef755SEric Paris u32 arg) 54063c882a0SEric Paris { 541000285deSEric Paris struct fsnotify_mark *fsn_mark; 542000285deSEric Paris struct inotify_inode_mark *i_mark; 54363c882a0SEric Paris __u32 old_mask, new_mask; 54438035c04SAmir Goldstein int replace = !(arg & IN_MASK_ADD); 5454d97f7d5SHenry Wilson int create = (arg & IN_MASK_CREATE); 54652cef755SEric Paris int ret; 54763c882a0SEric Paris 548b1362edfSJan Kara fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group); 549000285deSEric Paris if (!fsn_mark) 55052cef755SEric Paris return -ENOENT; 55162c9d267SZhangXiaoxu else if (create) { 55262c9d267SZhangXiaoxu ret = -EEXIST; 55362c9d267SZhangXiaoxu goto out; 55462c9d267SZhangXiaoxu } 55552cef755SEric Paris 556000285deSEric Paris i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); 55775fe2b26SEric Paris 558000285deSEric Paris spin_lock(&fsn_mark->lock); 559000285deSEric Paris old_mask = fsn_mark->mask; 56038035c04SAmir Goldstein if (replace) { 56138035c04SAmir Goldstein fsn_mark->mask = 0; 56238035c04SAmir Goldstein fsn_mark->flags &= ~INOTIFY_MARK_FLAGS; 56338035c04SAmir Goldstein } 56438035c04SAmir Goldstein fsn_mark->mask |= inotify_arg_to_mask(inode, arg); 56538035c04SAmir Goldstein fsn_mark->flags |= inotify_arg_to_flags(arg); 566000285deSEric Paris new_mask = fsn_mark->mask; 567000285deSEric Paris spin_unlock(&fsn_mark->lock); 56863c882a0SEric Paris 56963c882a0SEric Paris if (old_mask != new_mask) { 57063c882a0SEric Paris /* more bits in old than in new? */ 57163c882a0SEric Paris int dropped = (old_mask & ~new_mask); 572000285deSEric Paris /* more bits in this fsn_mark than the inode's mask? */ 57363c882a0SEric Paris int do_inode = (new_mask & ~inode->i_fsnotify_mask); 57463c882a0SEric Paris 575000285deSEric Paris /* update the inode with this new fsn_mark */ 57663c882a0SEric Paris if (dropped || do_inode) 5778920d273SJan Kara fsnotify_recalc_mask(inode->i_fsnotify_marks); 57863c882a0SEric Paris 57963c882a0SEric Paris } 58063c882a0SEric Paris 58152cef755SEric Paris /* return the wd */ 582000285deSEric Paris ret = i_mark->wd; 58352cef755SEric Paris 58462c9d267SZhangXiaoxu out: 585d0775441SEric Paris /* match the get from fsnotify_find_mark() */ 586000285deSEric Paris fsnotify_put_mark(fsn_mark); 58775fe2b26SEric Paris 58852cef755SEric Paris return ret; 58963c882a0SEric Paris } 5907e790dd5SEric Paris 59152cef755SEric Paris static int inotify_new_watch(struct fsnotify_group *group, 59252cef755SEric Paris struct inode *inode, 59352cef755SEric Paris u32 arg) 59452cef755SEric Paris { 595000285deSEric Paris struct inotify_inode_mark *tmp_i_mark; 59652cef755SEric Paris int ret; 597b7ba8371SEric Paris struct idr *idr = &group->inotify_data.idr; 598b7ba8371SEric Paris spinlock_t *idr_lock = &group->inotify_data.idr_lock; 59952cef755SEric Paris 600000285deSEric Paris tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); 601000285deSEric Paris if (unlikely(!tmp_i_mark)) 60252cef755SEric Paris return -ENOMEM; 60352cef755SEric Paris 604054c636eSJan Kara fsnotify_init_mark(&tmp_i_mark->fsn_mark, group); 60538035c04SAmir Goldstein tmp_i_mark->fsn_mark.mask = inotify_arg_to_mask(inode, arg); 60638035c04SAmir Goldstein tmp_i_mark->fsn_mark.flags = inotify_arg_to_flags(arg); 607000285deSEric Paris tmp_i_mark->wd = -1; 60852cef755SEric Paris 609a66c04b4SJeff Layton ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark); 610b7ba8371SEric Paris if (ret) 61152cef755SEric Paris goto out_err; 61252cef755SEric Paris 6131cce1eeaSNikolay Borisov /* increment the number of watches the user has */ 6141cce1eeaSNikolay Borisov if (!inc_inotify_watches(group->inotify_data.ucounts)) { 6151cce1eeaSNikolay Borisov inotify_remove_from_idr(group, tmp_i_mark); 6161cce1eeaSNikolay Borisov ret = -ENOSPC; 6171cce1eeaSNikolay Borisov goto out_err; 6181cce1eeaSNikolay Borisov } 6191cce1eeaSNikolay Borisov 62052cef755SEric Paris /* we are on the idr, now get on the inode */ 621b249f5beSAmir Goldstein ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0); 62252cef755SEric Paris if (ret) { 62352cef755SEric Paris /* we failed to get on the inode, get off the idr */ 624000285deSEric Paris inotify_remove_from_idr(group, tmp_i_mark); 62552cef755SEric Paris goto out_err; 62652cef755SEric Paris } 62752cef755SEric Paris 62852cef755SEric Paris 629000285deSEric Paris /* return the watch descriptor for this new mark */ 630000285deSEric Paris ret = tmp_i_mark->wd; 63152cef755SEric Paris 63252cef755SEric Paris out_err: 633000285deSEric Paris /* match the ref from fsnotify_init_mark() */ 634000285deSEric Paris fsnotify_put_mark(&tmp_i_mark->fsn_mark); 63552cef755SEric Paris 63652cef755SEric Paris return ret; 63752cef755SEric Paris } 63852cef755SEric Paris 63952cef755SEric Paris static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg) 64052cef755SEric Paris { 64152cef755SEric Paris int ret = 0; 64252cef755SEric Paris 643642054b8SAmir Goldstein fsnotify_group_lock(group); 64452cef755SEric Paris /* try to update and existing watch with the new arg */ 64552cef755SEric Paris ret = inotify_update_existing_watch(group, inode, arg); 64652cef755SEric Paris /* no mark present, try to add a new one */ 64752cef755SEric Paris if (ret == -ENOENT) 64852cef755SEric Paris ret = inotify_new_watch(group, inode, arg); 649642054b8SAmir Goldstein fsnotify_group_unlock(group); 65052cef755SEric Paris 65163c882a0SEric Paris return ret; 65263c882a0SEric Paris } 65363c882a0SEric Paris 654d0de4dc5SEric Paris static struct fsnotify_group *inotify_new_group(unsigned int max_events) 65563c882a0SEric Paris { 65663c882a0SEric Paris struct fsnotify_group *group; 657ff57cd58SJan Kara struct inotify_event_info *oevent; 65863c882a0SEric Paris 659867a448dSAmir Goldstein group = fsnotify_alloc_group(&inotify_fsnotify_ops, 660867a448dSAmir Goldstein FSNOTIFY_GROUP_USER); 66163c882a0SEric Paris if (IS_ERR(group)) 66263c882a0SEric Paris return group; 66363c882a0SEric Paris 664ac7b79fdSShakeel Butt oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL_ACCOUNT); 665ff57cd58SJan Kara if (unlikely(!oevent)) { 666ff57cd58SJan Kara fsnotify_destroy_group(group); 667ff57cd58SJan Kara return ERR_PTR(-ENOMEM); 668ff57cd58SJan Kara } 669ff57cd58SJan Kara group->overflow_event = &oevent->fse; 6708988f11aSAmir Goldstein fsnotify_init_event(group->overflow_event); 671a0a92d26SAmir Goldstein oevent->mask = FS_Q_OVERFLOW; 672ff57cd58SJan Kara oevent->wd = -1; 673ff57cd58SJan Kara oevent->sync_cookie = 0; 674ff57cd58SJan Kara oevent->name_len = 0; 675ff57cd58SJan Kara 67663c882a0SEric Paris group->max_events = max_events; 677d46eb14bSShakeel Butt group->memcg = get_mem_cgroup_from_mm(current->mm); 67863c882a0SEric Paris 67963c882a0SEric Paris spin_lock_init(&group->inotify_data.idr_lock); 68063c882a0SEric Paris idr_init(&group->inotify_data.idr); 6811cce1eeaSNikolay Borisov group->inotify_data.ucounts = inc_ucount(current_user_ns(), 6821cce1eeaSNikolay Borisov current_euid(), 6831cce1eeaSNikolay Borisov UCOUNT_INOTIFY_INSTANCES); 684d0de4dc5SEric Paris 6851cce1eeaSNikolay Borisov if (!group->inotify_data.ucounts) { 686d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 687d0de4dc5SEric Paris return ERR_PTR(-EMFILE); 688d0de4dc5SEric Paris } 68963c882a0SEric Paris 69063c882a0SEric Paris return group; 69163c882a0SEric Paris } 69263c882a0SEric Paris 69363c882a0SEric Paris 69463c882a0SEric Paris /* inotify syscalls */ 695d0d89d1eSDominik Brodowski static int do_inotify_init(int flags) 696272eb014SEric Paris { 69763c882a0SEric Paris struct fsnotify_group *group; 698c44dcc56SAl Viro int ret; 699272eb014SEric Paris 700272eb014SEric Paris /* Check the IN_* constants for consistency. */ 701272eb014SEric Paris BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC); 702272eb014SEric Paris BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK); 703272eb014SEric Paris 704272eb014SEric Paris if (flags & ~(IN_CLOEXEC | IN_NONBLOCK)) 705272eb014SEric Paris return -EINVAL; 706272eb014SEric Paris 70763c882a0SEric Paris /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */ 708d0de4dc5SEric Paris group = inotify_new_group(inotify_max_queued_events); 709d0de4dc5SEric Paris if (IS_ERR(group)) 710d0de4dc5SEric Paris return PTR_ERR(group); 711825f9692SAl Viro 712c44dcc56SAl Viro ret = anon_inode_getfd("inotify", &inotify_fops, group, 713c44dcc56SAl Viro O_RDONLY | flags); 714d0de4dc5SEric Paris if (ret < 0) 715d8153d4dSLino Sanfilippo fsnotify_destroy_group(group); 716d0de4dc5SEric Paris 717272eb014SEric Paris return ret; 718272eb014SEric Paris } 719272eb014SEric Paris 720d0d89d1eSDominik Brodowski SYSCALL_DEFINE1(inotify_init1, int, flags) 721d0d89d1eSDominik Brodowski { 722d0d89d1eSDominik Brodowski return do_inotify_init(flags); 723d0d89d1eSDominik Brodowski } 724d0d89d1eSDominik Brodowski 725938bb9f5SHeiko Carstens SYSCALL_DEFINE0(inotify_init) 726272eb014SEric Paris { 727d0d89d1eSDominik Brodowski return do_inotify_init(0); 728272eb014SEric Paris } 729272eb014SEric Paris 7302e4d0924SHeiko Carstens SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, 7312e4d0924SHeiko Carstens u32, mask) 732272eb014SEric Paris { 73363c882a0SEric Paris struct fsnotify_group *group; 734272eb014SEric Paris struct inode *inode; 735272eb014SEric Paris struct path path; 7362903ff01SAl Viro struct fd f; 7372903ff01SAl Viro int ret; 738272eb014SEric Paris unsigned flags = 0; 739272eb014SEric Paris 740d30e2c05SDave Hansen /* 741d30e2c05SDave Hansen * We share a lot of code with fs/dnotify. We also share 742d30e2c05SDave Hansen * the bit layout between inotify's IN_* and the fsnotify 743d30e2c05SDave Hansen * FS_*. This check ensures that only the inotify IN_* 744d30e2c05SDave Hansen * bits get passed in and set in watches/events. 745d30e2c05SDave Hansen */ 746d30e2c05SDave Hansen if (unlikely(mask & ~ALL_INOTIFY_BITS)) 747d30e2c05SDave Hansen return -EINVAL; 748d30e2c05SDave Hansen /* 749d30e2c05SDave Hansen * Require at least one valid bit set in the mask. 750d30e2c05SDave Hansen * Without _something_ set, we would have no events to 751d30e2c05SDave Hansen * watch for. 752d30e2c05SDave Hansen */ 75304df32faSZhao Hongjiang if (unlikely(!(mask & ALL_INOTIFY_BITS))) 75404df32faSZhao Hongjiang return -EINVAL; 75504df32faSZhao Hongjiang 7562903ff01SAl Viro f = fdget(fd); 7572903ff01SAl Viro if (unlikely(!f.file)) 758272eb014SEric Paris return -EBADF; 759272eb014SEric Paris 7604d97f7d5SHenry Wilson /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */ 761125892edSTetsuo Handa if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) { 762125892edSTetsuo Handa ret = -EINVAL; 763125892edSTetsuo Handa goto fput_and_out; 764125892edSTetsuo Handa } 7654d97f7d5SHenry Wilson 766272eb014SEric Paris /* verify that this is indeed an inotify instance */ 7672903ff01SAl Viro if (unlikely(f.file->f_op != &inotify_fops)) { 768272eb014SEric Paris ret = -EINVAL; 769272eb014SEric Paris goto fput_and_out; 770272eb014SEric Paris } 771272eb014SEric Paris 772272eb014SEric Paris if (!(mask & IN_DONT_FOLLOW)) 773272eb014SEric Paris flags |= LOOKUP_FOLLOW; 774272eb014SEric Paris if (mask & IN_ONLYDIR) 775272eb014SEric Paris flags |= LOOKUP_DIRECTORY; 776272eb014SEric Paris 777ac5656d8SAaron Goidel ret = inotify_find_inode(pathname, &path, flags, 778ac5656d8SAaron Goidel (mask & IN_ALL_EVENTS)); 77963c882a0SEric Paris if (ret) 780272eb014SEric Paris goto fput_and_out; 781272eb014SEric Paris 78263c882a0SEric Paris /* inode held in place by reference to path; group by fget on fd */ 783272eb014SEric Paris inode = path.dentry->d_inode; 7842903ff01SAl Viro group = f.file->private_data; 785272eb014SEric Paris 78663c882a0SEric Paris /* create/update an inode mark */ 78763c882a0SEric Paris ret = inotify_update_watch(group, inode, mask); 788272eb014SEric Paris path_put(&path); 789272eb014SEric Paris fput_and_out: 7902903ff01SAl Viro fdput(f); 791272eb014SEric Paris return ret; 792272eb014SEric Paris } 793272eb014SEric Paris 7942e4d0924SHeiko Carstens SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd) 795272eb014SEric Paris { 79663c882a0SEric Paris struct fsnotify_group *group; 797000285deSEric Paris struct inotify_inode_mark *i_mark; 7982903ff01SAl Viro struct fd f; 7997b26aa24Syoungjun int ret = -EINVAL; 800272eb014SEric Paris 8012903ff01SAl Viro f = fdget(fd); 8022903ff01SAl Viro if (unlikely(!f.file)) 803272eb014SEric Paris return -EBADF; 804272eb014SEric Paris 805272eb014SEric Paris /* verify that this is indeed an inotify instance */ 8062903ff01SAl Viro if (unlikely(f.file->f_op != &inotify_fops)) 807272eb014SEric Paris goto out; 808272eb014SEric Paris 8092903ff01SAl Viro group = f.file->private_data; 810272eb014SEric Paris 811000285deSEric Paris i_mark = inotify_idr_find(group, wd); 812000285deSEric Paris if (unlikely(!i_mark)) 81363c882a0SEric Paris goto out; 81463c882a0SEric Paris 815b7ba8371SEric Paris ret = 0; 816b7ba8371SEric Paris 817e2a29943SLino Sanfilippo fsnotify_destroy_mark(&i_mark->fsn_mark, group); 818b7ba8371SEric Paris 819b7ba8371SEric Paris /* match ref taken by inotify_idr_find */ 820000285deSEric Paris fsnotify_put_mark(&i_mark->fsn_mark); 821272eb014SEric Paris 822272eb014SEric Paris out: 8232903ff01SAl Viro fdput(f); 824272eb014SEric Paris return ret; 825272eb014SEric Paris } 826272eb014SEric Paris 827272eb014SEric Paris /* 828ae0e47f0SJustin P. Mattock * inotify_user_setup - Our initialization function. Note that we cannot return 829272eb014SEric Paris * error because we have compiled-in VFS hooks. So an (unlikely) failure here 830272eb014SEric Paris * must result in panic(). 831272eb014SEric Paris */ 832272eb014SEric Paris static int __init inotify_user_setup(void) 833272eb014SEric Paris { 83492890123SWaiman Long unsigned long watches_max; 83592890123SWaiman Long struct sysinfo si; 83692890123SWaiman Long 83792890123SWaiman Long si_meminfo(&si); 83892890123SWaiman Long /* 83992890123SWaiman Long * Allow up to 1% of addressable memory to be allocated for inotify 84092890123SWaiman Long * watches (per user) limited to the range [8192, 1048576]. 84192890123SWaiman Long */ 84292890123SWaiman Long watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) / 84392890123SWaiman Long INOTIFY_WATCH_COST; 84492890123SWaiman Long watches_max = clamp(watches_max, 8192UL, 1048576UL); 84592890123SWaiman Long 846f874e1acSEric Paris BUILD_BUG_ON(IN_ACCESS != FS_ACCESS); 847f874e1acSEric Paris BUILD_BUG_ON(IN_MODIFY != FS_MODIFY); 848f874e1acSEric Paris BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB); 849f874e1acSEric Paris BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE); 850f874e1acSEric Paris BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); 851f874e1acSEric Paris BUILD_BUG_ON(IN_OPEN != FS_OPEN); 852f874e1acSEric Paris BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM); 853f874e1acSEric Paris BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO); 854f874e1acSEric Paris BUILD_BUG_ON(IN_CREATE != FS_CREATE); 855f874e1acSEric Paris BUILD_BUG_ON(IN_DELETE != FS_DELETE); 856f874e1acSEric Paris BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF); 857f874e1acSEric Paris BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF); 858f874e1acSEric Paris BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT); 859f874e1acSEric Paris BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW); 860f874e1acSEric Paris BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED); 861b29866aaSEric Paris BUILD_BUG_ON(IN_ISDIR != FS_ISDIR); 862f874e1acSEric Paris 863a39f7ec4SAmir Goldstein BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22); 864f874e1acSEric Paris 865d46eb14bSShakeel Butt inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, 866d46eb14bSShakeel Butt SLAB_PANIC|SLAB_ACCOUNT); 86763c882a0SEric Paris 868272eb014SEric Paris inotify_max_queued_events = 16384; 8691cce1eeaSNikolay Borisov init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128; 87092890123SWaiman Long init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max; 8717b9ad122SXiaoming Ni inotify_sysctls_init(); 872272eb014SEric Paris 873272eb014SEric Paris return 0; 874272eb014SEric Paris } 875c013d5a4SPaul Gortmaker fs_initcall(inotify_user_setup); 876