xref: /freebsd/sys/kern/vfs_mount.c (revision a35f04fba2ebb8f86d4cbdc710c89a094572b08e)
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  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/conf.h>
42 #include <sys/fcntl.h>
43 #include <sys/jail.h>
44 #include <sys/kernel.h>
45 #include <sys/libkern.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/filedesc.h>
53 #include <sys/reboot.h>
54 #include <sys/sbuf.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysproto.h>
57 #include <sys/sx.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/systm.h>
61 #include <sys/vnode.h>
62 #include <vm/uma.h>
63 
64 #include <geom/geom.h>
65 
66 #include <machine/stdarg.h>
67 
68 #include <security/audit/audit.h>
69 #include <security/mac/mac_framework.h>
70 
71 #define	VFS_MOUNTARG_SIZE_MAX	(1024 * 64)
72 
73 static int	vfs_domount(struct thread *td, const char *fstype, char *fspath,
74 		    uint64_t fsflags, struct vfsoptlist **optlist);
75 static void	free_mntarg(struct mntarg *ma);
76 
77 static int	usermount = 0;
78 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
79     "Unprivileged users may mount and unmount file systems");
80 
81 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
82 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure");
83 static uma_zone_t mount_zone;
84 
85 /* List of mounted filesystems. */
86 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
87 
88 /* For any iteration/modification of mountlist */
89 struct mtx mountlist_mtx;
90 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
91 
92 /*
93  * Global opts, taken by all filesystems
94  */
95 static const char *global_opts[] = {
96 	"errmsg",
97 	"fstype",
98 	"fspath",
99 	"ro",
100 	"rw",
101 	"nosuid",
102 	"noexec",
103 	NULL
104 };
105 
106 static int
107 mount_init(void *mem, int size, int flags)
108 {
109 	struct mount *mp;
110 
111 	mp = (struct mount *)mem;
112 	mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
113 	mtx_init(&mp->mnt_listmtx, "struct mount vlist mtx", NULL, MTX_DEF);
114 	lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
115 	return (0);
116 }
117 
118 static void
119 mount_fini(void *mem, int size)
120 {
121 	struct mount *mp;
122 
123 	mp = (struct mount *)mem;
124 	lockdestroy(&mp->mnt_explock);
125 	mtx_destroy(&mp->mnt_listmtx);
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 occurrence 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 	TAILQ_INIT(&mp->mnt_activevnodelist);
466 	mp->mnt_activevnodelistsize = 0;
467 	TAILQ_INIT(&mp->mnt_tmpfreevnodelist);
468 	mp->mnt_tmpfreevnodelistsize = 0;
469 	mp->mnt_ref = 0;
470 	(void) vfs_busy(mp, MBF_NOWAIT);
471 	atomic_add_acq_int(&vfsp->vfc_refcount, 1);
472 	mp->mnt_op = vfsp->vfc_vfsops;
473 	mp->mnt_vfc = vfsp;
474 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
475 	mp->mnt_gen++;
476 	strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
477 	mp->mnt_vnodecovered = vp;
478 	mp->mnt_cred = crdup(cred);
479 	mp->mnt_stat.f_owner = cred->cr_uid;
480 	strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
481 	mp->mnt_iosize_max = DFLTPHYS;
482 #ifdef MAC
483 	mac_mount_init(mp);
484 	mac_mount_create(cred, mp);
485 #endif
486 	arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
487 	TAILQ_INIT(&mp->mnt_uppers);
488 	return (mp);
489 }
490 
491 /*
492  * Destroy the mount struct previously allocated by vfs_mount_alloc().
493  */
494 void
495 vfs_mount_destroy(struct mount *mp)
496 {
497 
498 	MNT_ILOCK(mp);
499 	mp->mnt_kern_flag |= MNTK_REFEXPIRE;
500 	if (mp->mnt_kern_flag & MNTK_MWAIT) {
501 		mp->mnt_kern_flag &= ~MNTK_MWAIT;
502 		wakeup(mp);
503 	}
504 	while (mp->mnt_ref)
505 		msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
506 	KASSERT(mp->mnt_ref == 0,
507 	    ("%s: invalid refcount in the drain path @ %s:%d", __func__,
508 	    __FILE__, __LINE__));
509 	if (mp->mnt_writeopcount != 0)
510 		panic("vfs_mount_destroy: nonzero writeopcount");
511 	if (mp->mnt_secondary_writes != 0)
512 		panic("vfs_mount_destroy: nonzero secondary_writes");
513 	atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
514 	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
515 		struct vnode *vp;
516 
517 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
518 			vn_printf(vp, "dangling vnode ");
519 		panic("unmount: dangling vnode");
520 	}
521 	KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
522 	if (mp->mnt_nvnodelistsize != 0)
523 		panic("vfs_mount_destroy: nonzero nvnodelistsize");
524 	if (mp->mnt_activevnodelistsize != 0)
525 		panic("vfs_mount_destroy: nonzero activevnodelistsize");
526 	if (mp->mnt_tmpfreevnodelistsize != 0)
527 		panic("vfs_mount_destroy: nonzero tmpfreevnodelistsize");
528 	if (mp->mnt_lockref != 0)
529 		panic("vfs_mount_destroy: nonzero lock refcount");
530 	MNT_IUNLOCK(mp);
531 #ifdef MAC
532 	mac_mount_destroy(mp);
533 #endif
534 	if (mp->mnt_opt != NULL)
535 		vfs_freeopts(mp->mnt_opt);
536 	crfree(mp->mnt_cred);
537 	uma_zfree(mount_zone, mp);
538 }
539 
540 int
541 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
542 {
543 	struct vfsoptlist *optlist;
544 	struct vfsopt *opt, *tmp_opt;
545 	char *fstype, *fspath, *errmsg;
546 	int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
547 
548 	errmsg = fspath = NULL;
549 	errmsg_len = fspathlen = 0;
550 	errmsg_pos = -1;
551 
552 	error = vfs_buildopts(fsoptions, &optlist);
553 	if (error)
554 		return (error);
555 
556 	if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
557 		errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
558 
559 	/*
560 	 * We need these two options before the others,
561 	 * and they are mandatory for any filesystem.
562 	 * Ensure they are NUL terminated as well.
563 	 */
564 	fstypelen = 0;
565 	error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
566 	if (error || fstype[fstypelen - 1] != '\0') {
567 		error = EINVAL;
568 		if (errmsg != NULL)
569 			strncpy(errmsg, "Invalid fstype", errmsg_len);
570 		goto bail;
571 	}
572 	fspathlen = 0;
573 	error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
574 	if (error || fspath[fspathlen - 1] != '\0') {
575 		error = EINVAL;
576 		if (errmsg != NULL)
577 			strncpy(errmsg, "Invalid fspath", errmsg_len);
578 		goto bail;
579 	}
580 
581 	/*
582 	 * We need to see if we have the "update" option
583 	 * before we call vfs_domount(), since vfs_domount() has special
584 	 * logic based on MNT_UPDATE.  This is very important
585 	 * when we want to update the root filesystem.
586 	 */
587 	TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
588 		if (strcmp(opt->name, "update") == 0) {
589 			fsflags |= MNT_UPDATE;
590 			vfs_freeopt(optlist, opt);
591 		}
592 		else if (strcmp(opt->name, "async") == 0)
593 			fsflags |= MNT_ASYNC;
594 		else if (strcmp(opt->name, "force") == 0) {
595 			fsflags |= MNT_FORCE;
596 			vfs_freeopt(optlist, opt);
597 		}
598 		else if (strcmp(opt->name, "reload") == 0) {
599 			fsflags |= MNT_RELOAD;
600 			vfs_freeopt(optlist, opt);
601 		}
602 		else if (strcmp(opt->name, "multilabel") == 0)
603 			fsflags |= MNT_MULTILABEL;
604 		else if (strcmp(opt->name, "noasync") == 0)
605 			fsflags &= ~MNT_ASYNC;
606 		else if (strcmp(opt->name, "noatime") == 0)
607 			fsflags |= MNT_NOATIME;
608 		else if (strcmp(opt->name, "atime") == 0) {
609 			free(opt->name, M_MOUNT);
610 			opt->name = strdup("nonoatime", M_MOUNT);
611 		}
612 		else if (strcmp(opt->name, "noclusterr") == 0)
613 			fsflags |= MNT_NOCLUSTERR;
614 		else if (strcmp(opt->name, "clusterr") == 0) {
615 			free(opt->name, M_MOUNT);
616 			opt->name = strdup("nonoclusterr", M_MOUNT);
617 		}
618 		else if (strcmp(opt->name, "noclusterw") == 0)
619 			fsflags |= MNT_NOCLUSTERW;
620 		else if (strcmp(opt->name, "clusterw") == 0) {
621 			free(opt->name, M_MOUNT);
622 			opt->name = strdup("nonoclusterw", M_MOUNT);
623 		}
624 		else if (strcmp(opt->name, "noexec") == 0)
625 			fsflags |= MNT_NOEXEC;
626 		else if (strcmp(opt->name, "exec") == 0) {
627 			free(opt->name, M_MOUNT);
628 			opt->name = strdup("nonoexec", M_MOUNT);
629 		}
630 		else if (strcmp(opt->name, "nosuid") == 0)
631 			fsflags |= MNT_NOSUID;
632 		else if (strcmp(opt->name, "suid") == 0) {
633 			free(opt->name, M_MOUNT);
634 			opt->name = strdup("nonosuid", M_MOUNT);
635 		}
636 		else if (strcmp(opt->name, "nosymfollow") == 0)
637 			fsflags |= MNT_NOSYMFOLLOW;
638 		else if (strcmp(opt->name, "symfollow") == 0) {
639 			free(opt->name, M_MOUNT);
640 			opt->name = strdup("nonosymfollow", M_MOUNT);
641 		}
642 		else if (strcmp(opt->name, "noro") == 0)
643 			fsflags &= ~MNT_RDONLY;
644 		else if (strcmp(opt->name, "rw") == 0)
645 			fsflags &= ~MNT_RDONLY;
646 		else if (strcmp(opt->name, "ro") == 0)
647 			fsflags |= MNT_RDONLY;
648 		else if (strcmp(opt->name, "rdonly") == 0) {
649 			free(opt->name, M_MOUNT);
650 			opt->name = strdup("ro", M_MOUNT);
651 			fsflags |= MNT_RDONLY;
652 		}
653 		else if (strcmp(opt->name, "suiddir") == 0)
654 			fsflags |= MNT_SUIDDIR;
655 		else if (strcmp(opt->name, "sync") == 0)
656 			fsflags |= MNT_SYNCHRONOUS;
657 		else if (strcmp(opt->name, "union") == 0)
658 			fsflags |= MNT_UNION;
659 		else if (strcmp(opt->name, "automounted") == 0) {
660 			fsflags |= MNT_AUTOMOUNTED;
661 			vfs_freeopt(optlist, opt);
662 		}
663 	}
664 
665 	/*
666 	 * Be ultra-paranoid about making sure the type and fspath
667 	 * variables will fit in our mp buffers, including the
668 	 * terminating NUL.
669 	 */
670 	if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
671 		error = ENAMETOOLONG;
672 		goto bail;
673 	}
674 
675 	error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
676 bail:
677 	/* copyout the errmsg */
678 	if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
679 	    && errmsg_len > 0 && errmsg != NULL) {
680 		if (fsoptions->uio_segflg == UIO_SYSSPACE) {
681 			bcopy(errmsg,
682 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
683 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
684 		} else {
685 			copyout(errmsg,
686 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
687 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
688 		}
689 	}
690 
691 	if (optlist != NULL)
692 		vfs_freeopts(optlist);
693 	return (error);
694 }
695 
696 /*
697  * Old mount API.
698  */
699 #ifndef _SYS_SYSPROTO_H_
700 struct mount_args {
701 	char	*type;
702 	char	*path;
703 	int	flags;
704 	caddr_t	data;
705 };
706 #endif
707 /* ARGSUSED */
708 int
709 sys_mount(td, uap)
710 	struct thread *td;
711 	struct mount_args /* {
712 		char *type;
713 		char *path;
714 		int flags;
715 		caddr_t data;
716 	} */ *uap;
717 {
718 	char *fstype;
719 	struct vfsconf *vfsp = NULL;
720 	struct mntarg *ma = NULL;
721 	uint64_t flags;
722 	int error;
723 
724 	/*
725 	 * Mount flags are now 64-bits. On 32-bit architectures only
726 	 * 32-bits are passed in, but from here on everything handles
727 	 * 64-bit flags correctly.
728 	 */
729 	flags = uap->flags;
730 
731 	AUDIT_ARG_FFLAGS(flags);
732 
733 	/*
734 	 * Filter out MNT_ROOTFS.  We do not want clients of mount() in
735 	 * userspace to set this flag, but we must filter it out if we want
736 	 * MNT_UPDATE on the root file system to work.
737 	 * MNT_ROOTFS should only be set by the kernel when mounting its
738 	 * root file system.
739 	 */
740 	flags &= ~MNT_ROOTFS;
741 
742 	fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
743 	error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
744 	if (error) {
745 		free(fstype, M_TEMP);
746 		return (error);
747 	}
748 
749 	AUDIT_ARG_TEXT(fstype);
750 	vfsp = vfs_byname_kld(fstype, td, &error);
751 	free(fstype, M_TEMP);
752 	if (vfsp == NULL)
753 		return (ENOENT);
754 	if (vfsp->vfc_vfsops->vfs_cmount == NULL)
755 		return (EOPNOTSUPP);
756 
757 	ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
758 	ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
759 	ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
760 	ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
761 	ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
762 
763 	error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags);
764 	return (error);
765 }
766 
767 /*
768  * vfs_domount_first(): first file system mount (not update)
769  */
770 static int
771 vfs_domount_first(
772 	struct thread *td,		/* Calling thread. */
773 	struct vfsconf *vfsp,		/* File system type. */
774 	char *fspath,			/* Mount path. */
775 	struct vnode *vp,		/* Vnode to be covered. */
776 	uint64_t fsflags,		/* Flags common to all filesystems. */
777 	struct vfsoptlist **optlist	/* Options local to the filesystem. */
778 	)
779 {
780 	struct vattr va;
781 	struct mount *mp;
782 	struct vnode *newdp;
783 	int error;
784 
785 	ASSERT_VOP_ELOCKED(vp, __func__);
786 	KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
787 
788 	/*
789 	 * If the user is not root, ensure that they own the directory
790 	 * onto which we are attempting to mount.
791 	 */
792 	error = VOP_GETATTR(vp, &va, td->td_ucred);
793 	if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
794 		error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN, 0);
795 	if (error == 0)
796 		error = vinvalbuf(vp, V_SAVE, 0, 0);
797 	if (error == 0 && vp->v_type != VDIR)
798 		error = ENOTDIR;
799 	if (error == 0) {
800 		VI_LOCK(vp);
801 		if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
802 			vp->v_iflag |= VI_MOUNT;
803 		else
804 			error = EBUSY;
805 		VI_UNLOCK(vp);
806 	}
807 	if (error != 0) {
808 		vput(vp);
809 		return (error);
810 	}
811 	VOP_UNLOCK(vp, 0);
812 
813 	/* Allocate and initialize the filesystem. */
814 	mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
815 	/* XXXMAC: pass to vfs_mount_alloc? */
816 	mp->mnt_optnew = *optlist;
817 	/* Set the mount level flags. */
818 	mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY));
819 
820 	/*
821 	 * Mount the filesystem.
822 	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
823 	 * get.  No freeing of cn_pnbuf.
824 	 */
825 	error = VFS_MOUNT(mp);
826 	if (error != 0) {
827 		vfs_unbusy(mp);
828 		vfs_mount_destroy(mp);
829 		VI_LOCK(vp);
830 		vp->v_iflag &= ~VI_MOUNT;
831 		VI_UNLOCK(vp);
832 		vrele(vp);
833 		return (error);
834 	}
835 
836 	if (mp->mnt_opt != NULL)
837 		vfs_freeopts(mp->mnt_opt);
838 	mp->mnt_opt = mp->mnt_optnew;
839 	*optlist = NULL;
840 	(void)VFS_STATFS(mp, &mp->mnt_stat);
841 
842 	/*
843 	 * Prevent external consumers of mount options from reading mnt_optnew.
844 	 */
845 	mp->mnt_optnew = NULL;
846 
847 	MNT_ILOCK(mp);
848 	if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
849 	    (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
850 		mp->mnt_kern_flag |= MNTK_ASYNC;
851 	else
852 		mp->mnt_kern_flag &= ~MNTK_ASYNC;
853 	MNT_IUNLOCK(mp);
854 
855 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
856 	cache_purge(vp);
857 	VI_LOCK(vp);
858 	vp->v_iflag &= ~VI_MOUNT;
859 	VI_UNLOCK(vp);
860 	vp->v_mountedhere = mp;
861 	/* Place the new filesystem at the end of the mount list. */
862 	mtx_lock(&mountlist_mtx);
863 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
864 	mtx_unlock(&mountlist_mtx);
865 	vfs_event_signal(NULL, VQ_MOUNT, 0);
866 	if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
867 		panic("mount: lost mount");
868 	VOP_UNLOCK(vp, 0);
869 	EVENTHANDLER_INVOKE(vfs_mounted, mp, newdp, td);
870 	VOP_UNLOCK(newdp, 0);
871 	mountcheckdirs(vp, newdp);
872 	vrele(newdp);
873 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
874 		vfs_allocate_syncvnode(mp);
875 	vfs_unbusy(mp);
876 	return (0);
877 }
878 
879 /*
880  * vfs_domount_update(): update of mounted file system
881  */
882 static int
883 vfs_domount_update(
884 	struct thread *td,		/* Calling thread. */
885 	struct vnode *vp,		/* Mount point vnode. */
886 	uint64_t fsflags,		/* Flags common to all filesystems. */
887 	struct vfsoptlist **optlist	/* Options local to the filesystem. */
888 	)
889 {
890 	struct export_args export;
891 	void *bufp;
892 	struct mount *mp;
893 	int error, export_error, len;
894 	uint64_t flag;
895 
896 	ASSERT_VOP_ELOCKED(vp, __func__);
897 	KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
898 	mp = vp->v_mount;
899 
900 	if ((vp->v_vflag & VV_ROOT) == 0) {
901 		if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
902 		    == 0)
903 			error = EXDEV;
904 		else
905 			error = EINVAL;
906 		vput(vp);
907 		return (error);
908 	}
909 
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 	if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
945 		MNT_IUNLOCK(mp);
946 		error = EBUSY;
947 		goto end;
948 	}
949 	mp->mnt_flag &= ~MNT_UPDATEMASK;
950 	mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
951 	    MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
952 	if ((mp->mnt_flag & MNT_ASYNC) == 0)
953 		mp->mnt_kern_flag &= ~MNTK_ASYNC;
954 	MNT_IUNLOCK(mp);
955 	mp->mnt_optnew = *optlist;
956 	vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
957 
958 	/*
959 	 * Mount the filesystem.
960 	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
961 	 * get.  No freeing of cn_pnbuf.
962 	 */
963 	error = VFS_MOUNT(mp);
964 
965 	export_error = 0;
966 	/* Process the export option. */
967 	if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
968 	    &len) == 0) {
969 		/* Assume that there is only 1 ABI for each length. */
970 		switch (len) {
971 		case (sizeof(struct oexport_args)):
972 			bzero(&export, sizeof(export));
973 			/* FALLTHROUGH */
974 		case (sizeof(export)):
975 			bcopy(bufp, &export, len);
976 			export_error = vfs_export(mp, &export);
977 			break;
978 		default:
979 			export_error = EINVAL;
980 			break;
981 		}
982 	}
983 
984 	MNT_ILOCK(mp);
985 	if (error == 0) {
986 		mp->mnt_flag &=	~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
987 		    MNT_SNAPSHOT);
988 	} else {
989 		/*
990 		 * If we fail, restore old mount flags. MNT_QUOTA is special,
991 		 * because it is not part of MNT_UPDATEMASK, but it could have
992 		 * changed in the meantime if quotactl(2) was called.
993 		 * All in all we want current value of MNT_QUOTA, not the old
994 		 * one.
995 		 */
996 		mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
997 	}
998 	if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
999 	    (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1000 		mp->mnt_kern_flag |= MNTK_ASYNC;
1001 	else
1002 		mp->mnt_kern_flag &= ~MNTK_ASYNC;
1003 	MNT_IUNLOCK(mp);
1004 
1005 	if (error != 0)
1006 		goto end;
1007 
1008 	if (mp->mnt_opt != NULL)
1009 		vfs_freeopts(mp->mnt_opt);
1010 	mp->mnt_opt = mp->mnt_optnew;
1011 	*optlist = NULL;
1012 	(void)VFS_STATFS(mp, &mp->mnt_stat);
1013 	/*
1014 	 * Prevent external consumers of mount options from reading
1015 	 * mnt_optnew.
1016 	 */
1017 	mp->mnt_optnew = NULL;
1018 
1019 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
1020 		vfs_allocate_syncvnode(mp);
1021 	else
1022 		vfs_deallocate_syncvnode(mp);
1023 end:
1024 	vfs_unbusy(mp);
1025 	VI_LOCK(vp);
1026 	vp->v_iflag &= ~VI_MOUNT;
1027 	VI_UNLOCK(vp);
1028 	vrele(vp);
1029 	return (error != 0 ? error : export_error);
1030 }
1031 
1032 /*
1033  * vfs_domount(): actually attempt a filesystem mount.
1034  */
1035 static int
1036 vfs_domount(
1037 	struct thread *td,		/* Calling thread. */
1038 	const char *fstype,		/* Filesystem type. */
1039 	char *fspath,			/* Mount path. */
1040 	uint64_t fsflags,		/* Flags common to all filesystems. */
1041 	struct vfsoptlist **optlist	/* Options local to the filesystem. */
1042 	)
1043 {
1044 	struct vfsconf *vfsp;
1045 	struct nameidata nd;
1046 	struct vnode *vp;
1047 	char *pathbuf;
1048 	int error;
1049 
1050 	/*
1051 	 * Be ultra-paranoid about making sure the type and fspath
1052 	 * variables will fit in our mp buffers, including the
1053 	 * terminating NUL.
1054 	 */
1055 	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1056 		return (ENAMETOOLONG);
1057 
1058 	if (jailed(td->td_ucred) || usermount == 0) {
1059 		if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1060 			return (error);
1061 	}
1062 
1063 	/*
1064 	 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1065 	 */
1066 	if (fsflags & MNT_EXPORTED) {
1067 		error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1068 		if (error)
1069 			return (error);
1070 	}
1071 	if (fsflags & MNT_SUIDDIR) {
1072 		error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1073 		if (error)
1074 			return (error);
1075 	}
1076 	/*
1077 	 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1078 	 */
1079 	if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1080 		if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1081 			fsflags |= MNT_NOSUID | MNT_USER;
1082 	}
1083 
1084 	/* Load KLDs before we lock the covered vnode to avoid reversals. */
1085 	vfsp = NULL;
1086 	if ((fsflags & MNT_UPDATE) == 0) {
1087 		/* Don't try to load KLDs if we're mounting the root. */
1088 		if (fsflags & MNT_ROOTFS)
1089 			vfsp = vfs_byname(fstype);
1090 		else
1091 			vfsp = vfs_byname_kld(fstype, td, &error);
1092 		if (vfsp == NULL)
1093 			return (ENODEV);
1094 		if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
1095 			return (EPERM);
1096 	}
1097 
1098 	/*
1099 	 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1100 	 */
1101 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1102 	    UIO_SYSSPACE, fspath, td);
1103 	error = namei(&nd);
1104 	if (error != 0)
1105 		return (error);
1106 	NDFREE(&nd, NDF_ONLY_PNBUF);
1107 	vp = nd.ni_vp;
1108 	if ((fsflags & MNT_UPDATE) == 0) {
1109 		pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1110 		strcpy(pathbuf, fspath);
1111 		error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
1112 		/* debug.disablefullpath == 1 results in ENODEV */
1113 		if (error == 0 || error == ENODEV) {
1114 			error = vfs_domount_first(td, vfsp, pathbuf, vp,
1115 			    fsflags, optlist);
1116 		}
1117 		free(pathbuf, M_TEMP);
1118 	} else
1119 		error = vfs_domount_update(td, vp, fsflags, optlist);
1120 
1121 	return (error);
1122 }
1123 
1124 /*
1125  * Unmount a filesystem.
1126  *
1127  * Note: unmount takes a path to the vnode mounted on as argument, not
1128  * special file (as before).
1129  */
1130 #ifndef _SYS_SYSPROTO_H_
1131 struct unmount_args {
1132 	char	*path;
1133 	int	flags;
1134 };
1135 #endif
1136 /* ARGSUSED */
1137 int
1138 sys_unmount(struct thread *td, struct unmount_args *uap)
1139 {
1140 	struct nameidata nd;
1141 	struct mount *mp;
1142 	char *pathbuf;
1143 	int error, id0, id1;
1144 
1145 	AUDIT_ARG_VALUE(uap->flags);
1146 	if (jailed(td->td_ucred) || usermount == 0) {
1147 		error = priv_check(td, PRIV_VFS_UNMOUNT);
1148 		if (error)
1149 			return (error);
1150 	}
1151 
1152 	pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1153 	error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1154 	if (error) {
1155 		free(pathbuf, M_TEMP);
1156 		return (error);
1157 	}
1158 	if (uap->flags & MNT_BYFSID) {
1159 		AUDIT_ARG_TEXT(pathbuf);
1160 		/* Decode the filesystem ID. */
1161 		if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1162 			free(pathbuf, M_TEMP);
1163 			return (EINVAL);
1164 		}
1165 
1166 		mtx_lock(&mountlist_mtx);
1167 		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1168 			if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1169 			    mp->mnt_stat.f_fsid.val[1] == id1) {
1170 				vfs_ref(mp);
1171 				break;
1172 			}
1173 		}
1174 		mtx_unlock(&mountlist_mtx);
1175 	} else {
1176 		/*
1177 		 * Try to find global path for path argument.
1178 		 */
1179 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1180 		    UIO_SYSSPACE, pathbuf, td);
1181 		if (namei(&nd) == 0) {
1182 			NDFREE(&nd, NDF_ONLY_PNBUF);
1183 			error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1184 			    MNAMELEN);
1185 			if (error == 0 || error == ENODEV)
1186 				vput(nd.ni_vp);
1187 		}
1188 		mtx_lock(&mountlist_mtx);
1189 		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1190 			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
1191 				vfs_ref(mp);
1192 				break;
1193 			}
1194 		}
1195 		mtx_unlock(&mountlist_mtx);
1196 	}
1197 	free(pathbuf, M_TEMP);
1198 	if (mp == NULL) {
1199 		/*
1200 		 * Previously we returned ENOENT for a nonexistent path and
1201 		 * EINVAL for a non-mountpoint.  We cannot tell these apart
1202 		 * now, so in the !MNT_BYFSID case return the more likely
1203 		 * EINVAL for compatibility.
1204 		 */
1205 		return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1206 	}
1207 
1208 	/*
1209 	 * Don't allow unmounting the root filesystem.
1210 	 */
1211 	if (mp->mnt_flag & MNT_ROOTFS) {
1212 		vfs_rel(mp);
1213 		return (EINVAL);
1214 	}
1215 	error = dounmount(mp, uap->flags, td);
1216 	return (error);
1217 }
1218 
1219 /*
1220  * Return error if any of the vnodes, ignoring the root vnode
1221  * and the syncer vnode, have non-zero usecount.
1222  *
1223  * This function is purely advisory - it can return false positives
1224  * and negatives.
1225  */
1226 static int
1227 vfs_check_usecounts(struct mount *mp)
1228 {
1229 	struct vnode *vp, *mvp;
1230 
1231 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1232 		if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
1233 		    vp->v_usecount != 0) {
1234 			VI_UNLOCK(vp);
1235 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1236 			return (EBUSY);
1237 		}
1238 		VI_UNLOCK(vp);
1239 	}
1240 
1241 	return (0);
1242 }
1243 
1244 static void
1245 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
1246 {
1247 
1248 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1249 	mp->mnt_kern_flag &= ~mntkflags;
1250 	if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
1251 		mp->mnt_kern_flag &= ~MNTK_MWAIT;
1252 		wakeup(mp);
1253 	}
1254 	MNT_IUNLOCK(mp);
1255 	if (coveredvp != NULL) {
1256 		VOP_UNLOCK(coveredvp, 0);
1257 		vdrop(coveredvp);
1258 	}
1259 	vn_finished_write(mp);
1260 }
1261 
1262 /*
1263  * Do the actual filesystem unmount.
1264  */
1265 int
1266 dounmount(struct mount *mp, int flags, struct thread *td)
1267 {
1268 	struct vnode *coveredvp, *fsrootvp;
1269 	int error;
1270 	uint64_t async_flag;
1271 	int mnt_gen_r;
1272 
1273 	if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1274 		mnt_gen_r = mp->mnt_gen;
1275 		VI_LOCK(coveredvp);
1276 		vholdl(coveredvp);
1277 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1278 		/*
1279 		 * Check for mp being unmounted while waiting for the
1280 		 * covered vnode lock.
1281 		 */
1282 		if (coveredvp->v_mountedhere != mp ||
1283 		    coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1284 			VOP_UNLOCK(coveredvp, 0);
1285 			vdrop(coveredvp);
1286 			vfs_rel(mp);
1287 			return (EBUSY);
1288 		}
1289 	}
1290 
1291 	/*
1292 	 * Only privileged root, or (if MNT_USER is set) the user that did the
1293 	 * original mount is permitted to unmount this filesystem.
1294 	 */
1295 	error = vfs_suser(mp, td);
1296 	if (error != 0) {
1297 		if (coveredvp != NULL) {
1298 			VOP_UNLOCK(coveredvp, 0);
1299 			vdrop(coveredvp);
1300 		}
1301 		vfs_rel(mp);
1302 		return (error);
1303 	}
1304 
1305 	vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
1306 	MNT_ILOCK(mp);
1307 	if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
1308 	    (mp->mnt_flag & MNT_UPDATE) != 0 ||
1309 	    !TAILQ_EMPTY(&mp->mnt_uppers)) {
1310 		dounmount_cleanup(mp, coveredvp, 0);
1311 		return (EBUSY);
1312 	}
1313 	mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1314 	if (flags & MNT_NONBUSY) {
1315 		MNT_IUNLOCK(mp);
1316 		error = vfs_check_usecounts(mp);
1317 		MNT_ILOCK(mp);
1318 		if (error != 0) {
1319 			dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT |
1320 			    MNTK_NOINSMNTQ);
1321 			return (error);
1322 		}
1323 	}
1324 	/* Allow filesystems to detect that a forced unmount is in progress. */
1325 	if (flags & MNT_FORCE) {
1326 		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1327 		MNT_IUNLOCK(mp);
1328 		/*
1329 		 * Must be done after setting MNTK_UNMOUNTF and before
1330 		 * waiting for mnt_lockref to become 0.
1331 		 */
1332 		VFS_PURGE(mp);
1333 		MNT_ILOCK(mp);
1334 	}
1335 	error = 0;
1336 	if (mp->mnt_lockref) {
1337 		mp->mnt_kern_flag |= MNTK_DRAINING;
1338 		error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1339 		    "mount drain", 0);
1340 	}
1341 	MNT_IUNLOCK(mp);
1342 	KASSERT(mp->mnt_lockref == 0,
1343 	    ("%s: invalid lock refcount in the drain path @ %s:%d",
1344 	    __func__, __FILE__, __LINE__));
1345 	KASSERT(error == 0,
1346 	    ("%s: invalid return value for msleep in the drain path @ %s:%d",
1347 	    __func__, __FILE__, __LINE__));
1348 
1349 	if (mp->mnt_flag & MNT_EXPUBLIC)
1350 		vfs_setpublicfs(NULL, NULL, NULL);
1351 
1352 	/*
1353 	 * From now, we can claim that the use reference on the
1354 	 * coveredvp is ours, and the ref can be released only by
1355 	 * successfull unmount by us, or left for later unmount
1356 	 * attempt.  The previously acquired hold reference is no
1357 	 * longer needed to protect the vnode from reuse.
1358 	 */
1359 	if (coveredvp != NULL)
1360 		vdrop(coveredvp);
1361 
1362 	vfs_msync(mp, MNT_WAIT);
1363 	MNT_ILOCK(mp);
1364 	async_flag = mp->mnt_flag & MNT_ASYNC;
1365 	mp->mnt_flag &= ~MNT_ASYNC;
1366 	mp->mnt_kern_flag &= ~MNTK_ASYNC;
1367 	MNT_IUNLOCK(mp);
1368 	cache_purgevfs(mp, false); /* remove cache entries for this file sys */
1369 	vfs_deallocate_syncvnode(mp);
1370 	/*
1371 	 * For forced unmounts, move process cdir/rdir refs on the fs root
1372 	 * vnode to the covered vnode.  For non-forced unmounts we want
1373 	 * such references to cause an EBUSY error.
1374 	 */
1375 	if ((flags & MNT_FORCE) &&
1376 	    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1377 		if (mp->mnt_vnodecovered != NULL &&
1378 		    (mp->mnt_flag & MNT_IGNORE) == 0)
1379 			mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1380 		if (fsrootvp == rootvnode) {
1381 			vrele(rootvnode);
1382 			rootvnode = NULL;
1383 		}
1384 		vput(fsrootvp);
1385 	}
1386 	if ((mp->mnt_flag & MNT_RDONLY) != 0 || (flags & MNT_FORCE) != 0 ||
1387 	    (error = VFS_SYNC(mp, MNT_WAIT)) == 0)
1388 		error = VFS_UNMOUNT(mp, flags);
1389 	vn_finished_write(mp);
1390 	/*
1391 	 * If we failed to flush the dirty blocks for this mount point,
1392 	 * undo all the cdir/rdir and rootvnode changes we made above.
1393 	 * Unless we failed to do so because the device is reporting that
1394 	 * it doesn't exist anymore.
1395 	 */
1396 	if (error && error != ENXIO) {
1397 		if ((flags & MNT_FORCE) &&
1398 		    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1399 			if (mp->mnt_vnodecovered != NULL &&
1400 			    (mp->mnt_flag & MNT_IGNORE) == 0)
1401 				mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1402 			if (rootvnode == NULL) {
1403 				rootvnode = fsrootvp;
1404 				vref(rootvnode);
1405 			}
1406 			vput(fsrootvp);
1407 		}
1408 		MNT_ILOCK(mp);
1409 		mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1410 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1411 			MNT_IUNLOCK(mp);
1412 			vfs_allocate_syncvnode(mp);
1413 			MNT_ILOCK(mp);
1414 		}
1415 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1416 		mp->mnt_flag |= async_flag;
1417 		if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1418 		    (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1419 			mp->mnt_kern_flag |= MNTK_ASYNC;
1420 		if (mp->mnt_kern_flag & MNTK_MWAIT) {
1421 			mp->mnt_kern_flag &= ~MNTK_MWAIT;
1422 			wakeup(mp);
1423 		}
1424 		MNT_IUNLOCK(mp);
1425 		if (coveredvp)
1426 			VOP_UNLOCK(coveredvp, 0);
1427 		return (error);
1428 	}
1429 	mtx_lock(&mountlist_mtx);
1430 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1431 	mtx_unlock(&mountlist_mtx);
1432 	EVENTHANDLER_INVOKE(vfs_unmounted, mp, td);
1433 	if (coveredvp != NULL) {
1434 		coveredvp->v_mountedhere = NULL;
1435 		vput(coveredvp);
1436 	}
1437 	vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1438 	if (mp == rootdevmp)
1439 		rootdevmp = NULL;
1440 	vfs_mount_destroy(mp);
1441 	return (0);
1442 }
1443 
1444 /*
1445  * Report errors during filesystem mounting.
1446  */
1447 void
1448 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1449 {
1450 	struct vfsoptlist *moptlist = mp->mnt_optnew;
1451 	va_list ap;
1452 	int error, len;
1453 	char *errmsg;
1454 
1455 	error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1456 	if (error || errmsg == NULL || len <= 0)
1457 		return;
1458 
1459 	va_start(ap, fmt);
1460 	vsnprintf(errmsg, (size_t)len, fmt, ap);
1461 	va_end(ap);
1462 }
1463 
1464 void
1465 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1466 {
1467 	va_list ap;
1468 	int error, len;
1469 	char *errmsg;
1470 
1471 	error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1472 	if (error || errmsg == NULL || len <= 0)
1473 		return;
1474 
1475 	va_start(ap, fmt);
1476 	vsnprintf(errmsg, (size_t)len, fmt, ap);
1477 	va_end(ap);
1478 }
1479 
1480 /*
1481  * ---------------------------------------------------------------------
1482  * Functions for querying mount options/arguments from filesystems.
1483  */
1484 
1485 /*
1486  * Check that no unknown options are given
1487  */
1488 int
1489 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1490 {
1491 	struct vfsopt *opt;
1492 	char errmsg[255];
1493 	const char **t, *p, *q;
1494 	int ret = 0;
1495 
1496 	TAILQ_FOREACH(opt, opts, link) {
1497 		p = opt->name;
1498 		q = NULL;
1499 		if (p[0] == 'n' && p[1] == 'o')
1500 			q = p + 2;
1501 		for(t = global_opts; *t != NULL; t++) {
1502 			if (strcmp(*t, p) == 0)
1503 				break;
1504 			if (q != NULL) {
1505 				if (strcmp(*t, q) == 0)
1506 					break;
1507 			}
1508 		}
1509 		if (*t != NULL)
1510 			continue;
1511 		for(t = legal; *t != NULL; t++) {
1512 			if (strcmp(*t, p) == 0)
1513 				break;
1514 			if (q != NULL) {
1515 				if (strcmp(*t, q) == 0)
1516 					break;
1517 			}
1518 		}
1519 		if (*t != NULL)
1520 			continue;
1521 		snprintf(errmsg, sizeof(errmsg),
1522 		    "mount option <%s> is unknown", p);
1523 		ret = EINVAL;
1524 	}
1525 	if (ret != 0) {
1526 		TAILQ_FOREACH(opt, opts, link) {
1527 			if (strcmp(opt->name, "errmsg") == 0) {
1528 				strncpy((char *)opt->value, errmsg, opt->len);
1529 				break;
1530 			}
1531 		}
1532 		if (opt == NULL)
1533 			printf("%s\n", errmsg);
1534 	}
1535 	return (ret);
1536 }
1537 
1538 /*
1539  * Get a mount option by its name.
1540  *
1541  * Return 0 if the option was found, ENOENT otherwise.
1542  * If len is non-NULL it will be filled with the length
1543  * of the option. If buf is non-NULL, it will be filled
1544  * with the address of the option.
1545  */
1546 int
1547 vfs_getopt(opts, name, buf, len)
1548 	struct vfsoptlist *opts;
1549 	const char *name;
1550 	void **buf;
1551 	int *len;
1552 {
1553 	struct vfsopt *opt;
1554 
1555 	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1556 
1557 	TAILQ_FOREACH(opt, opts, link) {
1558 		if (strcmp(name, opt->name) == 0) {
1559 			opt->seen = 1;
1560 			if (len != NULL)
1561 				*len = opt->len;
1562 			if (buf != NULL)
1563 				*buf = opt->value;
1564 			return (0);
1565 		}
1566 	}
1567 	return (ENOENT);
1568 }
1569 
1570 int
1571 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1572 {
1573 	struct vfsopt *opt;
1574 
1575 	if (opts == NULL)
1576 		return (-1);
1577 
1578 	TAILQ_FOREACH(opt, opts, link) {
1579 		if (strcmp(name, opt->name) == 0) {
1580 			opt->seen = 1;
1581 			return (opt->pos);
1582 		}
1583 	}
1584 	return (-1);
1585 }
1586 
1587 int
1588 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
1589 {
1590 	char *opt_value, *vtp;
1591 	quad_t iv;
1592 	int error, opt_len;
1593 
1594 	error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
1595 	if (error != 0)
1596 		return (error);
1597 	if (opt_len == 0 || opt_value == NULL)
1598 		return (EINVAL);
1599 	if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
1600 		return (EINVAL);
1601 	iv = strtoq(opt_value, &vtp, 0);
1602 	if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
1603 		return (EINVAL);
1604 	if (iv < 0)
1605 		return (EINVAL);
1606 	switch (vtp[0]) {
1607 	case 't':
1608 	case 'T':
1609 		iv *= 1024;
1610 	case 'g':
1611 	case 'G':
1612 		iv *= 1024;
1613 	case 'm':
1614 	case 'M':
1615 		iv *= 1024;
1616 	case 'k':
1617 	case 'K':
1618 		iv *= 1024;
1619 	case '\0':
1620 		break;
1621 	default:
1622 		return (EINVAL);
1623 	}
1624 	*value = iv;
1625 
1626 	return (0);
1627 }
1628 
1629 char *
1630 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1631 {
1632 	struct vfsopt *opt;
1633 
1634 	*error = 0;
1635 	TAILQ_FOREACH(opt, opts, link) {
1636 		if (strcmp(name, opt->name) != 0)
1637 			continue;
1638 		opt->seen = 1;
1639 		if (opt->len == 0 ||
1640 		    ((char *)opt->value)[opt->len - 1] != '\0') {
1641 			*error = EINVAL;
1642 			return (NULL);
1643 		}
1644 		return (opt->value);
1645 	}
1646 	*error = ENOENT;
1647 	return (NULL);
1648 }
1649 
1650 int
1651 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
1652 	uint64_t val)
1653 {
1654 	struct vfsopt *opt;
1655 
1656 	TAILQ_FOREACH(opt, opts, link) {
1657 		if (strcmp(name, opt->name) == 0) {
1658 			opt->seen = 1;
1659 			if (w != NULL)
1660 				*w |= val;
1661 			return (1);
1662 		}
1663 	}
1664 	if (w != NULL)
1665 		*w &= ~val;
1666 	return (0);
1667 }
1668 
1669 int
1670 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1671 {
1672 	va_list ap;
1673 	struct vfsopt *opt;
1674 	int ret;
1675 
1676 	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1677 
1678 	TAILQ_FOREACH(opt, opts, link) {
1679 		if (strcmp(name, opt->name) != 0)
1680 			continue;
1681 		opt->seen = 1;
1682 		if (opt->len == 0 || opt->value == NULL)
1683 			return (0);
1684 		if (((char *)opt->value)[opt->len - 1] != '\0')
1685 			return (0);
1686 		va_start(ap, fmt);
1687 		ret = vsscanf(opt->value, fmt, ap);
1688 		va_end(ap);
1689 		return (ret);
1690 	}
1691 	return (0);
1692 }
1693 
1694 int
1695 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
1696 {
1697 	struct vfsopt *opt;
1698 
1699 	TAILQ_FOREACH(opt, opts, link) {
1700 		if (strcmp(name, opt->name) != 0)
1701 			continue;
1702 		opt->seen = 1;
1703 		if (opt->value == NULL)
1704 			opt->len = len;
1705 		else {
1706 			if (opt->len != len)
1707 				return (EINVAL);
1708 			bcopy(value, opt->value, len);
1709 		}
1710 		return (0);
1711 	}
1712 	return (ENOENT);
1713 }
1714 
1715 int
1716 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
1717 {
1718 	struct vfsopt *opt;
1719 
1720 	TAILQ_FOREACH(opt, opts, link) {
1721 		if (strcmp(name, opt->name) != 0)
1722 			continue;
1723 		opt->seen = 1;
1724 		if (opt->value == NULL)
1725 			opt->len = len;
1726 		else {
1727 			if (opt->len < len)
1728 				return (EINVAL);
1729 			opt->len = len;
1730 			bcopy(value, opt->value, len);
1731 		}
1732 		return (0);
1733 	}
1734 	return (ENOENT);
1735 }
1736 
1737 int
1738 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
1739 {
1740 	struct vfsopt *opt;
1741 
1742 	TAILQ_FOREACH(opt, opts, link) {
1743 		if (strcmp(name, opt->name) != 0)
1744 			continue;
1745 		opt->seen = 1;
1746 		if (opt->value == NULL)
1747 			opt->len = strlen(value) + 1;
1748 		else if (strlcpy(opt->value, value, opt->len) >= opt->len)
1749 			return (EINVAL);
1750 		return (0);
1751 	}
1752 	return (ENOENT);
1753 }
1754 
1755 /*
1756  * Find and copy a mount option.
1757  *
1758  * The size of the buffer has to be specified
1759  * in len, if it is not the same length as the
1760  * mount option, EINVAL is returned.
1761  * Returns ENOENT if the option is not found.
1762  */
1763 int
1764 vfs_copyopt(opts, name, dest, len)
1765 	struct vfsoptlist *opts;
1766 	const char *name;
1767 	void *dest;
1768 	int len;
1769 {
1770 	struct vfsopt *opt;
1771 
1772 	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1773 
1774 	TAILQ_FOREACH(opt, opts, link) {
1775 		if (strcmp(name, opt->name) == 0) {
1776 			opt->seen = 1;
1777 			if (len != opt->len)
1778 				return (EINVAL);
1779 			bcopy(opt->value, dest, opt->len);
1780 			return (0);
1781 		}
1782 	}
1783 	return (ENOENT);
1784 }
1785 
1786 int
1787 __vfs_statfs(struct mount *mp, struct statfs *sbp)
1788 {
1789 	int error;
1790 
1791 	error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
1792 	if (sbp != &mp->mnt_stat)
1793 		*sbp = mp->mnt_stat;
1794 	return (error);
1795 }
1796 
1797 void
1798 vfs_mountedfrom(struct mount *mp, const char *from)
1799 {
1800 
1801 	bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1802 	strlcpy(mp->mnt_stat.f_mntfromname, from,
1803 	    sizeof mp->mnt_stat.f_mntfromname);
1804 }
1805 
1806 /*
1807  * ---------------------------------------------------------------------
1808  * This is the api for building mount args and mounting filesystems from
1809  * inside the kernel.
1810  *
1811  * The API works by accumulation of individual args.  First error is
1812  * latched.
1813  *
1814  * XXX: should be documented in new manpage kernel_mount(9)
1815  */
1816 
1817 /* A memory allocation which must be freed when we are done */
1818 struct mntaarg {
1819 	SLIST_ENTRY(mntaarg)	next;
1820 };
1821 
1822 /* The header for the mount arguments */
1823 struct mntarg {
1824 	struct iovec *v;
1825 	int len;
1826 	int error;
1827 	SLIST_HEAD(, mntaarg)	list;
1828 };
1829 
1830 /*
1831  * Add a boolean argument.
1832  *
1833  * flag is the boolean value.
1834  * name must start with "no".
1835  */
1836 struct mntarg *
1837 mount_argb(struct mntarg *ma, int flag, const char *name)
1838 {
1839 
1840 	KASSERT(name[0] == 'n' && name[1] == 'o',
1841 	    ("mount_argb(...,%s): name must start with 'no'", name));
1842 
1843 	return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1844 }
1845 
1846 /*
1847  * Add an argument printf style
1848  */
1849 struct mntarg *
1850 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1851 {
1852 	va_list ap;
1853 	struct mntaarg *maa;
1854 	struct sbuf *sb;
1855 	int len;
1856 
1857 	if (ma == NULL) {
1858 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1859 		SLIST_INIT(&ma->list);
1860 	}
1861 	if (ma->error)
1862 		return (ma);
1863 
1864 	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1865 	    M_MOUNT, M_WAITOK);
1866 	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1867 	ma->v[ma->len].iov_len = strlen(name) + 1;
1868 	ma->len++;
1869 
1870 	sb = sbuf_new_auto();
1871 	va_start(ap, fmt);
1872 	sbuf_vprintf(sb, fmt, ap);
1873 	va_end(ap);
1874 	sbuf_finish(sb);
1875 	len = sbuf_len(sb) + 1;
1876 	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1877 	SLIST_INSERT_HEAD(&ma->list, maa, next);
1878 	bcopy(sbuf_data(sb), maa + 1, len);
1879 	sbuf_delete(sb);
1880 
1881 	ma->v[ma->len].iov_base = maa + 1;
1882 	ma->v[ma->len].iov_len = len;
1883 	ma->len++;
1884 
1885 	return (ma);
1886 }
1887 
1888 /*
1889  * Add an argument which is a userland string.
1890  */
1891 struct mntarg *
1892 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1893 {
1894 	struct mntaarg *maa;
1895 	char *tbuf;
1896 
1897 	if (val == NULL)
1898 		return (ma);
1899 	if (ma == NULL) {
1900 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1901 		SLIST_INIT(&ma->list);
1902 	}
1903 	if (ma->error)
1904 		return (ma);
1905 	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1906 	SLIST_INSERT_HEAD(&ma->list, maa, next);
1907 	tbuf = (void *)(maa + 1);
1908 	ma->error = copyinstr(val, tbuf, len, NULL);
1909 	return (mount_arg(ma, name, tbuf, -1));
1910 }
1911 
1912 /*
1913  * Plain argument.
1914  *
1915  * If length is -1, treat value as a C string.
1916  */
1917 struct mntarg *
1918 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1919 {
1920 
1921 	if (ma == NULL) {
1922 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1923 		SLIST_INIT(&ma->list);
1924 	}
1925 	if (ma->error)
1926 		return (ma);
1927 
1928 	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1929 	    M_MOUNT, M_WAITOK);
1930 	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1931 	ma->v[ma->len].iov_len = strlen(name) + 1;
1932 	ma->len++;
1933 
1934 	ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1935 	if (len < 0)
1936 		ma->v[ma->len].iov_len = strlen(val) + 1;
1937 	else
1938 		ma->v[ma->len].iov_len = len;
1939 	ma->len++;
1940 	return (ma);
1941 }
1942 
1943 /*
1944  * Free a mntarg structure
1945  */
1946 static void
1947 free_mntarg(struct mntarg *ma)
1948 {
1949 	struct mntaarg *maa;
1950 
1951 	while (!SLIST_EMPTY(&ma->list)) {
1952 		maa = SLIST_FIRST(&ma->list);
1953 		SLIST_REMOVE_HEAD(&ma->list, next);
1954 		free(maa, M_MOUNT);
1955 	}
1956 	free(ma->v, M_MOUNT);
1957 	free(ma, M_MOUNT);
1958 }
1959 
1960 /*
1961  * Mount a filesystem
1962  */
1963 int
1964 kernel_mount(struct mntarg *ma, uint64_t flags)
1965 {
1966 	struct uio auio;
1967 	int error;
1968 
1969 	KASSERT(ma != NULL, ("kernel_mount NULL ma"));
1970 	KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
1971 	KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
1972 
1973 	auio.uio_iov = ma->v;
1974 	auio.uio_iovcnt = ma->len;
1975 	auio.uio_segflg = UIO_SYSSPACE;
1976 
1977 	error = ma->error;
1978 	if (!error)
1979 		error = vfs_donmount(curthread, flags, &auio);
1980 	free_mntarg(ma);
1981 	return (error);
1982 }
1983 
1984 /*
1985  * A printflike function to mount a filesystem.
1986  */
1987 int
1988 kernel_vmount(int flags, ...)
1989 {
1990 	struct mntarg *ma = NULL;
1991 	va_list ap;
1992 	const char *cp;
1993 	const void *vp;
1994 	int error;
1995 
1996 	va_start(ap, flags);
1997 	for (;;) {
1998 		cp = va_arg(ap, const char *);
1999 		if (cp == NULL)
2000 			break;
2001 		vp = va_arg(ap, const void *);
2002 		ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2003 	}
2004 	va_end(ap);
2005 
2006 	error = kernel_mount(ma, flags);
2007 	return (error);
2008 }
2009 
2010 void
2011 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
2012 {
2013 
2014 	bcopy(oexp, exp, sizeof(*oexp));
2015 	exp->ex_numsecflavors = 0;
2016 }
2017