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