btrfs-progs: dump-super: changes in options to specify superblocks
[platform/upstream/btrfs-progs.git] / cmds-inspect-dump-super.c
1 /*
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.
5  *
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.
10  *
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.
15  */
16
17 #include "kerncompat.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <ctype.h>
24 #include <uuid/uuid.h>
25 #include <errno.h>
26 #include <getopt.h>
27
28 #include "ctree.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "list.h"
33 #include "utils.h"
34 #include "commands.h"
35 #include "crc32c.h"
36 #include "cmds-inspect-dump-super.h"
37
38 static int check_csum_sblock(void *sb, int csum_size)
39 {
40         char result[BTRFS_CSUM_SIZE];
41         u32 crc = ~(u32)0;
42
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);
46
47         return !memcmp(sb, &result, csum_size);
48 }
49
50 static void print_sys_chunk_array(struct btrfs_super_block *sb)
51 {
52         struct extent_buffer *buf;
53         struct btrfs_disk_key *disk_key;
54         struct btrfs_chunk *chunk;
55         u8 *array_ptr;
56         unsigned long sb_array_offset;
57         u32 num_stripes;
58         u32 array_size;
59         u32 len = 0;
60         u32 cur_offset;
61         struct btrfs_key key;
62         int item;
63
64         buf = malloc(sizeof(*buf) + sizeof(*sb));
65         if (!buf) {
66                 error("not enough memory");
67                 goto out;
68         }
69         write_extent_buffer(buf, sb, 0, sizeof(*sb));
70         array_size = btrfs_super_sys_array_size(sb);
71
72         array_ptr = sb->sys_chunk_array;
73         sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
74         cur_offset = 0;
75         item = 0;
76
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)
81                         goto out_short_read;
82
83                 btrfs_disk_key_to_cpu(&key, disk_key);
84
85                 array_ptr += len;
86                 sb_array_offset += len;
87                 cur_offset += len;
88
89                 printf("\titem %d ", item);
90                 btrfs_print_key(disk_key);
91                 putchar('\n');
92
93                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
94                         chunk = (struct btrfs_chunk *)sb_array_offset;
95                         /*
96                          * At least one btrfs_chunk with one stripe must be
97                          * present, exact stripe count check comes afterwards
98                          */
99                         len = btrfs_chunk_item_size(1);
100                         if (cur_offset + len > array_size)
101                                 goto out_short_read;
102
103                         print_chunk(buf, chunk);
104                         num_stripes = btrfs_chunk_num_stripes(buf, chunk);
105                         if (!num_stripes) {
106                                 printk(
107             "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
108                                         num_stripes, cur_offset);
109                                 break;
110                         }
111                         len = btrfs_chunk_item_size(num_stripes);
112                         if (cur_offset + len > array_size)
113                                 goto out_short_read;
114                 } else {
115                         printk(
116                 "ERROR: unexpected item type %u in sys_array at offset %u\n",
117                                 (u32)key.type, cur_offset);
118                         break;
119                 }
120                 array_ptr += len;
121                 sb_array_offset += len;
122                 cur_offset += len;
123
124                 item++;
125         }
126
127         free(buf);
128 out:
129         return;
130
131 out_short_read:
132         printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
133                         len, cur_offset);
134         free(buf);
135 }
136
137 static int empty_backup(struct btrfs_root_backup *backup)
138 {
139         if (backup == NULL ||
140                 (backup->tree_root == 0 &&
141                  backup->tree_root_gen == 0))
142                 return 1;
143         return 0;
144 }
145
146 static void print_root_backup(struct btrfs_root_backup *backup)
147 {
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));
172
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));
179         putchar('\n');
180 }
181
182 static void print_backup_roots(struct btrfs_super_block *sb)
183 {
184         struct btrfs_root_backup *backup;
185         int i;
186
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);
192                 }
193         }
194 }
195
196 struct readable_flag_entry {
197         u64 bit;
198         char *output;
199 };
200
201 #define DEF_INCOMPAT_FLAG_ENTRY(bit_name)               \
202         {BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
203
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)
215 };
216 static const int incompat_flags_num = sizeof(incompat_flags_array) /
217                                       sizeof(struct readable_flag_entry);
218
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}
223
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)
231 };
232 static const int super_flags_num = ARRAY_SIZE(super_flags_array);
233
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)
240
241 static void __print_readable_flag(u64 flag, struct readable_flag_entry *array,
242                                   int array_size, u64 supported_flags)
243 {
244         int i;
245         int first = 1;
246         struct readable_flag_entry *entry;
247
248         if (!flag)
249                 return;
250
251         printf("\t\t\t( ");
252         for (i = 0; i < array_size; i++) {
253                 entry = array + i;
254                 if (flag & entry->bit) {
255                         if (first)
256                                 printf("%s ", entry->output);
257                         else
258                                 printf("|\n\t\t\t  %s ", entry->output);
259                         first = 0;
260                 }
261         }
262         flag &= ~supported_flags;
263         if (flag) {
264                 if (first)
265                         printf("unknown flag: 0x%llx ", flag);
266                 else
267                         printf("|\n\t\t\t  unknown flag: 0x%llx ", flag);
268         }
269         printf(")\n");
270 }
271
272 static void print_readable_incompat_flag(u64 flag)
273 {
274         return __print_readable_flag(flag, incompat_flags_array,
275                                      incompat_flags_num,
276                                      BTRFS_FEATURE_INCOMPAT_SUPP);
277 }
278
279 static void print_readable_super_flag(u64 flag)
280 {
281         return __print_readable_flag(flag, super_flags_array,
282                                      super_flags_num, BTRFS_SUPER_FLAG_SUPP);
283 }
284
285 static void dump_superblock(struct btrfs_super_block *sb, int full)
286 {
287         int i;
288         char *s, buf[BTRFS_UUID_UNPARSED_SIZE];
289         u8 *p;
290         u32 csum_size;
291         u16 csum_type;
292
293         csum_type = btrfs_super_csum_type(sb);
294         csum_size = BTRFS_CSUM_SIZE;
295         printf("csum_type\t\t%hu (", csum_type);
296         if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
297                 printf("INVALID");
298         } else {
299                 if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
300                         printf("crc32c");
301                         csum_size = btrfs_csum_sizes[csum_type];
302                 } else {
303                         printf("unknown");
304                 }
305         }
306         printf(")\n");
307         printf("csum_size\t\t%llu\n", (unsigned long long)csum_size);
308
309         printf("csum\t\t\t0x");
310         for (i = 0, p = sb->csum; i < csum_size; i++)
311                 printf("%02x", p[i]);
312         if (check_csum_sblock(sb, csum_size))
313                 printf(" [match]");
314         else
315                 printf(" [DON'T MATCH]");
316         putchar('\n');
317
318         printf("bytenr\t\t\t%llu\n",
319                 (unsigned long long)btrfs_super_bytenr(sb));
320         printf("flags\t\t\t0x%llx\n",
321                 (unsigned long long)btrfs_super_flags(sb));
322         print_readable_super_flag(btrfs_super_flags(sb));
323
324         printf("magic\t\t\t");
325         s = (char *) &sb->magic;
326         for (i = 0; i < 8; i++)
327                 putchar(isprint(s[i]) ? s[i] : '.');
328         if (btrfs_super_magic(sb) == BTRFS_MAGIC)
329                 printf(" [match]\n");
330         else
331                 printf(" [DON'T MATCH]\n");
332
333         uuid_unparse(sb->fsid, buf);
334         printf("fsid\t\t\t%s\n", buf);
335
336         printf("label\t\t\t");
337         s = sb->label;
338         for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
339                 putchar(isprint(s[i]) ? s[i] : '.');
340         putchar('\n');
341
342         printf("generation\t\t%llu\n",
343                (unsigned long long)btrfs_super_generation(sb));
344         printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
345         printf("sys_array_size\t\t%llu\n",
346                (unsigned long long)btrfs_super_sys_array_size(sb));
347         printf("chunk_root_generation\t%llu\n",
348                (unsigned long long)btrfs_super_chunk_root_generation(sb));
349         printf("root_level\t\t%llu\n",
350                (unsigned long long)btrfs_super_root_level(sb));
351         printf("chunk_root\t\t%llu\n",
352                (unsigned long long)btrfs_super_chunk_root(sb));
353         printf("chunk_root_level\t%llu\n",
354                (unsigned long long)btrfs_super_chunk_root_level(sb));
355         printf("log_root\t\t%llu\n",
356                (unsigned long long)btrfs_super_log_root(sb));
357         printf("log_root_transid\t%llu\n",
358                (unsigned long long)btrfs_super_log_root_transid(sb));
359         printf("log_root_level\t\t%llu\n",
360                (unsigned long long)btrfs_super_log_root_level(sb));
361         printf("total_bytes\t\t%llu\n",
362                (unsigned long long)btrfs_super_total_bytes(sb));
363         printf("bytes_used\t\t%llu\n",
364                (unsigned long long)btrfs_super_bytes_used(sb));
365         printf("sectorsize\t\t%llu\n",
366                (unsigned long long)btrfs_super_sectorsize(sb));
367         printf("nodesize\t\t%llu\n",
368                (unsigned long long)btrfs_super_nodesize(sb));
369         printf("leafsize\t\t%llu\n",
370                (unsigned long long)btrfs_super_leafsize(sb));
371         printf("stripesize\t\t%llu\n",
372                (unsigned long long)btrfs_super_stripesize(sb));
373         printf("root_dir\t\t%llu\n",
374                (unsigned long long)btrfs_super_root_dir(sb));
375         printf("num_devices\t\t%llu\n",
376                (unsigned long long)btrfs_super_num_devices(sb));
377         printf("compat_flags\t\t0x%llx\n",
378                (unsigned long long)btrfs_super_compat_flags(sb));
379         printf("compat_ro_flags\t\t0x%llx\n",
380                (unsigned long long)btrfs_super_compat_ro_flags(sb));
381         printf("incompat_flags\t\t0x%llx\n",
382                (unsigned long long)btrfs_super_incompat_flags(sb));
383         print_readable_incompat_flag(btrfs_super_incompat_flags(sb));
384         printf("cache_generation\t%llu\n",
385                (unsigned long long)btrfs_super_cache_generation(sb));
386         printf("uuid_tree_generation\t%llu\n",
387                (unsigned long long)btrfs_super_uuid_tree_generation(sb));
388
389         uuid_unparse(sb->dev_item.uuid, buf);
390         printf("dev_item.uuid\t\t%s\n", buf);
391
392         uuid_unparse(sb->dev_item.fsid, buf);
393         printf("dev_item.fsid\t\t%s %s\n", buf,
394                 !memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
395                         "[match]" : "[DON'T MATCH]");
396
397         printf("dev_item.type\t\t%llu\n", (unsigned long long)
398                btrfs_stack_device_type(&sb->dev_item));
399         printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
400                btrfs_stack_device_total_bytes(&sb->dev_item));
401         printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
402                btrfs_stack_device_bytes_used(&sb->dev_item));
403         printf("dev_item.io_align\t%u\n", (unsigned int)
404                btrfs_stack_device_io_align(&sb->dev_item));
405         printf("dev_item.io_width\t%u\n", (unsigned int)
406                btrfs_stack_device_io_width(&sb->dev_item));
407         printf("dev_item.sector_size\t%u\n", (unsigned int)
408                btrfs_stack_device_sector_size(&sb->dev_item));
409         printf("dev_item.devid\t\t%llu\n",
410                btrfs_stack_device_id(&sb->dev_item));
411         printf("dev_item.dev_group\t%u\n", (unsigned int)
412                btrfs_stack_device_group(&sb->dev_item));
413         printf("dev_item.seek_speed\t%u\n", (unsigned int)
414                btrfs_stack_device_seek_speed(&sb->dev_item));
415         printf("dev_item.bandwidth\t%u\n", (unsigned int)
416                btrfs_stack_device_bandwidth(&sb->dev_item));
417         printf("dev_item.generation\t%llu\n", (unsigned long long)
418                btrfs_stack_device_generation(&sb->dev_item));
419         if (full) {
420                 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
421                 print_sys_chunk_array(sb);
422                 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS);
423                 print_backup_roots(sb);
424         }
425 }
426
427 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
428                 int force)
429 {
430         u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
431         struct btrfs_super_block *sb;
432         u64 ret;
433
434         sb = (struct btrfs_super_block *)super_block_data;
435
436         ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
437         if (ret != BTRFS_SUPER_INFO_SIZE) {
438                 /* check if the disk if too short for further superblock */
439                 if (ret == 0 && errno == 0)
440                         return 0;
441
442                 error("failed to read the superblock on %s at %llu",
443                                 filename, (unsigned long long)sb_bytenr);
444                 error("error = '%s', errno = %d", strerror(errno), errno);
445                 return 1;
446         }
447         printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
448         printf("---------------------------------------------------------\n");
449         if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
450                 error("bad magic on superblock on %s at %llu",
451                                 filename, (unsigned long long)sb_bytenr);
452         } else {
453                 dump_superblock(sb, full);
454         }
455         return 0;
456 }
457
458 const char * const cmd_inspect_dump_super_usage[] = {
459         "btrfs inspect-internal dump-super [options] device [device...]",
460         "Dump superblock from a device in a textual form",
461         "-f|--full             print full superblock information, backup roots etc.",
462         "-a|--all              print information about all superblocks",
463         "-s|--super <super>    specify which copy to print out (values: 0, 1, 2)",
464         "-F|--force            attempt to dump superblocks with bad magic",
465         "--bytenr <offset>     specify alternate superblock offset",
466         "",
467         "Deprecated syntax:",
468         "-s <bytenr>           specify alternate superblock offset, values other than 0, 1, 2",
469         "                      will be interpreted as --bytenr for backward compatibility,",
470         "                      option renamed for consistency with other tools (eg. check)",
471         "-i <super>            specify which copy to print out (values: 0, 1, 2), now moved",
472         "                      to -s|--super",
473         NULL
474 };
475
476 int cmd_inspect_dump_super(int argc, char **argv)
477 {
478         int all = 0;
479         int full = 0;
480         int force = 0;
481         char *filename;
482         int fd = -1;
483         int i;
484         int ret = 0;
485         u64 arg;
486         u64 sb_bytenr = btrfs_sb_offset(0);
487
488         while (1) {
489                 int c;
490                 static const struct option long_options[] = {
491                         {"all", no_argument, NULL, 'a'},
492                         {"full", no_argument, NULL, 'f'},
493                         {"force", no_argument, NULL, 'F'},
494                         {"super", required_argument, NULL, 's' },
495                         {NULL, 0, NULL, 0}
496                 };
497
498                 c = getopt_long(argc, argv, "fFai:s:", long_options, NULL);
499                 if (c < 0)
500                         break;
501
502                 switch (c) {
503                 case 'i':
504                         warning(
505                             "option -i is deprecated, please use -s or --super");
506                         arg = arg_strtou64(optarg);
507                         if (arg >= BTRFS_SUPER_MIRROR_MAX) {
508                                 error("super mirror too big: %llu >= %d",
509                                         arg, BTRFS_SUPER_MIRROR_MAX);
510                                 return 1;
511                         }
512                         sb_bytenr = btrfs_sb_offset(arg);
513                         break;
514
515                 case 'a':
516                         all = 1;
517                         break;
518                 case 'f':
519                         full = 1;
520                         break;
521                 case 'F':
522                         force = 1;
523                         break;
524                 case 's':
525                         arg = arg_strtou64(optarg);
526                         if (BTRFS_SUPER_MIRROR_MAX <= arg) {
527                                 warning(
528                 "deprecated use of -s <bytenr> with %llu, assuming --bytenr",
529                                                 (unsigned long long)arg);
530                                 sb_bytenr = arg;
531                         } else {
532                                 sb_bytenr = btrfs_sb_offset(arg);
533                         }
534                         all = 0;
535                         break;
536                 default:
537                         usage(cmd_inspect_dump_super_usage);
538                 }
539         }
540
541         if (check_argc_min(argc - optind, 1))
542                 usage(cmd_inspect_dump_super_usage);
543
544         for (i = optind; i < argc; i++) {
545                 filename = argv[i];
546                 fd = open(filename, O_RDONLY);
547                 if (fd < 0) {
548                         error("cannot open %s: %s", filename, strerror(errno));
549                         ret = 1;
550                         goto out;
551                 }
552
553                 if (all) {
554                         int idx;
555
556                         for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
557                                 sb_bytenr = btrfs_sb_offset(idx);
558                                 if (load_and_dump_sb(filename, fd,
559                                                 sb_bytenr, full, force)) {
560                                         close(fd);
561                                         ret = 1;
562                                         goto out;
563                                 }
564
565                                 putchar('\n');
566                         }
567                 } else {
568                         load_and_dump_sb(filename, fd, sb_bytenr, full, force);
569                         putchar('\n');
570                 }
571                 close(fd);
572         }
573
574 out:
575         return ret;
576 }