Whamcloud - gitweb
libext2fs: make sure ismounted.c explicitly pulls in <sys/types.h>
[tools/e2fsprogs.git] / lib / ext2fs / initialize.c
index 5a6f8ea..36c94a9 100644 (file)
@@ -101,6 +101,7 @@ errcode_t ext2fs_initialize(const char *name, int flags,
        unsigned        reserved_inos;
        char            *buf = 0;
        char            c;
+       double          reserved_ratio;
 
        if (!param || !ext2fs_blocks_count(param))
                return EXT2_ET_INVALID_ARGUMENT;
@@ -172,6 +173,8 @@ errcode_t ext2fs_initialize(const char *name, int flags,
        set_field(s_raid_stripe_width, 0);      /* default stripe width: 0 */
        set_field(s_log_groups_per_flex, 0);
        set_field(s_flags, 0);
+       assign_field(s_backup_bgs[0]);
+       assign_field(s_backup_bgs[1]);
        if (super->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
                retval = EXT2_ET_UNSUPP_FEATURE;
                goto cleanup;
@@ -206,6 +209,8 @@ errcode_t ext2fs_initialize(const char *name, int flags,
                super->s_log_block_size;
 
        if (bigalloc_flag) {
+               unsigned long long bpg;
+
                if (param->s_blocks_per_group &&
                    param->s_clusters_per_group &&
                    ((param->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs)) !=
@@ -219,12 +224,19 @@ errcode_t ext2fs_initialize(const char *name, int flags,
                        super->s_clusters_per_group = 
                                param->s_blocks_per_group /
                                EXT2FS_CLUSTER_RATIO(fs);
-               else
+               else if (super->s_log_cluster_size + 15 < 32)
                        super->s_clusters_per_group = fs->blocksize * 8;
+               else
+                       super->s_clusters_per_group = (fs->blocksize - 1) * 8;
                if (super->s_clusters_per_group > EXT2_MAX_CLUSTERS_PER_GROUP(super))
                        super->s_clusters_per_group = EXT2_MAX_CLUSTERS_PER_GROUP(super);
-               super->s_blocks_per_group = EXT2FS_C2B(fs,
-                                      super->s_clusters_per_group);
+               bpg = EXT2FS_C2B(fs,
+                       (unsigned long long) super->s_clusters_per_group);
+               if (bpg >= (((unsigned long long) 1) << 32)) {
+                       retval = EXT2_ET_INVALID_ARGUMENT;
+                       goto cleanup;
+               }
+               super->s_blocks_per_group = bpg;
        } else {
                set_field(s_blocks_per_group, fs->blocksize * 8);
                if (super->s_blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(super))
@@ -240,6 +252,8 @@ errcode_t ext2fs_initialize(const char *name, int flags,
                goto cleanup;
        }
 
+       set_field(s_mmp_update_interval, 0);
+
        /*
         * If we're creating an external journal device, we don't need
         * to bother with the rest.
@@ -260,8 +274,9 @@ retry:
                goto cleanup;
        }
 
-       if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
-               super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
+       set_field(s_desc_size,
+                 super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
+                 EXT2_MIN_DESC_SIZE_64BIT : 0);
 
        fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
                                          EXT2_DESC_PER_BLOCK(super));
@@ -391,6 +406,14 @@ ipg_retry:
        if (rem && (rem < overhead+50)) {
                ext2fs_blocks_count_set(super, ext2fs_blocks_count(super) -
                                        rem);
+               /*
+                * If blocks count is changed, we need to recalculate
+                * reserved blocks count not to exceed 50%.
+                */
+               reserved_ratio = 100.0 * ext2fs_r_blocks_count(param) /
+                       ext2fs_blocks_count(param);
+               ext2fs_r_blocks_count_set(super, reserved_ratio *
+                       ext2fs_blocks_count(super) / 100.0);
 
                goto retry;
        }
@@ -401,6 +424,21 @@ ipg_retry:
         * count.
         */
 
+       /* Set up the locations of the backup superblocks */
+       if (super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
+               if (super->s_backup_bgs[0] >= fs->group_desc_count)
+                       super->s_backup_bgs[0] = fs->group_desc_count - 1;
+               if (super->s_backup_bgs[1] >= fs->group_desc_count)
+                       super->s_backup_bgs[1] = fs->group_desc_count - 1;
+               if (super->s_backup_bgs[0] == super->s_backup_bgs[1])
+                       super->s_backup_bgs[1] = 0;
+               if (super->s_backup_bgs[0] > super->s_backup_bgs[1]) {
+                       __u32 t = super->s_backup_bgs[0];
+                       super->s_backup_bgs[0] = super->s_backup_bgs[1];
+                       super->s_backup_bgs[1] = t;
+               }
+       }
+
        retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
        if (retval)
                goto cleanup;