Skip to contentSkip to conversation

btrfs: Rework error detection in init_tree_roots

PATCH Superseded Nikolay Borisov 3 participants 7 messages Aug 4, 2020 – Aug 27, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: Rework error detection in init_tree_roots

To avoid duplicating 3 lines of code the error detection logic in init_tree_roots is somewhat quirky. It first checks for the presence of any error condition, then checks for the specific condition to perform any specific actions. That's spurious because directly checking for each respective error condition and doing the necessary steps is more obvious. Additionally it results in smaller code and the code reads more linearly:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-95 (-95)
Function old new delta
open_ctree 17243 17148 -95
Total: Before=113104, After=113009, chg -0.08%

---
 fs/btrfs/disk-io.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
fs/btrfs/disk-io.c +8 -9
index 5fc5f62228f1..ecb8ca53244f 100644
@@ -2645,17 +2645,16 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
 		level = btrfs_super_root_level(sb);
 		tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb),
 						  generation, level, NULL);
-		if (IS_ERR(tree_root->node) ||
-		    !extent_buffer_uptodate(tree_root->node)) {
+		if (IS_ERR(tree_root->node)) {
 			handle_error = true;
+			ret = PTR_ERR(tree_root->node);
+			tree_root->node = NULL;
+			btrfs_warn(fs_info, "failed to read tree root");
+			continue;
 
-			if (IS_ERR(tree_root->node)) {
-				ret = PTR_ERR(tree_root->node);
-				tree_root->node = NULL;
-			} else if (!extent_buffer_uptodate(tree_root->node)) {
-				ret = -EUCLEAN;
-			}
-
+		} else if (!extent_buffer_uptodate(tree_root->node)) {
+			handle_error = true;
+			ret = -EUCLEAN;
 			btrfs_warn(fs_info, "failed to read tree root");
 			continue;
 		}
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Nikolay Borisov
fs/btrfs/disk-io.c +5 -2
@@ -2645,17 +2645,16 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
 		level = btrfs_super_root_level(sb);
 		tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb),
 						  generation, level, NULL);
-		if (IS_ERR(tree_root->node) ||
-		    !extent_buffer_uptodate(tree_root->node)) {
+		if (IS_ERR(tree_root->node)) {
 			handle_error = true;
+			ret = PTR_ERR(tree_root->node);
+			tree_root->node = NULL;
+			btrfs_warn(fs_info, "failed to read tree root");
+			continue;

[...]

fs/btrfs/disk-io.c static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) · line 2660

Quoted 3 lines
fs/btrfs/disk-io.c +0 -0
 			btrfs_warn(fs_info, "failed to read tree root");
 			continue;
 		}

Now we're duplicating the warning message. I think it's better to have two distinct messages so we can differentiate which of the two failure cases happened.

The 2nd one could be something like "tree root eb not uptodate".

Otherwise looks good,

#2 lore raw
Quoted 16 lines · Johannes Thumshirn
Quoted 12 lines · Nikolay Borisov
fs/btrfs/disk-io.c +5 -2
@@ -2645,17 +2645,16 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
 		level = btrfs_super_root_level(sb);
 		tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb),
 						  generation, level, NULL);
-		if (IS_ERR(tree_root->node) ||
-		    !extent_buffer_uptodate(tree_root->node)) {
+		if (IS_ERR(tree_root->node)) {
 			handle_error = true;
+			ret = PTR_ERR(tree_root->node);
+			tree_root->node = NULL;
+			btrfs_warn(fs_info, "failed to read tree root");
+			continue;

[...]

Quoted 8 lines

fs/btrfs/disk-io.c static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) · line 2660

Quoted 3 lines
fs/btrfs/disk-io.c +0 -0
 			btrfs_warn(fs_info, "failed to read tree root");
 			continue;
 		}

Now we're duplicating the warning message. I think it's better to have two distinct messages so we can differentiate which of the two failure cases happened.

The 2nd one could be something like "tree root eb not uptodate".

Sure, I'm happy too replace it with whatever is more informative. Will take another look at the code and see what I can derive.

Quoted 4 lines

Otherwise looks good,

#3 lore raw
Quoted 23 lines · Nikolay Borisov
Quoted 20 lines · Johannes Thumshirn
Quoted 12 lines · Nikolay Borisov
fs/btrfs/disk-io.c +5 -2
@@ -2645,17 +2645,16 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
 		level = btrfs_super_root_level(sb);
 		tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb),
 						  generation, level, NULL);
-		if (IS_ERR(tree_root->node) ||
-		    !extent_buffer_uptodate(tree_root->node)) {
+		if (IS_ERR(tree_root->node)) {
 			handle_error = true;
+			ret = PTR_ERR(tree_root->node);
+			tree_root->node = NULL;
+			btrfs_warn(fs_info, "failed to read tree root");
+			continue;

[...]

Quoted 3 lines
fs/btrfs/disk-io.c +0 -0
 			btrfs_warn(fs_info, "failed to read tree root");
 			continue;
 		}
Quoted 7 lines
Quoted 4 lines

Now we're duplicating the warning message. I think it's better to have two distinct messages so we can differentiate which of the two failure cases happened.

The 2nd one could be something like "tree root eb not uptodate".

Sure, I'm happy too replace it with whatever is more informative. Will take another look at the code and see what I can derive.

The errors are different, IS_ERR is because the block was not read at all for some reason, extent_buffer_uptodate is EIO in all other places that do this kind of check.

Here it's EUCLEAN and it's been like that since the beginning but I think it should be EIO.

#4 lore raw

PATCH v2 1/1 btrfs: Rework error detection in init_tree_roots

In reply to Nikolay Borisov

To avoid duplicating 3 lines of code the error detection logic in init_tree_roots is somewhat quirky. It first checks for the presence of any error condition, then checks for the specific condition to perform any specific actions. That's spurious because directly checking for each respective error condition and doing the necessary steps is more obvious. While at it change the -EUCLEAN to -EIO in case the extent buffer is not read correctly, this is in line with other sites which return -EIO when the eb couldn't be read.

Additionally it results in smaller code and the code reads more linearly:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-95 (-95)
Function old new delta
open_ctree 17243 17148 -95
Total: Before=113104, After=113009, chg -0.08%

---

Changes in v2:
* Return -EIO in case of !extent_buffer_uptodate
* Change the error messages to distinguish both cases albeit they are still rather similar.

 fs/btrfs/disk-io.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
fs/btrfs/disk-io.c +9 -10
index 2f4169231992..2483648f2915 100644
@@ -2624,18 +2624,17 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
 		level = btrfs_super_root_level(sb);
 		tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb),
 						  generation, level, NULL);
-		if (IS_ERR(tree_root->node) ||
-		    !extent_buffer_uptodate(tree_root->node)) {
+		if (IS_ERR(tree_root->node)) {
 			handle_error = true;
+			ret = PTR_ERR(tree_root->node);
+			tree_root->node = NULL;
+			btrfs_warn(fs_info, "Couldn't read tree root");
+			continue;
 
-			if (IS_ERR(tree_root->node)) {
-				ret = PTR_ERR(tree_root->node);
-				tree_root->node = NULL;
-			} else if (!extent_buffer_uptodate(tree_root->node)) {
-				ret = -EUCLEAN;
-			}
-
-			btrfs_warn(fs_info, "failed to read tree root");
+		} else if (!extent_buffer_uptodate(tree_root->node)) {
+			handle_error = true;
+			ret = -EIO;
+			btrfs_warn(fs_info, "Error while reading tree root");
 			continue;
 		}
 

--
2.17.1

#5 lore raw
Quoted 17 lines · Nikolay Borisov

To avoid duplicating 3 lines of code the error detection logic in init_tree_roots is somewhat quirky. It first checks for the presence of any error condition, then checks for the specific condition to perform any specific actions. That's spurious because directly checking for each respective error condition and doing the necessary steps is more obvious. While at it change the -EUCLEAN to -EIO in case the extent buffer is not read correctly, this is in line with other sites which return -EIO when the eb couldn't be read.

Additionally it results in smaller code and the code reads more linearly:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-95 (-95)
Function old new delta
open_ctree 17243 17148 -95
Total: Before=113104, After=113009, chg -0.08%

Quoted 7 lines

---

Changes in v2:
* Return -EIO in case of !extent_buffer_uptodate
* Change the error messages to distinguish both cases albeit they are still rather similar.

Any comments on V2 ?

#6 lore raw
Quoted 23 lines · Nikolay Borisov
Quoted 20 lines · Nikolay Borisov

To avoid duplicating 3 lines of code the error detection logic in init_tree_roots is somewhat quirky. It first checks for the presence of any error condition, then checks for the specific condition to perform any specific actions. That's spurious because directly checking for each respective error condition and doing the necessary steps is more obvious. While at it change the -EUCLEAN to -EIO in case the extent buffer is not read correctly, this is in line with other sites which return -EIO when the eb couldn't be read.

Additionally it results in smaller code and the code reads more linearly:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-95 (-95)
Function old new delta
open_ctree 17243 17148 -95
Total: Before=113104, After=113009, chg -0.08%

---

Quoted 6 lines
Quoted 4 lines

Changes in v2:
* Return -EIO in case of !extent_buffer_uptodate
* Change the error messages to distinguish both cases albeit they are still rather similar.

Any comments on V2 ?

Code is ok, the messages should not start with an uppercase letter, fixed.