Skip to contentSkip to conversation

btrfs: clear free space tree state on rebuild failure

PATCH Superseded Guanghui Yang 4 participants 8 messages Sep 15 – Sep 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: clear free space tree state on rebuild failure

btrfs_rebuild_free_space_tree() sets BTRFS_FS_CREATING_FREE_SPACE_TREE and BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED before rebuilding the free space tree. Several error paths return without clearing these flags.

This leaves the flags set on the live filesystem and affects delayed reference processing and free-space caching. Clear both flags on all free space tree rebuild failure paths.

Fixes 1d6a4fc85717 ("btrfs: make clear_cache mount option to rebuild FST without disabling it")
Assisted-by Codex:gpt-5

Tested: make -j$(nproc) fs/btrfs/free-space-tree.o

---
 fs/btrfs/free-space-tree.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
fs/btrfs/free-space-tree.c +11 -4
index 1b3d82ae3de8..1410da2766a9 100644
@@ -1353,7 +1353,7 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 	if (unlikely(ret)) {
 		btrfs_abort_transaction(trans, ret);
 		btrfs_end_transaction(trans);
-		return ret;
+		goto out_clear;
 	}
 
 	node = rb_first_cached(&fs_info->block_group_cache_tree);
@@ -1371,14 +1371,16 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 		if (unlikely(ret)) {
 			btrfs_abort_transaction(trans, ret);
 			btrfs_end_transaction(trans);
-			return ret;
+			goto out_clear;
 		}
 next:
 		if (btrfs_should_end_transaction(trans)) {
 			btrfs_end_transaction(trans);
 			trans = btrfs_start_transaction(free_space_root, 1);
-			if (IS_ERR(trans))
-				return PTR_ERR(trans);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+				goto out_clear;
+			}
 		}
 		node = rb_next(node);
 	}
@@ -1390,6 +1392,11 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 	ret = btrfs_commit_transaction(trans);
 	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
 	return ret;
+
+out_clear:
+	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
+	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
+	return ret;
 }
 
 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,

base-commit: 587858367581b9c55c3690f4e63382ad622719d4

Signature · 1 line
2.34.1
#1 lore raw
Quoted 10 lines · Guanghui Yang

btrfs_rebuild_free_space_tree() sets BTRFS_FS_CREATING_FREE_SPACE_TREE and BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED before rebuilding the free space tree. Several error paths return without clearing these flags.

This leaves the flags set on the live filesystem and affects delayed reference processing and free-space caching. Clear both flags on all free space tree rebuild failure paths.

Fixes 1d6a4fc85717 ("btrfs: make clear_cache mount option to rebuild FST without disabling it")

I don't think this fixes tag is correct. In that version of the code, all error paths aborted the transaction at which point the bit being set or not doesn't really matter, in my opinion.

Unless I am missing something about the importance of clearing the bit on an aborted transaction, I think the better fixes tag is:
882af9f13e83 ("btrfs: handle free space tree rebuild in multiple transactions")

Quoted 3 lines
Assisted-by Codex:gpt-5

Tested: make -j$(nproc) fs/btrfs/free-space-tree.o

Is this all the testing you did or did you also run fstests on this change?

Generally looks like a good fix for start_transaction() errors, though.

Thanks,
Boris

#2 lore raw
In reply to Guanghui Yang
Quoted 7 lines · Guanghui Yang

btrfs_rebuild_free_space_tree() sets BTRFS_FS_CREATING_FREE_SPACE_TREE and BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED before rebuilding the free space tree. Several error paths return without clearing these flags.

This leaves the flags set on the live filesystem and affects delayed reference processing and free-space caching.

What kind of effect?

Quoted 7 lines

Clear both flags on all
free space tree rebuild failure paths.

Fixes 1d6a4fc85717 ("btrfs: make clear_cache mount option to rebuild FST without disabling it")
Assisted-by Codex:gpt-5

Tested: make -j$(nproc) fs/btrfs/free-space-tree.o

That's not helpful, you should run the full fstests instead.

Quoted 39 lines
---
  fs/btrfs/free-space-tree.c | 15 +++++++++++----
  1 file changed, 11 insertions(+), 4 deletions(-)
fs/btrfs/free-space-tree.c +6 -4
index 1b3d82ae3de8..1410da2766a9 100644
@@ -1353,7 +1353,7 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
  	if (unlikely(ret)) {
  		btrfs_abort_transaction(trans, ret);
  		btrfs_end_transaction(trans);
-		return ret;
+		goto out_clear;
  	}
 
  	node = rb_first_cached(&fs_info->block_group_cache_tree);
@@ -1371,14 +1371,16 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
  		if (unlikely(ret)) {
  			btrfs_abort_transaction(trans, ret);
  			btrfs_end_transaction(trans);
-			return ret;
+			goto out_clear;
  		}
  next:
  		if (btrfs_should_end_transaction(trans)) {
  			btrfs_end_transaction(trans);
  			trans = btrfs_start_transaction(free_space_root, 1);
-			if (IS_ERR(trans))
-				return PTR_ERR(trans);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+				goto out_clear;
+			}
  		}
  		node = rb_next(node);
  	}

fs/btrfs/free-space-tree.c int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info) · line 1398

Quoted 7 lines
fs/btrfs/free-space-tree.c +4 -0
@@ -1390,6 +1392,11 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
  	ret = btrfs_commit_transaction(trans);
  	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
  	return ret;
+
+out_clear:
+	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
+	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);

The free space tree is not properly rebuilt, thus we can not trust it anymore.

Why you want to clear the UNTRUSTED flag?

Quoted 6 lines
fs/btrfs/free-space-tree.c +1 -0
+	return ret;
  }
 
  static int __add_block_group_free_space(struct btrfs_trans_handle *trans,

base-commit: 587858367581b9c55c3690f4e63382ad622719d4

#3 lore raw

PATCH v2 1/1 btrfs: clear free space tree creation state on rebuild failure

In reply to Guanghui Yang

btrfs_rebuild_free_space_tree() sets BTRFS_FS_CREATING_FREE_SPACE_TREE before rebuilding the free space tree. Several error paths return without clearing this flag.

The transaction restart failure path can leave the flag set on a live filesystem, causing delayed reference processing to be skipped. Clear it on all free space tree rebuild failure paths. Keep BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED set, since a failed rebuild leaves the free space tree untrusted. Callers must fall back to extent-tree caching.

Fixes 882af9f13e83 ("btrfs: handle free space tree rebuild in multiple transactions")
Assisted-by Codex:gpt-5

Tested: make -j$(nproc) fs/btrfs/free-space-tree.o

---
 fs/btrfs/free-space-tree.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
fs/btrfs/free-space-tree.c +10 -4
index 1b3d82ae3de8..b7a4a6ade30f 100644
@@ -1353,7 +1353,7 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 	if (unlikely(ret)) {
 		btrfs_abort_transaction(trans, ret);
 		btrfs_end_transaction(trans);
-		return ret;
+		goto out_clear;
 	}
 
 	node = rb_first_cached(&fs_info->block_group_cache_tree);
@@ -1371,14 +1371,16 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 		if (unlikely(ret)) {
 			btrfs_abort_transaction(trans, ret);
 			btrfs_end_transaction(trans);
-			return ret;
+			goto out_clear;
 		}
 next:
 		if (btrfs_should_end_transaction(trans)) {
 			btrfs_end_transaction(trans);
 			trans = btrfs_start_transaction(free_space_root, 1);
-			if (IS_ERR(trans))
-				return PTR_ERR(trans);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+				goto out_clear;
+			}
 		}
 		node = rb_next(node);
 	}
@@ -1390,6 +1392,10 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 	ret = btrfs_commit_transaction(trans);
 	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
 	return ret;
+
+out_clear:
+	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
+	return ret;
 }
 
 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,

base-commit: 587858367581b9c55c3690f4e63382ad622719d4

Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v2] btrfs: clear free space tree creation state on rebuild failure

Quoted 10 lines · Guanghui Yang

btrfs_rebuild_free_space_tree() sets BTRFS_FS_CREATING_FREE_SPACE_TREE before rebuilding the free space tree. Several error paths return without clearing this flag.

The transaction restart failure path can leave the flag set on a live filesystem, causing delayed reference processing to be skipped. Clear it on all free space tree rebuild failure paths. Keep BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED set, since a failed rebuild leaves the free space tree untrusted. Callers must fall back to extent-tree caching.

Quoted 4 lines
Fixes 882af9f13e83 ("btrfs: handle free space tree rebuild in multiple transactions")
Assisted-by Codex:gpt-5

Tested: make -j$(nproc) fs/btrfs/free-space-tree.o

Just remove the useless tag, and run a proper full fstests.

Quoted 51 lines
---
  fs/btrfs/free-space-tree.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
fs/btrfs/free-space-tree.c +10 -4
index 1b3d82ae3de8..b7a4a6ade30f 100644
@@ -1353,7 +1353,7 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
  	if (unlikely(ret)) {
  		btrfs_abort_transaction(trans, ret);
  		btrfs_end_transaction(trans);
-		return ret;
+		goto out_clear;
  	}
 
  	node = rb_first_cached(&fs_info->block_group_cache_tree);
@@ -1371,14 +1371,16 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
  		if (unlikely(ret)) {
  			btrfs_abort_transaction(trans, ret);
  			btrfs_end_transaction(trans);
-			return ret;
+			goto out_clear;
  		}
  next:
  		if (btrfs_should_end_transaction(trans)) {
  			btrfs_end_transaction(trans);
  			trans = btrfs_start_transaction(free_space_root, 1);
-			if (IS_ERR(trans))
-				return PTR_ERR(trans);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+				goto out_clear;
+			}
  		}
  		node = rb_next(node);
  	}
@@ -1390,6 +1392,10 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
  	ret = btrfs_commit_transaction(trans);
  	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
  	return ret;
+
+out_clear:
+	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
+	return ret;
  }
 
  static int __add_block_group_free_space(struct btrfs_trans_handle *trans,

base-commit: 587858367581b9c55c3690f4e63382ad622719d4

#5 lore raw

PATCH v3 1/1 btrfs: clear free space tree creation state on rebuild failure

In reply to Guanghui Yang

btrfs_rebuild_free_space_tree() sets BTRFS_FS_CREATING_FREE_SPACE_TREE before rebuilding the free space tree. Several error paths return without clearing this flag.

The transaction restart failure path can leave the flag set on a live filesystem, causing delayed reference processing to be skipped. Clear it on all free space tree rebuild failure paths. Keep BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED set, since a failed rebuild leaves the free space tree untrusted. Callers must fall back to extent-tree caching.

Fixes 882af9f13e83 ("btrfs: handle free space tree rebuild in multiple transactions")

Tested:
xfstests btrfs/015 btrfs/032 btrfs/104 btrfs/131 btrfs/352 (pass) xfstests btrfs/quick: 261 passed, 30 failed, 90 not run on Ubuntu 5.15.0-60-generic

Assisted-by LLM
---
 fs/btrfs/free-space-tree.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
fs/btrfs/free-space-tree.c +10 -4
index 1b3d82ae3de8..b7a4a6ade30f 100644
@@ -1353,7 +1353,7 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 	if (unlikely(ret)) {
 		btrfs_abort_transaction(trans, ret);
 		btrfs_end_transaction(trans);
-		return ret;
+		goto out_clear;
 	}
 
 	node = rb_first_cached(&fs_info->block_group_cache_tree);
@@ -1371,14 +1371,16 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 		if (unlikely(ret)) {
 			btrfs_abort_transaction(trans, ret);
 			btrfs_end_transaction(trans);
-			return ret;
+			goto out_clear;
 		}
 next:
 		if (btrfs_should_end_transaction(trans)) {
 			btrfs_end_transaction(trans);
 			trans = btrfs_start_transaction(free_space_root, 1);
-			if (IS_ERR(trans))
-				return PTR_ERR(trans);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+				goto out_clear;
+			}
 		}
 		node = rb_next(node);
 	}
@@ -1390,6 +1392,10 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 	ret = btrfs_commit_transaction(trans);
 	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
 	return ret;
+
+out_clear:
+	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
+	return ret;
 }
 
 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,

base-commit: 587858367581b9c55c3690f4e63382ad622719d4

Signature · 1 line
2.34.1
Reviewed-by on v3 1/1
#6 lore raw

Re: [PATCH v3] btrfs: clear free space tree creation state on rebuild failure

Quoted 10 lines · Guanghui Yang

btrfs_rebuild_free_space_tree() sets BTRFS_FS_CREATING_FREE_SPACE_TREE before rebuilding the free space tree. Several error paths return without clearing this flag.

The transaction restart failure path can leave the flag set on a live filesystem, causing delayed reference processing to be skipped. Clear it on all free space tree rebuild failure paths. Keep BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED set, since a failed rebuild leaves the free space tree untrusted. Callers must fall back to extent-tree caching.

Quoted 6 lines
Fixes 882af9f13e83 ("btrfs: handle free space tree rebuild in multiple transactions")

Tested:

   xfstests btrfs/015 btrfs/032 btrfs/104 btrfs/131 btrfs/352 (pass)
   xfstests btrfs/quick: 261 passed, 30 failed, 90 not run on
   Ubuntu 5.15.0-60-generic

WTF? You're not even running the tests with an upstream kernel with your patch applied?

Thankfully the patch still looks good to me.

I'll remove the stupid tag, and finally stop wasting time on someone who doesn't have any brain cell to properly test the patch.

Quoted 52 lines
Assisted-by LLM
---
  fs/btrfs/free-space-tree.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
fs/btrfs/free-space-tree.c +10 -4
index 1b3d82ae3de8..b7a4a6ade30f 100644
@@ -1353,7 +1353,7 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
  	if (unlikely(ret)) {
  		btrfs_abort_transaction(trans, ret);
  		btrfs_end_transaction(trans);
-		return ret;
+		goto out_clear;
  	}
 
  	node = rb_first_cached(&fs_info->block_group_cache_tree);
@@ -1371,14 +1371,16 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
  		if (unlikely(ret)) {
  			btrfs_abort_transaction(trans, ret);
  			btrfs_end_transaction(trans);
-			return ret;
+			goto out_clear;
  		}
  next:
  		if (btrfs_should_end_transaction(trans)) {
  			btrfs_end_transaction(trans);
  			trans = btrfs_start_transaction(free_space_root, 1);
-			if (IS_ERR(trans))
-				return PTR_ERR(trans);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+				goto out_clear;
+			}
  		}
  		node = rb_next(node);
  	}
@@ -1390,6 +1392,10 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
  	ret = btrfs_commit_transaction(trans);
  	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
  	return ret;
+
+out_clear:
+	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
+	return ret;
  }
 
  static int __add_block_group_free_space(struct btrfs_trans_handle *trans,

base-commit: 587858367581b9c55c3690f4e63382ad622719d4

Reviewed-by on v3 1/1
#7 lore raw

Re: [PATCH v3] btrfs: clear free space tree creation state on rebuild failure

In reply to Guanghui Yang
Quoted 10 lines · Guanghui Yang

btrfs_rebuild_free_space_tree() sets BTRFS_FS_CREATING_FREE_SPACE_TREE before rebuilding the free space tree. Several error paths return without clearing this flag.

The transaction restart failure path can leave the flag set on a live filesystem, causing delayed reference processing to be skipped. Clear it on all free space tree rebuild failure paths. Keep BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED set, since a failed rebuild leaves the free space tree untrusted. Callers must fall back to extent-tree caching.

Quoted 7 lines
Fixes 882af9f13e83 ("btrfs: handle free space tree rebuild in multiple transactions")

Tested:
xfstests btrfs/015 btrfs/032 btrfs/104 btrfs/131 btrfs/352 (pass) xfstests btrfs/quick: 261 passed, 30 failed, 90 not run on Ubuntu 5.15.0-60-generic

Assisted-by LLM
Quoted 54 lines
---
 fs/btrfs/free-space-tree.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
fs/btrfs/free-space-tree.c +10 -4
index 1b3d82ae3de8..b7a4a6ade30f 100644
@@ -1353,7 +1353,7 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 	if (unlikely(ret)) {
 		btrfs_abort_transaction(trans, ret);
 		btrfs_end_transaction(trans);
-		return ret;
+		goto out_clear;
 	}
 
 	node = rb_first_cached(&fs_info->block_group_cache_tree);
@@ -1371,14 +1371,16 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 		if (unlikely(ret)) {
 			btrfs_abort_transaction(trans, ret);
 			btrfs_end_transaction(trans);
-			return ret;
+			goto out_clear;
 		}
 next:
 		if (btrfs_should_end_transaction(trans)) {
 			btrfs_end_transaction(trans);
 			trans = btrfs_start_transaction(free_space_root, 1);
-			if (IS_ERR(trans))
-				return PTR_ERR(trans);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+				goto out_clear;
+			}
 		}
 		node = rb_next(node);
 	}
@@ -1390,6 +1392,10 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
 	ret = btrfs_commit_transaction(trans);
 	clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
 	return ret;
+
+out_clear:
+	clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
+	return ret;
 }
 
 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,

base-commit: 587858367581b9c55c3690f4e63382ad622719d4

Signature · 1 line
2.34.1