btrfs-progs: check/original: Don't overwrite return value when we failed to repair
[btrfs-progs-unstable/devel.git] / cmds-inspect-dump-super.c
blobe965267c5d96571539a267d7e16458fb897c6746
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 "help.h"
38 static int check_csum_sblock(void *sb, int csum_size)
40 u8 result[BTRFS_CSUM_SIZE];
41 u32 crc = ~(u32)0;
43 crc = btrfs_csum_data((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 return;
69 write_extent_buffer(buf, sb, 0, sizeof(*sb));
70 buf->len = sizeof(*sb);
71 array_size = btrfs_super_sys_array_size(sb);
73 array_ptr = sb->sys_chunk_array;
74 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
76 if (array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
77 error("sys_array_size %u shouldn't exceed %u bytes",
78 array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
79 goto out;
82 cur_offset = 0;
83 item = 0;
85 while (cur_offset < array_size) {
86 disk_key = (struct btrfs_disk_key *)array_ptr;
87 len = sizeof(*disk_key);
88 if (cur_offset + len > array_size)
89 goto out_short_read;
91 btrfs_disk_key_to_cpu(&key, disk_key);
93 array_ptr += len;
94 sb_array_offset += len;
95 cur_offset += len;
97 printf("\titem %d ", item);
98 btrfs_print_key(disk_key);
99 putchar('\n');
101 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
102 chunk = (struct btrfs_chunk *)sb_array_offset;
104 * At least one btrfs_chunk with one stripe must be
105 * present, exact stripe count check comes afterwards
107 len = btrfs_chunk_item_size(1);
108 if (cur_offset + len > array_size)
109 goto out_short_read;
111 num_stripes = btrfs_chunk_num_stripes(buf, chunk);
112 if (!num_stripes) {
113 error(
114 "invalid number of stripes %u in sys_array at offset %u",
115 num_stripes, cur_offset);
116 break;
118 len = btrfs_chunk_item_size(num_stripes);
119 if (cur_offset + len > array_size)
120 goto out_short_read;
121 print_chunk_item(buf, chunk);
122 } else {
123 error("unexpected item type %u in sys_array at offset %u",
124 (u32)key.type, cur_offset);
125 break;
127 array_ptr += len;
128 sb_array_offset += len;
129 cur_offset += len;
131 item++;
134 out:
135 free(buf);
136 return;
138 out_short_read:
139 error("sys_array too short to read %u bytes at offset %u",
140 len, cur_offset);
141 free(buf);
144 static int empty_backup(struct btrfs_root_backup *backup)
146 if (backup == NULL ||
147 (backup->tree_root == 0 &&
148 backup->tree_root_gen == 0))
149 return 1;
150 return 0;
153 static void print_root_backup(struct btrfs_root_backup *backup)
155 printf("\t\tbackup_tree_root:\t%llu\tgen: %llu\tlevel: %d\n",
156 btrfs_backup_tree_root(backup),
157 btrfs_backup_tree_root_gen(backup),
158 btrfs_backup_tree_root_level(backup));
159 printf("\t\tbackup_chunk_root:\t%llu\tgen: %llu\tlevel: %d\n",
160 btrfs_backup_chunk_root(backup),
161 btrfs_backup_chunk_root_gen(backup),
162 btrfs_backup_chunk_root_level(backup));
163 printf("\t\tbackup_extent_root:\t%llu\tgen: %llu\tlevel: %d\n",
164 btrfs_backup_extent_root(backup),
165 btrfs_backup_extent_root_gen(backup),
166 btrfs_backup_extent_root_level(backup));
167 printf("\t\tbackup_fs_root:\t\t%llu\tgen: %llu\tlevel: %d\n",
168 btrfs_backup_fs_root(backup),
169 btrfs_backup_fs_root_gen(backup),
170 btrfs_backup_fs_root_level(backup));
171 printf("\t\tbackup_dev_root:\t%llu\tgen: %llu\tlevel: %d\n",
172 btrfs_backup_dev_root(backup),
173 btrfs_backup_dev_root_gen(backup),
174 btrfs_backup_dev_root_level(backup));
175 printf("\t\tbackup_csum_root:\t%llu\tgen: %llu\tlevel: %d\n",
176 btrfs_backup_csum_root(backup),
177 btrfs_backup_csum_root_gen(backup),
178 btrfs_backup_csum_root_level(backup));
180 printf("\t\tbackup_total_bytes:\t%llu\n",
181 btrfs_backup_total_bytes(backup));
182 printf("\t\tbackup_bytes_used:\t%llu\n",
183 btrfs_backup_bytes_used(backup));
184 printf("\t\tbackup_num_devices:\t%llu\n",
185 btrfs_backup_num_devices(backup));
186 putchar('\n');
189 static void print_backup_roots(struct btrfs_super_block *sb)
191 struct btrfs_root_backup *backup;
192 int i;
194 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
195 backup = sb->super_roots + i;
196 if (!empty_backup(backup)) {
197 printf("\tbackup %d:\n", i);
198 print_root_backup(backup);
203 struct readable_flag_entry {
204 u64 bit;
205 char *output;
208 #define DEF_COMPAT_RO_FLAG_ENTRY(bit_name) \
209 {BTRFS_FEATURE_COMPAT_RO_##bit_name, #bit_name}
211 static struct readable_flag_entry compat_ro_flags_array[] = {
212 DEF_COMPAT_RO_FLAG_ENTRY(FREE_SPACE_TREE),
213 DEF_COMPAT_RO_FLAG_ENTRY(FREE_SPACE_TREE_VALID),
215 static const int compat_ro_flags_num = sizeof(compat_ro_flags_array) /
216 sizeof(struct readable_flag_entry);
218 #define DEF_INCOMPAT_FLAG_ENTRY(bit_name) \
219 {BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
221 static struct readable_flag_entry incompat_flags_array[] = {
222 DEF_INCOMPAT_FLAG_ENTRY(MIXED_BACKREF),
223 DEF_INCOMPAT_FLAG_ENTRY(DEFAULT_SUBVOL),
224 DEF_INCOMPAT_FLAG_ENTRY(MIXED_GROUPS),
225 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZO),
226 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_ZSTD),
227 DEF_INCOMPAT_FLAG_ENTRY(BIG_METADATA),
228 DEF_INCOMPAT_FLAG_ENTRY(EXTENDED_IREF),
229 DEF_INCOMPAT_FLAG_ENTRY(RAID56),
230 DEF_INCOMPAT_FLAG_ENTRY(SKINNY_METADATA),
231 DEF_INCOMPAT_FLAG_ENTRY(NO_HOLES)
233 static const int incompat_flags_num = sizeof(incompat_flags_array) /
234 sizeof(struct readable_flag_entry);
236 #define DEF_HEADER_FLAG_ENTRY(bit_name) \
237 {BTRFS_HEADER_FLAG_##bit_name, #bit_name}
238 #define DEF_SUPER_FLAG_ENTRY(bit_name) \
239 {BTRFS_SUPER_FLAG_##bit_name, #bit_name}
241 static struct readable_flag_entry super_flags_array[] = {
242 DEF_HEADER_FLAG_ENTRY(WRITTEN),
243 DEF_HEADER_FLAG_ENTRY(RELOC),
244 DEF_SUPER_FLAG_ENTRY(CHANGING_FSID),
245 DEF_SUPER_FLAG_ENTRY(SEEDING),
246 DEF_SUPER_FLAG_ENTRY(METADUMP),
247 DEF_SUPER_FLAG_ENTRY(METADUMP_V2)
249 static const int super_flags_num = ARRAY_SIZE(super_flags_array);
251 #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
252 BTRFS_HEADER_FLAG_RELOC |\
253 BTRFS_SUPER_FLAG_CHANGING_FSID |\
254 BTRFS_SUPER_FLAG_SEEDING |\
255 BTRFS_SUPER_FLAG_METADUMP |\
256 BTRFS_SUPER_FLAG_METADUMP_V2)
258 static void __print_readable_flag(u64 flag, struct readable_flag_entry *array,
259 int array_size, u64 supported_flags)
261 int i;
262 int first = 1;
263 struct readable_flag_entry *entry;
265 if (!flag)
266 return;
268 printf("\t\t\t( ");
269 for (i = 0; i < array_size; i++) {
270 entry = array + i;
271 if (flag & entry->bit) {
272 if (first)
273 printf("%s ", entry->output);
274 else
275 printf("|\n\t\t\t %s ", entry->output);
276 first = 0;
279 flag &= ~supported_flags;
280 if (flag) {
281 if (first)
282 printf("unknown flag: 0x%llx ", flag);
283 else
284 printf("|\n\t\t\t unknown flag: 0x%llx ", flag);
286 printf(")\n");
289 static void print_readable_compat_ro_flag(u64 flag)
292 * We know about the FREE_SPACE_TREE{,_VALID} bits, but we don't
293 * actually support them yet.
295 return __print_readable_flag(flag, compat_ro_flags_array,
296 compat_ro_flags_num,
297 BTRFS_FEATURE_COMPAT_RO_SUPP |
298 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |
299 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID);
302 static void print_readable_incompat_flag(u64 flag)
304 return __print_readable_flag(flag, incompat_flags_array,
305 incompat_flags_num,
306 BTRFS_FEATURE_INCOMPAT_SUPP);
309 static void print_readable_super_flag(u64 flag)
311 return __print_readable_flag(flag, super_flags_array,
312 super_flags_num, BTRFS_SUPER_FLAG_SUPP);
315 static void dump_superblock(struct btrfs_super_block *sb, int full)
317 int i;
318 char *s, buf[BTRFS_UUID_UNPARSED_SIZE];
319 u8 *p;
320 u32 csum_size;
321 u16 csum_type;
323 csum_type = btrfs_super_csum_type(sb);
324 csum_size = BTRFS_CSUM_SIZE;
325 printf("csum_type\t\t%hu (", csum_type);
326 if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
327 printf("INVALID");
328 } else {
329 if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
330 printf("crc32c");
331 csum_size = btrfs_csum_sizes[csum_type];
332 } else {
333 printf("unknown");
336 printf(")\n");
337 printf("csum_size\t\t%llu\n", (unsigned long long)csum_size);
339 printf("csum\t\t\t0x");
340 for (i = 0, p = sb->csum; i < csum_size; i++)
341 printf("%02x", p[i]);
342 if (csum_type != BTRFS_CSUM_TYPE_CRC32 ||
343 csum_size != btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32])
344 printf(" [UNKNOWN CSUM TYPE OR SIZE]");
345 else if (check_csum_sblock(sb, csum_size))
346 printf(" [match]");
347 else
348 printf(" [DON'T MATCH]");
349 putchar('\n');
351 printf("bytenr\t\t\t%llu\n",
352 (unsigned long long)btrfs_super_bytenr(sb));
353 printf("flags\t\t\t0x%llx\n",
354 (unsigned long long)btrfs_super_flags(sb));
355 print_readable_super_flag(btrfs_super_flags(sb));
357 printf("magic\t\t\t");
358 s = (char *) &sb->magic;
359 for (i = 0; i < 8; i++)
360 putchar(isprint(s[i]) ? s[i] : '.');
361 if (btrfs_super_magic(sb) == BTRFS_MAGIC)
362 printf(" [match]\n");
363 else
364 printf(" [DON'T MATCH]\n");
366 uuid_unparse(sb->fsid, buf);
367 printf("fsid\t\t\t%s\n", buf);
369 printf("label\t\t\t");
370 s = sb->label;
371 for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
372 putchar(isprint(s[i]) ? s[i] : '.');
373 putchar('\n');
375 printf("generation\t\t%llu\n",
376 (unsigned long long)btrfs_super_generation(sb));
377 printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
378 printf("sys_array_size\t\t%llu\n",
379 (unsigned long long)btrfs_super_sys_array_size(sb));
380 printf("chunk_root_generation\t%llu\n",
381 (unsigned long long)btrfs_super_chunk_root_generation(sb));
382 printf("root_level\t\t%llu\n",
383 (unsigned long long)btrfs_super_root_level(sb));
384 printf("chunk_root\t\t%llu\n",
385 (unsigned long long)btrfs_super_chunk_root(sb));
386 printf("chunk_root_level\t%llu\n",
387 (unsigned long long)btrfs_super_chunk_root_level(sb));
388 printf("log_root\t\t%llu\n",
389 (unsigned long long)btrfs_super_log_root(sb));
390 printf("log_root_transid\t%llu\n",
391 (unsigned long long)btrfs_super_log_root_transid(sb));
392 printf("log_root_level\t\t%llu\n",
393 (unsigned long long)btrfs_super_log_root_level(sb));
394 printf("total_bytes\t\t%llu\n",
395 (unsigned long long)btrfs_super_total_bytes(sb));
396 printf("bytes_used\t\t%llu\n",
397 (unsigned long long)btrfs_super_bytes_used(sb));
398 printf("sectorsize\t\t%llu\n",
399 (unsigned long long)btrfs_super_sectorsize(sb));
400 printf("nodesize\t\t%llu\n",
401 (unsigned long long)btrfs_super_nodesize(sb));
402 printf("leafsize (deprecated)\t\t%u\n",
403 le32_to_cpu(sb->__unused_leafsize));
404 printf("stripesize\t\t%llu\n",
405 (unsigned long long)btrfs_super_stripesize(sb));
406 printf("root_dir\t\t%llu\n",
407 (unsigned long long)btrfs_super_root_dir(sb));
408 printf("num_devices\t\t%llu\n",
409 (unsigned long long)btrfs_super_num_devices(sb));
410 printf("compat_flags\t\t0x%llx\n",
411 (unsigned long long)btrfs_super_compat_flags(sb));
412 printf("compat_ro_flags\t\t0x%llx\n",
413 (unsigned long long)btrfs_super_compat_ro_flags(sb));
414 print_readable_compat_ro_flag(btrfs_super_compat_ro_flags(sb));
415 printf("incompat_flags\t\t0x%llx\n",
416 (unsigned long long)btrfs_super_incompat_flags(sb));
417 print_readable_incompat_flag(btrfs_super_incompat_flags(sb));
418 printf("cache_generation\t%llu\n",
419 (unsigned long long)btrfs_super_cache_generation(sb));
420 printf("uuid_tree_generation\t%llu\n",
421 (unsigned long long)btrfs_super_uuid_tree_generation(sb));
423 uuid_unparse(sb->dev_item.uuid, buf);
424 printf("dev_item.uuid\t\t%s\n", buf);
426 uuid_unparse(sb->dev_item.fsid, buf);
427 printf("dev_item.fsid\t\t%s %s\n", buf,
428 !memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
429 "[match]" : "[DON'T MATCH]");
431 printf("dev_item.type\t\t%llu\n", (unsigned long long)
432 btrfs_stack_device_type(&sb->dev_item));
433 printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
434 btrfs_stack_device_total_bytes(&sb->dev_item));
435 printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
436 btrfs_stack_device_bytes_used(&sb->dev_item));
437 printf("dev_item.io_align\t%u\n", (unsigned int)
438 btrfs_stack_device_io_align(&sb->dev_item));
439 printf("dev_item.io_width\t%u\n", (unsigned int)
440 btrfs_stack_device_io_width(&sb->dev_item));
441 printf("dev_item.sector_size\t%u\n", (unsigned int)
442 btrfs_stack_device_sector_size(&sb->dev_item));
443 printf("dev_item.devid\t\t%llu\n",
444 btrfs_stack_device_id(&sb->dev_item));
445 printf("dev_item.dev_group\t%u\n", (unsigned int)
446 btrfs_stack_device_group(&sb->dev_item));
447 printf("dev_item.seek_speed\t%u\n", (unsigned int)
448 btrfs_stack_device_seek_speed(&sb->dev_item));
449 printf("dev_item.bandwidth\t%u\n", (unsigned int)
450 btrfs_stack_device_bandwidth(&sb->dev_item));
451 printf("dev_item.generation\t%llu\n", (unsigned long long)
452 btrfs_stack_device_generation(&sb->dev_item));
453 if (full) {
454 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
455 print_sys_chunk_array(sb);
456 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS);
457 print_backup_roots(sb);
461 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
462 int force)
464 u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
465 struct btrfs_super_block *sb;
466 u64 ret;
468 sb = (struct btrfs_super_block *)super_block_data;
470 ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
471 if (ret != BTRFS_SUPER_INFO_SIZE) {
472 /* check if the disk if too short for further superblock */
473 if (ret == 0 && errno == 0)
474 return 0;
476 error("failed to read the superblock on %s at %llu",
477 filename, (unsigned long long)sb_bytenr);
478 error("error = '%m', errno = %d", errno);
479 return 1;
481 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
482 printf("---------------------------------------------------------\n");
483 if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
484 error("bad magic on superblock on %s at %llu",
485 filename, (unsigned long long)sb_bytenr);
486 } else {
487 dump_superblock(sb, full);
489 return 0;
492 const char * const cmd_inspect_dump_super_usage[] = {
493 "btrfs inspect-internal dump-super [options] device [device...]",
494 "Dump superblock from a device in a textual form",
495 "-f|--full print full superblock information, backup roots etc.",
496 "-a|--all print information about all superblocks",
497 "-s|--super <super> specify which copy to print out (values: 0, 1, 2)",
498 "-F|--force attempt to dump superblocks with bad magic",
499 "--bytenr <offset> specify alternate superblock offset",
501 "Deprecated syntax:",
502 "-s <bytenr> specify alternate superblock offset, values other than 0, 1, 2",
503 " will be interpreted as --bytenr for backward compatibility,",
504 " option renamed for consistency with other tools (eg. check)",
505 "-i <super> specify which copy to print out (values: 0, 1, 2), now moved",
506 " to -s|--super",
507 NULL
510 int cmd_inspect_dump_super(int argc, char **argv)
512 int all = 0;
513 int full = 0;
514 int force = 0;
515 char *filename;
516 int fd = -1;
517 int i;
518 int ret = 0;
519 u64 arg;
520 u64 sb_bytenr = btrfs_sb_offset(0);
522 while (1) {
523 int c;
524 enum { GETOPT_VAL_BYTENR = 257 };
525 static const struct option long_options[] = {
526 {"all", no_argument, NULL, 'a'},
527 {"bytenr", required_argument, NULL, GETOPT_VAL_BYTENR },
528 {"full", no_argument, NULL, 'f'},
529 {"force", no_argument, NULL, 'F'},
530 {"super", required_argument, NULL, 's' },
531 {NULL, 0, NULL, 0}
534 c = getopt_long(argc, argv, "fFai:s:", long_options, NULL);
535 if (c < 0)
536 break;
538 switch (c) {
539 case 'i':
540 warning(
541 "option -i is deprecated, please use -s or --super");
542 arg = arg_strtou64(optarg);
543 if (arg >= BTRFS_SUPER_MIRROR_MAX) {
544 error("super mirror too big: %llu >= %d",
545 arg, BTRFS_SUPER_MIRROR_MAX);
546 return 1;
548 sb_bytenr = btrfs_sb_offset(arg);
549 break;
551 case 'a':
552 all = 1;
553 break;
554 case 'f':
555 full = 1;
556 break;
557 case 'F':
558 force = 1;
559 break;
560 case 's':
561 arg = arg_strtou64(optarg);
562 if (BTRFS_SUPER_MIRROR_MAX <= arg) {
563 warning(
564 "deprecated use of -s <bytenr> with %llu, assuming --bytenr",
565 (unsigned long long)arg);
566 sb_bytenr = arg;
567 } else {
568 sb_bytenr = btrfs_sb_offset(arg);
570 all = 0;
571 break;
572 case GETOPT_VAL_BYTENR:
573 arg = arg_strtou64(optarg);
574 sb_bytenr = arg;
575 all = 0;
576 break;
577 default:
578 usage(cmd_inspect_dump_super_usage);
582 if (check_argc_min(argc - optind, 1))
583 usage(cmd_inspect_dump_super_usage);
585 for (i = optind; i < argc; i++) {
586 filename = argv[i];
587 fd = open(filename, O_RDONLY);
588 if (fd < 0) {
589 error("cannot open %s: %m", filename);
590 ret = 1;
591 goto out;
594 if (all) {
595 int idx;
597 for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
598 sb_bytenr = btrfs_sb_offset(idx);
599 if (load_and_dump_sb(filename, fd,
600 sb_bytenr, full, force)) {
601 close(fd);
602 ret = 1;
603 goto out;
606 putchar('\n');
608 } else {
609 load_and_dump_sb(filename, fd, sb_bytenr, full, force);
610 putchar('\n');
612 close(fd);
615 out:
616 return ret;