Thomas Gleixner | 3e0a4e8 | 2019-05-23 11:14:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 2 | /* |
| 3 | * fs/inotify_user.c - inotify support for userspace |
| 4 | * |
| 5 | * Authors: |
| 6 | * John McCutchan <ttb@tentacle.dhs.org> |
| 7 | * Robert Love <rml@novell.com> |
| 8 | * |
| 9 | * Copyright (C) 2005 John McCutchan |
| 10 | * Copyright 2006 Hewlett-Packard Development Company, L.P. |
| 11 | * |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 12 | * Copyright (C) 2009 Eric Paris <Red Hat Inc> |
| 13 | * inotify was largely rewriten to make use of the fsnotify infrastructure |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 16 | #include <linux/file.h> |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 17 | #include <linux/fs.h> /* struct inode */ |
| 18 | #include <linux/fsnotify_backend.h> |
| 19 | #include <linux/idr.h> |
Paul Gortmaker | c013d5a | 2015-05-01 20:08:20 -0400 | [diff] [blame] | 20 | #include <linux/init.h> /* fs_initcall */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 21 | #include <linux/inotify.h> |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 22 | #include <linux/kernel.h> /* roundup() */ |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 23 | #include <linux/namei.h> /* LOOKUP_FOLLOW */ |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 24 | #include <linux/sched/signal.h> |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 25 | #include <linux/slab.h> /* struct kmem_cache */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 26 | #include <linux/syscalls.h> |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 27 | #include <linux/types.h> |
Al Viro | c44dcc5 | 2010-02-11 02:24:46 -0500 | [diff] [blame] | 28 | #include <linux/anon_inodes.h> |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 29 | #include <linux/uaccess.h> |
| 30 | #include <linux/poll.h> |
| 31 | #include <linux/wait.h> |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 32 | #include <linux/memcontrol.h> |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 33 | #include <linux/security.h> |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 34 | |
| 35 | #include "inotify.h" |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 36 | #include "../fdinfo.h" |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 37 | |
| 38 | #include <asm/ioctls.h> |
| 39 | |
Nikolay Borisov | 1cce1ee | 2016-12-14 15:56:33 +0200 | [diff] [blame] | 40 | /* configurable via /proc/sys/fs/inotify/ */ |
Harvey Harrison | 3c828e4 | 2008-02-14 19:31:21 -0800 | [diff] [blame] | 41 | static int inotify_max_queued_events __read_mostly; |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 42 | |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 43 | struct kmem_cache *inotify_inode_mark_cachep __read_mostly; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 44 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 45 | #ifdef CONFIG_SYSCTL |
| 46 | |
| 47 | #include <linux/sysctl.h> |
| 48 | |
Joe Perches | 92f778d | 2014-06-06 14:38:04 -0700 | [diff] [blame] | 49 | struct ctl_table inotify_table[] = { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 50 | { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 51 | .procname = "max_user_instances", |
Nikolay Borisov | 1cce1ee | 2016-12-14 15:56:33 +0200 | [diff] [blame] | 52 | .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES], |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 53 | .maxlen = sizeof(int), |
| 54 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 55 | .proc_handler = proc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 56 | .extra1 = SYSCTL_ZERO, |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 57 | }, |
| 58 | { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 59 | .procname = "max_user_watches", |
Nikolay Borisov | 1cce1ee | 2016-12-14 15:56:33 +0200 | [diff] [blame] | 60 | .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES], |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 61 | .maxlen = sizeof(int), |
| 62 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 63 | .proc_handler = proc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 64 | .extra1 = SYSCTL_ZERO, |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 65 | }, |
| 66 | { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 67 | .procname = "max_queued_events", |
| 68 | .data = &inotify_max_queued_events, |
| 69 | .maxlen = sizeof(int), |
| 70 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 71 | .proc_handler = proc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 72 | .extra1 = SYSCTL_ZERO |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 73 | }, |
Eric W. Biederman | ab09203 | 2009-11-05 14:25:10 -0800 | [diff] [blame] | 74 | { } |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 75 | }; |
| 76 | #endif /* CONFIG_SYSCTL */ |
| 77 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 78 | static inline __u32 inotify_arg_to_mask(u32 arg) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 79 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 80 | __u32 mask; |
| 81 | |
Eric Paris | 611da04 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 82 | /* |
| 83 | * everything should accept their own ignored, cares about children, |
| 84 | * and should receive events when the inode is unmounted |
| 85 | */ |
| 86 | mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 87 | |
| 88 | /* mask off the flags used to open the fd */ |
Eric Paris | 8c1934c | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 89 | mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK)); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 90 | |
| 91 | return mask; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 94 | static inline u32 inotify_mask_to_arg(__u32 mask) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 95 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 96 | return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED | |
| 97 | IN_Q_OVERFLOW); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 100 | /* intofiy userspace file descriptor functions */ |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 101 | static __poll_t inotify_poll(struct file *file, poll_table *wait) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 102 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 103 | struct fsnotify_group *group = file->private_data; |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 104 | __poll_t ret = 0; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 105 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 106 | poll_wait(file, &group->notification_waitq, wait); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 107 | spin_lock(&group->notification_lock); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 108 | if (!fsnotify_notify_queue_is_empty(group)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 109 | ret = EPOLLIN | EPOLLRDNORM; |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 110 | spin_unlock(&group->notification_lock); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 115 | static int round_event_name_len(struct fsnotify_event *fsn_event) |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 116 | { |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 117 | struct inotify_event_info *event; |
| 118 | |
| 119 | event = INOTIFY_E(fsn_event); |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 120 | if (!event->name_len) |
| 121 | return 0; |
| 122 | return roundup(event->name_len + 1, sizeof(struct inotify_event)); |
| 123 | } |
| 124 | |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 125 | /* |
| 126 | * Get an inotify_kernel_event if one exists and is small |
| 127 | * enough to fit in "count". Return an error pointer if |
| 128 | * not large enough. |
| 129 | * |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 130 | * Called with the group->notification_lock held. |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 131 | */ |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 132 | static struct fsnotify_event *get_one_event(struct fsnotify_group *group, |
| 133 | size_t count) |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 134 | { |
| 135 | size_t event_size = sizeof(struct inotify_event); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 136 | struct fsnotify_event *event; |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 137 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 138 | if (fsnotify_notify_queue_is_empty(group)) |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 139 | return NULL; |
| 140 | |
Jan Kara | 8ba8fa91 | 2014-08-06 16:03:26 -0700 | [diff] [blame] | 141 | event = fsnotify_peek_first_event(group); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 142 | |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 143 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 144 | |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 145 | event_size += round_event_name_len(event); |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 146 | if (event_size > count) |
| 147 | return ERR_PTR(-EINVAL); |
| 148 | |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 149 | /* held the notification_lock the whole time, so this is the |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 150 | * same event we peeked above */ |
Jan Kara | 8ba8fa91 | 2014-08-06 16:03:26 -0700 | [diff] [blame] | 151 | fsnotify_remove_first_event(group); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 152 | |
| 153 | return event; |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Copy an event to user space, returning how much we copied. |
| 158 | * |
| 159 | * We already checked that the event size is smaller than the |
| 160 | * buffer we had in "get_one_event()" above. |
| 161 | */ |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 162 | static ssize_t copy_event_to_user(struct fsnotify_group *group, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 163 | struct fsnotify_event *fsn_event, |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 164 | char __user *buf) |
| 165 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 166 | struct inotify_event inotify_event; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 167 | struct inotify_event_info *event; |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 168 | size_t event_size = sizeof(struct inotify_event); |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 169 | size_t name_len; |
| 170 | size_t pad_name_len; |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 171 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 172 | pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event); |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 173 | |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 174 | event = INOTIFY_E(fsn_event); |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 175 | name_len = event->name_len; |
Brian Rogers | b962e73 | 2009-08-28 10:00:05 -0400 | [diff] [blame] | 176 | /* |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 177 | * round up name length so it is a multiple of event_size |
Eric W. Biederman | 0db501b | 2009-08-27 03:20:04 -0700 | [diff] [blame] | 178 | * plus an extra byte for the terminating '\0'. |
| 179 | */ |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 180 | pad_name_len = round_event_name_len(fsn_event); |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 181 | inotify_event.len = pad_name_len; |
Amir Goldstein | a0a92d2 | 2019-01-10 19:04:31 +0200 | [diff] [blame] | 182 | inotify_event.mask = inotify_mask_to_arg(event->mask); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 183 | inotify_event.wd = event->wd; |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 184 | inotify_event.cookie = event->sync_cookie; |
| 185 | |
| 186 | /* send the main event */ |
| 187 | if (copy_to_user(buf, &inotify_event, event_size)) |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 188 | return -EFAULT; |
| 189 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 190 | buf += event_size; |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 191 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 192 | /* |
| 193 | * fsnotify only stores the pathname, so here we have to send the pathname |
| 194 | * and then pad that pathname out to a multiple of sizeof(inotify_event) |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 195 | * with zeros. |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 196 | */ |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 197 | if (pad_name_len) { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 198 | /* copy the path name */ |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 199 | if (copy_to_user(buf, event->name, name_len)) |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 200 | return -EFAULT; |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 201 | buf += name_len; |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 202 | |
Eric W. Biederman | 0db501b | 2009-08-27 03:20:04 -0700 | [diff] [blame] | 203 | /* fill userspace with 0's */ |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 204 | if (clear_user(buf, pad_name_len - name_len)) |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 205 | return -EFAULT; |
Jan Kara | e9fe690 | 2014-01-21 15:48:13 -0800 | [diff] [blame] | 206 | event_size += pad_name_len; |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 207 | } |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 208 | |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 209 | return event_size; |
| 210 | } |
| 211 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 212 | static ssize_t inotify_read(struct file *file, char __user *buf, |
| 213 | size_t count, loff_t *pos) |
| 214 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 215 | struct fsnotify_group *group; |
| 216 | struct fsnotify_event *kevent; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 217 | char __user *start; |
| 218 | int ret; |
Peter Zijlstra | e23738a | 2014-09-24 10:18:50 +0200 | [diff] [blame] | 219 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 220 | |
| 221 | start = buf; |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 222 | group = file->private_data; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 223 | |
Peter Zijlstra | e23738a | 2014-09-24 10:18:50 +0200 | [diff] [blame] | 224 | add_wait_queue(&group->notification_waitq, &wait); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 225 | while (1) { |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 226 | spin_lock(&group->notification_lock); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 227 | kevent = get_one_event(group, count); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 228 | spin_unlock(&group->notification_lock); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 229 | |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 230 | pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent); |
| 231 | |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 232 | if (kevent) { |
| 233 | ret = PTR_ERR(kevent); |
| 234 | if (IS_ERR(kevent)) |
| 235 | break; |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 236 | ret = copy_event_to_user(group, kevent, buf); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 237 | fsnotify_destroy_event(group, kevent); |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 238 | if (ret < 0) |
| 239 | break; |
| 240 | buf += ret; |
| 241 | count -= ret; |
| 242 | continue; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 245 | ret = -EAGAIN; |
| 246 | if (file->f_flags & O_NONBLOCK) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 247 | break; |
Eric Paris | 1ca39ab | 2012-03-26 13:07:59 -0400 | [diff] [blame] | 248 | ret = -ERESTARTSYS; |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 249 | if (signal_pending(current)) |
| 250 | break; |
| 251 | |
| 252 | if (start != buf) |
| 253 | break; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 254 | |
Peter Zijlstra | e23738a | 2014-09-24 10:18:50 +0200 | [diff] [blame] | 255 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 256 | } |
Peter Zijlstra | e23738a | 2014-09-24 10:18:50 +0200 | [diff] [blame] | 257 | remove_wait_queue(&group->notification_waitq, &wait); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 258 | |
Vegard Nossum | 3632dee | 2009-01-22 15:29:45 +0100 | [diff] [blame] | 259 | if (start != buf && ret != -EFAULT) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 260 | ret = buf - start; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static int inotify_release(struct inode *ignored, struct file *file) |
| 265 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 266 | struct fsnotify_group *group = file->private_data; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 267 | |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 268 | pr_debug("%s: group=%p\n", __func__, group); |
| 269 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 270 | /* free this group, matching get was inotify_init->fsnotify_obtain_group */ |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 271 | fsnotify_destroy_group(group); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static long inotify_ioctl(struct file *file, unsigned int cmd, |
| 277 | unsigned long arg) |
| 278 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 279 | struct fsnotify_group *group; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 280 | struct fsnotify_event *fsn_event; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 281 | void __user *p; |
| 282 | int ret = -ENOTTY; |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 283 | size_t send_len = 0; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 284 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 285 | group = file->private_data; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 286 | p = (void __user *) arg; |
| 287 | |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 288 | pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd); |
| 289 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 290 | switch (cmd) { |
| 291 | case FIONREAD: |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 292 | spin_lock(&group->notification_lock); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 293 | list_for_each_entry(fsn_event, &group->notification_list, |
| 294 | list) { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 295 | send_len += sizeof(struct inotify_event); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 296 | send_len += round_event_name_len(fsn_event); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 297 | } |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 298 | spin_unlock(&group->notification_lock); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 299 | ret = put_user(send_len, (int __user *) p); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 300 | break; |
Kirill Tkhai | e1603b6 | 2018-02-09 18:04:54 +0300 | [diff] [blame] | 301 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 302 | case INOTIFY_IOC_SETNEXTWD: |
| 303 | ret = -EINVAL; |
| 304 | if (arg >= 1 && arg <= INT_MAX) { |
| 305 | struct inotify_group_private_data *data; |
| 306 | |
| 307 | data = &group->inotify_data; |
| 308 | spin_lock(&data->idr_lock); |
| 309 | idr_set_cursor(&data->idr, (unsigned int)arg); |
| 310 | spin_unlock(&data->idr_lock); |
| 311 | ret = 0; |
| 312 | } |
| 313 | break; |
| 314 | #endif /* CONFIG_CHECKPOINT_RESTORE */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static const struct file_operations inotify_fops = { |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 321 | .show_fdinfo = inotify_show_fdinfo, |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 322 | .poll = inotify_poll, |
| 323 | .read = inotify_read, |
Eric Paris | 0a6b6bd | 2011-10-14 17:43:39 -0400 | [diff] [blame] | 324 | .fasync = fsnotify_fasync, |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 325 | .release = inotify_release, |
| 326 | .unlocked_ioctl = inotify_ioctl, |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 327 | .compat_ioctl = inotify_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 328 | .llseek = noop_llseek, |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 329 | }; |
| 330 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 331 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 332 | /* |
| 333 | * find_inode - resolve a user-given path to a specific inode |
| 334 | */ |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 335 | static int inotify_find_inode(const char __user *dirname, struct path *path, |
| 336 | unsigned int flags, __u64 mask) |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 337 | { |
| 338 | int error; |
| 339 | |
| 340 | error = user_path_at(AT_FDCWD, dirname, flags, path); |
| 341 | if (error) |
| 342 | return error; |
| 343 | /* you can only watch an inode if you have read permissions on it */ |
| 344 | error = inode_permission(path->dentry->d_inode, MAY_READ); |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 345 | if (error) { |
| 346 | path_put(path); |
| 347 | return error; |
| 348 | } |
| 349 | error = security_path_notify(path, mask, |
| 350 | FSNOTIFY_OBJ_TYPE_INODE); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 351 | if (error) |
| 352 | path_put(path); |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 353 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 354 | return error; |
| 355 | } |
| 356 | |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 357 | static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock, |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 358 | struct inotify_inode_mark *i_mark) |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 359 | { |
| 360 | int ret; |
| 361 | |
Tejun Heo | 4542da6 | 2013-02-27 17:04:50 -0800 | [diff] [blame] | 362 | idr_preload(GFP_KERNEL); |
| 363 | spin_lock(idr_lock); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 364 | |
Jeff Layton | a66c04b | 2013-04-29 16:21:21 -0700 | [diff] [blame] | 365 | ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT); |
Tejun Heo | 4542da6 | 2013-02-27 17:04:50 -0800 | [diff] [blame] | 366 | if (ret >= 0) { |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 367 | /* we added the mark to the idr, take a reference */ |
Tejun Heo | 4542da6 | 2013-02-27 17:04:50 -0800 | [diff] [blame] | 368 | i_mark->wd = ret; |
Tejun Heo | 4542da6 | 2013-02-27 17:04:50 -0800 | [diff] [blame] | 369 | fsnotify_get_mark(&i_mark->fsn_mark); |
| 370 | } |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 371 | |
Tejun Heo | 4542da6 | 2013-02-27 17:04:50 -0800 | [diff] [blame] | 372 | spin_unlock(idr_lock); |
| 373 | idr_preload_end(); |
| 374 | return ret < 0 ? ret : 0; |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 375 | } |
| 376 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 377 | static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group, |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 378 | int wd) |
| 379 | { |
| 380 | struct idr *idr = &group->inotify_data.idr; |
| 381 | spinlock_t *idr_lock = &group->inotify_data.idr_lock; |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 382 | struct inotify_inode_mark *i_mark; |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 383 | |
| 384 | assert_spin_locked(idr_lock); |
| 385 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 386 | i_mark = idr_find(idr, wd); |
| 387 | if (i_mark) { |
| 388 | struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark; |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 389 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 390 | fsnotify_get_mark(fsn_mark); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 391 | /* One ref for being in the idr, one ref we just took */ |
Elena Reshetova | ab97f873 | 2017-10-20 13:26:02 +0300 | [diff] [blame] | 392 | BUG_ON(refcount_read(&fsn_mark->refcnt) < 2); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 393 | } |
| 394 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 395 | return i_mark; |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 398 | static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group, |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 399 | int wd) |
| 400 | { |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 401 | struct inotify_inode_mark *i_mark; |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 402 | spinlock_t *idr_lock = &group->inotify_data.idr_lock; |
| 403 | |
| 404 | spin_lock(idr_lock); |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 405 | i_mark = inotify_idr_find_locked(group, wd); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 406 | spin_unlock(idr_lock); |
| 407 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 408 | return i_mark; |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 409 | } |
| 410 | |
Eric Paris | dead537 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 411 | /* |
| 412 | * Remove the mark from the idr (if present) and drop the reference |
| 413 | * on the mark because it was in the idr. |
| 414 | */ |
Eric Paris | 7e790dd | 2009-07-07 10:28:24 -0400 | [diff] [blame] | 415 | static void inotify_remove_from_idr(struct fsnotify_group *group, |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 416 | struct inotify_inode_mark *i_mark) |
Eric Paris | 7e790dd | 2009-07-07 10:28:24 -0400 | [diff] [blame] | 417 | { |
Jan Kara | e725376 | 2016-12-21 11:50:39 +0100 | [diff] [blame] | 418 | struct idr *idr = &group->inotify_data.idr; |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 419 | spinlock_t *idr_lock = &group->inotify_data.idr_lock; |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 420 | struct inotify_inode_mark *found_i_mark = NULL; |
Eric Paris | dead537 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 421 | int wd; |
Eric Paris | 7e790dd | 2009-07-07 10:28:24 -0400 | [diff] [blame] | 422 | |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 423 | spin_lock(idr_lock); |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 424 | wd = i_mark->wd; |
Eric Paris | dead537 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 425 | |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 426 | /* |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 427 | * does this i_mark think it is in the idr? we shouldn't get called |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 428 | * if it wasn't.... |
| 429 | */ |
| 430 | if (wd == -1) { |
Jan Kara | 25c829a | 2016-12-09 09:38:55 +0100 | [diff] [blame] | 431 | WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", |
| 432 | __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); |
Eric Paris | dead537 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 433 | goto out; |
| 434 | } |
| 435 | |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 436 | /* Lets look in the idr to see if we find it */ |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 437 | found_i_mark = inotify_idr_find_locked(group, wd); |
| 438 | if (unlikely(!found_i_mark)) { |
Jan Kara | 25c829a | 2016-12-09 09:38:55 +0100 | [diff] [blame] | 439 | WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", |
| 440 | __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 441 | goto out; |
| 442 | } |
Eric Paris | dead537 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 443 | |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 444 | /* |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 445 | * We found an mark in the idr at the right wd, but it's |
| 446 | * not the mark we were told to remove. eparis seriously |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 447 | * fucked up somewhere. |
| 448 | */ |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 449 | if (unlikely(found_i_mark != i_mark)) { |
| 450 | WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p " |
Jan Kara | 25c829a | 2016-12-09 09:38:55 +0100 | [diff] [blame] | 451 | "found_i_mark=%p found_i_mark->wd=%d " |
| 452 | "found_i_mark->group=%p\n", __func__, i_mark, |
| 453 | i_mark->wd, i_mark->fsn_mark.group, found_i_mark, |
| 454 | found_i_mark->wd, found_i_mark->fsn_mark.group); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 455 | goto out; |
| 456 | } |
Eric Paris | dead537 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 457 | |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 458 | /* |
| 459 | * One ref for being in the idr |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 460 | * one ref grabbed by inotify_idr_find |
| 461 | */ |
Elena Reshetova | ab97f873 | 2017-10-20 13:26:02 +0300 | [diff] [blame] | 462 | if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) { |
Jan Kara | 25c829a | 2016-12-09 09:38:55 +0100 | [diff] [blame] | 463 | printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n", |
| 464 | __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 465 | /* we can't really recover with bad ref cnting.. */ |
| 466 | BUG(); |
| 467 | } |
| 468 | |
Jan Kara | e725376 | 2016-12-21 11:50:39 +0100 | [diff] [blame] | 469 | idr_remove(idr, wd); |
| 470 | /* Removed from the idr, drop that ref. */ |
| 471 | fsnotify_put_mark(&i_mark->fsn_mark); |
Eric Paris | dead537 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 472 | out: |
Jan Kara | e725376 | 2016-12-21 11:50:39 +0100 | [diff] [blame] | 473 | i_mark->wd = -1; |
| 474 | spin_unlock(idr_lock); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 475 | /* match the ref taken by inotify_idr_find_locked() */ |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 476 | if (found_i_mark) |
| 477 | fsnotify_put_mark(&found_i_mark->fsn_mark); |
Eric Paris | 7e790dd | 2009-07-07 10:28:24 -0400 | [diff] [blame] | 478 | } |
Eric Paris | dead537 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 479 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 480 | /* |
Eric Paris | dead537 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 481 | * Send IN_IGNORED for this wd, remove this wd from the idr. |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 482 | */ |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 483 | void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark, |
Eric Paris | 528da3e | 2009-06-12 16:04:26 -0400 | [diff] [blame] | 484 | struct fsnotify_group *group) |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 485 | { |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 486 | struct inotify_inode_mark *i_mark; |
Amir Goldstein | 47d9c7c | 2018-04-20 16:10:52 -0700 | [diff] [blame] | 487 | struct fsnotify_iter_info iter_info = { }; |
| 488 | |
| 489 | fsnotify_iter_set_report_type_mark(&iter_info, FSNOTIFY_OBJ_TYPE_INODE, |
| 490 | fsn_mark); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 491 | |
| 492 | /* Queue ignore event for the watch */ |
Amir Goldstein | b54cecf | 2020-06-07 12:10:40 +0300 | [diff] [blame^] | 493 | inotify_handle_event(group, FS_IN_IGNORED, NULL, FSNOTIFY_EVENT_NONE, |
| 494 | NULL, NULL, 0, &iter_info); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 495 | |
Lino Sanfilippo | 8b99c3c | 2012-03-24 23:44:19 +0100 | [diff] [blame] | 496 | i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 497 | /* remove this mark from the idr */ |
| 498 | inotify_remove_from_idr(group, i_mark); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 499 | |
Nikolay Borisov | 1cce1ee | 2016-12-14 15:56:33 +0200 | [diff] [blame] | 500 | dec_inotify_watches(group->inotify_data.ucounts); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 501 | } |
| 502 | |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 503 | static int inotify_update_existing_watch(struct fsnotify_group *group, |
| 504 | struct inode *inode, |
| 505 | u32 arg) |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 506 | { |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 507 | struct fsnotify_mark *fsn_mark; |
| 508 | struct inotify_inode_mark *i_mark; |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 509 | __u32 old_mask, new_mask; |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 510 | __u32 mask; |
| 511 | int add = (arg & IN_MASK_ADD); |
Henry Wilson | 4d97f7d | 2018-05-31 09:43:03 +0000 | [diff] [blame] | 512 | int create = (arg & IN_MASK_CREATE); |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 513 | int ret; |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 514 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 515 | mask = inotify_arg_to_mask(arg); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 516 | |
Jan Kara | b1362ed | 2016-12-21 16:28:45 +0100 | [diff] [blame] | 517 | fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group); |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 518 | if (!fsn_mark) |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 519 | return -ENOENT; |
ZhangXiaoxu | 62c9d26 | 2019-03-02 09:17:32 +0800 | [diff] [blame] | 520 | else if (create) { |
| 521 | ret = -EEXIST; |
| 522 | goto out; |
| 523 | } |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 524 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 525 | i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); |
Eric Paris | 75fe2b26 | 2009-07-07 10:28:23 -0400 | [diff] [blame] | 526 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 527 | spin_lock(&fsn_mark->lock); |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 528 | old_mask = fsn_mark->mask; |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 529 | if (add) |
Jan Kara | 66d2b81 | 2016-12-21 16:03:59 +0100 | [diff] [blame] | 530 | fsn_mark->mask |= mask; |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 531 | else |
Jan Kara | 66d2b81 | 2016-12-21 16:03:59 +0100 | [diff] [blame] | 532 | fsn_mark->mask = mask; |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 533 | new_mask = fsn_mark->mask; |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 534 | spin_unlock(&fsn_mark->lock); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 535 | |
| 536 | if (old_mask != new_mask) { |
| 537 | /* more bits in old than in new? */ |
| 538 | int dropped = (old_mask & ~new_mask); |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 539 | /* more bits in this fsn_mark than the inode's mask? */ |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 540 | int do_inode = (new_mask & ~inode->i_fsnotify_mask); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 541 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 542 | /* update the inode with this new fsn_mark */ |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 543 | if (dropped || do_inode) |
Jan Kara | 8920d27 | 2016-12-21 16:13:54 +0100 | [diff] [blame] | 544 | fsnotify_recalc_mask(inode->i_fsnotify_marks); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 545 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 548 | /* return the wd */ |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 549 | ret = i_mark->wd; |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 550 | |
ZhangXiaoxu | 62c9d26 | 2019-03-02 09:17:32 +0800 | [diff] [blame] | 551 | out: |
Eric Paris | d077544 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 552 | /* match the get from fsnotify_find_mark() */ |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 553 | fsnotify_put_mark(fsn_mark); |
Eric Paris | 75fe2b26 | 2009-07-07 10:28:23 -0400 | [diff] [blame] | 554 | |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | static int inotify_new_watch(struct fsnotify_group *group, |
| 559 | struct inode *inode, |
| 560 | u32 arg) |
| 561 | { |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 562 | struct inotify_inode_mark *tmp_i_mark; |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 563 | __u32 mask; |
| 564 | int ret; |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 565 | struct idr *idr = &group->inotify_data.idr; |
| 566 | spinlock_t *idr_lock = &group->inotify_data.idr_lock; |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 567 | |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 568 | mask = inotify_arg_to_mask(arg); |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 569 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 570 | tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); |
| 571 | if (unlikely(!tmp_i_mark)) |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 572 | return -ENOMEM; |
| 573 | |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 574 | fsnotify_init_mark(&tmp_i_mark->fsn_mark, group); |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 575 | tmp_i_mark->fsn_mark.mask = mask; |
| 576 | tmp_i_mark->wd = -1; |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 577 | |
Jeff Layton | a66c04b | 2013-04-29 16:21:21 -0700 | [diff] [blame] | 578 | ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 579 | if (ret) |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 580 | goto out_err; |
| 581 | |
Nikolay Borisov | 1cce1ee | 2016-12-14 15:56:33 +0200 | [diff] [blame] | 582 | /* increment the number of watches the user has */ |
| 583 | if (!inc_inotify_watches(group->inotify_data.ucounts)) { |
| 584 | inotify_remove_from_idr(group, tmp_i_mark); |
| 585 | ret = -ENOSPC; |
| 586 | goto out_err; |
| 587 | } |
| 588 | |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 589 | /* we are on the idr, now get on the inode */ |
Amir Goldstein | b249f5b | 2018-04-20 16:10:55 -0700 | [diff] [blame] | 590 | ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0); |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 591 | if (ret) { |
| 592 | /* we failed to get on the inode, get off the idr */ |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 593 | inotify_remove_from_idr(group, tmp_i_mark); |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 594 | goto out_err; |
| 595 | } |
| 596 | |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 597 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 598 | /* return the watch descriptor for this new mark */ |
| 599 | ret = tmp_i_mark->wd; |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 600 | |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 601 | out_err: |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 602 | /* match the ref from fsnotify_init_mark() */ |
| 603 | fsnotify_put_mark(&tmp_i_mark->fsn_mark); |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 604 | |
| 605 | return ret; |
| 606 | } |
| 607 | |
| 608 | static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg) |
| 609 | { |
| 610 | int ret = 0; |
| 611 | |
Lino Sanfilippo | e1e5a9f | 2013-07-08 15:59:45 -0700 | [diff] [blame] | 612 | mutex_lock(&group->mark_mutex); |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 613 | /* try to update and existing watch with the new arg */ |
| 614 | ret = inotify_update_existing_watch(group, inode, arg); |
| 615 | /* no mark present, try to add a new one */ |
| 616 | if (ret == -ENOENT) |
| 617 | ret = inotify_new_watch(group, inode, arg); |
Lino Sanfilippo | e1e5a9f | 2013-07-08 15:59:45 -0700 | [diff] [blame] | 618 | mutex_unlock(&group->mark_mutex); |
Eric Paris | 52cef75 | 2009-08-24 16:03:35 -0400 | [diff] [blame] | 619 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 620 | return ret; |
| 621 | } |
| 622 | |
Eric Paris | d0de4dc | 2011-04-05 17:20:50 -0400 | [diff] [blame] | 623 | static struct fsnotify_group *inotify_new_group(unsigned int max_events) |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 624 | { |
| 625 | struct fsnotify_group *group; |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 626 | struct inotify_event_info *oevent; |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 627 | |
Eric Paris | 0d2e2a1 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 628 | group = fsnotify_alloc_group(&inotify_fsnotify_ops); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 629 | if (IS_ERR(group)) |
| 630 | return group; |
| 631 | |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 632 | oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL); |
| 633 | if (unlikely(!oevent)) { |
| 634 | fsnotify_destroy_group(group); |
| 635 | return ERR_PTR(-ENOMEM); |
| 636 | } |
| 637 | group->overflow_event = &oevent->fse; |
Amir Goldstein | dfc2d25 | 2020-03-19 17:10:15 +0200 | [diff] [blame] | 638 | fsnotify_init_event(group->overflow_event, 0); |
Amir Goldstein | a0a92d2 | 2019-01-10 19:04:31 +0200 | [diff] [blame] | 639 | oevent->mask = FS_Q_OVERFLOW; |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 640 | oevent->wd = -1; |
| 641 | oevent->sync_cookie = 0; |
| 642 | oevent->name_len = 0; |
| 643 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 644 | group->max_events = max_events; |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 645 | group->memcg = get_mem_cgroup_from_mm(current->mm); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 646 | |
| 647 | spin_lock_init(&group->inotify_data.idr_lock); |
| 648 | idr_init(&group->inotify_data.idr); |
Nikolay Borisov | 1cce1ee | 2016-12-14 15:56:33 +0200 | [diff] [blame] | 649 | group->inotify_data.ucounts = inc_ucount(current_user_ns(), |
| 650 | current_euid(), |
| 651 | UCOUNT_INOTIFY_INSTANCES); |
Eric Paris | d0de4dc | 2011-04-05 17:20:50 -0400 | [diff] [blame] | 652 | |
Nikolay Borisov | 1cce1ee | 2016-12-14 15:56:33 +0200 | [diff] [blame] | 653 | if (!group->inotify_data.ucounts) { |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 654 | fsnotify_destroy_group(group); |
Eric Paris | d0de4dc | 2011-04-05 17:20:50 -0400 | [diff] [blame] | 655 | return ERR_PTR(-EMFILE); |
| 656 | } |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 657 | |
| 658 | return group; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /* inotify syscalls */ |
Dominik Brodowski | d0d89d1 | 2018-03-13 21:27:21 +0100 | [diff] [blame] | 663 | static int do_inotify_init(int flags) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 664 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 665 | struct fsnotify_group *group; |
Al Viro | c44dcc5 | 2010-02-11 02:24:46 -0500 | [diff] [blame] | 666 | int ret; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 667 | |
Ulrich Drepper | e38b36f | 2008-07-23 21:29:42 -0700 | [diff] [blame] | 668 | /* Check the IN_* constants for consistency. */ |
| 669 | BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC); |
| 670 | BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK); |
| 671 | |
Ulrich Drepper | 510df2d | 2008-07-23 21:29:41 -0700 | [diff] [blame] | 672 | if (flags & ~(IN_CLOEXEC | IN_NONBLOCK)) |
Ulrich Drepper | 4006553 | 2008-07-23 21:29:32 -0700 | [diff] [blame] | 673 | return -EINVAL; |
| 674 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 675 | /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */ |
Eric Paris | d0de4dc | 2011-04-05 17:20:50 -0400 | [diff] [blame] | 676 | group = inotify_new_group(inotify_max_queued_events); |
| 677 | if (IS_ERR(group)) |
| 678 | return PTR_ERR(group); |
Al Viro | 825f969 | 2009-08-05 18:35:21 +0400 | [diff] [blame] | 679 | |
Al Viro | c44dcc5 | 2010-02-11 02:24:46 -0500 | [diff] [blame] | 680 | ret = anon_inode_getfd("inotify", &inotify_fops, group, |
| 681 | O_RDONLY | flags); |
Eric Paris | d0de4dc | 2011-04-05 17:20:50 -0400 | [diff] [blame] | 682 | if (ret < 0) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 683 | fsnotify_destroy_group(group); |
Al Viro | 825f969 | 2009-08-05 18:35:21 +0400 | [diff] [blame] | 684 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 685 | return ret; |
| 686 | } |
| 687 | |
Dominik Brodowski | d0d89d1 | 2018-03-13 21:27:21 +0100 | [diff] [blame] | 688 | SYSCALL_DEFINE1(inotify_init1, int, flags) |
| 689 | { |
| 690 | return do_inotify_init(flags); |
| 691 | } |
| 692 | |
Heiko Carstens | 938bb9f | 2009-01-14 14:14:30 +0100 | [diff] [blame] | 693 | SYSCALL_DEFINE0(inotify_init) |
Ulrich Drepper | 4006553 | 2008-07-23 21:29:32 -0700 | [diff] [blame] | 694 | { |
Dominik Brodowski | d0d89d1 | 2018-03-13 21:27:21 +0100 | [diff] [blame] | 695 | return do_inotify_init(0); |
Ulrich Drepper | 4006553 | 2008-07-23 21:29:32 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 698 | SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, |
| 699 | u32, mask) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 700 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 701 | struct fsnotify_group *group; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 702 | struct inode *inode; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 703 | struct path path; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 704 | struct fd f; |
| 705 | int ret; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 706 | unsigned flags = 0; |
| 707 | |
Dave Hansen | d30e2c0 | 2015-11-05 18:43:46 -0800 | [diff] [blame] | 708 | /* |
| 709 | * We share a lot of code with fs/dnotify. We also share |
| 710 | * the bit layout between inotify's IN_* and the fsnotify |
| 711 | * FS_*. This check ensures that only the inotify IN_* |
| 712 | * bits get passed in and set in watches/events. |
| 713 | */ |
| 714 | if (unlikely(mask & ~ALL_INOTIFY_BITS)) |
| 715 | return -EINVAL; |
| 716 | /* |
| 717 | * Require at least one valid bit set in the mask. |
| 718 | * Without _something_ set, we would have no events to |
| 719 | * watch for. |
| 720 | */ |
Zhao Hongjiang | 04df32f | 2013-04-30 15:26:46 -0700 | [diff] [blame] | 721 | if (unlikely(!(mask & ALL_INOTIFY_BITS))) |
| 722 | return -EINVAL; |
| 723 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 724 | f = fdget(fd); |
| 725 | if (unlikely(!f.file)) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 726 | return -EBADF; |
| 727 | |
Henry Wilson | 4d97f7d | 2018-05-31 09:43:03 +0000 | [diff] [blame] | 728 | /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */ |
Tetsuo Handa | 125892e | 2019-01-01 18:54:26 +0900 | [diff] [blame] | 729 | if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) { |
| 730 | ret = -EINVAL; |
| 731 | goto fput_and_out; |
| 732 | } |
Henry Wilson | 4d97f7d | 2018-05-31 09:43:03 +0000 | [diff] [blame] | 733 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 734 | /* verify that this is indeed an inotify instance */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 735 | if (unlikely(f.file->f_op != &inotify_fops)) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 736 | ret = -EINVAL; |
| 737 | goto fput_and_out; |
| 738 | } |
| 739 | |
| 740 | if (!(mask & IN_DONT_FOLLOW)) |
| 741 | flags |= LOOKUP_FOLLOW; |
| 742 | if (mask & IN_ONLYDIR) |
| 743 | flags |= LOOKUP_DIRECTORY; |
| 744 | |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 745 | ret = inotify_find_inode(pathname, &path, flags, |
| 746 | (mask & IN_ALL_EVENTS)); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 747 | if (ret) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 748 | goto fput_and_out; |
| 749 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 750 | /* inode held in place by reference to path; group by fget on fd */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 751 | inode = path.dentry->d_inode; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 752 | group = f.file->private_data; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 753 | |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 754 | /* create/update an inode mark */ |
| 755 | ret = inotify_update_watch(group, inode, mask); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 756 | path_put(&path); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 757 | fput_and_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 758 | fdput(f); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 759 | return ret; |
| 760 | } |
| 761 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 762 | SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 763 | { |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 764 | struct fsnotify_group *group; |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 765 | struct inotify_inode_mark *i_mark; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 766 | struct fd f; |
youngjun | 7b26aa2 | 2020-04-26 07:33:16 -0700 | [diff] [blame] | 767 | int ret = -EINVAL; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 768 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 769 | f = fdget(fd); |
| 770 | if (unlikely(!f.file)) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 771 | return -EBADF; |
| 772 | |
| 773 | /* verify that this is indeed an inotify instance */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 774 | if (unlikely(f.file->f_op != &inotify_fops)) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 775 | goto out; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 776 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 777 | group = f.file->private_data; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 778 | |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 779 | i_mark = inotify_idr_find(group, wd); |
| 780 | if (unlikely(!i_mark)) |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 781 | goto out; |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 782 | |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 783 | ret = 0; |
| 784 | |
Lino Sanfilippo | e2a2994 | 2011-06-14 17:29:51 +0200 | [diff] [blame] | 785 | fsnotify_destroy_mark(&i_mark->fsn_mark, group); |
Eric Paris | b7ba837 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 786 | |
| 787 | /* match ref taken by inotify_idr_find */ |
Eric Paris | 000285d | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 788 | fsnotify_put_mark(&i_mark->fsn_mark); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 789 | |
| 790 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 791 | fdput(f); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 792 | return ret; |
| 793 | } |
| 794 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 795 | /* |
Justin P. Mattock | ae0e47f | 2011-03-01 15:06:02 +0100 | [diff] [blame] | 796 | * inotify_user_setup - Our initialization function. Note that we cannot return |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 797 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 798 | * must result in panic(). |
| 799 | */ |
| 800 | static int __init inotify_user_setup(void) |
| 801 | { |
Eric Paris | f874e1a | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 802 | BUILD_BUG_ON(IN_ACCESS != FS_ACCESS); |
| 803 | BUILD_BUG_ON(IN_MODIFY != FS_MODIFY); |
| 804 | BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB); |
| 805 | BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE); |
| 806 | BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); |
| 807 | BUILD_BUG_ON(IN_OPEN != FS_OPEN); |
| 808 | BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM); |
| 809 | BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO); |
| 810 | BUILD_BUG_ON(IN_CREATE != FS_CREATE); |
| 811 | BUILD_BUG_ON(IN_DELETE != FS_DELETE); |
| 812 | BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF); |
| 813 | BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF); |
| 814 | BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT); |
| 815 | BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW); |
| 816 | BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED); |
| 817 | BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK); |
Eric Paris | b29866a | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 818 | BUILD_BUG_ON(IN_ISDIR != FS_ISDIR); |
Eric Paris | f874e1a | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 819 | BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT); |
| 820 | |
Amir Goldstein | a39f7ec | 2018-10-04 00:25:36 +0300 | [diff] [blame] | 821 | BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22); |
Eric Paris | f874e1a | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 822 | |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 823 | inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, |
| 824 | SLAB_PANIC|SLAB_ACCOUNT); |
Eric Paris | 63c882a | 2009-05-21 17:02:01 -0400 | [diff] [blame] | 825 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 826 | inotify_max_queued_events = 16384; |
Nikolay Borisov | 1cce1ee | 2016-12-14 15:56:33 +0200 | [diff] [blame] | 827 | init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128; |
| 828 | init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 829 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 830 | return 0; |
| 831 | } |
Paul Gortmaker | c013d5a | 2015-05-01 20:08:20 -0400 | [diff] [blame] | 832 | fs_initcall(inotify_user_setup); |