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