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"
38 static int check_csum_sblock(void *sb
, int csum_size
)
40 char result
[BTRFS_CSUM_SIZE
];
43 crc
= btrfs_csum_data(NULL
, (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 array_size
= btrfs_super_sys_array_size(sb
);
72 array_ptr
= sb
->sys_chunk_array
;
73 sb_array_offset
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
77 while (cur_offset
< array_size
) {
78 disk_key
= (struct btrfs_disk_key
*)array_ptr
;
79 len
= sizeof(*disk_key
);
80 if (cur_offset
+ len
> array_size
)
83 btrfs_disk_key_to_cpu(&key
, disk_key
);
86 sb_array_offset
+= len
;
89 printf("\titem %d ", item
);
90 btrfs_print_key(disk_key
);
93 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
94 chunk
= (struct btrfs_chunk
*)sb_array_offset
;
96 * At least one btrfs_chunk with one stripe must be
97 * present, exact stripe count check comes afterwards
99 len
= btrfs_chunk_item_size(1);
100 if (cur_offset
+ len
> array_size
)
103 print_chunk(buf
, chunk
);
104 num_stripes
= btrfs_chunk_num_stripes(buf
, chunk
);
107 "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
108 num_stripes
, cur_offset
);
111 len
= btrfs_chunk_item_size(num_stripes
);
112 if (cur_offset
+ len
> array_size
)
116 "ERROR: unexpected item type %u in sys_array at offset %u\n",
117 (u32
)key
.type
, cur_offset
);
121 sb_array_offset
+= len
;
132 printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
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_INCOMPAT_FLAG_ENTRY(bit_name) \
202 {BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
204 static struct readable_flag_entry incompat_flags_array
[] = {
205 DEF_INCOMPAT_FLAG_ENTRY(MIXED_BACKREF
),
206 DEF_INCOMPAT_FLAG_ENTRY(DEFAULT_SUBVOL
),
207 DEF_INCOMPAT_FLAG_ENTRY(MIXED_GROUPS
),
208 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZO
),
209 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZOv2
),
210 DEF_INCOMPAT_FLAG_ENTRY(BIG_METADATA
),
211 DEF_INCOMPAT_FLAG_ENTRY(EXTENDED_IREF
),
212 DEF_INCOMPAT_FLAG_ENTRY(RAID56
),
213 DEF_INCOMPAT_FLAG_ENTRY(SKINNY_METADATA
),
214 DEF_INCOMPAT_FLAG_ENTRY(NO_HOLES
)
216 static const int incompat_flags_num
= sizeof(incompat_flags_array
) /
217 sizeof(struct readable_flag_entry
);
219 #define DEF_HEADER_FLAG_ENTRY(bit_name) \
220 {BTRFS_HEADER_FLAG_##bit_name, #bit_name}
221 #define DEF_SUPER_FLAG_ENTRY(bit_name) \
222 {BTRFS_SUPER_FLAG_##bit_name, #bit_name}
224 static struct readable_flag_entry super_flags_array
[] = {
225 DEF_HEADER_FLAG_ENTRY(WRITTEN
),
226 DEF_HEADER_FLAG_ENTRY(RELOC
),
227 DEF_SUPER_FLAG_ENTRY(CHANGING_FSID
),
228 DEF_SUPER_FLAG_ENTRY(SEEDING
),
229 DEF_SUPER_FLAG_ENTRY(METADUMP
),
230 DEF_SUPER_FLAG_ENTRY(METADUMP_V2
)
232 static const int super_flags_num
= ARRAY_SIZE(super_flags_array
);
234 #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
235 BTRFS_HEADER_FLAG_RELOC |\
236 BTRFS_SUPER_FLAG_CHANGING_FSID |\
237 BTRFS_SUPER_FLAG_SEEDING |\
238 BTRFS_SUPER_FLAG_METADUMP |\
239 BTRFS_SUPER_FLAG_METADUMP_V2)
241 static void __print_readable_flag(u64 flag
, struct readable_flag_entry
*array
,
242 int array_size
, u64 supported_flags
)
246 struct readable_flag_entry
*entry
;
252 for (i
= 0; i
< array_size
; i
++) {
254 if (flag
& entry
->bit
) {
256 printf("%s ", entry
->output
);
258 printf("|\n\t\t\t %s ", entry
->output
);
262 flag
&= ~supported_flags
;
265 printf("unknown flag: 0x%llx ", flag
);
267 printf("|\n\t\t\t unknown flag: 0x%llx ", flag
);
272 static void print_readable_incompat_flag(u64 flag
)
274 return __print_readable_flag(flag
, incompat_flags_array
,
276 BTRFS_FEATURE_INCOMPAT_SUPP
);
279 static void print_readable_super_flag(u64 flag
)
281 return __print_readable_flag(flag
, super_flags_array
,
282 super_flags_num
, BTRFS_SUPER_FLAG_SUPP
);
285 static void dump_superblock(struct btrfs_super_block
*sb
, int full
)
288 char *s
, buf
[BTRFS_UUID_UNPARSED_SIZE
];
291 printf("csum\t\t\t0x");
292 for (i
= 0, p
= sb
->csum
; i
< btrfs_super_csum_size(sb
); i
++)
293 printf("%02x", p
[i
]);
294 if (check_csum_sblock(sb
, btrfs_super_csum_size(sb
)))
297 printf(" [DON'T MATCH]");
300 printf("bytenr\t\t\t%llu\n",
301 (unsigned long long)btrfs_super_bytenr(sb
));
302 printf("flags\t\t\t0x%llx\n",
303 (unsigned long long)btrfs_super_flags(sb
));
304 print_readable_super_flag(btrfs_super_flags(sb
));
306 printf("magic\t\t\t");
307 s
= (char *) &sb
->magic
;
308 for (i
= 0; i
< 8; i
++)
309 putchar(isprint(s
[i
]) ? s
[i
] : '.');
310 if (btrfs_super_magic(sb
) == BTRFS_MAGIC
)
311 printf(" [match]\n");
313 printf(" [DON'T MATCH]\n");
315 uuid_unparse(sb
->fsid
, buf
);
316 printf("fsid\t\t\t%s\n", buf
);
318 printf("label\t\t\t");
320 for (i
= 0; i
< BTRFS_LABEL_SIZE
&& s
[i
]; i
++)
321 putchar(isprint(s
[i
]) ? s
[i
] : '.');
324 printf("generation\t\t%llu\n",
325 (unsigned long long)btrfs_super_generation(sb
));
326 printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb
));
327 printf("sys_array_size\t\t%llu\n",
328 (unsigned long long)btrfs_super_sys_array_size(sb
));
329 printf("chunk_root_generation\t%llu\n",
330 (unsigned long long)btrfs_super_chunk_root_generation(sb
));
331 printf("root_level\t\t%llu\n",
332 (unsigned long long)btrfs_super_root_level(sb
));
333 printf("chunk_root\t\t%llu\n",
334 (unsigned long long)btrfs_super_chunk_root(sb
));
335 printf("chunk_root_level\t%llu\n",
336 (unsigned long long)btrfs_super_chunk_root_level(sb
));
337 printf("log_root\t\t%llu\n",
338 (unsigned long long)btrfs_super_log_root(sb
));
339 printf("log_root_transid\t%llu\n",
340 (unsigned long long)btrfs_super_log_root_transid(sb
));
341 printf("log_root_level\t\t%llu\n",
342 (unsigned long long)btrfs_super_log_root_level(sb
));
343 printf("total_bytes\t\t%llu\n",
344 (unsigned long long)btrfs_super_total_bytes(sb
));
345 printf("bytes_used\t\t%llu\n",
346 (unsigned long long)btrfs_super_bytes_used(sb
));
347 printf("sectorsize\t\t%llu\n",
348 (unsigned long long)btrfs_super_sectorsize(sb
));
349 printf("nodesize\t\t%llu\n",
350 (unsigned long long)btrfs_super_nodesize(sb
));
351 printf("leafsize\t\t%llu\n",
352 (unsigned long long)btrfs_super_leafsize(sb
));
353 printf("stripesize\t\t%llu\n",
354 (unsigned long long)btrfs_super_stripesize(sb
));
355 printf("root_dir\t\t%llu\n",
356 (unsigned long long)btrfs_super_root_dir(sb
));
357 printf("num_devices\t\t%llu\n",
358 (unsigned long long)btrfs_super_num_devices(sb
));
359 printf("compat_flags\t\t0x%llx\n",
360 (unsigned long long)btrfs_super_compat_flags(sb
));
361 printf("compat_ro_flags\t\t0x%llx\n",
362 (unsigned long long)btrfs_super_compat_ro_flags(sb
));
363 printf("incompat_flags\t\t0x%llx\n",
364 (unsigned long long)btrfs_super_incompat_flags(sb
));
365 print_readable_incompat_flag(btrfs_super_incompat_flags(sb
));
366 printf("csum_type\t\t%llu\n",
367 (unsigned long long)btrfs_super_csum_type(sb
));
368 printf("csum_size\t\t%llu\n",
369 (unsigned long long)btrfs_super_csum_size(sb
));
370 printf("cache_generation\t%llu\n",
371 (unsigned long long)btrfs_super_cache_generation(sb
));
372 printf("uuid_tree_generation\t%llu\n",
373 (unsigned long long)btrfs_super_uuid_tree_generation(sb
));
375 uuid_unparse(sb
->dev_item
.uuid
, buf
);
376 printf("dev_item.uuid\t\t%s\n", buf
);
378 uuid_unparse(sb
->dev_item
.fsid
, buf
);
379 printf("dev_item.fsid\t\t%s %s\n", buf
,
380 !memcmp(sb
->dev_item
.fsid
, sb
->fsid
, BTRFS_FSID_SIZE
) ?
381 "[match]" : "[DON'T MATCH]");
383 printf("dev_item.type\t\t%llu\n", (unsigned long long)
384 btrfs_stack_device_type(&sb
->dev_item
));
385 printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
386 btrfs_stack_device_total_bytes(&sb
->dev_item
));
387 printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
388 btrfs_stack_device_bytes_used(&sb
->dev_item
));
389 printf("dev_item.io_align\t%u\n", (unsigned int)
390 btrfs_stack_device_io_align(&sb
->dev_item
));
391 printf("dev_item.io_width\t%u\n", (unsigned int)
392 btrfs_stack_device_io_width(&sb
->dev_item
));
393 printf("dev_item.sector_size\t%u\n", (unsigned int)
394 btrfs_stack_device_sector_size(&sb
->dev_item
));
395 printf("dev_item.devid\t\t%llu\n",
396 btrfs_stack_device_id(&sb
->dev_item
));
397 printf("dev_item.dev_group\t%u\n", (unsigned int)
398 btrfs_stack_device_group(&sb
->dev_item
));
399 printf("dev_item.seek_speed\t%u\n", (unsigned int)
400 btrfs_stack_device_seek_speed(&sb
->dev_item
));
401 printf("dev_item.bandwidth\t%u\n", (unsigned int)
402 btrfs_stack_device_bandwidth(&sb
->dev_item
));
403 printf("dev_item.generation\t%llu\n", (unsigned long long)
404 btrfs_stack_device_generation(&sb
->dev_item
));
406 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
);
407 print_sys_chunk_array(sb
);
408 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS
);
409 print_backup_roots(sb
);
413 static int load_and_dump_sb(char *filename
, int fd
, u64 sb_bytenr
, int full
,
416 u8 super_block_data
[BTRFS_SUPER_INFO_SIZE
];
417 struct btrfs_super_block
*sb
;
420 sb
= (struct btrfs_super_block
*)super_block_data
;
422 ret
= pread64(fd
, super_block_data
, BTRFS_SUPER_INFO_SIZE
, sb_bytenr
);
423 if (ret
!= BTRFS_SUPER_INFO_SIZE
) {
424 /* check if the disk if too short for further superblock */
425 if (ret
== 0 && errno
== 0)
428 error("failed to read the superblock on %s at %llu",
429 filename
, (unsigned long long)sb_bytenr
);
430 error("error = '%s', errno = %d", strerror(errno
), errno
);
433 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr
, filename
);
434 printf("---------------------------------------------------------\n");
435 if (btrfs_super_magic(sb
) != BTRFS_MAGIC
&& !force
) {
436 error("bad magic on superblock on %s at %llu",
437 filename
, (unsigned long long)sb_bytenr
);
439 dump_superblock(sb
, full
);
444 const char * const cmd_inspect_dump_super_usage
[] = {
445 "btrfs inspect-internal dump-super [options] device [device...]",
446 "Dump superblock from a device in a textual form",
447 "-f|--full print full superblock information",
448 "-a|--all print information about all superblocks",
449 "-i <super_mirror> specify which mirror to print out",
450 "-F|--force attempt to dump superblocks with bad magic",
451 "-s <bytenr> specify alternate superblock offset",
455 int cmd_inspect_dump_super(int argc
, char **argv
)
465 u64 sb_bytenr
= btrfs_sb_offset(0);
469 static const struct option long_options
[] = {
470 {"all", no_argument
, NULL
, 'a'},
471 {"full", no_argument
, NULL
, 'f'},
472 {"force", no_argument
, NULL
, 'F'},
476 c
= getopt_long(argc
, argv
, "fFai:s:", long_options
, NULL
);
482 arg
= arg_strtou64(optarg
);
483 if (arg
>= BTRFS_SUPER_MIRROR_MAX
) {
484 error("super mirror too big: %llu >= %d",
485 arg
, BTRFS_SUPER_MIRROR_MAX
);
486 usage(cmd_inspect_dump_super_usage
);
488 sb_bytenr
= btrfs_sb_offset(arg
);
501 sb_bytenr
= arg_strtou64(optarg
);
505 usage(cmd_inspect_dump_super_usage
);
509 if (check_argc_min(argc
- optind
, 1))
510 usage(cmd_inspect_dump_super_usage
);
512 for (i
= optind
; i
< argc
; i
++) {
514 fd
= open(filename
, O_RDONLY
, 0666);
516 error("cannot open %s: %s", filename
, strerror(errno
));
524 for (idx
= 0; idx
< BTRFS_SUPER_MIRROR_MAX
; idx
++) {
525 sb_bytenr
= btrfs_sb_offset(idx
);
526 if (load_and_dump_sb(filename
, fd
,
527 sb_bytenr
, full
, force
)) {
536 load_and_dump_sb(filename
, fd
, sb_bytenr
, full
, force
);