Skip to content

Commit

Permalink
btrfs-progs: replace write_and_map_eb() by write_data_to_disk()
Browse files Browse the repository at this point in the history
The function write_and_map_eb() is quite abused as a way to write any
generic buffer back to disk.

But we have a more suitable function already, write_data_to_disk().

This patch would remove the abused write_data_to_disk() calls, and
convert the only three valid call sites to write_data_to_disk() instead.

Signed-off-by: Qu Wenruo <[email protected]>
Signed-off-by: David Sterba <[email protected]>
  • Loading branch information
adam900710 authored and kdave committed May 10, 2023
1 parent 32022eb commit af681f4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 90 deletions.
49 changes: 29 additions & 20 deletions btrfs-corrupt-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,46 @@

#define FIELD_BUF_LEN 80

static int debug_corrupt_block(struct extent_buffer *eb,
struct btrfs_root *root, u64 bytenr, u32 blocksize, u64 copy)
static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror)
{
const u32 sectorsize = root->fs_info->sectorsize;
struct btrfs_fs_info *fs_info = root->fs_info;
int ret;
int num_copies;
int mirror_num = 1;
void *buf;

buf = malloc(root->fs_info->sectorsize);
if (!buf) {
error_msg(ERROR_MSG_MEMORY, "allocating memory for bytenr %llu",
logical);
return -ENOMEM;
}

while (1) {
if (!copy || mirror_num == copy) {
u64 read_len = eb->len;
if (!mirror || mirror_num == mirror) {
u64 read_len = sectorsize;

ret = read_data_from_disk(eb->fs_info, eb->data,
eb->start, &read_len,
mirror_num);
if (read_len < eb->len)
ret = read_data_from_disk(fs_info, buf, logical,
&read_len, mirror_num);
if (read_len < sectorsize)
ret = -EIO;
if (ret < 0) {
errno = -ret;
error("cannot read eb bytenr %llu: %m", eb->start);
error("cannot read bytenr %llu: %m", logical);
return ret;
}
printf("corrupting %llu copy %d\n", eb->start,
mirror_num);
memset(eb->data, 0, eb->len);
ret = write_and_map_eb(eb->fs_info, eb);
printf("corrupting %llu copy %d\n", logical, mirror_num);
memset(buf, 0, sectorsize);
ret = write_data_to_disk(fs_info, buf, logical, sectorsize);
if (ret < 0) {
errno = -ret;
error("cannot write eb bytenr %llu: %m", eb->start);
error("cannot write bytenr %llu: %m", logical);
return ret;
}
}

num_copies = btrfs_num_copies(root->fs_info, eb->start,
eb->len);
num_copies = btrfs_num_copies(root->fs_info, logical, sectorsize);
if (num_copies == 1)
break;

Expand Down Expand Up @@ -157,7 +163,7 @@ static void corrupt_keys(struct btrfs_trans_handle *trans,
u16 csum_type = fs_info->csum_type;

csum_tree_block_size(eb, csum_size, 0, csum_type);
write_and_map_eb(eb->fs_info, eb);
write_data_to_disk(eb->fs_info, eb->data, eb->start, eb->len);
}
}

Expand Down Expand Up @@ -878,7 +884,7 @@ static int corrupt_metadata_block(struct btrfs_fs_info *fs_info, u64 block,
btrfs_set_header_generation(eb, bogus);
csum_tree_block_size(eb, fs_info->csum_size, 0,
fs_info->csum_type);
ret = write_and_map_eb(fs_info, eb);
ret = write_data_to_disk(fs_info, eb->data, eb->start, eb->len);
free_extent_buffer(eb);
if (ret < 0) {
errno = -ret;
Expand Down Expand Up @@ -1607,8 +1613,11 @@ int main(int argc, char **argv)
goto out_close;
}

debug_corrupt_block(eb, root, logical,
root->fs_info->sectorsize, copy);
ret = debug_corrupt_sector(root, logical, (int)copy);
if (ret < 0) {
ret = 1;
goto out_close;
}
free_extent_buffer(eb);
}
logical += root->fs_info->sectorsize;
Expand Down
20 changes: 8 additions & 12 deletions convert/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
u64 hole_len;
struct cache_extent *cache;
struct btrfs_key key;
struct extent_buffer *eb;
int ret = 0;

/*
Expand All @@ -370,6 +369,8 @@ static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
* migrate ranges that covered by old fs data.
*/
while (cur_off < range_end(range)) {
void *buf;

cache = search_cache_extent(used, cur_off);
if (!cache)
break;
Expand All @@ -391,25 +392,20 @@ static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
if (ret < 0)
break;

eb = malloc(sizeof(*eb) + cur_len);
if (!eb) {
buf = malloc(cur_len);
if (!buf) {
ret = -ENOMEM;
break;
}

ret = pread(fd, eb->data, cur_len, cur_off);
ret = pread(fd, buf, cur_len, cur_off);
if (ret < cur_len) {
ret = (ret < 0 ? ret : -EIO);
free(eb);
free(buf);
break;
}
eb->start = key.objectid;
eb->len = key.offset;
eb->fs_info = root->fs_info;

/* Write the data */
ret = write_and_map_eb(root->fs_info, eb);
free(eb);
ret = write_data_to_disk(root->fs_info, buf, key.objectid, key.offset);
free(buf);
if (ret < 0)
break;

Expand Down
10 changes: 1 addition & 9 deletions convert/source-reiserfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,22 +352,14 @@ static int convert_direct(struct btrfs_trans_handle *trans,
struct btrfs_key key;
u32 sectorsize = root->fs_info->sectorsize;
int ret;
struct extent_buffer *eb;

BUG_ON(length > sectorsize);
ret = btrfs_reserve_extent(trans, root, sectorsize,
0, 0, -1ULL, &key, 1);
if (ret)
return ret;

eb = alloc_extent_buffer(root->fs_info, key.objectid, sectorsize);

if (!eb)
return -ENOMEM;

write_extent_buffer(eb, body, 0, length);
ret = write_and_map_eb(root->fs_info, eb);
free_extent_buffer(eb);
ret = write_data_to_disk(root->fs_info, body, key.objectid, sectorsize);
if (ret)
return ret;

Expand Down
23 changes: 1 addition & 22 deletions kernel-shared/disk-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,27 +462,6 @@ struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
return eb;
}

int write_and_map_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
{
int ret;
u64 *raid_map = NULL;
struct btrfs_multi_bio *multi = NULL;

/* write_data_to_disk() will handle all mirrors and RAID56. */
ret = write_data_to_disk(fs_info, eb->data, eb->start, eb->len);
if (ret < 0) {
errno = -ret;
error("failed to write bytenr %llu length %u: %m",
eb->start, eb->len);
goto out;
}

out:
kfree(raid_map);
kfree(multi);
return ret;
}

int write_tree_block(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info,
struct extent_buffer *eb)
Expand All @@ -500,7 +479,7 @@ int write_tree_block(struct btrfs_trans_handle *trans,
btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
csum_tree_block(fs_info, eb, 0);

return write_and_map_eb(fs_info, eb);
return write_data_to_disk(fs_info, eb->data, eb->start, eb->len);
}

void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
Expand Down
1 change: 0 additions & 1 deletion kernel-shared/disk-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid);
int write_tree_block(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info,
struct extent_buffer *eb);
int write_and_map_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb);
int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2);
struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info,
Expand Down
39 changes: 13 additions & 26 deletions mkfs/rootdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static int add_file_items(struct btrfs_trans_handle *trans,
u64 file_pos = 0;
u64 cur_bytes;
u64 total_bytes;
struct extent_buffer *eb = NULL;
void *buf = NULL;
int fd;

if (st->st_size == 0)
Expand Down Expand Up @@ -358,12 +358,8 @@ static int add_file_items(struct btrfs_trans_handle *trans,
/* round up our st_size to the FS blocksize */
total_bytes = (u64)blocks * sectorsize;

/*
* do our IO in extent buffers so it can work
* against any raid type
*/
eb = calloc(1, sizeof(*eb) + sectorsize);
if (!eb) {
buf = malloc(sectorsize);
if (!buf) {
ret = -ENOMEM;
goto end;
}
Expand All @@ -385,37 +381,28 @@ static int add_file_items(struct btrfs_trans_handle *trans,

while (bytes_read < cur_bytes) {

memset(eb->data, 0, sectorsize);
memset(buf, 0, sectorsize);

ret_read = pread(fd, eb->data, sectorsize, file_pos +
bytes_read);
ret_read = pread(fd, buf, sectorsize, file_pos + bytes_read);
if (ret_read == -1) {
error("cannot read %s at offset %llu length %u: %m",
path_name, file_pos + bytes_read, sectorsize);
goto end;
}

eb->start = first_block + bytes_read;
eb->len = sectorsize;
eb->fs_info = root->fs_info;
ret = write_data_to_disk(root->fs_info, buf,
first_block + bytes_read, sectorsize);
if (ret) {
error("failed to write %s", path_name);
goto end;
}

/*
* we're doing the csum before we record the extent, but
* that's ok
*/
ret = btrfs_csum_file_block(trans,
first_block + bytes_read + sectorsize,
first_block + bytes_read,
eb->data, sectorsize);
first_block + bytes_read, buf, sectorsize);
if (ret)
goto end;

ret = write_and_map_eb(root->fs_info, eb);
if (ret) {
error("failed to write %s", path_name);
goto end;
}

bytes_read += sectorsize;
}

Expand All @@ -434,7 +421,7 @@ static int add_file_items(struct btrfs_trans_handle *trans,
goto again;

end:
free(eb);
free(buf);
close(fd);
return ret;
}
Expand Down

0 comments on commit af681f4

Please sign in to comment.