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