4afa852761b66950125606bd265877c0dd8f79f9
[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 "version.h"
35 #include "utils.h"
36 #include "crc32c.h"
37
38 static void print_usage(void);
39 static void dump_superblock(struct btrfs_super_block *sb, int full);
40 int main(int argc, char **argv);
41 static int load_and_dump_sb(char *, int fd, u64 sb_bytenr, int full, int force);
42
43
44 static void print_usage(void)
45 {
46         fprintf(stderr,
47                 "usage: btrfs-show-super [-i super_mirror|-a|-f|-F] dev [dev..]\n");
48         fprintf(stderr, "\t-f : print full superblock information\n");
49         fprintf(stderr, "\t-a : print information of all superblocks\n");
50         fprintf(stderr, "\t-i <super_mirror> : specify which mirror to print out\n");
51         fprintf(stderr, "\t-F : attempt to dump superblocks with bad magic\n");
52         fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
53 }
54
55 int main(int argc, char **argv)
56 {
57         int opt;
58         int all = 0;
59         int full = 0;
60         int force = 0;
61         char *filename;
62         int fd = -1;
63         int i;
64         u64 arg;
65         u64 sb_bytenr = btrfs_sb_offset(0);
66
67         while ((opt = getopt(argc, argv, "fFai:")) != -1) {
68                 switch (opt) {
69                 case 'i':
70                         arg = arg_strtou64(optarg);
71                         if (arg >= BTRFS_SUPER_MIRROR_MAX) {
72                                 fprintf(stderr,
73                                         "Illegal super_mirror %llu\n",
74                                         arg);
75                                 print_usage();
76                                 exit(1);
77                         }
78                         sb_bytenr = btrfs_sb_offset(arg);
79                         break;
80
81                 case 'a':
82                         all = 1;
83                         break;
84                 case 'f':
85                         full = 1;
86                         break;
87                 case 'F':
88                         force = 1;
89                         break;
90                 default:
91                         print_usage();
92                         exit(1);
93                 }
94         }
95
96         set_argv0(argv);
97         if (check_argc_min(argc - optind, 1)) {
98                 print_usage();
99                 exit(1);
100         }
101
102         for (i = optind; i < argc; i++) {
103                 filename = argv[i];
104                 fd = open(filename, O_RDONLY, 0666);
105                 if (fd < 0) {
106                         fprintf(stderr, "Could not open %s\n", filename);
107                         exit(1);
108                 }
109
110                 if (all) {
111                         int idx;
112                         for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
113                                 sb_bytenr = btrfs_sb_offset(idx);
114                                 if (load_and_dump_sb(filename, fd,
115                                                 sb_bytenr, full, force)) {
116                                         close(fd);
117                                         exit(1);
118                                 }
119
120                                 putchar('\n');
121                         }
122                 } else {
123                         load_and_dump_sb(filename, fd, sb_bytenr, full, force);
124                         putchar('\n');
125                 }
126                 close(fd);
127         }
128
129         exit(0);
130 }
131
132 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
133                 int force)
134 {
135         u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
136         struct btrfs_super_block *sb;
137         u64 ret;
138
139         sb = (struct btrfs_super_block *)super_block_data;
140
141         ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
142         if (ret != BTRFS_SUPER_INFO_SIZE) {
143                 int e = errno;
144
145                 /* check if the disk if too short for further superblock */
146                 if (ret == 0 && e == 0)
147                         return 0;
148
149                 fprintf(stderr,
150                    "ERROR: Failed to read the superblock on %s at %llu\n",
151                    filename, (unsigned long long)sb_bytenr);
152                 fprintf(stderr,
153                    "ERROR: error = '%s', errno = %d\n", strerror(e), e);
154                 return 1;
155         }
156         printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
157         printf("---------------------------------------------------------\n");
158         if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
159                 fprintf(stderr,
160                     "ERROR: bad magic on superblock on %s at %llu\n",
161                     filename, (unsigned long long)sb_bytenr);
162         } else {
163                 dump_superblock(sb, full);
164         }
165         return 0;
166 }
167
168 static int check_csum_sblock(void *sb, int csum_size)
169 {
170         char result[BTRFS_CSUM_SIZE];
171         u32 crc = ~(u32)0;
172
173         crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE,
174                                 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
175         btrfs_csum_final(crc, result);
176
177         return !memcmp(sb, &result, csum_size);
178 }
179
180 static void print_sys_chunk_array(struct btrfs_super_block *sb)
181 {
182         struct extent_buffer *buf;
183         struct btrfs_disk_key *disk_key;
184         struct btrfs_chunk *chunk;
185         struct btrfs_key key;
186         u8 *ptr, *array_end;
187         u32 num_stripes;
188         u32 len = 0;
189         int i = 0;
190
191         buf = malloc(sizeof(*buf) + sizeof(*sb));
192         if (!buf) {
193                 fprintf(stderr, "%s\n", strerror(ENOMEM));
194                 exit(1);
195         }
196         write_extent_buffer(buf, sb, 0, sizeof(*sb));
197         ptr = sb->sys_chunk_array;
198         array_end = ptr + btrfs_super_sys_array_size(sb);
199
200         while (ptr < array_end) {
201                 disk_key = (struct btrfs_disk_key *)ptr;
202                 btrfs_disk_key_to_cpu(&key, disk_key);
203
204                 printf("\titem %d ", i);
205                 btrfs_print_key(disk_key);
206
207                 len = sizeof(*disk_key);
208                 putchar('\n');
209                 ptr += len;
210
211                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
212                         chunk = (struct btrfs_chunk *)(ptr - (u8 *)sb);
213                         print_chunk(buf, chunk);
214                         num_stripes = btrfs_chunk_num_stripes(buf, chunk);
215                         len = btrfs_chunk_item_size(num_stripes);
216                 } else {
217                         BUG();
218                 }
219
220                 ptr += len;
221                 i++;
222         }
223
224         free(buf);
225 }
226
227 static int empty_backup(struct btrfs_root_backup *backup)
228 {
229         if (backup == NULL ||
230                 (backup->tree_root == 0 &&
231                  backup->tree_root_gen == 0))
232                 return 1;
233         return 0;
234 }
235
236 static void print_root_backup(struct btrfs_root_backup *backup)
237 {
238         printf("\t\tbackup_tree_root:\t%llu\tgen: %llu\tlevel: %d\n",
239                         btrfs_backup_tree_root(backup),
240                         btrfs_backup_tree_root_gen(backup),
241                         btrfs_backup_tree_root_level(backup));
242         printf("\t\tbackup_chunk_root:\t%llu\tgen: %llu\tlevel: %d\n",
243                         btrfs_backup_chunk_root(backup),
244                         btrfs_backup_chunk_root_gen(backup),
245                         btrfs_backup_chunk_root_level(backup));
246         printf("\t\tbackup_extent_root:\t%llu\tgen: %llu\tlevel: %d\n",
247                         btrfs_backup_extent_root(backup),
248                         btrfs_backup_extent_root_gen(backup),
249                         btrfs_backup_extent_root_level(backup));
250         printf("\t\tbackup_fs_root:\t\t%llu\tgen: %llu\tlevel: %d\n",
251                         btrfs_backup_fs_root(backup),
252                         btrfs_backup_fs_root_gen(backup),
253                         btrfs_backup_fs_root_level(backup));
254         printf("\t\tbackup_dev_root:\t%llu\tgen: %llu\tlevel: %d\n",
255                         btrfs_backup_dev_root(backup),
256                         btrfs_backup_dev_root_gen(backup),
257                         btrfs_backup_dev_root_level(backup));
258         printf("\t\tbackup_csum_root:\t%llu\tgen: %llu\tlevel: %d\n",
259                         btrfs_backup_csum_root(backup),
260                         btrfs_backup_csum_root_gen(backup),
261                         btrfs_backup_csum_root_level(backup));
262
263         printf("\t\tbackup_total_bytes:\t%llu\n",
264                                         btrfs_backup_total_bytes(backup));
265         printf("\t\tbackup_bytes_used:\t%llu\n",
266                                         btrfs_backup_bytes_used(backup));
267         printf("\t\tbackup_num_devices:\t%llu\n",
268                                         btrfs_backup_num_devices(backup));
269         putchar('\n');
270 }
271
272 static void print_backup_roots(struct btrfs_super_block *sb)
273 {
274         struct btrfs_root_backup *backup;
275         int i;
276
277         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
278                 backup = sb->super_roots + i;
279                 if (!empty_backup(backup)) {
280                         printf("\tbackup %d:\n", i);
281                         print_root_backup(backup);
282                 }
283         }
284 }
285
286 struct readable_flag_entry {
287         u64 bit;
288         char *output;
289 };
290
291 #define DEF_INCOMPAT_FLAG_ENTRY(bit_name)               \
292         {BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
293
294 struct readable_flag_entry incompat_flags_array[] = {
295         DEF_INCOMPAT_FLAG_ENTRY(MIXED_BACKREF),
296         DEF_INCOMPAT_FLAG_ENTRY(DEFAULT_SUBVOL),
297         DEF_INCOMPAT_FLAG_ENTRY(MIXED_GROUPS),
298         DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZO),
299         DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZOv2),
300         DEF_INCOMPAT_FLAG_ENTRY(BIG_METADATA),
301         DEF_INCOMPAT_FLAG_ENTRY(EXTENDED_IREF),
302         DEF_INCOMPAT_FLAG_ENTRY(RAID56),
303         DEF_INCOMPAT_FLAG_ENTRY(SKINNY_METADATA),
304         DEF_INCOMPAT_FLAG_ENTRY(NO_HOLES)
305 };
306 static const int incompat_flags_num = sizeof(incompat_flags_array) /
307                                       sizeof(struct readable_flag_entry);
308
309 static void print_readable_incompat_flag(u64 flag)
310 {
311         int i;
312         int first = 1;
313         struct readable_flag_entry *entry;
314
315         if (!flag)
316                 return;
317         printf("\t\t\t( ");
318         for (i = 0; i < incompat_flags_num; i++) {
319                 entry = incompat_flags_array + i;
320                 if (flag & entry->bit) {
321                         if (first)
322                                 printf("%s ", entry->output);
323                         else
324                                 printf("|\n\t\t\t  %s ", entry->output);
325                         first = 0;
326                 }
327         }
328         flag &= ~BTRFS_FEATURE_INCOMPAT_SUPP;
329         if (flag) {
330                 if (first)
331                         printf("unknown flag: 0x%llx ", flag);
332                 else
333                         printf("|\n\t\t\t  unknown flag: 0x%llx ", flag);
334         }
335         printf(")\n");
336 }
337
338 static void dump_superblock(struct btrfs_super_block *sb, int full)
339 {
340         int i;
341         char *s, buf[BTRFS_UUID_UNPARSED_SIZE];
342         u8 *p;
343
344         printf("csum\t\t\t0x");
345         for (i = 0, p = sb->csum; i < btrfs_super_csum_size(sb); i++)
346                 printf("%02x", p[i]);
347         if (check_csum_sblock(sb, btrfs_super_csum_size(sb)))
348                 printf(" [match]");
349         else
350                 printf(" [DON'T MATCH]");
351         putchar('\n');
352
353         printf("bytenr\t\t\t%llu\n",
354                 (unsigned long long)btrfs_super_bytenr(sb));
355         printf("flags\t\t\t0x%llx\n",
356                 (unsigned long long)btrfs_super_flags(sb));
357
358         printf("magic\t\t\t");
359         s = (char *) &sb->magic;
360         for (i = 0; i < 8; i++)
361                 putchar(isprint(s[i]) ? s[i] : '.');
362         if (btrfs_super_magic(sb) == BTRFS_MAGIC)
363                 printf(" [match]\n");
364         else
365                 printf(" [DON'T MATCH]\n");
366
367         uuid_unparse(sb->fsid, buf);
368         printf("fsid\t\t\t%s\n", buf);
369
370         printf("label\t\t\t");
371         s = sb->label;
372         for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
373                 putchar(isprint(s[i]) ? s[i] : '.');
374         putchar('\n');
375
376         printf("generation\t\t%llu\n",
377                (unsigned long long)btrfs_super_generation(sb));
378         printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
379         printf("sys_array_size\t\t%llu\n",
380                (unsigned long long)btrfs_super_sys_array_size(sb));
381         printf("chunk_root_generation\t%llu\n",
382                (unsigned long long)btrfs_super_chunk_root_generation(sb));
383         printf("root_level\t\t%llu\n",
384                (unsigned long long)btrfs_super_root_level(sb));
385         printf("chunk_root\t\t%llu\n",
386                (unsigned long long)btrfs_super_chunk_root(sb));
387         printf("chunk_root_level\t%llu\n",
388                (unsigned long long)btrfs_super_chunk_root_level(sb));
389         printf("log_root\t\t%llu\n",
390                (unsigned long long)btrfs_super_log_root(sb));
391         printf("log_root_transid\t%llu\n",
392                (unsigned long long)btrfs_super_log_root_transid(sb));
393         printf("log_root_level\t\t%llu\n",
394                (unsigned long long)btrfs_super_log_root_level(sb));
395         printf("total_bytes\t\t%llu\n",
396                (unsigned long long)btrfs_super_total_bytes(sb));
397         printf("bytes_used\t\t%llu\n",
398                (unsigned long long)btrfs_super_bytes_used(sb));
399         printf("sectorsize\t\t%llu\n",
400                (unsigned long long)btrfs_super_sectorsize(sb));
401         printf("nodesize\t\t%llu\n",
402                (unsigned long long)btrfs_super_nodesize(sb));
403         printf("leafsize\t\t%llu\n",
404                (unsigned long long)btrfs_super_leafsize(sb));
405         printf("stripesize\t\t%llu\n",
406                (unsigned long long)btrfs_super_stripesize(sb));
407         printf("root_dir\t\t%llu\n",
408                (unsigned long long)btrfs_super_root_dir(sb));
409         printf("num_devices\t\t%llu\n",
410                (unsigned long long)btrfs_super_num_devices(sb));
411         printf("compat_flags\t\t0x%llx\n",
412                (unsigned long long)btrfs_super_compat_flags(sb));
413         printf("compat_ro_flags\t\t0x%llx\n",
414                (unsigned long long)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("csum_type\t\t%llu\n",
419                (unsigned long long)btrfs_super_csum_type(sb));
420         printf("csum_size\t\t%llu\n",
421                (unsigned long long)btrfs_super_csum_size(sb));
422         printf("cache_generation\t%llu\n",
423                (unsigned long long)btrfs_super_cache_generation(sb));
424         printf("uuid_tree_generation\t%llu\n",
425                (unsigned long long)btrfs_super_uuid_tree_generation(sb));
426
427         uuid_unparse(sb->dev_item.uuid, buf);
428         printf("dev_item.uuid\t\t%s\n", buf);
429
430         uuid_unparse(sb->dev_item.fsid, buf);
431         printf("dev_item.fsid\t\t%s %s\n", buf,
432                 !memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
433                         "[match]" : "[DON'T MATCH]");
434
435         printf("dev_item.type\t\t%llu\n", (unsigned long long)
436                btrfs_stack_device_type(&sb->dev_item));
437         printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
438                btrfs_stack_device_total_bytes(&sb->dev_item));
439         printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
440                btrfs_stack_device_bytes_used(&sb->dev_item));
441         printf("dev_item.io_align\t%u\n", (unsigned int)
442                btrfs_stack_device_io_align(&sb->dev_item));
443         printf("dev_item.io_width\t%u\n", (unsigned int)
444                btrfs_stack_device_io_width(&sb->dev_item));
445         printf("dev_item.sector_size\t%u\n", (unsigned int)
446                btrfs_stack_device_sector_size(&sb->dev_item));
447         printf("dev_item.devid\t\t%llu\n",
448                btrfs_stack_device_id(&sb->dev_item));
449         printf("dev_item.dev_group\t%u\n", (unsigned int)
450                btrfs_stack_device_group(&sb->dev_item));
451         printf("dev_item.seek_speed\t%u\n", (unsigned int)
452                btrfs_stack_device_seek_speed(&sb->dev_item));
453         printf("dev_item.bandwidth\t%u\n", (unsigned int)
454                btrfs_stack_device_bandwidth(&sb->dev_item));
455         printf("dev_item.generation\t%llu\n", (unsigned long long)
456                btrfs_stack_device_generation(&sb->dev_item));
457         if (full) {
458                 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
459                 print_sys_chunk_array(sb);
460                 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS);
461                 print_backup_roots(sb);
462         }
463 }