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