btrfs-progs: handle memory allocation failure in add_file_items
[btrfs-progs-unstable/devel.git] / btrfs-show-super.c
blobd8ad69e27df912d05c980b2ef46ab1de4c91106d
1 /*
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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <ctype.h>
25 #include <uuid/uuid.h>
26 #include <errno.h>
28 #include "kerncompat.h"
29 #include "ctree.h"
30 #include "disk-io.h"
31 #include "print-tree.h"
32 #include "transaction.h"
33 #include "list.h"
34 #include "utils.h"
35 #include "crc32c.h"
37 static void print_usage(void);
38 static void dump_superblock(struct btrfs_super_block *sb, int full);
39 int main(int argc, char **argv);
40 static int load_and_dump_sb(char *, int fd, u64 sb_bytenr, int full, int force);
43 static void print_usage(void)
45 fprintf(stderr,
46 "usage: btrfs-show-super [-i super_mirror|-a|-f|-F] dev [dev..]\n");
47 fprintf(stderr, "\t-f : print full superblock information\n");
48 fprintf(stderr, "\t-a : print information of all superblocks\n");
49 fprintf(stderr, "\t-i <super_mirror> : specify which mirror to print out\n");
50 fprintf(stderr, "\t-F : attempt to dump superblocks with bad magic\n");
51 fprintf(stderr, "\t-s <bytenr> : specify alternate superblock offset\n");
52 fprintf(stderr, "%s\n", PACKAGE_STRING);
55 int main(int argc, char **argv)
57 int opt;
58 int all = 0;
59 int full = 0;
60 int force = 0;
61 char *filename;
62 int fd = -1;
63 int i;
64 u64 arg;
65 u64 sb_bytenr = btrfs_sb_offset(0);
67 while ((opt = getopt(argc, argv, "fFai:s:")) != -1) {
68 switch (opt) {
69 case 'i':
70 arg = arg_strtou64(optarg);
71 if (arg >= BTRFS_SUPER_MIRROR_MAX) {
72 fprintf(stderr,
73 "Illegal super_mirror %llu\n",
74 arg);
75 print_usage();
76 exit(1);
78 sb_bytenr = btrfs_sb_offset(arg);
79 break;
81 case 'a':
82 all = 1;
83 break;
84 case 'f':
85 full = 1;
86 break;
87 case 'F':
88 force = 1;
89 break;
90 case 's':
91 sb_bytenr = arg_strtou64(optarg);
92 all = 0;
93 break;
94 default:
95 print_usage();
96 exit(1);
100 set_argv0(argv);
101 if (check_argc_min(argc - optind, 1)) {
102 print_usage();
103 exit(1);
106 for (i = optind; i < argc; i++) {
107 filename = argv[i];
108 fd = open(filename, O_RDONLY, 0666);
109 if (fd < 0) {
110 fprintf(stderr, "Could not open %s\n", filename);
111 exit(1);
114 if (all) {
115 int idx;
116 for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
117 sb_bytenr = btrfs_sb_offset(idx);
118 if (load_and_dump_sb(filename, fd,
119 sb_bytenr, full, force)) {
120 close(fd);
121 exit(1);
124 putchar('\n');
126 } else {
127 load_and_dump_sb(filename, fd, sb_bytenr, full, force);
128 putchar('\n');
130 close(fd);
133 exit(0);
136 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
137 int force)
139 u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
140 struct btrfs_super_block *sb;
141 u64 ret;
143 sb = (struct btrfs_super_block *)super_block_data;
145 ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
146 if (ret != BTRFS_SUPER_INFO_SIZE) {
147 int e = errno;
149 /* check if the disk if too short for further superblock */
150 if (ret == 0 && e == 0)
151 return 0;
153 fprintf(stderr,
154 "ERROR: Failed to read the superblock on %s at %llu\n",
155 filename, (unsigned long long)sb_bytenr);
156 fprintf(stderr,
157 "ERROR: error = '%s', errno = %d\n", strerror(e), e);
158 return 1;
160 printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
161 printf("---------------------------------------------------------\n");
162 if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
163 fprintf(stderr,
164 "ERROR: bad magic on superblock on %s at %llu\n",
165 filename, (unsigned long long)sb_bytenr);
166 } else {
167 dump_superblock(sb, full);
169 return 0;
172 static int check_csum_sblock(void *sb, int csum_size)
174 char result[BTRFS_CSUM_SIZE];
175 u32 crc = ~(u32)0;
177 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE,
178 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
179 btrfs_csum_final(crc, result);
181 return !memcmp(sb, &result, csum_size);
184 static void print_sys_chunk_array(struct btrfs_super_block *sb)
186 struct extent_buffer *buf;
187 struct btrfs_disk_key *disk_key;
188 struct btrfs_chunk *chunk;
189 struct btrfs_key key;
190 u8 *ptr, *array_end;
191 u32 num_stripes;
192 u32 len = 0;
193 int i = 0;
195 buf = malloc(sizeof(*buf) + sizeof(*sb));
196 if (!buf) {
197 fprintf(stderr, "%s\n", strerror(ENOMEM));
198 exit(1);
200 write_extent_buffer(buf, sb, 0, sizeof(*sb));
201 ptr = sb->sys_chunk_array;
202 array_end = ptr + btrfs_super_sys_array_size(sb);
204 while (ptr < array_end) {
205 disk_key = (struct btrfs_disk_key *)ptr;
206 btrfs_disk_key_to_cpu(&key, disk_key);
208 printf("\titem %d ", i);
209 btrfs_print_key(disk_key);
211 len = sizeof(*disk_key);
212 putchar('\n');
213 ptr += len;
215 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
216 chunk = (struct btrfs_chunk *)(ptr - (u8 *)sb);
217 print_chunk(buf, chunk);
218 num_stripes = btrfs_chunk_num_stripes(buf, chunk);
219 len = btrfs_chunk_item_size(num_stripes);
220 } else {
221 BUG();
224 ptr += len;
225 i++;
228 free(buf);
231 static int empty_backup(struct btrfs_root_backup *backup)
233 if (backup == NULL ||
234 (backup->tree_root == 0 &&
235 backup->tree_root_gen == 0))
236 return 1;
237 return 0;
240 static void print_root_backup(struct btrfs_root_backup *backup)
242 printf("\t\tbackup_tree_root:\t%llu\tgen: %llu\tlevel: %d\n",
243 btrfs_backup_tree_root(backup),
244 btrfs_backup_tree_root_gen(backup),
245 btrfs_backup_tree_root_level(backup));
246 printf("\t\tbackup_chunk_root:\t%llu\tgen: %llu\tlevel: %d\n",
247 btrfs_backup_chunk_root(backup),
248 btrfs_backup_chunk_root_gen(backup),
249 btrfs_backup_chunk_root_level(backup));
250 printf("\t\tbackup_extent_root:\t%llu\tgen: %llu\tlevel: %d\n",
251 btrfs_backup_extent_root(backup),
252 btrfs_backup_extent_root_gen(backup),
253 btrfs_backup_extent_root_level(backup));
254 printf("\t\tbackup_fs_root:\t\t%llu\tgen: %llu\tlevel: %d\n",
255 btrfs_backup_fs_root(backup),
256 btrfs_backup_fs_root_gen(backup),
257 btrfs_backup_fs_root_level(backup));
258 printf("\t\tbackup_dev_root:\t%llu\tgen: %llu\tlevel: %d\n",
259 btrfs_backup_dev_root(backup),
260 btrfs_backup_dev_root_gen(backup),
261 btrfs_backup_dev_root_level(backup));
262 printf("\t\tbackup_csum_root:\t%llu\tgen: %llu\tlevel: %d\n",
263 btrfs_backup_csum_root(backup),
264 btrfs_backup_csum_root_gen(backup),
265 btrfs_backup_csum_root_level(backup));
267 printf("\t\tbackup_total_bytes:\t%llu\n",
268 btrfs_backup_total_bytes(backup));
269 printf("\t\tbackup_bytes_used:\t%llu\n",
270 btrfs_backup_bytes_used(backup));
271 printf("\t\tbackup_num_devices:\t%llu\n",
272 btrfs_backup_num_devices(backup));
273 putchar('\n');
276 static void print_backup_roots(struct btrfs_super_block *sb)
278 struct btrfs_root_backup *backup;
279 int i;
281 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
282 backup = sb->super_roots + i;
283 if (!empty_backup(backup)) {
284 printf("\tbackup %d:\n", i);
285 print_root_backup(backup);
290 struct readable_flag_entry {
291 u64 bit;
292 char *output;
295 #define DEF_INCOMPAT_FLAG_ENTRY(bit_name) \
296 {BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
298 struct readable_flag_entry incompat_flags_array[] = {
299 DEF_INCOMPAT_FLAG_ENTRY(MIXED_BACKREF),
300 DEF_INCOMPAT_FLAG_ENTRY(DEFAULT_SUBVOL),
301 DEF_INCOMPAT_FLAG_ENTRY(MIXED_GROUPS),
302 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZO),
303 DEF_INCOMPAT_FLAG_ENTRY(COMPRESS_LZOv2),
304 DEF_INCOMPAT_FLAG_ENTRY(BIG_METADATA),
305 DEF_INCOMPAT_FLAG_ENTRY(EXTENDED_IREF),
306 DEF_INCOMPAT_FLAG_ENTRY(RAID56),
307 DEF_INCOMPAT_FLAG_ENTRY(SKINNY_METADATA),
308 DEF_INCOMPAT_FLAG_ENTRY(NO_HOLES)
310 static const int incompat_flags_num = sizeof(incompat_flags_array) /
311 sizeof(struct readable_flag_entry);
313 #define DEF_HEADER_FLAG_ENTRY(bit_name) \
314 {BTRFS_HEADER_FLAG_##bit_name, #bit_name}
315 #define DEF_SUPER_FLAG_ENTRY(bit_name) \
316 {BTRFS_SUPER_FLAG_##bit_name, #bit_name}
318 struct readable_flag_entry super_flags_array[] = {
319 DEF_HEADER_FLAG_ENTRY(WRITTEN),
320 DEF_HEADER_FLAG_ENTRY(RELOC),
321 DEF_SUPER_FLAG_ENTRY(CHANGING_FSID),
322 DEF_SUPER_FLAG_ENTRY(SEEDING),
323 DEF_SUPER_FLAG_ENTRY(METADUMP),
324 DEF_SUPER_FLAG_ENTRY(METADUMP_V2)
326 static const int super_flags_num = ARRAY_SIZE(super_flags_array);
328 #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
329 BTRFS_HEADER_FLAG_RELOC |\
330 BTRFS_SUPER_FLAG_CHANGING_FSID |\
331 BTRFS_SUPER_FLAG_SEEDING |\
332 BTRFS_SUPER_FLAG_METADUMP |\
333 BTRFS_SUPER_FLAG_METADUMP_V2)
335 static void __print_readable_flag(u64 flag, struct readable_flag_entry *array,
336 int array_size, u64 supported_flags)
338 int i;
339 int first = 1;
340 struct readable_flag_entry *entry;
342 if (!flag)
343 return;
345 printf("\t\t\t( ");
346 for (i = 0; i < array_size; i++) {
347 entry = array + i;
348 if (flag & entry->bit) {
349 if (first)
350 printf("%s ", entry->output);
351 else
352 printf("|\n\t\t\t %s ", entry->output);
353 first = 0;
356 flag &= ~supported_flags;
357 if (flag) {
358 if (first)
359 printf("unknown flag: 0x%llx ", flag);
360 else
361 printf("|\n\t\t\t unknown flag: 0x%llx ", flag);
363 printf(")\n");
366 static void print_readable_incompat_flag(u64 flag)
368 return __print_readable_flag(flag, incompat_flags_array,
369 incompat_flags_num,
370 BTRFS_FEATURE_INCOMPAT_SUPP);
373 static void print_readable_super_flag(u64 flag)
375 return __print_readable_flag(flag, super_flags_array,
376 super_flags_num, BTRFS_SUPER_FLAG_SUPP);
379 static void dump_superblock(struct btrfs_super_block *sb, int full)
381 int i;
382 char *s, buf[BTRFS_UUID_UNPARSED_SIZE];
383 u8 *p;
385 printf("csum\t\t\t0x");
386 for (i = 0, p = sb->csum; i < btrfs_super_csum_size(sb); i++)
387 printf("%02x", p[i]);
388 if (check_csum_sblock(sb, btrfs_super_csum_size(sb)))
389 printf(" [match]");
390 else
391 printf(" [DON'T MATCH]");
392 putchar('\n');
394 printf("bytenr\t\t\t%llu\n",
395 (unsigned long long)btrfs_super_bytenr(sb));
396 printf("flags\t\t\t0x%llx\n",
397 (unsigned long long)btrfs_super_flags(sb));
398 print_readable_super_flag(btrfs_super_flags(sb));
400 printf("magic\t\t\t");
401 s = (char *) &sb->magic;
402 for (i = 0; i < 8; i++)
403 putchar(isprint(s[i]) ? s[i] : '.');
404 if (btrfs_super_magic(sb) == BTRFS_MAGIC)
405 printf(" [match]\n");
406 else
407 printf(" [DON'T MATCH]\n");
409 uuid_unparse(sb->fsid, buf);
410 printf("fsid\t\t\t%s\n", buf);
412 printf("label\t\t\t");
413 s = sb->label;
414 for (i = 0; i < BTRFS_LABEL_SIZE && s[i]; i++)
415 putchar(isprint(s[i]) ? s[i] : '.');
416 putchar('\n');
418 printf("generation\t\t%llu\n",
419 (unsigned long long)btrfs_super_generation(sb));
420 printf("root\t\t\t%llu\n", (unsigned long long)btrfs_super_root(sb));
421 printf("sys_array_size\t\t%llu\n",
422 (unsigned long long)btrfs_super_sys_array_size(sb));
423 printf("chunk_root_generation\t%llu\n",
424 (unsigned long long)btrfs_super_chunk_root_generation(sb));
425 printf("root_level\t\t%llu\n",
426 (unsigned long long)btrfs_super_root_level(sb));
427 printf("chunk_root\t\t%llu\n",
428 (unsigned long long)btrfs_super_chunk_root(sb));
429 printf("chunk_root_level\t%llu\n",
430 (unsigned long long)btrfs_super_chunk_root_level(sb));
431 printf("log_root\t\t%llu\n",
432 (unsigned long long)btrfs_super_log_root(sb));
433 printf("log_root_transid\t%llu\n",
434 (unsigned long long)btrfs_super_log_root_transid(sb));
435 printf("log_root_level\t\t%llu\n",
436 (unsigned long long)btrfs_super_log_root_level(sb));
437 printf("total_bytes\t\t%llu\n",
438 (unsigned long long)btrfs_super_total_bytes(sb));
439 printf("bytes_used\t\t%llu\n",
440 (unsigned long long)btrfs_super_bytes_used(sb));
441 printf("sectorsize\t\t%llu\n",
442 (unsigned long long)btrfs_super_sectorsize(sb));
443 printf("nodesize\t\t%llu\n",
444 (unsigned long long)btrfs_super_nodesize(sb));
445 printf("leafsize\t\t%llu\n",
446 (unsigned long long)btrfs_super_leafsize(sb));
447 printf("stripesize\t\t%llu\n",
448 (unsigned long long)btrfs_super_stripesize(sb));
449 printf("root_dir\t\t%llu\n",
450 (unsigned long long)btrfs_super_root_dir(sb));
451 printf("num_devices\t\t%llu\n",
452 (unsigned long long)btrfs_super_num_devices(sb));
453 printf("compat_flags\t\t0x%llx\n",
454 (unsigned long long)btrfs_super_compat_flags(sb));
455 printf("compat_ro_flags\t\t0x%llx\n",
456 (unsigned long long)btrfs_super_compat_ro_flags(sb));
457 printf("incompat_flags\t\t0x%llx\n",
458 (unsigned long long)btrfs_super_incompat_flags(sb));
459 print_readable_incompat_flag(btrfs_super_incompat_flags(sb));
460 printf("csum_type\t\t%llu\n",
461 (unsigned long long)btrfs_super_csum_type(sb));
462 printf("csum_size\t\t%llu\n",
463 (unsigned long long)btrfs_super_csum_size(sb));
464 printf("cache_generation\t%llu\n",
465 (unsigned long long)btrfs_super_cache_generation(sb));
466 printf("uuid_tree_generation\t%llu\n",
467 (unsigned long long)btrfs_super_uuid_tree_generation(sb));
469 uuid_unparse(sb->dev_item.uuid, buf);
470 printf("dev_item.uuid\t\t%s\n", buf);
472 uuid_unparse(sb->dev_item.fsid, buf);
473 printf("dev_item.fsid\t\t%s %s\n", buf,
474 !memcmp(sb->dev_item.fsid, sb->fsid, BTRFS_FSID_SIZE) ?
475 "[match]" : "[DON'T MATCH]");
477 printf("dev_item.type\t\t%llu\n", (unsigned long long)
478 btrfs_stack_device_type(&sb->dev_item));
479 printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
480 btrfs_stack_device_total_bytes(&sb->dev_item));
481 printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
482 btrfs_stack_device_bytes_used(&sb->dev_item));
483 printf("dev_item.io_align\t%u\n", (unsigned int)
484 btrfs_stack_device_io_align(&sb->dev_item));
485 printf("dev_item.io_width\t%u\n", (unsigned int)
486 btrfs_stack_device_io_width(&sb->dev_item));
487 printf("dev_item.sector_size\t%u\n", (unsigned int)
488 btrfs_stack_device_sector_size(&sb->dev_item));
489 printf("dev_item.devid\t\t%llu\n",
490 btrfs_stack_device_id(&sb->dev_item));
491 printf("dev_item.dev_group\t%u\n", (unsigned int)
492 btrfs_stack_device_group(&sb->dev_item));
493 printf("dev_item.seek_speed\t%u\n", (unsigned int)
494 btrfs_stack_device_seek_speed(&sb->dev_item));
495 printf("dev_item.bandwidth\t%u\n", (unsigned int)
496 btrfs_stack_device_bandwidth(&sb->dev_item));
497 printf("dev_item.generation\t%llu\n", (unsigned long long)
498 btrfs_stack_device_generation(&sb->dev_item));
499 if (full) {
500 printf("sys_chunk_array[%d]:\n", BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
501 print_sys_chunk_array(sb);
502 printf("backup_roots[%d]:\n", BTRFS_NUM_BACKUP_ROOTS);
503 print_backup_roots(sb);