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