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$");
   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, MFSNAMELEN);
  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         vn_start_write(NULL, &mp, V_WAIT);
 1251         MNT_ILOCK(mp);
 1252         if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
 1253                 MNT_IUNLOCK(mp);
 1254                 if (coveredvp)
 1255                         VOP_UNLOCK(coveredvp, 0);
 1256                 vn_finished_write(mp);
 1257                 return (EBUSY);
 1258         }
 1259         mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
 1260         /* Allow filesystems to detect that a forced unmount is in progress. */
 1261         if (flags & MNT_FORCE)
 1262                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
 1263         error = 0;
 1264         if (mp->mnt_lockref) {
 1265                 mp->mnt_kern_flag |= MNTK_DRAINING;
 1266                 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
 1267                     "mount drain", 0);
 1268         }
 1269         MNT_IUNLOCK(mp);
 1270         KASSERT(mp->mnt_lockref == 0,
 1271             ("%s: invalid lock refcount in the drain path @ %s:%d",
 1272             __func__, __FILE__, __LINE__));
 1273         KASSERT(error == 0,
 1274             ("%s: invalid return value for msleep in the drain path @ %s:%d",
 1275             __func__, __FILE__, __LINE__));
 1276 
 1277         if (mp->mnt_flag & MNT_EXPUBLIC)
 1278                 vfs_setpublicfs(NULL, NULL, NULL);
 1279 
 1280         vfs_msync(mp, MNT_WAIT);
 1281         MNT_ILOCK(mp);
 1282         async_flag = mp->mnt_flag & MNT_ASYNC;
 1283         mp->mnt_flag &= ~MNT_ASYNC;
 1284         mp->mnt_kern_flag &= ~MNTK_ASYNC;
 1285         MNT_IUNLOCK(mp);
 1286         cache_purgevfs(mp);     /* remove cache entries for this file sys */
 1287         if (mp->mnt_syncer != NULL)
 1288                 vrele(mp->mnt_syncer);
 1289         /*
 1290          * For forced unmounts, move process cdir/rdir refs on the fs root
 1291          * vnode to the covered vnode.  For non-forced unmounts we want
 1292          * such references to cause an EBUSY error.
 1293          */
 1294         if ((flags & MNT_FORCE) &&
 1295             VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
 1296                 if (mp->mnt_vnodecovered != NULL)
 1297                         mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
 1298                 if (fsrootvp == rootvnode) {
 1299                         vrele(rootvnode);
 1300                         rootvnode = NULL;
 1301                 }
 1302                 vput(fsrootvp);
 1303         }
 1304         if (((mp->mnt_flag & MNT_RDONLY) ||
 1305              (error = VFS_SYNC(mp, MNT_WAIT)) == 0) || (flags & MNT_FORCE) != 0)
 1306                 error = VFS_UNMOUNT(mp, flags);
 1307         vn_finished_write(mp);
 1308         /*
 1309          * If we failed to flush the dirty blocks for this mount point,
 1310          * undo all the cdir/rdir and rootvnode changes we made above.
 1311          * Unless we failed to do so because the device is reporting that
 1312          * it doesn't exist anymore.
 1313          */
 1314         if (error && error != ENXIO) {
 1315                 if ((flags & MNT_FORCE) &&
 1316                     VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
 1317                         if (mp->mnt_vnodecovered != NULL)
 1318                                 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
 1319                         if (rootvnode == NULL) {
 1320                                 rootvnode = fsrootvp;
 1321                                 vref(rootvnode);
 1322                         }
 1323                         vput(fsrootvp);
 1324                 }
 1325                 MNT_ILOCK(mp);
 1326                 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
 1327                 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) {
 1328                         MNT_IUNLOCK(mp);
 1329                         (void) vfs_allocate_syncvnode(mp);
 1330                         MNT_ILOCK(mp);
 1331                 }
 1332                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
 1333                 mp->mnt_flag |= async_flag;
 1334                 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
 1335                         mp->mnt_kern_flag |= MNTK_ASYNC;
 1336                 if (mp->mnt_kern_flag & MNTK_MWAIT) {
 1337                         mp->mnt_kern_flag &= ~MNTK_MWAIT;
 1338                         wakeup(mp);
 1339                 }
 1340                 MNT_IUNLOCK(mp);
 1341                 if (coveredvp)
 1342                         VOP_UNLOCK(coveredvp, 0);
 1343                 return (error);
 1344         }
 1345         mtx_lock(&mountlist_mtx);
 1346         TAILQ_REMOVE(&mountlist, mp, mnt_list);
 1347         mtx_unlock(&mountlist_mtx);
 1348         if (coveredvp != NULL) {
 1349                 coveredvp->v_mountedhere = NULL;
 1350                 vput(coveredvp);
 1351         }
 1352         vfs_event_signal(NULL, VQ_UNMOUNT, 0);
 1353         vfs_mount_destroy(mp);
 1354         return (0);
 1355 }
 1356 
 1357 /*
 1358  * ---------------------------------------------------------------------
 1359  * Mounting of root filesystem
 1360  *
 1361  */
 1362 
 1363 struct root_hold_token {
 1364         const char                      *who;
 1365         LIST_ENTRY(root_hold_token)     list;
 1366 };
 1367 
 1368 static LIST_HEAD(, root_hold_token)     root_holds =
 1369     LIST_HEAD_INITIALIZER(root_holds);
 1370 
 1371 static int root_mount_complete;
 1372 
 1373 /*
 1374  * Hold root mount.
 1375  */
 1376 struct root_hold_token *
 1377 root_mount_hold(const char *identifier)
 1378 {
 1379         struct root_hold_token *h;
 1380 
 1381         if (root_mounted())
 1382                 return (NULL);
 1383 
 1384         h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
 1385         h->who = identifier;
 1386         mtx_lock(&mountlist_mtx);
 1387         LIST_INSERT_HEAD(&root_holds, h, list);
 1388         mtx_unlock(&mountlist_mtx);
 1389         return (h);
 1390 }
 1391 
 1392 /*
 1393  * Release root mount.
 1394  */
 1395 void
 1396 root_mount_rel(struct root_hold_token *h)
 1397 {
 1398 
 1399         if (h == NULL)
 1400                 return;
 1401         mtx_lock(&mountlist_mtx);
 1402         LIST_REMOVE(h, list);
 1403         wakeup(&root_holds);
 1404         mtx_unlock(&mountlist_mtx);
 1405         free(h, M_DEVBUF);
 1406 }
 1407 
 1408 /*
 1409  * Wait for all subsystems to release root mount.
 1410  */
 1411 static void
 1412 root_mount_prepare(void)
 1413 {
 1414         struct root_hold_token *h;
 1415         struct timeval lastfail;
 1416         int curfail = 0;
 1417 
 1418         for (;;) {
 1419                 DROP_GIANT();
 1420                 g_waitidle();
 1421                 PICKUP_GIANT();
 1422                 mtx_lock(&mountlist_mtx);
 1423                 if (LIST_EMPTY(&root_holds)) {
 1424                         mtx_unlock(&mountlist_mtx);
 1425                         break;
 1426                 }
 1427                 if (ppsratecheck(&lastfail, &curfail, 1)) {
 1428                         printf("Root mount waiting for:");
 1429                         LIST_FOREACH(h, &root_holds, list)
 1430                                 printf(" %s", h->who);
 1431                         printf("\n");
 1432                 }
 1433                 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
 1434                     hz);
 1435         }
 1436 }
 1437 
 1438 /*
 1439  * Root was mounted, share the good news.
 1440  */
 1441 static void
 1442 root_mount_done(void)
 1443 {
 1444 
 1445         /* Keep prison0's root in sync with the global rootvnode. */
 1446         mtx_lock(&prison0.pr_mtx);
 1447         prison0.pr_root = rootvnode;
 1448         vref(prison0.pr_root);
 1449         mtx_unlock(&prison0.pr_mtx);
 1450         /*
 1451          * Use a mutex to prevent the wakeup being missed and waiting for
 1452          * an extra 1 second sleep.
 1453          */
 1454         mtx_lock(&mountlist_mtx);
 1455         root_mount_complete = 1;
 1456         wakeup(&root_mount_complete);
 1457         mtx_unlock(&mountlist_mtx);
 1458 }
 1459 
 1460 /*
 1461  * Return true if root is already mounted.
 1462  */
 1463 int
 1464 root_mounted(void)
 1465 {
 1466 
 1467         /* No mutex is acquired here because int stores are atomic. */
 1468         return (root_mount_complete);
 1469 }
 1470 
 1471 /*
 1472  * Wait until root is mounted.
 1473  */
 1474 void
 1475 root_mount_wait(void)
 1476 {
 1477 
 1478         /*
 1479          * Panic on an obvious deadlock - the function can't be called from
 1480          * a thread which is doing the whole SYSINIT stuff.
 1481          */
 1482         KASSERT(curthread->td_proc->p_pid != 0,
 1483             ("root_mount_wait: cannot be called from the swapper thread"));
 1484         mtx_lock(&mountlist_mtx);
 1485         while (!root_mount_complete) {
 1486                 msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait",
 1487                     hz);
 1488         }
 1489         mtx_unlock(&mountlist_mtx);
 1490 }
 1491 
 1492 static void
 1493 set_rootvnode()
 1494 {
 1495         struct proc *p;
 1496 
 1497         if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
 1498                 panic("Cannot find root vnode");
 1499 
 1500         VOP_UNLOCK(rootvnode, 0);
 1501 
 1502         p = curthread->td_proc;
 1503         FILEDESC_XLOCK(p->p_fd);
 1504 
 1505         if (p->p_fd->fd_cdir != NULL)
 1506                 vrele(p->p_fd->fd_cdir);
 1507         p->p_fd->fd_cdir = rootvnode;
 1508         VREF(rootvnode);
 1509 
 1510         if (p->p_fd->fd_rdir != NULL)
 1511                 vrele(p->p_fd->fd_rdir);
 1512         p->p_fd->fd_rdir = rootvnode;
 1513         VREF(rootvnode);
 1514 
 1515         FILEDESC_XUNLOCK(p->p_fd);
 1516 
 1517         EVENTHANDLER_INVOKE(mountroot);
 1518 }
 1519 
 1520 /*
 1521  * Mount /devfs as our root filesystem, but do not put it on the mountlist
 1522  * yet.  Create a /dev -> / symlink so that absolute pathnames will lookup.
 1523  */
 1524 
 1525 static void
 1526 devfs_first(void)
 1527 {
 1528         struct thread *td = curthread;
 1529         struct vfsoptlist *opts;
 1530         struct vfsconf *vfsp;
 1531         struct mount *mp = NULL;
 1532         int error;
 1533 
 1534         vfsp = vfs_byname("devfs");
 1535         KASSERT(vfsp != NULL, ("Could not find devfs by name"));
 1536         if (vfsp == NULL)
 1537                 return;
 1538 
 1539         mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
 1540 
 1541         error = VFS_MOUNT(mp);
 1542         KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
 1543         if (error)
 1544                 return;
 1545 
 1546         opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
 1547         TAILQ_INIT(opts);
 1548         mp->mnt_opt = opts;
 1549 
 1550         mtx_lock(&mountlist_mtx);
 1551         TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
 1552         mtx_unlock(&mountlist_mtx);
 1553 
 1554         set_rootvnode();
 1555 
 1556         error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
 1557         if (error)
 1558                 printf("kern_symlink /dev -> / returns %d\n", error);
 1559 }
 1560 
 1561 /*
 1562  * Surgically move our devfs to be mounted on /dev.
 1563  */
 1564 
 1565 static void
 1566 devfs_fixup(struct thread *td)
 1567 {
 1568         struct nameidata nd;
 1569         int error;
 1570         struct vnode *vp, *dvp;
 1571         struct mount *mp;
 1572 
 1573         /* Remove our devfs mount from the mountlist and purge the cache */
 1574         mtx_lock(&mountlist_mtx);
 1575         mp = TAILQ_FIRST(&mountlist);
 1576         TAILQ_REMOVE(&mountlist, mp, mnt_list);
 1577         mtx_unlock(&mountlist_mtx);
 1578         cache_purgevfs(mp);
 1579 
 1580         VFS_ROOT(mp, LK_EXCLUSIVE, &dvp);
 1581         VI_LOCK(dvp);
 1582         dvp->v_iflag &= ~VI_MOUNT;
 1583         VI_UNLOCK(dvp);
 1584         dvp->v_mountedhere = NULL;
 1585 
 1586         /* Set up the real rootvnode, and purge the cache */
 1587         TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
 1588         set_rootvnode();
 1589         cache_purgevfs(rootvnode->v_mount);
 1590 
 1591         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
 1592         error = namei(&nd);
 1593         if (error) {
 1594                 printf("Lookup of /dev for devfs, error: %d\n", error);
 1595                 return;
 1596         }
 1597         NDFREE(&nd, NDF_ONLY_PNBUF);
 1598         vp = nd.ni_vp;
 1599         if (vp->v_type != VDIR) {
 1600                 vput(vp);
 1601         }
 1602         error = vinvalbuf(vp, V_SAVE, 0, 0);
 1603         if (error) {
 1604                 vput(vp);
 1605         }
 1606         cache_purge(vp);
 1607         mp->mnt_vnodecovered = vp;
 1608         vp->v_mountedhere = mp;
 1609         mtx_lock(&mountlist_mtx);
 1610         TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
 1611         mtx_unlock(&mountlist_mtx);
 1612         VOP_UNLOCK(vp, 0);
 1613         vput(dvp);
 1614         vfs_unbusy(mp);
 1615 
 1616         /* Unlink the no longer needed /dev/dev -> / symlink */
 1617         kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
 1618 }
 1619 
 1620 /*
 1621  * Report errors during filesystem mounting.
 1622  */
 1623 void
 1624 vfs_mount_error(struct mount *mp, const char *fmt, ...)
 1625 {
 1626         struct vfsoptlist *moptlist = mp->mnt_optnew;
 1627         va_list ap;
 1628         int error, len;
 1629         char *errmsg;
 1630 
 1631         error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
 1632         if (error || errmsg == NULL || len <= 0)
 1633                 return;
 1634 
 1635         va_start(ap, fmt);
 1636         vsnprintf(errmsg, (size_t)len, fmt, ap);
 1637         va_end(ap);
 1638 }
 1639 
 1640 void
 1641 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
 1642 {
 1643         va_list ap;
 1644         int error, len;
 1645         char *errmsg;
 1646 
 1647         error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
 1648         if (error || errmsg == NULL || len <= 0)
 1649                 return;
 1650 
 1651         va_start(ap, fmt);
 1652         vsnprintf(errmsg, (size_t)len, fmt, ap);
 1653         va_end(ap);
 1654 }
 1655 
 1656 /*
 1657  * Find and mount the root filesystem
 1658  */
 1659 void
 1660 vfs_mountroot(void)
 1661 {
 1662         char *cp, *cpt, *options, *tmpdev;
 1663         int error, i, asked = 0;
 1664 
 1665         options = NULL;
 1666 
 1667         root_mount_prepare();
 1668 
 1669         mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
 1670             NULL, NULL, mount_init, mount_fini,
 1671             UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
 1672         devfs_first();
 1673 
 1674         /*
 1675          * We are booted with instructions to prompt for the root filesystem.
 1676          */
 1677         if (boothowto & RB_ASKNAME) {
 1678                 if (!vfs_mountroot_ask())
 1679                         goto mounted;
 1680                 asked = 1;
 1681         }
 1682 
 1683         options = getenv("vfs.root.mountfrom.options");
 1684 
 1685         /*
 1686          * The root filesystem information is compiled in, and we are
 1687          * booted with instructions to use it.
 1688          */
 1689         if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
 1690                 if (!vfs_mountroot_try(ctrootdevname, options))
 1691                         goto mounted;
 1692                 ctrootdevname = NULL;
 1693         }
 1694 
 1695         /*
 1696          * We've been given the generic "use CDROM as root" flag.  This is
 1697          * necessary because one media may be used in many different
 1698          * devices, so we need to search for them.
 1699          */
 1700         if (boothowto & RB_CDROM) {
 1701                 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
 1702                         if (!vfs_mountroot_try(cdrom_rootdevnames[i], options))
 1703                                 goto mounted;
 1704                 }
 1705         }
 1706 
 1707         /*
 1708          * Try to use the value read by the loader from /etc/fstab, or
 1709          * supplied via some other means.  This is the preferred
 1710          * mechanism.
 1711          */
 1712         cp = getenv("vfs.root.mountfrom");
 1713         if (cp != NULL) {
 1714                 cpt = cp;
 1715                 while ((tmpdev = strsep(&cpt, " \t")) != NULL) {
 1716                         error = vfs_mountroot_try(tmpdev, options);
 1717                         if (error == 0) {
 1718                                 freeenv(cp);
 1719                                 goto mounted;
 1720                         }
 1721                 }
 1722                 freeenv(cp);
 1723         }
 1724 
 1725         /*
 1726          * Try values that may have been computed by code during boot
 1727          */
 1728         if (!vfs_mountroot_try(rootdevnames[0], options))
 1729                 goto mounted;
 1730         if (!vfs_mountroot_try(rootdevnames[1], options))
 1731                 goto mounted;
 1732 
 1733         /*
 1734          * If we (still) have a compiled-in default, try it.
 1735          */
 1736         if (ctrootdevname != NULL)
 1737                 if (!vfs_mountroot_try(ctrootdevname, options))
 1738                         goto mounted;
 1739         /*
 1740          * Everything so far has failed, prompt on the console if we haven't
 1741          * already tried that.
 1742          */
 1743         if (!asked)
 1744                 if (!vfs_mountroot_ask())
 1745                         goto mounted;
 1746 
 1747         panic("Root mount failed, startup aborted.");
 1748 
 1749 mounted:
 1750         root_mount_done();
 1751         freeenv(options);
 1752 }
 1753 
 1754 static struct mntarg *
 1755 parse_mountroot_options(struct mntarg *ma, const char *options)
 1756 {
 1757         char *p;
 1758         char *name, *name_arg;
 1759         char *val, *val_arg;
 1760         char *opts;
 1761 
 1762         if (options == NULL || options[0] == '\0')
 1763                 return (ma);
 1764 
 1765         p = opts = strdup(options, M_MOUNT);
 1766         if (opts == NULL) {
 1767                 return (ma);
 1768         } 
 1769 
 1770         while((name = strsep(&p, ",")) != NULL) {
 1771                 if (name[0] == '\0')
 1772                         break;
 1773 
 1774                 val = strchr(name, '=');
 1775                 if (val != NULL) {
 1776                         *val = '\0';
 1777                         ++val;
 1778                 }
 1779                 if( strcmp(name, "rw") == 0 ||
 1780                     strcmp(name, "noro") == 0) {
 1781                         /*
 1782                          * The first time we mount the root file system,
 1783                          * we need to mount 'ro', so We need to ignore
 1784                          * 'rw' and 'noro' mount options.
 1785                          */
 1786                         continue;
 1787                 }
 1788                 name_arg = strdup(name, M_MOUNT);
 1789                 val_arg = NULL;
 1790                 if (val != NULL) 
 1791                         val_arg = strdup(val, M_MOUNT);
 1792 
 1793                 ma = mount_arg(ma, name_arg, val_arg,
 1794                     (val_arg != NULL ? -1 : 0));
 1795         }
 1796         free(opts, M_MOUNT);
 1797         return (ma);
 1798 }
 1799 
 1800 /*
 1801  * Mount (mountfrom) as the root filesystem.
 1802  */
 1803 static int
 1804 vfs_mountroot_try(const char *mountfrom, const char *options)
 1805 {
 1806         struct mount    *mp;
 1807         struct mntarg   *ma;
 1808         char            *vfsname, *path;
 1809         time_t          timebase;
 1810         int             error;
 1811         char            patt[32];
 1812         char            errmsg[255];
 1813 
 1814         vfsname = NULL;
 1815         path    = NULL;
 1816         mp      = NULL;
 1817         ma      = NULL;
 1818         error   = EINVAL;
 1819         bzero(errmsg, sizeof(errmsg));
 1820 
 1821         if (mountfrom == NULL)
 1822                 return (error);         /* don't complain */
 1823         printf("Trying to mount root from %s\n", mountfrom);
 1824 
 1825         /* parse vfs name and path */
 1826         vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
 1827         path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
 1828         vfsname[0] = path[0] = 0;
 1829         sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
 1830         if (sscanf(mountfrom, patt, vfsname, path) < 1)
 1831                 goto out;
 1832 
 1833         if (path[0] == '\0')
 1834                 strcpy(path, ROOTNAME);
 1835 
 1836         ma = mount_arg(ma, "fstype", vfsname, -1);
 1837         ma = mount_arg(ma, "fspath", "/", -1);
 1838         ma = mount_arg(ma, "from", path, -1);
 1839         ma = mount_arg(ma, "errmsg", errmsg, sizeof(errmsg));
 1840         ma = mount_arg(ma, "ro", NULL, 0);
 1841         ma = parse_mountroot_options(ma, options);
 1842         error = kernel_mount(ma, MNT_ROOTFS);
 1843 
 1844         if (error == 0) {
 1845                 /*
 1846                  * We mount devfs prior to mounting the / FS, so the first
 1847                  * entry will typically be devfs.
 1848                  */
 1849                 mp = TAILQ_FIRST(&mountlist);
 1850                 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
 1851 
 1852                 /*
 1853                  * Iterate over all currently mounted file systems and use
 1854                  * the time stamp found to check and/or initialize the RTC.
 1855                  * Typically devfs has no time stamp and the only other FS
 1856                  * is the actual / FS.
 1857                  * Call inittodr() only once and pass it the largest of the
 1858                  * timestamps we encounter.
 1859                  */
 1860                 timebase = 0;
 1861                 do {
 1862                         if (mp->mnt_time > timebase)
 1863                                 timebase = mp->mnt_time;
 1864                         mp = TAILQ_NEXT(mp, mnt_list);
 1865                 } while (mp != NULL);
 1866                 inittodr(timebase);
 1867 
 1868                 devfs_fixup(curthread);
 1869         }
 1870 
 1871         if (error != 0 ) {
 1872                 printf("ROOT MOUNT ERROR: %s\n", errmsg);
 1873                 printf("If you have invalid mount options, reboot, and ");
 1874                 printf("first try the following from\n");
 1875                 printf("the loader prompt:\n\n");
 1876                 printf("     set vfs.root.mountfrom.options=rw\n\n");
 1877                 printf("and then remove invalid mount options from ");
 1878                 printf("/etc/fstab.\n\n");
 1879         }
 1880 out:
 1881         free(path, M_MOUNT);
 1882         free(vfsname, M_MOUNT);
 1883         return (error);
 1884 }
 1885 
 1886 /*
 1887  * ---------------------------------------------------------------------
 1888  * Interactive root filesystem selection code.
 1889  */
 1890 
 1891 static int
 1892 vfs_mountroot_ask(void)
 1893 {
 1894         char name[128];
 1895         char *mountfrom;
 1896         char *options;
 1897 
 1898         for(;;) {
 1899                 printf("Loader variables:\n");
 1900                 printf("vfs.root.mountfrom=");
 1901                 mountfrom = getenv("vfs.root.mountfrom");
 1902                 if (mountfrom != NULL) {
 1903                         printf("%s", mountfrom);
 1904                 }
 1905                 printf("\n");
 1906                 printf("vfs.root.mountfrom.options=");
 1907                 options = getenv("vfs.root.mountfrom.options");
 1908                 if (options != NULL) {
 1909                         printf("%s", options);
 1910                 }
 1911                 printf("\n");
 1912                 freeenv(mountfrom);
 1913                 freeenv(options);
 1914                 printf("\nManual root filesystem specification:\n");
 1915                 printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
 1916                 printf("                       eg. ufs:/dev/da0s1a\n");
 1917                 printf("                       eg. cd9660:/dev/acd0\n");
 1918                 printf("                       This is equivalent to: ");
 1919                 printf("mount -t cd9660 /dev/acd0 /\n"); 
 1920                 printf("\n");
 1921                 printf("  ?                  List valid disk boot devices\n");
 1922                 printf("  <empty line>       Abort manual input\n");
 1923                 printf("\nmountroot> ");
 1924                 gets(name, sizeof(name), 1);
 1925                 if (name[0] == '\0')
 1926                         return (1);
 1927                 if (name[0] == '?') {
 1928                         printf("\nList of GEOM managed disk devices:\n  ");
 1929                         g_dev_print();
 1930                         continue;
 1931                 }
 1932                 if (!vfs_mountroot_try(name, NULL))
 1933                         return (0);
 1934         }
 1935 }
 1936 
 1937 /*
 1938  * ---------------------------------------------------------------------
 1939  * Functions for querying mount options/arguments from filesystems.
 1940  */
 1941 
 1942 /*
 1943  * Check that no unknown options are given
 1944  */
 1945 int
 1946 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
 1947 {
 1948         struct vfsopt *opt;
 1949         char errmsg[255];
 1950         const char **t, *p, *q;
 1951         int ret = 0;
 1952 
 1953         TAILQ_FOREACH(opt, opts, link) {
 1954                 p = opt->name;
 1955                 q = NULL;
 1956                 if (p[0] == 'n' && p[1] == 'o')
 1957                         q = p + 2;
 1958                 for(t = global_opts; *t != NULL; t++) {
 1959                         if (strcmp(*t, p) == 0)
 1960                                 break;
 1961                         if (q != NULL) {
 1962                                 if (strcmp(*t, q) == 0)
 1963                                         break;
 1964                         }
 1965                 }
 1966                 if (*t != NULL)
 1967                         continue;
 1968                 for(t = legal; *t != NULL; t++) {
 1969                         if (strcmp(*t, p) == 0)
 1970                                 break;
 1971                         if (q != NULL) {
 1972                                 if (strcmp(*t, q) == 0)
 1973                                         break;
 1974                         }
 1975                 }
 1976                 if (*t != NULL)
 1977                         continue;
 1978                 snprintf(errmsg, sizeof(errmsg),
 1979                     "mount option <%s> is unknown", p);
 1980                 printf("%s\n", errmsg);
 1981                 ret = EINVAL;
 1982         }
 1983         if (ret != 0) {
 1984                 TAILQ_FOREACH(opt, opts, link) {
 1985                         if (strcmp(opt->name, "errmsg") == 0) {
 1986                                 strncpy((char *)opt->value, errmsg, opt->len);
 1987                         }
 1988                 }
 1989         }
 1990         return (ret);
 1991 }
 1992 
 1993 /*
 1994  * Get a mount option by its name.
 1995  *
 1996  * Return 0 if the option was found, ENOENT otherwise.
 1997  * If len is non-NULL it will be filled with the length
 1998  * of the option. If buf is non-NULL, it will be filled
 1999  * with the address of the option.
 2000  */
 2001 int
 2002 vfs_getopt(opts, name, buf, len)
 2003         struct vfsoptlist *opts;
 2004         const char *name;
 2005         void **buf;
 2006         int *len;
 2007 {
 2008         struct vfsopt *opt;
 2009 
 2010         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
 2011 
 2012         TAILQ_FOREACH(opt, opts, link) {
 2013                 if (strcmp(name, opt->name) == 0) {
 2014                         opt->seen = 1;
 2015                         if (len != NULL)
 2016                                 *len = opt->len;
 2017                         if (buf != NULL)
 2018                                 *buf = opt->value;
 2019                         return (0);
 2020                 }
 2021         }
 2022         return (ENOENT);
 2023 }
 2024 
 2025 int
 2026 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
 2027 {
 2028         struct vfsopt *opt;
 2029 
 2030         if (opts == NULL)
 2031                 return (-1);
 2032 
 2033         TAILQ_FOREACH(opt, opts, link) {
 2034                 if (strcmp(name, opt->name) == 0) {
 2035                         opt->seen = 1;
 2036                         return (opt->pos);
 2037                 }
 2038         }
 2039         return (-1);
 2040 }
 2041 
 2042 char *
 2043 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
 2044 {
 2045         struct vfsopt *opt;
 2046 
 2047         *error = 0;
 2048         TAILQ_FOREACH(opt, opts, link) {
 2049                 if (strcmp(name, opt->name) != 0)
 2050                         continue;
 2051                 opt->seen = 1;
 2052                 if (opt->len == 0 ||
 2053                     ((char *)opt->value)[opt->len - 1] != '\0') {
 2054                         *error = EINVAL;
 2055                         return (NULL);
 2056                 }
 2057                 return (opt->value);
 2058         }
 2059         *error = ENOENT;
 2060         return (NULL);
 2061 }
 2062 
 2063 int
 2064 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
 2065 {
 2066         struct vfsopt *opt;
 2067 
 2068         TAILQ_FOREACH(opt, opts, link) {
 2069                 if (strcmp(name, opt->name) == 0) {
 2070                         opt->seen = 1;
 2071                         if (w != NULL)
 2072                                 *w |= val;
 2073                         return (1);
 2074                 }
 2075         }
 2076         if (w != NULL)
 2077                 *w &= ~val;
 2078         return (0);
 2079 }
 2080 
 2081 int
 2082 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
 2083 {
 2084         va_list ap;
 2085         struct vfsopt *opt;
 2086         int ret;
 2087 
 2088         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
 2089 
 2090         TAILQ_FOREACH(opt, opts, link) {
 2091                 if (strcmp(name, opt->name) != 0)
 2092                         continue;
 2093                 opt->seen = 1;
 2094                 if (opt->len == 0 || opt->value == NULL)
 2095                         return (0);
 2096                 if (((char *)opt->value)[opt->len - 1] != '\0')
 2097                         return (0);
 2098                 va_start(ap, fmt);
 2099                 ret = vsscanf(opt->value, fmt, ap);
 2100                 va_end(ap);
 2101                 return (ret);
 2102         }
 2103         return (0);
 2104 }
 2105 
 2106 int
 2107 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
 2108 {
 2109         struct vfsopt *opt;
 2110 
 2111         TAILQ_FOREACH(opt, opts, link) {
 2112                 if (strcmp(name, opt->name) != 0)
 2113                         continue;
 2114                 opt->seen = 1;
 2115                 if (opt->value == NULL)
 2116                         opt->len = len;
 2117                 else {
 2118                         if (opt->len != len)
 2119                                 return (EINVAL);
 2120                         bcopy(value, opt->value, len);
 2121                 }
 2122                 return (0);
 2123         }
 2124         return (ENOENT);
 2125 }
 2126 
 2127 int
 2128 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
 2129 {
 2130         struct vfsopt *opt;
 2131 
 2132         TAILQ_FOREACH(opt, opts, link) {
 2133                 if (strcmp(name, opt->name) != 0)
 2134                         continue;
 2135                 opt->seen = 1;
 2136                 if (opt->value == NULL)
 2137                         opt->len = len;
 2138                 else {
 2139                         if (opt->len < len)
 2140                                 return (EINVAL);
 2141                         opt->len = len;
 2142                         bcopy(value, opt->value, len);
 2143                 }
 2144                 return (0);
 2145         }
 2146         return (ENOENT);
 2147 }
 2148 
 2149 int
 2150 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
 2151 {
 2152         struct vfsopt *opt;
 2153 
 2154         TAILQ_FOREACH(opt, opts, link) {
 2155                 if (strcmp(name, opt->name) != 0)
 2156                         continue;
 2157                 opt->seen = 1;
 2158                 if (opt->value == NULL)
 2159                         opt->len = strlen(value) + 1;
 2160                 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
 2161                         return (EINVAL);
 2162                 return (0);
 2163         }
 2164         return (ENOENT);
 2165 }
 2166 
 2167 /*
 2168  * Find and copy a mount option.
 2169  *
 2170  * The size of the buffer has to be specified
 2171  * in len, if it is not the same length as the
 2172  * mount option, EINVAL is returned.
 2173  * Returns ENOENT if the option is not found.
 2174  */
 2175 int
 2176 vfs_copyopt(opts, name, dest, len)
 2177         struct vfsoptlist *opts;
 2178         const char *name;
 2179         void *dest;
 2180         int len;
 2181 {
 2182         struct vfsopt *opt;
 2183 
 2184         KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
 2185 
 2186         TAILQ_FOREACH(opt, opts, link) {
 2187                 if (strcmp(name, opt->name) == 0) {
 2188                         opt->seen = 1;
 2189                         if (len != opt->len)
 2190                                 return (EINVAL);
 2191                         bcopy(opt->value, dest, opt->len);
 2192                         return (0);
 2193                 }
 2194         }
 2195         return (ENOENT);
 2196 }
 2197 
 2198 /*
 2199  * This is a helper function for filesystems to traverse their
 2200  * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
 2201  */
 2202 
 2203 struct vnode *
 2204 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
 2205 {
 2206         struct vnode *vp;
 2207 
 2208         mtx_assert(MNT_MTX(mp), MA_OWNED);
 2209 
 2210         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
 2211         if ((*mvp)->v_yield++ == 500) {
 2212                 MNT_IUNLOCK(mp);
 2213                 (*mvp)->v_yield = 0;
 2214                 uio_yield();
 2215                 MNT_ILOCK(mp);
 2216         }
 2217         vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
 2218         while (vp != NULL && vp->v_type == VMARKER)
 2219                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
 2220 
 2221         /* Check if we are done */
 2222         if (vp == NULL) {
 2223                 __mnt_vnode_markerfree(mvp, mp);
 2224                 return (NULL);
 2225         }
 2226         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
 2227         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
 2228         return (vp);
 2229 }
 2230 
 2231 struct vnode *
 2232 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
 2233 {
 2234         struct vnode *vp;
 2235 
 2236         mtx_assert(MNT_MTX(mp), MA_OWNED);
 2237 
 2238         vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
 2239         while (vp != NULL && vp->v_type == VMARKER)
 2240                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
 2241 
 2242         /* Check if we are done */
 2243         if (vp == NULL) {
 2244                 *mvp = NULL;
 2245                 return (NULL);
 2246         }
 2247         MNT_REF(mp);
 2248         MNT_IUNLOCK(mp);
 2249         *mvp = (struct vnode *) malloc(sizeof(struct vnode),
 2250                                        M_VNODE_MARKER,
 2251                                        M_WAITOK | M_ZERO);
 2252         MNT_ILOCK(mp);
 2253         (*mvp)->v_type = VMARKER;
 2254 
 2255         vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
 2256         while (vp != NULL && vp->v_type == VMARKER)
 2257                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
 2258 
 2259         /* Check if we are done */
 2260         if (vp == NULL) {
 2261                 MNT_IUNLOCK(mp);
 2262                 free(*mvp, M_VNODE_MARKER);
 2263                 MNT_ILOCK(mp);
 2264                 *mvp = NULL;
 2265                 MNT_REL(mp);
 2266                 return (NULL);
 2267         }
 2268         (*mvp)->v_mount = mp;
 2269         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
 2270         return (vp);
 2271 }
 2272 
 2273 
 2274 void
 2275 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
 2276 {
 2277 
 2278         if (*mvp == NULL)
 2279                 return;
 2280 
 2281         mtx_assert(MNT_MTX(mp), MA_OWNED);
 2282 
 2283         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
 2284         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
 2285         MNT_IUNLOCK(mp);
 2286         free(*mvp, M_VNODE_MARKER);
 2287         MNT_ILOCK(mp);
 2288         *mvp = NULL;
 2289         MNT_REL(mp);
 2290 }
 2291 
 2292 
 2293 int
 2294 __vfs_statfs(struct mount *mp, struct statfs *sbp)
 2295 {
 2296         int error;
 2297 
 2298         error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
 2299         if (sbp != &mp->mnt_stat)
 2300                 *sbp = mp->mnt_stat;
 2301         return (error);
 2302 }
 2303 
 2304 void
 2305 vfs_mountedfrom(struct mount *mp, const char *from)
 2306 {
 2307 
 2308         bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
 2309         strlcpy(mp->mnt_stat.f_mntfromname, from,
 2310             sizeof mp->mnt_stat.f_mntfromname);
 2311 }
 2312 
 2313 /*
 2314  * ---------------------------------------------------------------------
 2315  * This is the api for building mount args and mounting filesystems from
 2316  * inside the kernel.
 2317  *
 2318  * The API works by accumulation of individual args.  First error is
 2319  * latched.
 2320  *
 2321  * XXX: should be documented in new manpage kernel_mount(9)
 2322  */
 2323 
 2324 /* A memory allocation which must be freed when we are done */
 2325 struct mntaarg {
 2326         SLIST_ENTRY(mntaarg)    next;
 2327 };
 2328 
 2329 /* The header for the mount arguments */
 2330 struct mntarg {
 2331         struct iovec *v;
 2332         int len;
 2333         int error;
 2334         SLIST_HEAD(, mntaarg)   list;
 2335 };
 2336 
 2337 /*
 2338  * Add a boolean argument.
 2339  *
 2340  * flag is the boolean value.
 2341  * name must start with "no".
 2342  */
 2343 struct mntarg *
 2344 mount_argb(struct mntarg *ma, int flag, const char *name)
 2345 {
 2346 
 2347         KASSERT(name[0] == 'n' && name[1] == 'o',
 2348             ("mount_argb(...,%s): name must start with 'no'", name));
 2349 
 2350         return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
 2351 }
 2352 
 2353 /*
 2354  * Add an argument printf style
 2355  */
 2356 struct mntarg *
 2357 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
 2358 {
 2359         va_list ap;
 2360         struct mntaarg *maa;
 2361         struct sbuf *sb;
 2362         int len;
 2363 
 2364         if (ma == NULL) {
 2365                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
 2366                 SLIST_INIT(&ma->list);
 2367         }
 2368         if (ma->error)
 2369                 return (ma);
 2370 
 2371         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
 2372             M_MOUNT, M_WAITOK);
 2373         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
 2374         ma->v[ma->len].iov_len = strlen(name) + 1;
 2375         ma->len++;
 2376 
 2377         sb = sbuf_new_auto();
 2378         va_start(ap, fmt);
 2379         sbuf_vprintf(sb, fmt, ap);
 2380         va_end(ap);
 2381         sbuf_finish(sb);
 2382         len = sbuf_len(sb) + 1;
 2383         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
 2384         SLIST_INSERT_HEAD(&ma->list, maa, next);
 2385         bcopy(sbuf_data(sb), maa + 1, len);
 2386         sbuf_delete(sb);
 2387 
 2388         ma->v[ma->len].iov_base = maa + 1;
 2389         ma->v[ma->len].iov_len = len;
 2390         ma->len++;
 2391 
 2392         return (ma);
 2393 }
 2394 
 2395 /*
 2396  * Add an argument which is a userland string.
 2397  */
 2398 struct mntarg *
 2399 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
 2400 {
 2401         struct mntaarg *maa;
 2402         char *tbuf;
 2403 
 2404         if (val == NULL)
 2405                 return (ma);
 2406         if (ma == NULL) {
 2407                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
 2408                 SLIST_INIT(&ma->list);
 2409         }
 2410         if (ma->error)
 2411                 return (ma);
 2412         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
 2413         SLIST_INSERT_HEAD(&ma->list, maa, next);
 2414         tbuf = (void *)(maa + 1);
 2415         ma->error = copyinstr(val, tbuf, len, NULL);
 2416         return (mount_arg(ma, name, tbuf, -1));
 2417 }
 2418 
 2419 /*
 2420  * Plain argument.
 2421  *
 2422  * If length is -1, treat value as a C string.
 2423  */
 2424 struct mntarg *
 2425 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
 2426 {
 2427 
 2428         if (ma == NULL) {
 2429                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
 2430                 SLIST_INIT(&ma->list);
 2431         }
 2432         if (ma->error)
 2433                 return (ma);
 2434 
 2435         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
 2436             M_MOUNT, M_WAITOK);
 2437         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
 2438         ma->v[ma->len].iov_len = strlen(name) + 1;
 2439         ma->len++;
 2440 
 2441         ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
 2442         if (len < 0)
 2443                 ma->v[ma->len].iov_len = strlen(val) + 1;
 2444         else
 2445                 ma->v[ma->len].iov_len = len;
 2446         ma->len++;
 2447         return (ma);
 2448 }
 2449 
 2450 /*
 2451  * Free a mntarg structure
 2452  */
 2453 static void
 2454 free_mntarg(struct mntarg *ma)
 2455 {
 2456         struct mntaarg *maa;
 2457 
 2458         while (!SLIST_EMPTY(&ma->list)) {
 2459                 maa = SLIST_FIRST(&ma->list);
 2460                 SLIST_REMOVE_HEAD(&ma->list, next);
 2461                 free(maa, M_MOUNT);
 2462         }
 2463         free(ma->v, M_MOUNT);
 2464         free(ma, M_MOUNT);
 2465 }
 2466 
 2467 /*
 2468  * Mount a filesystem
 2469  */
 2470 int
 2471 kernel_mount(struct mntarg *ma, int flags)
 2472 {
 2473         struct uio auio;
 2474         int error;
 2475 
 2476         KASSERT(ma != NULL, ("kernel_mount NULL ma"));
 2477         KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
 2478         KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
 2479 
 2480         auio.uio_iov = ma->v;
 2481         auio.uio_iovcnt = ma->len;
 2482         auio.uio_segflg = UIO_SYSSPACE;
 2483 
 2484         error = ma->error;
 2485         if (!error)
 2486                 error = vfs_donmount(curthread, flags, &auio);
 2487         free_mntarg(ma);
 2488         return (error);
 2489 }
 2490 
 2491 /*
 2492  * A printflike function to mount a filesystem.
 2493  */
 2494 int
 2495 kernel_vmount(int flags, ...)
 2496 {
 2497         struct mntarg *ma = NULL;
 2498         va_list ap;
 2499         const char *cp;
 2500         const void *vp;
 2501         int error;
 2502 
 2503         va_start(ap, flags);
 2504         for (;;) {
 2505                 cp = va_arg(ap, const char *);
 2506                 if (cp == NULL)
 2507                         break;
 2508                 vp = va_arg(ap, const void *);
 2509                 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
 2510         }
 2511         va_end(ap);
 2512 
 2513         error = kernel_mount(ma, flags);
 2514         return (error);
 2515 }
 2516 
 2517 void
 2518 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
 2519 {
 2520 
 2521         bcopy(oexp, exp, sizeof(*oexp));
 2522         exp->ex_numsecflavors = 0;
 2523 }

Cache object: f7fe3d4bbd10b9e51ecc93764b1bba00


[ 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.