btrfs-progs: fix qgroup realloc inheritance
[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);
42 int main(int argc, char **argv);
43 static int load_and_dump_sb(char *, int fd, u64 sb_bytenr);
44
45
46 static void print_usage(void)
47 {
48         fprintf(stderr,
49                 "usage: btrfs-show-super [-i super_mirror|-a] dev [dev..]\n");
50         fprintf(stderr, "\tThe super_mirror number is between 0 and %d.\n",
51                 BTRFS_SUPER_MIRROR_MAX - 1);
52         fprintf(stderr, "\tIf -a is passed all the superblocks are showed.\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         char *filename;
61         int fd = -1;
62         int arg, i;
63         u64 sb_bytenr = btrfs_sb_offset(0);
64
65         while ((opt = getopt(argc, argv, "ai:")) != -1) {
66                 switch (opt) {
67                 case 'i':
68                         arg = atoi(optarg);
69
70                         if (arg < 0 || arg >= BTRFS_SUPER_MIRROR_MAX) {
71                                 fprintf(stderr,
72                                         "Illegal super_mirror %d\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
84                 default:
85                         print_usage();
86                         exit(1);
87                 }
88         }
89
90         if (argc < optind + 1) {
91                 print_usage();
92                 exit(1);
93         }
94
95         for (i = optind; i < argc; i++) {
96                 filename = argv[i];
97                 fd = open(filename, O_RDONLY, 0666);
98                 if (fd < 0) {
99                         fprintf(stderr, "Could not open %s\n", filename);
100                         exit(1);
101                 }
102
103                 if (all) {
104                         int idx;
105                         for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
106                                 sb_bytenr = btrfs_sb_offset(idx);
107                                 if (load_and_dump_sb(filename, fd, sb_bytenr)) {
108                                         close(fd);
109                                         exit(1);
110                                 }
111
112                                 putchar('\n');
113                         }
114                 } else {
115                         load_and_dump_sb(filename, fd, sb_bytenr);
116                         putchar('\n');
117                 }
118                 close(fd);
119         }
120
121         exit(0);
122 }
123
124 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr)
125 {
126         u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
127         struct btrfs_super_block *sb;
128         u64 ret;
129
130         sb = (struct btrfs_super_block *)super_block_data;
131
132         ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
133         if (ret != BTRFS_SUPER_INFO_SIZE) {
134                 int e = errno;
135
136                 /* check if the disk if too short for further superblock */
137                 if (ret == 0 && e == 0)
138                         return 0;
139
140                 fprintf(stderr,
141                    "ERROR: Failed to read the superblock on %s at %llu\n",
142                    filename, (unsigned long long)sb_bytenr);
143                 fprintf(stderr,
144                    "ERROR: error = '%s', errno = %d\n", strerror(e), e);
145                 return 1;
146         }
147         printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
148         printf("---------------------------------------------------------\n");
149         dump_superblock(sb);
150         return 0;
151 }
152
153 static int check_csum_sblock(void *sb, int csum_size)
154 {
155         char result[BTRFS_CSUM_SIZE];
156         u32 crc = ~(u32)0;
157
158         crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE,
159                                 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
160         btrfs_csum_final(crc, result);
161
162         return !memcmp(sb, &result, csum_size);
163 }
164
165 static void dump_superblock(struct btrfs_super_block *sb)
166 {
167         int i;
168         char *s, buf[36+1];
169         u8 *p;
170
171         printf("csum\t\t\t0x");
172         for (i = 0, p = sb->csum; i < btrfs_super_csum_size(sb); i++)
173                 printf("%02x", p[i]);
174         if (check_csum_sblock(sb, btrfs_super_csum_size(sb)))
175                 printf(" [match]");
176         else
177                 printf(" [DON'T MATCH]");
178         putchar('\n');
179
180         printf("bytenr\t\t\t%llu\n",
181                 (unsigned long long)btrfs_super_bytenr(sb));
182         printf("flags\t\t\t0x%llx\n",
183                 (unsigned long long)btrfs_super_flags(sb));
184
185         printf("magic\t\t\t");
186         s = (char *) &sb->magic;
187         for (i = 0; i < 8; i++)
188                 putchar(isprint(s[i]) ? s[i] : '.');
189         if (btrfs_super_magic(sb) == BTRFS_MAGIC)
190                 printf(" [match]\n");
191         else
192                 printf(" [DON'T MATCH]\n");
193
194         uuid_unparse(sb->fsid, buf);
195         printf("fsid\t\t\t%s\n", buf);
196
197         printf("label\t\t\t");
198         s = sb->label;
199         for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
200                 putchar(isprint(s[i]) ? s[i] : '.');
201         putchar('\n');
202
203         printf("generation\t\t%llu\n",
204                (unsigned long long)btrfs_super_generation(sb));
205         printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
206         printf("sys_array_size\t\t%llu\n",
207                (unsigned long long)btrfs_super_sys_array_size(sb));
208         printf("chunk_root_generation\t%llu\n",
209                (unsigned long long)btrfs_super_chunk_root_generation(sb));
210         printf("root_level\t\t%llu\n",
211                (unsigned long long)btrfs_super_root_level(sb));
212         printf("chunk_root\t\t%llu\n",
213                (unsigned long long)btrfs_super_chunk_root(sb));
214         printf("chunk_root_level\t%llu\n",
215                (unsigned long long)btrfs_super_chunk_root_level(sb));
216         printf("log_root\t\t%llu\n",
217                (unsigned long long)btrfs_super_log_root(sb));
218         printf("log_root_transid\t%llu\n",
219                (unsigned long long)btrfs_super_log_root_transid(sb));
220         printf("log_root_level\t\t%llu\n",
221                (unsigned long long)btrfs_super_log_root_level(sb));
222         printf("total_bytes\t\t%llu\n",
223                (unsigned long long)btrfs_super_total_bytes(sb));
224         printf("bytes_used\t\t%llu\n",
225                (unsigned long long)btrfs_super_bytes_used(sb));
226         printf("sectorsize\t\t%llu\n",
227                (unsigned long long)btrfs_super_sectorsize(sb));
228         printf("nodesize\t\t%llu\n",
229                (unsigned long long)btrfs_super_nodesize(sb));
230         printf("leafsize\t\t%llu\n",
231                (unsigned long long)btrfs_super_leafsize(sb));
232         printf("stripesize\t\t%llu\n",
233                (unsigned long long)btrfs_super_stripesize(sb));
234         printf("root_dir\t\t%llu\n",
235                (unsigned long long)btrfs_super_root_dir(sb));
236         printf("num_devices\t\t%llu\n",
237                (unsigned long long)btrfs_super_num_devices(sb));
238         printf("compat_flags\t\t0x%llx\n",
239                (unsigned long long)btrfs_super_compat_flags(sb));
240         printf("compat_ro_flags\t\t0x%llx\n",
241                (unsigned long long)btrfs_super_compat_ro_flags(sb));
242         printf("incompat_flags\t\t0x%llx\n",
243                (unsigned long long)btrfs_super_incompat_flags(sb));
244         printf("csum_type\t\t%llu\n",
245                (unsigned long long)btrfs_super_csum_type(sb));
246         printf("csum_size\t\t%llu\n",
247                (unsigned long long)btrfs_super_csum_size(sb));
248         printf("cache_generation\t%llu\n",
249                (unsigned long long)btrfs_super_cache_generation(sb));
250         printf("uuid_tree_generation\t%llu\n",
251                (unsigned long long)btrfs_super_uuid_tree_generation(sb));
252
253         uuid_unparse(sb->dev_item.uuid, buf);
254         printf("dev_item.uuid\t\t%s\n", buf);
255
256         uuid_unparse(sb->dev_item.fsid, buf);
257         printf("dev_item.fsid\t\t%s %s\n", buf,
258                 !memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
259                         "[match]" : "[DON'T MATCH]");
260
261         printf("dev_item.type\t\t%llu\n", (unsigned long long)
262                btrfs_stack_device_type(&sb->dev_item));
263         printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
264                btrfs_stack_device_total_bytes(&sb->dev_item));
265         printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
266                btrfs_stack_device_bytes_used(&sb->dev_item));
267         printf("dev_item.io_align\t%u\n", (unsigned int)
268                btrfs_stack_device_io_align(&sb->dev_item));
269         printf("dev_item.io_width\t%u\n", (unsigned int)
270                btrfs_stack_device_io_width(&sb->dev_item));
271         printf("dev_item.sector_size\t%u\n", (unsigned int)
272                btrfs_stack_device_sector_size(&sb->dev_item));
273         printf("dev_item.devid\t\t%llu\n",
274                btrfs_stack_device_id(&sb->dev_item));
275         printf("dev_item.dev_group\t%u\n", (unsigned int)
276                btrfs_stack_device_group(&sb->dev_item));
277         printf("dev_item.seek_speed\t%u\n", (unsigned int)
278                btrfs_stack_device_seek_speed(&sb->dev_item));
279         printf("dev_item.bandwidth\t%u\n", (unsigned int)
280                btrfs_stack_device_bandwidth(&sb->dev_item));
281         printf("dev_item.generation\t%llu\n", (unsigned long long)
282                btrfs_stack_device_generation(&sb->dev_item));
283 }