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