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