@@ -1840,6 +1840,33 @@ struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
18401840 return inode ;
18411841}
18421842
1843+ /* Get an inode object given its location and corresponding root.
1844+ * Returns in *is_new if the inode was read from disk
1845+ */
1846+ struct inode * btrfs_iget (struct super_block * s , struct btrfs_key * location ,
1847+ struct btrfs_root * root , int * is_new )
1848+ {
1849+ struct inode * inode ;
1850+
1851+ inode = btrfs_iget_locked (s , location -> objectid , root );
1852+ if (!inode )
1853+ return ERR_PTR (- EACCES );
1854+
1855+ if (inode -> i_state & I_NEW ) {
1856+ BTRFS_I (inode )-> root = root ;
1857+ memcpy (& BTRFS_I (inode )-> location , location , sizeof (* location ));
1858+ btrfs_read_locked_inode (inode );
1859+ unlock_new_inode (inode );
1860+ if (is_new )
1861+ * is_new = 1 ;
1862+ } else {
1863+ if (is_new )
1864+ * is_new = 0 ;
1865+ }
1866+
1867+ return inode ;
1868+ }
1869+
18431870static struct dentry * btrfs_lookup (struct inode * dir , struct dentry * dentry ,
18441871 struct nameidata * nd )
18451872{
@@ -1848,7 +1875,7 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
18481875 struct btrfs_root * root = bi -> root ;
18491876 struct btrfs_root * sub_root = root ;
18501877 struct btrfs_key location ;
1851- int ret , do_orphan = 0 ;
1878+ int ret , new , do_orphan = 0 ;
18521879
18531880 if (dentry -> d_name .len > BTRFS_NAME_LEN )
18541881 return ERR_PTR (- ENAMETOOLONG );
@@ -1866,23 +1893,15 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
18661893 return ERR_PTR (ret );
18671894 if (ret > 0 )
18681895 return ERR_PTR (- ENOENT );
1869-
1870- inode = btrfs_iget_locked (dir -> i_sb , location .objectid ,
1871- sub_root );
1872- if (!inode )
1873- return ERR_PTR (- EACCES );
1874- if (inode -> i_state & I_NEW ) {
1875- /* the inode and parent dir are two different roots */
1876- if (sub_root != root ) {
1877- igrab (inode );
1878- sub_root -> inode = inode ;
1879- do_orphan = 1 ;
1880- }
1881- BTRFS_I (inode )-> root = sub_root ;
1882- memcpy (& BTRFS_I (inode )-> location , & location ,
1883- sizeof (location ));
1884- btrfs_read_locked_inode (inode );
1885- unlock_new_inode (inode );
1896+ inode = btrfs_iget (dir -> i_sb , & location , sub_root , & new );
1897+ if (IS_ERR (inode ))
1898+ return ERR_CAST (inode );
1899+
1900+ /* the inode and parent dir are two different roots */
1901+ if (new && root != sub_root ) {
1902+ igrab (inode );
1903+ sub_root -> inode = inode ;
1904+ do_orphan = 1 ;
18861905 }
18871906 }
18881907
0 commit comments