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