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
This commit is contained in:
Alfredo Dal'Ava Junior 2022-12-16 23:54:39 -03:00
parent e7815784cc
commit b13110e9f3
2 changed files with 25 additions and 3 deletions

View File

@ -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 ||

View File

@ -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.
@ -382,6 +384,17 @@ validate_sblock(struct fs *fs, int flags)
prtmsg = ((flags & UFS_NOMSG) == 0);
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
@ -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)