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