~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to fs/inode.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * linux/fs/inode.c
3
 
 *
4
2
 * (C) 1997 Linus Torvalds
 
3
 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
5
4
 */
6
 
 
7
5
#include <linux/fs.h>
8
6
#include <linux/mm.h>
9
7
#include <linux/dcache.h>
24
22
#include <linux/mount.h>
25
23
#include <linux/async.h>
26
24
#include <linux/posix_acl.h>
 
25
#include <linux/prefetch.h>
27
26
#include <linux/ima.h>
28
 
 
29
 
/*
30
 
 * This is needed for the following functions:
31
 
 *  - inode_has_buffers
32
 
 *  - invalidate_bdev
33
 
 *
34
 
 * FIXME: remove all knowledge of the buffer layer from this file
35
 
 */
36
 
#include <linux/buffer_head.h>
37
 
 
38
 
/*
39
 
 * New inode.c implementation.
40
 
 *
41
 
 * This implementation has the basic premise of trying
42
 
 * to be extremely low-overhead and SMP-safe, yet be
43
 
 * simple enough to be "obviously correct".
44
 
 *
45
 
 * Famous last words.
46
 
 */
47
 
 
48
 
/* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
49
 
 
50
 
/* #define INODE_PARANOIA 1 */
51
 
/* #define INODE_DEBUG 1 */
52
 
 
53
 
/*
54
 
 * Inode lookup is no longer as critical as it used to be:
55
 
 * most of the lookups are going to be through the dcache.
56
 
 */
57
 
#define I_HASHBITS      i_hash_shift
58
 
#define I_HASHMASK      i_hash_mask
 
27
#include <linux/cred.h>
 
28
#include <linux/buffer_head.h> /* for inode_has_buffers */
 
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
 */
59
61
 
60
62
static unsigned int i_hash_mask __read_mostly;
61
63
static unsigned int i_hash_shift __read_mostly;
62
 
 
63
 
/*
64
 
 * Each inode can be on two separate lists. One is
65
 
 * the hash list of the inode, used for lookups. The
66
 
 * other linked list is the "type" list:
67
 
 *  "in_use" - valid inode, i_count > 0, i_nlink > 0
68
 
 *  "dirty"  - as "in_use" but also dirty
69
 
 *  "unused" - valid inode, i_count = 0
70
 
 *
71
 
 * A "dirty" list is maintained for each super block,
72
 
 * allowing for low-overhead inode sync() operations.
73
 
 */
 
64
static struct hlist_head *inode_hashtable __read_mostly;
 
65
static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
74
66
 
75
67
static LIST_HEAD(inode_lru);
76
 
static struct hlist_head *inode_hashtable __read_mostly;
77
 
 
78
 
/*
79
 
 * A simple spinlock to protect the list manipulations.
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
 
 */
84
 
DEFINE_SPINLOCK(inode_lock);
85
 
EXPORT_SYMBOL(inode_lock);
86
 
 
87
 
/*
88
 
 * iprune_sem provides exclusion between the kswapd or try_to_free_pages
89
 
 * icache shrinking path, and the umount path.  Without this exclusion,
90
 
 * by the time prune_icache calls iput for the inode whose pages it has
91
 
 * been invalidating, or by the time it calls clear_inode & destroy_inode
92
 
 * from its final dispose_list, the struct super_block they refer to
93
 
 * (for inode->i_sb->s_op) may already have been freed and reused.
94
 
 *
95
 
 * We make this an rwsem because the fastpath is icache shrinking. In
96
 
 * some cases a filesystem may be doing a significant amount of work in
97
 
 * its inode reclaim code, so this should improve parallelism.
 
68
static DEFINE_SPINLOCK(inode_lru_lock);
 
69
 
 
70
__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
 
71
__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
 
72
EXPORT_SYMBOL(inode_sb_list_lock);
 
73
 
 
74
/*
 
75
 * iprune_sem provides exclusion between the icache shrinking and the
 
76
 * umount path.
 
77
 *
 
78
 * We don't actually need it to protect anything in the umount path,
 
79
 * but only need to cycle through it to make sure any inode that
 
80
 * prune_icache took off the LRU list has been fully torn down by the
 
81
 * time we are past evict_inodes.
98
82
 */
99
83
static DECLARE_RWSEM(iprune_sem);
100
84
 
101
85
/*
 
86
 * Empty aops. Can be used for the cases where the user does not
 
87
 * define any of the address_space operations.
 
88
 */
 
89
const struct address_space_operations empty_aops = {
 
90
};
 
91
EXPORT_SYMBOL(empty_aops);
 
92
 
 
93
/*
102
94
 * Statistics gathering..
103
95
 */
104
96
struct inodes_stat_t inodes_stat;
140
132
}
141
133
#endif
142
134
 
143
 
static void wake_up_inode(struct inode *inode)
144
 
{
145
 
        /*
146
 
         * Prevent speculative execution through spin_unlock(&inode_lock);
147
 
         */
148
 
        smp_mb();
149
 
        wake_up_bit(&inode->i_state, __I_NEW);
150
 
}
151
 
 
152
135
/**
153
136
 * inode_init_always - perform inode structure intialisation
154
137
 * @sb: superblock inode belongs to
159
142
 */
160
143
int inode_init_always(struct super_block *sb, struct inode *inode)
161
144
{
162
 
        static const struct address_space_operations empty_aops;
163
145
        static const struct inode_operations empty_iops;
164
146
        static const struct file_operations empty_fops;
165
147
        struct address_space *const mapping = &inode->i_data;
301
283
        memset(mapping, 0, sizeof(*mapping));
302
284
        INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
303
285
        spin_lock_init(&mapping->tree_lock);
304
 
        spin_lock_init(&mapping->i_mmap_lock);
 
286
        mutex_init(&mapping->i_mmap_mutex);
305
287
        INIT_LIST_HEAD(&mapping->private_list);
306
288
        spin_lock_init(&mapping->private_lock);
307
289
        INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
308
290
        INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
309
 
        mutex_init(&mapping->unmap_mutex);
310
291
}
311
292
EXPORT_SYMBOL(address_space_init_once);
312
293
 
339
320
}
340
321
 
341
322
/*
342
 
 * inode_lock must be held
 
323
 * inode->i_lock must be held
343
324
 */
344
325
void __iget(struct inode *inode)
345
326
{
357
338
 
358
339
static void inode_lru_list_add(struct inode *inode)
359
340
{
 
341
        spin_lock(&inode_lru_lock);
360
342
        if (list_empty(&inode->i_lru)) {
361
343
                list_add(&inode->i_lru, &inode_lru);
362
344
                inodes_stat.nr_unused++;
363
345
        }
 
346
        spin_unlock(&inode_lru_lock);
364
347
}
365
348
 
366
349
static void inode_lru_list_del(struct inode *inode)
367
350
{
 
351
        spin_lock(&inode_lru_lock);
368
352
        if (!list_empty(&inode->i_lru)) {
369
353
                list_del_init(&inode->i_lru);
370
354
                inodes_stat.nr_unused--;
371
355
        }
372
 
}
373
 
 
374
 
static inline void __inode_sb_list_add(struct inode *inode)
375
 
{
376
 
        list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
 
356
        spin_unlock(&inode_lru_lock);
377
357
}
378
358
 
379
359
/**
382
362
 */
383
363
void inode_sb_list_add(struct inode *inode)
384
364
{
385
 
        spin_lock(&inode_lock);
386
 
        __inode_sb_list_add(inode);
387
 
        spin_unlock(&inode_lock);
 
365
        spin_lock(&inode_sb_list_lock);
 
366
        list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
 
367
        spin_unlock(&inode_sb_list_lock);
388
368
}
389
369
EXPORT_SYMBOL_GPL(inode_sb_list_add);
390
370
 
391
 
static inline void __inode_sb_list_del(struct inode *inode)
 
371
static inline void inode_sb_list_del(struct inode *inode)
392
372
{
 
373
        spin_lock(&inode_sb_list_lock);
393
374
        list_del_init(&inode->i_sb_list);
 
375
        spin_unlock(&inode_sb_list_lock);
394
376
}
395
377
 
396
378
static unsigned long hash(struct super_block *sb, unsigned long hashval)
399
381
 
400
382
        tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
401
383
                        L1_CACHE_BYTES;
402
 
        tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
403
 
        return tmp & I_HASHMASK;
 
384
        tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
 
385
        return tmp & i_hash_mask;
404
386
}
405
387
 
406
388
/**
415
397
{
416
398
        struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
417
399
 
418
 
        spin_lock(&inode_lock);
 
400
        spin_lock(&inode_hash_lock);
 
401
        spin_lock(&inode->i_lock);
419
402
        hlist_add_head(&inode->i_hash, b);
420
 
        spin_unlock(&inode_lock);
 
403
        spin_unlock(&inode->i_lock);
 
404
        spin_unlock(&inode_hash_lock);
421
405
}
422
406
EXPORT_SYMBOL(__insert_inode_hash);
423
407
 
424
408
/**
425
 
 *      __remove_inode_hash - remove an inode from the hash
426
 
 *      @inode: inode to unhash
427
 
 *
428
 
 *      Remove an inode from the superblock.
429
 
 */
430
 
static void __remove_inode_hash(struct inode *inode)
431
 
{
432
 
        hlist_del_init(&inode->i_hash);
433
 
}
434
 
 
435
 
/**
436
409
 *      remove_inode_hash - remove an inode from the hash
437
410
 *      @inode: inode to unhash
438
411
 *
440
413
 */
441
414
void remove_inode_hash(struct inode *inode)
442
415
{
443
 
        spin_lock(&inode_lock);
 
416
        spin_lock(&inode_hash_lock);
 
417
        spin_lock(&inode->i_lock);
444
418
        hlist_del_init(&inode->i_hash);
445
 
        spin_unlock(&inode_lock);
 
419
        spin_unlock(&inode->i_lock);
 
420
        spin_unlock(&inode_hash_lock);
446
421
}
447
422
EXPORT_SYMBOL(remove_inode_hash);
448
423
 
449
424
void end_writeback(struct inode *inode)
450
425
{
451
426
        might_sleep();
 
427
        /*
 
428
         * We have to cycle tree_lock here because reclaim can be still in the
 
429
         * process of removing the last page (in __delete_from_page_cache())
 
430
         * and we must not free mapping under it.
 
431
         */
 
432
        spin_lock_irq(&inode->i_data.tree_lock);
452
433
        BUG_ON(inode->i_data.nrpages);
 
434
        spin_unlock_irq(&inode->i_data.tree_lock);
453
435
        BUG_ON(!list_empty(&inode->i_data.private_list));
454
436
        BUG_ON(!(inode->i_state & I_FREEING));
455
437
        BUG_ON(inode->i_state & I_CLEAR);
459
441
}
460
442
EXPORT_SYMBOL(end_writeback);
461
443
 
 
444
/*
 
445
 * Free the inode passed in, removing it from the lists it is still connected
 
446
 * to. We remove any pages still attached to the inode and wait for any IO that
 
447
 * is still in progress before finally destroying the inode.
 
448
 *
 
449
 * An inode must already be marked I_FREEING so that we avoid the inode being
 
450
 * moved back onto lists if we race with other code that manipulates the lists
 
451
 * (e.g. writeback_single_inode). The caller is responsible for setting this.
 
452
 *
 
453
 * An inode must already be removed from the LRU list before being evicted from
 
454
 * the cache. This should occur atomically with setting the I_FREEING state
 
455
 * flag, so no inodes here should ever be on the LRU when being evicted.
 
456
 */
462
457
static void evict(struct inode *inode)
463
458
{
464
459
        const struct super_operations *op = inode->i_sb->s_op;
465
460
 
 
461
        BUG_ON(!(inode->i_state & I_FREEING));
 
462
        BUG_ON(!list_empty(&inode->i_lru));
 
463
 
 
464
        inode_wb_list_del(inode);
 
465
        inode_sb_list_del(inode);
 
466
 
466
467
        if (op->evict_inode) {
467
468
                op->evict_inode(inode);
468
469
        } else {
474
475
                bd_forget(inode);
475
476
        if (S_ISCHR(inode->i_mode) && inode->i_cdev)
476
477
                cd_forget(inode);
 
478
 
 
479
        remove_inode_hash(inode);
 
480
 
 
481
        spin_lock(&inode->i_lock);
 
482
        wake_up_bit(&inode->i_state, __I_NEW);
 
483
        BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
 
484
        spin_unlock(&inode->i_lock);
 
485
 
 
486
        destroy_inode(inode);
477
487
}
478
488
 
479
489
/*
492
502
                list_del_init(&inode->i_lru);
493
503
 
494
504
                evict(inode);
495
 
 
496
 
                spin_lock(&inode_lock);
497
 
                __remove_inode_hash(inode);
498
 
                __inode_sb_list_del(inode);
499
 
                spin_unlock(&inode_lock);
500
 
 
501
 
                wake_up_inode(inode);
502
 
                destroy_inode(inode);
503
505
        }
504
506
}
505
507
 
517
519
        struct inode *inode, *next;
518
520
        LIST_HEAD(dispose);
519
521
 
520
 
        down_write(&iprune_sem);
521
 
 
522
 
        spin_lock(&inode_lock);
 
522
        spin_lock(&inode_sb_list_lock);
523
523
        list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
524
524
                if (atomic_read(&inode->i_count))
525
525
                        continue;
526
526
 
 
527
                spin_lock(&inode->i_lock);
527
528
                if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
528
 
                        WARN_ON(1);
 
529
                        spin_unlock(&inode->i_lock);
529
530
                        continue;
530
531
                }
531
532
 
532
533
                inode->i_state |= I_FREEING;
533
 
 
534
 
                /*
535
 
                 * Move the inode off the IO lists and LRU once I_FREEING is
536
 
                 * set so that it won't get moved back on there if it is dirty.
537
 
                 */
538
 
                list_move(&inode->i_lru, &dispose);
539
 
                list_del_init(&inode->i_wb_list);
540
 
                if (!(inode->i_state & (I_DIRTY | I_SYNC)))
541
 
                        inodes_stat.nr_unused--;
 
534
                inode_lru_list_del(inode);
 
535
                spin_unlock(&inode->i_lock);
 
536
                list_add(&inode->i_lru, &dispose);
542
537
        }
543
 
        spin_unlock(&inode_lock);
 
538
        spin_unlock(&inode_sb_list_lock);
544
539
 
545
540
        dispose_list(&dispose);
 
541
 
 
542
        /*
 
543
         * Cycle through iprune_sem to make sure any inode that prune_icache
 
544
         * moved off the list before we took the lock has been fully torn
 
545
         * down.
 
546
         */
 
547
        down_write(&iprune_sem);
546
548
        up_write(&iprune_sem);
547
549
}
548
550
 
562
564
        struct inode *inode, *next;
563
565
        LIST_HEAD(dispose);
564
566
 
565
 
        down_write(&iprune_sem);
566
 
 
567
 
        spin_lock(&inode_lock);
 
567
        spin_lock(&inode_sb_list_lock);
568
568
        list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
569
 
                if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE))
 
569
                spin_lock(&inode->i_lock);
 
570
                if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
 
571
                        spin_unlock(&inode->i_lock);
570
572
                        continue;
 
573
                }
571
574
                if (inode->i_state & I_DIRTY && !kill_dirty) {
 
575
                        spin_unlock(&inode->i_lock);
572
576
                        busy = 1;
573
577
                        continue;
574
578
                }
575
579
                if (atomic_read(&inode->i_count)) {
 
580
                        spin_unlock(&inode->i_lock);
576
581
                        busy = 1;
577
582
                        continue;
578
583
                }
579
584
 
580
585
                inode->i_state |= I_FREEING;
581
 
 
582
 
                /*
583
 
                 * Move the inode off the IO lists and LRU once I_FREEING is
584
 
                 * set so that it won't get moved back on there if it is dirty.
585
 
                 */
586
 
                list_move(&inode->i_lru, &dispose);
587
 
                list_del_init(&inode->i_wb_list);
588
 
                if (!(inode->i_state & (I_DIRTY | I_SYNC)))
589
 
                        inodes_stat.nr_unused--;
 
586
                inode_lru_list_del(inode);
 
587
                spin_unlock(&inode->i_lock);
 
588
                list_add(&inode->i_lru, &dispose);
590
589
        }
591
 
        spin_unlock(&inode_lock);
 
590
        spin_unlock(&inode_sb_list_lock);
592
591
 
593
592
        dispose_list(&dispose);
594
 
        up_write(&iprune_sem);
595
593
 
596
594
        return busy;
597
595
}
611
609
 
612
610
/*
613
611
 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
614
 
 * temporary list and then are freed outside inode_lock by dispose_list().
 
612
 * temporary list and then are freed outside inode_lru_lock by dispose_list().
615
613
 *
616
614
 * Any inodes which are pinned purely because of attached pagecache have their
617
615
 * pagecache removed.  If the inode has metadata buffers attached to
632
630
        unsigned long reap = 0;
633
631
 
634
632
        down_read(&iprune_sem);
635
 
        spin_lock(&inode_lock);
 
633
        spin_lock(&inode_lru_lock);
636
634
        for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
637
635
                struct inode *inode;
638
636
 
642
640
                inode = list_entry(inode_lru.prev, struct inode, i_lru);
643
641
 
644
642
                /*
 
643
                 * we are inverting the inode_lru_lock/inode->i_lock here,
 
644
                 * so use a trylock. If we fail to get the lock, just move the
 
645
                 * inode to the back of the list so we don't spin on it.
 
646
                 */
 
647
                if (!spin_trylock(&inode->i_lock)) {
 
648
                        list_move(&inode->i_lru, &inode_lru);
 
649
                        continue;
 
650
                }
 
651
 
 
652
                /*
645
653
                 * Referenced or dirty inodes are still in use. Give them
646
654
                 * another pass through the LRU as we canot reclaim them now.
647
655
                 */
648
656
                if (atomic_read(&inode->i_count) ||
649
657
                    (inode->i_state & ~I_REFERENCED)) {
650
658
                        list_del_init(&inode->i_lru);
 
659
                        spin_unlock(&inode->i_lock);
651
660
                        inodes_stat.nr_unused--;
652
661
                        continue;
653
662
                }
654
663
 
655
664
                /* recently referenced inodes get one more pass */
656
665
                if (inode->i_state & I_REFERENCED) {
657
 
                        list_move(&inode->i_lru, &inode_lru);
658
666
                        inode->i_state &= ~I_REFERENCED;
 
667
                        list_move(&inode->i_lru, &inode_lru);
 
668
                        spin_unlock(&inode->i_lock);
659
669
                        continue;
660
670
                }
661
671
                if (inode_has_buffers(inode) || inode->i_data.nrpages) {
662
672
                        __iget(inode);
663
 
                        spin_unlock(&inode_lock);
 
673
                        spin_unlock(&inode->i_lock);
 
674
                        spin_unlock(&inode_lru_lock);
664
675
                        if (remove_inode_buffers(inode))
665
676
                                reap += invalidate_mapping_pages(&inode->i_data,
666
677
                                                                0, -1);
667
678
                        iput(inode);
668
 
                        spin_lock(&inode_lock);
 
679
                        spin_lock(&inode_lru_lock);
669
680
 
670
681
                        if (inode != list_entry(inode_lru.next,
671
682
                                                struct inode, i_lru))
672
683
                                continue;       /* wrong inode or list_empty */
673
 
                        if (!can_unuse(inode))
674
 
                                continue;
 
684
                        /* avoid lock inversions with trylock */
 
685
                        if (!spin_trylock(&inode->i_lock))
 
686
                                continue;
 
687
                        if (!can_unuse(inode)) {
 
688
                                spin_unlock(&inode->i_lock);
 
689
                                continue;
 
690
                        }
675
691
                }
676
692
                WARN_ON(inode->i_state & I_NEW);
677
693
                inode->i_state |= I_FREEING;
 
694
                spin_unlock(&inode->i_lock);
678
695
 
679
 
                /*
680
 
                 * Move the inode off the IO lists and LRU once I_FREEING is
681
 
                 * set so that it won't get moved back on there if it is dirty.
682
 
                 */
683
696
                list_move(&inode->i_lru, &freeable);
684
 
                list_del_init(&inode->i_wb_list);
685
697
                inodes_stat.nr_unused--;
686
698
        }
687
699
        if (current_is_kswapd())
688
700
                __count_vm_events(KSWAPD_INODESTEAL, reap);
689
701
        else
690
702
                __count_vm_events(PGINODESTEAL, reap);
691
 
        spin_unlock(&inode_lock);
 
703
        spin_unlock(&inode_lru_lock);
692
704
 
693
705
        dispose_list(&freeable);
694
706
        up_read(&iprune_sem);
703
715
 * This function is passed the number of inodes to scan, and it returns the
704
716
 * total number of remaining possibly-reclaimable inodes.
705
717
 */
706
 
static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
 
718
static int shrink_icache_memory(struct shrinker *shrink,
 
719
                                struct shrink_control *sc)
707
720
{
 
721
        int nr = sc->nr_to_scan;
 
722
        gfp_t gfp_mask = sc->gfp_mask;
 
723
 
708
724
        if (nr) {
709
725
                /*
710
726
                 * Nasty deadlock avoidance.  We may hold various FS locks,
737
753
 
738
754
repeat:
739
755
        hlist_for_each_entry(inode, node, head, i_hash) {
740
 
                if (inode->i_sb != sb)
741
 
                        continue;
742
 
                if (!test(inode, data))
743
 
                        continue;
 
756
                spin_lock(&inode->i_lock);
 
757
                if (inode->i_sb != sb) {
 
758
                        spin_unlock(&inode->i_lock);
 
759
                        continue;
 
760
                }
 
761
                if (!test(inode, data)) {
 
762
                        spin_unlock(&inode->i_lock);
 
763
                        continue;
 
764
                }
744
765
                if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
745
766
                        __wait_on_freeing_inode(inode);
746
767
                        goto repeat;
747
768
                }
748
769
                __iget(inode);
 
770
                spin_unlock(&inode->i_lock);
749
771
                return inode;
750
772
        }
751
773
        return NULL;
763
785
 
764
786
repeat:
765
787
        hlist_for_each_entry(inode, node, head, i_hash) {
766
 
                if (inode->i_ino != ino)
767
 
                        continue;
768
 
                if (inode->i_sb != sb)
769
 
                        continue;
 
788
                spin_lock(&inode->i_lock);
 
789
                if (inode->i_ino != ino) {
 
790
                        spin_unlock(&inode->i_lock);
 
791
                        continue;
 
792
                }
 
793
                if (inode->i_sb != sb) {
 
794
                        spin_unlock(&inode->i_lock);
 
795
                        continue;
 
796
                }
770
797
                if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
771
798
                        __wait_on_freeing_inode(inode);
772
799
                        goto repeat;
773
800
                }
774
801
                __iget(inode);
 
802
                spin_unlock(&inode->i_lock);
775
803
                return inode;
776
804
        }
777
805
        return NULL;
831
859
{
832
860
        struct inode *inode;
833
861
 
834
 
        spin_lock_prefetch(&inode_lock);
 
862
        spin_lock_prefetch(&inode_sb_list_lock);
835
863
 
836
864
        inode = alloc_inode(sb);
837
865
        if (inode) {
838
 
                spin_lock(&inode_lock);
839
 
                __inode_sb_list_add(inode);
 
866
                spin_lock(&inode->i_lock);
840
867
                inode->i_state = 0;
841
 
                spin_unlock(&inode_lock);
 
868
                spin_unlock(&inode->i_lock);
 
869
                inode_sb_list_add(inode);
842
870
        }
843
871
        return inode;
844
872
}
845
873
EXPORT_SYMBOL(new_inode);
846
874
 
 
875
/**
 
876
 * unlock_new_inode - clear the I_NEW state and wake up any waiters
 
877
 * @inode:      new inode to unlock
 
878
 *
 
879
 * Called when the inode is fully initialised to clear the new state of the
 
880
 * inode and wake up anyone waiting for the inode to finish initialisation.
 
881
 */
847
882
void unlock_new_inode(struct inode *inode)
848
883
{
849
884
#ifdef CONFIG_DEBUG_LOCK_ALLOC
863
898
                }
864
899
        }
865
900
#endif
866
 
        /*
867
 
         * This is special!  We do not need the spinlock when clearing I_NEW,
868
 
         * because we're guaranteed that nobody else tries to do anything about
869
 
         * the state of the inode when it is locked, as we just created it (so
870
 
         * there can be no old holders that haven't tested I_NEW).
871
 
         * However we must emit the memory barrier so that other CPUs reliably
872
 
         * see the clearing of I_NEW after the other inode initialisation has
873
 
         * completed.
874
 
         */
875
 
        smp_mb();
 
901
        spin_lock(&inode->i_lock);
876
902
        WARN_ON(!(inode->i_state & I_NEW));
877
903
        inode->i_state &= ~I_NEW;
878
 
        wake_up_inode(inode);
 
904
        wake_up_bit(&inode->i_state, __I_NEW);
 
905
        spin_unlock(&inode->i_lock);
879
906
}
880
907
EXPORT_SYMBOL(unlock_new_inode);
881
908
 
882
 
/*
883
 
 * This is called without the inode lock held.. Be careful.
884
 
 *
885
 
 * We no longer cache the sb_flags in i_flags - see fs.h
886
 
 *      -- rmk@arm.uk.linux.org
 
909
/**
 
910
 * iget5_locked - obtain an inode from a mounted file system
 
911
 * @sb:         super block of file system
 
912
 * @hashval:    hash value (usually inode number) to get
 
913
 * @test:       callback used for comparisons between inodes
 
914
 * @set:        callback used to initialize a new struct inode
 
915
 * @data:       opaque data pointer to pass to @test and @set
 
916
 *
 
917
 * Search for the inode specified by @hashval and @data in the inode cache,
 
918
 * and if present it is return it with an increased reference count. This is
 
919
 * a generalized version of iget_locked() for file systems where the inode
 
920
 * number is not sufficient for unique identification of an inode.
 
921
 *
 
922
 * If the inode is not in cache, allocate a new inode and return it locked,
 
923
 * hashed, and with the I_NEW flag set. The file system gets to fill it in
 
924
 * before unlocking it via unlock_new_inode().
 
925
 *
 
926
 * Note both @test and @set are called with the inode_hash_lock held, so can't
 
927
 * sleep.
887
928
 */
888
 
static struct inode *get_new_inode(struct super_block *sb,
889
 
                                struct hlist_head *head,
890
 
                                int (*test)(struct inode *, void *),
891
 
                                int (*set)(struct inode *, void *),
892
 
                                void *data)
 
929
struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
 
930
                int (*test)(struct inode *, void *),
 
931
                int (*set)(struct inode *, void *), void *data)
893
932
{
 
933
        struct hlist_head *head = inode_hashtable + hash(sb, hashval);
894
934
        struct inode *inode;
895
935
 
 
936
        spin_lock(&inode_hash_lock);
 
937
        inode = find_inode(sb, head, test, data);
 
938
        spin_unlock(&inode_hash_lock);
 
939
 
 
940
        if (inode) {
 
941
                wait_on_inode(inode);
 
942
                return inode;
 
943
        }
 
944
 
896
945
        inode = alloc_inode(sb);
897
946
        if (inode) {
898
947
                struct inode *old;
899
948
 
900
 
                spin_lock(&inode_lock);
 
949
                spin_lock(&inode_hash_lock);
901
950
                /* We released the lock, so.. */
902
951
                old = find_inode(sb, head, test, data);
903
952
                if (!old) {
904
953
                        if (set(inode, data))
905
954
                                goto set_failed;
906
955
 
 
956
                        spin_lock(&inode->i_lock);
 
957
                        inode->i_state = I_NEW;
907
958
                        hlist_add_head(&inode->i_hash, head);
908
 
                        __inode_sb_list_add(inode);
909
 
                        inode->i_state = I_NEW;
910
 
                        spin_unlock(&inode_lock);
 
959
                        spin_unlock(&inode->i_lock);
 
960
                        inode_sb_list_add(inode);
 
961
                        spin_unlock(&inode_hash_lock);
911
962
 
912
963
                        /* Return the locked inode with I_NEW set, the
913
964
                         * caller is responsible for filling in the contents
920
971
                 * us. Use the old inode instead of the one we just
921
972
                 * allocated.
922
973
                 */
923
 
                spin_unlock(&inode_lock);
 
974
                spin_unlock(&inode_hash_lock);
924
975
                destroy_inode(inode);
925
976
                inode = old;
926
977
                wait_on_inode(inode);
928
979
        return inode;
929
980
 
930
981
set_failed:
931
 
        spin_unlock(&inode_lock);
 
982
        spin_unlock(&inode_hash_lock);
932
983
        destroy_inode(inode);
933
984
        return NULL;
934
985
}
 
986
EXPORT_SYMBOL(iget5_locked);
935
987
 
936
 
/*
937
 
 * get_new_inode_fast is the fast path version of get_new_inode, see the
938
 
 * comment at iget_locked for details.
 
988
/**
 
989
 * iget_locked - obtain an inode from a mounted file system
 
990
 * @sb:         super block of file system
 
991
 * @ino:        inode number to get
 
992
 *
 
993
 * Search for the inode specified by @ino in the inode cache and if present
 
994
 * return it with an increased reference count. This is for file systems
 
995
 * where the inode number is sufficient for unique identification of an inode.
 
996
 *
 
997
 * If the inode is not in cache, allocate a new inode and return it locked,
 
998
 * hashed, and with the I_NEW flag set.  The file system gets to fill it in
 
999
 * before unlocking it via unlock_new_inode().
939
1000
 */
940
 
static struct inode *get_new_inode_fast(struct super_block *sb,
941
 
                                struct hlist_head *head, unsigned long ino)
 
1001
struct inode *iget_locked(struct super_block *sb, unsigned long ino)
942
1002
{
 
1003
        struct hlist_head *head = inode_hashtable + hash(sb, ino);
943
1004
        struct inode *inode;
944
1005
 
 
1006
        spin_lock(&inode_hash_lock);
 
1007
        inode = find_inode_fast(sb, head, ino);
 
1008
        spin_unlock(&inode_hash_lock);
 
1009
        if (inode) {
 
1010
                wait_on_inode(inode);
 
1011
                return inode;
 
1012
        }
 
1013
 
945
1014
        inode = alloc_inode(sb);
946
1015
        if (inode) {
947
1016
                struct inode *old;
948
1017
 
949
 
                spin_lock(&inode_lock);
 
1018
                spin_lock(&inode_hash_lock);
950
1019
                /* We released the lock, so.. */
951
1020
                old = find_inode_fast(sb, head, ino);
952
1021
                if (!old) {
953
1022
                        inode->i_ino = ino;
 
1023
                        spin_lock(&inode->i_lock);
 
1024
                        inode->i_state = I_NEW;
954
1025
                        hlist_add_head(&inode->i_hash, head);
955
 
                        __inode_sb_list_add(inode);
956
 
                        inode->i_state = I_NEW;
957
 
                        spin_unlock(&inode_lock);
 
1026
                        spin_unlock(&inode->i_lock);
 
1027
                        inode_sb_list_add(inode);
 
1028
                        spin_unlock(&inode_hash_lock);
958
1029
 
959
1030
                        /* Return the locked inode with I_NEW set, the
960
1031
                         * caller is responsible for filling in the contents
967
1038
                 * us. Use the old inode instead of the one we just
968
1039
                 * allocated.
969
1040
                 */
970
 
                spin_unlock(&inode_lock);
 
1041
                spin_unlock(&inode_hash_lock);
971
1042
                destroy_inode(inode);
972
1043
                inode = old;
973
1044
                wait_on_inode(inode);
974
1045
        }
975
1046
        return inode;
976
1047
}
 
1048
EXPORT_SYMBOL(iget_locked);
977
1049
 
978
1050
/*
979
1051
 * search the inode cache for a matching inode number.
988
1060
        struct hlist_node *node;
989
1061
        struct inode *inode;
990
1062
 
 
1063
        spin_lock(&inode_hash_lock);
991
1064
        hlist_for_each_entry(inode, node, b, i_hash) {
992
 
                if (inode->i_ino == ino && inode->i_sb == sb)
 
1065
                if (inode->i_ino == ino && inode->i_sb == sb) {
 
1066
                        spin_unlock(&inode_hash_lock);
993
1067
                        return 0;
 
1068
                }
994
1069
        }
 
1070
        spin_unlock(&inode_hash_lock);
995
1071
 
996
1072
        return 1;
997
1073
}
1021
1097
        static unsigned int counter;
1022
1098
        ino_t res;
1023
1099
 
1024
 
        spin_lock(&inode_lock);
1025
1100
        spin_lock(&iunique_lock);
1026
1101
        do {
1027
1102
                if (counter <= max_reserved)
1029
1104
                res = counter++;
1030
1105
        } while (!test_inode_iunique(sb, res));
1031
1106
        spin_unlock(&iunique_lock);
1032
 
        spin_unlock(&inode_lock);
1033
1107
 
1034
1108
        return res;
1035
1109
}
1037
1111
 
1038
1112
struct inode *igrab(struct inode *inode)
1039
1113
{
1040
 
        spin_lock(&inode_lock);
1041
 
        if (!(inode->i_state & (I_FREEING|I_WILL_FREE)))
 
1114
        spin_lock(&inode->i_lock);
 
1115
        if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
1042
1116
                __iget(inode);
1043
 
        else
 
1117
                spin_unlock(&inode->i_lock);
 
1118
        } else {
 
1119
                spin_unlock(&inode->i_lock);
1044
1120
                /*
1045
1121
                 * Handle the case where s_op->clear_inode is not been
1046
1122
                 * called yet, and somebody is calling igrab
1047
1123
                 * while the inode is getting freed.
1048
1124
                 */
1049
1125
                inode = NULL;
1050
 
        spin_unlock(&inode_lock);
 
1126
        }
1051
1127
        return inode;
1052
1128
}
1053
1129
EXPORT_SYMBOL(igrab);
1054
1130
 
1055
1131
/**
1056
 
 * ifind - internal function, you want ilookup5() or iget5().
1057
 
 * @sb:         super block of file system to search
1058
 
 * @head:       the head of the list to search
1059
 
 * @test:       callback used for comparisons between inodes
1060
 
 * @data:       opaque data pointer to pass to @test
1061
 
 * @wait:       if true wait for the inode to be unlocked, if false do not
1062
 
 *
1063
 
 * ifind() searches for the inode specified by @data in the inode
1064
 
 * cache. This is a generalized version of ifind_fast() for file systems where
1065
 
 * the inode number is not sufficient for unique identification of an inode.
1066
 
 *
1067
 
 * If the inode is in the cache, the inode is returned with an incremented
1068
 
 * reference count.
1069
 
 *
1070
 
 * Otherwise NULL is returned.
1071
 
 *
1072
 
 * Note, @test is called with the inode_lock held, so can't sleep.
1073
 
 */
1074
 
static struct inode *ifind(struct super_block *sb,
1075
 
                struct hlist_head *head, int (*test)(struct inode *, void *),
1076
 
                void *data, const int wait)
1077
 
{
1078
 
        struct inode *inode;
1079
 
 
1080
 
        spin_lock(&inode_lock);
1081
 
        inode = find_inode(sb, head, test, data);
1082
 
        if (inode) {
1083
 
                spin_unlock(&inode_lock);
1084
 
                if (likely(wait))
1085
 
                        wait_on_inode(inode);
1086
 
                return inode;
1087
 
        }
1088
 
        spin_unlock(&inode_lock);
1089
 
        return NULL;
1090
 
}
1091
 
 
1092
 
/**
1093
 
 * ifind_fast - internal function, you want ilookup() or iget().
1094
 
 * @sb:         super block of file system to search
1095
 
 * @head:       head of the list to search
1096
 
 * @ino:        inode number to search for
1097
 
 *
1098
 
 * ifind_fast() searches for the inode @ino in the inode cache. This is for
1099
 
 * file systems where the inode number is sufficient for unique identification
1100
 
 * of an inode.
1101
 
 *
1102
 
 * If the inode is in the cache, the inode is returned with an incremented
1103
 
 * reference count.
1104
 
 *
1105
 
 * Otherwise NULL is returned.
1106
 
 */
1107
 
static struct inode *ifind_fast(struct super_block *sb,
1108
 
                struct hlist_head *head, unsigned long ino)
1109
 
{
1110
 
        struct inode *inode;
1111
 
 
1112
 
        spin_lock(&inode_lock);
1113
 
        inode = find_inode_fast(sb, head, ino);
1114
 
        if (inode) {
1115
 
                spin_unlock(&inode_lock);
1116
 
                wait_on_inode(inode);
1117
 
                return inode;
1118
 
        }
1119
 
        spin_unlock(&inode_lock);
1120
 
        return NULL;
1121
 
}
1122
 
 
1123
 
/**
1124
1132
 * ilookup5_nowait - search for an inode in the inode cache
1125
1133
 * @sb:         super block of file system to search
1126
1134
 * @hashval:    hash value (usually inode number) to search for
1127
1135
 * @test:       callback used for comparisons between inodes
1128
1136
 * @data:       opaque data pointer to pass to @test
1129
1137
 *
1130
 
 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1131
 
 * @data in the inode cache. This is a generalized version of ilookup() for
1132
 
 * file systems where the inode number is not sufficient for unique
1133
 
 * identification of an inode.
1134
 
 *
 
1138
 * Search for the inode specified by @hashval and @data in the inode cache.
1135
1139
 * If the inode is in the cache, the inode is returned with an incremented
1136
 
 * reference count.  Note, the inode lock is not waited upon so you have to be
1137
 
 * very careful what you do with the returned inode.  You probably should be
1138
 
 * using ilookup5() instead.
1139
 
 *
1140
 
 * Otherwise NULL is returned.
1141
 
 *
1142
 
 * Note, @test is called with the inode_lock held, so can't sleep.
 
1140
 * reference count.
 
1141
 *
 
1142
 * Note: I_NEW is not waited upon so you have to be very careful what you do
 
1143
 * with the returned inode.  You probably should be using ilookup5() instead.
 
1144
 *
 
1145
 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
1143
1146
 */
1144
1147
struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1145
1148
                int (*test)(struct inode *, void *), void *data)
1146
1149
{
1147
1150
        struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1148
 
 
1149
 
        return ifind(sb, head, test, data, 0);
 
1151
        struct inode *inode;
 
1152
 
 
1153
        spin_lock(&inode_hash_lock);
 
1154
        inode = find_inode(sb, head, test, data);
 
1155
        spin_unlock(&inode_hash_lock);
 
1156
 
 
1157
        return inode;
1150
1158
}
1151
1159
EXPORT_SYMBOL(ilookup5_nowait);
1152
1160
 
1157
1165
 * @test:       callback used for comparisons between inodes
1158
1166
 * @data:       opaque data pointer to pass to @test
1159
1167
 *
1160
 
 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1161
 
 * @data in the inode cache. This is a generalized version of ilookup() for
1162
 
 * file systems where the inode number is not sufficient for unique
1163
 
 * identification of an inode.
1164
 
 *
1165
 
 * If the inode is in the cache, the inode lock is waited upon and the inode is
 
1168
 * Search for the inode specified by @hashval and @data in the inode cache,
 
1169
 * and if the inode is in the cache, return the inode with an incremented
 
1170
 * reference count.  Waits on I_NEW before returning the inode.
1166
1171
 * returned with an incremented reference count.
1167
1172
 *
1168
 
 * Otherwise NULL is returned.
 
1173
 * This is a generalized version of ilookup() for file systems where the
 
1174
 * inode number is not sufficient for unique identification of an inode.
1169
1175
 *
1170
 
 * Note, @test is called with the inode_lock held, so can't sleep.
 
1176
 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1171
1177
 */
1172
1178
struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1173
1179
                int (*test)(struct inode *, void *), void *data)
1174
1180
{
1175
 
        struct hlist_head *head = inode_hashtable + hash(sb, hashval);
 
1181
        struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
1176
1182
 
1177
 
        return ifind(sb, head, test, data, 1);
 
1183
        if (inode)
 
1184
                wait_on_inode(inode);
 
1185
        return inode;
1178
1186
}
1179
1187
EXPORT_SYMBOL(ilookup5);
1180
1188
 
1183
1191
 * @sb:         super block of file system to search
1184
1192
 * @ino:        inode number to search for
1185
1193
 *
1186
 
 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
1187
 
 * This is for file systems where the inode number is sufficient for unique
1188
 
 * identification of an inode.
1189
 
 *
1190
 
 * If the inode is in the cache, the inode is returned with an incremented
1191
 
 * reference count.
1192
 
 *
1193
 
 * Otherwise NULL is returned.
 
1194
 * Search for the inode @ino in the inode cache, and if the inode is in the
 
1195
 * cache, the inode is returned with an incremented reference count.
1194
1196
 */
1195
1197
struct inode *ilookup(struct super_block *sb, unsigned long ino)
1196
1198
{
1197
1199
        struct hlist_head *head = inode_hashtable + hash(sb, ino);
1198
 
 
1199
 
        return ifind_fast(sb, head, ino);
 
1200
        struct inode *inode;
 
1201
 
 
1202
        spin_lock(&inode_hash_lock);
 
1203
        inode = find_inode_fast(sb, head, ino);
 
1204
        spin_unlock(&inode_hash_lock);
 
1205
 
 
1206
        if (inode)
 
1207
                wait_on_inode(inode);
 
1208
        return inode;
1200
1209
}
1201
1210
EXPORT_SYMBOL(ilookup);
1202
1211
 
1203
 
/**
1204
 
 * iget5_locked - obtain an inode from a mounted file system
1205
 
 * @sb:         super block of file system
1206
 
 * @hashval:    hash value (usually inode number) to get
1207
 
 * @test:       callback used for comparisons between inodes
1208
 
 * @set:        callback used to initialize a new struct inode
1209
 
 * @data:       opaque data pointer to pass to @test and @set
1210
 
 *
1211
 
 * iget5_locked() uses ifind() to search for the inode specified by @hashval
1212
 
 * and @data in the inode cache and if present it is returned with an increased
1213
 
 * reference count. This is a generalized version of iget_locked() for file
1214
 
 * systems where the inode number is not sufficient for unique identification
1215
 
 * of an inode.
1216
 
 *
1217
 
 * If the inode is not in cache, get_new_inode() is called to allocate a new
1218
 
 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
1219
 
 * file system gets to fill it in before unlocking it via unlock_new_inode().
1220
 
 *
1221
 
 * Note both @test and @set are called with the inode_lock held, so can't sleep.
1222
 
 */
1223
 
struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1224
 
                int (*test)(struct inode *, void *),
1225
 
                int (*set)(struct inode *, void *), void *data)
1226
 
{
1227
 
        struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1228
 
        struct inode *inode;
1229
 
 
1230
 
        inode = ifind(sb, head, test, data, 1);
1231
 
        if (inode)
1232
 
                return inode;
1233
 
        /*
1234
 
         * get_new_inode() will do the right thing, re-trying the search
1235
 
         * in case it had to block at any point.
1236
 
         */
1237
 
        return get_new_inode(sb, head, test, set, data);
1238
 
}
1239
 
EXPORT_SYMBOL(iget5_locked);
1240
 
 
1241
 
/**
1242
 
 * iget_locked - obtain an inode from a mounted file system
1243
 
 * @sb:         super block of file system
1244
 
 * @ino:        inode number to get
1245
 
 *
1246
 
 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1247
 
 * the inode cache and if present it is returned with an increased reference
1248
 
 * count. This is for file systems where the inode number is sufficient for
1249
 
 * unique identification of an inode.
1250
 
 *
1251
 
 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1252
 
 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1253
 
 * The file system gets to fill it in before unlocking it via
1254
 
 * unlock_new_inode().
1255
 
 */
1256
 
struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1257
 
{
1258
 
        struct hlist_head *head = inode_hashtable + hash(sb, ino);
1259
 
        struct inode *inode;
1260
 
 
1261
 
        inode = ifind_fast(sb, head, ino);
1262
 
        if (inode)
1263
 
                return inode;
1264
 
        /*
1265
 
         * get_new_inode_fast() will do the right thing, re-trying the search
1266
 
         * in case it had to block at any point.
1267
 
         */
1268
 
        return get_new_inode_fast(sb, head, ino);
1269
 
}
1270
 
EXPORT_SYMBOL(iget_locked);
1271
 
 
1272
1212
int insert_inode_locked(struct inode *inode)
1273
1213
{
1274
1214
        struct super_block *sb = inode->i_sb;
1275
1215
        ino_t ino = inode->i_ino;
1276
1216
        struct hlist_head *head = inode_hashtable + hash(sb, ino);
1277
1217
 
1278
 
        inode->i_state |= I_NEW;
1279
1218
        while (1) {
1280
1219
                struct hlist_node *node;
1281
1220
                struct inode *old = NULL;
1282
 
                spin_lock(&inode_lock);
 
1221
                spin_lock(&inode_hash_lock);
1283
1222
                hlist_for_each_entry(old, node, head, i_hash) {
1284
1223
                        if (old->i_ino != ino)
1285
1224
                                continue;
1286
1225
                        if (old->i_sb != sb)
1287
1226
                                continue;
1288
 
                        if (old->i_state & (I_FREEING|I_WILL_FREE))
 
1227
                        spin_lock(&old->i_lock);
 
1228
                        if (old->i_state & (I_FREEING|I_WILL_FREE)) {
 
1229
                                spin_unlock(&old->i_lock);
1289
1230
                                continue;
 
1231
                        }
1290
1232
                        break;
1291
1233
                }
1292
1234
                if (likely(!node)) {
 
1235
                        spin_lock(&inode->i_lock);
 
1236
                        inode->i_state |= I_NEW;
1293
1237
                        hlist_add_head(&inode->i_hash, head);
1294
 
                        spin_unlock(&inode_lock);
 
1238
                        spin_unlock(&inode->i_lock);
 
1239
                        spin_unlock(&inode_hash_lock);
1295
1240
                        return 0;
1296
1241
                }
1297
1242
                __iget(old);
1298
 
                spin_unlock(&inode_lock);
 
1243
                spin_unlock(&old->i_lock);
 
1244
                spin_unlock(&inode_hash_lock);
1299
1245
                wait_on_inode(old);
1300
1246
                if (unlikely(!inode_unhashed(old))) {
1301
1247
                        iput(old);
1312
1258
        struct super_block *sb = inode->i_sb;
1313
1259
        struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1314
1260
 
1315
 
        inode->i_state |= I_NEW;
1316
 
 
1317
1261
        while (1) {
1318
1262
                struct hlist_node *node;
1319
1263
                struct inode *old = NULL;
1320
1264
 
1321
 
                spin_lock(&inode_lock);
 
1265
                spin_lock(&inode_hash_lock);
1322
1266
                hlist_for_each_entry(old, node, head, i_hash) {
1323
1267
                        if (old->i_sb != sb)
1324
1268
                                continue;
1325
1269
                        if (!test(old, data))
1326
1270
                                continue;
1327
 
                        if (old->i_state & (I_FREEING|I_WILL_FREE))
 
1271
                        spin_lock(&old->i_lock);
 
1272
                        if (old->i_state & (I_FREEING|I_WILL_FREE)) {
 
1273
                                spin_unlock(&old->i_lock);
1328
1274
                                continue;
 
1275
                        }
1329
1276
                        break;
1330
1277
                }
1331
1278
                if (likely(!node)) {
 
1279
                        spin_lock(&inode->i_lock);
 
1280
                        inode->i_state |= I_NEW;
1332
1281
                        hlist_add_head(&inode->i_hash, head);
1333
 
                        spin_unlock(&inode_lock);
 
1282
                        spin_unlock(&inode->i_lock);
 
1283
                        spin_unlock(&inode_hash_lock);
1334
1284
                        return 0;
1335
1285
                }
1336
1286
                __iget(old);
1337
 
                spin_unlock(&inode_lock);
 
1287
                spin_unlock(&old->i_lock);
 
1288
                spin_unlock(&inode_hash_lock);
1338
1289
                wait_on_inode(old);
1339
1290
                if (unlikely(!inode_unhashed(old))) {
1340
1291
                        iput(old);
1379
1330
        const struct super_operations *op = inode->i_sb->s_op;
1380
1331
        int drop;
1381
1332
 
 
1333
        WARN_ON(inode->i_state & I_NEW);
 
1334
 
1382
1335
        if (op && op->drop_inode)
1383
1336
                drop = op->drop_inode(inode);
1384
1337
        else
1385
1338
                drop = generic_drop_inode(inode);
1386
1339
 
 
1340
        if (!drop && (sb->s_flags & MS_ACTIVE)) {
 
1341
                inode->i_state |= I_REFERENCED;
 
1342
                if (!(inode->i_state & (I_DIRTY|I_SYNC)))
 
1343
                        inode_lru_list_add(inode);
 
1344
                spin_unlock(&inode->i_lock);
 
1345
                return;
 
1346
        }
 
1347
 
1387
1348
        if (!drop) {
1388
 
                if (sb->s_flags & MS_ACTIVE) {
1389
 
                        inode->i_state |= I_REFERENCED;
1390
 
                        if (!(inode->i_state & (I_DIRTY|I_SYNC))) {
1391
 
                                inode_lru_list_add(inode);
1392
 
                        }
1393
 
                        spin_unlock(&inode_lock);
1394
 
                        return;
1395
 
                }
1396
 
                WARN_ON(inode->i_state & I_NEW);
1397
1349
                inode->i_state |= I_WILL_FREE;
1398
 
                spin_unlock(&inode_lock);
 
1350
                spin_unlock(&inode->i_lock);
1399
1351
                write_inode_now(inode, 1);
1400
 
                spin_lock(&inode_lock);
 
1352
                spin_lock(&inode->i_lock);
1401
1353
                WARN_ON(inode->i_state & I_NEW);
1402
1354
                inode->i_state &= ~I_WILL_FREE;
1403
 
                __remove_inode_hash(inode);
1404
1355
        }
1405
1356
 
1406
 
        WARN_ON(inode->i_state & I_NEW);
1407
1357
        inode->i_state |= I_FREEING;
1408
 
 
1409
 
        /*
1410
 
         * Move the inode off the IO lists and LRU once I_FREEING is
1411
 
         * set so that it won't get moved back on there if it is dirty.
1412
 
         */
1413
1358
        inode_lru_list_del(inode);
1414
 
        list_del_init(&inode->i_wb_list);
 
1359
        spin_unlock(&inode->i_lock);
1415
1360
 
1416
 
        __inode_sb_list_del(inode);
1417
 
        spin_unlock(&inode_lock);
1418
1361
        evict(inode);
1419
 
        remove_inode_hash(inode);
1420
 
        wake_up_inode(inode);
1421
 
        BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
1422
 
        destroy_inode(inode);
1423
1362
}
1424
1363
 
1425
1364
/**
1436
1375
        if (inode) {
1437
1376
                BUG_ON(inode->i_state & I_CLEAR);
1438
1377
 
1439
 
                if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
 
1378
                if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
1440
1379
                        iput_final(inode);
1441
1380
        }
1442
1381
}
1615
1554
 * to recheck inode state.
1616
1555
 *
1617
1556
 * It doesn't matter if I_NEW is not set initially, a call to
1618
 
 * wake_up_inode() after removing from the hash list will DTRT.
1619
 
 *
1620
 
 * This is called with inode_lock held.
 
1557
 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
 
1558
 * will DTRT.
1621
1559
 */
1622
1560
static void __wait_on_freeing_inode(struct inode *inode)
1623
1561
{
1625
1563
        DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1626
1564
        wq = bit_waitqueue(&inode->i_state, __I_NEW);
1627
1565
        prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1628
 
        spin_unlock(&inode_lock);
 
1566
        spin_unlock(&inode->i_lock);
 
1567
        spin_unlock(&inode_hash_lock);
1629
1568
        schedule();
1630
1569
        finish_wait(wq, &wait.wait);
1631
 
        spin_lock(&inode_lock);
 
1570
        spin_lock(&inode_hash_lock);
1632
1571
}
1633
1572
 
1634
1573
static __initdata unsigned long ihash_entries;
1720
1659
EXPORT_SYMBOL(init_special_inode);
1721
1660
 
1722
1661
/**
1723
 
 * Init uid,gid,mode for new inode according to posix standards
 
1662
 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
1724
1663
 * @inode: New inode
1725
1664
 * @dir: Directory inode
1726
1665
 * @mode: mode of the new inode
1739
1678
}
1740
1679
EXPORT_SYMBOL(inode_init_owner);
1741
1680
 
 
1681
/**
 
1682
 * inode_owner_or_capable - check current task permissions to inode
 
1683
 * @inode: inode being checked
 
1684
 *
 
1685
 * Return true if current either has CAP_FOWNER to the inode, or
 
1686
 * owns the file.
 
1687
 */
 
1688
bool inode_owner_or_capable(const struct inode *inode)
 
1689
{
 
1690
        struct user_namespace *ns = inode_userns(inode);
 
1691
 
 
1692
        if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
 
1693
                return true;
 
1694
        if (ns_capable(ns, CAP_FOWNER))
 
1695
                return true;
 
1696
        return false;
 
1697
}
 
1698
EXPORT_SYMBOL(inode_owner_or_capable);
 
1699
 
1742
1700
#define CREATE_TRACE_POINTS
1743
1701
#include <trace/events/vfs.h>
1744
1702