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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #include <sys/clock.h>
44 #include <sys/kernel.h>
45 #include <sys/libkern.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
52 #include <sys/filedesc.h>
53 #include <sys/reboot.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysproto.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysent.h>
59 #include <sys/systm.h>
60 #include <sys/vnode.h>
63 #include <geom/geom.h>
65 #include <machine/stdarg.h>
67 #include <security/audit/audit.h>
68 #include <security/mac/mac_framework.h>
70 #include "opt_rootdevname.h"
78 #define ROOTNAME "root_device"
79 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
81 static int vfs_domount(struct thread *td, const char *fstype,
82 char *fspath, int fsflags, void *fsdata);
83 static int vfs_mountroot_ask(void);
84 static int vfs_mountroot_try(const char *mountfrom);
85 static void free_mntarg(struct mntarg *ma);
86 static int vfs_getopt_pos(struct vfsoptlist *opts, const char *name);
88 static int usermount = 0;
89 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
90 "Unprivileged users may mount and unmount file systems");
92 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
93 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
94 static uma_zone_t mount_zone;
96 /* List of mounted filesystems. */
97 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
99 /* For any iteration/modification of mountlist */
100 struct mtx mountlist_mtx;
101 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
103 TAILQ_HEAD(vfsoptlist, vfsopt);
105 TAILQ_ENTRY(vfsopt) link;
112 * The vnode of the system's root (/ in the filesystem, without chroot
115 struct vnode *rootvnode;
118 * The root filesystem is detailed in the kernel environment variable
119 * vfs.root.mountfrom, which is expected to be in the general format
122 * vfsname := the name of a VFS known to the kernel and capable
123 * of being mounted as root
124 * path := disk device name or other data used by the filesystem
125 * to locate its physical store
129 * Global opts, taken by all filesystems
131 static const char *global_opts[] = {
143 * The root specifiers we will try if RB_CDROM is specified.
145 static char *cdrom_rootdevnames[] = {
151 /* legacy find-root code */
152 char *rootdevnames[2] = {NULL, NULL};
154 # define ROOTDEVNAME NULL
156 static const char *ctrootdevname = ROOTDEVNAME;
159 * ---------------------------------------------------------------------
160 * Functions for building and sanitizing the mount options
163 /* Remove one mount option. */
165 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
168 TAILQ_REMOVE(opts, opt, link);
169 free(opt->name, M_MOUNT);
170 if (opt->value != NULL)
171 free(opt->value, M_MOUNT);
173 else if (opt->len != 0)
174 panic("%s: mount option with NULL value but length != 0",
180 /* Release all resources related to the mount options. */
182 vfs_freeopts(struct vfsoptlist *opts)
186 while (!TAILQ_EMPTY(opts)) {
187 opt = TAILQ_FIRST(opts);
188 vfs_freeopt(opts, opt);
194 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
196 struct vfsopt *opt, *temp;
200 TAILQ_FOREACH_SAFE(opt, opts, link, temp) {
201 if (strcmp(opt->name, name) == 0)
202 vfs_freeopt(opts, opt);
207 * Check if options are equal (with or without the "no" prefix).
210 vfs_equalopts(const char *opt1, const char *opt2)
213 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
214 if (strcmp(opt1, opt2) == 0)
216 /* "noopt" vs. "opt" */
217 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
219 /* "opt" vs. "noopt" */
220 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
226 * If a mount option is specified several times,
227 * (with or without the "no" prefix) only keep
228 * the last occurence of it.
231 vfs_sanitizeopts(struct vfsoptlist *opts)
233 struct vfsopt *opt, *opt2, *tmp;
235 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
236 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
237 while (opt2 != NULL) {
238 if (vfs_equalopts(opt->name, opt2->name)) {
239 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
240 vfs_freeopt(opts, opt2);
243 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
250 * Build a linked list of mount options from a struct uio.
253 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
255 struct vfsoptlist *opts;
258 unsigned int i, iovcnt;
259 int error, namelen, optlen;
261 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
264 iovcnt = auio->uio_iovcnt;
265 for (i = 0; i < iovcnt; i += 2) {
266 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
267 namelen = auio->uio_iov[i].iov_len;
268 optlen = auio->uio_iov[i + 1].iov_len;
269 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
274 * Do this early, so jumps to "bad" will free the current
277 TAILQ_INSERT_TAIL(opts, opt, link);
278 memused += sizeof(struct vfsopt) + optlen + namelen;
281 * Avoid consuming too much memory, and attempts to overflow
284 if (memused > VFS_MOUNTARG_SIZE_MAX ||
285 optlen > VFS_MOUNTARG_SIZE_MAX ||
286 namelen > VFS_MOUNTARG_SIZE_MAX) {
291 if (auio->uio_segflg == UIO_SYSSPACE) {
292 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
294 error = copyin(auio->uio_iov[i].iov_base, opt->name,
299 /* Ensure names are null-terminated strings. */
300 if (opt->name[namelen - 1] != '\0') {
306 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
307 if (auio->uio_segflg == UIO_SYSSPACE) {
308 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
311 error = copyin(auio->uio_iov[i + 1].iov_base,
318 vfs_sanitizeopts(opts);
327 * Merge the old mount options with the new ones passed
328 * in the MNT_UPDATE case.
331 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
333 struct vfsopt *opt, *opt2, *new;
335 TAILQ_FOREACH(opt, opts, link) {
337 * Check that this option hasn't been redefined
338 * nor cancelled with a "no" mount option.
340 opt2 = TAILQ_FIRST(toopts);
341 while (opt2 != NULL) {
342 if (strcmp(opt2->name, opt->name) == 0)
344 if (strncmp(opt2->name, "no", 2) == 0 &&
345 strcmp(opt2->name + 2, opt->name) == 0) {
346 vfs_freeopt(toopts, opt2);
349 opt2 = TAILQ_NEXT(opt2, link);
351 /* We want this option, duplicate it. */
352 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
353 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
354 strcpy(new->name, opt->name);
356 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
357 bcopy(opt->value, new->value, opt->len);
362 TAILQ_INSERT_TAIL(toopts, new, link);
369 * Mount a filesystem.
374 struct nmount_args /* {
386 AUDIT_ARG(fflags, uap->flags);
389 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
390 * userspace to set this flag, but we must filter it out if we want
391 * MNT_UPDATE on the root file system to work.
392 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
394 uap->flags &= ~MNT_ROOTFS;
396 iovcnt = uap->iovcnt;
398 * Check that we have an even number of iovec's
399 * and that we have at least two options.
401 if ((iovcnt & 1) || (iovcnt < 4))
404 error = copyinuio(uap->iovp, iovcnt, &auio);
408 for (i = 0; i < iovcnt; i++) {
409 if (iov->iov_len > MMAXOPTIONLEN) {
415 error = vfs_donmount(td, uap->flags, auio);
422 * ---------------------------------------------------------------------
423 * Various utility functions
427 vfs_ref(struct mount *mp)
436 vfs_rel(struct mount *mp)
445 mount_init(void *mem, int size, int flags)
449 mp = (struct mount *)mem;
450 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
451 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
452 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
457 mount_fini(void *mem, int size)
461 mp = (struct mount *)mem;
462 lockdestroy(&mp->mnt_explock);
463 lockdestroy(&mp->mnt_lock);
464 mtx_destroy(&mp->mnt_mtx);
468 * Allocate and initialize the mount point struct.
471 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
472 const char *fspath, struct thread *td)
476 mp = uma_zalloc(mount_zone, M_WAITOK);
477 bzero(&mp->mnt_startzero,
478 __rangeof(struct mount, mnt_startzero, mnt_endzero));
479 TAILQ_INIT(&mp->mnt_nvnodelist);
480 mp->mnt_nvnodelistsize = 0;
482 (void) vfs_busy(mp, LK_NOWAIT, 0, td);
483 mp->mnt_op = vfsp->vfc_vfsops;
485 vfsp->vfc_refcount++; /* XXX Unlocked */
486 mp->mnt_stat.f_type = vfsp->vfc_typenum;
488 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
489 mp->mnt_vnodecovered = vp;
490 mp->mnt_cred = crdup(td->td_ucred);
491 mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
492 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
493 mp->mnt_iosize_max = DFLTPHYS;
496 mac_create_mount(td->td_ucred, mp);
498 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
503 * Destroy the mount struct previously allocated by vfs_mount_alloc().
506 vfs_mount_destroy(struct mount *mp)
511 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
512 if (mp->mnt_kern_flag & MNTK_MWAIT) {
513 mp->mnt_kern_flag &= ~MNTK_MWAIT;
516 for (i = 0; mp->mnt_ref && i < 3; i++)
517 msleep(mp, MNT_MTX(mp), PVFS, "mntref", hz);
519 * This will always cause a 3 second delay in rebooting due to
520 * refs on the root mountpoint that never go away. Most of these
521 * are held by init which never exits.
523 if (i == 3 && (!rebooting || bootverbose))
524 printf("Mount point %s had %d dangling refs\n",
525 mp->mnt_stat.f_mntonname, mp->mnt_ref);
526 if (mp->mnt_holdcnt != 0) {
527 printf("Waiting for mount point to be unheld\n");
528 while (mp->mnt_holdcnt != 0) {
529 mp->mnt_holdcntwaiters++;
530 msleep(&mp->mnt_holdcnt, MNT_MTX(mp),
531 PZERO, "mntdestroy", 0);
532 mp->mnt_holdcntwaiters--;
534 printf("mount point unheld\n");
536 if (mp->mnt_writeopcount > 0) {
537 printf("Waiting for mount point write ops\n");
538 while (mp->mnt_writeopcount > 0) {
539 mp->mnt_kern_flag |= MNTK_SUSPEND;
540 msleep(&mp->mnt_writeopcount,
542 PZERO, "mntdestroy2", 0);
544 printf("mount point write ops completed\n");
546 if (mp->mnt_secondary_writes > 0) {
547 printf("Waiting for mount point secondary write ops\n");
548 while (mp->mnt_secondary_writes > 0) {
549 mp->mnt_kern_flag |= MNTK_SUSPEND;
550 msleep(&mp->mnt_secondary_writes,
552 PZERO, "mntdestroy3", 0);
554 printf("mount point secondary write ops completed\n");
557 mp->mnt_vfc->vfc_refcount--;
558 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
561 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
563 panic("unmount: dangling vnode");
566 if (mp->mnt_kern_flag & MNTK_MWAIT)
568 if (mp->mnt_writeopcount != 0)
569 panic("vfs_mount_destroy: nonzero writeopcount");
570 if (mp->mnt_secondary_writes != 0)
571 panic("vfs_mount_destroy: nonzero secondary_writes");
572 if (mp->mnt_nvnodelistsize != 0)
573 panic("vfs_mount_destroy: nonzero nvnodelistsize");
574 mp->mnt_writeopcount = -1000;
575 mp->mnt_nvnodelistsize = -1000;
576 mp->mnt_secondary_writes = -1000;
579 mac_destroy_mount(mp);
581 if (mp->mnt_opt != NULL)
582 vfs_freeopts(mp->mnt_opt);
583 crfree(mp->mnt_cred);
584 uma_zfree(mount_zone, mp);
588 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
590 struct vfsoptlist *optlist;
591 struct vfsopt *opt, *noro_opt, *tmp_opt;
592 char *fstype, *fspath, *errmsg;
593 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
594 int has_rw, has_noro;
602 error = vfs_buildopts(fsoptions, &optlist);
606 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
607 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
610 * We need these two options before the others,
611 * and they are mandatory for any filesystem.
612 * Ensure they are NUL terminated as well.
615 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
616 if (error || fstype[fstypelen - 1] != '\0') {
619 strncpy(errmsg, "Invalid fstype", errmsg_len);
623 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
624 if (error || fspath[fspathlen - 1] != '\0') {
627 strncpy(errmsg, "Invalid fspath", errmsg_len);
632 * We need to see if we have the "update" option
633 * before we call vfs_domount(), since vfs_domount() has special
634 * logic based on MNT_UPDATE. This is very important
635 * when we want to update the root filesystem.
637 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
638 if (strcmp(opt->name, "update") == 0) {
639 fsflags |= MNT_UPDATE;
640 vfs_freeopt(optlist, opt);
642 else if (strcmp(opt->name, "async") == 0)
643 fsflags |= MNT_ASYNC;
644 else if (strcmp(opt->name, "force") == 0) {
645 fsflags |= MNT_FORCE;
646 vfs_freeopt(optlist, opt);
648 else if (strcmp(opt->name, "reload") == 0) {
649 fsflags |= MNT_RELOAD;
650 vfs_freeopt(optlist, opt);
652 else if (strcmp(opt->name, "multilabel") == 0)
653 fsflags |= MNT_MULTILABEL;
654 else if (strcmp(opt->name, "noasync") == 0)
655 fsflags &= ~MNT_ASYNC;
656 else if (strcmp(opt->name, "noatime") == 0)
657 fsflags |= MNT_NOATIME;
658 else if (strcmp(opt->name, "atime") == 0) {
659 free(opt->name, M_MOUNT);
660 opt->name = strdup("nonoatime", M_MOUNT);
662 else if (strcmp(opt->name, "noclusterr") == 0)
663 fsflags |= MNT_NOCLUSTERR;
664 else if (strcmp(opt->name, "clusterr") == 0) {
665 free(opt->name, M_MOUNT);
666 opt->name = strdup("nonoclusterr", M_MOUNT);
668 else if (strcmp(opt->name, "noclusterw") == 0)
669 fsflags |= MNT_NOCLUSTERW;
670 else if (strcmp(opt->name, "clusterw") == 0) {
671 free(opt->name, M_MOUNT);
672 opt->name = strdup("nonoclusterw", M_MOUNT);
674 else if (strcmp(opt->name, "noexec") == 0)
675 fsflags |= MNT_NOEXEC;
676 else if (strcmp(opt->name, "exec") == 0) {
677 free(opt->name, M_MOUNT);
678 opt->name = strdup("nonoexec", M_MOUNT);
680 else if (strcmp(opt->name, "nosuid") == 0)
681 fsflags |= MNT_NOSUID;
682 else if (strcmp(opt->name, "suid") == 0) {
683 free(opt->name, M_MOUNT);
684 opt->name = strdup("nonosuid", M_MOUNT);
686 else if (strcmp(opt->name, "nosymfollow") == 0)
687 fsflags |= MNT_NOSYMFOLLOW;
688 else if (strcmp(opt->name, "symfollow") == 0) {
689 free(opt->name, M_MOUNT);
690 opt->name = strdup("nonosymfollow", M_MOUNT);
692 else if (strcmp(opt->name, "noro") == 0) {
693 fsflags &= ~MNT_RDONLY;
696 else if (strcmp(opt->name, "rw") == 0) {
697 fsflags &= ~MNT_RDONLY;
700 else if (strcmp(opt->name, "ro") == 0)
701 fsflags |= MNT_RDONLY;
702 else if (strcmp(opt->name, "rdonly") == 0) {
703 free(opt->name, M_MOUNT);
704 opt->name = strdup("ro", M_MOUNT);
705 fsflags |= MNT_RDONLY;
707 else if (strcmp(opt->name, "suiddir") == 0)
708 fsflags |= MNT_SUIDDIR;
709 else if (strcmp(opt->name, "sync") == 0)
710 fsflags |= MNT_SYNCHRONOUS;
711 else if (strcmp(opt->name, "union") == 0)
712 fsflags |= MNT_UNION;
716 * If "rw" was specified as a mount option, and we
717 * are trying to update a mount-point from "ro" to "rw",
718 * we need a mount option "noro", since in vfs_mergeopts(),
719 * "noro" will cancel "ro", but "rw" will not do anything.
721 if (has_rw && !has_noro) {
722 noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
723 noro_opt->name = strdup("noro", M_MOUNT);
724 noro_opt->value = NULL;
726 TAILQ_INSERT_TAIL(optlist, noro_opt, link);
730 * Be ultra-paranoid about making sure the type and fspath
731 * variables will fit in our mp buffers, including the
734 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
735 error = ENAMETOOLONG;
740 error = vfs_domount(td, fstype, fspath, fsflags, optlist);
743 /* copyout the errmsg */
744 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
745 && errmsg_len > 0 && errmsg != NULL) {
746 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
748 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
749 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
752 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
753 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
758 vfs_freeopts(optlist);
765 #ifndef _SYS_SYSPROTO_H_
777 struct mount_args /* {
785 struct vfsconf *vfsp = NULL;
786 struct mntarg *ma = NULL;
789 AUDIT_ARG(fflags, uap->flags);
792 * Filter out MNT_ROOTFS. We do not want clients of mount() in
793 * userspace to set this flag, but we must filter it out if we want
794 * MNT_UPDATE on the root file system to work.
795 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
797 uap->flags &= ~MNT_ROOTFS;
799 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
800 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
802 free(fstype, M_TEMP);
806 AUDIT_ARG(text, fstype);
808 vfsp = vfs_byname_kld(fstype, td, &error);
809 free(fstype, M_TEMP);
814 if (vfsp->vfc_vfsops->vfs_cmount == NULL) {
819 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
820 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
821 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
822 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
823 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
825 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
832 * vfs_domount(): actually attempt a filesystem mount.
836 struct thread *td, /* Calling thread. */
837 const char *fstype, /* Filesystem type. */
838 char *fspath, /* Mount path. */
839 int fsflags, /* Flags common to all filesystems. */
840 void *fsdata /* Options local to the filesystem. */
845 struct vfsconf *vfsp;
846 struct export_args export;
851 mtx_assert(&Giant, MA_OWNED);
853 * Be ultra-paranoid about making sure the type and fspath
854 * variables will fit in our mp buffers, including the
857 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
858 return (ENAMETOOLONG);
860 if (jailed(td->td_ucred) || usermount == 0) {
861 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
866 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
868 if (fsflags & MNT_EXPORTED) {
869 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
873 if (fsflags & MNT_SUIDDIR) {
874 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
879 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
881 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
882 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
883 fsflags |= MNT_NOSUID | MNT_USER;
886 /* Load KLDs before we lock the covered vnode to avoid reversals. */
888 if ((fsflags & MNT_UPDATE) == 0) {
889 /* Don't try to load KLDs if we're mounting the root. */
890 if (fsflags & MNT_ROOTFS)
891 vfsp = vfs_byname(fstype);
893 vfsp = vfs_byname_kld(fstype, td, &error);
896 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
900 * Get vnode to be covered
902 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
904 if ((error = namei(&nd)) != 0)
906 NDFREE(&nd, NDF_ONLY_PNBUF);
908 if (fsflags & MNT_UPDATE) {
909 if ((vp->v_vflag & VV_ROOT) == 0) {
917 * We only allow the filesystem to be reloaded if it
918 * is currently mounted read-only.
920 if ((fsflags & MNT_RELOAD) &&
921 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
924 return (EOPNOTSUPP); /* Needs translation */
928 * Only privileged root, or (if MNT_USER is set) the user that
929 * did the original mount is permitted to update it.
931 error = vfs_suser(mp, td);
936 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
941 if ((vp->v_iflag & VI_MOUNT) != 0 ||
942 vp->v_mountedhere != NULL) {
948 vp->v_iflag |= VI_MOUNT;
951 mp->mnt_flag |= fsflags &
952 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
954 VOP_UNLOCK(vp, 0, td);
955 mp->mnt_optnew = fsdata;
956 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
959 * If the user is not root, ensure that they own the directory
960 * onto which we are attempting to mount.
962 error = VOP_GETATTR(vp, &va, td->td_ucred, td);
967 if (va.va_uid != td->td_ucred->cr_uid) {
968 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN,
975 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
980 if (vp->v_type != VDIR) {
985 if ((vp->v_iflag & VI_MOUNT) != 0 ||
986 vp->v_mountedhere != NULL) {
991 vp->v_iflag |= VI_MOUNT;
995 * Allocate and initialize the filesystem.
997 mp = vfs_mount_alloc(vp, vfsp, fspath, td);
998 VOP_UNLOCK(vp, 0, td);
1000 /* XXXMAC: pass to vfs_mount_alloc? */
1001 mp->mnt_optnew = fsdata;
1005 * Set the mount level flags.
1008 mp->mnt_flag = (mp->mnt_flag & ~MNT_UPDATEMASK) |
1009 (fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS |
1011 if ((mp->mnt_flag & MNT_ASYNC) == 0)
1012 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1015 * Mount the filesystem.
1016 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1017 * get. No freeing of cn_pnbuf.
1019 error = VFS_MOUNT(mp, td);
1022 * Process the export option only if we are
1023 * updating mount options.
1025 if (!error && (fsflags & MNT_UPDATE)) {
1026 if (vfs_copyopt(mp->mnt_optnew, "export", &export,
1027 sizeof(export)) == 0)
1028 error = vfs_export(mp, &export);
1032 if (mp->mnt_opt != NULL)
1033 vfs_freeopts(mp->mnt_opt);
1034 mp->mnt_opt = mp->mnt_optnew;
1035 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
1038 * Prevent external consumers of mount options from reading
1041 mp->mnt_optnew = NULL;
1042 if (mp->mnt_flag & MNT_UPDATE) {
1045 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) |
1046 (flag & ~MNT_QUOTA);
1048 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD |
1049 MNT_FORCE | MNT_SNAPSHOT);
1050 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1051 mp->mnt_kern_flag |= MNTK_ASYNC;
1053 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1055 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1056 if (mp->mnt_syncer == NULL)
1057 error = vfs_allocate_syncvnode(mp);
1059 if (mp->mnt_syncer != NULL)
1060 vrele(mp->mnt_syncer);
1061 mp->mnt_syncer = NULL;
1065 vp->v_iflag &= ~VI_MOUNT;
1071 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1072 mp->mnt_kern_flag |= MNTK_ASYNC;
1074 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1076 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1078 * Put the new filesystem on the mount list after root.
1082 struct vnode *newdp;
1085 vp->v_iflag &= ~VI_MOUNT;
1087 vp->v_mountedhere = mp;
1088 mtx_lock(&mountlist_mtx);
1089 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1090 mtx_unlock(&mountlist_mtx);
1091 vfs_event_signal(NULL, VQ_MOUNT, 0);
1092 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
1093 panic("mount: lost mount");
1094 mountcheckdirs(vp, newdp);
1096 VOP_UNLOCK(vp, 0, td);
1097 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1098 error = vfs_allocate_syncvnode(mp);
1104 vp->v_iflag &= ~VI_MOUNT;
1107 vfs_mount_destroy(mp);
1114 * Unmount a filesystem.
1116 * Note: unmount takes a path to the vnode mounted on as argument, not
1117 * special file (as before).
1119 #ifndef _SYS_SYSPROTO_H_
1120 struct unmount_args {
1129 register struct unmount_args /* {
1136 int error, id0, id1;
1138 if (jailed(td->td_ucred) || usermount == 0) {
1139 error = priv_check(td, PRIV_VFS_UNMOUNT);
1144 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1145 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1147 free(pathbuf, M_TEMP);
1150 AUDIT_ARG(upath, td, pathbuf, ARG_UPATH1);
1152 if (uap->flags & MNT_BYFSID) {
1153 /* Decode the filesystem ID. */
1154 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1156 free(pathbuf, M_TEMP);
1160 mtx_lock(&mountlist_mtx);
1161 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1162 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1163 mp->mnt_stat.f_fsid.val[1] == id1)
1166 mtx_unlock(&mountlist_mtx);
1168 mtx_lock(&mountlist_mtx);
1169 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1170 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1173 mtx_unlock(&mountlist_mtx);
1175 free(pathbuf, M_TEMP);
1178 * Previously we returned ENOENT for a nonexistent path and
1179 * EINVAL for a non-mountpoint. We cannot tell these apart
1180 * now, so in the !MNT_BYFSID case return the more likely
1181 * EINVAL for compatibility.
1184 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1188 * Don't allow unmounting the root filesystem.
1190 if (mp->mnt_flag & MNT_ROOTFS) {
1194 error = dounmount(mp, uap->flags, td);
1200 * Do the actual filesystem unmount.
1203 dounmount(mp, flags, td)
1208 struct vnode *coveredvp, *fsrootvp;
1213 mtx_assert(&Giant, MA_OWNED);
1215 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1216 mnt_gen_r = mp->mnt_gen;
1219 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY, td);
1222 * Check for mp being unmounted while waiting for the
1223 * covered vnode lock.
1225 if (coveredvp->v_mountedhere != mp ||
1226 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1227 VOP_UNLOCK(coveredvp, 0, td);
1232 * Only privileged root, or (if MNT_USER is set) the user that did the
1233 * original mount is permitted to unmount this filesystem.
1235 error = vfs_suser(mp, td);
1238 VOP_UNLOCK(coveredvp, 0, td);
1243 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1246 VOP_UNLOCK(coveredvp, 0, td);
1249 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1250 /* Allow filesystems to detect that a forced unmount is in progress. */
1251 if (flags & MNT_FORCE)
1252 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1253 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1254 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
1257 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_NOINSMNTQ |
1259 if (mp->mnt_kern_flag & MNTK_MWAIT)
1263 VOP_UNLOCK(coveredvp, 0, td);
1266 vn_start_write(NULL, &mp, V_WAIT);
1268 if (mp->mnt_flag & MNT_EXPUBLIC)
1269 vfs_setpublicfs(NULL, NULL, NULL);
1271 vfs_msync(mp, MNT_WAIT);
1273 async_flag = mp->mnt_flag & MNT_ASYNC;
1274 mp->mnt_flag &= ~MNT_ASYNC;
1275 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1277 cache_purgevfs(mp); /* remove cache entries for this file sys */
1278 if (mp->mnt_syncer != NULL)
1279 vrele(mp->mnt_syncer);
1281 * For forced unmounts, move process cdir/rdir refs on the fs root
1282 * vnode to the covered vnode. For non-forced unmounts we want
1283 * such references to cause an EBUSY error.
1285 if ((flags & MNT_FORCE) &&
1286 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1287 if (mp->mnt_vnodecovered != NULL)
1288 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1289 if (fsrootvp == rootvnode) {
1295 if (((mp->mnt_flag & MNT_RDONLY) ||
1296 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
1297 (flags & MNT_FORCE)) {
1298 error = VFS_UNMOUNT(mp, flags, td);
1300 vn_finished_write(mp);
1302 * If we failed to flush the dirty blocks for this mount point,
1303 * undo all the cdir/rdir and rootvnode changes we made above.
1304 * Unless we failed to do so because the device is reporting that
1305 * it doesn't exist anymore.
1307 if (error && error != ENXIO) {
1308 if ((flags & MNT_FORCE) &&
1309 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1310 if (mp->mnt_vnodecovered != NULL)
1311 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1312 if (rootvnode == NULL) {
1313 rootvnode = fsrootvp;
1319 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1320 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) {
1322 (void) vfs_allocate_syncvnode(mp);
1325 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1326 mp->mnt_flag |= async_flag;
1327 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1328 mp->mnt_kern_flag |= MNTK_ASYNC;
1329 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1330 if (mp->mnt_kern_flag & MNTK_MWAIT)
1334 VOP_UNLOCK(coveredvp, 0, td);
1337 mtx_lock(&mountlist_mtx);
1338 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1339 mtx_unlock(&mountlist_mtx);
1340 if (coveredvp != NULL) {
1341 coveredvp->v_mountedhere = NULL;
1344 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1345 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1346 vfs_mount_destroy(mp);
1351 * ---------------------------------------------------------------------
1352 * Mounting of root filesystem
1356 struct root_hold_token {
1358 LIST_ENTRY(root_hold_token) list;
1361 static LIST_HEAD(, root_hold_token) root_holds =
1362 LIST_HEAD_INITIALIZER(&root_holds);
1364 static int root_mount_complete;
1369 struct root_hold_token *
1370 root_mount_hold(const char *identifier)
1372 struct root_hold_token *h;
1374 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1375 h->who = identifier;
1376 mtx_lock(&mountlist_mtx);
1377 LIST_INSERT_HEAD(&root_holds, h, list);
1378 mtx_unlock(&mountlist_mtx);
1383 * Release root mount.
1386 root_mount_rel(struct root_hold_token *h)
1389 mtx_lock(&mountlist_mtx);
1390 LIST_REMOVE(h, list);
1391 wakeup(&root_holds);
1392 mtx_unlock(&mountlist_mtx);
1397 * Wait for all subsystems to release root mount.
1400 root_mount_prepare(void)
1402 struct root_hold_token *h;
1408 mtx_lock(&mountlist_mtx);
1409 if (LIST_EMPTY(&root_holds)) {
1410 mtx_unlock(&mountlist_mtx);
1413 printf("Root mount waiting for:");
1414 LIST_FOREACH(h, &root_holds, list)
1415 printf(" %s", h->who);
1417 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1423 * Root was mounted, share the good news.
1426 root_mount_done(void)
1430 * Use a mutex to prevent the wakeup being missed and waiting for
1431 * an extra 1 second sleep.
1433 mtx_lock(&mountlist_mtx);
1434 root_mount_complete = 1;
1435 wakeup(&root_mount_complete);
1436 mtx_unlock(&mountlist_mtx);
1440 * Return true if root is already mounted.
1446 /* No mutex is acquired here because int stores are atomic. */
1447 return (root_mount_complete);
1451 * Wait until root is mounted.
1454 root_mount_wait(void)
1458 * Panic on an obvious deadlock - the function can't be called from
1459 * a thread which is doing the whole SYSINIT stuff.
1461 KASSERT(curthread->td_proc->p_pid != 0,
1462 ("root_mount_wait: cannot be called from the swapper thread"));
1463 mtx_lock(&mountlist_mtx);
1464 while (!root_mount_complete) {
1465 msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait",
1468 mtx_unlock(&mountlist_mtx);
1472 set_rootvnode(struct thread *td)
1476 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1477 panic("Cannot find root vnode");
1480 FILEDESC_SLOCK(p->p_fd);
1482 if (p->p_fd->fd_cdir != NULL)
1483 vrele(p->p_fd->fd_cdir);
1484 p->p_fd->fd_cdir = rootvnode;
1487 if (p->p_fd->fd_rdir != NULL)
1488 vrele(p->p_fd->fd_rdir);
1489 p->p_fd->fd_rdir = rootvnode;
1492 FILEDESC_SUNLOCK(p->p_fd);
1494 VOP_UNLOCK(rootvnode, 0, td);
1498 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1499 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup.
1505 struct thread *td = curthread;
1506 struct vfsoptlist *opts;
1507 struct vfsconf *vfsp;
1508 struct mount *mp = NULL;
1511 vfsp = vfs_byname("devfs");
1512 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1516 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td);
1518 error = VFS_MOUNT(mp, td);
1519 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1523 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1527 mtx_lock(&mountlist_mtx);
1528 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1529 mtx_unlock(&mountlist_mtx);
1533 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1535 printf("kern_symlink /dev -> / returns %d\n", error);
1539 * Surgically move our devfs to be mounted on /dev.
1543 devfs_fixup(struct thread *td)
1545 struct nameidata nd;
1547 struct vnode *vp, *dvp;
1550 /* Remove our devfs mount from the mountlist and purge the cache */
1551 mtx_lock(&mountlist_mtx);
1552 mp = TAILQ_FIRST(&mountlist);
1553 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1554 mtx_unlock(&mountlist_mtx);
1557 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1559 dvp->v_iflag &= ~VI_MOUNT;
1561 dvp->v_mountedhere = NULL;
1563 /* Set up the real rootvnode, and purge the cache */
1564 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1566 cache_purgevfs(rootvnode->v_mount);
1568 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1571 printf("Lookup of /dev for devfs, error: %d\n", error);
1574 NDFREE(&nd, NDF_ONLY_PNBUF);
1576 if (vp->v_type != VDIR) {
1579 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1584 mp->mnt_vnodecovered = vp;
1585 vp->v_mountedhere = mp;
1586 mtx_lock(&mountlist_mtx);
1587 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1588 mtx_unlock(&mountlist_mtx);
1589 VOP_UNLOCK(vp, 0, td);
1593 /* Unlink the no longer needed /dev/dev -> / symlink */
1594 kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1598 * Report errors during filesystem mounting.
1601 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1603 struct vfsoptlist *moptlist = mp->mnt_optnew;
1608 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1609 if (error || errmsg == NULL || len <= 0)
1613 vsnprintf(errmsg, (size_t)len, fmt, ap);
1618 * Find and mount the root filesystem
1624 int error, i, asked = 0;
1626 root_mount_prepare();
1628 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
1629 NULL, NULL, mount_init, mount_fini,
1630 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1634 * We are booted with instructions to prompt for the root filesystem.
1636 if (boothowto & RB_ASKNAME) {
1637 if (!vfs_mountroot_ask())
1643 * The root filesystem information is compiled in, and we are
1644 * booted with instructions to use it.
1646 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1647 if (!vfs_mountroot_try(ctrootdevname))
1649 ctrootdevname = NULL;
1653 * We've been given the generic "use CDROM as root" flag. This is
1654 * necessary because one media may be used in many different
1655 * devices, so we need to search for them.
1657 if (boothowto & RB_CDROM) {
1658 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1659 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1665 * Try to use the value read by the loader from /etc/fstab, or
1666 * supplied via some other means. This is the preferred
1669 cp = getenv("vfs.root.mountfrom");
1671 error = vfs_mountroot_try(cp);
1678 * Try values that may have been computed by code during boot
1680 if (!vfs_mountroot_try(rootdevnames[0]))
1682 if (!vfs_mountroot_try(rootdevnames[1]))
1686 * If we (still) have a compiled-in default, try it.
1688 if (ctrootdevname != NULL)
1689 if (!vfs_mountroot_try(ctrootdevname))
1692 * Everything so far has failed, prompt on the console if we haven't
1693 * already tried that.
1696 if (!vfs_mountroot_ask())
1699 panic("Root mount failed, startup aborted.");
1706 * Mount (mountfrom) as the root filesystem.
1709 vfs_mountroot_try(const char *mountfrom)
1712 char *vfsname, *path;
1722 if (mountfrom == NULL)
1723 return (error); /* don't complain */
1724 printf("Trying to mount root from %s\n", mountfrom);
1726 /* parse vfs name and path */
1727 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1728 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1729 vfsname[0] = path[0] = 0;
1730 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1731 if (sscanf(mountfrom, patt, vfsname, path) < 1)
1734 if (path[0] == '\0')
1735 strcpy(path, ROOTNAME);
1737 error = kernel_vmount(
1738 MNT_RDONLY | MNT_ROOTFS,
1745 * We mount devfs prior to mounting the / FS, so the first
1746 * entry will typically be devfs.
1748 mp = TAILQ_FIRST(&mountlist);
1749 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1752 * Iterate over all currently mounted file systems and use
1753 * the time stamp found to check and/or initialize the RTC.
1754 * Typically devfs has no time stamp and the only other FS
1755 * is the actual / FS.
1756 * Call inittodr() only once and pass it the largest of the
1757 * timestamps we encounter.
1761 if (mp->mnt_time > timebase)
1762 timebase = mp->mnt_time;
1763 mp = TAILQ_NEXT(mp, mnt_list);
1764 } while (mp != NULL);
1767 devfs_fixup(curthread);
1770 free(path, M_MOUNT);
1771 free(vfsname, M_MOUNT);
1776 * ---------------------------------------------------------------------
1777 * Interactive root filesystem selection code.
1781 vfs_mountroot_ask(void)
1786 printf("\nManual root filesystem specification:\n");
1787 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
1788 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
1789 printf(" eg. ufs:da0s1a\n");
1791 printf(" eg. ufs:/dev/da0a\n");
1793 printf(" ? List valid disk boot devices\n");
1794 printf(" <empty line> Abort manual input\n");
1795 printf("\nmountroot> ");
1796 gets(name, sizeof(name), 1);
1797 if (name[0] == '\0')
1799 if (name[0] == '?') {
1800 printf("\nList of GEOM managed disk devices:\n ");
1804 if (!vfs_mountroot_try(name))
1810 * ---------------------------------------------------------------------
1811 * Functions for querying mount options/arguments from filesystems.
1815 * Check that no unknown options are given
1818 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1822 const char **t, *p, *q;
1825 TAILQ_FOREACH(opt, opts, link) {
1828 if (p[0] == 'n' && p[1] == 'o')
1830 for(t = global_opts; *t != NULL; t++) {
1831 if (strcmp(*t, p) == 0)
1834 if (strcmp(*t, q) == 0)
1840 for(t = legal; *t != NULL; t++) {
1841 if (strcmp(*t, p) == 0)
1844 if (strcmp(*t, q) == 0)
1850 snprintf(errmsg, sizeof(errmsg),
1851 "mount option <%s> is unknown", p);
1852 printf("%s\n", errmsg);
1856 TAILQ_FOREACH(opt, opts, link) {
1857 if (strcmp(opt->name, "errmsg") == 0) {
1858 strncpy((char *)opt->value, errmsg, opt->len);
1866 * Get a mount option by its name.
1868 * Return 0 if the option was found, ENOENT otherwise.
1869 * If len is non-NULL it will be filled with the length
1870 * of the option. If buf is non-NULL, it will be filled
1871 * with the address of the option.
1874 vfs_getopt(opts, name, buf, len)
1875 struct vfsoptlist *opts;
1882 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1884 TAILQ_FOREACH(opt, opts, link) {
1885 if (strcmp(name, opt->name) == 0) {
1897 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1906 TAILQ_FOREACH(opt, opts, link) {
1907 if (strcmp(name, opt->name) == 0)
1915 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1920 TAILQ_FOREACH(opt, opts, link) {
1921 if (strcmp(name, opt->name) != 0)
1923 if (((char *)opt->value)[opt->len - 1] != '\0') {
1927 return (opt->value);
1934 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1938 TAILQ_FOREACH(opt, opts, link) {
1939 if (strcmp(name, opt->name) == 0) {
1951 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1957 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1959 TAILQ_FOREACH(opt, opts, link) {
1960 if (strcmp(name, opt->name) != 0)
1962 if (opt->len == 0 || opt->value == NULL)
1964 if (((char *)opt->value)[opt->len - 1] != '\0')
1967 ret = vsscanf(opt->value, fmt, ap);
1975 * Find and copy a mount option.
1977 * The size of the buffer has to be specified
1978 * in len, if it is not the same length as the
1979 * mount option, EINVAL is returned.
1980 * Returns ENOENT if the option is not found.
1983 vfs_copyopt(opts, name, dest, len)
1984 struct vfsoptlist *opts;
1991 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1993 TAILQ_FOREACH(opt, opts, link) {
1994 if (strcmp(name, opt->name) == 0) {
1995 if (len != opt->len)
1997 bcopy(opt->value, dest, opt->len);
2005 * This is a helper function for filesystems to traverse their
2006 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h
2010 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
2014 mtx_assert(MNT_MTX(mp), MA_OWNED);
2016 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2017 if ((*mvp)->v_yield++ == 500) {
2019 (*mvp)->v_yield = 0;
2023 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
2024 while (vp != NULL && vp->v_type == VMARKER)
2025 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2027 /* Check if we are done */
2029 __mnt_vnode_markerfree(mvp, mp);
2032 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2033 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2038 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
2042 mtx_assert(MNT_MTX(mp), MA_OWNED);
2044 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2045 while (vp != NULL && vp->v_type == VMARKER)
2046 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2048 /* Check if we are done */
2055 *mvp = (struct vnode *) malloc(sizeof(struct vnode),
2059 (*mvp)->v_type = VMARKER;
2061 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2062 while (vp != NULL && vp->v_type == VMARKER)
2063 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2065 /* Check if we are done */
2068 free(*mvp, M_VNODE_MARKER);
2072 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
2073 wakeup(&mp->mnt_holdcnt);
2076 mp->mnt_markercnt++;
2077 (*mvp)->v_mount = mp;
2078 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2084 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
2090 mtx_assert(MNT_MTX(mp), MA_OWNED);
2092 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2093 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2095 free(*mvp, M_VNODE_MARKER);
2099 mp->mnt_markercnt--;
2101 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
2102 wakeup(&mp->mnt_holdcnt);
2107 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
2111 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
2112 if (sbp != &mp->mnt_stat)
2113 *sbp = mp->mnt_stat;
2118 vfs_mountedfrom(struct mount *mp, const char *from)
2121 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2122 strlcpy(mp->mnt_stat.f_mntfromname, from,
2123 sizeof mp->mnt_stat.f_mntfromname);
2127 * ---------------------------------------------------------------------
2128 * This is the api for building mount args and mounting filesystems from
2129 * inside the kernel.
2131 * The API works by accumulation of individual args. First error is
2134 * XXX: should be documented in new manpage kernel_mount(9)
2137 /* A memory allocation which must be freed when we are done */
2139 SLIST_ENTRY(mntaarg) next;
2142 /* The header for the mount arguments */
2147 SLIST_HEAD(, mntaarg) list;
2151 * Add a boolean argument.
2153 * flag is the boolean value.
2154 * name must start with "no".
2157 mount_argb(struct mntarg *ma, int flag, const char *name)
2160 KASSERT(name[0] == 'n' && name[1] == 'o',
2161 ("mount_argb(...,%s): name must start with 'no'", name));
2163 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2167 * Add an argument printf style
2170 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2173 struct mntaarg *maa;
2178 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2179 SLIST_INIT(&ma->list);
2184 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2186 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2187 ma->v[ma->len].iov_len = strlen(name) + 1;
2190 sb = sbuf_new_auto();
2192 sbuf_vprintf(sb, fmt, ap);
2195 len = sbuf_len(sb) + 1;
2196 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2197 SLIST_INSERT_HEAD(&ma->list, maa, next);
2198 bcopy(sbuf_data(sb), maa + 1, len);
2201 ma->v[ma->len].iov_base = maa + 1;
2202 ma->v[ma->len].iov_len = len;
2209 * Add an argument which is a userland string.
2212 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2214 struct mntaarg *maa;
2220 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2221 SLIST_INIT(&ma->list);
2225 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2226 SLIST_INSERT_HEAD(&ma->list, maa, next);
2227 tbuf = (void *)(maa + 1);
2228 ma->error = copyinstr(val, tbuf, len, NULL);
2229 return (mount_arg(ma, name, tbuf, -1));
2235 * If length is -1, use printf.
2238 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2242 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2243 SLIST_INIT(&ma->list);
2248 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2250 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2251 ma->v[ma->len].iov_len = strlen(name) + 1;
2254 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2256 ma->v[ma->len].iov_len = strlen(val) + 1;
2258 ma->v[ma->len].iov_len = len;
2264 * Free a mntarg structure
2267 free_mntarg(struct mntarg *ma)
2269 struct mntaarg *maa;
2271 while (!SLIST_EMPTY(&ma->list)) {
2272 maa = SLIST_FIRST(&ma->list);
2273 SLIST_REMOVE_HEAD(&ma->list, next);
2276 free(ma->v, M_MOUNT);
2281 * Mount a filesystem
2284 kernel_mount(struct mntarg *ma, int flags)
2289 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2290 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2291 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2293 auio.uio_iov = ma->v;
2294 auio.uio_iovcnt = ma->len;
2295 auio.uio_segflg = UIO_SYSSPACE;
2299 error = vfs_donmount(curthread, flags, &auio);
2305 * A printflike function to mount a filesystem.
2308 kernel_vmount(int flags, ...)
2310 struct mntarg *ma = NULL;
2316 va_start(ap, flags);
2318 cp = va_arg(ap, const char *);
2321 vp = va_arg(ap, const void *);
2322 ma = mount_arg(ma, cp, vp, -1);
2326 error = kernel_mount(ma, flags);