The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_mount.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: releng/8.3/sys/kern/vfs_mount.c 229978 2012-01-11 19:12:29Z mckusick $");
   39 
   40 #include <sys/param.h>
   41 #include <sys/conf.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/jail.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>
   50 #include <sys/priv.h>
   51 #include <sys/proc.h>
   52 #include <sys/filedesc.h>
   53 #include <sys/reboot.h>
   54 #include <sys/syscallsubr.h>
   55 #include <sys/sysproto.h>
   56 #include <sys/sx.h>
   57 #include <sys/sysctl.h>
   58 #include <sys/sysent.h>
   59 #include <sys/systm.h>
   60 #include <sys/vnode.h>
   61 #include <vm/uma.h>
   62 
   63 #include <geom/geom.h>
   64 
   65 #include <machine/stdarg.h>
   66 
   67 #include <security/audit/audit.h>
   68 #include <security/mac/mac_framework.h>
   69 
   70 #include "opt_rootdevname.h"
   71 
   72 #define ROOTNAME                "root_device"
   73 #define VFS_MOUNTARG_SIZE_MAX   (1024 * 64)
   74 
   75 static void     set_rootvnode(void);
   76 static int      vfs_domount(struct thread *td, const char *fstype,
   77                     char *fspath, int fsflags, void *fsdata);
   78 static int      vfs_mountroot_ask(void);
   79 static int      vfs_mountroot_try(const char *mountfrom, const char *options);
   80 static void     free_mntarg(struct mntarg *ma);
   81 
   82 static int      usermount = 0;
   83 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
   84     "Unprivileged users may mount and unmount file systems");
   85 
   86 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
   87 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
   88 static uma_zone_t mount_zone;
   89 
   90 /* List of mounted filesystems. */
   91 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
   92 
   93 /* For any iteration/modification of mountlist */
   94 struct mtx mountlist_mtx;
   95 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
   96 
   97 /*
   98  * The vnode of the system's root (/ in the filesystem, without chroot
   99  * active.)
  100  */
  101 struct vnode    *rootvnode;
  102 
  103 /*
  104  * The root filesystem is detailed in the kernel environment variable
  105  * vfs.root.mountfrom, which is expected to be in the general format
  106  *
  107  * <vfsname>:[<path>][  <vfsname>:[<path>] ...]
  108  * vfsname   := the name of a VFS known to the kernel and capable
  109  *              of being mounted as root
  110  * path      := disk device name or other data used by the filesystem
  111  *              to locate its physical store
  112  *
  113  * If the environment variable vfs.root.mountfrom is a space separated list,
  114  * each list element is tried in turn and the root filesystem will be mounted
  115  * from the first one that suceeds.
  116  *
  117  * The environment variable vfs.root.mountfrom.options is a comma delimited
  118  * set of string mount options.  These mount options must be parseable
  119  * by nmount() in the kernel.
  120  */
  121 
  122 /*
  123  * Global opts, taken by all filesystems
  124  */
  125 static const char *global_opts[] = {
  126         "errmsg",
  127         "fstype",
  128         "fspath",
  129         "ro",
  130         "rw",
  131         "nosuid",
  132         "noexec",
  133         NULL
  134 };
  135 
  136 /*
  137  * The root specifiers we will try if RB_CDROM is specified.
  138  */
  139 static char *cdrom_rootdevnames[] = {
  140         "cd9660:cd0",
  141         "cd9660:acd0",
  142         NULL
  143 };
  144 
  145 /* legacy find-root code */
  146 char            *rootdevnames[2] = {NULL, NULL};
  147 #ifndef ROOTDEVNAME
  148 #  define ROOTDEVNAME NULL
  149 #endif
  150 static const char       *ctrootdevname = ROOTDEVNAME;
  151 
  152 /*
  153  * ---------------------------------------------------------------------
  154  * Functions for building and sanitizing the mount options
  155  */
  156 
  157 /* Remove one mount option. */
  158 static void
  159 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
  160 {
  161 
  162         TAILQ_REMOVE(opts, opt, link);
  163         free(opt->name, M_MOUNT);
  164         if (opt->value != NULL)
  165                 free(opt->value, M_MOUNT);
  166         free(opt, M_MOUNT);
  167 }
  168 
  169 /* Release all resources related to the mount options. */
  170 void
  171 vfs_freeopts(struct vfsoptlist *opts)
  172 {
  173         struct vfsopt *opt;
  174 
  175         while (!TAILQ_EMPTY(opts)) {
  176                 opt = TAILQ_FIRST(opts);
  177                 vfs_freeopt(opts, opt);
  178         }
  179         free(opts, M_MOUNT);
  180 }
  181 
  182 void
  183 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
  184 {
  185         struct vfsopt *opt, *temp;
  186 
  187         if (opts == NULL)
  188                 return;
  189         TAILQ_FOREACH_SAFE(opt, opts, link, temp)  {
  190                 if (strcmp(opt->name, name) == 0)
  191                         vfs_freeopt(opts, opt);
  192         }
  193 }
  194 
  195 static int
  196 vfs_isopt_ro(const char *opt)
  197 {
  198 
  199         if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
  200             strcmp(opt, "norw") == 0)
  201                 return (1);
  202         return (0);
  203 }
  204 
  205 static int
  206 vfs_isopt_rw(const char *opt)
  207 {
  208 
  209         if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
  210                 return (1);
  211         return (0);
  212 }
  213 
  214 /*
  215  * Check if options are equal (with or without the "no" prefix).
  216  */
  217 static int
  218 vfs_equalopts(const char *opt1, const char *opt2)
  219 {
  220         char *p;
  221 
  222         /* "opt" vs. "opt" or "noopt" vs. "noopt" */
  223         if (strcmp(opt1, opt2) == 0)
  224                 return (1);
  225         /* "noopt" vs. "opt" */
  226         if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
  227                 return (1);
  228         /* "opt" vs. "noopt" */
  229         if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
  230                 return (1);
  231         while ((p = strchr(opt1, '.')) != NULL &&
  232             !strncmp(opt1, opt2, ++p - opt1)) {
  233                 opt2 += p - opt1;
  234                 opt1 = p;
  235                 /* "foo.noopt" vs. "foo.opt" */
  236                 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
  237                         return (1);
  238                 /* "foo.opt" vs. "foo.noopt" */
  239                 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
  240                         return (1);
  241         }
  242         /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
  243         if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
  244             (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
  245                 return (1);
  246         return (0);
  247 }
  248 
  249 /*
  250  * If a mount option is specified several times,
  251  * (with or without the "no" prefix) only keep
  252  * the last occurence of it.
  253  */
  254 static void
  255 vfs_sanitizeopts(struct vfsoptlist *opts)
  256 {
  257         struct vfsopt *opt, *opt2, *tmp;
  258 
  259         TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
  260                 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
  261                 while (opt2 != NULL) {
  262                         if (vfs_equalopts(opt->name, opt2->name)) {
  263                                 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
  264                                 vfs_freeopt(opts, opt2);
  265                                 opt2 = tmp;
  266                         } else {
  267                                 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
  268                         }
  269                 }
  270         }
  271 }
  272 
  273 /*
  274  * Build a linked list of mount options from a struct uio.
  275  */
  276 int
  277 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
  278 {
  279         struct vfsoptlist *opts;
  280         struct vfsopt *opt;
  281         size_t memused, namelen, optlen;
  282         unsigned int i, iovcnt;
  283         int error;
  284 
  285         opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
  286         TAILQ_INIT(opts);
  287         memused = 0;
  288         iovcnt = auio->uio_iovcnt;
  289         for (i = 0; i < iovcnt; i += 2) {
  290                 namelen = auio->uio_iov[i].iov_len;
  291                 optlen = auio->uio_iov[i + 1].iov_len;
  292                 memused += sizeof(struct vfsopt) + optlen + namelen;
  293                 /*
  294                  * Avoid consuming too much memory, and attempts to overflow
  295                  * memused.
  296                  */
  297                 if (memused > VFS_MOUNTARG_SIZE_MAX ||
  298                     optlen > VFS_MOUNTARG_SIZE_MAX ||
  299                     namelen > VFS_MOUNTARG_SIZE_MAX) {
  300                         error = EINVAL;
  301                         goto bad;
  302                 }
  303 
  304                 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
  305                 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
  306                 opt->value = NULL;
  307                 opt->len = 0;
  308                 opt->pos = i / 2;
  309                 opt->seen = 0;
  310 
  311                 /*
  312                  * Do this early, so jumps to "bad" will free the current
  313                  * option.
  314                  */
  315                 TAILQ_INSERT_TAIL(opts, opt, link);
  316 
  317                 if (auio->uio_segflg == UIO_SYSSPACE) {
  318                         bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
  319                 } else {
  320                         error = copyin(auio->uio_iov[i].iov_base, opt->name,
  321                             namelen);
  322                         if (error)
  323                                 goto bad;
  324                 }
  325                 /* Ensure names are null-terminated strings. */
  326                 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
  327                         error = EINVAL;
  328                         goto bad;
  329                 }
  330                 if (optlen != 0) {
  331                         opt->len = optlen;
  332                         opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
  333                         if (auio->uio_segflg == UIO_SYSSPACE) {
  334                                 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
  335                                     optlen);
  336                         } else {
  337                                 error = copyin(auio->uio_iov[i + 1].iov_base,
  338                                     opt->value, optlen);
  339                                 if (error)
  340                                         goto bad;
  341                         }
  342                 }
  343         }
  344         vfs_sanitizeopts(opts);
  345         *options = opts;
  346         return (0);
  347 bad:
  348         vfs_freeopts(opts);
  349         return (error);
  350 }
  351 
  352 /*
  353  * Merge the old mount options with the new ones passed
  354  * in the MNT_UPDATE case.
  355  *
  356  * XXX This function will keep a "nofoo" option in the
  357  *     new options if there is no matching "foo" option
  358  *     to be cancelled in the old options.  This is a bug
  359  *     if the option's canonical name is "foo".  E.g., "noro"
  360  *     shouldn't end up in the mount point's active options,
  361  *     but it can.
  362  */
  363 static void
  364 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
  365 {
  366         struct vfsopt *opt, *opt2, *new;
  367 
  368         TAILQ_FOREACH(opt, opts, link) {
  369                 /*
  370                  * Check that this option hasn't been redefined
  371                  * nor cancelled with a "no" mount option.
  372                  */
  373                 opt2 = TAILQ_FIRST(toopts);
  374                 while (opt2 != NULL) {
  375                         if (strcmp(opt2->name, opt->name) == 0)
  376                                 goto next;
  377                         if (strncmp(opt2->name, "no", 2) == 0 &&
  378                             strcmp(opt2->name + 2, opt->name) == 0) {
  379                                 vfs_freeopt(toopts, opt2);
  380                                 goto next;
  381                         }
  382                         opt2 = TAILQ_NEXT(opt2, link);
  383                 }
  384                 /* We want this option, duplicate it. */
  385                 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
  386                 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
  387                 strcpy(new->name, opt->name);
  388                 if (opt->len != 0) {
  389                         new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
  390                         bcopy(opt->value, new->value, opt->len);
  391                 } else {
  392                         new->value = NULL;
  393                 }
  394                 new->len = opt->len;
  395                 new->seen = opt->seen;
  396                 TAILQ_INSERT_TAIL(toopts, new, link);
  397 next:
  398                 continue;
  399         }
  400 }
  401 
  402 /*
  403  * Mount a filesystem.
  404  */
  405 int
  406 nmount(td, uap)
  407         struct thread *td;
  408         struct nmount_args /* {
  409                 struct iovec *iovp;
  410                 unsigned int iovcnt;
  411                 int flags;
  412         } */ *uap;
  413 {
  414         struct uio *auio;
  415         int error;
  416         u_int iovcnt;
  417 
  418         AUDIT_ARG_FFLAGS(uap->flags);
  419         CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
  420             uap->iovp, uap->iovcnt, uap->flags);
  421 
  422         /*
  423          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
  424          * userspace to set this flag, but we must filter it out if we want
  425          * MNT_UPDATE on the root file system to work.
  426          * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
  427          */
  428         uap->flags &= ~MNT_ROOTFS;
  429 
  430         iovcnt = uap->iovcnt;
  431         /*
  432          * Check that we have an even number of iovec's
  433          * and that we have at least two options.
  434          */
  435         if ((iovcnt & 1) || (iovcnt < 4)) {
  436                 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
  437                     uap->iovcnt);
  438                 return (EINVAL);
  439         }
  440 
  441         error = copyinuio(uap->iovp, iovcnt, &auio);
  442         if (error) {
  443                 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
  444                     __func__, error);
  445                 return (error);
  446         }
  447         error = vfs_donmount(td, uap->flags, auio);
  448 
  449         free(auio, M_IOV);
  450         return (error);
  451 }
  452 
  453 /*
  454  * ---------------------------------------------------------------------
  455  * Various utility functions
  456  */
  457 
  458 void
  459 vfs_ref(struct mount *mp)
  460 {
  461 
  462         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
  463         MNT_ILOCK(mp);
  464         MNT_REF(mp);
  465         MNT_IUNLOCK(mp);
  466 }
  467 
  468 void
  469 vfs_rel(struct mount *mp)
  470 {
  471 
  472         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
  473         MNT_ILOCK(mp);
  474         MNT_REL(mp);
  475         MNT_IUNLOCK(mp);
  476 }
  477 
  478 static int
  479 mount_init(void *mem, int size, int flags)
  480 {
  481         struct mount *mp;
  482 
  483         mp = (struct mount *)mem;
  484         mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
  485         lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
  486         return (0);
  487 }
  488 
  489 static void
  490 mount_fini(void *mem, int size)
  491 {
  492         struct mount *mp;
  493 
  494         mp = (struct mount *)mem;
  495         lockdestroy(&mp->mnt_explock);
  496         mtx_destroy(&mp->mnt_mtx);
  497 }
  498 
  499 /*
  500  * Allocate and initialize the mount point struct.
  501  */
  502 struct mount *
  503 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
  504     struct ucred *cred)
  505 {
  506         struct mount *mp;
  507 
  508         mp = uma_zalloc(mount_zone, M_WAITOK);
  509         bzero(&mp->mnt_startzero,
  510             __rangeof(struct mount, mnt_startzero, mnt_endzero));
  511         TAILQ_INIT(&mp->mnt_nvnodelist);
  512         mp->mnt_nvnodelistsize = 0;
  513         mp->mnt_ref = 0;
  514         (void) vfs_busy(mp, MBF_NOWAIT);
  515         mp->mnt_op = vfsp->vfc_vfsops;
  516         mp->mnt_vfc = vfsp;
  517         vfsp->vfc_refcount++;   /* XXX Unlocked */
  518         mp->mnt_stat.f_type = vfsp->vfc_typenum;
  519         mp->mnt_gen++;
  520         strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
  521         mp->mnt_vnodecovered = vp;
  522         mp->mnt_cred = crdup(cred);
  523         mp->mnt_stat.f_owner = cred->cr_uid;
  524         strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
  525         mp->mnt_iosize_max = DFLTPHYS;
  526 #ifdef MAC
  527         mac_mount_init(mp);
  528         mac_mount_create(cred, mp);
  529 #endif
  530         arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
  531         return (mp);
  532 }
  533 
  534 /*
  535  * Destroy the mount struct previously allocated by vfs_mount_alloc().
  536  */
  537 void
  538 vfs_mount_destroy(struct mount *mp)
  539 {
  540 
  541         MNT_ILOCK(mp);
  542         mp->mnt_kern_flag |= MNTK_REFEXPIRE;
  543         if (mp->mnt_kern_flag & MNTK_MWAIT) {
  544                 mp->mnt_kern_flag &= ~MNTK_MWAIT;
  545                 wakeup(mp);
  546         }
  547         while (mp->mnt_ref)
  548                 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
  549         KASSERT(mp->mnt_ref == 0,
  550             ("%s: invalid refcount in the drain path @ %s:%d", __func__,
  551             __FILE__, __LINE__));
  552         if (mp->mnt_writeopcount != 0)
  553                 panic("vfs_mount_destroy: nonzero writeopcount");
  554         if (mp->mnt_secondary_writes != 0)
  555                 panic("vfs_mount_destroy: nonzero secondary_writes");
  556         mp->mnt_vfc->vfc_refcount--;
  557         if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
  558                 struct vnode *vp;
  559 
  560                 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
  561                         vprint("", vp);
  562                 panic("unmount: dangling vnode");
  563         }
  564         if (mp->mnt_nvnodelistsize != 0)
  565                 panic("vfs_mount_destroy: nonzero nvnodelistsize");
  566         if (mp->mnt_lockref != 0)
  567                 panic("vfs_mount_destroy: nonzero lock refcount");
  568         MNT_IUNLOCK(mp);
  569 #ifdef MAC
  570         mac_mount_destroy(mp);
  571 #endif
  572         if (mp->mnt_opt != NULL)
  573                 vfs_freeopts(mp->mnt_opt);
  574         crfree(mp->mnt_cred);
  575         uma_zfree(mount_zone, mp);
  576 }
  577 
  578 int
  579 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
  580 {
  581         struct vfsoptlist *optlist;
  582         struct vfsopt *opt, *noro_opt, *tmp_opt;
  583         char *fstype, *fspath, *errmsg;
  584         int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
  585         int has_rw, has_noro;
  586 
  587         errmsg = fspath = NULL;
  588         errmsg_len = has_noro = has_rw = fspathlen = 0;
  589         errmsg_pos = -1;
  590 
  591         error = vfs_buildopts(fsoptions, &optlist);
  592         if (error)
  593                 return (error);
  594 
  595         if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
  596                 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
  597 
  598         /*
  599          * We need these two options before the others,
  600          * and they are mandatory for any filesystem.
  601          * Ensure they are NUL terminated as well.
  602          */
  603         fstypelen = 0;
  604         error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
  605         if (error || fstype[fstypelen - 1] != '\0') {
  606                 error = EINVAL;
  607                 if (errmsg != NULL)
  608                         strncpy(errmsg, "Invalid fstype", errmsg_len);
  609                 goto bail;
  610         }
  611         fspathlen = 0;
  612         error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
  613         if (error || fspath[fspathlen - 1] != '\0') {
  614                 error = EINVAL;
  615                 if (errmsg != NULL)
  616                         strncpy(errmsg, "Invalid fspath", errmsg_len);
  617                 goto bail;
  618         }
  619 
  620         /*
  621          * We need to see if we have the "update" option
  622          * before we call vfs_domount(), since vfs_domount() has special
  623          * logic based on MNT_UPDATE.  This is very important
  624          * when we want to update the root filesystem.
  625          */
  626         TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
  627                 if (strcmp(opt->name, "update") == 0) {
  628                         fsflags |= MNT_UPDATE;
  629                         vfs_freeopt(optlist, opt);
  630                 }
  631                 else if (strcmp(opt->name, "async") == 0)
  632                         fsflags |= MNT_ASYNC;
  633                 else if (strcmp(opt->name, "force") == 0) {
  634                         fsflags |= MNT_FORCE;
  635                         vfs_freeopt(optlist, opt);
  636                 }
  637                 else if (strcmp(opt->name, "reload") == 0) {
  638                         fsflags |= MNT_RELOAD;
  639                         vfs_freeopt(optlist, opt);
  640                 }
  641                 else if (strcmp(opt->name, "multilabel") == 0)
  642                         fsflags |= MNT_MULTILABEL;
  643                 else if (strcmp(opt->name, "noasync") == 0)
  644                         fsflags &= ~MNT_ASYNC;
  645                 else if (strcmp(opt->name, "noatime") == 0)
  646                         fsflags |= MNT_NOATIME;
  647                 else if (strcmp(opt->name, "atime") == 0) {
  648                         free(opt->name, M_MOUNT);
  649                         opt->name = strdup("nonoatime", M_MOUNT);
  650                 }
  651                 else if (strcmp(opt->name, "noclusterr") == 0)
  652                         fsflags |= MNT_NOCLUSTERR;
  653                 else if (strcmp(opt->name, "clusterr") == 0) {
  654                         free(opt->name, M_MOUNT);
  655                         opt->name = strdup("nonoclusterr", M_MOUNT);
  656                 }
  657                 else if (strcmp(opt->name, "noclusterw") == 0)
  658                         fsflags |= MNT_NOCLUSTERW;
  659                 else if (strcmp(opt->name, "clusterw") == 0) {
  660                         free(opt->name, M_MOUNT);
  661                         opt->name = strdup("nonoclusterw", M_MOUNT);
  662                 }
  663                 else if (strcmp(opt->name, "noexec") == 0)
  664                         fsflags |= MNT_NOEXEC;
  665                 else if (strcmp(opt->name, "exec") == 0) {
  666                         free(opt->name, M_MOUNT);
  667                         opt->name = strdup("nonoexec", M_MOUNT);
  668                 }
  669                 else if (strcmp(opt->name, "nosuid") == 0)
  670                         fsflags |= MNT_NOSUID;
  671                 else if (strcmp(opt->name, "suid") == 0) {
  672                         free(opt->name, M_MOUNT);
  673                         opt->name = strdup("nonosuid", M_MOUNT);
  674                 }
  675                 else if (strcmp(opt->name, "nosymfollow") == 0)
  676                         fsflags |= MNT_NOSYMFOLLOW;
  677                 else if (strcmp(opt->name, "symfollow") == 0) {
  678                         free(opt->name, M_MOUNT);
  679                         opt->name = strdup("nonosymfollow", M_MOUNT);
  680                 }
  681                 else if (strcmp(opt->name, "noro") == 0) {
  682                         fsflags &= ~MNT_RDONLY;
  683                         has_noro = 1;
  684                 }
  685                 else if (strcmp(opt->name, "rw") == 0) {
  686                         fsflags &= ~MNT_RDONLY;
  687                         has_rw = 1;
  688                 }
  689                 else if (strcmp(opt->name, "ro") == 0)
  690                         fsflags |= MNT_RDONLY;
  691                 else if (strcmp(opt->name, "rdonly") == 0) {
  692                         free(opt->name, M_MOUNT);
  693                         opt->name = strdup("ro", M_MOUNT);
  694                         fsflags |= MNT_RDONLY;
  695                 }
  696                 else if (strcmp(opt->name, "suiddir") == 0)
  697                         fsflags |= MNT_SUIDDIR;
  698                 else if (strcmp(opt->name, "sync") == 0)
  699                         fsflags |= MNT_SYNCHRONOUS;
  700                 else if (strcmp(opt->name, "union") == 0)
  701                         fsflags |= MNT_UNION;
  702         }
  703 
  704         /*
  705          * If "rw" was specified as a mount option, and we
  706          * are trying to update a mount-point from "ro" to "rw",
  707          * we need a mount option "noro", since in vfs_mergeopts(),
  708          * "noro" will cancel "ro", but "rw" will not do anything.
  709          */
  710         if (has_rw && !has_noro) {
  711                 noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
  712                 noro_opt->name = strdup("noro", M_MOUNT);
  713                 noro_opt->value = NULL;
  714                 noro_opt->len = 0;
  715                 noro_opt->pos = -1;
  716                 noro_opt->seen = 1;
  717                 TAILQ_INSERT_TAIL(optlist, noro_opt, link);
  718         }
  719 
  720         /*
  721          * Be ultra-paranoid about making sure the type and fspath
  722          * variables will fit in our mp buffers, including the
  723          * terminating NUL.
  724          */
  725         if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
  726                 error = ENAMETOOLONG;
  727                 goto bail;
  728         }
  729 
  730         mtx_lock(&Giant);
  731         error = vfs_domount(td, fstype, fspath, fsflags, optlist);
  732         mtx_unlock(&Giant);
  733 bail:
  734         /* copyout the errmsg */
  735         if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
  736             && errmsg_len > 0 && errmsg != NULL) {
  737                 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
  738                         bcopy(errmsg,
  739                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
  740                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
  741                 } else {
  742                         copyout(errmsg,
  743                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
  744                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
  745                 }
  746         }
  747 
  748         if (error != 0)
  749                 vfs_freeopts(optlist);
  750         return (error);
  751 }
  752 
  753 /*
  754  * Old mount API.
  755  */
  756 #ifndef _SYS_SYSPROTO_H_
  757 struct mount_args {
  758         char    *type;
  759         char    *path;
  760         int     flags;
  761         caddr_t data;
  762 };
  763 #endif
  764 /* ARGSUSED */
  765 int
  766 mount(td, uap)
  767         struct thread *td;
  768         struct mount_args /* {
  769                 char *type;
  770                 char *path;
  771                 int flags;
  772                 caddr_t data;
  773         } */ *uap;
  774 {
  775         char *fstype;
  776         struct vfsconf *vfsp = NULL;
  777         struct mntarg *ma = NULL;
  778         int error;
  779 
  780         AUDIT_ARG_FFLAGS(uap->flags);
  781 
  782         /*
  783          * Filter out MNT_ROOTFS.  We do not want clients of mount() in
  784          * userspace to set this flag, but we must filter it out if we want
  785          * MNT_UPDATE on the root file system to work.
  786          * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
  787          */
  788         uap->flags &= ~MNT_ROOTFS;
  789 
  790         fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
  791         error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
  792         if (error) {
  793                 free(fstype, M_TEMP);
  794                 return (error);
  795         }
  796 
  797         AUDIT_ARG_TEXT(fstype);
  798         mtx_lock(&Giant);
  799         vfsp = vfs_byname_kld(fstype, td, &error);
  800         free(fstype, M_TEMP);
  801         if (vfsp == NULL) {
  802                 mtx_unlock(&Giant);
  803                 return (ENOENT);
  804         }
  805         if (vfsp->vfc_vfsops->vfs_cmount == NULL) {
  806                 mtx_unlock(&Giant);
  807                 return (EOPNOTSUPP);
  808         }
  809 
  810         ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
  811         ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
  812         ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
  813         ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
  814         ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
  815 
  816         error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags);
  817         mtx_unlock(&Giant);
  818         return (error);
  819 }
  820 
  821 
  822 /*
  823  * vfs_domount(): actually attempt a filesystem mount.
  824  */
  825 static int
  826 vfs_domount(
  827         struct thread *td,      /* Calling thread. */
  828         const char *fstype,     /* Filesystem type. */
  829         char *fspath,           /* Mount path. */
  830         int fsflags,            /* Flags common to all filesystems. */
  831         void *fsdata            /* Options local to the filesystem. */
  832         )
  833 {
  834         struct vnode *vp;
  835         struct mount *mp;
  836         struct vfsconf *vfsp;
  837         struct oexport_args oexport;
  838         struct export_args export;
  839         int error, flag = 0;
  840         struct vattr va;
  841         struct nameidata nd;
  842 
  843         mtx_assert(&Giant, MA_OWNED);
  844         /*
  845          * Be ultra-paranoid about making sure the type and fspath
  846          * variables will fit in our mp buffers, including the
  847          * terminating NUL.
  848          */
  849         if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
  850                 return (ENAMETOOLONG);
  851 
  852         if (jailed(td->td_ucred) || usermount == 0) {
  853                 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
  854                         return (error);
  855         }
  856 
  857         /*
  858          * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
  859          */
  860         if (fsflags & MNT_EXPORTED) {
  861                 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
  862                 if (error)
  863                         return (error);
  864         }
  865         if (fsflags & MNT_SUIDDIR) {
  866                 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
  867                 if (error)
  868                         return (error);
  869         }
  870         /*
  871          * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
  872          */
  873         if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
  874                 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
  875                         fsflags |= MNT_NOSUID | MNT_USER;
  876         }
  877 
  878         /* Load KLDs before we lock the covered vnode to avoid reversals. */
  879         vfsp = NULL;
  880         if ((fsflags & MNT_UPDATE) == 0) {
  881                 /* Don't try to load KLDs if we're mounting the root. */
  882                 if (fsflags & MNT_ROOTFS)
  883                         vfsp = vfs_byname(fstype);
  884                 else
  885                         vfsp = vfs_byname_kld(fstype, td, &error);
  886                 if (vfsp == NULL)
  887                         return (ENODEV);
  888                 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
  889                         return (EPERM);
  890         }
  891         /*
  892          * Get vnode to be covered
  893          */
  894         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
  895             fspath, td);
  896         if ((error = namei(&nd)) != 0)
  897                 return (error);
  898         NDFREE(&nd, NDF_ONLY_PNBUF);
  899         vp = nd.ni_vp;
  900         if (fsflags & MNT_UPDATE) {
  901                 if ((vp->v_vflag & VV_ROOT) == 0) {
  902                         vput(vp);
  903                         return (EINVAL);
  904                 }
  905                 mp = vp->v_mount;
  906                 MNT_ILOCK(mp);
  907                 flag = mp->mnt_flag;
  908                 /*
  909                  * We only allow the filesystem to be reloaded if it
  910                  * is currently mounted read-only.
  911                  */
  912                 if ((fsflags & MNT_RELOAD) &&
  913                     ((mp->mnt_flag & MNT_RDONLY) == 0)) {
  914                         MNT_IUNLOCK(mp);
  915                         vput(vp);
  916                         return (EOPNOTSUPP);    /* Needs translation */
  917                 }
  918                 MNT_IUNLOCK(mp);
  919                 /*
  920                  * Only privileged root, or (if MNT_USER is set) the user that
  921                  * did the original mount is permitted to update it.
  922                  */
  923                 error = vfs_suser(mp, td);
  924                 if (error) {
  925                         vput(vp);
  926                         return (error);
  927                 }
  928                 if (vfs_busy(mp, MBF_NOWAIT)) {
  929                         vput(vp);
  930                         return (EBUSY);
  931                 }
  932                 VI_LOCK(vp);
  933                 if ((vp->v_iflag & VI_MOUNT) != 0 ||
  934                     vp->v_mountedhere != NULL) {
  935                         VI_UNLOCK(vp);
  936                         vfs_unbusy(mp);
  937                         vput(vp);
  938                         return (EBUSY);
  939                 }
  940                 vp->v_iflag |= VI_MOUNT;
  941                 VI_UNLOCK(vp);
  942                 MNT_ILOCK(mp);
  943                 mp->mnt_flag |= fsflags &
  944                     (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
  945                 MNT_IUNLOCK(mp);
  946                 VOP_UNLOCK(vp, 0);
  947                 mp->mnt_optnew = fsdata;
  948                 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
  949         } else {
  950                 /*
  951                  * If the user is not root, ensure that they own the directory
  952                  * onto which we are attempting to mount.
  953                  */
  954                 error = VOP_GETATTR(vp, &va, td->td_ucred);
  955                 if (error) {
  956                         vput(vp);
  957                         return (error);
  958                 }
  959                 if (va.va_uid != td->td_ucred->cr_uid) {
  960                         error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN,
  961                             0);
  962                         if (error) {
  963                                 vput(vp);
  964                                 return (error);
  965                         }
  966                 }
  967                 error = vinvalbuf(vp, V_SAVE, 0, 0);
  968                 if (error != 0) {
  969                         vput(vp);
  970                         return (error);
  971                 }
  972                 if (vp->v_type != VDIR) {
  973                         vput(vp);
  974                         return (ENOTDIR);
  975                 }
  976                 VI_LOCK(vp);
  977                 if ((vp->v_iflag & VI_MOUNT) != 0 ||
  978                     vp->v_mountedhere != NULL) {
  979                         VI_UNLOCK(vp);
  980                         vput(vp);
  981                         return (EBUSY);
  982                 }
  983                 vp->v_iflag |= VI_MOUNT;
  984                 VI_UNLOCK(vp);
  985 
  986                 /*
  987                  * Allocate and initialize the filesystem.
  988                  */
  989                 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
  990                 VOP_UNLOCK(vp, 0);
  991 
  992                 /* XXXMAC: pass to vfs_mount_alloc? */
  993                 mp->mnt_optnew = fsdata;
  994         }
  995 
  996         /*
  997          * Set the mount level flags.
  998          */
  999         MNT_ILOCK(mp);
 1000         mp->mnt_flag = (mp->mnt_flag & ~MNT_UPDATEMASK) |
 1001                 (fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS |
 1002                             MNT_RDONLY));
 1003         if ((mp->mnt_flag & MNT_ASYNC) == 0)
 1004                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
 1005         MNT_IUNLOCK(mp);
 1006         /*
 1007          * Mount the filesystem.
 1008          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
 1009          * get.  No freeing of cn_pnbuf.
 1010          */
 1011         error = VFS_MOUNT(mp);
 1012 
 1013         /*
 1014          * Process the export option only if we are
 1015          * updating mount options.
 1016          */
 1017         if (!error && (fsflags & MNT_UPDATE)) {
 1018                 if (vfs_copyopt(mp->mnt_optnew, "export", &export,
 1019                     sizeof(export)) == 0)
 1020                         error = vfs_export(mp, &export);
 1021                 else if (vfs_copyopt(mp->mnt_optnew, "export", &oexport,
 1022                         sizeof(oexport)) == 0) {
 1023                         export.ex_flags = oexport.ex_flags;
 1024                         export.ex_root = oexport.ex_root;
 1025                         export.ex_anon = oexport.ex_anon;
 1026                         export.ex_addr = oexport.ex_addr;
 1027                         export.ex_addrlen = oexport.ex_addrlen;
 1028                         export.ex_mask = oexport.ex_mask;
 1029                         export.ex_masklen = oexport.ex_masklen;
 1030                         export.ex_indexfile = oexport.ex_indexfile;
 1031                         export.ex_numsecflavors = 0;
 1032                         error = vfs_export(mp, &export);
 1033                 }
 1034         }
 1035 
 1036         if (!error) {
 1037                 if (mp->mnt_opt != NULL)
 1038                         vfs_freeopts(mp->mnt_opt);
 1039                 mp->mnt_opt = mp->mnt_optnew;
 1040                 (void)VFS_STATFS(mp, &mp->mnt_stat);
 1041         }
 1042         /*
 1043          * Prevent external consumers of mount options from reading
 1044          * mnt_optnew.
 1045         */
 1046         mp->mnt_optnew = NULL;
 1047         if (mp->mnt_flag & MNT_UPDATE) {
 1048                 MNT_ILOCK(mp);
 1049                 if (error)
 1050                         mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) |
 1051                                 (flag & ~MNT_QUOTA);
 1052                 else
 1053                         mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD |
 1054                                           MNT_FORCE | MNT_SNAPSHOT);
 1055                 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
 1056                         mp->mnt_kern_flag |= MNTK_ASYNC;
 1057                 else
 1058                         mp->mnt_kern_flag &= ~MNTK_ASYNC;
 1059                 MNT_IUNLOCK(mp);
 1060                 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
 1061                         if (mp->mnt_syncer == NULL)
 1062                                 error = vfs_allocate_syncvnode(mp);
 1063                 } else {
 1064                         if (mp->mnt_syncer != NULL)
 1065                                 vrele(mp->mnt_syncer);
 1066                         mp->mnt_syncer = NULL;
 1067                 }
 1068                 vfs_unbusy(mp);
 1069                 VI_LOCK(vp);
 1070                 vp->v_iflag &= ~VI_MOUNT;
 1071                 VI_UNLOCK(vp);
 1072                 vrele(vp);
 1073                 return (error);
 1074         }
 1075         MNT_ILOCK(mp);
 1076         if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
 1077                 mp->mnt_kern_flag |= MNTK_ASYNC;
 1078         else
 1079                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
 1080         MNT_IUNLOCK(mp);
 1081         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 1082         /*
 1083          * Put the new filesystem on the mount list after root.
 1084          */
 1085         cache_purge(vp);
 1086         if (!error) {
 1087                 struct vnode *newdp;
 1088 
 1089                 VI_LOCK(vp);
 1090                 vp->v_iflag &= ~VI_MOUNT;
 1091                 VI_UNLOCK(vp);
 1092                 vp->v_mountedhere = mp;
 1093                 mtx_lock(&mountlist_mtx);
 1094                 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
 1095                 mtx_unlock(&mountlist_mtx);
 1096                 vfs_event_signal(NULL, VQ_MOUNT, 0);
 1097                 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
 1098                         panic("mount: lost mount");
 1099                 VOP_UNLOCK(newdp, 0);
 1100                 VOP_UNLOCK(vp, 0);
 1101                 mountcheckdirs(vp, newdp);
 1102                 vrele(newdp);
 1103                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
 1104                         error = vfs_allocate_syncvnode(mp);
 1105                 vfs_unbusy(mp);
 1106                 if (error)
 1107                         vrele(vp);
 1108         } else {
 1109                 VI_LOCK(vp);
 1110                 vp->v_iflag &= ~VI_MOUNT;
 1111                 VI_UNLOCK(vp);
 1112                 vfs_unbusy(mp);
 1113                 vfs_mount_destroy(mp);
 1114                 vput(vp);
 1115         }
 1116         return (error);
 1117 }
 1118 
 1119 /*
 1120  * Unmount a filesystem.
 1121  *
 1122  * Note: unmount takes a path to the vnode mounted on as argument, not
 1123  * special file (as before).
 1124  */
 1125 #ifndef _SYS_SYSPROTO_H_
 1126 struct unmount_args {
 1127         char    *path;
 1128         int     flags;
 1129 };
 1130 #endif
 1131 /* ARGSUSED */
 1132 int
 1133 unmount(td, uap)
 1134         struct thread *td;
 1135         register struct unmount_args /* {
 1136                 char *path;
 1137                 int flags;
 1138         } */ *uap;
 1139 {
 1140         struct mount *mp;
 1141         char *pathbuf;
 1142         int error, id0, id1;
 1143 
 1144         AUDIT_ARG_VALUE(uap->flags);
 1145         if (jailed(td->td_ucred) || usermount == 0) {
 1146                 error = priv_check(td, PRIV_VFS_UNMOUNT);
 1147                 if (error)
 1148                         return (error);
 1149         }
 1150 
 1151         pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
 1152         error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
 1153         if (error) {
 1154                 free(pathbuf, M_TEMP);
 1155                 return (error);
 1156         }
 1157         mtx_lock(&Giant);
 1158         if (uap->flags & MNT_BYFSID) {
 1159                 AUDIT_ARG_TEXT(pathbuf);
 1160                 /* Decode the filesystem ID. */
 1161                 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
 1162                         mtx_unlock(&Giant);
 1163                         free(pathbuf, M_TEMP);
 1164                         return (EINVAL);
 1165                 }
 1166 
 1167                 mtx_lock(&mountlist_mtx);
 1168                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
 1169                         if (mp->mnt_stat.f_fsid.val[0] == id0 &&
 1170                             mp->mnt_stat.f_fsid.val[1] == id1)
 1171                                 break;
 1172                 }
 1173                 mtx_unlock(&mountlist_mtx);
 1174         } else {
 1175                 AUDIT_ARG_UPATH1(td, pathbuf);
 1176                 mtx_lock(&mountlist_mtx);
 1177                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
 1178                         if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
 1179                                 break;
 1180                 }
 1181                 mtx_unlock(&mountlist_mtx);
 1182         }
 1183         free(pathbuf, M_TEMP);
 1184         if (mp == NULL) {
 1185                 /*
 1186                  * Previously we returned ENOENT for a nonexistent path and
 1187                  * EINVAL for a non-mountpoint.  We cannot tell these apart
 1188                  * now, so in the !MNT_BYFSID case return the more likely
 1189                  * EINVAL for compatibility.
 1190                  */
 1191                 mtx_unlock(&Giant);
 1192                 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
 1193         }
 1194 
 1195         /*
 1196          * Don't allow unmounting the root filesystem.
 1197          */
 1198         if (mp->mnt_flag & MNT_ROOTFS) {
 1199                 mtx_unlock(&Giant);
 1200                 return (EINVAL);
 1201         }
 1202         error = dounmount(mp, uap->flags, td);
 1203         mtx_unlock(&Giant);
 1204         return (error);
 1205 }
 1206 
 1207 /*
 1208  * Do the actual filesystem unmount.
 1209  */
 1210 int
 1211 dounmount(mp, flags, td)
 1212         struct mount *mp;
 1213         int flags;
 1214         struct thread *td;
 1215 {
 1216         struct vnode *coveredvp, *fsrootvp;
 1217         int error;
 1218         int async_flag;
 1219         int mnt_gen_r;
 1220 
 1221         mtx_assert(&Giant, MA_OWNED);
 1222 
 1223         if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
 1224                 mnt_gen_r = mp->mnt_gen;
 1225                 VI_LOCK(coveredvp);
 1226                 vholdl(coveredvp);
 1227                 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
 1228                 vdrop(coveredvp);
 1229                 /*
 1230                  * Check for mp being unmounted while waiting for the
 1231                  * covered vnode lock.
 1232                  */
 1233                 if (coveredvp->v_mountedhere != mp ||
 1234                     coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
 1235                         VOP_UNLOCK(coveredvp, 0);
 1236                         return (EBUSY);
 1237                 }
 1238         }
 1239         /*
 1240          * Only privileged root, or (if MNT_USER is set) the user that did the
 1241          * original mount is permitted to unmount this filesystem.
 1242          */
 1243         error = vfs_suser(mp, td);
 1244         if (error) {
 1245                 if (coveredvp)
 1246                         VOP_UNLOCK(coveredvp, 0);
 1247                 return (error);
 1248         }
 1249 
 1250         MNT_ILOCK(mp);
 1251         if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
 1252                 MNT_IUNLOCK(mp);
 1253                 if (coveredvp)
 1254                         VOP_UNLOCK(coveredvp, 0);
 1255                 return (EBUSY);
 1256         }
 1257         mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
 1258         /* Allow filesystems to detect that a forced unmount is in progress. */
 1259         if (flags & MNT_FORCE)
 1260                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
 1261         error = 0;
 1262         if (mp->mnt_lockref) {
 1263                 mp->mnt_kern_flag |= MNTK_DRAINING;
 1264                 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
 1265                     "mount drain", 0);
 1266         }
 1267         MNT_IUNLOCK(mp);
 1268         KASSERT(mp->mnt_lockref == 0,
 1269             ("%s: invalid lock refcount in the drain path @ %s:%d",
 1270             __func__, __FILE__, __LINE__));
 1271         KASSERT(error == 0,
 1272             ("%s: invalid return value for msleep in the drain path @ %s:%d",
 1273             __func__, __FILE__, __LINE__));
 1274         vn_start_write(NULL, &mp, V_WAIT);
 1275 
 1276         if (mp->mnt_flag & MNT_EXPUBLIC)
 1277                 vfs_setpublicfs(NULL, NULL, NULL);
 1278 
 1279         vfs_msync(mp, MNT_WAIT);
 1280         MNT_ILOCK(mp);
 1281         async_flag = mp->mnt_flag & MNT_ASYNC;
 1282         mp->mnt_flag &= ~MNT_ASYNC;
 1283         mp->mnt_kern_flag &= ~MNTK_ASYNC;
 1284         MNT_IUNLOCK(mp);
 1285         cache_purgevfs(mp);     /* remove cache entries for this file sys */
 1286         if (mp->mnt_syncer != NULL)
 1287                 vrele(mp->mnt_syncer);
 1288         /*
 1289          * For forced unmounts, move process cdir/rdir refs on the fs root
 1290          * vnode to the covered vnode.  For non-forced unmounts we want
 1291          * such references to cause an EBUSY error.
 1292          */
 1293         if ((flags & MNT_FORCE) &&
 1294             VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
 1295                 if (mp->mnt_vnodecovered != NULL)
 1296                         mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
 1297                 if (fsrootvp == rootvnode) {
 1298                         vrele(rootvnode);
 1299                         rootvnode = NULL;
 1300                 }
 1301                 vput(fsrootvp);
 1302         }
 1303         if (((mp->mnt_flag & MNT_RDONLY) ||
 1304              (error = VFS_SYNC(mp, MNT_WAIT)) == 0) || (flags & MNT_FORCE) != 0)
 1305                 error = VFS_UNMOUNT(mp, flags);
 1306         vn_finished_write(mp);
 1307         /*
 1308          * If we failed to flush the dirty blocks for this mount point,
 1309          * undo all the cdir/rdir and rootvnode changes we made above.
 1310          * Unless we failed to do so because the device is reporting that
 1311          * it doesn't exist anymore.
 1312          */
 1313         if (error && error != ENXIO) {
 1314                 if ((flags & MNT_FORCE) &&
 1315                     VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
 1316                         if (mp->mnt_vnodecovered != NULL)
 1317                                 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
 1318                         if (rootvnode == NULL) {
 1319                                 rootvnode = fsrootvp;
 1320                                 vref(rootvnode);
 1321                         }
 1322                         vput(fsrootvp);
 1323                 }
 1324                 MNT_ILOCK(mp);
 1325                 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
 1326                 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) {
 1327                         MNT_IUNLOCK(mp);
 1328                         (void) vfs_allocate_syncvnode(mp);
 1329                         MNT_ILOCK(mp);
 1330                 }
 1331                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
 1332                 mp->mnt_flag |= async_flag;
 1333                 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
 1334                         mp->mnt_kern_flag |= MNTK_ASYNC;
 1335                 if (mp->mnt_kern_flag & MNTK_MWAIT) {
 1336                         mp->mnt_kern_flag &= ~MNTK_MWAIT;
 1337                         wakeup(mp);
 1338                 }
 1339                 MNT_IUNLOCK(mp);
 1340                 if (coveredvp)
 1341                         VOP_UNLOCK(coveredvp, 0);
 1342                 return (error);
 1343         }
 1344         mtx_lock(&mountlist_mtx);
 1345         TAILQ_REMOVE(&mountlist, mp, mnt_list);
 1346         mtx_unlock(&mountlist_mtx);
 1347         if (coveredvp != NULL) {
 1348                 coveredvp->v_mountedhere = NULL;
 1349                 vput(coveredvp);
 1350         }
 1351         vfs_event_signal(NULL, VQ_UNMOUNT, 0);
 1352         vfs_mount_destroy(mp);
 1353         return (0);
 1354 }
 1355 
 1356 /*
 1357  * ---------------------------------------------------------------------
 1358  * Mounting of root filesystem
 1359  *
 1360  */
 1361 
 1362 struct root_hold_token {
 1363         const char                      *who;
 1364         LIST_ENTRY(root_hold_token)     list;
 1365 };
 1366 
 1367 static LIST_HEAD(, root_hold_token)     root_holds =
 1368     LIST_HEAD_INITIALIZER(root_holds);
 1369 
 1370 static int root_mount_complete;
 1371 
 1372 /*
 1373  * Hold root mount.
 1374  */
 1375 struct root_hold_token *
 1376 root_mount_hold(const char *identifier)
 1377 {
 1378         struct root_hold_token *h;
 1379 
 1380         if (root_mounted())
 1381                 return (NULL);
 1382 
 1383         h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
 1384         h->who = identifier;
 1385         mtx_lock(&mountlist_mtx);
 1386         LIST_INSERT_HEAD(&root_holds, h, list);
 1387         mtx_unlock(&mountlist_mtx);
 1388         return (h);
 1389 }
 1390 
 1391 /*
 1392  * Release root mount.
 1393  */
 1394 void
 1395 root_mount_rel(struct root_hold_token *h)
 1396 {
 1397 
 1398         if (h == NULL)
 1399                 return;
 1400         mtx_lock(&mountlist_mtx);
 1401         LIST_REMOVE(h, list);
 1402         wakeup(&root_holds);
 1403         mtx_unlock(&mountlist_mtx);
 1404         free(h, M_DEVBUF);
 1405 }
 1406 
 1407 /*
 1408  * Wait for all subsystems to release root mount.
 1409  */
 1410 static void
 1411 root_mount_prepare(void)
 1412 {
 1413         struct root_hold_token *h;
 1414         struct timeval lastfail;
 1415         int curfail = 0;
 1416 
 1417         for (;;) {
 1418                 DROP_GIANT();
 1419                 g_waitidle();
 1420                 PICKUP_GIANT();
 1421                 mtx_lock(&mountlist_mtx);
 1422                 if (LIST_EMPTY(&root_holds)) {
 1423                         mtx_unlock(&mountlist_mtx);
 1424                         break;
 1425                 }
 1426                 if (ppsratecheck(&lastfail, &curfail, 1)) {
 1427                         printf("Root mount waiting for:");
 1428                         LIST_FOREACH(h, &root_holds, list)
 1429                                 printf(" %s", h->who);
 1430                         printf("\n");
 1431                 }
 1432                 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
 1433                     hz);
 1434         }
 1435 }
 1436 
 1437 /*
 1438  * Root was mounted, share the good news.
 1439  */
 1440 static void
 1441 root_mount_done(void)
 1442 {
 1443 
 1444         /* Keep prison0's root in sync with the global rootvnode. */
 1445         mtx_lock(&prison0.pr_mtx);
 1446         prison0.pr_root = rootvnode;
 1447         vref(prison0.pr_root);
 1448         mtx_unlock(&prison0.pr_mtx);
 1449         /*
 1450          * Use a mutex to prevent the wakeup being missed and waiting for
 1451          * an extra 1 second sleep.
 1452          */
 1453         mtx_lock(&mountlist_mtx);
 1454         root_mount_complete = 1;
 1455         wakeup(&root_mount_complete);
 1456         mtx_unlock(&mountlist_mtx);
 1457 }
 1458 
 1459 /*
 1460  * Return true if root is already mounted.
 1461  */
 1462 int
 1463 root_mounted(void)
 1464 {
 1465 
 1466         /* No mutex is acquired here because int stores are atomic. */
 1467         return (root_mount_complete);
 1468 }
 1469 
 1470 /*
 1471  * Wait until root is mounted.
 1472  */
 1473 void
 1474 root_mount_wait(void)
 1475 {
 1476 
 1477         /*
 1478          * Panic on an obvious deadlock - the function can't be called from
 1479          * a thread which is doing the whole SYSINIT stuff.
 1480          */
 1481         KASSERT(curthread->td_proc->p_pid != 0,
 1482             ("root_mount_wait: cannot be called from the swapper thread"));
 1483         mtx_lock(&mountlist_mtx);
 1484         while (!root_mount_complete) {
 1485                 msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait",
 1486                     hz);
 1487         }
 1488         mtx_unlock(&mountlist_mtx);
 1489 }
 1490 
 1491 static void
 1492 set_rootvnode()
 1493 {
 1494         struct proc *p;
 1495 
 1496         if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
 1497                 panic("Cannot find root vnode");
 1498 
 1499         VOP_UNLOCK(rootvnode, 0);
 1500 
 1501         p = curthread->td_proc;
 1502         FILEDESC_XLOCK(p->p_fd);
 1503 
 1504         if (p->p_fd->fd_cdir != NULL)
 1505                 vrele(p->p_fd->fd_cdir);
 1506         p->p_fd->fd_cdir = rootvnode;
 1507         VREF(rootvnode);
 1508 
 1509         if (p->p_fd->fd_rdir != NULL)
 1510                 vrele(p->p_fd->fd_rdir);
 1511         p->p_fd->fd_rdir = rootvnode;
 1512         VREF(rootvnode);
 1513 
 1514         FILEDESC_XUNLOCK(p->p_fd);
 1515 
 1516         EVENTHANDLER_INVOKE(mountroot);
 1517 }
 1518 
 1519 /*
 1520  * Mount /devfs as our root filesystem, but do not put it on the mountlist
 1521  * yet.  Create a /dev -> / symlink so that absolute pathnames will lookup.
 1522  */
 1523 
 1524 static void
 1525 devfs_first(void)
 1526 {
 1527         struct thread *td = curthread;
 1528         struct vfsoptlist *opts;
 1529         struct vfsconf *vfsp;
 1530         struct mount *mp = NULL;
 1531         int error;
 1532 
 1533         vfsp = vfs_byname("devfs");
 1534         KASSERT(vfsp != NULL, ("Could not find devfs by name"));
 1535         if (vfsp == NULL)
 1536                 return;
 1537 
 1538         mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
 1539 
 1540         error = VFS_MOUNT(mp);
 1541         KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
 1542         if (error)
 1543                 return;
 1544 
 1545         opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
 1546         TAILQ_INIT(opts);
 1547         mp->mnt_opt = opts;
 1548 
 1549         mtx_lock(&mountlist_mtx);
 1550         TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
 1551         mtx_unlock(&mountlist_mtx);
 1552 
 1553         set_rootvnode();
 1554 
 1555         error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
 1556         if (error)
 1557                 printf("kern_symlink /dev -> / returns %d\n", error);
 1558 }
 1559 
 1560 /*
 1561  * Surgically move our devfs to be mounted on /dev.
 1562  */
 1563 
 1564 static void
 1565 devfs_fixup(struct thread *td)
 1566 {
 1567         struct nameidata nd;
 1568         int error;
 1569         struct vnode *vp, *dvp;
 1570         struct mount *mp;
 1571 
 1572         /* Remove our devfs mount from the mountlist and purge the cache */
 1573         mtx_lock(&mountlist_mtx);
 1574         mp = TAILQ_FIRST(&mountlist);
 1575         TAILQ_REMOVE(&mountlist, mp, mnt_list);
 1576         mtx_unlock(&mountlist_mtx);
 1577         cache_purgevfs(mp);
 1578 
 1579         VFS_ROOT(mp, LK_EXCLUSIVE, &dvp);
 1580         VI_LOCK(dvp);
 1581         dvp->v_iflag &= ~VI_MOUNT;
 1582         VI_UNLOCK(dvp);
 1583         dvp->v_mountedhere = NULL;
 1584 
 1585         /* Set up the real rootvnode, and purge the cache */
 1586         TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
 1587         set_rootvnode();
 1588         cache_purgevfs(rootvnode->v_mount);
 1589 
 1590         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
 1591         error = namei(&nd);
 1592         if (error) {
 1593                 printf("Lookup of /dev for devfs, error: %d\n", error);
 1594                 return;
 1595         }
 1596         NDFREE(&nd, NDF_ONLY_PNBUF);
 1597         vp = nd.ni_vp;
 1598         if (vp->v_type != VDIR) {
 1599                 vput(vp);
 1600         }
 1601         error = vinvalbuf(vp, V_SAVE, 0, 0);
 1602         if (error) {
 1603                 vput(vp);
 1604         }
 1605         cache_purge(vp);
 1606         mp->mnt_vnodecovered = vp;
 1607         vp->v_mountedhere = mp;
 1608         mtx_lock(&mountlist_mtx);
 1609         TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
 1610         mtx_unlock(&mountlist_mtx);
 1611         VOP_UNLOCK(vp, 0);
 1612         vput(dvp);
 1613         vfs_unbusy(mp);
 1614 
 1615         /* Unlink the no longer needed /dev/dev -> / symlink */
 1616         kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
 1617 }
 1618 
 1619 /*
 1620  * Report errors during filesystem mounting.
 1621  */
 1622 void
 1623 vfs_mount_error(struct mount *mp, const char *fmt, ...)
 1624 {
 1625         struct vfsoptlist *moptlist = mp->mnt_optnew;
 1626         va_list ap;
 1627         int error, len;
 1628         char *errmsg;
 1629 
 1630         error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
 1631         if (error || errmsg == NULL || len <= 0)
 1632                 return;
 1633 
 1634         va_start(ap, fmt);
 1635         vsnprintf(errmsg, (size_t)len, fmt, ap);
 1636         va_end(ap);
 1637 }
 1638 
 1639 void
 1640 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
 1641 {
 1642         va_list ap;
 1643         int error, len;
 1644         char *errmsg;
 1645 
 1646         error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
 1647         if (error || errmsg == NULL || len <= 0)
 1648                 return;
 1649 
 1650         va_start(ap, fmt);
 1651         vsnprintf(errmsg, (size_t)len, fmt, ap);
 1652         va_end(ap);
 1653 }
 1654 
 1655 /*
 1656  * Find and mount the root filesystem
 1657  */
 1658 void
 1659 vfs_mountroot(void)
 1660 {
 1661         char *cp, *cpt, *options, *tmpdev;
 1662         int error, i, asked = 0;
 1663 
 1664         options = NULL;
 1665 
 1666         root_mount_prepare();
 1667 
 1668         mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
 1669             NULL, NULL, mount_init, mount_fini,
 1670             UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
 1671         devfs_first();
 1672 
 1673         /*
 1674          * We are booted with instructions to prompt for the root filesystem.
 1675          */
 1676         if (boothowto & RB_ASKNAME) {
 1677                 if (!vfs_mountroot_ask())
 1678                         goto mounted;
 1679                 asked = 1;
 1680         }
 1681 
 1682         options = getenv("vfs.root.mountfrom.options");
 1683 
 1684         /*
 1685          * The root filesystem information is compiled in, and we are
 1686          * booted with instructions to use it.
 1687          */
 1688         if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
 1689                 if (!vfs_mountroot_try(ctrootdevname, options))
 1690                         goto mounted;
 1691                 ctrootdevname = NULL;
 1692         }
 1693 
 1694         /*
 1695          * We've been given the generic "use CDROM as root" flag.  This is
 1696          * necessary because one media may be used in many different
 1697          * devices, so we need to search for them.
 1698          */
 1699         if (boothowto & RB_CDROM) {
 1700                 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
 1701                         if (!vfs_mountroot_try(cdrom_rootdevnames[i], options))
 1702                                 goto mounted;
 1703                 }
 1704         }
 1705 
 1706         /*
 1707          * Try to use the value read by the loader from /etc/fstab, or
 1708          * supplied via some other means.  This is the preferred
 1709          * mechanism.
 1710          */
 1711         cp = getenv("vfs.root.mountfrom");
 1712         if (cp != NULL) {
 1713                 cpt = cp;
 1714                 while ((tmpdev = strsep(&cpt, " \t")) != NULL) {
 1715                         error = vfs_mountroot_try(tmpdev, options);
 1716                         if (error == 0) {
 1717                                 freeenv(cp);
 1718                                 goto mounted;
 1719                         }
 1720                 }
 1721                 freeenv(cp);
 1722         }
 1723 
 1724         /*
 1725          * Try values that may have been computed by code during boot
 1726          */
 1727         if (!vfs_mountroot_try(rootdevnames[0], options))
 1728                 goto mounted;
 1729         if (!vfs_mountroot_try(rootdevnames[1], options))
 1730                 goto mounted;
 1731 
 1732         /*
 1733          * If we (still) have a compiled-in default, try it.
 1734          */
 1735         if (ctrootdevname != NULL)
 1736                 if (!vfs_mountroot_try(ctrootdevname, options))
 1737                         goto mounted;
 1738         /*
 1739          * Everything so far has failed, prompt on the console if we haven't
 1740          * already tried that.
 1741          */
 1742         if (!asked)
 1743                 if (!vfs_mountroot_ask())
 1744                         goto mounted;
 1745 
 1746         panic("Root mount failed, startup aborted.");
 1747 
 1748 mounted:
 1749         root_mount_done();
 1750         freeenv(options);
 1751 }
 1752 
 1753 static struct mntarg *
 1754 parse_mountroot_options(struct mntarg *ma, const char *options)
 1755 {
 1756         char *p;
 1757         char *name, *name_arg;
 1758         char *val, *val_arg;
 1759         char *opts;
 1760 
 1761         if (options == NULL || options[0] == '\0')
 1762                 return (ma);
 1763 
 1764         p = opts = strdup(options, M_MOUNT);
 1765         if (opts == NULL) {
 1766                 return (ma);
 1767         } 
 1768 
 1769         while((name = strsep(&p, ",")) != NULL) {
 1770                 if (name[0] == '\0')
 1771                         break;
 1772 
 1773                 val = strchr(name, '=');
 1774                 if (val != NULL) {
 1775                         *val = '\0';
 1776                         ++val;
 1777                 }
 1778                 if( strcmp(name, "rw") == 0 ||
 1779                     strcmp(name, "noro") == 0) {
 1780                         /*
 1781                          * The first time we mount the root file system,
 1782                          * we need to mount 'ro', so We need to ignore
 1783                          * 'rw' and 'noro' mount options.
 1784                          */
 1785                         continue;
 1786                 }
 1787                 name_arg = strdup(name, M_MOUNT);
 1788                 val_arg = NULL;
 1789                 if (val != NULL) 
 1790                         val_arg = strdup(val, M_MOUNT);
 1791 
 1792                 ma = mount_arg(ma, name_arg, val_arg,
 1793                     (val_arg != NULL ? -1 : 0));
 1794         }
 1795         free(opts, M_MOUNT);
 1796         return (ma);
 1797 }
 1798 
 1799 /*
 1800  * Mount (mountfrom) as the root filesystem.
 1801  */
 1802 static int
 1803 vfs_mountroot_try(const char *mountfrom, const char *options)
 1804 {
 1805         struct mount    *mp;
 1806         struct mntarg   *ma;
 1807         char            *vfsname, *path;
 1808         time_t          timebase;
 1809         int             error;
 1810         char            patt[32];
 1811         char            errmsg[255];
 1812 
 1813         vfsname = NULL;
 1814         path    = NULL;
 1815         mp      = NULL;
 1816         ma      = NULL;
 1817         error   = EINVAL;
 1818         bzero(errmsg, sizeof(errmsg));
 1819 
 1820         if (mountfrom == NULL)
 1821                 return (error);         /* don't complain */
 1822         printf("Trying to mount root from %s\n", mountfrom);
 1823 
 1824         /* parse vfs name and path */
 1825         vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
 1826         path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
 1827         vfsname[0] = path[0] = 0;
 1828         sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
 1829         if (sscanf(mountfrom, patt, vfsname, path) < 1)
 1830                 goto out;
 1831 
 1832         if (path[0] == '\0')
 1833                 strcpy(path, ROOTNAME);
 1834 
 1835         ma = mount_arg(ma, "fstype", vfsname, -1);
 1836         ma = mount_arg(ma, "fspath", "/", -1);
 1837         ma = mount_arg(ma, "from", path, -1);
 1838         ma = mount_arg(ma, "errmsg", errmsg, sizeof(errmsg));
 1839         ma = mount_arg(ma, "ro", NULL, 0);
 1840         ma = parse_mountroot_options(ma, options);
 1841         error = kernel_mount(ma, MNT_ROOTFS);
 1842 
 1843         if (error == 0) {
 1844                 /*
 1845                  * We mount devfs prior to mounting the / FS, so the first
 1846                  * entry will typically be devfs.
 1847                  */
 1848                 mp = TAILQ_FIRST(&mountlist);
 1849                 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
 1850 
 1851                 /*
 1852                  * Iterate over all currently mounted file systems and use
 1853                  * the time stamp found to check and/or initialize the RTC.
 1854                  * Typically devfs has no time stamp and the only other FS
 1855                  * is the actual / FS.
 1856                  * Call inittodr() only once and pass it the largest of the
 1857                  * timestamps we encounter.
 1858                  */
 1859                 timebase = 0;
 1860                 do {
 1861                         if (mp->mnt_time > timebase)
 1862                                 timebase = mp->mnt_time;
 1863                         mp = TAILQ_NEXT(mp, mnt_list);
 1864                 } while (mp != NULL);
 1865                 inittodr(timebase);
 1866 
 1867                 devfs_fixup(curthread);
 1868         }
 1869 
 1870         if (error != 0 ) {
 1871                 printf("ROOT MOUNT ERROR: %s\n", errmsg);
 1872                 printf("If you have invalid mount options, reboot, and ");
 1873                 printf("first try the following from\n");
 1874                 printf("the loader prompt:\n\n");
 1875                 printf("     set vfs.root.mountfrom.options=rw\n\n");
 1876                 printf("and then remove invalid mount options from ");
 1877                 printf("/etc/fstab.\n\n");
 1878         }
 1879 out:
 1880         free(path, M_MOUNT);
 1881         free(vfsname, M_MOUNT);
 1882         return (error);
 1883 }
 1884 
 1885 /*
 1886  * ---------------------------------------------------------------------
 1887  * Interactive root filesystem selection code.
 1888  */
 1889 
 1890 static int
 1891 vfs_mountroot_ask(void)
 1892 {
 1893         char name[128];
 1894         char *mountfrom;
 1895         char *options;
 1896 
 1897         for(;;) {
 1898                 printf("Loader variables:\n");
 1899                 printf("vfs.root.mountfrom=");
 1900                 mountfrom = getenv("vfs.root.mountfrom");
 1901                 if (mountfrom != NULL) {
 1902                         printf("%s", mountfrom);
 1903                 }
 1904                 printf("\n");
 1905                 printf("vfs.root.mountfrom.options=");
 1906                 options = getenv("vfs.root.mountfrom.options");
 1907                 if (options != NULL) {
 1908                         printf("%s", options);
 1909                 }
 1910                 printf("\n");
 1911                 freeenv(mountfrom);
 1912                 freeenv(options);
 1913                 printf("\nManual root filesystem specification:\n");
 1914                 printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
 1915                 printf("                       eg. ufs:/dev/da0s1a\n");
 1916                 printf("                       eg. cd9660:/dev/acd0\n");
 1917                 printf("                       This is equivalent to: ");
 1918                 printf("mount -t cd9660 /dev/acd0 /\n"); 
 1919                 printf("\n");
 1920                 printf("  ?                  List valid disk boot devices\n");
 1921                 printf("  <empty line>       Abort manual input\n");
 1922                 printf("\nmountroot> ");
 1923                 gets(name, sizeof(name), 1);
 1924                 if (name[0] == '\0')
 1925                         return (1);
 1926                 if (name[0] == '?') {
 1927                         printf("\nList of GEOM managed disk devices:\n  ");
 1928                         g_dev_print();
 1929                         continue;
 1930                 }
 1931                 if (!vfs_mountroot_try(name, NULL))
 1932                         return (0);
 1933         }
 1934 }
 1935 
 1936 /*
 1937  * ---------------------------------------------------------------------
 1938  * Functions for querying mount options/arguments from filesystems.
 1939  */
 1940 
 1941 /*
 1942  * Check that no unknown options are given
 1943  */
 1944 int
 1945 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
 1946 {
 1947         struct vfsopt *opt;
 1948         char errmsg[255];
 1949         const char **t, *p, *q;
 1950         int ret = 0;
 1951 
 1952         TAILQ_FOREACH(opt, opts, link) {
 1953                 p = opt->name;
 1954                 q = NULL;
 1955                 if (p[0] == 'n' && p[1] == 'o')
 1956                         q = p + 2;
 1957                 for(t = global_opts; *t != NULL; t++) {
 1958                         if (strcmp(*t, p) == 0)
 1959                                 break;
 1960                         if (q != NULL) {
 1961                                 if (strcmp(*t, q) == 0)
 1962                                         break;
 1963                         }
 1964                 }
 1965                 if (*t != NULL)
 1966                         continue;
 1967                 for(t = legal; *t != NULL; t++) {
 1968                         if (strcmp(*t, p) == 0)
 1969                                 break;
 1970                         if (q != NULL) {
 1971                                 if (strcmp(*t, q) == 0)
 1972                                         break;
 1973                         }
 1974                 }
 1975                 if (*t != NULL)
 1976                         continue;
 1977                 snprintf(errmsg, sizeof(errmsg),
 1978                     "mount option <%s> is unknown", p);
 1979                 printf("%s\n", errmsg);
 1980                 ret = EINVAL;
 1981         }
 1982         if (ret != 0) {
 1983                 TAILQ_FOREACH(opt, opts, link) {
 1984                         if (strcmp(opt->name, "errmsg") == 0) {
 1985                                 strncpy((char *)opt->value, errmsg, opt->len);
 1986                         }
 1987                 }
 1988         }
 1989         return (ret);
 1990 }
 1991 
 1992 /*
 1993  * Get a mount option by its name.
 1994  *
 1995  * Return 0 if the option was found, ENOENT otherwise.
 1996  * If len is non-NULL it will be filled with the length
 1997  * of the option. If buf is non-NULL, it will be filled
 1998  * with the address of the option.
 1999  */
 2000 int
 2001 vfs_getopt(opts, name, buf, len)
 2002         struct vfsoptlist *opts;
 2003         const char *name;
 2004         void **buf;
 2005         int *len;
 2006 {
 2007         struct vfsopt *opt;
 2008 
 2009         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
 2010 
 2011         TAILQ_FOREACH(opt, opts, link) {
 2012                 if (strcmp(name, opt->name) == 0) {
 2013                         opt->seen = 1;
 2014                         if (len != NULL)
 2015                                 *len = opt->len;
 2016                         if (buf != NULL)
 2017                                 *buf = opt->value;
 2018                         return (0);
 2019                 }
 2020         }
 2021         return (ENOENT);
 2022 }
 2023 
 2024 int
 2025 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
 2026 {
 2027         struct vfsopt *opt;
 2028 
 2029         if (opts == NULL)
 2030                 return (-1);
 2031 
 2032         TAILQ_FOREACH(opt, opts, link) {
 2033                 if (strcmp(name, opt->name) == 0) {
 2034                         opt->seen = 1;
 2035                         return (opt->pos);
 2036                 }
 2037         }
 2038         return (-1);
 2039 }
 2040 
 2041 char *
 2042 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
 2043 {
 2044         struct vfsopt *opt;
 2045 
 2046         *error = 0;
 2047         TAILQ_FOREACH(opt, opts, link) {
 2048                 if (strcmp(name, opt->name) != 0)
 2049                         continue;
 2050                 opt->seen = 1;
 2051                 if (opt->len == 0 ||
 2052                     ((char *)opt->value)[opt->len - 1] != '\0') {
 2053                         *error = EINVAL;
 2054                         return (NULL);
 2055                 }
 2056                 return (opt->value);
 2057         }
 2058         *error = ENOENT;
 2059         return (NULL);
 2060 }
 2061 
 2062 int
 2063 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
 2064 {
 2065         struct vfsopt *opt;
 2066 
 2067         TAILQ_FOREACH(opt, opts, link) {
 2068                 if (strcmp(name, opt->name) == 0) {
 2069                         opt->seen = 1;
 2070                         if (w != NULL)
 2071                                 *w |= val;
 2072                         return (1);
 2073                 }
 2074         }
 2075         if (w != NULL)
 2076                 *w &= ~val;
 2077         return (0);
 2078 }
 2079 
 2080 int
 2081 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
 2082 {
 2083         va_list ap;
 2084         struct vfsopt *opt;
 2085         int ret;
 2086 
 2087         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
 2088 
 2089         TAILQ_FOREACH(opt, opts, link) {
 2090                 if (strcmp(name, opt->name) != 0)
 2091                         continue;
 2092                 opt->seen = 1;
 2093                 if (opt->len == 0 || opt->value == NULL)
 2094                         return (0);
 2095                 if (((char *)opt->value)[opt->len - 1] != '\0')
 2096                         return (0);
 2097                 va_start(ap, fmt);
 2098                 ret = vsscanf(opt->value, fmt, ap);
 2099                 va_end(ap);
 2100                 return (ret);
 2101         }
 2102         return (0);
 2103 }
 2104 
 2105 int
 2106 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
 2107 {
 2108         struct vfsopt *opt;
 2109 
 2110         TAILQ_FOREACH(opt, opts, link) {
 2111                 if (strcmp(name, opt->name) != 0)
 2112                         continue;
 2113                 opt->seen = 1;
 2114                 if (opt->value == NULL)
 2115                         opt->len = len;
 2116                 else {
 2117                         if (opt->len != len)
 2118                                 return (EINVAL);
 2119                         bcopy(value, opt->value, len);
 2120                 }
 2121                 return (0);
 2122         }
 2123         return (ENOENT);
 2124 }
 2125 
 2126 int
 2127 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
 2128 {
 2129         struct vfsopt *opt;
 2130 
 2131         TAILQ_FOREACH(opt, opts, link) {
 2132                 if (strcmp(name, opt->name) != 0)
 2133                         continue;
 2134                 opt->seen = 1;
 2135                 if (opt->value == NULL)
 2136                         opt->len = len;
 2137                 else {
 2138                         if (opt->len < len)
 2139                                 return (EINVAL);
 2140                         opt->len = len;
 2141                         bcopy(value, opt->value, len);
 2142                 }
 2143                 return (0);
 2144         }
 2145         return (ENOENT);
 2146 }
 2147 
 2148 int
 2149 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
 2150 {
 2151         struct vfsopt *opt;
 2152 
 2153         TAILQ_FOREACH(opt, opts, link) {
 2154                 if (strcmp(name, opt->name) != 0)
 2155                         continue;
 2156                 opt->seen = 1;
 2157                 if (opt->value == NULL)
 2158                         opt->len = strlen(value) + 1;
 2159                 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
 2160                         return (EINVAL);
 2161                 return (0);
 2162         }
 2163         return (ENOENT);
 2164 }
 2165 
 2166 /*
 2167  * Find and copy a mount option.
 2168  *
 2169  * The size of the buffer has to be specified
 2170  * in len, if it is not the same length as the
 2171  * mount option, EINVAL is returned.
 2172  * Returns ENOENT if the option is not found.
 2173  */
 2174 int
 2175 vfs_copyopt(opts, name, dest, len)
 2176         struct vfsoptlist *opts;
 2177         const char *name;
 2178         void *dest;
 2179         int len;
 2180 {
 2181         struct vfsopt *opt;
 2182 
 2183         KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
 2184 
 2185         TAILQ_FOREACH(opt, opts, link) {
 2186                 if (strcmp(name, opt->name) == 0) {
 2187                         opt->seen = 1;
 2188                         if (len != opt->len)
 2189                                 return (EINVAL);
 2190                         bcopy(opt->value, dest, opt->len);
 2191                         return (0);
 2192                 }
 2193         }
 2194         return (ENOENT);
 2195 }
 2196 
 2197 /*
 2198  * This is a helper function for filesystems to traverse their
 2199  * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
 2200  */
 2201 
 2202 struct vnode *
 2203 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
 2204 {
 2205         struct vnode *vp;
 2206 
 2207         mtx_assert(MNT_MTX(mp), MA_OWNED);
 2208 
 2209         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
 2210         if ((*mvp)->v_yield++ == 500) {
 2211                 MNT_IUNLOCK(mp);
 2212                 (*mvp)->v_yield = 0;
 2213                 uio_yield();
 2214                 MNT_ILOCK(mp);
 2215         }
 2216         vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
 2217         while (vp != NULL && vp->v_type == VMARKER)
 2218                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
 2219 
 2220         /* Check if we are done */
 2221         if (vp == NULL) {
 2222                 __mnt_vnode_markerfree(mvp, mp);
 2223                 return (NULL);
 2224         }
 2225         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
 2226         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
 2227         return (vp);
 2228 }
 2229 
 2230 struct vnode *
 2231 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
 2232 {
 2233         struct vnode *vp;
 2234 
 2235         mtx_assert(MNT_MTX(mp), MA_OWNED);
 2236 
 2237         vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
 2238         while (vp != NULL && vp->v_type == VMARKER)
 2239                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
 2240 
 2241         /* Check if we are done */
 2242         if (vp == NULL) {
 2243                 *mvp = NULL;
 2244                 return (NULL);
 2245         }
 2246         MNT_REF(mp);
 2247         MNT_IUNLOCK(mp);
 2248         *mvp = (struct vnode *) malloc(sizeof(struct vnode),
 2249                                        M_VNODE_MARKER,
 2250                                        M_WAITOK | M_ZERO);
 2251         MNT_ILOCK(mp);
 2252         (*mvp)->v_type = VMARKER;
 2253 
 2254         vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
 2255         while (vp != NULL && vp->v_type == VMARKER)
 2256                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
 2257 
 2258         /* Check if we are done */
 2259         if (vp == NULL) {
 2260                 MNT_IUNLOCK(mp);
 2261                 free(*mvp, M_VNODE_MARKER);
 2262                 MNT_ILOCK(mp);
 2263                 *mvp = NULL;
 2264                 MNT_REL(mp);
 2265                 return (NULL);
 2266         }
 2267         (*mvp)->v_mount = mp;
 2268         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
 2269         return (vp);
 2270 }
 2271 
 2272 
 2273 void
 2274 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
 2275 {
 2276 
 2277         if (*mvp == NULL)
 2278                 return;
 2279 
 2280         mtx_assert(MNT_MTX(mp), MA_OWNED);
 2281 
 2282         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
 2283         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
 2284         MNT_IUNLOCK(mp);
 2285         free(*mvp, M_VNODE_MARKER);
 2286         MNT_ILOCK(mp);
 2287         *mvp = NULL;
 2288         MNT_REL(mp);
 2289 }
 2290 
 2291 
 2292 int
 2293 __vfs_statfs(struct mount *mp, struct statfs *sbp)
 2294 {
 2295         int error;
 2296 
 2297         error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
 2298         if (sbp != &mp->mnt_stat)
 2299                 *sbp = mp->mnt_stat;
 2300         return (error);
 2301 }
 2302 
 2303 void
 2304 vfs_mountedfrom(struct mount *mp, const char *from)
 2305 {
 2306 
 2307         bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
 2308         strlcpy(mp->mnt_stat.f_mntfromname, from,
 2309             sizeof mp->mnt_stat.f_mntfromname);
 2310 }
 2311 
 2312 /*
 2313  * ---------------------------------------------------------------------
 2314  * This is the api for building mount args and mounting filesystems from
 2315  * inside the kernel.
 2316  *
 2317  * The API works by accumulation of individual args.  First error is
 2318  * latched.
 2319  *
 2320  * XXX: should be documented in new manpage kernel_mount(9)
 2321  */
 2322 
 2323 /* A memory allocation which must be freed when we are done */
 2324 struct mntaarg {
 2325         SLIST_ENTRY(mntaarg)    next;
 2326 };
 2327 
 2328 /* The header for the mount arguments */
 2329 struct mntarg {
 2330         struct iovec *v;
 2331         int len;
 2332         int error;
 2333         SLIST_HEAD(, mntaarg)   list;
 2334 };
 2335 
 2336 /*
 2337  * Add a boolean argument.
 2338  *
 2339  * flag is the boolean value.
 2340  * name must start with "no".
 2341  */
 2342 struct mntarg *
 2343 mount_argb(struct mntarg *ma, int flag, const char *name)
 2344 {
 2345 
 2346         KASSERT(name[0] == 'n' && name[1] == 'o',
 2347             ("mount_argb(...,%s): name must start with 'no'", name));
 2348 
 2349         return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
 2350 }
 2351 
 2352 /*
 2353  * Add an argument printf style
 2354  */
 2355 struct mntarg *
 2356 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
 2357 {
 2358         va_list ap;
 2359         struct mntaarg *maa;
 2360         struct sbuf *sb;
 2361         int len;
 2362 
 2363         if (ma == NULL) {
 2364                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
 2365                 SLIST_INIT(&ma->list);
 2366         }
 2367         if (ma->error)
 2368                 return (ma);
 2369 
 2370         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
 2371             M_MOUNT, M_WAITOK);
 2372         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
 2373         ma->v[ma->len].iov_len = strlen(name) + 1;
 2374         ma->len++;
 2375 
 2376         sb = sbuf_new_auto();
 2377         va_start(ap, fmt);
 2378         sbuf_vprintf(sb, fmt, ap);
 2379         va_end(ap);
 2380         sbuf_finish(sb);
 2381         len = sbuf_len(sb) + 1;
 2382         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
 2383         SLIST_INSERT_HEAD(&ma->list, maa, next);
 2384         bcopy(sbuf_data(sb), maa + 1, len);
 2385         sbuf_delete(sb);
 2386 
 2387         ma->v[ma->len].iov_base = maa + 1;
 2388         ma->v[ma->len].iov_len = len;
 2389         ma->len++;
 2390 
 2391         return (ma);
 2392 }
 2393 
 2394 /*
 2395  * Add an argument which is a userland string.
 2396  */
 2397 struct mntarg *
 2398 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
 2399 {
 2400         struct mntaarg *maa;
 2401         char *tbuf;
 2402 
 2403         if (val == NULL)
 2404                 return (ma);
 2405         if (ma == NULL) {
 2406                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
 2407                 SLIST_INIT(&ma->list);
 2408         }
 2409         if (ma->error)
 2410                 return (ma);
 2411         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
 2412         SLIST_INSERT_HEAD(&ma->list, maa, next);
 2413         tbuf = (void *)(maa + 1);
 2414         ma->error = copyinstr(val, tbuf, len, NULL);
 2415         return (mount_arg(ma, name, tbuf, -1));
 2416 }
 2417 
 2418 /*
 2419  * Plain argument.
 2420  *
 2421  * If length is -1, treat value as a C string.
 2422  */
 2423 struct mntarg *
 2424 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
 2425 {
 2426 
 2427         if (ma == NULL) {
 2428                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
 2429                 SLIST_INIT(&ma->list);
 2430         }
 2431         if (ma->error)
 2432                 return (ma);
 2433 
 2434         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
 2435             M_MOUNT, M_WAITOK);
 2436         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
 2437         ma->v[ma->len].iov_len = strlen(name) + 1;
 2438         ma->len++;
 2439 
 2440         ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
 2441         if (len < 0)
 2442                 ma->v[ma->len].iov_len = strlen(val) + 1;
 2443         else
 2444                 ma->v[ma->len].iov_len = len;
 2445         ma->len++;
 2446         return (ma);
 2447 }
 2448 
 2449 /*
 2450  * Free a mntarg structure
 2451  */
 2452 static void
 2453 free_mntarg(struct mntarg *ma)
 2454 {
 2455         struct mntaarg *maa;
 2456 
 2457         while (!SLIST_EMPTY(&ma->list)) {
 2458                 maa = SLIST_FIRST(&ma->list);
 2459                 SLIST_REMOVE_HEAD(&ma->list, next);
 2460                 free(maa, M_MOUNT);
 2461         }
 2462         free(ma->v, M_MOUNT);
 2463         free(ma, M_MOUNT);
 2464 }
 2465 
 2466 /*
 2467  * Mount a filesystem
 2468  */
 2469 int
 2470 kernel_mount(struct mntarg *ma, int flags)
 2471 {
 2472         struct uio auio;
 2473         int error;
 2474 
 2475         KASSERT(ma != NULL, ("kernel_mount NULL ma"));
 2476         KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
 2477         KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
 2478 
 2479         auio.uio_iov = ma->v;
 2480         auio.uio_iovcnt = ma->len;
 2481         auio.uio_segflg = UIO_SYSSPACE;
 2482 
 2483         error = ma->error;
 2484         if (!error)
 2485                 error = vfs_donmount(curthread, flags, &auio);
 2486         free_mntarg(ma);
 2487         return (error);
 2488 }
 2489 
 2490 /*
 2491  * A printflike function to mount a filesystem.
 2492  */
 2493 int
 2494 kernel_vmount(int flags, ...)
 2495 {
 2496         struct mntarg *ma = NULL;
 2497         va_list ap;
 2498         const char *cp;
 2499         const void *vp;
 2500         int error;
 2501 
 2502         va_start(ap, flags);
 2503         for (;;) {
 2504                 cp = va_arg(ap, const char *);
 2505                 if (cp == NULL)
 2506                         break;
 2507                 vp = va_arg(ap, const void *);
 2508                 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
 2509         }
 2510         va_end(ap);
 2511 
 2512         error = kernel_mount(ma, flags);
 2513         return (error);
 2514 }
 2515 
 2516 void
 2517 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
 2518 {
 2519 
 2520         bcopy(oexp, exp, sizeof(*oexp));
 2521         exp->ex_numsecflavors = 0;
 2522 }

Cache object: 4572185335b7d4f4c76a6d56f6a9ed21


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.