diff options
Diffstat (limited to 'fs/notify/inotify/inotify_user.c')
-rw-r--r-- | fs/notify/inotify/inotify_user.c | 369 |
1 files changed, 234 insertions, 135 deletions
diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c index e46ca685b9be..bf7f6d776c31 100644 --- a/fs/notify/inotify/inotify_user.c +++ b/fs/notify/inotify/inotify_user.c | |||
@@ -46,17 +46,11 @@ | |||
46 | /* these are configurable via /proc/sys/fs/inotify/ */ | 46 | /* these are configurable via /proc/sys/fs/inotify/ */ |
47 | static int inotify_max_user_instances __read_mostly; | 47 | static int inotify_max_user_instances __read_mostly; |
48 | static int inotify_max_queued_events __read_mostly; | 48 | static int inotify_max_queued_events __read_mostly; |
49 | int inotify_max_user_watches __read_mostly; | 49 | static int inotify_max_user_watches __read_mostly; |
50 | 50 | ||
51 | static struct kmem_cache *inotify_inode_mark_cachep __read_mostly; | 51 | static struct kmem_cache *inotify_inode_mark_cachep __read_mostly; |
52 | struct kmem_cache *event_priv_cachep __read_mostly; | 52 | struct kmem_cache *event_priv_cachep __read_mostly; |
53 | 53 | ||
54 | /* | ||
55 | * When inotify registers a new group it increments this and uses that | ||
56 | * value as an offset to set the fsnotify group "name" and priority. | ||
57 | */ | ||
58 | static atomic_t inotify_grp_num; | ||
59 | |||
60 | #ifdef CONFIG_SYSCTL | 54 | #ifdef CONFIG_SYSCTL |
61 | 55 | ||
62 | #include <linux/sysctl.h> | 56 | #include <linux/sysctl.h> |
@@ -96,11 +90,14 @@ static inline __u32 inotify_arg_to_mask(u32 arg) | |||
96 | { | 90 | { |
97 | __u32 mask; | 91 | __u32 mask; |
98 | 92 | ||
99 | /* everything should accept their own ignored and cares about children */ | 93 | /* |
100 | mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD); | 94 | * everything should accept their own ignored, cares about children, |
95 | * and should receive events when the inode is unmounted | ||
96 | */ | ||
97 | mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT); | ||
101 | 98 | ||
102 | /* mask off the flags used to open the fd */ | 99 | /* mask off the flags used to open the fd */ |
103 | mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT)); | 100 | mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK)); |
104 | 101 | ||
105 | return mask; | 102 | return mask; |
106 | } | 103 | } |
@@ -144,6 +141,8 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group, | |||
144 | 141 | ||
145 | event = fsnotify_peek_notify_event(group); | 142 | event = fsnotify_peek_notify_event(group); |
146 | 143 | ||
144 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); | ||
145 | |||
147 | if (event->name_len) | 146 | if (event->name_len) |
148 | event_size += roundup(event->name_len + 1, event_size); | 147 | event_size += roundup(event->name_len + 1, event_size); |
149 | 148 | ||
@@ -173,6 +172,8 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, | |||
173 | size_t event_size = sizeof(struct inotify_event); | 172 | size_t event_size = sizeof(struct inotify_event); |
174 | size_t name_len = 0; | 173 | size_t name_len = 0; |
175 | 174 | ||
175 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); | ||
176 | |||
176 | /* we get the inotify watch descriptor from the event private data */ | 177 | /* we get the inotify watch descriptor from the event private data */ |
177 | spin_lock(&event->lock); | 178 | spin_lock(&event->lock); |
178 | fsn_priv = fsnotify_remove_priv_from_event(group, event); | 179 | fsn_priv = fsnotify_remove_priv_from_event(group, event); |
@@ -245,6 +246,8 @@ static ssize_t inotify_read(struct file *file, char __user *buf, | |||
245 | kevent = get_one_event(group, count); | 246 | kevent = get_one_event(group, count); |
246 | mutex_unlock(&group->notification_mutex); | 247 | mutex_unlock(&group->notification_mutex); |
247 | 248 | ||
249 | pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent); | ||
250 | |||
248 | if (kevent) { | 251 | if (kevent) { |
249 | ret = PTR_ERR(kevent); | 252 | ret = PTR_ERR(kevent); |
250 | if (IS_ERR(kevent)) | 253 | if (IS_ERR(kevent)) |
@@ -289,6 +292,8 @@ static int inotify_release(struct inode *ignored, struct file *file) | |||
289 | struct fsnotify_group *group = file->private_data; | 292 | struct fsnotify_group *group = file->private_data; |
290 | struct user_struct *user = group->inotify_data.user; | 293 | struct user_struct *user = group->inotify_data.user; |
291 | 294 | ||
295 | pr_debug("%s: group=%p\n", __func__, group); | ||
296 | |||
292 | fsnotify_clear_marks_by_group(group); | 297 | fsnotify_clear_marks_by_group(group); |
293 | 298 | ||
294 | /* free this group, matching get was inotify_init->fsnotify_obtain_group */ | 299 | /* free this group, matching get was inotify_init->fsnotify_obtain_group */ |
@@ -312,6 +317,8 @@ static long inotify_ioctl(struct file *file, unsigned int cmd, | |||
312 | group = file->private_data; | 317 | group = file->private_data; |
313 | p = (void __user *) arg; | 318 | p = (void __user *) arg; |
314 | 319 | ||
320 | pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd); | ||
321 | |||
315 | switch (cmd) { | 322 | switch (cmd) { |
316 | case FIONREAD: | 323 | case FIONREAD: |
317 | mutex_lock(&group->notification_mutex); | 324 | mutex_lock(&group->notification_mutex); |
@@ -357,59 +364,159 @@ static int inotify_find_inode(const char __user *dirname, struct path *path, uns | |||
357 | return error; | 364 | return error; |
358 | } | 365 | } |
359 | 366 | ||
367 | static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock, | ||
368 | int *last_wd, | ||
369 | struct inotify_inode_mark *i_mark) | ||
370 | { | ||
371 | int ret; | ||
372 | |||
373 | do { | ||
374 | if (unlikely(!idr_pre_get(idr, GFP_KERNEL))) | ||
375 | return -ENOMEM; | ||
376 | |||
377 | spin_lock(idr_lock); | ||
378 | ret = idr_get_new_above(idr, i_mark, *last_wd + 1, | ||
379 | &i_mark->wd); | ||
380 | /* we added the mark to the idr, take a reference */ | ||
381 | if (!ret) { | ||
382 | *last_wd = i_mark->wd; | ||
383 | fsnotify_get_mark(&i_mark->fsn_mark); | ||
384 | } | ||
385 | spin_unlock(idr_lock); | ||
386 | } while (ret == -EAGAIN); | ||
387 | |||
388 | return ret; | ||
389 | } | ||
390 | |||
391 | static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group, | ||
392 | int wd) | ||
393 | { | ||
394 | struct idr *idr = &group->inotify_data.idr; | ||
395 | spinlock_t *idr_lock = &group->inotify_data.idr_lock; | ||
396 | struct inotify_inode_mark *i_mark; | ||
397 | |||
398 | assert_spin_locked(idr_lock); | ||
399 | |||
400 | i_mark = idr_find(idr, wd); | ||
401 | if (i_mark) { | ||
402 | struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark; | ||
403 | |||
404 | fsnotify_get_mark(fsn_mark); | ||
405 | /* One ref for being in the idr, one ref we just took */ | ||
406 | BUG_ON(atomic_read(&fsn_mark->refcnt) < 2); | ||
407 | } | ||
408 | |||
409 | return i_mark; | ||
410 | } | ||
411 | |||
412 | static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group, | ||
413 | int wd) | ||
414 | { | ||
415 | struct inotify_inode_mark *i_mark; | ||
416 | spinlock_t *idr_lock = &group->inotify_data.idr_lock; | ||
417 | |||
418 | spin_lock(idr_lock); | ||
419 | i_mark = inotify_idr_find_locked(group, wd); | ||
420 | spin_unlock(idr_lock); | ||
421 | |||
422 | return i_mark; | ||
423 | } | ||
424 | |||
425 | static void do_inotify_remove_from_idr(struct fsnotify_group *group, | ||
426 | struct inotify_inode_mark *i_mark) | ||
427 | { | ||
428 | struct idr *idr = &group->inotify_data.idr; | ||
429 | spinlock_t *idr_lock = &group->inotify_data.idr_lock; | ||
430 | int wd = i_mark->wd; | ||
431 | |||
432 | assert_spin_locked(idr_lock); | ||
433 | |||
434 | idr_remove(idr, wd); | ||
435 | |||
436 | /* removed from the idr, drop that ref */ | ||
437 | fsnotify_put_mark(&i_mark->fsn_mark); | ||
438 | } | ||
439 | |||
360 | /* | 440 | /* |
361 | * Remove the mark from the idr (if present) and drop the reference | 441 | * Remove the mark from the idr (if present) and drop the reference |
362 | * on the mark because it was in the idr. | 442 | * on the mark because it was in the idr. |
363 | */ | 443 | */ |
364 | static void inotify_remove_from_idr(struct fsnotify_group *group, | 444 | static void inotify_remove_from_idr(struct fsnotify_group *group, |
365 | struct inotify_inode_mark_entry *ientry) | 445 | struct inotify_inode_mark *i_mark) |
366 | { | 446 | { |
367 | struct idr *idr; | 447 | spinlock_t *idr_lock = &group->inotify_data.idr_lock; |
368 | struct fsnotify_mark_entry *entry; | 448 | struct inotify_inode_mark *found_i_mark = NULL; |
369 | struct inotify_inode_mark_entry *found_ientry; | ||
370 | int wd; | 449 | int wd; |
371 | 450 | ||
372 | spin_lock(&group->inotify_data.idr_lock); | 451 | spin_lock(idr_lock); |
373 | idr = &group->inotify_data.idr; | 452 | wd = i_mark->wd; |
374 | wd = ientry->wd; | ||
375 | 453 | ||
376 | if (wd == -1) | 454 | /* |
455 | * does this i_mark think it is in the idr? we shouldn't get called | ||
456 | * if it wasn't.... | ||
457 | */ | ||
458 | if (wd == -1) { | ||
459 | WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p" | ||
460 | " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd, | ||
461 | i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode); | ||
377 | goto out; | 462 | goto out; |
463 | } | ||
378 | 464 | ||
379 | entry = idr_find(&group->inotify_data.idr, wd); | 465 | /* Lets look in the idr to see if we find it */ |
380 | if (unlikely(!entry)) | 466 | found_i_mark = inotify_idr_find_locked(group, wd); |
467 | if (unlikely(!found_i_mark)) { | ||
468 | WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p" | ||
469 | " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd, | ||
470 | i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode); | ||
381 | goto out; | 471 | goto out; |
472 | } | ||
382 | 473 | ||
383 | found_ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry); | 474 | /* |
384 | if (unlikely(found_ientry != ientry)) { | 475 | * We found an mark in the idr at the right wd, but it's |
385 | /* We found an entry in the idr with the right wd, but it's | 476 | * not the mark we were told to remove. eparis seriously |
386 | * not the entry we were told to remove. eparis seriously | 477 | * fucked up somewhere. |
387 | * fucked up somewhere. */ | 478 | */ |
388 | WARN_ON(1); | 479 | if (unlikely(found_i_mark != i_mark)) { |
389 | ientry->wd = -1; | 480 | WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p " |
481 | "mark->inode=%p found_i_mark=%p found_i_mark->wd=%d " | ||
482 | "found_i_mark->group=%p found_i_mark->inode=%p\n", | ||
483 | __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group, | ||
484 | i_mark->fsn_mark.i.inode, found_i_mark, found_i_mark->wd, | ||
485 | found_i_mark->fsn_mark.group, | ||
486 | found_i_mark->fsn_mark.i.inode); | ||
390 | goto out; | 487 | goto out; |
391 | } | 488 | } |
392 | 489 | ||
393 | /* One ref for being in the idr, one ref held by the caller */ | 490 | /* |
394 | BUG_ON(atomic_read(&entry->refcnt) < 2); | 491 | * One ref for being in the idr |
395 | 492 | * one ref held by the caller trying to kill us | |
396 | idr_remove(idr, wd); | 493 | * one ref grabbed by inotify_idr_find |
397 | ientry->wd = -1; | 494 | */ |
495 | if (unlikely(atomic_read(&i_mark->fsn_mark.refcnt) < 3)) { | ||
496 | printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p" | ||
497 | " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd, | ||
498 | i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode); | ||
499 | /* we can't really recover with bad ref cnting.. */ | ||
500 | BUG(); | ||
501 | } | ||
398 | 502 | ||
399 | /* removed from the idr, drop that ref */ | 503 | do_inotify_remove_from_idr(group, i_mark); |
400 | fsnotify_put_mark(entry); | ||
401 | out: | 504 | out: |
402 | spin_unlock(&group->inotify_data.idr_lock); | 505 | /* match the ref taken by inotify_idr_find_locked() */ |
506 | if (found_i_mark) | ||
507 | fsnotify_put_mark(&found_i_mark->fsn_mark); | ||
508 | i_mark->wd = -1; | ||
509 | spin_unlock(idr_lock); | ||
403 | } | 510 | } |
404 | 511 | ||
405 | /* | 512 | /* |
406 | * Send IN_IGNORED for this wd, remove this wd from the idr. | 513 | * Send IN_IGNORED for this wd, remove this wd from the idr. |
407 | */ | 514 | */ |
408 | void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry, | 515 | void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark, |
409 | struct fsnotify_group *group) | 516 | struct fsnotify_group *group) |
410 | { | 517 | { |
411 | struct inotify_inode_mark_entry *ientry; | 518 | struct inotify_inode_mark *i_mark; |
412 | struct fsnotify_event *ignored_event; | 519 | struct fsnotify_event *ignored_event, *notify_event; |
413 | struct inotify_event_private_data *event_priv; | 520 | struct inotify_event_private_data *event_priv; |
414 | struct fsnotify_event_private_data *fsn_event_priv; | 521 | struct fsnotify_event_private_data *fsn_event_priv; |
415 | int ret; | 522 | int ret; |
@@ -420,7 +527,7 @@ void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry, | |||
420 | if (!ignored_event) | 527 | if (!ignored_event) |
421 | return; | 528 | return; |
422 | 529 | ||
423 | ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry); | 530 | i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); |
424 | 531 | ||
425 | event_priv = kmem_cache_alloc(event_priv_cachep, GFP_NOFS); | 532 | event_priv = kmem_cache_alloc(event_priv_cachep, GFP_NOFS); |
426 | if (unlikely(!event_priv)) | 533 | if (unlikely(!event_priv)) |
@@ -429,37 +536,44 @@ void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry, | |||
429 | fsn_event_priv = &event_priv->fsnotify_event_priv_data; | 536 | fsn_event_priv = &event_priv->fsnotify_event_priv_data; |
430 | 537 | ||
431 | fsn_event_priv->group = group; | 538 | fsn_event_priv->group = group; |
432 | event_priv->wd = ientry->wd; | 539 | event_priv->wd = i_mark->wd; |
433 | 540 | ||
434 | ret = fsnotify_add_notify_event(group, ignored_event, fsn_event_priv); | 541 | notify_event = fsnotify_add_notify_event(group, ignored_event, fsn_event_priv, NULL); |
435 | if (ret) | 542 | if (notify_event) { |
543 | if (IS_ERR(notify_event)) | ||
544 | ret = PTR_ERR(notify_event); | ||
545 | else | ||
546 | fsnotify_put_event(notify_event); | ||
436 | inotify_free_event_priv(fsn_event_priv); | 547 | inotify_free_event_priv(fsn_event_priv); |
548 | } | ||
437 | 549 | ||
438 | skip_send_ignore: | 550 | skip_send_ignore: |
439 | 551 | ||
440 | /* matches the reference taken when the event was created */ | 552 | /* matches the reference taken when the event was created */ |
441 | fsnotify_put_event(ignored_event); | 553 | fsnotify_put_event(ignored_event); |
442 | 554 | ||
443 | /* remove this entry from the idr */ | 555 | /* remove this mark from the idr */ |
444 | inotify_remove_from_idr(group, ientry); | 556 | inotify_remove_from_idr(group, i_mark); |
445 | 557 | ||
446 | atomic_dec(&group->inotify_data.user->inotify_watches); | 558 | atomic_dec(&group->inotify_data.user->inotify_watches); |
447 | } | 559 | } |
448 | 560 | ||
449 | /* ding dong the mark is dead */ | 561 | /* ding dong the mark is dead */ |
450 | static void inotify_free_mark(struct fsnotify_mark_entry *entry) | 562 | static void inotify_free_mark(struct fsnotify_mark *fsn_mark) |
451 | { | 563 | { |
452 | struct inotify_inode_mark_entry *ientry = (struct inotify_inode_mark_entry *)entry; | 564 | struct inotify_inode_mark *i_mark; |
565 | |||
566 | i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); | ||
453 | 567 | ||
454 | kmem_cache_free(inotify_inode_mark_cachep, ientry); | 568 | kmem_cache_free(inotify_inode_mark_cachep, i_mark); |
455 | } | 569 | } |
456 | 570 | ||
457 | static int inotify_update_existing_watch(struct fsnotify_group *group, | 571 | static int inotify_update_existing_watch(struct fsnotify_group *group, |
458 | struct inode *inode, | 572 | struct inode *inode, |
459 | u32 arg) | 573 | u32 arg) |
460 | { | 574 | { |
461 | struct fsnotify_mark_entry *entry; | 575 | struct fsnotify_mark *fsn_mark; |
462 | struct inotify_inode_mark_entry *ientry; | 576 | struct inotify_inode_mark *i_mark; |
463 | __u32 old_mask, new_mask; | 577 | __u32 old_mask, new_mask; |
464 | __u32 mask; | 578 | __u32 mask; |
465 | int add = (arg & IN_MASK_ADD); | 579 | int add = (arg & IN_MASK_ADD); |
@@ -467,52 +581,43 @@ static int inotify_update_existing_watch(struct fsnotify_group *group, | |||
467 | 581 | ||
468 | /* don't allow invalid bits: we don't want flags set */ | 582 | /* don't allow invalid bits: we don't want flags set */ |
469 | mask = inotify_arg_to_mask(arg); | 583 | mask = inotify_arg_to_mask(arg); |
470 | if (unlikely(!mask)) | 584 | if (unlikely(!(mask & IN_ALL_EVENTS))) |
471 | return -EINVAL; | 585 | return -EINVAL; |
472 | 586 | ||
473 | spin_lock(&inode->i_lock); | 587 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
474 | entry = fsnotify_find_mark_entry(group, inode); | 588 | if (!fsn_mark) |
475 | spin_unlock(&inode->i_lock); | ||
476 | if (!entry) | ||
477 | return -ENOENT; | 589 | return -ENOENT; |
478 | 590 | ||
479 | ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry); | 591 | i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); |
480 | 592 | ||
481 | spin_lock(&entry->lock); | 593 | spin_lock(&fsn_mark->lock); |
482 | 594 | ||
483 | old_mask = entry->mask; | 595 | old_mask = fsn_mark->mask; |
484 | if (add) { | 596 | if (add) |
485 | entry->mask |= mask; | 597 | fsnotify_set_mark_mask_locked(fsn_mark, (fsn_mark->mask | mask)); |
486 | new_mask = entry->mask; | 598 | else |
487 | } else { | 599 | fsnotify_set_mark_mask_locked(fsn_mark, mask); |
488 | entry->mask = mask; | 600 | new_mask = fsn_mark->mask; |
489 | new_mask = entry->mask; | ||
490 | } | ||
491 | 601 | ||
492 | spin_unlock(&entry->lock); | 602 | spin_unlock(&fsn_mark->lock); |
493 | 603 | ||
494 | if (old_mask != new_mask) { | 604 | if (old_mask != new_mask) { |
495 | /* more bits in old than in new? */ | 605 | /* more bits in old than in new? */ |
496 | int dropped = (old_mask & ~new_mask); | 606 | int dropped = (old_mask & ~new_mask); |
497 | /* more bits in this entry than the inode's mask? */ | 607 | /* more bits in this fsn_mark than the inode's mask? */ |
498 | int do_inode = (new_mask & ~inode->i_fsnotify_mask); | 608 | int do_inode = (new_mask & ~inode->i_fsnotify_mask); |
499 | /* more bits in this entry than the group? */ | ||
500 | int do_group = (new_mask & ~group->mask); | ||
501 | 609 | ||
502 | /* update the inode with this new entry */ | 610 | /* update the inode with this new fsn_mark */ |
503 | if (dropped || do_inode) | 611 | if (dropped || do_inode) |
504 | fsnotify_recalc_inode_mask(inode); | 612 | fsnotify_recalc_inode_mask(inode); |
505 | 613 | ||
506 | /* update the group mask with the new mask */ | ||
507 | if (dropped || do_group) | ||
508 | fsnotify_recalc_group_mask(group); | ||
509 | } | 614 | } |
510 | 615 | ||
511 | /* return the wd */ | 616 | /* return the wd */ |
512 | ret = ientry->wd; | 617 | ret = i_mark->wd; |
513 | 618 | ||
514 | /* match the get from fsnotify_find_mark_entry() */ | 619 | /* match the get from fsnotify_find_mark() */ |
515 | fsnotify_put_mark(entry); | 620 | fsnotify_put_mark(fsn_mark); |
516 | 621 | ||
517 | return ret; | 622 | return ret; |
518 | } | 623 | } |
@@ -521,73 +626,51 @@ static int inotify_new_watch(struct fsnotify_group *group, | |||
521 | struct inode *inode, | 626 | struct inode *inode, |
522 | u32 arg) | 627 | u32 arg) |
523 | { | 628 | { |
524 | struct inotify_inode_mark_entry *tmp_ientry; | 629 | struct inotify_inode_mark *tmp_i_mark; |
525 | __u32 mask; | 630 | __u32 mask; |
526 | int ret; | 631 | int ret; |
632 | struct idr *idr = &group->inotify_data.idr; | ||
633 | spinlock_t *idr_lock = &group->inotify_data.idr_lock; | ||
527 | 634 | ||
528 | /* don't allow invalid bits: we don't want flags set */ | 635 | /* don't allow invalid bits: we don't want flags set */ |
529 | mask = inotify_arg_to_mask(arg); | 636 | mask = inotify_arg_to_mask(arg); |
530 | if (unlikely(!mask)) | 637 | if (unlikely(!(mask & IN_ALL_EVENTS))) |
531 | return -EINVAL; | 638 | return -EINVAL; |
532 | 639 | ||
533 | tmp_ientry = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); | 640 | tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); |
534 | if (unlikely(!tmp_ientry)) | 641 | if (unlikely(!tmp_i_mark)) |
535 | return -ENOMEM; | 642 | return -ENOMEM; |
536 | 643 | ||
537 | fsnotify_init_mark(&tmp_ientry->fsn_entry, inotify_free_mark); | 644 | fsnotify_init_mark(&tmp_i_mark->fsn_mark, inotify_free_mark); |
538 | tmp_ientry->fsn_entry.mask = mask; | 645 | tmp_i_mark->fsn_mark.mask = mask; |
539 | tmp_ientry->wd = -1; | 646 | tmp_i_mark->wd = -1; |
540 | 647 | ||
541 | ret = -ENOSPC; | 648 | ret = -ENOSPC; |
542 | if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches) | 649 | if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches) |
543 | goto out_err; | 650 | goto out_err; |
544 | retry: | ||
545 | ret = -ENOMEM; | ||
546 | if (unlikely(!idr_pre_get(&group->inotify_data.idr, GFP_KERNEL))) | ||
547 | goto out_err; | ||
548 | 651 | ||
549 | /* we are putting the mark on the idr, take a reference */ | 652 | ret = inotify_add_to_idr(idr, idr_lock, &group->inotify_data.last_wd, |
550 | fsnotify_get_mark(&tmp_ientry->fsn_entry); | 653 | tmp_i_mark); |
551 | 654 | if (ret) | |
552 | spin_lock(&group->inotify_data.idr_lock); | ||
553 | ret = idr_get_new_above(&group->inotify_data.idr, &tmp_ientry->fsn_entry, | ||
554 | group->inotify_data.last_wd+1, | ||
555 | &tmp_ientry->wd); | ||
556 | spin_unlock(&group->inotify_data.idr_lock); | ||
557 | if (ret) { | ||
558 | /* we didn't get on the idr, drop the idr reference */ | ||
559 | fsnotify_put_mark(&tmp_ientry->fsn_entry); | ||
560 | |||
561 | /* idr was out of memory allocate and try again */ | ||
562 | if (ret == -EAGAIN) | ||
563 | goto retry; | ||
564 | goto out_err; | 655 | goto out_err; |
565 | } | ||
566 | 656 | ||
567 | /* we are on the idr, now get on the inode */ | 657 | /* we are on the idr, now get on the inode */ |
568 | ret = fsnotify_add_mark(&tmp_ientry->fsn_entry, group, inode); | 658 | ret = fsnotify_add_mark(&tmp_i_mark->fsn_mark, group, inode, NULL, 0); |
569 | if (ret) { | 659 | if (ret) { |
570 | /* we failed to get on the inode, get off the idr */ | 660 | /* we failed to get on the inode, get off the idr */ |
571 | inotify_remove_from_idr(group, tmp_ientry); | 661 | inotify_remove_from_idr(group, tmp_i_mark); |
572 | goto out_err; | 662 | goto out_err; |
573 | } | 663 | } |
574 | 664 | ||
575 | /* update the idr hint, who cares about races, it's just a hint */ | ||
576 | group->inotify_data.last_wd = tmp_ientry->wd; | ||
577 | |||
578 | /* increment the number of watches the user has */ | 665 | /* increment the number of watches the user has */ |
579 | atomic_inc(&group->inotify_data.user->inotify_watches); | 666 | atomic_inc(&group->inotify_data.user->inotify_watches); |
580 | 667 | ||
581 | /* return the watch descriptor for this new entry */ | 668 | /* return the watch descriptor for this new mark */ |
582 | ret = tmp_ientry->wd; | 669 | ret = tmp_i_mark->wd; |
583 | |||
584 | /* if this mark added a new event update the group mask */ | ||
585 | if (mask & ~group->mask) | ||
586 | fsnotify_recalc_group_mask(group); | ||
587 | 670 | ||
588 | out_err: | 671 | out_err: |
589 | /* match the ref from fsnotify_init_markentry() */ | 672 | /* match the ref from fsnotify_init_mark() */ |
590 | fsnotify_put_mark(&tmp_ientry->fsn_entry); | 673 | fsnotify_put_mark(&tmp_i_mark->fsn_mark); |
591 | 674 | ||
592 | return ret; | 675 | return ret; |
593 | } | 676 | } |
@@ -616,11 +699,8 @@ retry: | |||
616 | static struct fsnotify_group *inotify_new_group(struct user_struct *user, unsigned int max_events) | 699 | static struct fsnotify_group *inotify_new_group(struct user_struct *user, unsigned int max_events) |
617 | { | 700 | { |
618 | struct fsnotify_group *group; | 701 | struct fsnotify_group *group; |
619 | unsigned int grp_num; | ||
620 | 702 | ||
621 | /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */ | 703 | group = fsnotify_alloc_group(&inotify_fsnotify_ops); |
622 | grp_num = (INOTIFY_GROUP_NUM - atomic_inc_return(&inotify_grp_num)); | ||
623 | group = fsnotify_obtain_group(grp_num, 0, &inotify_fsnotify_ops); | ||
624 | if (IS_ERR(group)) | 704 | if (IS_ERR(group)) |
625 | return group; | 705 | return group; |
626 | 706 | ||
@@ -726,7 +806,7 @@ fput_and_out: | |||
726 | SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd) | 806 | SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd) |
727 | { | 807 | { |
728 | struct fsnotify_group *group; | 808 | struct fsnotify_group *group; |
729 | struct fsnotify_mark_entry *entry; | 809 | struct inotify_inode_mark *i_mark; |
730 | struct file *filp; | 810 | struct file *filp; |
731 | int ret = 0, fput_needed; | 811 | int ret = 0, fput_needed; |
732 | 812 | ||
@@ -735,25 +815,23 @@ SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd) | |||
735 | return -EBADF; | 815 | return -EBADF; |
736 | 816 | ||
737 | /* verify that this is indeed an inotify instance */ | 817 | /* verify that this is indeed an inotify instance */ |
738 | if (unlikely(filp->f_op != &inotify_fops)) { | 818 | ret = -EINVAL; |
739 | ret = -EINVAL; | 819 | if (unlikely(filp->f_op != &inotify_fops)) |
740 | goto out; | 820 | goto out; |
741 | } | ||
742 | 821 | ||
743 | group = filp->private_data; | 822 | group = filp->private_data; |
744 | 823 | ||
745 | spin_lock(&group->inotify_data.idr_lock); | 824 | ret = -EINVAL; |
746 | entry = idr_find(&group->inotify_data.idr, wd); | 825 | i_mark = inotify_idr_find(group, wd); |
747 | if (unlikely(!entry)) { | 826 | if (unlikely(!i_mark)) |
748 | spin_unlock(&group->inotify_data.idr_lock); | ||
749 | ret = -EINVAL; | ||
750 | goto out; | 827 | goto out; |
751 | } | ||
752 | fsnotify_get_mark(entry); | ||
753 | spin_unlock(&group->inotify_data.idr_lock); | ||
754 | 828 | ||
755 | fsnotify_destroy_mark_by_entry(entry); | 829 | ret = 0; |
756 | fsnotify_put_mark(entry); | 830 | |
831 | fsnotify_destroy_mark(&i_mark->fsn_mark); | ||
832 | |||
833 | /* match ref taken by inotify_idr_find */ | ||
834 | fsnotify_put_mark(&i_mark->fsn_mark); | ||
757 | 835 | ||
758 | out: | 836 | out: |
759 | fput_light(filp, fput_needed); | 837 | fput_light(filp, fput_needed); |
@@ -767,7 +845,28 @@ out: | |||
767 | */ | 845 | */ |
768 | static int __init inotify_user_setup(void) | 846 | static int __init inotify_user_setup(void) |
769 | { | 847 | { |
770 | inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark_entry, SLAB_PANIC); | 848 | BUILD_BUG_ON(IN_ACCESS != FS_ACCESS); |
849 | BUILD_BUG_ON(IN_MODIFY != FS_MODIFY); | ||
850 | BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB); | ||
851 | BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE); | ||
852 | BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); | ||
853 | BUILD_BUG_ON(IN_OPEN != FS_OPEN); | ||
854 | BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM); | ||
855 | BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO); | ||
856 | BUILD_BUG_ON(IN_CREATE != FS_CREATE); | ||
857 | BUILD_BUG_ON(IN_DELETE != FS_DELETE); | ||
858 | BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF); | ||
859 | BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF); | ||
860 | BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT); | ||
861 | BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW); | ||
862 | BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED); | ||
863 | BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK); | ||
864 | BUILD_BUG_ON(IN_ISDIR != FS_IN_ISDIR); | ||
865 | BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT); | ||
866 | |||
867 | BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21); | ||
868 | |||
869 | inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC); | ||
771 | event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC); | 870 | event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC); |
772 | 871 | ||
773 | inotify_max_queued_events = 16384; | 872 | inotify_max_queued_events = 16384; |