vfs_mount.c revision 181463
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 181463 2008-08-09 11:14:05Z des $");
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#include "opt_rootdevname.h"
71#include "opt_mac.h"
72
73#define	ROOTNAME		"root_device"
74#define	VFS_MOUNTARG_SIZE_MAX	(1024 * 64)
75
76static int	vfs_domount(struct thread *td, const char *fstype,
77		    char *fspath, int fsflags, void *fsdata);
78static int	vfs_mountroot_ask(void);
79static int	vfs_mountroot_try(const char *mountfrom);
80static int	vfs_donmount(struct thread *td, int fsflags,
81		    struct uio *fsoptions);
82static void	free_mntarg(struct mntarg *ma);
83static int	vfs_getopt_pos(struct vfsoptlist *opts, const char *name);
84
85static int	usermount = 0;
86SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
87    "Unprivileged users may mount and unmount file systems");
88
89MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
90MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
91static uma_zone_t mount_zone;
92
93/* List of mounted filesystems. */
94struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
95
96/* For any iteration/modification of mountlist */
97struct mtx mountlist_mtx;
98MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
99
100TAILQ_HEAD(vfsoptlist, vfsopt);
101struct vfsopt {
102	TAILQ_ENTRY(vfsopt) link;
103	char	*name;
104	void	*value;
105	int	len;
106};
107
108/*
109 * The vnode of the system's root (/ in the filesystem, without chroot
110 * active.)
111 */
112struct vnode	*rootvnode;
113
114/*
115 * The root filesystem is detailed in the kernel environment variable
116 * vfs.root.mountfrom, which is expected to be in the general format
117 *
118 * <vfsname>:[<path>]
119 * vfsname   := the name of a VFS known to the kernel and capable
120 *              of being mounted as root
121 * path      := disk device name or other data used by the filesystem
122 *              to locate its physical store
123 */
124
125/*
126 * Global opts, taken by all filesystems
127 */
128static const char *global_opts[] = {
129	"errmsg",
130	"fstype",
131	"fspath",
132	"ro",
133	"rw",
134	"nosuid",
135	"noexec",
136	NULL
137};
138
139/*
140 * The root specifiers we will try if RB_CDROM is specified.
141 */
142static char *cdrom_rootdevnames[] = {
143	"cd9660:cd0",
144	"cd9660:acd0",
145	NULL
146};
147
148/* legacy find-root code */
149char		*rootdevnames[2] = {NULL, NULL};
150#ifndef ROOTDEVNAME
151#  define ROOTDEVNAME NULL
152#endif
153static const char	*ctrootdevname = ROOTDEVNAME;
154
155/*
156 * ---------------------------------------------------------------------
157 * Functions for building and sanitizing the mount options
158 */
159
160/* Remove one mount option. */
161static void
162vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
163{
164
165	TAILQ_REMOVE(opts, opt, link);
166	free(opt->name, M_MOUNT);
167	if (opt->value != NULL)
168		free(opt->value, M_MOUNT);
169#ifdef INVARIANTS
170	else if (opt->len != 0)
171		panic("%s: mount option with NULL value but length != 0",
172		    __func__);
173#endif
174	free(opt, M_MOUNT);
175}
176
177/* Release all resources related to the mount options. */
178void
179vfs_freeopts(struct vfsoptlist *opts)
180{
181	struct vfsopt *opt;
182
183	while (!TAILQ_EMPTY(opts)) {
184		opt = TAILQ_FIRST(opts);
185		vfs_freeopt(opts, opt);
186	}
187	free(opts, M_MOUNT);
188}
189
190void
191vfs_deleteopt(struct vfsoptlist *opts, const char *name)
192{
193	struct vfsopt *opt, *temp;
194
195	TAILQ_FOREACH_SAFE(opt, opts, link, temp)  {
196		if (strcmp(opt->name, name) == 0)
197			vfs_freeopt(opts, opt);
198	}
199}
200
201/*
202 * Check if options are equal (with or without the "no" prefix).
203 */
204static int
205vfs_equalopts(const char *opt1, const char *opt2)
206{
207
208	/* "opt" vs. "opt" or "noopt" vs. "noopt" */
209	if (strcmp(opt1, opt2) == 0)
210		return (1);
211	/* "noopt" vs. "opt" */
212	if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
213		return (1);
214	/* "opt" vs. "noopt" */
215	if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
216		return (1);
217	return (0);
218}
219
220/*
221 * If a mount option is specified several times,
222 * (with or without the "no" prefix) only keep
223 * the last occurence of it.
224 */
225static void
226vfs_sanitizeopts(struct vfsoptlist *opts)
227{
228	struct vfsopt *opt, *opt2, *tmp;
229
230	TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
231		opt2 = TAILQ_PREV(opt, vfsoptlist, link);
232		while (opt2 != NULL) {
233			if (vfs_equalopts(opt->name, opt2->name)) {
234				tmp = TAILQ_PREV(opt2, vfsoptlist, link);
235				vfs_freeopt(opts, opt2);
236				opt2 = tmp;
237			} else {
238				opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
239			}
240		}
241	}
242}
243
244/*
245 * Build a linked list of mount options from a struct uio.
246 */
247static int
248vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
249{
250	struct vfsoptlist *opts;
251	struct vfsopt *opt;
252	size_t memused;
253	unsigned int i, iovcnt;
254	int error, namelen, optlen;
255
256	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
257	TAILQ_INIT(opts);
258	memused = 0;
259	iovcnt = auio->uio_iovcnt;
260	for (i = 0; i < iovcnt; i += 2) {
261		opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
262		namelen = auio->uio_iov[i].iov_len;
263		optlen = auio->uio_iov[i + 1].iov_len;
264		opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
265		opt->value = NULL;
266		opt->len = 0;
267
268		/*
269		 * Do this early, so jumps to "bad" will free the current
270		 * option.
271		 */
272		TAILQ_INSERT_TAIL(opts, opt, link);
273		memused += sizeof(struct vfsopt) + optlen + namelen;
274
275		/*
276		 * Avoid consuming too much memory, and attempts to overflow
277		 * memused.
278		 */
279		if (memused > VFS_MOUNTARG_SIZE_MAX ||
280		    optlen > VFS_MOUNTARG_SIZE_MAX ||
281		    namelen > VFS_MOUNTARG_SIZE_MAX) {
282			error = EINVAL;
283			goto bad;
284		}
285
286		if (auio->uio_segflg == UIO_SYSSPACE) {
287			bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
288		} else {
289			error = copyin(auio->uio_iov[i].iov_base, opt->name,
290			    namelen);
291			if (error)
292				goto bad;
293		}
294		/* Ensure names are null-terminated strings. */
295		if (opt->name[namelen - 1] != '\0') {
296			error = EINVAL;
297			goto bad;
298		}
299		if (optlen != 0) {
300			opt->len = optlen;
301			opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
302			if (auio->uio_segflg == UIO_SYSSPACE) {
303				bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
304				    optlen);
305			} else {
306				error = copyin(auio->uio_iov[i + 1].iov_base,
307				    opt->value, optlen);
308				if (error)
309					goto bad;
310			}
311		}
312	}
313	vfs_sanitizeopts(opts);
314	*options = opts;
315	return (0);
316bad:
317	vfs_freeopts(opts);
318	return (error);
319}
320
321/*
322 * Merge the old mount options with the new ones passed
323 * in the MNT_UPDATE case.
324 *
325 * XXX This function will keep a "nofoo" option in the
326 *     new options if there is no matching "foo" option
327 *     to be cancelled in the old options.  This is a bug
328 *     if the option's canonical name is "foo".  E.g., "noro"
329 *     shouldn't end up in the mount point's active options,
330 *     but it can.
331 */
332static void
333vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
334{
335	struct vfsopt *opt, *opt2, *new;
336
337	TAILQ_FOREACH(opt, opts, link) {
338		/*
339		 * Check that this option hasn't been redefined
340		 * nor cancelled with a "no" mount option.
341		 */
342		opt2 = TAILQ_FIRST(toopts);
343		while (opt2 != NULL) {
344			if (strcmp(opt2->name, opt->name) == 0)
345				goto next;
346			if (strncmp(opt2->name, "no", 2) == 0 &&
347			    strcmp(opt2->name + 2, opt->name) == 0) {
348				vfs_freeopt(toopts, opt2);
349				goto next;
350			}
351			opt2 = TAILQ_NEXT(opt2, link);
352		}
353		/* We want this option, duplicate it. */
354		new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
355		new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
356		strcpy(new->name, opt->name);
357		if (opt->len != 0) {
358			new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
359			bcopy(opt->value, new->value, opt->len);
360		} else {
361			new->value = NULL;
362		}
363		new->len = opt->len;
364		TAILQ_INSERT_TAIL(toopts, new, link);
365next:
366		continue;
367	}
368}
369
370/*
371 * Mount a filesystem.
372 */
373int
374nmount(td, uap)
375	struct thread *td;
376	struct nmount_args /* {
377		struct iovec *iovp;
378		unsigned int iovcnt;
379		int flags;
380	} */ *uap;
381{
382	struct uio *auio;
383	struct iovec *iov;
384	unsigned int i;
385	int error;
386	u_int iovcnt;
387
388	AUDIT_ARG(fflags, uap->flags);
389
390	/*
391	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
392	 * userspace to set this flag, but we must filter it out if we want
393	 * MNT_UPDATE on the root file system to work.
394	 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
395	 */
396	uap->flags &= ~MNT_ROOTFS;
397
398	iovcnt = uap->iovcnt;
399	/*
400	 * Check that we have an even number of iovec's
401	 * and that we have at least two options.
402	 */
403	if ((iovcnt & 1) || (iovcnt < 4))
404		return (EINVAL);
405
406	error = copyinuio(uap->iovp, iovcnt, &auio);
407	if (error)
408		return (error);
409	iov = auio->uio_iov;
410	for (i = 0; i < iovcnt; i++) {
411		if (iov->iov_len > MMAXOPTIONLEN) {
412			free(auio, M_IOV);
413			return (EINVAL);
414		}
415		iov++;
416	}
417	error = vfs_donmount(td, uap->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	MNT_ILOCK(mp);
433	MNT_REF(mp);
434	MNT_IUNLOCK(mp);
435}
436
437void
438vfs_rel(struct mount *mp)
439{
440
441	MNT_ILOCK(mp);
442	MNT_REL(mp);
443	MNT_IUNLOCK(mp);
444}
445
446static int
447mount_init(void *mem, int size, int flags)
448{
449	struct mount *mp;
450
451	mp = (struct mount *)mem;
452	mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
453	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
454	lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
455	return (0);
456}
457
458static void
459mount_fini(void *mem, int size)
460{
461	struct mount *mp;
462
463	mp = (struct mount *)mem;
464	lockdestroy(&mp->mnt_explock);
465	lockdestroy(&mp->mnt_lock);
466	mtx_destroy(&mp->mnt_mtx);
467}
468
469/*
470 * Allocate and initialize the mount point struct.
471 */
472struct mount *
473vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
474    const char *fspath, struct thread *td)
475{
476	struct mount *mp;
477
478	mp = uma_zalloc(mount_zone, M_WAITOK);
479	bzero(&mp->mnt_startzero,
480	    __rangeof(struct mount, mnt_startzero, mnt_endzero));
481	TAILQ_INIT(&mp->mnt_nvnodelist);
482	mp->mnt_nvnodelistsize = 0;
483	mp->mnt_ref = 0;
484	(void) vfs_busy(mp, LK_NOWAIT, 0, td);
485	mp->mnt_op = vfsp->vfc_vfsops;
486	mp->mnt_vfc = vfsp;
487	vfsp->vfc_refcount++;	/* XXX Unlocked */
488	mp->mnt_stat.f_type = vfsp->vfc_typenum;
489	mp->mnt_gen++;
490	strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
491	mp->mnt_vnodecovered = vp;
492	mp->mnt_cred = crdup(td->td_ucred);
493	mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
494	strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
495	mp->mnt_iosize_max = DFLTPHYS;
496#ifdef MAC
497	mac_mount_init(mp);
498	mac_mount_create(td->td_ucred, mp);
499#endif
500	arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
501	return (mp);
502}
503
504/*
505 * Destroy the mount struct previously allocated by vfs_mount_alloc().
506 */
507void
508vfs_mount_destroy(struct mount *mp)
509{
510	int i;
511
512	MNT_ILOCK(mp);
513	for (i = 0; mp->mnt_ref && i < 3; i++)
514		msleep(mp, MNT_MTX(mp), PVFS, "mntref", hz);
515	/*
516	 * This will always cause a 3 second delay in rebooting due to
517	 * refs on the root mountpoint that never go away.  Most of these
518	 * are held by init which never exits.
519	 */
520	if (i == 3 && (!rebooting || bootverbose))
521		printf("Mount point %s had %d dangling refs\n",
522		    mp->mnt_stat.f_mntonname, mp->mnt_ref);
523	if (mp->mnt_holdcnt != 0) {
524		printf("Waiting for mount point to be unheld\n");
525		while (mp->mnt_holdcnt != 0) {
526			mp->mnt_holdcntwaiters++;
527			msleep(&mp->mnt_holdcnt, MNT_MTX(mp),
528			       PZERO, "mntdestroy", 0);
529			mp->mnt_holdcntwaiters--;
530		}
531		printf("mount point unheld\n");
532	}
533	if (mp->mnt_writeopcount > 0) {
534		printf("Waiting for mount point write ops\n");
535		while (mp->mnt_writeopcount > 0) {
536			mp->mnt_kern_flag |= MNTK_SUSPEND;
537			msleep(&mp->mnt_writeopcount,
538			       MNT_MTX(mp),
539			       PZERO, "mntdestroy2", 0);
540		}
541		printf("mount point write ops completed\n");
542	}
543	if (mp->mnt_secondary_writes > 0) {
544		printf("Waiting for mount point secondary write ops\n");
545		while (mp->mnt_secondary_writes > 0) {
546			mp->mnt_kern_flag |= MNTK_SUSPEND;
547			msleep(&mp->mnt_secondary_writes,
548			       MNT_MTX(mp),
549			       PZERO, "mntdestroy3", 0);
550		}
551		printf("mount point secondary write ops completed\n");
552	}
553	MNT_IUNLOCK(mp);
554	mp->mnt_vfc->vfc_refcount--;
555	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
556		struct vnode *vp;
557
558		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
559			vprint("", vp);
560		panic("unmount: dangling vnode");
561	}
562	MNT_ILOCK(mp);
563	if (mp->mnt_kern_flag & MNTK_MWAIT)
564		wakeup(mp);
565	if (mp->mnt_writeopcount != 0)
566		panic("vfs_mount_destroy: nonzero writeopcount");
567	if (mp->mnt_secondary_writes != 0)
568		panic("vfs_mount_destroy: nonzero secondary_writes");
569	if (mp->mnt_nvnodelistsize != 0)
570		panic("vfs_mount_destroy: nonzero nvnodelistsize");
571	mp->mnt_writeopcount = -1000;
572	mp->mnt_nvnodelistsize = -1000;
573	mp->mnt_secondary_writes = -1000;
574	MNT_IUNLOCK(mp);
575#ifdef MAC
576	mac_mount_destroy(mp);
577#endif
578	if (mp->mnt_opt != NULL)
579		vfs_freeopts(mp->mnt_opt);
580	crfree(mp->mnt_cred);
581	uma_zfree(mount_zone, mp);
582}
583
584static int
585vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
586{
587	struct vfsoptlist *optlist;
588	struct vfsopt *opt, *noro_opt, *tmp_opt;
589	char *fstype, *fspath, *errmsg;
590	int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
591	int has_rw, has_noro;
592
593	errmsg = NULL;
594	errmsg_len = 0;
595	errmsg_pos = -1;
596	has_rw = 0;
597	has_noro = 0;
598
599	error = vfs_buildopts(fsoptions, &optlist);
600	if (error)
601		return (error);
602
603	if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
604		errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
605
606	/*
607	 * We need these two options before the others,
608	 * and they are mandatory for any filesystem.
609	 * Ensure they are NUL terminated as well.
610	 */
611	fstypelen = 0;
612	error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
613	if (error || fstype[fstypelen - 1] != '\0') {
614		error = EINVAL;
615		if (errmsg != NULL)
616			strncpy(errmsg, "Invalid fstype", errmsg_len);
617		goto bail;
618	}
619	fspathlen = 0;
620	error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
621	if (error || fspath[fspathlen - 1] != '\0') {
622		error = EINVAL;
623		if (errmsg != NULL)
624			strncpy(errmsg, "Invalid fspath", errmsg_len);
625		goto bail;
626	}
627
628	/*
629	 * We need to see if we have the "update" option
630	 * before we call vfs_domount(), since vfs_domount() has special
631	 * logic based on MNT_UPDATE.  This is very important
632	 * when we want to update the root filesystem.
633	 */
634	TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
635		if (strcmp(opt->name, "update") == 0) {
636			fsflags |= MNT_UPDATE;
637			vfs_freeopt(optlist, opt);
638		}
639		else if (strcmp(opt->name, "async") == 0)
640			fsflags |= MNT_ASYNC;
641		else if (strcmp(opt->name, "force") == 0)
642			fsflags |= MNT_FORCE;
643		else if (strcmp(opt->name, "multilabel") == 0)
644			fsflags |= MNT_MULTILABEL;
645		else if (strcmp(opt->name, "noasync") == 0)
646			fsflags &= ~MNT_ASYNC;
647		else if (strcmp(opt->name, "noatime") == 0)
648			fsflags |= MNT_NOATIME;
649		else if (strcmp(opt->name, "atime") == 0) {
650			free(opt->name, M_MOUNT);
651			opt->name = strdup("nonoatime", M_MOUNT);
652		}
653		else if (strcmp(opt->name, "noclusterr") == 0)
654			fsflags |= MNT_NOCLUSTERR;
655		else if (strcmp(opt->name, "clusterr") == 0) {
656			free(opt->name, M_MOUNT);
657			opt->name = strdup("nonoclusterr", M_MOUNT);
658		}
659		else if (strcmp(opt->name, "noclusterw") == 0)
660			fsflags |= MNT_NOCLUSTERW;
661		else if (strcmp(opt->name, "clusterw") == 0) {
662			free(opt->name, M_MOUNT);
663			opt->name = strdup("nonoclusterw", M_MOUNT);
664		}
665		else if (strcmp(opt->name, "noexec") == 0)
666			fsflags |= MNT_NOEXEC;
667		else if (strcmp(opt->name, "exec") == 0) {
668			free(opt->name, M_MOUNT);
669			opt->name = strdup("nonoexec", M_MOUNT);
670		}
671		else if (strcmp(opt->name, "nosuid") == 0)
672			fsflags |= MNT_NOSUID;
673		else if (strcmp(opt->name, "suid") == 0) {
674			free(opt->name, M_MOUNT);
675			opt->name = strdup("nonosuid", M_MOUNT);
676		}
677		else if (strcmp(opt->name, "nosymfollow") == 0)
678			fsflags |= MNT_NOSYMFOLLOW;
679		else if (strcmp(opt->name, "symfollow") == 0) {
680			free(opt->name, M_MOUNT);
681			opt->name = strdup("nonosymfollow", M_MOUNT);
682		}
683		else if (strcmp(opt->name, "noro") == 0) {
684			fsflags &= ~MNT_RDONLY;
685			has_noro = 1;
686		}
687		else if (strcmp(opt->name, "rw") == 0) {
688			fsflags &= ~MNT_RDONLY;
689			has_rw = 1;
690		}
691		else if (strcmp(opt->name, "ro") == 0)
692			fsflags |= MNT_RDONLY;
693		else if (strcmp(opt->name, "rdonly") == 0) {
694			free(opt->name, M_MOUNT);
695			opt->name = strdup("ro", M_MOUNT);
696			fsflags |= MNT_RDONLY;
697		}
698		else if (strcmp(opt->name, "suiddir") == 0)
699			fsflags |= MNT_SUIDDIR;
700		else if (strcmp(opt->name, "sync") == 0)
701			fsflags |= MNT_SYNCHRONOUS;
702		else if (strcmp(opt->name, "union") == 0)
703			fsflags |= MNT_UNION;
704	}
705
706	/*
707	 * If "rw" was specified as a mount option, and we
708	 * are trying to update a mount-point from "ro" to "rw",
709	 * we need a mount option "noro", since in vfs_mergeopts(),
710	 * "noro" will cancel "ro", but "rw" will not do anything.
711	 */
712	if (has_rw && !has_noro) {
713		noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
714		noro_opt->name = strdup("noro", M_MOUNT);
715		noro_opt->value = NULL;
716		noro_opt->len = 0;
717		TAILQ_INSERT_TAIL(optlist, noro_opt, link);
718	}
719
720	/*
721	 * Be ultra-paranoid about making sure the type and fspath
722	 * variables will fit in our mp buffers, including the
723	 * terminating NUL.
724	 */
725	if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
726		error = ENAMETOOLONG;
727		goto bail;
728	}
729
730	mtx_lock(&Giant);
731	error = vfs_domount(td, fstype, fspath, fsflags, optlist);
732	mtx_unlock(&Giant);
733bail:
734	/* copyout the errmsg */
735	if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
736	    && errmsg_len > 0 && errmsg != NULL) {
737		if (fsoptions->uio_segflg == UIO_SYSSPACE) {
738			bcopy(errmsg,
739			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
740			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
741		} else {
742			copyout(errmsg,
743			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
744			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
745		}
746	}
747
748	if (error != 0)
749		vfs_freeopts(optlist);
750	return (error);
751}
752
753/*
754 * Old mount API.
755 */
756#ifndef _SYS_SYSPROTO_H_
757struct mount_args {
758	char	*type;
759	char	*path;
760	int	flags;
761	caddr_t	data;
762};
763#endif
764/* ARGSUSED */
765int
766mount(td, uap)
767	struct thread *td;
768	struct mount_args /* {
769		char *type;
770		char *path;
771		int flags;
772		caddr_t data;
773	} */ *uap;
774{
775	char *fstype;
776	struct vfsconf *vfsp = NULL;
777	struct mntarg *ma = NULL;
778	int error;
779
780	AUDIT_ARG(fflags, uap->flags);
781
782	/*
783	 * Filter out MNT_ROOTFS.  We do not want clients of mount() in
784	 * userspace to set this flag, but we must filter it out if we want
785	 * MNT_UPDATE on the root file system to work.
786	 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
787	 */
788	uap->flags &= ~MNT_ROOTFS;
789
790	fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
791	error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
792	if (error) {
793		free(fstype, M_TEMP);
794		return (error);
795	}
796
797	AUDIT_ARG(text, fstype);
798	mtx_lock(&Giant);
799	vfsp = vfs_byname_kld(fstype, td, &error);
800	free(fstype, M_TEMP);
801	if (vfsp == NULL) {
802		mtx_unlock(&Giant);
803		return (ENOENT);
804	}
805	if (vfsp->vfc_vfsops->vfs_cmount == NULL) {
806		mtx_unlock(&Giant);
807		return (EOPNOTSUPP);
808	}
809
810	ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
811	ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
812	ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
813	ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
814	ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
815
816	error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
817	mtx_unlock(&Giant);
818	return (error);
819}
820
821
822/*
823 * vfs_domount(): actually attempt a filesystem mount.
824 */
825static int
826vfs_domount(
827	struct thread *td,	/* Calling thread. */
828	const char *fstype,	/* Filesystem type. */
829	char *fspath,		/* Mount path. */
830	int fsflags,		/* Flags common to all filesystems. */
831	void *fsdata		/* Options local to the filesystem. */
832	)
833{
834	struct vnode *vp;
835	struct mount *mp;
836	struct vfsconf *vfsp;
837	struct export_args export;
838	int error, flag = 0;
839	struct vattr va;
840	struct nameidata nd;
841
842	mtx_assert(&Giant, MA_OWNED);
843	/*
844	 * Be ultra-paranoid about making sure the type and fspath
845	 * variables will fit in our mp buffers, including the
846	 * terminating NUL.
847	 */
848	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
849		return (ENAMETOOLONG);
850
851	if (jailed(td->td_ucred) || usermount == 0) {
852		if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
853			return (error);
854	}
855
856	/*
857	 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
858	 */
859	if (fsflags & MNT_EXPORTED) {
860		error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
861		if (error)
862			return (error);
863	}
864	if (fsflags & MNT_SUIDDIR) {
865		error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
866		if (error)
867			return (error);
868	}
869	/*
870	 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
871	 */
872	if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
873		if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
874			fsflags |= MNT_NOSUID | MNT_USER;
875	}
876
877	/* Load KLDs before we lock the covered vnode to avoid reversals. */
878	vfsp = NULL;
879	if ((fsflags & MNT_UPDATE) == 0) {
880		/* Don't try to load KLDs if we're mounting the root. */
881		if (fsflags & MNT_ROOTFS)
882			vfsp = vfs_byname(fstype);
883		else
884			vfsp = vfs_byname_kld(fstype, td, &error);
885		if (vfsp == NULL)
886			return (ENODEV);
887		if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
888			return (EPERM);
889	}
890	/*
891	 * Get vnode to be covered
892	 */
893	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
894	    fspath, td);
895	if ((error = namei(&nd)) != 0)
896		return (error);
897	NDFREE(&nd, NDF_ONLY_PNBUF);
898	vp = nd.ni_vp;
899	if (fsflags & MNT_UPDATE) {
900		if ((vp->v_vflag & VV_ROOT) == 0) {
901			vput(vp);
902			return (EINVAL);
903		}
904		mp = vp->v_mount;
905		MNT_ILOCK(mp);
906		flag = mp->mnt_flag;
907		/*
908		 * We only allow the filesystem to be reloaded if it
909		 * is currently mounted read-only.
910		 */
911		if ((fsflags & MNT_RELOAD) &&
912		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
913			MNT_IUNLOCK(mp);
914			vput(vp);
915			return (EOPNOTSUPP);	/* Needs translation */
916		}
917		MNT_IUNLOCK(mp);
918		/*
919		 * Only privileged root, or (if MNT_USER is set) the user that
920		 * did the original mount is permitted to update it.
921		 */
922		error = vfs_suser(mp, td);
923		if (error) {
924			vput(vp);
925			return (error);
926		}
927		if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
928			vput(vp);
929			return (EBUSY);
930		}
931		VI_LOCK(vp);
932		if ((vp->v_iflag & VI_MOUNT) != 0 ||
933		    vp->v_mountedhere != NULL) {
934			VI_UNLOCK(vp);
935			vfs_unbusy(mp, td);
936			vput(vp);
937			return (EBUSY);
938		}
939		vp->v_iflag |= VI_MOUNT;
940		VI_UNLOCK(vp);
941		MNT_ILOCK(mp);
942		mp->mnt_flag |= fsflags &
943		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
944		MNT_IUNLOCK(mp);
945		VOP_UNLOCK(vp, 0);
946		mp->mnt_optnew = fsdata;
947		vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
948	} else {
949		/*
950		 * If the user is not root, ensure that they own the directory
951		 * onto which we are attempting to mount.
952		 */
953		error = VOP_GETATTR(vp, &va, td->td_ucred, td);
954		if (error) {
955			vput(vp);
956			return (error);
957		}
958		if (va.va_uid != td->td_ucred->cr_uid) {
959			error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN,
960			    0);
961			if (error) {
962				vput(vp);
963				return (error);
964			}
965		}
966		error = vinvalbuf(vp, V_SAVE, td, 0, 0);
967		if (error != 0) {
968			vput(vp);
969			return (error);
970		}
971		if (vp->v_type != VDIR) {
972			vput(vp);
973			return (ENOTDIR);
974		}
975		VI_LOCK(vp);
976		if ((vp->v_iflag & VI_MOUNT) != 0 ||
977		    vp->v_mountedhere != NULL) {
978			VI_UNLOCK(vp);
979			vput(vp);
980			return (EBUSY);
981		}
982		vp->v_iflag |= VI_MOUNT;
983		VI_UNLOCK(vp);
984
985		/*
986		 * Allocate and initialize the filesystem.
987		 */
988		mp = vfs_mount_alloc(vp, vfsp, fspath, td);
989		VOP_UNLOCK(vp, 0);
990
991		/* XXXMAC: pass to vfs_mount_alloc? */
992		mp->mnt_optnew = fsdata;
993	}
994
995	/*
996	 * Set the mount level flags.
997	 */
998	MNT_ILOCK(mp);
999	mp->mnt_flag = (mp->mnt_flag & ~MNT_UPDATEMASK) |
1000		(fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS |
1001			    MNT_RDONLY));
1002	if ((mp->mnt_flag & MNT_ASYNC) == 0)
1003		mp->mnt_kern_flag &= ~MNTK_ASYNC;
1004	MNT_IUNLOCK(mp);
1005	/*
1006	 * Mount the filesystem.
1007	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1008	 * get.  No freeing of cn_pnbuf.
1009	 */
1010        error = VFS_MOUNT(mp, td);
1011
1012	/*
1013	 * Process the export option only if we are
1014	 * updating mount options.
1015	 */
1016	if (!error && (fsflags & MNT_UPDATE)) {
1017		if (vfs_copyopt(mp->mnt_optnew, "export", &export,
1018		    sizeof(export)) == 0)
1019			error = vfs_export(mp, &export);
1020	}
1021
1022	if (!error) {
1023		if (mp->mnt_opt != NULL)
1024			vfs_freeopts(mp->mnt_opt);
1025		mp->mnt_opt = mp->mnt_optnew;
1026		(void)VFS_STATFS(mp, &mp->mnt_stat, td);
1027	}
1028	/*
1029	 * Prevent external consumers of mount options from reading
1030	 * mnt_optnew.
1031	*/
1032	mp->mnt_optnew = NULL;
1033	if (mp->mnt_flag & MNT_UPDATE) {
1034		MNT_ILOCK(mp);
1035		if (error)
1036			mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) |
1037				(flag & ~MNT_QUOTA);
1038		else
1039			mp->mnt_flag &=	~(MNT_UPDATE | MNT_RELOAD |
1040					  MNT_FORCE | MNT_SNAPSHOT);
1041		if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1042			mp->mnt_kern_flag |= MNTK_ASYNC;
1043		else
1044			mp->mnt_kern_flag &= ~MNTK_ASYNC;
1045		MNT_IUNLOCK(mp);
1046		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1047			if (mp->mnt_syncer == NULL)
1048				error = vfs_allocate_syncvnode(mp);
1049		} else {
1050			if (mp->mnt_syncer != NULL)
1051				vrele(mp->mnt_syncer);
1052			mp->mnt_syncer = NULL;
1053		}
1054		vfs_unbusy(mp, td);
1055		VI_LOCK(vp);
1056		vp->v_iflag &= ~VI_MOUNT;
1057		VI_UNLOCK(vp);
1058		vrele(vp);
1059		return (error);
1060	}
1061	MNT_ILOCK(mp);
1062	if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1063		mp->mnt_kern_flag |= MNTK_ASYNC;
1064	else
1065		mp->mnt_kern_flag &= ~MNTK_ASYNC;
1066	MNT_IUNLOCK(mp);
1067	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1068	/*
1069	 * Put the new filesystem on the mount list after root.
1070	 */
1071	cache_purge(vp);
1072	if (!error) {
1073		struct vnode *newdp;
1074
1075		VI_LOCK(vp);
1076		vp->v_iflag &= ~VI_MOUNT;
1077		VI_UNLOCK(vp);
1078		vp->v_mountedhere = mp;
1079		mtx_lock(&mountlist_mtx);
1080		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1081		mtx_unlock(&mountlist_mtx);
1082		vfs_event_signal(NULL, VQ_MOUNT, 0);
1083		if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
1084			panic("mount: lost mount");
1085		mountcheckdirs(vp, newdp);
1086		vput(newdp);
1087		VOP_UNLOCK(vp, 0);
1088		if ((mp->mnt_flag & MNT_RDONLY) == 0)
1089			error = vfs_allocate_syncvnode(mp);
1090		vfs_unbusy(mp, td);
1091		if (error)
1092			vrele(vp);
1093	} else {
1094		VI_LOCK(vp);
1095		vp->v_iflag &= ~VI_MOUNT;
1096		VI_UNLOCK(vp);
1097		vfs_unbusy(mp, td);
1098		vfs_mount_destroy(mp);
1099		vput(vp);
1100	}
1101	return (error);
1102}
1103
1104/*
1105 * Unmount a filesystem.
1106 *
1107 * Note: unmount takes a path to the vnode mounted on as argument, not
1108 * special file (as before).
1109 */
1110#ifndef _SYS_SYSPROTO_H_
1111struct unmount_args {
1112	char	*path;
1113	int	flags;
1114};
1115#endif
1116/* ARGSUSED */
1117int
1118unmount(td, uap)
1119	struct thread *td;
1120	register struct unmount_args /* {
1121		char *path;
1122		int flags;
1123	} */ *uap;
1124{
1125	struct mount *mp;
1126	char *pathbuf;
1127	int error, id0, id1;
1128
1129	if (jailed(td->td_ucred) || usermount == 0) {
1130		error = priv_check(td, PRIV_VFS_UNMOUNT);
1131		if (error)
1132			return (error);
1133	}
1134
1135	pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1136	error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1137	if (error) {
1138		free(pathbuf, M_TEMP);
1139		return (error);
1140	}
1141	AUDIT_ARG(upath, td, pathbuf, ARG_UPATH1);
1142	mtx_lock(&Giant);
1143	if (uap->flags & MNT_BYFSID) {
1144		/* Decode the filesystem ID. */
1145		if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1146			mtx_unlock(&Giant);
1147			free(pathbuf, M_TEMP);
1148			return (EINVAL);
1149		}
1150
1151		mtx_lock(&mountlist_mtx);
1152		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1153			if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1154			    mp->mnt_stat.f_fsid.val[1] == id1)
1155				break;
1156		}
1157		mtx_unlock(&mountlist_mtx);
1158	} else {
1159		mtx_lock(&mountlist_mtx);
1160		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1161			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1162				break;
1163		}
1164		mtx_unlock(&mountlist_mtx);
1165	}
1166	free(pathbuf, M_TEMP);
1167	if (mp == NULL) {
1168		/*
1169		 * Previously we returned ENOENT for a nonexistent path and
1170		 * EINVAL for a non-mountpoint.  We cannot tell these apart
1171		 * now, so in the !MNT_BYFSID case return the more likely
1172		 * EINVAL for compatibility.
1173		 */
1174		mtx_unlock(&Giant);
1175		return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1176	}
1177
1178	/*
1179	 * Don't allow unmounting the root filesystem.
1180	 */
1181	if (mp->mnt_flag & MNT_ROOTFS) {
1182		mtx_unlock(&Giant);
1183		return (EINVAL);
1184	}
1185	error = dounmount(mp, uap->flags, td);
1186	mtx_unlock(&Giant);
1187	return (error);
1188}
1189
1190/*
1191 * Do the actual filesystem unmount.
1192 */
1193int
1194dounmount(mp, flags, td)
1195	struct mount *mp;
1196	int flags;
1197	struct thread *td;
1198{
1199	struct vnode *coveredvp, *fsrootvp;
1200	int error;
1201	int async_flag;
1202	int mnt_gen_r;
1203
1204	mtx_assert(&Giant, MA_OWNED);
1205
1206	if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1207		mnt_gen_r = mp->mnt_gen;
1208		VI_LOCK(coveredvp);
1209		vholdl(coveredvp);
1210		vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1211		vdrop(coveredvp);
1212		/*
1213		 * Check for mp being unmounted while waiting for the
1214		 * covered vnode lock.
1215		 */
1216		if (coveredvp->v_mountedhere != mp ||
1217		    coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1218			VOP_UNLOCK(coveredvp, 0);
1219			return (EBUSY);
1220		}
1221	}
1222	/*
1223	 * Only privileged root, or (if MNT_USER is set) the user that did the
1224	 * original mount is permitted to unmount this filesystem.
1225	 */
1226	error = vfs_suser(mp, td);
1227	if (error) {
1228		if (coveredvp)
1229			VOP_UNLOCK(coveredvp, 0);
1230		return (error);
1231	}
1232
1233	MNT_ILOCK(mp);
1234	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1235		MNT_IUNLOCK(mp);
1236		if (coveredvp)
1237			VOP_UNLOCK(coveredvp, 0);
1238		return (EBUSY);
1239	}
1240	mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1241	/* Allow filesystems to detect that a forced unmount is in progress. */
1242	if (flags & MNT_FORCE)
1243		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1244	error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1245	    ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp));
1246	if (error) {
1247		MNT_ILOCK(mp);
1248		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_NOINSMNTQ |
1249		    MNTK_UNMOUNTF);
1250		if (mp->mnt_kern_flag & MNTK_MWAIT)
1251			wakeup(mp);
1252		MNT_IUNLOCK(mp);
1253		if (coveredvp)
1254			VOP_UNLOCK(coveredvp, 0);
1255		return (error);
1256	}
1257	vn_start_write(NULL, &mp, V_WAIT);
1258
1259	if (mp->mnt_flag & MNT_EXPUBLIC)
1260		vfs_setpublicfs(NULL, NULL, NULL);
1261
1262	vfs_msync(mp, MNT_WAIT);
1263	MNT_ILOCK(mp);
1264	async_flag = mp->mnt_flag & MNT_ASYNC;
1265	mp->mnt_flag &= ~MNT_ASYNC;
1266	mp->mnt_kern_flag &= ~MNTK_ASYNC;
1267	MNT_IUNLOCK(mp);
1268	cache_purgevfs(mp);	/* remove cache entries for this file sys */
1269	if (mp->mnt_syncer != NULL)
1270		vrele(mp->mnt_syncer);
1271	/*
1272	 * For forced unmounts, move process cdir/rdir refs on the fs root
1273	 * vnode to the covered vnode.  For non-forced unmounts we want
1274	 * such references to cause an EBUSY error.
1275	 */
1276	if ((flags & MNT_FORCE) &&
1277	    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1278		if (mp->mnt_vnodecovered != NULL)
1279			mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1280		if (fsrootvp == rootvnode) {
1281			vrele(rootvnode);
1282			rootvnode = NULL;
1283		}
1284		vput(fsrootvp);
1285	}
1286	if (((mp->mnt_flag & MNT_RDONLY) ||
1287	     (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
1288	    (flags & MNT_FORCE)) {
1289		error = VFS_UNMOUNT(mp, flags, td);
1290	}
1291	vn_finished_write(mp);
1292	/*
1293	 * If we failed to flush the dirty blocks for this mount point,
1294	 * undo all the cdir/rdir and rootvnode changes we made above.
1295	 * Unless we failed to do so because the device is reporting that
1296	 * it doesn't exist anymore.
1297	 */
1298	if (error && error != ENXIO) {
1299		if ((flags & MNT_FORCE) &&
1300		    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1301			if (mp->mnt_vnodecovered != NULL)
1302				mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1303			if (rootvnode == NULL) {
1304				rootvnode = fsrootvp;
1305				vref(rootvnode);
1306			}
1307			vput(fsrootvp);
1308		}
1309		MNT_ILOCK(mp);
1310		mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1311		if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) {
1312			MNT_IUNLOCK(mp);
1313			(void) vfs_allocate_syncvnode(mp);
1314			MNT_ILOCK(mp);
1315		}
1316		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1317		mp->mnt_flag |= async_flag;
1318		if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1319			mp->mnt_kern_flag |= MNTK_ASYNC;
1320		lockmgr(&mp->mnt_lock, LK_RELEASE, NULL);
1321		if (mp->mnt_kern_flag & MNTK_MWAIT)
1322			wakeup(mp);
1323		MNT_IUNLOCK(mp);
1324		if (coveredvp)
1325			VOP_UNLOCK(coveredvp, 0);
1326		return (error);
1327	}
1328	mtx_lock(&mountlist_mtx);
1329	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1330	mtx_unlock(&mountlist_mtx);
1331	if (coveredvp != NULL) {
1332		coveredvp->v_mountedhere = NULL;
1333		vput(coveredvp);
1334	}
1335	vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1336	lockmgr(&mp->mnt_lock, LK_RELEASE, NULL);
1337	vfs_mount_destroy(mp);
1338	return (0);
1339}
1340
1341/*
1342 * ---------------------------------------------------------------------
1343 * Mounting of root filesystem
1344 *
1345 */
1346
1347struct root_hold_token {
1348	const char			*who;
1349	LIST_ENTRY(root_hold_token)	list;
1350};
1351
1352static LIST_HEAD(, root_hold_token)	root_holds =
1353    LIST_HEAD_INITIALIZER(&root_holds);
1354
1355static int root_mount_complete;
1356
1357/*
1358 * Hold root mount.
1359 */
1360struct root_hold_token *
1361root_mount_hold(const char *identifier)
1362{
1363	struct root_hold_token *h;
1364
1365	h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1366	h->who = identifier;
1367	mtx_lock(&mountlist_mtx);
1368	LIST_INSERT_HEAD(&root_holds, h, list);
1369	mtx_unlock(&mountlist_mtx);
1370	return (h);
1371}
1372
1373/*
1374 * Release root mount.
1375 */
1376void
1377root_mount_rel(struct root_hold_token *h)
1378{
1379
1380	mtx_lock(&mountlist_mtx);
1381	LIST_REMOVE(h, list);
1382	wakeup(&root_holds);
1383	mtx_unlock(&mountlist_mtx);
1384	free(h, M_DEVBUF);
1385}
1386
1387/*
1388 * Wait for all subsystems to release root mount.
1389 */
1390static void
1391root_mount_prepare(void)
1392{
1393	struct root_hold_token *h;
1394
1395	for (;;) {
1396		DROP_GIANT();
1397		g_waitidle();
1398		PICKUP_GIANT();
1399		mtx_lock(&mountlist_mtx);
1400		if (LIST_EMPTY(&root_holds)) {
1401			mtx_unlock(&mountlist_mtx);
1402			break;
1403		}
1404		printf("Root mount waiting for:");
1405		LIST_FOREACH(h, &root_holds, list)
1406			printf(" %s", h->who);
1407		printf("\n");
1408		msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1409		    hz);
1410	}
1411}
1412
1413/*
1414 * Root was mounted, share the good news.
1415 */
1416static void
1417root_mount_done(void)
1418{
1419
1420	/*
1421	 * Use a mutex to prevent the wakeup being missed and waiting for
1422	 * an extra 1 second sleep.
1423	 */
1424	mtx_lock(&mountlist_mtx);
1425	root_mount_complete = 1;
1426	wakeup(&root_mount_complete);
1427	mtx_unlock(&mountlist_mtx);
1428}
1429
1430/*
1431 * Return true if root is already mounted.
1432 */
1433int
1434root_mounted(void)
1435{
1436
1437	/* No mutex is acquired here because int stores are atomic. */
1438	return (root_mount_complete);
1439}
1440
1441/*
1442 * Wait until root is mounted.
1443 */
1444void
1445root_mount_wait(void)
1446{
1447
1448	/*
1449	 * Panic on an obvious deadlock - the function can't be called from
1450	 * a thread which is doing the whole SYSINIT stuff.
1451	 */
1452	KASSERT(curthread->td_proc->p_pid != 0,
1453	    ("root_mount_wait: cannot be called from the swapper thread"));
1454	mtx_lock(&mountlist_mtx);
1455	while (!root_mount_complete) {
1456		msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait",
1457		    hz);
1458	}
1459	mtx_unlock(&mountlist_mtx);
1460}
1461
1462static void
1463set_rootvnode(struct thread *td)
1464{
1465	struct proc *p;
1466
1467	if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1468		panic("Cannot find root vnode");
1469
1470	p = td->td_proc;
1471	FILEDESC_XLOCK(p->p_fd);
1472
1473	if (p->p_fd->fd_cdir != NULL)
1474		vrele(p->p_fd->fd_cdir);
1475	p->p_fd->fd_cdir = rootvnode;
1476	VREF(rootvnode);
1477
1478	if (p->p_fd->fd_rdir != NULL)
1479		vrele(p->p_fd->fd_rdir);
1480	p->p_fd->fd_rdir = rootvnode;
1481	VREF(rootvnode);
1482
1483	FILEDESC_XUNLOCK(p->p_fd);
1484
1485	VOP_UNLOCK(rootvnode, 0);
1486
1487	EVENTHANDLER_INVOKE(mountroot);
1488}
1489
1490/*
1491 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1492 * yet.  Create a /dev -> / symlink so that absolute pathnames will lookup.
1493 */
1494
1495static void
1496devfs_first(void)
1497{
1498	struct thread *td = curthread;
1499	struct vfsoptlist *opts;
1500	struct vfsconf *vfsp;
1501	struct mount *mp = NULL;
1502	int error;
1503
1504	vfsp = vfs_byname("devfs");
1505	KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1506	if (vfsp == NULL)
1507		return;
1508
1509	mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td);
1510
1511	error = VFS_MOUNT(mp, td);
1512	KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1513	if (error)
1514		return;
1515
1516	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1517	TAILQ_INIT(opts);
1518	mp->mnt_opt = opts;
1519
1520	mtx_lock(&mountlist_mtx);
1521	TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1522	mtx_unlock(&mountlist_mtx);
1523
1524	set_rootvnode(td);
1525
1526	error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1527	if (error)
1528		printf("kern_symlink /dev -> / returns %d\n", error);
1529}
1530
1531/*
1532 * Surgically move our devfs to be mounted on /dev.
1533 */
1534
1535static void
1536devfs_fixup(struct thread *td)
1537{
1538	struct nameidata nd;
1539	int error;
1540	struct vnode *vp, *dvp;
1541	struct mount *mp;
1542
1543	/* Remove our devfs mount from the mountlist and purge the cache */
1544	mtx_lock(&mountlist_mtx);
1545	mp = TAILQ_FIRST(&mountlist);
1546	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1547	mtx_unlock(&mountlist_mtx);
1548	cache_purgevfs(mp);
1549
1550	VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1551	VI_LOCK(dvp);
1552	dvp->v_iflag &= ~VI_MOUNT;
1553	VI_UNLOCK(dvp);
1554	dvp->v_mountedhere = NULL;
1555
1556	/* Set up the real rootvnode, and purge the cache */
1557	TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1558	set_rootvnode(td);
1559	cache_purgevfs(rootvnode->v_mount);
1560
1561	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1562	error = namei(&nd);
1563	if (error) {
1564		printf("Lookup of /dev for devfs, error: %d\n", error);
1565		return;
1566	}
1567	NDFREE(&nd, NDF_ONLY_PNBUF);
1568	vp = nd.ni_vp;
1569	if (vp->v_type != VDIR) {
1570		vput(vp);
1571	}
1572	error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1573	if (error) {
1574		vput(vp);
1575	}
1576	cache_purge(vp);
1577	mp->mnt_vnodecovered = vp;
1578	vp->v_mountedhere = mp;
1579	mtx_lock(&mountlist_mtx);
1580	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1581	mtx_unlock(&mountlist_mtx);
1582	VOP_UNLOCK(vp, 0);
1583	vput(dvp);
1584	vfs_unbusy(mp, td);
1585
1586	/* Unlink the no longer needed /dev/dev -> / symlink */
1587	kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1588}
1589
1590/*
1591 * Report errors during filesystem mounting.
1592 */
1593void
1594vfs_mount_error(struct mount *mp, const char *fmt, ...)
1595{
1596	struct vfsoptlist *moptlist = mp->mnt_optnew;
1597	va_list ap;
1598	int error, len;
1599	char *errmsg;
1600
1601	error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1602	if (error || errmsg == NULL || len <= 0)
1603		return;
1604
1605	va_start(ap, fmt);
1606	vsnprintf(errmsg, (size_t)len, fmt, ap);
1607	va_end(ap);
1608}
1609
1610/*
1611 * Find and mount the root filesystem
1612 */
1613void
1614vfs_mountroot(void)
1615{
1616	char *cp;
1617	int error, i, asked = 0;
1618
1619	root_mount_prepare();
1620
1621	mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
1622	    NULL, NULL, mount_init, mount_fini,
1623	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1624	devfs_first();
1625
1626	/*
1627	 * We are booted with instructions to prompt for the root filesystem.
1628	 */
1629	if (boothowto & RB_ASKNAME) {
1630		if (!vfs_mountroot_ask())
1631			goto mounted;
1632		asked = 1;
1633	}
1634
1635	/*
1636	 * The root filesystem information is compiled in, and we are
1637	 * booted with instructions to use it.
1638	 */
1639	if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1640		if (!vfs_mountroot_try(ctrootdevname))
1641			goto mounted;
1642		ctrootdevname = NULL;
1643	}
1644
1645	/*
1646	 * We've been given the generic "use CDROM as root" flag.  This is
1647	 * necessary because one media may be used in many different
1648	 * devices, so we need to search for them.
1649	 */
1650	if (boothowto & RB_CDROM) {
1651		for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1652			if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1653				goto mounted;
1654		}
1655	}
1656
1657	/*
1658	 * Try to use the value read by the loader from /etc/fstab, or
1659	 * supplied via some other means.  This is the preferred
1660	 * mechanism.
1661	 */
1662	cp = getenv("vfs.root.mountfrom");
1663	if (cp != NULL) {
1664		error = vfs_mountroot_try(cp);
1665		freeenv(cp);
1666		if (!error)
1667			goto mounted;
1668	}
1669
1670	/*
1671	 * Try values that may have been computed by code during boot
1672	 */
1673	if (!vfs_mountroot_try(rootdevnames[0]))
1674		goto mounted;
1675	if (!vfs_mountroot_try(rootdevnames[1]))
1676		goto mounted;
1677
1678	/*
1679	 * If we (still) have a compiled-in default, try it.
1680	 */
1681	if (ctrootdevname != NULL)
1682		if (!vfs_mountroot_try(ctrootdevname))
1683			goto mounted;
1684	/*
1685	 * Everything so far has failed, prompt on the console if we haven't
1686	 * already tried that.
1687	 */
1688	if (!asked)
1689		if (!vfs_mountroot_ask())
1690			goto mounted;
1691
1692	panic("Root mount failed, startup aborted.");
1693
1694mounted:
1695	root_mount_done();
1696}
1697
1698/*
1699 * Mount (mountfrom) as the root filesystem.
1700 */
1701static int
1702vfs_mountroot_try(const char *mountfrom)
1703{
1704	struct mount	*mp;
1705	char		*vfsname, *path;
1706	time_t		timebase;
1707	int		error;
1708	char		patt[32];
1709
1710	vfsname = NULL;
1711	path    = NULL;
1712	mp      = NULL;
1713	error   = EINVAL;
1714
1715	if (mountfrom == NULL)
1716		return (error);		/* don't complain */
1717	printf("Trying to mount root from %s\n", mountfrom);
1718
1719	/* parse vfs name and path */
1720	vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1721	path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1722	vfsname[0] = path[0] = 0;
1723	sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1724	if (sscanf(mountfrom, patt, vfsname, path) < 1)
1725		goto out;
1726
1727	if (path[0] == '\0')
1728		strcpy(path, ROOTNAME);
1729
1730	error = kernel_vmount(
1731	    MNT_RDONLY | MNT_ROOTFS,
1732	    "fstype", vfsname,
1733	    "fspath", "/",
1734	    "from", path,
1735	    NULL);
1736	if (error == 0) {
1737		/*
1738		 * We mount devfs prior to mounting the / FS, so the first
1739		 * entry will typically be devfs.
1740		 */
1741		mp = TAILQ_FIRST(&mountlist);
1742		KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1743
1744		/*
1745		 * Iterate over all currently mounted file systems and use
1746		 * the time stamp found to check and/or initialize the RTC.
1747		 * Typically devfs has no time stamp and the only other FS
1748		 * is the actual / FS.
1749		 * Call inittodr() only once and pass it the largest of the
1750		 * timestamps we encounter.
1751		 */
1752		timebase = 0;
1753		do {
1754			if (mp->mnt_time > timebase)
1755				timebase = mp->mnt_time;
1756			mp = TAILQ_NEXT(mp, mnt_list);
1757		} while (mp != NULL);
1758		inittodr(timebase);
1759
1760		devfs_fixup(curthread);
1761	}
1762out:
1763	free(path, M_MOUNT);
1764	free(vfsname, M_MOUNT);
1765	return (error);
1766}
1767
1768/*
1769 * ---------------------------------------------------------------------
1770 * Interactive root filesystem selection code.
1771 */
1772
1773static int
1774vfs_mountroot_ask(void)
1775{
1776	char name[128];
1777
1778	for(;;) {
1779		printf("\nManual root filesystem specification:\n");
1780		printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
1781#if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
1782		printf("                       eg. ufs:da0s1a\n");
1783#else
1784		printf("                       eg. ufs:/dev/da0a\n");
1785#endif
1786		printf("  ?                  List valid disk boot devices\n");
1787		printf("  <empty line>       Abort manual input\n");
1788		printf("\nmountroot> ");
1789		gets(name, sizeof(name), 1);
1790		if (name[0] == '\0')
1791			return (1);
1792		if (name[0] == '?') {
1793			printf("\nList of GEOM managed disk devices:\n  ");
1794			g_dev_print();
1795			continue;
1796		}
1797		if (!vfs_mountroot_try(name))
1798			return (0);
1799	}
1800}
1801
1802/*
1803 * ---------------------------------------------------------------------
1804 * Functions for querying mount options/arguments from filesystems.
1805 */
1806
1807/*
1808 * Check that no unknown options are given
1809 */
1810int
1811vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1812{
1813	struct vfsopt *opt;
1814	char errmsg[255];
1815	const char **t, *p, *q;
1816	int ret = 0;
1817
1818	TAILQ_FOREACH(opt, opts, link) {
1819		p = opt->name;
1820		q = NULL;
1821		if (p[0] == 'n' && p[1] == 'o')
1822			q = p + 2;
1823		for(t = global_opts; *t != NULL; t++) {
1824			if (strcmp(*t, p) == 0)
1825				break;
1826			if (q != NULL) {
1827				if (strcmp(*t, q) == 0)
1828					break;
1829			}
1830		}
1831		if (*t != NULL)
1832			continue;
1833		for(t = legal; *t != NULL; t++) {
1834			if (strcmp(*t, p) == 0)
1835				break;
1836			if (q != NULL) {
1837				if (strcmp(*t, q) == 0)
1838					break;
1839			}
1840		}
1841		if (*t != NULL)
1842			continue;
1843		sprintf(errmsg, "mount option <%s> is unknown", p);
1844		printf("%s\n", errmsg);
1845		ret = EINVAL;
1846	}
1847	if (ret != 0) {
1848		TAILQ_FOREACH(opt, opts, link) {
1849			if (strcmp(opt->name, "errmsg") == 0) {
1850				strncpy((char *)opt->value, errmsg, opt->len);
1851			}
1852		}
1853	}
1854	return (ret);
1855}
1856
1857/*
1858 * Get a mount option by its name.
1859 *
1860 * Return 0 if the option was found, ENOENT otherwise.
1861 * If len is non-NULL it will be filled with the length
1862 * of the option. If buf is non-NULL, it will be filled
1863 * with the address of the option.
1864 */
1865int
1866vfs_getopt(opts, name, buf, len)
1867	struct vfsoptlist *opts;
1868	const char *name;
1869	void **buf;
1870	int *len;
1871{
1872	struct vfsopt *opt;
1873
1874	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1875
1876	TAILQ_FOREACH(opt, opts, link) {
1877		if (strcmp(name, opt->name) == 0) {
1878			if (len != NULL)
1879				*len = opt->len;
1880			if (buf != NULL)
1881				*buf = opt->value;
1882			return (0);
1883		}
1884	}
1885	return (ENOENT);
1886}
1887
1888static int
1889vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1890{
1891	struct vfsopt *opt;
1892	int i;
1893
1894	if (opts == NULL)
1895		return (-1);
1896
1897	i = 0;
1898	TAILQ_FOREACH(opt, opts, link) {
1899		if (strcmp(name, opt->name) == 0)
1900			return (i);
1901		++i;
1902	}
1903	return (-1);
1904}
1905
1906char *
1907vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1908{
1909	struct vfsopt *opt;
1910
1911	*error = 0;
1912	TAILQ_FOREACH(opt, opts, link) {
1913		if (strcmp(name, opt->name) != 0)
1914			continue;
1915		if (((char *)opt->value)[opt->len - 1] != '\0') {
1916			*error = EINVAL;
1917			return (NULL);
1918		}
1919		return (opt->value);
1920	}
1921	*error = ENOENT;
1922	return (NULL);
1923}
1924
1925int
1926vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1927{
1928	struct vfsopt *opt;
1929
1930	TAILQ_FOREACH(opt, opts, link) {
1931		if (strcmp(name, opt->name) == 0) {
1932			if (w != NULL)
1933				*w |= val;
1934			return (1);
1935		}
1936	}
1937	if (w != NULL)
1938		*w &= ~val;
1939	return (0);
1940}
1941
1942int
1943vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1944{
1945	va_list ap;
1946	struct vfsopt *opt;
1947	int ret;
1948
1949	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1950
1951	TAILQ_FOREACH(opt, opts, link) {
1952		if (strcmp(name, opt->name) != 0)
1953			continue;
1954		if (opt->len == 0 || opt->value == NULL)
1955			return (0);
1956		if (((char *)opt->value)[opt->len - 1] != '\0')
1957			return (0);
1958		va_start(ap, fmt);
1959		ret = vsscanf(opt->value, fmt, ap);
1960		va_end(ap);
1961		return (ret);
1962	}
1963	return (0);
1964}
1965
1966/*
1967 * Find and copy a mount option.
1968 *
1969 * The size of the buffer has to be specified
1970 * in len, if it is not the same length as the
1971 * mount option, EINVAL is returned.
1972 * Returns ENOENT if the option is not found.
1973 */
1974int
1975vfs_copyopt(opts, name, dest, len)
1976	struct vfsoptlist *opts;
1977	const char *name;
1978	void *dest;
1979	int len;
1980{
1981	struct vfsopt *opt;
1982
1983	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1984
1985	TAILQ_FOREACH(opt, opts, link) {
1986		if (strcmp(name, opt->name) == 0) {
1987			if (len != opt->len)
1988				return (EINVAL);
1989			bcopy(opt->value, dest, opt->len);
1990			return (0);
1991		}
1992	}
1993	return (ENOENT);
1994}
1995
1996/*
1997 * This is a helper function for filesystems to traverse their
1998 * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
1999 */
2000
2001struct vnode *
2002__mnt_vnode_next(struct vnode **mvp, struct mount *mp)
2003{
2004	struct vnode *vp;
2005
2006	mtx_assert(MNT_MTX(mp), MA_OWNED);
2007
2008	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2009	if ((*mvp)->v_yield++ == 500) {
2010		MNT_IUNLOCK(mp);
2011		(*mvp)->v_yield = 0;
2012		uio_yield();
2013		MNT_ILOCK(mp);
2014	}
2015	vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
2016	while (vp != NULL && vp->v_type == VMARKER)
2017		vp = TAILQ_NEXT(vp, v_nmntvnodes);
2018
2019	/* Check if we are done */
2020	if (vp == NULL) {
2021		__mnt_vnode_markerfree(mvp, mp);
2022		return (NULL);
2023	}
2024	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2025	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2026	return (vp);
2027}
2028
2029struct vnode *
2030__mnt_vnode_first(struct vnode **mvp, struct mount *mp)
2031{
2032	struct vnode *vp;
2033
2034	mtx_assert(MNT_MTX(mp), MA_OWNED);
2035
2036	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2037	while (vp != NULL && vp->v_type == VMARKER)
2038		vp = TAILQ_NEXT(vp, v_nmntvnodes);
2039
2040	/* Check if we are done */
2041	if (vp == NULL) {
2042		*mvp = NULL;
2043		return (NULL);
2044	}
2045	mp->mnt_holdcnt++;
2046	MNT_IUNLOCK(mp);
2047	*mvp = (struct vnode *) malloc(sizeof(struct vnode),
2048				       M_VNODE_MARKER,
2049				       M_WAITOK | M_ZERO);
2050	MNT_ILOCK(mp);
2051	(*mvp)->v_type = VMARKER;
2052
2053	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2054	while (vp != NULL && vp->v_type == VMARKER)
2055		vp = TAILQ_NEXT(vp, v_nmntvnodes);
2056
2057	/* Check if we are done */
2058	if (vp == NULL) {
2059		MNT_IUNLOCK(mp);
2060		free(*mvp, M_VNODE_MARKER);
2061		MNT_ILOCK(mp);
2062		*mvp = NULL;
2063		mp->mnt_holdcnt--;
2064		if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
2065			wakeup(&mp->mnt_holdcnt);
2066		return (NULL);
2067	}
2068	mp->mnt_markercnt++;
2069	(*mvp)->v_mount = mp;
2070	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2071	return (vp);
2072}
2073
2074
2075void
2076__mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
2077{
2078
2079	if (*mvp == NULL)
2080		return;
2081
2082	mtx_assert(MNT_MTX(mp), MA_OWNED);
2083
2084	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2085	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2086	MNT_IUNLOCK(mp);
2087	free(*mvp, M_VNODE_MARKER);
2088	MNT_ILOCK(mp);
2089	*mvp = NULL;
2090
2091	mp->mnt_markercnt--;
2092	mp->mnt_holdcnt--;
2093	if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
2094		wakeup(&mp->mnt_holdcnt);
2095}
2096
2097
2098int
2099__vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
2100{
2101	int error;
2102
2103	error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
2104	if (sbp != &mp->mnt_stat)
2105		*sbp = mp->mnt_stat;
2106	return (error);
2107}
2108
2109void
2110vfs_mountedfrom(struct mount *mp, const char *from)
2111{
2112
2113	bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2114	strlcpy(mp->mnt_stat.f_mntfromname, from,
2115	    sizeof mp->mnt_stat.f_mntfromname);
2116}
2117
2118/*
2119 * ---------------------------------------------------------------------
2120 * This is the api for building mount args and mounting filesystems from
2121 * inside the kernel.
2122 *
2123 * The API works by accumulation of individual args.  First error is
2124 * latched.
2125 *
2126 * XXX: should be documented in new manpage kernel_mount(9)
2127 */
2128
2129/* A memory allocation which must be freed when we are done */
2130struct mntaarg {
2131	SLIST_ENTRY(mntaarg)	next;
2132};
2133
2134/* The header for the mount arguments */
2135struct mntarg {
2136	struct iovec *v;
2137	int len;
2138	int error;
2139	SLIST_HEAD(, mntaarg)	list;
2140};
2141
2142/*
2143 * Add a boolean argument.
2144 *
2145 * flag is the boolean value.
2146 * name must start with "no".
2147 */
2148struct mntarg *
2149mount_argb(struct mntarg *ma, int flag, const char *name)
2150{
2151
2152	KASSERT(name[0] == 'n' && name[1] == 'o',
2153	    ("mount_argb(...,%s): name must start with 'no'", name));
2154
2155	return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2156}
2157
2158/*
2159 * Add an argument printf style
2160 */
2161struct mntarg *
2162mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2163{
2164	va_list ap;
2165	struct mntaarg *maa;
2166	struct sbuf *sb;
2167	int len;
2168
2169	if (ma == NULL) {
2170		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2171		SLIST_INIT(&ma->list);
2172	}
2173	if (ma->error)
2174		return (ma);
2175
2176	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2177	    M_MOUNT, M_WAITOK);
2178	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2179	ma->v[ma->len].iov_len = strlen(name) + 1;
2180	ma->len++;
2181
2182	sb = sbuf_new_auto();
2183	va_start(ap, fmt);
2184	sbuf_vprintf(sb, fmt, ap);
2185	va_end(ap);
2186	sbuf_finish(sb);
2187	len = sbuf_len(sb) + 1;
2188	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2189	SLIST_INSERT_HEAD(&ma->list, maa, next);
2190	bcopy(sbuf_data(sb), maa + 1, len);
2191	sbuf_delete(sb);
2192
2193	ma->v[ma->len].iov_base = maa + 1;
2194	ma->v[ma->len].iov_len = len;
2195	ma->len++;
2196
2197	return (ma);
2198}
2199
2200/*
2201 * Add an argument which is a userland string.
2202 */
2203struct mntarg *
2204mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2205{
2206	struct mntaarg *maa;
2207	char *tbuf;
2208
2209	if (val == NULL)
2210		return (ma);
2211	if (ma == NULL) {
2212		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2213		SLIST_INIT(&ma->list);
2214	}
2215	if (ma->error)
2216		return (ma);
2217	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2218	SLIST_INSERT_HEAD(&ma->list, maa, next);
2219	tbuf = (void *)(maa + 1);
2220	ma->error = copyinstr(val, tbuf, len, NULL);
2221	return (mount_arg(ma, name, tbuf, -1));
2222}
2223
2224/*
2225 * Plain argument.
2226 *
2227 * If length is -1, treat value as a C string.
2228 */
2229struct mntarg *
2230mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2231{
2232
2233	if (ma == NULL) {
2234		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2235		SLIST_INIT(&ma->list);
2236	}
2237	if (ma->error)
2238		return (ma);
2239
2240	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2241	    M_MOUNT, M_WAITOK);
2242	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2243	ma->v[ma->len].iov_len = strlen(name) + 1;
2244	ma->len++;
2245
2246	ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2247	if (len < 0)
2248		ma->v[ma->len].iov_len = strlen(val) + 1;
2249	else
2250		ma->v[ma->len].iov_len = len;
2251	ma->len++;
2252	return (ma);
2253}
2254
2255/*
2256 * Free a mntarg structure
2257 */
2258static void
2259free_mntarg(struct mntarg *ma)
2260{
2261	struct mntaarg *maa;
2262
2263	while (!SLIST_EMPTY(&ma->list)) {
2264		maa = SLIST_FIRST(&ma->list);
2265		SLIST_REMOVE_HEAD(&ma->list, next);
2266		free(maa, M_MOUNT);
2267	}
2268	free(ma->v, M_MOUNT);
2269	free(ma, M_MOUNT);
2270}
2271
2272/*
2273 * Mount a filesystem
2274 */
2275int
2276kernel_mount(struct mntarg *ma, int flags)
2277{
2278	struct uio auio;
2279	int error;
2280
2281	KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2282	KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2283	KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2284
2285	auio.uio_iov = ma->v;
2286	auio.uio_iovcnt = ma->len;
2287	auio.uio_segflg = UIO_SYSSPACE;
2288
2289	error = ma->error;
2290	if (!error)
2291		error = vfs_donmount(curthread, flags, &auio);
2292	free_mntarg(ma);
2293	return (error);
2294}
2295
2296/*
2297 * A printflike function to mount a filesystem.
2298 */
2299int
2300kernel_vmount(int flags, ...)
2301{
2302	struct mntarg *ma = NULL;
2303	va_list ap;
2304	const char *cp;
2305	const void *vp;
2306	int error;
2307
2308	va_start(ap, flags);
2309	for (;;) {
2310		cp = va_arg(ap, const char *);
2311		if (cp == NULL)
2312			break;
2313		vp = va_arg(ap, const void *);
2314		ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2315	}
2316	va_end(ap);
2317
2318	error = kernel_mount(ma, flags);
2319	return (error);
2320}
2321