vfs_mount.c revision 157322
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: head/sys/kern/vfs_mount.c 157322 2006-03-31 03:49:51Z jeff $");
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
77static int	vfs_domount(struct thread *td, const char *fstype,
78		    char *fspath, int fsflags, void *fsdata);
79static struct mount *vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp,
80		    const char *fspath, struct thread *td);
81static int	vfs_mountroot_ask(void);
82static int	vfs_mountroot_try(const char *mountfrom);
83static int	vfs_donmount(struct thread *td, int fsflags,
84		    struct uio *fsoptions);
85static void	free_mntarg(struct mntarg *ma);
86static void	vfs_mount_destroy(struct mount *, struct thread *);
87static int	vfs_getopt_pos(struct vfsoptlist *opts, const char *name);
88
89static int	usermount = 0;
90SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
91    "Unprivileged users may mount and unmount file systems");
92
93MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
94MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
95static uma_zone_t mount_zone;
96
97/* List of mounted filesystems. */
98struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
99
100/* For any iteration/modification of mountlist */
101struct mtx mountlist_mtx;
102MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
103
104TAILQ_HEAD(vfsoptlist, vfsopt);
105struct 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 */
116struct 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 */
132static 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 */
147static char *cdrom_rootdevnames[] = {
148	"cd9660:cd0",
149	"cd9660:acd0",
150	NULL
151};
152
153/* legacy find-root code */
154char		*rootdevnames[2] = {NULL, NULL};
155#ifndef ROOTDEVNAME
156#  define ROOTDEVNAME NULL
157#endif
158static const char	*ctrootdevname = ROOTDEVNAME;
159
160/*
161 * ---------------------------------------------------------------------
162 * Functions for building and sanitizing the mount options
163 */
164
165/* Remove one mount option. */
166static void
167vfs_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. */
183static void
184vfs_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 */
198static int
199vfs_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 */
219static void
220vfs_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 */
241static int
242vfs_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);
310bad:
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 */
319static void
320vfs_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);
352next:
353		continue;
354	}
355}
356
357/*
358 * ---------------------------------------------------------------------
359 * Mount a filesystem
360 */
361int
362nmount(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
410void
411vfs_ref(struct mount *mp)
412{
413
414	MNT_ILOCK(mp);
415	MNT_REF(mp);
416	MNT_IUNLOCK(mp);
417}
418
419void
420vfs_rel(struct mount *mp)
421{
422
423	MNT_ILOCK(mp);
424	MNT_REL(mp);
425	MNT_IUNLOCK(mp);
426}
427
428static int
429mount_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
439static void
440mount_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 */
452static struct mount *
453vfs_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 */
487static void
488vfs_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		panic("unmount: dangling vnode");
538	MNT_ILOCK(mp);
539	if (mp->mnt_kern_flag & MNTK_MWAIT)
540		wakeup(mp);
541	if (mp->mnt_writeopcount != 0)
542		panic("vfs_mount_destroy: nonzero writeopcount");
543	if (mp->mnt_secondary_writes != 0)
544		panic("vfs_mount_destroy: nonzero secondary_writes");
545	if (mp->mnt_nvnodelistsize != 0)
546		panic("vfs_mount_destroy: nonzero nvnodelistsize");
547	mp->mnt_writeopcount = -1000;
548	mp->mnt_nvnodelistsize = -1000;
549	mp->mnt_secondary_writes = -1000;
550	MNT_IUNLOCK(mp);
551#ifdef MAC
552	mac_destroy_mount(mp);
553#endif
554	if (mp->mnt_opt != NULL)
555		vfs_freeopts(mp->mnt_opt);
556	crfree(mp->mnt_cred);
557	uma_zfree(mount_zone, mp);
558}
559
560static int
561vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
562{
563	struct vfsoptlist *optlist;
564	struct vfsopt *opt;
565	char *fstype, *fspath, *errmsg;
566	int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
567
568	errmsg_len = 0;
569	errmsg_pos = -1;
570
571	error = vfs_buildopts(fsoptions, &optlist);
572	if (error)
573		return (error);
574
575	if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
576		errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
577	else
578		errmsg_len = 0;
579
580	/*
581	 * We need these two options before the others,
582	 * and they are mandatory for any filesystem.
583	 * Ensure they are NUL terminated as well.
584	 */
585	fstypelen = 0;
586	error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
587	if (error || fstype[fstypelen - 1] != '\0') {
588		error = EINVAL;
589		if (errmsg != NULL)
590			strncpy(errmsg, "Invalid fstype", errmsg_len);
591		goto bail;
592	}
593	fspathlen = 0;
594	error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
595	if (error || fspath[fspathlen - 1] != '\0') {
596		error = EINVAL;
597		if (errmsg != NULL)
598			strncpy(errmsg, "Invalid fspath", errmsg_len);
599		goto bail;
600	}
601
602	/*
603	 * We need to see if we have the "update" option
604	 * before we call vfs_domount(), since vfs_domount() has special
605	 * logic based on MNT_UPDATE.  This is very important
606	 * when we want to update the root filesystem.
607	 */
608	TAILQ_FOREACH(opt, optlist, link) {
609		if (strcmp(opt->name, "update") == 0)
610			fsflags |= MNT_UPDATE;
611		else if (strcmp(opt->name, "async") == 0)
612			fsflags |= MNT_ASYNC;
613		else if (strcmp(opt->name, "force") == 0)
614			fsflags |= MNT_FORCE;
615		else if (strcmp(opt->name, "multilabel") == 0)
616			fsflags |= MNT_MULTILABEL;
617		else if (strcmp(opt->name, "noasync") == 0)
618			fsflags &= ~MNT_ASYNC;
619		else if (strcmp(opt->name, "noatime") == 0)
620			fsflags |= MNT_NOATIME;
621		else if (strcmp(opt->name, "noclusterr") == 0)
622			fsflags |= MNT_NOCLUSTERR;
623		else if (strcmp(opt->name, "noclusterw") == 0)
624			fsflags |= MNT_NOCLUSTERW;
625		else if (strcmp(opt->name, "noexec") == 0)
626			fsflags |= MNT_NOEXEC;
627		else if (strcmp(opt->name, "nosuid") == 0)
628			fsflags |= MNT_NOSUID;
629		else if (strcmp(opt->name, "nosymfollow") == 0)
630			fsflags |= MNT_NOSYMFOLLOW;
631		else if (strcmp(opt->name, "noro") == 0 ||
632		    strcmp(opt->name, "rw") == 0)
633			fsflags &= ~MNT_RDONLY;
634		else if (strcmp(opt->name, "ro") == 0 ||
635		    strcmp(opt->name, "rdonly") == 0)
636			fsflags |= MNT_RDONLY;
637		else if (strcmp(opt->name, "snapshot") == 0)
638			fsflags |= MNT_SNAPSHOT;
639		else if (strcmp(opt->name, "suiddir") == 0)
640			fsflags |= MNT_SUIDDIR;
641		else if (strcmp(opt->name, "sync") == 0)
642			fsflags |= MNT_SYNCHRONOUS;
643		else if (strcmp(opt->name, "union") == 0)
644			fsflags |= MNT_UNION;
645	}
646
647	/*
648	 * Be ultra-paranoid about making sure the type and fspath
649	 * variables will fit in our mp buffers, including the
650	 * terminating NUL.
651	 */
652	if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
653		error = ENAMETOOLONG;
654		goto bail;
655	}
656
657	mtx_lock(&Giant);
658	error = vfs_domount(td, fstype, fspath, fsflags, optlist);
659	mtx_unlock(&Giant);
660bail:
661	/* copyout the errmsg */
662	if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
663	    && errmsg_len > 0 && errmsg != NULL) {
664		if (fsoptions->uio_segflg == UIO_SYSSPACE) {
665			strncpy(fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
666			    errmsg,
667			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
668		} else {
669			copystr(errmsg,
670			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
671			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len,
672			    NULL);
673		}
674	}
675
676	if (error != 0)
677		vfs_freeopts(optlist);
678	return (error);
679}
680
681/*
682 * ---------------------------------------------------------------------
683 * Old mount API.
684 */
685#ifndef _SYS_SYSPROTO_H_
686struct mount_args {
687	char	*type;
688	char	*path;
689	int	flags;
690	caddr_t	data;
691};
692#endif
693/* ARGSUSED */
694int
695mount(td, uap)
696	struct thread *td;
697	struct mount_args /* {
698		char *type;
699		char *path;
700		int flags;
701		caddr_t data;
702	} */ *uap;
703{
704	char *fstype;
705	struct vfsconf *vfsp = NULL;
706	struct mntarg *ma = NULL;
707	int error;
708
709	/* Kick out MNT_ROOTFS early as it is legal internally */
710	uap->flags &= ~MNT_ROOTFS;
711
712	if (uap->data == NULL)
713		return (EINVAL);
714
715	fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
716	error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
717	if (!error) {
718		mtx_lock(&Giant);	/* XXX ? */
719		vfsp = vfs_byname_kld(fstype, td, &error);
720		mtx_unlock(&Giant);
721	}
722	free(fstype, M_TEMP);
723	if (error)
724		return (error);
725	if (vfsp == NULL)
726		return (ENOENT);
727	if (vfsp->vfc_vfsops->vfs_cmount == NULL)
728		return (EOPNOTSUPP);
729
730	ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
731	ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
732	ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
733	ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
734	ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
735
736	error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
737	return (error);
738}
739
740
741/*
742 * vfs_domount(): actually attempt a filesystem mount.
743 */
744static int
745vfs_domount(
746	struct thread *td,	/* Flags common to all filesystems. */
747	const char *fstype,	/* Filesystem type. */
748	char *fspath,		/* Mount path. */
749	int fsflags,		/* Flags common to all filesystems. */
750	void *fsdata		/* Options local to the filesystem. */
751	)
752{
753	struct vnode *vp;
754	struct mount *mp;
755	struct vfsconf *vfsp;
756	int error, flag = 0, kern_flag = 0;
757	struct vattr va;
758	struct nameidata nd;
759
760	mtx_assert(&Giant, MA_OWNED);
761	/*
762	 * Be ultra-paranoid about making sure the type and fspath
763	 * variables will fit in our mp buffers, including the
764	 * terminating NUL.
765	 */
766	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
767		return (ENAMETOOLONG);
768
769	if (jailed(td->td_ucred))
770		return (EPERM);
771	if (usermount == 0) {
772		if ((error = suser(td)) != 0)
773			return (error);
774	}
775
776	/*
777	 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
778	 */
779	if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) {
780		if ((error = suser(td)) != 0)
781			return (error);
782	}
783	/*
784	 * Silently enforce MNT_NOSUID and MNT_USER for
785	 * unprivileged users.
786	 */
787	if (suser(td) != 0)
788		fsflags |= MNT_NOSUID | MNT_USER;
789
790	/* Load KLDs before we lock the covered vnode to avoid reversals. */
791	vfsp = NULL;
792	if ((fsflags & MNT_UPDATE) == 0) {
793		/* Don't try to load KLDs if we're mounting the root. */
794		if (fsflags & MNT_ROOTFS)
795			vfsp = vfs_byname(fstype);
796		else
797			vfsp = vfs_byname_kld(fstype, td, &error);
798		if (vfsp == NULL)
799			return (ENODEV);
800	}
801	/*
802	 * Get vnode to be covered
803	 */
804	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
805	if ((error = namei(&nd)) != 0)
806		return (error);
807	NDFREE(&nd, NDF_ONLY_PNBUF);
808	vp = nd.ni_vp;
809	if (fsflags & MNT_UPDATE) {
810		if ((vp->v_vflag & VV_ROOT) == 0) {
811			vput(vp);
812			return (EINVAL);
813		}
814		mp = vp->v_mount;
815		flag = mp->mnt_flag;
816		kern_flag = mp->mnt_kern_flag;
817		/*
818		 * We only allow the filesystem to be reloaded if it
819		 * is currently mounted read-only.
820		 */
821		if ((fsflags & MNT_RELOAD) &&
822		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
823			vput(vp);
824			return (EOPNOTSUPP);	/* Needs translation */
825		}
826		/*
827		 * Only privileged root, or (if MNT_USER is set) the user that
828		 * did the original mount is permitted to update it.
829		 */
830		error = vfs_suser(mp, td);
831		if (error) {
832			vput(vp);
833			return (error);
834		}
835		if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
836			vput(vp);
837			return (EBUSY);
838		}
839		VI_LOCK(vp);
840		if ((vp->v_iflag & VI_MOUNT) != 0 ||
841		    vp->v_mountedhere != NULL) {
842			VI_UNLOCK(vp);
843			vfs_unbusy(mp, td);
844			vput(vp);
845			return (EBUSY);
846		}
847		vp->v_iflag |= VI_MOUNT;
848		VI_UNLOCK(vp);
849		mp->mnt_flag |= fsflags &
850		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
851		VOP_UNLOCK(vp, 0, td);
852		mp->mnt_optnew = fsdata;
853		vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
854	} else {
855		/*
856		 * If the user is not root, ensure that they own the directory
857		 * onto which we are attempting to mount.
858		 */
859		error = VOP_GETATTR(vp, &va, td->td_ucred, td);
860		if (error) {
861			vput(vp);
862			return (error);
863		}
864		if (va.va_uid != td->td_ucred->cr_uid) {
865			if ((error = suser(td)) != 0) {
866				vput(vp);
867				return (error);
868			}
869		}
870		error = vinvalbuf(vp, V_SAVE, td, 0, 0);
871		if (error != 0) {
872			vput(vp);
873			return (error);
874		}
875		if (vp->v_type != VDIR) {
876			vput(vp);
877			return (ENOTDIR);
878		}
879		VI_LOCK(vp);
880		if ((vp->v_iflag & VI_MOUNT) != 0 ||
881		    vp->v_mountedhere != NULL) {
882			VI_UNLOCK(vp);
883			vput(vp);
884			return (EBUSY);
885		}
886		vp->v_iflag |= VI_MOUNT;
887		VI_UNLOCK(vp);
888
889		/*
890		 * Allocate and initialize the filesystem.
891		 */
892		mp = vfs_mount_alloc(vp, vfsp, fspath, td);
893		VOP_UNLOCK(vp, 0, td);
894
895		/* XXXMAC: pass to vfs_mount_alloc? */
896		mp->mnt_optnew = fsdata;
897	}
898
899	/*
900	 * Set the mount level flags.
901	 */
902	if (fsflags & MNT_RDONLY)
903		mp->mnt_flag |= MNT_RDONLY;
904	mp->mnt_flag &=~ MNT_UPDATEMASK;
905	mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
906	/*
907	 * Mount the filesystem.
908	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
909	 * get.  No freeing of cn_pnbuf.
910	 */
911        error = VFS_MOUNT(mp, td);
912	if (!error) {
913		if (mp->mnt_opt != NULL)
914			vfs_freeopts(mp->mnt_opt);
915		mp->mnt_opt = mp->mnt_optnew;
916		(void)VFS_STATFS(mp, &mp->mnt_stat, td);
917	}
918	/*
919	 * Prevent external consumers of mount options from reading
920	 * mnt_optnew.
921	*/
922	mp->mnt_optnew = NULL;
923	if (mp->mnt_flag & MNT_UPDATE) {
924		mp->mnt_flag &=
925		    ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
926		if (error) {
927			mp->mnt_flag = flag;
928			mp->mnt_kern_flag = kern_flag;
929		}
930		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
931			if (mp->mnt_syncer == NULL)
932				error = vfs_allocate_syncvnode(mp);
933		} else {
934			if (mp->mnt_syncer != NULL)
935				vrele(mp->mnt_syncer);
936			mp->mnt_syncer = NULL;
937		}
938		vfs_unbusy(mp, td);
939		VI_LOCK(vp);
940		vp->v_iflag &= ~VI_MOUNT;
941		VI_UNLOCK(vp);
942		vrele(vp);
943		return (error);
944	}
945	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
946	/*
947	 * Put the new filesystem on the mount list after root.
948	 */
949	cache_purge(vp);
950	if (!error) {
951		struct vnode *newdp;
952
953		VI_LOCK(vp);
954		vp->v_iflag &= ~VI_MOUNT;
955		VI_UNLOCK(vp);
956		vp->v_mountedhere = mp;
957		mtx_lock(&mountlist_mtx);
958		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
959		mtx_unlock(&mountlist_mtx);
960		vfs_event_signal(NULL, VQ_MOUNT, 0);
961		if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
962			panic("mount: lost mount");
963		mountcheckdirs(vp, newdp);
964		vput(newdp);
965		VOP_UNLOCK(vp, 0, td);
966		if ((mp->mnt_flag & MNT_RDONLY) == 0)
967			error = vfs_allocate_syncvnode(mp);
968		vfs_unbusy(mp, td);
969		if (error)
970			vrele(vp);
971	} else {
972		VI_LOCK(vp);
973		vp->v_iflag &= ~VI_MOUNT;
974		VI_UNLOCK(vp);
975		vfs_mount_destroy(mp, td);
976		vput(vp);
977	}
978	return (error);
979}
980
981/*
982 * ---------------------------------------------------------------------
983 * Unmount a filesystem.
984 *
985 * Note: unmount takes a path to the vnode mounted on as argument,
986 * not special file (as before).
987 */
988#ifndef _SYS_SYSPROTO_H_
989struct unmount_args {
990	char	*path;
991	int	flags;
992};
993#endif
994/* ARGSUSED */
995int
996unmount(td, uap)
997	struct thread *td;
998	register struct unmount_args /* {
999		char *path;
1000		int flags;
1001	} */ *uap;
1002{
1003	struct mount *mp;
1004	char *pathbuf;
1005	int error, id0, id1;
1006
1007	if (jailed(td->td_ucred))
1008		return (EPERM);
1009	if (usermount == 0) {
1010		if ((error = suser(td)) != 0)
1011			return (error);
1012	}
1013
1014	pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1015	error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1016	if (error) {
1017		free(pathbuf, M_TEMP);
1018		return (error);
1019	}
1020	if (uap->flags & MNT_BYFSID) {
1021		/* Decode the filesystem ID. */
1022		if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1023			free(pathbuf, M_TEMP);
1024			return (EINVAL);
1025		}
1026
1027		mtx_lock(&mountlist_mtx);
1028		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1029			if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1030			    mp->mnt_stat.f_fsid.val[1] == id1)
1031				break;
1032		}
1033		mtx_unlock(&mountlist_mtx);
1034	} else {
1035		mtx_lock(&mountlist_mtx);
1036		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1037			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1038				break;
1039		}
1040		mtx_unlock(&mountlist_mtx);
1041	}
1042	free(pathbuf, M_TEMP);
1043	if (mp == NULL) {
1044		/*
1045		 * Previously we returned ENOENT for a nonexistent path and
1046		 * EINVAL for a non-mountpoint.  We cannot tell these apart
1047		 * now, so in the !MNT_BYFSID case return the more likely
1048		 * EINVAL for compatibility.
1049		 */
1050		return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1051	}
1052
1053	/*
1054	 * Only privileged root, or (if MNT_USER is set) the user that did the
1055	 * original mount is permitted to unmount this filesystem.
1056	 */
1057	error = vfs_suser(mp, td);
1058	if (error)
1059		return (error);
1060
1061	/*
1062	 * Don't allow unmounting the root filesystem.
1063	 */
1064	if (mp->mnt_flag & MNT_ROOTFS)
1065		return (EINVAL);
1066	mtx_lock(&Giant);
1067	error = dounmount(mp, uap->flags, td);
1068	mtx_unlock(&Giant);
1069	return (error);
1070}
1071
1072/*
1073 * Do the actual filesystem unmount.
1074 */
1075int
1076dounmount(mp, flags, td)
1077	struct mount *mp;
1078	int flags;
1079	struct thread *td;
1080{
1081	struct vnode *coveredvp, *fsrootvp;
1082	int error;
1083	int async_flag;
1084
1085	mtx_assert(&Giant, MA_OWNED);
1086
1087	if ((coveredvp = mp->mnt_vnodecovered) != NULL)
1088		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY, td);
1089	MNT_ILOCK(mp);
1090	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1091		MNT_IUNLOCK(mp);
1092		if (coveredvp)
1093			VOP_UNLOCK(coveredvp, 0, td);
1094		return (EBUSY);
1095	}
1096	mp->mnt_kern_flag |= MNTK_UNMOUNT;
1097	/* Allow filesystems to detect that a forced unmount is in progress. */
1098	if (flags & MNT_FORCE)
1099		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1100	error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1101	    ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
1102	if (error) {
1103		MNT_ILOCK(mp);
1104		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1105		if (mp->mnt_kern_flag & MNTK_MWAIT)
1106			wakeup(mp);
1107		MNT_IUNLOCK(mp);
1108		if (coveredvp)
1109			VOP_UNLOCK(coveredvp, 0, td);
1110		return (error);
1111	}
1112	vn_start_write(NULL, &mp, V_WAIT);
1113
1114	if (mp->mnt_flag & MNT_EXPUBLIC)
1115		vfs_setpublicfs(NULL, NULL, NULL);
1116
1117	vfs_msync(mp, MNT_WAIT);
1118	async_flag = mp->mnt_flag & MNT_ASYNC;
1119	mp->mnt_flag &= ~MNT_ASYNC;
1120	cache_purgevfs(mp);	/* remove cache entries for this file sys */
1121	if (mp->mnt_syncer != NULL)
1122		vrele(mp->mnt_syncer);
1123	/*
1124	 * For forced unmounts, move process cdir/rdir refs on the fs root
1125	 * vnode to the covered vnode.  For non-forced unmounts we want
1126	 * such references to cause an EBUSY error.
1127	 */
1128	if ((flags & MNT_FORCE) &&
1129	    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1130		if (mp->mnt_vnodecovered != NULL)
1131			mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1132		if (fsrootvp == rootvnode) {
1133			vrele(rootvnode);
1134			rootvnode = NULL;
1135		}
1136		vput(fsrootvp);
1137	}
1138	if (((mp->mnt_flag & MNT_RDONLY) ||
1139	     (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
1140	    (flags & MNT_FORCE)) {
1141		error = VFS_UNMOUNT(mp, flags, td);
1142	}
1143	vn_finished_write(mp);
1144	if (error) {
1145		/* Undo cdir/rdir and rootvnode changes made above. */
1146		if ((flags & MNT_FORCE) &&
1147		    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1148			if (mp->mnt_vnodecovered != NULL)
1149				mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1150			if (rootvnode == NULL) {
1151				rootvnode = fsrootvp;
1152				vref(rootvnode);
1153			}
1154			vput(fsrootvp);
1155		}
1156		if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
1157			(void) vfs_allocate_syncvnode(mp);
1158		MNT_ILOCK(mp);
1159		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1160		mp->mnt_flag |= async_flag;
1161		lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1162		if (mp->mnt_kern_flag & MNTK_MWAIT)
1163			wakeup(mp);
1164		MNT_IUNLOCK(mp);
1165		if (coveredvp)
1166			VOP_UNLOCK(coveredvp, 0, td);
1167		return (error);
1168	}
1169	mtx_lock(&mountlist_mtx);
1170	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1171	mtx_unlock(&mountlist_mtx);
1172	if (coveredvp != NULL) {
1173		coveredvp->v_mountedhere = NULL;
1174		vput(coveredvp);
1175	}
1176	vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1177	vfs_mount_destroy(mp, td);
1178	return (0);
1179}
1180
1181/*
1182 * ---------------------------------------------------------------------
1183 * Mounting of root filesystem
1184 *
1185 */
1186
1187struct root_hold_token {
1188	const char 			*who;
1189	LIST_ENTRY(root_hold_token)	list;
1190};
1191
1192static LIST_HEAD(, root_hold_token)	root_holds =
1193    LIST_HEAD_INITIALIZER(&root_holds);
1194
1195struct root_hold_token *
1196root_mount_hold(const char *identifier)
1197{
1198	struct root_hold_token *h;
1199
1200	h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1201	h->who = identifier;
1202	mtx_lock(&mountlist_mtx);
1203	LIST_INSERT_HEAD(&root_holds, h, list);
1204	mtx_unlock(&mountlist_mtx);
1205	return (h);
1206}
1207
1208void
1209root_mount_rel(struct root_hold_token *h)
1210{
1211
1212	mtx_lock(&mountlist_mtx);
1213	LIST_REMOVE(h, list);
1214	wakeup(&root_holds);
1215	mtx_unlock(&mountlist_mtx);
1216	free(h, M_DEVBUF);
1217}
1218
1219static void
1220root_mount_wait(void)
1221{
1222	struct root_hold_token *h;
1223
1224	for (;;) {
1225		DROP_GIANT();
1226		g_waitidle();
1227		PICKUP_GIANT();
1228		mtx_lock(&mountlist_mtx);
1229		if (LIST_EMPTY(&root_holds)) {
1230			mtx_unlock(&mountlist_mtx);
1231			break;
1232		}
1233		printf("Root mount waiting for:");
1234		LIST_FOREACH(h, &root_holds, list)
1235			printf(" %s", h->who);
1236		printf("\n");
1237		msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1238		    hz);
1239	}
1240}
1241
1242static void
1243set_rootvnode(struct thread *td)
1244{
1245	struct proc *p;
1246
1247	if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1248		panic("Cannot find root vnode");
1249
1250	p = td->td_proc;
1251	FILEDESC_LOCK(p->p_fd);
1252
1253	if (p->p_fd->fd_cdir != NULL)
1254		vrele(p->p_fd->fd_cdir);
1255	p->p_fd->fd_cdir = rootvnode;
1256	VREF(rootvnode);
1257
1258	if (p->p_fd->fd_rdir != NULL)
1259		vrele(p->p_fd->fd_rdir);
1260	p->p_fd->fd_rdir = rootvnode;
1261	VREF(rootvnode);
1262
1263	FILEDESC_UNLOCK(p->p_fd);
1264
1265	VOP_UNLOCK(rootvnode, 0, td);
1266}
1267
1268/*
1269 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1270 * yet.  Create a /dev -> / symlink so that absolute pathnames will lookup.
1271 */
1272
1273static void
1274devfs_first(void)
1275{
1276	struct thread *td = curthread;
1277	struct vfsoptlist *opts;
1278	struct vfsconf *vfsp;
1279	struct mount *mp = NULL;
1280	int error;
1281
1282	vfsp = vfs_byname("devfs");
1283	KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1284	if (vfsp == NULL)
1285		return;
1286
1287	mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td);
1288
1289	error = VFS_MOUNT(mp, td);
1290	KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1291	if (error)
1292		return;
1293
1294	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1295	TAILQ_INIT(opts);
1296	mp->mnt_opt = opts;
1297
1298	mtx_lock(&mountlist_mtx);
1299	TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1300	mtx_unlock(&mountlist_mtx);
1301
1302	set_rootvnode(td);
1303
1304	error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1305	if (error)
1306		printf("kern_symlink /dev -> / returns %d\n", error);
1307}
1308
1309/*
1310 * Surgically move our devfs to be mounted on /dev.
1311 */
1312
1313static void
1314devfs_fixup(struct thread *td)
1315{
1316	struct nameidata nd;
1317	int error;
1318	struct vnode *vp, *dvp;
1319	struct mount *mp;
1320
1321	/* Remove our devfs mount from the mountlist and purge the cache */
1322	mtx_lock(&mountlist_mtx);
1323	mp = TAILQ_FIRST(&mountlist);
1324	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1325	mtx_unlock(&mountlist_mtx);
1326	cache_purgevfs(mp);
1327
1328	VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1329	VI_LOCK(dvp);
1330	dvp->v_iflag &= ~VI_MOUNT;
1331	dvp->v_mountedhere = NULL;
1332	VI_UNLOCK(dvp);
1333
1334	/* Set up the real rootvnode, and purge the cache */
1335	TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1336	set_rootvnode(td);
1337	cache_purgevfs(rootvnode->v_mount);
1338
1339	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1340	error = namei(&nd);
1341	if (error) {
1342		printf("Lookup of /dev for devfs, error: %d\n", error);
1343		return;
1344	}
1345	NDFREE(&nd, NDF_ONLY_PNBUF);
1346	vp = nd.ni_vp;
1347	if (vp->v_type != VDIR) {
1348		vput(vp);
1349	}
1350	error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1351	if (error) {
1352		vput(vp);
1353	}
1354	cache_purge(vp);
1355	mp->mnt_vnodecovered = vp;
1356	vp->v_mountedhere = mp;
1357	mtx_lock(&mountlist_mtx);
1358	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1359	mtx_unlock(&mountlist_mtx);
1360	VOP_UNLOCK(vp, 0, td);
1361	vput(dvp);
1362	vfs_unbusy(mp, td);
1363
1364	/* Unlink the no longer needed /dev/dev -> / symlink */
1365	kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1366}
1367
1368/*
1369 * Report errors during filesystem mounting.
1370 */
1371void
1372vfs_mount_error(struct mount *mp, const char *fmt, ...)
1373{
1374	struct vfsoptlist *moptlist = mp->mnt_optnew;
1375	va_list ap;
1376	int error, len;
1377	char *errmsg;
1378
1379	error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1380	if (error || errmsg == NULL || len <= 0)
1381		return;
1382
1383	va_start(ap, fmt);
1384	vsnprintf(errmsg, (size_t)len, fmt, ap);
1385	va_end(ap);
1386}
1387
1388/*
1389 * Find and mount the root filesystem
1390 */
1391void
1392vfs_mountroot(void)
1393{
1394	char *cp;
1395	int error, i, asked = 0;
1396
1397	root_mount_wait();
1398
1399	mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
1400	    NULL, NULL, mount_init, mount_fini,
1401	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1402	devfs_first();
1403
1404	/*
1405	 * We are booted with instructions to prompt for the root filesystem.
1406	 */
1407	if (boothowto & RB_ASKNAME) {
1408		if (!vfs_mountroot_ask())
1409			return;
1410		asked = 1;
1411	}
1412
1413	/*
1414	 * The root filesystem information is compiled in, and we are
1415	 * booted with instructions to use it.
1416	 */
1417	if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1418		if (!vfs_mountroot_try(ctrootdevname))
1419			return;
1420		ctrootdevname = NULL;
1421	}
1422
1423	/*
1424	 * We've been given the generic "use CDROM as root" flag.  This is
1425	 * necessary because one media may be used in many different
1426	 * devices, so we need to search for them.
1427	 */
1428	if (boothowto & RB_CDROM) {
1429		for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1430			if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1431				return;
1432		}
1433	}
1434
1435	/*
1436	 * Try to use the value read by the loader from /etc/fstab, or
1437	 * supplied via some other means.  This is the preferred
1438	 * mechanism.
1439	 */
1440	cp = getenv("vfs.root.mountfrom");
1441	if (cp != NULL) {
1442		error = vfs_mountroot_try(cp);
1443		freeenv(cp);
1444		if (!error)
1445			return;
1446	}
1447
1448	/*
1449	 * Try values that may have been computed by code during boot
1450	 */
1451	if (!vfs_mountroot_try(rootdevnames[0]))
1452		return;
1453	if (!vfs_mountroot_try(rootdevnames[1]))
1454		return;
1455
1456	/*
1457	 * If we (still) have a compiled-in default, try it.
1458	 */
1459	if (ctrootdevname != NULL)
1460		if (!vfs_mountroot_try(ctrootdevname))
1461			return;
1462	/*
1463	 * Everything so far has failed, prompt on the console if we haven't
1464	 * already tried that.
1465	 */
1466	if (!asked)
1467		if (!vfs_mountroot_ask())
1468			return;
1469
1470	panic("Root mount failed, startup aborted.");
1471}
1472
1473/*
1474 * Mount (mountfrom) as the root filesystem.
1475 */
1476static int
1477vfs_mountroot_try(const char *mountfrom)
1478{
1479	struct mount	*mp;
1480	char		*vfsname, *path;
1481	time_t		timebase;
1482	int		error;
1483	char		patt[32];
1484
1485	vfsname = NULL;
1486	path    = NULL;
1487	mp      = NULL;
1488	error   = EINVAL;
1489
1490	if (mountfrom == NULL)
1491		return (error);		/* don't complain */
1492	printf("Trying to mount root from %s\n", mountfrom);
1493
1494	/* parse vfs name and path */
1495	vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1496	path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1497	vfsname[0] = path[0] = 0;
1498	sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1499	if (sscanf(mountfrom, patt, vfsname, path) < 1)
1500		goto out;
1501
1502	if (path[0] == '\0')
1503		strcpy(path, ROOTNAME);
1504
1505	error = kernel_vmount(
1506	    MNT_RDONLY | MNT_ROOTFS,
1507	    "fstype", vfsname,
1508	    "fspath", "/",
1509	    "from", path,
1510	    NULL);
1511	if (error == 0) {
1512		/*
1513		 * We mount devfs prior to mounting the / FS, so the first
1514		 * entry will typically be devfs.
1515		 */
1516		mp = TAILQ_FIRST(&mountlist);
1517		KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1518
1519		/*
1520		 * Iterate over all currently mounted file systems and use
1521		 * the time stamp found to check and/or initialize the RTC.
1522		 * Typically devfs has no time stamp and the only other FS
1523		 * is the actual / FS.
1524		 * Call inittodr() only once and pass it the largest of the
1525		 * timestamps we encounter.
1526		 */
1527		timebase = 0;
1528		do {
1529			if (mp->mnt_time > timebase)
1530				timebase = mp->mnt_time;
1531			mp = TAILQ_NEXT(mp, mnt_list);
1532		} while (mp != NULL);
1533		inittodr(timebase);
1534
1535		devfs_fixup(curthread);
1536	}
1537out:
1538	free(path, M_MOUNT);
1539	free(vfsname, M_MOUNT);
1540	return (error);
1541}
1542
1543/*
1544 * ---------------------------------------------------------------------
1545 * Interactive root filesystem selection code.
1546 */
1547
1548static int
1549vfs_mountroot_ask(void)
1550{
1551	char name[128];
1552
1553	for(;;) {
1554		printf("\nManual root filesystem specification:\n");
1555		printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
1556#if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
1557		printf("                       eg. ufs:da0s1a\n");
1558#else
1559		printf("                       eg. ufs:/dev/da0a\n");
1560#endif
1561		printf("  ?                  List valid disk boot devices\n");
1562		printf("  <empty line>       Abort manual input\n");
1563		printf("\nmountroot> ");
1564		gets(name, sizeof(name), 1);
1565		if (name[0] == '\0')
1566			return (1);
1567		if (name[0] == '?') {
1568			printf("\nList of GEOM managed disk devices:\n  ");
1569			g_dev_print();
1570			continue;
1571		}
1572		if (!vfs_mountroot_try(name))
1573			return (0);
1574	}
1575}
1576
1577/*
1578 * ---------------------------------------------------------------------
1579 * Functions for querying mount options/arguments from filesystems.
1580 */
1581
1582/*
1583 * Check that no unknown options are given
1584 */
1585int
1586vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1587{
1588	struct vfsopt *opt;
1589	const char **t, *p;
1590
1591
1592	TAILQ_FOREACH(opt, opts, link) {
1593		p = opt->name;
1594		if (p[0] == 'n' && p[1] == 'o')
1595			p += 2;
1596		for(t = global_opts; *t != NULL; t++)
1597			if (!strcmp(*t, p))
1598				break;
1599		if (*t != NULL)
1600			continue;
1601		for(t = legal; *t != NULL; t++)
1602			if (!strcmp(*t, p))
1603				break;
1604		if (*t != NULL)
1605			continue;
1606		printf("mount option <%s> is unknown\n", p);
1607		return (EINVAL);
1608	}
1609	return (0);
1610}
1611
1612/*
1613 * Get a mount option by its name.
1614 *
1615 * Return 0 if the option was found, ENOENT otherwise.
1616 * If len is non-NULL it will be filled with the length
1617 * of the option. If buf is non-NULL, it will be filled
1618 * with the address of the option.
1619 */
1620int
1621vfs_getopt(opts, name, buf, len)
1622	struct vfsoptlist *opts;
1623	const char *name;
1624	void **buf;
1625	int *len;
1626{
1627	struct vfsopt *opt;
1628
1629	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1630
1631	TAILQ_FOREACH(opt, opts, link) {
1632		if (strcmp(name, opt->name) == 0) {
1633			if (len != NULL)
1634				*len = opt->len;
1635			if (buf != NULL)
1636				*buf = opt->value;
1637			return (0);
1638		}
1639	}
1640	return (ENOENT);
1641}
1642
1643static int
1644vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1645{
1646	struct vfsopt *opt;
1647	int i;
1648
1649	if (opts == NULL)
1650		return (-1);
1651
1652	i = 0;
1653	TAILQ_FOREACH(opt, opts, link) {
1654		if (strcmp(name, opt->name) == 0)
1655			return (i);
1656		++i;
1657	}
1658	return (-1);
1659}
1660
1661char *
1662vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1663{
1664	struct vfsopt *opt;
1665
1666	*error = 0;
1667	TAILQ_FOREACH(opt, opts, link) {
1668		if (strcmp(name, opt->name) != 0)
1669			continue;
1670		if (((char *)opt->value)[opt->len - 1] != '\0') {
1671			*error = EINVAL;
1672			return (NULL);
1673		}
1674		return (opt->value);
1675	}
1676	return (NULL);
1677}
1678
1679int
1680vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1681{
1682	struct vfsopt *opt;
1683
1684	TAILQ_FOREACH(opt, opts, link) {
1685		if (strcmp(name, opt->name) == 0) {
1686			if (w != NULL)
1687				*w |= val;
1688			return (1);
1689		}
1690	}
1691	if (w != NULL)
1692		*w &= ~val;
1693	return (0);
1694}
1695
1696int
1697vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1698{
1699	va_list ap;
1700	struct vfsopt *opt;
1701	int ret;
1702
1703	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1704
1705	TAILQ_FOREACH(opt, opts, link) {
1706		if (strcmp(name, opt->name) != 0)
1707			continue;
1708		if (((char *)opt->value)[opt->len - 1] != '\0')
1709			return (0);
1710		va_start(ap, fmt);
1711		ret = vsscanf(opt->value, fmt, ap);
1712		va_end(ap);
1713		return (ret);
1714	}
1715	return (0);
1716}
1717
1718/*
1719 * Find and copy a mount option.
1720 *
1721 * The size of the buffer has to be specified
1722 * in len, if it is not the same length as the
1723 * mount option, EINVAL is returned.
1724 * Returns ENOENT if the option is not found.
1725 */
1726int
1727vfs_copyopt(opts, name, dest, len)
1728	struct vfsoptlist *opts;
1729	const char *name;
1730	void *dest;
1731	int len;
1732{
1733	struct vfsopt *opt;
1734
1735	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1736
1737	TAILQ_FOREACH(opt, opts, link) {
1738		if (strcmp(name, opt->name) == 0) {
1739			if (len != opt->len)
1740				return (EINVAL);
1741			bcopy(opt->value, dest, opt->len);
1742			return (0);
1743		}
1744	}
1745	return (ENOENT);
1746}
1747
1748/*
1749 * This is a helper function for filesystems to traverse their
1750 * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
1751 */
1752
1753struct vnode *
1754__mnt_vnode_next(struct vnode **mvp, struct mount *mp)
1755{
1756	struct vnode *vp;
1757
1758	mtx_assert(MNT_MTX(mp), MA_OWNED);
1759
1760	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1761	vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
1762	while (vp != NULL && vp->v_type == VMARKER)
1763		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1764
1765	/* Check if we are done */
1766	if (vp == NULL) {
1767		__mnt_vnode_markerfree(mvp, mp);
1768		return (NULL);
1769	}
1770	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1771	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1772	return (vp);
1773}
1774
1775struct vnode *
1776__mnt_vnode_first(struct vnode **mvp, struct mount *mp)
1777{
1778	struct vnode *vp;
1779
1780	mtx_assert(MNT_MTX(mp), MA_OWNED);
1781
1782	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1783	while (vp != NULL && vp->v_type == VMARKER)
1784		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1785
1786	/* Check if we are done */
1787	if (vp == NULL) {
1788		*mvp = NULL;
1789		return (NULL);
1790	}
1791	mp->mnt_holdcnt++;
1792	MNT_IUNLOCK(mp);
1793	*mvp = (struct vnode *) malloc(sizeof(struct vnode),
1794				       M_VNODE_MARKER,
1795				       M_WAITOK | M_ZERO);
1796	MNT_ILOCK(mp);
1797	(*mvp)->v_type = VMARKER;
1798
1799	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1800	while (vp != NULL && vp->v_type == VMARKER)
1801		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1802
1803	/* Check if we are done */
1804	if (vp == NULL) {
1805		MNT_IUNLOCK(mp);
1806		free(*mvp, M_VNODE_MARKER);
1807		MNT_ILOCK(mp);
1808		*mvp = NULL;
1809		mp->mnt_holdcnt--;
1810		if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1811			wakeup(&mp->mnt_holdcnt);
1812		return (NULL);
1813	}
1814	mp->mnt_markercnt++;
1815	(*mvp)->v_mount = mp;
1816	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1817	return (vp);
1818}
1819
1820
1821void
1822__mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
1823{
1824
1825	if (*mvp == NULL)
1826		return;
1827
1828	mtx_assert(MNT_MTX(mp), MA_OWNED);
1829
1830	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1831	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1832	MNT_IUNLOCK(mp);
1833	free(*mvp, M_VNODE_MARKER);
1834	MNT_ILOCK(mp);
1835	*mvp = NULL;
1836
1837	mp->mnt_markercnt--;
1838	mp->mnt_holdcnt--;
1839	if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1840		wakeup(&mp->mnt_holdcnt);
1841}
1842
1843
1844int
1845__vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
1846{
1847	int error;
1848
1849	error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
1850	if (sbp != &mp->mnt_stat)
1851		*sbp = mp->mnt_stat;
1852	return (error);
1853}
1854
1855void
1856vfs_mountedfrom(struct mount *mp, const char *from)
1857{
1858
1859	bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1860	strlcpy(mp->mnt_stat.f_mntfromname, from,
1861	    sizeof mp->mnt_stat.f_mntfromname);
1862}
1863
1864/*
1865 * ---------------------------------------------------------------------
1866 * This is the api for building mount args and mounting filesystems from
1867 * inside the kernel.
1868 *
1869 * The API works by accumulation of individual args.  First error is
1870 * latched.
1871 *
1872 * XXX: should be documented in new manpage kernel_mount(9)
1873 */
1874
1875/* A memory allocation which must be freed when we are done */
1876struct mntaarg {
1877	SLIST_ENTRY(mntaarg)	next;
1878};
1879
1880/* The header for the mount arguments */
1881struct mntarg {
1882	struct iovec *v;
1883	int len;
1884	int error;
1885	SLIST_HEAD(, mntaarg)	list;
1886};
1887
1888/*
1889 * Add a boolean argument.
1890 *
1891 * flag is the boolean value.
1892 * name must start with "no".
1893 */
1894struct mntarg *
1895mount_argb(struct mntarg *ma, int flag, const char *name)
1896{
1897
1898	KASSERT(name[0] == 'n' && name[1] == 'o',
1899	    ("mount_argb(...,%s): name must start with 'no'", name));
1900
1901	return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1902}
1903
1904/*
1905 * Add an argument printf style
1906 */
1907struct mntarg *
1908mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1909{
1910	va_list ap;
1911	struct mntaarg *maa;
1912	struct sbuf *sb;
1913	int len;
1914
1915	if (ma == NULL) {
1916		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1917		SLIST_INIT(&ma->list);
1918	}
1919	if (ma->error)
1920		return (ma);
1921
1922	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1923	    M_MOUNT, M_WAITOK);
1924	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1925	ma->v[ma->len].iov_len = strlen(name) + 1;
1926	ma->len++;
1927
1928	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
1929	va_start(ap, fmt);
1930	sbuf_vprintf(sb, fmt, ap);
1931	va_end(ap);
1932	sbuf_finish(sb);
1933	len = sbuf_len(sb) + 1;
1934	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1935	SLIST_INSERT_HEAD(&ma->list, maa, next);
1936	bcopy(sbuf_data(sb), maa + 1, len);
1937	sbuf_delete(sb);
1938
1939	ma->v[ma->len].iov_base = maa + 1;
1940	ma->v[ma->len].iov_len = len;
1941	ma->len++;
1942
1943	return (ma);
1944}
1945
1946/*
1947 * Add an argument which is a userland string.
1948 */
1949struct mntarg *
1950mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1951{
1952	struct mntaarg *maa;
1953	char *tbuf;
1954
1955	if (val == NULL)
1956		return (ma);
1957	if (ma == NULL) {
1958		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1959		SLIST_INIT(&ma->list);
1960	}
1961	if (ma->error)
1962		return (ma);
1963	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1964	SLIST_INSERT_HEAD(&ma->list, maa, next);
1965	tbuf = (void *)(maa + 1);
1966	ma->error = copyinstr(val, tbuf, len, NULL);
1967	return (mount_arg(ma, name, tbuf, -1));
1968}
1969
1970/*
1971 * Plain argument.
1972 *
1973 * If length is -1, use printf.
1974 */
1975struct mntarg *
1976mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1977{
1978
1979	if (ma == NULL) {
1980		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1981		SLIST_INIT(&ma->list);
1982	}
1983	if (ma->error)
1984		return (ma);
1985
1986	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1987	    M_MOUNT, M_WAITOK);
1988	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1989	ma->v[ma->len].iov_len = strlen(name) + 1;
1990	ma->len++;
1991
1992	ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1993	if (len < 0)
1994		ma->v[ma->len].iov_len = strlen(val) + 1;
1995	else
1996		ma->v[ma->len].iov_len = len;
1997	ma->len++;
1998	return (ma);
1999}
2000
2001/*
2002 * Free a mntarg structure
2003 */
2004static void
2005free_mntarg(struct mntarg *ma)
2006{
2007	struct mntaarg *maa;
2008
2009	while (!SLIST_EMPTY(&ma->list)) {
2010		maa = SLIST_FIRST(&ma->list);
2011		SLIST_REMOVE_HEAD(&ma->list, next);
2012		free(maa, M_MOUNT);
2013	}
2014	free(ma->v, M_MOUNT);
2015	free(ma, M_MOUNT);
2016}
2017
2018/*
2019 * Mount a filesystem
2020 */
2021int
2022kernel_mount(struct mntarg *ma, int flags)
2023{
2024	struct uio auio;
2025	int error;
2026
2027	KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2028	KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2029	KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2030
2031	auio.uio_iov = ma->v;
2032	auio.uio_iovcnt = ma->len;
2033	auio.uio_segflg = UIO_SYSSPACE;
2034
2035	error = ma->error;
2036	if (!error)
2037		error = vfs_donmount(curthread, flags, &auio);
2038	free_mntarg(ma);
2039	return (error);
2040}
2041
2042/*
2043 * A printflike function to mount a filesystem.
2044 */
2045int
2046kernel_vmount(int flags, ...)
2047{
2048	struct mntarg *ma = NULL;
2049	va_list ap;
2050	const char *cp;
2051	const void *vp;
2052	int error;
2053
2054	va_start(ap, flags);
2055	for (;;) {
2056		cp = va_arg(ap, const char *);
2057		if (cp == NULL)
2058			break;
2059		vp = va_arg(ap, const void *);
2060		ma = mount_arg(ma, cp, vp, -1);
2061	}
2062	va_end(ap);
2063
2064	error = kernel_mount(ma, flags);
2065	return (error);
2066}
2067