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);
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)) {
116 load_and_dump_sb(filename, fd, sb_bytenr);
125 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr)
127 u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
128 struct btrfs_super_block *sb;
131 sb = (struct btrfs_super_block *)super_block_data;
133 ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
134 if (ret != BTRFS_SUPER_INFO_SIZE) {
137 /* check if the disk if too short for further superblock */
138 if (ret == 0 && e == 0)
142 "ERROR: Failed to read the superblock on %s at %llu\n",
143 filename, (unsigned long long)sb_bytenr);
145 "ERROR: error = '%s', errno = %d\n", strerror(e), e);
148 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
149 printf("---------------------------------------------------------\n");
154 static int check_csum_sblock(void *sb, int csum_size)
156 char result[csum_size];
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);
163 return !memcmp(sb, &result, csum_size);
166 static void dump_superblock(struct btrfs_super_block *sb)
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)))
178 printf(" [DON'T MATCH]");
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));
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");
193 printf(" [DON'T MATCH]\n");
195 uuid_unparse(sb->fsid, buf);
196 printf("fsid\t\t\t%s\n", buf);
198 printf("label\t\t\t");
200 for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
201 putchar(isprint(s[i]) ? s[i] : '.');
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));
252 uuid_unparse(sb->dev_item.uuid, buf);
253 printf("dev_item.uuid\t\t%s\n", buf);
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]");
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));