Skip to content

Commit 868d3dc

Browse files
author
Josef Bacik
committed
Btrfs-progs: rebuild the crc tree with --init-csum-tree
We have --init-csum-tree, which just empties the csum tree. I'm not sure why we would ever need this, but we definitely need to be able to rebuild the csum tree in some cases. This patch adds the ability to completely rebuild the crc tree by reading all of the data and adding csum entries for them. This patch doesn't pay attention to NODATASUM inodes, it'll happily add csums for everything. Thanks, Signed-off-by: Josef Bacik <[email protected]>
1 parent fcbad32 commit 868d3dc

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

cmds-check.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6625,6 +6625,98 @@ static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
66256625
return ret;
66266626
}
66276627

6628+
static int populate_csum(struct btrfs_trans_handle *trans,
6629+
struct btrfs_root *csum_root, char *buf, u64 start,
6630+
u64 len)
6631+
{
6632+
u64 offset = 0;
6633+
u64 sectorsize;
6634+
int ret = 0;
6635+
6636+
while (offset < len) {
6637+
sectorsize = csum_root->sectorsize;
6638+
ret = read_extent_data(csum_root, buf, start + offset,
6639+
&sectorsize, 0);
6640+
if (ret)
6641+
break;
6642+
ret = btrfs_csum_file_block(trans, csum_root, start + len,
6643+
start + offset, buf, sectorsize);
6644+
if (ret)
6645+
break;
6646+
offset += sectorsize;
6647+
}
6648+
return ret;
6649+
}
6650+
6651+
static int fill_csum_tree(struct btrfs_trans_handle *trans,
6652+
struct btrfs_root *csum_root)
6653+
{
6654+
struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
6655+
struct btrfs_path *path;
6656+
struct btrfs_extent_item *ei;
6657+
struct extent_buffer *leaf;
6658+
char *buf;
6659+
struct btrfs_key key;
6660+
int ret;
6661+
6662+
path = btrfs_alloc_path();
6663+
if (!path)
6664+
return -ENOMEM;
6665+
6666+
key.objectid = 0;
6667+
key.type = BTRFS_EXTENT_ITEM_KEY;
6668+
key.offset = 0;
6669+
6670+
ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
6671+
if (ret < 0) {
6672+
btrfs_free_path(path);
6673+
return ret;
6674+
}
6675+
6676+
buf = malloc(csum_root->sectorsize);
6677+
if (!buf) {
6678+
btrfs_free_path(path);
6679+
return -ENOMEM;
6680+
}
6681+
6682+
while (1) {
6683+
if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6684+
ret = btrfs_next_leaf(extent_root, path);
6685+
if (ret < 0)
6686+
break;
6687+
if (ret) {
6688+
ret = 0;
6689+
break;
6690+
}
6691+
}
6692+
leaf = path->nodes[0];
6693+
6694+
btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6695+
if (key.type != BTRFS_EXTENT_ITEM_KEY) {
6696+
path->slots[0]++;
6697+
continue;
6698+
}
6699+
6700+
ei = btrfs_item_ptr(leaf, path->slots[0],
6701+
struct btrfs_extent_item);
6702+
if (!(btrfs_extent_flags(leaf, ei) &
6703+
BTRFS_EXTENT_FLAG_DATA)) {
6704+
path->slots[0]++;
6705+
continue;
6706+
}
6707+
6708+
ret = populate_csum(trans, csum_root, buf, key.objectid,
6709+
key.offset);
6710+
if (ret)
6711+
break;
6712+
path->slots[0]++;
6713+
}
6714+
6715+
btrfs_free_path(path);
6716+
free(buf);
6717+
return ret;
6718+
}
6719+
66286720
static struct option long_options[] = {
66296721
{ "super", 1, NULL, 's' },
66306722
{ "repair", 0, NULL, 0 },
@@ -6794,6 +6886,12 @@ int cmd_check(int argc, char **argv)
67946886
ret = -EIO;
67956887
goto close_out;
67966888
}
6889+
6890+
ret = fill_csum_tree(trans, info->csum_root);
6891+
if (ret) {
6892+
fprintf(stderr, "crc refilling failed\n");
6893+
return -EIO;
6894+
}
67976895
}
67986896
/*
67996897
* Ok now we commit and run the normal fsck, which will add

0 commit comments

Comments
 (0)