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