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 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the |
| 13 | * Free Software Foundation; either version 2, or (at your option) any |
| 14 | * later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, but |
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/file.h> |
| 27 | #include <linux/mount.h> |
| 28 | #include <linux/namei.h> |
| 29 | #include <linux/poll.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/list.h> |
| 32 | #include <linux/inotify.h> |
| 33 | #include <linux/syscalls.h> |
Andrey Mirkin | fd5eea4 | 2007-10-16 23:30:13 -0700 | [diff] [blame] | 34 | #include <linux/magic.h> |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 35 | |
| 36 | #include <asm/ioctls.h> |
| 37 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 38 | static struct kmem_cache *watch_cachep __read_mostly; |
| 39 | static struct kmem_cache *event_cachep __read_mostly; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 40 | |
| 41 | static struct vfsmount *inotify_mnt __read_mostly; |
| 42 | |
| 43 | /* these are configurable via /proc/sys/fs/inotify/ */ |
Harvey Harrison | 3c828e4 | 2008-02-14 19:31:21 -0800 | [diff] [blame] | 44 | static int inotify_max_user_instances __read_mostly; |
| 45 | static int inotify_max_user_watches __read_mostly; |
| 46 | static int inotify_max_queued_events __read_mostly; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Lock ordering: |
| 50 | * |
| 51 | * inotify_dev->up_mutex (ensures we don't re-add the same watch) |
| 52 | * inode->inotify_mutex (protects inode's watch list) |
| 53 | * inotify_handle->mutex (protects inotify_handle's watch list) |
| 54 | * inotify_dev->ev_mutex (protects device's event queue) |
| 55 | */ |
| 56 | |
| 57 | /* |
| 58 | * Lifetimes of the main data structures: |
| 59 | * |
| 60 | * inotify_device: Lifetime is managed by reference count, from |
| 61 | * sys_inotify_init() until release. Additional references can bump the count |
| 62 | * via get_inotify_dev() and drop the count via put_inotify_dev(). |
| 63 | * |
| 64 | * inotify_user_watch: Lifetime is from create_watch() to the receipt of an |
| 65 | * IN_IGNORED event from inotify, or when using IN_ONESHOT, to receipt of the |
| 66 | * first event, or to inotify_destroy(). |
| 67 | */ |
| 68 | |
| 69 | /* |
| 70 | * struct inotify_device - represents an inotify instance |
| 71 | * |
| 72 | * This structure is protected by the mutex 'mutex'. |
| 73 | */ |
| 74 | struct inotify_device { |
| 75 | wait_queue_head_t wq; /* wait queue for i/o */ |
| 76 | struct mutex ev_mutex; /* protects event queue */ |
| 77 | struct mutex up_mutex; /* synchronizes watch updates */ |
| 78 | struct list_head events; /* list of queued events */ |
| 79 | atomic_t count; /* reference count */ |
| 80 | struct user_struct *user; /* user who opened this dev */ |
| 81 | struct inotify_handle *ih; /* inotify handle */ |
Dmitry Antipov | bcfbf84 | 2008-02-06 01:36:19 -0800 | [diff] [blame] | 82 | struct fasync_struct *fa; /* async notification */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 83 | unsigned int queue_size; /* size of the queue (bytes) */ |
| 84 | unsigned int event_count; /* number of pending events */ |
| 85 | unsigned int max_events; /* maximum number of events */ |
| 86 | }; |
| 87 | |
| 88 | /* |
| 89 | * struct inotify_kernel_event - An inotify event, originating from a watch and |
| 90 | * queued for user-space. A list of these is attached to each instance of the |
| 91 | * device. In read(), this list is walked and all events that can fit in the |
| 92 | * buffer are returned. |
| 93 | * |
| 94 | * Protected by dev->ev_mutex of the device in which we are queued. |
| 95 | */ |
| 96 | struct inotify_kernel_event { |
| 97 | struct inotify_event event; /* the user-space event */ |
| 98 | struct list_head list; /* entry in inotify_device's list */ |
| 99 | char *name; /* filename, if any */ |
| 100 | }; |
| 101 | |
| 102 | /* |
| 103 | * struct inotify_user_watch - our version of an inotify_watch, we add |
| 104 | * a reference to the associated inotify_device. |
| 105 | */ |
| 106 | struct inotify_user_watch { |
| 107 | struct inotify_device *dev; /* associated device */ |
| 108 | struct inotify_watch wdata; /* inotify watch data */ |
| 109 | }; |
| 110 | |
| 111 | #ifdef CONFIG_SYSCTL |
| 112 | |
| 113 | #include <linux/sysctl.h> |
| 114 | |
| 115 | static int zero; |
| 116 | |
| 117 | ctl_table inotify_table[] = { |
| 118 | { |
| 119 | .ctl_name = INOTIFY_MAX_USER_INSTANCES, |
| 120 | .procname = "max_user_instances", |
| 121 | .data = &inotify_max_user_instances, |
| 122 | .maxlen = sizeof(int), |
| 123 | .mode = 0644, |
| 124 | .proc_handler = &proc_dointvec_minmax, |
| 125 | .strategy = &sysctl_intvec, |
| 126 | .extra1 = &zero, |
| 127 | }, |
| 128 | { |
| 129 | .ctl_name = INOTIFY_MAX_USER_WATCHES, |
| 130 | .procname = "max_user_watches", |
| 131 | .data = &inotify_max_user_watches, |
| 132 | .maxlen = sizeof(int), |
| 133 | .mode = 0644, |
| 134 | .proc_handler = &proc_dointvec_minmax, |
| 135 | .strategy = &sysctl_intvec, |
| 136 | .extra1 = &zero, |
| 137 | }, |
| 138 | { |
| 139 | .ctl_name = INOTIFY_MAX_QUEUED_EVENTS, |
| 140 | .procname = "max_queued_events", |
| 141 | .data = &inotify_max_queued_events, |
| 142 | .maxlen = sizeof(int), |
| 143 | .mode = 0644, |
| 144 | .proc_handler = &proc_dointvec_minmax, |
| 145 | .strategy = &sysctl_intvec, |
| 146 | .extra1 = &zero |
| 147 | }, |
| 148 | { .ctl_name = 0 } |
| 149 | }; |
| 150 | #endif /* CONFIG_SYSCTL */ |
| 151 | |
| 152 | static inline void get_inotify_dev(struct inotify_device *dev) |
| 153 | { |
| 154 | atomic_inc(&dev->count); |
| 155 | } |
| 156 | |
| 157 | static inline void put_inotify_dev(struct inotify_device *dev) |
| 158 | { |
| 159 | if (atomic_dec_and_test(&dev->count)) { |
| 160 | atomic_dec(&dev->user->inotify_devs); |
| 161 | free_uid(dev->user); |
| 162 | kfree(dev); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * free_inotify_user_watch - cleans up the watch and its references |
| 168 | */ |
| 169 | static void free_inotify_user_watch(struct inotify_watch *w) |
| 170 | { |
| 171 | struct inotify_user_watch *watch; |
| 172 | struct inotify_device *dev; |
| 173 | |
| 174 | watch = container_of(w, struct inotify_user_watch, wdata); |
| 175 | dev = watch->dev; |
| 176 | |
| 177 | atomic_dec(&dev->user->inotify_watches); |
| 178 | put_inotify_dev(dev); |
| 179 | kmem_cache_free(watch_cachep, watch); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * kernel_event - create a new kernel event with the given parameters |
| 184 | * |
| 185 | * This function can sleep. |
| 186 | */ |
| 187 | static struct inotify_kernel_event * kernel_event(s32 wd, u32 mask, u32 cookie, |
| 188 | const char *name) |
| 189 | { |
| 190 | struct inotify_kernel_event *kevent; |
| 191 | |
Arjan van de Ven | 5b6509a | 2006-07-30 03:03:54 -0700 | [diff] [blame] | 192 | kevent = kmem_cache_alloc(event_cachep, GFP_NOFS); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 193 | if (unlikely(!kevent)) |
| 194 | return NULL; |
| 195 | |
| 196 | /* we hand this out to user-space, so zero it just in case */ |
| 197 | memset(&kevent->event, 0, sizeof(struct inotify_event)); |
| 198 | |
| 199 | kevent->event.wd = wd; |
| 200 | kevent->event.mask = mask; |
| 201 | kevent->event.cookie = cookie; |
| 202 | |
| 203 | INIT_LIST_HEAD(&kevent->list); |
| 204 | |
| 205 | if (name) { |
| 206 | size_t len, rem, event_size = sizeof(struct inotify_event); |
| 207 | |
| 208 | /* |
| 209 | * We need to pad the filename so as to properly align an |
| 210 | * array of inotify_event structures. Because the structure is |
| 211 | * small and the common case is a small filename, we just round |
| 212 | * up to the next multiple of the structure's sizeof. This is |
| 213 | * simple and safe for all architectures. |
| 214 | */ |
| 215 | len = strlen(name) + 1; |
| 216 | rem = event_size - len; |
| 217 | if (len > event_size) { |
| 218 | rem = event_size - (len % event_size); |
| 219 | if (len % event_size == 0) |
| 220 | rem = 0; |
| 221 | } |
| 222 | |
| 223 | kevent->name = kmalloc(len + rem, GFP_KERNEL); |
| 224 | if (unlikely(!kevent->name)) { |
| 225 | kmem_cache_free(event_cachep, kevent); |
| 226 | return NULL; |
| 227 | } |
| 228 | memcpy(kevent->name, name, len); |
| 229 | if (rem) |
| 230 | memset(kevent->name + len, 0, rem); |
| 231 | kevent->event.len = len + rem; |
| 232 | } else { |
| 233 | kevent->event.len = 0; |
| 234 | kevent->name = NULL; |
| 235 | } |
| 236 | |
| 237 | return kevent; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * inotify_dev_get_event - return the next event in the given dev's queue |
| 242 | * |
| 243 | * Caller must hold dev->ev_mutex. |
| 244 | */ |
| 245 | static inline struct inotify_kernel_event * |
| 246 | inotify_dev_get_event(struct inotify_device *dev) |
| 247 | { |
| 248 | return list_entry(dev->events.next, struct inotify_kernel_event, list); |
| 249 | } |
| 250 | |
| 251 | /* |
Yan Zheng | 1c17d18 | 2008-02-06 01:36:09 -0800 | [diff] [blame] | 252 | * inotify_dev_get_last_event - return the last event in the given dev's queue |
| 253 | * |
| 254 | * Caller must hold dev->ev_mutex. |
| 255 | */ |
| 256 | static inline struct inotify_kernel_event * |
| 257 | inotify_dev_get_last_event(struct inotify_device *dev) |
| 258 | { |
| 259 | if (list_empty(&dev->events)) |
| 260 | return NULL; |
| 261 | return list_entry(dev->events.prev, struct inotify_kernel_event, list); |
| 262 | } |
| 263 | |
| 264 | /* |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 265 | * inotify_dev_queue_event - event handler registered with core inotify, adds |
| 266 | * a new event to the given device |
| 267 | * |
| 268 | * Can sleep (calls kernel_event()). |
| 269 | */ |
| 270 | static void inotify_dev_queue_event(struct inotify_watch *w, u32 wd, u32 mask, |
Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 271 | u32 cookie, const char *name, |
| 272 | struct inode *ignored) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 273 | { |
| 274 | struct inotify_user_watch *watch; |
| 275 | struct inotify_device *dev; |
| 276 | struct inotify_kernel_event *kevent, *last; |
| 277 | |
| 278 | watch = container_of(w, struct inotify_user_watch, wdata); |
| 279 | dev = watch->dev; |
| 280 | |
| 281 | mutex_lock(&dev->ev_mutex); |
| 282 | |
| 283 | /* we can safely put the watch as we don't reference it while |
| 284 | * generating the event |
| 285 | */ |
Ulisses Furquim | ac74c00 | 2008-02-08 04:18:16 -0800 | [diff] [blame] | 286 | if (mask & IN_IGNORED || w->mask & IN_ONESHOT) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 287 | put_inotify_watch(w); /* final put */ |
| 288 | |
| 289 | /* coalescing: drop this event if it is a dupe of the previous */ |
Yan Zheng | 1c17d18 | 2008-02-06 01:36:09 -0800 | [diff] [blame] | 290 | last = inotify_dev_get_last_event(dev); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 291 | if (last && last->event.mask == mask && last->event.wd == wd && |
| 292 | last->event.cookie == cookie) { |
| 293 | const char *lastname = last->name; |
| 294 | |
| 295 | if (!name && !lastname) |
| 296 | goto out; |
| 297 | if (name && lastname && !strcmp(lastname, name)) |
| 298 | goto out; |
| 299 | } |
| 300 | |
| 301 | /* the queue overflowed and we already sent the Q_OVERFLOW event */ |
| 302 | if (unlikely(dev->event_count > dev->max_events)) |
| 303 | goto out; |
| 304 | |
| 305 | /* if the queue overflows, we need to notify user space */ |
| 306 | if (unlikely(dev->event_count == dev->max_events)) |
| 307 | kevent = kernel_event(-1, IN_Q_OVERFLOW, cookie, NULL); |
| 308 | else |
| 309 | kevent = kernel_event(wd, mask, cookie, name); |
| 310 | |
| 311 | if (unlikely(!kevent)) |
| 312 | goto out; |
| 313 | |
| 314 | /* queue the event and wake up anyone waiting */ |
| 315 | dev->event_count++; |
| 316 | dev->queue_size += sizeof(struct inotify_event) + kevent->event.len; |
| 317 | list_add_tail(&kevent->list, &dev->events); |
| 318 | wake_up_interruptible(&dev->wq); |
Dmitry Antipov | bcfbf84 | 2008-02-06 01:36:19 -0800 | [diff] [blame] | 319 | kill_fasync(&dev->fa, SIGIO, POLL_IN); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 320 | |
| 321 | out: |
| 322 | mutex_unlock(&dev->ev_mutex); |
| 323 | } |
| 324 | |
| 325 | /* |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 326 | * remove_kevent - cleans up the given kevent |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 327 | * |
| 328 | * Caller must hold dev->ev_mutex. |
| 329 | */ |
| 330 | static void remove_kevent(struct inotify_device *dev, |
| 331 | struct inotify_kernel_event *kevent) |
| 332 | { |
| 333 | list_del(&kevent->list); |
| 334 | |
| 335 | dev->event_count--; |
| 336 | dev->queue_size -= sizeof(struct inotify_event) + kevent->event.len; |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 337 | } |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 338 | |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 339 | /* |
| 340 | * free_kevent - frees the given kevent. |
| 341 | */ |
| 342 | static void free_kevent(struct inotify_kernel_event *kevent) |
| 343 | { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 344 | kfree(kevent->name); |
| 345 | kmem_cache_free(event_cachep, kevent); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * inotify_dev_event_dequeue - destroy an event on the given device |
| 350 | * |
| 351 | * Caller must hold dev->ev_mutex. |
| 352 | */ |
| 353 | static void inotify_dev_event_dequeue(struct inotify_device *dev) |
| 354 | { |
| 355 | if (!list_empty(&dev->events)) { |
| 356 | struct inotify_kernel_event *kevent; |
| 357 | kevent = inotify_dev_get_event(dev); |
| 358 | remove_kevent(dev, kevent); |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 359 | free_kevent(kevent); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | /* |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 364 | * find_inode - resolve a user-given path to a specific inode |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 365 | */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 366 | static int find_inode(const char __user *dirname, struct path *path, |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 367 | unsigned flags) |
| 368 | { |
| 369 | int error; |
| 370 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 371 | error = user_path_at(AT_FDCWD, dirname, flags, path); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 372 | if (error) |
| 373 | return error; |
| 374 | /* you can only watch an inode if you have read permissions on it */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 375 | error = inode_permission(path->dentry->d_inode, MAY_READ); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 376 | if (error) |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 377 | path_put(path); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 378 | return error; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * create_watch - creates a watch on the given device. |
| 383 | * |
| 384 | * Callers must hold dev->up_mutex. |
| 385 | */ |
| 386 | static int create_watch(struct inotify_device *dev, struct inode *inode, |
| 387 | u32 mask) |
| 388 | { |
| 389 | struct inotify_user_watch *watch; |
| 390 | int ret; |
| 391 | |
| 392 | if (atomic_read(&dev->user->inotify_watches) >= |
| 393 | inotify_max_user_watches) |
| 394 | return -ENOSPC; |
| 395 | |
| 396 | watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL); |
| 397 | if (unlikely(!watch)) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | /* save a reference to device and bump the count to make it official */ |
| 401 | get_inotify_dev(dev); |
| 402 | watch->dev = dev; |
| 403 | |
| 404 | atomic_inc(&dev->user->inotify_watches); |
| 405 | |
Amy Griffis | a9dc971 | 2006-06-01 13:11:03 -0700 | [diff] [blame] | 406 | inotify_init_watch(&watch->wdata); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 407 | ret = inotify_add_watch(dev->ih, &watch->wdata, inode, mask); |
| 408 | if (ret < 0) |
| 409 | free_inotify_user_watch(&watch->wdata); |
| 410 | |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | /* Device Interface */ |
| 415 | |
| 416 | static unsigned int inotify_poll(struct file *file, poll_table *wait) |
| 417 | { |
| 418 | struct inotify_device *dev = file->private_data; |
| 419 | int ret = 0; |
| 420 | |
| 421 | poll_wait(file, &dev->wq, wait); |
| 422 | mutex_lock(&dev->ev_mutex); |
| 423 | if (!list_empty(&dev->events)) |
| 424 | ret = POLLIN | POLLRDNORM; |
| 425 | mutex_unlock(&dev->ev_mutex); |
| 426 | |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | static ssize_t inotify_read(struct file *file, char __user *buf, |
| 431 | size_t count, loff_t *pos) |
| 432 | { |
| 433 | size_t event_size = sizeof (struct inotify_event); |
| 434 | struct inotify_device *dev; |
| 435 | char __user *start; |
| 436 | int ret; |
| 437 | DEFINE_WAIT(wait); |
| 438 | |
| 439 | start = buf; |
| 440 | dev = file->private_data; |
| 441 | |
| 442 | while (1) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 443 | |
| 444 | prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE); |
| 445 | |
| 446 | mutex_lock(&dev->ev_mutex); |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 447 | if (!list_empty(&dev->events)) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 448 | ret = 0; |
| 449 | break; |
| 450 | } |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 451 | mutex_unlock(&dev->ev_mutex); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 452 | |
| 453 | if (file->f_flags & O_NONBLOCK) { |
| 454 | ret = -EAGAIN; |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | if (signal_pending(current)) { |
| 459 | ret = -EINTR; |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | schedule(); |
| 464 | } |
| 465 | |
| 466 | finish_wait(&dev->wq, &wait); |
| 467 | if (ret) |
| 468 | return ret; |
| 469 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 470 | while (1) { |
| 471 | struct inotify_kernel_event *kevent; |
| 472 | |
| 473 | ret = buf - start; |
| 474 | if (list_empty(&dev->events)) |
| 475 | break; |
| 476 | |
| 477 | kevent = inotify_dev_get_event(dev); |
Nick Piggin | f9e4acf | 2007-02-12 00:51:59 -0800 | [diff] [blame] | 478 | if (event_size + kevent->event.len > count) { |
| 479 | if (ret == 0 && count > 0) { |
| 480 | /* |
| 481 | * could not get a single event because we |
| 482 | * didn't have enough buffer space. |
| 483 | */ |
| 484 | ret = -EINVAL; |
| 485 | } |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 486 | break; |
Nick Piggin | f9e4acf | 2007-02-12 00:51:59 -0800 | [diff] [blame] | 487 | } |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 488 | remove_kevent(dev, kevent); |
| 489 | |
| 490 | /* |
| 491 | * Must perform the copy_to_user outside the mutex in order |
| 492 | * to avoid a lock order reversal with mmap_sem. |
| 493 | */ |
| 494 | mutex_unlock(&dev->ev_mutex); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 495 | |
| 496 | if (copy_to_user(buf, &kevent->event, event_size)) { |
| 497 | ret = -EFAULT; |
| 498 | break; |
| 499 | } |
| 500 | buf += event_size; |
| 501 | count -= event_size; |
| 502 | |
| 503 | if (kevent->name) { |
| 504 | if (copy_to_user(buf, kevent->name, kevent->event.len)){ |
| 505 | ret = -EFAULT; |
| 506 | break; |
| 507 | } |
| 508 | buf += kevent->event.len; |
| 509 | count -= kevent->event.len; |
| 510 | } |
| 511 | |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 512 | free_kevent(kevent); |
| 513 | |
| 514 | mutex_lock(&dev->ev_mutex); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 515 | } |
| 516 | mutex_unlock(&dev->ev_mutex); |
| 517 | |
| 518 | return ret; |
| 519 | } |
| 520 | |
Dmitry Antipov | bcfbf84 | 2008-02-06 01:36:19 -0800 | [diff] [blame] | 521 | static int inotify_fasync(int fd, struct file *file, int on) |
| 522 | { |
| 523 | struct inotify_device *dev = file->private_data; |
| 524 | |
| 525 | return fasync_helper(fd, file, on, &dev->fa) >= 0 ? 0 : -EIO; |
| 526 | } |
| 527 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 528 | static int inotify_release(struct inode *ignored, struct file *file) |
| 529 | { |
| 530 | struct inotify_device *dev = file->private_data; |
| 531 | |
| 532 | inotify_destroy(dev->ih); |
| 533 | |
| 534 | /* destroy all of the events on this device */ |
| 535 | mutex_lock(&dev->ev_mutex); |
| 536 | while (!list_empty(&dev->events)) |
| 537 | inotify_dev_event_dequeue(dev); |
| 538 | mutex_unlock(&dev->ev_mutex); |
| 539 | |
Dmitry Antipov | bcfbf84 | 2008-02-06 01:36:19 -0800 | [diff] [blame] | 540 | if (file->f_flags & FASYNC) |
| 541 | inotify_fasync(-1, file, 0); |
| 542 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 543 | /* free this device: the put matching the get in inotify_init() */ |
| 544 | put_inotify_dev(dev); |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static long inotify_ioctl(struct file *file, unsigned int cmd, |
| 550 | unsigned long arg) |
| 551 | { |
| 552 | struct inotify_device *dev; |
| 553 | void __user *p; |
| 554 | int ret = -ENOTTY; |
| 555 | |
| 556 | dev = file->private_data; |
| 557 | p = (void __user *) arg; |
| 558 | |
| 559 | switch (cmd) { |
| 560 | case FIONREAD: |
| 561 | ret = put_user(dev->queue_size, (int __user *) p); |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | return ret; |
| 566 | } |
| 567 | |
| 568 | static const struct file_operations inotify_fops = { |
| 569 | .poll = inotify_poll, |
| 570 | .read = inotify_read, |
Dmitry Antipov | bcfbf84 | 2008-02-06 01:36:19 -0800 | [diff] [blame] | 571 | .fasync = inotify_fasync, |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 572 | .release = inotify_release, |
| 573 | .unlocked_ioctl = inotify_ioctl, |
| 574 | .compat_ioctl = inotify_ioctl, |
| 575 | }; |
| 576 | |
| 577 | static const struct inotify_operations inotify_user_ops = { |
| 578 | .handle_event = inotify_dev_queue_event, |
| 579 | .destroy_watch = free_inotify_user_watch, |
| 580 | }; |
| 581 | |
Ulrich Drepper | 4006553 | 2008-07-23 21:29:32 -0700 | [diff] [blame] | 582 | asmlinkage long sys_inotify_init1(int flags) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 583 | { |
| 584 | struct inotify_device *dev; |
| 585 | struct inotify_handle *ih; |
| 586 | struct user_struct *user; |
| 587 | struct file *filp; |
| 588 | int fd, ret; |
| 589 | |
Ulrich Drepper | e38b36f | 2008-07-23 21:29:42 -0700 | [diff] [blame] | 590 | /* Check the IN_* constants for consistency. */ |
| 591 | BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC); |
| 592 | BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK); |
| 593 | |
Ulrich Drepper | 510df2d | 2008-07-23 21:29:41 -0700 | [diff] [blame] | 594 | if (flags & ~(IN_CLOEXEC | IN_NONBLOCK)) |
Ulrich Drepper | 4006553 | 2008-07-23 21:29:32 -0700 | [diff] [blame] | 595 | return -EINVAL; |
| 596 | |
| 597 | fd = get_unused_fd_flags(flags & O_CLOEXEC); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 598 | if (fd < 0) |
| 599 | return fd; |
| 600 | |
| 601 | filp = get_empty_filp(); |
| 602 | if (!filp) { |
| 603 | ret = -ENFILE; |
| 604 | goto out_put_fd; |
| 605 | } |
| 606 | |
| 607 | user = get_uid(current->user); |
| 608 | if (unlikely(atomic_read(&user->inotify_devs) >= |
| 609 | inotify_max_user_instances)) { |
| 610 | ret = -EMFILE; |
| 611 | goto out_free_uid; |
| 612 | } |
| 613 | |
| 614 | dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL); |
| 615 | if (unlikely(!dev)) { |
| 616 | ret = -ENOMEM; |
| 617 | goto out_free_uid; |
| 618 | } |
| 619 | |
| 620 | ih = inotify_init(&inotify_user_ops); |
Hirofumi Nakagawa | 801678c | 2008-04-29 01:03:09 -0700 | [diff] [blame] | 621 | if (IS_ERR(ih)) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 622 | ret = PTR_ERR(ih); |
| 623 | goto out_free_dev; |
| 624 | } |
| 625 | dev->ih = ih; |
Dmitry Antipov | bcfbf84 | 2008-02-06 01:36:19 -0800 | [diff] [blame] | 626 | dev->fa = NULL; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 627 | |
| 628 | filp->f_op = &inotify_fops; |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 629 | filp->f_path.mnt = mntget(inotify_mnt); |
| 630 | filp->f_path.dentry = dget(inotify_mnt->mnt_root); |
| 631 | filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 632 | filp->f_mode = FMODE_READ; |
Ulrich Drepper | 510df2d | 2008-07-23 21:29:41 -0700 | [diff] [blame] | 633 | filp->f_flags = O_RDONLY | (flags & O_NONBLOCK); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 634 | filp->private_data = dev; |
| 635 | |
| 636 | INIT_LIST_HEAD(&dev->events); |
| 637 | init_waitqueue_head(&dev->wq); |
| 638 | mutex_init(&dev->ev_mutex); |
| 639 | mutex_init(&dev->up_mutex); |
| 640 | dev->event_count = 0; |
| 641 | dev->queue_size = 0; |
| 642 | dev->max_events = inotify_max_queued_events; |
| 643 | dev->user = user; |
| 644 | atomic_set(&dev->count, 0); |
| 645 | |
| 646 | get_inotify_dev(dev); |
| 647 | atomic_inc(&user->inotify_devs); |
| 648 | fd_install(fd, filp); |
| 649 | |
| 650 | return fd; |
| 651 | out_free_dev: |
| 652 | kfree(dev); |
| 653 | out_free_uid: |
| 654 | free_uid(user); |
| 655 | put_filp(filp); |
| 656 | out_put_fd: |
| 657 | put_unused_fd(fd); |
| 658 | return ret; |
| 659 | } |
| 660 | |
Ulrich Drepper | 4006553 | 2008-07-23 21:29:32 -0700 | [diff] [blame] | 661 | asmlinkage long sys_inotify_init(void) |
| 662 | { |
| 663 | return sys_inotify_init1(0); |
| 664 | } |
| 665 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 666 | asmlinkage long sys_inotify_add_watch(int fd, const char __user *pathname, u32 mask) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 667 | { |
| 668 | struct inode *inode; |
| 669 | struct inotify_device *dev; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 670 | struct path path; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 671 | struct file *filp; |
| 672 | int ret, fput_needed; |
| 673 | unsigned flags = 0; |
| 674 | |
| 675 | filp = fget_light(fd, &fput_needed); |
| 676 | if (unlikely(!filp)) |
| 677 | return -EBADF; |
| 678 | |
| 679 | /* verify that this is indeed an inotify instance */ |
| 680 | if (unlikely(filp->f_op != &inotify_fops)) { |
| 681 | ret = -EINVAL; |
| 682 | goto fput_and_out; |
| 683 | } |
| 684 | |
| 685 | if (!(mask & IN_DONT_FOLLOW)) |
| 686 | flags |= LOOKUP_FOLLOW; |
| 687 | if (mask & IN_ONLYDIR) |
| 688 | flags |= LOOKUP_DIRECTORY; |
| 689 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 690 | ret = find_inode(pathname, &path, flags); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 691 | if (unlikely(ret)) |
| 692 | goto fput_and_out; |
| 693 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 694 | /* inode held in place by reference to path; dev by fget on fd */ |
| 695 | inode = path.dentry->d_inode; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 696 | dev = filp->private_data; |
| 697 | |
| 698 | mutex_lock(&dev->up_mutex); |
| 699 | ret = inotify_find_update_watch(dev->ih, inode, mask); |
| 700 | if (ret == -ENOENT) |
| 701 | ret = create_watch(dev, inode, mask); |
| 702 | mutex_unlock(&dev->up_mutex); |
| 703 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 704 | path_put(&path); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 705 | fput_and_out: |
| 706 | fput_light(filp, fput_needed); |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | asmlinkage long sys_inotify_rm_watch(int fd, u32 wd) |
| 711 | { |
| 712 | struct file *filp; |
| 713 | struct inotify_device *dev; |
| 714 | int ret, fput_needed; |
| 715 | |
| 716 | filp = fget_light(fd, &fput_needed); |
| 717 | if (unlikely(!filp)) |
| 718 | return -EBADF; |
| 719 | |
| 720 | /* verify that this is indeed an inotify instance */ |
| 721 | if (unlikely(filp->f_op != &inotify_fops)) { |
| 722 | ret = -EINVAL; |
| 723 | goto out; |
| 724 | } |
| 725 | |
| 726 | dev = filp->private_data; |
| 727 | |
| 728 | /* we free our watch data when we get IN_IGNORED */ |
| 729 | ret = inotify_rm_wd(dev->ih, wd); |
| 730 | |
| 731 | out: |
| 732 | fput_light(filp, fput_needed); |
| 733 | return ret; |
| 734 | } |
| 735 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 736 | static int |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 737 | inotify_get_sb(struct file_system_type *fs_type, int flags, |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 738 | const char *dev_name, void *data, struct vfsmount *mnt) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 739 | { |
Andrey Mirkin | fd5eea4 | 2007-10-16 23:30:13 -0700 | [diff] [blame] | 740 | return get_sb_pseudo(fs_type, "inotify", NULL, |
| 741 | INOTIFYFS_SUPER_MAGIC, mnt); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static struct file_system_type inotify_fs_type = { |
| 745 | .name = "inotifyfs", |
| 746 | .get_sb = inotify_get_sb, |
| 747 | .kill_sb = kill_anon_super, |
| 748 | }; |
| 749 | |
| 750 | /* |
| 751 | * inotify_user_setup - Our initialization function. Note that we cannnot return |
| 752 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 753 | * must result in panic(). |
| 754 | */ |
| 755 | static int __init inotify_user_setup(void) |
| 756 | { |
| 757 | int ret; |
| 758 | |
| 759 | ret = register_filesystem(&inotify_fs_type); |
| 760 | if (unlikely(ret)) |
| 761 | panic("inotify: register_filesystem returned %d!\n", ret); |
| 762 | |
| 763 | inotify_mnt = kern_mount(&inotify_fs_type); |
| 764 | if (IS_ERR(inotify_mnt)) |
| 765 | panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt)); |
| 766 | |
| 767 | inotify_max_queued_events = 16384; |
| 768 | inotify_max_user_instances = 128; |
| 769 | inotify_max_user_watches = 8192; |
| 770 | |
| 771 | watch_cachep = kmem_cache_create("inotify_watch_cache", |
| 772 | sizeof(struct inotify_user_watch), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 773 | 0, SLAB_PANIC, NULL); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 774 | event_cachep = kmem_cache_create("inotify_event_cache", |
| 775 | sizeof(struct inotify_kernel_event), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 776 | 0, SLAB_PANIC, NULL); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | module_init(inotify_user_setup); |