|
38 | 38 |
|
39 | 39 | #define FIELD_BUF_LEN 80
|
40 | 40 |
|
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) |
43 | 42 | {
|
| 43 | + const u32 sectorsize = root->fs_info->sectorsize; |
| 44 | + struct btrfs_fs_info *fs_info = root->fs_info; |
44 | 45 | int ret;
|
45 | 46 | int num_copies;
|
46 | 47 | 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 | + } |
47 | 56 |
|
48 | 57 | 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; |
51 | 60 |
|
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) |
56 | 64 | ret = -EIO;
|
57 | 65 | if (ret < 0) {
|
58 | 66 | errno = -ret;
|
59 |
| - error("cannot read eb bytenr %llu: %m", eb->start); |
| 67 | + error("cannot read bytenr %llu: %m", logical); |
60 | 68 | return ret;
|
61 | 69 | }
|
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); |
66 | 73 | if (ret < 0) {
|
67 | 74 | errno = -ret;
|
68 |
| - error("cannot write eb bytenr %llu: %m", eb->start); |
| 75 | + error("cannot write bytenr %llu: %m", logical); |
69 | 76 | return ret;
|
70 | 77 | }
|
71 | 78 | }
|
72 | 79 |
|
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); |
75 | 81 | if (num_copies == 1)
|
76 | 82 | break;
|
77 | 83 |
|
@@ -157,7 +163,7 @@ static void corrupt_keys(struct btrfs_trans_handle *trans,
|
157 | 163 | u16 csum_type = fs_info->csum_type;
|
158 | 164 |
|
159 | 165 | 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); |
161 | 167 | }
|
162 | 168 | }
|
163 | 169 |
|
@@ -878,7 +884,7 @@ static int corrupt_metadata_block(struct btrfs_fs_info *fs_info, u64 block,
|
878 | 884 | btrfs_set_header_generation(eb, bogus);
|
879 | 885 | csum_tree_block_size(eb, fs_info->csum_size, 0,
|
880 | 886 | 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); |
882 | 888 | free_extent_buffer(eb);
|
883 | 889 | if (ret < 0) {
|
884 | 890 | errno = -ret;
|
@@ -1607,8 +1613,11 @@ int main(int argc, char **argv)
|
1607 | 1613 | goto out_close;
|
1608 | 1614 | }
|
1609 | 1615 |
|
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 | + } |
1612 | 1621 | free_extent_buffer(eb);
|
1613 | 1622 | }
|
1614 | 1623 | logical += root->fs_info->sectorsize;
|
|
0 commit comments