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