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