vfs_mount.c revision 122640
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 122640 2003-11-14 05:27:41Z kan $");
67
68#include <sys/param.h>
69#include <sys/conf.h>
70#include <sys/cons.h>
71#include <sys/kernel.h>
72#include <sys/linker.h>
73#include <sys/mac.h>
74#include <sys/malloc.h>
75#include <sys/mount.h>
76#include <sys/mutex.h>
77#include <sys/namei.h>
78#include <sys/proc.h>
79#include <sys/filedesc.h>
80#include <sys/reboot.h>
81#include <sys/sysproto.h>
82#include <sys/sx.h>
83#include <sys/sysctl.h>
84#include <sys/sysent.h>
85#include <sys/systm.h>
86#include <sys/vnode.h>
87
88#include <geom/geom.h>
89
90#include <machine/stdarg.h>
91
92#include "opt_rootdevname.h"
93#include "opt_ddb.h"
94#include "opt_mac.h"
95
96#ifdef DDB
97#include <ddb/ddb.h>
98#endif
99
100#define	ROOTNAME		"root_device"
101#define	VFS_MOUNTARG_SIZE_MAX	(1024 * 64)
102
103static void	checkdirs(struct vnode *olddp, struct vnode *newdp);
104static void	gets(char *cp);
105static int	vfs_domount(struct thread *td, const char *fstype,
106		    char *fspath, int fsflags, void *fsdata, int compat);
107static int	vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp,
108		    const char *fspath, struct thread *td, struct mount **mpp);
109static int	vfs_mountroot_ask(void);
110static int	vfs_mountroot_try(char *mountfrom);
111static int	vfs_nmount(struct thread *td, int fsflags, struct uio *fsopts);
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	TAILQ_INIT(&mp->mnt_reservedvnlist);
495	mp->mnt_nvnodelistsize = 0;
496	mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
497	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
498	vfs_busy(mp, LK_NOWAIT, 0, td);
499	mp->mnt_op = vfsp->vfc_vfsops;
500	mp->mnt_vfc = vfsp;
501	vfsp->vfc_refcount++;
502	mp->mnt_stat.f_type = vfsp->vfc_typenum;
503	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
504	strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
505	mp->mnt_vnodecovered = vp;
506	mp->mnt_cred = crdup(td->td_ucred);
507	mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
508	strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
509	mp->mnt_iosize_max = DFLTPHYS;
510#ifdef MAC
511	mac_init_mount(mp);
512	mac_create_mount(td->td_ucred, mp);
513#endif
514	*mpp = mp;
515	return (0);
516}
517
518/*
519 * Destroy the mount struct previously allocated by vfs_mount_alloc().
520 */
521void
522vfs_mount_destroy(struct mount *mp, struct thread *td)
523{
524
525	mp->mnt_vfc->vfc_refcount--;
526	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
527		panic("unmount: dangling vnode");
528	vfs_unbusy(mp,td);
529	lockdestroy(&mp->mnt_lock);
530	mtx_destroy(&mp->mnt_mtx);
531	if (mp->mnt_kern_flag & MNTK_MWAIT)
532		wakeup(mp);
533#ifdef MAC
534	mac_destroy_mount(mp);
535#endif
536	if (mp->mnt_op->vfs_mount == NULL)
537		vfs_freeopts(mp->mnt_opt);
538	crfree(mp->mnt_cred);
539	free(mp, M_MOUNT);
540}
541
542static int
543vfs_nmount(struct thread *td, int fsflags, struct uio *fsoptions)
544{
545	struct vfsoptlist *optlist;
546	char *fstype, *fspath;
547	int error, fstypelen, fspathlen;
548
549	error = vfs_buildopts(fsoptions, &optlist);
550	if (error)
551		return (error);
552
553	/*
554	 * We need these two options before the others,
555	 * and they are mandatory for any filesystem.
556	 * Ensure they are NUL terminated as well.
557	 */
558	fstypelen = 0;
559	error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
560	if (error || fstype[fstypelen - 1] != '\0') {
561		error = EINVAL;
562		goto bail;
563	}
564	fspathlen = 0;
565	error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
566	if (error || fspath[fspathlen - 1] != '\0') {
567		error = EINVAL;
568		goto bail;
569	}
570
571	/*
572	 * Be ultra-paranoid about making sure the type and fspath
573	 * variables will fit in our mp buffers, including the
574	 * terminating NUL.
575	 */
576	if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
577		error = ENAMETOOLONG;
578		goto bail;
579	}
580
581	error = vfs_domount(td, fstype, fspath, fsflags, optlist, 0);
582bail:
583	if (error)
584		vfs_freeopts(optlist);
585	return (error);
586}
587
588/*
589 * Old mount API.
590 */
591#ifndef _SYS_SYSPROTO_H_
592struct mount_args {
593	char	*type;
594	char	*path;
595	int	flags;
596	caddr_t	data;
597};
598#endif
599/* ARGSUSED */
600int
601mount(td, uap)
602	struct thread *td;
603	struct mount_args /* {
604		char *type;
605		char *path;
606		int flags;
607		caddr_t data;
608	} */ *uap;
609{
610	char *fstype;
611	char *fspath;
612	int error;
613
614	fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
615	fspath = malloc(MNAMELEN, M_TEMP, M_WAITOK);
616
617	/*
618	 * vfs_mount() actually takes a kernel string for `type' and
619	 * `path' now, so extract them.
620	 */
621	error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
622	if (error == 0)
623		error = copyinstr(uap->path, fspath, MNAMELEN, NULL);
624	if (error == 0)
625		error = vfs_domount(td, fstype, fspath, uap->flags,
626		    uap->data, 1);
627	free(fstype, M_TEMP);
628	free(fspath, M_TEMP);
629	return (error);
630}
631
632/*
633 * vfs_mount(): actually attempt a filesystem mount.
634 *
635 * This routine is designed to be a "generic" entry point for routines
636 * that wish to mount a filesystem. All parameters except `fsdata' are
637 * pointers into kernel space. `fsdata' is currently still a pointer
638 * into userspace.
639 */
640int
641vfs_mount(td, fstype, fspath, fsflags, fsdata)
642	struct thread *td;
643	const char *fstype;
644	char *fspath;
645	int fsflags;
646	void *fsdata;
647{
648
649	return (vfs_domount(td, fstype, fspath, fsflags, fsdata, 1));
650}
651
652/*
653 * vfs_domount(): actually attempt a filesystem mount.
654 */
655static int
656vfs_domount(
657	struct thread *td,	/* Flags common to all filesystems. */
658	const char *fstype,	/* Filesystem type. */
659	char *fspath,		/* Mount path. */
660	int fsflags,		/* Flags common to all filesystems. */
661	void *fsdata,		/* Options local to the filesystem. */
662	int compat		/* Invocation from compat syscall. */
663	)
664{
665	linker_file_t lf;
666	struct vnode *vp;
667	struct mount *mp;
668	struct vfsconf *vfsp;
669	int error, flag = 0, kern_flag = 0;
670	struct vattr va;
671	struct nameidata nd;
672
673	/*
674	 * Be ultra-paranoid about making sure the type and fspath
675	 * variables will fit in our mp buffers, including the
676	 * terminating NUL.
677	 */
678	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
679		return (ENAMETOOLONG);
680
681	if (usermount == 0) {
682		error = suser(td);
683		if (error)
684			return (error);
685	}
686	/*
687	 * Do not allow NFS export by non-root users.
688	 */
689	if (fsflags & MNT_EXPORTED) {
690		error = suser(td);
691		if (error)
692			return (error);
693	}
694	/*
695	 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users.
696	 */
697	if (suser(td))
698		fsflags |= MNT_NOSUID | MNT_NODEV;
699	/*
700	 * Get vnode to be covered
701	 */
702	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
703	if ((error = namei(&nd)) != 0)
704		return (error);
705	NDFREE(&nd, NDF_ONLY_PNBUF);
706	vp = nd.ni_vp;
707	if (fsflags & MNT_UPDATE) {
708		if ((vp->v_vflag & VV_ROOT) == 0) {
709			vput(vp);
710			return (EINVAL);
711		}
712		mp = vp->v_mount;
713		flag = mp->mnt_flag;
714		kern_flag = mp->mnt_kern_flag;
715		/*
716		 * We only allow the filesystem to be reloaded if it
717		 * is currently mounted read-only.
718		 */
719		if ((fsflags & MNT_RELOAD) &&
720		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
721			vput(vp);
722			return (EOPNOTSUPP);	/* Needs translation */
723		}
724		/*
725		 * Only root, or the user that did the original mount is
726		 * permitted to update it.
727		 */
728		if (mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
729			error = suser(td);
730			if (error) {
731				vput(vp);
732				return (error);
733			}
734		}
735		if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
736			vput(vp);
737			return (EBUSY);
738		}
739		VI_LOCK(vp);
740		if ((vp->v_iflag & VI_MOUNT) != 0 ||
741		    vp->v_mountedhere != NULL) {
742			VI_UNLOCK(vp);
743			vfs_unbusy(mp, td);
744			vput(vp);
745			return (EBUSY);
746		}
747		vp->v_iflag |= VI_MOUNT;
748		VI_UNLOCK(vp);
749		mp->mnt_flag |= fsflags &
750		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT);
751		VOP_UNLOCK(vp, 0, td);
752		if (compat == 0) {
753			mp->mnt_optnew = fsdata;
754			vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
755		}
756		goto update;
757	}
758	/*
759	 * If the user is not root, ensure that they own the directory
760	 * onto which we are attempting to mount.
761	 */
762	error = VOP_GETATTR(vp, &va, td->td_ucred, td);
763	if (error) {
764		vput(vp);
765		return (error);
766	}
767	if (va.va_uid != td->td_ucred->cr_uid) {
768		error = suser(td);
769		if (error) {
770			vput(vp);
771			return (error);
772		}
773	}
774	if ((error = vinvalbuf(vp, V_SAVE, td->td_ucred, td, 0, 0)) != 0) {
775		vput(vp);
776		return (error);
777	}
778	if (vp->v_type != VDIR) {
779		vput(vp);
780		return (ENOTDIR);
781	}
782	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
783		if (!strcmp(vfsp->vfc_name, fstype))
784			break;
785	if (vfsp == NULL) {
786		/* Only load modules for root (very important!). */
787		error = suser(td);
788		if (error) {
789			vput(vp);
790			return (error);
791		}
792		error = securelevel_gt(td->td_ucred, 0);
793		if (error) {
794			vput(vp);
795			return (error);
796		}
797		error = linker_load_module(NULL, fstype, NULL, NULL, &lf);
798		if (error || lf == NULL) {
799			vput(vp);
800			if (lf == NULL)
801				error = ENODEV;
802			return (error);
803		}
804		lf->userrefs++;
805		/* Look up again to see if the VFS was loaded. */
806		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
807			if (!strcmp(vfsp->vfc_name, fstype))
808				break;
809		if (vfsp == NULL) {
810			lf->userrefs--;
811			linker_file_unload(lf);
812			vput(vp);
813			return (ENODEV);
814		}
815	}
816	VI_LOCK(vp);
817	if ((vp->v_iflag & VI_MOUNT) != 0 ||
818	    vp->v_mountedhere != NULL) {
819		VI_UNLOCK(vp);
820		vput(vp);
821		return (EBUSY);
822	}
823	vp->v_iflag |= VI_MOUNT;
824	VI_UNLOCK(vp);
825
826	/*
827	 * Allocate and initialize the filesystem.
828	 */
829	error = vfs_mount_alloc(vp, vfsp, fspath, td, &mp);
830	if (error) {
831		vput(vp);
832		return (error);
833	}
834	VOP_UNLOCK(vp, 0, td);
835
836	/* XXXMAC: pass to vfs_mount_alloc? */
837	if (compat == 0)
838		mp->mnt_optnew = fsdata;
839update:
840	/*
841	 * Check if the fs implements the type VFS_[N]MOUNT()
842	 * function we are looking for.
843	 */
844	if ((compat == 0) == (mp->mnt_op->vfs_mount != NULL)) {
845		printf("%s doesn't support the %s mount syscall\n",
846		    mp->mnt_vfc->vfc_name, compat? "old" : "new");
847		VI_LOCK(vp);
848		vp->v_iflag &= ~VI_MOUNT;
849		VI_UNLOCK(vp);
850		if (mp->mnt_flag & MNT_UPDATE)
851			vfs_unbusy(mp, td);
852		else
853			vfs_mount_destroy(mp, td);
854		vrele(vp);
855		return (EOPNOTSUPP);
856	}
857
858	/*
859	 * Set the mount level flags.
860	 */
861	if (fsflags & MNT_RDONLY)
862		mp->mnt_flag |= MNT_RDONLY;
863	else if (mp->mnt_flag & MNT_RDONLY)
864		mp->mnt_kern_flag |= MNTK_WANTRDWR;
865	mp->mnt_flag &=~ MNT_UPDATEMASK;
866	mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE);
867	/*
868	 * Mount the filesystem.
869	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
870	 * get.  No freeing of cn_pnbuf.
871	 */
872	error = compat? VFS_MOUNT(mp, fspath, fsdata, &nd, td) :
873	    VFS_NMOUNT(mp, &nd, td);
874	if (!error) {
875		if (mp->mnt_opt != NULL)
876			vfs_freeopts(mp->mnt_opt);
877		mp->mnt_opt = mp->mnt_optnew;
878	}
879	/*
880	 * Prevent external consumers of mount options from reading
881	 * mnt_optnew.
882	*/
883	mp->mnt_optnew = NULL;
884	if (mp->mnt_flag & MNT_UPDATE) {
885		if (mp->mnt_kern_flag & MNTK_WANTRDWR)
886			mp->mnt_flag &= ~MNT_RDONLY;
887		mp->mnt_flag &=~
888		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
889		mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
890		if (error) {
891			mp->mnt_flag = flag;
892			mp->mnt_kern_flag = kern_flag;
893		}
894		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
895			if (mp->mnt_syncer == NULL)
896				error = vfs_allocate_syncvnode(mp);
897		} else {
898			if (mp->mnt_syncer != NULL)
899				vrele(mp->mnt_syncer);
900			mp->mnt_syncer = NULL;
901		}
902		vfs_unbusy(mp, td);
903		VI_LOCK(vp);
904		vp->v_iflag &= ~VI_MOUNT;
905		VI_UNLOCK(vp);
906		vrele(vp);
907		return (error);
908	}
909	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
910	/*
911	 * Put the new filesystem on the mount list after root.
912	 */
913	cache_purge(vp);
914	if (!error) {
915		struct vnode *newdp;
916
917		VI_LOCK(vp);
918		vp->v_iflag &= ~VI_MOUNT;
919		VI_UNLOCK(vp);
920		vp->v_mountedhere = mp;
921		mtx_lock(&mountlist_mtx);
922		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
923		mtx_unlock(&mountlist_mtx);
924		if (VFS_ROOT(mp, &newdp))
925			panic("mount: lost mount");
926		checkdirs(vp, newdp);
927		vput(newdp);
928		VOP_UNLOCK(vp, 0, td);
929		if ((mp->mnt_flag & MNT_RDONLY) == 0)
930			error = vfs_allocate_syncvnode(mp);
931		vfs_unbusy(mp, td);
932		if ((error = VFS_START(mp, 0, td)) != 0)
933			vrele(vp);
934	} else {
935		VI_LOCK(vp);
936		vp->v_iflag &= ~VI_MOUNT;
937		VI_UNLOCK(vp);
938		vfs_mount_destroy(mp, td);
939		vput(vp);
940	}
941	return (error);
942}
943
944/*
945 * Scan all active processes to see if any of them have a current
946 * or root directory of `olddp'. If so, replace them with the new
947 * mount point.
948 */
949static void
950checkdirs(olddp, newdp)
951	struct vnode *olddp, *newdp;
952{
953	struct filedesc *fdp;
954	struct proc *p;
955	int nrele;
956
957	if (vrefcnt(olddp) == 1)
958		return;
959	sx_slock(&allproc_lock);
960	LIST_FOREACH(p, &allproc, p_list) {
961		mtx_lock(&fdesc_mtx);
962		fdp = p->p_fd;
963		if (fdp == NULL) {
964			mtx_unlock(&fdesc_mtx);
965			continue;
966		}
967		nrele = 0;
968		FILEDESC_LOCK(fdp);
969		if (fdp->fd_cdir == olddp) {
970			VREF(newdp);
971			fdp->fd_cdir = newdp;
972			nrele++;
973		}
974		if (fdp->fd_rdir == olddp) {
975			VREF(newdp);
976			fdp->fd_rdir = newdp;
977			nrele++;
978		}
979		FILEDESC_UNLOCK(fdp);
980		mtx_unlock(&fdesc_mtx);
981		while (nrele--)
982			vrele(olddp);
983	}
984	sx_sunlock(&allproc_lock);
985	if (rootvnode == olddp) {
986		vrele(rootvnode);
987		VREF(newdp);
988		rootvnode = newdp;
989	}
990}
991
992/*
993 * Unmount a filesystem.
994 *
995 * Note: unmount takes a path to the vnode mounted on as argument,
996 * not special file (as before).
997 */
998#ifndef _SYS_SYSPROTO_H_
999struct unmount_args {
1000	char	*path;
1001	int	flags;
1002};
1003#endif
1004/* ARGSUSED */
1005int
1006unmount(td, uap)
1007	struct thread *td;
1008	register struct unmount_args /* {
1009		char *path;
1010		int flags;
1011	} */ *uap;
1012{
1013	struct mount *mp;
1014	char *pathbuf;
1015	int error, id0, id1;
1016
1017	pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1018	error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1019	if (error) {
1020		free(pathbuf, M_TEMP);
1021		return (error);
1022	}
1023	if (uap->flags & MNT_BYFSID) {
1024		/* Decode the filesystem ID. */
1025		if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1026			free(pathbuf, M_TEMP);
1027			return (EINVAL);
1028		}
1029
1030		mtx_lock(&mountlist_mtx);
1031		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list)
1032			if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1033			    mp->mnt_stat.f_fsid.val[1] == id1)
1034				break;
1035		mtx_unlock(&mountlist_mtx);
1036	} else {
1037		mtx_lock(&mountlist_mtx);
1038		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list)
1039			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1040				break;
1041		mtx_unlock(&mountlist_mtx);
1042	}
1043	free(pathbuf, M_TEMP);
1044	if (mp == NULL) {
1045		/*
1046		 * Previously we returned ENOENT for a nonexistent path and
1047		 * EINVAL for a non-mountpoint.  We cannot tell these apart
1048		 * now, so in the !MNT_BYFSID case return the more likely
1049		 * EINVAL for compatibility.
1050		 */
1051		return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1052	}
1053
1054	/*
1055	 * Only root, or the user that did the original mount is
1056	 * permitted to unmount this filesystem.
1057	 */
1058	if (mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
1059		error = suser(td);
1060		if (error)
1061			return (error);
1062	}
1063
1064	/*
1065	 * Don't allow unmounting the root filesystem.
1066	 */
1067	if (mp->mnt_flag & MNT_ROOTFS)
1068		return (EINVAL);
1069	return (dounmount(mp, uap->flags, td));
1070}
1071
1072/*
1073 * Do the actual filesystem unmount.
1074 */
1075int
1076dounmount(mp, flags, td)
1077	struct mount *mp;
1078	int flags;
1079	struct thread *td;
1080{
1081	struct vnode *coveredvp, *fsrootvp;
1082	int error;
1083	int async_flag;
1084
1085	mtx_lock(&mountlist_mtx);
1086	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1087		mtx_unlock(&mountlist_mtx);
1088		return (EBUSY);
1089	}
1090	mp->mnt_kern_flag |= MNTK_UNMOUNT;
1091	/* Allow filesystems to detect that a forced unmount is in progress. */
1092	if (flags & MNT_FORCE)
1093		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1094	error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1095	    ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_mtx, td);
1096	if (error) {
1097		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1098		if (mp->mnt_kern_flag & MNTK_MWAIT)
1099			wakeup(mp);
1100		return (error);
1101	}
1102	vn_start_write(NULL, &mp, V_WAIT);
1103
1104	if (mp->mnt_flag & MNT_EXPUBLIC)
1105		vfs_setpublicfs(NULL, NULL, NULL);
1106
1107	vfs_msync(mp, MNT_WAIT);
1108	async_flag = mp->mnt_flag & MNT_ASYNC;
1109	mp->mnt_flag &=~ MNT_ASYNC;
1110	cache_purgevfs(mp);	/* remove cache entries for this file sys */
1111	if (mp->mnt_syncer != NULL)
1112		vrele(mp->mnt_syncer);
1113	/* Move process cdir/rdir refs on fs root to underlying vnode. */
1114	if (VFS_ROOT(mp, &fsrootvp) == 0) {
1115		if (mp->mnt_vnodecovered != NULL)
1116			checkdirs(fsrootvp, mp->mnt_vnodecovered);
1117		if (fsrootvp == rootvnode) {
1118			vrele(rootvnode);
1119			rootvnode = NULL;
1120		}
1121		vput(fsrootvp);
1122	}
1123	if (((mp->mnt_flag & MNT_RDONLY) ||
1124	     (error = VFS_SYNC(mp, MNT_WAIT, td->td_ucred, td)) == 0) ||
1125	    (flags & MNT_FORCE)) {
1126		error = VFS_UNMOUNT(mp, flags, td);
1127	}
1128	vn_finished_write(mp);
1129	if (error) {
1130		/* Undo cdir/rdir and rootvnode changes made above. */
1131		if (VFS_ROOT(mp, &fsrootvp) == 0) {
1132			if (mp->mnt_vnodecovered != NULL)
1133				checkdirs(mp->mnt_vnodecovered, fsrootvp);
1134			if (rootvnode == NULL) {
1135				rootvnode = fsrootvp;
1136				vref(rootvnode);
1137			}
1138			vput(fsrootvp);
1139		}
1140		if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
1141			(void) vfs_allocate_syncvnode(mp);
1142		mtx_lock(&mountlist_mtx);
1143		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1144		mp->mnt_flag |= async_flag;
1145		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK,
1146		    &mountlist_mtx, td);
1147		if (mp->mnt_kern_flag & MNTK_MWAIT)
1148			wakeup(mp);
1149		return (error);
1150	}
1151	mtx_lock(&mountlist_mtx);
1152	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1153	if ((coveredvp = mp->mnt_vnodecovered) != NULL)
1154		coveredvp->v_mountedhere = NULL;
1155	mtx_unlock(&mountlist_mtx);
1156	vfs_mount_destroy(mp, td);
1157	if (coveredvp != NULL)
1158		vrele(coveredvp);
1159	return (0);
1160}
1161
1162/*
1163 * Lookup a filesystem type, and if found allocate and initialize
1164 * a mount structure for it.
1165 *
1166 * Devname is usually updated by mount(8) after booting.
1167 */
1168int
1169vfs_rootmountalloc(fstypename, devname, mpp)
1170	char *fstypename;
1171	char *devname;
1172	struct mount **mpp;
1173{
1174	struct thread *td = curthread;	/* XXX */
1175	struct vfsconf *vfsp;
1176	struct mount *mp;
1177	int error;
1178
1179	if (fstypename == NULL)
1180		return (ENODEV);
1181	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1182		if (!strcmp(vfsp->vfc_name, fstypename))
1183			break;
1184	if (vfsp == NULL)
1185		return (ENODEV);
1186	error = vfs_mount_alloc(NULLVP, vfsp, "/", td, &mp);
1187	if (error)
1188		return (error);
1189	mp->mnt_flag |= MNT_RDONLY | MNT_ROOTFS;
1190	strlcpy(mp->mnt_stat.f_mntfromname, devname, MNAMELEN);
1191	*mpp = mp;
1192	return (0);
1193}
1194
1195/*
1196 * Find and mount the root filesystem
1197 */
1198void
1199vfs_mountroot(void)
1200{
1201	char *cp;
1202	int error, i;
1203
1204	g_waitidle();
1205
1206	/*
1207	 * The root filesystem information is compiled in, and we are
1208	 * booted with instructions to use it.
1209	 */
1210#ifdef ROOTDEVNAME
1211	if ((boothowto & RB_DFLTROOT) && !vfs_mountroot_try(ROOTDEVNAME))
1212		return;
1213#endif
1214	/*
1215	 * We are booted with instructions to prompt for the root filesystem,
1216	 * or to use the compiled-in default when it doesn't exist.
1217	 */
1218	if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
1219		if (!vfs_mountroot_ask())
1220			return;
1221	}
1222
1223	/*
1224	 * We've been given the generic "use CDROM as root" flag.  This is
1225	 * necessary because one media may be used in many different
1226	 * devices, so we need to search for them.
1227	 */
1228	if (boothowto & RB_CDROM) {
1229		for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1230			if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1231				return;
1232		}
1233	}
1234
1235	/*
1236	 * Try to use the value read by the loader from /etc/fstab, or
1237	 * supplied via some other means.  This is the preferred
1238	 * mechanism.
1239	 */
1240	if ((cp = getenv("vfs.root.mountfrom")) != NULL) {
1241		error = vfs_mountroot_try(cp);
1242		freeenv(cp);
1243		if (!error)
1244			return;
1245	}
1246
1247	/*
1248	 * Try values that may have been computed by the machine-dependant
1249	 * legacy code.
1250	 */
1251	if (!vfs_mountroot_try(rootdevnames[0]))
1252		return;
1253	if (!vfs_mountroot_try(rootdevnames[1]))
1254		return;
1255
1256	/*
1257	 * If we have a compiled-in default, and haven't already tried it, try
1258	 * it now.
1259	 */
1260#ifdef ROOTDEVNAME
1261	if (!(boothowto & RB_DFLTROOT))
1262		if (!vfs_mountroot_try(ROOTDEVNAME))
1263			return;
1264#endif
1265
1266	/*
1267	 * Everything so far has failed, prompt on the console if we haven't
1268	 * already tried that.
1269	 */
1270	if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
1271		return;
1272	panic("Root mount failed, startup aborted.");
1273}
1274
1275/*
1276 * Mount (mountfrom) as the root filesystem.
1277 */
1278static int
1279vfs_mountroot_try(char *mountfrom)
1280{
1281        struct mount	*mp;
1282	char		*vfsname, *path;
1283	const char	*devname;
1284	int		error;
1285	char		patt[32];
1286	int		s;
1287
1288	vfsname = NULL;
1289	path    = NULL;
1290	mp      = NULL;
1291	error   = EINVAL;
1292
1293	if (mountfrom == NULL)
1294		return(error);		/* don't complain */
1295
1296	s = splcam();			/* Overkill, but annoying without it */
1297	printf("Mounting root from %s\n", mountfrom);
1298	splx(s);
1299
1300	/* parse vfs name and path */
1301	vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1302	path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1303	vfsname[0] = path[0] = 0;
1304	sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1305	if (sscanf(mountfrom, patt, vfsname, path) < 1)
1306		goto done;
1307
1308	/* allocate a root mount */
1309	error = vfs_rootmountalloc(vfsname, path[0] != 0 ? path : ROOTNAME,
1310				   &mp);
1311	if (error != 0) {
1312		printf("Can't allocate root mount for filesystem '%s': %d\n",
1313		       vfsname, error);
1314		goto done;
1315	}
1316
1317	/* do our best to set rootdev */
1318	if ((path[0] != 0) && setrootbyname(path))
1319		printf("setrootbyname failed\n");
1320
1321	/* If the root device is a type "memory disk", mount RW */
1322	if (rootdev != NODEV && devsw(rootdev) != NULL) {
1323		devname = devtoname(rootdev);
1324		if (devname[0] == 'm' && devname[1] == 'd')
1325			mp->mnt_flag &= ~MNT_RDONLY;
1326	}
1327
1328	error = VFS_MOUNT(mp, NULL, NULL, NULL, curthread);
1329
1330done:
1331	if (vfsname != NULL)
1332		free(vfsname, M_MOUNT);
1333	if (path != NULL)
1334		free(path, M_MOUNT);
1335	if (error != 0) {
1336		if (mp != NULL)
1337			vfs_mount_destroy(mp, curthread);
1338		printf("Root mount failed: %d\n", error);
1339	} else {
1340
1341		/* register with list of mounted filesystems */
1342		mtx_lock(&mountlist_mtx);
1343		TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1344		mtx_unlock(&mountlist_mtx);
1345
1346		/* sanity check system clock against root fs timestamp */
1347		inittodr(mp->mnt_time);
1348		vfs_unbusy(mp, curthread);
1349		error = VFS_START(mp, 0, curthread);
1350	}
1351	return(error);
1352}
1353
1354/*
1355 * Spin prompting on the console for a suitable root filesystem
1356 */
1357static int
1358vfs_mountroot_ask(void)
1359{
1360	char name[128];
1361
1362	for(;;) {
1363		printf("\nManual root filesystem specification:\n");
1364		printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
1365#if defined(__i386__) || defined(__ia64__)
1366		printf("                       eg. ufs:da0s1a\n");
1367#else
1368		printf("                       eg. ufs:/dev/da0a\n");
1369#endif
1370		printf("  ?                  List valid disk boot devices\n");
1371		printf("  <empty line>       Abort manual input\n");
1372		printf("\nmountroot> ");
1373		gets(name);
1374		if (name[0] == 0)
1375			return(1);
1376		if (name[0] == '?') {
1377			printf("\nList of GEOM managed disk devices:\n  ");
1378			g_dev_print();
1379			continue;
1380		}
1381		if (!vfs_mountroot_try(name))
1382			return(0);
1383	}
1384}
1385
1386/*
1387 * Local helper function for vfs_mountroot_ask.
1388 */
1389static void
1390gets(char *cp)
1391{
1392	char *lp;
1393	int c;
1394
1395	lp = cp;
1396	for (;;) {
1397		printf("%c", c = cngetc() & 0177);
1398		switch (c) {
1399		case -1:
1400		case '\n':
1401		case '\r':
1402			*lp++ = '\0';
1403			return;
1404		case '\b':
1405		case '\177':
1406			if (lp > cp) {
1407				printf(" \b");
1408				lp--;
1409			}
1410			continue;
1411		case '#':
1412			lp--;
1413			if (lp < cp)
1414				lp = cp;
1415			continue;
1416		case '@':
1417		case 'u' & 037:
1418			lp = cp;
1419			printf("%c", '\n');
1420			continue;
1421		default:
1422			*lp++ = c;
1423		}
1424	}
1425}
1426
1427/*
1428 * Convert a given name to the dev_t of the disk-like device
1429 * it refers to.
1430 */
1431dev_t
1432getdiskbyname(char *name) {
1433	char *cp;
1434	dev_t dev;
1435
1436	cp = name;
1437	if (!bcmp(cp, "/dev/", 5))
1438		cp += 5;
1439
1440	dev = NODEV;
1441	EVENTHANDLER_INVOKE(dev_clone, cp, strlen(cp), &dev);
1442	return (dev);
1443}
1444
1445/*
1446 * Set rootdev to match (name), given that we expect it to
1447 * refer to a disk-like device.
1448 */
1449static int
1450setrootbyname(char *name)
1451{
1452	dev_t diskdev;
1453
1454	diskdev = getdiskbyname(name);
1455	if (diskdev != NODEV) {
1456		rootdev = diskdev;
1457		return (0);
1458	}
1459
1460	return (1);
1461}
1462
1463/* Show the dev_t for a disk specified by name */
1464#ifdef DDB
1465DB_SHOW_COMMAND(disk, db_getdiskbyname)
1466{
1467	dev_t dev;
1468
1469	if (modif[0] == '\0') {
1470		db_error("usage: show disk/devicename");
1471		return;
1472	}
1473	dev = getdiskbyname(modif);
1474	if (dev != NODEV)
1475		db_printf("dev_t = %p\n", dev);
1476	else
1477		db_printf("No disk device matched.\n");
1478}
1479#endif
1480
1481/*
1482 * Get a mount option by its name.
1483 *
1484 * Return 0 if the option was found, ENOENT otherwise.
1485 * If len is non-NULL it will be filled with the length
1486 * of the option. If buf is non-NULL, it will be filled
1487 * with the address of the option.
1488 */
1489int
1490vfs_getopt(opts, name, buf, len)
1491	struct vfsoptlist *opts;
1492	const char *name;
1493	void **buf;
1494	int *len;
1495{
1496	struct vfsopt *opt;
1497
1498	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1499
1500	TAILQ_FOREACH(opt, opts, link) {
1501		if (strcmp(name, opt->name) == 0) {
1502			if (len != NULL)
1503				*len = opt->len;
1504			if (buf != NULL)
1505				*buf = opt->value;
1506			return (0);
1507		}
1508	}
1509	return (ENOENT);
1510}
1511
1512/*
1513 * Find and copy a mount option.
1514 *
1515 * The size of the buffer has to be specified
1516 * in len, if it is not the same length as the
1517 * mount option, EINVAL is returned.
1518 * Returns ENOENT if the option is not found.
1519 */
1520int
1521vfs_copyopt(opts, name, dest, len)
1522	struct vfsoptlist *opts;
1523	const char *name;
1524	void *dest;
1525	int len;
1526{
1527	struct vfsopt *opt;
1528
1529	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1530
1531	TAILQ_FOREACH(opt, opts, link) {
1532		if (strcmp(name, opt->name) == 0) {
1533			if (len != opt->len)
1534				return (EINVAL);
1535			bcopy(opt->value, dest, opt->len);
1536			return (0);
1537		}
1538	}
1539	return (ENOENT);
1540}
1541