2 * Copyright (C) 2009 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #define _XOPEN_SOURCE 500
26 #include "kerncompat.h"
30 #include "print-tree.h"
31 #include "transaction.h"
35 /* we write the mirror info to stdout unless they are dumping the data
38 static FILE *info_file
;
40 struct extent_buffer
*debug_corrupt_block(struct btrfs_root
*root
, u64 bytenr
,
41 u32 blocksize
, int copy
)
44 struct extent_buffer
*eb
;
46 struct btrfs_multi_bio
*multi
= NULL
;
47 struct btrfs_device
*device
;
51 eb
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
57 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
58 eb
->start
, &length
, &multi
, mirror_num
);
60 device
= multi
->stripes
[0].dev
;
63 eb
->dev_bytenr
= multi
->stripes
[0].physical
;
65 fprintf(info_file
, "mirror %d logical %Lu physical %Lu "
66 "device %s\n", mirror_num
, (unsigned long long)bytenr
,
67 (unsigned long long)eb
->dev_bytenr
, device
->name
);
70 if (!copy
|| mirror_num
== copy
) {
71 ret
= read_extent_from_disk(eb
);
72 printf("corrupting %llu copy %d\n", eb
->start
,
74 memset(eb
->data
, 0, eb
->len
);
75 write_extent_to_disk(eb
);
79 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
85 if (mirror_num
> num_copies
)
91 static void print_usage(void)
93 fprintf(stderr
, "usage: btrfs-map-logical [options] mount_point\n");
94 fprintf(stderr
, "\t-l Logical extent to map\n");
95 fprintf(stderr
, "\t-c Copy of the extent to read (usually 1 or 2)\n");
96 fprintf(stderr
, "\t-o Output file to hold the extent\n");
97 fprintf(stderr
, "\t-b Number of bytes to read\n");
101 static struct option long_options
[] = {
102 /* { "byte-count", 1, NULL, 'b' }, */
103 { "logical", 1, NULL
, 'l' },
104 { "copy", 1, NULL
, 'c' },
105 { "bytes", 1, NULL
, 'b' },
109 int main(int ac
, char **av
)
111 struct cache_tree root_cache
;
112 struct btrfs_root
*root
;
113 struct extent_buffer
*eb
;
115 char *output_file
= NULL
;
118 int option_index
= 0;
126 c
= getopt_long(ac
, av
, "l:c:", long_options
,
132 logical
= atoll(optarg
);
135 "invalid extent number\n");
143 "invalid copy number\n");
148 bytes
= atoll(optarg
);
151 "invalid byte count\n");
170 cache_tree_init(&root_cache
);
172 root
= open_ctree(dev
, 0, 1);
174 fprintf(stderr
, "Open ctree failed\n");
180 if (strcmp(output_file
, "-") == 0) {
184 out_fd
= open(output_file
, O_RDWR
| O_CREAT
, 0600);
187 err
= ftruncate(out_fd
, 0);
197 bytes
= root
->sectorsize
;
199 bytes
= (bytes
+ root
->sectorsize
- 1) / root
->sectorsize
;
200 bytes
*= root
->sectorsize
;
203 eb
= debug_corrupt_block(root
, logical
, root
->sectorsize
, copy
);
204 if (eb
&& output_file
) {
205 err
= write(out_fd
, eb
->data
, eb
->len
);
206 if (err
< 0 || err
!= eb
->len
) {
207 fprintf(stderr
, "output file write failed\n");
211 free_extent_buffer(eb
);
212 logical
+= root
->sectorsize
;
213 bytes
-= root
->sectorsize
;
217 if (output_file
&& out_fd
!= 1)