vfs_mount.c revision 127058
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * Copyright (c) 1999 Michael Smith
39 * All rights reserved.
40 * Copyright (c) 1999 Poul-Henning Kamp
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65#include <sys/cdefs.h>
66__FBSDID("$FreeBSD: head/sys/kern/vfs_mount.c 127058 2004-03-16 08:59:37Z tjr $");
67
68#include <sys/param.h>
69#include <sys/conf.h>
70#include <sys/cons.h>
71#include <sys/jail.h>
72#include <sys/kernel.h>
73#include <sys/linker.h>
74#include <sys/mac.h>
75#include <sys/malloc.h>
76#include <sys/mount.h>
77#include <sys/mutex.h>
78#include <sys/namei.h>
79#include <sys/proc.h>
80#include <sys/filedesc.h>
81#include <sys/reboot.h>
82#include <sys/sysproto.h>
83#include <sys/sx.h>
84#include <sys/sysctl.h>
85#include <sys/sysent.h>
86#include <sys/systm.h>
87#include <sys/vnode.h>
88
89#include <geom/geom.h>
90
91#include <machine/stdarg.h>
92
93#include "opt_rootdevname.h"
94#include "opt_ddb.h"
95#include "opt_mac.h"
96
97#ifdef DDB
98#include <ddb/ddb.h>
99#endif
100
101#define	ROOTNAME		"root_device"
102#define	VFS_MOUNTARG_SIZE_MAX	(1024 * 64)
103
104static void	checkdirs(struct vnode *olddp, struct vnode *newdp);
105static void	gets(char *cp);
106static int	vfs_domount(struct thread *td, const char *fstype,
107		    char *fspath, int fsflags, void *fsdata, int compat);
108static int	vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp,
109		    const char *fspath, struct thread *td, struct mount **mpp);
110static int	vfs_mountroot_ask(void);
111static int	vfs_mountroot_try(char *mountfrom);
112
113static int	usermount = 0;	/* if 1, non-root can mount fs. */
114SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
115
116MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
117
118/* List of mounted filesystems. */
119struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
120
121/* For any iteration/modification of mountlist */
122struct mtx mountlist_mtx;
123
124/*
125 * The vnode of the system's root (/ in the filesystem, without chroot
126 * active.)
127 */
128struct vnode	*rootvnode;
129
130/*
131 * The root filesystem is detailed in the kernel environment variable
132 * vfs.root.mountfrom, which is expected to be in the general format
133 *
134 * <vfsname>:[<path>]
135 * vfsname   := the name of a VFS known to the kernel and capable
136 *              of being mounted as root
137 * path      := disk device name or other data used by the filesystem
138 *              to locate its physical store
139 */
140
141/*
142 * The root specifiers we will try if RB_CDROM is specified.
143 */
144static char *cdrom_rootdevnames[] = {
145	"cd9660:cd0",
146	"cd9660:acd0",
147	NULL
148};
149
150/* legacy find-root code */
151char		*rootdevnames[2] = {NULL, NULL};
152static int	setrootbyname(char *name);
153dev_t		rootdev = NODEV;
154
155/*
156 * Has to be dynamic as the value of rootdev can change; however, it can't
157 * change after the root is mounted, so a user process can't access this
158 * sysctl until after the value is unchangeable.
159 */
160static int
161sysctl_rootdev(SYSCTL_HANDLER_ARGS)
162{
163	int error;
164
165	/* _RD prevents this from happening. */
166	KASSERT(req->newptr == NULL, ("Attempt to change root device name"));
167
168	if (rootdev != NODEV)
169		error = sysctl_handle_string(oidp, rootdev->si_name, 0, req);
170	else
171		error = sysctl_handle_string(oidp, "", 0, req);
172
173	return (error);
174}
175
176SYSCTL_PROC(_kern, OID_AUTO, rootdev, CTLTYPE_STRING | CTLFLAG_RD,
177    0, 0, sysctl_rootdev, "A", "Root file system device");
178
179/* Remove one mount option. */
180static void
181vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
182{
183
184	TAILQ_REMOVE(opts, opt, link);
185	free(opt->name, M_MOUNT);
186	if (opt->value != NULL)
187		free(opt->value, M_MOUNT);
188#ifdef INVARIANTS
189	else if (opt->len != 0)
190		panic("%s: mount option with NULL value but length != 0",
191		    __func__);
192#endif
193	free(opt, M_MOUNT);
194}
195
196/* Release all resources related to the mount options. */
197static void
198vfs_freeopts(struct vfsoptlist *opts)
199{
200	struct vfsopt *opt;
201
202	while (!TAILQ_EMPTY(opts)) {
203		opt = TAILQ_FIRST(opts);
204		vfs_freeopt(opts, opt);
205	}
206	free(opts, M_MOUNT);
207}
208
209/*
210 * If a mount option is specified several times,
211 * (with or without the "no" prefix) only keep
212 * the last occurence of it.
213 */
214static void
215vfs_sanitizeopts(struct vfsoptlist *opts)
216{
217	struct vfsopt *opt, *opt2, *tmp;
218	int noopt;
219
220	TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
221		if (strncmp(opt->name, "no", 2) == 0)
222			noopt = 1;
223		else
224			noopt = 0;
225		opt2 = TAILQ_PREV(opt, vfsoptlist, link);
226		while (opt2 != NULL) {
227			if (strcmp(opt2->name, opt->name) == 0 ||
228			    (noopt && strcmp(opt->name + 2, opt2->name) == 0) ||
229			    (!noopt && strncmp(opt2->name, "no", 2) == 0 &&
230			    strcmp(opt2->name + 2, opt->name) == 0)) {
231				tmp = TAILQ_PREV(opt2, vfsoptlist, link);
232				vfs_freeopt(opts, opt2);
233				opt2 = tmp;
234			} else {
235				opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
236			}
237		}
238	}
239}
240
241/*
242 * Build a linked list of mount options from a struct uio.
243 */
244static int
245vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
246{
247	struct vfsoptlist *opts;
248	struct vfsopt *opt;
249	size_t memused;
250	unsigned int i, iovcnt;
251	int error, namelen, optlen;
252
253	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
254	TAILQ_INIT(opts);
255	memused = 0;
256	iovcnt = auio->uio_iovcnt;
257	for (i = 0; i < iovcnt; i += 2) {
258		opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
259		namelen = auio->uio_iov[i].iov_len;
260		optlen = auio->uio_iov[i + 1].iov_len;
261		opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
262		opt->value = NULL;
263		opt->len = optlen;
264
265		/*
266		 * Do this early, so jumps to "bad" will free the current
267		 * option.
268		 */
269		TAILQ_INSERT_TAIL(opts, opt, link);
270		memused += sizeof(struct vfsopt) + optlen + namelen;
271
272		/*
273		 * Avoid consuming too much memory, and attempts to overflow
274		 * memused.
275		 */
276		if (memused > VFS_MOUNTARG_SIZE_MAX ||
277		    optlen > VFS_MOUNTARG_SIZE_MAX ||
278		    namelen > VFS_MOUNTARG_SIZE_MAX) {
279			error = EINVAL;
280			goto bad;
281		}
282
283		if (auio->uio_segflg == UIO_SYSSPACE) {
284			bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
285		} else {
286			error = copyin(auio->uio_iov[i].iov_base, opt->name,
287			    namelen);
288			if (error)
289				goto bad;
290		}
291		/* Ensure names are null-terminated strings. */
292		if (opt->name[namelen - 1] != '\0') {
293			error = EINVAL;
294			goto bad;
295		}
296		if (optlen != 0) {
297			opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
298			if (auio->uio_segflg == UIO_SYSSPACE) {
299				bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
300				    optlen);
301			} else {
302				error = copyin(auio->uio_iov[i + 1].iov_base,
303				    opt->value, optlen);
304				if (error)
305					goto bad;
306			}
307		}
308	}
309	vfs_sanitizeopts(opts);
310	*options = opts;
311	return (0);
312bad:
313	vfs_freeopts(opts);
314	return (error);
315}
316
317/*
318 * Merge the old mount options with the new ones passed
319 * in the MNT_UPDATE case.
320 */
321static void
322vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
323{
324	struct vfsopt *opt, *opt2, *new;
325
326	TAILQ_FOREACH(opt, opts, link) {
327		/*
328		 * Check that this option hasn't been redefined
329		 * nor cancelled with a "no" mount option.
330		 */
331		opt2 = TAILQ_FIRST(toopts);
332		while (opt2 != NULL) {
333			if (strcmp(opt2->name, opt->name) == 0)
334				goto next;
335			if (strncmp(opt2->name, "no", 2) == 0 &&
336			    strcmp(opt2->name + 2, opt->name) == 0) {
337				vfs_freeopt(toopts, opt2);
338				goto next;
339			}
340			opt2 = TAILQ_NEXT(opt2, link);
341		}
342		/* We want this option, duplicate it. */
343		new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
344		new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
345		strcpy(new->name, opt->name);
346		if (opt->len != 0) {
347			new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
348			bcopy(opt->value, new->value, opt->len);
349		} else {
350			new->value = NULL;
351		}
352		new->len = opt->len;
353		TAILQ_INSERT_TAIL(toopts, new, link);
354next:
355		continue;
356	}
357}
358
359/*
360 * New mount API.
361 */
362int
363nmount(td, uap)
364	struct thread *td;
365	struct nmount_args /* {
366		struct iovec *iovp;
367		unsigned int iovcnt;
368		int flags;
369	} */ *uap;
370{
371	struct uio auio;
372	struct iovec *iov, *needfree;
373	struct iovec aiov[UIO_SMALLIOV];
374	unsigned int i;
375	int error;
376	u_int iovlen, iovcnt;
377
378	iovcnt = uap->iovcnt;
379	iovlen = iovcnt * sizeof (struct iovec);
380	/*
381	 * Check that we have an even number of iovec's
382	 * and that we have at least two options.
383	 */
384	if ((iovcnt & 1) || (iovcnt < 4) || (iovcnt > UIO_MAXIOV))
385		return (EINVAL);
386
387	if (iovcnt > UIO_SMALLIOV) {
388		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
389		needfree = iov;
390	} else {
391		iov = aiov;
392		needfree = NULL;
393	}
394	auio.uio_iov = iov;
395	auio.uio_iovcnt = iovcnt;
396	auio.uio_segflg = UIO_USERSPACE;
397	if ((error = copyin(uap->iovp, iov, iovlen)))
398		goto finish;
399
400	for (i = 0; i < iovcnt; i++) {
401		if (iov->iov_len > MMAXOPTIONLEN) {
402			error = EINVAL;
403			goto finish;
404		}
405		iov++;
406	}
407	error = vfs_nmount(td, uap->flags, &auio);
408finish:
409	if (needfree != NULL)
410		free(needfree, M_TEMP);
411	return (error);
412}
413
414int
415kernel_mount(iovp, iovcnt, flags)
416	struct iovec *iovp;
417	unsigned int iovcnt;
418	int flags;
419{
420	struct uio auio;
421	int error;
422
423	/*
424	 * Check that we have an even number of iovec's
425	 * and that we have at least two options.
426	 */
427	if ((iovcnt & 1) || (iovcnt < 4))
428		return (EINVAL);
429
430	auio.uio_iov = iovp;
431	auio.uio_iovcnt = iovcnt;
432	auio.uio_segflg = UIO_SYSSPACE;
433
434	error = vfs_nmount(curthread, flags, &auio);
435	return (error);
436}
437
438int
439kernel_vmount(int flags, ...)
440{
441	struct iovec *iovp;
442	struct uio auio;
443	va_list ap;
444	unsigned int iovcnt, iovlen, len;
445	const char *cp;
446	char *buf, *pos;
447	size_t n;
448	int error, i;
449
450	len = 0;
451	va_start(ap, flags);
452	for (iovcnt = 0; (cp = va_arg(ap, const char *)) != NULL; iovcnt++)
453		len += strlen(cp) + 1;
454	va_end(ap);
455
456	if (iovcnt < 4 || iovcnt & 1)
457		return (EINVAL);
458
459	iovlen = iovcnt * sizeof (struct iovec);
460	MALLOC(iovp, struct iovec *, iovlen, M_MOUNT, M_WAITOK);
461	MALLOC(buf, char *, len, M_MOUNT, M_WAITOK);
462	pos = buf;
463	va_start(ap, flags);
464	for (i = 0; i < iovcnt; i++) {
465		cp = va_arg(ap, const char *);
466		copystr(cp, pos, len - (pos - buf), &n);
467		iovp[i].iov_base = pos;
468		iovp[i].iov_len = n;
469		pos += n;
470	}
471	va_end(ap);
472
473	auio.uio_iov = iovp;
474	auio.uio_iovcnt = iovcnt;
475	auio.uio_segflg = UIO_SYSSPACE;
476
477	error = vfs_nmount(curthread, flags, &auio);
478	FREE(iovp, M_MOUNT);
479	FREE(buf, M_MOUNT);
480	return (error);
481}
482
483/*
484 * Allocate and initialize the mount point struct.
485 */
486static int
487vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
488    const char *fspath, struct thread *td, struct mount **mpp)
489{
490	struct mount *mp;
491
492	mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO);
493	TAILQ_INIT(&mp->mnt_nvnodelist);
494	mp->mnt_nvnodelistsize = 0;
495	mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
496	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
497	vfs_busy(mp, LK_NOWAIT, 0, td);
498	mp->mnt_op = vfsp->vfc_vfsops;
499	mp->mnt_vfc = vfsp;
500	vfsp->vfc_refcount++;
501	mp->mnt_stat.f_type = vfsp->vfc_typenum;
502	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
503	strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
504	mp->mnt_vnodecovered = vp;
505	mp->mnt_cred = crdup(td->td_ucred);
506	mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
507	strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
508	mp->mnt_iosize_max = DFLTPHYS;
509#ifdef MAC
510	mac_init_mount(mp);
511	mac_create_mount(td->td_ucred, mp);
512#endif
513	*mpp = mp;
514	return (0);
515}
516
517/*
518 * Destroy the mount struct previously allocated by vfs_mount_alloc().
519 */
520void
521vfs_mount_destroy(struct mount *mp, struct thread *td)
522{
523
524	mp->mnt_vfc->vfc_refcount--;
525	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
526		panic("unmount: dangling vnode");
527	vfs_unbusy(mp,td);
528	lockdestroy(&mp->mnt_lock);
529	mtx_destroy(&mp->mnt_mtx);
530	if (mp->mnt_kern_flag & MNTK_MWAIT)
531		wakeup(mp);
532#ifdef MAC
533	mac_destroy_mount(mp);
534#endif
535	if (mp->mnt_opt != NULL)
536		vfs_freeopts(mp->mnt_opt);
537	crfree(mp->mnt_cred);
538	free(mp, M_MOUNT);
539}
540
541int
542vfs_nmount(struct thread *td, int fsflags, struct uio *fsoptions)
543{
544	struct vfsoptlist *optlist;
545	char *fstype, *fspath;
546	int error, fstypelen, fspathlen;
547
548	error = vfs_buildopts(fsoptions, &optlist);
549	if (error)
550		return (error);
551
552	/*
553	 * We need these two options before the others,
554	 * and they are mandatory for any filesystem.
555	 * Ensure they are NUL terminated as well.
556	 */
557	fstypelen = 0;
558	error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
559	if (error || fstype[fstypelen - 1] != '\0') {
560		error = EINVAL;
561		goto bail;
562	}
563	fspathlen = 0;
564	error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
565	if (error || fspath[fspathlen - 1] != '\0') {
566		error = EINVAL;
567		goto bail;
568	}
569
570	/*
571	 * Be ultra-paranoid about making sure the type and fspath
572	 * variables will fit in our mp buffers, including the
573	 * terminating NUL.
574	 */
575	if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
576		error = ENAMETOOLONG;
577		goto bail;
578	}
579
580	error = vfs_domount(td, fstype, fspath, fsflags, optlist, 0);
581bail:
582	if (error)
583		vfs_freeopts(optlist);
584	return (error);
585}
586
587/*
588 * Old mount API.
589 */
590#ifndef _SYS_SYSPROTO_H_
591struct mount_args {
592	char	*type;
593	char	*path;
594	int	flags;
595	caddr_t	data;
596};
597#endif
598/* ARGSUSED */
599int
600mount(td, uap)
601	struct thread *td;
602	struct mount_args /* {
603		char *type;
604		char *path;
605		int flags;
606		caddr_t data;
607	} */ *uap;
608{
609	char *fstype;
610	char *fspath;
611	int error;
612
613	fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
614	fspath = malloc(MNAMELEN, M_TEMP, M_WAITOK);
615
616	/*
617	 * vfs_mount() actually takes a kernel string for `type' and
618	 * `path' now, so extract them.
619	 */
620	error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
621	if (error == 0)
622		error = copyinstr(uap->path, fspath, MNAMELEN, NULL);
623	if (error == 0)
624		error = vfs_domount(td, fstype, fspath, uap->flags,
625		    uap->data, 1);
626	free(fstype, M_TEMP);
627	free(fspath, M_TEMP);
628	return (error);
629}
630
631/*
632 * vfs_mount(): actually attempt a filesystem mount.
633 *
634 * This routine is designed to be a "generic" entry point for routines
635 * that wish to mount a filesystem. All parameters except `fsdata' are
636 * pointers into kernel space. `fsdata' is currently still a pointer
637 * into userspace.
638 */
639int
640vfs_mount(td, fstype, fspath, fsflags, fsdata)
641	struct thread *td;
642	const char *fstype;
643	char *fspath;
644	int fsflags;
645	void *fsdata;
646{
647
648	return (vfs_domount(td, fstype, fspath, fsflags, fsdata, 1));
649}
650
651/*
652 * vfs_domount(): actually attempt a filesystem mount.
653 */
654static int
655vfs_domount(
656	struct thread *td,	/* Flags common to all filesystems. */
657	const char *fstype,	/* Filesystem type. */
658	char *fspath,		/* Mount path. */
659	int fsflags,		/* Flags common to all filesystems. */
660	void *fsdata,		/* Options local to the filesystem. */
661	int compat		/* Invocation from compat syscall. */
662	)
663{
664	linker_file_t lf;
665	struct vnode *vp;
666	struct mount *mp;
667	struct vfsconf *vfsp;
668	int error, flag = 0, kern_flag = 0;
669	struct vattr va;
670	struct nameidata nd;
671
672	/*
673	 * Be ultra-paranoid about making sure the type and fspath
674	 * variables will fit in our mp buffers, including the
675	 * terminating NUL.
676	 */
677	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
678		return (ENAMETOOLONG);
679
680	/* mount(2) is not permitted inside the jail. */
681	if (jailed(td->td_ucred))
682		return (EPERM);
683
684	if (usermount == 0) {
685		error = suser(td);
686		if (error)
687			return (error);
688	}
689	/*
690	 * Do not allow NFS export by non-root users.
691	 */
692	if (fsflags & MNT_EXPORTED) {
693		error = suser(td);
694		if (error)
695			return (error);
696	}
697	/*
698	 * Silently enforce MNT_NOSUID, MNT_NODEV and MNT_USER
699	 * for unprivileged users.
700	 */
701	if (suser(td))
702		fsflags |= MNT_NOSUID | MNT_NODEV | MNT_USER;
703	/*
704	 * Get vnode to be covered
705	 */
706	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
707	if ((error = namei(&nd)) != 0)
708		return (error);
709	NDFREE(&nd, NDF_ONLY_PNBUF);
710	vp = nd.ni_vp;
711	if (fsflags & MNT_UPDATE) {
712		if ((vp->v_vflag & VV_ROOT) == 0) {
713			vput(vp);
714			return (EINVAL);
715		}
716		mp = vp->v_mount;
717		flag = mp->mnt_flag;
718		kern_flag = mp->mnt_kern_flag;
719		/*
720		 * We only allow the filesystem to be reloaded if it
721		 * is currently mounted read-only.
722		 */
723		if ((fsflags & MNT_RELOAD) &&
724		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
725			vput(vp);
726			return (EOPNOTSUPP);	/* Needs translation */
727		}
728		/*
729		 * Only root, or the user that did the original mount is
730		 * permitted to update it.
731		 */
732		if ((mp->mnt_flag & MNT_USER) != 0) {
733			if (mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
734				if ((error = suser(td)) != 0) {
735					vput(vp);
736					return (error);
737				}
738			}
739		} else {
740			if ((error = suser(td)) != 0) {
741				vput(vp);
742				return (error);
743			}
744		}
745		if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
746			vput(vp);
747			return (EBUSY);
748		}
749		VI_LOCK(vp);
750		if ((vp->v_iflag & VI_MOUNT) != 0 ||
751		    vp->v_mountedhere != NULL) {
752			VI_UNLOCK(vp);
753			vfs_unbusy(mp, td);
754			vput(vp);
755			return (EBUSY);
756		}
757		vp->v_iflag |= VI_MOUNT;
758		VI_UNLOCK(vp);
759		mp->mnt_flag |= fsflags &
760		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT);
761		VOP_UNLOCK(vp, 0, td);
762		if (compat == 0) {
763			mp->mnt_optnew = fsdata;
764			vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
765		}
766		goto update;
767	}
768	/*
769	 * If the user is not root, ensure that they own the directory
770	 * onto which we are attempting to mount.
771	 */
772	error = VOP_GETATTR(vp, &va, td->td_ucred, td);
773	if (error) {
774		vput(vp);
775		return (error);
776	}
777	if (va.va_uid != td->td_ucred->cr_uid) {
778		error = suser(td);
779		if (error) {
780			vput(vp);
781			return (error);
782		}
783	}
784	if ((error = vinvalbuf(vp, V_SAVE, td->td_ucred, td, 0, 0)) != 0) {
785		vput(vp);
786		return (error);
787	}
788	if (vp->v_type != VDIR) {
789		vput(vp);
790		return (ENOTDIR);
791	}
792	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
793		if (!strcmp(vfsp->vfc_name, fstype))
794			break;
795	if (vfsp == NULL) {
796		/* Only load modules for root (very important!). */
797		error = suser(td);
798		if (error) {
799			vput(vp);
800			return (error);
801		}
802		error = securelevel_gt(td->td_ucred, 0);
803		if (error) {
804			vput(vp);
805			return (error);
806		}
807		error = linker_load_module(NULL, fstype, NULL, NULL, &lf);
808		if (error || lf == NULL) {
809			vput(vp);
810			if (lf == NULL)
811				error = ENODEV;
812			return (error);
813		}
814		lf->userrefs++;
815		/* Look up again to see if the VFS was loaded. */
816		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
817			if (!strcmp(vfsp->vfc_name, fstype))
818				break;
819		if (vfsp == NULL) {
820			lf->userrefs--;
821			linker_file_unload(lf);
822			vput(vp);
823			return (ENODEV);
824		}
825	}
826	VI_LOCK(vp);
827	if ((vp->v_iflag & VI_MOUNT) != 0 ||
828	    vp->v_mountedhere != NULL) {
829		VI_UNLOCK(vp);
830		vput(vp);
831		return (EBUSY);
832	}
833	vp->v_iflag |= VI_MOUNT;
834	VI_UNLOCK(vp);
835
836	/*
837	 * Allocate and initialize the filesystem.
838	 */
839	error = vfs_mount_alloc(vp, vfsp, fspath, td, &mp);
840	if (error) {
841		vput(vp);
842		return (error);
843	}
844	VOP_UNLOCK(vp, 0, td);
845
846	/* XXXMAC: pass to vfs_mount_alloc? */
847	if (compat == 0)
848		mp->mnt_optnew = fsdata;
849update:
850	/*
851	 * Check if the fs implements the type VFS_[N]MOUNT()
852	 * function we are looking for.
853	 */
854	if ((compat == 0) == (mp->mnt_op->vfs_mount != NULL)) {
855		printf("%s doesn't support the %s mount syscall\n",
856		    mp->mnt_vfc->vfc_name, compat? "old" : "new");
857		VI_LOCK(vp);
858		vp->v_iflag &= ~VI_MOUNT;
859		VI_UNLOCK(vp);
860		if (mp->mnt_flag & MNT_UPDATE)
861			vfs_unbusy(mp, td);
862		else
863			vfs_mount_destroy(mp, td);
864		vrele(vp);
865		return (EOPNOTSUPP);
866	}
867
868	/*
869	 * Set the mount level flags.
870	 */
871	if (fsflags & MNT_RDONLY)
872		mp->mnt_flag |= MNT_RDONLY;
873	else if (mp->mnt_flag & MNT_RDONLY)
874		mp->mnt_kern_flag |= MNTK_WANTRDWR;
875	mp->mnt_flag &=~ MNT_UPDATEMASK;
876	mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE);
877	/*
878	 * Mount the filesystem.
879	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
880	 * get.  No freeing of cn_pnbuf.
881	 */
882	error = compat? VFS_MOUNT(mp, fspath, fsdata, &nd, td) :
883	    VFS_NMOUNT(mp, &nd, td);
884	if (!error) {
885		if (mp->mnt_opt != NULL)
886			vfs_freeopts(mp->mnt_opt);
887		mp->mnt_opt = mp->mnt_optnew;
888	}
889	/*
890	 * Prevent external consumers of mount options from reading
891	 * mnt_optnew.
892	*/
893	mp->mnt_optnew = NULL;
894	if (mp->mnt_flag & MNT_UPDATE) {
895		if (mp->mnt_kern_flag & MNTK_WANTRDWR)
896			mp->mnt_flag &= ~MNT_RDONLY;
897		mp->mnt_flag &=~
898		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
899		mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
900		if (error) {
901			mp->mnt_flag = flag;
902			mp->mnt_kern_flag = kern_flag;
903		}
904		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
905			if (mp->mnt_syncer == NULL)
906				error = vfs_allocate_syncvnode(mp);
907		} else {
908			if (mp->mnt_syncer != NULL)
909				vrele(mp->mnt_syncer);
910			mp->mnt_syncer = NULL;
911		}
912		vfs_unbusy(mp, td);
913		VI_LOCK(vp);
914		vp->v_iflag &= ~VI_MOUNT;
915		VI_UNLOCK(vp);
916		vrele(vp);
917		return (error);
918	}
919	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
920	/*
921	 * Put the new filesystem on the mount list after root.
922	 */
923	cache_purge(vp);
924	if (!error) {
925		struct vnode *newdp;
926
927		VI_LOCK(vp);
928		vp->v_iflag &= ~VI_MOUNT;
929		VI_UNLOCK(vp);
930		vp->v_mountedhere = mp;
931		mtx_lock(&mountlist_mtx);
932		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
933		mtx_unlock(&mountlist_mtx);
934		if (VFS_ROOT(mp, &newdp))
935			panic("mount: lost mount");
936		checkdirs(vp, newdp);
937		vput(newdp);
938		VOP_UNLOCK(vp, 0, td);
939		if ((mp->mnt_flag & MNT_RDONLY) == 0)
940			error = vfs_allocate_syncvnode(mp);
941		vfs_unbusy(mp, td);
942		if (error || (error = VFS_START(mp, 0, td)) != 0)
943			vrele(vp);
944	} else {
945		VI_LOCK(vp);
946		vp->v_iflag &= ~VI_MOUNT;
947		VI_UNLOCK(vp);
948		vfs_mount_destroy(mp, td);
949		vput(vp);
950	}
951	return (error);
952}
953
954/*
955 * Scan all active processes to see if any of them have a current
956 * or root directory of `olddp'. If so, replace them with the new
957 * mount point.
958 */
959static void
960checkdirs(olddp, newdp)
961	struct vnode *olddp, *newdp;
962{
963	struct filedesc *fdp;
964	struct proc *p;
965	int nrele;
966
967	if (vrefcnt(olddp) == 1)
968		return;
969	sx_slock(&allproc_lock);
970	LIST_FOREACH(p, &allproc, p_list) {
971		mtx_lock(&fdesc_mtx);
972		fdp = p->p_fd;
973		if (fdp == NULL) {
974			mtx_unlock(&fdesc_mtx);
975			continue;
976		}
977		nrele = 0;
978		FILEDESC_LOCK(fdp);
979		if (fdp->fd_cdir == olddp) {
980			VREF(newdp);
981			fdp->fd_cdir = newdp;
982			nrele++;
983		}
984		if (fdp->fd_rdir == olddp) {
985			VREF(newdp);
986			fdp->fd_rdir = newdp;
987			nrele++;
988		}
989		FILEDESC_UNLOCK(fdp);
990		mtx_unlock(&fdesc_mtx);
991		while (nrele--)
992			vrele(olddp);
993	}
994	sx_sunlock(&allproc_lock);
995	if (rootvnode == olddp) {
996		vrele(rootvnode);
997		VREF(newdp);
998		rootvnode = newdp;
999	}
1000}
1001
1002/*
1003 * Unmount a filesystem.
1004 *
1005 * Note: unmount takes a path to the vnode mounted on as argument,
1006 * not special file (as before).
1007 */
1008#ifndef _SYS_SYSPROTO_H_
1009struct unmount_args {
1010	char	*path;
1011	int	flags;
1012};
1013#endif
1014/* ARGSUSED */
1015int
1016unmount(td, uap)
1017	struct thread *td;
1018	register struct unmount_args /* {
1019		char *path;
1020		int flags;
1021	} */ *uap;
1022{
1023	struct mount *mp;
1024	char *pathbuf;
1025	int error, id0, id1;
1026
1027	/* unmount(2) is not permitted inside the jail. */
1028	if (jailed(td->td_ucred))
1029		return (EPERM);
1030
1031	if (usermount == 0) {
1032		if ((error = suser(td)) != 0)
1033			return (error);
1034	}
1035
1036	pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1037	error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1038	if (error) {
1039		free(pathbuf, M_TEMP);
1040		return (error);
1041	}
1042	if (uap->flags & MNT_BYFSID) {
1043		/* Decode the filesystem ID. */
1044		if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1045			free(pathbuf, M_TEMP);
1046			return (EINVAL);
1047		}
1048
1049		mtx_lock(&mountlist_mtx);
1050		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list)
1051			if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1052			    mp->mnt_stat.f_fsid.val[1] == id1)
1053				break;
1054		mtx_unlock(&mountlist_mtx);
1055	} else {
1056		mtx_lock(&mountlist_mtx);
1057		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list)
1058			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1059				break;
1060		mtx_unlock(&mountlist_mtx);
1061	}
1062	free(pathbuf, M_TEMP);
1063	if (mp == NULL) {
1064		/*
1065		 * Previously we returned ENOENT for a nonexistent path and
1066		 * EINVAL for a non-mountpoint.  We cannot tell these apart
1067		 * now, so in the !MNT_BYFSID case return the more likely
1068		 * EINVAL for compatibility.
1069		 */
1070		return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1071	}
1072
1073	/*
1074	 * Only root, or the user that did the original mount is
1075	 * permitted to unmount this filesystem.
1076	 */
1077	if ((mp->mnt_flag & MNT_USER) != 0) {
1078		if (mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
1079			if ((error = suser(td)) != 0)
1080				return (error);
1081		}
1082	} else {
1083		if ((error = suser(td)) != 0)
1084			return (error);
1085	}
1086
1087	/*
1088	 * Don't allow unmounting the root filesystem.
1089	 */
1090	if (mp->mnt_flag & MNT_ROOTFS)
1091		return (EINVAL);
1092	return (dounmount(mp, uap->flags, td));
1093}
1094
1095/*
1096 * Do the actual filesystem unmount.
1097 */
1098int
1099dounmount(mp, flags, td)
1100	struct mount *mp;
1101	int flags;
1102	struct thread *td;
1103{
1104	struct vnode *coveredvp, *fsrootvp;
1105	int error;
1106	int async_flag;
1107
1108	mtx_lock(&mountlist_mtx);
1109	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1110		mtx_unlock(&mountlist_mtx);
1111		return (EBUSY);
1112	}
1113	mp->mnt_kern_flag |= MNTK_UNMOUNT;
1114	/* Allow filesystems to detect that a forced unmount is in progress. */
1115	if (flags & MNT_FORCE)
1116		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1117	error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1118	    ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_mtx, td);
1119	if (error) {
1120		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1121		if (mp->mnt_kern_flag & MNTK_MWAIT)
1122			wakeup(mp);
1123		return (error);
1124	}
1125	vn_start_write(NULL, &mp, V_WAIT);
1126
1127	if (mp->mnt_flag & MNT_EXPUBLIC)
1128		vfs_setpublicfs(NULL, NULL, NULL);
1129
1130	vfs_msync(mp, MNT_WAIT);
1131	async_flag = mp->mnt_flag & MNT_ASYNC;
1132	mp->mnt_flag &=~ MNT_ASYNC;
1133	cache_purgevfs(mp);	/* remove cache entries for this file sys */
1134	if (mp->mnt_syncer != NULL)
1135		vrele(mp->mnt_syncer);
1136	/*
1137	 * For forced unmounts, move process cdir/rdir refs on the fs root
1138	 * vnode to the covered vnode.  For non-forced unmounts we want
1139	 * such references to cause an EBUSY error.
1140	 */
1141	if ((flags & MNT_FORCE) && VFS_ROOT(mp, &fsrootvp) == 0) {
1142		if (mp->mnt_vnodecovered != NULL)
1143			checkdirs(fsrootvp, mp->mnt_vnodecovered);
1144		if (fsrootvp == rootvnode) {
1145			vrele(rootvnode);
1146			rootvnode = NULL;
1147		}
1148		vput(fsrootvp);
1149	}
1150	if (((mp->mnt_flag & MNT_RDONLY) ||
1151	     (error = VFS_SYNC(mp, MNT_WAIT, td->td_ucred, td)) == 0) ||
1152	    (flags & MNT_FORCE)) {
1153		error = VFS_UNMOUNT(mp, flags, td);
1154	}
1155	vn_finished_write(mp);
1156	if (error) {
1157		/* Undo cdir/rdir and rootvnode changes made above. */
1158		if ((flags & MNT_FORCE) && VFS_ROOT(mp, &fsrootvp) == 0) {
1159			if (mp->mnt_vnodecovered != NULL)
1160				checkdirs(mp->mnt_vnodecovered, fsrootvp);
1161			if (rootvnode == NULL) {
1162				rootvnode = fsrootvp;
1163				vref(rootvnode);
1164			}
1165			vput(fsrootvp);
1166		}
1167		if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
1168			(void) vfs_allocate_syncvnode(mp);
1169		mtx_lock(&mountlist_mtx);
1170		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1171		mp->mnt_flag |= async_flag;
1172		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK,
1173		    &mountlist_mtx, td);
1174		if (mp->mnt_kern_flag & MNTK_MWAIT)
1175			wakeup(mp);
1176		return (error);
1177	}
1178	mtx_lock(&mountlist_mtx);
1179	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1180	if ((coveredvp = mp->mnt_vnodecovered) != NULL)
1181		coveredvp->v_mountedhere = NULL;
1182	mtx_unlock(&mountlist_mtx);
1183	vfs_mount_destroy(mp, td);
1184	if (coveredvp != NULL)
1185		vrele(coveredvp);
1186	return (0);
1187}
1188
1189/*
1190 * Lookup a filesystem type, and if found allocate and initialize
1191 * a mount structure for it.
1192 *
1193 * Devname is usually updated by mount(8) after booting.
1194 */
1195int
1196vfs_rootmountalloc(fstypename, devname, mpp)
1197	char *fstypename;
1198	char *devname;
1199	struct mount **mpp;
1200{
1201	struct thread *td = curthread;	/* XXX */
1202	struct vfsconf *vfsp;
1203	struct mount *mp;
1204	int error;
1205
1206	if (fstypename == NULL)
1207		return (ENODEV);
1208	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1209		if (!strcmp(vfsp->vfc_name, fstypename))
1210			break;
1211	if (vfsp == NULL)
1212		return (ENODEV);
1213	error = vfs_mount_alloc(NULLVP, vfsp, "/", td, &mp);
1214	if (error)
1215		return (error);
1216	mp->mnt_flag |= MNT_RDONLY | MNT_ROOTFS;
1217	strlcpy(mp->mnt_stat.f_mntfromname, devname, MNAMELEN);
1218	*mpp = mp;
1219	return (0);
1220}
1221
1222/*
1223 * Find and mount the root filesystem
1224 */
1225void
1226vfs_mountroot(void)
1227{
1228	char *cp;
1229	int error, i;
1230
1231	g_waitidle();
1232
1233	/*
1234	 * The root filesystem information is compiled in, and we are
1235	 * booted with instructions to use it.
1236	 */
1237#ifdef ROOTDEVNAME
1238	if ((boothowto & RB_DFLTROOT) && !vfs_mountroot_try(ROOTDEVNAME))
1239		return;
1240#endif
1241	/*
1242	 * We are booted with instructions to prompt for the root filesystem,
1243	 * or to use the compiled-in default when it doesn't exist.
1244	 */
1245	if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
1246		if (!vfs_mountroot_ask())
1247			return;
1248	}
1249
1250	/*
1251	 * We've been given the generic "use CDROM as root" flag.  This is
1252	 * necessary because one media may be used in many different
1253	 * devices, so we need to search for them.
1254	 */
1255	if (boothowto & RB_CDROM) {
1256		for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1257			if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1258				return;
1259		}
1260	}
1261
1262	/*
1263	 * Try to use the value read by the loader from /etc/fstab, or
1264	 * supplied via some other means.  This is the preferred
1265	 * mechanism.
1266	 */
1267	if ((cp = getenv("vfs.root.mountfrom")) != NULL) {
1268		error = vfs_mountroot_try(cp);
1269		freeenv(cp);
1270		if (!error)
1271			return;
1272	}
1273
1274	/*
1275	 * Try values that may have been computed by the machine-dependant
1276	 * legacy code.
1277	 */
1278	if (!vfs_mountroot_try(rootdevnames[0]))
1279		return;
1280	if (!vfs_mountroot_try(rootdevnames[1]))
1281		return;
1282
1283	/*
1284	 * If we have a compiled-in default, and haven't already tried it, try
1285	 * it now.
1286	 */
1287#ifdef ROOTDEVNAME
1288	if (!(boothowto & RB_DFLTROOT))
1289		if (!vfs_mountroot_try(ROOTDEVNAME))
1290			return;
1291#endif
1292
1293	/*
1294	 * Everything so far has failed, prompt on the console if we haven't
1295	 * already tried that.
1296	 */
1297	if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
1298		return;
1299	panic("Root mount failed, startup aborted.");
1300}
1301
1302/*
1303 * Mount (mountfrom) as the root filesystem.
1304 */
1305static int
1306vfs_mountroot_try(char *mountfrom)
1307{
1308        struct mount	*mp;
1309	char		*vfsname, *path;
1310	const char	*devname;
1311	int		error;
1312	char		patt[32];
1313	int		s;
1314
1315	vfsname = NULL;
1316	path    = NULL;
1317	mp      = NULL;
1318	error   = EINVAL;
1319
1320	if (mountfrom == NULL)
1321		return(error);		/* don't complain */
1322
1323	s = splcam();			/* Overkill, but annoying without it */
1324	printf("Mounting root from %s\n", mountfrom);
1325	splx(s);
1326
1327	/* parse vfs name and path */
1328	vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1329	path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1330	vfsname[0] = path[0] = 0;
1331	sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1332	if (sscanf(mountfrom, patt, vfsname, path) < 1)
1333		goto done;
1334
1335	/* allocate a root mount */
1336	error = vfs_rootmountalloc(vfsname, path[0] != 0 ? path : ROOTNAME,
1337				   &mp);
1338	if (error != 0) {
1339		printf("Can't allocate root mount for filesystem '%s': %d\n",
1340		       vfsname, error);
1341		goto done;
1342	}
1343
1344	/* do our best to set rootdev */
1345	if ((path[0] != 0) && setrootbyname(path))
1346		printf("setrootbyname failed\n");
1347
1348	/* If the root device is a type "memory disk", mount RW */
1349	if (rootdev != NODEV && devsw(rootdev) != NULL) {
1350		devname = devtoname(rootdev);
1351		if (devname[0] == 'm' && devname[1] == 'd')
1352			mp->mnt_flag &= ~MNT_RDONLY;
1353	}
1354
1355	error = VFS_MOUNT(mp, NULL, NULL, NULL, curthread);
1356
1357done:
1358	if (vfsname != NULL)
1359		free(vfsname, M_MOUNT);
1360	if (path != NULL)
1361		free(path, M_MOUNT);
1362	if (error != 0) {
1363		if (mp != NULL)
1364			vfs_mount_destroy(mp, curthread);
1365		printf("Root mount failed: %d\n", error);
1366	} else {
1367
1368		/* register with list of mounted filesystems */
1369		mtx_lock(&mountlist_mtx);
1370		TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1371		mtx_unlock(&mountlist_mtx);
1372
1373		/* sanity check system clock against root fs timestamp */
1374		inittodr(mp->mnt_time);
1375		vfs_unbusy(mp, curthread);
1376		error = VFS_START(mp, 0, curthread);
1377	}
1378	return(error);
1379}
1380
1381/*
1382 * Spin prompting on the console for a suitable root filesystem
1383 */
1384static int
1385vfs_mountroot_ask(void)
1386{
1387	char name[128];
1388
1389	for(;;) {
1390		printf("\nManual root filesystem specification:\n");
1391		printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
1392#if defined(__i386__) || defined(__ia64__)
1393		printf("                       eg. ufs:da0s1a\n");
1394#else
1395		printf("                       eg. ufs:/dev/da0a\n");
1396#endif
1397		printf("  ?                  List valid disk boot devices\n");
1398		printf("  <empty line>       Abort manual input\n");
1399		printf("\nmountroot> ");
1400		gets(name);
1401		if (name[0] == 0)
1402			return(1);
1403		if (name[0] == '?') {
1404			printf("\nList of GEOM managed disk devices:\n  ");
1405			g_dev_print();
1406			continue;
1407		}
1408		if (!vfs_mountroot_try(name))
1409			return(0);
1410	}
1411}
1412
1413/*
1414 * Local helper function for vfs_mountroot_ask.
1415 */
1416static void
1417gets(char *cp)
1418{
1419	char *lp;
1420	int c;
1421
1422	lp = cp;
1423	for (;;) {
1424		printf("%c", c = cngetc() & 0177);
1425		switch (c) {
1426		case -1:
1427		case '\n':
1428		case '\r':
1429			*lp++ = '\0';
1430			return;
1431		case '\b':
1432		case '\177':
1433			if (lp > cp) {
1434				printf(" \b");
1435				lp--;
1436			}
1437			continue;
1438		case '#':
1439			lp--;
1440			if (lp < cp)
1441				lp = cp;
1442			continue;
1443		case '@':
1444		case 'u' & 037:
1445			lp = cp;
1446			printf("%c", '\n');
1447			continue;
1448		default:
1449			*lp++ = c;
1450		}
1451	}
1452}
1453
1454/*
1455 * Convert a given name to the dev_t of the disk-like device
1456 * it refers to.
1457 */
1458dev_t
1459getdiskbyname(char *name) {
1460	char *cp;
1461	dev_t dev;
1462
1463	cp = name;
1464	if (!bcmp(cp, "/dev/", 5))
1465		cp += 5;
1466
1467	dev = NODEV;
1468	EVENTHANDLER_INVOKE(dev_clone, cp, strlen(cp), &dev);
1469	return (dev);
1470}
1471
1472/*
1473 * Set rootdev to match (name), given that we expect it to
1474 * refer to a disk-like device.
1475 */
1476static int
1477setrootbyname(char *name)
1478{
1479	dev_t diskdev;
1480
1481	diskdev = getdiskbyname(name);
1482	if (diskdev != NODEV) {
1483		rootdev = diskdev;
1484		return (0);
1485	}
1486
1487	return (1);
1488}
1489
1490/* Show the dev_t for a disk specified by name */
1491#ifdef DDB
1492DB_SHOW_COMMAND(disk, db_getdiskbyname)
1493{
1494	dev_t dev;
1495
1496	if (modif[0] == '\0') {
1497		db_error("usage: show disk/devicename");
1498		return;
1499	}
1500	dev = getdiskbyname(modif);
1501	if (dev != NODEV)
1502		db_printf("dev_t = %p\n", dev);
1503	else
1504		db_printf("No disk device matched.\n");
1505}
1506#endif
1507
1508/*
1509 * Get a mount option by its name.
1510 *
1511 * Return 0 if the option was found, ENOENT otherwise.
1512 * If len is non-NULL it will be filled with the length
1513 * of the option. If buf is non-NULL, it will be filled
1514 * with the address of the option.
1515 */
1516int
1517vfs_getopt(opts, name, buf, len)
1518	struct vfsoptlist *opts;
1519	const char *name;
1520	void **buf;
1521	int *len;
1522{
1523	struct vfsopt *opt;
1524
1525	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1526
1527	TAILQ_FOREACH(opt, opts, link) {
1528		if (strcmp(name, opt->name) == 0) {
1529			if (len != NULL)
1530				*len = opt->len;
1531			if (buf != NULL)
1532				*buf = opt->value;
1533			return (0);
1534		}
1535	}
1536	return (ENOENT);
1537}
1538
1539/*
1540 * Find and copy a mount option.
1541 *
1542 * The size of the buffer has to be specified
1543 * in len, if it is not the same length as the
1544 * mount option, EINVAL is returned.
1545 * Returns ENOENT if the option is not found.
1546 */
1547int
1548vfs_copyopt(opts, name, dest, len)
1549	struct vfsoptlist *opts;
1550	const char *name;
1551	void *dest;
1552	int len;
1553{
1554	struct vfsopt *opt;
1555
1556	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1557
1558	TAILQ_FOREACH(opt, opts, link) {
1559		if (strcmp(name, opt->name) == 0) {
1560			if (len != opt->len)
1561				return (EINVAL);
1562			bcopy(opt->value, dest, opt->len);
1563			return (0);
1564		}
1565	}
1566	return (ENOENT);
1567}
1568