The branch main has been updated by alfredo:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=b13110e9f3cad590abb86519e313eb1449ddc2e7

commit b13110e9f3cad590abb86519e313eb1449ddc2e7
Author:     Alfredo Dal'Ava Junior <alfr...@freebsd.org>
AuthorDate: 2022-12-17 02:54:39 +0000
Commit:     Alfredo Dal'Ava Junior <alfr...@freebsd.org>
CommitDate: 2022-12-20 03:20:11 +0000

    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
---
 sys/kern/vfs_mountroot.c |  2 +-
 sys/ufs/ffs/ffs_subr.c   | 26 ++++++++++++++++++++++++--
 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.
@@ -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)

Reply via email to