@@ -1002,6 +1002,70 @@ void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
10021002}
10031003EXPORT_SYMBOL (unlock_two_nondirectories );
10041004
1005+ /**
1006+ * inode_insert5 - obtain an inode from a mounted file system
1007+ * @inode: pre-allocated inode to use for insert to cache
1008+ * @hashval: hash value (usually inode number) to get
1009+ * @test: callback used for comparisons between inodes
1010+ * @set: callback used to initialize a new struct inode
1011+ * @data: opaque data pointer to pass to @test and @set
1012+ *
1013+ * Search for the inode specified by @hashval and @data in the inode cache,
1014+ * and if present it is return it with an increased reference count. This is
1015+ * a variant of iget5_locked() for callers that don't want to fail on memory
1016+ * allocation of inode.
1017+ *
1018+ * If the inode is not in cache, insert the pre-allocated inode to cache and
1019+ * return it locked, hashed, and with the I_NEW flag set. The file system gets
1020+ * to fill it in before unlocking it via unlock_new_inode().
1021+ *
1022+ * Note both @test and @set are called with the inode_hash_lock held, so can't
1023+ * sleep.
1024+ */
1025+ struct inode * inode_insert5 (struct inode * inode , unsigned long hashval ,
1026+ int (* test )(struct inode * , void * ),
1027+ int (* set )(struct inode * , void * ), void * data )
1028+ {
1029+ struct hlist_head * head = inode_hashtable + hash (inode -> i_sb , hashval );
1030+ struct inode * old ;
1031+
1032+ again :
1033+ spin_lock (& inode_hash_lock );
1034+ old = find_inode (inode -> i_sb , head , test , data );
1035+ if (unlikely (old )) {
1036+ /*
1037+ * Uhhuh, somebody else created the same inode under us.
1038+ * Use the old inode instead of the preallocated one.
1039+ */
1040+ spin_unlock (& inode_hash_lock );
1041+ wait_on_inode (old );
1042+ if (unlikely (inode_unhashed (old ))) {
1043+ iput (old );
1044+ goto again ;
1045+ }
1046+ return old ;
1047+ }
1048+
1049+ if (set && unlikely (set (inode , data ))) {
1050+ inode = NULL ;
1051+ goto unlock ;
1052+ }
1053+
1054+ /*
1055+ * Return the locked inode with I_NEW set, the
1056+ * caller is responsible for filling in the contents
1057+ */
1058+ spin_lock (& inode -> i_lock );
1059+ inode -> i_state |= I_NEW ;
1060+ hlist_add_head (& inode -> i_hash , head );
1061+ spin_unlock (& inode -> i_lock );
1062+ unlock :
1063+ spin_unlock (& inode_hash_lock );
1064+
1065+ return inode ;
1066+ }
1067+ EXPORT_SYMBOL (inode_insert5 );
1068+
10051069/**
10061070 * iget5_locked - obtain an inode from a mounted file system
10071071 * @sb: super block of file system
@@ -1026,66 +1090,18 @@ struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
10261090 int (* test )(struct inode * , void * ),
10271091 int (* set )(struct inode * , void * ), void * data )
10281092{
1029- struct hlist_head * head = inode_hashtable + hash (sb , hashval );
1030- struct inode * inode ;
1031- again :
1032- spin_lock (& inode_hash_lock );
1033- inode = find_inode (sb , head , test , data );
1034- spin_unlock (& inode_hash_lock );
1093+ struct inode * inode = ilookup5 (sb , hashval , test , data );
10351094
1036- if (inode ) {
1037- wait_on_inode (inode );
1038- if (unlikely (inode_unhashed (inode ))) {
1039- iput (inode );
1040- goto again ;
1041- }
1042- return inode ;
1043- }
1095+ if (!inode ) {
1096+ struct inode * new = new_inode (sb );
10441097
1045- inode = alloc_inode (sb );
1046- if (inode ) {
1047- struct inode * old ;
1048-
1049- spin_lock (& inode_hash_lock );
1050- /* We released the lock, so.. */
1051- old = find_inode (sb , head , test , data );
1052- if (!old ) {
1053- if (set (inode , data ))
1054- goto set_failed ;
1055-
1056- spin_lock (& inode -> i_lock );
1057- inode -> i_state = I_NEW ;
1058- hlist_add_head (& inode -> i_hash , head );
1059- spin_unlock (& inode -> i_lock );
1060- inode_sb_list_add (inode );
1061- spin_unlock (& inode_hash_lock );
1062-
1063- /* Return the locked inode with I_NEW set, the
1064- * caller is responsible for filling in the contents
1065- */
1066- return inode ;
1067- }
1068-
1069- /*
1070- * Uhhuh, somebody else created the same inode under
1071- * us. Use the old inode instead of the one we just
1072- * allocated.
1073- */
1074- spin_unlock (& inode_hash_lock );
1075- destroy_inode (inode );
1076- inode = old ;
1077- wait_on_inode (inode );
1078- if (unlikely (inode_unhashed (inode ))) {
1079- iput (inode );
1080- goto again ;
1098+ if (new ) {
1099+ inode = inode_insert5 (new , hashval , test , set , data );
1100+ if (unlikely (inode != new ))
1101+ iput (new );
10811102 }
10821103 }
10831104 return inode ;
1084-
1085- set_failed :
1086- spin_unlock (& inode_hash_lock );
1087- destroy_inode (inode );
1088- return NULL ;
10891105}
10901106EXPORT_SYMBOL (iget5_locked );
10911107
@@ -1426,43 +1442,13 @@ EXPORT_SYMBOL(insert_inode_locked);
14261442int insert_inode_locked4 (struct inode * inode , unsigned long hashval ,
14271443 int (* test )(struct inode * , void * ), void * data )
14281444{
1429- struct super_block * sb = inode -> i_sb ;
1430- struct hlist_head * head = inode_hashtable + hash (sb , hashval );
1445+ struct inode * old = inode_insert5 (inode , hashval , test , NULL , data );
14311446
1432- while (1 ) {
1433- struct inode * old = NULL ;
1434-
1435- spin_lock (& inode_hash_lock );
1436- hlist_for_each_entry (old , head , i_hash ) {
1437- if (old -> i_sb != sb )
1438- continue ;
1439- if (!test (old , data ))
1440- continue ;
1441- spin_lock (& old -> i_lock );
1442- if (old -> i_state & (I_FREEING |I_WILL_FREE )) {
1443- spin_unlock (& old -> i_lock );
1444- continue ;
1445- }
1446- break ;
1447- }
1448- if (likely (!old )) {
1449- spin_lock (& inode -> i_lock );
1450- inode -> i_state |= I_NEW ;
1451- hlist_add_head (& inode -> i_hash , head );
1452- spin_unlock (& inode -> i_lock );
1453- spin_unlock (& inode_hash_lock );
1454- return 0 ;
1455- }
1456- __iget (old );
1457- spin_unlock (& old -> i_lock );
1458- spin_unlock (& inode_hash_lock );
1459- wait_on_inode (old );
1460- if (unlikely (!inode_unhashed (old ))) {
1461- iput (old );
1462- return - EBUSY ;
1463- }
1447+ if (old != inode ) {
14641448 iput (old );
1449+ return - EBUSY ;
14651450 }
1451+ return 0 ;
14661452}
14671453EXPORT_SYMBOL (insert_inode_locked4 );
14681454
0 commit comments