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>
30
* This is needed for the following functions:
34
* FIXME: remove all knowledge of the buffer layer from this file
36
#include <linux/buffer_head.h>
39
* New inode.c implementation.
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".
48
/* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
50
/* #define INODE_PARANOIA 1 */
51
/* #define INODE_DEBUG 1 */
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.
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 */
32
* Inode locking rules:
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
60
62
static unsigned int i_hash_mask __read_mostly;
61
63
static unsigned int i_hash_shift __read_mostly;
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
71
* A "dirty" list is maintained for each super block,
72
* allowing for low-overhead inode sync() operations.
64
static struct hlist_head *inode_hashtable __read_mostly;
65
static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
75
67
static LIST_HEAD(inode_lru);
76
static struct hlist_head *inode_hashtable __read_mostly;
79
* A simple spinlock to protect the list manipulations.
81
* NOTE! You also have to own the lock if you change
82
* the i_state of an inode while it is in use..
84
DEFINE_SPINLOCK(inode_lock);
85
EXPORT_SYMBOL(inode_lock);
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.
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);
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);
75
* iprune_sem provides exclusion between the icache shrinking and the
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.
99
83
static DECLARE_RWSEM(iprune_sem);
86
* Empty aops. Can be used for the cases where the user does not
87
* define any of the address_space operations.
89
const struct address_space_operations empty_aops = {
91
EXPORT_SYMBOL(empty_aops);
102
94
* Statistics gathering..
104
96
struct inodes_stat_t inodes_stat;
441
414
void remove_inode_hash(struct inode *inode)
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);
447
422
EXPORT_SYMBOL(remove_inode_hash);
449
424
void end_writeback(struct inode *inode)
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.
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);
460
442
EXPORT_SYMBOL(end_writeback);
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.
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.
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.
462
457
static void evict(struct inode *inode)
464
459
const struct super_operations *op = inode->i_sb->s_op;
461
BUG_ON(!(inode->i_state & I_FREEING));
462
BUG_ON(!list_empty(&inode->i_lru));
464
inode_wb_list_del(inode);
465
inode_sb_list_del(inode);
466
467
if (op->evict_inode) {
467
468
op->evict_inode(inode);
517
519
struct inode *inode, *next;
518
520
LIST_HEAD(dispose);
520
down_write(&iprune_sem);
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))
527
spin_lock(&inode->i_lock);
527
528
if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
529
spin_unlock(&inode->i_lock);
532
533
inode->i_state |= I_FREEING;
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.
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);
543
spin_unlock(&inode_lock);
538
spin_unlock(&inode_sb_list_lock);
545
540
dispose_list(&dispose);
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
547
down_write(&iprune_sem);
546
548
up_write(&iprune_sem);
562
564
struct inode *inode, *next;
563
565
LIST_HEAD(dispose);
565
down_write(&iprune_sem);
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);
571
574
if (inode->i_state & I_DIRTY && !kill_dirty) {
575
spin_unlock(&inode->i_lock);
575
579
if (atomic_read(&inode->i_count)) {
580
spin_unlock(&inode->i_lock);
580
585
inode->i_state |= I_FREEING;
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.
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);
591
spin_unlock(&inode_lock);
590
spin_unlock(&inode_sb_list_lock);
593
592
dispose_list(&dispose);
594
up_write(&iprune_sem);
642
640
inode = list_entry(inode_lru.prev, struct inode, i_lru);
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.
647
if (!spin_trylock(&inode->i_lock)) {
648
list_move(&inode->i_lru, &inode_lru);
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.
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--;
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);
661
671
if (inode_has_buffers(inode) || inode->i_data.nrpages) {
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,
668
spin_lock(&inode_lock);
679
spin_lock(&inode_lru_lock);
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))
684
/* avoid lock inversions with trylock */
685
if (!spin_trylock(&inode->i_lock))
687
if (!can_unuse(inode)) {
688
spin_unlock(&inode->i_lock);
676
692
WARN_ON(inode->i_state & I_NEW);
677
693
inode->i_state |= I_FREEING;
694
spin_unlock(&inode->i_lock);
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.
683
696
list_move(&inode->i_lru, &freeable);
684
list_del_init(&inode->i_wb_list);
685
697
inodes_stat.nr_unused--;
687
699
if (current_is_kswapd())
688
700
__count_vm_events(KSWAPD_INODESTEAL, reap);
690
702
__count_vm_events(PGINODESTEAL, reap);
691
spin_unlock(&inode_lock);
703
spin_unlock(&inode_lru_lock);
693
705
dispose_list(&freeable);
694
706
up_read(&iprune_sem);
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
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);
880
907
EXPORT_SYMBOL(unlock_new_inode);
883
* This is called without the inode lock held.. Be careful.
885
* We no longer cache the sb_flags in i_flags - see fs.h
886
* -- rmk@arm.uk.linux.org
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
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.
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().
926
* Note both @test and @set are called with the inode_hash_lock held, so can't
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 *),
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)
933
struct hlist_head *head = inode_hashtable + hash(sb, hashval);
894
934
struct inode *inode;
936
spin_lock(&inode_hash_lock);
937
inode = find_inode(sb, head, test, data);
938
spin_unlock(&inode_hash_lock);
941
wait_on_inode(inode);
896
945
inode = alloc_inode(sb);
898
947
struct inode *old;
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);
904
953
if (set(inode, data))
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);
912
963
/* Return the locked inode with I_NEW set, the
913
964
* caller is responsible for filling in the contents
931
spin_unlock(&inode_lock);
982
spin_unlock(&inode_hash_lock);
932
983
destroy_inode(inode);
986
EXPORT_SYMBOL(iget5_locked);
937
* get_new_inode_fast is the fast path version of get_new_inode, see the
938
* comment at iget_locked for details.
989
* iget_locked - obtain an inode from a mounted file system
990
* @sb: super block of file system
991
* @ino: inode number to get
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.
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().
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)
1003
struct hlist_head *head = inode_hashtable + hash(sb, ino);
943
1004
struct inode *inode;
1006
spin_lock(&inode_hash_lock);
1007
inode = find_inode_fast(sb, head, ino);
1008
spin_unlock(&inode_hash_lock);
1010
wait_on_inode(inode);
945
1014
inode = alloc_inode(sb);
947
1016
struct inode *old;
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);
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);
959
1030
/* Return the locked inode with I_NEW set, the
960
1031
* caller is responsible for filling in the contents
1038
1112
struct inode *igrab(struct inode *inode)
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))) {
1117
spin_unlock(&inode->i_lock);
1119
spin_unlock(&inode->i_lock);
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.
1050
spin_unlock(&inode_lock);
1053
1129
EXPORT_SYMBOL(igrab);
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
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.
1067
* If the inode is in the cache, the inode is returned with an incremented
1070
* Otherwise NULL is returned.
1072
* Note, @test is called with the inode_lock held, so can't sleep.
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)
1078
struct inode *inode;
1080
spin_lock(&inode_lock);
1081
inode = find_inode(sb, head, test, data);
1083
spin_unlock(&inode_lock);
1085
wait_on_inode(inode);
1088
spin_unlock(&inode_lock);
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
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
1102
* If the inode is in the cache, the inode is returned with an incremented
1105
* Otherwise NULL is returned.
1107
static struct inode *ifind_fast(struct super_block *sb,
1108
struct hlist_head *head, unsigned long ino)
1110
struct inode *inode;
1112
spin_lock(&inode_lock);
1113
inode = find_inode_fast(sb, head, ino);
1115
spin_unlock(&inode_lock);
1116
wait_on_inode(inode);
1119
spin_unlock(&inode_lock);
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
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.
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.
1140
* Otherwise NULL is returned.
1142
* Note, @test is called with the inode_lock held, so can't sleep.
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.
1145
* Note2: @test is called with the inode_hash_lock held, so can't sleep.
1144
1147
struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1145
1148
int (*test)(struct inode *, void *), void *data)
1147
1150
struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1149
return ifind(sb, head, test, data, 0);
1151
struct inode *inode;
1153
spin_lock(&inode_hash_lock);
1154
inode = find_inode(sb, head, test, data);
1155
spin_unlock(&inode_hash_lock);
1151
1159
EXPORT_SYMBOL(ilookup5_nowait);
1157
1165
* @test: callback used for comparisons between inodes
1158
1166
* @data: opaque data pointer to pass to @test
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.
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.
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.
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.
1172
1178
struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1173
1179
int (*test)(struct inode *, void *), void *data)
1175
struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1181
struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
1177
return ifind(sb, head, test, data, 1);
1184
wait_on_inode(inode);
1179
1187
EXPORT_SYMBOL(ilookup5);
1183
1191
* @sb: super block of file system to search
1184
1192
* @ino: inode number to search for
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.
1190
* If the inode is in the cache, the inode is returned with an incremented
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.
1195
1197
struct inode *ilookup(struct super_block *sb, unsigned long ino)
1197
1199
struct hlist_head *head = inode_hashtable + hash(sb, ino);
1199
return ifind_fast(sb, head, ino);
1200
struct inode *inode;
1202
spin_lock(&inode_hash_lock);
1203
inode = find_inode_fast(sb, head, ino);
1204
spin_unlock(&inode_hash_lock);
1207
wait_on_inode(inode);
1201
1210
EXPORT_SYMBOL(ilookup);
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
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
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().
1221
* Note both @test and @set are called with the inode_lock held, so can't sleep.
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)
1227
struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1228
struct inode *inode;
1230
inode = ifind(sb, head, test, data, 1);
1234
* get_new_inode() will do the right thing, re-trying the search
1235
* in case it had to block at any point.
1237
return get_new_inode(sb, head, test, set, data);
1239
EXPORT_SYMBOL(iget5_locked);
1242
* iget_locked - obtain an inode from a mounted file system
1243
* @sb: super block of file system
1244
* @ino: inode number to get
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.
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().
1256
struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1258
struct hlist_head *head = inode_hashtable + hash(sb, ino);
1259
struct inode *inode;
1261
inode = ifind_fast(sb, head, ino);
1265
* get_new_inode_fast() will do the right thing, re-trying the search
1266
* in case it had to block at any point.
1268
return get_new_inode_fast(sb, head, ino);
1270
EXPORT_SYMBOL(iget_locked);
1272
1212
int insert_inode_locked(struct inode *inode)
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);
1278
inode->i_state |= I_NEW;
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)
1286
1225
if (old->i_sb != sb)
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);
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);
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))) {
1312
1258
struct super_block *sb = inode->i_sb;
1313
1259
struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1315
inode->i_state |= I_NEW;
1318
1262
struct hlist_node *node;
1319
1263
struct inode *old = NULL;
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)
1325
1269
if (!test(old, data))
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);
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);
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))) {
1379
1330
const struct super_operations *op = inode->i_sb->s_op;
1333
WARN_ON(inode->i_state & I_NEW);
1382
1335
if (op && op->drop_inode)
1383
1336
drop = op->drop_inode(inode);
1385
1338
drop = generic_drop_inode(inode);
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);
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);
1393
spin_unlock(&inode_lock);
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);
1406
WARN_ON(inode->i_state & I_NEW);
1407
1357
inode->i_state |= I_FREEING;
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.
1413
1358
inode_lru_list_del(inode);
1414
list_del_init(&inode->i_wb_list);
1359
spin_unlock(&inode->i_lock);
1416
__inode_sb_list_del(inode);
1417
spin_unlock(&inode_lock);
1419
remove_inode_hash(inode);
1420
wake_up_inode(inode);
1421
BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
1422
destroy_inode(inode);