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