Subject: Using tmpfs instead of mfs for "MAKEDEV -M"
To: None <tech-userlevel@NetBSD.org>
From: Alan Barrett <apb@cequrux.com>
List: tech-userlevel
Date: 12/09/2007 14:07:33
mount_tmpfs(8) seems to be quite stable, so I'd like to
start using it instead of mount_mfs(8) for "MAKEDEV -M".
Not all our INSTALL-like or GENERIC-like kernels include
"file-system TMPFS", so I'd make MAKEDEV fall back to
using mfs if tmpfs doesn't work.

I append a patch for comment or objections.

--apb (Alan Barrett)

Index: src/etc/MAKEDEV.tmpl
--- MAKEDEV.tmpl	9 Dec 2007 08:46:16 -0000	1.94
+++ MAKEDEV.tmpl	9 Dec 2007 11:53:30 -0000
@@ -1901,24 +1901,57 @@
 	mkdev r${name}${unit}$rn c $chr $(($unit * $doff + $ro)) 640 $g_operator
 }
 
+# create_mfs_dev nodes
+#       Create a memory file system for a given number of device nodes,
+#       and mount it.  Attempts to use mount_tmpfs, or falls back to
+#       mount_mfs.
+#
+#       If do_redirect, then also redirect output to the console.
+#
 create_mfs_dev()
 {
-	ninode=$((${1-1200} * 11 / 10))
-	fs_size=$((8192 + 2 * 8192 + 4096 + ninode*(128+18) + 8192))
-	fs_size=$((fs_size/512))
-	[ "$fs_size" -lt 80 ] && fs_size=80
-
+	ndevnodes=${1-1200}
 	dev_mountpoint=${PWD:-/dev}
-	mount_mfs -b 4096 -f 512 -s $fs_size -n $ninode -p 07555 -o union swap $dev_mountpoint
 
-	cd $dev_mountpoint
+	# Number of inodes is the specified number of device nodes, plus
+	# a small margin to allow for subdirectories, space for extra
+	# device nodes created later, and inodes reserved by the file
+	# system.
+	ninode=$((${ndevnodes} * 11 / 10))
+	# This file system size calculation is exact for mount_mfs(8)
+	# with 512-byte sectors; it's a little larger than necessary
+	# for mount_tmpfs(8).  The 40kB minimum is arbitrary.
+	fs_bytes=$((8192 + 2 * 8192 + 4096 + ninode*(128+18) + 8192))
+	[ "$fs_bytes" -lt 40960 ] && fs_bytes=40960
+	fs_blocks=$((fs_bytes/512))
+
+	# Try tmpfs; if that fails try mfs.
+	if mount_tmpfs -s $fs_bytes -n $ninode -m 07555 \
+		-o union tmpfs "$dev_mountpoint"
+	then
+	    fstype=tmpfs
+	elif mount_mfs -b 4096 -f 512 -s $fs_blocks -n $ninode -p 07555 \
+		-o union swap "$dev_mountpoint"
+	then
+	    fstype=mfs
+	else
+	    warn "Failed to create memory file system"
+	    exit 1
+	fi
+
+	# Our current directory was in the lower file system; change it to
+	# the newly mounted upper file system.
+	cd "$dev_mountpoint"
+
 	if $do_redirect; then
 		# Redirect stdout and stderr to console
 		mknod -m 600 -g 0 -u 0 temp_console c 0 0
 		exec >temp_console 2>&1
 		rm temp_console
 	fi
-	echo "Created mfs $dev_mountpoint ($fs_size blocks, $ninode inodes)"
+
+	echo "Created $fstype $dev_mountpoint" \
+		"($fs_blocks blocks, $ninode inodes)"
 }
 
 #