forked from kdave/btrfs-progs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds-inspect-dump-super.c
558 lines (498 loc) · 15.7 KB
/
cmds-inspect-dump-super.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include "kerncompat.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <ctype.h>
#include <uuid/uuid.h>
#include <errno.h>
#include <getopt.h>
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
#include "transaction.h"
#include "list.h"
#include "utils.h"
#include "commands.h"
#include "crc32c.h"
#include "cmds-inspect-dump-super.h"
static int check_csum_sblock(void *sb, int csum_size)
{
char result[BTRFS_CSUM_SIZE];
u32 crc = ~(u32)0;
crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE,
crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
btrfs_csum_final(crc, result);
return !memcmp(sb, &result, csum_size);
}
static void print_sys_chunk_array(struct btrfs_super_block *sb)
{
struct extent_buffer *buf;
struct btrfs_disk_key *disk_key;
struct btrfs_chunk *chunk;
u8 *array_ptr;
unsigned long sb_array_offset;
u32 num_stripes;
u32 array_size;
u32 len = 0;
u32 cur_offset;
struct btrfs_key key;
int item;
buf = malloc(sizeof(*buf) + sizeof(*sb));
if (!buf) {
error("not enough memory");
goto out;
}
write_extent_buffer(buf, sb, 0, sizeof(*sb));
array_size = btrfs_super_sys_array_size(sb);
array_ptr = sb->sys_chunk_array;
sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
cur_offset = 0;
item = 0;
while (cur_offset < array_size) {
disk_key = (struct btrfs_disk_key *)array_ptr;
len = sizeof(*disk_key);
if (cur_offset + len > array_size)
goto out_short_read;
btrfs_disk_key_to_cpu(&key, disk_key);
array_ptr += len;
sb_array_offset += len;
cur_offset += len;
printf("\titem %d ", item);
btrfs_print_key(disk_key);
putchar('\n');
if (key.type == BTRFS_CHUNK_ITEM_KEY) {
chunk = (struct btrfs_chunk *)sb_array_offset;
/*
* At least one btrfs_chunk with one stripe must be
* present, exact stripe count check comes afterwards
*/
len = btrfs_chunk_item_size(1);
if (cur_offset + len > array_size)
goto out_short_read;
print_chunk(buf, chunk);
num_stripes = btrfs_chunk_num_stripes(buf, chunk);
if (!num_stripes) {
printk(
"ERROR: invalid number of stripes %u in sys_array at offset %u\n",
num_stripes, cur_offset);
break;
}
len = btrfs_chunk_item_size(num_stripes);
if (cur_offset + len > array_size)
goto out_short_read;
} else {
printk(
"ERROR: unexpected item type %u in sys_array at offset %u\n",
(u32)key.type, cur_offset);
break;
}
array_ptr += len;
sb_array_offset += len;
cur_offset += len;
item++;
}
free(buf);
out:
return;
out_short_read:
printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
len, cur_offset);
free(buf);
}
static int empty_backup(struct btrfs_root_backup *backup)
{
if (backup == NULL ||
(backup->tree_root == 0 &&
backup->tree_root_gen == 0))
return 1;
return 0;
}
static void print_root_backup(struct btrfs_root_backup *backup)
{
printf("\t\tbackup_tree_root:\t%llu\tgen: %llu\tlevel: %d\n",
btrfs_backup_tree_root(backup),
btrfs_backup_tree_root_gen(backup),
btrfs_backup_tree_root_level(backup));
printf("\t\tbackup_chunk_root:\t%llu\tgen: %llu\tlevel: %d\n",
btrfs_backup_chunk_root(backup),
btrfs_backup_chunk_root_gen(backup),
btrfs_backup_chunk_root_level(backup));
printf("\t\tbackup_extent_root:\t%llu\tgen: %llu\tlevel: %d\n",
btrfs_backup_extent_root(backup),
btrfs_backup_extent_root_gen(backup),
btrfs_backup_extent_root_level(backup));
printf("\t\tbackup_fs_root:\t\t%llu\tgen: %llu\tlevel: %d\n",
btrfs_backup_fs_root(backup),
btrfs_backup_fs_root_gen(backup),
btrfs_backup_fs_root_level(backup));
printf("\t\tbackup_dev_root:\t%llu\tgen: %llu\tlevel: %d\n",
btrfs_backup_dev_root(backup),
btrfs_backup_dev_root_gen(backup),
btrfs_backup_dev_root_level(backup));
printf("\t\tbackup_csum_root:\t%llu\tgen: %llu\tlevel: %d\n",
btrfs_backup_csum_root(backup),
btrfs_backup_csum_root_gen(backup),
btrfs_backup_csum_root_level(backup));
printf("\t\tbackup_total_bytes:\t%llu\n",
btrfs_backup_total_bytes(backup));
printf("\t\tbackup_bytes_used:\t%llu\n",
btrfs_backup_bytes_used(backup));
printf("\t\tbackup_num_devices:\t%llu\n",
btrfs_backup_num_devices(backup));
putchar('\n');
}
static void print_backup_roots(struct btrfs_super_block *sb)
{
struct btrfs_root_backup *backup;
int i;
for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
backup = sb->super_roots + i;
if (!empty_backup(backup)) {
printf("\tbackup %d:\n", i);
print_root_backup(backup);
}
}
}
struct readable_flag_entry {
u64 bit;
char *output;
};
#define DEF_INCOMPAT_FLAG_ENTRY(bit_name) \
{BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
static struct readable_flag_entry incompat_flags_array[] = {
DEF_INCOMPAT_FLAG_ENTRY(MIXED_BACKREF),
DEF_INCOMPAT_FLAG_ENTRY(DEFAULT_SUBVOL),
DEF_INCOMPAT_FLAG_ENTRY(MIXED_GROUPS),
DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZO),
DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZOv2),
DEF_INCOMPAT_FLAG_ENTRY(BIG_METADATA),
DEF_INCOMPAT_FLAG_ENTRY(EXTENDED_IREF),
DEF_INCOMPAT_FLAG_ENTRY(RAID56),
DEF_INCOMPAT_FLAG_ENTRY(SKINNY_METADATA),
DEF_INCOMPAT_FLAG_ENTRY(NO_HOLES)
};
static const int incompat_flags_num = sizeof(incompat_flags_array) /
sizeof(struct readable_flag_entry);
#define DEF_HEADER_FLAG_ENTRY(bit_name) \
{BTRFS_HEADER_FLAG_##bit_name, #bit_name}
#define DEF_SUPER_FLAG_ENTRY(bit_name) \
{BTRFS_SUPER_FLAG_##bit_name, #bit_name}
static struct readable_flag_entry super_flags_array[] = {
DEF_HEADER_FLAG_ENTRY(WRITTEN),
DEF_HEADER_FLAG_ENTRY(RELOC),
DEF_SUPER_FLAG_ENTRY(CHANGING_FSID),
DEF_SUPER_FLAG_ENTRY(SEEDING),
DEF_SUPER_FLAG_ENTRY(METADUMP),
DEF_SUPER_FLAG_ENTRY(METADUMP_V2)
};
static const int super_flags_num = ARRAY_SIZE(super_flags_array);
#define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
BTRFS_HEADER_FLAG_RELOC |\
BTRFS_SUPER_FLAG_CHANGING_FSID |\
BTRFS_SUPER_FLAG_SEEDING |\
BTRFS_SUPER_FLAG_METADUMP |\
BTRFS_SUPER_FLAG_METADUMP_V2)
static void __print_readable_flag(u64 flag, struct readable_flag_entry *array,
int array_size, u64 supported_flags)
{
int i;
int first = 1;
struct readable_flag_entry *entry;
if (!flag)
return;
printf("\t\t\t( ");
for (i = 0; i < array_size; i++) {
entry = array + i;
if (flag & entry->bit) {
if (first)
printf("%s ", entry->output);
else
printf("|\n\t\t\t %s ", entry->output);
first = 0;
}
}
flag &= ~supported_flags;
if (flag) {
if (first)
printf("unknown flag: 0x%llx ", flag);
else
printf("|\n\t\t\t unknown flag: 0x%llx ", flag);
}
printf(")\n");
}
static void print_readable_incompat_flag(u64 flag)
{
return __print_readable_flag(flag, incompat_flags_array,
incompat_flags_num,
BTRFS_FEATURE_INCOMPAT_SUPP);
}
static void print_readable_super_flag(u64 flag)
{
return __print_readable_flag(flag, super_flags_array,
super_flags_num, BTRFS_SUPER_FLAG_SUPP);
}
static void dump_superblock(struct btrfs_super_block *sb, int full)
{
int i;
char *s, buf[BTRFS_UUID_UNPARSED_SIZE];
u8 *p;
u32 csum_size;
u16 csum_type;
csum_type = btrfs_super_csum_type(sb);
csum_size = BTRFS_CSUM_SIZE;
printf("csum_type\t\t%hu (", csum_type);
if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
printf("INVALID");
} else {
if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
printf("crc32c");
csum_size = btrfs_csum_sizes[csum_type];
} else {
printf("unknown");
}
}
printf(")\n");
printf("csum_size\t\t%llu\n", (unsigned long long)csum_size);
printf("csum\t\t\t0x");
for (i = 0, p = sb->csum; i < csum_size; i++)
printf("%02x", p[i]);
if (check_csum_sblock(sb, csum_size))
printf(" [match]");
else
printf(" [DON'T MATCH]");
putchar('\n');
printf("bytenr\t\t\t%llu\n",
(unsigned long long)btrfs_super_bytenr(sb));
printf("flags\t\t\t0x%llx\n",
(unsigned long long)btrfs_super_flags(sb));
print_readable_super_flag(btrfs_super_flags(sb));
printf("magic\t\t\t");
s = (char *) &sb->magic;
for (i = 0; i < 8; i++)
putchar(isprint(s[i]) ? s[i] : '.');
if (btrfs_super_magic(sb) == BTRFS_MAGIC)
printf(" [match]\n");
else
printf(" [DON'T MATCH]\n");
uuid_unparse(sb->fsid, buf);
printf("fsid\t\t\t%s\n", buf);
printf("label\t\t\t");
s = sb->label;
for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
putchar(isprint(s[i]) ? s[i] : '.');
putchar('\n');
printf("generation\t\t%llu\n",
(unsigned long long)btrfs_super_generation(sb));
printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
printf("sys_array_size\t\t%llu\n",
(unsigned long long)btrfs_super_sys_array_size(sb));
printf("chunk_root_generation\t%llu\n",
(unsigned long long)btrfs_super_chunk_root_generation(sb));
printf("root_level\t\t%llu\n",
(unsigned long long)btrfs_super_root_level(sb));
printf("chunk_root\t\t%llu\n",
(unsigned long long)btrfs_super_chunk_root(sb));
printf("chunk_root_level\t%llu\n",
(unsigned long long)btrfs_super_chunk_root_level(sb));
printf("log_root\t\t%llu\n",
(unsigned long long)btrfs_super_log_root(sb));
printf("log_root_transid\t%llu\n",
(unsigned long long)btrfs_super_log_root_transid(sb));
printf("log_root_level\t\t%llu\n",
(unsigned long long)btrfs_super_log_root_level(sb));
printf("total_bytes\t\t%llu\n",
(unsigned long long)btrfs_super_total_bytes(sb));
printf("bytes_used\t\t%llu\n",
(unsigned long long)btrfs_super_bytes_used(sb));
printf("sectorsize\t\t%llu\n",
(unsigned long long)btrfs_super_sectorsize(sb));
printf("nodesize\t\t%llu\n",
(unsigned long long)btrfs_super_nodesize(sb));
printf("leafsize\t\t%llu\n",
(unsigned long long)btrfs_super_leafsize(sb));
printf("stripesize\t\t%llu\n",
(unsigned long long)btrfs_super_stripesize(sb));
printf("root_dir\t\t%llu\n",
(unsigned long long)btrfs_super_root_dir(sb));
printf("num_devices\t\t%llu\n",
(unsigned long long)btrfs_super_num_devices(sb));
printf("compat_flags\t\t0x%llx\n",
(unsigned long long)btrfs_super_compat_flags(sb));
printf("compat_ro_flags\t\t0x%llx\n",
(unsigned long long)btrfs_super_compat_ro_flags(sb));
printf("incompat_flags\t\t0x%llx\n",
(unsigned long long)btrfs_super_incompat_flags(sb));
print_readable_incompat_flag(btrfs_super_incompat_flags(sb));
printf("cache_generation\t%llu\n",
(unsigned long long)btrfs_super_cache_generation(sb));
printf("uuid_tree_generation\t%llu\n",
(unsigned long long)btrfs_super_uuid_tree_generation(sb));
uuid_unparse(sb->dev_item.uuid, buf);
printf("dev_item.uuid\t\t%s\n", buf);
uuid_unparse(sb->dev_item.fsid, buf);
printf("dev_item.fsid\t\t%s %s\n", buf,
!memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
"[match]" : "[DON'T MATCH]");
printf("dev_item.type\t\t%llu\n", (unsigned long long)
btrfs_stack_device_type(&sb->dev_item));
printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
btrfs_stack_device_total_bytes(&sb->dev_item));
printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
btrfs_stack_device_bytes_used(&sb->dev_item));
printf("dev_item.io_align\t%u\n", (unsigned int)
btrfs_stack_device_io_align(&sb->dev_item));
printf("dev_item.io_width\t%u\n", (unsigned int)
btrfs_stack_device_io_width(&sb->dev_item));
printf("dev_item.sector_size\t%u\n", (unsigned int)
btrfs_stack_device_sector_size(&sb->dev_item));
printf("dev_item.devid\t\t%llu\n",
btrfs_stack_device_id(&sb->dev_item));
printf("dev_item.dev_group\t%u\n", (unsigned int)
btrfs_stack_device_group(&sb->dev_item));
printf("dev_item.seek_speed\t%u\n", (unsigned int)
btrfs_stack_device_seek_speed(&sb->dev_item));
printf("dev_item.bandwidth\t%u\n", (unsigned int)
btrfs_stack_device_bandwidth(&sb->dev_item));
printf("dev_item.generation\t%llu\n", (unsigned long long)
btrfs_stack_device_generation(&sb->dev_item));
if (full) {
printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
print_sys_chunk_array(sb);
printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS);
print_backup_roots(sb);
}
}
static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
int force)
{
u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
struct btrfs_super_block *sb;
u64 ret;
sb = (struct btrfs_super_block *)super_block_data;
ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
if (ret != BTRFS_SUPER_INFO_SIZE) {
/* check if the disk if too short for further superblock */
if (ret == 0 && errno == 0)
return 0;
error("failed to read the superblock on %s at %llu",
filename, (unsigned long long)sb_bytenr);
error("error = '%s', errno = %d", strerror(errno), errno);
return 1;
}
printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
printf("---------------------------------------------------------\n");
if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
error("bad magic on superblock on %s at %llu",
filename, (unsigned long long)sb_bytenr);
} else {
dump_superblock(sb, full);
}
return 0;
}
const char * const cmd_inspect_dump_super_usage[] = {
"btrfs inspect-internal dump-super [options] device [device...]",
"Dump superblock from a device in a textual form",
"-f|--full print full superblock information",
"-a|--all print information about all superblocks",
"-i <super_mirror> specify which mirror to print out",
"-F|--force attempt to dump superblocks with bad magic",
"-s <bytenr> specify alternate superblock offset",
NULL
};
int cmd_inspect_dump_super(int argc, char **argv)
{
int all = 0;
int full = 0;
int force = 0;
char *filename;
int fd = -1;
int i;
int ret = 0;
u64 arg;
u64 sb_bytenr = btrfs_sb_offset(0);
while (1) {
int c;
static const struct option long_options[] = {
{"all", no_argument, NULL, 'a'},
{"full", no_argument, NULL, 'f'},
{"force", no_argument, NULL, 'F'},
{NULL, 0, NULL, 0}
};
c = getopt_long(argc, argv, "fFai:s:", long_options, NULL);
if (c < 0)
break;
switch (c) {
case 'i':
arg = arg_strtou64(optarg);
if (arg >= BTRFS_SUPER_MIRROR_MAX) {
error("super mirror too big: %llu >= %d",
arg, BTRFS_SUPER_MIRROR_MAX);
usage(cmd_inspect_dump_super_usage);
}
sb_bytenr = btrfs_sb_offset(arg);
break;
case 'a':
all = 1;
break;
case 'f':
full = 1;
break;
case 'F':
force = 1;
break;
case 's':
sb_bytenr = arg_strtou64(optarg);
all = 0;
break;
default:
usage(cmd_inspect_dump_super_usage);
}
}
if (check_argc_min(argc - optind, 1))
usage(cmd_inspect_dump_super_usage);
for (i = optind; i < argc; i++) {
filename = argv[i];
fd = open(filename, O_RDONLY);
if (fd < 0) {
error("cannot open %s: %s", filename, strerror(errno));
ret = 1;
goto out;
}
if (all) {
int idx;
for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
sb_bytenr = btrfs_sb_offset(idx);
if (load_and_dump_sb(filename, fd,
sb_bytenr, full, force)) {
close(fd);
ret = 1;
goto out;
}
putchar('\n');
}
} else {
load_and_dump_sb(filename, fd, sb_bytenr, full, force);
putchar('\n');
}
close(fd);
}
out:
return ret;
}