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