Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / misc / dumpe2fs.c
index 298dbb2..ae54f8a 100644 (file)
@@ -20,6 +20,7 @@
  * 94/02/27    - Ported to use the ext2fs library
  */
 
+#include "config.h"
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
 #else
@@ -44,9 +45,10 @@ extern int optind;
 
 #define in_use(m, x)   (ext2fs_test_bit ((x), (m)))
 
-const char * program_name = "dumpe2fs";
-char * device_name = NULL;
-int hex_format = 0;
+static const char * program_name = "dumpe2fs";
+static char * device_name = NULL;
+static int hex_format = 0;
+static int blocks64 = 0;
 
 static void usage(void)
 {
@@ -55,41 +57,48 @@ static void usage(void)
        exit (1);
 }
 
-static void print_number(unsigned long num)
+static void print_number(unsigned long long num)
 {
-       if (hex_format)
-               printf("0x%04lx", num);
-       else
-               printf("%lu", num);
+       if (hex_format) {
+               if (blocks64)
+                       printf("0x%08llx", num);
+               else
+                       printf("0x%04llx", num);
+       } else
+               printf("%llu", num);
 }
 
-static void print_range(unsigned long a, unsigned long b)
+static void print_range(unsigned long long a, unsigned long long b)
 {
-       if (hex_format)
-               printf("0x%04lx-0x%04lx", a, b);
-       else
-               printf("%lu-%lu", a, b);
+       if (hex_format) {
+               if (blocks64)
+                       printf("0x%08llx-0x%08llx", a, b);
+               else
+                       printf("0x%04llx-0x%04llx", a, b);
+       } else
+               printf("%llu-%llu", a, b);
 }
 
-static void print_free (unsigned long group, char * bitmap,
-                       unsigned long nbytes, unsigned long offset)
+static void print_free(unsigned long group, char * bitmap,
+                      unsigned long num, unsigned long offset, int ratio)
 {
        int p = 0;
        unsigned long i;
        unsigned long j;
 
-       offset += group * nbytes;
-       for (i = 0; i < nbytes; i++)
+       offset /= ratio;
+       offset += group * num;
+       for (i = 0; i < num; i++)
                if (!in_use (bitmap, i))
                {
                        if (p)
                                printf (", ");
-                       print_number(i + offset);
-                       for (j = i; j < nbytes && !in_use (bitmap, j); j++)
+                       print_number((i + offset) * ratio);
+                       for (j = i; j < num && !in_use (bitmap, j); j++)
                                ;
                        if (--j != i) {
                                fputc('-', stdout);
-                               print_number(j + offset);
+                               print_number((j + offset) * ratio);
                                i = j;
                        }
                        p = 1;
@@ -112,7 +121,7 @@ static void print_bg_opts(ext2_filsys fs, dgrp_t i)
 {
        int first = 1, bg_flags = 0;
 
-       if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
+       if (ext2fs_has_group_desc_csum(fs))
                bg_flags = ext2fs_bg_flags(fs, i);
 
        print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
@@ -126,20 +135,40 @@ static void print_bg_opts(ext2_filsys fs, dgrp_t i)
        fputc('\n', stdout);
 }
 
+static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
+                               blk64_t first_block, blk64_t last_block)
+{
+       if ((block >= first_block) && (block <= last_block)) {
+               if (itable && block == first_block)
+                       return;
+               printf(" (+%u)", (unsigned)(block - first_block));
+       } else if (fs->super->s_feature_incompat &
+                  EXT4_FEATURE_INCOMPAT_FLEX_BG) {
+               dgrp_t flex_grp = ext2fs_group_of_blk2(fs, block);
+               printf(" (bg #%u + %u)", flex_grp,
+                      (unsigned)(block-ext2fs_group_first_block2(fs,flex_grp)));
+       }
+}
+
 static void list_desc (ext2_filsys fs)
 {
        unsigned long i;
-       long diff;
-       blk_t   first_block, last_block;
-       blk_t   super_blk, old_desc_blk, new_desc_blk;
+       blk64_t first_block, last_block;
+       blk64_t super_blk, old_desc_blk, new_desc_blk;
        char *block_bitmap=NULL, *inode_bitmap=NULL;
+       const char *units = _("blocks");
        int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
        int             block_nbytes, inode_nbytes;
        int has_super;
-       blk_t           blk_itr = fs->super->s_first_data_block;
+       blk64_t         blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
        ext2_ino_t      ino_itr = 1;
+       errcode_t       retval;
 
-       block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
+       if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
+                                      EXT4_FEATURE_RO_COMPAT_BIGALLOC))
+               units = _("clusters");
+
+       block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
        inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
 
        if (fs->block_map)
@@ -159,20 +188,26 @@ static void list_desc (ext2_filsys fs)
        else
                old_desc_blocks = fs->desc_blocks;
        for (i = 0; i < fs->group_desc_count; i++) {
-               first_block = ext2fs_group_first_block(fs, i);
-               last_block = ext2fs_group_last_block(fs, i);
+               first_block = ext2fs_group_first_block2(fs, i);
+               last_block = ext2fs_group_last_block2(fs, i);
 
-               ext2fs_super_and_bgd_loc(fs, i, &super_blk,
-                                        &old_desc_blk, &new_desc_blk, 0);
+               ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
+                                         &old_desc_blk, &new_desc_blk, 0);
 
                printf (_("Group %lu: (Blocks "), i);
                print_range(first_block, last_block);
                fputs(")", stdout);
                print_bg_opts(fs, i);
-               if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
-                       printf(_("  Checksum 0x%04x, unused inodes %u\n"),
-                              ext2fs_bg_checksum(fs, i),
+               if (ext2fs_has_group_desc_csum(fs)) {
+                       unsigned csum = ext2fs_bg_checksum(fs, i);
+                       unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
+
+                       printf(_("  Checksum 0x%04x"), csum);
+                       if (csum != exp_csum)
+                               printf(_(" (EXPECTED 0x%04x)"), exp_csum);
+                       printf(_(", unused inodes %u\n"),
                               ext2fs_bg_itable_unused(fs, i));
+               }
                has_super = ((i==0) || super_blk);
                if (has_super) {
                        printf (_("  %s superblock at "),
@@ -180,18 +215,18 @@ static void list_desc (ext2_filsys fs)
                        print_number(super_blk);
                }
                if (old_desc_blk) {
-                       printf(_(", Group descriptors at "));
+                       printf("%s", _(", Group descriptors at "));
                        print_range(old_desc_blk,
                                    old_desc_blk + old_desc_blocks - 1);
                        if (reserved_gdt) {
-                               printf(_("\n  Reserved GDT blocks at "));
+                               printf("%s", _("\n  Reserved GDT blocks at "));
                                print_range(old_desc_blk + old_desc_blocks,
                                            old_desc_blk + old_desc_blocks +
                                            reserved_gdt - 1);
                        }
                } else if (new_desc_blk) {
                        fputc(has_super ? ',' : ' ', stdout);
-                       printf(_(" Group descriptor at "));
+                       printf("%s", _(" Group descriptor at "));
                        print_number(new_desc_blk);
                        has_super++;
                }
@@ -199,24 +234,29 @@ static void list_desc (ext2_filsys fs)
                        fputc('\n', stdout);
                fputs(_("  Block bitmap at "), stdout);
                print_number(ext2fs_block_bitmap_loc(fs, i));
-               diff = ext2fs_block_bitmap_loc(fs, i) - first_block;
-               if (diff >= 0)
-                       printf(" (+%ld)", diff);
+               print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
+                                   first_block, last_block);
+               if (fs->super->s_feature_ro_compat &
+                   EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
+                       printf(_(", csum 0x%08x"),
+                              ext2fs_block_bitmap_checksum(fs, i));
                fputs(_(", Inode bitmap at "), stdout);
                print_number(ext2fs_inode_bitmap_loc(fs, i));
-               diff = ext2fs_inode_bitmap_loc(fs, i) - first_block;
-               if (diff >= 0)
-                       printf(" (+%ld)", diff);
+               print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
+                                   first_block, last_block);
+               if (fs->super->s_feature_ro_compat &
+                   EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
+                       printf(_(", csum 0x%08x"),
+                              ext2fs_inode_bitmap_checksum(fs, i));
                fputs(_("\n  Inode table at "), stdout);
                print_range(ext2fs_inode_table_loc(fs, i),
                            ext2fs_inode_table_loc(fs, i) +
                            inode_blocks_per_group - 1);
-               diff = ext2fs_inode_table_loc(fs, i) - first_block;
-               if (diff > 0)
-                       printf(" (+%ld)", diff);
-               printf (_("\n  %u free blocks, %u free inodes, "
+               print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
+                                   first_block, last_block);
+               printf (_("\n  %u free %s, %u free inodes, "
                          "%u directories%s"),
-                       ext2fs_bg_free_blocks_count(fs, i),
+                       ext2fs_bg_free_blocks_count(fs, i), units,
                        ext2fs_bg_free_inodes_count(fs, i),
                        ext2fs_bg_used_dirs_count(fs, i),
                        ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
@@ -225,20 +265,30 @@ static void list_desc (ext2_filsys fs)
                                ext2fs_bg_itable_unused(fs, i));
                if (block_bitmap) {
                        fputs(_("  Free blocks: "), stdout);
-                       ext2fs_get_block_bitmap_range2(fs->block_map,
+                       retval = ext2fs_get_block_bitmap_range2(fs->block_map,
                                 blk_itr, block_nbytes << 3, block_bitmap);
-                       print_free (i, block_bitmap,
-                                   fs->super->s_blocks_per_group,
-                                   fs->super->s_first_data_block);
+                       if (retval)
+                               com_err("list_desc", retval,
+                                       "while reading block bitmap");
+                       else
+                               print_free(i, block_bitmap,
+                                          fs->super->s_clusters_per_group,
+                                          fs->super->s_first_data_block,
+                                          EXT2FS_CLUSTER_RATIO(fs));
                        fputc('\n', stdout);
-                       blk_itr += fs->super->s_blocks_per_group;
+                       blk_itr += fs->super->s_clusters_per_group;
                }
                if (inode_bitmap) {
                        fputs(_("  Free inodes: "), stdout);
-                       ext2fs_get_inode_bitmap_range2(fs->inode_map,
+                       retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
                                 ino_itr, inode_nbytes << 3, inode_bitmap);
-                       print_free (i, inode_bitmap,
-                                   fs->super->s_inodes_per_group, 1);
+                       if (retval)
+                               com_err("list_desc", retval,
+                                       "while reading inode bitmap");
+                       else
+                               print_free(i, inode_bitmap,
+                                          fs->super->s_inodes_per_group,
+                                          1, 1);
                        fputc('\n', stdout);
                        ino_itr += fs->super->s_inodes_per_group;
                }
@@ -265,7 +315,7 @@ static void list_bad_blocks(ext2_filsys fs, int dump)
        retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
        if (retval) {
                com_err("ext2fs_badblocks_list_iterate_begin", retval,
-                       _("while printing bad block list"));
+                       "%s", _("while printing bad block list"));
                return;
        }
        if (dump) {
@@ -284,6 +334,16 @@ static void list_bad_blocks(ext2_filsys fs, int dump)
        ext2fs_badblocks_list_free(bb_list);
 }
 
+static const char *journal_checksum_type_str(__u8 type)
+{
+       switch (type) {
+       case JBD2_CRC32C_CHKSUM:
+               return "crc32c";
+       default:
+               return "unknown";
+       }
+}
+
 static void print_inline_journal_information(ext2_filsys fs)
 {
        journal_superblock_t    *jsb;
@@ -295,32 +355,34 @@ static void print_inline_journal_information(ext2_filsys fs)
        __u32                   *mask_ptr, mask, m;
        int                     i, j, size, printed = 0;
 
+       if (fs->flags & EXT2_FLAG_IMAGE_FILE)
+               return;
        retval = ext2fs_read_inode(fs, ino,  &inode);
        if (retval) {
-               com_err(program_name, retval,
+               com_err(program_name, retval, "%s",
                        _("while reading journal inode"));
                exit(1);
        }
        retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
        if (retval) {
-               com_err(program_name, retval,
+               com_err(program_name, retval, "%s",
                        _("while opening journal inode"));
                exit(1);
        }
        retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
        if (retval) {
-               com_err(program_name, retval,
+               com_err(program_name, retval, "%s",
                        _("while reading journal super block"));
                exit(1);
        }
        ext2fs_file_close(journal_file);
        jsb = (journal_superblock_t *) buf;
        if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
-               fprintf(stderr,
-                       "Journal superblock magic number invalid!\n");
+               fprintf(stderr, "%s",
+                       _("Journal superblock magic number invalid!\n"));
                exit(1);
        }
-       printf(_("Journal features:        "));
+       printf("%s", _("Journal features:        "));
        for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
                mask = be32_to_cpu(*mask_ptr);
                for (j=0,m=1; j < 32; j++, m<<=1) {
@@ -350,6 +412,18 @@ static void print_inline_journal_information(ext2_filsys fs)
               (unsigned int)ntohl(jsb->s_maxlen),
               (unsigned int)ntohl(jsb->s_sequence),
               (unsigned int)ntohl(jsb->s_start));
+       if (jsb->s_feature_compat &
+           ext2fs_cpu_to_be32(JFS_FEATURE_COMPAT_CHECKSUM))
+               printf(_("Journal checksum type:    crc32\n"));
+       if (jsb->s_feature_incompat &
+           ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V2))
+               printf(_("Journal checksum type:    %s\n"
+                        "Journal checksum:         0x%08x\n"),
+                      journal_checksum_type_str(jsb->s_checksum_type),
+                      ext2fs_be32_to_cpu(jsb->s_checksum));
+       if (jsb->s_errno != 0)
+               printf(_("Journal errno:            %d\n"),
+                      (int) ntohl(jsb->s_errno));
 }
 
 static void print_journal_information(ext2_filsys fs)
@@ -361,8 +435,10 @@ static void print_journal_information(ext2_filsys fs)
        journal_superblock_t    *jsb;
 
        /* Get the journal superblock */
-       if ((retval = io_channel_read_blk64(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
-               com_err(program_name, retval,
+       if ((retval = io_channel_read_blk64(fs->io,
+                                           fs->super->s_first_data_block + 1,
+                                           -1024, buf))) {
+               com_err(program_name, retval, "%s",
                        _("while reading journal superblock"));
                exit(1);
        }
@@ -370,11 +446,21 @@ static void print_journal_information(ext2_filsys fs)
        if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
            (jsb->s_header.h_blocktype !=
             (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
-               com_err(program_name, 0,
+               com_err(program_name, 0, "%s",
                        _("Couldn't find journal superblock magic numbers"));
                exit(1);
        }
 
+       if (jsb->s_feature_compat &
+           ext2fs_cpu_to_be32(JFS_FEATURE_COMPAT_CHECKSUM))
+               printf(_("Journal checksum type:    crc32\n"));
+       if (jsb->s_feature_incompat &
+           ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V2))
+               printf(_("Journal checksum type:    %s\n"
+                        "Journal checksum:         0x%08x\n"),
+                      journal_checksum_type_str(jsb->s_checksum_type),
+                      ext2fs_be32_to_cpu(jsb->s_checksum));
+
        printf(_("\nJournal block size:       %u\n"
                 "Journal length:           %u\n"
                 "Journal first block:      %u\n"
@@ -393,7 +479,7 @@ static void print_journal_information(ext2_filsys fs)
        }
 }
 
-static void parse_extended_opts(const char *opts, blk_t *superblock,
+static void parse_extended_opts(const char *opts, blk64_t *superblock,
                                int *blocksize)
 {
        char    *buf, *token, *next, *p, *arg, *badopt = 0;
@@ -403,7 +489,7 @@ static void parse_extended_opts(const char *opts, blk_t *superblock,
        len = strlen(opts);
        buf = malloc(len+1);
        if (!buf) {
-               fprintf(stderr,
+               fprintf(stderr, "%s",
                        _("Couldn't allocate memory to parse options!\n"));
                exit(1);
        }
@@ -475,7 +561,7 @@ int main (int argc, char ** argv)
        errcode_t       retval;
        ext2_filsys     fs;
        int             print_badblocks = 0;
-       blk_t           use_superblock = 0;
+       blk64_t         use_superblock = 0;
        int             use_blocksize = 0;
        int             image_dump = 0;
        int             force = 0;
@@ -488,6 +574,7 @@ int main (int argc, char ** argv)
        setlocale(LC_CTYPE, "");
        bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
        textdomain(NLS_CAT_NAME);
+       set_com_err_gettext(gettext);
 #endif
        add_error_table(&et_ext2_error_table);
        fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
@@ -528,7 +615,7 @@ int main (int argc, char ** argv)
        if (optind > argc - 1)
                usage();
        device_name = argv[optind++];
-       flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES;
+       flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
        if (force)
                flags |= EXT2_FLAG_FORCE;
        if (image_dump)
@@ -551,9 +638,12 @@ int main (int argc, char ** argv)
        if (retval) {
                com_err (program_name, retval, _("while trying to open %s"),
                         device_name);
-               printf (_("Couldn't find valid filesystem superblock.\n"));
+               printf("%s", _("Couldn't find valid filesystem superblock.\n"));
                exit (1);
        }
+       fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
+       if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
+               blocks64 = 1;
        if (print_badblocks) {
                list_bad_blocks(fs, 1);
        } else {