btrfs-progs: move btrfs_fix_block_accounting to repair.c

We have this helper sitting in extent-tree.c, but it's a repair
function.  I'm going to need to make changes to this for extent-tree-v2
and would rather this live outside of the code we need to share with the
kernel.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Josef Bacik 2021-11-10 15:08:01 -05:00 committed by David Sterba
parent 29e56c7e05
commit 550fd48136
4 changed files with 77 additions and 74 deletions

View File

@ -17,6 +17,7 @@
*/
#include "kernel-shared/ctree.h"
#include "kernel-shared/transaction.h"
#include "common/extent-cache.h"
#include "common/utils.h"
#include "common/repair.h"
@ -50,3 +51,77 @@ int btrfs_add_corrupt_extent_record(struct btrfs_fs_info *info,
return ret;
}
/*
* Fixup block accounting. The initial block accounting created by
* make_block_groups isn't accuracy in this case.
*/
int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans)
{
int ret = 0;
int slot;
u64 start = 0;
u64 bytes_used = 0;
struct btrfs_path path;
struct btrfs_key key;
struct extent_buffer *leaf;
struct btrfs_block_group *cache;
struct btrfs_fs_info *fs_info = trans->fs_info;
struct btrfs_root *root = fs_info->extent_root;
ret = btrfs_run_delayed_refs(trans, -1);
if (ret)
return ret;
while(1) {
cache = btrfs_lookup_first_block_group(fs_info, start);
if (!cache)
break;
start = cache->start + cache->length;
cache->used = 0;
cache->space_info->bytes_used = 0;
if (list_empty(&cache->dirty_list))
list_add_tail(&cache->dirty_list, &trans->dirty_bgs);
}
btrfs_init_path(&path);
key.offset = 0;
key.objectid = 0;
key.type = BTRFS_EXTENT_ITEM_KEY;
ret = btrfs_search_slot(trans, root->fs_info->extent_root,
&key, &path, 0, 0);
if (ret < 0)
return ret;
while(1) {
leaf = path.nodes[0];
slot = path.slots[0];
if (slot >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(root, &path);
if (ret < 0)
return ret;
if (ret > 0)
break;
leaf = path.nodes[0];
slot = path.slots[0];
}
btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.type == BTRFS_EXTENT_ITEM_KEY) {
bytes_used += key.offset;
ret = btrfs_update_block_group(trans,
key.objectid, key.offset, 1, 0);
BUG_ON(ret);
} else if (key.type == BTRFS_METADATA_ITEM_KEY) {
bytes_used += fs_info->nodesize;
ret = btrfs_update_block_group(trans,
key.objectid, fs_info->nodesize, 1, 0);
if (ret)
goto out;
}
path.slots[0]++;
}
btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
ret = 0;
out:
btrfs_release_path(&path);
return ret;
}

View File

@ -32,5 +32,6 @@ struct btrfs_corrupt_block {
int btrfs_add_corrupt_extent_record(struct btrfs_fs_info *info,
struct btrfs_key *first_key,
u64 start, u64 len, int level);
int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans);
#endif

View File

@ -109,6 +109,7 @@
#include "common/device-scan.h"
#include "common/box.h"
#include "common/open-utils.h"
#include "common/repair.h"
extern const struct btrfs_convert_operations ext2_convert_ops;
extern const struct btrfs_convert_operations reiserfs_convert_ops;

View File

@ -3247,80 +3247,6 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
return ret;
}
/*
* Fixup block accounting. The initial block accounting created by
* make_block_groups isn't accuracy in this case.
*/
int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans)
{
int ret = 0;
int slot;
u64 start = 0;
u64 bytes_used = 0;
struct btrfs_path path;
struct btrfs_key key;
struct extent_buffer *leaf;
struct btrfs_block_group *cache;
struct btrfs_fs_info *fs_info = trans->fs_info;
struct btrfs_root *root = fs_info->extent_root;
ret = btrfs_run_delayed_refs(trans, -1);
if (ret)
return ret;
while(1) {
cache = btrfs_lookup_first_block_group(fs_info, start);
if (!cache)
break;
start = cache->start + cache->length;
cache->used = 0;
cache->space_info->bytes_used = 0;
if (list_empty(&cache->dirty_list))
list_add_tail(&cache->dirty_list, &trans->dirty_bgs);
}
btrfs_init_path(&path);
key.offset = 0;
key.objectid = 0;
key.type = BTRFS_EXTENT_ITEM_KEY;
ret = btrfs_search_slot(trans, root->fs_info->extent_root,
&key, &path, 0, 0);
if (ret < 0)
return ret;
while(1) {
leaf = path.nodes[0];
slot = path.slots[0];
if (slot >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(root, &path);
if (ret < 0)
return ret;
if (ret > 0)
break;
leaf = path.nodes[0];
slot = path.slots[0];
}
btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.type == BTRFS_EXTENT_ITEM_KEY) {
bytes_used += key.offset;
ret = btrfs_update_block_group(trans,
key.objectid, key.offset, 1, 0);
BUG_ON(ret);
} else if (key.type == BTRFS_METADATA_ITEM_KEY) {
bytes_used += fs_info->nodesize;
ret = btrfs_update_block_group(trans,
key.objectid, fs_info->nodesize, 1, 0);
if (ret)
goto out;
}
path.slots[0]++;
}
btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
ret = 0;
out:
btrfs_release_path(&path);
return ret;
}
static void __get_extent_size(struct btrfs_root *root, struct btrfs_path *path,
u64 *start, u64 *len)