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