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