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