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