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