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
, "\t-s <bytenr> : specify alternate superblock offset\n");
52 fprintf(stderr
, "%s\n", PACKAGE_STRING
);
55 int main(int argc
, char **argv
)
65 u64 sb_bytenr
= btrfs_sb_offset(0);
67 while ((opt
= getopt(argc
, argv
, "fFai:s:")) != -1) {
70 arg
= arg_strtou64(optarg
);
71 if (arg
>= BTRFS_SUPER_MIRROR_MAX
) {
73 "Illegal super_mirror %llu\n",
78 sb_bytenr
= btrfs_sb_offset(arg
);
91 sb_bytenr
= arg_strtou64(optarg
);
101 if (check_argc_min(argc
- optind
, 1)) {
106 for (i
= optind
; i
< argc
; i
++) {
108 fd
= open(filename
, O_RDONLY
, 0666);
110 fprintf(stderr
, "Could not open %s\n", filename
);
116 for (idx
= 0; idx
< BTRFS_SUPER_MIRROR_MAX
; idx
++) {
117 sb_bytenr
= btrfs_sb_offset(idx
);
118 if (load_and_dump_sb(filename
, fd
,
119 sb_bytenr
, full
, force
)) {
127 load_and_dump_sb(filename
, fd
, sb_bytenr
, full
, force
);
136 static int load_and_dump_sb(char *filename
, int fd
, u64 sb_bytenr
, int full
,
139 u8 super_block_data
[BTRFS_SUPER_INFO_SIZE
];
140 struct btrfs_super_block
*sb
;
143 sb
= (struct btrfs_super_block
*)super_block_data
;
145 ret
= pread64(fd
, super_block_data
, BTRFS_SUPER_INFO_SIZE
, sb_bytenr
);
146 if (ret
!= BTRFS_SUPER_INFO_SIZE
) {
149 /* check if the disk if too short for further superblock */
150 if (ret
== 0 && e
== 0)
154 "ERROR: Failed to read the superblock on %s at %llu\n",
155 filename
, (unsigned long long)sb_bytenr
);
157 "ERROR: error = '%s', errno = %d\n", strerror(e
), e
);
160 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr
, filename
);
161 printf("---------------------------------------------------------\n");
162 if (btrfs_super_magic(sb
) != BTRFS_MAGIC
&& !force
) {
164 "ERROR: bad magic on superblock on %s at %llu\n",
165 filename
, (unsigned long long)sb_bytenr
);
167 dump_superblock(sb
, full
);
172 static int check_csum_sblock(void *sb
, int csum_size
)
174 char result
[BTRFS_CSUM_SIZE
];
177 crc
= btrfs_csum_data(NULL
, (char *)sb
+ BTRFS_CSUM_SIZE
,
178 crc
, BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
179 btrfs_csum_final(crc
, result
);
181 return !memcmp(sb
, &result
, csum_size
);
184 static void print_sys_chunk_array(struct btrfs_super_block
*sb
)
186 struct extent_buffer
*buf
;
187 struct btrfs_disk_key
*disk_key
;
188 struct btrfs_chunk
*chunk
;
189 struct btrfs_key key
;
195 buf
= malloc(sizeof(*buf
) + sizeof(*sb
));
197 fprintf(stderr
, "%s\n", strerror(ENOMEM
));
200 write_extent_buffer(buf
, sb
, 0, sizeof(*sb
));
201 ptr
= sb
->sys_chunk_array
;
202 array_end
= ptr
+ btrfs_super_sys_array_size(sb
);
204 while (ptr
< array_end
) {
205 disk_key
= (struct btrfs_disk_key
*)ptr
;
206 btrfs_disk_key_to_cpu(&key
, disk_key
);
208 printf("\titem %d ", i
);
209 btrfs_print_key(disk_key
);
211 len
= sizeof(*disk_key
);
215 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
216 chunk
= (struct btrfs_chunk
*)(ptr
- (u8
*)sb
);
217 print_chunk(buf
, chunk
);
218 num_stripes
= btrfs_chunk_num_stripes(buf
, chunk
);
219 len
= btrfs_chunk_item_size(num_stripes
);
231 static int empty_backup(struct btrfs_root_backup
*backup
)
233 if (backup
== NULL
||
234 (backup
->tree_root
== 0 &&
235 backup
->tree_root_gen
== 0))
240 static void print_root_backup(struct btrfs_root_backup
*backup
)
242 printf("\t\tbackup_tree_root:\t%llu\tgen: %llu\tlevel: %d\n",
243 btrfs_backup_tree_root(backup
),
244 btrfs_backup_tree_root_gen(backup
),
245 btrfs_backup_tree_root_level(backup
));
246 printf("\t\tbackup_chunk_root:\t%llu\tgen: %llu\tlevel: %d\n",
247 btrfs_backup_chunk_root(backup
),
248 btrfs_backup_chunk_root_gen(backup
),
249 btrfs_backup_chunk_root_level(backup
));
250 printf("\t\tbackup_extent_root:\t%llu\tgen: %llu\tlevel: %d\n",
251 btrfs_backup_extent_root(backup
),
252 btrfs_backup_extent_root_gen(backup
),
253 btrfs_backup_extent_root_level(backup
));
254 printf("\t\tbackup_fs_root:\t\t%llu\tgen: %llu\tlevel: %d\n",
255 btrfs_backup_fs_root(backup
),
256 btrfs_backup_fs_root_gen(backup
),
257 btrfs_backup_fs_root_level(backup
));
258 printf("\t\tbackup_dev_root:\t%llu\tgen: %llu\tlevel: %d\n",
259 btrfs_backup_dev_root(backup
),
260 btrfs_backup_dev_root_gen(backup
),
261 btrfs_backup_dev_root_level(backup
));
262 printf("\t\tbackup_csum_root:\t%llu\tgen: %llu\tlevel: %d\n",
263 btrfs_backup_csum_root(backup
),
264 btrfs_backup_csum_root_gen(backup
),
265 btrfs_backup_csum_root_level(backup
));
267 printf("\t\tbackup_total_bytes:\t%llu\n",
268 btrfs_backup_total_bytes(backup
));
269 printf("\t\tbackup_bytes_used:\t%llu\n",
270 btrfs_backup_bytes_used(backup
));
271 printf("\t\tbackup_num_devices:\t%llu\n",
272 btrfs_backup_num_devices(backup
));
276 static void print_backup_roots(struct btrfs_super_block
*sb
)
278 struct btrfs_root_backup
*backup
;
281 for (i
= 0; i
< BTRFS_NUM_BACKUP_ROOTS
; i
++) {
282 backup
= sb
->super_roots
+ i
;
283 if (!empty_backup(backup
)) {
284 printf("\tbackup %d:\n", i
);
285 print_root_backup(backup
);
290 struct readable_flag_entry
{
295 #define DEF_INCOMPAT_FLAG_ENTRY(bit_name) \
296 {BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
298 struct readable_flag_entry incompat_flags_array
[] = {
299 DEF_INCOMPAT_FLAG_ENTRY(MIXED_BACKREF
),
300 DEF_INCOMPAT_FLAG_ENTRY(DEFAULT_SUBVOL
),
301 DEF_INCOMPAT_FLAG_ENTRY(MIXED_GROUPS
),
302 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZO
),
303 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZOv2
),
304 DEF_INCOMPAT_FLAG_ENTRY(BIG_METADATA
),
305 DEF_INCOMPAT_FLAG_ENTRY(EXTENDED_IREF
),
306 DEF_INCOMPAT_FLAG_ENTRY(RAID56
),
307 DEF_INCOMPAT_FLAG_ENTRY(SKINNY_METADATA
),
308 DEF_INCOMPAT_FLAG_ENTRY(NO_HOLES
)
310 static const int incompat_flags_num
= sizeof(incompat_flags_array
) /
311 sizeof(struct readable_flag_entry
);
313 #define DEF_HEADER_FLAG_ENTRY(bit_name) \
314 {BTRFS_HEADER_FLAG_##bit_name, #bit_name}
315 #define DEF_SUPER_FLAG_ENTRY(bit_name) \
316 {BTRFS_SUPER_FLAG_##bit_name, #bit_name}
318 struct readable_flag_entry super_flags_array
[] = {
319 DEF_HEADER_FLAG_ENTRY(WRITTEN
),
320 DEF_HEADER_FLAG_ENTRY(RELOC
),
321 DEF_SUPER_FLAG_ENTRY(CHANGING_FSID
),
322 DEF_SUPER_FLAG_ENTRY(SEEDING
),
323 DEF_SUPER_FLAG_ENTRY(METADUMP
),
324 DEF_SUPER_FLAG_ENTRY(METADUMP_V2
)
326 static const int super_flags_num
= ARRAY_SIZE(super_flags_array
);
328 #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
329 BTRFS_HEADER_FLAG_RELOC |\
330 BTRFS_SUPER_FLAG_CHANGING_FSID |\
331 BTRFS_SUPER_FLAG_SEEDING |\
332 BTRFS_SUPER_FLAG_METADUMP |\
333 BTRFS_SUPER_FLAG_METADUMP_V2)
335 static void __print_readable_flag(u64 flag
, struct readable_flag_entry
*array
,
336 int array_size
, u64 supported_flags
)
340 struct readable_flag_entry
*entry
;
346 for (i
= 0; i
< array_size
; i
++) {
348 if (flag
& entry
->bit
) {
350 printf("%s ", entry
->output
);
352 printf("|\n\t\t\t %s ", entry
->output
);
356 flag
&= ~supported_flags
;
359 printf("unknown flag: 0x%llx ", flag
);
361 printf("|\n\t\t\t unknown flag: 0x%llx ", flag
);
366 static void print_readable_incompat_flag(u64 flag
)
368 return __print_readable_flag(flag
, incompat_flags_array
,
370 BTRFS_FEATURE_INCOMPAT_SUPP
);
373 static void print_readable_super_flag(u64 flag
)
375 return __print_readable_flag(flag
, super_flags_array
,
376 super_flags_num
, BTRFS_SUPER_FLAG_SUPP
);
379 static void dump_superblock(struct btrfs_super_block
*sb
, int full
)
382 char *s
, buf
[BTRFS_UUID_UNPARSED_SIZE
];
385 printf("csum\t\t\t0x");
386 for (i
= 0, p
= sb
->csum
; i
< btrfs_super_csum_size(sb
); i
++)
387 printf("%02x", p
[i
]);
388 if (check_csum_sblock(sb
, btrfs_super_csum_size(sb
)))
391 printf(" [DON'T MATCH]");
394 printf("bytenr\t\t\t%llu\n",
395 (unsigned long long)btrfs_super_bytenr(sb
));
396 printf("flags\t\t\t0x%llx\n",
397 (unsigned long long)btrfs_super_flags(sb
));
398 print_readable_super_flag(btrfs_super_flags(sb
));
400 printf("magic\t\t\t");
401 s
= (char *) &sb
->magic
;
402 for (i
= 0; i
< 8; i
++)
403 putchar(isprint(s
[i
]) ? s
[i
] : '.');
404 if (btrfs_super_magic(sb
) == BTRFS_MAGIC
)
405 printf(" [match]\n");
407 printf(" [DON'T MATCH]\n");
409 uuid_unparse(sb
->fsid
, buf
);
410 printf("fsid\t\t\t%s\n", buf
);
412 printf("label\t\t\t");
414 for (i
= 0; i
< BTRFS_LABEL_SIZE
&& s
[i
]; i
++)
415 putchar(isprint(s
[i
]) ? s
[i
] : '.');
418 printf("generation\t\t%llu\n",
419 (unsigned long long)btrfs_super_generation(sb
));
420 printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb
));
421 printf("sys_array_size\t\t%llu\n",
422 (unsigned long long)btrfs_super_sys_array_size(sb
));
423 printf("chunk_root_generation\t%llu\n",
424 (unsigned long long)btrfs_super_chunk_root_generation(sb
));
425 printf("root_level\t\t%llu\n",
426 (unsigned long long)btrfs_super_root_level(sb
));
427 printf("chunk_root\t\t%llu\n",
428 (unsigned long long)btrfs_super_chunk_root(sb
));
429 printf("chunk_root_level\t%llu\n",
430 (unsigned long long)btrfs_super_chunk_root_level(sb
));
431 printf("log_root\t\t%llu\n",
432 (unsigned long long)btrfs_super_log_root(sb
));
433 printf("log_root_transid\t%llu\n",
434 (unsigned long long)btrfs_super_log_root_transid(sb
));
435 printf("log_root_level\t\t%llu\n",
436 (unsigned long long)btrfs_super_log_root_level(sb
));
437 printf("total_bytes\t\t%llu\n",
438 (unsigned long long)btrfs_super_total_bytes(sb
));
439 printf("bytes_used\t\t%llu\n",
440 (unsigned long long)btrfs_super_bytes_used(sb
));
441 printf("sectorsize\t\t%llu\n",
442 (unsigned long long)btrfs_super_sectorsize(sb
));
443 printf("nodesize\t\t%llu\n",
444 (unsigned long long)btrfs_super_nodesize(sb
));
445 printf("leafsize\t\t%llu\n",
446 (unsigned long long)btrfs_super_leafsize(sb
));
447 printf("stripesize\t\t%llu\n",
448 (unsigned long long)btrfs_super_stripesize(sb
));
449 printf("root_dir\t\t%llu\n",
450 (unsigned long long)btrfs_super_root_dir(sb
));
451 printf("num_devices\t\t%llu\n",
452 (unsigned long long)btrfs_super_num_devices(sb
));
453 printf("compat_flags\t\t0x%llx\n",
454 (unsigned long long)btrfs_super_compat_flags(sb
));
455 printf("compat_ro_flags\t\t0x%llx\n",
456 (unsigned long long)btrfs_super_compat_ro_flags(sb
));
457 printf("incompat_flags\t\t0x%llx\n",
458 (unsigned long long)btrfs_super_incompat_flags(sb
));
459 print_readable_incompat_flag(btrfs_super_incompat_flags(sb
));
460 printf("csum_type\t\t%llu\n",
461 (unsigned long long)btrfs_super_csum_type(sb
));
462 printf("csum_size\t\t%llu\n",
463 (unsigned long long)btrfs_super_csum_size(sb
));
464 printf("cache_generation\t%llu\n",
465 (unsigned long long)btrfs_super_cache_generation(sb
));
466 printf("uuid_tree_generation\t%llu\n",
467 (unsigned long long)btrfs_super_uuid_tree_generation(sb
));
469 uuid_unparse(sb
->dev_item
.uuid
, buf
);
470 printf("dev_item.uuid\t\t%s\n", buf
);
472 uuid_unparse(sb
->dev_item
.fsid
, buf
);
473 printf("dev_item.fsid\t\t%s %s\n", buf
,
474 !memcmp(sb
->dev_item
.fsid
, sb
->fsid
, BTRFS_FSID_SIZE
) ?
475 "[match]" : "[DON'T MATCH]");
477 printf("dev_item.type\t\t%llu\n", (unsigned long long)
478 btrfs_stack_device_type(&sb
->dev_item
));
479 printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
480 btrfs_stack_device_total_bytes(&sb
->dev_item
));
481 printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
482 btrfs_stack_device_bytes_used(&sb
->dev_item
));
483 printf("dev_item.io_align\t%u\n", (unsigned int)
484 btrfs_stack_device_io_align(&sb
->dev_item
));
485 printf("dev_item.io_width\t%u\n", (unsigned int)
486 btrfs_stack_device_io_width(&sb
->dev_item
));
487 printf("dev_item.sector_size\t%u\n", (unsigned int)
488 btrfs_stack_device_sector_size(&sb
->dev_item
));
489 printf("dev_item.devid\t\t%llu\n",
490 btrfs_stack_device_id(&sb
->dev_item
));
491 printf("dev_item.dev_group\t%u\n", (unsigned int)
492 btrfs_stack_device_group(&sb
->dev_item
));
493 printf("dev_item.seek_speed\t%u\n", (unsigned int)
494 btrfs_stack_device_seek_speed(&sb
->dev_item
));
495 printf("dev_item.bandwidth\t%u\n", (unsigned int)
496 btrfs_stack_device_bandwidth(&sb
->dev_item
));
497 printf("dev_item.generation\t%llu\n", (unsigned long long)
498 btrfs_stack_device_generation(&sb
->dev_item
));
500 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
);
501 print_sys_chunk_array(sb
);
502 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS
);
503 print_backup_roots(sb
);