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