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