Whamcloud - gitweb
misc: handle very large files with filefrag
[tools/e2fsprogs.git] / misc / mke2fs.c
index 9f18c83..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;
@@ -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,9 +447,9 @@ 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,
@@ -553,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);
@@ -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);
@@ -778,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);
@@ -829,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++;
@@ -856,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++;
@@ -1026,6 +1054,28 @@ static void parse_extended_opts(struct ext2_super_block *param,
                                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;
@@ -1050,6 +1100,8 @@ static void parse_extended_opts(struct ext2_super_block *param,
                        "\ttest_fs\n"
                        "\tdiscard\n"
                        "\tnodiscard\n"
+                       "\tencoding=<encoding>\n"
+                       "\tencoding_flags=<flags>\n"
                        "\tquotatype=<quota type(s) to be enabled>\n\n"),
                        badopt ? badopt : "");
                free(buf);
@@ -1061,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);
 }
 
@@ -1077,11 +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_CSUM_SEED,
+               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|
@@ -1092,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
 };
 
 
@@ -1503,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))
@@ -1869,6 +1940,12 @@ 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);
 
@@ -1892,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"));
@@ -1985,6 +2067,8 @@ 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);
@@ -2010,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 */
@@ -2092,9 +2181,11 @@ profile_error:
        }
        /*
         * Guard against group descriptor count overflowing... Mostly to avoid
-        * strange results for absurdly large devices.
+        * strange results for absurdly large devices.  This is in log2:
+        * (blocksize) * (bits per byte) * (maximum number of block groups)
         */
-       if (fs_blocks_count > ((1ULL << (fs_param.s_log_block_size + 3 + 32)) - 1)) {
+       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"),
@@ -2295,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;
@@ -2319,6 +2437,35 @@ profile_error:
                        (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))
@@ -2419,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;
@@ -2827,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"));
@@ -2862,7 +3012,7 @@ int main (int argc, char *argv[])
        if (ext2fs_has_feature_csum_seed(fs->super) &&
            !ext2fs_has_feature_metadata_csum(fs->super)) {
                printf("%s", _("The metadata_csum_seed feature "
-                              "requres the metadata_csum feature.\n"));
+                              "requires the metadata_csum feature.\n"));
                exit(1);
        }
 
@@ -2914,7 +3064,14 @@ 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);
@@ -2937,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.
@@ -3196,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);
 
@@ -3226,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"))