Subject: multiple mounts on same directory
To: None <tech-userlevel@netbsd.org>
From: Alan Barrett <apb@cequrux.com>
List: tech-userlevel
Date: 07/18/2003 16:07:18
I want to have several union mounts on the same destination directory.
When I perform the mounts one by one, it all works fine.  For example,

	mount -t null -o union /source1 /target
	mount -t null -o union /source2 /target

However, when I put the following lines in /etc/fstab:

	/source1	/target		null	rw,union	0 0
	/source2	/target		null	rw,union	0 0

and run "mount -av", then only the first mount works.  The second mount
falls foul of a duplicate detection test in the mountfs() function
near line 352 of src/sbin/mount/mount.c.  The test in question looks
something like this:

		/*
		 * XXX can't check f_mntfromname,
		 * thanks to mfs, union, etc.
		 */
		if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
		    strncmp(vfstype, sfp[i].f_fstypename,
			MFSNAMELEN) == 0) {
			/* ignore duplicate ... */
		}
 
I can see why we would sometimes want to avoid checking the f_mntfromname
field (for example, with mfs mounts, it shows "mfs:<pid>" instead of just
"mfs".  But sometimes (as in the multiple-union-mount case) we really
do need to check f_mntfromname.

I have the following ideas for fixing this:

  1. Add a MNT_foo flag to mean "Do not use the f_mntfromname field
     as part of duplicate detection"; let the flag be set by procfs,
     kernfs, mfs, and any others that need it; and let it be tested by
     the relevant part of mountfs().

  2. Add some special rules to the relevant part of mountfs() to help it
     guess whether or not to use f_mntfromname for duplicate detection.
     For example, it could assume that f_mntfromname is important for
     all mounts with fstype="union" or with the MNT_UNION flag set.

  3. Add fuzzy matching of f_mntfromname to the relevant part of
     mountfs().  For example, it could decide that "mfs" and "mfs:123"
     are close enough to count as duplicates.

  4. Keep both the original name (e.g."mfs") and the modified name (e.g.
     "mfs:<pid>") around in the kernel, and make them both available in
     struct statfs.  Let the relevant part of mountfs() use the original
     name for duplicate detection.

I like option 1 best.  I have written code for option 2 and it seems to
work.
 
--apb (Alan Barrett)