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