git: 50a53aacc9d3 - stable/12 - Allow bootstrapping makefs on older FreeBSD hosts and Linux/macOS

From: Jessica Clarke <jrtc27_at_FreeBSD.org>
Date: Thu, 12 Dec 2024 22:43:54 UTC
The branch stable/12 has been updated by jrtc27:

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

commit 50a53aacc9d39f1f40e96e3949f876567cbb8091
Author:     Alex Richardson <arichardson@FreeBSD.org>
AuthorDate: 2020-01-27 12:02:41 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2024-12-12 21:41:16 +0000

    Allow bootstrapping makefs on older FreeBSD hosts and Linux/macOS
    
    In order to do so we need to install the msdosfs headers to the bootstrap
    sysroot and avoid includes of kernel headers that may not exist on every
    host (e.g. sys/lockmgr.h). This change should allow bootstrapping of makefs
    on FreeBSD 11+ as well as Linux and macOS.
    
    We also have to avoid using the IO_SYNC macro since that may not be
    available. In makefs it is only used to switch between calling
    bwrite() and bdwrite() which both call the same function. Therefore we
    can simply always call bwrite().
    
    For our CheriBSD builds we always bootstrap makefs by setting
    LOCAL_XTOOL_DIRS='lib/libnetbsd usr.sbin/makefs' and use the makefs binary
    from the build tree to create a bootable disk image.
    
    Reviewed By:    brooks
    Differential Revision: https://reviews.freebsd.org/D23201
    
    (cherry picked from commit 162ae9c834f6d9f9cb443bd62cceb23e0b5fef48)
---
 sbin/newfs_msdos/mkfs_msdos.c          | 55 +++++++++++++++++++++++++++-------
 sys/fs/msdosfs/msdosfsmount.h          |  4 +++
 tools/build/Makefile                   | 20 ++++++++++++-
 usr.sbin/makefs/msdos/msdosfs_denode.c |  4 ---
 usr.sbin/makefs/msdos/msdosfs_vnops.c  |  4 +++
 5 files changed, 71 insertions(+), 16 deletions(-)

diff --git a/sbin/newfs_msdos/mkfs_msdos.c b/sbin/newfs_msdos/mkfs_msdos.c
index 2369a5089fcf..18545a79ea01 100644
--- a/sbin/newfs_msdos/mkfs_msdos.c
+++ b/sbin/newfs_msdos/mkfs_msdos.c
@@ -31,10 +31,15 @@ static const char rcsid[] =
 #endif /* not lint */
 
 #include <sys/param.h>
+#ifdef MAKEFS
+/* In the makefs case we only want struct disklabel */
+#include <sys/disk/bsd.h>
+#else
 #include <sys/fdcio.h>
 #include <sys/disk.h>
 #include <sys/disklabel.h>
 #include <sys/mount.h>
+#endif
 #include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/time.h>
@@ -291,14 +296,18 @@ mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
 	if (!S_ISREG(sb.st_mode))
 	    warnx("warning, %s is not a regular file", fname);
     } else {
-#ifndef MAKEFS
+#ifdef MAKEFS
+	errx(1, "o.create_size must be set!");
+#else
 	if (!S_ISCHR(sb.st_mode))
 	    warnx("warning, %s is not a character device", fname);
 #endif
     }
+#ifndef MAKEFS
     if (!o.no_create)
 	if (check_mounted(fname, sb.st_mode) == -1)
 	    goto done;
+#endif
     if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
 	warnx("cannot seek to %jd", (intmax_t)o.offset);
 	goto done;
@@ -629,10 +638,12 @@ mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
 				   bpb.bpbBigFATsecs) * bpb.bpbFATs;
 	memset(&si_sa, 0, sizeof(si_sa));
 	si_sa.sa_handler = infohandler;
+#ifdef SIGINFO
 	if (sigaction(SIGINFO, &si_sa, NULL) == -1) {
 	    warn("sigaction SIGINFO");
 	    goto done;
 	}
+#endif
 	for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) {
 	    if (got_siginfo) {
 		    fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
@@ -792,6 +803,11 @@ done:
 static int
 check_mounted(const char *fname, mode_t mode)
 {
+/*
+ * If getmntinfo() is not available (e.g. Linux) don't check. This should
+ * not be a problem since we will only be using makefs to create images.
+ */
+#if !defined(MAKEFS)
     struct statfs *mp;
     const char *s1, *s2;
     size_t len;
@@ -816,6 +832,7 @@ check_mounted(const char *fname, mode_t mode)
 	    return -1;
 	}
     }
+#endif
     return 0;
 }
 
@@ -878,6 +895,23 @@ getstdfmt(const char *fmt, struct bpb *bpb)
     return 0;
 }
 
+static void
+compute_geometry_from_file(int fd, const char *fname, struct disklabel *lp)
+{
+	struct stat st;
+	off_t ms;
+
+	if (fstat(fd, &st))
+		err(1, "cannot get disk size");
+	if (!S_ISREG(st.st_mode))
+		errx(1, "%s is not a regular file", fname);
+	ms = st.st_size;
+	lp->d_secsize = 512;
+	lp->d_nsectors = 63;
+	lp->d_ntracks = 255;
+	lp->d_secperunit = ms / lp->d_secsize;
+}
+
 /*
  * Get disk slice, partition, and geometry information.
  */
@@ -886,8 +920,10 @@ getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
 	    struct bpb *bpb)
 {
     struct disklabel *lp, dlp;
+    off_t hs = 0;
+#ifndef MAKEFS
+    off_t ms;
     struct fd_type type;
-    off_t ms, hs = 0;
 
     lp = NULL;
 
@@ -899,16 +935,8 @@ getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
     /* Maybe it's a floppy drive */
     if (lp == NULL) {
 	if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
-	    struct stat st;
-
-	    if (fstat(fd, &st))
-		err(1, "cannot get disk size");
 	    /* create a fake geometry for a file image */
-	    ms = st.st_size;
-	    dlp.d_secsize = 512;
-	    dlp.d_nsectors = 63;
-	    dlp.d_ntracks = 255;
-	    dlp.d_secperunit = ms / dlp.d_secsize;
+	    compute_geometry_from_file(fd, fname, &dlp);
 	    lp = &dlp;
 	} else if (ioctl(fd, FD_GTYPE, &type) != -1) {
 	    dlp.d_secsize = 128 << type.secsize;
@@ -948,6 +976,11 @@ getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
 	hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
 	lp = &dlp;
     }
+#else
+    /* In the makefs case we only support image files: */
+    compute_geometry_from_file(fd, fname, &dlp);
+    lp = &dlp;
+#endif
 
     if (bpb->bpbBytesPerSec == 0) {
 	if (ckgeom(fname, lp->d_secsize, "bytes/sector") == -1)
diff --git a/sys/fs/msdosfs/msdosfsmount.h b/sys/fs/msdosfs/msdosfsmount.h
index 77985e56d65b..adcee78abf7e 100644
--- a/sys/fs/msdosfs/msdosfsmount.h
+++ b/sys/fs/msdosfs/msdosfsmount.h
@@ -56,8 +56,10 @@
 #ifdef _KERNEL
 
 #include <sys/types.h>
+#ifndef MAKEFS
 #include <sys/lock.h>
 #include <sys/lockmgr.h>
+#endif
 #include <sys/tree.h>
 
 #ifdef MALLOC_DECLARE
@@ -110,7 +112,9 @@ struct msdosfsmount {
 	void *pm_w2u;	/* Unicode->Local iconv handle */
 	void *pm_u2d;	/* Unicode->DOS iconv handle */
 	void *pm_d2u;	/* DOS->Local iconv handle */
+#ifndef MAKEFS
 	struct lock pm_fatlock;	/* lockmgr protecting allocations */
+#endif
 };
 
 /*
diff --git a/tools/build/Makefile b/tools/build/Makefile
index 0e6d77716852..690237aca490 100644
--- a/tools/build/Makefile
+++ b/tools/build/Makefile
@@ -4,10 +4,15 @@
 
 LIB=		egacy
 SRC=
-INCSGROUPS=	INCS SYSINCS
+INCSGROUPS=	INCS SYSINCS UFSINCS FFSINCS MSDOSFSINCS DISKINCS
 INCS=
 
 SYSINCSDIR=	${INCLUDEDIR}/sys
+# Also add ufs/ffs/msdosfs/disk headers to allow building makefs as a bootstrap tool
+UFSINCSDIR=	${INCLUDEDIR}/ufs/ufs
+FFSINCSDIR=	${INCLUDEDIR}/ufs/ffs
+MSDOSFSINCSDIR=	${INCLUDEDIR}/fs/msdosfs
+DISKINCSDIR=	${INCLUDEDIR}/sys/disk
 
 BOOTSTRAPPING?=	0
 
@@ -56,6 +61,19 @@ SRCS=		dummy.c
 SUBDIR=		cross-build
 .endif
 
+# To allow bootstrapping makefs on FreeBSD 11 or non-FreeBSD systems:
+UFSINCS+=	${SRCTOP}/sys/ufs/ufs/dinode.h
+UFSINCS+=	${SRCTOP}/sys/ufs/ufs/dir.h
+FFSINCS+=	${SRCTOP}/sys/ufs/ffs/fs.h
+
+MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/bootsect.h
+MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/bpb.h
+MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/denode.h
+MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/direntry.h
+MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/fat.h
+MSDOSFSINCS+=	${SRCTOP}/sys/fs/msdosfs/msdosfsmount.h
+DISKINCS+=	${SRCTOP}/sys/sys/disk/bsd.h
+
 # Needed to build config (since it uses libnv)
 SYSINCS+=	${SRCTOP}/sys/sys/nv.h ${SRCTOP}/sys/sys/cnv.h
 
diff --git a/usr.sbin/makefs/msdos/msdosfs_denode.c b/usr.sbin/makefs/msdos/msdosfs_denode.c
index 39131921bce3..6711bb3f5e3e 100644
--- a/usr.sbin/makefs/msdos/msdosfs_denode.c
+++ b/usr.sbin/makefs/msdos/msdosfs_denode.c
@@ -54,7 +54,6 @@ __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
 #include <sys/errno.h>
-#include <sys/vnode.h>
 
 #include <stdio.h>
 #include <string.h>
@@ -287,10 +286,7 @@ detrunc(struct denode *dep, u_long length, int flags)
 				return (error);
 			}
 			memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
-			if (flags & IO_SYNC)
 				bwrite(bp);
-			else
-				bdwrite(bp);
 		}
 	}
 
diff --git a/usr.sbin/makefs/msdos/msdosfs_vnops.c b/usr.sbin/makefs/msdos/msdosfs_vnops.c
index fbd2a1542fce..5ee693dd5ce8 100644
--- a/usr.sbin/makefs/msdos/msdosfs_vnops.c
+++ b/usr.sbin/makefs/msdos/msdosfs_vnops.c
@@ -104,7 +104,11 @@ msdosfs_times(struct denode *dep, const struct stat *st)
 	if (stampst.st_ino)
 		st = &stampst;
 
+#ifdef HAVE_STRUCT_STAT_BIRTHTIME
 	unix2fattime(&st->st_birthtim, &dep->de_CDate, &dep->de_CTime);
+#else
+	unix2fattime(&st->st_ctim, &dep->de_CDate, &dep->de_CTime);
+#endif
 	unix2fattime(&st->st_atim, &dep->de_ADate, NULL);
 	unix2fattime(&st->st_mtim, &dep->de_MDate, &dep->de_MTime);
 }