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