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