aboutsummaryrefslogtreecommitdiffstats
path: root/fs/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/inode.c')
-rw-r--r--fs/inode.c676
1 files changed, 332 insertions, 344 deletions
diff --git a/fs/inode.c b/fs/inode.c
index 16fefd373fc2..05a1f75ae791 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -25,6 +25,39 @@
25#include <linux/async.h> 25#include <linux/async.h>
26#include <linux/posix_acl.h> 26#include <linux/posix_acl.h>
27#include <linux/ima.h> 27#include <linux/ima.h>
28#include <linux/cred.h>
29#include "internal.h"
30
31/*
32 * inode locking rules.
33 *
34 * inode->i_lock protects:
35 * inode->i_state, inode->i_hash, __iget()
36 * inode_lru_lock protects:
37 * inode_lru, inode->i_lru
38 * inode_sb_list_lock protects:
39 * sb->s_inodes, inode->i_sb_list
40 * inode_wb_list_lock protects:
41 * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
42 * inode_hash_lock protects:
43 * inode_hashtable, inode->i_hash
44 *
45 * Lock ordering:
46 *
47 * inode_sb_list_lock
48 * inode->i_lock
49 * inode_lru_lock
50 *
51 * inode_wb_list_lock
52 * inode->i_lock
53 *
54 * inode_hash_lock
55 * inode_sb_list_lock
56 * inode->i_lock
57 *
58 * iunique_lock
59 * inode_hash_lock
60 */
28 61
29/* 62/*
30 * This is needed for the following functions: 63 * This is needed for the following functions:
@@ -59,6 +92,8 @@
59 92
60static unsigned int i_hash_mask __read_mostly; 93static unsigned int i_hash_mask __read_mostly;
61static unsigned int i_hash_shift __read_mostly; 94static unsigned int i_hash_shift __read_mostly;
95static struct hlist_head *inode_hashtable __read_mostly;
96static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
62 97
63/* 98/*
64 * Each inode can be on two separate lists. One is 99 * Each inode can be on two separate lists. One is
@@ -73,15 +108,10 @@ static unsigned int i_hash_shift __read_mostly;
73 */ 108 */
74 109
75static LIST_HEAD(inode_lru); 110static LIST_HEAD(inode_lru);
76static struct hlist_head *inode_hashtable __read_mostly; 111static DEFINE_SPINLOCK(inode_lru_lock);
77 112
78/* 113__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
79 * A simple spinlock to protect the list manipulations. 114__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
80 *
81 * NOTE! You also have to own the lock if you change
82 * the i_state of an inode while it is in use..
83 */
84DEFINE_SPINLOCK(inode_lock);
85 115
86/* 116/*
87 * iprune_sem provides exclusion between the icache shrinking and the 117 * iprune_sem provides exclusion between the icache shrinking and the
@@ -136,15 +166,6 @@ int proc_nr_inodes(ctl_table *table, int write,
136} 166}
137#endif 167#endif
138 168
139static void wake_up_inode(struct inode *inode)
140{
141 /*
142 * Prevent speculative execution through spin_unlock(&inode_lock);
143 */
144 smp_mb();
145 wake_up_bit(&inode->i_state, __I_NEW);
146}
147
148/** 169/**
149 * inode_init_always - perform inode structure intialisation 170 * inode_init_always - perform inode structure intialisation
150 * @sb: superblock inode belongs to 171 * @sb: superblock inode belongs to
@@ -335,7 +356,7 @@ static void init_once(void *foo)
335} 356}
336 357
337/* 358/*
338 * inode_lock must be held 359 * inode->i_lock must be held
339 */ 360 */
340void __iget(struct inode *inode) 361void __iget(struct inode *inode)
341{ 362{
@@ -353,23 +374,22 @@ EXPORT_SYMBOL(ihold);
353 374
354static void inode_lru_list_add(struct inode *inode) 375static void inode_lru_list_add(struct inode *inode)
355{ 376{
377 spin_lock(&inode_lru_lock);
356 if (list_empty(&inode->i_lru)) { 378 if (list_empty(&inode->i_lru)) {
357 list_add(&inode->i_lru, &inode_lru); 379 list_add(&inode->i_lru, &inode_lru);
358 inodes_stat.nr_unused++; 380 inodes_stat.nr_unused++;
359 } 381 }
382 spin_unlock(&inode_lru_lock);
360} 383}
361 384
362static void inode_lru_list_del(struct inode *inode) 385static void inode_lru_list_del(struct inode *inode)
363{ 386{
387 spin_lock(&inode_lru_lock);
364 if (!list_empty(&inode->i_lru)) { 388 if (!list_empty(&inode->i_lru)) {
365 list_del_init(&inode->i_lru); 389 list_del_init(&inode->i_lru);
366 inodes_stat.nr_unused--; 390 inodes_stat.nr_unused--;
367 } 391 }
368} 392 spin_unlock(&inode_lru_lock);
369
370static inline void __inode_sb_list_add(struct inode *inode)
371{
372 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
373} 393}
374 394
375/** 395/**
@@ -378,15 +398,17 @@ static inline void __inode_sb_list_add(struct inode *inode)
378 */ 398 */
379void inode_sb_list_add(struct inode *inode) 399void inode_sb_list_add(struct inode *inode)
380{ 400{
381 spin_lock(&inode_lock); 401 spin_lock(&inode_sb_list_lock);
382 __inode_sb_list_add(inode); 402 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
383 spin_unlock(&inode_lock); 403 spin_unlock(&inode_sb_list_lock);
384} 404}
385EXPORT_SYMBOL_GPL(inode_sb_list_add); 405EXPORT_SYMBOL_GPL(inode_sb_list_add);
386 406
387static inline void __inode_sb_list_del(struct inode *inode) 407static inline void inode_sb_list_del(struct inode *inode)
388{ 408{
409 spin_lock(&inode_sb_list_lock);
389 list_del_init(&inode->i_sb_list); 410 list_del_init(&inode->i_sb_list);
411 spin_unlock(&inode_sb_list_lock);
390} 412}
391 413
392static unsigned long hash(struct super_block *sb, unsigned long hashval) 414static unsigned long hash(struct super_block *sb, unsigned long hashval)
@@ -411,24 +433,15 @@ void __insert_inode_hash(struct inode *inode, unsigned long hashval)
411{ 433{
412 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval); 434 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
413 435
414 spin_lock(&inode_lock); 436 spin_lock(&inode_hash_lock);
437 spin_lock(&inode->i_lock);
415 hlist_add_head(&inode->i_hash, b); 438 hlist_add_head(&inode->i_hash, b);
416 spin_unlock(&inode_lock); 439 spin_unlock(&inode->i_lock);
440 spin_unlock(&inode_hash_lock);
417} 441}
418EXPORT_SYMBOL(__insert_inode_hash); 442EXPORT_SYMBOL(__insert_inode_hash);
419 443
420/** 444/**
421 * __remove_inode_hash - remove an inode from the hash
422 * @inode: inode to unhash
423 *
424 * Remove an inode from the superblock.
425 */
426static void __remove_inode_hash(struct inode *inode)
427{
428 hlist_del_init(&inode->i_hash);
429}
430
431/**
432 * remove_inode_hash - remove an inode from the hash 445 * remove_inode_hash - remove an inode from the hash
433 * @inode: inode to unhash 446 * @inode: inode to unhash
434 * 447 *
@@ -436,9 +449,11 @@ static void __remove_inode_hash(struct inode *inode)
436 */ 449 */
437void remove_inode_hash(struct inode *inode) 450void remove_inode_hash(struct inode *inode)
438{ 451{
439 spin_lock(&inode_lock); 452 spin_lock(&inode_hash_lock);
453 spin_lock(&inode->i_lock);
440 hlist_del_init(&inode->i_hash); 454 hlist_del_init(&inode->i_hash);
441 spin_unlock(&inode_lock); 455 spin_unlock(&inode->i_lock);
456 spin_unlock(&inode_hash_lock);
442} 457}
443EXPORT_SYMBOL(remove_inode_hash); 458EXPORT_SYMBOL(remove_inode_hash);
444 459
@@ -455,10 +470,29 @@ void end_writeback(struct inode *inode)
455} 470}
456EXPORT_SYMBOL(end_writeback); 471EXPORT_SYMBOL(end_writeback);
457 472
473/*
474 * Free the inode passed in, removing it from the lists it is still connected
475 * to. We remove any pages still attached to the inode and wait for any IO that
476 * is still in progress before finally destroying the inode.
477 *
478 * An inode must already be marked I_FREEING so that we avoid the inode being
479 * moved back onto lists if we race with other code that manipulates the lists
480 * (e.g. writeback_single_inode). The caller is responsible for setting this.
481 *
482 * An inode must already be removed from the LRU list before being evicted from
483 * the cache. This should occur atomically with setting the I_FREEING state
484 * flag, so no inodes here should ever be on the LRU when being evicted.
485 */
458static void evict(struct inode *inode) 486static void evict(struct inode *inode)
459{ 487{
460 const struct super_operations *op = inode->i_sb->s_op; 488 const struct super_operations *op = inode->i_sb->s_op;
461 489
490 BUG_ON(!(inode->i_state & I_FREEING));
491 BUG_ON(!list_empty(&inode->i_lru));
492
493 inode_wb_list_del(inode);
494 inode_sb_list_del(inode);
495
462 if (op->evict_inode) { 496 if (op->evict_inode) {
463 op->evict_inode(inode); 497 op->evict_inode(inode);
464 } else { 498 } else {
@@ -470,6 +504,15 @@ static void evict(struct inode *inode)
470 bd_forget(inode); 504 bd_forget(inode);
471 if (S_ISCHR(inode->i_mode) && inode->i_cdev) 505 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
472 cd_forget(inode); 506 cd_forget(inode);
507
508 remove_inode_hash(inode);
509
510 spin_lock(&inode->i_lock);
511 wake_up_bit(&inode->i_state, __I_NEW);
512 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
513 spin_unlock(&inode->i_lock);
514
515 destroy_inode(inode);
473} 516}
474 517
475/* 518/*
@@ -488,14 +531,6 @@ static void dispose_list(struct list_head *head)
488 list_del_init(&inode->i_lru); 531 list_del_init(&inode->i_lru);
489 532
490 evict(inode); 533 evict(inode);
491
492 spin_lock(&inode_lock);
493 __remove_inode_hash(inode);
494 __inode_sb_list_del(inode);
495 spin_unlock(&inode_lock);
496
497 wake_up_inode(inode);
498 destroy_inode(inode);
499 } 534 }
500} 535}
501 536
@@ -513,25 +548,23 @@ void evict_inodes(struct super_block *sb)
513 struct inode *inode, *next; 548 struct inode *inode, *next;
514 LIST_HEAD(dispose); 549 LIST_HEAD(dispose);
515 550
516 spin_lock(&inode_lock); 551 spin_lock(&inode_sb_list_lock);
517 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { 552 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
518 if (atomic_read(&inode->i_count)) 553 if (atomic_read(&inode->i_count))
519 continue; 554 continue;
520 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) 555
556 spin_lock(&inode->i_lock);
557 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
558 spin_unlock(&inode->i_lock);
521 continue; 559 continue;
560 }
522 561
523 inode->i_state |= I_FREEING; 562 inode->i_state |= I_FREEING;
524 563 inode_lru_list_del(inode);
525 /* 564 spin_unlock(&inode->i_lock);
526 * Move the inode off the IO lists and LRU once I_FREEING is 565 list_add(&inode->i_lru, &dispose);
527 * set so that it won't get moved back on there if it is dirty.
528 */
529 list_move(&inode->i_lru, &dispose);
530 list_del_init(&inode->i_wb_list);
531 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
532 inodes_stat.nr_unused--;
533 } 566 }
534 spin_unlock(&inode_lock); 567 spin_unlock(&inode_sb_list_lock);
535 568
536 dispose_list(&dispose); 569 dispose_list(&dispose);
537 570
@@ -560,31 +593,30 @@ int invalidate_inodes(struct super_block *sb, bool kill_dirty)
560 struct inode *inode, *next; 593 struct inode *inode, *next;
561 LIST_HEAD(dispose); 594 LIST_HEAD(dispose);
562 595
563 spin_lock(&inode_lock); 596 spin_lock(&inode_sb_list_lock);
564 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { 597 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
565 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) 598 spin_lock(&inode->i_lock);
599 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
600 spin_unlock(&inode->i_lock);
566 continue; 601 continue;
602 }
567 if (inode->i_state & I_DIRTY && !kill_dirty) { 603 if (inode->i_state & I_DIRTY && !kill_dirty) {
604 spin_unlock(&inode->i_lock);
568 busy = 1; 605 busy = 1;
569 continue; 606 continue;
570 } 607 }
571 if (atomic_read(&inode->i_count)) { 608 if (atomic_read(&inode->i_count)) {
609 spin_unlock(&inode->i_lock);
572 busy = 1; 610 busy = 1;
573 continue; 611 continue;
574 } 612 }
575 613
576 inode->i_state |= I_FREEING; 614 inode->i_state |= I_FREEING;
577 615 inode_lru_list_del(inode);
578 /* 616 spin_unlock(&inode->i_lock);
579 * Move the inode off the IO lists and LRU once I_FREEING is 617 list_add(&inode->i_lru, &dispose);
580 * set so that it won't get moved back on there if it is dirty.
581 */
582 list_move(&inode->i_lru, &dispose);
583 list_del_init(&inode->i_wb_list);
584 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
585 inodes_stat.nr_unused--;
586 } 618 }
587 spin_unlock(&inode_lock); 619 spin_unlock(&inode_sb_list_lock);
588 620
589 dispose_list(&dispose); 621 dispose_list(&dispose);
590 622
@@ -606,7 +638,7 @@ static int can_unuse(struct inode *inode)
606 638
607/* 639/*
608 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a 640 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
609 * temporary list and then are freed outside inode_lock by dispose_list(). 641 * temporary list and then are freed outside inode_lru_lock by dispose_list().
610 * 642 *
611 * Any inodes which are pinned purely because of attached pagecache have their 643 * Any inodes which are pinned purely because of attached pagecache have their
612 * pagecache removed. If the inode has metadata buffers attached to 644 * pagecache removed. If the inode has metadata buffers attached to
@@ -627,7 +659,7 @@ static void prune_icache(int nr_to_scan)
627 unsigned long reap = 0; 659 unsigned long reap = 0;
628 660
629 down_read(&iprune_sem); 661 down_read(&iprune_sem);
630 spin_lock(&inode_lock); 662 spin_lock(&inode_lru_lock);
631 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) { 663 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
632 struct inode *inode; 664 struct inode *inode;
633 665
@@ -637,53 +669,67 @@ static void prune_icache(int nr_to_scan)
637 inode = list_entry(inode_lru.prev, struct inode, i_lru); 669 inode = list_entry(inode_lru.prev, struct inode, i_lru);
638 670
639 /* 671 /*
672 * we are inverting the inode_lru_lock/inode->i_lock here,
673 * so use a trylock. If we fail to get the lock, just move the
674 * inode to the back of the list so we don't spin on it.
675 */
676 if (!spin_trylock(&inode->i_lock)) {
677 list_move(&inode->i_lru, &inode_lru);
678 continue;
679 }
680
681 /*
640 * Referenced or dirty inodes are still in use. Give them 682 * Referenced or dirty inodes are still in use. Give them
641 * another pass through the LRU as we canot reclaim them now. 683 * another pass through the LRU as we canot reclaim them now.
642 */ 684 */
643 if (atomic_read(&inode->i_count) || 685 if (atomic_read(&inode->i_count) ||
644 (inode->i_state & ~I_REFERENCED)) { 686 (inode->i_state & ~I_REFERENCED)) {
645 list_del_init(&inode->i_lru); 687 list_del_init(&inode->i_lru);
688 spin_unlock(&inode->i_lock);
646 inodes_stat.nr_unused--; 689 inodes_stat.nr_unused--;
647 continue; 690 continue;
648 } 691 }
649 692
650 /* recently referenced inodes get one more pass */ 693 /* recently referenced inodes get one more pass */
651 if (inode->i_state & I_REFERENCED) { 694 if (inode->i_state & I_REFERENCED) {
652 list_move(&inode->i_lru, &inode_lru);
653 inode->i_state &= ~I_REFERENCED; 695 inode->i_state &= ~I_REFERENCED;
696 list_move(&inode->i_lru, &inode_lru);
697 spin_unlock(&inode->i_lock);
654 continue; 698 continue;
655 } 699 }
656 if (inode_has_buffers(inode) || inode->i_data.nrpages) { 700 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
657 __iget(inode); 701 __iget(inode);
658 spin_unlock(&inode_lock); 702 spin_unlock(&inode->i_lock);
703 spin_unlock(&inode_lru_lock);
659 if (remove_inode_buffers(inode)) 704 if (remove_inode_buffers(inode))
660 reap += invalidate_mapping_pages(&inode->i_data, 705 reap += invalidate_mapping_pages(&inode->i_data,
661 0, -1); 706 0, -1);
662 iput(inode); 707 iput(inode);
663 spin_lock(&inode_lock); 708 spin_lock(&inode_lru_lock);
664 709
665 if (inode != list_entry(inode_lru.next, 710 if (inode != list_entry(inode_lru.next,
666 struct inode, i_lru)) 711 struct inode, i_lru))
667 continue; /* wrong inode or list_empty */ 712 continue; /* wrong inode or list_empty */
668 if (!can_unuse(inode)) 713 /* avoid lock inversions with trylock */
714 if (!spin_trylock(&inode->i_lock))
669 continue; 715 continue;
716 if (!can_unuse(inode)) {
717 spin_unlock(&inode->i_lock);
718 continue;
719 }
670 } 720 }
671 WARN_ON(inode->i_state & I_NEW); 721 WARN_ON(inode->i_state & I_NEW);
672 inode->i_state |= I_FREEING; 722 inode->i_state |= I_FREEING;
723 spin_unlock(&inode->i_lock);
673 724
674 /*
675 * Move the inode off the IO lists and LRU once I_FREEING is
676 * set so that it won't get moved back on there if it is dirty.
677 */
678 list_move(&inode->i_lru, &freeable); 725 list_move(&inode->i_lru, &freeable);
679 list_del_init(&inode->i_wb_list);
680 inodes_stat.nr_unused--; 726 inodes_stat.nr_unused--;
681 } 727 }
682 if (current_is_kswapd()) 728 if (current_is_kswapd())
683 __count_vm_events(KSWAPD_INODESTEAL, reap); 729 __count_vm_events(KSWAPD_INODESTEAL, reap);
684 else 730 else
685 __count_vm_events(PGINODESTEAL, reap); 731 __count_vm_events(PGINODESTEAL, reap);
686 spin_unlock(&inode_lock); 732 spin_unlock(&inode_lru_lock);
687 733
688 dispose_list(&freeable); 734 dispose_list(&freeable);
689 up_read(&iprune_sem); 735 up_read(&iprune_sem);
@@ -732,15 +778,21 @@ static struct inode *find_inode(struct super_block *sb,
732 778
733repeat: 779repeat:
734 hlist_for_each_entry(inode, node, head, i_hash) { 780 hlist_for_each_entry(inode, node, head, i_hash) {
735 if (inode->i_sb != sb) 781 spin_lock(&inode->i_lock);
782 if (inode->i_sb != sb) {
783 spin_unlock(&inode->i_lock);
736 continue; 784 continue;
737 if (!test(inode, data)) 785 }
786 if (!test(inode, data)) {
787 spin_unlock(&inode->i_lock);
738 continue; 788 continue;
789 }
739 if (inode->i_state & (I_FREEING|I_WILL_FREE)) { 790 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
740 __wait_on_freeing_inode(inode); 791 __wait_on_freeing_inode(inode);
741 goto repeat; 792 goto repeat;
742 } 793 }
743 __iget(inode); 794 __iget(inode);
795 spin_unlock(&inode->i_lock);
744 return inode; 796 return inode;
745 } 797 }
746 return NULL; 798 return NULL;
@@ -758,15 +810,21 @@ static struct inode *find_inode_fast(struct super_block *sb,
758 810
759repeat: 811repeat:
760 hlist_for_each_entry(inode, node, head, i_hash) { 812 hlist_for_each_entry(inode, node, head, i_hash) {
761 if (inode->i_ino != ino) 813 spin_lock(&inode->i_lock);
814 if (inode->i_ino != ino) {
815 spin_unlock(&inode->i_lock);
762 continue; 816 continue;
763 if (inode->i_sb != sb) 817 }
818 if (inode->i_sb != sb) {
819 spin_unlock(&inode->i_lock);
764 continue; 820 continue;
821 }
765 if (inode->i_state & (I_FREEING|I_WILL_FREE)) { 822 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
766 __wait_on_freeing_inode(inode); 823 __wait_on_freeing_inode(inode);
767 goto repeat; 824 goto repeat;
768 } 825 }
769 __iget(inode); 826 __iget(inode);
827 spin_unlock(&inode->i_lock);
770 return inode; 828 return inode;
771 } 829 }
772 return NULL; 830 return NULL;
@@ -826,19 +884,26 @@ struct inode *new_inode(struct super_block *sb)
826{ 884{
827 struct inode *inode; 885 struct inode *inode;
828 886
829 spin_lock_prefetch(&inode_lock); 887 spin_lock_prefetch(&inode_sb_list_lock);
830 888
831 inode = alloc_inode(sb); 889 inode = alloc_inode(sb);
832 if (inode) { 890 if (inode) {
833 spin_lock(&inode_lock); 891 spin_lock(&inode->i_lock);
834 __inode_sb_list_add(inode);
835 inode->i_state = 0; 892 inode->i_state = 0;
836 spin_unlock(&inode_lock); 893 spin_unlock(&inode->i_lock);
894 inode_sb_list_add(inode);
837 } 895 }
838 return inode; 896 return inode;
839} 897}
840EXPORT_SYMBOL(new_inode); 898EXPORT_SYMBOL(new_inode);
841 899
900/**
901 * unlock_new_inode - clear the I_NEW state and wake up any waiters
902 * @inode: new inode to unlock
903 *
904 * Called when the inode is fully initialised to clear the new state of the
905 * inode and wake up anyone waiting for the inode to finish initialisation.
906 */
842void unlock_new_inode(struct inode *inode) 907void unlock_new_inode(struct inode *inode)
843{ 908{
844#ifdef CONFIG_DEBUG_LOCK_ALLOC 909#ifdef CONFIG_DEBUG_LOCK_ALLOC
@@ -858,51 +923,67 @@ void unlock_new_inode(struct inode *inode)
858 } 923 }
859 } 924 }
860#endif 925#endif
861 /* 926 spin_lock(&inode->i_lock);
862 * This is special! We do not need the spinlock when clearing I_NEW,
863 * because we're guaranteed that nobody else tries to do anything about
864 * the state of the inode when it is locked, as we just created it (so
865 * there can be no old holders that haven't tested I_NEW).
866 * However we must emit the memory barrier so that other CPUs reliably
867 * see the clearing of I_NEW after the other inode initialisation has
868 * completed.
869 */
870 smp_mb();
871 WARN_ON(!(inode->i_state & I_NEW)); 927 WARN_ON(!(inode->i_state & I_NEW));
872 inode->i_state &= ~I_NEW; 928 inode->i_state &= ~I_NEW;
873 wake_up_inode(inode); 929 wake_up_bit(&inode->i_state, __I_NEW);
930 spin_unlock(&inode->i_lock);
874} 931}
875EXPORT_SYMBOL(unlock_new_inode); 932EXPORT_SYMBOL(unlock_new_inode);
876 933
877/* 934/**
878 * This is called without the inode lock held.. Be careful. 935 * iget5_locked - obtain an inode from a mounted file system
936 * @sb: super block of file system
937 * @hashval: hash value (usually inode number) to get
938 * @test: callback used for comparisons between inodes
939 * @set: callback used to initialize a new struct inode
940 * @data: opaque data pointer to pass to @test and @set
941 *
942 * Search for the inode specified by @hashval and @data in the inode cache,
943 * and if present it is return it with an increased reference count. This is
944 * a generalized version of iget_locked() for file systems where the inode
945 * number is not sufficient for unique identification of an inode.
946 *
947 * If the inode is not in cache, allocate a new inode and return it locked,
948 * hashed, and with the I_NEW flag set. The file system gets to fill it in
949 * before unlocking it via unlock_new_inode().
879 * 950 *
880 * We no longer cache the sb_flags in i_flags - see fs.h 951 * Note both @test and @set are called with the inode_hash_lock held, so can't
881 * -- rmk@arm.uk.linux.org 952 * sleep.
882 */ 953 */
883static struct inode *get_new_inode(struct super_block *sb, 954struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
884 struct hlist_head *head, 955 int (*test)(struct inode *, void *),
885 int (*test)(struct inode *, void *), 956 int (*set)(struct inode *, void *), void *data)
886 int (*set)(struct inode *, void *),
887 void *data)
888{ 957{
958 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
889 struct inode *inode; 959 struct inode *inode;
890 960
961 spin_lock(&inode_hash_lock);
962 inode = find_inode(sb, head, test, data);
963 spin_unlock(&inode_hash_lock);
964
965 if (inode) {
966 wait_on_inode(inode);
967 return inode;
968 }
969
891 inode = alloc_inode(sb); 970 inode = alloc_inode(sb);
892 if (inode) { 971 if (inode) {
893 struct inode *old; 972 struct inode *old;
894 973
895 spin_lock(&inode_lock); 974 spin_lock(&inode_hash_lock);
896 /* We released the lock, so.. */ 975 /* We released the lock, so.. */
897 old = find_inode(sb, head, test, data); 976 old = find_inode(sb, head, test, data);
898 if (!old) { 977 if (!old) {
899 if (set(inode, data)) 978 if (set(inode, data))
900 goto set_failed; 979 goto set_failed;
901 980
902 hlist_add_head(&inode->i_hash, head); 981 spin_lock(&inode->i_lock);
903 __inode_sb_list_add(inode);
904 inode->i_state = I_NEW; 982 inode->i_state = I_NEW;
905 spin_unlock(&inode_lock); 983 hlist_add_head(&inode->i_hash, head);
984 spin_unlock(&inode->i_lock);
985 inode_sb_list_add(inode);
986 spin_unlock(&inode_hash_lock);
906 987
907 /* Return the locked inode with I_NEW set, the 988 /* Return the locked inode with I_NEW set, the
908 * caller is responsible for filling in the contents 989 * caller is responsible for filling in the contents
@@ -915,7 +996,7 @@ static struct inode *get_new_inode(struct super_block *sb,
915 * us. Use the old inode instead of the one we just 996 * us. Use the old inode instead of the one we just
916 * allocated. 997 * allocated.
917 */ 998 */
918 spin_unlock(&inode_lock); 999 spin_unlock(&inode_hash_lock);
919 destroy_inode(inode); 1000 destroy_inode(inode);
920 inode = old; 1001 inode = old;
921 wait_on_inode(inode); 1002 wait_on_inode(inode);
@@ -923,33 +1004,53 @@ static struct inode *get_new_inode(struct super_block *sb,
923 return inode; 1004 return inode;
924 1005
925set_failed: 1006set_failed:
926 spin_unlock(&inode_lock); 1007 spin_unlock(&inode_hash_lock);
927 destroy_inode(inode); 1008 destroy_inode(inode);
928 return NULL; 1009 return NULL;
929} 1010}
1011EXPORT_SYMBOL(iget5_locked);
930 1012
931/* 1013/**
932 * get_new_inode_fast is the fast path version of get_new_inode, see the 1014 * iget_locked - obtain an inode from a mounted file system
933 * comment at iget_locked for details. 1015 * @sb: super block of file system
1016 * @ino: inode number to get
1017 *
1018 * Search for the inode specified by @ino in the inode cache and if present
1019 * return it with an increased reference count. This is for file systems
1020 * where the inode number is sufficient for unique identification of an inode.
1021 *
1022 * If the inode is not in cache, allocate a new inode and return it locked,
1023 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1024 * before unlocking it via unlock_new_inode().
934 */ 1025 */
935static struct inode *get_new_inode_fast(struct super_block *sb, 1026struct inode *iget_locked(struct super_block *sb, unsigned long ino)
936 struct hlist_head *head, unsigned long ino)
937{ 1027{
1028 struct hlist_head *head = inode_hashtable + hash(sb, ino);
938 struct inode *inode; 1029 struct inode *inode;
939 1030
1031 spin_lock(&inode_hash_lock);
1032 inode = find_inode_fast(sb, head, ino);
1033 spin_unlock(&inode_hash_lock);
1034 if (inode) {
1035 wait_on_inode(inode);
1036 return inode;
1037 }
1038
940 inode = alloc_inode(sb); 1039 inode = alloc_inode(sb);
941 if (inode) { 1040 if (inode) {
942 struct inode *old; 1041 struct inode *old;
943 1042
944 spin_lock(&inode_lock); 1043 spin_lock(&inode_hash_lock);
945 /* We released the lock, so.. */ 1044 /* We released the lock, so.. */
946 old = find_inode_fast(sb, head, ino); 1045 old = find_inode_fast(sb, head, ino);
947 if (!old) { 1046 if (!old) {
948 inode->i_ino = ino; 1047 inode->i_ino = ino;
949 hlist_add_head(&inode->i_hash, head); 1048 spin_lock(&inode->i_lock);
950 __inode_sb_list_add(inode);
951 inode->i_state = I_NEW; 1049 inode->i_state = I_NEW;
952 spin_unlock(&inode_lock); 1050 hlist_add_head(&inode->i_hash, head);
1051 spin_unlock(&inode->i_lock);
1052 inode_sb_list_add(inode);
1053 spin_unlock(&inode_hash_lock);
953 1054
954 /* Return the locked inode with I_NEW set, the 1055 /* Return the locked inode with I_NEW set, the
955 * caller is responsible for filling in the contents 1056 * caller is responsible for filling in the contents
@@ -962,13 +1063,14 @@ static struct inode *get_new_inode_fast(struct super_block *sb,
962 * us. Use the old inode instead of the one we just 1063 * us. Use the old inode instead of the one we just
963 * allocated. 1064 * allocated.
964 */ 1065 */
965 spin_unlock(&inode_lock); 1066 spin_unlock(&inode_hash_lock);
966 destroy_inode(inode); 1067 destroy_inode(inode);
967 inode = old; 1068 inode = old;
968 wait_on_inode(inode); 1069 wait_on_inode(inode);
969 } 1070 }
970 return inode; 1071 return inode;
971} 1072}
1073EXPORT_SYMBOL(iget_locked);
972 1074
973/* 1075/*
974 * search the inode cache for a matching inode number. 1076 * search the inode cache for a matching inode number.
@@ -983,10 +1085,14 @@ static int test_inode_iunique(struct super_block *sb, unsigned long ino)
983 struct hlist_node *node; 1085 struct hlist_node *node;
984 struct inode *inode; 1086 struct inode *inode;
985 1087
1088 spin_lock(&inode_hash_lock);
986 hlist_for_each_entry(inode, node, b, i_hash) { 1089 hlist_for_each_entry(inode, node, b, i_hash) {
987 if (inode->i_ino == ino && inode->i_sb == sb) 1090 if (inode->i_ino == ino && inode->i_sb == sb) {
1091 spin_unlock(&inode_hash_lock);
988 return 0; 1092 return 0;
1093 }
989 } 1094 }
1095 spin_unlock(&inode_hash_lock);
990 1096
991 return 1; 1097 return 1;
992} 1098}
@@ -1016,7 +1122,6 @@ ino_t iunique(struct super_block *sb, ino_t max_reserved)
1016 static unsigned int counter; 1122 static unsigned int counter;
1017 ino_t res; 1123 ino_t res;
1018 1124
1019 spin_lock(&inode_lock);
1020 spin_lock(&iunique_lock); 1125 spin_lock(&iunique_lock);
1021 do { 1126 do {
1022 if (counter <= max_reserved) 1127 if (counter <= max_reserved)
@@ -1024,7 +1129,6 @@ ino_t iunique(struct super_block *sb, ino_t max_reserved)
1024 res = counter++; 1129 res = counter++;
1025 } while (!test_inode_iunique(sb, res)); 1130 } while (!test_inode_iunique(sb, res));
1026 spin_unlock(&iunique_lock); 1131 spin_unlock(&iunique_lock);
1027 spin_unlock(&inode_lock);
1028 1132
1029 return res; 1133 return res;
1030} 1134}
@@ -1032,116 +1136,50 @@ EXPORT_SYMBOL(iunique);
1032 1136
1033struct inode *igrab(struct inode *inode) 1137struct inode *igrab(struct inode *inode)
1034{ 1138{
1035 spin_lock(&inode_lock); 1139 spin_lock(&inode->i_lock);
1036 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) 1140 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
1037 __iget(inode); 1141 __iget(inode);
1038 else 1142 spin_unlock(&inode->i_lock);
1143 } else {
1144 spin_unlock(&inode->i_lock);
1039 /* 1145 /*
1040 * Handle the case where s_op->clear_inode is not been 1146 * Handle the case where s_op->clear_inode is not been
1041 * called yet, and somebody is calling igrab 1147 * called yet, and somebody is calling igrab
1042 * while the inode is getting freed. 1148 * while the inode is getting freed.
1043 */ 1149 */
1044 inode = NULL; 1150 inode = NULL;
1045 spin_unlock(&inode_lock); 1151 }
1046 return inode; 1152 return inode;
1047} 1153}
1048EXPORT_SYMBOL(igrab); 1154EXPORT_SYMBOL(igrab);
1049 1155
1050/** 1156/**
1051 * ifind - internal function, you want ilookup5() or iget5().
1052 * @sb: super block of file system to search
1053 * @head: the head of the list to search
1054 * @test: callback used for comparisons between inodes
1055 * @data: opaque data pointer to pass to @test
1056 * @wait: if true wait for the inode to be unlocked, if false do not
1057 *
1058 * ifind() searches for the inode specified by @data in the inode
1059 * cache. This is a generalized version of ifind_fast() for file systems where
1060 * the inode number is not sufficient for unique identification of an inode.
1061 *
1062 * If the inode is in the cache, the inode is returned with an incremented
1063 * reference count.
1064 *
1065 * Otherwise NULL is returned.
1066 *
1067 * Note, @test is called with the inode_lock held, so can't sleep.
1068 */
1069static struct inode *ifind(struct super_block *sb,
1070 struct hlist_head *head, int (*test)(struct inode *, void *),
1071 void *data, const int wait)
1072{
1073 struct inode *inode;
1074
1075 spin_lock(&inode_lock);
1076 inode = find_inode(sb, head, test, data);
1077 if (inode) {
1078 spin_unlock(&inode_lock);
1079 if (likely(wait))
1080 wait_on_inode(inode);
1081 return inode;
1082 }
1083 spin_unlock(&inode_lock);
1084 return NULL;
1085}
1086
1087/**
1088 * ifind_fast - internal function, you want ilookup() or iget().
1089 * @sb: super block of file system to search
1090 * @head: head of the list to search
1091 * @ino: inode number to search for
1092 *
1093 * ifind_fast() searches for the inode @ino in the inode cache. This is for
1094 * file systems where the inode number is sufficient for unique identification
1095 * of an inode.
1096 *
1097 * If the inode is in the cache, the inode is returned with an incremented
1098 * reference count.
1099 *
1100 * Otherwise NULL is returned.
1101 */
1102static struct inode *ifind_fast(struct super_block *sb,
1103 struct hlist_head *head, unsigned long ino)
1104{
1105 struct inode *inode;
1106
1107 spin_lock(&inode_lock);
1108 inode = find_inode_fast(sb, head, ino);
1109 if (inode) {
1110 spin_unlock(&inode_lock);
1111 wait_on_inode(inode);
1112 return inode;
1113 }
1114 spin_unlock(&inode_lock);
1115 return NULL;
1116}
1117
1118/**
1119 * ilookup5_nowait - search for an inode in the inode cache 1157 * ilookup5_nowait - search for an inode in the inode cache
1120 * @sb: super block of file system to search 1158 * @sb: super block of file system to search
1121 * @hashval: hash value (usually inode number) to search for 1159 * @hashval: hash value (usually inode number) to search for
1122 * @test: callback used for comparisons between inodes 1160 * @test: callback used for comparisons between inodes
1123 * @data: opaque data pointer to pass to @test 1161 * @data: opaque data pointer to pass to @test
1124 * 1162 *
1125 * ilookup5() uses ifind() to search for the inode specified by @hashval and 1163 * Search for the inode specified by @hashval and @data in the inode cache.
1126 * @data in the inode cache. This is a generalized version of ilookup() for
1127 * file systems where the inode number is not sufficient for unique
1128 * identification of an inode.
1129 *
1130 * If the inode is in the cache, the inode is returned with an incremented 1164 * If the inode is in the cache, the inode is returned with an incremented
1131 * reference count. Note, the inode lock is not waited upon so you have to be 1165 * reference count.
1132 * very careful what you do with the returned inode. You probably should be
1133 * using ilookup5() instead.
1134 * 1166 *
1135 * Otherwise NULL is returned. 1167 * Note: I_NEW is not waited upon so you have to be very careful what you do
1168 * with the returned inode. You probably should be using ilookup5() instead.
1136 * 1169 *
1137 * Note, @test is called with the inode_lock held, so can't sleep. 1170 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1138 */ 1171 */
1139struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval, 1172struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1140 int (*test)(struct inode *, void *), void *data) 1173 int (*test)(struct inode *, void *), void *data)
1141{ 1174{
1142 struct hlist_head *head = inode_hashtable + hash(sb, hashval); 1175 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1176 struct inode *inode;
1177
1178 spin_lock(&inode_hash_lock);
1179 inode = find_inode(sb, head, test, data);
1180 spin_unlock(&inode_hash_lock);
1143 1181
1144 return ifind(sb, head, test, data, 0); 1182 return inode;
1145} 1183}
1146EXPORT_SYMBOL(ilookup5_nowait); 1184EXPORT_SYMBOL(ilookup5_nowait);
1147 1185
@@ -1152,24 +1190,24 @@ EXPORT_SYMBOL(ilookup5_nowait);
1152 * @test: callback used for comparisons between inodes 1190 * @test: callback used for comparisons between inodes
1153 * @data: opaque data pointer to pass to @test 1191 * @data: opaque data pointer to pass to @test
1154 * 1192 *
1155 * ilookup5() uses ifind() to search for the inode specified by @hashval and 1193 * Search for the inode specified by @hashval and @data in the inode cache,
1156 * @data in the inode cache. This is a generalized version of ilookup() for 1194 * and if the inode is in the cache, return the inode with an incremented
1157 * file systems where the inode number is not sufficient for unique 1195 * reference count. Waits on I_NEW before returning the inode.
1158 * identification of an inode.
1159 *
1160 * If the inode is in the cache, the inode lock is waited upon and the inode is
1161 * returned with an incremented reference count. 1196 * returned with an incremented reference count.
1162 * 1197 *
1163 * Otherwise NULL is returned. 1198 * This is a generalized version of ilookup() for file systems where the
1199 * inode number is not sufficient for unique identification of an inode.
1164 * 1200 *
1165 * Note, @test is called with the inode_lock held, so can't sleep. 1201 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1166 */ 1202 */
1167struct inode *ilookup5(struct super_block *sb, unsigned long hashval, 1203struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1168 int (*test)(struct inode *, void *), void *data) 1204 int (*test)(struct inode *, void *), void *data)
1169{ 1205{
1170 struct hlist_head *head = inode_hashtable + hash(sb, hashval); 1206 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
1171 1207
1172 return ifind(sb, head, test, data, 1); 1208 if (inode)
1209 wait_on_inode(inode);
1210 return inode;
1173} 1211}
1174EXPORT_SYMBOL(ilookup5); 1212EXPORT_SYMBOL(ilookup5);
1175 1213
@@ -1178,91 +1216,23 @@ EXPORT_SYMBOL(ilookup5);
1178 * @sb: super block of file system to search 1216 * @sb: super block of file system to search
1179 * @ino: inode number to search for 1217 * @ino: inode number to search for
1180 * 1218 *
1181 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache. 1219 * Search for the inode @ino in the inode cache, and if the inode is in the
1182 * This is for file systems where the inode number is sufficient for unique 1220 * cache, the inode is returned with an incremented reference count.
1183 * identification of an inode.
1184 *
1185 * If the inode is in the cache, the inode is returned with an incremented
1186 * reference count.
1187 *
1188 * Otherwise NULL is returned.
1189 */ 1221 */
1190struct inode *ilookup(struct super_block *sb, unsigned long ino) 1222struct inode *ilookup(struct super_block *sb, unsigned long ino)
1191{ 1223{
1192 struct hlist_head *head = inode_hashtable + hash(sb, ino); 1224 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1193
1194 return ifind_fast(sb, head, ino);
1195}
1196EXPORT_SYMBOL(ilookup);
1197
1198/**
1199 * iget5_locked - obtain an inode from a mounted file system
1200 * @sb: super block of file system
1201 * @hashval: hash value (usually inode number) to get
1202 * @test: callback used for comparisons between inodes
1203 * @set: callback used to initialize a new struct inode
1204 * @data: opaque data pointer to pass to @test and @set
1205 *
1206 * iget5_locked() uses ifind() to search for the inode specified by @hashval
1207 * and @data in the inode cache and if present it is returned with an increased
1208 * reference count. This is a generalized version of iget_locked() for file
1209 * systems where the inode number is not sufficient for unique identification
1210 * of an inode.
1211 *
1212 * If the inode is not in cache, get_new_inode() is called to allocate a new
1213 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
1214 * file system gets to fill it in before unlocking it via unlock_new_inode().
1215 *
1216 * Note both @test and @set are called with the inode_lock held, so can't sleep.
1217 */
1218struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1219 int (*test)(struct inode *, void *),
1220 int (*set)(struct inode *, void *), void *data)
1221{
1222 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1223 struct inode *inode; 1225 struct inode *inode;
1224 1226
1225 inode = ifind(sb, head, test, data, 1); 1227 spin_lock(&inode_hash_lock);
1226 if (inode) 1228 inode = find_inode_fast(sb, head, ino);
1227 return inode; 1229 spin_unlock(&inode_hash_lock);
1228 /*
1229 * get_new_inode() will do the right thing, re-trying the search
1230 * in case it had to block at any point.
1231 */
1232 return get_new_inode(sb, head, test, set, data);
1233}
1234EXPORT_SYMBOL(iget5_locked);
1235
1236/**
1237 * iget_locked - obtain an inode from a mounted file system
1238 * @sb: super block of file system
1239 * @ino: inode number to get
1240 *
1241 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1242 * the inode cache and if present it is returned with an increased reference
1243 * count. This is for file systems where the inode number is sufficient for
1244 * unique identification of an inode.
1245 *
1246 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1247 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1248 * The file system gets to fill it in before unlocking it via
1249 * unlock_new_inode().
1250 */
1251struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1252{
1253 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1254 struct inode *inode;
1255 1230
1256 inode = ifind_fast(sb, head, ino);
1257 if (inode) 1231 if (inode)
1258 return inode; 1232 wait_on_inode(inode);
1259 /* 1233 return inode;
1260 * get_new_inode_fast() will do the right thing, re-trying the search
1261 * in case it had to block at any point.
1262 */
1263 return get_new_inode_fast(sb, head, ino);
1264} 1234}
1265EXPORT_SYMBOL(iget_locked); 1235EXPORT_SYMBOL(ilookup);
1266 1236
1267int insert_inode_locked(struct inode *inode) 1237int insert_inode_locked(struct inode *inode)
1268{ 1238{
@@ -1270,27 +1240,33 @@ int insert_inode_locked(struct inode *inode)
1270 ino_t ino = inode->i_ino; 1240 ino_t ino = inode->i_ino;
1271 struct hlist_head *head = inode_hashtable + hash(sb, ino); 1241 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1272 1242
1273 inode->i_state |= I_NEW;
1274 while (1) { 1243 while (1) {
1275 struct hlist_node *node; 1244 struct hlist_node *node;
1276 struct inode *old = NULL; 1245 struct inode *old = NULL;
1277 spin_lock(&inode_lock); 1246 spin_lock(&inode_hash_lock);
1278 hlist_for_each_entry(old, node, head, i_hash) { 1247 hlist_for_each_entry(old, node, head, i_hash) {
1279 if (old->i_ino != ino) 1248 if (old->i_ino != ino)
1280 continue; 1249 continue;
1281 if (old->i_sb != sb) 1250 if (old->i_sb != sb)
1282 continue; 1251 continue;
1283 if (old->i_state & (I_FREEING|I_WILL_FREE)) 1252 spin_lock(&old->i_lock);
1253 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1254 spin_unlock(&old->i_lock);
1284 continue; 1255 continue;
1256 }
1285 break; 1257 break;
1286 } 1258 }
1287 if (likely(!node)) { 1259 if (likely(!node)) {
1260 spin_lock(&inode->i_lock);
1261 inode->i_state |= I_NEW;
1288 hlist_add_head(&inode->i_hash, head); 1262 hlist_add_head(&inode->i_hash, head);
1289 spin_unlock(&inode_lock); 1263 spin_unlock(&inode->i_lock);
1264 spin_unlock(&inode_hash_lock);
1290 return 0; 1265 return 0;
1291 } 1266 }
1292 __iget(old); 1267 __iget(old);
1293 spin_unlock(&inode_lock); 1268 spin_unlock(&old->i_lock);
1269 spin_unlock(&inode_hash_lock);
1294 wait_on_inode(old); 1270 wait_on_inode(old);
1295 if (unlikely(!inode_unhashed(old))) { 1271 if (unlikely(!inode_unhashed(old))) {
1296 iput(old); 1272 iput(old);
@@ -1307,29 +1283,34 @@ int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1307 struct super_block *sb = inode->i_sb; 1283 struct super_block *sb = inode->i_sb;
1308 struct hlist_head *head = inode_hashtable + hash(sb, hashval); 1284 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1309 1285
1310 inode->i_state |= I_NEW;
1311
1312 while (1) { 1286 while (1) {
1313 struct hlist_node *node; 1287 struct hlist_node *node;
1314 struct inode *old = NULL; 1288 struct inode *old = NULL;
1315 1289
1316 spin_lock(&inode_lock); 1290 spin_lock(&inode_hash_lock);
1317 hlist_for_each_entry(old, node, head, i_hash) { 1291 hlist_for_each_entry(old, node, head, i_hash) {
1318 if (old->i_sb != sb) 1292 if (old->i_sb != sb)
1319 continue; 1293 continue;
1320 if (!test(old, data)) 1294 if (!test(old, data))
1321 continue; 1295 continue;
1322 if (old->i_state & (I_FREEING|I_WILL_FREE)) 1296 spin_lock(&old->i_lock);
1297 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1298 spin_unlock(&old->i_lock);
1323 continue; 1299 continue;
1300 }
1324 break; 1301 break;
1325 } 1302 }
1326 if (likely(!node)) { 1303 if (likely(!node)) {
1304 spin_lock(&inode->i_lock);
1305 inode->i_state |= I_NEW;
1327 hlist_add_head(&inode->i_hash, head); 1306 hlist_add_head(&inode->i_hash, head);
1328 spin_unlock(&inode_lock); 1307 spin_unlock(&inode->i_lock);
1308 spin_unlock(&inode_hash_lock);
1329 return 0; 1309 return 0;
1330 } 1310 }
1331 __iget(old); 1311 __iget(old);
1332 spin_unlock(&inode_lock); 1312 spin_unlock(&old->i_lock);
1313 spin_unlock(&inode_hash_lock);
1333 wait_on_inode(old); 1314 wait_on_inode(old);
1334 if (unlikely(!inode_unhashed(old))) { 1315 if (unlikely(!inode_unhashed(old))) {
1335 iput(old); 1316 iput(old);
@@ -1374,47 +1355,35 @@ static void iput_final(struct inode *inode)
1374 const struct super_operations *op = inode->i_sb->s_op; 1355 const struct super_operations *op = inode->i_sb->s_op;
1375 int drop; 1356 int drop;
1376 1357
1358 WARN_ON(inode->i_state & I_NEW);
1359
1377 if (op && op->drop_inode) 1360 if (op && op->drop_inode)
1378 drop = op->drop_inode(inode); 1361 drop = op->drop_inode(inode);
1379 else 1362 else
1380 drop = generic_drop_inode(inode); 1363 drop = generic_drop_inode(inode);
1381 1364
1365 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1366 inode->i_state |= I_REFERENCED;
1367 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1368 inode_lru_list_add(inode);
1369 spin_unlock(&inode->i_lock);
1370 return;
1371 }
1372
1382 if (!drop) { 1373 if (!drop) {
1383 if (sb->s_flags & MS_ACTIVE) {
1384 inode->i_state |= I_REFERENCED;
1385 if (!(inode->i_state & (I_DIRTY|I_SYNC))) {
1386 inode_lru_list_add(inode);
1387 }
1388 spin_unlock(&inode_lock);
1389 return;
1390 }
1391 WARN_ON(inode->i_state & I_NEW);
1392 inode->i_state |= I_WILL_FREE; 1374 inode->i_state |= I_WILL_FREE;
1393 spin_unlock(&inode_lock); 1375 spin_unlock(&inode->i_lock);
1394 write_inode_now(inode, 1); 1376 write_inode_now(inode, 1);
1395 spin_lock(&inode_lock); 1377 spin_lock(&inode->i_lock);
1396 WARN_ON(inode->i_state & I_NEW); 1378 WARN_ON(inode->i_state & I_NEW);
1397 inode->i_state &= ~I_WILL_FREE; 1379 inode->i_state &= ~I_WILL_FREE;
1398 __remove_inode_hash(inode);
1399 } 1380 }
1400 1381
1401 WARN_ON(inode->i_state & I_NEW);
1402 inode->i_state |= I_FREEING; 1382 inode->i_state |= I_FREEING;
1403
1404 /*
1405 * Move the inode off the IO lists and LRU once I_FREEING is
1406 * set so that it won't get moved back on there if it is dirty.
1407 */
1408 inode_lru_list_del(inode); 1383 inode_lru_list_del(inode);
1409 list_del_init(&inode->i_wb_list); 1384 spin_unlock(&inode->i_lock);
1410 1385
1411 __inode_sb_list_del(inode);
1412 spin_unlock(&inode_lock);
1413 evict(inode); 1386 evict(inode);
1414 remove_inode_hash(inode);
1415 wake_up_inode(inode);
1416 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
1417 destroy_inode(inode);
1418} 1387}
1419 1388
1420/** 1389/**
@@ -1431,7 +1400,7 @@ void iput(struct inode *inode)
1431 if (inode) { 1400 if (inode) {
1432 BUG_ON(inode->i_state & I_CLEAR); 1401 BUG_ON(inode->i_state & I_CLEAR);
1433 1402
1434 if (atomic_dec_and_lock(&inode->i_count, &inode_lock)) 1403 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
1435 iput_final(inode); 1404 iput_final(inode);
1436 } 1405 }
1437} 1406}
@@ -1610,9 +1579,8 @@ EXPORT_SYMBOL(inode_wait);
1610 * to recheck inode state. 1579 * to recheck inode state.
1611 * 1580 *
1612 * It doesn't matter if I_NEW is not set initially, a call to 1581 * It doesn't matter if I_NEW is not set initially, a call to
1613 * wake_up_inode() after removing from the hash list will DTRT. 1582 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1614 * 1583 * will DTRT.
1615 * This is called with inode_lock held.
1616 */ 1584 */
1617static void __wait_on_freeing_inode(struct inode *inode) 1585static void __wait_on_freeing_inode(struct inode *inode)
1618{ 1586{
@@ -1620,10 +1588,11 @@ static void __wait_on_freeing_inode(struct inode *inode)
1620 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW); 1588 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1621 wq = bit_waitqueue(&inode->i_state, __I_NEW); 1589 wq = bit_waitqueue(&inode->i_state, __I_NEW);
1622 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); 1590 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1623 spin_unlock(&inode_lock); 1591 spin_unlock(&inode->i_lock);
1592 spin_unlock(&inode_hash_lock);
1624 schedule(); 1593 schedule();
1625 finish_wait(wq, &wait.wait); 1594 finish_wait(wq, &wait.wait);
1626 spin_lock(&inode_lock); 1595 spin_lock(&inode_hash_lock);
1627} 1596}
1628 1597
1629static __initdata unsigned long ihash_entries; 1598static __initdata unsigned long ihash_entries;
@@ -1733,3 +1702,22 @@ void inode_init_owner(struct inode *inode, const struct inode *dir,
1733 inode->i_mode = mode; 1702 inode->i_mode = mode;
1734} 1703}
1735EXPORT_SYMBOL(inode_init_owner); 1704EXPORT_SYMBOL(inode_init_owner);
1705
1706/**
1707 * inode_owner_or_capable - check current task permissions to inode
1708 * @inode: inode being checked
1709 *
1710 * Return true if current either has CAP_FOWNER to the inode, or
1711 * owns the file.
1712 */
1713bool inode_owner_or_capable(const struct inode *inode)
1714{
1715 struct user_namespace *ns = inode_userns(inode);
1716
1717 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1718 return true;
1719 if (ns_capable(ns, CAP_FOWNER))
1720 return true;
1721 return false;
1722}
1723EXPORT_SYMBOL(inode_owner_or_capable);