Skip to contentSkip to conversation

btrfs: move all btree initialization into btrfs_init_btree_inode

PATCH In review Christoph Hellwig 5 participants 11 messages Feb 19, 2023 – Feb 14, 2024 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: move all btree initialization into btrfs_init_btree_inode

Move the remaining code that deals with initializing the btree inode into btrfs_init_btree_inode instead of splitting it between that helpers and its only caller.

---
 fs/btrfs/disk-io.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
fs/btrfs/disk-io.c +12 -8
index b53f0e30ce2b3b..981973b40b065a 100644
@@ -2125,11 +2125,16 @@ static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
 	atomic_set(&fs_info->reloc_cancel_req, 0);
 }
 
-static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
+static int btrfs_init_btree_inode(struct super_block *sb)
 {
-	struct inode *inode = fs_info->btree_inode;
+	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 	unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID,
 					      fs_info->tree_root);
+	struct inode *inode;
+
+	inode = new_inode(sb);
+	if (!inode)
+		return -ENOMEM;
 
 	inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
 	set_nlink(inode, 1);
@@ -2140,6 +2145,7 @@ static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
 	 */
 	inode->i_size = OFFSET_MAX;
 	inode->i_mapping->a_ops = &btree_aops;
+	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
 
 	RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
 	extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
@@ -2152,6 +2158,8 @@ static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
 	BTRFS_I(inode)->location.offset = 0;
 	set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
 	__insert_inode_hash(inode, hash);
+	fs_info->btree_inode = inode;
+	return 0;
 }
 
 static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
@@ -3351,13 +3359,9 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 		goto fail;
 	}
 
-	fs_info->btree_inode = new_inode(sb);
-	if (!fs_info->btree_inode) {
-		err = -ENOMEM;
+	err = btrfs_init_btree_inode(sb);
+	if (err)
 		goto fail;
-	}
-	mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
-	btrfs_init_btree_inode(fs_info);
 
 	invalidate_bdev(fs_devices->latest_dev->bdev);
 
Signature · 1 line
2.39.1
#3 lore raw
Quoted 8 lines · David Sterba
Quoted 5 lines · Christoph Hellwig

Move the remaining code that deals with initializing the btree inode into btrfs_init_btree_inode instead of splitting it between that helpers and its only caller.

Added to misc-next, thanks.

Btw, in case you need to rebase again, the subject needs a 'inode' after 'btree'.

#4 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · David Sterba
Quoted 4 lines · Christoph Hellwig

Move the remaining code that deals with initializing the btree inode into btrfs_init_btree_inode instead of splitting it between that helpers and its only caller.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Added to misc-next, thanks.

Btw, in case you need to rebase again, the subject needs a 'inode' after 'btree'.

I see, fixed, thanks.

Reviewed-by on v1 1/1
#5 lore raw
In reply to Christoph Hellwig
Quoted 55 lines · Christoph Hellwig

Move the remaining code that deals with initializing the btree inode into btrfs_init_btree_inode instead of splitting it between that helpers and its only caller.

---
  fs/btrfs/disk-io.c | 20 ++++++++++++--------
  1 file changed, 12 insertions(+), 8 deletions(-)
fs/btrfs/disk-io.c +10 -4
index b53f0e30ce2b3b..981973b40b065a 100644
@@ -2125,11 +2125,16 @@ static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
  	atomic_set(&fs_info->reloc_cancel_req, 0);
  }
 
-static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
+static int btrfs_init_btree_inode(struct super_block *sb)
  {
-	struct inode *inode = fs_info->btree_inode;
+	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  	unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID,
  					      fs_info->tree_root);
+	struct inode *inode;
+
+	inode = new_inode(sb);
+	if (!inode)
+		return -ENOMEM;
 
  	inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
  	set_nlink(inode, 1);
@@ -2140,6 +2145,7 @@ static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
  	 */
  	inode->i_size = OFFSET_MAX;
  	inode->i_mapping->a_ops = &btree_aops;
+	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
 
  	RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  	extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
@@ -2152,6 +2158,8 @@ static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
  	BTRFS_I(inode)->location.offset = 0;
  	set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  	__insert_inode_hash(inode, hash);
+	fs_info->btree_inode = inode;
+	return 0;
  }
 
  static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
@@ -3351,13 +3359,9 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
  		goto fail;
  	}
 
-	fs_info->btree_inode = new_inode(sb);
-	if (!fs_info->btree_inode) {

fs/btrfs/disk-io.c int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · line 3367

Quoted 10 lines
fs/btrfs/disk-io.c +2 -4
-		err = -ENOMEM;
+	err = btrfs_init_btree_inode(sb);
+	if (err)
  		goto fail;
-	}
-	mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
-	btrfs_init_btree_inode(fs_info);
 
  	invalidate_bdev(fs_devices->latest_dev->bdev);
 

Dave,

This patch is causing a regression, as reported here:

https://patchwork.kernel.org/project/linux-btrfs/patch/2de92bdcebd36e4119467797dedae8a9d97a9df3.1677314616.git.wqu@suse.com/

There are many child functions in open_ctree() that rely on the default value of @err, which is -EINVAL, to return an error instead of ret. The issue is that @err is being overwritten by the return value of btrfs_init_btree_inode() in this patch.

To fix this issue, please fold the following diff into the patch.

Once that's done, you can add:

fs/btrfs/disk-io.c +4 -2
index 48368d4bc331..0e0c30fe6df6 100644
@@ -3360,9 +3360,11 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
                goto fail;
        }
 
-       err = btrfs_init_btree_inode(sb);
-       if (err)
+       ret = btrfs_init_btree_inode(sb);
+       if (ret) {
+               err = ret;
                goto fail;
+       }
 
        invalidate_bdev(fs_devices->latest_dev->bdev);
 
#6 lore raw

Thanks Anand. The fix looks good to me. The error handling is a bit confsing and hopefully we can get it fixed up eventually.

Just curious how you found the error as I didn't see anything in xfstest runs.

#7 lore raw
In reply to Anand Jain
Quoted 8 lines · Anand Jain
Quoted 1 line · Christoph Hellwig

Dave,

This patch is causing a regression, as reported here:

Quoted 8 lines

https://patchwork.kernel.org/project/linux-btrfs/patch/2de92bdcebd36e4119467797dedae8a9d97a9df3.1677314616.git.wqu@suse.com/

There are many child functions in open_ctree() that rely on the default value of @err, which is -EINVAL, to return an error instead of ret. The issue is that @err is being overwritten by the return value of btrfs_init_btree_inode() in this patch.

To fix this issue, please fold the following diff into the patch.

Thanks. open_ctree has mixed the original and newish style of error variables. In some btrfs functions but more widely seen in other VFS code the error value is set once and the errors only go to return, while what I think we should unify is the error set right before goto or inherited from the call itself. It's a few lines more but the exact error value is at the place where it happens and not somewhere in the function and possibly changed on any line between. Feel free to clean up open_ctree().

Reviewed-by on v1 1/1
#8 lore raw
In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig

Move the remaining code that deals with initializing the btree inode into btrfs_init_btree_inode instead of splitting it between that helpers and its only caller.

Just one small nitpick.

Quoted 9 lines
---
  fs/btrfs/disk-io.c | 20 ++++++++++++--------
  1 file changed, 12 insertions(+), 8 deletions(-)
fs/btrfs/disk-io.c +0 -0
index b53f0e30ce2b3b..981973b40b065a 100644

fs/btrfs/disk-io.c static void btrfs_init_balance(struct btrfs_fs_info *fs_info) · line 2128

Quoted 5 lines
fs/btrfs/disk-io.c +1 -1
@@ -2125,11 +2125,16 @@ static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
  	atomic_set(&fs_info->reloc_cancel_req, 0);
  }
 
-static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
+static int btrfs_init_btree_inode(struct super_block *sb)

It's preferred to use btrfs_fs_info * as a parameter, just to keep the consistency of all btrfs interfaces.

Thanks,
Qu

Quoted 46 lines
fs/btrfs/disk-io.c +11 -7
  {
-	struct inode *inode = fs_info->btree_inode;
+	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  	unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID,
  					      fs_info->tree_root);
+	struct inode *inode;
+
+	inode = new_inode(sb);
+	if (!inode)
+		return -ENOMEM;
 
  	inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
  	set_nlink(inode, 1);
@@ -2140,6 +2145,7 @@ static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
  	 */
  	inode->i_size = OFFSET_MAX;
  	inode->i_mapping->a_ops = &btree_aops;
+	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
 
  	RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  	extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
@@ -2152,6 +2158,8 @@ static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
  	BTRFS_I(inode)->location.offset = 0;
  	set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  	__insert_inode_hash(inode, hash);
+	fs_info->btree_inode = inode;
+	return 0;
  }
 
  static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
@@ -3351,13 +3359,9 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
  		goto fail;
  	}
 
-	fs_info->btree_inode = new_inode(sb);
-	if (!fs_info->btree_inode) {
-		err = -ENOMEM;
+	err = btrfs_init_btree_inode(sb);
+	if (err)
  		goto fail;
-	}
-	mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
-	btrfs_init_btree_inode(fs_info);
 
  	invalidate_bdev(fs_devices->latest_dev->bdev);
 
#9 lore raw
Quoted 7 lines · Qu Wenruo
Quoted 4 lines · Christoph Hellwig

Move the remaining code that deals with initializing the btree inode into btrfs_init_btree_inode instead of splitting it between that helpers and its only caller.

Quoted 5 lines
Quoted 1 line

Just one small nitpick.

The patch is almost one year old, it does not make much sense to send reviews, if there's something to be fixed please send a new patch.

#10 lore raw
Quoted 8 lines · David Sterba
Quoted 7 lines · Qu Wenruo
Quoted 4 lines · Christoph Hellwig

Move the remaining code that deals with initializing the btree inode into btrfs_init_btree_inode instead of splitting it between that helpers and its only caller.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Just one small nitpick.

The patch is almost one year old, it does not make much sense to send reviews, if there's something to be fixed please send a new patch.

My bad, I'm re-setting up my mail client, and this one popped up and I thought it's new...

Sorry for the noise,
Qu