xref: /freebsd/sys/kern/vfs_mount.c (revision b7c60aadbbd5c846a250c05791fe7406d6d78bf4)
1 /*-
2  * Copyright (c) 1999-2004 Poul-Henning Kamp
3  * Copyright (c) 1999 Michael Smith
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "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 
75 static int	vfs_domount(struct thread *td, const char *fstype, char *fspath,
76 		    uint64_t fsflags, struct vfsoptlist **optlist);
77 static void	free_mntarg(struct mntarg *ma);
78 
79 static int	usermount = 0;
80 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
81     "Unprivileged users may mount and unmount file systems");
82 
83 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
84 static MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
85 static uma_zone_t mount_zone;
86 
87 /* List of mounted filesystems. */
88 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
89 
90 /* For any iteration/modification of mountlist */
91 struct mtx mountlist_mtx;
92 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
93 
94 /*
95  * Global opts, taken by all filesystems
96  */
97 static const char *global_opts[] = {
98 	"errmsg",
99 	"fstype",
100 	"fspath",
101 	"ro",
102 	"rw",
103 	"nosuid",
104 	"noexec",
105 	NULL
106 };
107 
108 static int
109 mount_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 
119 static void
120 mount_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 
129 static void
130 vfs_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 }
136 SYSINIT(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. */
144 static void
145 vfs_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. */
156 void
157 vfs_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 
168 void
169 vfs_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 
181 static int
182 vfs_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 
191 static int
192 vfs_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  */
203 static int
204 vfs_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  */
240 static void
241 vfs_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  */
262 int
263 vfs_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);
333 bad:
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  */
346 static void
347 vfs_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  */
369 int
370 sys_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 
431 void
432 vfs_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 
441 void
442 vfs_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  */
454 struct mount *
455 vfs_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  */
489 void
490 vfs_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 
530 int
531 vfs_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);
662 bail:
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_
686 struct mount_args {
687 	char	*type;
688 	char	*path;
689 	int	flags;
690 	caddr_t	data;
691 };
692 #endif
693 /* ARGSUSED */
694 int
695 sys_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  */
762 static int
763 vfs_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 && mp->mnt_noasync == 0)
855 		mp->mnt_kern_flag |= MNTK_ASYNC;
856 	else
857 		mp->mnt_kern_flag &= ~MNTK_ASYNC;
858 	MNT_IUNLOCK(mp);
859 
860 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
861 	cache_purge(vp);
862 	VI_LOCK(vp);
863 	vp->v_iflag &= ~VI_MOUNT;
864 	VI_UNLOCK(vp);
865 	vp->v_mountedhere = mp;
866 	/* Place the new filesystem at the end of the mount list. */
867 	mtx_lock(&mountlist_mtx);
868 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
869 	mtx_unlock(&mountlist_mtx);
870 	vfs_event_signal(NULL, VQ_MOUNT, 0);
871 	if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
872 		panic("mount: lost mount");
873 	VOP_UNLOCK(newdp, 0);
874 	VOP_UNLOCK(vp, 0);
875 	mountcheckdirs(vp, newdp);
876 	vrele(newdp);
877 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
878 		vfs_allocate_syncvnode(mp);
879 	vfs_unbusy(mp);
880 	return (0);
881 }
882 
883 /*
884  * vfs_domount_update(): update of mounted file system
885  */
886 static int
887 vfs_domount_update(
888 	struct thread *td,		/* Calling thread. */
889 	struct vnode *vp,		/* Mount point vnode. */
890 	uint64_t fsflags,		/* Flags common to all filesystems. */
891 	struct vfsoptlist **optlist	/* Options local to the filesystem. */
892 	)
893 {
894 	struct oexport_args oexport;
895 	struct export_args export;
896 	struct mount *mp;
897 	int error, export_error;
898 	uint64_t flag;
899 
900 	mtx_assert(&Giant, MA_OWNED);
901 	ASSERT_VOP_ELOCKED(vp, __func__);
902 	KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
903 
904 	if ((vp->v_vflag & VV_ROOT) == 0) {
905 		vput(vp);
906 		return (EINVAL);
907 	}
908 	mp = vp->v_mount;
909 	/*
910 	 * We only allow the filesystem to be reloaded if it
911 	 * is currently mounted read-only.
912 	 */
913 	flag = mp->mnt_flag;
914 	if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
915 		vput(vp);
916 		return (EOPNOTSUPP);	/* Needs translation */
917 	}
918 	/*
919 	 * Only privileged root, or (if MNT_USER is set) the user that
920 	 * did the original mount is permitted to update it.
921 	 */
922 	error = vfs_suser(mp, td);
923 	if (error != 0) {
924 		vput(vp);
925 		return (error);
926 	}
927 	if (vfs_busy(mp, MBF_NOWAIT)) {
928 		vput(vp);
929 		return (EBUSY);
930 	}
931 	VI_LOCK(vp);
932 	if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
933 		VI_UNLOCK(vp);
934 		vfs_unbusy(mp);
935 		vput(vp);
936 		return (EBUSY);
937 	}
938 	vp->v_iflag |= VI_MOUNT;
939 	VI_UNLOCK(vp);
940 	VOP_UNLOCK(vp, 0);
941 
942 	MNT_ILOCK(mp);
943 	mp->mnt_flag &= ~MNT_UPDATEMASK;
944 	mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
945 	    MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
946 	if ((mp->mnt_flag & MNT_ASYNC) == 0)
947 		mp->mnt_kern_flag &= ~MNTK_ASYNC;
948 	MNT_IUNLOCK(mp);
949 	mp->mnt_optnew = *optlist;
950 	vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
951 
952 	/*
953 	 * Mount the filesystem.
954 	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
955 	 * get.  No freeing of cn_pnbuf.
956 	 */
957 	error = VFS_MOUNT(mp);
958 
959 	export_error = 0;
960 	if (error == 0) {
961 		/* Process the export option. */
962 		if (vfs_copyopt(mp->mnt_optnew, "export", &export,
963 		    sizeof(export)) == 0) {
964 			export_error = vfs_export(mp, &export);
965 		} else if (vfs_copyopt(mp->mnt_optnew, "export", &oexport,
966 		    sizeof(oexport)) == 0) {
967 			export.ex_flags = oexport.ex_flags;
968 			export.ex_root = oexport.ex_root;
969 			export.ex_anon = oexport.ex_anon;
970 			export.ex_addr = oexport.ex_addr;
971 			export.ex_addrlen = oexport.ex_addrlen;
972 			export.ex_mask = oexport.ex_mask;
973 			export.ex_masklen = oexport.ex_masklen;
974 			export.ex_indexfile = oexport.ex_indexfile;
975 			export.ex_numsecflavors = 0;
976 			export_error = vfs_export(mp, &export);
977 		}
978 	}
979 
980 	MNT_ILOCK(mp);
981 	if (error == 0) {
982 		mp->mnt_flag &=	~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
983 		    MNT_SNAPSHOT);
984 	} else {
985 		/*
986 		 * If we fail, restore old mount flags. MNT_QUOTA is special,
987 		 * because it is not part of MNT_UPDATEMASK, but it could have
988 		 * changed in the meantime if quotactl(2) was called.
989 		 * All in all we want current value of MNT_QUOTA, not the old
990 		 * one.
991 		 */
992 		mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
993 	}
994 	if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
995 		mp->mnt_kern_flag |= MNTK_ASYNC;
996 	else
997 		mp->mnt_kern_flag &= ~MNTK_ASYNC;
998 	MNT_IUNLOCK(mp);
999 
1000 	if (error != 0)
1001 		goto end;
1002 
1003 	if (mp->mnt_opt != NULL)
1004 		vfs_freeopts(mp->mnt_opt);
1005 	mp->mnt_opt = mp->mnt_optnew;
1006 	*optlist = NULL;
1007 	(void)VFS_STATFS(mp, &mp->mnt_stat);
1008 	/*
1009 	 * Prevent external consumers of mount options from reading
1010 	 * mnt_optnew.
1011 	 */
1012 	mp->mnt_optnew = NULL;
1013 
1014 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
1015 		vfs_allocate_syncvnode(mp);
1016 	else
1017 		vfs_deallocate_syncvnode(mp);
1018 end:
1019 	vfs_unbusy(mp);
1020 	VI_LOCK(vp);
1021 	vp->v_iflag &= ~VI_MOUNT;
1022 	VI_UNLOCK(vp);
1023 	vrele(vp);
1024 	return (error != 0 ? error : export_error);
1025 }
1026 
1027 /*
1028  * vfs_domount(): actually attempt a filesystem mount.
1029  */
1030 static int
1031 vfs_domount(
1032 	struct thread *td,		/* Calling thread. */
1033 	const char *fstype,		/* Filesystem type. */
1034 	char *fspath,			/* Mount path. */
1035 	uint64_t fsflags,		/* Flags common to all filesystems. */
1036 	struct vfsoptlist **optlist	/* Options local to the filesystem. */
1037 	)
1038 {
1039 	struct vfsconf *vfsp;
1040 	struct nameidata nd;
1041 	struct vnode *vp;
1042 	int error;
1043 
1044 	/*
1045 	 * Be ultra-paranoid about making sure the type and fspath
1046 	 * variables will fit in our mp buffers, including the
1047 	 * terminating NUL.
1048 	 */
1049 	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1050 		return (ENAMETOOLONG);
1051 
1052 	if (jailed(td->td_ucred) || usermount == 0) {
1053 		if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1054 			return (error);
1055 	}
1056 
1057 	/*
1058 	 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1059 	 */
1060 	if (fsflags & MNT_EXPORTED) {
1061 		error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1062 		if (error)
1063 			return (error);
1064 	}
1065 	if (fsflags & MNT_SUIDDIR) {
1066 		error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1067 		if (error)
1068 			return (error);
1069 	}
1070 	/*
1071 	 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1072 	 */
1073 	if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1074 		if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1075 			fsflags |= MNT_NOSUID | MNT_USER;
1076 	}
1077 
1078 	/* Load KLDs before we lock the covered vnode to avoid reversals. */
1079 	vfsp = NULL;
1080 	if ((fsflags & MNT_UPDATE) == 0) {
1081 		/* Don't try to load KLDs if we're mounting the root. */
1082 		if (fsflags & MNT_ROOTFS)
1083 			vfsp = vfs_byname(fstype);
1084 		else
1085 			vfsp = vfs_byname_kld(fstype, td, &error);
1086 		if (vfsp == NULL)
1087 			return (ENODEV);
1088 		if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
1089 			return (EPERM);
1090 	}
1091 
1092 	/*
1093 	 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1094 	 */
1095 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
1096 	    UIO_SYSSPACE, fspath, td);
1097 	error = namei(&nd);
1098 	if (error != 0)
1099 		return (error);
1100 	if (!NDHASGIANT(&nd))
1101 		mtx_lock(&Giant);
1102 	NDFREE(&nd, NDF_ONLY_PNBUF);
1103 	vp = nd.ni_vp;
1104 	if ((fsflags & MNT_UPDATE) == 0) {
1105 		error = vn_path_to_global_path(td, vp, fspath, MNAMELEN);
1106 		/* debug.disablefullpath == 1 results in ENODEV */
1107 		if (error == 0 || error == ENODEV) {
1108 			error = vfs_domount_first(td, vfsp, fspath, vp,
1109 			    fsflags, optlist);
1110 		}
1111 	} else
1112 		error = vfs_domount_update(td, vp, fsflags, optlist);
1113 	mtx_unlock(&Giant);
1114 
1115 	ASSERT_VI_UNLOCKED(vp, __func__);
1116 	ASSERT_VOP_UNLOCKED(vp, __func__);
1117 
1118 	return (error);
1119 }
1120 
1121 /*
1122  * Unmount a filesystem.
1123  *
1124  * Note: unmount takes a path to the vnode mounted on as argument, not
1125  * special file (as before).
1126  */
1127 #ifndef _SYS_SYSPROTO_H_
1128 struct unmount_args {
1129 	char	*path;
1130 	int	flags;
1131 };
1132 #endif
1133 /* ARGSUSED */
1134 int
1135 sys_unmount(td, uap)
1136 	struct thread *td;
1137 	register struct unmount_args /* {
1138 		char *path;
1139 		int flags;
1140 	} */ *uap;
1141 {
1142 	struct nameidata nd;
1143 	struct mount *mp;
1144 	char *pathbuf;
1145 	int error, id0, id1, vfslocked;
1146 
1147 	AUDIT_ARG_VALUE(uap->flags);
1148 	if (jailed(td->td_ucred) || usermount == 0) {
1149 		error = priv_check(td, PRIV_VFS_UNMOUNT);
1150 		if (error)
1151 			return (error);
1152 	}
1153 
1154 	pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1155 	error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1156 	if (error) {
1157 		free(pathbuf, M_TEMP);
1158 		return (error);
1159 	}
1160 	mtx_lock(&Giant);
1161 	if (uap->flags & MNT_BYFSID) {
1162 		AUDIT_ARG_TEXT(pathbuf);
1163 		/* Decode the filesystem ID. */
1164 		if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1165 			mtx_unlock(&Giant);
1166 			free(pathbuf, M_TEMP);
1167 			return (EINVAL);
1168 		}
1169 
1170 		mtx_lock(&mountlist_mtx);
1171 		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1172 			if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1173 			    mp->mnt_stat.f_fsid.val[1] == id1)
1174 				break;
1175 		}
1176 		mtx_unlock(&mountlist_mtx);
1177 	} else {
1178 		AUDIT_ARG_UPATH1(td, pathbuf);
1179 		/*
1180 		 * Try to find global path for path argument.
1181 		 */
1182 		NDINIT(&nd, LOOKUP,
1183 		    FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
1184 		    UIO_SYSSPACE, pathbuf, td);
1185 		if (namei(&nd) == 0) {
1186 			vfslocked = NDHASGIANT(&nd);
1187 			NDFREE(&nd, NDF_ONLY_PNBUF);
1188 			error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1189 			    MNAMELEN);
1190 			if (error == 0 || error == ENODEV)
1191 				vput(nd.ni_vp);
1192 			VFS_UNLOCK_GIANT(vfslocked);
1193 		}
1194 		mtx_lock(&mountlist_mtx);
1195 		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1196 			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1197 				break;
1198 		}
1199 		mtx_unlock(&mountlist_mtx);
1200 	}
1201 	free(pathbuf, M_TEMP);
1202 	if (mp == NULL) {
1203 		/*
1204 		 * Previously we returned ENOENT for a nonexistent path and
1205 		 * EINVAL for a non-mountpoint.  We cannot tell these apart
1206 		 * now, so in the !MNT_BYFSID case return the more likely
1207 		 * EINVAL for compatibility.
1208 		 */
1209 		mtx_unlock(&Giant);
1210 		return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1211 	}
1212 
1213 	/*
1214 	 * Don't allow unmounting the root filesystem.
1215 	 */
1216 	if (mp->mnt_flag & MNT_ROOTFS) {
1217 		mtx_unlock(&Giant);
1218 		return (EINVAL);
1219 	}
1220 	error = dounmount(mp, uap->flags, td);
1221 	mtx_unlock(&Giant);
1222 	return (error);
1223 }
1224 
1225 /*
1226  * Do the actual filesystem unmount.
1227  */
1228 int
1229 dounmount(mp, flags, td)
1230 	struct mount *mp;
1231 	int flags;
1232 	struct thread *td;
1233 {
1234 	struct vnode *coveredvp, *fsrootvp;
1235 	int error;
1236 	uint64_t async_flag;
1237 	int mnt_gen_r;
1238 
1239 	mtx_assert(&Giant, MA_OWNED);
1240 
1241 	if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1242 		mnt_gen_r = mp->mnt_gen;
1243 		VI_LOCK(coveredvp);
1244 		vholdl(coveredvp);
1245 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1246 		vdrop(coveredvp);
1247 		/*
1248 		 * Check for mp being unmounted while waiting for the
1249 		 * covered vnode lock.
1250 		 */
1251 		if (coveredvp->v_mountedhere != mp ||
1252 		    coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1253 			VOP_UNLOCK(coveredvp, 0);
1254 			return (EBUSY);
1255 		}
1256 	}
1257 	/*
1258 	 * Only privileged root, or (if MNT_USER is set) the user that did the
1259 	 * original mount is permitted to unmount this filesystem.
1260 	 */
1261 	error = vfs_suser(mp, td);
1262 	if (error) {
1263 		if (coveredvp)
1264 			VOP_UNLOCK(coveredvp, 0);
1265 		return (error);
1266 	}
1267 
1268 	MNT_ILOCK(mp);
1269 	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1270 		MNT_IUNLOCK(mp);
1271 		if (coveredvp)
1272 			VOP_UNLOCK(coveredvp, 0);
1273 		return (EBUSY);
1274 	}
1275 	mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1276 	/* Allow filesystems to detect that a forced unmount is in progress. */
1277 	if (flags & MNT_FORCE)
1278 		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1279 	error = 0;
1280 	if (mp->mnt_lockref) {
1281 		mp->mnt_kern_flag |= MNTK_DRAINING;
1282 		error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1283 		    "mount drain", 0);
1284 	}
1285 	MNT_IUNLOCK(mp);
1286 	KASSERT(mp->mnt_lockref == 0,
1287 	    ("%s: invalid lock refcount in the drain path @ %s:%d",
1288 	    __func__, __FILE__, __LINE__));
1289 	KASSERT(error == 0,
1290 	    ("%s: invalid return value for msleep in the drain path @ %s:%d",
1291 	    __func__, __FILE__, __LINE__));
1292 	vn_start_write(NULL, &mp, V_WAIT);
1293 
1294 	if (mp->mnt_flag & MNT_EXPUBLIC)
1295 		vfs_setpublicfs(NULL, NULL, NULL);
1296 
1297 	vfs_msync(mp, MNT_WAIT);
1298 	MNT_ILOCK(mp);
1299 	async_flag = mp->mnt_flag & MNT_ASYNC;
1300 	mp->mnt_flag &= ~MNT_ASYNC;
1301 	mp->mnt_kern_flag &= ~MNTK_ASYNC;
1302 	MNT_IUNLOCK(mp);
1303 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
1304 	vfs_deallocate_syncvnode(mp);
1305 	/*
1306 	 * For forced unmounts, move process cdir/rdir refs on the fs root
1307 	 * vnode to the covered vnode.  For non-forced unmounts we want
1308 	 * such references to cause an EBUSY error.
1309 	 */
1310 	if ((flags & MNT_FORCE) &&
1311 	    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1312 		if (mp->mnt_vnodecovered != NULL)
1313 			mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1314 		if (fsrootvp == rootvnode) {
1315 			vrele(rootvnode);
1316 			rootvnode = NULL;
1317 		}
1318 		vput(fsrootvp);
1319 	}
1320 	if (((mp->mnt_flag & MNT_RDONLY) ||
1321 	     (error = VFS_SYNC(mp, MNT_WAIT)) == 0) || (flags & MNT_FORCE) != 0)
1322 		error = VFS_UNMOUNT(mp, flags);
1323 	vn_finished_write(mp);
1324 	/*
1325 	 * If we failed to flush the dirty blocks for this mount point,
1326 	 * undo all the cdir/rdir and rootvnode changes we made above.
1327 	 * Unless we failed to do so because the device is reporting that
1328 	 * it doesn't exist anymore.
1329 	 */
1330 	if (error && error != ENXIO) {
1331 		if ((flags & MNT_FORCE) &&
1332 		    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1333 			if (mp->mnt_vnodecovered != NULL)
1334 				mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1335 			if (rootvnode == NULL) {
1336 				rootvnode = fsrootvp;
1337 				vref(rootvnode);
1338 			}
1339 			vput(fsrootvp);
1340 		}
1341 		MNT_ILOCK(mp);
1342 		mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1343 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1344 			MNT_IUNLOCK(mp);
1345 			vfs_allocate_syncvnode(mp);
1346 			MNT_ILOCK(mp);
1347 		}
1348 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1349 		mp->mnt_flag |= async_flag;
1350 		if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1351 			mp->mnt_kern_flag |= MNTK_ASYNC;
1352 		if (mp->mnt_kern_flag & MNTK_MWAIT) {
1353 			mp->mnt_kern_flag &= ~MNTK_MWAIT;
1354 			wakeup(mp);
1355 		}
1356 		MNT_IUNLOCK(mp);
1357 		if (coveredvp)
1358 			VOP_UNLOCK(coveredvp, 0);
1359 		return (error);
1360 	}
1361 	mtx_lock(&mountlist_mtx);
1362 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1363 	mtx_unlock(&mountlist_mtx);
1364 	if (coveredvp != NULL) {
1365 		coveredvp->v_mountedhere = NULL;
1366 		vput(coveredvp);
1367 	}
1368 	vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1369 	vfs_mount_destroy(mp);
1370 	return (0);
1371 }
1372 
1373 /*
1374  * Report errors during filesystem mounting.
1375  */
1376 void
1377 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1378 {
1379 	struct vfsoptlist *moptlist = mp->mnt_optnew;
1380 	va_list ap;
1381 	int error, len;
1382 	char *errmsg;
1383 
1384 	error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1385 	if (error || errmsg == NULL || len <= 0)
1386 		return;
1387 
1388 	va_start(ap, fmt);
1389 	vsnprintf(errmsg, (size_t)len, fmt, ap);
1390 	va_end(ap);
1391 }
1392 
1393 void
1394 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1395 {
1396 	va_list ap;
1397 	int error, len;
1398 	char *errmsg;
1399 
1400 	error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1401 	if (error || errmsg == NULL || len <= 0)
1402 		return;
1403 
1404 	va_start(ap, fmt);
1405 	vsnprintf(errmsg, (size_t)len, fmt, ap);
1406 	va_end(ap);
1407 }
1408 
1409 /*
1410  * ---------------------------------------------------------------------
1411  * Functions for querying mount options/arguments from filesystems.
1412  */
1413 
1414 /*
1415  * Check that no unknown options are given
1416  */
1417 int
1418 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1419 {
1420 	struct vfsopt *opt;
1421 	char errmsg[255];
1422 	const char **t, *p, *q;
1423 	int ret = 0;
1424 
1425 	TAILQ_FOREACH(opt, opts, link) {
1426 		p = opt->name;
1427 		q = NULL;
1428 		if (p[0] == 'n' && p[1] == 'o')
1429 			q = p + 2;
1430 		for(t = global_opts; *t != NULL; t++) {
1431 			if (strcmp(*t, p) == 0)
1432 				break;
1433 			if (q != NULL) {
1434 				if (strcmp(*t, q) == 0)
1435 					break;
1436 			}
1437 		}
1438 		if (*t != NULL)
1439 			continue;
1440 		for(t = legal; *t != NULL; t++) {
1441 			if (strcmp(*t, p) == 0)
1442 				break;
1443 			if (q != NULL) {
1444 				if (strcmp(*t, q) == 0)
1445 					break;
1446 			}
1447 		}
1448 		if (*t != NULL)
1449 			continue;
1450 		snprintf(errmsg, sizeof(errmsg),
1451 		    "mount option <%s> is unknown", p);
1452 		ret = EINVAL;
1453 	}
1454 	if (ret != 0) {
1455 		TAILQ_FOREACH(opt, opts, link) {
1456 			if (strcmp(opt->name, "errmsg") == 0) {
1457 				strncpy((char *)opt->value, errmsg, opt->len);
1458 				break;
1459 			}
1460 		}
1461 		if (opt == NULL)
1462 			printf("%s\n", errmsg);
1463 	}
1464 	return (ret);
1465 }
1466 
1467 /*
1468  * Get a mount option by its name.
1469  *
1470  * Return 0 if the option was found, ENOENT otherwise.
1471  * If len is non-NULL it will be filled with the length
1472  * of the option. If buf is non-NULL, it will be filled
1473  * with the address of the option.
1474  */
1475 int
1476 vfs_getopt(opts, name, buf, len)
1477 	struct vfsoptlist *opts;
1478 	const char *name;
1479 	void **buf;
1480 	int *len;
1481 {
1482 	struct vfsopt *opt;
1483 
1484 	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1485 
1486 	TAILQ_FOREACH(opt, opts, link) {
1487 		if (strcmp(name, opt->name) == 0) {
1488 			opt->seen = 1;
1489 			if (len != NULL)
1490 				*len = opt->len;
1491 			if (buf != NULL)
1492 				*buf = opt->value;
1493 			return (0);
1494 		}
1495 	}
1496 	return (ENOENT);
1497 }
1498 
1499 int
1500 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1501 {
1502 	struct vfsopt *opt;
1503 
1504 	if (opts == NULL)
1505 		return (-1);
1506 
1507 	TAILQ_FOREACH(opt, opts, link) {
1508 		if (strcmp(name, opt->name) == 0) {
1509 			opt->seen = 1;
1510 			return (opt->pos);
1511 		}
1512 	}
1513 	return (-1);
1514 }
1515 
1516 char *
1517 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1518 {
1519 	struct vfsopt *opt;
1520 
1521 	*error = 0;
1522 	TAILQ_FOREACH(opt, opts, link) {
1523 		if (strcmp(name, opt->name) != 0)
1524 			continue;
1525 		opt->seen = 1;
1526 		if (opt->len == 0 ||
1527 		    ((char *)opt->value)[opt->len - 1] != '\0') {
1528 			*error = EINVAL;
1529 			return (NULL);
1530 		}
1531 		return (opt->value);
1532 	}
1533 	*error = ENOENT;
1534 	return (NULL);
1535 }
1536 
1537 int
1538 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
1539 	uint64_t val)
1540 {
1541 	struct vfsopt *opt;
1542 
1543 	TAILQ_FOREACH(opt, opts, link) {
1544 		if (strcmp(name, opt->name) == 0) {
1545 			opt->seen = 1;
1546 			if (w != NULL)
1547 				*w |= val;
1548 			return (1);
1549 		}
1550 	}
1551 	if (w != NULL)
1552 		*w &= ~val;
1553 	return (0);
1554 }
1555 
1556 int
1557 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1558 {
1559 	va_list ap;
1560 	struct vfsopt *opt;
1561 	int ret;
1562 
1563 	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1564 
1565 	TAILQ_FOREACH(opt, opts, link) {
1566 		if (strcmp(name, opt->name) != 0)
1567 			continue;
1568 		opt->seen = 1;
1569 		if (opt->len == 0 || opt->value == NULL)
1570 			return (0);
1571 		if (((char *)opt->value)[opt->len - 1] != '\0')
1572 			return (0);
1573 		va_start(ap, fmt);
1574 		ret = vsscanf(opt->value, fmt, ap);
1575 		va_end(ap);
1576 		return (ret);
1577 	}
1578 	return (0);
1579 }
1580 
1581 int
1582 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
1583 {
1584 	struct vfsopt *opt;
1585 
1586 	TAILQ_FOREACH(opt, opts, link) {
1587 		if (strcmp(name, opt->name) != 0)
1588 			continue;
1589 		opt->seen = 1;
1590 		if (opt->value == NULL)
1591 			opt->len = len;
1592 		else {
1593 			if (opt->len != len)
1594 				return (EINVAL);
1595 			bcopy(value, opt->value, len);
1596 		}
1597 		return (0);
1598 	}
1599 	return (ENOENT);
1600 }
1601 
1602 int
1603 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
1604 {
1605 	struct vfsopt *opt;
1606 
1607 	TAILQ_FOREACH(opt, opts, link) {
1608 		if (strcmp(name, opt->name) != 0)
1609 			continue;
1610 		opt->seen = 1;
1611 		if (opt->value == NULL)
1612 			opt->len = len;
1613 		else {
1614 			if (opt->len < len)
1615 				return (EINVAL);
1616 			opt->len = len;
1617 			bcopy(value, opt->value, len);
1618 		}
1619 		return (0);
1620 	}
1621 	return (ENOENT);
1622 }
1623 
1624 int
1625 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
1626 {
1627 	struct vfsopt *opt;
1628 
1629 	TAILQ_FOREACH(opt, opts, link) {
1630 		if (strcmp(name, opt->name) != 0)
1631 			continue;
1632 		opt->seen = 1;
1633 		if (opt->value == NULL)
1634 			opt->len = strlen(value) + 1;
1635 		else if (strlcpy(opt->value, value, opt->len) >= opt->len)
1636 			return (EINVAL);
1637 		return (0);
1638 	}
1639 	return (ENOENT);
1640 }
1641 
1642 /*
1643  * Find and copy a mount option.
1644  *
1645  * The size of the buffer has to be specified
1646  * in len, if it is not the same length as the
1647  * mount option, EINVAL is returned.
1648  * Returns ENOENT if the option is not found.
1649  */
1650 int
1651 vfs_copyopt(opts, name, dest, len)
1652 	struct vfsoptlist *opts;
1653 	const char *name;
1654 	void *dest;
1655 	int len;
1656 {
1657 	struct vfsopt *opt;
1658 
1659 	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1660 
1661 	TAILQ_FOREACH(opt, opts, link) {
1662 		if (strcmp(name, opt->name) == 0) {
1663 			opt->seen = 1;
1664 			if (len != opt->len)
1665 				return (EINVAL);
1666 			bcopy(opt->value, dest, opt->len);
1667 			return (0);
1668 		}
1669 	}
1670 	return (ENOENT);
1671 }
1672 
1673 /*
1674  * This is a helper function for filesystems to traverse their
1675  * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
1676  */
1677 
1678 struct vnode *
1679 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
1680 {
1681 	struct vnode *vp;
1682 
1683 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1684 
1685 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1686 	if (should_yield()) {
1687 		MNT_IUNLOCK(mp);
1688 		kern_yield(PRI_UNCHANGED);
1689 		MNT_ILOCK(mp);
1690 	}
1691 	vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
1692 	while (vp != NULL && vp->v_type == VMARKER)
1693 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1694 
1695 	/* Check if we are done */
1696 	if (vp == NULL) {
1697 		__mnt_vnode_markerfree(mvp, mp);
1698 		return (NULL);
1699 	}
1700 	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1701 	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1702 	return (vp);
1703 }
1704 
1705 struct vnode *
1706 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
1707 {
1708 	struct vnode *vp;
1709 
1710 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1711 
1712 	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1713 	while (vp != NULL && vp->v_type == VMARKER)
1714 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1715 
1716 	/* Check if we are done */
1717 	if (vp == NULL) {
1718 		*mvp = NULL;
1719 		return (NULL);
1720 	}
1721 	MNT_REF(mp);
1722 	MNT_IUNLOCK(mp);
1723 	*mvp = (struct vnode *) malloc(sizeof(struct vnode),
1724 				       M_VNODE_MARKER,
1725 				       M_WAITOK | M_ZERO);
1726 	MNT_ILOCK(mp);
1727 	(*mvp)->v_type = VMARKER;
1728 
1729 	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1730 	while (vp != NULL && vp->v_type == VMARKER)
1731 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1732 
1733 	/* Check if we are done */
1734 	if (vp == NULL) {
1735 		MNT_IUNLOCK(mp);
1736 		free(*mvp, M_VNODE_MARKER);
1737 		MNT_ILOCK(mp);
1738 		*mvp = NULL;
1739 		MNT_REL(mp);
1740 		return (NULL);
1741 	}
1742 	(*mvp)->v_mount = mp;
1743 	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1744 	return (vp);
1745 }
1746 
1747 
1748 void
1749 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
1750 {
1751 
1752 	if (*mvp == NULL)
1753 		return;
1754 
1755 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1756 
1757 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1758 	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1759 	MNT_IUNLOCK(mp);
1760 	free(*mvp, M_VNODE_MARKER);
1761 	MNT_ILOCK(mp);
1762 	*mvp = NULL;
1763 	MNT_REL(mp);
1764 }
1765 
1766 
1767 int
1768 __vfs_statfs(struct mount *mp, struct statfs *sbp)
1769 {
1770 	int error;
1771 
1772 	error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
1773 	if (sbp != &mp->mnt_stat)
1774 		*sbp = mp->mnt_stat;
1775 	return (error);
1776 }
1777 
1778 void
1779 vfs_mountedfrom(struct mount *mp, const char *from)
1780 {
1781 
1782 	bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1783 	strlcpy(mp->mnt_stat.f_mntfromname, from,
1784 	    sizeof mp->mnt_stat.f_mntfromname);
1785 }
1786 
1787 /*
1788  * ---------------------------------------------------------------------
1789  * This is the api for building mount args and mounting filesystems from
1790  * inside the kernel.
1791  *
1792  * The API works by accumulation of individual args.  First error is
1793  * latched.
1794  *
1795  * XXX: should be documented in new manpage kernel_mount(9)
1796  */
1797 
1798 /* A memory allocation which must be freed when we are done */
1799 struct mntaarg {
1800 	SLIST_ENTRY(mntaarg)	next;
1801 };
1802 
1803 /* The header for the mount arguments */
1804 struct mntarg {
1805 	struct iovec *v;
1806 	int len;
1807 	int error;
1808 	SLIST_HEAD(, mntaarg)	list;
1809 };
1810 
1811 /*
1812  * Add a boolean argument.
1813  *
1814  * flag is the boolean value.
1815  * name must start with "no".
1816  */
1817 struct mntarg *
1818 mount_argb(struct mntarg *ma, int flag, const char *name)
1819 {
1820 
1821 	KASSERT(name[0] == 'n' && name[1] == 'o',
1822 	    ("mount_argb(...,%s): name must start with 'no'", name));
1823 
1824 	return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1825 }
1826 
1827 /*
1828  * Add an argument printf style
1829  */
1830 struct mntarg *
1831 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1832 {
1833 	va_list ap;
1834 	struct mntaarg *maa;
1835 	struct sbuf *sb;
1836 	int len;
1837 
1838 	if (ma == NULL) {
1839 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1840 		SLIST_INIT(&ma->list);
1841 	}
1842 	if (ma->error)
1843 		return (ma);
1844 
1845 	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1846 	    M_MOUNT, M_WAITOK);
1847 	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1848 	ma->v[ma->len].iov_len = strlen(name) + 1;
1849 	ma->len++;
1850 
1851 	sb = sbuf_new_auto();
1852 	va_start(ap, fmt);
1853 	sbuf_vprintf(sb, fmt, ap);
1854 	va_end(ap);
1855 	sbuf_finish(sb);
1856 	len = sbuf_len(sb) + 1;
1857 	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1858 	SLIST_INSERT_HEAD(&ma->list, maa, next);
1859 	bcopy(sbuf_data(sb), maa + 1, len);
1860 	sbuf_delete(sb);
1861 
1862 	ma->v[ma->len].iov_base = maa + 1;
1863 	ma->v[ma->len].iov_len = len;
1864 	ma->len++;
1865 
1866 	return (ma);
1867 }
1868 
1869 /*
1870  * Add an argument which is a userland string.
1871  */
1872 struct mntarg *
1873 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1874 {
1875 	struct mntaarg *maa;
1876 	char *tbuf;
1877 
1878 	if (val == NULL)
1879 		return (ma);
1880 	if (ma == NULL) {
1881 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1882 		SLIST_INIT(&ma->list);
1883 	}
1884 	if (ma->error)
1885 		return (ma);
1886 	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1887 	SLIST_INSERT_HEAD(&ma->list, maa, next);
1888 	tbuf = (void *)(maa + 1);
1889 	ma->error = copyinstr(val, tbuf, len, NULL);
1890 	return (mount_arg(ma, name, tbuf, -1));
1891 }
1892 
1893 /*
1894  * Plain argument.
1895  *
1896  * If length is -1, treat value as a C string.
1897  */
1898 struct mntarg *
1899 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1900 {
1901 
1902 	if (ma == NULL) {
1903 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1904 		SLIST_INIT(&ma->list);
1905 	}
1906 	if (ma->error)
1907 		return (ma);
1908 
1909 	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1910 	    M_MOUNT, M_WAITOK);
1911 	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1912 	ma->v[ma->len].iov_len = strlen(name) + 1;
1913 	ma->len++;
1914 
1915 	ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1916 	if (len < 0)
1917 		ma->v[ma->len].iov_len = strlen(val) + 1;
1918 	else
1919 		ma->v[ma->len].iov_len = len;
1920 	ma->len++;
1921 	return (ma);
1922 }
1923 
1924 /*
1925  * Free a mntarg structure
1926  */
1927 static void
1928 free_mntarg(struct mntarg *ma)
1929 {
1930 	struct mntaarg *maa;
1931 
1932 	while (!SLIST_EMPTY(&ma->list)) {
1933 		maa = SLIST_FIRST(&ma->list);
1934 		SLIST_REMOVE_HEAD(&ma->list, next);
1935 		free(maa, M_MOUNT);
1936 	}
1937 	free(ma->v, M_MOUNT);
1938 	free(ma, M_MOUNT);
1939 }
1940 
1941 /*
1942  * Mount a filesystem
1943  */
1944 int
1945 kernel_mount(struct mntarg *ma, uint64_t flags)
1946 {
1947 	struct uio auio;
1948 	int error;
1949 
1950 	KASSERT(ma != NULL, ("kernel_mount NULL ma"));
1951 	KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
1952 	KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
1953 
1954 	auio.uio_iov = ma->v;
1955 	auio.uio_iovcnt = ma->len;
1956 	auio.uio_segflg = UIO_SYSSPACE;
1957 
1958 	error = ma->error;
1959 	if (!error)
1960 		error = vfs_donmount(curthread, flags, &auio);
1961 	free_mntarg(ma);
1962 	return (error);
1963 }
1964 
1965 /*
1966  * A printflike function to mount a filesystem.
1967  */
1968 int
1969 kernel_vmount(int flags, ...)
1970 {
1971 	struct mntarg *ma = NULL;
1972 	va_list ap;
1973 	const char *cp;
1974 	const void *vp;
1975 	int error;
1976 
1977 	va_start(ap, flags);
1978 	for (;;) {
1979 		cp = va_arg(ap, const char *);
1980 		if (cp == NULL)
1981 			break;
1982 		vp = va_arg(ap, const void *);
1983 		ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
1984 	}
1985 	va_end(ap);
1986 
1987 	error = kernel_mount(ma, flags);
1988 	return (error);
1989 }
1990 
1991 void
1992 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
1993 {
1994 
1995 	bcopy(oexp, exp, sizeof(*oexp));
1996 	exp->ex_numsecflavors = 0;
1997 }
1998