Whamcloud - gitweb
LU-4017 e2fsprogs: add project quota support
[tools/e2fsprogs.git] / misc / mke2fs.c
index d5e16c0..53c30ce 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
@@ -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,54 +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,
-                              unsigned int rev)
-{
-       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, rev);
-}
-#else
-static int is_before_linux_ver(unsigned int major, unsigned int minor,
-                              unsigned int rev)
-{
-       return 0;
-}
-#endif
-
 /*
  * Helper function for read_bb_file and test_disk
  */
@@ -729,12 +678,30 @@ 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)
 {
        char    *buf, *token, *next, *p, *arg, *badopt = 0;
        int     len;
        int     r_usage = 0;
+       int     ret;
 
        len = strlen(opts);
        buf = malloc(len+1);
@@ -969,14 +936,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;
                        }
@@ -1032,7 +994,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|
@@ -1044,6 +1008,7 @@ static __u32 ok_features[3] = {
                EXT4_FEATURE_RO_COMPAT_BIGALLOC|
 #ifdef CONFIG_QUOTA
                EXT4_FEATURE_RO_COMPAT_QUOTA|
+               EXT4_FEATURE_RO_COMPAT_PROJECT|
 #endif
                0
 };
@@ -1310,6 +1275,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)
 {
@@ -1487,7 +1464,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, 0))
+       if (ext2fs_is_before_linux_ver(2, 2, 0))
                fs_param.s_rev_level = 0;
 
        if (argc && *argv) {
@@ -1572,6 +1549,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);
@@ -1778,6 +1761,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);
 
@@ -1853,7 +1838,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);
 
@@ -1911,7 +1896,8 @@ profile_error:
 
                if (use_bsize == -1) {
                        use_bsize = sys_page_size;
-                       if (is_before_linux_ver(2, 6, 0) && 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)
@@ -2110,7 +2096,7 @@ profile_error:
         * On newer kernels we do have lazy_itable_init support. So pick the
         * right default in case ext4 module is not loaded.
         */
-       if (is_before_linux_ver(2, 6, 37))
+       if (ext2fs_is_before_linux_ver(2, 6, 37))
                lazy_itable_init = 0;
        else
                lazy_itable_init = 1;
@@ -2211,8 +2197,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)) {
@@ -2237,6 +2223,20 @@ profile_error:
                fs_param.s_inode_size = inode_size;
        }
 
+       /*
+        * If inode size is 128 and project quota is enabled, we need
+        * to notify users that project ID will never be useful.
+        */
+       if (fs_param.s_feature_incompat & EXT4_FEATURE_RO_COMPAT_QUOTA &&
+           fs_param.s_feature_incompat & EXT4_FEATURE_RO_COMPAT_PROJECT &&
+           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;
@@ -2506,9 +2506,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;
@@ -2892,10 +2892,13 @@ no_journal:
                                       EXT4_FEATURE_RO_COMPAT_BIGALLOC))
                fix_cluster_bg_counts(fs);
        if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
+                                      EXT4_FEATURE_RO_COMPAT_PROJECT))
+               quotatype_bits |= QUOTA_PRJ_BIT;
+       if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
                                       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");