Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit af681f4

Browse files
adam900710kdave
authored andcommittedMay 10, 2023
btrfs-progs: replace write_and_map_eb() by write_data_to_disk()
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 <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 32022eb commit af681f4

File tree

6 files changed

+52
-90
lines changed

6 files changed

+52
-90
lines changed
 

‎btrfs-corrupt-block.c

+29-20
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,46 @@
3838

3939
#define FIELD_BUF_LEN 80
4040

41-
static int debug_corrupt_block(struct extent_buffer *eb,
42-
struct btrfs_root *root, u64 bytenr, u32 blocksize, u64 copy)
41+
static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror)
4342
{
43+
const u32 sectorsize = root->fs_info->sectorsize;
44+
struct btrfs_fs_info *fs_info = root->fs_info;
4445
int ret;
4546
int num_copies;
4647
int mirror_num = 1;
48+
void *buf;
49+
50+
buf = malloc(root->fs_info->sectorsize);
51+
if (!buf) {
52+
error_msg(ERROR_MSG_MEMORY, "allocating memory for bytenr %llu",
53+
logical);
54+
return -ENOMEM;
55+
}
4756

4857
while (1) {
49-
if (!copy || mirror_num == copy) {
50-
u64 read_len = eb->len;
58+
if (!mirror || mirror_num == mirror) {
59+
u64 read_len = sectorsize;
5160

52-
ret = read_data_from_disk(eb->fs_info, eb->data,
53-
eb->start, &read_len,
54-
mirror_num);
55-
if (read_len < eb->len)
61+
ret = read_data_from_disk(fs_info, buf, logical,
62+
&read_len, mirror_num);
63+
if (read_len < sectorsize)
5664
ret = -EIO;
5765
if (ret < 0) {
5866
errno = -ret;
59-
error("cannot read eb bytenr %llu: %m", eb->start);
67+
error("cannot read bytenr %llu: %m", logical);
6068
return ret;
6169
}
62-
printf("corrupting %llu copy %d\n", eb->start,
63-
mirror_num);
64-
memset(eb->data, 0, eb->len);
65-
ret = write_and_map_eb(eb->fs_info, eb);
70+
printf("corrupting %llu copy %d\n", logical, mirror_num);
71+
memset(buf, 0, sectorsize);
72+
ret = write_data_to_disk(fs_info, buf, logical, sectorsize);
6673
if (ret < 0) {
6774
errno = -ret;
68-
error("cannot write eb bytenr %llu: %m", eb->start);
75+
error("cannot write bytenr %llu: %m", logical);
6976
return ret;
7077
}
7178
}
7279

73-
num_copies = btrfs_num_copies(root->fs_info, eb->start,
74-
eb->len);
80+
num_copies = btrfs_num_copies(root->fs_info, logical, sectorsize);
7581
if (num_copies == 1)
7682
break;
7783

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

159165
csum_tree_block_size(eb, csum_size, 0, csum_type);
160-
write_and_map_eb(eb->fs_info, eb);
166+
write_data_to_disk(eb->fs_info, eb->data, eb->start, eb->len);
161167
}
162168
}
163169

@@ -878,7 +884,7 @@ static int corrupt_metadata_block(struct btrfs_fs_info *fs_info, u64 block,
878884
btrfs_set_header_generation(eb, bogus);
879885
csum_tree_block_size(eb, fs_info->csum_size, 0,
880886
fs_info->csum_type);
881-
ret = write_and_map_eb(fs_info, eb);
887+
ret = write_data_to_disk(fs_info, eb->data, eb->start, eb->len);
882888
free_extent_buffer(eb);
883889
if (ret < 0) {
884890
errno = -ret;
@@ -1607,8 +1613,11 @@ int main(int argc, char **argv)
16071613
goto out_close;
16081614
}
16091615

1610-
debug_corrupt_block(eb, root, logical,
1611-
root->fs_info->sectorsize, copy);
1616+
ret = debug_corrupt_sector(root, logical, (int)copy);
1617+
if (ret < 0) {
1618+
ret = 1;
1619+
goto out_close;
1620+
}
16121621
free_extent_buffer(eb);
16131622
}
16141623
logical += root->fs_info->sectorsize;

‎convert/main.c

+8-12
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
359359
u64 hole_len;
360360
struct cache_extent *cache;
361361
struct btrfs_key key;
362-
struct extent_buffer *eb;
363362
int ret = 0;
364363

365364
/*
@@ -370,6 +369,8 @@ static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
370369
* migrate ranges that covered by old fs data.
371370
*/
372371
while (cur_off < range_end(range)) {
372+
void *buf;
373+
373374
cache = search_cache_extent(used, cur_off);
374375
if (!cache)
375376
break;
@@ -391,25 +392,20 @@ static int migrate_one_reserved_range(struct btrfs_trans_handle *trans,
391392
if (ret < 0)
392393
break;
393394

394-
eb = malloc(sizeof(*eb) + cur_len);
395-
if (!eb) {
395+
buf = malloc(cur_len);
396+
if (!buf) {
396397
ret = -ENOMEM;
397398
break;
398399
}
399400

400-
ret = pread(fd, eb->data, cur_len, cur_off);
401+
ret = pread(fd, buf, cur_len, cur_off);
401402
if (ret < cur_len) {
402403
ret = (ret < 0 ? ret : -EIO);
403-
free(eb);
404+
free(buf);
404405
break;
405406
}
406-
eb->start = key.objectid;
407-
eb->len = key.offset;
408-
eb->fs_info = root->fs_info;
409-
410-
/* Write the data */
411-
ret = write_and_map_eb(root->fs_info, eb);
412-
free(eb);
407+
ret = write_data_to_disk(root->fs_info, buf, key.objectid, key.offset);
408+
free(buf);
413409
if (ret < 0)
414410
break;
415411

‎convert/source-reiserfs.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -352,22 +352,14 @@ static int convert_direct(struct btrfs_trans_handle *trans,
352352
struct btrfs_key key;
353353
u32 sectorsize = root->fs_info->sectorsize;
354354
int ret;
355-
struct extent_buffer *eb;
356355

357356
BUG_ON(length > sectorsize);
358357
ret = btrfs_reserve_extent(trans, root, sectorsize,
359358
0, 0, -1ULL, &key, 1);
360359
if (ret)
361360
return ret;
362361

363-
eb = alloc_extent_buffer(root->fs_info, key.objectid, sectorsize);
364-
365-
if (!eb)
366-
return -ENOMEM;
367-
368-
write_extent_buffer(eb, body, 0, length);
369-
ret = write_and_map_eb(root->fs_info, eb);
370-
free_extent_buffer(eb);
362+
ret = write_data_to_disk(root->fs_info, body, key.objectid, sectorsize);
371363
if (ret)
372364
return ret;
373365

‎kernel-shared/disk-io.c

+1-22
Original file line numberDiff line numberDiff line change
@@ -462,27 +462,6 @@ struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
462462
return eb;
463463
}
464464

465-
int write_and_map_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
466-
{
467-
int ret;
468-
u64 *raid_map = NULL;
469-
struct btrfs_multi_bio *multi = NULL;
470-
471-
/* write_data_to_disk() will handle all mirrors and RAID56. */
472-
ret = write_data_to_disk(fs_info, eb->data, eb->start, eb->len);
473-
if (ret < 0) {
474-
errno = -ret;
475-
error("failed to write bytenr %llu length %u: %m",
476-
eb->start, eb->len);
477-
goto out;
478-
}
479-
480-
out:
481-
kfree(raid_map);
482-
kfree(multi);
483-
return ret;
484-
}
485-
486465
int write_tree_block(struct btrfs_trans_handle *trans,
487466
struct btrfs_fs_info *fs_info,
488467
struct extent_buffer *eb)
@@ -500,7 +479,7 @@ int write_tree_block(struct btrfs_trans_handle *trans,
500479
btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
501480
csum_tree_block(fs_info, eb, 0);
502481

503-
return write_and_map_eb(fs_info, eb);
482+
return write_data_to_disk(fs_info, eb->data, eb->start, eb->len);
504483
}
505484

506485
void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,

‎kernel-shared/disk-io.h

-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid);
222222
int write_tree_block(struct btrfs_trans_handle *trans,
223223
struct btrfs_fs_info *fs_info,
224224
struct extent_buffer *eb);
225-
int write_and_map_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb);
226225
int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2);
227226
struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
228227
struct btrfs_fs_info *fs_info,

‎mkfs/rootdir.c

+13-26
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ static int add_file_items(struct btrfs_trans_handle *trans,
316316
u64 file_pos = 0;
317317
u64 cur_bytes;
318318
u64 total_bytes;
319-
struct extent_buffer *eb = NULL;
319+
void *buf = NULL;
320320
int fd;
321321

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

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

386382
while (bytes_read < cur_bytes) {
387383

388-
memset(eb->data, 0, sectorsize);
384+
memset(buf, 0, sectorsize);
389385

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

398-
eb->start = first_block + bytes_read;
399-
eb->len = sectorsize;
400-
eb->fs_info = root->fs_info;
393+
ret = write_data_to_disk(root->fs_info, buf,
394+
first_block + bytes_read, sectorsize);
395+
if (ret) {
396+
error("failed to write %s", path_name);
397+
goto end;
398+
}
401399

402-
/*
403-
* we're doing the csum before we record the extent, but
404-
* that's ok
405-
*/
406400
ret = btrfs_csum_file_block(trans,
407401
first_block + bytes_read + sectorsize,
408-
first_block + bytes_read,
409-
eb->data, sectorsize);
402+
first_block + bytes_read, buf, sectorsize);
410403
if (ret)
411404
goto end;
412405

413-
ret = write_and_map_eb(root->fs_info, eb);
414-
if (ret) {
415-
error("failed to write %s", path_name);
416-
goto end;
417-
}
418-
419406
bytes_read += sectorsize;
420407
}
421408

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

436423
end:
437-
free(eb);
424+
free(buf);
438425
close(fd);
439426
return ret;
440427
}

0 commit comments

Comments
 (0)