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