- int opt;
- int all = 0;
- char *filename;
- int fd = -1;
- int arg, i;
- u64 sb_bytenr = btrfs_sb_offset(0);
-
- while ((opt = getopt(argc, argv, "ai:")) != -1) {
- switch (opt) {
- case 'i':
- arg = atoi(optarg);
-
- if (arg < 0 || arg >= BTRFS_SUPER_MIRROR_MAX) {
- fprintf(stderr,
- "Illegal super_mirror %d\n",
- arg);
- print_usage();
- exit(1);
- }
- sb_bytenr = btrfs_sb_offset(arg);
- break;
-
- case 'a':
- all = 1;
- break;
-
- default:
- print_usage();
- exit(1);
- }
- }
-
- if (argc < optind + 1) {
- print_usage();
- exit(1);
- }
-
- for (i = optind; i < argc; i++) {
- filename = argv[i];
- fd = open(filename, O_RDONLY, 0666);
- if (fd < 0) {
- fprintf(stderr, "Could not open %s\n", filename);
- exit(1);
- }
-
- if (all) {
- int idx;
- for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
- sb_bytenr = btrfs_sb_offset(idx);
- if (load_and_dump_sb(filename, fd, sb_bytenr)) {
- close(fd);
- exit(1);
- }
-
- putchar('\n');
- }
- } else {
- load_and_dump_sb(filename, fd, sb_bytenr);
- putchar('\n');
- }
- close(fd);
- }
-
- exit(0);
-}
-
-static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr)
-{
- u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
- struct btrfs_super_block *sb;
- u64 ret;
-
- sb = (struct btrfs_super_block *)super_block_data;
-
- ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
- if (ret != BTRFS_SUPER_INFO_SIZE) {
- int e = errno;
-
- /* check if the disk if too short for further superblock */
- if (ret == 0 && e == 0)
- return 0;
-
- fprintf(stderr,
- "ERROR: Failed to read the superblock on %s at %llu\n",
- filename, (unsigned long long)sb_bytenr);
- fprintf(stderr,
- "ERROR: error = '%s', errno = %d\n", strerror(e), e);
- return 1;
- }
- printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
- printf("---------------------------------------------------------\n");
- dump_superblock(sb);
- return 0;
-}
-
-static int check_csum_sblock(void *sb, int csum_size)
-{
- char result[csum_size];
- u32 crc = ~(u32)0;
-
- crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE,
- crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
- btrfs_csum_final(crc, result);
-
- return !memcmp(sb, &result, csum_size);
-}
-
-static void dump_superblock(struct btrfs_super_block *sb)
-{
- int i;
- char *s, buf[36+1];
- u8 *p;
-
- printf("csum\t\t\t0x");
- for (i = 0, p = sb->csum; i < btrfs_super_csum_size(sb); i++)
- printf("%02x", p[i]);
- if (check_csum_sblock(sb, btrfs_super_csum_size(sb)))
- printf(" [match]");
- else
- printf(" [DON'T MATCH]");
- putchar('\n');
-
- printf("bytenr\t\t\t%llu\n",
- (unsigned long long)btrfs_super_bytenr(sb));
- printf("flags\t\t\t0x%llx\n",
- (unsigned long long)btrfs_super_flags(sb));
-
- printf("magic\t\t\t");
- s = (char *) &sb->magic;
- for (i = 0; i < 8; i++)
- putchar(isprint(s[i]) ? s[i] : '.');
- if (sb->magic == cpu_to_le64(BTRFS_MAGIC))
- printf(" [match]\n");
- else
- printf(" [DON'T MATCH]\n");