2 * Copyright (C) 2012 STRATO AG. 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.
25 #include <uuid/uuid.h>
28 #include "kerncompat.h"
31 #include "print-tree.h"
32 #include "transaction.h"
37 static void print_usage(void);
38 static void dump_superblock(struct btrfs_super_block *sb, int full);
39 int main(int argc, char **argv);
40 static int load_and_dump_sb(char *, int fd, u64 sb_bytenr, int full, int force);
43 static void print_usage(void)
46 "usage: btrfs-show-super [-i super_mirror|-a|-f|-F] dev [dev..]\n");
47 fprintf(stderr, "\t-f : print full superblock information\n");
48 fprintf(stderr, "\t-a : print information of all superblocks\n");
49 fprintf(stderr, "\t-i <super_mirror> : specify which mirror to print out\n");
50 fprintf(stderr, "\t-F : attempt to dump superblocks with bad magic\n");
51 fprintf(stderr, "%s\n", PACKAGE_STRING);
54 int main(int argc, char **argv)
64 u64 sb_bytenr = btrfs_sb_offset(0);
66 while ((opt = getopt(argc, argv, "fFai:")) != -1) {
69 arg = arg_strtou64(optarg);
70 if (arg >= BTRFS_SUPER_MIRROR_MAX) {
72 "Illegal super_mirror %llu\n",
77 sb_bytenr = btrfs_sb_offset(arg);
96 if (check_argc_min(argc - optind, 1)) {
101 for (i = optind; i < argc; i++) {
103 fd = open(filename, O_RDONLY, 0666);
105 fprintf(stderr, "Could not open %s\n", filename);
111 for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
112 sb_bytenr = btrfs_sb_offset(idx);
113 if (load_and_dump_sb(filename, fd,
114 sb_bytenr, full, force)) {
122 load_and_dump_sb(filename, fd, sb_bytenr, full, force);
131 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
134 u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
135 struct btrfs_super_block *sb;
138 sb = (struct btrfs_super_block *)super_block_data;
140 ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
141 if (ret != BTRFS_SUPER_INFO_SIZE) {
144 /* check if the disk if too short for further superblock */
145 if (ret == 0 && e == 0)
149 "ERROR: Failed to read the superblock on %s at %llu\n",
150 filename, (unsigned long long)sb_bytenr);
152 "ERROR: error = '%s', errno = %d\n", strerror(e), e);
155 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
156 printf("---------------------------------------------------------\n");
157 if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
159 "ERROR: bad magic on superblock on %s at %llu\n",
160 filename, (unsigned long long)sb_bytenr);
162 dump_superblock(sb, full);
167 static int check_csum_sblock(void *sb, int csum_size)
169 char result[BTRFS_CSUM_SIZE];
172 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE,
173 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
174 btrfs_csum_final(crc, result);
176 return !memcmp(sb, &result, csum_size);
179 static void print_sys_chunk_array(struct btrfs_super_block *sb)
181 struct extent_buffer *buf;
182 struct btrfs_disk_key *disk_key;
183 struct btrfs_chunk *chunk;
184 struct btrfs_key key;
190 buf = malloc(sizeof(*buf) + sizeof(*sb));
192 fprintf(stderr, "%s\n", strerror(ENOMEM));
195 write_extent_buffer(buf, sb, 0, sizeof(*sb));
196 ptr = sb->sys_chunk_array;
197 array_end = ptr + btrfs_super_sys_array_size(sb);
199 while (ptr < array_end) {
200 disk_key = (struct btrfs_disk_key *)ptr;
201 btrfs_disk_key_to_cpu(&key, disk_key);
203 printf("\titem %d ", i);
204 btrfs_print_key(disk_key);
206 len = sizeof(*disk_key);
210 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
211 chunk = (struct btrfs_chunk *)(ptr - (u8 *)sb);
212 print_chunk(buf, chunk);
213 num_stripes = btrfs_chunk_num_stripes(buf, chunk);
214 len = btrfs_chunk_item_size(num_stripes);
226 static int empty_backup(struct btrfs_root_backup *backup)
228 if (backup == NULL ||
229 (backup->tree_root == 0 &&
230 backup->tree_root_gen == 0))
235 static void print_root_backup(struct btrfs_root_backup *backup)
237 printf("\t\tbackup_tree_root:\t%llu\tgen: %llu\tlevel: %d\n",
238 btrfs_backup_tree_root(backup),
239 btrfs_backup_tree_root_gen(backup),
240 btrfs_backup_tree_root_level(backup));
241 printf("\t\tbackup_chunk_root:\t%llu\tgen: %llu\tlevel: %d\n",
242 btrfs_backup_chunk_root(backup),
243 btrfs_backup_chunk_root_gen(backup),
244 btrfs_backup_chunk_root_level(backup));
245 printf("\t\tbackup_extent_root:\t%llu\tgen: %llu\tlevel: %d\n",
246 btrfs_backup_extent_root(backup),
247 btrfs_backup_extent_root_gen(backup),
248 btrfs_backup_extent_root_level(backup));
249 printf("\t\tbackup_fs_root:\t\t%llu\tgen: %llu\tlevel: %d\n",
250 btrfs_backup_fs_root(backup),
251 btrfs_backup_fs_root_gen(backup),
252 btrfs_backup_fs_root_level(backup));
253 printf("\t\tbackup_dev_root:\t%llu\tgen: %llu\tlevel: %d\n",
254 btrfs_backup_dev_root(backup),
255 btrfs_backup_dev_root_gen(backup),
256 btrfs_backup_dev_root_level(backup));
257 printf("\t\tbackup_csum_root:\t%llu\tgen: %llu\tlevel: %d\n",
258 btrfs_backup_csum_root(backup),
259 btrfs_backup_csum_root_gen(backup),
260 btrfs_backup_csum_root_level(backup));
262 printf("\t\tbackup_total_bytes:\t%llu\n",
263 btrfs_backup_total_bytes(backup));
264 printf("\t\tbackup_bytes_used:\t%llu\n",
265 btrfs_backup_bytes_used(backup));
266 printf("\t\tbackup_num_devices:\t%llu\n",
267 btrfs_backup_num_devices(backup));
271 static void print_backup_roots(struct btrfs_super_block *sb)
273 struct btrfs_root_backup *backup;
276 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
277 backup = sb->super_roots + i;
278 if (!empty_backup(backup)) {
279 printf("\tbackup %d:\n", i);
280 print_root_backup(backup);
285 struct readable_flag_entry {
290 #define DEF_INCOMPAT_FLAG_ENTRY(bit_name) \
291 {BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
293 struct readable_flag_entry incompat_flags_array[] = {
294 DEF_INCOMPAT_FLAG_ENTRY(MIXED_BACKREF),
295 DEF_INCOMPAT_FLAG_ENTRY(DEFAULT_SUBVOL),
296 DEF_INCOMPAT_FLAG_ENTRY(MIXED_GROUPS),
297 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZO),
298 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZOv2),
299 DEF_INCOMPAT_FLAG_ENTRY(BIG_METADATA),
300 DEF_INCOMPAT_FLAG_ENTRY(EXTENDED_IREF),
301 DEF_INCOMPAT_FLAG_ENTRY(RAID56),
302 DEF_INCOMPAT_FLAG_ENTRY(SKINNY_METADATA),
303 DEF_INCOMPAT_FLAG_ENTRY(NO_HOLES)
305 static const int incompat_flags_num = sizeof(incompat_flags_array) /
306 sizeof(struct readable_flag_entry);
308 #define DEF_HEADER_FLAG_ENTRY(bit_name) \
309 {BTRFS_HEADER_FLAG_##bit_name, #bit_name}
310 #define DEF_SUPER_FLAG_ENTRY(bit_name) \
311 {BTRFS_SUPER_FLAG_##bit_name, #bit_name}
313 struct readable_flag_entry super_flags_array[] = {
314 DEF_HEADER_FLAG_ENTRY(WRITTEN),
315 DEF_HEADER_FLAG_ENTRY(RELOC),
316 DEF_SUPER_FLAG_ENTRY(CHANGING_FSID),
317 DEF_SUPER_FLAG_ENTRY(SEEDING),
318 DEF_SUPER_FLAG_ENTRY(METADUMP),
319 DEF_SUPER_FLAG_ENTRY(METADUMP_V2)
321 static const int super_flags_num = ARRAY_SIZE(super_flags_array);
323 #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
324 BTRFS_HEADER_FLAG_RELOC |\
325 BTRFS_SUPER_FLAG_CHANGING_FSID |\
326 BTRFS_SUPER_FLAG_SEEDING |\
327 BTRFS_SUPER_FLAG_METADUMP |\
328 BTRFS_SUPER_FLAG_METADUMP_V2)
330 static void __print_readable_flag(u64 flag, struct readable_flag_entry *array,
331 int array_size, u64 supported_flags)
335 struct readable_flag_entry *entry;
341 for (i = 0; i < array_size; i++) {
343 if (flag & entry->bit) {
345 printf("%s ", entry->output);
347 printf("|\n\t\t\t %s ", entry->output);
351 flag &= ~supported_flags;
354 printf("unknown flag: 0x%llx ", flag);
356 printf("|\n\t\t\t unknown flag: 0x%llx ", flag);
361 static void print_readable_incompat_flag(u64 flag)
363 return __print_readable_flag(flag, incompat_flags_array,
365 BTRFS_FEATURE_INCOMPAT_SUPP);
368 static void print_readable_super_flag(u64 flag)
370 return __print_readable_flag(flag, super_flags_array,
371 super_flags_num, BTRFS_SUPER_FLAG_SUPP);
374 static void dump_superblock(struct btrfs_super_block *sb, int full)
377 char *s, buf[BTRFS_UUID_UNPARSED_SIZE];
380 printf("csum\t\t\t0x");
381 for (i = 0, p = sb->csum; i < btrfs_super_csum_size(sb); i++)
382 printf("%02x", p[i]);
383 if (check_csum_sblock(sb, btrfs_super_csum_size(sb)))
386 printf(" [DON'T MATCH]");
389 printf("bytenr\t\t\t%llu\n",
390 (unsigned long long)btrfs_super_bytenr(sb));
391 printf("flags\t\t\t0x%llx\n",
392 (unsigned long long)btrfs_super_flags(sb));
393 print_readable_super_flag(btrfs_super_flags(sb));
395 printf("magic\t\t\t");
396 s = (char *) &sb->magic;
397 for (i = 0; i < 8; i++)
398 putchar(isprint(s[i]) ? s[i] : '.');
399 if (btrfs_super_magic(sb) == BTRFS_MAGIC)
400 printf(" [match]\n");
402 printf(" [DON'T MATCH]\n");
404 uuid_unparse(sb->fsid, buf);
405 printf("fsid\t\t\t%s\n", buf);
407 printf("label\t\t\t");
409 for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
410 putchar(isprint(s[i]) ? s[i] : '.');
413 printf("generation\t\t%llu\n",
414 (unsigned long long)btrfs_super_generation(sb));
415 printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
416 printf("sys_array_size\t\t%llu\n",
417 (unsigned long long)btrfs_super_sys_array_size(sb));
418 printf("chunk_root_generation\t%llu\n",
419 (unsigned long long)btrfs_super_chunk_root_generation(sb));
420 printf("root_level\t\t%llu\n",
421 (unsigned long long)btrfs_super_root_level(sb));
422 printf("chunk_root\t\t%llu\n",
423 (unsigned long long)btrfs_super_chunk_root(sb));
424 printf("chunk_root_level\t%llu\n",
425 (unsigned long long)btrfs_super_chunk_root_level(sb));
426 printf("log_root\t\t%llu\n",
427 (unsigned long long)btrfs_super_log_root(sb));
428 printf("log_root_transid\t%llu\n",
429 (unsigned long long)btrfs_super_log_root_transid(sb));
430 printf("log_root_level\t\t%llu\n",
431 (unsigned long long)btrfs_super_log_root_level(sb));
432 printf("total_bytes\t\t%llu\n",
433 (unsigned long long)btrfs_super_total_bytes(sb));
434 printf("bytes_used\t\t%llu\n",
435 (unsigned long long)btrfs_super_bytes_used(sb));
436 printf("sectorsize\t\t%llu\n",
437 (unsigned long long)btrfs_super_sectorsize(sb));
438 printf("nodesize\t\t%llu\n",
439 (unsigned long long)btrfs_super_nodesize(sb));
440 printf("leafsize\t\t%llu\n",
441 (unsigned long long)btrfs_super_leafsize(sb));
442 printf("stripesize\t\t%llu\n",
443 (unsigned long long)btrfs_super_stripesize(sb));
444 printf("root_dir\t\t%llu\n",
445 (unsigned long long)btrfs_super_root_dir(sb));
446 printf("num_devices\t\t%llu\n",
447 (unsigned long long)btrfs_super_num_devices(sb));
448 printf("compat_flags\t\t0x%llx\n",
449 (unsigned long long)btrfs_super_compat_flags(sb));
450 printf("compat_ro_flags\t\t0x%llx\n",
451 (unsigned long long)btrfs_super_compat_ro_flags(sb));
452 printf("incompat_flags\t\t0x%llx\n",
453 (unsigned long long)btrfs_super_incompat_flags(sb));
454 print_readable_incompat_flag(btrfs_super_incompat_flags(sb));
455 printf("csum_type\t\t%llu\n",
456 (unsigned long long)btrfs_super_csum_type(sb));
457 printf("csum_size\t\t%llu\n",
458 (unsigned long long)btrfs_super_csum_size(sb));
459 printf("cache_generation\t%llu\n",
460 (unsigned long long)btrfs_super_cache_generation(sb));
461 printf("uuid_tree_generation\t%llu\n",
462 (unsigned long long)btrfs_super_uuid_tree_generation(sb));
464 uuid_unparse(sb->dev_item.uuid, buf);
465 printf("dev_item.uuid\t\t%s\n", buf);
467 uuid_unparse(sb->dev_item.fsid, buf);
468 printf("dev_item.fsid\t\t%s %s\n", buf,
469 !memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
470 "[match]" : "[DON'T MATCH]");
472 printf("dev_item.type\t\t%llu\n", (unsigned long long)
473 btrfs_stack_device_type(&sb->dev_item));
474 printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
475 btrfs_stack_device_total_bytes(&sb->dev_item));
476 printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
477 btrfs_stack_device_bytes_used(&sb->dev_item));
478 printf("dev_item.io_align\t%u\n", (unsigned int)
479 btrfs_stack_device_io_align(&sb->dev_item));
480 printf("dev_item.io_width\t%u\n", (unsigned int)
481 btrfs_stack_device_io_width(&sb->dev_item));
482 printf("dev_item.sector_size\t%u\n", (unsigned int)
483 btrfs_stack_device_sector_size(&sb->dev_item));
484 printf("dev_item.devid\t\t%llu\n",
485 btrfs_stack_device_id(&sb->dev_item));
486 printf("dev_item.dev_group\t%u\n", (unsigned int)
487 btrfs_stack_device_group(&sb->dev_item));
488 printf("dev_item.seek_speed\t%u\n", (unsigned int)
489 btrfs_stack_device_seek_speed(&sb->dev_item));
490 printf("dev_item.bandwidth\t%u\n", (unsigned int)
491 btrfs_stack_device_bandwidth(&sb->dev_item));
492 printf("dev_item.generation\t%llu\n", (unsigned long long)
493 btrfs_stack_device_generation(&sb->dev_item));
495 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
496 print_sys_chunk_array(sb);
497 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS);
498 print_backup_roots(sb);