btrfs-progs: test/fsck/021: Cleanup custom check by overriding check_image
[btrfs-progs-unstable/devel.git] / cmds-inspect-dump-super.c
blob23a711553571b40236caa28537d363c5304db24f
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 (check_csum_sblock(sb, csum_size))
343 printf(" [match]");
344 else
345 printf(" [DON'T MATCH]");
346 putchar('\n');
348 printf("bytenr\t\t\t%llu\n",
349 (unsigned long long)btrfs_super_bytenr(sb));
350 printf("flags\t\t\t0x%llx\n",
351 (unsigned long long)btrfs_super_flags(sb));
352 print_readable_super_flag(btrfs_super_flags(sb));
354 printf("magic\t\t\t");
355 s = (char *) &sb->magic;
356 for (i = 0; i < 8; i++)
357 putchar(isprint(s[i]) ? s[i] : '.');
358 if (btrfs_super_magic(sb) == BTRFS_MAGIC)
359 printf(" [match]\n");
360 else
361 printf(" [DON'T MATCH]\n");
363 uuid_unparse(sb->fsid, buf);
364 printf("fsid\t\t\t%s\n", buf);
366 printf("label\t\t\t");
367 s = sb->label;
368 for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
369 putchar(isprint(s[i]) ? s[i] : '.');
370 putchar('\n');
372 printf("generation\t\t%llu\n",
373 (unsigned long long)btrfs_super_generation(sb));
374 printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
375 printf("sys_array_size\t\t%llu\n",
376 (unsigned long long)btrfs_super_sys_array_size(sb));
377 printf("chunk_root_generation\t%llu\n",
378 (unsigned long long)btrfs_super_chunk_root_generation(sb));
379 printf("root_level\t\t%llu\n",
380 (unsigned long long)btrfs_super_root_level(sb));
381 printf("chunk_root\t\t%llu\n",
382 (unsigned long long)btrfs_super_chunk_root(sb));
383 printf("chunk_root_level\t%llu\n",
384 (unsigned long long)btrfs_super_chunk_root_level(sb));
385 printf("log_root\t\t%llu\n",
386 (unsigned long long)btrfs_super_log_root(sb));
387 printf("log_root_transid\t%llu\n",
388 (unsigned long long)btrfs_super_log_root_transid(sb));
389 printf("log_root_level\t\t%llu\n",
390 (unsigned long long)btrfs_super_log_root_level(sb));
391 printf("total_bytes\t\t%llu\n",
392 (unsigned long long)btrfs_super_total_bytes(sb));
393 printf("bytes_used\t\t%llu\n",
394 (unsigned long long)btrfs_super_bytes_used(sb));
395 printf("sectorsize\t\t%llu\n",
396 (unsigned long long)btrfs_super_sectorsize(sb));
397 printf("nodesize\t\t%llu\n",
398 (unsigned long long)btrfs_super_nodesize(sb));
399 printf("leafsize (deprecated)\t\t%u\n",
400 le32_to_cpu(sb->__unused_leafsize));
401 printf("stripesize\t\t%llu\n",
402 (unsigned long long)btrfs_super_stripesize(sb));
403 printf("root_dir\t\t%llu\n",
404 (unsigned long long)btrfs_super_root_dir(sb));
405 printf("num_devices\t\t%llu\n",
406 (unsigned long long)btrfs_super_num_devices(sb));
407 printf("compat_flags\t\t0x%llx\n",
408 (unsigned long long)btrfs_super_compat_flags(sb));
409 printf("compat_ro_flags\t\t0x%llx\n",
410 (unsigned long long)btrfs_super_compat_ro_flags(sb));
411 print_readable_compat_ro_flag(btrfs_super_compat_ro_flags(sb));
412 printf("incompat_flags\t\t0x%llx\n",
413 (unsigned long long)btrfs_super_incompat_flags(sb));
414 print_readable_incompat_flag(btrfs_super_incompat_flags(sb));
415 printf("cache_generation\t%llu\n",
416 (unsigned long long)btrfs_super_cache_generation(sb));
417 printf("uuid_tree_generation\t%llu\n",
418 (unsigned long long)btrfs_super_uuid_tree_generation(sb));
420 uuid_unparse(sb->dev_item.uuid, buf);
421 printf("dev_item.uuid\t\t%s\n", buf);
423 uuid_unparse(sb->dev_item.fsid, buf);
424 printf("dev_item.fsid\t\t%s %s\n", buf,
425 !memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
426 "[match]" : "[DON'T MATCH]");
428 printf("dev_item.type\t\t%llu\n", (unsigned long long)
429 btrfs_stack_device_type(&sb->dev_item));
430 printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
431 btrfs_stack_device_total_bytes(&sb->dev_item));
432 printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
433 btrfs_stack_device_bytes_used(&sb->dev_item));
434 printf("dev_item.io_align\t%u\n", (unsigned int)
435 btrfs_stack_device_io_align(&sb->dev_item));
436 printf("dev_item.io_width\t%u\n", (unsigned int)
437 btrfs_stack_device_io_width(&sb->dev_item));
438 printf("dev_item.sector_size\t%u\n", (unsigned int)
439 btrfs_stack_device_sector_size(&sb->dev_item));
440 printf("dev_item.devid\t\t%llu\n",
441 btrfs_stack_device_id(&sb->dev_item));
442 printf("dev_item.dev_group\t%u\n", (unsigned int)
443 btrfs_stack_device_group(&sb->dev_item));
444 printf("dev_item.seek_speed\t%u\n", (unsigned int)
445 btrfs_stack_device_seek_speed(&sb->dev_item));
446 printf("dev_item.bandwidth\t%u\n", (unsigned int)
447 btrfs_stack_device_bandwidth(&sb->dev_item));
448 printf("dev_item.generation\t%llu\n", (unsigned long long)
449 btrfs_stack_device_generation(&sb->dev_item));
450 if (full) {
451 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
452 print_sys_chunk_array(sb);
453 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS);
454 print_backup_roots(sb);
458 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
459 int force)
461 u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
462 struct btrfs_super_block *sb;
463 u64 ret;
465 sb = (struct btrfs_super_block *)super_block_data;
467 ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
468 if (ret != BTRFS_SUPER_INFO_SIZE) {
469 /* check if the disk if too short for further superblock */
470 if (ret == 0 && errno == 0)
471 return 0;
473 error("failed to read the superblock on %s at %llu",
474 filename, (unsigned long long)sb_bytenr);
475 error("error = '%s', errno = %d", strerror(errno), errno);
476 return 1;
478 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
479 printf("---------------------------------------------------------\n");
480 if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
481 error("bad magic on superblock on %s at %llu",
482 filename, (unsigned long long)sb_bytenr);
483 } else {
484 dump_superblock(sb, full);
486 return 0;
489 const char * const cmd_inspect_dump_super_usage[] = {
490 "btrfs inspect-internal dump-super [options] device [device...]",
491 "Dump superblock from a device in a textual form",
492 "-f|--full print full superblock information, backup roots etc.",
493 "-a|--all print information about all superblocks",
494 "-s|--super <super> specify which copy to print out (values: 0, 1, 2)",
495 "-F|--force attempt to dump superblocks with bad magic",
496 "--bytenr <offset> specify alternate superblock offset",
498 "Deprecated syntax:",
499 "-s <bytenr> specify alternate superblock offset, values other than 0, 1, 2",
500 " will be interpreted as --bytenr for backward compatibility,",
501 " option renamed for consistency with other tools (eg. check)",
502 "-i <super> specify which copy to print out (values: 0, 1, 2), now moved",
503 " to -s|--super",
504 NULL
507 int cmd_inspect_dump_super(int argc, char **argv)
509 int all = 0;
510 int full = 0;
511 int force = 0;
512 char *filename;
513 int fd = -1;
514 int i;
515 int ret = 0;
516 u64 arg;
517 u64 sb_bytenr = btrfs_sb_offset(0);
519 while (1) {
520 int c;
521 enum { GETOPT_VAL_BYTENR = 257 };
522 static const struct option long_options[] = {
523 {"all", no_argument, NULL, 'a'},
524 {"bytenr", required_argument, NULL, GETOPT_VAL_BYTENR },
525 {"full", no_argument, NULL, 'f'},
526 {"force", no_argument, NULL, 'F'},
527 {"super", required_argument, NULL, 's' },
528 {NULL, 0, NULL, 0}
531 c = getopt_long(argc, argv, "fFai:s:", long_options, NULL);
532 if (c < 0)
533 break;
535 switch (c) {
536 case 'i':
537 warning(
538 "option -i is deprecated, please use -s or --super");
539 arg = arg_strtou64(optarg);
540 if (arg >= BTRFS_SUPER_MIRROR_MAX) {
541 error("super mirror too big: %llu >= %d",
542 arg, BTRFS_SUPER_MIRROR_MAX);
543 return 1;
545 sb_bytenr = btrfs_sb_offset(arg);
546 break;
548 case 'a':
549 all = 1;
550 break;
551 case 'f':
552 full = 1;
553 break;
554 case 'F':
555 force = 1;
556 break;
557 case 's':
558 arg = arg_strtou64(optarg);
559 if (BTRFS_SUPER_MIRROR_MAX <= arg) {
560 warning(
561 "deprecated use of -s <bytenr> with %llu, assuming --bytenr",
562 (unsigned long long)arg);
563 sb_bytenr = arg;
564 } else {
565 sb_bytenr = btrfs_sb_offset(arg);
567 all = 0;
568 break;
569 case GETOPT_VAL_BYTENR:
570 arg = arg_strtou64(optarg);
571 sb_bytenr = arg;
572 all = 0;
573 break;
574 default:
575 usage(cmd_inspect_dump_super_usage);
579 if (check_argc_min(argc - optind, 1))
580 usage(cmd_inspect_dump_super_usage);
582 for (i = optind; i < argc; i++) {
583 filename = argv[i];
584 fd = open(filename, O_RDONLY);
585 if (fd < 0) {
586 error("cannot open %s: %s", filename, strerror(errno));
587 ret = 1;
588 goto out;
591 if (all) {
592 int idx;
594 for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
595 sb_bytenr = btrfs_sb_offset(idx);
596 if (load_and_dump_sb(filename, fd,
597 sb_bytenr, full, force)) {
598 close(fd);
599 ret = 1;
600 goto out;
603 putchar('\n');
605 } else {
606 load_and_dump_sb(filename, fd, sb_bytenr, full, force);
607 putchar('\n');
609 close(fd);
612 out:
613 return ret;