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