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