aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlfredo Dal'Ava Junior <alfredo@FreeBSD.org>2022-12-17 02:54:39 +0000
committerAlfredo Dal'Ava Junior <alfredo@FreeBSD.org>2022-12-20 03:20:11 +0000
commitb13110e9f3cad590abb86519e313eb1449ddc2e7 (patch)
treeed41403807f88877384fb4a71fa6c490a84b0080
parente7815784cc28e923892ddcb30a1084870abd77ad (diff)
downloadsrc-b13110e9f3cad590abb86519e313eb1449ddc2e7.tar.gz
src-b13110e9f3cad590abb86519e313eb1449ddc2e7.zip
ufs/ffs: detect endian mismatch between machine and filesystem
Mount on a LE machine a filesystem formatted for BE is not supported currently. This adds a check for the superblock magic number using swapped bytes to guess and warn the user that it may be a valid superblock but endian is incompatible. MFC after: 2 weeks Reviewed by: mckusick Obtained from: mckusick, alfredo Differential Revision: https://reviews.freebsd.org/D37675
-rw-r--r--sys/kern/vfs_mountroot.c2
-rw-r--r--sys/ufs/ffs/ffs_subr.c26
2 files changed, 25 insertions, 3 deletions
diff --git a/sys/kern/vfs_mountroot.c b/sys/kern/vfs_mountroot.c
index 5361f41276a0..3c506378ad2f 100644
--- a/sys/kern/vfs_mountroot.c
+++ b/sys/kern/vfs_mountroot.c
@@ -794,7 +794,7 @@ parse_mount(char **conf)
ma = parse_mountroot_options(ma, opts);
error = kernel_mount(ma, MNT_ROOTFS);
- if (error == 0 || timeout <= 0)
+ if (error == 0 || error == EILSEQ || timeout <= 0)
break;
if (root_mount_timeout * hz == timeout ||
diff --git a/sys/ufs/ffs/ffs_subr.c b/sys/ufs/ffs/ffs_subr.c
index 705f8c9c961d..31f30ba5d175 100644
--- a/sys/ufs/ffs/ffs_subr.c
+++ b/sys/ufs/ffs/ffs_subr.c
@@ -35,6 +35,7 @@
__FBSDID("$FreeBSD$");
#include <sys/param.h>
+#include <sys/endian.h>
#include <sys/limits.h>
#ifndef _KERNEL
@@ -144,6 +145,7 @@ static int validate_sblock(struct fs *, int);
* EIO: non-existent or truncated superblock.
* EIO: error reading summary information.
* ENOENT: no usable known superblock found.
+ * EILSEQ: filesystem with wrong byte order found.
* ENOMEM: failed to allocate space for the superblock.
* EINVAL: The previous newfs operation on this volume did not complete.
* The administrator must complete newfs before using this volume.
@@ -383,6 +385,17 @@ validate_sblock(struct fs *fs, int flags)
warnerr = (flags & UFS_NOWARNFAIL) == UFS_NOWARNFAIL ? 0 : ENOENT;
wmsg = warnerr ? "" : " (Ignored)";
/*
+ * Check for endian mismatch between machine and filesystem.
+ */
+ if (((fs->fs_magic != FS_UFS2_MAGIC) &&
+ (bswap32(fs->fs_magic) == FS_UFS2_MAGIC)) ||
+ ((fs->fs_magic != FS_UFS1_MAGIC) &&
+ (bswap32(fs->fs_magic) == FS_UFS1_MAGIC))) {
+ MPRINT("UFS superblock failed due to endian mismatch "
+ "between machine and filesystem\n");
+ return(EILSEQ);
+ }
+ /*
* If just validating for recovery, then do just the minimal
* checks needed for the superblock fields needed to find
* alternate superblocks.
@@ -627,8 +640,16 @@ ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags,
* failure can be avoided.
*/
flags = UFS_NOMSG | nocsum;
- if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) == 0)
- return (0);
+ error = ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc);
+ /*
+ * If successful or endian error, no need to try further.
+ */
+ if (error == 0 || error == EILSEQ) {
+ if (msg && error == EILSEQ)
+ printf("UFS superblock failed due to endian mismatch "
+ "between machine and filesystem\n");
+ return (error);
+ }
/*
* First try: ignoring hash failures.
*/
@@ -677,6 +698,7 @@ ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags,
* but some devices lie. So we just try a plausible range.
*/
error = ENOENT;
+ fsrbuf = NULL;
for (secsize = dbtob(1); secsize <= SBLOCKSIZE; secsize *= 2)
if ((error = (*readfunc)(devfd, (SBLOCK_UFS2 - secsize),
&fsrbuf, secsize)) == 0)