diff options
Diffstat (limited to 'fs/inotify.c')
| -rw-r--r-- | fs/inotify.c | 1037 |
1 files changed, 1037 insertions, 0 deletions
diff --git a/fs/inotify.c b/fs/inotify.c new file mode 100644 index 000000000000..54757be888b6 --- /dev/null +++ b/fs/inotify.c | |||
| @@ -0,0 +1,1037 @@ | |||
| 1 | /* | ||
| 2 | * fs/inotify.c - inode-based file event notifications | ||
| 3 | * | ||
| 4 | * Authors: | ||
| 5 | * John McCutchan <ttb@tentacle.dhs.org> | ||
| 6 | * Robert Love <rml@novell.com> | ||
| 7 | * | ||
| 8 | * Copyright (C) 2005 John McCutchan | ||
| 9 | * | ||
| 10 | * This program is free software; you can redistribute it and/or modify it | ||
| 11 | * under the terms of the GNU General Public License as published by the | ||
| 12 | * Free Software Foundation; either version 2, or (at your option) any | ||
| 13 | * later version. | ||
| 14 | * | ||
| 15 | * This program is distributed in the hope that it will be useful, but | ||
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 18 | * General Public License for more details. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <linux/module.h> | ||
| 22 | #include <linux/kernel.h> | ||
| 23 | #include <linux/sched.h> | ||
| 24 | #include <linux/spinlock.h> | ||
| 25 | #include <linux/idr.h> | ||
| 26 | #include <linux/slab.h> | ||
| 27 | #include <linux/fs.h> | ||
| 28 | #include <linux/file.h> | ||
| 29 | #include <linux/mount.h> | ||
| 30 | #include <linux/namei.h> | ||
| 31 | #include <linux/poll.h> | ||
| 32 | #include <linux/init.h> | ||
| 33 | #include <linux/list.h> | ||
| 34 | #include <linux/writeback.h> | ||
| 35 | #include <linux/inotify.h> | ||
| 36 | |||
| 37 | #include <asm/ioctls.h> | ||
| 38 | |||
| 39 | static atomic_t inotify_cookie; | ||
| 40 | |||
| 41 | static kmem_cache_t *watch_cachep; | ||
| 42 | static kmem_cache_t *event_cachep; | ||
| 43 | |||
| 44 | static struct vfsmount *inotify_mnt; | ||
| 45 | |||
| 46 | /* these are configurable via /proc/sys/fs/inotify/ */ | ||
| 47 | int inotify_max_user_instances; | ||
| 48 | int inotify_max_user_watches; | ||
| 49 | int inotify_max_queued_events; | ||
| 50 | |||
| 51 | /* | ||
| 52 | * Lock ordering: | ||
| 53 | * | ||
| 54 | * dentry->d_lock (used to keep d_move() away from dentry->d_parent) | ||
| 55 | * iprune_sem (synchronize shrink_icache_memory()) | ||
| 56 | * inode_lock (protects the super_block->s_inodes list) | ||
| 57 | * inode->inotify_sem (protects inode->inotify_watches and watches->i_list) | ||
| 58 | * inotify_dev->sem (protects inotify_device and watches->d_list) | ||
| 59 | */ | ||
| 60 | |||
| 61 | /* | ||
| 62 | * Lifetimes of the three main data structures--inotify_device, inode, and | ||
| 63 | * inotify_watch--are managed by reference count. | ||
| 64 | * | ||
| 65 | * inotify_device: Lifetime is from open until release. Additional references | ||
| 66 | * can bump the count via get_inotify_dev() and drop the count via | ||
| 67 | * put_inotify_dev(). | ||
| 68 | * | ||
| 69 | * inotify_watch: Lifetime is from create_watch() to destory_watch(). | ||
| 70 | * Additional references can bump the count via get_inotify_watch() and drop | ||
| 71 | * the count via put_inotify_watch(). | ||
| 72 | * | ||
| 73 | * inode: Pinned so long as the inode is associated with a watch, from | ||
| 74 | * create_watch() to put_inotify_watch(). | ||
| 75 | */ | ||
| 76 | |||
| 77 | /* | ||
| 78 | * struct inotify_device - represents an open instance of an inotify device | ||
| 79 | * | ||
| 80 | * This structure is protected by the semaphore 'sem'. | ||
| 81 | */ | ||
| 82 | struct inotify_device { | ||
| 83 | wait_queue_head_t wq; /* wait queue for i/o */ | ||
| 84 | struct idr idr; /* idr mapping wd -> watch */ | ||
| 85 | struct semaphore sem; /* protects this bad boy */ | ||
| 86 | struct list_head events; /* list of queued events */ | ||
| 87 | struct list_head watches; /* list of watches */ | ||
| 88 | atomic_t count; /* reference count */ | ||
| 89 | struct user_struct *user; /* user who opened this dev */ | ||
| 90 | unsigned int queue_size; /* size of the queue (bytes) */ | ||
| 91 | unsigned int event_count; /* number of pending events */ | ||
| 92 | unsigned int max_events; /* maximum number of events */ | ||
| 93 | }; | ||
| 94 | |||
| 95 | /* | ||
| 96 | * struct inotify_kernel_event - An inotify event, originating from a watch and | ||
| 97 | * queued for user-space. A list of these is attached to each instance of the | ||
| 98 | * device. In read(), this list is walked and all events that can fit in the | ||
| 99 | * buffer are returned. | ||
| 100 | * | ||
| 101 | * Protected by dev->sem of the device in which we are queued. | ||
| 102 | */ | ||
| 103 | struct inotify_kernel_event { | ||
| 104 | struct inotify_event event; /* the user-space event */ | ||
| 105 | struct list_head list; /* entry in inotify_device's list */ | ||
| 106 | char *name; /* filename, if any */ | ||
| 107 | }; | ||
| 108 | |||
| 109 | /* | ||
| 110 | * struct inotify_watch - represents a watch request on a specific inode | ||
| 111 | * | ||
| 112 | * d_list is protected by dev->sem of the associated watch->dev. | ||
| 113 | * i_list and mask are protected by inode->inotify_sem of the associated inode. | ||
| 114 | * dev, inode, and wd are never written to once the watch is created. | ||
| 115 | */ | ||
| 116 | struct inotify_watch { | ||
| 117 | struct list_head d_list; /* entry in inotify_device's list */ | ||
| 118 | struct list_head i_list; /* entry in inode's list */ | ||
| 119 | atomic_t count; /* reference count */ | ||
| 120 | struct inotify_device *dev; /* associated device */ | ||
| 121 | struct inode *inode; /* associated inode */ | ||
| 122 | s32 wd; /* watch descriptor */ | ||
| 123 | u32 mask; /* event mask for this watch */ | ||
| 124 | }; | ||
| 125 | |||
| 126 | #ifdef CONFIG_SYSCTL | ||
| 127 | |||
| 128 | #include <linux/sysctl.h> | ||
| 129 | |||
| 130 | static int zero; | ||
| 131 | |||
| 132 | ctl_table inotify_table[] = { | ||
| 133 | { | ||
| 134 | .ctl_name = INOTIFY_MAX_USER_INSTANCES, | ||
| 135 | .procname = "max_user_instances", | ||
| 136 | .data = &inotify_max_user_instances, | ||
| 137 | .maxlen = sizeof(int), | ||
| 138 | .mode = 0644, | ||
| 139 | .proc_handler = &proc_dointvec_minmax, | ||
| 140 | .strategy = &sysctl_intvec, | ||
| 141 | .extra1 = &zero, | ||
| 142 | }, | ||
| 143 | { | ||
| 144 | .ctl_name = INOTIFY_MAX_USER_WATCHES, | ||
| 145 | .procname = "max_user_watches", | ||
| 146 | .data = &inotify_max_user_watches, | ||
| 147 | .maxlen = sizeof(int), | ||
| 148 | .mode = 0644, | ||
| 149 | .proc_handler = &proc_dointvec_minmax, | ||
| 150 | .strategy = &sysctl_intvec, | ||
| 151 | .extra1 = &zero, | ||
| 152 | }, | ||
| 153 | { | ||
| 154 | .ctl_name = INOTIFY_MAX_QUEUED_EVENTS, | ||
| 155 | .procname = "max_queued_events", | ||
| 156 | .data = &inotify_max_queued_events, | ||
| 157 | .maxlen = sizeof(int), | ||
| 158 | .mode = 0644, | ||
| 159 | .proc_handler = &proc_dointvec_minmax, | ||
| 160 | .strategy = &sysctl_intvec, | ||
| 161 | .extra1 = &zero | ||
| 162 | }, | ||
| 163 | { .ctl_name = 0 } | ||
| 164 | }; | ||
| 165 | #endif /* CONFIG_SYSCTL */ | ||
| 166 | |||
| 167 | static inline void get_inotify_dev(struct inotify_device *dev) | ||
| 168 | { | ||
| 169 | atomic_inc(&dev->count); | ||
| 170 | } | ||
| 171 | |||
| 172 | static inline void put_inotify_dev(struct inotify_device *dev) | ||
| 173 | { | ||
| 174 | if (atomic_dec_and_test(&dev->count)) { | ||
| 175 | atomic_dec(&dev->user->inotify_devs); | ||
| 176 | free_uid(dev->user); | ||
| 177 | kfree(dev); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | static inline void get_inotify_watch(struct inotify_watch *watch) | ||
| 182 | { | ||
| 183 | atomic_inc(&watch->count); | ||
| 184 | } | ||
| 185 | |||
| 186 | /* | ||
| 187 | * put_inotify_watch - decrements the ref count on a given watch. cleans up | ||
| 188 | * the watch and its references if the count reaches zero. | ||
| 189 | */ | ||
| 190 | static inline void put_inotify_watch(struct inotify_watch *watch) | ||
| 191 | { | ||
| 192 | if (atomic_dec_and_test(&watch->count)) { | ||
| 193 | put_inotify_dev(watch->dev); | ||
| 194 | iput(watch->inode); | ||
| 195 | kmem_cache_free(watch_cachep, watch); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | /* | ||
| 200 | * kernel_event - create a new kernel event with the given parameters | ||
| 201 | * | ||
| 202 | * This function can sleep. | ||
| 203 | */ | ||
| 204 | static struct inotify_kernel_event * kernel_event(s32 wd, u32 mask, u32 cookie, | ||
| 205 | const char *name) | ||
| 206 | { | ||
| 207 | struct inotify_kernel_event *kevent; | ||
| 208 | |||
| 209 | kevent = kmem_cache_alloc(event_cachep, GFP_KERNEL); | ||
| 210 | if (unlikely(!kevent)) | ||
| 211 | return NULL; | ||
| 212 | |||
| 213 | /* we hand this out to user-space, so zero it just in case */ | ||
| 214 | memset(&kevent->event, 0, sizeof(struct inotify_event)); | ||
| 215 | |||
| 216 | kevent->event.wd = wd; | ||
| 217 | kevent->event.mask = mask; | ||
| 218 | kevent->event.cookie = cookie; | ||
| 219 | |||
| 220 | INIT_LIST_HEAD(&kevent->list); | ||
| 221 | |||
| 222 | if (name) { | ||
| 223 | size_t len, rem, event_size = sizeof(struct inotify_event); | ||
| 224 | |||
| 225 | /* | ||
| 226 | * We need to pad the filename so as to properly align an | ||
| 227 | * array of inotify_event structures. Because the structure is | ||
| 228 | * small and the common case is a small filename, we just round | ||
| 229 | * up to the next multiple of the structure's sizeof. This is | ||
| 230 | * simple and safe for all architectures. | ||
| 231 | */ | ||
| 232 | len = strlen(name) + 1; | ||
| 233 | rem = event_size - len; | ||
| 234 | if (len > event_size) { | ||
| 235 | rem = event_size - (len % event_size); | ||
| 236 | if (len % event_size == 0) | ||
| 237 | rem = 0; | ||
| 238 | } | ||
| 239 | |||
| 240 | kevent->name = kmalloc(len + rem, GFP_KERNEL); | ||
| 241 | if (unlikely(!kevent->name)) { | ||
| 242 | kmem_cache_free(event_cachep, kevent); | ||
| 243 | return NULL; | ||
| 244 | } | ||
| 245 | memcpy(kevent->name, name, len); | ||
| 246 | if (rem) | ||
| 247 | memset(kevent->name + len, 0, rem); | ||
| 248 | kevent->event.len = len + rem; | ||
| 249 | } else { | ||
| 250 | kevent->event.len = 0; | ||
| 251 | kevent->name = NULL; | ||
| 252 | } | ||
| 253 | |||
| 254 | return kevent; | ||
| 255 | } | ||
| 256 | |||
| 257 | /* | ||
| 258 | * inotify_dev_get_event - return the next event in the given dev's queue | ||
| 259 | * | ||
| 260 | * Caller must hold dev->sem. | ||
| 261 | */ | ||
| 262 | static inline struct inotify_kernel_event * | ||
| 263 | inotify_dev_get_event(struct inotify_device *dev) | ||
| 264 | { | ||
| 265 | return list_entry(dev->events.next, struct inotify_kernel_event, list); | ||
| 266 | } | ||
| 267 | |||
| 268 | /* | ||
| 269 | * inotify_dev_queue_event - add a new event to the given device | ||
| 270 | * | ||
| 271 | * Caller must hold dev->sem. Can sleep (calls kernel_event()). | ||
| 272 | */ | ||
| 273 | static void inotify_dev_queue_event(struct inotify_device *dev, | ||
| 274 | struct inotify_watch *watch, u32 mask, | ||
| 275 | u32 cookie, const char *name) | ||
| 276 | { | ||
| 277 | struct inotify_kernel_event *kevent, *last; | ||
| 278 | |||
| 279 | /* coalescing: drop this event if it is a dupe of the previous */ | ||
| 280 | last = inotify_dev_get_event(dev); | ||
| 281 | if (last && last->event.mask == mask && last->event.wd == watch->wd && | ||
| 282 | last->event.cookie == cookie) { | ||
| 283 | const char *lastname = last->name; | ||
| 284 | |||
| 285 | if (!name && !lastname) | ||
| 286 | return; | ||
| 287 | if (name && lastname && !strcmp(lastname, name)) | ||
| 288 | return; | ||
| 289 | } | ||
| 290 | |||
| 291 | /* the queue overflowed and we already sent the Q_OVERFLOW event */ | ||
| 292 | if (unlikely(dev->event_count > dev->max_events)) | ||
| 293 | return; | ||
| 294 | |||
| 295 | /* if the queue overflows, we need to notify user space */ | ||
| 296 | if (unlikely(dev->event_count == dev->max_events)) | ||
| 297 | kevent = kernel_event(-1, IN_Q_OVERFLOW, cookie, NULL); | ||
| 298 | else | ||
| 299 | kevent = kernel_event(watch->wd, mask, cookie, name); | ||
| 300 | |||
| 301 | if (unlikely(!kevent)) | ||
| 302 | return; | ||
| 303 | |||
| 304 | /* queue the event and wake up anyone waiting */ | ||
| 305 | dev->event_count++; | ||
| 306 | dev->queue_size += sizeof(struct inotify_event) + kevent->event.len; | ||
| 307 | list_add_tail(&kevent->list, &dev->events); | ||
| 308 | wake_up_interruptible(&dev->wq); | ||
| 309 | } | ||
| 310 | |||
| 311 | /* | ||
| 312 | * remove_kevent - cleans up and ultimately frees the given kevent | ||
| 313 | * | ||
| 314 | * Caller must hold dev->sem. | ||
| 315 | */ | ||
| 316 | static void remove_kevent(struct inotify_device *dev, | ||
| 317 | struct inotify_kernel_event *kevent) | ||
| 318 | { | ||
| 319 | list_del(&kevent->list); | ||
| 320 | |||
| 321 | dev->event_count--; | ||
| 322 | dev->queue_size -= sizeof(struct inotify_event) + kevent->event.len; | ||
| 323 | |||
| 324 | kfree(kevent->name); | ||
| 325 | kmem_cache_free(event_cachep, kevent); | ||
| 326 | } | ||
| 327 | |||
| 328 | /* | ||
| 329 | * inotify_dev_event_dequeue - destroy an event on the given device | ||
| 330 | * | ||
| 331 | * Caller must hold dev->sem. | ||
| 332 | */ | ||
| 333 | static void inotify_dev_event_dequeue(struct inotify_device *dev) | ||
| 334 | { | ||
| 335 | if (!list_empty(&dev->events)) { | ||
| 336 | struct inotify_kernel_event *kevent; | ||
| 337 | kevent = inotify_dev_get_event(dev); | ||
| 338 | remove_kevent(dev, kevent); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | /* | ||
| 343 | * inotify_dev_get_wd - returns the next WD for use by the given dev | ||
| 344 | * | ||
| 345 | * Callers must hold dev->sem. This function can sleep. | ||
| 346 | */ | ||
| 347 | static int inotify_dev_get_wd(struct inotify_device *dev, | ||
| 348 | struct inotify_watch *watch) | ||
| 349 | { | ||
| 350 | int ret; | ||
| 351 | |||
| 352 | do { | ||
| 353 | if (unlikely(!idr_pre_get(&dev->idr, GFP_KERNEL))) | ||
| 354 | return -ENOSPC; | ||
| 355 | ret = idr_get_new(&dev->idr, watch, &watch->wd); | ||
| 356 | } while (ret == -EAGAIN); | ||
| 357 | |||
| 358 | return ret; | ||
| 359 | } | ||
| 360 | |||
| 361 | /* | ||
| 362 | * find_inode - resolve a user-given path to a specific inode and return a nd | ||
| 363 | */ | ||
| 364 | static int find_inode(const char __user *dirname, struct nameidata *nd) | ||
| 365 | { | ||
| 366 | int error; | ||
| 367 | |||
| 368 | error = __user_walk(dirname, LOOKUP_FOLLOW, nd); | ||
| 369 | if (error) | ||
| 370 | return error; | ||
| 371 | /* you can only watch an inode if you have read permissions on it */ | ||
| 372 | error = permission(nd->dentry->d_inode, MAY_READ, NULL); | ||
| 373 | if (error) | ||
| 374 | path_release (nd); | ||
| 375 | return error; | ||
| 376 | } | ||
| 377 | |||
| 378 | /* | ||
| 379 | * create_watch - creates a watch on the given device. | ||
| 380 | * | ||
| 381 | * Callers must hold dev->sem. Calls inotify_dev_get_wd() so may sleep. | ||
| 382 | * Both 'dev' and 'inode' (by way of nameidata) need to be pinned. | ||
| 383 | */ | ||
| 384 | static struct inotify_watch *create_watch(struct inotify_device *dev, | ||
| 385 | u32 mask, struct inode *inode) | ||
| 386 | { | ||
| 387 | struct inotify_watch *watch; | ||
| 388 | int ret; | ||
| 389 | |||
| 390 | if (atomic_read(&dev->user->inotify_watches) >= inotify_max_user_watches) | ||
| 391 | return ERR_PTR(-ENOSPC); | ||
| 392 | |||
| 393 | watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL); | ||
| 394 | if (unlikely(!watch)) | ||
| 395 | return ERR_PTR(-ENOMEM); | ||
| 396 | |||
| 397 | ret = inotify_dev_get_wd(dev, watch); | ||
| 398 | if (unlikely(ret)) { | ||
| 399 | kmem_cache_free(watch_cachep, watch); | ||
| 400 | return ERR_PTR(ret); | ||
| 401 | } | ||
| 402 | |||
| 403 | watch->mask = mask; | ||
| 404 | atomic_set(&watch->count, 0); | ||
| 405 | INIT_LIST_HEAD(&watch->d_list); | ||
| 406 | INIT_LIST_HEAD(&watch->i_list); | ||
| 407 | |||
| 408 | /* save a reference to device and bump the count to make it official */ | ||
| 409 | get_inotify_dev(dev); | ||
| 410 | watch->dev = dev; | ||
| 411 | |||
| 412 | /* | ||
| 413 | * Save a reference to the inode and bump the ref count to make it | ||
| 414 | * official. We hold a reference to nameidata, which makes this safe. | ||
| 415 | */ | ||
| 416 | watch->inode = igrab(inode); | ||
| 417 | |||
| 418 | /* bump our own count, corresponding to our entry in dev->watches */ | ||
| 419 | get_inotify_watch(watch); | ||
| 420 | |||
| 421 | atomic_inc(&dev->user->inotify_watches); | ||
| 422 | |||
| 423 | return watch; | ||
| 424 | } | ||
| 425 | |||
| 426 | /* | ||
| 427 | * inotify_find_dev - find the watch associated with the given inode and dev | ||
| 428 | * | ||
| 429 | * Callers must hold inode->inotify_sem. | ||
| 430 | */ | ||
| 431 | static struct inotify_watch *inode_find_dev(struct inode *inode, | ||
| 432 | struct inotify_device *dev) | ||
| 433 | { | ||
| 434 | struct inotify_watch *watch; | ||
| 435 | |||
| 436 | list_for_each_entry(watch, &inode->inotify_watches, i_list) { | ||
| 437 | if (watch->dev == dev) | ||
| 438 | return watch; | ||
| 439 | } | ||
| 440 | |||
| 441 | return NULL; | ||
| 442 | } | ||
| 443 | |||
| 444 | /* | ||
| 445 | * remove_watch_no_event - remove_watch() without the IN_IGNORED event. | ||
| 446 | */ | ||
| 447 | static void remove_watch_no_event(struct inotify_watch *watch, | ||
| 448 | struct inotify_device *dev) | ||
| 449 | { | ||
| 450 | list_del(&watch->i_list); | ||
| 451 | list_del(&watch->d_list); | ||
| 452 | |||
| 453 | atomic_dec(&dev->user->inotify_watches); | ||
| 454 | idr_remove(&dev->idr, watch->wd); | ||
| 455 | put_inotify_watch(watch); | ||
| 456 | } | ||
| 457 | |||
| 458 | /* | ||
| 459 | * remove_watch - Remove a watch from both the device and the inode. Sends | ||
| 460 | * the IN_IGNORED event to the given device signifying that the inode is no | ||
| 461 | * longer watched. | ||
| 462 | * | ||
| 463 | * Callers must hold both inode->inotify_sem and dev->sem. We drop a | ||
| 464 | * reference to the inode before returning. | ||
| 465 | * | ||
| 466 | * The inode is not iput() so as to remain atomic. If the inode needs to be | ||
| 467 | * iput(), the call returns one. Otherwise, it returns zero. | ||
| 468 | */ | ||
| 469 | static void remove_watch(struct inotify_watch *watch,struct inotify_device *dev) | ||
| 470 | { | ||
| 471 | inotify_dev_queue_event(dev, watch, IN_IGNORED, 0, NULL); | ||
| 472 | remove_watch_no_event(watch, dev); | ||
| 473 | } | ||
| 474 | |||
| 475 | /* | ||
| 476 | * inotify_inode_watched - returns nonzero if there are watches on this inode | ||
| 477 | * and zero otherwise. We call this lockless, we do not care if we race. | ||
| 478 | */ | ||
| 479 | static inline int inotify_inode_watched(struct inode *inode) | ||
| 480 | { | ||
| 481 | return !list_empty(&inode->inotify_watches); | ||
| 482 | } | ||
| 483 | |||
| 484 | /* Kernel API */ | ||
| 485 | |||
| 486 | /** | ||
| 487 | * inotify_inode_queue_event - queue an event to all watches on this inode | ||
| 488 | * @inode: inode event is originating from | ||
| 489 | * @mask: event mask describing this event | ||
| 490 | * @cookie: cookie for synchronization, or zero | ||
| 491 | * @name: filename, if any | ||
| 492 | */ | ||
| 493 | void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie, | ||
| 494 | const char *name) | ||
| 495 | { | ||
| 496 | struct inotify_watch *watch, *next; | ||
| 497 | |||
| 498 | if (!inotify_inode_watched(inode)) | ||
| 499 | return; | ||
| 500 | |||
| 501 | down(&inode->inotify_sem); | ||
| 502 | list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) { | ||
| 503 | u32 watch_mask = watch->mask; | ||
| 504 | if (watch_mask & mask) { | ||
| 505 | struct inotify_device *dev = watch->dev; | ||
| 506 | get_inotify_watch(watch); | ||
| 507 | down(&dev->sem); | ||
| 508 | inotify_dev_queue_event(dev, watch, mask, cookie, name); | ||
| 509 | if (watch_mask & IN_ONESHOT) | ||
| 510 | remove_watch_no_event(watch, dev); | ||
| 511 | up(&dev->sem); | ||
| 512 | put_inotify_watch(watch); | ||
| 513 | } | ||
| 514 | } | ||
| 515 | up(&inode->inotify_sem); | ||
| 516 | } | ||
| 517 | EXPORT_SYMBOL_GPL(inotify_inode_queue_event); | ||
| 518 | |||
| 519 | /** | ||
| 520 | * inotify_dentry_parent_queue_event - queue an event to a dentry's parent | ||
| 521 | * @dentry: the dentry in question, we queue against this dentry's parent | ||
| 522 | * @mask: event mask describing this event | ||
| 523 | * @cookie: cookie for synchronization, or zero | ||
| 524 | * @name: filename, if any | ||
| 525 | */ | ||
| 526 | void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask, | ||
| 527 | u32 cookie, const char *name) | ||
| 528 | { | ||
| 529 | struct dentry *parent; | ||
| 530 | struct inode *inode; | ||
| 531 | |||
| 532 | spin_lock(&dentry->d_lock); | ||
| 533 | parent = dentry->d_parent; | ||
| 534 | inode = parent->d_inode; | ||
| 535 | |||
| 536 | if (inotify_inode_watched(inode)) { | ||
| 537 | dget(parent); | ||
| 538 | spin_unlock(&dentry->d_lock); | ||
| 539 | inotify_inode_queue_event(inode, mask, cookie, name); | ||
| 540 | dput(parent); | ||
| 541 | } else | ||
| 542 | spin_unlock(&dentry->d_lock); | ||
| 543 | } | ||
| 544 | EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event); | ||
| 545 | |||
| 546 | /** | ||
| 547 | * inotify_get_cookie - return a unique cookie for use in synchronizing events. | ||
| 548 | */ | ||
| 549 | u32 inotify_get_cookie(void) | ||
| 550 | { | ||
| 551 | return atomic_inc_return(&inotify_cookie); | ||
| 552 | } | ||
| 553 | EXPORT_SYMBOL_GPL(inotify_get_cookie); | ||
| 554 | |||
| 555 | /** | ||
| 556 | * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes. | ||
| 557 | * @list: list of inodes being unmounted (sb->s_inodes) | ||
| 558 | * | ||
| 559 | * Called with inode_lock held, protecting the unmounting super block's list | ||
| 560 | * of inodes, and with iprune_sem held, keeping shrink_icache_memory() at bay. | ||
| 561 | * We temporarily drop inode_lock, however, and CAN block. | ||
| 562 | */ | ||
| 563 | void inotify_unmount_inodes(struct list_head *list) | ||
| 564 | { | ||
| 565 | struct inode *inode, *next_i, *need_iput = NULL; | ||
| 566 | |||
| 567 | list_for_each_entry_safe(inode, next_i, list, i_sb_list) { | ||
| 568 | struct inotify_watch *watch, *next_w; | ||
| 569 | struct inode *need_iput_tmp; | ||
| 570 | struct list_head *watches; | ||
| 571 | |||
| 572 | /* | ||
| 573 | * If i_count is zero, the inode cannot have any watches and | ||
| 574 | * doing an __iget/iput with MS_ACTIVE clear would actually | ||
| 575 | * evict all inodes with zero i_count from icache which is | ||
| 576 | * unnecessarily violent and may in fact be illegal to do. | ||
| 577 | */ | ||
| 578 | if (!atomic_read(&inode->i_count)) | ||
| 579 | continue; | ||
| 580 | |||
| 581 | /* | ||
| 582 | * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or | ||
| 583 | * I_WILL_FREE which is fine because by that point the inode | ||
| 584 | * cannot have any associated watches. | ||
| 585 | */ | ||
| 586 | if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE)) | ||
| 587 | continue; | ||
| 588 | |||
| 589 | need_iput_tmp = need_iput; | ||
| 590 | need_iput = NULL; | ||
| 591 | /* In case the remove_watch() drops a reference. */ | ||
| 592 | if (inode != need_iput_tmp) | ||
| 593 | __iget(inode); | ||
| 594 | else | ||
| 595 | need_iput_tmp = NULL; | ||
| 596 | /* In case the dropping of a reference would nuke next_i. */ | ||
| 597 | if ((&next_i->i_sb_list != list) && | ||
| 598 | atomic_read(&next_i->i_count) && | ||
| 599 | !(next_i->i_state & (I_CLEAR | I_FREEING | | ||
| 600 | I_WILL_FREE))) { | ||
| 601 | __iget(next_i); | ||
| 602 | need_iput = next_i; | ||
| 603 | } | ||
| 604 | |||
| 605 | /* | ||
| 606 | * We can safely drop inode_lock here because we hold | ||
| 607 | * references on both inode and next_i. Also no new inodes | ||
| 608 | * will be added since the umount has begun. Finally, | ||
| 609 | * iprune_sem keeps shrink_icache_memory() away. | ||
| 610 | */ | ||
| 611 | spin_unlock(&inode_lock); | ||
| 612 | |||
| 613 | if (need_iput_tmp) | ||
| 614 | iput(need_iput_tmp); | ||
| 615 | |||
| 616 | /* for each watch, send IN_UNMOUNT and then remove it */ | ||
| 617 | down(&inode->inotify_sem); | ||
| 618 | watches = &inode->inotify_watches; | ||
| 619 | list_for_each_entry_safe(watch, next_w, watches, i_list) { | ||
| 620 | struct inotify_device *dev = watch->dev; | ||
| 621 | down(&dev->sem); | ||
| 622 | inotify_dev_queue_event(dev, watch, IN_UNMOUNT,0,NULL); | ||
| 623 | remove_watch(watch, dev); | ||
| 624 | up(&dev->sem); | ||
| 625 | } | ||
| 626 | up(&inode->inotify_sem); | ||
| 627 | iput(inode); | ||
| 628 | |||
| 629 | spin_lock(&inode_lock); | ||
| 630 | } | ||
| 631 | } | ||
| 632 | EXPORT_SYMBOL_GPL(inotify_unmount_inodes); | ||
| 633 | |||
| 634 | /** | ||
| 635 | * inotify_inode_is_dead - an inode has been deleted, cleanup any watches | ||
| 636 | * @inode: inode that is about to be removed | ||
| 637 | */ | ||
| 638 | void inotify_inode_is_dead(struct inode *inode) | ||
| 639 | { | ||
| 640 | struct inotify_watch *watch, *next; | ||
| 641 | |||
| 642 | down(&inode->inotify_sem); | ||
| 643 | list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) { | ||
| 644 | struct inotify_device *dev = watch->dev; | ||
| 645 | down(&dev->sem); | ||
| 646 | remove_watch(watch, dev); | ||
| 647 | up(&dev->sem); | ||
| 648 | } | ||
| 649 | up(&inode->inotify_sem); | ||
| 650 | } | ||
| 651 | EXPORT_SYMBOL_GPL(inotify_inode_is_dead); | ||
| 652 | |||
| 653 | /* Device Interface */ | ||
| 654 | |||
| 655 | static unsigned int inotify_poll(struct file *file, poll_table *wait) | ||
| 656 | { | ||
| 657 | struct inotify_device *dev = file->private_data; | ||
| 658 | int ret = 0; | ||
| 659 | |||
| 660 | poll_wait(file, &dev->wq, wait); | ||
| 661 | down(&dev->sem); | ||
| 662 | if (!list_empty(&dev->events)) | ||
| 663 | ret = POLLIN | POLLRDNORM; | ||
| 664 | up(&dev->sem); | ||
| 665 | |||
| 666 | return ret; | ||
| 667 | } | ||
| 668 | |||
| 669 | static ssize_t inotify_read(struct file *file, char __user *buf, | ||
| 670 | size_t count, loff_t *pos) | ||
| 671 | { | ||
| 672 | size_t event_size = sizeof (struct inotify_event); | ||
| 673 | struct inotify_device *dev; | ||
| 674 | char __user *start; | ||
| 675 | int ret; | ||
| 676 | DEFINE_WAIT(wait); | ||
| 677 | |||
| 678 | start = buf; | ||
| 679 | dev = file->private_data; | ||
| 680 | |||
| 681 | while (1) { | ||
| 682 | int events; | ||
| 683 | |||
| 684 | prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE); | ||
| 685 | |||
| 686 | down(&dev->sem); | ||
| 687 | events = !list_empty(&dev->events); | ||
| 688 | up(&dev->sem); | ||
| 689 | if (events) { | ||
| 690 | ret = 0; | ||
| 691 | break; | ||
| 692 | } | ||
| 693 | |||
| 694 | if (file->f_flags & O_NONBLOCK) { | ||
| 695 | ret = -EAGAIN; | ||
| 696 | break; | ||
| 697 | } | ||
| 698 | |||
| 699 | if (signal_pending(current)) { | ||
| 700 | ret = -EINTR; | ||
| 701 | break; | ||
| 702 | } | ||
| 703 | |||
| 704 | schedule(); | ||
| 705 | } | ||
| 706 | |||
| 707 | finish_wait(&dev->wq, &wait); | ||
| 708 | if (ret) | ||
| 709 | return ret; | ||
| 710 | |||
| 711 | down(&dev->sem); | ||
| 712 | while (1) { | ||
| 713 | struct inotify_kernel_event *kevent; | ||
| 714 | |||
| 715 | ret = buf - start; | ||
| 716 | if (list_empty(&dev->events)) | ||
| 717 | break; | ||
| 718 | |||
| 719 | kevent = inotify_dev_get_event(dev); | ||
| 720 | if (event_size + kevent->event.len > count) | ||
| 721 | break; | ||
| 722 | |||
| 723 | if (copy_to_user(buf, &kevent->event, event_size)) { | ||
| 724 | ret = -EFAULT; | ||
| 725 | break; | ||
| 726 | } | ||
| 727 | buf += event_size; | ||
| 728 | count -= event_size; | ||
| 729 | |||
| 730 | if (kevent->name) { | ||
| 731 | if (copy_to_user(buf, kevent->name, kevent->event.len)){ | ||
| 732 | ret = -EFAULT; | ||
| 733 | break; | ||
| 734 | } | ||
| 735 | buf += kevent->event.len; | ||
| 736 | count -= kevent->event.len; | ||
| 737 | } | ||
| 738 | |||
| 739 | remove_kevent(dev, kevent); | ||
| 740 | } | ||
| 741 | up(&dev->sem); | ||
| 742 | |||
| 743 | return ret; | ||
| 744 | } | ||
| 745 | |||
| 746 | static int inotify_release(struct inode *ignored, struct file *file) | ||
| 747 | { | ||
| 748 | struct inotify_device *dev = file->private_data; | ||
| 749 | |||
| 750 | /* | ||
| 751 | * Destroy all of the watches on this device. Unfortunately, not very | ||
| 752 | * pretty. We cannot do a simple iteration over the list, because we | ||
| 753 | * do not know the inode until we iterate to the watch. But we need to | ||
| 754 | * hold inode->inotify_sem before dev->sem. The following works. | ||
| 755 | */ | ||
| 756 | while (1) { | ||
| 757 | struct inotify_watch *watch; | ||
| 758 | struct list_head *watches; | ||
| 759 | struct inode *inode; | ||
| 760 | |||
| 761 | down(&dev->sem); | ||
| 762 | watches = &dev->watches; | ||
| 763 | if (list_empty(watches)) { | ||
| 764 | up(&dev->sem); | ||
| 765 | break; | ||
| 766 | } | ||
| 767 | watch = list_entry(watches->next, struct inotify_watch, d_list); | ||
| 768 | get_inotify_watch(watch); | ||
| 769 | up(&dev->sem); | ||
| 770 | |||
| 771 | inode = watch->inode; | ||
| 772 | down(&inode->inotify_sem); | ||
| 773 | down(&dev->sem); | ||
| 774 | remove_watch_no_event(watch, dev); | ||
| 775 | up(&dev->sem); | ||
| 776 | up(&inode->inotify_sem); | ||
| 777 | put_inotify_watch(watch); | ||
| 778 | } | ||
| 779 | |||
| 780 | /* destroy all of the events on this device */ | ||
| 781 | down(&dev->sem); | ||
| 782 | while (!list_empty(&dev->events)) | ||
| 783 | inotify_dev_event_dequeue(dev); | ||
| 784 | up(&dev->sem); | ||
| 785 | |||
| 786 | /* free this device: the put matching the get in inotify_open() */ | ||
| 787 | put_inotify_dev(dev); | ||
| 788 | |||
| 789 | return 0; | ||
| 790 | } | ||
| 791 | |||
| 792 | /* | ||
| 793 | * inotify_ignore - handle the INOTIFY_IGNORE ioctl, asking that a given wd be | ||
| 794 | * removed from the device. | ||
| 795 | * | ||
| 796 | * Can sleep. | ||
| 797 | */ | ||
| 798 | static int inotify_ignore(struct inotify_device *dev, s32 wd) | ||
| 799 | { | ||
| 800 | struct inotify_watch *watch; | ||
| 801 | struct inode *inode; | ||
| 802 | |||
| 803 | down(&dev->sem); | ||
| 804 | watch = idr_find(&dev->idr, wd); | ||
| 805 | if (unlikely(!watch)) { | ||
| 806 | up(&dev->sem); | ||
| 807 | return -EINVAL; | ||
| 808 | } | ||
| 809 | get_inotify_watch(watch); | ||
| 810 | inode = watch->inode; | ||
| 811 | up(&dev->sem); | ||
| 812 | |||
| 813 | down(&inode->inotify_sem); | ||
| 814 | down(&dev->sem); | ||
| 815 | |||
| 816 | /* make sure that we did not race */ | ||
| 817 | watch = idr_find(&dev->idr, wd); | ||
| 818 | if (likely(watch)) | ||
| 819 | remove_watch(watch, dev); | ||
| 820 | |||
| 821 | up(&dev->sem); | ||
| 822 | up(&inode->inotify_sem); | ||
| 823 | put_inotify_watch(watch); | ||
| 824 | |||
| 825 | return 0; | ||
| 826 | } | ||
| 827 | |||
| 828 | static long inotify_ioctl(struct file *file, unsigned int cmd, | ||
| 829 | unsigned long arg) | ||
| 830 | { | ||
| 831 | struct inotify_device *dev; | ||
| 832 | void __user *p; | ||
| 833 | int ret = -ENOTTY; | ||
| 834 | |||
| 835 | dev = file->private_data; | ||
| 836 | p = (void __user *) arg; | ||
| 837 | |||
| 838 | switch (cmd) { | ||
| 839 | case FIONREAD: | ||
| 840 | ret = put_user(dev->queue_size, (int __user *) p); | ||
| 841 | break; | ||
| 842 | } | ||
| 843 | |||
| 844 | return ret; | ||
| 845 | } | ||
| 846 | |||
| 847 | static struct file_operations inotify_fops = { | ||
| 848 | .poll = inotify_poll, | ||
| 849 | .read = inotify_read, | ||
| 850 | .release = inotify_release, | ||
| 851 | .unlocked_ioctl = inotify_ioctl, | ||
| 852 | .compat_ioctl = inotify_ioctl, | ||
| 853 | }; | ||
| 854 | |||
| 855 | asmlinkage long sys_inotify_init(void) | ||
| 856 | { | ||
| 857 | struct inotify_device *dev; | ||
| 858 | struct user_struct *user; | ||
| 859 | int ret = -ENOTTY; | ||
| 860 | int fd; | ||
| 861 | struct file *filp; | ||
| 862 | |||
| 863 | fd = get_unused_fd(); | ||
| 864 | if (fd < 0) { | ||
| 865 | ret = fd; | ||
| 866 | goto out; | ||
| 867 | } | ||
| 868 | |||
| 869 | filp = get_empty_filp(); | ||
| 870 | if (!filp) { | ||
| 871 | put_unused_fd(fd); | ||
| 872 | ret = -ENFILE; | ||
| 873 | goto out; | ||
| 874 | } | ||
| 875 | filp->f_op = &inotify_fops; | ||
| 876 | filp->f_vfsmnt = mntget(inotify_mnt); | ||
| 877 | filp->f_dentry = dget(inotify_mnt->mnt_root); | ||
| 878 | filp->f_mapping = filp->f_dentry->d_inode->i_mapping; | ||
| 879 | filp->f_mode = FMODE_READ; | ||
| 880 | filp->f_flags = O_RDONLY; | ||
| 881 | |||
| 882 | user = get_uid(current->user); | ||
| 883 | |||
| 884 | if (unlikely(atomic_read(&user->inotify_devs) >= inotify_max_user_instances)) { | ||
| 885 | ret = -EMFILE; | ||
| 886 | goto out_err; | ||
| 887 | } | ||
| 888 | |||
| 889 | dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL); | ||
| 890 | if (unlikely(!dev)) { | ||
| 891 | ret = -ENOMEM; | ||
| 892 | goto out_err; | ||
| 893 | } | ||
| 894 | |||
| 895 | idr_init(&dev->idr); | ||
| 896 | INIT_LIST_HEAD(&dev->events); | ||
| 897 | INIT_LIST_HEAD(&dev->watches); | ||
| 898 | init_waitqueue_head(&dev->wq); | ||
| 899 | sema_init(&dev->sem, 1); | ||
| 900 | dev->event_count = 0; | ||
| 901 | dev->queue_size = 0; | ||
| 902 | dev->max_events = inotify_max_queued_events; | ||
| 903 | dev->user = user; | ||
| 904 | atomic_set(&dev->count, 0); | ||
| 905 | |||
| 906 | get_inotify_dev(dev); | ||
| 907 | atomic_inc(&user->inotify_devs); | ||
| 908 | |||
| 909 | filp->private_data = dev; | ||
| 910 | fd_install (fd, filp); | ||
| 911 | return fd; | ||
| 912 | out_err: | ||
| 913 | put_unused_fd (fd); | ||
| 914 | put_filp (filp); | ||
| 915 | free_uid(user); | ||
| 916 | out: | ||
| 917 | return ret; | ||
| 918 | } | ||
| 919 | |||
| 920 | asmlinkage long sys_inotify_add_watch(int fd, const char *path, u32 mask) | ||
| 921 | { | ||
| 922 | struct inotify_watch *watch, *old; | ||
| 923 | struct inode *inode; | ||
| 924 | struct inotify_device *dev; | ||
| 925 | struct nameidata nd; | ||
| 926 | struct file *filp; | ||
| 927 | int ret; | ||
| 928 | |||
| 929 | filp = fget(fd); | ||
| 930 | if (!filp) | ||
| 931 | return -EBADF; | ||
| 932 | |||
| 933 | dev = filp->private_data; | ||
| 934 | |||
| 935 | ret = find_inode((const char __user*) path, &nd); | ||
| 936 | if (ret) | ||
| 937 | goto fput_and_out; | ||
| 938 | |||
| 939 | /* Held in place by reference in nd */ | ||
| 940 | inode = nd.dentry->d_inode; | ||
| 941 | |||
| 942 | down(&inode->inotify_sem); | ||
| 943 | down(&dev->sem); | ||
| 944 | |||
| 945 | /* don't let user-space set invalid bits: we don't want flags set */ | ||
| 946 | mask &= IN_ALL_EVENTS; | ||
| 947 | if (!mask) { | ||
| 948 | ret = -EINVAL; | ||
| 949 | goto out; | ||
| 950 | } | ||
| 951 | |||
| 952 | /* | ||
| 953 | * Handle the case of re-adding a watch on an (inode,dev) pair that we | ||
| 954 | * are already watching. We just update the mask and return its wd. | ||
| 955 | */ | ||
| 956 | old = inode_find_dev(inode, dev); | ||
| 957 | if (unlikely(old)) { | ||
| 958 | old->mask = mask; | ||
| 959 | ret = old->wd; | ||
| 960 | goto out; | ||
| 961 | } | ||
| 962 | |||
| 963 | watch = create_watch(dev, mask, inode); | ||
| 964 | if (unlikely(IS_ERR(watch))) { | ||
| 965 | ret = PTR_ERR(watch); | ||
| 966 | goto out; | ||
| 967 | } | ||
| 968 | |||
| 969 | /* Add the watch to the device's and the inode's list */ | ||
| 970 | list_add(&watch->d_list, &dev->watches); | ||
| 971 | list_add(&watch->i_list, &inode->inotify_watches); | ||
| 972 | ret = watch->wd; | ||
| 973 | out: | ||
| 974 | path_release (&nd); | ||
| 975 | up(&dev->sem); | ||
| 976 | up(&inode->inotify_sem); | ||
| 977 | fput_and_out: | ||
| 978 | fput(filp); | ||
| 979 | return ret; | ||
| 980 | } | ||
| 981 | |||
| 982 | asmlinkage long sys_inotify_rm_watch(int fd, u32 wd) | ||
| 983 | { | ||
| 984 | struct file *filp; | ||
| 985 | struct inotify_device *dev; | ||
| 986 | int ret; | ||
| 987 | |||
| 988 | filp = fget(fd); | ||
| 989 | if (!filp) | ||
| 990 | return -EBADF; | ||
| 991 | dev = filp->private_data; | ||
| 992 | ret = inotify_ignore(dev, wd); | ||
| 993 | fput(filp); | ||
| 994 | |||
| 995 | return ret; | ||
| 996 | } | ||
| 997 | |||
| 998 | static struct super_block * | ||
| 999 | inotify_get_sb(struct file_system_type *fs_type, int flags, | ||
| 1000 | const char *dev_name, void *data) | ||
| 1001 | { | ||
| 1002 | return get_sb_pseudo(fs_type, "inotify", NULL, 0xBAD1DEA); | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | static struct file_system_type inotify_fs_type = { | ||
| 1006 | .name = "inotifyfs", | ||
| 1007 | .get_sb = inotify_get_sb, | ||
| 1008 | .kill_sb = kill_anon_super, | ||
| 1009 | }; | ||
| 1010 | |||
| 1011 | /* | ||
| 1012 | * inotify_init - Our initialization function. Note that we cannnot return | ||
| 1013 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here | ||
| 1014 | * must result in panic(). | ||
| 1015 | */ | ||
| 1016 | static int __init inotify_init(void) | ||
| 1017 | { | ||
| 1018 | register_filesystem(&inotify_fs_type); | ||
| 1019 | inotify_mnt = kern_mount(&inotify_fs_type); | ||
| 1020 | |||
| 1021 | inotify_max_queued_events = 8192; | ||
| 1022 | inotify_max_user_instances = 8; | ||
| 1023 | inotify_max_user_watches = 8192; | ||
| 1024 | |||
| 1025 | atomic_set(&inotify_cookie, 0); | ||
| 1026 | |||
| 1027 | watch_cachep = kmem_cache_create("inotify_watch_cache", | ||
| 1028 | sizeof(struct inotify_watch), | ||
| 1029 | 0, SLAB_PANIC, NULL, NULL); | ||
| 1030 | event_cachep = kmem_cache_create("inotify_event_cache", | ||
| 1031 | sizeof(struct inotify_kernel_event), | ||
| 1032 | 0, SLAB_PANIC, NULL, NULL); | ||
| 1033 | |||
| 1034 | return 0; | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | module_init(inotify_init); | ||
