btrfs-progs: check: switch to iterating over the backref_tree
[btrfs-progs-unstable/devel.git] / cmds-inspect-dump-super.c
blob3e09ee8c6eedb219120a22e7fc599207bb13745e
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
17 #include "kerncompat.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <ctype.h>
24 #include <uuid/uuid.h>
25 #include <errno.h>
26 #include <getopt.h>
28 #include "ctree.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "list.h"
33 #include "utils.h"
34 #include "commands.h"
35 #include "crc32c.h"
36 #include "cmds-inspect-dump-super.h"
38 static int check_csum_sblock(void *sb, int csum_size)
40 char result[BTRFS_CSUM_SIZE];
41 u32 crc = ~(u32)0;
43 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE,
44 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
45 btrfs_csum_final(crc, result);
47 return !memcmp(sb, &result, csum_size);
50 static void print_sys_chunk_array(struct btrfs_super_block *sb)
52 struct extent_buffer *buf;
53 struct btrfs_disk_key *disk_key;
54 struct btrfs_chunk *chunk;
55 u8 *array_ptr;
56 unsigned long sb_array_offset;
57 u32 num_stripes;
58 u32 array_size;
59 u32 len = 0;
60 u32 cur_offset;
61 struct btrfs_key key;
62 int item;
64 buf = malloc(sizeof(*buf) + sizeof(*sb));
65 if (!buf) {
66 error("not enough memory");
67 goto out;
69 write_extent_buffer(buf, sb, 0, sizeof(*sb));
70 array_size = btrfs_super_sys_array_size(sb);
72 array_ptr = sb->sys_chunk_array;
73 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
74 cur_offset = 0;
75 item = 0;
77 while (cur_offset < array_size) {
78 disk_key = (struct btrfs_disk_key *)array_ptr;
79 len = sizeof(*disk_key);
80 if (cur_offset + len > array_size)
81 goto out_short_read;
83 btrfs_disk_key_to_cpu(&key, disk_key);
85 array_ptr += len;
86 sb_array_offset += len;
87 cur_offset += len;
89 printf("\titem %d ", item);
90 btrfs_print_key(disk_key);
91 putchar('\n');
93 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
94 chunk = (struct btrfs_chunk *)sb_array_offset;
96 * At least one btrfs_chunk with one stripe must be
97 * present, exact stripe count check comes afterwards
99 len = btrfs_chunk_item_size(1);
100 if (cur_offset + len > array_size)
101 goto out_short_read;
103 print_chunk(buf, chunk);
104 num_stripes = btrfs_chunk_num_stripes(buf, chunk);
105 if (!num_stripes) {
106 printk(
107 "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
108 num_stripes, cur_offset);
109 break;
111 len = btrfs_chunk_item_size(num_stripes);
112 if (cur_offset + len > array_size)
113 goto out_short_read;
114 } else {
115 printk(
116 "ERROR: unexpected item type %u in sys_array at offset %u\n",
117 (u32)key.type, cur_offset);
118 break;
120 array_ptr += len;
121 sb_array_offset += len;
122 cur_offset += len;
124 item++;
127 free(buf);
128 out:
129 return;
131 out_short_read:
132 printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
133 len, cur_offset);
134 free(buf);
137 static int empty_backup(struct btrfs_root_backup *backup)
139 if (backup == NULL ||
140 (backup->tree_root == 0 &&
141 backup->tree_root_gen == 0))
142 return 1;
143 return 0;
146 static void print_root_backup(struct btrfs_root_backup *backup)
148 printf("\t\tbackup_tree_root:\t%llu\tgen: %llu\tlevel: %d\n",
149 btrfs_backup_tree_root(backup),
150 btrfs_backup_tree_root_gen(backup),
151 btrfs_backup_tree_root_level(backup));
152 printf("\t\tbackup_chunk_root:\t%llu\tgen: %llu\tlevel: %d\n",
153 btrfs_backup_chunk_root(backup),
154 btrfs_backup_chunk_root_gen(backup),
155 btrfs_backup_chunk_root_level(backup));
156 printf("\t\tbackup_extent_root:\t%llu\tgen: %llu\tlevel: %d\n",
157 btrfs_backup_extent_root(backup),
158 btrfs_backup_extent_root_gen(backup),
159 btrfs_backup_extent_root_level(backup));
160 printf("\t\tbackup_fs_root:\t\t%llu\tgen: %llu\tlevel: %d\n",
161 btrfs_backup_fs_root(backup),
162 btrfs_backup_fs_root_gen(backup),
163 btrfs_backup_fs_root_level(backup));
164 printf("\t\tbackup_dev_root:\t%llu\tgen: %llu\tlevel: %d\n",
165 btrfs_backup_dev_root(backup),
166 btrfs_backup_dev_root_gen(backup),
167 btrfs_backup_dev_root_level(backup));
168 printf("\t\tbackup_csum_root:\t%llu\tgen: %llu\tlevel: %d\n",
169 btrfs_backup_csum_root(backup),
170 btrfs_backup_csum_root_gen(backup),
171 btrfs_backup_csum_root_level(backup));
173 printf("\t\tbackup_total_bytes:\t%llu\n",
174 btrfs_backup_total_bytes(backup));
175 printf("\t\tbackup_bytes_used:\t%llu\n",
176 btrfs_backup_bytes_used(backup));
177 printf("\t\tbackup_num_devices:\t%llu\n",
178 btrfs_backup_num_devices(backup));
179 putchar('\n');
182 static void print_backup_roots(struct btrfs_super_block *sb)
184 struct btrfs_root_backup *backup;
185 int i;
187 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
188 backup = sb->super_roots + i;
189 if (!empty_backup(backup)) {
190 printf("\tbackup %d:\n", i);
191 print_root_backup(backup);
196 struct readable_flag_entry {
197 u64 bit;
198 char *output;
201 #define DEF_INCOMPAT_FLAG_ENTRY(bit_name) \
202 {BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
204 static struct readable_flag_entry incompat_flags_array[] = {
205 DEF_INCOMPAT_FLAG_ENTRY(MIXED_BACKREF),
206 DEF_INCOMPAT_FLAG_ENTRY(DEFAULT_SUBVOL),
207 DEF_INCOMPAT_FLAG_ENTRY(MIXED_GROUPS),
208 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZO),
209 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZOv2),
210 DEF_INCOMPAT_FLAG_ENTRY(BIG_METADATA),
211 DEF_INCOMPAT_FLAG_ENTRY(EXTENDED_IREF),
212 DEF_INCOMPAT_FLAG_ENTRY(RAID56),
213 DEF_INCOMPAT_FLAG_ENTRY(SKINNY_METADATA),
214 DEF_INCOMPAT_FLAG_ENTRY(NO_HOLES)
216 static const int incompat_flags_num = sizeof(incompat_flags_array) /
217 sizeof(struct readable_flag_entry);
219 #define DEF_HEADER_FLAG_ENTRY(bit_name) \
220 {BTRFS_HEADER_FLAG_##bit_name, #bit_name}
221 #define DEF_SUPER_FLAG_ENTRY(bit_name) \
222 {BTRFS_SUPER_FLAG_##bit_name, #bit_name}
224 static struct readable_flag_entry super_flags_array[] = {
225 DEF_HEADER_FLAG_ENTRY(WRITTEN),
226 DEF_HEADER_FLAG_ENTRY(RELOC),
227 DEF_SUPER_FLAG_ENTRY(CHANGING_FSID),
228 DEF_SUPER_FLAG_ENTRY(SEEDING),
229 DEF_SUPER_FLAG_ENTRY(METADUMP),
230 DEF_SUPER_FLAG_ENTRY(METADUMP_V2)
232 static const int super_flags_num = ARRAY_SIZE(super_flags_array);
234 #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
235 BTRFS_HEADER_FLAG_RELOC |\
236 BTRFS_SUPER_FLAG_CHANGING_FSID |\
237 BTRFS_SUPER_FLAG_SEEDING |\
238 BTRFS_SUPER_FLAG_METADUMP |\
239 BTRFS_SUPER_FLAG_METADUMP_V2)
241 static void __print_readable_flag(u64 flag, struct readable_flag_entry *array,
242 int array_size, u64 supported_flags)
244 int i;
245 int first = 1;
246 struct readable_flag_entry *entry;
248 if (!flag)
249 return;
251 printf("\t\t\t( ");
252 for (i = 0; i < array_size; i++) {
253 entry = array + i;
254 if (flag & entry->bit) {
255 if (first)
256 printf("%s ", entry->output);
257 else
258 printf("|\n\t\t\t %s ", entry->output);
259 first = 0;
262 flag &= ~supported_flags;
263 if (flag) {
264 if (first)
265 printf("unknown flag: 0x%llx ", flag);
266 else
267 printf("|\n\t\t\t unknown flag: 0x%llx ", flag);
269 printf(")\n");
272 static void print_readable_incompat_flag(u64 flag)
274 return __print_readable_flag(flag, incompat_flags_array,
275 incompat_flags_num,
276 BTRFS_FEATURE_INCOMPAT_SUPP);
279 static void print_readable_super_flag(u64 flag)
281 return __print_readable_flag(flag, super_flags_array,
282 super_flags_num, BTRFS_SUPER_FLAG_SUPP);
285 static void dump_superblock(struct btrfs_super_block *sb, int full)
287 int i;
288 char *s, buf[BTRFS_UUID_UNPARSED_SIZE];
289 u8 *p;
291 printf("csum\t\t\t0x");
292 for (i = 0, p = sb->csum; i < btrfs_super_csum_size(sb); i++)
293 printf("%02x", p[i]);
294 if (check_csum_sblock(sb, btrfs_super_csum_size(sb)))
295 printf(" [match]");
296 else
297 printf(" [DON'T MATCH]");
298 putchar('\n');
300 printf("bytenr\t\t\t%llu\n",
301 (unsigned long long)btrfs_super_bytenr(sb));
302 printf("flags\t\t\t0x%llx\n",
303 (unsigned long long)btrfs_super_flags(sb));
304 print_readable_super_flag(btrfs_super_flags(sb));
306 printf("magic\t\t\t");
307 s = (char *) &sb->magic;
308 for (i = 0; i < 8; i++)
309 putchar(isprint(s[i]) ? s[i] : '.');
310 if (btrfs_super_magic(sb) == BTRFS_MAGIC)
311 printf(" [match]\n");
312 else
313 printf(" [DON'T MATCH]\n");
315 uuid_unparse(sb->fsid, buf);
316 printf("fsid\t\t\t%s\n", buf);
318 printf("label\t\t\t");
319 s = sb->label;
320 for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
321 putchar(isprint(s[i]) ? s[i] : '.');
322 putchar('\n');
324 printf("generation\t\t%llu\n",
325 (unsigned long long)btrfs_super_generation(sb));
326 printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
327 printf("sys_array_size\t\t%llu\n",
328 (unsigned long long)btrfs_super_sys_array_size(sb));
329 printf("chunk_root_generation\t%llu\n",
330 (unsigned long long)btrfs_super_chunk_root_generation(sb));
331 printf("root_level\t\t%llu\n",
332 (unsigned long long)btrfs_super_root_level(sb));
333 printf("chunk_root\t\t%llu\n",
334 (unsigned long long)btrfs_super_chunk_root(sb));
335 printf("chunk_root_level\t%llu\n",
336 (unsigned long long)btrfs_super_chunk_root_level(sb));
337 printf("log_root\t\t%llu\n",
338 (unsigned long long)btrfs_super_log_root(sb));
339 printf("log_root_transid\t%llu\n",
340 (unsigned long long)btrfs_super_log_root_transid(sb));
341 printf("log_root_level\t\t%llu\n",
342 (unsigned long long)btrfs_super_log_root_level(sb));
343 printf("total_bytes\t\t%llu\n",
344 (unsigned long long)btrfs_super_total_bytes(sb));
345 printf("bytes_used\t\t%llu\n",
346 (unsigned long long)btrfs_super_bytes_used(sb));
347 printf("sectorsize\t\t%llu\n",
348 (unsigned long long)btrfs_super_sectorsize(sb));
349 printf("nodesize\t\t%llu\n",
350 (unsigned long long)btrfs_super_nodesize(sb));
351 printf("leafsize\t\t%llu\n",
352 (unsigned long long)btrfs_super_leafsize(sb));
353 printf("stripesize\t\t%llu\n",
354 (unsigned long long)btrfs_super_stripesize(sb));
355 printf("root_dir\t\t%llu\n",
356 (unsigned long long)btrfs_super_root_dir(sb));
357 printf("num_devices\t\t%llu\n",
358 (unsigned long long)btrfs_super_num_devices(sb));
359 printf("compat_flags\t\t0x%llx\n",
360 (unsigned long long)btrfs_super_compat_flags(sb));
361 printf("compat_ro_flags\t\t0x%llx\n",
362 (unsigned long long)btrfs_super_compat_ro_flags(sb));
363 printf("incompat_flags\t\t0x%llx\n",
364 (unsigned long long)btrfs_super_incompat_flags(sb));
365 print_readable_incompat_flag(btrfs_super_incompat_flags(sb));
366 printf("csum_type\t\t%llu\n",
367 (unsigned long long)btrfs_super_csum_type(sb));
368 printf("csum_size\t\t%llu\n",
369 (unsigned long long)btrfs_super_csum_size(sb));
370 printf("cache_generation\t%llu\n",
371 (unsigned long long)btrfs_super_cache_generation(sb));
372 printf("uuid_tree_generation\t%llu\n",
373 (unsigned long long)btrfs_super_uuid_tree_generation(sb));
375 uuid_unparse(sb->dev_item.uuid, buf);
376 printf("dev_item.uuid\t\t%s\n", buf);
378 uuid_unparse(sb->dev_item.fsid, buf);
379 printf("dev_item.fsid\t\t%s %s\n", buf,
380 !memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
381 "[match]" : "[DON'T MATCH]");
383 printf("dev_item.type\t\t%llu\n", (unsigned long long)
384 btrfs_stack_device_type(&sb->dev_item));
385 printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
386 btrfs_stack_device_total_bytes(&sb->dev_item));
387 printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
388 btrfs_stack_device_bytes_used(&sb->dev_item));
389 printf("dev_item.io_align\t%u\n", (unsigned int)
390 btrfs_stack_device_io_align(&sb->dev_item));
391 printf("dev_item.io_width\t%u\n", (unsigned int)
392 btrfs_stack_device_io_width(&sb->dev_item));
393 printf("dev_item.sector_size\t%u\n", (unsigned int)
394 btrfs_stack_device_sector_size(&sb->dev_item));
395 printf("dev_item.devid\t\t%llu\n",
396 btrfs_stack_device_id(&sb->dev_item));
397 printf("dev_item.dev_group\t%u\n", (unsigned int)
398 btrfs_stack_device_group(&sb->dev_item));
399 printf("dev_item.seek_speed\t%u\n", (unsigned int)
400 btrfs_stack_device_seek_speed(&sb->dev_item));
401 printf("dev_item.bandwidth\t%u\n", (unsigned int)
402 btrfs_stack_device_bandwidth(&sb->dev_item));
403 printf("dev_item.generation\t%llu\n", (unsigned long long)
404 btrfs_stack_device_generation(&sb->dev_item));
405 if (full) {
406 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
407 print_sys_chunk_array(sb);
408 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS);
409 print_backup_roots(sb);
413 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
414 int force)
416 u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
417 struct btrfs_super_block *sb;
418 u64 ret;
420 sb = (struct btrfs_super_block *)super_block_data;
422 ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
423 if (ret != BTRFS_SUPER_INFO_SIZE) {
424 /* check if the disk if too short for further superblock */
425 if (ret == 0 && errno == 0)
426 return 0;
428 error("failed to read the superblock on %s at %llu",
429 filename, (unsigned long long)sb_bytenr);
430 error("error = '%s', errno = %d", strerror(errno), errno);
431 return 1;
433 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
434 printf("---------------------------------------------------------\n");
435 if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
436 error("bad magic on superblock on %s at %llu",
437 filename, (unsigned long long)sb_bytenr);
438 } else {
439 dump_superblock(sb, full);
441 return 0;
444 const char * const cmd_inspect_dump_super_usage[] = {
445 "btrfs inspect-internal dump-super [options] device [device...]",
446 "Dump superblock from a device in a textual form",
447 "-f|--full print full superblock information",
448 "-a|--all print information about all superblocks",
449 "-i <super_mirror> specify which mirror to print out",
450 "-F|--force attempt to dump superblocks with bad magic",
451 "-s <bytenr> specify alternate superblock offset",
452 NULL
455 int cmd_inspect_dump_super(int argc, char **argv)
457 int all = 0;
458 int full = 0;
459 int force = 0;
460 char *filename;
461 int fd = -1;
462 int i;
463 int ret = 0;
464 u64 arg;
465 u64 sb_bytenr = btrfs_sb_offset(0);
467 while (1) {
468 int c;
469 static const struct option long_options[] = {
470 {"all", no_argument, NULL, 'a'},
471 {"full", no_argument, NULL, 'f'},
472 {"force", no_argument, NULL, 'F'},
473 {NULL, 0, NULL, 0}
476 c = getopt_long(argc, argv, "fFai:s:", long_options, NULL);
477 if (c < 0)
478 break;
480 switch (c) {
481 case 'i':
482 arg = arg_strtou64(optarg);
483 if (arg >= BTRFS_SUPER_MIRROR_MAX) {
484 error("super mirror too big: %llu >= %d",
485 arg, BTRFS_SUPER_MIRROR_MAX);
486 usage(cmd_inspect_dump_super_usage);
488 sb_bytenr = btrfs_sb_offset(arg);
489 break;
491 case 'a':
492 all = 1;
493 break;
494 case 'f':
495 full = 1;
496 break;
497 case 'F':
498 force = 1;
499 break;
500 case 's':
501 sb_bytenr = arg_strtou64(optarg);
502 all = 0;
503 break;
504 default:
505 usage(cmd_inspect_dump_super_usage);
509 if (check_argc_min(argc - optind, 1))
510 usage(cmd_inspect_dump_super_usage);
512 for (i = optind; i < argc; i++) {
513 filename = argv[i];
514 fd = open(filename, O_RDONLY, 0666);
515 if (fd < 0) {
516 error("cannot open %s: %s", filename, strerror(errno));
517 ret = 1;
518 goto out;
521 if (all) {
522 int idx;
524 for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
525 sb_bytenr = btrfs_sb_offset(idx);
526 if (load_and_dump_sb(filename, fd,
527 sb_bytenr, full, force)) {
528 close(fd);
529 ret = 1;
530 goto out;
533 putchar('\n');
535 } else {
536 load_and_dump_sb(filename, fd, sb_bytenr, full, force);
537 putchar('\n');
539 close(fd);
542 out:
543 return ret;