btrfs-progs: add repair support to delete orphan
cover btrfs-progs: add repair support to delete orphan
This is the same PR in github:
https://github.com/kdave/btrfs-progs/pull/1083
The mail thread is mostly for tracking and review purposes.
Since v6.16.1 btrfs check adds the ability to detect orphan free space entries, and older mkfs.btrfs forgot to delete free space entries of temporary chunks, which can be detected by newer btrfs-progs now.
Although we're already pushing for a kernel auto fix, the kernel fix will take some time to reach upstream, and more time to reach distros.
So there is still some value to have the repair ability in btrfs-check.
Furthermore, the new repair ability is more flex than the kernel one, which can handle orphan free space info key, not only the ones exposed by the recent btrfs-check.
Although only the recent exposed one is tested.
Qu Wenruo (3):
btrfs-progs: check: report all orphan free space info keys
btrfs-progs: check: add repair support for orphan fst entry
btrfs-progs: fsck-tests: add a new test case for deleting orphan fst
entries
common/clear-cache.c | 141 +++++++++++++++++- .../071-orphan-fst-entry/.lowmem_repairable | 0 .../orphan_fst_entries.img.xz | Bin 0 -> 1864 bytes 3 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 tests/fsck-tests/071-orphan-fst-entry/.lowmem_repairable create mode 100644 tests/fsck-tests/071-orphan-fst-entry/orphan_fst_entries.img.xz
--
2.52.0
PATCH 1/3 btrfs-progs: check: report all orphan free space info keys
And also output the logical and length for the orphan space info key.
Now the new reports will looks like this:
[4/8] checking free space tree
Space key logical 1048576 length 4194304 has no corresponding block group Space key logical 5242880 length 8388608 has no corresponding block group
This shouldn't affect end users that much, but provide much better debug info for developers who want to fix those bad free space tree entries.
--- common/clear-cache.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
common/clear-cache.c +8 -4
@@ -138,6 +138,7 @@ static int check_free_space_tree(struct btrfs_root *root)struct btrfs_key key = {0 };struct btrfs_path path = {0 };int ret =0 ; +bool found_orphan = false;while (1 ) {struct btrfs_block_group *bg;
@@ -167,16 +168,19 @@ static int check_free_space_tree(struct btrfs_root *root) bg =btrfs_lookup_block_group (fs_info, key.objectid);if (!bg) {fprintf (stderr, -"We have a space info key for a block group that doesn't exist\n" ); - ret = -EINVAL ; - goto out; +"Space key logical %llu length %llu has no corresponding block group\n" , + key.objectid, key.offset); + found_orphan = true; }btrfs_release_path (&path); key.objectid += key.offset; key.offset =0 ; } - ret =0 ; +if (found_orphan) + ret = -EINVAL ; +else + ret =0 ; out:btrfs_release_path (&path);return ret;
Signature · 1 line
2.52.0
PATCH 2/3 btrfs-progs: check: add repair support for orphan fst entry
In reply to Qu WenruoAlthough we're pushing for the kernel auto fix, it may still take some time.
Meanwhile having a proper way to fix it in progs will never hurt.
The repair itself is a little more complex than the kernel one, which only needs to bother the entries before the first block group.
This progs version in theory is able to handle orphan entries at any location, but for now it's only for the entries before the first bg.
--- common/clear-cache.c | 131 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 1 deletion(-)
common/clear-cache.c +130 -1
@@ -29,6 +29,7 @@#include "kernel-shared/volumes.h" #include "kernel-shared/transaction.h" #include "kernel-shared/file-item.h" +#include "kernel-shared/print-tree.h" #include "common/internal.h" #include "common/messages.h" #include "common/clear-cache.h"
@@ -132,6 +133,128 @@ close_out:return ret; } +/* +* Return 0 if we found an fst entry in range [start, start + length), @path will +* be updated to pointing to that entry. +* +* Return >0 if we found no more fst entry in range [start, start + length). +* +* Return <0 for error. +*/ +static int find_first_fst_entry (struct btrfs_root *root,struct btrfs_path *path, +u64 start,u64 length) +{ +struct btrfs_key key = { .objectid = start }; +int ret; + + ret =btrfs_search_slot (NULL , root, &key, path,0 ,0 ); +if (unlikely (ret <0 )) + goto out; +if (unlikely (ret ==0 )) { + ret = -EUCLEAN ; +error ("unexpected key found in slot %u\n" , path->slots[0 ]); +btrfs_print_leaf (path->nodes[0 ]); + goto out; + } +while (true) { +if (path->slots[0 ] >=btrfs_header_nritems (path->nodes[0 ])) { + ret =btrfs_next_leaf (root, path); +if (ret) + goto out; + } +btrfs_item_key_to_cpu (path->nodes[0 ], &key, path->slots[0 ]); +/* Beyond the range. No more entry. */ +if (key.objectid >= start + length) { + ret =1 ; + goto out; + } +/* In the range, return this one. */ +if (key.objectid >= start && key.objectid < start + length) +break ; + +/* The current key is small than our range, continue searching. */ + path->slots[0 ]++; + } + +/* We found a key in our range. */ +UASSERT (path->nodes[0 ]); +return 0 ; +out: +UASSERT (ret !=0 ); +btrfs_release_path (path); +return ret; +} + +static int remove_free_space_entries (struct btrfs_root *root,struct btrfs_path *path, +const struct btrfs_key *space_info_key) +{ +struct btrfs_trans_handle *trans =NULL ; +u64 start = space_info_key->objectid; +const u64 end = start + space_info_key->offset -1 ; +u64 cur = start; +int ret; + +while (cur <= end) { +struct btrfs_key found_key; +int found_slot; +int last_slot; + + ret =find_first_fst_entry (root, path, cur, end +1 - cur); +if (ret <0 ) + goto error; +if (ret >0 ) +break ; + +btrfs_item_key_to_cpu (path->nodes[0 ], &found_key, path->slots[0 ]); +btrfs_release_path (path); + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { +error_msg (ERROR_MSG_START_TRANS ,"remove orphan fst entry" ); +return PTR_ERR (trans); + } + ret =btrfs_search_slot (trans, root, &found_key, path,-1 ,1 ); +if (ret >0 ) + ret = -ENOENT ; +if (ret <0 ) + goto error; + + found_slot = path->slots[0 ]; + +/* +* @last_slot will be the next slot of the last item which matches +* our range. +*/ +for (last_slot = found_slot +1 ; + last_slot <btrfs_header_nritems (path->nodes[0 ]); last_slot++){ +btrfs_item_key_to_cpu (path->nodes[0 ], &found_key, last_slot); +if (found_key.objectid >= start && found_key.objectid <= end) + cur = found_key.objectid; +else +break ; + } + ret =btrfs_del_items (trans, root, path, found_slot, + last_slot - found_slot); +if (ret <0 ) + goto error; +btrfs_release_path (path); + ret =btrfs_commit_transaction (trans, root); + trans =NULL ; +if (ret <0 ) { +error_msg (ERROR_MSG_START_TRANS ,"remove orphan fst entry" ); + goto error; + } + } +printf ("deleted orphan fst entries for range [%llu, %llu)\n" , + start, end +1 ); +return 0 ; +error: +btrfs_release_path (path); +if (trans) +btrfs_abort_transaction (trans, ret); +return ret; +} +static int check_free_space_tree (struct btrfs_root *root) {struct btrfs_fs_info *fs_info = root->fs_info;
@@ -167,10 +290,16 @@ static int check_free_space_tree(struct btrfs_root *root) bg =btrfs_lookup_block_group (fs_info, key.objectid);if (!bg) { +btrfs_release_path (&path);fprintf (stderr,"Space key logical %llu length %llu has no corresponding block group\n" , key.objectid, key.offset); - found_orphan = true; +if (opt_check_repair) + ret =remove_free_space_entries (root, &path, &key); +else + ret = -EINVAL ; +if (ret <0 ) + found_orphan = true; }btrfs_release_path (&path);
Signature · 1 line
2.52.0
PATCH 3/3 btrfs-progs: fsck-tests: add a new test case for deleting orphan fst entries
In reply to Qu WenruoThe image is created by creating an empty btrfs with older v6.16 btrfs-progs. Which has free space tree entries for two temporary block groups.
And the test itself is going through the common repair test procedure.
--- .../071-orphan-fst-entry/.lowmem_repairable | 0 .../orphan_fst_entries.img.xz | Bin 0 -> 1864 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/fsck-tests/071-orphan-fst-entry/.lowmem_repairable create mode 100644 tests/fsck-tests/071-orphan-fst-entry/orphan_fst_entries.img.xz
tests/fsck-tests/071-orphan-fst-entry/.lowmem_repairable new file +0 -0
tests/fsck-tests/071-orphan-fst-entry/orphan_fst_entries.img.xz new file binary +0 -0
Signature · 1 line
2.52.0