]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mkfs.minix: check user input carefully
authorSami Kerola <kerolasa@iki.fi>
Wed, 24 Jun 2015 08:15:13 +0000 (09:15 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 30 Jul 2015 09:39:14 +0000 (11:39 +0200)
File name lenght and version input needs to be checked against each
other, which will determine what version of file system is in question.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
disk-utils/mkfs.minix.8
disk-utils/mkfs.minix.c

index e46a76898e6345f26ad685d13710083e370688f4..fcf0e7c56de8b8813621b2fca3d87c1d1e04a21a 100644 (file)
@@ -42,10 +42,9 @@ Check the device for bad blocks before creating the filesystem.  If any
 are found, the count is printed.
 .TP
 \fB\-n\fR, \fB\-\-namelength\fR \fIlength\fR
-Specify the maximum length of filenames.
-Currently, the only allowable values are 14 and 30.
-The default is 30.  Note that kernels older than 0.99p7
-only accept namelength 14.
+Specify the maximum length of filenames.  Currently, the only allowable
+values are 14 and 30 for file system versions 1 and 2.  Version 3 allows
+only value 60.  The default is 30.
 .TP
 \fB\-i\fR, \fB\-\-inodes\fR \fInumber\fR
 Specify the number of inodes for the filesystem.
@@ -57,7 +56,7 @@ The file has one bad-block number per line.  The count of bad blocks read
 is printed.
 .TP
 .B \-1
-Make a Minix version 1 filesystem.
+Make a Minix version 1 filesystem.  This is the default.
 .TP
 .BR \-2 , " \-v"
 Make a Minix version 2 filesystem.
index fcf7f37989d1ed70175bef5dc75dd9d5e6d772bb..59e1674ec913dac4243f39d7c039d71b4c91b4fc 100644 (file)
 
 #define MINIX_MAX_INODES 65535
 
+#define DEFAULT_FS_VERSION 1
+
 /*
  * Global variables used in minix_programs.h inline fuctions
  */
-int fs_version = 1;
+int fs_version = DEFAULT_FS_VERSION;
 char *super_block_buffer;
 
 static char *inode_buffer = NULL;
@@ -104,7 +106,7 @@ struct fs_control {
        unsigned long long fs_blocks;   /* device block count for the file system */
        int fs_used_blocks;             /* used blocks on a device */
        int fs_bad_blocks;              /* number of bad blocks found from device */
-       size_t fs_namelen;              /* maximum length of filenames */
+       uint16_t fs_namelen;            /* maximum length of filenames */
        size_t fs_dirsize;              /* maximum size of directory */
        unsigned long fs_inodes;        /* number of inodes */
        int fs_magic;                   /* file system magic number */
@@ -632,12 +634,54 @@ static void get_list_blocks(struct fs_control *ctl, char *filename) {
                printf(P_("%d bad block\n", "%d bad blocks\n", ctl->fs_bad_blocks), ctl->fs_bad_blocks);
 }
 
+static int find_super_magic(const struct fs_control *ctl)
+{
+       switch (fs_version) {
+       case 1:
+               if (ctl->fs_namelen == 14)
+                       return MINIX_SUPER_MAGIC;
+               else
+                       return MINIX_SUPER_MAGIC2;
+               break;
+       case 2:
+               if (ctl->fs_namelen == 14)
+                       return MINIX2_SUPER_MAGIC;
+               else
+                       return MINIX2_SUPER_MAGIC2;
+               break;
+       case 3:
+               return MINIX3_SUPER_MAGIC;
+       default:
+               abort();
+       }
+}
+
+static void check_user_instructions(struct fs_control *ctl)
+{
+       switch (fs_version) {
+       case 1:
+       case 2:
+               if (ctl->fs_namelen == 14 || ctl->fs_namelen == 30)
+                       ctl->fs_dirsize = ctl->fs_namelen + 2;
+               else
+                       errx(MKFS_EX_ERROR, _("unsupported name length: %d"), ctl->fs_namelen);
+               break;
+       case 3:
+               if (ctl->fs_namelen == 60)
+                       ctl->fs_dirsize = ctl->fs_namelen + 4;
+               else
+                       errx(MKFS_EX_ERROR, _("unsupported name length: %d"), ctl->fs_namelen);
+               break;
+       default:
+               errx(MKFS_EX_ERROR, _("unsupported minix file system version: %d"), fs_version);
+       }
+       ctl->fs_magic = find_super_magic(ctl);
+}
+
 int main(int argc, char ** argv)
 {
        struct fs_control ctl = {
-               .fs_namelen = 30,
-               .fs_dirsize = 32,
-               .fs_magic = MINIX_SUPER_MAGIC2,         /* version 1, long names */
+               .fs_namelen = 30,       /* keep in sync with DEFAULT_FS_VERSION */
                0
        };
        int i;
@@ -672,19 +716,10 @@ int main(int argc, char ** argv)
                case '3':
                        fs_version = 3;
                        ctl.fs_namelen = 60;
-                       ctl.fs_dirsize = 64;
                        break;
                case 'n':
-                       i = strtoul_or_err(optarg,
+                       ctl.fs_namelen = strtou16_or_err(optarg,
                                        _("failed to parse maximum length of filenames"));
-                       if (i == 14)
-                               ctl.fs_magic = MINIX_SUPER_MAGIC;
-                       else if (i == 30)
-                               ctl.fs_magic = MINIX_SUPER_MAGIC2;
-                       else
-                               usage(stderr);
-                       ctl.fs_namelen = i;
-                       ctl.fs_dirsize = i + 2;
                        break;
                case 'i':
                        ctl.fs_inodes = strtoul_or_err(optarg,
@@ -717,6 +752,7 @@ int main(int argc, char ** argv)
        if (!ctl.device_name) {
                usage(stderr);
        }
+       check_user_instructions(&ctl);
        if (is_mounted(ctl.device_name))
                errx(MKFS_EX_ERROR, _("%s is mounted; will not make a filesystem here!"),
                        ctl.device_name);
@@ -774,17 +810,8 @@ int main(int argc, char ** argv)
        }
        if (ctl.fs_blocks < 10)
                errx(MKFS_EX_ERROR, _("%s: number of blocks too small"), ctl.device_name);
-
-       if (fs_version == 3)
-               ctl.fs_magic = MINIX3_SUPER_MAGIC;
-       else if (fs_version == 2) {
-               if (ctl.fs_namelen == 14)
-                       ctl.fs_magic = MINIX2_SUPER_MAGIC;
-               else
-                       ctl.fs_magic = MINIX2_SUPER_MAGIC2;
-       } else /* fs_version == 1 */
-               if (ctl.fs_blocks > MINIX_MAX_INODES)
-                       ctl.fs_blocks = MINIX_MAX_INODES;
+       if (fs_version == 1 && ctl.fs_blocks > MINIX_MAX_INODES)
+               ctl.fs_blocks = MINIX_MAX_INODES;
        if (ctl.fs_blocks > MINIX_MAX_INODES * BITS_PER_BLOCK)
                ctl.fs_blocks = MINIX_MAX_INODES * BITS_PER_BLOCK;      /* Utter maximum: Clip. */
        setup_tables(&ctl);