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