btrfs-progs: add a utility to corrupt a single block
[platform/upstream/btrfs-progs.git] / btrfs-corrupt-block.c
1 /*
2  * Copyright (C) 2009 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "volumes.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "list.h"
33 #include "version.h"
34
35 /* we write the mirror info to stdout unless they are dumping the data
36  * to stdout
37  * */
38 static FILE *info_file;
39
40 struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
41                                      u32 blocksize, int copy)
42 {
43         int ret;
44         struct extent_buffer *eb;
45         u64 length;
46         struct btrfs_multi_bio *multi = NULL;
47         struct btrfs_device *device;
48         int num_copies;
49         int mirror_num = 1;
50
51         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
52         if (!eb)
53                 return NULL;
54
55         length = blocksize;
56         while (1) {
57                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
58                                       eb->start, &length, &multi, mirror_num);
59                 BUG_ON(ret);
60                 device = multi->stripes[0].dev;
61                 eb->fd = device->fd;
62                 device->total_ios++;
63                 eb->dev_bytenr = multi->stripes[0].physical;
64
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);
68                 kfree(multi);
69
70                 if (!copy || mirror_num == copy) {
71                         ret = read_extent_from_disk(eb);
72                         printf("corrupting %llu copy %d\n", eb->start,
73                                mirror_num);
74                         memset(eb->data, 0, eb->len);
75                         write_extent_to_disk(eb);
76                         fsync(eb->fd);
77                 }
78
79                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
80                                               eb->start, eb->len);
81                 if (num_copies == 1)
82                         break;
83
84                 mirror_num++;
85                 if (mirror_num > num_copies)
86                         break;
87         }
88         return eb;
89 }
90
91 static void print_usage(void)
92 {
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");
98         exit(1);
99 }
100
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' },
106         { 0, 0, 0, 0}
107 };
108
109 int main(int ac, char **av)
110 {
111         struct cache_tree root_cache;
112         struct btrfs_root *root;
113         struct extent_buffer *eb;
114         char *dev;
115         char *output_file = NULL;
116         u64 logical = 0;
117         int ret = 0;
118         int option_index = 0;
119         int copy = 0;
120         u64 bytes = 4096;
121         int out_fd = 0;
122         int err;
123
124         while(1) {
125                 int c;
126                 c = getopt_long(ac, av, "l:c:", long_options,
127                                 &option_index);
128                 if (c < 0)
129                         break;
130                 switch(c) {
131                         case 'l':
132                                 logical = atoll(optarg);
133                                 if (logical == 0) {
134                                         fprintf(stderr,
135                                                 "invalid extent number\n");
136                                         print_usage();
137                                 }
138                                 break;
139                         case 'c':
140                                 copy = atoi(optarg);
141                                 if (copy == 0) {
142                                         fprintf(stderr,
143                                                 "invalid copy number\n");
144                                         print_usage();
145                                 }
146                                 break;
147                         case 'b':
148                                 bytes = atoll(optarg);
149                                 if (bytes == 0) {
150                                         fprintf(stderr,
151                                                 "invalid byte count\n");
152                                         print_usage();
153                                 }
154                                 break;
155                         default:
156                                 print_usage();
157                 }
158         }
159         ac = ac - optind;
160         if (ac == 0)
161                 print_usage();
162         if (logical == 0)
163                 print_usage();
164         if (copy < 0)
165                 print_usage();
166
167         dev = av[optind];
168
169         radix_tree_init();
170         cache_tree_init(&root_cache);
171
172         root = open_ctree(dev, 0, 1);
173         if (!root) {
174                 fprintf(stderr, "Open ctree failed\n");
175                 exit(1);
176         }
177
178         info_file = stdout;
179         if (output_file) {
180                 if (strcmp(output_file, "-") == 0) {
181                         out_fd = 1;
182                         info_file = stderr;
183                 } else {
184                         out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
185                         if (out_fd < 0)
186                                 goto close;
187                         err = ftruncate(out_fd, 0);
188                         if (err) {
189                                 close(out_fd);
190                                 goto close;
191                         }
192                         info_file = stdout;
193                 }
194         }
195
196         if (bytes == 0)
197                 bytes = root->sectorsize;
198
199         bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
200         bytes *= root->sectorsize;
201
202         while (bytes > 0) {
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");
208                                 goto out_close_fd;
209                         }
210                 }
211                 free_extent_buffer(eb);
212                 logical += root->sectorsize;
213                 bytes -= root->sectorsize;
214         }
215
216 out_close_fd:
217         if (output_file && out_fd != 1)
218                 close(out_fd);
219 close:
220         return ret;
221 }