c2ad332b0481a4acd5a5c7e54af0d4d0aecea712
[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 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <ctype.h>
27 #include <uuid/uuid.h>
28 #include <errno.h>
29
30 #include "kerncompat.h"
31 #include "ctree.h"
32 #include "disk-io.h"
33 #include "print-tree.h"
34 #include "transaction.h"
35 #include "list.h"
36 #include "version.h"
37 #include "utils.h"
38 #include "crc32c.h"
39
40 static void print_usage(void);
41 static void dump_superblock(struct btrfs_super_block *sb, int full);
42 int main(int argc, char **argv);
43 static int load_and_dump_sb(char *, int fd, u64 sb_bytenr, int full);
44
45
46 static void print_usage(void)
47 {
48         fprintf(stderr,
49                 "usage: btrfs-show-super [-i super_mirror|-a|-f] dev [dev..]\n");
50         fprintf(stderr, "\t-f : print full superblock information\n");
51         fprintf(stderr, "\t-a : print information of all superblocks\n");
52         fprintf(stderr, "\t-i <super_mirror> : specify which mirror to print out\n");
53         fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
54 }
55
56 int main(int argc, char **argv)
57 {
58         int opt;
59         int all = 0;
60         int full = 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, "fai:")) != -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                 default:
88                         print_usage();
89                         exit(1);
90                 }
91         }
92
93         if (argc < optind + 1) {
94                 print_usage();
95                 exit(1);
96         }
97
98         for (i = optind; i < argc; i++) {
99                 filename = argv[i];
100                 fd = open(filename, O_RDONLY, 0666);
101                 if (fd < 0) {
102                         fprintf(stderr, "Could not open %s\n", filename);
103                         exit(1);
104                 }
105
106                 if (all) {
107                         int idx;
108                         for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
109                                 sb_bytenr = btrfs_sb_offset(idx);
110                                 if (load_and_dump_sb(filename, fd,
111                                                         sb_bytenr, full)) {
112                                         close(fd);
113                                         exit(1);
114                                 }
115
116                                 putchar('\n');
117                         }
118                 } else {
119                         load_and_dump_sb(filename, fd, sb_bytenr, full);
120                         putchar('\n');
121                 }
122                 close(fd);
123         }
124
125         exit(0);
126 }
127
128 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full)
129 {
130         u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
131         struct btrfs_super_block *sb;
132         u64 ret;
133
134         sb = (struct btrfs_super_block *)super_block_data;
135
136         ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
137         if (ret != BTRFS_SUPER_INFO_SIZE) {
138                 int e = errno;
139
140                 /* check if the disk if too short for further superblock */
141                 if (ret == 0 && e == 0)
142                         return 0;
143
144                 fprintf(stderr,
145                    "ERROR: Failed to read the superblock on %s at %llu\n",
146                    filename, (unsigned long long)sb_bytenr);
147                 fprintf(stderr,
148                    "ERROR: error = '%s', errno = %d\n", strerror(e), e);
149                 return 1;
150         }
151         printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
152         printf("---------------------------------------------------------\n");
153         dump_superblock(sb, full);
154         return 0;
155 }
156
157 static int check_csum_sblock(void *sb, int csum_size)
158 {
159         char result[BTRFS_CSUM_SIZE];
160         u32 crc = ~(u32)0;
161
162         crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE,
163                                 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
164         btrfs_csum_final(crc, result);
165
166         return !memcmp(sb, &result, csum_size);
167 }
168
169 static void print_sys_chunk_array(struct btrfs_super_block *sb)
170 {
171         struct extent_buffer *buf;
172         struct btrfs_disk_key *disk_key;
173         struct btrfs_chunk *chunk;
174         struct btrfs_key key;
175         u8 *ptr, *array_end;
176         u32 num_stripes;
177         u32 len = 0;
178         int i = 0;
179
180         buf = malloc(sizeof(*buf) + sizeof(*sb));
181         if (!buf) {
182                 fprintf(stderr, "%s\n", strerror(ENOMEM));
183                 exit(1);
184         }
185         write_extent_buffer(buf, sb, 0, sizeof(*sb));
186         ptr = sb->sys_chunk_array;
187         array_end = ptr + btrfs_super_sys_array_size(sb);
188
189         while (ptr < array_end) {
190                 disk_key = (struct btrfs_disk_key *)ptr;
191                 btrfs_disk_key_to_cpu(&key, disk_key);
192
193                 printf("\titem %d ", i);
194                 btrfs_print_key(disk_key);
195
196                 len = sizeof(*disk_key);
197                 putchar('\n');
198                 ptr += len;
199
200                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
201                         chunk = (struct btrfs_chunk *)(ptr - (u8 *)sb);
202                         print_chunk(buf, chunk);
203                         num_stripes = btrfs_chunk_num_stripes(buf, chunk);
204                         len = btrfs_chunk_item_size(num_stripes);
205                 } else {
206                         BUG();
207                 }
208
209                 ptr += len;
210                 i++;
211         }
212
213         free(buf);
214 }
215
216 static int empty_backup(struct btrfs_root_backup *backup)
217 {
218         if (backup == NULL ||
219                 (backup->tree_root == 0 &&
220                  backup->tree_root_gen == 0))
221                 return 1;
222         return 0;
223 }
224
225 static void print_root_backup(struct btrfs_root_backup *backup)
226 {
227         printf("\t\tbackup_tree_root:\t%llu\tgen: %llu\tlevel: %d\n",
228                         btrfs_backup_tree_root(backup),
229                         btrfs_backup_tree_root_gen(backup),
230                         btrfs_backup_tree_root_level(backup));
231         printf("\t\tbackup_chunk_root:\t%llu\tgen: %llu\tlevel: %d\n",
232                         btrfs_backup_chunk_root(backup),
233                         btrfs_backup_chunk_root_gen(backup),
234                         btrfs_backup_chunk_root_level(backup));
235         printf("\t\tbackup_extent_root:\t%llu\tgen: %llu\tlevel: %d\n",
236                         btrfs_backup_extent_root(backup),
237                         btrfs_backup_extent_root_gen(backup),
238                         btrfs_backup_extent_root_level(backup));
239         printf("\t\tbackup_fs_root:\t\t%llu\tgen: %llu\tlevel: %d\n",
240                         btrfs_backup_fs_root(backup),
241                         btrfs_backup_fs_root_gen(backup),
242                         btrfs_backup_fs_root_level(backup));
243         printf("\t\tbackup_dev_root:\t%llu\tgen: %llu\tlevel: %d\n",
244                         btrfs_backup_dev_root(backup),
245                         btrfs_backup_dev_root_gen(backup),
246                         btrfs_backup_dev_root_level(backup));
247         printf("\t\tbackup_csum_root:\t%llu\tgen: %llu\tlevel: %d\n",
248                         btrfs_backup_csum_root(backup),
249                         btrfs_backup_csum_root_gen(backup),
250                         btrfs_backup_csum_root_level(backup));
251
252         printf("\t\tbackup_total_bytes:\t%llu\n",
253                                         btrfs_backup_total_bytes(backup));
254         printf("\t\tbackup_bytes_used:\t%llu\n",
255                                         btrfs_backup_bytes_used(backup));
256         printf("\t\tbackup_num_devices:\t%llu\n",
257                                         btrfs_backup_num_devices(backup));
258         putchar('\n');
259 }
260
261 static void print_backup_roots(struct btrfs_super_block *sb)
262 {
263         struct btrfs_root_backup *backup;
264         int i;
265
266         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
267                 backup = sb->super_roots + i;
268                 if (!empty_backup(backup)) {
269                         printf("\tbackup %d:\n", i);
270                         print_root_backup(backup);
271                 }
272         }
273 }
274
275 static void dump_superblock(struct btrfs_super_block *sb, int full)
276 {
277         int i;
278         char *s, buf[BTRFS_UUID_UNPARSED_SIZE];
279         u8 *p;
280
281         printf("csum\t\t\t0x");
282         for (i = 0, p = sb->csum; i < btrfs_super_csum_size(sb); i++)
283                 printf("%02x", p[i]);
284         if (check_csum_sblock(sb, btrfs_super_csum_size(sb)))
285                 printf(" [match]");
286         else
287                 printf(" [DON'T MATCH]");
288         putchar('\n');
289
290         printf("bytenr\t\t\t%llu\n",
291                 (unsigned long long)btrfs_super_bytenr(sb));
292         printf("flags\t\t\t0x%llx\n",
293                 (unsigned long long)btrfs_super_flags(sb));
294
295         printf("magic\t\t\t");
296         s = (char *) &sb->magic;
297         for (i = 0; i < 8; i++)
298                 putchar(isprint(s[i]) ? s[i] : '.');
299         if (btrfs_super_magic(sb) == BTRFS_MAGIC)
300                 printf(" [match]\n");
301         else
302                 printf(" [DON'T MATCH]\n");
303
304         uuid_unparse(sb->fsid, buf);
305         printf("fsid\t\t\t%s\n", buf);
306
307         printf("label\t\t\t");
308         s = sb->label;
309         for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
310                 putchar(isprint(s[i]) ? s[i] : '.');
311         putchar('\n');
312
313         printf("generation\t\t%llu\n",
314                (unsigned long long)btrfs_super_generation(sb));
315         printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
316         printf("sys_array_size\t\t%llu\n",
317                (unsigned long long)btrfs_super_sys_array_size(sb));
318         printf("chunk_root_generation\t%llu\n",
319                (unsigned long long)btrfs_super_chunk_root_generation(sb));
320         printf("root_level\t\t%llu\n",
321                (unsigned long long)btrfs_super_root_level(sb));
322         printf("chunk_root\t\t%llu\n",
323                (unsigned long long)btrfs_super_chunk_root(sb));
324         printf("chunk_root_level\t%llu\n",
325                (unsigned long long)btrfs_super_chunk_root_level(sb));
326         printf("log_root\t\t%llu\n",
327                (unsigned long long)btrfs_super_log_root(sb));
328         printf("log_root_transid\t%llu\n",
329                (unsigned long long)btrfs_super_log_root_transid(sb));
330         printf("log_root_level\t\t%llu\n",
331                (unsigned long long)btrfs_super_log_root_level(sb));
332         printf("total_bytes\t\t%llu\n",
333                (unsigned long long)btrfs_super_total_bytes(sb));
334         printf("bytes_used\t\t%llu\n",
335                (unsigned long long)btrfs_super_bytes_used(sb));
336         printf("sectorsize\t\t%llu\n",
337                (unsigned long long)btrfs_super_sectorsize(sb));
338         printf("nodesize\t\t%llu\n",
339                (unsigned long long)btrfs_super_nodesize(sb));
340         printf("leafsize\t\t%llu\n",
341                (unsigned long long)btrfs_super_leafsize(sb));
342         printf("stripesize\t\t%llu\n",
343                (unsigned long long)btrfs_super_stripesize(sb));
344         printf("root_dir\t\t%llu\n",
345                (unsigned long long)btrfs_super_root_dir(sb));
346         printf("num_devices\t\t%llu\n",
347                (unsigned long long)btrfs_super_num_devices(sb));
348         printf("compat_flags\t\t0x%llx\n",
349                (unsigned long long)btrfs_super_compat_flags(sb));
350         printf("compat_ro_flags\t\t0x%llx\n",
351                (unsigned long long)btrfs_super_compat_ro_flags(sb));
352         printf("incompat_flags\t\t0x%llx\n",
353                (unsigned long long)btrfs_super_incompat_flags(sb));
354         printf("csum_type\t\t%llu\n",
355                (unsigned long long)btrfs_super_csum_type(sb));
356         printf("csum_size\t\t%llu\n",
357                (unsigned long long)btrfs_super_csum_size(sb));
358         printf("cache_generation\t%llu\n",
359                (unsigned long long)btrfs_super_cache_generation(sb));
360         printf("uuid_tree_generation\t%llu\n",
361                (unsigned long long)btrfs_super_uuid_tree_generation(sb));
362
363         uuid_unparse(sb->dev_item.uuid, buf);
364         printf("dev_item.uuid\t\t%s\n", buf);
365
366         uuid_unparse(sb->dev_item.fsid, buf);
367         printf("dev_item.fsid\t\t%s %s\n", buf,
368                 !memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
369                         "[match]" : "[DON'T MATCH]");
370
371         printf("dev_item.type\t\t%llu\n", (unsigned long long)
372                btrfs_stack_device_type(&sb->dev_item));
373         printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
374                btrfs_stack_device_total_bytes(&sb->dev_item));
375         printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
376                btrfs_stack_device_bytes_used(&sb->dev_item));
377         printf("dev_item.io_align\t%u\n", (unsigned int)
378                btrfs_stack_device_io_align(&sb->dev_item));
379         printf("dev_item.io_width\t%u\n", (unsigned int)
380                btrfs_stack_device_io_width(&sb->dev_item));
381         printf("dev_item.sector_size\t%u\n", (unsigned int)
382                btrfs_stack_device_sector_size(&sb->dev_item));
383         printf("dev_item.devid\t\t%llu\n",
384                btrfs_stack_device_id(&sb->dev_item));
385         printf("dev_item.dev_group\t%u\n", (unsigned int)
386                btrfs_stack_device_group(&sb->dev_item));
387         printf("dev_item.seek_speed\t%u\n", (unsigned int)
388                btrfs_stack_device_seek_speed(&sb->dev_item));
389         printf("dev_item.bandwidth\t%u\n", (unsigned int)
390                btrfs_stack_device_bandwidth(&sb->dev_item));
391         printf("dev_item.generation\t%llu\n", (unsigned long long)
392                btrfs_stack_device_generation(&sb->dev_item));
393         if (full) {
394                 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
395                 print_sys_chunk_array(sb);
396                 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS);
397                 print_backup_roots(sb);
398         }
399 }