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"
38 static int check_csum_sblock(void *sb
, int csum_size
)
40 u8 result
[BTRFS_CSUM_SIZE
];
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
;
56 unsigned long sb_array_offset
;
64 buf
= malloc(sizeof(*buf
) + sizeof(*sb
));
66 error("not enough memory");
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
);
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
)
91 btrfs_disk_key_to_cpu(&key
, disk_key
);
94 sb_array_offset
+= len
;
97 printf("\titem %d ", item
);
98 btrfs_print_key(disk_key
);
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
)
111 num_stripes
= btrfs_chunk_num_stripes(buf
, chunk
);
114 "invalid number of stripes %u in sys_array at offset %u",
115 num_stripes
, cur_offset
);
118 len
= btrfs_chunk_item_size(num_stripes
);
119 if (cur_offset
+ len
> array_size
)
121 print_chunk_item(buf
, chunk
);
123 error("unexpected item type %u in sys_array at offset %u",
124 (u32
)key
.type
, cur_offset
);
128 sb_array_offset
+= len
;
139 error("sys_array too short to read %u bytes at offset %u",
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))
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
));
189 static void print_backup_roots(struct btrfs_super_block
*sb
)
191 struct btrfs_root_backup
*backup
;
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
{
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
)
263 struct readable_flag_entry
*entry
;
269 for (i
= 0; i
< array_size
; i
++) {
271 if (flag
& entry
->bit
) {
273 printf("%s ", entry
->output
);
275 printf("|\n\t\t\t %s ", entry
->output
);
279 flag
&= ~supported_flags
;
282 printf("unknown flag: 0x%llx ", flag
);
284 printf("|\n\t\t\t unknown flag: 0x%llx ", flag
);
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
,
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
,
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
)
318 char *s
, buf
[BTRFS_UUID_UNPARSED_SIZE
];
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
)) {
329 if (csum_type
== BTRFS_CSUM_TYPE_CRC32
) {
331 csum_size
= btrfs_csum_sizes
[csum_type
];
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
))
348 printf(" [DON'T MATCH]");
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");
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");
371 for (i
= 0; i
< BTRFS_LABEL_SIZE
&& s
[i
]; i
++)
372 putchar(isprint(s
[i
]) ? s
[i
] : '.');
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
));
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
,
464 u8 super_block_data
[BTRFS_SUPER_INFO_SIZE
];
465 struct btrfs_super_block
*sb
;
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)
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
);
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
);
487 dump_superblock(sb
, full
);
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",
510 int cmd_inspect_dump_super(int argc
, char **argv
)
520 u64 sb_bytenr
= btrfs_sb_offset(0);
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' },
534 c
= getopt_long(argc
, argv
, "fFai:s:", long_options
, NULL
);
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
);
548 sb_bytenr
= btrfs_sb_offset(arg
);
561 arg
= arg_strtou64(optarg
);
562 if (BTRFS_SUPER_MIRROR_MAX
<= arg
) {
564 "deprecated use of -s <bytenr> with %llu, assuming --bytenr",
565 (unsigned long long)arg
);
568 sb_bytenr
= btrfs_sb_offset(arg
);
572 case GETOPT_VAL_BYTENR
:
573 arg
= arg_strtou64(optarg
);
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
++) {
587 fd
= open(filename
, O_RDONLY
);
589 error("cannot open %s: %m", filename
);
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
)) {
609 load_and_dump_sb(filename
, fd
, sb_bytenr
, full
, force
);