Whamcloud - gitweb
misc: handle very large files with filefrag
[tools/e2fsprogs.git] / misc / mke2fs.c
index 082a590..27e7d17 100644 (file)
@@ -16,7 +16,7 @@
  * enforced (but it's not much fun on a character device :-).
  */
 
-#define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX */
+#define _XOPEN_SOURCE 600
 
 #include "config.h"
 #include <stdio.h>
@@ -43,7 +43,9 @@ extern int optind;
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
+#ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
+#endif
 #include <libgen.h>
 #include <limits.h>
 #include <blkid/blkid.h>
@@ -93,6 +95,7 @@ int   journal_size;
 int    journal_flags;
 static int     lazy_itable_init;
 static int     packed_meta_blocks;
+int            no_copy_xattrs;
 static char    *bad_blocks_filename = NULL;
 static __u32   fs_stride;
 /* Initialize usr/grp quotas by default */
@@ -103,6 +106,7 @@ static int  proceed_delay = -1;
 static blk64_t dev_size;
 
 static struct ext2_super_block fs_param;
+static __u32 zero_buf[4];
 static char *fs_uuid = NULL;
 static char *creator_os;
 static char *volume_label;
@@ -113,6 +117,8 @@ char **fs_types;
 const char *src_root_dir;  /* Copy files from the specified directory */
 static char *undo_file;
 
+static int android_sparse_file; /* -E android_sparse */
+
 static profile_t       profile;
 
 static int sys_page_size = 4096;
@@ -132,7 +138,7 @@ static void usage(void)
        "[-r fs-revision] [-E extended-option[,...]]\n"
        "\t[-t fs-type] [-T usage-type ] [-U UUID] [-e errors_behavior]"
        "[-z undo_file]\n"
-       "\t[-jnqvDFKSV] device [blocks-count]\n"),
+       "\t[-jnqvDFSV] device [blocks-count]\n"),
                program_name);
        exit(1);
 }
@@ -338,7 +344,7 @@ _("Warning: the backup superblock/group descriptors at block %u contain\n"
                exit(1);
        }
        while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
-               ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
+               ext2fs_mark_block_bitmap2(fs->block_map, blk);
        ext2fs_badblocks_list_iterate_end(bb_iter);
 }
 
@@ -354,9 +360,15 @@ static void write_reserved_inodes(ext2_filsys fs)
                exit(1);
        }
 
-       for (ino = 1; ino < EXT2_FIRST_INO(fs->super); ino++)
-               ext2fs_write_inode_full(fs, ino, inode,
-                                       EXT2_INODE_SIZE(fs->super));
+       for (ino = 1; ino < EXT2_FIRST_INO(fs->super); ino++) {
+               retval = ext2fs_write_inode_full(fs, ino, inode,
+                                                EXT2_INODE_SIZE(fs->super));
+               if (retval) {
+                       com_err("ext2fs_write_inode_full", retval,
+                               _("while writing reserved inodes"));
+                       exit(1);
+               }
+       }
 
        ext2fs_free_mem(&inode);
 }
@@ -435,17 +447,16 @@ static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
                }
                if (sync_kludge) {
                        if (sync_kludge == 1)
-                               sync();
+                               io_channel_flush(fs->io);
                        else if ((i % sync_kludge) == 0)
-                               sync();
+                               io_channel_flush(fs->io);
                }
        }
        ext2fs_numeric_progress_close(fs, &progress,
                                      _("done                            \n"));
 
        /* Reserved inodes must always have correct checksums */
-       if (fs->super->s_creator_os == EXT2_OS_LINUX &&
-           ext2fs_has_feature_metadata_csum(fs->super))
+       if (ext2fs_has_feature_metadata_csum(fs->super))
                write_reserved_inodes(fs);
 }
 
@@ -554,7 +565,7 @@ static void zap_sector(ext2_filsys fs, int sect, int nsect)
        int retval;
        unsigned int *magic;
 
-       buf = malloc(512*nsect);
+       buf = calloc(512, nsect);
        if (!buf) {
                printf(_("Out of memory erasing sectors %d-%d\n"),
                       sect, sect + nsect - 1);
@@ -633,6 +644,7 @@ write_superblock:
        retval = io_channel_write_blk64(fs->io,
                                        fs->super->s_first_data_block+1,
                                        1, buf);
+       (void) ext2fs_free_mem(&buf);
        if (retval) {
                com_err("create_journal_dev", retval, "%s",
                        _("while writing journal superblock"));
@@ -643,7 +655,6 @@ write_superblock:
 static void show_stats(ext2_filsys fs)
 {
        struct ext2_super_block *s = fs->super;
-       char                    buf[80];
         char                    *os;
        blk64_t                 group_block;
        dgrp_t                  i;
@@ -661,9 +672,8 @@ static void show_stats(ext2_filsys fs)
                fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
                       ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
 
-       memset(buf, 0, sizeof(buf));
-       strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
-       printf(_("Filesystem label=%s\n"), buf);
+       printf(_("Filesystem label=%.*s\n"), EXT2_LEN_STR(s->s_volume_name));
+
        os = e2p_os2string(fs->super->s_creator_os);
        if (os)
                printf(_("OS type: %s\n"), os);
@@ -771,23 +781,6 @@ static int set_os(struct ext2_super_block *sb, char *os)
 
 #define PATH_SET "PATH=/sbin"
 
-static int option_handle_function(char *token, void *data)
-{
-       if (!strncmp(token, "usr", 3)) {
-               quotatype_bits |= QUOTA_USR_BIT;
-       } else if (!strncmp(token, "grp", 3)) {
-               quotatype_bits |= QUOTA_GRP_BIT;
-       } else if (!strncmp(token, "prj", 3)) {
-               quotatype_bits |= QUOTA_PRJ_BIT;
-       } else {
-               fprintf(stderr, _("Invalid quotatype parameter: %s\n"),
-                               token);
-               return 1;
-       }
-       return 0;
-
-}
-
 static void parse_extended_opts(struct ext2_super_block *param,
                                const char *opts)
 {
@@ -795,6 +788,8 @@ static void parse_extended_opts(struct ext2_super_block *param,
        int     len;
        int     r_usage = 0;
        int     ret;
+       int     encoding = -1;
+       char    *encoding_flags = NULL;
 
        len = strlen(opts);
        buf = malloc(len+1);
@@ -846,6 +841,19 @@ static void parse_extended_opts(struct ext2_super_block *param,
                                continue;
                        }
                        param->s_desc_size = desc_size;
+               } else if (strcmp(token, "hash_seed") == 0) {
+                       if (!arg) {
+                               r_usage++;
+                               badopt = token;
+                               continue;
+                       }
+                       if (uuid_parse(arg,
+                               (unsigned char *)param->s_hash_seed) != 0) {
+                               fprintf(stderr,
+                                       _("Invalid hash seed: %s\n"), arg);
+                               r_usage++;
+                               continue;
+                       }
                } else if (strcmp(token, "offset") == 0) {
                        if (!arg) {
                                r_usage++;
@@ -873,6 +881,9 @@ static void parse_extended_opts(struct ext2_super_block *param,
                                r_usage++;
                                continue;
                        }
+               } else if (strcmp(token, "no_copy_xattrs") == 0) {
+                       no_copy_xattrs = 1;
+                       continue;
                } else if (strcmp(token, "num_backup_sb") == 0) {
                        if (!arg) {
                                r_usage++;
@@ -1022,17 +1033,49 @@ static void parse_extended_opts(struct ext2_super_block *param,
                } else if (!strcmp(token, "nodiscard")) {
                        discard = 0;
                } else if (!strcmp(token, "quotatype")) {
+                       char *errtok = NULL;
+
                        if (!arg) {
                                r_usage++;
                                badopt = token;
                                continue;
                        }
-                       ret = parse_quota_opts(arg, option_handle_function,
-                                              NULL);
+                       quotatype_bits = 0;
+                       ret = parse_quota_types(arg, &quotatype_bits, &errtok);
                        if (ret) {
+                               if (errtok) {
+                                       fprintf(stderr,
+                               "Failed to parse quota type at %s", errtok);
+                                       free(errtok);
+                               } else
+                                       com_err(program_name, ret,
+                                               "while parsing quota type");
+                               r_usage++;
+                               badopt = token;
+                               continue;
+                       }
+               } else if (!strcmp(token, "android_sparse")) {
+                       android_sparse_file = 1;
+               } else if (!strcmp(token, "encoding")) {
+                       if (!arg) {
+                               r_usage++;
+                               continue;
+                       }
+
+                       encoding = e2p_str2encoding(arg);
+                       if (encoding < 0) {
+                               fprintf(stderr, _("Invalid encoding: %s"), arg);
                                r_usage++;
                                continue;
                        }
+                       param->s_encoding = encoding;
+                       ext2fs_set_feature_casefold(param);
+               } else if (!strcmp(token, "encoding_flags")) {
+                       if (!arg) {
+                               r_usage++;
+                               continue;
+                       }
+                       encoding_flags = arg;
                } else {
                        r_usage++;
                        badopt = token;
@@ -1053,12 +1096,13 @@ static void parse_extended_opts(struct ext2_super_block *param,
                        "\tpacked_meta_blocks=<0 to disable, 1 to enable>\n"
                        "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
                        "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
-                       "\troot_uid=<uid of root directory>\n"
-                       "\troot_gid=<gid of root directory>\n"
+                       "\troot_owner=<uid of root dir>:<gid of root dir>\n"
                        "\ttest_fs\n"
                        "\tdiscard\n"
                        "\tnodiscard\n"
-                       "\tquotatype=<usr OR grp>\n\n"),
+                       "\tencoding=<encoding>\n"
+                       "\tencoding_flags=<flags>\n"
+                       "\tquotatype=<quota type(s) to be enabled>\n\n"),
                        badopt ? badopt : "");
                free(buf);
                exit(1);
@@ -1069,6 +1113,25 @@ static void parse_extended_opts(struct ext2_super_block *param,
                                  "multiple of stride %u.\n\n"),
                        param->s_raid_stripe_width, param->s_raid_stride);
 
+       if (ext2fs_has_feature_casefold(param)) {
+               param->s_encoding_flags =
+                       e2p_get_encoding_flags(param->s_encoding);
+
+               if (encoding_flags &&
+                   e2p_str2encoding_flags(param->s_encoding, encoding_flags,
+                                          &param->s_encoding_flags)) {
+                       fprintf(stderr, _("error: Invalid encoding flag: %s\n"),
+                               encoding_flags);
+                       free(buf);
+                       exit(1);
+               }
+       } else if (encoding_flags) {
+               fprintf(stderr, _("error: An encoding must be explicitly "
+                                 "specified when passing encoding-flags\n"));
+               free(buf);
+               exit(1);
+       }
+
        free(buf);
 }
 
@@ -1085,10 +1148,14 @@ static __u32 ok_features[3] = {
                EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
                EXT2_FEATURE_INCOMPAT_META_BG|
                EXT4_FEATURE_INCOMPAT_FLEX_BG|
+               EXT4_FEATURE_INCOMPAT_EA_INODE|
                EXT4_FEATURE_INCOMPAT_MMP |
                EXT4_FEATURE_INCOMPAT_64BIT|
                EXT4_FEATURE_INCOMPAT_INLINE_DATA|
-               EXT4_FEATURE_INCOMPAT_ENCRYPT,
+               EXT4_FEATURE_INCOMPAT_ENCRYPT |
+               EXT4_FEATURE_INCOMPAT_CASEFOLD |
+               EXT4_FEATURE_INCOMPAT_CSUM_SEED |
+               EXT4_FEATURE_INCOMPAT_LARGEDIR,
        /* R/O compat */
        EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
                EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
@@ -1099,7 +1166,8 @@ static __u32 ok_features[3] = {
                EXT4_FEATURE_RO_COMPAT_BIGALLOC|
                EXT4_FEATURE_RO_COMPAT_QUOTA|
                EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|
-               EXT4_FEATURE_RO_COMPAT_PROJECT
+               EXT4_FEATURE_RO_COMPAT_PROJECT|
+               EXT4_FEATURE_RO_COMPAT_VERITY
 };
 
 
@@ -1146,7 +1214,7 @@ struct str_list {
 static errcode_t init_list(struct str_list *sl)
 {
        sl->num = 0;
-       sl->max = 0;
+       sl->max = 1;
        sl->list = malloc((sl->max+1) * sizeof(char *));
        if (!sl->list)
                return ENOMEM;
@@ -1455,6 +1523,7 @@ static void PRS(int argc, char *argv[])
        int             b, c, flags;
        int             cluster_size = 0;
        char            *tmp, **cpp;
+       int             explicit_fssize = 0;
        int             blocksize = 0;
        int             inode_ratio = 0;
        int             inode_size = 0;
@@ -1487,6 +1556,7 @@ static void PRS(int argc, char *argv[])
        long            sysval;
        int             s_opt = -1, r_opt = -1;
        char            *fs_features = 0;
+       int             fs_features_size = 0;
        int             use_bsize;
        char            *newpath;
        int             pathlen = sizeof(PATH_SET) + 1;
@@ -1508,10 +1578,6 @@ static void PRS(int argc, char *argv[])
        }
        putenv (newpath);
 
-       tmp = getenv("MKE2FS_SYNC");
-       if (tmp)
-               sync_kludge = atoi(tmp);
-
        /* Determine the system page size if possible */
 #ifdef HAVE_SYSCONF
 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
@@ -1704,6 +1770,11 @@ profile_error:
                        break;
                case 'L':
                        volume_label = optarg;
+                       if (strlen(volume_label) > EXT2_LABEL_LEN) {
+                               volume_label[EXT2_LABEL_LEN] = '\0';
+                               fprintf(stderr, _("Warning: label too long; will be truncated to '%s'\n\n"),
+                                       volume_label);
+                       }
                        break;
                case 'm':
                        reserved_ratio = strtod(optarg, &tmp);
@@ -1733,7 +1804,20 @@ profile_error:
                        creator_os = optarg;
                        break;
                case 'O':
-                       fs_features = optarg;
+                       retval = ext2fs_resize_mem(fs_features_size,
+                                  fs_features_size + 1 + strlen(optarg),
+                                                  &fs_features);
+                       if (retval) {
+                               com_err(program_name, retval,
+                                    _("while allocating fs_feature string"));
+                               exit(1);
+                       }
+                       if (fs_features_size)
+                               strcat(fs_features, ",");
+                       else
+                               fs_features[0] = 0;
+                       strcat(fs_features, optarg);
+                       fs_features_size += 1 + strlen(optarg);
                        break;
                case 'q':
                        quiet = 1;
@@ -1856,17 +1940,25 @@ profile_error:
        if (optind < argc)
                usage();
 
+       profile_get_integer(profile, "options", "sync_kludge", 0, 0,
+                           &sync_kludge);
+       tmp = getenv("MKE2FS_SYNC");
+       if (tmp)
+               sync_kludge = atoi(tmp);
+
        profile_get_integer(profile, "options", "proceed_delay", 0, 0,
                            &proceed_delay);
 
        /* The isatty() test is so we don't break existing scripts */
        flags = CREATE_FILE;
-       if (isatty(0) && isatty(1))
+       if (isatty(0) && isatty(1) && !offset)
                flags |= CHECK_FS_EXIST;
        if (!quiet)
                flags |= VERBOSE_CREATE;
        if (fs_blocks_count == 0)
                flags |= NO_SIZE;
+       else
+               explicit_fssize = 1;
        if (!check_plausibility(device_name, flags, &is_device) && !force)
                proceed_question(proceed_delay);
 
@@ -1877,10 +1969,15 @@ profile_error:
                dev_size = fs_blocks_count;
                retval = 0;
        } else
+#ifndef _WIN32
                retval = ext2fs_get_device_size2(device_name,
                                                 EXT2_BLOCK_SIZE(&fs_param),
                                                 &dev_size);
-
+#else
+               retval = ext2fs_get_device_size(device_name,
+                                               EXT2_BLOCK_SIZE(&fs_param),
+                                               &dev_size);
+#endif
        if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
                com_err(program_name, retval, "%s",
                        _("while trying to determine filesystem size"));
@@ -1970,11 +2067,14 @@ profile_error:
                ext2fs_clear_feature_filetype(&fs_param);
                ext2fs_clear_feature_huge_file(&fs_param);
                ext2fs_clear_feature_metadata_csum(&fs_param);
+               ext2fs_clear_feature_ea_inode(&fs_param);
+               ext2fs_clear_feature_casefold(&fs_param);
        }
        edit_feature(fs_features ? fs_features : tmp,
                     &fs_param.s_feature_compat);
        if (tmp)
                free(tmp);
+       (void) ext2fs_free_mem(&fs_features);
        /*
         * If the user specified features incompatible with the Hurd, complain
         */
@@ -1994,6 +2094,11 @@ profile_error:
                                                "metadata_csum feature.\n"));
                        exit(1);
                }
+               if (ext2fs_has_feature_ea_inode(&fs_param)) {
+                       fprintf(stderr, "%s", _("The HURD does not support the "
+                                               "ea_inode feature.\n"));
+                       exit(1);
+               }
        }
 
        /* Get the hardware sector sizes, if available */
@@ -2074,10 +2179,30 @@ profile_error:
                        EXT2_BLOCK_SIZE(&fs_param));
                exit(1);
        }
+       /*
+        * Guard against group descriptor count overflowing... Mostly to avoid
+        * strange results for absurdly large devices.  This is in log2:
+        * (blocksize) * (bits per byte) * (maximum number of block groups)
+        */
+       if (fs_blocks_count >
+           (1ULL << (EXT2_BLOCK_SIZE_BITS(&fs_param) + 3 + 32)) - 1) {
+               fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
+                                 "too big to create\n\t"
+                                 "a filesystem using a blocksize of %d.\n"),
+                       program_name, fs_blocks_count, device_name,
+                       EXT2_BLOCK_SIZE(&fs_param));
+               exit(1);
+       }
 
        ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
 
        if (ext2fs_has_feature_journal_dev(&fs_param)) {
+               int i;
+
+               for (i=0; fs_types[i]; i++) {
+                       free(fs_types[i]);
+                       fs_types[i] = 0;
+               }
                fs_types[0] = strdup("journal");
                fs_types[1] = 0;
        }
@@ -2261,6 +2386,33 @@ profile_error:
        if (packed_meta_blocks)
                journal_location = 0;
 
+       if (ext2fs_has_feature_casefold(&fs_param)) {
+               char *ef, *en = get_string_from_profile(fs_types,
+                                                       "encoding", "utf8");
+               int encoding = e2p_str2encoding(en);
+
+               if (encoding < 0) {
+                       com_err(program_name, 0,
+                               _("Unknown filename encoding from profile: %s"),
+                               en);
+                       exit(1);
+               }
+               free(en);
+               fs_param.s_encoding = encoding;
+               ef = get_string_from_profile(fs_types, "encoding_flags", NULL);
+               if (ef) {
+                       if (e2p_str2encoding_flags(encoding, ef,
+                                       &fs_param.s_encoding_flags) < 0) {
+                               com_err(program_name, 0,
+                       _("Unknown encoding flags from profile: %s"), ef);
+                               exit(1);
+                       }
+                       free(ef);
+               } else
+                       fs_param.s_encoding_flags =
+                               e2p_get_encoding_flags(encoding);
+       }
+
        /* Get options from profile */
        for (cpp = fs_types; *cpp; cpp++) {
                tmp = NULL;
@@ -2273,6 +2425,47 @@ profile_error:
        if (extended_opts)
                parse_extended_opts(&fs_param, extended_opts);
 
+       if (explicit_fssize == 0 && offset > 0) {
+               fs_blocks_count -= offset / EXT2_BLOCK_SIZE(&fs_param);
+               ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
+               fprintf(stderr,
+                       _("\nWarning: offset specified without an "
+                         "explicit file system size.\n"
+                         "Creating a file system with %llu blocks "
+                         "but this might\n"
+                         "not be what you want.\n\n"),
+                       (unsigned long long) fs_blocks_count);
+       }
+
+       if (quotatype_bits & QUOTA_PRJ_BIT)
+               ext2fs_set_feature_project(&fs_param);
+
+       if (ext2fs_has_feature_project(&fs_param)) {
+               quotatype_bits |= QUOTA_PRJ_BIT;
+               if (inode_size == EXT2_GOOD_OLD_INODE_SIZE) {
+                       com_err(program_name, 0,
+                               _("%d byte inodes are too small for "
+                                 "project quota"),
+                               inode_size);
+                       exit(1);
+               }
+               if (inode_size == 0) {
+                       inode_size = get_int_from_profile(fs_types,
+                                                         "inode_size", 0);
+                       if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE*2)
+                               inode_size = EXT2_GOOD_OLD_INODE_SIZE*2;
+               }
+       }
+
+       if (ext2fs_has_feature_casefold(&fs_param) &&
+           ext2fs_has_feature_encrypt(&fs_param)) {
+               com_err(program_name, 0, "%s",
+                       _("The encrypt and casefold features are not "
+                         "compatible.\nThey can not be both enabled "
+                         "simultaneously.\n"));
+                     exit (1);
+       }
+
        /* Don't allow user to set both metadata_csum and uninit_bg bits. */
        if (ext2fs_has_feature_metadata_csum(&fs_param) &&
            ext2fs_has_feature_gdt_csum(&fs_param))
@@ -2373,19 +2566,6 @@ profile_error:
                exit(1);
        }
 
-       /*
-        * If inode size is 128 and project quota is enabled, we need
-        * to notify users that project ID will never be useful.
-        */
-       if (ext2fs_has_feature_project(&fs_param) &&
-           fs_param.s_inode_size == EXT2_GOOD_OLD_INODE_SIZE) {
-               com_err(program_name, 0,
-                       _("%d byte inodes are too small for project quota; "
-                         "specify larger size"),
-                       fs_param.s_inode_size);
-               exit(1);
-       }
-
        /* Make sure number of inodes specified will fit in 32 bits */
        if (num_inodes == 0) {
                unsigned long long n;
@@ -2673,10 +2853,21 @@ static void fix_cluster_bg_counts(ext2_filsys fs)
 static int create_quota_inodes(ext2_filsys fs)
 {
        quota_ctx_t qctx;
+       errcode_t retval;
 
-       quota_init_context(&qctx, fs, QUOTA_ALL_BIT);
+       retval = quota_init_context(&qctx, fs, quotatype_bits);
+       if (retval) {
+               com_err(program_name, retval,
+                       _("while initializing quota context"));
+               exit(1);
+       }
        quota_compute_usage(qctx);
-       quota_write_inode(qctx, quotatype_bits);
+       retval = quota_write_inode(qctx, quotatype_bits);
+       if (retval) {
+               com_err(program_name, retval,
+                       _("while writing quota inodes"));
+               exit(1);
+       }
        quota_release_context(&qctx);
 
        return 0;
@@ -2770,7 +2961,23 @@ int main (int argc, char *argv[])
         */
        if (!quiet)
                flags |= EXT2_FLAG_PRINT_PROGRESS;
-       retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
+       if (android_sparse_file) {
+               char *android_sparse_params = malloc(strlen(device_name) + 48);
+
+               if (!android_sparse_params) {
+                       com_err(program_name, ENOMEM, "%s",
+                               _("in malloc for android_sparse_params"));
+                       exit(1);
+               }
+               sprintf(android_sparse_params, "(%s):%u:%u",
+                        device_name, fs_param.s_blocks_count,
+                        1024 << fs_param.s_log_block_size);
+               retval = ext2fs_initialize(android_sparse_params, flags,
+                                          &fs_param, sparse_io_manager, &fs);
+               free(android_sparse_params);
+       } else
+               retval = ext2fs_initialize(device_name, flags, &fs_param,
+                                          io_ptr, &fs);
        if (retval) {
                com_err(device_name, retval, "%s",
                        _("while setting up superblock"));
@@ -2802,11 +3009,26 @@ int main (int argc, char *argv[])
                                 "Pass -O 64bit to rectify.\n"));
        }
 
+       if (ext2fs_has_feature_csum_seed(fs->super) &&
+           !ext2fs_has_feature_metadata_csum(fs->super)) {
+               printf("%s", _("The metadata_csum_seed feature "
+                              "requires the metadata_csum feature.\n"));
+               exit(1);
+       }
+
        /* Calculate journal blocks */
        if (!journal_device && ((journal_size) ||
            ext2fs_has_feature_journal(&fs_param)))
                journal_blocks = figure_journal_size(journal_size, fs);
 
+       sprintf(opt_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
+               32768 : fs->blocksize * 8);
+       io_channel_set_options(fs->io, opt_string);
+       if (offset) {
+               sprintf(opt_string, "offset=%llu", offset);
+               io_channel_set_options(fs->io, opt_string);
+       }
+
        /* Can't undo discard ... */
        if (!noaction && discard && dev_size && (io_ptr != undo_io_manager)) {
                retval = mke2fs_discard_device(fs);
@@ -2821,14 +3043,6 @@ int main (int argc, char *argv[])
                }
        }
 
-       sprintf(opt_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
-               32768 : fs->blocksize * 8);
-       io_channel_set_options(fs->io, opt_string);
-       if (offset) {
-               sprintf(opt_string, "offset=%llu", offset);
-               io_channel_set_options(fs->io, opt_string);
-       }
-
        if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
                fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
 
@@ -2850,13 +3064,25 @@ int main (int argc, char *argv[])
         * Parse or generate a UUID for the filesystem
         */
        if (fs_uuid) {
-               if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
+               if ((strcasecmp(fs_uuid, "null") == 0) ||
+                   (strcasecmp(fs_uuid, "clear") == 0)) {
+                       uuid_clear(fs->super->s_uuid);
+               } else if (strcasecmp(fs_uuid, "time") == 0) {
+                       uuid_generate_time(fs->super->s_uuid);
+               } else if (strcasecmp(fs_uuid, "random") == 0) {
+                       uuid_generate(fs->super->s_uuid);
+               } else if (uuid_parse(fs_uuid, fs->super->s_uuid) != 0) {
                        com_err(device_name, 0, "could not parse UUID: %s\n",
                                fs_uuid);
                        exit(1);
                }
        } else
                uuid_generate(fs->super->s_uuid);
+
+       if (ext2fs_has_feature_csum_seed(fs->super))
+               fs->super->s_checksum_seed = ext2fs_crc32c_le(~0,
+                               fs->super->s_uuid, sizeof(fs->super->s_uuid));
+
        ext2fs_init_csum_seed(fs);
 
        /*
@@ -2868,7 +3094,13 @@ int main (int argc, char *argv[])
        free(hash_alg_str);
        fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
                EXT2_HASH_HALF_MD4;
-       uuid_generate((unsigned char *) fs->super->s_hash_seed);
+
+       if (memcmp(fs_param.s_hash_seed, zero_buf,
+               sizeof(fs_param.s_hash_seed)) != 0) {
+               memcpy(fs->super->s_hash_seed, fs_param.s_hash_seed,
+                       sizeof(fs->super->s_hash_seed));
+       } else
+               uuid_generate((unsigned char *) fs->super->s_hash_seed);
 
        /*
         * Periodic checks can be enabled/disabled via config file.
@@ -3127,8 +3359,6 @@ no_journal:
 
        if (ext2fs_has_feature_bigalloc(&fs_param))
                fix_cluster_bg_counts(fs);
-       if (ext2fs_has_feature_project(&fs_param))
-               quotatype_bits |= QUOTA_PRJ_BIT;
        if (ext2fs_has_feature_quota(&fs_param))
                create_quota_inodes(fs);
 
@@ -3157,8 +3387,9 @@ no_journal:
        max_mnt_count = fs->super->s_max_mnt_count;
        retval = ext2fs_close_free(&fs);
        if (retval) {
-               fprintf(stderr, "%s",
-                       _("\nWarning, had trouble writing out superblocks."));
+               com_err(program_name, retval, "%s",
+                       _("while writing out and closing file system"));
+               retval = 1;
        } else if (!quiet) {
                printf("%s", _("done\n\n"));
                if (!getenv("MKE2FS_SKIP_CHECK_MSG"))