]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - lib/ext2fs/openfs.c
Merge branch 'maint' into next
[thirdparty/e2fsprogs.git] / lib / ext2fs / openfs.c
index aac8894e1353c6ea21ddc6caab2f27a8f5af5d48..3331452df910c86183a1301c95569f47632da27c 100644 (file)
@@ -29,7 +29,6 @@
 
 #include "ext2_fs.h"
 
-
 #include "ext2fs.h"
 #include "e2image.h"
 
@@ -37,12 +36,19 @@ 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))
@@ -71,7 +77,7 @@ blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block,
                else
                        has_super = 0;
        }
-       return ret_blk + has_super;
+       return ret_blk + has_super + group_zero_adjust;
 }
 
 blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
@@ -87,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.
@@ -113,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);
 
@@ -131,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;
@@ -169,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);
        }
 
        /*
@@ -207,6 +228,14 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
        if (fs->orig_super)
                memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
 
+       if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
+               retval = 0;
+               if (!ext2fs_verify_csum_type(fs, fs->super))
+                       retval = EXT2_ET_UNKNOWN_CSUM;
+               if (!ext2fs_superblock_csum_verify(fs, fs->super))
+                       retval = EXT2_ET_SB_CSUM_INVALID;
+       }
+
 #ifdef WORDS_BIGENDIAN
        fs->flags |= EXT2_FLAG_SWAP_BYTES;
        ext2fs_swap_super(fs->super);
@@ -217,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;
@@ -252,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;
        }
@@ -269,24 +298,34 @@ 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 (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;
+               }
+       }
+
        fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
                fs->super->s_log_block_size;
        if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
@@ -313,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;
@@ -324,6 +362,8 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
                goto cleanup;
        }
+       /* Precompute the FS UUID to seed other checksums */
+       ext2fs_init_csum_seed(fs);
 
        /*
         * Read group descriptors
@@ -337,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;
@@ -372,11 +435,18 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                gdp = (struct ext2_group_desc *) dest;
                for (j=0; j < groups_per_block*first_meta_bg; j++) {
                        gdp = ext2fs_group_desc(fs, fs->group_desc, j);
-                       ext2fs_swap_group_desc2(fs, gdp);
+                       if (gdp)
+                               ext2fs_swap_group_desc2(fs, gdp);
                }
 #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);
@@ -386,7 +456,8 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                for (j=0; j < groups_per_block; j++) {
                        gdp = ext2fs_group_desc(fs, fs->group_desc,
                                                i * groups_per_block + j);
-                       ext2fs_swap_group_desc2(fs, gdp);
+                       if (gdp)
+                               ext2fs_swap_group_desc2(fs, gdp);
                }
 #endif
                dest += fs->blocksize;
@@ -398,8 +469,7 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
         * If recovery is from backup superblock, Clear _UNININT flags &
         * reset bg_itable_unused to zero
         */
-       if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
-                                       EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
+       if (superblock > 1 && ext2fs_has_group_desc_csum(fs)) {
                dgrp_t group;
 
                for (group = 0; group < fs->group_desc_count; group++) {
@@ -413,8 +483,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);
@@ -425,15 +495,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;
 }