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"
24 #include <uuid/uuid.h>
30 #include "print-tree.h"
31 #include "transaction.h"
36 #include "cmds-inspect-dump-super.h"
39 static int check_csum_sblock(void *sb
, int csum_size
)
41 u8 result
[BTRFS_CSUM_SIZE
];
44 crc
= btrfs_csum_data(NULL
, (char *)sb
+ BTRFS_CSUM_SIZE
,
45 crc
, BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
46 btrfs_csum_final(crc
, result
);
48 return !memcmp(sb
, &result
, csum_size
);
51 static void print_sys_chunk_array(struct btrfs_super_block
*sb
)
53 struct extent_buffer
*buf
;
54 struct btrfs_disk_key
*disk_key
;
55 struct btrfs_chunk
*chunk
;
57 unsigned long sb_array_offset
;
65 buf
= malloc(sizeof(*buf
) + sizeof(*sb
));
67 error("not enough memory");
70 write_extent_buffer(buf
, sb
, 0, 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
);
78 while (cur_offset
< array_size
) {
79 disk_key
= (struct btrfs_disk_key
*)array_ptr
;
80 len
= sizeof(*disk_key
);
81 if (cur_offset
+ len
> array_size
)
84 btrfs_disk_key_to_cpu(&key
, disk_key
);
87 sb_array_offset
+= len
;
90 printf("\titem %d ", item
);
91 btrfs_print_key(disk_key
);
94 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
95 chunk
= (struct btrfs_chunk
*)sb_array_offset
;
97 * At least one btrfs_chunk with one stripe must be
98 * present, exact stripe count check comes afterwards
100 len
= btrfs_chunk_item_size(1);
101 if (cur_offset
+ len
> array_size
)
104 num_stripes
= btrfs_chunk_num_stripes(buf
, chunk
);
107 "invalid number of stripes %u in sys_array at offset %u",
108 num_stripes
, cur_offset
);
111 len
= btrfs_chunk_item_size(num_stripes
);
112 if (cur_offset
+ len
> array_size
)
114 print_chunk(buf
, chunk
);
116 error("unexpected item type %u in sys_array at offset %u",
117 (u32
)key
.type
, cur_offset
);
121 sb_array_offset
+= len
;
132 error("sys_array too short to read %u bytes at offset %u",
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))
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
));
182 static void print_backup_roots(struct btrfs_super_block
*sb
)
184 struct btrfs_root_backup
*backup
;
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
{
201 #define DEF_COMPAT_RO_FLAG_ENTRY(bit_name) \
202 {BTRFS_FEATURE_COMPAT_RO_##bit_name, #bit_name}
204 static struct readable_flag_entry compat_ro_flags_array
[] = {
205 DEF_COMPAT_RO_FLAG_ENTRY(FREE_SPACE_TREE
),
206 DEF_COMPAT_RO_FLAG_ENTRY(FREE_SPACE_TREE_VALID
),
208 static const int compat_ro_flags_num
= sizeof(compat_ro_flags_array
) /
209 sizeof(struct readable_flag_entry
);
211 #define DEF_INCOMPAT_FLAG_ENTRY(bit_name) \
212 {BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
214 static struct readable_flag_entry incompat_flags_array
[] = {
215 DEF_INCOMPAT_FLAG_ENTRY(MIXED_BACKREF
),
216 DEF_INCOMPAT_FLAG_ENTRY(DEFAULT_SUBVOL
),
217 DEF_INCOMPAT_FLAG_ENTRY(MIXED_GROUPS
),
218 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZO
),
219 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZOv2
),
220 DEF_INCOMPAT_FLAG_ENTRY(BIG_METADATA
),
221 DEF_INCOMPAT_FLAG_ENTRY(EXTENDED_IREF
),
222 DEF_INCOMPAT_FLAG_ENTRY(RAID56
),
223 DEF_INCOMPAT_FLAG_ENTRY(SKINNY_METADATA
),
224 DEF_INCOMPAT_FLAG_ENTRY(NO_HOLES
)
226 static const int incompat_flags_num
= sizeof(incompat_flags_array
) /
227 sizeof(struct readable_flag_entry
);
229 #define DEF_HEADER_FLAG_ENTRY(bit_name) \
230 {BTRFS_HEADER_FLAG_##bit_name, #bit_name}
231 #define DEF_SUPER_FLAG_ENTRY(bit_name) \
232 {BTRFS_SUPER_FLAG_##bit_name, #bit_name}
234 static struct readable_flag_entry super_flags_array
[] = {
235 DEF_HEADER_FLAG_ENTRY(WRITTEN
),
236 DEF_HEADER_FLAG_ENTRY(RELOC
),
237 DEF_SUPER_FLAG_ENTRY(CHANGING_FSID
),
238 DEF_SUPER_FLAG_ENTRY(SEEDING
),
239 DEF_SUPER_FLAG_ENTRY(METADUMP
),
240 DEF_SUPER_FLAG_ENTRY(METADUMP_V2
)
242 static const int super_flags_num
= ARRAY_SIZE(super_flags_array
);
244 #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
245 BTRFS_HEADER_FLAG_RELOC |\
246 BTRFS_SUPER_FLAG_CHANGING_FSID |\
247 BTRFS_SUPER_FLAG_SEEDING |\
248 BTRFS_SUPER_FLAG_METADUMP |\
249 BTRFS_SUPER_FLAG_METADUMP_V2)
251 static void __print_readable_flag(u64 flag
, struct readable_flag_entry
*array
,
252 int array_size
, u64 supported_flags
)
256 struct readable_flag_entry
*entry
;
262 for (i
= 0; i
< array_size
; i
++) {
264 if (flag
& entry
->bit
) {
266 printf("%s ", entry
->output
);
268 printf("|\n\t\t\t %s ", entry
->output
);
272 flag
&= ~supported_flags
;
275 printf("unknown flag: 0x%llx ", flag
);
277 printf("|\n\t\t\t unknown flag: 0x%llx ", flag
);
282 static void print_readable_compat_ro_flag(u64 flag
)
285 * We know about the FREE_SPACE_TREE{,_VALID} bits, but we don't
286 * actually support them yet.
288 return __print_readable_flag(flag
, compat_ro_flags_array
,
290 BTRFS_FEATURE_COMPAT_RO_SUPP
|
291 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE
|
292 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID
);
295 static void print_readable_incompat_flag(u64 flag
)
297 return __print_readable_flag(flag
, incompat_flags_array
,
299 BTRFS_FEATURE_INCOMPAT_SUPP
);
302 static void print_readable_super_flag(u64 flag
)
304 return __print_readable_flag(flag
, super_flags_array
,
305 super_flags_num
, BTRFS_SUPER_FLAG_SUPP
);
308 static void dump_superblock(struct btrfs_super_block
*sb
, int full
)
311 char *s
, buf
[BTRFS_UUID_UNPARSED_SIZE
];
316 csum_type
= btrfs_super_csum_type(sb
);
317 csum_size
= BTRFS_CSUM_SIZE
;
318 printf("csum_type\t\t%hu (", csum_type
);
319 if (csum_type
>= ARRAY_SIZE(btrfs_csum_sizes
)) {
322 if (csum_type
== BTRFS_CSUM_TYPE_CRC32
) {
324 csum_size
= btrfs_csum_sizes
[csum_type
];
330 printf("csum_size\t\t%llu\n", (unsigned long long)csum_size
);
332 printf("csum\t\t\t0x");
333 for (i
= 0, p
= sb
->csum
; i
< csum_size
; i
++)
334 printf("%02x", p
[i
]);
335 if (check_csum_sblock(sb
, csum_size
))
338 printf(" [DON'T MATCH]");
341 printf("bytenr\t\t\t%llu\n",
342 (unsigned long long)btrfs_super_bytenr(sb
));
343 printf("flags\t\t\t0x%llx\n",
344 (unsigned long long)btrfs_super_flags(sb
));
345 print_readable_super_flag(btrfs_super_flags(sb
));
347 printf("magic\t\t\t");
348 s
= (char *) &sb
->magic
;
349 for (i
= 0; i
< 8; i
++)
350 putchar(isprint(s
[i
]) ? s
[i
] : '.');
351 if (btrfs_super_magic(sb
) == BTRFS_MAGIC
)
352 printf(" [match]\n");
354 printf(" [DON'T MATCH]\n");
356 uuid_unparse(sb
->fsid
, buf
);
357 printf("fsid\t\t\t%s\n", buf
);
359 printf("label\t\t\t");
361 for (i
= 0; i
< BTRFS_LABEL_SIZE
&& s
[i
]; i
++)
362 putchar(isprint(s
[i
]) ? s
[i
] : '.');
365 printf("generation\t\t%llu\n",
366 (unsigned long long)btrfs_super_generation(sb
));
367 printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb
));
368 printf("sys_array_size\t\t%llu\n",
369 (unsigned long long)btrfs_super_sys_array_size(sb
));
370 printf("chunk_root_generation\t%llu\n",
371 (unsigned long long)btrfs_super_chunk_root_generation(sb
));
372 printf("root_level\t\t%llu\n",
373 (unsigned long long)btrfs_super_root_level(sb
));
374 printf("chunk_root\t\t%llu\n",
375 (unsigned long long)btrfs_super_chunk_root(sb
));
376 printf("chunk_root_level\t%llu\n",
377 (unsigned long long)btrfs_super_chunk_root_level(sb
));
378 printf("log_root\t\t%llu\n",
379 (unsigned long long)btrfs_super_log_root(sb
));
380 printf("log_root_transid\t%llu\n",
381 (unsigned long long)btrfs_super_log_root_transid(sb
));
382 printf("log_root_level\t\t%llu\n",
383 (unsigned long long)btrfs_super_log_root_level(sb
));
384 printf("total_bytes\t\t%llu\n",
385 (unsigned long long)btrfs_super_total_bytes(sb
));
386 printf("bytes_used\t\t%llu\n",
387 (unsigned long long)btrfs_super_bytes_used(sb
));
388 printf("sectorsize\t\t%llu\n",
389 (unsigned long long)btrfs_super_sectorsize(sb
));
390 printf("nodesize\t\t%llu\n",
391 (unsigned long long)btrfs_super_nodesize(sb
));
392 printf("leafsize\t\t%llu\n",
393 (unsigned long long)btrfs_super_leafsize(sb
));
394 printf("stripesize\t\t%llu\n",
395 (unsigned long long)btrfs_super_stripesize(sb
));
396 printf("root_dir\t\t%llu\n",
397 (unsigned long long)btrfs_super_root_dir(sb
));
398 printf("num_devices\t\t%llu\n",
399 (unsigned long long)btrfs_super_num_devices(sb
));
400 printf("compat_flags\t\t0x%llx\n",
401 (unsigned long long)btrfs_super_compat_flags(sb
));
402 printf("compat_ro_flags\t\t0x%llx\n",
403 (unsigned long long)btrfs_super_compat_ro_flags(sb
));
404 print_readable_compat_ro_flag(btrfs_super_compat_ro_flags(sb
));
405 printf("incompat_flags\t\t0x%llx\n",
406 (unsigned long long)btrfs_super_incompat_flags(sb
));
407 print_readable_incompat_flag(btrfs_super_incompat_flags(sb
));
408 printf("cache_generation\t%llu\n",
409 (unsigned long long)btrfs_super_cache_generation(sb
));
410 printf("uuid_tree_generation\t%llu\n",
411 (unsigned long long)btrfs_super_uuid_tree_generation(sb
));
413 uuid_unparse(sb
->dev_item
.uuid
, buf
);
414 printf("dev_item.uuid\t\t%s\n", buf
);
416 uuid_unparse(sb
->dev_item
.fsid
, buf
);
417 printf("dev_item.fsid\t\t%s %s\n", buf
,
418 !memcmp(sb
->dev_item
.fsid
, sb
->fsid
, BTRFS_FSID_SIZE
) ?
419 "[match]" : "[DON'T MATCH]");
421 printf("dev_item.type\t\t%llu\n", (unsigned long long)
422 btrfs_stack_device_type(&sb
->dev_item
));
423 printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
424 btrfs_stack_device_total_bytes(&sb
->dev_item
));
425 printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
426 btrfs_stack_device_bytes_used(&sb
->dev_item
));
427 printf("dev_item.io_align\t%u\n", (unsigned int)
428 btrfs_stack_device_io_align(&sb
->dev_item
));
429 printf("dev_item.io_width\t%u\n", (unsigned int)
430 btrfs_stack_device_io_width(&sb
->dev_item
));
431 printf("dev_item.sector_size\t%u\n", (unsigned int)
432 btrfs_stack_device_sector_size(&sb
->dev_item
));
433 printf("dev_item.devid\t\t%llu\n",
434 btrfs_stack_device_id(&sb
->dev_item
));
435 printf("dev_item.dev_group\t%u\n", (unsigned int)
436 btrfs_stack_device_group(&sb
->dev_item
));
437 printf("dev_item.seek_speed\t%u\n", (unsigned int)
438 btrfs_stack_device_seek_speed(&sb
->dev_item
));
439 printf("dev_item.bandwidth\t%u\n", (unsigned int)
440 btrfs_stack_device_bandwidth(&sb
->dev_item
));
441 printf("dev_item.generation\t%llu\n", (unsigned long long)
442 btrfs_stack_device_generation(&sb
->dev_item
));
444 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
);
445 print_sys_chunk_array(sb
);
446 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS
);
447 print_backup_roots(sb
);
451 static int load_and_dump_sb(char *filename
, int fd
, u64 sb_bytenr
, int full
,
454 u8 super_block_data
[BTRFS_SUPER_INFO_SIZE
];
455 struct btrfs_super_block
*sb
;
458 sb
= (struct btrfs_super_block
*)super_block_data
;
460 ret
= pread64(fd
, super_block_data
, BTRFS_SUPER_INFO_SIZE
, sb_bytenr
);
461 if (ret
!= BTRFS_SUPER_INFO_SIZE
) {
462 /* check if the disk if too short for further superblock */
463 if (ret
== 0 && errno
== 0)
466 error("failed to read the superblock on %s at %llu",
467 filename
, (unsigned long long)sb_bytenr
);
468 error("error = '%s', errno = %d", strerror(errno
), errno
);
471 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr
, filename
);
472 printf("---------------------------------------------------------\n");
473 if (btrfs_super_magic(sb
) != BTRFS_MAGIC
&& !force
) {
474 error("bad magic on superblock on %s at %llu",
475 filename
, (unsigned long long)sb_bytenr
);
477 dump_superblock(sb
, full
);
482 const char * const cmd_inspect_dump_super_usage
[] = {
483 "btrfs inspect-internal dump-super [options] device [device...]",
484 "Dump superblock from a device in a textual form",
485 "-f|--full print full superblock information, backup roots etc.",
486 "-a|--all print information about all superblocks",
487 "-s|--super <super> specify which copy to print out (values: 0, 1, 2)",
488 "-F|--force attempt to dump superblocks with bad magic",
489 "--bytenr <offset> specify alternate superblock offset",
491 "Deprecated syntax:",
492 "-s <bytenr> specify alternate superblock offset, values other than 0, 1, 2",
493 " will be interpreted as --bytenr for backward compatibility,",
494 " option renamed for consistency with other tools (eg. check)",
495 "-i <super> specify which copy to print out (values: 0, 1, 2), now moved",
500 int cmd_inspect_dump_super(int argc
, char **argv
)
510 u64 sb_bytenr
= btrfs_sb_offset(0);
514 static const struct option long_options
[] = {
515 {"all", no_argument
, NULL
, 'a'},
516 {"full", no_argument
, NULL
, 'f'},
517 {"force", no_argument
, NULL
, 'F'},
518 {"super", required_argument
, NULL
, 's' },
522 c
= getopt_long(argc
, argv
, "fFai:s:", long_options
, NULL
);
529 "option -i is deprecated, please use -s or --super");
530 arg
= arg_strtou64(optarg
);
531 if (arg
>= BTRFS_SUPER_MIRROR_MAX
) {
532 error("super mirror too big: %llu >= %d",
533 arg
, BTRFS_SUPER_MIRROR_MAX
);
536 sb_bytenr
= btrfs_sb_offset(arg
);
549 arg
= arg_strtou64(optarg
);
550 if (BTRFS_SUPER_MIRROR_MAX
<= arg
) {
552 "deprecated use of -s <bytenr> with %llu, assuming --bytenr",
553 (unsigned long long)arg
);
556 sb_bytenr
= btrfs_sb_offset(arg
);
561 usage(cmd_inspect_dump_super_usage
);
565 if (check_argc_min(argc
- optind
, 1))
566 usage(cmd_inspect_dump_super_usage
);
568 for (i
= optind
; i
< argc
; i
++) {
570 fd
= open(filename
, O_RDONLY
);
572 error("cannot open %s: %s", filename
, strerror(errno
));
580 for (idx
= 0; idx
< BTRFS_SUPER_MIRROR_MAX
; idx
++) {
581 sb_bytenr
= btrfs_sb_offset(idx
);
582 if (load_and_dump_sb(filename
, fd
,
583 sb_bytenr
, full
, force
)) {
592 load_and_dump_sb(filename
, fd
, sb_bytenr
, full
, force
);