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