Whamcloud - gitweb
Set pointer to NULL after ext2fs_free
[tools/e2fsprogs.git] / lib / ext2fs / openfs.c
index 525693e..a1a3517 100644 (file)
@@ -1,14 +1,15 @@
 /*
  * openfs.c --- open an ext2 filesystem
- * 
+ *
  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
- * 
+ *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
@@ -22,6 +23,9 @@
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
 
 #include "ext2_fs.h"
 
 #include "ext2fs.h"
 #include "e2image.h"
 
-blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
+blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block,
+                                    dgrp_t i)
 {
        int     bg;
-       int     has_super = 0;
-       int     ret_blk;
+       int     has_super = 0, group_zero_adjust = 0;
+       blk64_t ret_blk;
+
+       /*
+        * 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 (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
            (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_block(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
@@ -52,17 +64,33 @@ blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
         * 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) <
-            fs->super->s_blocks_count))
+           ((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)
+{
+       return ext2fs_descriptor_block_loc2(fs, group_block, i);
 }
 
 errcode_t ext2fs_open(const char *name, int flags, int superblock,
-                     unsigned int block_size, io_manager manager, 
+                     unsigned int block_size, io_manager manager,
                      ext2_filsys *ret_fs)
 {
-       return ext2fs_open2(name, 0, flags, superblock, block_size, 
+       return ext2fs_open2(name, 0, flags, superblock, block_size,
                            manager, ret_fs);
 }
 
@@ -71,35 +99,40 @@ errcode_t ext2fs_open(const char *name, int flags, int superblock,
  *     Superblock and block_size can be zero to use the default size.
  *
  * Valid flags for ext2fs_open()
- * 
+ *
  *     EXT2_FLAG_RW    - Open the filesystem for read/write.
  *     EXT2_FLAG_FORCE - Open the filesystem even if some of the
  *                             features aren't supported.
  *     EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
+ *     EXT2_FLAG_SKIP_MMP - Open without multi-mount protection check.
+ *     EXT2_FLAG_64BITS - Allow 64-bit bitfields (needed for large
+ *                             filesystems)
  */
 errcode_t ext2fs_open2(const char *name, const char *io_options,
                       int flags, int superblock,
-                      unsigned int block_size, io_manager manager, 
+                      unsigned int block_size, io_manager manager,
                       ext2_filsys *ret_fs)
 {
        ext2_filsys     fs;
        errcode_t       retval;
-       unsigned long   i;
+       unsigned long   i, first_meta_bg;
        __u32           features;
-       int             groups_per_block, blocks_per_group, io_flags;
-       blk_t           group_block, blk;
+       unsigned int    blocks_per_group, io_flags;
+       blk64_t         group_block, blk;
        char            *dest, *cp;
+       int             group_zero_adjust = 0;
 #ifdef WORDS_BIGENDIAN
+       unsigned int    groups_per_block;
        struct ext2_group_desc *gdp;
        int             j;
 #endif
-       
+
        EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
 
        retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
        if (retval)
                return retval;
-       
+
        memset(fs, 0, sizeof(struct struct_ext2_filsys));
        fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
        fs->flags = flags;
@@ -115,21 +148,23 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                *cp++ = 0;
                io_options = cp;
        }
-               
+
        io_flags = 0;
        if (flags & EXT2_FLAG_RW)
                io_flags |= IO_FLAG_RW;
        if (flags & EXT2_FLAG_EXCLUSIVE)
                io_flags |= IO_FLAG_EXCLUSIVE;
+       if (flags & EXT2_FLAG_DIRECT_IO)
+               io_flags |= IO_FLAG_DIRECT_IO;
        retval = manager->open(fs->device_name, io_flags, &fs->io);
        if (retval)
                goto cleanup;
-       if (io_options && 
+       if (io_options &&
            (retval = io_channel_set_options(fs->io, io_options)))
                goto cleanup;
        fs->image_io = fs->io;
        fs->io->app_data = fs;
-       retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
+       retval = io_channel_alloc_buf(fs->io, -SUPERBLOCK_SIZE, &fs->super);
        if (retval)
                goto cleanup;
        if (flags & EXT2_FLAG_IMAGE_FILE) {
@@ -189,7 +224,7 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                goto cleanup;
        }
 #endif
-       
+
        if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
                retval = EXT2_ET_BAD_MAGIC;
                goto cleanup;
@@ -206,7 +241,7 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                features = fs->super->s_feature_incompat;
 #ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
                if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
-                       features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;
+                       features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT;
 #endif
                if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
                        retval = EXT2_ET_UNSUPP_FEATURE;
@@ -216,7 +251,7 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                features = fs->super->s_feature_ro_compat;
 #ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
                if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
-                       features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT;
+                       features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT;
 #endif
                if ((flags & EXT2_FLAG_RW) &&
                    (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
@@ -231,19 +266,43 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                        goto cleanup;
                }
        }
-       
+
        if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
            EXT2_MAX_BLOCK_LOG_SIZE) {
                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
                goto cleanup;
        }
-       fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
+
+       /*
+        * 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) &&
+           !(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) &&
+           (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) {
                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
                goto cleanup;
        }
-       fs->fragsize = EXT2_FRAG_SIZE(fs->super);
-       fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
+       fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
+               fs->super->s_log_block_size;
+       if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
+           EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
+               retval = EXT2_ET_CORRUPT_SUPERBLOCK;
+               goto cleanup;
+       }
+       fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
                                       EXT2_INODE_SIZE(fs->super) +
                                       EXT2_BLOCK_SIZE(fs->super) - 1) /
                                      EXT2_BLOCK_SIZE(fs->super));
@@ -268,20 +327,32 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                *ret_fs = fs;
                return 0;
        }
-       
+
+       if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
+               retval = EXT2_ET_CORRUPT_SUPERBLOCK;
+               goto cleanup;
+       }
+
        /*
         * Read group descriptors
         */
        blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
        if (blocks_per_group == 0 ||
            blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
-           fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) {
+           fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
+           EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
+           fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
+               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) !=
+           fs->super->s_inodes_count) {
                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
                goto cleanup;
        }
-       fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
-                                              fs->super->s_first_data_block,
-                                              blocks_per_group);
        fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
                                          EXT2_DESC_PER_BLOCK(fs->super));
        retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
@@ -290,17 +361,53 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
                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_zero_adjust = 1;
        dest = (char *) fs->group_desc;
+#ifdef WORDS_BIGENDIAN
        groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
-       for (i=0 ; i < fs->desc_blocks; i++) {
-               blk = ext2fs_descriptor_block_loc(fs, group_block, i);
-               retval = io_channel_read_blk(fs->io, blk, 1, dest);
+#endif
+       if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
+               first_meta_bg = fs->super->s_first_meta_bg;
+       else
+               first_meta_bg = fs->desc_blocks;
+       if (first_meta_bg) {
+               retval = io_channel_read_blk(fs->io, group_block +
+                                            group_zero_adjust + 1,
+                                            first_meta_bg, dest);
                if (retval)
                        goto cleanup;
 #ifdef WORDS_BIGENDIAN
                gdp = (struct ext2_group_desc *) dest;
-               for (j=0; j < groups_per_block; j++)
-                       ext2fs_swap_group_desc(gdp++);
+               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);
+               }
+#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);
+               retval = io_channel_read_blk64(fs->io, blk, 1, dest);
+               if (retval)
+                       goto cleanup;
+#ifdef WORDS_BIGENDIAN
+               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);
+               }
 #endif
                dest += fs->blocksize;
        }
@@ -313,30 +420,47 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
         */
        if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
                                        EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
-               struct ext2_group_desc *gd;
-               for (i = 0, gd = fs->group_desc; i < fs->group_desc_count;
-                    i++, gd++) {
-                       gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
-                       gd->bg_flags &= ~EXT2_BG_INODE_UNINIT;
-                       gd->bg_itable_unused = 0;
+               dgrp_t group;
+
+               for (group = 0; group < fs->group_desc_count; group++) {
+                       ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
+                       ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
+                       ext2fs_bg_itable_unused_set(fs, group, 0);
+                       /* The checksum will be reset later, but fix it here
+                        * anyway to avoid printing a lot of spurious errors. */
+                       ext2fs_group_desc_csum_set(fs, group);
+               }
+               if (fs->flags & EXT2_FLAG_RW)
+                       ext2fs_mark_super_dirty(fs);
+       }
+
+       if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
+           !(flags & EXT2_FLAG_SKIP_MMP) &&
+           (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
+               retval = ext2fs_mmp_start(fs);
+               if (retval) {
+                       fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
+                       ext2fs_mmp_stop(fs);
+                       goto cleanup;
                }
-               ext2fs_mark_super_dirty(fs);
        }
 
        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;
 }
 
 /*
  * Set/get the filesystem data I/O channel.
- * 
+ *
  * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
  */
 errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
@@ -359,10 +483,22 @@ errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
 
 errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
 {
+       errcode_t err;
+
        if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
                return EXT2_ET_NOT_IMAGE_FILE;
+       err = io_channel_set_blksize(new_io, fs->blocksize);
+       if (err)
+               return err;
+       if ((new_io == fs->image_io) || (new_io == fs->io))
+               return 0;
+       if ((fs->image_io != fs->io) &&
+           fs->image_io)
+               io_channel_close(fs->image_io);
+       if (fs->io)
+               io_channel_close(fs->io);
        fs->io = fs->image_io = new_io;
-       fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW | 
+       fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
                EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
        fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
        return 0;