xref: /freebsd/sys/kern/vfs_mount.c (revision 99f3b482da15db6f2ae4702d592b53dd36d4cf55)
1 /*-
2  * Copyright (c) 1999-2004 Poul-Henning Kamp
3  * Copyright (c) 1999 Michael Smith
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/conf.h>
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/libkern.h>
45 #include <sys/mac.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/filedesc.h>
52 #include <sys/reboot.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
55 #include <sys/sx.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
60 #include <vm/uma.h>
61 
62 #include <geom/geom.h>
63 
64 #include <machine/stdarg.h>
65 
66 #include <security/audit/audit.h>
67 
68 #include "opt_rootdevname.h"
69 #include "opt_ddb.h"
70 #include "opt_mac.h"
71 
72 #ifdef DDB
73 #include <ddb/ddb.h>
74 #endif
75 
76 #define	ROOTNAME		"root_device"
77 #define	VFS_MOUNTARG_SIZE_MAX	(1024 * 64)
78 
79 static int	vfs_domount(struct thread *td, const char *fstype,
80 		    char *fspath, int fsflags, void *fsdata);
81 static struct mount *vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp,
82 		    const char *fspath, struct thread *td);
83 static int	vfs_mountroot_ask(void);
84 static int	vfs_mountroot_try(const char *mountfrom);
85 static int	vfs_donmount(struct thread *td, int fsflags,
86 		    struct uio *fsoptions);
87 static void	free_mntarg(struct mntarg *ma);
88 static void	vfs_mount_destroy(struct mount *);
89 static int	vfs_getopt_pos(struct vfsoptlist *opts, const char *name);
90 
91 static int	usermount = 0;
92 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
93     "Unprivileged users may mount and unmount file systems");
94 
95 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
96 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
97 static uma_zone_t mount_zone;
98 
99 /* List of mounted filesystems. */
100 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
101 
102 /* For any iteration/modification of mountlist */
103 struct mtx mountlist_mtx;
104 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
105 
106 TAILQ_HEAD(vfsoptlist, vfsopt);
107 struct vfsopt {
108 	TAILQ_ENTRY(vfsopt) link;
109 	char	*name;
110 	void	*value;
111 	int	len;
112 };
113 
114 /*
115  * The vnode of the system's root (/ in the filesystem, without chroot
116  * active.)
117  */
118 struct vnode	*rootvnode;
119 
120 /*
121  * The root filesystem is detailed in the kernel environment variable
122  * vfs.root.mountfrom, which is expected to be in the general format
123  *
124  * <vfsname>:[<path>]
125  * vfsname   := the name of a VFS known to the kernel and capable
126  *              of being mounted as root
127  * path      := disk device name or other data used by the filesystem
128  *              to locate its physical store
129  */
130 
131 /*
132  * Global opts, taken by all filesystems
133  */
134 static const char *global_opts[] = {
135 	"errmsg",
136 	"fstype",
137 	"fspath",
138 	"rdonly",
139 	"ro",
140 	"rw",
141 	"suid",
142 	"exec",
143 	"update",
144 	NULL
145 };
146 
147 /*
148  * The root specifiers we will try if RB_CDROM is specified.
149  */
150 static char *cdrom_rootdevnames[] = {
151 	"cd9660:cd0",
152 	"cd9660:acd0",
153 	NULL
154 };
155 
156 /* legacy find-root code */
157 char		*rootdevnames[2] = {NULL, NULL};
158 #ifndef ROOTDEVNAME
159 #  define ROOTDEVNAME NULL
160 #endif
161 static const char	*ctrootdevname = ROOTDEVNAME;
162 
163 /*
164  * ---------------------------------------------------------------------
165  * Functions for building and sanitizing the mount options
166  */
167 
168 /* Remove one mount option. */
169 static void
170 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
171 {
172 
173 	TAILQ_REMOVE(opts, opt, link);
174 	free(opt->name, M_MOUNT);
175 	if (opt->value != NULL)
176 		free(opt->value, M_MOUNT);
177 #ifdef INVARIANTS
178 	else if (opt->len != 0)
179 		panic("%s: mount option with NULL value but length != 0",
180 		    __func__);
181 #endif
182 	free(opt, M_MOUNT);
183 }
184 
185 /* Release all resources related to the mount options. */
186 static void
187 vfs_freeopts(struct vfsoptlist *opts)
188 {
189 	struct vfsopt *opt;
190 
191 	while (!TAILQ_EMPTY(opts)) {
192 		opt = TAILQ_FIRST(opts);
193 		vfs_freeopt(opts, opt);
194 	}
195 	free(opts, M_MOUNT);
196 }
197 
198 /*
199  * Check if options are equal (with or without the "no" prefix).
200  */
201 static int
202 vfs_equalopts(const char *opt1, const char *opt2)
203 {
204 
205 	/* "opt" vs. "opt" or "noopt" vs. "noopt" */
206 	if (strcmp(opt1, opt2) == 0)
207 		return (1);
208 	/* "noopt" vs. "opt" */
209 	if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
210 		return (1);
211 	/* "opt" vs. "noopt" */
212 	if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
213 		return (1);
214 	return (0);
215 }
216 
217 /*
218  * If a mount option is specified several times,
219  * (with or without the "no" prefix) only keep
220  * the last occurence of it.
221  */
222 static void
223 vfs_sanitizeopts(struct vfsoptlist *opts)
224 {
225 	struct vfsopt *opt, *opt2, *tmp;
226 
227 	TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
228 		opt2 = TAILQ_PREV(opt, vfsoptlist, link);
229 		while (opt2 != NULL) {
230 			if (vfs_equalopts(opt->name, opt2->name)) {
231 				tmp = TAILQ_PREV(opt2, vfsoptlist, link);
232 				vfs_freeopt(opts, opt2);
233 				opt2 = tmp;
234 			} else {
235 				opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
236 			}
237 		}
238 	}
239 }
240 
241 /*
242  * Build a linked list of mount options from a struct uio.
243  */
244 static int
245 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
246 {
247 	struct vfsoptlist *opts;
248 	struct vfsopt *opt;
249 	size_t memused;
250 	unsigned int i, iovcnt;
251 	int error, namelen, optlen;
252 
253 	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
254 	TAILQ_INIT(opts);
255 	memused = 0;
256 	iovcnt = auio->uio_iovcnt;
257 	for (i = 0; i < iovcnt; i += 2) {
258 		opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
259 		namelen = auio->uio_iov[i].iov_len;
260 		optlen = auio->uio_iov[i + 1].iov_len;
261 		opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
262 		opt->value = NULL;
263 		opt->len = 0;
264 
265 		/*
266 		 * Do this early, so jumps to "bad" will free the current
267 		 * option.
268 		 */
269 		TAILQ_INSERT_TAIL(opts, opt, link);
270 		memused += sizeof(struct vfsopt) + optlen + namelen;
271 
272 		/*
273 		 * Avoid consuming too much memory, and attempts to overflow
274 		 * memused.
275 		 */
276 		if (memused > VFS_MOUNTARG_SIZE_MAX ||
277 		    optlen > VFS_MOUNTARG_SIZE_MAX ||
278 		    namelen > VFS_MOUNTARG_SIZE_MAX) {
279 			error = EINVAL;
280 			goto bad;
281 		}
282 
283 		if (auio->uio_segflg == UIO_SYSSPACE) {
284 			bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
285 		} else {
286 			error = copyin(auio->uio_iov[i].iov_base, opt->name,
287 			    namelen);
288 			if (error)
289 				goto bad;
290 		}
291 		/* Ensure names are null-terminated strings. */
292 		if (opt->name[namelen - 1] != '\0') {
293 			error = EINVAL;
294 			goto bad;
295 		}
296 		if (optlen != 0) {
297 			opt->len = optlen;
298 			opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
299 			if (auio->uio_segflg == UIO_SYSSPACE) {
300 				bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
301 				    optlen);
302 			} else {
303 				error = copyin(auio->uio_iov[i + 1].iov_base,
304 				    opt->value, optlen);
305 				if (error)
306 					goto bad;
307 			}
308 		}
309 	}
310 	vfs_sanitizeopts(opts);
311 	*options = opts;
312 	return (0);
313 bad:
314 	vfs_freeopts(opts);
315 	return (error);
316 }
317 
318 /*
319  * Merge the old mount options with the new ones passed
320  * in the MNT_UPDATE case.
321  */
322 static void
323 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
324 {
325 	struct vfsopt *opt, *opt2, *new;
326 
327 	TAILQ_FOREACH(opt, opts, link) {
328 		/*
329 		 * Check that this option hasn't been redefined
330 		 * nor cancelled with a "no" mount option.
331 		 */
332 		opt2 = TAILQ_FIRST(toopts);
333 		while (opt2 != NULL) {
334 			if (strcmp(opt2->name, opt->name) == 0)
335 				goto next;
336 			if (strncmp(opt2->name, "no", 2) == 0 &&
337 			    strcmp(opt2->name + 2, opt->name) == 0) {
338 				vfs_freeopt(toopts, opt2);
339 				goto next;
340 			}
341 			opt2 = TAILQ_NEXT(opt2, link);
342 		}
343 		/* We want this option, duplicate it. */
344 		new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
345 		new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
346 		strcpy(new->name, opt->name);
347 		if (opt->len != 0) {
348 			new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
349 			bcopy(opt->value, new->value, opt->len);
350 		} else {
351 			new->value = NULL;
352 		}
353 		new->len = opt->len;
354 		TAILQ_INSERT_TAIL(toopts, new, link);
355 next:
356 		continue;
357 	}
358 }
359 
360 /*
361  * ---------------------------------------------------------------------
362  * Mount a filesystem
363  */
364 int
365 nmount(td, uap)
366 	struct thread *td;
367 	struct nmount_args /* {
368 		struct iovec *iovp;
369 		unsigned int iovcnt;
370 		int flags;
371 	} */ *uap;
372 {
373 	struct uio *auio;
374 	struct iovec *iov;
375 	unsigned int i;
376 	int error;
377 	u_int iovcnt;
378 
379 	AUDIT_ARG(fflags, uap->flags);
380 
381 	/* Kick out MNT_ROOTFS early as it is legal internally */
382 	if (uap->flags & MNT_ROOTFS)
383 		return (EINVAL);
384 
385 	iovcnt = uap->iovcnt;
386 	/*
387 	 * Check that we have an even number of iovec's
388 	 * and that we have at least two options.
389 	 */
390 	if ((iovcnt & 1) || (iovcnt < 4))
391 		return (EINVAL);
392 
393 	error = copyinuio(uap->iovp, iovcnt, &auio);
394 	if (error)
395 		return (error);
396 	iov = auio->uio_iov;
397 	for (i = 0; i < iovcnt; i++) {
398 		if (iov->iov_len > MMAXOPTIONLEN) {
399 			free(auio, M_IOV);
400 			return (EINVAL);
401 		}
402 		iov++;
403 	}
404 	error = vfs_donmount(td, uap->flags, auio);
405 
406 	free(auio, M_IOV);
407 	return (error);
408 }
409 
410 /*
411  * ---------------------------------------------------------------------
412  * Various utility functions
413  */
414 
415 void
416 vfs_ref(struct mount *mp)
417 {
418 
419 	MNT_ILOCK(mp);
420 	MNT_REF(mp);
421 	MNT_IUNLOCK(mp);
422 }
423 
424 void
425 vfs_rel(struct mount *mp)
426 {
427 
428 	MNT_ILOCK(mp);
429 	MNT_REL(mp);
430 	MNT_IUNLOCK(mp);
431 }
432 
433 static int
434 mount_init(void *mem, int size, int flags)
435 {
436 	struct mount *mp;
437 
438 	mp = (struct mount *)mem;
439 	mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
440 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
441 	return (0);
442 }
443 
444 static void
445 mount_fini(void *mem, int size)
446 {
447 	struct mount *mp;
448 
449 	mp = (struct mount *)mem;
450 	lockdestroy(&mp->mnt_lock);
451 	mtx_destroy(&mp->mnt_mtx);
452 }
453 
454 /*
455  * Allocate and initialize the mount point struct.
456  */
457 static struct mount *
458 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
459     const char *fspath, struct thread *td)
460 {
461 	struct mount *mp;
462 
463 	mp = uma_zalloc(mount_zone, M_WAITOK);
464 	bzero(&mp->mnt_startzero,
465 	    __rangeof(struct mount, mnt_startzero, mnt_endzero));
466 	TAILQ_INIT(&mp->mnt_nvnodelist);
467 	mp->mnt_nvnodelistsize = 0;
468 	mp->mnt_ref = 0;
469 	(void) vfs_busy(mp, LK_NOWAIT, 0, td);
470 	mp->mnt_op = vfsp->vfc_vfsops;
471 	mp->mnt_vfc = vfsp;
472 	vfsp->vfc_refcount++;	/* XXX Unlocked */
473 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
474 	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
475 	strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
476 	mp->mnt_vnodecovered = vp;
477 	mp->mnt_cred = crdup(td->td_ucred);
478 	mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
479 	strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
480 	mp->mnt_iosize_max = DFLTPHYS;
481 #ifdef MAC
482 	mac_init_mount(mp);
483 	mac_create_mount(td->td_ucred, mp);
484 #endif
485 	arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
486 	return (mp);
487 }
488 
489 /*
490  * Destroy the mount struct previously allocated by vfs_mount_alloc().
491  */
492 static void
493 vfs_mount_destroy(struct mount *mp)
494 {
495 	int i;
496 
497 	MNT_ILOCK(mp);
498 	for (i = 0; mp->mnt_ref && i < 3; i++)
499 		msleep(mp, MNT_MTX(mp), PVFS, "mntref", hz);
500 	/*
501 	 * This will always cause a 3 second delay in rebooting due to
502 	 * refs on the root mountpoint that never go away.  Most of these
503 	 * are held by init which never exits.
504 	 */
505 	if (i == 3 && (!rebooting || bootverbose))
506 		printf("Mount point %s had %d dangling refs\n",
507 		    mp->mnt_stat.f_mntonname, mp->mnt_ref);
508 	if (mp->mnt_holdcnt != 0) {
509 		printf("Waiting for mount point to be unheld\n");
510 		while (mp->mnt_holdcnt != 0) {
511 			mp->mnt_holdcntwaiters++;
512 			msleep(&mp->mnt_holdcnt, MNT_MTX(mp),
513 			       PZERO, "mntdestroy", 0);
514 			mp->mnt_holdcntwaiters--;
515 		}
516 		printf("mount point unheld\n");
517 	}
518 	if (mp->mnt_writeopcount > 0) {
519 		printf("Waiting for mount point write ops\n");
520 		while (mp->mnt_writeopcount > 0) {
521 			mp->mnt_kern_flag |= MNTK_SUSPEND;
522 			msleep(&mp->mnt_writeopcount,
523 			       MNT_MTX(mp),
524 			       PZERO, "mntdestroy2", 0);
525 		}
526 		printf("mount point write ops completed\n");
527 	}
528 	if (mp->mnt_secondary_writes > 0) {
529 		printf("Waiting for mount point secondary write ops\n");
530 		while (mp->mnt_secondary_writes > 0) {
531 			mp->mnt_kern_flag |= MNTK_SUSPEND;
532 			msleep(&mp->mnt_secondary_writes,
533 			       MNT_MTX(mp),
534 			       PZERO, "mntdestroy3", 0);
535 		}
536 		printf("mount point secondary write ops completed\n");
537 	}
538 	MNT_IUNLOCK(mp);
539 	mp->mnt_vfc->vfc_refcount--;
540 	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
541 		struct vnode *vp;
542 
543 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
544 			vprint("", vp);
545 		panic("unmount: dangling vnode");
546 	}
547 	MNT_ILOCK(mp);
548 	if (mp->mnt_kern_flag & MNTK_MWAIT)
549 		wakeup(mp);
550 	if (mp->mnt_writeopcount != 0)
551 		panic("vfs_mount_destroy: nonzero writeopcount");
552 	if (mp->mnt_secondary_writes != 0)
553 		panic("vfs_mount_destroy: nonzero secondary_writes");
554 	if (mp->mnt_nvnodelistsize != 0)
555 		panic("vfs_mount_destroy: nonzero nvnodelistsize");
556 	mp->mnt_writeopcount = -1000;
557 	mp->mnt_nvnodelistsize = -1000;
558 	mp->mnt_secondary_writes = -1000;
559 	MNT_IUNLOCK(mp);
560 #ifdef MAC
561 	mac_destroy_mount(mp);
562 #endif
563 	if (mp->mnt_opt != NULL)
564 		vfs_freeopts(mp->mnt_opt);
565 	crfree(mp->mnt_cred);
566 	uma_zfree(mount_zone, mp);
567 }
568 
569 static int
570 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
571 {
572 	struct vfsoptlist *optlist;
573 	struct vfsopt *opt, *noro_opt;
574 	char *fstype, *fspath, *errmsg;
575 	int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
576 	int has_rw, has_noro;
577 
578 	errmsg = NULL;
579 	errmsg_len = 0;
580 	errmsg_pos = -1;
581 	has_rw = 0;
582 	has_noro = 0;
583 
584 	error = vfs_buildopts(fsoptions, &optlist);
585 	if (error)
586 		return (error);
587 
588 	if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
589 		errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
590 
591 	/*
592 	 * We need these two options before the others,
593 	 * and they are mandatory for any filesystem.
594 	 * Ensure they are NUL terminated as well.
595 	 */
596 	fstypelen = 0;
597 	error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
598 	if (error || fstype[fstypelen - 1] != '\0') {
599 		error = EINVAL;
600 		if (errmsg != NULL)
601 			strncpy(errmsg, "Invalid fstype", errmsg_len);
602 		goto bail;
603 	}
604 	fspathlen = 0;
605 	error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
606 	if (error || fspath[fspathlen - 1] != '\0') {
607 		error = EINVAL;
608 		if (errmsg != NULL)
609 			strncpy(errmsg, "Invalid fspath", errmsg_len);
610 		goto bail;
611 	}
612 
613 	/*
614 	 * We need to see if we have the "update" option
615 	 * before we call vfs_domount(), since vfs_domount() has special
616 	 * logic based on MNT_UPDATE.  This is very important
617 	 * when we want to update the root filesystem.
618 	 */
619 	TAILQ_FOREACH(opt, optlist, link) {
620 		if (strcmp(opt->name, "update") == 0)
621 			fsflags |= MNT_UPDATE;
622 		else if (strcmp(opt->name, "async") == 0)
623 			fsflags |= MNT_ASYNC;
624 		else if (strcmp(opt->name, "force") == 0)
625 			fsflags |= MNT_FORCE;
626 		else if (strcmp(opt->name, "multilabel") == 0)
627 			fsflags |= MNT_MULTILABEL;
628 		else if (strcmp(opt->name, "noasync") == 0)
629 			fsflags &= ~MNT_ASYNC;
630 		else if (strcmp(opt->name, "noatime") == 0)
631 			fsflags |= MNT_NOATIME;
632 		else if (strcmp(opt->name, "noclusterr") == 0)
633 			fsflags |= MNT_NOCLUSTERR;
634 		else if (strcmp(opt->name, "noclusterw") == 0)
635 			fsflags |= MNT_NOCLUSTERW;
636 		else if (strcmp(opt->name, "noexec") == 0)
637 			fsflags |= MNT_NOEXEC;
638 		else if (strcmp(opt->name, "nosuid") == 0)
639 			fsflags |= MNT_NOSUID;
640 		else if (strcmp(opt->name, "nosymfollow") == 0)
641 			fsflags |= MNT_NOSYMFOLLOW;
642 		else if (strcmp(opt->name, "noro") == 0) {
643 			fsflags &= ~MNT_RDONLY;
644 			has_noro = 1;
645 		}
646 		else if (strcmp(opt->name, "rw") == 0) {
647 			fsflags &= ~MNT_RDONLY;
648 			has_rw = 1;
649 		}
650 		else if (strcmp(opt->name, "ro") == 0 ||
651 		    strcmp(opt->name, "rdonly") == 0)
652 			fsflags |= MNT_RDONLY;
653 		else if (strcmp(opt->name, "snapshot") == 0)
654 			fsflags |= MNT_SNAPSHOT;
655 		else if (strcmp(opt->name, "suiddir") == 0)
656 			fsflags |= MNT_SUIDDIR;
657 		else if (strcmp(opt->name, "sync") == 0)
658 			fsflags |= MNT_SYNCHRONOUS;
659 		else if (strcmp(opt->name, "union") == 0)
660 			fsflags |= MNT_UNION;
661 	}
662 
663 	/*
664 	 * If "rw" was specified as a mount option, and we
665 	 * are trying to update a mount-point from "ro" to "rw",
666 	 * we need a mount option "noro", since in vfs_mergeopts(),
667 	 * "noro" will cancel "ro", but "rw" will not do anything.
668 	 */
669 	if (has_rw && !has_noro) {
670 		noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
671 		noro_opt->name = strdup("noro", M_MOUNT);
672 		noro_opt->value = NULL;
673 		noro_opt->len = 0;
674 		TAILQ_INSERT_TAIL(optlist, noro_opt, link);
675 	}
676 
677 	/*
678 	 * Be ultra-paranoid about making sure the type and fspath
679 	 * variables will fit in our mp buffers, including the
680 	 * terminating NUL.
681 	 */
682 	if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
683 		error = ENAMETOOLONG;
684 		goto bail;
685 	}
686 
687 	mtx_lock(&Giant);
688 	error = vfs_domount(td, fstype, fspath, fsflags, optlist);
689 	mtx_unlock(&Giant);
690 bail:
691 	/* copyout the errmsg */
692 	if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
693 	    && errmsg_len > 0 && errmsg != NULL) {
694 		if (fsoptions->uio_segflg == UIO_SYSSPACE) {
695 			bcopy(errmsg,
696 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
697 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
698 		} else {
699 			copyout(errmsg,
700 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
701 			    fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
702 		}
703 	}
704 
705 	if (error != 0)
706 		vfs_freeopts(optlist);
707 	return (error);
708 }
709 
710 /*
711  * ---------------------------------------------------------------------
712  * Old mount API.
713  */
714 #ifndef _SYS_SYSPROTO_H_
715 struct mount_args {
716 	char	*type;
717 	char	*path;
718 	int	flags;
719 	caddr_t	data;
720 };
721 #endif
722 /* ARGSUSED */
723 int
724 mount(td, uap)
725 	struct thread *td;
726 	struct mount_args /* {
727 		char *type;
728 		char *path;
729 		int flags;
730 		caddr_t data;
731 	} */ *uap;
732 {
733 	char *fstype;
734 	struct vfsconf *vfsp = NULL;
735 	struct mntarg *ma = NULL;
736 	int error;
737 
738 	AUDIT_ARG(fflags, uap->flags);
739 
740 	/* Kick out MNT_ROOTFS early as it is legal internally */
741 	uap->flags &= ~MNT_ROOTFS;
742 
743 	fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
744 	error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
745 	if (error) {
746 		free(fstype, M_TEMP);
747 		return (error);
748 	}
749 
750 	AUDIT_ARG(text, fstype);
751 	mtx_lock(&Giant);
752 	vfsp = vfs_byname_kld(fstype, td, &error);
753 	free(fstype, M_TEMP);
754 	if (vfsp == NULL) {
755 		mtx_unlock(&Giant);
756 		return (ENOENT);
757 	}
758 	if (vfsp->vfc_vfsops->vfs_cmount == NULL) {
759 		mtx_unlock(&Giant);
760 		return (EOPNOTSUPP);
761 	}
762 
763 	ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
764 	ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
765 	ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
766 	ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
767 	ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
768 
769 	error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
770 	mtx_unlock(&Giant);
771 	return (error);
772 }
773 
774 
775 /*
776  * vfs_domount(): actually attempt a filesystem mount.
777  */
778 static int
779 vfs_domount(
780 	struct thread *td,	/* Calling thread. */
781 	const char *fstype,	/* Filesystem type. */
782 	char *fspath,		/* Mount path. */
783 	int fsflags,		/* Flags common to all filesystems. */
784 	void *fsdata		/* Options local to the filesystem. */
785 	)
786 {
787 	struct vnode *vp;
788 	struct mount *mp;
789 	struct vfsconf *vfsp;
790 	struct export_args export;
791 	int error, flag = 0, kern_flag = 0;
792 	struct vattr va;
793 	struct nameidata nd;
794 
795 	mtx_assert(&Giant, MA_OWNED);
796 	/*
797 	 * Be ultra-paranoid about making sure the type and fspath
798 	 * variables will fit in our mp buffers, including the
799 	 * terminating NUL.
800 	 */
801 	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
802 		return (ENAMETOOLONG);
803 
804 	if (jailed(td->td_ucred))
805 		return (EPERM);
806 	if (usermount == 0) {
807 		if ((error = suser(td)) != 0)
808 			return (error);
809 	}
810 
811 	/*
812 	 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
813 	 */
814 	if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) {
815 		if ((error = suser(td)) != 0)
816 			return (error);
817 	}
818 	/*
819 	 * Silently enforce MNT_NOSUID and MNT_USER for
820 	 * unprivileged users.
821 	 */
822 	if (suser(td) != 0)
823 		fsflags |= MNT_NOSUID | MNT_USER;
824 
825 	/* Load KLDs before we lock the covered vnode to avoid reversals. */
826 	vfsp = NULL;
827 	if ((fsflags & MNT_UPDATE) == 0) {
828 		/* Don't try to load KLDs if we're mounting the root. */
829 		if (fsflags & MNT_ROOTFS)
830 			vfsp = vfs_byname(fstype);
831 		else
832 			vfsp = vfs_byname_kld(fstype, td, &error);
833 		if (vfsp == NULL)
834 			return (ENODEV);
835 	}
836 	/*
837 	 * Get vnode to be covered
838 	 */
839 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
840 	    fspath, td);
841 	if ((error = namei(&nd)) != 0)
842 		return (error);
843 	NDFREE(&nd, NDF_ONLY_PNBUF);
844 	vp = nd.ni_vp;
845 	if (fsflags & MNT_UPDATE) {
846 		if ((vp->v_vflag & VV_ROOT) == 0) {
847 			vput(vp);
848 			return (EINVAL);
849 		}
850 		mp = vp->v_mount;
851 		flag = mp->mnt_flag;
852 		kern_flag = mp->mnt_kern_flag;
853 		/*
854 		 * We only allow the filesystem to be reloaded if it
855 		 * is currently mounted read-only.
856 		 */
857 		if ((fsflags & MNT_RELOAD) &&
858 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
859 			vput(vp);
860 			return (EOPNOTSUPP);	/* Needs translation */
861 		}
862 		/*
863 		 * Only privileged root, or (if MNT_USER is set) the user that
864 		 * did the original mount is permitted to update it.
865 		 */
866 		error = vfs_suser(mp, td);
867 		if (error) {
868 			vput(vp);
869 			return (error);
870 		}
871 		if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
872 			vput(vp);
873 			return (EBUSY);
874 		}
875 		VI_LOCK(vp);
876 		if ((vp->v_iflag & VI_MOUNT) != 0 ||
877 		    vp->v_mountedhere != NULL) {
878 			VI_UNLOCK(vp);
879 			vfs_unbusy(mp, td);
880 			vput(vp);
881 			return (EBUSY);
882 		}
883 		vp->v_iflag |= VI_MOUNT;
884 		VI_UNLOCK(vp);
885 		mp->mnt_flag |= fsflags &
886 		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
887 		VOP_UNLOCK(vp, 0, td);
888 		mp->mnt_optnew = fsdata;
889 		vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
890 	} else {
891 		/*
892 		 * If the user is not root, ensure that they own the directory
893 		 * onto which we are attempting to mount.
894 		 */
895 		error = VOP_GETATTR(vp, &va, td->td_ucred, td);
896 		if (error) {
897 			vput(vp);
898 			return (error);
899 		}
900 		if (va.va_uid != td->td_ucred->cr_uid) {
901 			if ((error = suser(td)) != 0) {
902 				vput(vp);
903 				return (error);
904 			}
905 		}
906 		error = vinvalbuf(vp, V_SAVE, td, 0, 0);
907 		if (error != 0) {
908 			vput(vp);
909 			return (error);
910 		}
911 		if (vp->v_type != VDIR) {
912 			vput(vp);
913 			return (ENOTDIR);
914 		}
915 		VI_LOCK(vp);
916 		if ((vp->v_iflag & VI_MOUNT) != 0 ||
917 		    vp->v_mountedhere != NULL) {
918 			VI_UNLOCK(vp);
919 			vput(vp);
920 			return (EBUSY);
921 		}
922 		vp->v_iflag |= VI_MOUNT;
923 		VI_UNLOCK(vp);
924 
925 		/*
926 		 * Allocate and initialize the filesystem.
927 		 */
928 		mp = vfs_mount_alloc(vp, vfsp, fspath, td);
929 		VOP_UNLOCK(vp, 0, td);
930 
931 		/* XXXMAC: pass to vfs_mount_alloc? */
932 		mp->mnt_optnew = fsdata;
933 	}
934 
935 	/*
936 	 * Set the mount level flags.
937 	 */
938 	if (fsflags & MNT_RDONLY)
939 		mp->mnt_flag |= MNT_RDONLY;
940 	mp->mnt_flag &=~ MNT_UPDATEMASK;
941 	mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
942 	/*
943 	 * Mount the filesystem.
944 	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
945 	 * get.  No freeing of cn_pnbuf.
946 	 */
947         error = VFS_MOUNT(mp, td);
948 
949 	/*
950 	 * Process the export option only if we are
951 	 * updating mount options.
952 	 */
953 	if (!error && (fsflags & MNT_UPDATE)) {
954 		if (vfs_copyopt(mp->mnt_optnew, "export", &export,
955 		    sizeof(export)) == 0)
956 			error = vfs_export(mp, &export);
957 	}
958 
959 	if (!error) {
960 		if (mp->mnt_opt != NULL)
961 			vfs_freeopts(mp->mnt_opt);
962 		mp->mnt_opt = mp->mnt_optnew;
963 		(void)VFS_STATFS(mp, &mp->mnt_stat, td);
964 	}
965 	/*
966 	 * Prevent external consumers of mount options from reading
967 	 * mnt_optnew.
968 	*/
969 	mp->mnt_optnew = NULL;
970 	if (mp->mnt_flag & MNT_UPDATE) {
971 		mp->mnt_flag &=
972 		    ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
973 		if (error) {
974 			mp->mnt_flag = flag;
975 			mp->mnt_kern_flag = kern_flag;
976 		}
977 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
978 			if (mp->mnt_syncer == NULL)
979 				error = vfs_allocate_syncvnode(mp);
980 		} else {
981 			if (mp->mnt_syncer != NULL)
982 				vrele(mp->mnt_syncer);
983 			mp->mnt_syncer = NULL;
984 		}
985 		vfs_unbusy(mp, td);
986 		VI_LOCK(vp);
987 		vp->v_iflag &= ~VI_MOUNT;
988 		VI_UNLOCK(vp);
989 		vrele(vp);
990 		return (error);
991 	}
992 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
993 	/*
994 	 * Put the new filesystem on the mount list after root.
995 	 */
996 	cache_purge(vp);
997 	if (!error) {
998 		struct vnode *newdp;
999 
1000 		VI_LOCK(vp);
1001 		vp->v_iflag &= ~VI_MOUNT;
1002 		VI_UNLOCK(vp);
1003 		vp->v_mountedhere = mp;
1004 		mtx_lock(&mountlist_mtx);
1005 		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1006 		mtx_unlock(&mountlist_mtx);
1007 		vfs_event_signal(NULL, VQ_MOUNT, 0);
1008 		if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
1009 			panic("mount: lost mount");
1010 		mountcheckdirs(vp, newdp);
1011 		vput(newdp);
1012 		VOP_UNLOCK(vp, 0, td);
1013 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
1014 			error = vfs_allocate_syncvnode(mp);
1015 		vfs_unbusy(mp, td);
1016 		if (error)
1017 			vrele(vp);
1018 	} else {
1019 		VI_LOCK(vp);
1020 		vp->v_iflag &= ~VI_MOUNT;
1021 		VI_UNLOCK(vp);
1022 		vfs_unbusy(mp, td);
1023 		vfs_mount_destroy(mp);
1024 		vput(vp);
1025 	}
1026 	return (error);
1027 }
1028 
1029 /*
1030  * ---------------------------------------------------------------------
1031  * Unmount a filesystem.
1032  *
1033  * Note: unmount takes a path to the vnode mounted on as argument,
1034  * not special file (as before).
1035  */
1036 #ifndef _SYS_SYSPROTO_H_
1037 struct unmount_args {
1038 	char	*path;
1039 	int	flags;
1040 };
1041 #endif
1042 /* ARGSUSED */
1043 int
1044 unmount(td, uap)
1045 	struct thread *td;
1046 	register struct unmount_args /* {
1047 		char *path;
1048 		int flags;
1049 	} */ *uap;
1050 {
1051 	struct mount *mp;
1052 	char *pathbuf;
1053 	int error, id0, id1;
1054 
1055 	if (jailed(td->td_ucred))
1056 		return (EPERM);
1057 	if (usermount == 0) {
1058 		if ((error = suser(td)) != 0)
1059 			return (error);
1060 	}
1061 
1062 	pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1063 	error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1064 	if (error) {
1065 		free(pathbuf, M_TEMP);
1066 		return (error);
1067 	}
1068 	AUDIT_ARG(upath, td, pathbuf, ARG_UPATH1);
1069 	mtx_lock(&Giant);
1070 	if (uap->flags & MNT_BYFSID) {
1071 		/* Decode the filesystem ID. */
1072 		if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1073 			mtx_unlock(&Giant);
1074 			free(pathbuf, M_TEMP);
1075 			return (EINVAL);
1076 		}
1077 
1078 		mtx_lock(&mountlist_mtx);
1079 		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1080 			if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1081 			    mp->mnt_stat.f_fsid.val[1] == id1)
1082 				break;
1083 		}
1084 		mtx_unlock(&mountlist_mtx);
1085 	} else {
1086 		mtx_lock(&mountlist_mtx);
1087 		TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1088 			if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1089 				break;
1090 		}
1091 		mtx_unlock(&mountlist_mtx);
1092 	}
1093 	free(pathbuf, M_TEMP);
1094 	if (mp == NULL) {
1095 		/*
1096 		 * Previously we returned ENOENT for a nonexistent path and
1097 		 * EINVAL for a non-mountpoint.  We cannot tell these apart
1098 		 * now, so in the !MNT_BYFSID case return the more likely
1099 		 * EINVAL for compatibility.
1100 		 */
1101 		mtx_unlock(&Giant);
1102 		return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1103 	}
1104 
1105 	/*
1106 	 * Only privileged root, or (if MNT_USER is set) the user that did the
1107 	 * original mount is permitted to unmount this filesystem.
1108 	 */
1109 	error = vfs_suser(mp, td);
1110 	if (error) {
1111 		mtx_unlock(&Giant);
1112 		return (error);
1113 	}
1114 
1115 	/*
1116 	 * Don't allow unmounting the root filesystem.
1117 	 */
1118 	if (mp->mnt_flag & MNT_ROOTFS) {
1119 		mtx_unlock(&Giant);
1120 		return (EINVAL);
1121 	}
1122 	error = dounmount(mp, uap->flags, td);
1123 	mtx_unlock(&Giant);
1124 	return (error);
1125 }
1126 
1127 /*
1128  * Do the actual filesystem unmount.
1129  */
1130 int
1131 dounmount(mp, flags, td)
1132 	struct mount *mp;
1133 	int flags;
1134 	struct thread *td;
1135 {
1136 	struct vnode *coveredvp, *fsrootvp;
1137 	int error;
1138 	int async_flag;
1139 
1140 	mtx_assert(&Giant, MA_OWNED);
1141 
1142 	if ((coveredvp = mp->mnt_vnodecovered) != NULL)
1143 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY, td);
1144 	MNT_ILOCK(mp);
1145 	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1146 		MNT_IUNLOCK(mp);
1147 		if (coveredvp)
1148 			VOP_UNLOCK(coveredvp, 0, td);
1149 		return (EBUSY);
1150 	}
1151 	mp->mnt_kern_flag |= MNTK_UNMOUNT;
1152 	/* Allow filesystems to detect that a forced unmount is in progress. */
1153 	if (flags & MNT_FORCE)
1154 		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1155 	error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1156 	    ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
1157 	if (error) {
1158 		MNT_ILOCK(mp);
1159 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1160 		if (mp->mnt_kern_flag & MNTK_MWAIT)
1161 			wakeup(mp);
1162 		MNT_IUNLOCK(mp);
1163 		if (coveredvp)
1164 			VOP_UNLOCK(coveredvp, 0, td);
1165 		return (error);
1166 	}
1167 	vn_start_write(NULL, &mp, V_WAIT);
1168 
1169 	if (mp->mnt_flag & MNT_EXPUBLIC)
1170 		vfs_setpublicfs(NULL, NULL, NULL);
1171 
1172 	vfs_msync(mp, MNT_WAIT);
1173 	async_flag = mp->mnt_flag & MNT_ASYNC;
1174 	mp->mnt_flag &= ~MNT_ASYNC;
1175 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
1176 	if (mp->mnt_syncer != NULL)
1177 		vrele(mp->mnt_syncer);
1178 	/*
1179 	 * For forced unmounts, move process cdir/rdir refs on the fs root
1180 	 * vnode to the covered vnode.  For non-forced unmounts we want
1181 	 * such references to cause an EBUSY error.
1182 	 */
1183 	if ((flags & MNT_FORCE) &&
1184 	    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1185 		if (mp->mnt_vnodecovered != NULL)
1186 			mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1187 		if (fsrootvp == rootvnode) {
1188 			vrele(rootvnode);
1189 			rootvnode = NULL;
1190 		}
1191 		vput(fsrootvp);
1192 	}
1193 	if (((mp->mnt_flag & MNT_RDONLY) ||
1194 	     (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
1195 	    (flags & MNT_FORCE)) {
1196 		error = VFS_UNMOUNT(mp, flags, td);
1197 	}
1198 	vn_finished_write(mp);
1199 	if (error) {
1200 		/* Undo cdir/rdir and rootvnode changes made above. */
1201 		if ((flags & MNT_FORCE) &&
1202 		    VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1203 			if (mp->mnt_vnodecovered != NULL)
1204 				mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1205 			if (rootvnode == NULL) {
1206 				rootvnode = fsrootvp;
1207 				vref(rootvnode);
1208 			}
1209 			vput(fsrootvp);
1210 		}
1211 		if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
1212 			(void) vfs_allocate_syncvnode(mp);
1213 		MNT_ILOCK(mp);
1214 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1215 		mp->mnt_flag |= async_flag;
1216 		lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1217 		if (mp->mnt_kern_flag & MNTK_MWAIT)
1218 			wakeup(mp);
1219 		MNT_IUNLOCK(mp);
1220 		if (coveredvp)
1221 			VOP_UNLOCK(coveredvp, 0, td);
1222 		return (error);
1223 	}
1224 	mtx_lock(&mountlist_mtx);
1225 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1226 	mtx_unlock(&mountlist_mtx);
1227 	if (coveredvp != NULL) {
1228 		coveredvp->v_mountedhere = NULL;
1229 		vput(coveredvp);
1230 	}
1231 	vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1232 	lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1233 	vfs_mount_destroy(mp);
1234 	return (0);
1235 }
1236 
1237 /*
1238  * ---------------------------------------------------------------------
1239  * Mounting of root filesystem
1240  *
1241  */
1242 
1243 struct root_hold_token {
1244 	const char 			*who;
1245 	LIST_ENTRY(root_hold_token)	list;
1246 };
1247 
1248 static LIST_HEAD(, root_hold_token)	root_holds =
1249     LIST_HEAD_INITIALIZER(&root_holds);
1250 
1251 struct root_hold_token *
1252 root_mount_hold(const char *identifier)
1253 {
1254 	struct root_hold_token *h;
1255 
1256 	h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1257 	h->who = identifier;
1258 	mtx_lock(&mountlist_mtx);
1259 	LIST_INSERT_HEAD(&root_holds, h, list);
1260 	mtx_unlock(&mountlist_mtx);
1261 	return (h);
1262 }
1263 
1264 void
1265 root_mount_rel(struct root_hold_token *h)
1266 {
1267 
1268 	mtx_lock(&mountlist_mtx);
1269 	LIST_REMOVE(h, list);
1270 	wakeup(&root_holds);
1271 	mtx_unlock(&mountlist_mtx);
1272 	free(h, M_DEVBUF);
1273 }
1274 
1275 static void
1276 root_mount_wait(void)
1277 {
1278 	struct root_hold_token *h;
1279 
1280 	for (;;) {
1281 		DROP_GIANT();
1282 		g_waitidle();
1283 		PICKUP_GIANT();
1284 		mtx_lock(&mountlist_mtx);
1285 		if (LIST_EMPTY(&root_holds)) {
1286 			mtx_unlock(&mountlist_mtx);
1287 			break;
1288 		}
1289 		printf("Root mount waiting for:");
1290 		LIST_FOREACH(h, &root_holds, list)
1291 			printf(" %s", h->who);
1292 		printf("\n");
1293 		msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1294 		    hz);
1295 	}
1296 }
1297 
1298 static void
1299 set_rootvnode(struct thread *td)
1300 {
1301 	struct proc *p;
1302 
1303 	if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1304 		panic("Cannot find root vnode");
1305 
1306 	p = td->td_proc;
1307 	FILEDESC_LOCK(p->p_fd);
1308 
1309 	if (p->p_fd->fd_cdir != NULL)
1310 		vrele(p->p_fd->fd_cdir);
1311 	p->p_fd->fd_cdir = rootvnode;
1312 	VREF(rootvnode);
1313 
1314 	if (p->p_fd->fd_rdir != NULL)
1315 		vrele(p->p_fd->fd_rdir);
1316 	p->p_fd->fd_rdir = rootvnode;
1317 	VREF(rootvnode);
1318 
1319 	FILEDESC_UNLOCK(p->p_fd);
1320 
1321 	VOP_UNLOCK(rootvnode, 0, td);
1322 }
1323 
1324 /*
1325  * Mount /devfs as our root filesystem, but do not put it on the mountlist
1326  * yet.  Create a /dev -> / symlink so that absolute pathnames will lookup.
1327  */
1328 
1329 static void
1330 devfs_first(void)
1331 {
1332 	struct thread *td = curthread;
1333 	struct vfsoptlist *opts;
1334 	struct vfsconf *vfsp;
1335 	struct mount *mp = NULL;
1336 	int error;
1337 
1338 	vfsp = vfs_byname("devfs");
1339 	KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1340 	if (vfsp == NULL)
1341 		return;
1342 
1343 	mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td);
1344 
1345 	error = VFS_MOUNT(mp, td);
1346 	KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1347 	if (error)
1348 		return;
1349 
1350 	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1351 	TAILQ_INIT(opts);
1352 	mp->mnt_opt = opts;
1353 
1354 	mtx_lock(&mountlist_mtx);
1355 	TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1356 	mtx_unlock(&mountlist_mtx);
1357 
1358 	set_rootvnode(td);
1359 
1360 	error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1361 	if (error)
1362 		printf("kern_symlink /dev -> / returns %d\n", error);
1363 }
1364 
1365 /*
1366  * Surgically move our devfs to be mounted on /dev.
1367  */
1368 
1369 static void
1370 devfs_fixup(struct thread *td)
1371 {
1372 	struct nameidata nd;
1373 	int error;
1374 	struct vnode *vp, *dvp;
1375 	struct mount *mp;
1376 
1377 	/* Remove our devfs mount from the mountlist and purge the cache */
1378 	mtx_lock(&mountlist_mtx);
1379 	mp = TAILQ_FIRST(&mountlist);
1380 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
1381 	mtx_unlock(&mountlist_mtx);
1382 	cache_purgevfs(mp);
1383 
1384 	VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1385 	VI_LOCK(dvp);
1386 	dvp->v_iflag &= ~VI_MOUNT;
1387 	dvp->v_mountedhere = NULL;
1388 	VI_UNLOCK(dvp);
1389 
1390 	/* Set up the real rootvnode, and purge the cache */
1391 	TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1392 	set_rootvnode(td);
1393 	cache_purgevfs(rootvnode->v_mount);
1394 
1395 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1396 	error = namei(&nd);
1397 	if (error) {
1398 		printf("Lookup of /dev for devfs, error: %d\n", error);
1399 		return;
1400 	}
1401 	NDFREE(&nd, NDF_ONLY_PNBUF);
1402 	vp = nd.ni_vp;
1403 	if (vp->v_type != VDIR) {
1404 		vput(vp);
1405 	}
1406 	error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1407 	if (error) {
1408 		vput(vp);
1409 	}
1410 	cache_purge(vp);
1411 	mp->mnt_vnodecovered = vp;
1412 	vp->v_mountedhere = mp;
1413 	mtx_lock(&mountlist_mtx);
1414 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1415 	mtx_unlock(&mountlist_mtx);
1416 	VOP_UNLOCK(vp, 0, td);
1417 	vput(dvp);
1418 	vfs_unbusy(mp, td);
1419 
1420 	/* Unlink the no longer needed /dev/dev -> / symlink */
1421 	kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1422 }
1423 
1424 /*
1425  * Report errors during filesystem mounting.
1426  */
1427 void
1428 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1429 {
1430 	struct vfsoptlist *moptlist = mp->mnt_optnew;
1431 	va_list ap;
1432 	int error, len;
1433 	char *errmsg;
1434 
1435 	error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1436 	if (error || errmsg == NULL || len <= 0)
1437 		return;
1438 
1439 	va_start(ap, fmt);
1440 	vsnprintf(errmsg, (size_t)len, fmt, ap);
1441 	va_end(ap);
1442 }
1443 
1444 /*
1445  * Find and mount the root filesystem
1446  */
1447 void
1448 vfs_mountroot(void)
1449 {
1450 	char *cp;
1451 	int error, i, asked = 0;
1452 
1453 	root_mount_wait();
1454 
1455 	mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
1456 	    NULL, NULL, mount_init, mount_fini,
1457 	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1458 	devfs_first();
1459 
1460 	/*
1461 	 * We are booted with instructions to prompt for the root filesystem.
1462 	 */
1463 	if (boothowto & RB_ASKNAME) {
1464 		if (!vfs_mountroot_ask())
1465 			return;
1466 		asked = 1;
1467 	}
1468 
1469 	/*
1470 	 * The root filesystem information is compiled in, and we are
1471 	 * booted with instructions to use it.
1472 	 */
1473 	if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1474 		if (!vfs_mountroot_try(ctrootdevname))
1475 			return;
1476 		ctrootdevname = NULL;
1477 	}
1478 
1479 	/*
1480 	 * We've been given the generic "use CDROM as root" flag.  This is
1481 	 * necessary because one media may be used in many different
1482 	 * devices, so we need to search for them.
1483 	 */
1484 	if (boothowto & RB_CDROM) {
1485 		for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1486 			if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1487 				return;
1488 		}
1489 	}
1490 
1491 	/*
1492 	 * Try to use the value read by the loader from /etc/fstab, or
1493 	 * supplied via some other means.  This is the preferred
1494 	 * mechanism.
1495 	 */
1496 	cp = getenv("vfs.root.mountfrom");
1497 	if (cp != NULL) {
1498 		error = vfs_mountroot_try(cp);
1499 		freeenv(cp);
1500 		if (!error)
1501 			return;
1502 	}
1503 
1504 	/*
1505 	 * Try values that may have been computed by code during boot
1506 	 */
1507 	if (!vfs_mountroot_try(rootdevnames[0]))
1508 		return;
1509 	if (!vfs_mountroot_try(rootdevnames[1]))
1510 		return;
1511 
1512 	/*
1513 	 * If we (still) have a compiled-in default, try it.
1514 	 */
1515 	if (ctrootdevname != NULL)
1516 		if (!vfs_mountroot_try(ctrootdevname))
1517 			return;
1518 	/*
1519 	 * Everything so far has failed, prompt on the console if we haven't
1520 	 * already tried that.
1521 	 */
1522 	if (!asked)
1523 		if (!vfs_mountroot_ask())
1524 			return;
1525 
1526 	panic("Root mount failed, startup aborted.");
1527 }
1528 
1529 /*
1530  * Mount (mountfrom) as the root filesystem.
1531  */
1532 static int
1533 vfs_mountroot_try(const char *mountfrom)
1534 {
1535 	struct mount	*mp;
1536 	char		*vfsname, *path;
1537 	time_t		timebase;
1538 	int		error;
1539 	char		patt[32];
1540 
1541 	vfsname = NULL;
1542 	path    = NULL;
1543 	mp      = NULL;
1544 	error   = EINVAL;
1545 
1546 	if (mountfrom == NULL)
1547 		return (error);		/* don't complain */
1548 	printf("Trying to mount root from %s\n", mountfrom);
1549 
1550 	/* parse vfs name and path */
1551 	vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1552 	path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1553 	vfsname[0] = path[0] = 0;
1554 	sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1555 	if (sscanf(mountfrom, patt, vfsname, path) < 1)
1556 		goto out;
1557 
1558 	if (path[0] == '\0')
1559 		strcpy(path, ROOTNAME);
1560 
1561 	error = kernel_vmount(
1562 	    MNT_RDONLY | MNT_ROOTFS,
1563 	    "fstype", vfsname,
1564 	    "fspath", "/",
1565 	    "from", path,
1566 	    NULL);
1567 	if (error == 0) {
1568 		/*
1569 		 * We mount devfs prior to mounting the / FS, so the first
1570 		 * entry will typically be devfs.
1571 		 */
1572 		mp = TAILQ_FIRST(&mountlist);
1573 		KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1574 
1575 		/*
1576 		 * Iterate over all currently mounted file systems and use
1577 		 * the time stamp found to check and/or initialize the RTC.
1578 		 * Typically devfs has no time stamp and the only other FS
1579 		 * is the actual / FS.
1580 		 * Call inittodr() only once and pass it the largest of the
1581 		 * timestamps we encounter.
1582 		 */
1583 		timebase = 0;
1584 		do {
1585 			if (mp->mnt_time > timebase)
1586 				timebase = mp->mnt_time;
1587 			mp = TAILQ_NEXT(mp, mnt_list);
1588 		} while (mp != NULL);
1589 		inittodr(timebase);
1590 
1591 		devfs_fixup(curthread);
1592 	}
1593 out:
1594 	free(path, M_MOUNT);
1595 	free(vfsname, M_MOUNT);
1596 	return (error);
1597 }
1598 
1599 /*
1600  * ---------------------------------------------------------------------
1601  * Interactive root filesystem selection code.
1602  */
1603 
1604 static int
1605 vfs_mountroot_ask(void)
1606 {
1607 	char name[128];
1608 
1609 	for(;;) {
1610 		printf("\nManual root filesystem specification:\n");
1611 		printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
1612 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
1613 		printf("                       eg. ufs:da0s1a\n");
1614 #else
1615 		printf("                       eg. ufs:/dev/da0a\n");
1616 #endif
1617 		printf("  ?                  List valid disk boot devices\n");
1618 		printf("  <empty line>       Abort manual input\n");
1619 		printf("\nmountroot> ");
1620 		gets(name, sizeof(name), 1);
1621 		if (name[0] == '\0')
1622 			return (1);
1623 		if (name[0] == '?') {
1624 			printf("\nList of GEOM managed disk devices:\n  ");
1625 			g_dev_print();
1626 			continue;
1627 		}
1628 		if (!vfs_mountroot_try(name))
1629 			return (0);
1630 	}
1631 }
1632 
1633 /*
1634  * ---------------------------------------------------------------------
1635  * Functions for querying mount options/arguments from filesystems.
1636  */
1637 
1638 /*
1639  * Check that no unknown options are given
1640  */
1641 int
1642 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1643 {
1644 	struct vfsopt *opt;
1645 	const char **t, *p;
1646 
1647 
1648 	TAILQ_FOREACH(opt, opts, link) {
1649 		p = opt->name;
1650 		if (p[0] == 'n' && p[1] == 'o')
1651 			p += 2;
1652 		for(t = global_opts; *t != NULL; t++)
1653 			if (!strcmp(*t, p))
1654 				break;
1655 		if (*t != NULL)
1656 			continue;
1657 		for(t = legal; *t != NULL; t++)
1658 			if (!strcmp(*t, p))
1659 				break;
1660 		if (*t != NULL)
1661 			continue;
1662 		printf("mount option <%s> is unknown\n", p);
1663 		return (EINVAL);
1664 	}
1665 	return (0);
1666 }
1667 
1668 /*
1669  * Get a mount option by its name.
1670  *
1671  * Return 0 if the option was found, ENOENT otherwise.
1672  * If len is non-NULL it will be filled with the length
1673  * of the option. If buf is non-NULL, it will be filled
1674  * with the address of the option.
1675  */
1676 int
1677 vfs_getopt(opts, name, buf, len)
1678 	struct vfsoptlist *opts;
1679 	const char *name;
1680 	void **buf;
1681 	int *len;
1682 {
1683 	struct vfsopt *opt;
1684 
1685 	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1686 
1687 	TAILQ_FOREACH(opt, opts, link) {
1688 		if (strcmp(name, opt->name) == 0) {
1689 			if (len != NULL)
1690 				*len = opt->len;
1691 			if (buf != NULL)
1692 				*buf = opt->value;
1693 			return (0);
1694 		}
1695 	}
1696 	return (ENOENT);
1697 }
1698 
1699 static int
1700 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1701 {
1702 	struct vfsopt *opt;
1703 	int i;
1704 
1705 	if (opts == NULL)
1706 		return (-1);
1707 
1708 	i = 0;
1709 	TAILQ_FOREACH(opt, opts, link) {
1710 		if (strcmp(name, opt->name) == 0)
1711 			return (i);
1712 		++i;
1713 	}
1714 	return (-1);
1715 }
1716 
1717 char *
1718 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1719 {
1720 	struct vfsopt *opt;
1721 
1722 	*error = 0;
1723 	TAILQ_FOREACH(opt, opts, link) {
1724 		if (strcmp(name, opt->name) != 0)
1725 			continue;
1726 		if (((char *)opt->value)[opt->len - 1] != '\0') {
1727 			*error = EINVAL;
1728 			return (NULL);
1729 		}
1730 		return (opt->value);
1731 	}
1732 	return (NULL);
1733 }
1734 
1735 int
1736 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1737 {
1738 	struct vfsopt *opt;
1739 
1740 	TAILQ_FOREACH(opt, opts, link) {
1741 		if (strcmp(name, opt->name) == 0) {
1742 			if (w != NULL)
1743 				*w |= val;
1744 			return (1);
1745 		}
1746 	}
1747 	if (w != NULL)
1748 		*w &= ~val;
1749 	return (0);
1750 }
1751 
1752 int
1753 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1754 {
1755 	va_list ap;
1756 	struct vfsopt *opt;
1757 	int ret;
1758 
1759 	KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1760 
1761 	TAILQ_FOREACH(opt, opts, link) {
1762 		if (strcmp(name, opt->name) != 0)
1763 			continue;
1764 		if (((char *)opt->value)[opt->len - 1] != '\0')
1765 			return (0);
1766 		va_start(ap, fmt);
1767 		ret = vsscanf(opt->value, fmt, ap);
1768 		va_end(ap);
1769 		return (ret);
1770 	}
1771 	return (0);
1772 }
1773 
1774 /*
1775  * Find and copy a mount option.
1776  *
1777  * The size of the buffer has to be specified
1778  * in len, if it is not the same length as the
1779  * mount option, EINVAL is returned.
1780  * Returns ENOENT if the option is not found.
1781  */
1782 int
1783 vfs_copyopt(opts, name, dest, len)
1784 	struct vfsoptlist *opts;
1785 	const char *name;
1786 	void *dest;
1787 	int len;
1788 {
1789 	struct vfsopt *opt;
1790 
1791 	KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1792 
1793 	TAILQ_FOREACH(opt, opts, link) {
1794 		if (strcmp(name, opt->name) == 0) {
1795 			if (len != opt->len)
1796 				return (EINVAL);
1797 			bcopy(opt->value, dest, opt->len);
1798 			return (0);
1799 		}
1800 	}
1801 	return (ENOENT);
1802 }
1803 
1804 /*
1805  * This is a helper function for filesystems to traverse their
1806  * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
1807  */
1808 
1809 struct vnode *
1810 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
1811 {
1812 	struct vnode *vp;
1813 
1814 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1815 
1816 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1817 	vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
1818 	while (vp != NULL && vp->v_type == VMARKER)
1819 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1820 
1821 	/* Check if we are done */
1822 	if (vp == NULL) {
1823 		__mnt_vnode_markerfree(mvp, mp);
1824 		return (NULL);
1825 	}
1826 	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1827 	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1828 	return (vp);
1829 }
1830 
1831 struct vnode *
1832 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
1833 {
1834 	struct vnode *vp;
1835 
1836 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1837 
1838 	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1839 	while (vp != NULL && vp->v_type == VMARKER)
1840 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1841 
1842 	/* Check if we are done */
1843 	if (vp == NULL) {
1844 		*mvp = NULL;
1845 		return (NULL);
1846 	}
1847 	mp->mnt_holdcnt++;
1848 	MNT_IUNLOCK(mp);
1849 	*mvp = (struct vnode *) malloc(sizeof(struct vnode),
1850 				       M_VNODE_MARKER,
1851 				       M_WAITOK | M_ZERO);
1852 	MNT_ILOCK(mp);
1853 	(*mvp)->v_type = VMARKER;
1854 
1855 	vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1856 	while (vp != NULL && vp->v_type == VMARKER)
1857 		vp = TAILQ_NEXT(vp, v_nmntvnodes);
1858 
1859 	/* Check if we are done */
1860 	if (vp == NULL) {
1861 		MNT_IUNLOCK(mp);
1862 		free(*mvp, M_VNODE_MARKER);
1863 		MNT_ILOCK(mp);
1864 		*mvp = NULL;
1865 		mp->mnt_holdcnt--;
1866 		if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1867 			wakeup(&mp->mnt_holdcnt);
1868 		return (NULL);
1869 	}
1870 	mp->mnt_markercnt++;
1871 	(*mvp)->v_mount = mp;
1872 	TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1873 	return (vp);
1874 }
1875 
1876 
1877 void
1878 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
1879 {
1880 
1881 	if (*mvp == NULL)
1882 		return;
1883 
1884 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1885 
1886 	KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1887 	TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1888 	MNT_IUNLOCK(mp);
1889 	free(*mvp, M_VNODE_MARKER);
1890 	MNT_ILOCK(mp);
1891 	*mvp = NULL;
1892 
1893 	mp->mnt_markercnt--;
1894 	mp->mnt_holdcnt--;
1895 	if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1896 		wakeup(&mp->mnt_holdcnt);
1897 }
1898 
1899 
1900 int
1901 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
1902 {
1903 	int error;
1904 
1905 	error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
1906 	if (sbp != &mp->mnt_stat)
1907 		*sbp = mp->mnt_stat;
1908 	return (error);
1909 }
1910 
1911 void
1912 vfs_mountedfrom(struct mount *mp, const char *from)
1913 {
1914 
1915 	bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1916 	strlcpy(mp->mnt_stat.f_mntfromname, from,
1917 	    sizeof mp->mnt_stat.f_mntfromname);
1918 }
1919 
1920 /*
1921  * ---------------------------------------------------------------------
1922  * This is the api for building mount args and mounting filesystems from
1923  * inside the kernel.
1924  *
1925  * The API works by accumulation of individual args.  First error is
1926  * latched.
1927  *
1928  * XXX: should be documented in new manpage kernel_mount(9)
1929  */
1930 
1931 /* A memory allocation which must be freed when we are done */
1932 struct mntaarg {
1933 	SLIST_ENTRY(mntaarg)	next;
1934 };
1935 
1936 /* The header for the mount arguments */
1937 struct mntarg {
1938 	struct iovec *v;
1939 	int len;
1940 	int error;
1941 	SLIST_HEAD(, mntaarg)	list;
1942 };
1943 
1944 /*
1945  * Add a boolean argument.
1946  *
1947  * flag is the boolean value.
1948  * name must start with "no".
1949  */
1950 struct mntarg *
1951 mount_argb(struct mntarg *ma, int flag, const char *name)
1952 {
1953 
1954 	KASSERT(name[0] == 'n' && name[1] == 'o',
1955 	    ("mount_argb(...,%s): name must start with 'no'", name));
1956 
1957 	return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1958 }
1959 
1960 /*
1961  * Add an argument printf style
1962  */
1963 struct mntarg *
1964 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1965 {
1966 	va_list ap;
1967 	struct mntaarg *maa;
1968 	struct sbuf *sb;
1969 	int len;
1970 
1971 	if (ma == NULL) {
1972 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1973 		SLIST_INIT(&ma->list);
1974 	}
1975 	if (ma->error)
1976 		return (ma);
1977 
1978 	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1979 	    M_MOUNT, M_WAITOK);
1980 	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1981 	ma->v[ma->len].iov_len = strlen(name) + 1;
1982 	ma->len++;
1983 
1984 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
1985 	va_start(ap, fmt);
1986 	sbuf_vprintf(sb, fmt, ap);
1987 	va_end(ap);
1988 	sbuf_finish(sb);
1989 	len = sbuf_len(sb) + 1;
1990 	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1991 	SLIST_INSERT_HEAD(&ma->list, maa, next);
1992 	bcopy(sbuf_data(sb), maa + 1, len);
1993 	sbuf_delete(sb);
1994 
1995 	ma->v[ma->len].iov_base = maa + 1;
1996 	ma->v[ma->len].iov_len = len;
1997 	ma->len++;
1998 
1999 	return (ma);
2000 }
2001 
2002 /*
2003  * Add an argument which is a userland string.
2004  */
2005 struct mntarg *
2006 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2007 {
2008 	struct mntaarg *maa;
2009 	char *tbuf;
2010 
2011 	if (val == NULL)
2012 		return (ma);
2013 	if (ma == NULL) {
2014 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2015 		SLIST_INIT(&ma->list);
2016 	}
2017 	if (ma->error)
2018 		return (ma);
2019 	maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2020 	SLIST_INSERT_HEAD(&ma->list, maa, next);
2021 	tbuf = (void *)(maa + 1);
2022 	ma->error = copyinstr(val, tbuf, len, NULL);
2023 	return (mount_arg(ma, name, tbuf, -1));
2024 }
2025 
2026 /*
2027  * Plain argument.
2028  *
2029  * If length is -1, use printf.
2030  */
2031 struct mntarg *
2032 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2033 {
2034 
2035 	if (ma == NULL) {
2036 		ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2037 		SLIST_INIT(&ma->list);
2038 	}
2039 	if (ma->error)
2040 		return (ma);
2041 
2042 	ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2043 	    M_MOUNT, M_WAITOK);
2044 	ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2045 	ma->v[ma->len].iov_len = strlen(name) + 1;
2046 	ma->len++;
2047 
2048 	ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2049 	if (len < 0)
2050 		ma->v[ma->len].iov_len = strlen(val) + 1;
2051 	else
2052 		ma->v[ma->len].iov_len = len;
2053 	ma->len++;
2054 	return (ma);
2055 }
2056 
2057 /*
2058  * Free a mntarg structure
2059  */
2060 static void
2061 free_mntarg(struct mntarg *ma)
2062 {
2063 	struct mntaarg *maa;
2064 
2065 	while (!SLIST_EMPTY(&ma->list)) {
2066 		maa = SLIST_FIRST(&ma->list);
2067 		SLIST_REMOVE_HEAD(&ma->list, next);
2068 		free(maa, M_MOUNT);
2069 	}
2070 	free(ma->v, M_MOUNT);
2071 	free(ma, M_MOUNT);
2072 }
2073 
2074 /*
2075  * Mount a filesystem
2076  */
2077 int
2078 kernel_mount(struct mntarg *ma, int flags)
2079 {
2080 	struct uio auio;
2081 	int error;
2082 
2083 	KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2084 	KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2085 	KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2086 
2087 	auio.uio_iov = ma->v;
2088 	auio.uio_iovcnt = ma->len;
2089 	auio.uio_segflg = UIO_SYSSPACE;
2090 
2091 	error = ma->error;
2092 	if (!error)
2093 		error = vfs_donmount(curthread, flags, &auio);
2094 	free_mntarg(ma);
2095 	return (error);
2096 }
2097 
2098 /*
2099  * A printflike function to mount a filesystem.
2100  */
2101 int
2102 kernel_vmount(int flags, ...)
2103 {
2104 	struct mntarg *ma = NULL;
2105 	va_list ap;
2106 	const char *cp;
2107 	const void *vp;
2108 	int error;
2109 
2110 	va_start(ap, flags);
2111 	for (;;) {
2112 		cp = va_arg(ap, const char *);
2113 		if (cp == NULL)
2114 			break;
2115 		vp = va_arg(ap, const void *);
2116 		ma = mount_arg(ma, cp, vp, -1);
2117 	}
2118 	va_end(ap);
2119 
2120 	error = kernel_mount(ma, flags);
2121 	return (error);
2122 }
2123