Whamcloud - gitweb
LU-4017 e2fsprogs: clean up codes for adding new quota type
[tools/e2fsprogs.git] / misc / mke2fs.c
index 51532ef..0e4d493 100644 (file)
 #include <fcntl.h>
 #include <ctype.h>
 #include <time.h>
-#ifdef __linux__
-#include <sys/utsname.h>
-#include <linux/version.h>
-#endif
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
 #else
@@ -62,7 +58,7 @@ extern int optind;
 #include "prof_err.h"
 #include "../version.h"
 #include "nls-enable.h"
-#include "quota/mkquota.h"
+#include "quota/quotaio.h"
 #include "mke2fs.h"
 
 #define STRIDE_LENGTH 8
@@ -99,7 +95,8 @@ static int    lazy_itable_init;
 static int     packed_meta_blocks;
 static char    *bad_blocks_filename = NULL;
 static __u32   fs_stride;
-static int     quotatype = -1;  /* Initialize both user and group quotas by default */
+/* Initialize usr/grp quotas by default */
+static unsigned int quotatype_bits = (QUOTA_USR_BIT | QUOTA_GRP_BIT);
 static __u64   offset;
 static blk64_t journal_location = ~0LL;
 static int     proceed_delay = -1;
@@ -155,52 +152,6 @@ int int_log10(unsigned long long arg)
        return l;
 }
 
-#ifdef __linux__
-static int parse_version_number(const char *s)
-{
-       int     major, minor, rev;
-       char    *endptr;
-       const char *cp = s;
-
-       if (!s)
-               return 0;
-       major = strtol(cp, &endptr, 10);
-       if (cp == endptr || *endptr != '.')
-               return 0;
-       cp = endptr + 1;
-       minor = strtol(cp, &endptr, 10);
-       if (cp == endptr || *endptr != '.')
-               return 0;
-       cp = endptr + 1;
-       rev = strtol(cp, &endptr, 10);
-       if (cp == endptr)
-               return 0;
-       return KERNEL_VERSION(major, minor, rev);
-}
-
-static int is_before_linux_ver(unsigned int major, unsigned int minor)
-{
-       struct          utsname ut;
-       static int      linux_version_code = -1;
-
-       if (uname(&ut)) {
-               perror("uname");
-               exit(1);
-       }
-       if (linux_version_code < 0)
-               linux_version_code = parse_version_number(ut.release);
-       if (linux_version_code == 0)
-               return 0;
-
-       return linux_version_code < KERNEL_VERSION(major, minor, 0);
-}
-#else
-static int is_before_linux_ver(unsigned int major, unsigned int minor)
-{
-       return 0;
-}
-#endif
-
 /*
  * Helper function for read_bb_file and test_disk
  */
@@ -367,6 +318,7 @@ static errcode_t packed_allocate_tables(ext2_filsys fs)
                ext2fs_block_alloc_stats_range(fs, goal,
                                               fs->inode_blocks_per_group, +1);
                ext2fs_inode_table_loc_set(fs, i, goal);
+               ext2fs_group_desc_csum_set(fs, i);
        }
        return 0;
 }
@@ -726,12 +678,28 @@ 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 {
+               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)
 {
        char    *buf, *token, *next, *p, *arg, *badopt = 0;
        int     len;
        int     r_usage = 0;
+       int     ret;
 
        len = strlen(opts);
        buf = malloc(len+1);
@@ -821,7 +789,7 @@ static void parse_extended_opts(struct ext2_super_block *param,
                        if (*p || num_backups > 2) {
                                fprintf(stderr,
                                        _("Invalid # of backup "
-                                         "superbocks: %s\n"),
+                                         "superblocks: %s\n"),
                                        arg);
                                r_usage++;
                                continue;
@@ -966,14 +934,9 @@ static void parse_extended_opts(struct ext2_super_block *param,
                                badopt = token;
                                continue;
                        }
-                       if (!strncmp(arg, "usr", 3)) {
-                               quotatype = 0;
-                       } else if (!strncmp(arg, "grp", 3)) {
-                               quotatype = 1;
-                       } else {
-                               fprintf(stderr,
-                                       _("Invalid quotatype parameter: %s\n"),
-                                       arg);
+                       ret = parse_quota_opts(arg, option_handle_function,
+                                              NULL);
+                       if (ret) {
                                r_usage++;
                                continue;
                        }
@@ -1029,7 +992,9 @@ 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_DIRDATA|
                EXT4_FEATURE_INCOMPAT_64BIT,
        /* R/O compat */
        EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
@@ -1307,6 +1272,18 @@ int get_int_from_profile(char **types, const char *opt, int def_val)
        return ret;
 }
 
+static unsigned int get_uint_from_profile(char **types, const char *opt,
+                                       unsigned int def_val)
+{
+       unsigned int ret;
+       char **cpp;
+
+       profile_get_uint(profile, "defaults", opt, 0, def_val, &ret);
+       for (cpp = types; *cpp; cpp++)
+               profile_get_uint(profile, "fs_types", *cpp, opt, ret, &ret);
+       return ret;
+}
+
 static double get_double_from_profile(char **types, const char *opt,
                                      double def_val)
 {
@@ -1484,7 +1461,7 @@ profile_error:
        memset(&fs_param, 0, sizeof(struct ext2_super_block));
        fs_param.s_rev_level = 1;  /* Create revision 1 filesystems now */
 
-       if (is_before_linux_ver(2, 2))
+       if (ext2fs_is_before_linux_ver(2, 2, 0))
                fs_param.s_rev_level = 0;
 
        if (argc && *argv) {
@@ -1569,6 +1546,12 @@ profile_error:
                                        _("flex_bg size must be a power of 2"));
                                exit(1);
                        }
+                       if (flex_bg_size > MAX_32_NUM) {
+                               com_err(program_name, 0,
+                               _("flex_bg size (%lu) must be less than"
+                               " or equal to 2^31"), flex_bg_size);
+                               exit(1);
+                       }
                        break;
                case 'i':
                        inode_ratio = strtoul(optarg, &tmp, 0);
@@ -1658,6 +1641,11 @@ profile_error:
                                        _("bad revision level - %s"), optarg);
                                exit(1);
                        }
+                       if (r_opt > EXT2_MAX_SUPP_REV) {
+                               com_err(program_name, EXT2_ET_REV_TOO_HIGH,
+                                       _("while trying to create revision %d"), r_opt);
+                               exit(1);
+                       }
                        fs_param.s_rev_level = r_opt;
                        break;
                case 's':       /* deprecated */
@@ -1745,7 +1733,7 @@ profile_error:
                printf(_("Using journal device's blocksize: %d\n"), blocksize);
                fs_param.s_log_block_size =
                        int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
-               ext2fs_close(jfs);
+               ext2fs_close_free(&jfs);
        }
 
        if (optind < argc) {
@@ -1770,6 +1758,8 @@ profile_error:
                flags |= CHECK_FS_EXIST;
        if (!quiet)
                flags |= VERBOSE_CREATE;
+       if (fs_blocks_count == 0)
+               flags |= NO_SIZE;
        if (!check_plausibility(device_name, flags, &is_device) && !force)
                proceed_question(proceed_delay);
 
@@ -1845,7 +1835,7 @@ profile_error:
        tmp = NULL;
        if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
                tmp = get_string_from_profile(fs_types, "base_features",
-                     "sparse_super,filetype,resize_inode,dir_index");
+                     "sparse_super,large_file,filetype,resize_inode,dir_index");
                edit_feature(tmp, &fs_param.s_feature_compat);
                free(tmp);
 
@@ -1903,7 +1893,8 @@ profile_error:
 
                if (use_bsize == -1) {
                        use_bsize = sys_page_size;
-                       if (is_before_linux_ver(2, 6) && use_bsize > 4096)
+                       if (ext2fs_is_before_linux_ver(2, 6, 0) &&
+                           use_bsize > 4096)
                                use_bsize = 4096;
                }
                if (lsector_size && use_bsize < lsector_size)
@@ -2098,7 +2089,15 @@ profile_error:
                        blocksize, sys_page_size);
        }
 
-       lazy_itable_init = 0;
+       /*
+        * On newer kernels we do have lazy_itable_init support. So pick the
+        * right default in case ext4 module is not loaded.
+        */
+       if (ext2fs_is_before_linux_ver(2, 6, 37))
+               lazy_itable_init = 0;
+       else
+               lazy_itable_init = 1;
+
        if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
                lazy_itable_init = 1;
 
@@ -2162,13 +2161,6 @@ profile_error:
                                  "See https://ext4.wiki.kernel.org/"
                                  "index.php/Bigalloc for more information\n\n"));
 
-       if (!quiet &&
-           (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
-               fprintf(stderr, "%s", _("\nWarning: the quota feature is "
-                                 "still under development\n"
-                                 "See https://ext4.wiki.kernel.org/"
-                                 "index.php/Quota for more information\n\n"));
-
        /* Since sparse_super is the default, we would only have a problem
         * here if it was explicitly disabled.
         */
@@ -2202,8 +2194,8 @@ profile_error:
                inode_size = get_int_from_profile(fs_types, "inode_size", 0);
        if (!flex_bg_size && (fs_param.s_feature_incompat &
                              EXT4_FEATURE_INCOMPAT_FLEX_BG))
-               flex_bg_size = get_int_from_profile(fs_types,
-                                                   "flex_bg_size", 16);
+               flex_bg_size = get_uint_from_profile(fs_types,
+                                                    "flex_bg_size", 16);
        if (flex_bg_size) {
                if (!(fs_param.s_feature_incompat &
                      EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
@@ -2497,9 +2489,9 @@ static int create_quota_inodes(ext2_filsys fs)
 {
        quota_ctx_t qctx;
 
-       quota_init_context(&qctx, fs, -1);
+       quota_init_context(&qctx, fs, QUOTA_ALL_BIT);
        quota_compute_usage(qctx);
-       quota_write_inode(qctx, quotatype);
+       quota_write_inode(qctx, quotatype_bits);
        quota_release_context(&qctx);
 
        return 0;
@@ -2709,7 +2701,7 @@ int main (int argc, char *argv[])
        if (fs->super->s_feature_incompat &
            EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
                create_journal_dev(fs);
-               exit(ext2fs_close(fs) ? 1 : 0);
+               exit(ext2fs_close_free(&fs) ? 1 : 0);
        }
 
        if (bad_blocks_filename)
@@ -2831,7 +2823,7 @@ int main (int argc, char *argv[])
                }
                if (!quiet)
                        printf("%s", _("done\n"));
-               ext2fs_close(jfs);
+               ext2fs_close_free(&jfs);
                free(journal_device);
        } else if ((journal_size) ||
                   (fs_param.s_feature_compat &
@@ -2886,7 +2878,7 @@ no_journal:
                                       EXT4_FEATURE_RO_COMPAT_QUOTA))
                create_quota_inodes(fs);
 
-       retval = mk_hugefiles(fs);
+       retval = mk_hugefiles(fs, device_name);
        if (retval)
                com_err(program_name, retval, "while creating huge files");
 
@@ -2895,7 +2887,7 @@ no_journal:
                       "filesystem accounting information: "));
        checkinterval = fs->super->s_checkinterval;
        max_mnt_count = fs->super->s_max_mnt_count;
-       retval = ext2fs_close(fs);
+       retval = ext2fs_close_free(&fs);
        if (retval) {
                fprintf(stderr, "%s",
                        _("\nWarning, had trouble writing out superblocks."));