@@ -3759,8 +3759,41 @@ static int btrfs_init_file_extent_tree(struct btrfs_inode *inode)
3759
3759
return 0 ;
3760
3760
}
3761
3761
3762
+ static int btrfs_add_inode_to_root (struct btrfs_inode * inode , bool prealloc )
3763
+ {
3764
+ struct btrfs_root * root = inode -> root ;
3765
+ struct btrfs_inode * existing ;
3766
+ const u64 ino = btrfs_ino (inode );
3767
+ int ret ;
3768
+
3769
+ if (inode_unhashed (& inode -> vfs_inode ))
3770
+ return 0 ;
3771
+
3772
+ if (prealloc ) {
3773
+ ret = xa_reserve (& root -> inodes , ino , GFP_NOFS );
3774
+ if (ret )
3775
+ return ret ;
3776
+ }
3777
+
3778
+ existing = xa_store (& root -> inodes , ino , inode , GFP_ATOMIC );
3779
+
3780
+ if (xa_is_err (existing )) {
3781
+ ret = xa_err (existing );
3782
+ ASSERT (ret != - EINVAL );
3783
+ ASSERT (ret != - ENOMEM );
3784
+ return ret ;
3785
+ } else if (existing ) {
3786
+ WARN_ON (!(existing -> vfs_inode .i_state & (I_WILL_FREE | I_FREEING )));
3787
+ }
3788
+
3789
+ return 0 ;
3790
+ }
3791
+
3762
3792
/*
3763
- * read an inode from the btree into the in-memory inode
3793
+ * Read a locked inode from the btree into the in-memory inode and add it to
3794
+ * its root list/tree.
3795
+ *
3796
+ * On failure clean up the inode.
3764
3797
*/
3765
3798
static int btrfs_read_locked_inode (struct inode * inode ,
3766
3799
struct btrfs_path * in_path )
@@ -3780,7 +3813,7 @@ static int btrfs_read_locked_inode(struct inode *inode,
3780
3813
3781
3814
ret = btrfs_init_file_extent_tree (BTRFS_I (inode ));
3782
3815
if (ret )
3783
- return ret ;
3816
+ goto out ;
3784
3817
3785
3818
ret = btrfs_fill_inode (inode , & rdev );
3786
3819
if (!ret )
@@ -3789,7 +3822,7 @@ static int btrfs_read_locked_inode(struct inode *inode,
3789
3822
if (!path ) {
3790
3823
path = btrfs_alloc_path ();
3791
3824
if (!path )
3792
- return - ENOMEM ;
3825
+ goto out ;
3793
3826
}
3794
3827
3795
3828
btrfs_get_inode_key (BTRFS_I (inode ), & location );
@@ -3798,7 +3831,13 @@ static int btrfs_read_locked_inode(struct inode *inode,
3798
3831
if (ret ) {
3799
3832
if (path != in_path )
3800
3833
btrfs_free_path (path );
3801
- return ret ;
3834
+ /*
3835
+ * ret > 0 can come from btrfs_search_slot called by
3836
+ * btrfs_lookup_inode(), this means the inode was not found.
3837
+ */
3838
+ if (ret > 0 )
3839
+ ret = - ENOENT ;
3840
+ goto out ;
3802
3841
}
3803
3842
3804
3843
leaf = path -> nodes [0 ];
@@ -3961,7 +4000,15 @@ static int btrfs_read_locked_inode(struct inode *inode,
3961
4000
}
3962
4001
3963
4002
btrfs_sync_inode_flags_to_i_flags (inode );
4003
+
4004
+ ret = btrfs_add_inode_to_root (BTRFS_I (inode ), true);
4005
+ if (ret )
4006
+ goto out ;
4007
+
3964
4008
return 0 ;
4009
+ out :
4010
+ iget_failed (inode );
4011
+ return ret ;
3965
4012
}
3966
4013
3967
4014
/*
@@ -5470,35 +5517,7 @@ static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5470
5517
return err ;
5471
5518
}
5472
5519
5473
- static int btrfs_add_inode_to_root (struct btrfs_inode * inode , bool prealloc )
5474
- {
5475
- struct btrfs_root * root = inode -> root ;
5476
- struct btrfs_inode * existing ;
5477
- const u64 ino = btrfs_ino (inode );
5478
- int ret ;
5479
5520
5480
- if (inode_unhashed (& inode -> vfs_inode ))
5481
- return 0 ;
5482
-
5483
- if (prealloc ) {
5484
- ret = xa_reserve (& root -> inodes , ino , GFP_NOFS );
5485
- if (ret )
5486
- return ret ;
5487
- }
5488
-
5489
- existing = xa_store (& root -> inodes , ino , inode , GFP_ATOMIC );
5490
-
5491
- if (xa_is_err (existing )) {
5492
- ret = xa_err (existing );
5493
- ASSERT (ret != - EINVAL );
5494
- ASSERT (ret != - ENOMEM );
5495
- return ret ;
5496
- } else if (existing ) {
5497
- WARN_ON (!(existing -> vfs_inode .i_state & (I_WILL_FREE | I_FREEING )));
5498
- }
5499
-
5500
- return 0 ;
5501
- }
5502
5521
5503
5522
static void btrfs_del_inode_from_root (struct btrfs_inode * inode )
5504
5523
{
@@ -5579,25 +5598,11 @@ struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root,
5579
5598
return inode ;
5580
5599
5581
5600
ret = btrfs_read_locked_inode (inode , path );
5582
- /*
5583
- * ret > 0 can come from btrfs_search_slot called by
5584
- * btrfs_read_locked_inode(), this means the inode item was not found.
5585
- */
5586
- if (ret > 0 )
5587
- ret = - ENOENT ;
5588
- if (ret < 0 )
5589
- goto error ;
5590
-
5591
- ret = btrfs_add_inode_to_root (BTRFS_I (inode ), true);
5592
- if (ret < 0 )
5593
- goto error ;
5601
+ if (ret )
5602
+ return ERR_PTR (ret );
5594
5603
5595
5604
unlock_new_inode (inode );
5596
-
5597
5605
return inode ;
5598
- error :
5599
- iget_failed (inode );
5600
- return ERR_PTR (ret );
5601
5606
}
5602
5607
5603
5608
struct inode * btrfs_iget (u64 ino , struct btrfs_root * root )
0 commit comments