From a719ab7780eb524ce96cb845637b692fc5ce81c8 Mon Sep 17 00:00:00 2001 From: Erik van der Kouwe Date: Tue, 3 Aug 2010 06:28:58 +0000 Subject: [PATCH] Auto-detect ext2 partitions in mount --- commands/mount/mount.c | 26 +++++++++++++++++--------- commands/printroot/printroot.c | 15 +++++++-------- include/minix/minlib.h | 6 ++++++ lib/libc/other/fsversion.c | 27 +++++++++++++++++++-------- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/commands/mount/mount.c b/commands/mount/mount.c index 38c2dd77c..0d6060b2f 100644 --- a/commands/mount/mount.c +++ b/commands/mount/mount.c @@ -61,6 +61,17 @@ char *argv[]; device = argv[1]; if (!strcmp(device, "none")) device = NULL; + /* auto-detect type */ + v = fsversion(argv[1], "mount"); + if (type == NULL) { + switch (v) { + case FSVERSION_MFS1: + case FSVERSION_MFS2: + case FSVERSION_MFS3: type = "mfs"; break; + case FSVERSION_EXT2: type = "ext2"; break; + } + } + if (mount(device, argv[2], mountflags, type, args) < 0) { err = strerror(errno); fprintf(stderr, "mount: Can't mount %s on %s: %s\n", @@ -88,15 +99,12 @@ char *argv[]; } /* For MFS, use a version number. Otherwise, use the FS type name. */ if (type == NULL || !strcmp(type, MINIX_FS_TYPE)) { - v = fsversion(argv[1], "mount"); - if (v == 1) - vs = "1"; - else if (v == 2) - vs = "2"; - else if (v == 3) - vs = "3"; - else - vs = "0"; + switch (v) { + case FSVERSION_MFS1: vs = "1"; break; + case FSVERSION_MFS2: vs = "2"; break; + case FSVERSION_MFS3: vs = "3"; break; + default: vs = "0"; break; + } } else { /* Keep the version field sufficiently short. */ if (strlen(type) < sizeof(version)) diff --git a/commands/printroot/printroot.c b/commands/printroot/printroot.c index d9ad00c8f..b00fc44a8 100644 --- a/commands/printroot/printroot.c +++ b/commands/printroot/printroot.c @@ -73,13 +73,12 @@ int status; } write(1, MESSAGE, sizeof MESSAGE - 1); v = fsversion(name, "printroot"); /* determine file system version */ - if (v == 1) - write(1, "1 rw\n", 5); - else if (v == 2) - write(1, "2 rw\n", 5); - else if (v == 3) - write(1, "3 rw\n", 5); - else - write(1, "0 rw\n", 5); + switch (v) { + case FSVERSION_MFS1: write(1, "1 rw\n", 5); break; + case FSVERSION_MFS2: write(1, "2 rw\n", 5); break; + case FSVERSION_MFS3: write(1, "3 rw\n", 5); break; + case FSVERSION_EXT2: write(1, "ext2 rw\n", 8); break; + default: write(1, "0 rw\n", 5); break; + } exit(status); } diff --git a/include/minix/minlib.h b/include/minix/minlib.h index 9c9c3187e..b123e5f7d 100644 --- a/include/minix/minlib.h +++ b/include/minix/minlib.h @@ -21,4 +21,10 @@ _PROTOTYPE(int rewrite_mtab, (char *_prog_name)); _PROTOTYPE(int get_mtab_entry, (char *_s1, char *_s2, char *_s3, char *_s4)); _PROTOTYPE(int put_mtab_entry, (char *_s1, char *_s2, char *_s3, char *_s4)); +/* return values for fsversion */ +#define FSVERSION_MFS1 0x00001 +#define FSVERSION_MFS2 0x00002 +#define FSVERSION_MFS3 0x00003 +#define FSVERSION_EXT2 0x10002 + #endif diff --git a/lib/libc/other/fsversion.c b/lib/libc/other/fsversion.c index 3a0faa59f..49418995b 100644 --- a/lib/libc/other/fsversion.c +++ b/lib/libc/other/fsversion.c @@ -16,12 +16,20 @@ #include #include #include +#include #include "mfs/const.h" -#include "mfs/type.h" -#include "mfs/super.h" -static struct super_block super, *sp; +static char super[SUPER_BLOCK_BYTES]; + +#define MAGIC_OFFSET_MFS 0x18 +#define MAGIC_OFFSET_EXT 0x38 +#define MAGIC_VALUE_EXT2 0xef53 + +static int check_super(off_t offset, unsigned short magic) +{ + return (memcmp(super + offset, &magic, sizeof(magic)) == 0) ? 1 : 0; +} int fsversion(dev, prog) char *dev, *prog; @@ -36,7 +44,7 @@ char *dev, *prog; } lseek(fd, (off_t) SUPER_BLOCK_BYTES, SEEK_SET); /* skip boot block */ - if (read(fd, (char *) &super, (unsigned) SUPER_SIZE) != SUPER_SIZE) { + if (read(fd, (char *) &super, sizeof(super)) != sizeof(super)) { std_err(prog); std_err(" cannot read super block on "); perror(dev); @@ -44,9 +52,12 @@ char *dev, *prog; return(-1); } close(fd); - sp = &super; - if (sp->s_magic == SUPER_MAGIC) return(1); - if (sp->s_magic == SUPER_V2) return(2); - if (sp->s_magic == SUPER_V3) return(3); + + /* first check MFS, a valid MFS may look like EXT but not vice versa */ + if (check_super(MAGIC_OFFSET_MFS, SUPER_MAGIC)) return FSVERSION_MFS1; + if (check_super(MAGIC_OFFSET_MFS, SUPER_V2)) return FSVERSION_MFS2; + if (check_super(MAGIC_OFFSET_MFS, SUPER_V3)) return FSVERSION_MFS3; + if (check_super(MAGIC_OFFSET_EXT, MAGIC_VALUE_EXT2)) return FSVERSION_EXT2; + return(-1); }