vfs_mount.c revision 175294
1241675Suqs/*-
2241675Suqs * Copyright (c) 1999-2004 Poul-Henning Kamp
3241675Suqs * Copyright (c) 1999 Michael Smith
4241675Suqs * Copyright (c) 1989, 1993
5241675Suqs *	The Regents of the University of California.  All rights reserved.
6241675Suqs * (c) UNIX System Laboratories, Inc.
7241675Suqs * All or some portions of this file are derived from material licensed
8241675Suqs * to the University of California by American Telephone and Telegraph
9241675Suqs * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10241675Suqs * the permission of UNIX System Laboratories, Inc.
11241675Suqs *
12241675Suqs * Redistribution and use in source and binary forms, with or without
13241675Suqs * modification, are permitted provided that the following conditions
14241675Suqs * are met:
15241675Suqs * 1. Redistributions of source code must retain the above copyright
16241675Suqs *    notice, this list of conditions and the following disclaimer.
17241675Suqs * 2. Redistributions in binary form must reproduce the above copyright
18241675Suqs *    notice, this list of conditions and the following disclaimer in the
19241675Suqs *    documentation and/or other materials provided with the distribution.
20241675Suqs * 4. Neither the name of the University nor the names of its contributors
21241675Suqs *    may be used to endorse or promote products derived from this software
22241675Suqs *    without specific prior written permission.
23241675Suqs *
24241675Suqs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25241675Suqs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26241675Suqs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27241675Suqs * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28241675Suqs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29241675Suqs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30241675Suqs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31241675Suqs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32241675Suqs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33241675Suqs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34241675Suqs * SUCH DAMAGE.
35241675Suqs */
36241675Suqs
37241675Suqs#include <sys/cdefs.h>
38241675Suqs__FBSDID("$FreeBSD: head/sys/kern/vfs_mount.c 175294 2008-01-13 14:44:15Z attilio $");
39241675Suqs
40241675Suqs#include <sys/param.h>
41241675Suqs#include <sys/conf.h>
42241675Suqs#include <sys/clock.h>
43241675Suqs#include <sys/jail.h>
44241675Suqs#include <sys/kernel.h>
45241675Suqs#include <sys/libkern.h>
46241675Suqs#include <sys/malloc.h>
47241675Suqs#include <sys/mount.h>
48241675Suqs#include <sys/mutex.h>
49241675Suqs#include <sys/namei.h>
50241675Suqs#include <sys/priv.h>
51241675Suqs#include <sys/proc.h>
52241675Suqs#include <sys/filedesc.h>
53241675Suqs#include <sys/reboot.h>
54241675Suqs#include <sys/syscallsubr.h>
55241675Suqs#include <sys/sysproto.h>
56241675Suqs#include <sys/sx.h>
57241675Suqs#include <sys/sysctl.h>
58241675Suqs#include <sys/sysent.h>
59241675Suqs#include <sys/systm.h>
60241675Suqs#include <sys/vnode.h>
61241675Suqs#include <vm/uma.h>
62241675Suqs
63241675Suqs#include <geom/geom.h>
64241675Suqs
65241675Suqs#include <machine/stdarg.h>
66241675Suqs
67241675Suqs#include <security/audit/audit.h>
68241675Suqs#include <security/mac/mac_framework.h>
69241675Suqs
70241675Suqs#include "opt_rootdevname.h"
71241675Suqs#include "opt_ddb.h"
72241675Suqs#include "opt_mac.h"
73241675Suqs
74241675Suqs#ifdef DDB
75241675Suqs#include <ddb/ddb.h>
76241675Suqs#endif
77241675Suqs
78241675Suqs#define	ROOTNAME		"root_device"
79241675Suqs#define	VFS_MOUNTARG_SIZE_MAX	(1024 * 64)
80241675Suqs
81241675Suqsstatic int	vfs_domount(struct thread *td, const char *fstype,
82241675Suqs		    char *fspath, int fsflags, void *fsdata);
83241675Suqsstatic int	vfs_mountroot_ask(void);
84241675Suqsstatic int	vfs_mountroot_try(const char *mountfrom);
85241675Suqsstatic int	vfs_donmount(struct thread *td, int fsflags,
86241675Suqs		    struct uio *fsoptions);
87241675Suqsstatic void	free_mntarg(struct mntarg *ma);
88241675Suqsstatic int	vfs_getopt_pos(struct vfsoptlist *opts, const char *name);
89241675Suqs
90241675Suqsstatic int	usermount = 0;
91241675SuqsSYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
92241675Suqs    "Unprivileged users may mount and unmount file systems");
93241675Suqs
94241675SuqsMALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
95241675SuqsMALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
96241675Suqsstatic uma_zone_t mount_zone;
97241675Suqs
98241675Suqs/* List of mounted filesystems. */
99241675Suqsstruct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
100241675Suqs
101241675Suqs/* For any iteration/modification of mountlist */
102241675Suqsstruct mtx mountlist_mtx;
103241675SuqsMTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
104241675Suqs
105241675SuqsTAILQ_HEAD(vfsoptlist, vfsopt);
106241675Suqsstruct vfsopt {
107241675Suqs	TAILQ_ENTRY(vfsopt) link;
108241675Suqs	char	*name;
109241675Suqs	void	*value;
110241675Suqs	int	len;
111241675Suqs};
112241675Suqs
113241675Suqs/*
114241675Suqs * The vnode of the system's root (/ in the filesystem, without chroot
115241675Suqs * active.)
116241675Suqs */
117241675Suqsstruct vnode	*rootvnode;
118241675Suqs
119241675Suqs/*
120241675Suqs * The root filesystem is detailed in the kernel environment variable
121241675Suqs * vfs.root.mountfrom, which is expected to be in the general format
122241675Suqs *
123241675Suqs * <vfsname>:[<path>]
124241675Suqs * vfsname   := the name of a VFS known to the kernel and capable
125241675Suqs *              of being mounted as root
126241675Suqs * path      := disk device name or other data used by the filesystem
127241675Suqs *              to locate its physical store
128241675Suqs */
129241675Suqs
130241675Suqs/*
131241675Suqs * Global opts, taken by all filesystems
132241675Suqs */
133241675Suqsstatic const char *global_opts[] = {
134241675Suqs	"errmsg",
135241675Suqs	"fstype",
136241675Suqs	"fspath",
137241675Suqs	"ro",
138241675Suqs	"rw",
139241675Suqs	"nosuid",
140241675Suqs	"noexec",
141241675Suqs	"update",
142241675Suqs	NULL
143241675Suqs};
144241675Suqs
145241675Suqs/*
146241675Suqs * The root specifiers we will try if RB_CDROM is specified.
147241675Suqs */
148241675Suqsstatic char *cdrom_rootdevnames[] = {
149241675Suqs	"cd9660:cd0",
150241675Suqs	"cd9660:acd0",
151241675Suqs	NULL
152241675Suqs};
153241675Suqs
154241675Suqs/* legacy find-root code */
155241675Suqschar		*rootdevnames[2] = {NULL, NULL};
156241675Suqs#ifndef ROOTDEVNAME
157241675Suqs#  define ROOTDEVNAME NULL
158241675Suqs#endif
159241675Suqsstatic const char	*ctrootdevname = ROOTDEVNAME;
160241675Suqs
161241675Suqs/*
162241675Suqs * ---------------------------------------------------------------------
163241675Suqs * Functions for building and sanitizing the mount options
164241675Suqs */
165241675Suqs
166241675Suqs/* Remove one mount option. */
167241675Suqsstatic void
168241675Suqsvfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
169241675Suqs{
170241675Suqs
171241675Suqs	TAILQ_REMOVE(opts, opt, link);
172241675Suqs	free(opt->name, M_MOUNT);
173241675Suqs	if (opt->value != NULL)
174241675Suqs		free(opt->value, M_MOUNT);
175241675Suqs#ifdef INVARIANTS
176241675Suqs	else if (opt->len != 0)
177241675Suqs		panic("%s: mount option with NULL value but length != 0",
178241675Suqs		    __func__);
179241675Suqs#endif
180241675Suqs	free(opt, M_MOUNT);
181241675Suqs}
182241675Suqs
183241675Suqs/* Release all resources related to the mount options. */
184241675Suqsvoid
185241675Suqsvfs_freeopts(struct vfsoptlist *opts)
186241675Suqs{
187241675Suqs	struct vfsopt *opt;
188241675Suqs
189241675Suqs	while (!TAILQ_EMPTY(opts)) {
190241675Suqs		opt = TAILQ_FIRST(opts);
191241675Suqs		vfs_freeopt(opts, opt);
192241675Suqs	}
193241675Suqs	free(opts, M_MOUNT);
194241675Suqs}
195241675Suqs
196241675Suqsvoid
197241675Suqsvfs_deleteopt(struct vfsoptlist *opts, const char *name)
198241675Suqs{
199241675Suqs	struct vfsopt *opt, *temp;
200241675Suqs
201241675Suqs	TAILQ_FOREACH_SAFE(opt, opts, link, temp)  {
202241675Suqs		if (strcmp(opt->name, name) == 0)
203241675Suqs			vfs_freeopt(opts, opt);
204241675Suqs	}
205241675Suqs}
206241675Suqs
207241675Suqs/*
208241675Suqs * Check if options are equal (with or without the "no" prefix).
209241675Suqs */
210241675Suqsstatic int
211241675Suqsvfs_equalopts(const char *opt1, const char *opt2)
212241675Suqs{
213241675Suqs
214241675Suqs	/* "opt" vs. "opt" or "noopt" vs. "noopt" */
215241675Suqs	if (strcmp(opt1, opt2) == 0)
216241675Suqs		return (1);
217241675Suqs	/* "noopt" vs. "opt" */
218241675Suqs	if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
219241675Suqs		return (1);
220241675Suqs	/* "opt" vs. "noopt" */
221241675Suqs	if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
222241675Suqs		return (1);
223241675Suqs	return (0);
224241675Suqs}
225241675Suqs
226241675Suqs/*
227241675Suqs * If a mount option is specified several times,
228241675Suqs * (with or without the "no" prefix) only keep
229241675Suqs * the last occurence of it.
230241675Suqs */
231241675Suqsstatic void
232241675Suqsvfs_sanitizeopts(struct vfsoptlist *opts)
233241675Suqs{
234241675Suqs	struct vfsopt *opt, *opt2, *tmp;
235241675Suqs
236241675Suqs	TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
237241675Suqs		opt2 = TAILQ_PREV(opt, vfsoptlist, link);
238241675Suqs		while (opt2 != NULL) {
239241675Suqs			if (vfs_equalopts(opt->name, opt2->name)) {
240241675Suqs				tmp = TAILQ_PREV(opt2, vfsoptlist, link);
241241675Suqs				vfs_freeopt(opts, opt2);
242241675Suqs				opt2 = tmp;
243241675Suqs			} else {
244241675Suqs				opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
245241675Suqs			}
246241675Suqs		}
247241675Suqs	}
248241675Suqs}
249241675Suqs
250241675Suqs/*
251241675Suqs * Build a linked list of mount options from a struct uio.
252241675Suqs */
253241675Suqsstatic int
254241675Suqsvfs_buildopts(struct uio *auio, struct vfsoptlist **options)
255241675Suqs{
256241675Suqs	struct vfsoptlist *opts;
257241675Suqs	struct vfsopt *opt;
258241675Suqs	size_t memused;
259241675Suqs	unsigned int i, iovcnt;
260241675Suqs	int error, namelen, optlen;
261241675Suqs
262241675Suqs	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
263241675Suqs	TAILQ_INIT(opts);
264241675Suqs	memused = 0;
265241675Suqs	iovcnt = auio->uio_iovcnt;
266241675Suqs	for (i = 0; i < iovcnt; i += 2) {
267241675Suqs		opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
268241675Suqs		namelen = auio->uio_iov[i].iov_len;
269241675Suqs		optlen = auio->uio_iov[i + 1].iov_len;
270241675Suqs		opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
271241675Suqs		opt->value = NULL;
272241675Suqs		opt->len = 0;
273241675Suqs
274241675Suqs		/*
275241675Suqs		 * Do this early, so jumps to "bad" will free the current
276241675Suqs		 * option.
277241675Suqs		 */
278241675Suqs		TAILQ_INSERT_TAIL(opts, opt, link);
279241675Suqs		memused += sizeof(struct vfsopt) + optlen + namelen;
280241675Suqs
281241675Suqs		/*
282241675Suqs		 * Avoid consuming too much memory, and attempts to overflow
283241675Suqs		 * memused.
284241675Suqs		 */
285241675Suqs		if (memused > VFS_MOUNTARG_SIZE_MAX ||
286241675Suqs		    optlen > VFS_MOUNTARG_SIZE_MAX ||
287241675Suqs		    namelen > VFS_MOUNTARG_SIZE_MAX) {
288241675Suqs			error = EINVAL;
289241675Suqs			goto bad;
290241675Suqs		}
291241675Suqs
292241675Suqs		if (auio->uio_segflg == UIO_SYSSPACE) {
293241675Suqs			bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
294241675Suqs		} else {
295241675Suqs			error = copyin(auio->uio_iov[i].iov_base, opt->name,
296241675Suqs			    namelen);
297241675Suqs			if (error)
298241675Suqs				goto bad;
299241675Suqs		}
300241675Suqs		/* Ensure names are null-terminated strings. */
301241675Suqs		if (opt->name[namelen - 1] != '\0') {
302241675Suqs			error = EINVAL;
303241675Suqs			goto bad;
304241675Suqs		}
305241675Suqs		if (optlen != 0) {
306241675Suqs			opt->len = optlen;
307241675Suqs			opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
308241675Suqs			if (auio->uio_segflg == UIO_SYSSPACE) {
309241675Suqs				bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
310241675Suqs				    optlen);
311241675Suqs			} else {
312241675Suqs				error = copyin(auio->uio_iov[i + 1].iov_base,
313241675Suqs				    opt->value, optlen);
314241675Suqs				if (error)
315241675Suqs					goto bad;
316241675Suqs			}
317241675Suqs		}
318241675Suqs	}
319241675Suqs	vfs_sanitizeopts(opts);
320241675Suqs	*options = opts;
321241675Suqs	return (0);
322241675Suqsbad:
323241675Suqs	vfs_freeopts(opts);
324241675Suqs	return (error);
325241675Suqs}
326241675Suqs
327241675Suqs/*
328241675Suqs * Merge the old mount options with the new ones passed
329241675Suqs * in the MNT_UPDATE case.
330241675Suqs */
331241675Suqsstatic void
332241675Suqsvfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
333241675Suqs{
334241675Suqs	struct vfsopt *opt, *opt2, *new;
335241675Suqs
336241675Suqs	TAILQ_FOREACH(opt, opts, link) {
337241675Suqs		/*
338241675Suqs		 * Check that this option hasn't been redefined
339241675Suqs		 * nor cancelled with a "no" mount option.
340241675Suqs		 */
341241675Suqs		opt2 = TAILQ_FIRST(toopts);
342241675Suqs		while (opt2 != NULL) {
343241675Suqs			if (strcmp(opt2->name, opt->name) == 0)
344241675Suqs				goto next;
345241675Suqs			if (strncmp(opt2->name, "no", 2) == 0 &&
346241675Suqs			    strcmp(opt2->name + 2, opt->name) == 0) {
347241675Suqs				vfs_freeopt(toopts, opt2);
348241675Suqs				goto next;
349241675Suqs			}
350241675Suqs			opt2 = TAILQ_NEXT(opt2, link);
351241675Suqs		}
352241675Suqs		/* We want this option, duplicate it. */
353241675Suqs		new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
354241675Suqs		new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
355241675Suqs		strcpy(new->name, opt->name);
356241675Suqs		if (opt->len != 0) {
357241675Suqs			new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
358241675Suqs			bcopy(opt->value, new->value, opt->len);
359241675Suqs		} else {
360241675Suqs			new->value = NULL;
361241675Suqs		}
362241675Suqs		new->len = opt->len;
363241675Suqs		TAILQ_INSERT_TAIL(toopts, new, link);
364241675Suqsnext:
365241675Suqs		continue;
366241675Suqs	}
367241675Suqs}
368241675Suqs
369241675Suqs/*
370241675Suqs * Mount a filesystem.
371241675Suqs */
372241675Suqsint
373241675Suqsnmount(td, uap)
374241675Suqs	struct thread *td;
375241675Suqs	struct nmount_args /* {
376241675Suqs		struct iovec *iovp;
377241675Suqs		unsigned int iovcnt;
378241675Suqs		int flags;
379241675Suqs	} */ *uap;
380241675Suqs{
381241675Suqs	struct uio *auio;
382241675Suqs	struct iovec *iov;
383241675Suqs	unsigned int i;
384241675Suqs	int error;
385241675Suqs	u_int iovcnt;
386241675Suqs
387241675Suqs	AUDIT_ARG(fflags, uap->flags);
388241675Suqs
389241675Suqs	/*
390241675Suqs	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
391241675Suqs	 * userspace to set this flag, but we must filter it out if we want
392241675Suqs	 * MNT_UPDATE on the root file system to work.
393241675Suqs	 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
394241675Suqs	 */
395241675Suqs	uap->flags &= ~MNT_ROOTFS;
396241675Suqs
397241675Suqs	iovcnt = uap->iovcnt;
398241675Suqs	/*
399241675Suqs	 * Check that we have an even number of iovec's
400241675Suqs	 * and that we have at least two options.
401241675Suqs	 */
402241675Suqs	if ((iovcnt & 1) || (iovcnt < 4))
403241675Suqs		return (EINVAL);
404241675Suqs
405241675Suqs	error = copyinuio(uap->iovp, iovcnt, &auio);
406241675Suqs	if (error)
407241675Suqs		return (error);
408241675Suqs	iov = auio->uio_iov;
409241675Suqs	for (i = 0; i < iovcnt; i++) {
410241675Suqs		if (iov->iov_len > MMAXOPTIONLEN) {
411241675Suqs			free(auio, M_IOV);
412241675Suqs			return (EINVAL);
413241675Suqs		}
414241675Suqs		iov++;
415241675Suqs	}
416241675Suqs	error = vfs_donmount(td, uap->flags, auio);
417241675Suqs
418241675Suqs	free(auio, M_IOV);
419241675Suqs	return (error);
420241675Suqs}
421241675Suqs
422241675Suqs/*
423241675Suqs * ---------------------------------------------------------------------
424241675Suqs * Various utility functions
425241675Suqs */
426241675Suqs
427241675Suqsvoid
428241675Suqsvfs_ref(struct mount *mp)
429241675Suqs{
430241675Suqs
431241675Suqs	MNT_ILOCK(mp);
432241675Suqs	MNT_REF(mp);
433241675Suqs	MNT_IUNLOCK(mp);
434241675Suqs}
435241675Suqs
436241675Suqsvoid
437241675Suqsvfs_rel(struct mount *mp)
438241675Suqs{
439241675Suqs
440241675Suqs	MNT_ILOCK(mp);
441241675Suqs	MNT_REL(mp);
442241675Suqs	MNT_IUNLOCK(mp);
443241675Suqs}
444241675Suqs
445241675Suqsstatic int
446241675Suqsmount_init(void *mem, int size, int flags)
447241675Suqs{
448241675Suqs	struct mount *mp;
449241675Suqs
450241675Suqs	mp = (struct mount *)mem;
451241675Suqs	mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
452241675Suqs	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
453241675Suqs	return (0);
454241675Suqs}
455241675Suqs
456241675Suqsstatic void
457241675Suqsmount_fini(void *mem, int size)
458241675Suqs{
459241675Suqs	struct mount *mp;
460241675Suqs
461241675Suqs	mp = (struct mount *)mem;
462241675Suqs	lockdestroy(&mp->mnt_lock);
463241675Suqs	mtx_destroy(&mp->mnt_mtx);
464241675Suqs}
465241675Suqs
466241675Suqs/*
467241675Suqs * Allocate and initialize the mount point struct.
468241675Suqs */
469241675Suqsstruct mount *
470241675Suqsvfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
471241675Suqs    const char *fspath, struct thread *td)
472241675Suqs{
473241675Suqs	struct mount *mp;
474241675Suqs
475241675Suqs	mp = uma_zalloc(mount_zone, M_WAITOK);
476241675Suqs	bzero(&mp->mnt_startzero,
477241675Suqs	    __rangeof(struct mount, mnt_startzero, mnt_endzero));
478241675Suqs	TAILQ_INIT(&mp->mnt_nvnodelist);
479241675Suqs	mp->mnt_nvnodelistsize = 0;
480241675Suqs	mp->mnt_ref = 0;
481241675Suqs	(void) vfs_busy(mp, LK_NOWAIT, 0, td);
482241675Suqs	mp->mnt_op = vfsp->vfc_vfsops;
483241675Suqs	mp->mnt_vfc = vfsp;
484241675Suqs	vfsp->vfc_refcount++;	/* XXX Unlocked */
485241675Suqs	mp->mnt_stat.f_type = vfsp->vfc_typenum;
486241675Suqs	mp->mnt_gen++;
487241675Suqs	strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
488241675Suqs	mp->mnt_vnodecovered = vp;
489241675Suqs	mp->mnt_cred = crdup(td->td_ucred);
490241675Suqs	mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
491241675Suqs	strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
492241675Suqs	mp->mnt_iosize_max = DFLTPHYS;
493241675Suqs#ifdef MAC
494241675Suqs	mac_mount_init(mp);
495241675Suqs	mac_mount_create(td->td_ucred, mp);
496241675Suqs#endif
497241675Suqs	arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
498241675Suqs	return (mp);
499241675Suqs}
500241675Suqs
501241675Suqs/*
502241675Suqs * Destroy the mount struct previously allocated by vfs_mount_alloc().
503241675Suqs */
504241675Suqsvoid
505241675Suqsvfs_mount_destroy(struct mount *mp)
506241675Suqs{
507241675Suqs	int i;
508241675Suqs
509241675Suqs	MNT_ILOCK(mp);
510241675Suqs	for (i = 0; mp->mnt_ref && i < 3; i++)
511241675Suqs		msleep(mp, MNT_MTX(mp), PVFS, "mntref", hz);
512241675Suqs	/*
513241675Suqs	 * This will always cause a 3 second delay in rebooting due to
514241675Suqs	 * refs on the root mountpoint that never go away.  Most of these
515241675Suqs	 * are held by init which never exits.
516241675Suqs	 */
517241675Suqs	if (i == 3 && (!rebooting || bootverbose))
518241675Suqs		printf("Mount point %s had %d dangling refs\n",
519241675Suqs		    mp->mnt_stat.f_mntonname, mp->mnt_ref);
520241675Suqs	if (mp->mnt_holdcnt != 0) {
521241675Suqs		printf("Waiting for mount point to be unheld\n");
522241675Suqs		while (mp->mnt_holdcnt != 0) {
523241675Suqs			mp->mnt_holdcntwaiters++;
524241675Suqs			msleep(&mp->mnt_holdcnt, MNT_MTX(mp),
525241675Suqs			       PZERO, "mntdestroy", 0);
526241675Suqs			mp->mnt_holdcntwaiters--;
527241675Suqs		}
528241675Suqs		printf("mount point unheld\n");
529241675Suqs	}
530241675Suqs	if (mp->mnt_writeopcount > 0) {
531241675Suqs		printf("Waiting for mount point write ops\n");
532241675Suqs		while (mp->mnt_writeopcount > 0) {
533241675Suqs			mp->mnt_kern_flag |= MNTK_SUSPEND;
534241675Suqs			msleep(&mp->mnt_writeopcount,
535241675Suqs			       MNT_MTX(mp),
536241675Suqs			       PZERO, "mntdestroy2", 0);
537241675Suqs		}
538241675Suqs		printf("mount point write ops completed\n");
539241675Suqs	}
540241675Suqs	if (mp->mnt_secondary_writes > 0) {
541241675Suqs		printf("Waiting for mount point secondary write ops\n");
542241675Suqs		while (mp->mnt_secondary_writes > 0) {
543241675Suqs			mp->mnt_kern_flag |= MNTK_SUSPEND;
544241675Suqs			msleep(&mp->mnt_secondary_writes,
545241675Suqs			       MNT_MTX(mp),
546241675Suqs			       PZERO, "mntdestroy3", 0);
547241675Suqs		}
548241675Suqs		printf("mount point secondary write ops completed\n");
549241675Suqs	}
550241675Suqs	MNT_IUNLOCK(mp);
551241675Suqs	mp->mnt_vfc->vfc_refcount--;
552241675Suqs	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
553241675Suqs		struct vnode *vp;
554241675Suqs
555241675Suqs		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
556241675Suqs			vprint("", vp);
557241675Suqs		panic("unmount: dangling vnode");
558241675Suqs	}
559241675Suqs	MNT_ILOCK(mp);
560241675Suqs	if (mp->mnt_kern_flag & MNTK_MWAIT)
561241675Suqs		wakeup(mp);
562241675Suqs	if (mp->mnt_writeopcount != 0)
563241675Suqs		panic("vfs_mount_destroy: nonzero writeopcount");
564241675Suqs	if (mp->mnt_secondary_writes != 0)
565241675Suqs		panic("vfs_mount_destroy: nonzero secondary_writes");
566241675Suqs	if (mp->mnt_nvnodelistsize != 0)
567241675Suqs		panic("vfs_mount_destroy: nonzero nvnodelistsize");
568241675Suqs	mp->mnt_writeopcount = -1000;
569241675Suqs	mp->mnt_nvnodelistsize = -1000;
570241675Suqs	mp->mnt_secondary_writes = -1000;
571241675Suqs	MNT_IUNLOCK(mp);
572241675Suqs#ifdef MAC
573241675Suqs	mac_mount_destroy(mp);
574241675Suqs#endif
575241675Suqs	if (mp->mnt_opt != NULL)
576241675Suqs		vfs_freeopts(mp->mnt_opt);
577241675Suqs	crfree(mp->mnt_cred);
578241675Suqs	uma_zfree(mount_zone, mp);
579241675Suqs}
580241675Suqs
581241675Suqsstatic int
582241675Suqsvfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
583241675Suqs{
584241675Suqs	struct vfsoptlist *optlist;
585241675Suqs	struct vfsopt *opt, *noro_opt;
586241675Suqs	char *fstype, *fspath, *errmsg;
587241675Suqs	int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
588241675Suqs	int has_rw, has_noro;
589241675Suqs
590241675Suqs	errmsg = NULL;
591241675Suqs	errmsg_len = 0;
592241675Suqs	errmsg_pos = -1;
593241675Suqs	has_rw = 0;
594241675Suqs	has_noro = 0;
595241675Suqs
596241675Suqs	error = vfs_buildopts(fsoptions, &optlist);
597241675Suqs	if (error)
598241675Suqs		return (error);
599241675Suqs
600241675Suqs	if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
601241675Suqs		errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
602241675Suqs
603241675Suqs	/*
604241675Suqs	 * We need these two options before the others,
605241675Suqs	 * and they are mandatory for any filesystem.
606241675Suqs	 * Ensure they are NUL terminated as well.
607241675Suqs	 */
608241675Suqs	fstypelen = 0;
609241675Suqs	error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
610241675Suqs	if (error || fstype[fstypelen - 1] != '\0') {
611241675Suqs		error = EINVAL;
612241675Suqs		if (errmsg != NULL)
613241675Suqs			strncpy(errmsg, "Invalid fstype", errmsg_len);
614241675Suqs		goto bail;
615241675Suqs	}
616241675Suqs	fspathlen = 0;
617241675Suqs	error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
618241675Suqs	if (error || fspath[fspathlen - 1] != '\0') {
619241675Suqs		error = EINVAL;
620241675Suqs		if (errmsg != NULL)
621241675Suqs			strncpy(errmsg, "Invalid fspath", errmsg_len);
622241675Suqs		goto bail;
623241675Suqs	}
624241675Suqs
625241675Suqs	/*
626241675Suqs	 * We need to see if we have the "update" option
627241675Suqs	 * before we call vfs_domount(), since vfs_domount() has special
628241675Suqs	 * logic based on MNT_UPDATE.  This is very important
629241675Suqs	 * when we want to update the root filesystem.
630241675Suqs	 */
631241675Suqs	TAILQ_FOREACH(opt, optlist, link) {
632241675Suqs		if (strcmp(opt->name, "update") == 0)
633241675Suqs			fsflags |= MNT_UPDATE;
634241675Suqs		else if (strcmp(opt->name, "async") == 0)
635241675Suqs			fsflags |= MNT_ASYNC;
636241675Suqs		else if (strcmp(opt->name, "force") == 0)
637241675Suqs			fsflags |= MNT_FORCE;
638241675Suqs		else if (strcmp(opt->name, "multilabel") == 0)
639241675Suqs			fsflags |= MNT_MULTILABEL;
640241675Suqs		else if (strcmp(opt->name, "noasync") == 0)
641241675Suqs			fsflags &= ~MNT_ASYNC;
642241675Suqs		else if (strcmp(opt->name, "noatime") == 0)
643241675Suqs			fsflags |= MNT_NOATIME;
644241675Suqs		else if (strcmp(opt->name, "atime") == 0) {
645241675Suqs			free(opt->name, M_MOUNT);
646241675Suqs			opt->name = strdup("nonoatime", M_MOUNT);
647241675Suqs		}
648241675Suqs		else if (strcmp(opt->name, "noclusterr") == 0)
649241675Suqs			fsflags |= MNT_NOCLUSTERR;
650241675Suqs		else if (strcmp(opt->name, "clusterr") == 0) {
651241675Suqs			free(opt->name, M_MOUNT);
652241675Suqs			opt->name = strdup("nonoclusterr", M_MOUNT);
653241675Suqs		}
654241675Suqs		else if (strcmp(opt->name, "noclusterw") == 0)
655241675Suqs			fsflags |= MNT_NOCLUSTERW;
656241675Suqs		else if (strcmp(opt->name, "clusterw") == 0) {
657241675Suqs			free(opt->name, M_MOUNT);
658241675Suqs			opt->name = strdup("nonoclusterw", M_MOUNT);
659241675Suqs		}
660241675Suqs		else if (strcmp(opt->name, "noexec") == 0)
661241675Suqs			fsflags |= MNT_NOEXEC;
662241675Suqs		else if (strcmp(opt->name, "exec") == 0) {
663241675Suqs			free(opt->name, M_MOUNT);
664241675Suqs			opt->name = strdup("nonoexec", M_MOUNT);
665241675Suqs		}
666241675Suqs		else if (strcmp(opt->name, "nosuid") == 0)
667241675Suqs			fsflags |= MNT_NOSUID;
668241675Suqs		else if (strcmp(opt->name, "suid") == 0) {
669241675Suqs			free(opt->name, M_MOUNT);
670241675Suqs			opt->name = strdup("nonosuid", M_MOUNT);
671241675Suqs		}
672241675Suqs		else if (strcmp(opt->name, "nosymfollow") == 0)
673241675Suqs			fsflags |= MNT_NOSYMFOLLOW;
674241675Suqs		else if (strcmp(opt->name, "symfollow") == 0) {
675241675Suqs			free(opt->name, M_MOUNT);
676241675Suqs			opt->name = strdup("nonosymfollow", M_MOUNT);
677241675Suqs		}
678241675Suqs		else if (strcmp(opt->name, "noro") == 0) {
679241675Suqs			fsflags &= ~MNT_RDONLY;
680241675Suqs			has_noro = 1;
681241675Suqs		}
682241675Suqs		else if (strcmp(opt->name, "rw") == 0) {
683241675Suqs			fsflags &= ~MNT_RDONLY;
684241675Suqs			has_rw = 1;
685241675Suqs		}
686241675Suqs		else if (strcmp(opt->name, "ro") == 0)
687241675Suqs			fsflags |= MNT_RDONLY;
688241675Suqs		else if (strcmp(opt->name, "rdonly") == 0) {
689241675Suqs			free(opt->name, M_MOUNT);
690241675Suqs			opt->name = strdup("ro", M_MOUNT);
691241675Suqs			fsflags |= MNT_RDONLY;
692241675Suqs		}
693241675Suqs		else if (strcmp(opt->name, "snapshot") == 0)
694241675Suqs			fsflags |= MNT_SNAPSHOT;
695241675Suqs		else if (strcmp(opt->name, "suiddir") == 0)
696241675Suqs			fsflags |= MNT_SUIDDIR;
697241675Suqs		else if (strcmp(opt->name, "sync") == 0)
698241675Suqs			fsflags |= MNT_SYNCHRONOUS;
699241675Suqs		else if (strcmp(opt->name, "union") == 0)
700241675Suqs			fsflags |= MNT_UNION;
701241675Suqs	}
702241675Suqs
703241675Suqs	/*
704241675Suqs	 * If "rw" was specified as a mount option, and we
705241675Suqs	 * are trying to update a mount-point from "ro" to "rw",
706241675Suqs	 * we need a mount option "noro", since in vfs_mergeopts(),
707241675Suqs	 * "noro" will cancel "ro", but "rw" will not do anything.
708241675Suqs	 */
709241675Suqs	if (has_rw && !has_noro) {
710241675Suqs		noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
711241675Suqs		noro_opt->name = strdup("noro", M_MOUNT);
712241675Suqs		noro_opt->value = NULL;
713241675Suqs		noro_opt->len = 0;
714241675Suqs		TAILQ_INSERT_TAIL(optlist, noro_opt, link);
715241675Suqs	}
716241675Suqs
717241675Suqs	/*
718241675Suqs	 * Be ultra-paranoid about making sure the type and fspath
719241675Suqs	 * variables will fit in our mp buffers, including the
720241675Suqs	 * terminating NUL.
721241675Suqs	 */
722241675Suqs	if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
723241675Suqs		error = ENAMETOOLONG;
724241675Suqs		goto bail;
725241675Suqs	}
726241675Suqs
727241675Suqs	mtx_lock(&Giant);
728241675Suqs	error = vfs_domount(td, fstype, fspath, fsflags, optlist);
729241675Suqs	mtx_unlock(&Giant);
730241675Suqsbail:
731241675Suqs	/* copyout the errmsg */
732241675Suqs	if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
733241675Suqs	    && errmsg_len > 0 && errmsg != NULL) {
734241675Suqs		if (fsoptions->uio_segflg == UIO_SYSSPACE) {
735241675Suqs			bcopy(errmsg,
736			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
737			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
738		} else {
739			copyout(errmsg,
740			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
741			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
742		}
743	}
744
745	if (error != 0)
746		vfs_freeopts(optlist);
747	return (error);
748}
749
750/*
751 * Old mount API.
752 */
753#ifndef _SYS_SYSPROTO_H_
754struct mount_args {
755	char	*type;
756	char	*path;
757	int	flags;
758	caddr_t	data;
759};
760#endif
761/* ARGSUSED */
762int
763mount(td, uap)
764	struct thread *td;
765	struct mount_args /* {
766		char *type;
767		char *path;
768		int flags;
769		caddr_t data;
770	} */ *uap;
771{
772	char *fstype;
773	struct vfsconf *vfsp = NULL;
774	struct mntarg *ma = NULL;
775	int error;
776
777	AUDIT_ARG(fflags, uap->flags);
778
779	/*
780	 * Filter out MNT_ROOTFS.  We do not want clients of mount() in
781	 * userspace to set this flag, but we must filter it out if we want
782	 * MNT_UPDATE on the root file system to work.
783	 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
784	 */
785	uap->flags &= ~MNT_ROOTFS;
786
787	fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
788	error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
789	if (error) {
790		free(fstype, M_TEMP);
791		return (error);
792	}
793
794	AUDIT_ARG(text, fstype);
795	mtx_lock(&Giant);
796	vfsp = vfs_byname_kld(fstype, td, &error);
797	free(fstype, M_TEMP);
798	if (vfsp == NULL) {
799		mtx_unlock(&Giant);
800		return (ENOENT);
801	}
802	if (vfsp->vfc_vfsops->vfs_cmount == NULL) {
803		mtx_unlock(&Giant);
804		return (EOPNOTSUPP);
805	}
806
807	ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
808	ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
809	ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
810	ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
811	ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
812
813	error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
814	mtx_unlock(&Giant);
815	return (error);
816}
817
818
819/*
820 * vfs_domount(): actually attempt a filesystem mount.
821 */
822static int
823vfs_domount(
824	struct thread *td,	/* Calling thread. */
825	const char *fstype,	/* Filesystem type. */
826	char *fspath,		/* Mount path. */
827	int fsflags,		/* Flags common to all filesystems. */
828	void *fsdata		/* Options local to the filesystem. */
829	)
830{
831	struct vnode *vp;
832	struct mount *mp;
833	struct vfsconf *vfsp;
834	struct export_args export;
835	int error, flag = 0;
836	struct vattr va;
837	struct nameidata nd;
838
839	mtx_assert(&Giant, MA_OWNED);
840	/*
841	 * Be ultra-paranoid about making sure the type and fspath
842	 * variables will fit in our mp buffers, including the
843	 * terminating NUL.
844	 */
845	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
846		return (ENAMETOOLONG);
847
848	if (jailed(td->td_ucred) || usermount == 0) {
849		if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
850			return (error);
851	}
852
853	/*
854	 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
855	 */
856	if (fsflags & MNT_EXPORTED) {
857		error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
858		if (error)
859			return (error);
860	}
861	if (fsflags & MNT_SUIDDIR) {
862		error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
863		if (error)
864			return (error);
865	}
866	/*
867	 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
868	 */
869	if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
870		if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
871			fsflags |= MNT_NOSUID | MNT_USER;
872	}
873
874	/* Load KLDs before we lock the covered vnode to avoid reversals. */
875	vfsp = NULL;
876	if ((fsflags & MNT_UPDATE) == 0) {
877		/* Don't try to load KLDs if we're mounting the root. */
878		if (fsflags & MNT_ROOTFS)
879			vfsp = vfs_byname(fstype);
880		else
881			vfsp = vfs_byname_kld(fstype, td, &error);
882		if (vfsp == NULL)
883			return (ENODEV);
884		if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
885			return (EPERM);
886	}
887	/*
888	 * Get vnode to be covered
889	 */
890	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
891	    fspath, td);
892	if ((error = namei(&nd)) != 0)
893		return (error);
894	NDFREE(&nd, NDF_ONLY_PNBUF);
895	vp = nd.ni_vp;
896	if (fsflags & MNT_UPDATE) {
897		if ((vp->v_vflag & VV_ROOT) == 0) {
898			vput(vp);
899			return (EINVAL);
900		}
901		mp = vp->v_mount;
902		MNT_ILOCK(mp);
903		flag = mp->mnt_flag;
904		/*
905		 * We only allow the filesystem to be reloaded if it
906		 * is currently mounted read-only.
907		 */
908		if ((fsflags & MNT_RELOAD) &&
909		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
910			MNT_IUNLOCK(mp);
911			vput(vp);
912			return (EOPNOTSUPP);	/* Needs translation */
913		}
914		MNT_IUNLOCK(mp);
915		/*
916		 * Only privileged root, or (if MNT_USER is set) the user that
917		 * did the original mount is permitted to update it.
918		 */
919		error = vfs_suser(mp, td);
920		if (error) {
921			vput(vp);
922			return (error);
923		}
924		if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
925			vput(vp);
926			return (EBUSY);
927		}
928		VI_LOCK(vp);
929		if ((vp->v_iflag & VI_MOUNT) != 0 ||
930		    vp->v_mountedhere != NULL) {
931			VI_UNLOCK(vp);
932			vfs_unbusy(mp, td);
933			vput(vp);
934			return (EBUSY);
935		}
936		vp->v_iflag |= VI_MOUNT;
937		VI_UNLOCK(vp);
938		MNT_ILOCK(mp);
939		mp->mnt_flag |= fsflags &
940		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
941		MNT_IUNLOCK(mp);
942		VOP_UNLOCK(vp, 0);
943		mp->mnt_optnew = fsdata;
944		vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
945	} else {
946		/*
947		 * If the user is not root, ensure that they own the directory
948		 * onto which we are attempting to mount.
949		 */
950		error = VOP_GETATTR(vp, &va, td->td_ucred, td);
951		if (error) {
952			vput(vp);
953			return (error);
954		}
955		if (va.va_uid != td->td_ucred->cr_uid) {
956			error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN,
957			    0);
958			if (error) {
959				vput(vp);
960				return (error);
961			}
962		}
963		error = vinvalbuf(vp, V_SAVE, td, 0, 0);
964		if (error != 0) {
965			vput(vp);
966			return (error);
967		}
968		if (vp->v_type != VDIR) {
969			vput(vp);
970			return (ENOTDIR);
971		}
972		VI_LOCK(vp);
973		if ((vp->v_iflag & VI_MOUNT) != 0 ||
974		    vp->v_mountedhere != NULL) {
975			VI_UNLOCK(vp);
976			vput(vp);
977			return (EBUSY);
978		}
979		vp->v_iflag |= VI_MOUNT;
980		VI_UNLOCK(vp);
981
982		/*
983		 * Allocate and initialize the filesystem.
984		 */
985		mp = vfs_mount_alloc(vp, vfsp, fspath, td);
986		VOP_UNLOCK(vp, 0);
987
988		/* XXXMAC: pass to vfs_mount_alloc? */
989		mp->mnt_optnew = fsdata;
990	}
991
992	/*
993	 * Set the mount level flags.
994	 */
995	MNT_ILOCK(mp);
996	mp->mnt_flag = (mp->mnt_flag & ~MNT_UPDATEMASK) |
997		(fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS |
998			    MNT_RDONLY));
999	if ((mp->mnt_flag & MNT_ASYNC) == 0)
1000		mp->mnt_kern_flag &= ~MNTK_ASYNC;
1001	MNT_IUNLOCK(mp);
1002	/*
1003	 * Mount the filesystem.
1004	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1005	 * get.  No freeing of cn_pnbuf.
1006	 */
1007        error = VFS_MOUNT(mp, td);
1008
1009	/*
1010	 * Process the export option only if we are
1011	 * updating mount options.
1012	 */
1013	if (!error && (fsflags & MNT_UPDATE)) {
1014		if (vfs_copyopt(mp->mnt_optnew, "export", &export,
1015		    sizeof(export)) == 0)
1016			error = vfs_export(mp, &export);
1017	}
1018
1019	if (!error) {
1020		if (mp->mnt_opt != NULL)
1021			vfs_freeopts(mp->mnt_opt);
1022		mp->mnt_opt = mp->mnt_optnew;
1023		(void)VFS_STATFS(mp, &mp->mnt_stat, td);
1024	}
1025	/*
1026	 * Prevent external consumers of mount options from reading
1027	 * mnt_optnew.
1028	*/
1029	mp->mnt_optnew = NULL;
1030	if (mp->mnt_flag & MNT_UPDATE) {
1031		MNT_ILOCK(mp);
1032		if (error)
1033			mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) |
1034				(flag & ~MNT_QUOTA);
1035		else
1036			mp->mnt_flag &=	~(MNT_UPDATE | MNT_RELOAD |
1037					  MNT_FORCE | MNT_SNAPSHOT);
1038		if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1039			mp->mnt_kern_flag |= MNTK_ASYNC;
1040		else
1041			mp->mnt_kern_flag &= ~MNTK_ASYNC;
1042		MNT_IUNLOCK(mp);
1043		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1044			if (mp->mnt_syncer == NULL)
1045				error = vfs_allocate_syncvnode(mp);
1046		} else {
1047			if (mp->mnt_syncer != NULL)
1048				vrele(mp->mnt_syncer);
1049			mp->mnt_syncer = NULL;
1050		}
1051		vfs_unbusy(mp, td);
1052		VI_LOCK(vp);
1053		vp->v_iflag &= ~VI_MOUNT;
1054		VI_UNLOCK(vp);
1055		vrele(vp);
1056		return (error);
1057	}
1058	MNT_ILOCK(mp);
1059	if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1060		mp->mnt_kern_flag |= MNTK_ASYNC;
1061	else
1062		mp->mnt_kern_flag &= ~MNTK_ASYNC;
1063	MNT_IUNLOCK(mp);
1064	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1065	/*
1066	 * Put the new filesystem on the mount list after root.
1067	 */
1068	cache_purge(vp);
1069	if (!error) {
1070		struct vnode *newdp;
1071
1072		VI_LOCK(vp);
1073		vp->v_iflag &= ~VI_MOUNT;
1074		VI_UNLOCK(vp);
1075		vp->v_mountedhere = mp;
1076		mtx_lock(&mountlist_mtx);
1077		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1078		mtx_unlock(&mountlist_mtx);
1079		vfs_event_signal(NULL, VQ_MOUNT, 0);
1080		if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
1081			panic("mount: lost mount");
1082		mountcheckdirs(vp, newdp);
1083		vput(newdp);
1084		VOP_UNLOCK(vp, 0);
1085		if ((mp->mnt_flag & MNT_RDONLY) == 0)
1086			error = vfs_allocate_syncvnode(mp);
1087		vfs_unbusy(mp, td);
1088		if (error)
1089			vrele(vp);
1090	} else {
1091		VI_LOCK(vp);
1092		vp->v_iflag &= ~VI_MOUNT;
1093		VI_UNLOCK(vp);
1094		vfs_unbusy(mp, td);
1095		vfs_mount_destroy(mp);
1096		vput(vp);
1097	}
1098	return (error);
1099}
1100
1101/*
1102 * Unmount a filesystem.
1103 *
1104 * Note: unmount takes a path to the vnode mounted on as argument, not
1105 * special file (as before).
1106 */
1107#ifndef _SYS_SYSPROTO_H_
1108struct unmount_args {
1109	char	*path;
1110	int	flags;
1111};
1112#endif
1113/* ARGSUSED */
1114int
1115unmount(td, uap)
1116	struct thread *td;
1117	register struct unmount_args /* {
1118		char *path;
1119		int flags;
1120	} */ *uap;
1121{
1122	struct mount *mp;
1123	char *pathbuf;
1124	int error, id0, id1;
1125
1126	if (jailed(td->td_ucred) || usermount == 0) {
1127		error = priv_check(td, PRIV_VFS_UNMOUNT);
1128		if (error)
1129			return (error);
1130	}
1131
1132	pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1133	error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1134	if (error) {
1135		free(pathbuf, M_TEMP);
1136		return (error);
1137	}
1138	AUDIT_ARG(upath, td, pathbuf, ARG_UPATH1);
1139	mtx_lock(&Giant);
1140	if (uap->flags & MNT_BYFSID) {
1141		/* Decode the filesystem ID. */
1142		if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1143			mtx_unlock(&Giant);
1144			free(pathbuf, M_TEMP);
1145			return (EINVAL);
1146		}
1147
1148		mtx_lock(&mountlist_mtx);
1149		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1150			if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1151			    mp->mnt_stat.f_fsid.val[1] == id1)
1152				break;
1153		}
1154		mtx_unlock(&mountlist_mtx);
1155	} else {
1156		mtx_lock(&mountlist_mtx);
1157		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1158			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1159				break;
1160		}
1161		mtx_unlock(&mountlist_mtx);
1162	}
1163	free(pathbuf, M_TEMP);
1164	if (mp == NULL) {
1165		/*
1166		 * Previously we returned ENOENT for a nonexistent path and
1167		 * EINVAL for a non-mountpoint.  We cannot tell these apart
1168		 * now, so in the !MNT_BYFSID case return the more likely
1169		 * EINVAL for compatibility.
1170		 */
1171		mtx_unlock(&Giant);
1172		return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1173	}
1174
1175	/*
1176	 * Don't allow unmounting the root filesystem.
1177	 */
1178	if (mp->mnt_flag & MNT_ROOTFS) {
1179		mtx_unlock(&Giant);
1180		return (EINVAL);
1181	}
1182	error = dounmount(mp, uap->flags, td);
1183	mtx_unlock(&Giant);
1184	return (error);
1185}
1186
1187/*
1188 * Do the actual filesystem unmount.
1189 */
1190int
1191dounmount(mp, flags, td)
1192	struct mount *mp;
1193	int flags;
1194	struct thread *td;
1195{
1196	struct vnode *coveredvp, *fsrootvp;
1197	int error;
1198	int async_flag;
1199	int mnt_gen_r;
1200
1201	mtx_assert(&Giant, MA_OWNED);
1202
1203	if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1204		mnt_gen_r = mp->mnt_gen;
1205		VI_LOCK(coveredvp);
1206		vholdl(coveredvp);
1207		vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1208		vdrop(coveredvp);
1209		/*
1210		 * Check for mp being unmounted while waiting for the
1211		 * covered vnode lock.
1212		 */
1213		if (coveredvp->v_mountedhere != mp ||
1214		    coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1215			VOP_UNLOCK(coveredvp, 0);
1216			return (EBUSY);
1217		}
1218	}
1219	/*
1220	 * Only privileged root, or (if MNT_USER is set) the user that did the
1221	 * original mount is permitted to unmount this filesystem.
1222	 */
1223	error = vfs_suser(mp, td);
1224	if (error) {
1225		if (coveredvp)
1226			VOP_UNLOCK(coveredvp, 0);
1227		return (error);
1228	}
1229
1230	MNT_ILOCK(mp);
1231	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1232		MNT_IUNLOCK(mp);
1233		if (coveredvp)
1234			VOP_UNLOCK(coveredvp, 0);
1235		return (EBUSY);
1236	}
1237	mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1238	/* Allow filesystems to detect that a forced unmount is in progress. */
1239	if (flags & MNT_FORCE)
1240		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1241	error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1242	    ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
1243	if (error) {
1244		MNT_ILOCK(mp);
1245		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_NOINSMNTQ |
1246		    MNTK_UNMOUNTF);
1247		if (mp->mnt_kern_flag & MNTK_MWAIT)
1248			wakeup(mp);
1249		MNT_IUNLOCK(mp);
1250		if (coveredvp)
1251			VOP_UNLOCK(coveredvp, 0);
1252		return (error);
1253	}
1254	vn_start_write(NULL, &mp, V_WAIT);
1255
1256	if (mp->mnt_flag & MNT_EXPUBLIC)
1257		vfs_setpublicfs(NULL, NULL, NULL);
1258
1259	vfs_msync(mp, MNT_WAIT);
1260	MNT_ILOCK(mp);
1261	async_flag = mp->mnt_flag & MNT_ASYNC;
1262	mp->mnt_flag &= ~MNT_ASYNC;
1263	mp->mnt_kern_flag &= ~MNTK_ASYNC;
1264	MNT_IUNLOCK(mp);
1265	cache_purgevfs(mp);	/* remove cache entries for this file sys */
1266	if (mp->mnt_syncer != NULL)
1267		vrele(mp->mnt_syncer);
1268	/*
1269	 * For forced unmounts, move process cdir/rdir refs on the fs root
1270	 * vnode to the covered vnode.  For non-forced unmounts we want
1271	 * such references to cause an EBUSY error.
1272	 */
1273	if ((flags & MNT_FORCE) &&
1274	    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1275		if (mp->mnt_vnodecovered != NULL)
1276			mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1277		if (fsrootvp == rootvnode) {
1278			vrele(rootvnode);
1279			rootvnode = NULL;
1280		}
1281		vput(fsrootvp);
1282	}
1283	if (((mp->mnt_flag & MNT_RDONLY) ||
1284	     (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
1285	    (flags & MNT_FORCE)) {
1286		error = VFS_UNMOUNT(mp, flags, td);
1287	}
1288	vn_finished_write(mp);
1289	/*
1290	 * If we failed to flush the dirty blocks for this mount point,
1291	 * undo all the cdir/rdir and rootvnode changes we made above.
1292	 * Unless we failed to do so because the device is reporting that
1293	 * it doesn't exist anymore.
1294	 */
1295	if (error && error != ENXIO) {
1296		if ((flags & MNT_FORCE) &&
1297		    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1298			if (mp->mnt_vnodecovered != NULL)
1299				mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1300			if (rootvnode == NULL) {
1301				rootvnode = fsrootvp;
1302				vref(rootvnode);
1303			}
1304			vput(fsrootvp);
1305		}
1306		MNT_ILOCK(mp);
1307		mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1308		if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) {
1309			MNT_IUNLOCK(mp);
1310			(void) vfs_allocate_syncvnode(mp);
1311			MNT_ILOCK(mp);
1312		}
1313		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1314		mp->mnt_flag |= async_flag;
1315		if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1316			mp->mnt_kern_flag |= MNTK_ASYNC;
1317		lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1318		if (mp->mnt_kern_flag & MNTK_MWAIT)
1319			wakeup(mp);
1320		MNT_IUNLOCK(mp);
1321		if (coveredvp)
1322			VOP_UNLOCK(coveredvp, 0);
1323		return (error);
1324	}
1325	mtx_lock(&mountlist_mtx);
1326	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1327	mtx_unlock(&mountlist_mtx);
1328	if (coveredvp != NULL) {
1329		coveredvp->v_mountedhere = NULL;
1330		vput(coveredvp);
1331	}
1332	vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1333	lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1334	vfs_mount_destroy(mp);
1335	return (0);
1336}
1337
1338/*
1339 * ---------------------------------------------------------------------
1340 * Mounting of root filesystem
1341 *
1342 */
1343
1344struct root_hold_token {
1345	const char			*who;
1346	LIST_ENTRY(root_hold_token)	list;
1347};
1348
1349static LIST_HEAD(, root_hold_token)	root_holds =
1350    LIST_HEAD_INITIALIZER(&root_holds);
1351
1352static int root_mount_complete;
1353
1354/*
1355 * Hold root mount.
1356 */
1357struct root_hold_token *
1358root_mount_hold(const char *identifier)
1359{
1360	struct root_hold_token *h;
1361
1362	h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1363	h->who = identifier;
1364	mtx_lock(&mountlist_mtx);
1365	LIST_INSERT_HEAD(&root_holds, h, list);
1366	mtx_unlock(&mountlist_mtx);
1367	return (h);
1368}
1369
1370/*
1371 * Release root mount.
1372 */
1373void
1374root_mount_rel(struct root_hold_token *h)
1375{
1376
1377	mtx_lock(&mountlist_mtx);
1378	LIST_REMOVE(h, list);
1379	wakeup(&root_holds);
1380	mtx_unlock(&mountlist_mtx);
1381	free(h, M_DEVBUF);
1382}
1383
1384/*
1385 * Wait for all subsystems to release root mount.
1386 */
1387static void
1388root_mount_prepare(void)
1389{
1390	struct root_hold_token *h;
1391
1392	for (;;) {
1393		DROP_GIANT();
1394		g_waitidle();
1395		PICKUP_GIANT();
1396		mtx_lock(&mountlist_mtx);
1397		if (LIST_EMPTY(&root_holds)) {
1398			mtx_unlock(&mountlist_mtx);
1399			break;
1400		}
1401		printf("Root mount waiting for:");
1402		LIST_FOREACH(h, &root_holds, list)
1403			printf(" %s", h->who);
1404		printf("\n");
1405		msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1406		    hz);
1407	}
1408}
1409
1410/*
1411 * Root was mounted, share the good news.
1412 */
1413static void
1414root_mount_done(void)
1415{
1416
1417	/*
1418	 * Use a mutex to prevent the wakeup being missed and waiting for
1419	 * an extra 1 second sleep.
1420	 */
1421	mtx_lock(&mountlist_mtx);
1422	root_mount_complete = 1;
1423	wakeup(&root_mount_complete);
1424	mtx_unlock(&mountlist_mtx);
1425}
1426
1427/*
1428 * Return true if root is already mounted.
1429 */
1430int
1431root_mounted(void)
1432{
1433
1434	/* No mutex is acquired here because int stores are atomic. */
1435	return (root_mount_complete);
1436}
1437
1438/*
1439 * Wait until root is mounted.
1440 */
1441void
1442root_mount_wait(void)
1443{
1444
1445	/*
1446	 * Panic on an obvious deadlock - the function can't be called from
1447	 * a thread which is doing the whole SYSINIT stuff.
1448	 */
1449	KASSERT(curthread->td_proc->p_pid != 0,
1450	    ("root_mount_wait: cannot be called from the swapper thread"));
1451	mtx_lock(&mountlist_mtx);
1452	while (!root_mount_complete) {
1453		msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait",
1454		    hz);
1455	}
1456	mtx_unlock(&mountlist_mtx);
1457}
1458
1459static void
1460set_rootvnode(struct thread *td)
1461{
1462	struct proc *p;
1463
1464	if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1465		panic("Cannot find root vnode");
1466
1467	p = td->td_proc;
1468	FILEDESC_SLOCK(p->p_fd);
1469
1470	if (p->p_fd->fd_cdir != NULL)
1471		vrele(p->p_fd->fd_cdir);
1472	p->p_fd->fd_cdir = rootvnode;
1473	VREF(rootvnode);
1474
1475	if (p->p_fd->fd_rdir != NULL)
1476		vrele(p->p_fd->fd_rdir);
1477	p->p_fd->fd_rdir = rootvnode;
1478	VREF(rootvnode);
1479
1480	FILEDESC_SUNLOCK(p->p_fd);
1481
1482	VOP_UNLOCK(rootvnode, 0);
1483}
1484
1485/*
1486 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1487 * yet.  Create a /dev -> / symlink so that absolute pathnames will lookup.
1488 */
1489
1490static void
1491devfs_first(void)
1492{
1493	struct thread *td = curthread;
1494	struct vfsoptlist *opts;
1495	struct vfsconf *vfsp;
1496	struct mount *mp = NULL;
1497	int error;
1498
1499	vfsp = vfs_byname("devfs");
1500	KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1501	if (vfsp == NULL)
1502		return;
1503
1504	mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td);
1505
1506	error = VFS_MOUNT(mp, td);
1507	KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1508	if (error)
1509		return;
1510
1511	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1512	TAILQ_INIT(opts);
1513	mp->mnt_opt = opts;
1514
1515	mtx_lock(&mountlist_mtx);
1516	TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1517	mtx_unlock(&mountlist_mtx);
1518
1519	set_rootvnode(td);
1520
1521	error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1522	if (error)
1523		printf("kern_symlink /dev -> / returns %d\n", error);
1524}
1525
1526/*
1527 * Surgically move our devfs to be mounted on /dev.
1528 */
1529
1530static void
1531devfs_fixup(struct thread *td)
1532{
1533	struct nameidata nd;
1534	int error;
1535	struct vnode *vp, *dvp;
1536	struct mount *mp;
1537
1538	/* Remove our devfs mount from the mountlist and purge the cache */
1539	mtx_lock(&mountlist_mtx);
1540	mp = TAILQ_FIRST(&mountlist);
1541	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1542	mtx_unlock(&mountlist_mtx);
1543	cache_purgevfs(mp);
1544
1545	VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1546	VI_LOCK(dvp);
1547	dvp->v_iflag &= ~VI_MOUNT;
1548	VI_UNLOCK(dvp);
1549	dvp->v_mountedhere = NULL;
1550
1551	/* Set up the real rootvnode, and purge the cache */
1552	TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1553	set_rootvnode(td);
1554	cache_purgevfs(rootvnode->v_mount);
1555
1556	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1557	error = namei(&nd);
1558	if (error) {
1559		printf("Lookup of /dev for devfs, error: %d\n", error);
1560		return;
1561	}
1562	NDFREE(&nd, NDF_ONLY_PNBUF);
1563	vp = nd.ni_vp;
1564	if (vp->v_type != VDIR) {
1565		vput(vp);
1566	}
1567	error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1568	if (error) {
1569		vput(vp);
1570	}
1571	cache_purge(vp);
1572	mp->mnt_vnodecovered = vp;
1573	vp->v_mountedhere = mp;
1574	mtx_lock(&mountlist_mtx);
1575	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1576	mtx_unlock(&mountlist_mtx);
1577	VOP_UNLOCK(vp, 0);
1578	vput(dvp);
1579	vfs_unbusy(mp, td);
1580
1581	/* Unlink the no longer needed /dev/dev -> / symlink */
1582	kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1583}
1584
1585/*
1586 * Report errors during filesystem mounting.
1587 */
1588void
1589vfs_mount_error(struct mount *mp, const char *fmt, ...)
1590{
1591	struct vfsoptlist *moptlist = mp->mnt_optnew;
1592	va_list ap;
1593	int error, len;
1594	char *errmsg;
1595
1596	error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1597	if (error || errmsg == NULL || len <= 0)
1598		return;
1599
1600	va_start(ap, fmt);
1601	vsnprintf(errmsg, (size_t)len, fmt, ap);
1602	va_end(ap);
1603}
1604
1605/*
1606 * Find and mount the root filesystem
1607 */
1608void
1609vfs_mountroot(void)
1610{
1611	char *cp;
1612	int error, i, asked = 0;
1613
1614	root_mount_prepare();
1615
1616	mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
1617	    NULL, NULL, mount_init, mount_fini,
1618	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1619	devfs_first();
1620
1621	/*
1622	 * We are booted with instructions to prompt for the root filesystem.
1623	 */
1624	if (boothowto & RB_ASKNAME) {
1625		if (!vfs_mountroot_ask())
1626			goto mounted;
1627		asked = 1;
1628	}
1629
1630	/*
1631	 * The root filesystem information is compiled in, and we are
1632	 * booted with instructions to use it.
1633	 */
1634	if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1635		if (!vfs_mountroot_try(ctrootdevname))
1636			goto mounted;
1637		ctrootdevname = NULL;
1638	}
1639
1640	/*
1641	 * We've been given the generic "use CDROM as root" flag.  This is
1642	 * necessary because one media may be used in many different
1643	 * devices, so we need to search for them.
1644	 */
1645	if (boothowto & RB_CDROM) {
1646		for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1647			if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1648				goto mounted;
1649		}
1650	}
1651
1652	/*
1653	 * Try to use the value read by the loader from /etc/fstab, or
1654	 * supplied via some other means.  This is the preferred
1655	 * mechanism.
1656	 */
1657	cp = getenv("vfs.root.mountfrom");
1658	if (cp != NULL) {
1659		error = vfs_mountroot_try(cp);
1660		freeenv(cp);
1661		if (!error)
1662			goto mounted;
1663	}
1664
1665	/*
1666	 * Try values that may have been computed by code during boot
1667	 */
1668	if (!vfs_mountroot_try(rootdevnames[0]))
1669		goto mounted;
1670	if (!vfs_mountroot_try(rootdevnames[1]))
1671		goto mounted;
1672
1673	/*
1674	 * If we (still) have a compiled-in default, try it.
1675	 */
1676	if (ctrootdevname != NULL)
1677		if (!vfs_mountroot_try(ctrootdevname))
1678			goto mounted;
1679	/*
1680	 * Everything so far has failed, prompt on the console if we haven't
1681	 * already tried that.
1682	 */
1683	if (!asked)
1684		if (!vfs_mountroot_ask())
1685			goto mounted;
1686
1687	panic("Root mount failed, startup aborted.");
1688
1689mounted:
1690	root_mount_done();
1691}
1692
1693/*
1694 * Mount (mountfrom) as the root filesystem.
1695 */
1696static int
1697vfs_mountroot_try(const char *mountfrom)
1698{
1699	struct mount	*mp;
1700	char		*vfsname, *path;
1701	time_t		timebase;
1702	int		error;
1703	char		patt[32];
1704
1705	vfsname = NULL;
1706	path    = NULL;
1707	mp      = NULL;
1708	error   = EINVAL;
1709
1710	if (mountfrom == NULL)
1711		return (error);		/* don't complain */
1712	printf("Trying to mount root from %s\n", mountfrom);
1713
1714	/* parse vfs name and path */
1715	vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1716	path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1717	vfsname[0] = path[0] = 0;
1718	sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1719	if (sscanf(mountfrom, patt, vfsname, path) < 1)
1720		goto out;
1721
1722	if (path[0] == '\0')
1723		strcpy(path, ROOTNAME);
1724
1725	error = kernel_vmount(
1726	    MNT_RDONLY | MNT_ROOTFS,
1727	    "fstype", vfsname,
1728	    "fspath", "/",
1729	    "from", path,
1730	    NULL);
1731	if (error == 0) {
1732		/*
1733		 * We mount devfs prior to mounting the / FS, so the first
1734		 * entry will typically be devfs.
1735		 */
1736		mp = TAILQ_FIRST(&mountlist);
1737		KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1738
1739		/*
1740		 * Iterate over all currently mounted file systems and use
1741		 * the time stamp found to check and/or initialize the RTC.
1742		 * Typically devfs has no time stamp and the only other FS
1743		 * is the actual / FS.
1744		 * Call inittodr() only once and pass it the largest of the
1745		 * timestamps we encounter.
1746		 */
1747		timebase = 0;
1748		do {
1749			if (mp->mnt_time > timebase)
1750				timebase = mp->mnt_time;
1751			mp = TAILQ_NEXT(mp, mnt_list);
1752		} while (mp != NULL);
1753		inittodr(timebase);
1754
1755		devfs_fixup(curthread);
1756	}
1757out:
1758	free(path, M_MOUNT);
1759	free(vfsname, M_MOUNT);
1760	return (error);
1761}
1762
1763/*
1764 * ---------------------------------------------------------------------
1765 * Interactive root filesystem selection code.
1766 */
1767
1768static int
1769vfs_mountroot_ask(void)
1770{
1771	char name[128];
1772
1773	for(;;) {
1774		printf("\nManual root filesystem specification:\n");
1775		printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
1776#if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
1777		printf("                       eg. ufs:da0s1a\n");
1778#else
1779		printf("                       eg. ufs:/dev/da0a\n");
1780#endif
1781		printf("  ?                  List valid disk boot devices\n");
1782		printf("  <empty line>       Abort manual input\n");
1783		printf("\nmountroot> ");
1784		gets(name, sizeof(name), 1);
1785		if (name[0] == '\0')
1786			return (1);
1787		if (name[0] == '?') {
1788			printf("\nList of GEOM managed disk devices:\n  ");
1789			g_dev_print();
1790			continue;
1791		}
1792		if (!vfs_mountroot_try(name))
1793			return (0);
1794	}
1795}
1796
1797/*
1798 * ---------------------------------------------------------------------
1799 * Functions for querying mount options/arguments from filesystems.
1800 */
1801
1802/*
1803 * Check that no unknown options are given
1804 */
1805int
1806vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1807{
1808	struct vfsopt *opt;
1809	char errmsg[255];
1810	const char **t, *p, *q;
1811	int ret = 0;
1812
1813	TAILQ_FOREACH(opt, opts, link) {
1814		p = opt->name;
1815		q = NULL;
1816		if (p[0] == 'n' && p[1] == 'o')
1817			q = p + 2;
1818		for(t = global_opts; *t != NULL; t++) {
1819			if (strcmp(*t, p) == 0)
1820				break;
1821			if (q != NULL) {
1822				if (strcmp(*t, q) == 0)
1823					break;
1824			}
1825		}
1826		if (*t != NULL)
1827			continue;
1828		for(t = legal; *t != NULL; t++) {
1829			if (strcmp(*t, p) == 0)
1830				break;
1831			if (q != NULL) {
1832				if (strcmp(*t, q) == 0)
1833					break;
1834			}
1835		}
1836		if (*t != NULL)
1837			continue;
1838		sprintf(errmsg, "mount option <%s> is unknown", p);
1839		printf("%s\n", errmsg);
1840		ret = EINVAL;
1841	}
1842	if (ret != 0) {
1843		TAILQ_FOREACH(opt, opts, link) {
1844			if (strcmp(opt->name, "errmsg") == 0) {
1845				strncpy((char *)opt->value, errmsg, opt->len);
1846			}
1847		}
1848	}
1849	return (ret);
1850}
1851
1852/*
1853 * Get a mount option by its name.
1854 *
1855 * Return 0 if the option was found, ENOENT otherwise.
1856 * If len is non-NULL it will be filled with the length
1857 * of the option. If buf is non-NULL, it will be filled
1858 * with the address of the option.
1859 */
1860int
1861vfs_getopt(opts, name, buf, len)
1862	struct vfsoptlist *opts;
1863	const char *name;
1864	void **buf;
1865	int *len;
1866{
1867	struct vfsopt *opt;
1868
1869	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1870
1871	TAILQ_FOREACH(opt, opts, link) {
1872		if (strcmp(name, opt->name) == 0) {
1873			if (len != NULL)
1874				*len = opt->len;
1875			if (buf != NULL)
1876				*buf = opt->value;
1877			return (0);
1878		}
1879	}
1880	return (ENOENT);
1881}
1882
1883static int
1884vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1885{
1886	struct vfsopt *opt;
1887	int i;
1888
1889	if (opts == NULL)
1890		return (-1);
1891
1892	i = 0;
1893	TAILQ_FOREACH(opt, opts, link) {
1894		if (strcmp(name, opt->name) == 0)
1895			return (i);
1896		++i;
1897	}
1898	return (-1);
1899}
1900
1901char *
1902vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1903{
1904	struct vfsopt *opt;
1905
1906	*error = 0;
1907	TAILQ_FOREACH(opt, opts, link) {
1908		if (strcmp(name, opt->name) != 0)
1909			continue;
1910		if (((char *)opt->value)[opt->len - 1] != '\0') {
1911			*error = EINVAL;
1912			return (NULL);
1913		}
1914		return (opt->value);
1915	}
1916	*error = ENOENT;
1917	return (NULL);
1918}
1919
1920int
1921vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1922{
1923	struct vfsopt *opt;
1924
1925	TAILQ_FOREACH(opt, opts, link) {
1926		if (strcmp(name, opt->name) == 0) {
1927			if (w != NULL)
1928				*w |= val;
1929			return (1);
1930		}
1931	}
1932	if (w != NULL)
1933		*w &= ~val;
1934	return (0);
1935}
1936
1937int
1938vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1939{
1940	va_list ap;
1941	struct vfsopt *opt;
1942	int ret;
1943
1944	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1945
1946	TAILQ_FOREACH(opt, opts, link) {
1947		if (strcmp(name, opt->name) != 0)
1948			continue;
1949		if (opt->len == 0 || opt->value == NULL)
1950			return (0);
1951		if (((char *)opt->value)[opt->len - 1] != '\0')
1952			return (0);
1953		va_start(ap, fmt);
1954		ret = vsscanf(opt->value, fmt, ap);
1955		va_end(ap);
1956		return (ret);
1957	}
1958	return (0);
1959}
1960
1961/*
1962 * Find and copy a mount option.
1963 *
1964 * The size of the buffer has to be specified
1965 * in len, if it is not the same length as the
1966 * mount option, EINVAL is returned.
1967 * Returns ENOENT if the option is not found.
1968 */
1969int
1970vfs_copyopt(opts, name, dest, len)
1971	struct vfsoptlist *opts;
1972	const char *name;
1973	void *dest;
1974	int len;
1975{
1976	struct vfsopt *opt;
1977
1978	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1979
1980	TAILQ_FOREACH(opt, opts, link) {
1981		if (strcmp(name, opt->name) == 0) {
1982			if (len != opt->len)
1983				return (EINVAL);
1984			bcopy(opt->value, dest, opt->len);
1985			return (0);
1986		}
1987	}
1988	return (ENOENT);
1989}
1990
1991/*
1992 * This is a helper function for filesystems to traverse their
1993 * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
1994 */
1995
1996struct vnode *
1997__mnt_vnode_next(struct vnode **mvp, struct mount *mp)
1998{
1999	struct vnode *vp;
2000
2001	mtx_assert(MNT_MTX(mp), MA_OWNED);
2002
2003	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2004	vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
2005	while (vp != NULL && vp->v_type == VMARKER)
2006		vp = TAILQ_NEXT(vp, v_nmntvnodes);
2007
2008	/* Check if we are done */
2009	if (vp == NULL) {
2010		__mnt_vnode_markerfree(mvp, mp);
2011		return (NULL);
2012	}
2013	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2014	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2015	return (vp);
2016}
2017
2018struct vnode *
2019__mnt_vnode_first(struct vnode **mvp, struct mount *mp)
2020{
2021	struct vnode *vp;
2022
2023	mtx_assert(MNT_MTX(mp), MA_OWNED);
2024
2025	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2026	while (vp != NULL && vp->v_type == VMARKER)
2027		vp = TAILQ_NEXT(vp, v_nmntvnodes);
2028
2029	/* Check if we are done */
2030	if (vp == NULL) {
2031		*mvp = NULL;
2032		return (NULL);
2033	}
2034	mp->mnt_holdcnt++;
2035	MNT_IUNLOCK(mp);
2036	*mvp = (struct vnode *) malloc(sizeof(struct vnode),
2037				       M_VNODE_MARKER,
2038				       M_WAITOK | M_ZERO);
2039	MNT_ILOCK(mp);
2040	(*mvp)->v_type = VMARKER;
2041
2042	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2043	while (vp != NULL && vp->v_type == VMARKER)
2044		vp = TAILQ_NEXT(vp, v_nmntvnodes);
2045
2046	/* Check if we are done */
2047	if (vp == NULL) {
2048		MNT_IUNLOCK(mp);
2049		free(*mvp, M_VNODE_MARKER);
2050		MNT_ILOCK(mp);
2051		*mvp = NULL;
2052		mp->mnt_holdcnt--;
2053		if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
2054			wakeup(&mp->mnt_holdcnt);
2055		return (NULL);
2056	}
2057	mp->mnt_markercnt++;
2058	(*mvp)->v_mount = mp;
2059	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2060	return (vp);
2061}
2062
2063
2064void
2065__mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
2066{
2067
2068	if (*mvp == NULL)
2069		return;
2070
2071	mtx_assert(MNT_MTX(mp), MA_OWNED);
2072
2073	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2074	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2075	MNT_IUNLOCK(mp);
2076	free(*mvp, M_VNODE_MARKER);
2077	MNT_ILOCK(mp);
2078	*mvp = NULL;
2079
2080	mp->mnt_markercnt--;
2081	mp->mnt_holdcnt--;
2082	if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
2083		wakeup(&mp->mnt_holdcnt);
2084}
2085
2086
2087int
2088__vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
2089{
2090	int error;
2091
2092	error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
2093	if (sbp != &mp->mnt_stat)
2094		*sbp = mp->mnt_stat;
2095	return (error);
2096}
2097
2098void
2099vfs_mountedfrom(struct mount *mp, const char *from)
2100{
2101
2102	bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2103	strlcpy(mp->mnt_stat.f_mntfromname, from,
2104	    sizeof mp->mnt_stat.f_mntfromname);
2105}
2106
2107/*
2108 * ---------------------------------------------------------------------
2109 * This is the api for building mount args and mounting filesystems from
2110 * inside the kernel.
2111 *
2112 * The API works by accumulation of individual args.  First error is
2113 * latched.
2114 *
2115 * XXX: should be documented in new manpage kernel_mount(9)
2116 */
2117
2118/* A memory allocation which must be freed when we are done */
2119struct mntaarg {
2120	SLIST_ENTRY(mntaarg)	next;
2121};
2122
2123/* The header for the mount arguments */
2124struct mntarg {
2125	struct iovec *v;
2126	int len;
2127	int error;
2128	SLIST_HEAD(, mntaarg)	list;
2129};
2130
2131/*
2132 * Add a boolean argument.
2133 *
2134 * flag is the boolean value.
2135 * name must start with "no".
2136 */
2137struct mntarg *
2138mount_argb(struct mntarg *ma, int flag, const char *name)
2139{
2140
2141	KASSERT(name[0] == 'n' && name[1] == 'o',
2142	    ("mount_argb(...,%s): name must start with 'no'", name));
2143
2144	return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2145}
2146
2147/*
2148 * Add an argument printf style
2149 */
2150struct mntarg *
2151mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2152{
2153	va_list ap;
2154	struct mntaarg *maa;
2155	struct sbuf *sb;
2156	int len;
2157
2158	if (ma == NULL) {
2159		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2160		SLIST_INIT(&ma->list);
2161	}
2162	if (ma->error)
2163		return (ma);
2164
2165	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2166	    M_MOUNT, M_WAITOK);
2167	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2168	ma->v[ma->len].iov_len = strlen(name) + 1;
2169	ma->len++;
2170
2171	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
2172	va_start(ap, fmt);
2173	sbuf_vprintf(sb, fmt, ap);
2174	va_end(ap);
2175	sbuf_finish(sb);
2176	len = sbuf_len(sb) + 1;
2177	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2178	SLIST_INSERT_HEAD(&ma->list, maa, next);
2179	bcopy(sbuf_data(sb), maa + 1, len);
2180	sbuf_delete(sb);
2181
2182	ma->v[ma->len].iov_base = maa + 1;
2183	ma->v[ma->len].iov_len = len;
2184	ma->len++;
2185
2186	return (ma);
2187}
2188
2189/*
2190 * Add an argument which is a userland string.
2191 */
2192struct mntarg *
2193mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2194{
2195	struct mntaarg *maa;
2196	char *tbuf;
2197
2198	if (val == NULL)
2199		return (ma);
2200	if (ma == NULL) {
2201		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2202		SLIST_INIT(&ma->list);
2203	}
2204	if (ma->error)
2205		return (ma);
2206	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2207	SLIST_INSERT_HEAD(&ma->list, maa, next);
2208	tbuf = (void *)(maa + 1);
2209	ma->error = copyinstr(val, tbuf, len, NULL);
2210	return (mount_arg(ma, name, tbuf, -1));
2211}
2212
2213/*
2214 * Plain argument.
2215 *
2216 * If length is -1, use printf.
2217 */
2218struct mntarg *
2219mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2220{
2221
2222	if (ma == NULL) {
2223		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2224		SLIST_INIT(&ma->list);
2225	}
2226	if (ma->error)
2227		return (ma);
2228
2229	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2230	    M_MOUNT, M_WAITOK);
2231	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2232	ma->v[ma->len].iov_len = strlen(name) + 1;
2233	ma->len++;
2234
2235	ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2236	if (len < 0)
2237		ma->v[ma->len].iov_len = strlen(val) + 1;
2238	else
2239		ma->v[ma->len].iov_len = len;
2240	ma->len++;
2241	return (ma);
2242}
2243
2244/*
2245 * Free a mntarg structure
2246 */
2247static void
2248free_mntarg(struct mntarg *ma)
2249{
2250	struct mntaarg *maa;
2251
2252	while (!SLIST_EMPTY(&ma->list)) {
2253		maa = SLIST_FIRST(&ma->list);
2254		SLIST_REMOVE_HEAD(&ma->list, next);
2255		free(maa, M_MOUNT);
2256	}
2257	free(ma->v, M_MOUNT);
2258	free(ma, M_MOUNT);
2259}
2260
2261/*
2262 * Mount a filesystem
2263 */
2264int
2265kernel_mount(struct mntarg *ma, int flags)
2266{
2267	struct uio auio;
2268	int error;
2269
2270	KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2271	KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2272	KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2273
2274	auio.uio_iov = ma->v;
2275	auio.uio_iovcnt = ma->len;
2276	auio.uio_segflg = UIO_SYSSPACE;
2277
2278	error = ma->error;
2279	if (!error)
2280		error = vfs_donmount(curthread, flags, &auio);
2281	free_mntarg(ma);
2282	return (error);
2283}
2284
2285/*
2286 * A printflike function to mount a filesystem.
2287 */
2288int
2289kernel_vmount(int flags, ...)
2290{
2291	struct mntarg *ma = NULL;
2292	va_list ap;
2293	const char *cp;
2294	const void *vp;
2295	int error;
2296
2297	va_start(ap, flags);
2298	for (;;) {
2299		cp = va_arg(ap, const char *);
2300		if (cp == NULL)
2301			break;
2302		vp = va_arg(ap, const void *);
2303		ma = mount_arg(ma, cp, vp, -1);
2304	}
2305	va_end(ap);
2306
2307	error = kernel_mount(ma, flags);
2308	return (error);
2309}
2310