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