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