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


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

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

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

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

Cache object: 60253f74604f07ce827e3c5d4e3e3e1a


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