2 * Copyright (C) 2012 STRATO AG. All rights reserved.
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.
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.
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.
19 #define _XOPEN_SOURCE 500
27 #include <uuid/uuid.h>
30 #include "kerncompat.h"
33 #include "print-tree.h"
34 #include "transaction.h"
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);
46 static void print_usage(void)
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);
56 int main(int argc, char **argv)
63 u64 sb_bytenr = btrfs_sb_offset(0);
65 while ((opt = getopt(argc, argv, "ai:")) != -1) {
70 if (arg < 0 || arg >= BTRFS_SUPER_MIRROR_MAX) {
72 "Illegal super_mirror %d\n",
77 sb_bytenr = btrfs_sb_offset(arg);
90 if (argc < optind + 1) {
95 for (i = optind; i < argc; i++) {
97 fd = open(filename, O_RDONLY, 0666);
99 fprintf(stderr, "Could not open %s\n", filename);
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)) {
115 load_and_dump_sb(filename, fd, sb_bytenr);
124 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr)
126 u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
127 struct btrfs_super_block *sb;
130 sb = (struct btrfs_super_block *)super_block_data;
132 ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
133 if (ret != BTRFS_SUPER_INFO_SIZE) {
136 /* check if the disk if too short for further superblock */
137 if (ret == 0 && e == 0)
141 "ERROR: Failed to read the superblock on %s at %llu\n",
142 filename, (unsigned long long)sb_bytenr);
144 "ERROR: error = '%s', errno = %d\n", strerror(e), e);
147 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
148 printf("---------------------------------------------------------\n");
153 static int check_csum_sblock(void *sb, int csum_size)
155 char result[BTRFS_CSUM_SIZE];
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);
162 return !memcmp(sb, &result, csum_size);
165 static void dump_superblock(struct btrfs_super_block *sb)
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)))
177 printf(" [DON'T MATCH]");
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));
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");
192 printf(" [DON'T MATCH]\n");
194 uuid_unparse(sb->fsid, buf);
195 printf("fsid\t\t\t%s\n", buf);
197 printf("label\t\t\t");
199 for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
200 putchar(isprint(s[i]) ? s[i] : '.');
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));
253 uuid_unparse(sb->dev_item.uuid, buf);
254 printf("dev_item.uuid\t\t%s\n", buf);
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]");
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));