xref: /freebsd/sys/kern/vfs_mount.c (revision 3d11b6c8f01e1fca5936a11d6996448467851a94)
1 /*-
2  * Copyright (c) 1999-2004 Poul-Henning Kamp
3  * Copyright (c) 1999 Michael Smith
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/conf.h>
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/libkern.h>
45 #include <sys/mac.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/filedesc.h>
52 #include <sys/reboot.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
55 #include <sys/sx.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
60 #include <vm/uma.h>
61 
62 #include <geom/geom.h>
63 
64 #include <machine/stdarg.h>
65 
66 #include "opt_rootdevname.h"
67 #include "opt_ddb.h"
68 #include "opt_mac.h"
69 
70 #ifdef DDB
71 #include <ddb/ddb.h>
72 #endif
73 
74 #define	ROOTNAME		"root_device"
75 #define	VFS_MOUNTARG_SIZE_MAX	(1024 * 64)
76 
77 static int	vfs_domount(struct thread *td, const char *fstype,
78 		    char *fspath, int fsflags, void *fsdata);
79 static struct mount *vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp,
80 		    const char *fspath, struct thread *td);
81 static int	vfs_mountroot_ask(void);
82 static int	vfs_mountroot_try(const char *mountfrom);
83 static int	vfs_donmount(struct thread *td, int fsflags,
84 		    struct uio *fsoptions);
85 static void	free_mntarg(struct mntarg *ma);
86 static void	vfs_mount_destroy(struct mount *, struct thread *);
87 static int	vfs_getopt_pos(struct vfsoptlist *opts, const char *name);
88 
89 static int	usermount = 0;
90 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
91     "Unprivileged users may mount and unmount file systems");
92 
93 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
94 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
95 static uma_zone_t mount_zone;
96 
97 /* List of mounted filesystems. */
98 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
99 
100 /* For any iteration/modification of mountlist */
101 struct mtx mountlist_mtx;
102 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
103 
104 TAILQ_HEAD(vfsoptlist, vfsopt);
105 struct vfsopt {
106 	TAILQ_ENTRY(vfsopt) link;
107 	char	*name;
108 	void	*value;
109 	int	len;
110 };
111 
112 /*
113  * The vnode of the system's root (/ in the filesystem, without chroot
114  * active.)
115  */
116 struct vnode	*rootvnode;
117 
118 /*
119  * The root filesystem is detailed in the kernel environment variable
120  * vfs.root.mountfrom, which is expected to be in the general format
121  *
122  * <vfsname>:[<path>]
123  * vfsname   := the name of a VFS known to the kernel and capable
124  *              of being mounted as root
125  * path      := disk device name or other data used by the filesystem
126  *              to locate its physical store
127  */
128 
129 /*
130  * Global opts, taken by all filesystems
131  */
132 static const char *global_opts[] = {
133 	"errmsg",
134 	"fstype",
135 	"fspath",
136 	"rdonly",
137 	"ro",
138 	"rw",
139 	"suid",
140 	"exec",
141 	NULL
142 };
143 
144 /*
145  * The root specifiers we will try if RB_CDROM is specified.
146  */
147 static char *cdrom_rootdevnames[] = {
148 	"cd9660:cd0",
149 	"cd9660:acd0",
150 	NULL
151 };
152 
153 /* legacy find-root code */
154 char		*rootdevnames[2] = {NULL, NULL};
155 #ifndef ROOTDEVNAME
156 #  define ROOTDEVNAME NULL
157 #endif
158 static const char	*ctrootdevname = ROOTDEVNAME;
159 
160 /*
161  * ---------------------------------------------------------------------
162  * Functions for building and sanitizing the mount options
163  */
164 
165 /* Remove one mount option. */
166 static void
167 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
168 {
169 
170 	TAILQ_REMOVE(opts, opt, link);
171 	free(opt->name, M_MOUNT);
172 	if (opt->value != NULL)
173 		free(opt->value, M_MOUNT);
174 #ifdef INVARIANTS
175 	else if (opt->len != 0)
176 		panic("%s: mount option with NULL value but length != 0",
177 		    __func__);
178 #endif
179 	free(opt, M_MOUNT);
180 }
181 
182 /* Release all resources related to the mount options. */
183 static void
184 vfs_freeopts(struct vfsoptlist *opts)
185 {
186 	struct vfsopt *opt;
187 
188 	while (!TAILQ_EMPTY(opts)) {
189 		opt = TAILQ_FIRST(opts);
190 		vfs_freeopt(opts, opt);
191 	}
192 	free(opts, M_MOUNT);
193 }
194 
195 /*
196  * Check if options are equal (with or without the "no" prefix).
197  */
198 static int
199 vfs_equalopts(const char *opt1, const char *opt2)
200 {
201 
202 	/* "opt" vs. "opt" or "noopt" vs. "noopt" */
203 	if (strcmp(opt1, opt2) == 0)
204 		return (1);
205 	/* "noopt" vs. "opt" */
206 	if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
207 		return (1);
208 	/* "opt" vs. "noopt" */
209 	if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
210 		return (1);
211 	return (0);
212 }
213 
214 /*
215  * If a mount option is specified several times,
216  * (with or without the "no" prefix) only keep
217  * the last occurence of it.
218  */
219 static void
220 vfs_sanitizeopts(struct vfsoptlist *opts)
221 {
222 	struct vfsopt *opt, *opt2, *tmp;
223 
224 	TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
225 		opt2 = TAILQ_PREV(opt, vfsoptlist, link);
226 		while (opt2 != NULL) {
227 			if (vfs_equalopts(opt->name, opt2->name)) {
228 				tmp = TAILQ_PREV(opt2, vfsoptlist, link);
229 				vfs_freeopt(opts, opt2);
230 				opt2 = tmp;
231 			} else {
232 				opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
233 			}
234 		}
235 	}
236 }
237 
238 /*
239  * Build a linked list of mount options from a struct uio.
240  */
241 static int
242 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
243 {
244 	struct vfsoptlist *opts;
245 	struct vfsopt *opt;
246 	size_t memused;
247 	unsigned int i, iovcnt;
248 	int error, namelen, optlen;
249 
250 	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
251 	TAILQ_INIT(opts);
252 	memused = 0;
253 	iovcnt = auio->uio_iovcnt;
254 	for (i = 0; i < iovcnt; i += 2) {
255 		opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
256 		namelen = auio->uio_iov[i].iov_len;
257 		optlen = auio->uio_iov[i + 1].iov_len;
258 		opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
259 		opt->value = NULL;
260 		opt->len = 0;
261 
262 		/*
263 		 * Do this early, so jumps to "bad" will free the current
264 		 * option.
265 		 */
266 		TAILQ_INSERT_TAIL(opts, opt, link);
267 		memused += sizeof(struct vfsopt) + optlen + namelen;
268 
269 		/*
270 		 * Avoid consuming too much memory, and attempts to overflow
271 		 * memused.
272 		 */
273 		if (memused > VFS_MOUNTARG_SIZE_MAX ||
274 		    optlen > VFS_MOUNTARG_SIZE_MAX ||
275 		    namelen > VFS_MOUNTARG_SIZE_MAX) {
276 			error = EINVAL;
277 			goto bad;
278 		}
279 
280 		if (auio->uio_segflg == UIO_SYSSPACE) {
281 			bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
282 		} else {
283 			error = copyin(auio->uio_iov[i].iov_base, opt->name,
284 			    namelen);
285 			if (error)
286 				goto bad;
287 		}
288 		/* Ensure names are null-terminated strings. */
289 		if (opt->name[namelen - 1] != '\0') {
290 			error = EINVAL;
291 			goto bad;
292 		}
293 		if (optlen != 0) {
294 			opt->len = optlen;
295 			opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
296 			if (auio->uio_segflg == UIO_SYSSPACE) {
297 				bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
298 				    optlen);
299 			} else {
300 				error = copyin(auio->uio_iov[i + 1].iov_base,
301 				    opt->value, optlen);
302 				if (error)
303 					goto bad;
304 			}
305 		}
306 	}
307 	vfs_sanitizeopts(opts);
308 	*options = opts;
309 	return (0);
310 bad:
311 	vfs_freeopts(opts);
312 	return (error);
313 }
314 
315 /*
316  * Merge the old mount options with the new ones passed
317  * in the MNT_UPDATE case.
318  */
319 static void
320 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
321 {
322 	struct vfsopt *opt, *opt2, *new;
323 
324 	TAILQ_FOREACH(opt, opts, link) {
325 		/*
326 		 * Check that this option hasn't been redefined
327 		 * nor cancelled with a "no" mount option.
328 		 */
329 		opt2 = TAILQ_FIRST(toopts);
330 		while (opt2 != NULL) {
331 			if (strcmp(opt2->name, opt->name) == 0)
332 				goto next;
333 			if (strncmp(opt2->name, "no", 2) == 0 &&
334 			    strcmp(opt2->name + 2, opt->name) == 0) {
335 				vfs_freeopt(toopts, opt2);
336 				goto next;
337 			}
338 			opt2 = TAILQ_NEXT(opt2, link);
339 		}
340 		/* We want this option, duplicate it. */
341 		new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
342 		new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
343 		strcpy(new->name, opt->name);
344 		if (opt->len != 0) {
345 			new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
346 			bcopy(opt->value, new->value, opt->len);
347 		} else {
348 			new->value = NULL;
349 		}
350 		new->len = opt->len;
351 		TAILQ_INSERT_TAIL(toopts, new, link);
352 next:
353 		continue;
354 	}
355 }
356 
357 /*
358  * ---------------------------------------------------------------------
359  * Mount a filesystem
360  */
361 int
362 nmount(td, uap)
363 	struct thread *td;
364 	struct nmount_args /* {
365 		struct iovec *iovp;
366 		unsigned int iovcnt;
367 		int flags;
368 	} */ *uap;
369 {
370 	struct uio *auio;
371 	struct iovec *iov;
372 	unsigned int i;
373 	int error;
374 	u_int iovcnt;
375 
376 	/* Kick out MNT_ROOTFS early as it is legal internally */
377 	if (uap->flags & MNT_ROOTFS)
378 		return (EINVAL);
379 
380 	iovcnt = uap->iovcnt;
381 	/*
382 	 * Check that we have an even number of iovec's
383 	 * and that we have at least two options.
384 	 */
385 	if ((iovcnt & 1) || (iovcnt < 4))
386 		return (EINVAL);
387 
388 	error = copyinuio(uap->iovp, iovcnt, &auio);
389 	if (error)
390 		return (error);
391 	iov = auio->uio_iov;
392 	for (i = 0; i < iovcnt; i++) {
393 		if (iov->iov_len > MMAXOPTIONLEN) {
394 			free(auio, M_IOV);
395 			return (EINVAL);
396 		}
397 		iov++;
398 	}
399 	error = vfs_donmount(td, uap->flags, auio);
400 
401 	free(auio, M_IOV);
402 	return (error);
403 }
404 
405 /*
406  * ---------------------------------------------------------------------
407  * Various utility functions
408  */
409 
410 void
411 vfs_ref(struct mount *mp)
412 {
413 
414 	MNT_ILOCK(mp);
415 	MNT_REF(mp);
416 	MNT_IUNLOCK(mp);
417 }
418 
419 void
420 vfs_rel(struct mount *mp)
421 {
422 
423 	MNT_ILOCK(mp);
424 	MNT_REL(mp);
425 	MNT_IUNLOCK(mp);
426 }
427 
428 static int
429 mount_init(void *mem, int size, int flags)
430 {
431 	struct mount *mp;
432 
433 	mp = (struct mount *)mem;
434 	mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
435 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
436 	return (0);
437 }
438 
439 static void
440 mount_fini(void *mem, int size)
441 {
442 	struct mount *mp;
443 
444 	mp = (struct mount *)mem;
445 	lockdestroy(&mp->mnt_lock);
446 	mtx_destroy(&mp->mnt_mtx);
447 }
448 
449 /*
450  * Allocate and initialize the mount point struct.
451  */
452 static struct mount *
453 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
454     const char *fspath, struct thread *td)
455 {
456 	struct mount *mp;
457 
458 	mp = uma_zalloc(mount_zone, M_WAITOK);
459 	bzero(&mp->mnt_startzero,
460 	    __rangeof(struct mount, mnt_startzero, mnt_endzero));
461 	TAILQ_INIT(&mp->mnt_nvnodelist);
462 	mp->mnt_nvnodelistsize = 0;
463 	mp->mnt_ref = 0;
464 	(void) vfs_busy(mp, LK_NOWAIT, 0, td);
465 	mp->mnt_op = vfsp->vfc_vfsops;
466 	mp->mnt_vfc = vfsp;
467 	vfsp->vfc_refcount++;	/* XXX Unlocked */
468 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
469 	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
470 	strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
471 	mp->mnt_vnodecovered = vp;
472 	mp->mnt_cred = crdup(td->td_ucred);
473 	mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
474 	strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
475 	mp->mnt_iosize_max = DFLTPHYS;
476 #ifdef MAC
477 	mac_init_mount(mp);
478 	mac_create_mount(td->td_ucred, mp);
479 #endif
480 	arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
481 	return (mp);
482 }
483 
484 /*
485  * Destroy the mount struct previously allocated by vfs_mount_alloc().
486  */
487 static void
488 vfs_mount_destroy(struct mount *mp, struct thread *td)
489 {
490 	int i;
491 
492 	lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
493 	MNT_ILOCK(mp);
494 	for (i = 0; mp->mnt_ref && i < 3; i++)
495 		msleep(mp, MNT_MTX(mp), PVFS, "mntref", hz);
496 	/*
497 	 * This will always cause a 3 second delay in rebooting due to
498 	 * refs on the root mountpoint that never go away.  Most of these
499 	 * are held by init which never exits.
500 	 */
501 	if (i == 3 && (!rebooting || bootverbose))
502 		printf("Mount point %s had %d dangling refs\n",
503 		    mp->mnt_stat.f_mntonname, mp->mnt_ref);
504 	if (mp->mnt_holdcnt != 0) {
505 		printf("Waiting for mount point to be unheld\n");
506 		while (mp->mnt_holdcnt != 0) {
507 			mp->mnt_holdcntwaiters++;
508 			msleep(&mp->mnt_holdcnt, MNT_MTX(mp),
509 			       PZERO, "mntdestroy", 0);
510 			mp->mnt_holdcntwaiters--;
511 		}
512 		printf("mount point unheld\n");
513 	}
514 	if (mp->mnt_writeopcount > 0) {
515 		printf("Waiting for mount point write ops\n");
516 		while (mp->mnt_writeopcount > 0) {
517 			mp->mnt_kern_flag |= MNTK_SUSPEND;
518 			msleep(&mp->mnt_writeopcount,
519 			       MNT_MTX(mp),
520 			       PZERO, "mntdestroy2", 0);
521 		}
522 		printf("mount point write ops completed\n");
523 	}
524 	if (mp->mnt_secondary_writes > 0) {
525 		printf("Waiting for mount point secondary write ops\n");
526 		while (mp->mnt_secondary_writes > 0) {
527 			mp->mnt_kern_flag |= MNTK_SUSPEND;
528 			msleep(&mp->mnt_secondary_writes,
529 			       MNT_MTX(mp),
530 			       PZERO, "mntdestroy3", 0);
531 		}
532 		printf("mount point secondary write ops completed\n");
533 	}
534 	MNT_IUNLOCK(mp);
535 	mp->mnt_vfc->vfc_refcount--;
536 	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
537 		struct vnode *vp;
538 
539 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
540 			vprint("", vp);
541 		panic("unmount: dangling vnode");
542 	}
543 	MNT_ILOCK(mp);
544 	if (mp->mnt_kern_flag & MNTK_MWAIT)
545 		wakeup(mp);
546 	if (mp->mnt_writeopcount != 0)
547 		panic("vfs_mount_destroy: nonzero writeopcount");
548 	if (mp->mnt_secondary_writes != 0)
549 		panic("vfs_mount_destroy: nonzero secondary_writes");
550 	if (mp->mnt_nvnodelistsize != 0)
551 		panic("vfs_mount_destroy: nonzero nvnodelistsize");
552 	mp->mnt_writeopcount = -1000;
553 	mp->mnt_nvnodelistsize = -1000;
554 	mp->mnt_secondary_writes = -1000;
555 	MNT_IUNLOCK(mp);
556 #ifdef MAC
557 	mac_destroy_mount(mp);
558 #endif
559 	if (mp->mnt_opt != NULL)
560 		vfs_freeopts(mp->mnt_opt);
561 	crfree(mp->mnt_cred);
562 	uma_zfree(mount_zone, mp);
563 }
564 
565 static int
566 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
567 {
568 	struct vfsoptlist *optlist;
569 	struct vfsopt *opt;
570 	char *fstype, *fspath, *errmsg;
571 	int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
572 
573 	errmsg_len = 0;
574 	errmsg_pos = -1;
575 
576 	error = vfs_buildopts(fsoptions, &optlist);
577 	if (error)
578 		return (error);
579 
580 	if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
581 		errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
582 	else
583 		errmsg_len = 0;
584 
585 	/*
586 	 * We need these two options before the others,
587 	 * and they are mandatory for any filesystem.
588 	 * Ensure they are NUL terminated as well.
589 	 */
590 	fstypelen = 0;
591 	error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
592 	if (error || fstype[fstypelen - 1] != '\0') {
593 		error = EINVAL;
594 		if (errmsg != NULL)
595 			strncpy(errmsg, "Invalid fstype", errmsg_len);
596 		goto bail;
597 	}
598 	fspathlen = 0;
599 	error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
600 	if (error || fspath[fspathlen - 1] != '\0') {
601 		error = EINVAL;
602 		if (errmsg != NULL)
603 			strncpy(errmsg, "Invalid fspath", errmsg_len);
604 		goto bail;
605 	}
606 
607 	/*
608 	 * We need to see if we have the "update" option
609 	 * before we call vfs_domount(), since vfs_domount() has special
610 	 * logic based on MNT_UPDATE.  This is very important
611 	 * when we want to update the root filesystem.
612 	 */
613 	TAILQ_FOREACH(opt, optlist, link) {
614 		if (strcmp(opt->name, "update") == 0)
615 			fsflags |= MNT_UPDATE;
616 		else if (strcmp(opt->name, "async") == 0)
617 			fsflags |= MNT_ASYNC;
618 		else if (strcmp(opt->name, "force") == 0)
619 			fsflags |= MNT_FORCE;
620 		else if (strcmp(opt->name, "multilabel") == 0)
621 			fsflags |= MNT_MULTILABEL;
622 		else if (strcmp(opt->name, "noasync") == 0)
623 			fsflags &= ~MNT_ASYNC;
624 		else if (strcmp(opt->name, "noatime") == 0)
625 			fsflags |= MNT_NOATIME;
626 		else if (strcmp(opt->name, "noclusterr") == 0)
627 			fsflags |= MNT_NOCLUSTERR;
628 		else if (strcmp(opt->name, "noclusterw") == 0)
629 			fsflags |= MNT_NOCLUSTERW;
630 		else if (strcmp(opt->name, "noexec") == 0)
631 			fsflags |= MNT_NOEXEC;
632 		else if (strcmp(opt->name, "nosuid") == 0)
633 			fsflags |= MNT_NOSUID;
634 		else if (strcmp(opt->name, "nosymfollow") == 0)
635 			fsflags |= MNT_NOSYMFOLLOW;
636 		else if (strcmp(opt->name, "noro") == 0 ||
637 		    strcmp(opt->name, "rw") == 0)
638 			fsflags &= ~MNT_RDONLY;
639 		else if (strcmp(opt->name, "ro") == 0 ||
640 		    strcmp(opt->name, "rdonly") == 0)
641 			fsflags |= MNT_RDONLY;
642 		else if (strcmp(opt->name, "snapshot") == 0)
643 			fsflags |= MNT_SNAPSHOT;
644 		else if (strcmp(opt->name, "suiddir") == 0)
645 			fsflags |= MNT_SUIDDIR;
646 		else if (strcmp(opt->name, "sync") == 0)
647 			fsflags |= MNT_SYNCHRONOUS;
648 		else if (strcmp(opt->name, "union") == 0)
649 			fsflags |= MNT_UNION;
650 	}
651 
652 	/*
653 	 * Be ultra-paranoid about making sure the type and fspath
654 	 * variables will fit in our mp buffers, including the
655 	 * terminating NUL.
656 	 */
657 	if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
658 		error = ENAMETOOLONG;
659 		goto bail;
660 	}
661 
662 	mtx_lock(&Giant);
663 	error = vfs_domount(td, fstype, fspath, fsflags, optlist);
664 	mtx_unlock(&Giant);
665 bail:
666 	/* copyout the errmsg */
667 	if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
668 	    && errmsg_len > 0 && errmsg != NULL) {
669 		if (fsoptions->uio_segflg == UIO_SYSSPACE) {
670 			strncpy(fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
671 			    errmsg,
672 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
673 		} else {
674 			copystr(errmsg,
675 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
676 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len,
677 			    NULL);
678 		}
679 	}
680 
681 	if (error != 0)
682 		vfs_freeopts(optlist);
683 	return (error);
684 }
685 
686 /*
687  * ---------------------------------------------------------------------
688  * Old mount API.
689  */
690 #ifndef _SYS_SYSPROTO_H_
691 struct mount_args {
692 	char	*type;
693 	char	*path;
694 	int	flags;
695 	caddr_t	data;
696 };
697 #endif
698 /* ARGSUSED */
699 int
700 mount(td, uap)
701 	struct thread *td;
702 	struct mount_args /* {
703 		char *type;
704 		char *path;
705 		int flags;
706 		caddr_t data;
707 	} */ *uap;
708 {
709 	char *fstype;
710 	struct vfsconf *vfsp = NULL;
711 	struct mntarg *ma = NULL;
712 	int error;
713 
714 	/* Kick out MNT_ROOTFS early as it is legal internally */
715 	uap->flags &= ~MNT_ROOTFS;
716 
717 	if (uap->data == NULL)
718 		return (EINVAL);
719 
720 	fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
721 	error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
722 	if (!error) {
723 		mtx_lock(&Giant);	/* XXX ? */
724 		vfsp = vfs_byname_kld(fstype, td, &error);
725 		mtx_unlock(&Giant);
726 	}
727 	free(fstype, M_TEMP);
728 	if (error)
729 		return (error);
730 	if (vfsp == NULL)
731 		return (ENOENT);
732 	if (vfsp->vfc_vfsops->vfs_cmount == NULL)
733 		return (EOPNOTSUPP);
734 
735 	ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
736 	ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
737 	ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
738 	ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
739 	ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
740 
741 	error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
742 	return (error);
743 }
744 
745 
746 /*
747  * vfs_domount(): actually attempt a filesystem mount.
748  */
749 static int
750 vfs_domount(
751 	struct thread *td,	/* Flags common to all filesystems. */
752 	const char *fstype,	/* Filesystem type. */
753 	char *fspath,		/* Mount path. */
754 	int fsflags,		/* Flags common to all filesystems. */
755 	void *fsdata		/* Options local to the filesystem. */
756 	)
757 {
758 	struct vnode *vp;
759 	struct mount *mp;
760 	struct vfsconf *vfsp;
761 	int error, flag = 0, kern_flag = 0;
762 	struct vattr va;
763 	struct nameidata nd;
764 
765 	mtx_assert(&Giant, MA_OWNED);
766 	/*
767 	 * Be ultra-paranoid about making sure the type and fspath
768 	 * variables will fit in our mp buffers, including the
769 	 * terminating NUL.
770 	 */
771 	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
772 		return (ENAMETOOLONG);
773 
774 	if (jailed(td->td_ucred))
775 		return (EPERM);
776 	if (usermount == 0) {
777 		if ((error = suser(td)) != 0)
778 			return (error);
779 	}
780 
781 	/*
782 	 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
783 	 */
784 	if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) {
785 		if ((error = suser(td)) != 0)
786 			return (error);
787 	}
788 	/*
789 	 * Silently enforce MNT_NOSUID and MNT_USER for
790 	 * unprivileged users.
791 	 */
792 	if (suser(td) != 0)
793 		fsflags |= MNT_NOSUID | MNT_USER;
794 
795 	/* Load KLDs before we lock the covered vnode to avoid reversals. */
796 	vfsp = NULL;
797 	if ((fsflags & MNT_UPDATE) == 0) {
798 		/* Don't try to load KLDs if we're mounting the root. */
799 		if (fsflags & MNT_ROOTFS)
800 			vfsp = vfs_byname(fstype);
801 		else
802 			vfsp = vfs_byname_kld(fstype, td, &error);
803 		if (vfsp == NULL)
804 			return (ENODEV);
805 	}
806 	/*
807 	 * Get vnode to be covered
808 	 */
809 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
810 	if ((error = namei(&nd)) != 0)
811 		return (error);
812 	NDFREE(&nd, NDF_ONLY_PNBUF);
813 	vp = nd.ni_vp;
814 	if (fsflags & MNT_UPDATE) {
815 		if ((vp->v_vflag & VV_ROOT) == 0) {
816 			vput(vp);
817 			return (EINVAL);
818 		}
819 		mp = vp->v_mount;
820 		flag = mp->mnt_flag;
821 		kern_flag = mp->mnt_kern_flag;
822 		/*
823 		 * We only allow the filesystem to be reloaded if it
824 		 * is currently mounted read-only.
825 		 */
826 		if ((fsflags & MNT_RELOAD) &&
827 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
828 			vput(vp);
829 			return (EOPNOTSUPP);	/* Needs translation */
830 		}
831 		/*
832 		 * Only privileged root, or (if MNT_USER is set) the user that
833 		 * did the original mount is permitted to update it.
834 		 */
835 		error = vfs_suser(mp, td);
836 		if (error) {
837 			vput(vp);
838 			return (error);
839 		}
840 		if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
841 			vput(vp);
842 			return (EBUSY);
843 		}
844 		VI_LOCK(vp);
845 		if ((vp->v_iflag & VI_MOUNT) != 0 ||
846 		    vp->v_mountedhere != NULL) {
847 			VI_UNLOCK(vp);
848 			vfs_unbusy(mp, td);
849 			vput(vp);
850 			return (EBUSY);
851 		}
852 		vp->v_iflag |= VI_MOUNT;
853 		VI_UNLOCK(vp);
854 		mp->mnt_flag |= fsflags &
855 		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
856 		VOP_UNLOCK(vp, 0, td);
857 		mp->mnt_optnew = fsdata;
858 		vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
859 	} else {
860 		/*
861 		 * If the user is not root, ensure that they own the directory
862 		 * onto which we are attempting to mount.
863 		 */
864 		error = VOP_GETATTR(vp, &va, td->td_ucred, td);
865 		if (error) {
866 			vput(vp);
867 			return (error);
868 		}
869 		if (va.va_uid != td->td_ucred->cr_uid) {
870 			if ((error = suser(td)) != 0) {
871 				vput(vp);
872 				return (error);
873 			}
874 		}
875 		error = vinvalbuf(vp, V_SAVE, td, 0, 0);
876 		if (error != 0) {
877 			vput(vp);
878 			return (error);
879 		}
880 		if (vp->v_type != VDIR) {
881 			vput(vp);
882 			return (ENOTDIR);
883 		}
884 		VI_LOCK(vp);
885 		if ((vp->v_iflag & VI_MOUNT) != 0 ||
886 		    vp->v_mountedhere != NULL) {
887 			VI_UNLOCK(vp);
888 			vput(vp);
889 			return (EBUSY);
890 		}
891 		vp->v_iflag |= VI_MOUNT;
892 		VI_UNLOCK(vp);
893 
894 		/*
895 		 * Allocate and initialize the filesystem.
896 		 */
897 		mp = vfs_mount_alloc(vp, vfsp, fspath, td);
898 		VOP_UNLOCK(vp, 0, td);
899 
900 		/* XXXMAC: pass to vfs_mount_alloc? */
901 		mp->mnt_optnew = fsdata;
902 	}
903 
904 	/*
905 	 * Set the mount level flags.
906 	 */
907 	if (fsflags & MNT_RDONLY)
908 		mp->mnt_flag |= MNT_RDONLY;
909 	mp->mnt_flag &=~ MNT_UPDATEMASK;
910 	mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
911 	/*
912 	 * Mount the filesystem.
913 	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
914 	 * get.  No freeing of cn_pnbuf.
915 	 */
916         error = VFS_MOUNT(mp, td);
917 	if (!error) {
918 		if (mp->mnt_opt != NULL)
919 			vfs_freeopts(mp->mnt_opt);
920 		mp->mnt_opt = mp->mnt_optnew;
921 		(void)VFS_STATFS(mp, &mp->mnt_stat, td);
922 	}
923 	/*
924 	 * Prevent external consumers of mount options from reading
925 	 * mnt_optnew.
926 	*/
927 	mp->mnt_optnew = NULL;
928 	if (mp->mnt_flag & MNT_UPDATE) {
929 		mp->mnt_flag &=
930 		    ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
931 		if (error) {
932 			mp->mnt_flag = flag;
933 			mp->mnt_kern_flag = kern_flag;
934 		}
935 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
936 			if (mp->mnt_syncer == NULL)
937 				error = vfs_allocate_syncvnode(mp);
938 		} else {
939 			if (mp->mnt_syncer != NULL)
940 				vrele(mp->mnt_syncer);
941 			mp->mnt_syncer = NULL;
942 		}
943 		vfs_unbusy(mp, td);
944 		VI_LOCK(vp);
945 		vp->v_iflag &= ~VI_MOUNT;
946 		VI_UNLOCK(vp);
947 		vrele(vp);
948 		return (error);
949 	}
950 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
951 	/*
952 	 * Put the new filesystem on the mount list after root.
953 	 */
954 	cache_purge(vp);
955 	if (!error) {
956 		struct vnode *newdp;
957 
958 		VI_LOCK(vp);
959 		vp->v_iflag &= ~VI_MOUNT;
960 		VI_UNLOCK(vp);
961 		vp->v_mountedhere = mp;
962 		mtx_lock(&mountlist_mtx);
963 		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
964 		mtx_unlock(&mountlist_mtx);
965 		vfs_event_signal(NULL, VQ_MOUNT, 0);
966 		if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
967 			panic("mount: lost mount");
968 		mountcheckdirs(vp, newdp);
969 		vput(newdp);
970 		VOP_UNLOCK(vp, 0, td);
971 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
972 			error = vfs_allocate_syncvnode(mp);
973 		vfs_unbusy(mp, td);
974 		if (error)
975 			vrele(vp);
976 	} else {
977 		VI_LOCK(vp);
978 		vp->v_iflag &= ~VI_MOUNT;
979 		VI_UNLOCK(vp);
980 		vfs_mount_destroy(mp, td);
981 		vput(vp);
982 	}
983 	return (error);
984 }
985 
986 /*
987  * ---------------------------------------------------------------------
988  * Unmount a filesystem.
989  *
990  * Note: unmount takes a path to the vnode mounted on as argument,
991  * not special file (as before).
992  */
993 #ifndef _SYS_SYSPROTO_H_
994 struct unmount_args {
995 	char	*path;
996 	int	flags;
997 };
998 #endif
999 /* ARGSUSED */
1000 int
1001 unmount(td, uap)
1002 	struct thread *td;
1003 	register struct unmount_args /* {
1004 		char *path;
1005 		int flags;
1006 	} */ *uap;
1007 {
1008 	struct mount *mp;
1009 	char *pathbuf;
1010 	int error, id0, id1;
1011 
1012 	if (jailed(td->td_ucred))
1013 		return (EPERM);
1014 	if (usermount == 0) {
1015 		if ((error = suser(td)) != 0)
1016 			return (error);
1017 	}
1018 
1019 	pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1020 	error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1021 	if (error) {
1022 		free(pathbuf, M_TEMP);
1023 		return (error);
1024 	}
1025 	if (uap->flags & MNT_BYFSID) {
1026 		/* Decode the filesystem ID. */
1027 		if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1028 			free(pathbuf, M_TEMP);
1029 			return (EINVAL);
1030 		}
1031 
1032 		mtx_lock(&mountlist_mtx);
1033 		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1034 			if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1035 			    mp->mnt_stat.f_fsid.val[1] == id1)
1036 				break;
1037 		}
1038 		mtx_unlock(&mountlist_mtx);
1039 	} else {
1040 		mtx_lock(&mountlist_mtx);
1041 		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1042 			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1043 				break;
1044 		}
1045 		mtx_unlock(&mountlist_mtx);
1046 	}
1047 	free(pathbuf, M_TEMP);
1048 	if (mp == NULL) {
1049 		/*
1050 		 * Previously we returned ENOENT for a nonexistent path and
1051 		 * EINVAL for a non-mountpoint.  We cannot tell these apart
1052 		 * now, so in the !MNT_BYFSID case return the more likely
1053 		 * EINVAL for compatibility.
1054 		 */
1055 		return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1056 	}
1057 
1058 	/*
1059 	 * Only privileged root, or (if MNT_USER is set) the user that did the
1060 	 * original mount is permitted to unmount this filesystem.
1061 	 */
1062 	error = vfs_suser(mp, td);
1063 	if (error)
1064 		return (error);
1065 
1066 	/*
1067 	 * Don't allow unmounting the root filesystem.
1068 	 */
1069 	if (mp->mnt_flag & MNT_ROOTFS)
1070 		return (EINVAL);
1071 	mtx_lock(&Giant);
1072 	error = dounmount(mp, uap->flags, td);
1073 	mtx_unlock(&Giant);
1074 	return (error);
1075 }
1076 
1077 /*
1078  * Do the actual filesystem unmount.
1079  */
1080 int
1081 dounmount(mp, flags, td)
1082 	struct mount *mp;
1083 	int flags;
1084 	struct thread *td;
1085 {
1086 	struct vnode *coveredvp, *fsrootvp;
1087 	int error;
1088 	int async_flag;
1089 
1090 	mtx_assert(&Giant, MA_OWNED);
1091 
1092 	if ((coveredvp = mp->mnt_vnodecovered) != NULL)
1093 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY, td);
1094 	MNT_ILOCK(mp);
1095 	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1096 		MNT_IUNLOCK(mp);
1097 		if (coveredvp)
1098 			VOP_UNLOCK(coveredvp, 0, td);
1099 		return (EBUSY);
1100 	}
1101 	mp->mnt_kern_flag |= MNTK_UNMOUNT;
1102 	/* Allow filesystems to detect that a forced unmount is in progress. */
1103 	if (flags & MNT_FORCE)
1104 		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1105 	error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1106 	    ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
1107 	if (error) {
1108 		MNT_ILOCK(mp);
1109 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1110 		if (mp->mnt_kern_flag & MNTK_MWAIT)
1111 			wakeup(mp);
1112 		MNT_IUNLOCK(mp);
1113 		if (coveredvp)
1114 			VOP_UNLOCK(coveredvp, 0, td);
1115 		return (error);
1116 	}
1117 	vn_start_write(NULL, &mp, V_WAIT);
1118 
1119 	if (mp->mnt_flag & MNT_EXPUBLIC)
1120 		vfs_setpublicfs(NULL, NULL, NULL);
1121 
1122 	vfs_msync(mp, MNT_WAIT);
1123 	async_flag = mp->mnt_flag & MNT_ASYNC;
1124 	mp->mnt_flag &= ~MNT_ASYNC;
1125 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
1126 	if (mp->mnt_syncer != NULL)
1127 		vrele(mp->mnt_syncer);
1128 	/*
1129 	 * For forced unmounts, move process cdir/rdir refs on the fs root
1130 	 * vnode to the covered vnode.  For non-forced unmounts we want
1131 	 * such references to cause an EBUSY error.
1132 	 */
1133 	if ((flags & MNT_FORCE) &&
1134 	    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1135 		if (mp->mnt_vnodecovered != NULL)
1136 			mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1137 		if (fsrootvp == rootvnode) {
1138 			vrele(rootvnode);
1139 			rootvnode = NULL;
1140 		}
1141 		vput(fsrootvp);
1142 	}
1143 	if (((mp->mnt_flag & MNT_RDONLY) ||
1144 	     (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
1145 	    (flags & MNT_FORCE)) {
1146 		error = VFS_UNMOUNT(mp, flags, td);
1147 	}
1148 	vn_finished_write(mp);
1149 	if (error) {
1150 		/* Undo cdir/rdir and rootvnode changes made above. */
1151 		if ((flags & MNT_FORCE) &&
1152 		    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1153 			if (mp->mnt_vnodecovered != NULL)
1154 				mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1155 			if (rootvnode == NULL) {
1156 				rootvnode = fsrootvp;
1157 				vref(rootvnode);
1158 			}
1159 			vput(fsrootvp);
1160 		}
1161 		if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
1162 			(void) vfs_allocate_syncvnode(mp);
1163 		MNT_ILOCK(mp);
1164 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1165 		mp->mnt_flag |= async_flag;
1166 		lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1167 		if (mp->mnt_kern_flag & MNTK_MWAIT)
1168 			wakeup(mp);
1169 		MNT_IUNLOCK(mp);
1170 		if (coveredvp)
1171 			VOP_UNLOCK(coveredvp, 0, td);
1172 		return (error);
1173 	}
1174 	mtx_lock(&mountlist_mtx);
1175 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1176 	mtx_unlock(&mountlist_mtx);
1177 	if (coveredvp != NULL) {
1178 		coveredvp->v_mountedhere = NULL;
1179 		vput(coveredvp);
1180 	}
1181 	vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1182 	vfs_mount_destroy(mp, td);
1183 	return (0);
1184 }
1185 
1186 /*
1187  * ---------------------------------------------------------------------
1188  * Mounting of root filesystem
1189  *
1190  */
1191 
1192 struct root_hold_token {
1193 	const char 			*who;
1194 	LIST_ENTRY(root_hold_token)	list;
1195 };
1196 
1197 static LIST_HEAD(, root_hold_token)	root_holds =
1198     LIST_HEAD_INITIALIZER(&root_holds);
1199 
1200 struct root_hold_token *
1201 root_mount_hold(const char *identifier)
1202 {
1203 	struct root_hold_token *h;
1204 
1205 	h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1206 	h->who = identifier;
1207 	mtx_lock(&mountlist_mtx);
1208 	LIST_INSERT_HEAD(&root_holds, h, list);
1209 	mtx_unlock(&mountlist_mtx);
1210 	return (h);
1211 }
1212 
1213 void
1214 root_mount_rel(struct root_hold_token *h)
1215 {
1216 
1217 	mtx_lock(&mountlist_mtx);
1218 	LIST_REMOVE(h, list);
1219 	wakeup(&root_holds);
1220 	mtx_unlock(&mountlist_mtx);
1221 	free(h, M_DEVBUF);
1222 }
1223 
1224 static void
1225 root_mount_wait(void)
1226 {
1227 	struct root_hold_token *h;
1228 
1229 	for (;;) {
1230 		DROP_GIANT();
1231 		g_waitidle();
1232 		PICKUP_GIANT();
1233 		mtx_lock(&mountlist_mtx);
1234 		if (LIST_EMPTY(&root_holds)) {
1235 			mtx_unlock(&mountlist_mtx);
1236 			break;
1237 		}
1238 		printf("Root mount waiting for:");
1239 		LIST_FOREACH(h, &root_holds, list)
1240 			printf(" %s", h->who);
1241 		printf("\n");
1242 		msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1243 		    hz);
1244 	}
1245 }
1246 
1247 static void
1248 set_rootvnode(struct thread *td)
1249 {
1250 	struct proc *p;
1251 
1252 	if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1253 		panic("Cannot find root vnode");
1254 
1255 	p = td->td_proc;
1256 	FILEDESC_LOCK(p->p_fd);
1257 
1258 	if (p->p_fd->fd_cdir != NULL)
1259 		vrele(p->p_fd->fd_cdir);
1260 	p->p_fd->fd_cdir = rootvnode;
1261 	VREF(rootvnode);
1262 
1263 	if (p->p_fd->fd_rdir != NULL)
1264 		vrele(p->p_fd->fd_rdir);
1265 	p->p_fd->fd_rdir = rootvnode;
1266 	VREF(rootvnode);
1267 
1268 	FILEDESC_UNLOCK(p->p_fd);
1269 
1270 	VOP_UNLOCK(rootvnode, 0, td);
1271 }
1272 
1273 /*
1274  * Mount /devfs as our root filesystem, but do not put it on the mountlist
1275  * yet.  Create a /dev -> / symlink so that absolute pathnames will lookup.
1276  */
1277 
1278 static void
1279 devfs_first(void)
1280 {
1281 	struct thread *td = curthread;
1282 	struct vfsoptlist *opts;
1283 	struct vfsconf *vfsp;
1284 	struct mount *mp = NULL;
1285 	int error;
1286 
1287 	vfsp = vfs_byname("devfs");
1288 	KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1289 	if (vfsp == NULL)
1290 		return;
1291 
1292 	mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td);
1293 
1294 	error = VFS_MOUNT(mp, td);
1295 	KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1296 	if (error)
1297 		return;
1298 
1299 	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1300 	TAILQ_INIT(opts);
1301 	mp->mnt_opt = opts;
1302 
1303 	mtx_lock(&mountlist_mtx);
1304 	TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1305 	mtx_unlock(&mountlist_mtx);
1306 
1307 	set_rootvnode(td);
1308 
1309 	error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1310 	if (error)
1311 		printf("kern_symlink /dev -> / returns %d\n", error);
1312 }
1313 
1314 /*
1315  * Surgically move our devfs to be mounted on /dev.
1316  */
1317 
1318 static void
1319 devfs_fixup(struct thread *td)
1320 {
1321 	struct nameidata nd;
1322 	int error;
1323 	struct vnode *vp, *dvp;
1324 	struct mount *mp;
1325 
1326 	/* Remove our devfs mount from the mountlist and purge the cache */
1327 	mtx_lock(&mountlist_mtx);
1328 	mp = TAILQ_FIRST(&mountlist);
1329 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1330 	mtx_unlock(&mountlist_mtx);
1331 	cache_purgevfs(mp);
1332 
1333 	VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1334 	VI_LOCK(dvp);
1335 	dvp->v_iflag &= ~VI_MOUNT;
1336 	dvp->v_mountedhere = NULL;
1337 	VI_UNLOCK(dvp);
1338 
1339 	/* Set up the real rootvnode, and purge the cache */
1340 	TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1341 	set_rootvnode(td);
1342 	cache_purgevfs(rootvnode->v_mount);
1343 
1344 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1345 	error = namei(&nd);
1346 	if (error) {
1347 		printf("Lookup of /dev for devfs, error: %d\n", error);
1348 		return;
1349 	}
1350 	NDFREE(&nd, NDF_ONLY_PNBUF);
1351 	vp = nd.ni_vp;
1352 	if (vp->v_type != VDIR) {
1353 		vput(vp);
1354 	}
1355 	error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1356 	if (error) {
1357 		vput(vp);
1358 	}
1359 	cache_purge(vp);
1360 	mp->mnt_vnodecovered = vp;
1361 	vp->v_mountedhere = mp;
1362 	mtx_lock(&mountlist_mtx);
1363 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1364 	mtx_unlock(&mountlist_mtx);
1365 	VOP_UNLOCK(vp, 0, td);
1366 	vput(dvp);
1367 	vfs_unbusy(mp, td);
1368 
1369 	/* Unlink the no longer needed /dev/dev -> / symlink */
1370 	kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1371 }
1372 
1373 /*
1374  * Report errors during filesystem mounting.
1375  */
1376 void
1377 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1378 {
1379 	struct vfsoptlist *moptlist = mp->mnt_optnew;
1380 	va_list ap;
1381 	int error, len;
1382 	char *errmsg;
1383 
1384 	error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1385 	if (error || errmsg == NULL || len <= 0)
1386 		return;
1387 
1388 	va_start(ap, fmt);
1389 	vsnprintf(errmsg, (size_t)len, fmt, ap);
1390 	va_end(ap);
1391 }
1392 
1393 /*
1394  * Find and mount the root filesystem
1395  */
1396 void
1397 vfs_mountroot(void)
1398 {
1399 	char *cp;
1400 	int error, i, asked = 0;
1401 
1402 	root_mount_wait();
1403 
1404 	mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
1405 	    NULL, NULL, mount_init, mount_fini,
1406 	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1407 	devfs_first();
1408 
1409 	/*
1410 	 * We are booted with instructions to prompt for the root filesystem.
1411 	 */
1412 	if (boothowto & RB_ASKNAME) {
1413 		if (!vfs_mountroot_ask())
1414 			return;
1415 		asked = 1;
1416 	}
1417 
1418 	/*
1419 	 * The root filesystem information is compiled in, and we are
1420 	 * booted with instructions to use it.
1421 	 */
1422 	if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1423 		if (!vfs_mountroot_try(ctrootdevname))
1424 			return;
1425 		ctrootdevname = NULL;
1426 	}
1427 
1428 	/*
1429 	 * We've been given the generic "use CDROM as root" flag.  This is
1430 	 * necessary because one media may be used in many different
1431 	 * devices, so we need to search for them.
1432 	 */
1433 	if (boothowto & RB_CDROM) {
1434 		for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1435 			if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1436 				return;
1437 		}
1438 	}
1439 
1440 	/*
1441 	 * Try to use the value read by the loader from /etc/fstab, or
1442 	 * supplied via some other means.  This is the preferred
1443 	 * mechanism.
1444 	 */
1445 	cp = getenv("vfs.root.mountfrom");
1446 	if (cp != NULL) {
1447 		error = vfs_mountroot_try(cp);
1448 		freeenv(cp);
1449 		if (!error)
1450 			return;
1451 	}
1452 
1453 	/*
1454 	 * Try values that may have been computed by code during boot
1455 	 */
1456 	if (!vfs_mountroot_try(rootdevnames[0]))
1457 		return;
1458 	if (!vfs_mountroot_try(rootdevnames[1]))
1459 		return;
1460 
1461 	/*
1462 	 * If we (still) have a compiled-in default, try it.
1463 	 */
1464 	if (ctrootdevname != NULL)
1465 		if (!vfs_mountroot_try(ctrootdevname))
1466 			return;
1467 	/*
1468 	 * Everything so far has failed, prompt on the console if we haven't
1469 	 * already tried that.
1470 	 */
1471 	if (!asked)
1472 		if (!vfs_mountroot_ask())
1473 			return;
1474 
1475 	panic("Root mount failed, startup aborted.");
1476 }
1477 
1478 /*
1479  * Mount (mountfrom) as the root filesystem.
1480  */
1481 static int
1482 vfs_mountroot_try(const char *mountfrom)
1483 {
1484 	struct mount	*mp;
1485 	char		*vfsname, *path;
1486 	time_t		timebase;
1487 	int		error;
1488 	char		patt[32];
1489 
1490 	vfsname = NULL;
1491 	path    = NULL;
1492 	mp      = NULL;
1493 	error   = EINVAL;
1494 
1495 	if (mountfrom == NULL)
1496 		return (error);		/* don't complain */
1497 	printf("Trying to mount root from %s\n", mountfrom);
1498 
1499 	/* parse vfs name and path */
1500 	vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1501 	path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1502 	vfsname[0] = path[0] = 0;
1503 	sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1504 	if (sscanf(mountfrom, patt, vfsname, path) < 1)
1505 		goto out;
1506 
1507 	if (path[0] == '\0')
1508 		strcpy(path, ROOTNAME);
1509 
1510 	error = kernel_vmount(
1511 	    MNT_RDONLY | MNT_ROOTFS,
1512 	    "fstype", vfsname,
1513 	    "fspath", "/",
1514 	    "from", path,
1515 	    NULL);
1516 	if (error == 0) {
1517 		/*
1518 		 * We mount devfs prior to mounting the / FS, so the first
1519 		 * entry will typically be devfs.
1520 		 */
1521 		mp = TAILQ_FIRST(&mountlist);
1522 		KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1523 
1524 		/*
1525 		 * Iterate over all currently mounted file systems and use
1526 		 * the time stamp found to check and/or initialize the RTC.
1527 		 * Typically devfs has no time stamp and the only other FS
1528 		 * is the actual / FS.
1529 		 * Call inittodr() only once and pass it the largest of the
1530 		 * timestamps we encounter.
1531 		 */
1532 		timebase = 0;
1533 		do {
1534 			if (mp->mnt_time > timebase)
1535 				timebase = mp->mnt_time;
1536 			mp = TAILQ_NEXT(mp, mnt_list);
1537 		} while (mp != NULL);
1538 		inittodr(timebase);
1539 
1540 		devfs_fixup(curthread);
1541 	}
1542 out:
1543 	free(path, M_MOUNT);
1544 	free(vfsname, M_MOUNT);
1545 	return (error);
1546 }
1547 
1548 /*
1549  * ---------------------------------------------------------------------
1550  * Interactive root filesystem selection code.
1551  */
1552 
1553 static int
1554 vfs_mountroot_ask(void)
1555 {
1556 	char name[128];
1557 
1558 	for(;;) {
1559 		printf("\nManual root filesystem specification:\n");
1560 		printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
1561 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
1562 		printf("                       eg. ufs:da0s1a\n");
1563 #else
1564 		printf("                       eg. ufs:/dev/da0a\n");
1565 #endif
1566 		printf("  ?                  List valid disk boot devices\n");
1567 		printf("  <empty line>       Abort manual input\n");
1568 		printf("\nmountroot> ");
1569 		gets(name, sizeof(name), 1);
1570 		if (name[0] == '\0')
1571 			return (1);
1572 		if (name[0] == '?') {
1573 			printf("\nList of GEOM managed disk devices:\n  ");
1574 			g_dev_print();
1575 			continue;
1576 		}
1577 		if (!vfs_mountroot_try(name))
1578 			return (0);
1579 	}
1580 }
1581 
1582 /*
1583  * ---------------------------------------------------------------------
1584  * Functions for querying mount options/arguments from filesystems.
1585  */
1586 
1587 /*
1588  * Check that no unknown options are given
1589  */
1590 int
1591 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1592 {
1593 	struct vfsopt *opt;
1594 	const char **t, *p;
1595 
1596 
1597 	TAILQ_FOREACH(opt, opts, link) {
1598 		p = opt->name;
1599 		if (p[0] == 'n' && p[1] == 'o')
1600 			p += 2;
1601 		for(t = global_opts; *t != NULL; t++)
1602 			if (!strcmp(*t, p))
1603 				break;
1604 		if (*t != NULL)
1605 			continue;
1606 		for(t = legal; *t != NULL; t++)
1607 			if (!strcmp(*t, p))
1608 				break;
1609 		if (*t != NULL)
1610 			continue;
1611 		printf("mount option <%s> is unknown\n", p);
1612 		return (EINVAL);
1613 	}
1614 	return (0);
1615 }
1616 
1617 /*
1618  * Get a mount option by its name.
1619  *
1620  * Return 0 if the option was found, ENOENT otherwise.
1621  * If len is non-NULL it will be filled with the length
1622  * of the option. If buf is non-NULL, it will be filled
1623  * with the address of the option.
1624  */
1625 int
1626 vfs_getopt(opts, name, buf, len)
1627 	struct vfsoptlist *opts;
1628 	const char *name;
1629 	void **buf;
1630 	int *len;
1631 {
1632 	struct vfsopt *opt;
1633 
1634 	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1635 
1636 	TAILQ_FOREACH(opt, opts, link) {
1637 		if (strcmp(name, opt->name) == 0) {
1638 			if (len != NULL)
1639 				*len = opt->len;
1640 			if (buf != NULL)
1641 				*buf = opt->value;
1642 			return (0);
1643 		}
1644 	}
1645 	return (ENOENT);
1646 }
1647 
1648 static int
1649 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1650 {
1651 	struct vfsopt *opt;
1652 	int i;
1653 
1654 	if (opts == NULL)
1655 		return (-1);
1656 
1657 	i = 0;
1658 	TAILQ_FOREACH(opt, opts, link) {
1659 		if (strcmp(name, opt->name) == 0)
1660 			return (i);
1661 		++i;
1662 	}
1663 	return (-1);
1664 }
1665 
1666 char *
1667 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1668 {
1669 	struct vfsopt *opt;
1670 
1671 	*error = 0;
1672 	TAILQ_FOREACH(opt, opts, link) {
1673 		if (strcmp(name, opt->name) != 0)
1674 			continue;
1675 		if (((char *)opt->value)[opt->len - 1] != '\0') {
1676 			*error = EINVAL;
1677 			return (NULL);
1678 		}
1679 		return (opt->value);
1680 	}
1681 	return (NULL);
1682 }
1683 
1684 int
1685 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1686 {
1687 	struct vfsopt *opt;
1688 
1689 	TAILQ_FOREACH(opt, opts, link) {
1690 		if (strcmp(name, opt->name) == 0) {
1691 			if (w != NULL)
1692 				*w |= val;
1693 			return (1);
1694 		}
1695 	}
1696 	if (w != NULL)
1697 		*w &= ~val;
1698 	return (0);
1699 }
1700 
1701 int
1702 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1703 {
1704 	va_list ap;
1705 	struct vfsopt *opt;
1706 	int ret;
1707 
1708 	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1709 
1710 	TAILQ_FOREACH(opt, opts, link) {
1711 		if (strcmp(name, opt->name) != 0)
1712 			continue;
1713 		if (((char *)opt->value)[opt->len - 1] != '\0')
1714 			return (0);
1715 		va_start(ap, fmt);
1716 		ret = vsscanf(opt->value, fmt, ap);
1717 		va_end(ap);
1718 		return (ret);
1719 	}
1720 	return (0);
1721 }
1722 
1723 /*
1724  * Find and copy a mount option.
1725  *
1726  * The size of the buffer has to be specified
1727  * in len, if it is not the same length as the
1728  * mount option, EINVAL is returned.
1729  * Returns ENOENT if the option is not found.
1730  */
1731 int
1732 vfs_copyopt(opts, name, dest, len)
1733 	struct vfsoptlist *opts;
1734 	const char *name;
1735 	void *dest;
1736 	int len;
1737 {
1738 	struct vfsopt *opt;
1739 
1740 	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1741 
1742 	TAILQ_FOREACH(opt, opts, link) {
1743 		if (strcmp(name, opt->name) == 0) {
1744 			if (len != opt->len)
1745 				return (EINVAL);
1746 			bcopy(opt->value, dest, opt->len);
1747 			return (0);
1748 		}
1749 	}
1750 	return (ENOENT);
1751 }
1752 
1753 /*
1754  * This is a helper function for filesystems to traverse their
1755  * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
1756  */
1757 
1758 struct vnode *
1759 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
1760 {
1761 	struct vnode *vp;
1762 
1763 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1764 
1765 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1766 	vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
1767 	while (vp != NULL && vp->v_type == VMARKER)
1768 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1769 
1770 	/* Check if we are done */
1771 	if (vp == NULL) {
1772 		__mnt_vnode_markerfree(mvp, mp);
1773 		return (NULL);
1774 	}
1775 	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1776 	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1777 	return (vp);
1778 }
1779 
1780 struct vnode *
1781 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
1782 {
1783 	struct vnode *vp;
1784 
1785 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1786 
1787 	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1788 	while (vp != NULL && vp->v_type == VMARKER)
1789 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1790 
1791 	/* Check if we are done */
1792 	if (vp == NULL) {
1793 		*mvp = NULL;
1794 		return (NULL);
1795 	}
1796 	mp->mnt_holdcnt++;
1797 	MNT_IUNLOCK(mp);
1798 	*mvp = (struct vnode *) malloc(sizeof(struct vnode),
1799 				       M_VNODE_MARKER,
1800 				       M_WAITOK | M_ZERO);
1801 	MNT_ILOCK(mp);
1802 	(*mvp)->v_type = VMARKER;
1803 
1804 	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1805 	while (vp != NULL && vp->v_type == VMARKER)
1806 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1807 
1808 	/* Check if we are done */
1809 	if (vp == NULL) {
1810 		MNT_IUNLOCK(mp);
1811 		free(*mvp, M_VNODE_MARKER);
1812 		MNT_ILOCK(mp);
1813 		*mvp = NULL;
1814 		mp->mnt_holdcnt--;
1815 		if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1816 			wakeup(&mp->mnt_holdcnt);
1817 		return (NULL);
1818 	}
1819 	mp->mnt_markercnt++;
1820 	(*mvp)->v_mount = mp;
1821 	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1822 	return (vp);
1823 }
1824 
1825 
1826 void
1827 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
1828 {
1829 
1830 	if (*mvp == NULL)
1831 		return;
1832 
1833 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1834 
1835 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1836 	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1837 	MNT_IUNLOCK(mp);
1838 	free(*mvp, M_VNODE_MARKER);
1839 	MNT_ILOCK(mp);
1840 	*mvp = NULL;
1841 
1842 	mp->mnt_markercnt--;
1843 	mp->mnt_holdcnt--;
1844 	if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1845 		wakeup(&mp->mnt_holdcnt);
1846 }
1847 
1848 
1849 int
1850 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
1851 {
1852 	int error;
1853 
1854 	error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
1855 	if (sbp != &mp->mnt_stat)
1856 		*sbp = mp->mnt_stat;
1857 	return (error);
1858 }
1859 
1860 void
1861 vfs_mountedfrom(struct mount *mp, const char *from)
1862 {
1863 
1864 	bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1865 	strlcpy(mp->mnt_stat.f_mntfromname, from,
1866 	    sizeof mp->mnt_stat.f_mntfromname);
1867 }
1868 
1869 /*
1870  * ---------------------------------------------------------------------
1871  * This is the api for building mount args and mounting filesystems from
1872  * inside the kernel.
1873  *
1874  * The API works by accumulation of individual args.  First error is
1875  * latched.
1876  *
1877  * XXX: should be documented in new manpage kernel_mount(9)
1878  */
1879 
1880 /* A memory allocation which must be freed when we are done */
1881 struct mntaarg {
1882 	SLIST_ENTRY(mntaarg)	next;
1883 };
1884 
1885 /* The header for the mount arguments */
1886 struct mntarg {
1887 	struct iovec *v;
1888 	int len;
1889 	int error;
1890 	SLIST_HEAD(, mntaarg)	list;
1891 };
1892 
1893 /*
1894  * Add a boolean argument.
1895  *
1896  * flag is the boolean value.
1897  * name must start with "no".
1898  */
1899 struct mntarg *
1900 mount_argb(struct mntarg *ma, int flag, const char *name)
1901 {
1902 
1903 	KASSERT(name[0] == 'n' && name[1] == 'o',
1904 	    ("mount_argb(...,%s): name must start with 'no'", name));
1905 
1906 	return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1907 }
1908 
1909 /*
1910  * Add an argument printf style
1911  */
1912 struct mntarg *
1913 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1914 {
1915 	va_list ap;
1916 	struct mntaarg *maa;
1917 	struct sbuf *sb;
1918 	int len;
1919 
1920 	if (ma == NULL) {
1921 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1922 		SLIST_INIT(&ma->list);
1923 	}
1924 	if (ma->error)
1925 		return (ma);
1926 
1927 	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1928 	    M_MOUNT, M_WAITOK);
1929 	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1930 	ma->v[ma->len].iov_len = strlen(name) + 1;
1931 	ma->len++;
1932 
1933 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
1934 	va_start(ap, fmt);
1935 	sbuf_vprintf(sb, fmt, ap);
1936 	va_end(ap);
1937 	sbuf_finish(sb);
1938 	len = sbuf_len(sb) + 1;
1939 	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1940 	SLIST_INSERT_HEAD(&ma->list, maa, next);
1941 	bcopy(sbuf_data(sb), maa + 1, len);
1942 	sbuf_delete(sb);
1943 
1944 	ma->v[ma->len].iov_base = maa + 1;
1945 	ma->v[ma->len].iov_len = len;
1946 	ma->len++;
1947 
1948 	return (ma);
1949 }
1950 
1951 /*
1952  * Add an argument which is a userland string.
1953  */
1954 struct mntarg *
1955 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1956 {
1957 	struct mntaarg *maa;
1958 	char *tbuf;
1959 
1960 	if (val == NULL)
1961 		return (ma);
1962 	if (ma == NULL) {
1963 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1964 		SLIST_INIT(&ma->list);
1965 	}
1966 	if (ma->error)
1967 		return (ma);
1968 	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1969 	SLIST_INSERT_HEAD(&ma->list, maa, next);
1970 	tbuf = (void *)(maa + 1);
1971 	ma->error = copyinstr(val, tbuf, len, NULL);
1972 	return (mount_arg(ma, name, tbuf, -1));
1973 }
1974 
1975 /*
1976  * Plain argument.
1977  *
1978  * If length is -1, use printf.
1979  */
1980 struct mntarg *
1981 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1982 {
1983 
1984 	if (ma == NULL) {
1985 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1986 		SLIST_INIT(&ma->list);
1987 	}
1988 	if (ma->error)
1989 		return (ma);
1990 
1991 	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1992 	    M_MOUNT, M_WAITOK);
1993 	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1994 	ma->v[ma->len].iov_len = strlen(name) + 1;
1995 	ma->len++;
1996 
1997 	ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1998 	if (len < 0)
1999 		ma->v[ma->len].iov_len = strlen(val) + 1;
2000 	else
2001 		ma->v[ma->len].iov_len = len;
2002 	ma->len++;
2003 	return (ma);
2004 }
2005 
2006 /*
2007  * Free a mntarg structure
2008  */
2009 static void
2010 free_mntarg(struct mntarg *ma)
2011 {
2012 	struct mntaarg *maa;
2013 
2014 	while (!SLIST_EMPTY(&ma->list)) {
2015 		maa = SLIST_FIRST(&ma->list);
2016 		SLIST_REMOVE_HEAD(&ma->list, next);
2017 		free(maa, M_MOUNT);
2018 	}
2019 	free(ma->v, M_MOUNT);
2020 	free(ma, M_MOUNT);
2021 }
2022 
2023 /*
2024  * Mount a filesystem
2025  */
2026 int
2027 kernel_mount(struct mntarg *ma, int flags)
2028 {
2029 	struct uio auio;
2030 	int error;
2031 
2032 	KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2033 	KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2034 	KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2035 
2036 	auio.uio_iov = ma->v;
2037 	auio.uio_iovcnt = ma->len;
2038 	auio.uio_segflg = UIO_SYSSPACE;
2039 
2040 	error = ma->error;
2041 	if (!error)
2042 		error = vfs_donmount(curthread, flags, &auio);
2043 	free_mntarg(ma);
2044 	return (error);
2045 }
2046 
2047 /*
2048  * A printflike function to mount a filesystem.
2049  */
2050 int
2051 kernel_vmount(int flags, ...)
2052 {
2053 	struct mntarg *ma = NULL;
2054 	va_list ap;
2055 	const char *cp;
2056 	const void *vp;
2057 	int error;
2058 
2059 	va_start(ap, flags);
2060 	for (;;) {
2061 		cp = va_arg(ap, const char *);
2062 		if (cp == NULL)
2063 			break;
2064 		vp = va_arg(ap, const void *);
2065 		ma = mount_arg(ma, cp, vp, -1);
2066 	}
2067 	va_end(ap);
2068 
2069 	error = kernel_mount(ma, flags);
2070 	return (error);
2071 }
2072