Whamcloud - gitweb
Teach ext2fs_open2() to honor the EXT2_FLAG_SUPER_ONLY flag
[tools/e2fsprogs.git] / lib / ext2fs / openfs.c
index cc8a90d..ec2d6cb 100644 (file)
@@ -29,7 +29,6 @@
 
 #include "ext2_fs.h"
 
-
 #include "ext2fs.h"
 #include "e2image.h"
 
@@ -37,17 +36,24 @@ blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block,
                                     dgrp_t i)
 {
        int     bg;
-       int     has_super = 0;
+       int     has_super = 0, group_zero_adjust = 0;
        blk64_t ret_blk;
 
-       if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
+       /*
+        * On a bigalloc FS with 1K blocks, block 0 is reserved for non-ext4
+        * stuff, so adjust for that if we're being asked for group 0.
+        */
+       if (i == 0 && fs->blocksize == 1024 && EXT2FS_CLUSTER_RATIO(fs) > 1)
+               group_zero_adjust = 1;
+
+       if (!ext2fs_has_feature_meta_bg(fs->super) ||
            (i < fs->super->s_first_meta_bg))
-               return (group_block + i + 1);
+               return group_block + i + 1 + group_zero_adjust;
 
        bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
        if (ext2fs_bg_has_super(fs, bg))
                has_super = 1;
-       ret_blk = ext2fs_group_first_block2(fs, bg) + has_super;
+       ret_blk = ext2fs_group_first_block2(fs, bg);
        /*
         * If group_block is not the normal value, we're trying to use
         * the backup group descriptors and superblock --- so use the
@@ -57,10 +63,21 @@ blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block,
         * have the infrastructure in place to do that.
         */
        if (group_block != fs->super->s_first_data_block &&
-           ((ret_blk + fs->super->s_blocks_per_group) <
-            ext2fs_blocks_count(fs->super)))
+           ((ret_blk + has_super + fs->super->s_blocks_per_group) <
+            ext2fs_blocks_count(fs->super))) {
                ret_blk += fs->super->s_blocks_per_group;
-       return ret_blk;
+
+               /*
+                * If we're going to jump forward a block group, make sure
+                * that we adjust has_super to account for the next group's
+                * backup superblock (or lack thereof).
+                */
+               if (ext2fs_bg_has_super(fs, bg + 1))
+                       has_super = 1;
+               else
+                       has_super = 0;
+       }
+       return ret_blk + has_super + group_zero_adjust;
 }
 
 blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
@@ -76,6 +93,12 @@ errcode_t ext2fs_open(const char *name, int flags, int superblock,
                            manager, ret_fs);
 }
 
+static void block_sha_map_free_entry(void *data)
+{
+       free(data);
+       return;
+}
+
 /*
  *  Note: if superblock is non-zero, block-size must also be non-zero.
  *     Superblock and block_size can be zero to use the default size.
@@ -102,11 +125,15 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
        unsigned int    blocks_per_group, io_flags;
        blk64_t         group_block, blk;
        char            *dest, *cp;
+       int             group_zero_adjust = 0;
+       unsigned int    inode_size;
+       __u64           groups_cnt;
 #ifdef WORDS_BIGENDIAN
        unsigned int    groups_per_block;
        struct ext2_group_desc *gdp;
        int             j;
 #endif
+       char            *time_env;
 
        EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
 
@@ -120,6 +147,11 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
        /* don't overwrite sb backups unless flag is explicitly cleared */
        fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
        fs->umask = 022;
+
+       time_env = getenv("E2FSPROGS_FAKE_TIME");
+       if (time_env)
+               fs->now = strtoul(time_env, NULL, 0);
+
        retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
        if (retval)
                goto cleanup;
@@ -158,10 +190,10 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                                             fs->image_header);
                if (retval)
                        goto cleanup;
-               if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
+               if (ext2fs_le32_to_cpu(fs->image_header->magic_number) != EXT2_ET_MAGIC_E2IMAGE)
                        return EXT2_ET_MAGIC_E2IMAGE;
                superblock = 1;
-               block_size = fs->image_header->fs_blocksize;
+               block_size = ext2fs_le32_to_cpu(fs->image_header->fs_blocksize);
        }
 
        /*
@@ -202,8 +234,6 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                        retval = EXT2_ET_UNKNOWN_CSUM;
                if (!ext2fs_superblock_csum_verify(fs, fs->super))
                        retval = EXT2_ET_SB_CSUM_INVALID;
-               if (retval)
-                       goto cleanup;
        }
 
 #ifdef WORDS_BIGENDIAN
@@ -216,10 +246,11 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
        }
 #endif
 
-       if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
+       if (fs->super->s_magic != EXT2_SUPER_MAGIC)
                retval = EXT2_ET_BAD_MAGIC;
+       if (retval)
                goto cleanup;
-       }
+
        if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
                retval = EXT2_ET_REV_TOO_HIGH;
                goto cleanup;
@@ -251,15 +282,14 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                }
 
                if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
-                   (fs->super->s_feature_incompat &
-                    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
+                   ext2fs_has_feature_journal_dev(fs->super)) {
                        retval = EXT2_ET_UNSUPP_FEATURE;
                        goto cleanup;
                }
        }
 
-       if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
-           EXT2_MAX_BLOCK_LOG_SIZE) {
+       if (fs->super->s_log_block_size >
+           (unsigned) (EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) {
                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
                goto cleanup;
        }
@@ -268,37 +298,32 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
         * bigalloc requires cluster-aware bitfield operations, which at the
         * moment means we need EXT2_FLAG_64BITS.
         */
-       if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
-                                      EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
+       if (ext2fs_has_feature_bigalloc(fs->super) &&
            !(flags & EXT2_FLAG_64BITS)) {
                retval = EXT2_ET_CANT_USE_LEGACY_BITMAPS;
                goto cleanup;
        }
 
-       if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
-                                       EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
+       if (!ext2fs_has_feature_bigalloc(fs->super) &&
            (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
                goto cleanup;
        }
        fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
-       if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
+       inode_size = EXT2_INODE_SIZE(fs->super);
+       if ((inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
+           (inode_size > fs->blocksize) ||
+           (inode_size & (inode_size - 1))) {
                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
                goto cleanup;
        }
 
        /* Enforce the block group descriptor size */
-       if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) {
+       if (ext2fs_has_feature_64bit(fs->super)) {
                if (fs->super->s_desc_size < EXT2_MIN_DESC_SIZE_64BIT) {
                        retval = EXT2_ET_BAD_DESC_SIZE;
                        goto cleanup;
                }
-       } else {
-               if (fs->super->s_desc_size &&
-                   fs->super->s_desc_size != EXT2_MIN_DESC_SIZE) {
-                       retval = EXT2_ET_BAD_DESC_SIZE;
-                       goto cleanup;
-               }
        }
 
        fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
@@ -327,8 +352,7 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
         * If this is an external journal device, don't try to read
         * the group descriptors, because they're not there.
         */
-       if (fs->super->s_feature_incompat &
-           EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
+       if (ext2fs_has_feature_journal_dev(fs->super)) {
                fs->group_desc_count = 0;
                *ret_fs = fs;
                return 0;
@@ -353,34 +377,57 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
                goto cleanup;
        }
-       fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
-                                                fs->super->s_first_data_block,
-                                                blocks_per_group);
-       if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
+       groups_cnt = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
+                                      fs->super->s_first_data_block,
+                                      blocks_per_group);
+       if (groups_cnt >> 32) {
+               retval = EXT2_ET_CORRUPT_SUPERBLOCK;
+               goto cleanup;
+       }
+       fs->group_desc_count =  groups_cnt;
+       if (!(flags & EXT2_FLAG_IGNORE_SB_ERRORS) &&
+           (__u64)fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
            fs->super->s_inodes_count) {
                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
                goto cleanup;
        }
        fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
                                          EXT2_DESC_PER_BLOCK(fs->super));
+       if (flags & EXT2_FLAG_SUPER_ONLY)
+               goto skip_read_bg;
        retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
                                &fs->group_desc);
        if (retval)
                goto cleanup;
        if (!group_block)
                group_block = fs->super->s_first_data_block;
+       /*
+        * On a FS with a 1K blocksize, block 0 is reserved for bootloaders
+        * so we must increment block numbers to any group 0 items.
+        *
+        * However, we cannot touch group_block directly because in the meta_bg
+        * case, the ext2fs_descriptor_block_loc2() function will interpret
+        * group_block != s_first_data_block to mean that we want to access the
+        * backup group descriptors.  This is not what we want if the caller
+        * set superblock == 0 (i.e. auto-detect the superblock), which is
+        * what's going on here.
+        */
        if (group_block == 0 && fs->blocksize == 1024)
-               group_block = 1; /* Deal with 1024 blocksize && bigalloc */
+               group_zero_adjust = 1;
        dest = (char *) fs->group_desc;
 #ifdef WORDS_BIGENDIAN
        groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
 #endif
-       if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
+       if (ext2fs_has_feature_meta_bg(fs->super) &&
+           !(flags & EXT2_FLAG_IMAGE_FILE)) {
                first_meta_bg = fs->super->s_first_meta_bg;
-       else
+               if (first_meta_bg > fs->desc_blocks)
+                       first_meta_bg = fs->desc_blocks;
+       } else
                first_meta_bg = fs->desc_blocks;
        if (first_meta_bg) {
-               retval = io_channel_read_blk(fs->io, group_block+1,
+               retval = io_channel_read_blk(fs->io, group_block +
+                                            group_zero_adjust + 1,
                                             first_meta_bg, dest);
                if (retval)
                        goto cleanup;
@@ -393,6 +440,12 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
 #endif
                dest += fs->blocksize*first_meta_bg;
        }
+
+       for (i = first_meta_bg ; i < fs->desc_blocks; i++) {
+               blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
+               io_channel_cache_readahead(fs->io, blk, 1);
+       }
+
        for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
                blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
                retval = io_channel_read_blk64(fs->io, blk, 1, dest);
@@ -428,8 +481,8 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                if (fs->flags & EXT2_FLAG_RW)
                        ext2fs_mark_super_dirty(fs);
        }
-
-       if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
+skip_read_bg:
+       if (ext2fs_has_feature_mmp(fs->super) &&
            !(flags & EXT2_FLAG_SKIP_MMP) &&
            (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
                retval = ext2fs_mmp_start(fs);
@@ -440,15 +493,29 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                }
        }
 
+       if (fs->flags & EXT2_FLAG_SHARE_DUP) {
+               fs->block_sha_map = ext2fs_hashmap_create(ext2fs_djb2_hash,
+                                       block_sha_map_free_entry, 4096);
+               if (!fs->block_sha_map) {
+                       retval = EXT2_ET_NO_MEMORY;
+                       goto cleanup;
+               }
+               ext2fs_set_feature_shared_blocks(fs->super);
+       }
+
+       if (ext2fs_has_feature_casefold(fs->super))
+               fs->encoding = ext2fs_load_nls_table(fs->super->s_encoding);
+
        fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
        *ret_fs = fs;
 
        return 0;
 cleanup:
-       if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
-               *ret_fs = fs;
-       else
+       if (!(flags & EXT2_FLAG_NOFREE_ON_ERROR)) {
                ext2fs_free(fs);
+               fs = NULL;
+       }
+       *ret_fs = fs;
        return retval;
 }