Index: head/etc/defaults/rc.conf =================================================================== --- head/etc/defaults/rc.conf +++ head/etc/defaults/rc.conf @@ -53,6 +53,7 @@ varmfs="AUTO" # Set to YES to always create an mfs /var, NO to never varsize="32m" # Size of mfs /var if created varmfs_flags="-S" # Extra mount options for the mfs /var +mfs_type="auto" # "md", "tmpfs", "auto" to prefer tmpfs with md as fallback populate_var="AUTO" # Set to YES to always (re)populate /var, NO to never cleanvar_enable="YES" # Clean the /var directory local_startup="/usr/local/etc/rc.d" # startup script dirs. Index: head/etc/rc.initdiskless =================================================================== --- head/etc/rc.initdiskless +++ head/etc/rc.initdiskless @@ -195,10 +195,10 @@ to_umount="$b ${to_umount}" } -# Create a generic memory disk (using tmpfs) -# +# Create a generic memory disk. +# The 'auto' parameter will attempt to use tmpfs(5), falls back to md(4). mount_md() { - mount -t tmpfs -o size=$(($1 * 512)) tmpfs $2 + /sbin/mdmfs $flags -s $1 auto $2 } # Create the memory filesystem if it has not already been created Index: head/etc/rc.subr =================================================================== --- head/etc/rc.subr +++ head/etc/rc.subr @@ -1790,7 +1790,7 @@ if [ -n "$3" ]; then flags="$3" fi - /sbin/mdmfs $flags -s $1 md $2 + /sbin/mdmfs $flags -s $1 ${mfs_type} $2 } # Code common to scripts that need to load a kernel module Index: head/sbin/mdmfs/mdmfs.8 =================================================================== --- head/sbin/mdmfs/mdmfs.8 +++ head/sbin/mdmfs/mdmfs.8 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd September 9, 2016 +.Dd September 9, 2017 .Dt MDMFS 8 .Os .Sh NAME @@ -33,7 +33,9 @@ .Nm mount_mfs .Nd configure and mount an in-memory file system using the .Xr md 4 -driver +driver or the +.Xr tmpfs 5 +filesystem .Sh SYNOPSIS .Nm .Op Fl DLlMNnPStTUX @@ -63,9 +65,13 @@ .Xr mount_mfs 8 . The end result is essentially the same, but is accomplished in a completely different way. -The +Based on +.Ar md-device , +the .Nm -utility configures an +utility either creates a +.Xr tmpfs 5 +filesystem, or it configures an .Xr md 4 disk using .Xr mdconfig 8 , @@ -81,6 +87,44 @@ All the command line options are passed to the appropriate program at the appropriate stage in order to achieve the desired effect. .Pp +When +.Ar md-device +is `auto', +.Nm +uses +.Xr tmpfs 5 +if it is present in the kernel or can be loaded as a module, +otherwise it falls back to using +.Xr md 4 +auto-unit as if `md' had been specified. +.Pp +When +.Ar md-device +is `tmpfs', +.Nm +mounts a +.Xr tmpfs 5 +filesystem, translating the +.Fl s +size option, if present, into a `-o size=' mount option. +Any +.Fl o +options on the command line are passed through to the +.Xr tmpfs 5 +mount. +Options specific to +.Xr mdconfig 8 +or +.Xr newfs 8 +are ignored. +.Pp +When +.Ar md-device +does not result in +.Xr tmpfs 5 +being used, then an +.Xr md 4 +device is configured instead. By default, .Nm creates a swap-based @@ -219,14 +263,10 @@ .Em not specified. That is, -this will work for the default swap-backed -.Pq Dv MD_SWAP -disks, -and the optional -.Pq Fl M -.Xr malloc 9 -backed disks -.Pq Dv MD_MALLOC . +this will work when the backing storage is some form of +memory, as opposed to a fixed-size file. +The size may include the usual SI suffixes (k, m, g, t, p). +A number without a suffix is interpreted as a count of 512-byte sectors. .It Fl t Turn on the TRIM enable flag for .Xr newfs 8 . @@ -392,6 +432,7 @@ .Sh SEE ALSO .Xr md 4 , .Xr fstab 5 , +.Xr tmpfs 5 , .Xr mdconfig 8 , .Xr mount 8 , .Xr newfs 8 Index: head/sbin/mdmfs/mdmfs.c =================================================================== --- head/sbin/mdmfs/mdmfs.c +++ head/sbin/mdmfs/mdmfs.c @@ -34,15 +34,19 @@ __FBSDID("$FreeBSD$"); #include +#include #include +#include #include #include #include #include #include +#include #include #include +#include #include #include #include @@ -78,7 +82,8 @@ static void do_mdconfig_attach(const char *, const enum md_types); static void do_mdconfig_attach_au(const char *, const enum md_types); static void do_mdconfig_detach(void); -static void do_mount(const char *, const char *); +static void do_mount_md(const char *, const char *); +static void do_mount_tmpfs(const char *, const char *); static void do_mtptsetup(const char *, struct mtpt_info *); static void do_newfs(const char *); static void extract_ugid(const char *, struct mtpt_info *); @@ -89,14 +94,15 @@ main(int argc, char **argv) { struct mtpt_info mi; /* Mountpoint info. */ + intmax_t mdsize; char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */ *mount_arg; enum md_types mdtype; /* The type of our memory disk. */ - bool have_mdtype; + bool have_mdtype, mlmac; bool detach, softdep, autounit, newfs; - char *mtpoint, *unitstr; + const char *mtpoint, *size_arg, *unitstr; char *p; - int ch; + int ch, idx; void *set; unsigned long ul; @@ -105,6 +111,7 @@ detach = true; softdep = true; autounit = false; + mlmac = false; newfs = true; have_mdtype = false; mdtype = MD_SWAP; @@ -119,6 +126,7 @@ mdconfig_arg = strdup(""); newfs_arg = strdup(""); mount_arg = strdup(""); + size_arg = NULL; /* If we were started as mount_mfs or mfs, imply -C. */ if (strcmp(getprogname(), "mount_mfs") == 0 || @@ -175,6 +183,7 @@ loudsubs = true; break; case 'l': + mlmac = true; argappend(&newfs_arg, "-l"); break; case 'M': @@ -213,7 +222,7 @@ softdep = false; break; case 's': - argappend(&mdconfig_arg, "-s %s", optarg); + size_arg = optarg; break; case 't': argappend(&newfs_arg, "-t"); @@ -242,42 +251,107 @@ if (argc < 2) usage(); - /* Derive 'unit' (global). */ - unitstr = argv[0]; - if (strncmp(unitstr, "/dev/", 5) == 0) - unitstr += 5; - if (strncmp(unitstr, mdname, mdnamelen) == 0) - unitstr += mdnamelen; - if (!isdigit(*unitstr)) { - autounit = true; - unit = -1; - mdsuffix = unitstr; - } else { - ul = strtoul(unitstr, &p, 10); - if (ul == ULONG_MAX) - errx(1, "bad device unit: %s", unitstr); - unit = ul; - mdsuffix = p; /* can be empty */ + /* + * Historically our size arg was passed directly to mdconfig, which + * treats a number without a suffix as a count of 512-byte sectors; + * tmpfs would treat it as a count of bytes. To get predictable + * behavior for 'auto' we document that the size always uses mdconfig + * rules. To make that work, decode the size here so it can be passed + * to either tmpfs or mdconfig as a count of bytes. + */ + if (size_arg != NULL) { + mdsize = (intmax_t)strtoumax(size_arg, &p, 0); + if (p == size_arg || (p[0] != 0 && p[1] != 0) || mdsize < 0) + errx(1, "invalid size '%s'", size_arg); + switch (*p) { + case 'p': + case 'P': + mdsize *= 1024; + case 't': + case 'T': + mdsize *= 1024; + case 'g': + case 'G': + mdsize *= 1024; + case 'm': + case 'M': + mdsize *= 1024; + case 'k': + case 'K': + mdsize *= 1024; + case 'b': + case 'B': + break; + case '\0': + mdsize *= 512; + break; + default: + errx(1, "invalid size suffix on '%s'", size_arg); + } } + /* + * Based on the command line 'md-device' either mount a tmpfs filesystem + * or configure the md device then format and mount a filesystem on it. + * If the device is 'auto' use tmpfs if it is available and there is no + * request for multilabel MAC (which tmpfs does not support). + */ + unitstr = argv[0]; mtpoint = argv[1]; - if (!have_mdtype) - mdtype = MD_SWAP; - if (softdep) - argappend(&newfs_arg, "-U"); - if (mdtype != MD_VNODE && !newfs) - errx(1, "-P requires a vnode-backed disk"); - /* Do the work. */ - if (detach && !autounit) - do_mdconfig_detach(); - if (autounit) - do_mdconfig_attach_au(mdconfig_arg, mdtype); - else - do_mdconfig_attach(mdconfig_arg, mdtype); - if (newfs) - do_newfs(newfs_arg); - do_mount(mount_arg, mtpoint); + if (strcmp(unitstr, "auto") == 0) { + if (mlmac) + idx = -1; /* Must use md for mlmac. */ + else if ((idx = modfind("tmpfs")) == -1) + idx = kldload("tmpfs"); + if (idx == -1) + unitstr = "md"; + else + unitstr = "tmpfs"; + } + + if (strcmp(unitstr, "tmpfs") == 0) { + if (size_arg != NULL && mdsize != 0) + argappend(&mount_arg, "-o size=%jd", mdsize); + do_mount_tmpfs(mount_arg, mtpoint); + } else { + if (size_arg != NULL) + argappend(&mdconfig_arg, "-s %jdB", mdsize); + if (strncmp(unitstr, "/dev/", 5) == 0) + unitstr += 5; + if (strncmp(unitstr, mdname, mdnamelen) == 0) + unitstr += mdnamelen; + if (!isdigit(*unitstr)) { + autounit = true; + unit = -1; + mdsuffix = unitstr; + } else { + ul = strtoul(unitstr, &p, 10); + if (ul == ULONG_MAX) + errx(1, "bad device unit: %s", unitstr); + unit = ul; + mdsuffix = p; /* can be empty */ + } + + if (!have_mdtype) + mdtype = MD_SWAP; + if (softdep) + argappend(&newfs_arg, "-U"); + if (mdtype != MD_VNODE && !newfs) + errx(1, "-P requires a vnode-backed disk"); + + /* Do the work. */ + if (detach && !autounit) + do_mdconfig_detach(); + if (autounit) + do_mdconfig_attach_au(mdconfig_arg, mdtype); + else + do_mdconfig_attach(mdconfig_arg, mdtype); + if (newfs) + do_newfs(newfs_arg); + do_mount_md(mount_arg, mtpoint); + } + do_mtptsetup(mtpoint, &mi); return (0); @@ -434,7 +508,7 @@ * Mount the configured memory disk. */ static void -do_mount(const char *args, const char *mtpoint) +do_mount_md(const char *args, const char *mtpoint) { int rv; @@ -442,6 +516,19 @@ mdname, unit, mdsuffix, mtpoint); if (rv) errx(1, "mount exited with error code %d", rv); +} + +/* + * Mount the configured tmpfs. + */ +static void +do_mount_tmpfs(const char *args, const char *mtpoint) +{ + int rv; + + rv = run(NULL, "%s -t tmpfs %s tmp %s", _PATH_MOUNT, args, mtpoint); + if (rv) + errx(1, "tmpfs mount exited with error code %d", rv); } /*