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/11.2/sys/kern/vfs_mount.c 332753 2018-04-19 05:37:32Z avg $");
   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/sbuf.h>
   55 #include <sys/syscallsubr.h>
   56 #include <sys/sysproto.h>
   57 #include <sys/sx.h>
   58 #include <sys/sysctl.h>
   59 #include <sys/sysent.h>
   60 #include <sys/systm.h>
   61 #include <sys/vnode.h>
   62 #include <vm/uma.h>
   63 
   64 #include <geom/geom.h>
   65 
   66 #include <machine/stdarg.h>
   67 
   68 #include <security/audit/audit.h>
   69 #include <security/mac/mac_framework.h>
   70 
   71 #define VFS_MOUNTARG_SIZE_MAX   (1024 * 64)
   72 
   73 static int      vfs_domount(struct thread *td, const char *fstype, char *fspath,
   74                     uint64_t fsflags, struct vfsoptlist **optlist);
   75 static void     free_mntarg(struct mntarg *ma);
   76 
   77 static int      usermount = 0;
   78 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
   79     "Unprivileged users may mount and unmount file systems");
   80 
   81 static bool     default_autoro = false;
   82 SYSCTL_BOOL(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0,
   83     "Retry failed r/w mount as r/o if no explicit ro/rw option is specified");
   84 
   85 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
   86 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure");
   87 static uma_zone_t mount_zone;
   88 
   89 /* List of mounted filesystems. */
   90 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
   91 
   92 /* For any iteration/modification of mountlist */
   93 struct mtx mountlist_mtx;
   94 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
   95 
   96 /*
   97  * Global opts, taken by all filesystems
   98  */
   99 static const char *global_opts[] = {
  100         "errmsg",
  101         "fstype",
  102         "fspath",
  103         "ro",
  104         "rw",
  105         "nosuid",
  106         "noexec",
  107         NULL
  108 };
  109 
  110 static int
  111 mount_init(void *mem, int size, int flags)
  112 {
  113         struct mount *mp;
  114 
  115         mp = (struct mount *)mem;
  116         mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
  117         lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
  118         return (0);
  119 }
  120 
  121 static void
  122 mount_fini(void *mem, int size)
  123 {
  124         struct mount *mp;
  125 
  126         mp = (struct mount *)mem;
  127         lockdestroy(&mp->mnt_explock);
  128         mtx_destroy(&mp->mnt_mtx);
  129 }
  130 
  131 static void
  132 vfs_mount_init(void *dummy __unused)
  133 {
  134 
  135         mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
  136             NULL, mount_init, mount_fini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  137 }
  138 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);
  139 
  140 /*
  141  * ---------------------------------------------------------------------
  142  * Functions for building and sanitizing the mount options
  143  */
  144 
  145 /* Remove one mount option. */
  146 static void
  147 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
  148 {
  149 
  150         TAILQ_REMOVE(opts, opt, link);
  151         free(opt->name, M_MOUNT);
  152         if (opt->value != NULL)
  153                 free(opt->value, M_MOUNT);
  154         free(opt, M_MOUNT);
  155 }
  156 
  157 /* Release all resources related to the mount options. */
  158 void
  159 vfs_freeopts(struct vfsoptlist *opts)
  160 {
  161         struct vfsopt *opt;
  162 
  163         while (!TAILQ_EMPTY(opts)) {
  164                 opt = TAILQ_FIRST(opts);
  165                 vfs_freeopt(opts, opt);
  166         }
  167         free(opts, M_MOUNT);
  168 }
  169 
  170 void
  171 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
  172 {
  173         struct vfsopt *opt, *temp;
  174 
  175         if (opts == NULL)
  176                 return;
  177         TAILQ_FOREACH_SAFE(opt, opts, link, temp)  {
  178                 if (strcmp(opt->name, name) == 0)
  179                         vfs_freeopt(opts, opt);
  180         }
  181 }
  182 
  183 static int
  184 vfs_isopt_ro(const char *opt)
  185 {
  186 
  187         if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
  188             strcmp(opt, "norw") == 0)
  189                 return (1);
  190         return (0);
  191 }
  192 
  193 static int
  194 vfs_isopt_rw(const char *opt)
  195 {
  196 
  197         if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
  198                 return (1);
  199         return (0);
  200 }
  201 
  202 /*
  203  * Check if options are equal (with or without the "no" prefix).
  204  */
  205 static int
  206 vfs_equalopts(const char *opt1, const char *opt2)
  207 {
  208         char *p;
  209 
  210         /* "opt" vs. "opt" or "noopt" vs. "noopt" */
  211         if (strcmp(opt1, opt2) == 0)
  212                 return (1);
  213         /* "noopt" vs. "opt" */
  214         if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
  215                 return (1);
  216         /* "opt" vs. "noopt" */
  217         if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
  218                 return (1);
  219         while ((p = strchr(opt1, '.')) != NULL &&
  220             !strncmp(opt1, opt2, ++p - opt1)) {
  221                 opt2 += p - opt1;
  222                 opt1 = p;
  223                 /* "foo.noopt" vs. "foo.opt" */
  224                 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
  225                         return (1);
  226                 /* "foo.opt" vs. "foo.noopt" */
  227                 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
  228                         return (1);
  229         }
  230         /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
  231         if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
  232             (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
  233                 return (1);
  234         return (0);
  235 }
  236 
  237 /*
  238  * If a mount option is specified several times,
  239  * (with or without the "no" prefix) only keep
  240  * the last occurrence of it.
  241  */
  242 static void
  243 vfs_sanitizeopts(struct vfsoptlist *opts)
  244 {
  245         struct vfsopt *opt, *opt2, *tmp;
  246 
  247         TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
  248                 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
  249                 while (opt2 != NULL) {
  250                         if (vfs_equalopts(opt->name, opt2->name)) {
  251                                 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
  252                                 vfs_freeopt(opts, opt2);
  253                                 opt2 = tmp;
  254                         } else {
  255                                 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
  256                         }
  257                 }
  258         }
  259 }
  260 
  261 /*
  262  * Build a linked list of mount options from a struct uio.
  263  */
  264 int
  265 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
  266 {
  267         struct vfsoptlist *opts;
  268         struct vfsopt *opt;
  269         size_t memused, namelen, optlen;
  270         unsigned int i, iovcnt;
  271         int error;
  272 
  273         opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
  274         TAILQ_INIT(opts);
  275         memused = 0;
  276         iovcnt = auio->uio_iovcnt;
  277         for (i = 0; i < iovcnt; i += 2) {
  278                 namelen = auio->uio_iov[i].iov_len;
  279                 optlen = auio->uio_iov[i + 1].iov_len;
  280                 memused += sizeof(struct vfsopt) + optlen + namelen;
  281                 /*
  282                  * Avoid consuming too much memory, and attempts to overflow
  283                  * memused.
  284                  */
  285                 if (memused > VFS_MOUNTARG_SIZE_MAX ||
  286                     optlen > VFS_MOUNTARG_SIZE_MAX ||
  287                     namelen > VFS_MOUNTARG_SIZE_MAX) {
  288                         error = EINVAL;
  289                         goto bad;
  290                 }
  291 
  292                 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
  293                 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
  294                 opt->value = NULL;
  295                 opt->len = 0;
  296                 opt->pos = i / 2;
  297                 opt->seen = 0;
  298 
  299                 /*
  300                  * Do this early, so jumps to "bad" will free the current
  301                  * option.
  302                  */
  303                 TAILQ_INSERT_TAIL(opts, opt, link);
  304 
  305                 if (auio->uio_segflg == UIO_SYSSPACE) {
  306                         bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
  307                 } else {
  308                         error = copyin(auio->uio_iov[i].iov_base, opt->name,
  309                             namelen);
  310                         if (error)
  311                                 goto bad;
  312                 }
  313                 /* Ensure names are null-terminated strings. */
  314                 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
  315                         error = EINVAL;
  316                         goto bad;
  317                 }
  318                 if (optlen != 0) {
  319                         opt->len = optlen;
  320                         opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
  321                         if (auio->uio_segflg == UIO_SYSSPACE) {
  322                                 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
  323                                     optlen);
  324                         } else {
  325                                 error = copyin(auio->uio_iov[i + 1].iov_base,
  326                                     opt->value, optlen);
  327                                 if (error)
  328                                         goto bad;
  329                         }
  330                 }
  331         }
  332         vfs_sanitizeopts(opts);
  333         *options = opts;
  334         return (0);
  335 bad:
  336         vfs_freeopts(opts);
  337         return (error);
  338 }
  339 
  340 /*
  341  * Merge the old mount options with the new ones passed
  342  * in the MNT_UPDATE case.
  343  *
  344  * XXX: This function will keep a "nofoo" option in the new
  345  * options.  E.g, if the option's canonical name is "foo",
  346  * "nofoo" ends up in the mount point's active options.
  347  */
  348 static void
  349 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
  350 {
  351         struct vfsopt *opt, *new;
  352 
  353         TAILQ_FOREACH(opt, oldopts, link) {
  354                 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
  355                 new->name = strdup(opt->name, M_MOUNT);
  356                 if (opt->len != 0) {
  357                         new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
  358                         bcopy(opt->value, new->value, opt->len);
  359                 } else
  360                         new->value = NULL;
  361                 new->len = opt->len;
  362                 new->seen = opt->seen;
  363                 TAILQ_INSERT_HEAD(toopts, new, link);
  364         }
  365         vfs_sanitizeopts(toopts);
  366 }
  367 
  368 /*
  369  * Mount a filesystem.
  370  */
  371 #ifndef _SYS_SYSPROTO_H_
  372 struct nmount_args {
  373         struct iovec *iovp;
  374         unsigned int iovcnt;
  375         int flags;
  376 };
  377 #endif
  378 int
  379 sys_nmount(struct thread *td, struct nmount_args *uap)
  380 {
  381         struct uio *auio;
  382         int error;
  383         u_int iovcnt;
  384         uint64_t flags;
  385 
  386         /*
  387          * Mount flags are now 64-bits. On 32-bit archtectures only
  388          * 32-bits are passed in, but from here on everything handles
  389          * 64-bit flags correctly.
  390          */
  391         flags = uap->flags;
  392 
  393         AUDIT_ARG_FFLAGS(flags);
  394         CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
  395             uap->iovp, uap->iovcnt, flags);
  396 
  397         /*
  398          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
  399          * userspace to set this flag, but we must filter it out if we want
  400          * MNT_UPDATE on the root file system to work.
  401          * MNT_ROOTFS should only be set by the kernel when mounting its
  402          * root file system.
  403          */
  404         flags &= ~MNT_ROOTFS;
  405 
  406         iovcnt = uap->iovcnt;
  407         /*
  408          * Check that we have an even number of iovec's
  409          * and that we have at least two options.
  410          */
  411         if ((iovcnt & 1) || (iovcnt < 4)) {
  412                 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
  413                     uap->iovcnt);
  414                 return (EINVAL);
  415         }
  416 
  417         error = copyinuio(uap->iovp, iovcnt, &auio);
  418         if (error) {
  419                 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
  420                     __func__, error);
  421                 return (error);
  422         }
  423         error = vfs_donmount(td, flags, auio);
  424 
  425         free(auio, M_IOV);
  426         return (error);
  427 }
  428 
  429 /*
  430  * ---------------------------------------------------------------------
  431  * Various utility functions
  432  */
  433 
  434 void
  435 vfs_ref(struct mount *mp)
  436 {
  437 
  438         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
  439         MNT_ILOCK(mp);
  440         MNT_REF(mp);
  441         MNT_IUNLOCK(mp);
  442 }
  443 
  444 void
  445 vfs_rel(struct mount *mp)
  446 {
  447 
  448         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
  449         MNT_ILOCK(mp);
  450         MNT_REL(mp);
  451         MNT_IUNLOCK(mp);
  452 }
  453 
  454 /*
  455  * Allocate and initialize the mount point struct.
  456  */
  457 struct mount *
  458 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
  459     struct ucred *cred)
  460 {
  461         struct mount *mp;
  462 
  463         mp = uma_zalloc(mount_zone, M_WAITOK);
  464         bzero(&mp->mnt_startzero,
  465             __rangeof(struct mount, mnt_startzero, mnt_endzero));
  466         TAILQ_INIT(&mp->mnt_nvnodelist);
  467         mp->mnt_nvnodelistsize = 0;
  468         TAILQ_INIT(&mp->mnt_activevnodelist);
  469         mp->mnt_activevnodelistsize = 0;
  470         mp->mnt_ref = 0;
  471         (void) vfs_busy(mp, MBF_NOWAIT);
  472         atomic_add_acq_int(&vfsp->vfc_refcount, 1);
  473         mp->mnt_op = vfsp->vfc_vfsops;
  474         mp->mnt_vfc = vfsp;
  475         mp->mnt_stat.f_type = vfsp->vfc_typenum;
  476         mp->mnt_gen++;
  477         strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
  478         mp->mnt_vnodecovered = vp;
  479         mp->mnt_cred = crdup(cred);
  480         mp->mnt_stat.f_owner = cred->cr_uid;
  481         strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
  482         mp->mnt_iosize_max = DFLTPHYS;
  483 #ifdef MAC
  484         mac_mount_init(mp);
  485         mac_mount_create(cred, mp);
  486 #endif
  487         arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
  488         TAILQ_INIT(&mp->mnt_uppers);
  489         return (mp);
  490 }
  491 
  492 /*
  493  * Destroy the mount struct previously allocated by vfs_mount_alloc().
  494  */
  495 void
  496 vfs_mount_destroy(struct mount *mp)
  497 {
  498 
  499         MNT_ILOCK(mp);
  500         mp->mnt_kern_flag |= MNTK_REFEXPIRE;
  501         if (mp->mnt_kern_flag & MNTK_MWAIT) {
  502                 mp->mnt_kern_flag &= ~MNTK_MWAIT;
  503                 wakeup(mp);
  504         }
  505         while (mp->mnt_ref)
  506                 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
  507         KASSERT(mp->mnt_ref == 0,
  508             ("%s: invalid refcount in the drain path @ %s:%d", __func__,
  509             __FILE__, __LINE__));
  510         if (mp->mnt_writeopcount != 0)
  511                 panic("vfs_mount_destroy: nonzero writeopcount");
  512         if (mp->mnt_secondary_writes != 0)
  513                 panic("vfs_mount_destroy: nonzero secondary_writes");
  514         atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
  515         if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
  516                 struct vnode *vp;
  517 
  518                 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
  519                         vn_printf(vp, "dangling vnode ");
  520                 panic("unmount: dangling vnode");
  521         }
  522         KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
  523         if (mp->mnt_nvnodelistsize != 0)
  524                 panic("vfs_mount_destroy: nonzero nvnodelistsize");
  525         if (mp->mnt_activevnodelistsize != 0)
  526                 panic("vfs_mount_destroy: nonzero activevnodelistsize");
  527         if (mp->mnt_lockref != 0)
  528                 panic("vfs_mount_destroy: nonzero lock refcount");
  529         MNT_IUNLOCK(mp);
  530         if (mp->mnt_vnodecovered != NULL)
  531                 vrele(mp->mnt_vnodecovered);
  532 #ifdef MAC
  533         mac_mount_destroy(mp);
  534 #endif
  535         if (mp->mnt_opt != NULL)
  536                 vfs_freeopts(mp->mnt_opt);
  537         crfree(mp->mnt_cred);
  538         uma_zfree(mount_zone, mp);
  539 }
  540 
  541 static bool
  542 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error)
  543 {
  544         /* This is an upgrade of an exisiting mount. */
  545         if ((fsflags & MNT_UPDATE) != 0)
  546                 return (false);
  547         /* This is already an R/O mount. */
  548         if ((fsflags & MNT_RDONLY) != 0)
  549                 return (false);
  550 
  551         switch (error) {
  552         case ENODEV:    /* generic, geom, ... */
  553         case EACCES:    /* cam/scsi, ... */
  554         case EROFS:     /* md, mmcsd, ... */
  555                 /*
  556                  * These errors can be returned by the storage layer to signal
  557                  * that the media is read-only.  No harm in the R/O mount
  558                  * attempt if the error was returned for some other reason.
  559                  */
  560                 return (true);
  561         default:
  562                 return (false);
  563         }
  564 }
  565 
  566 int
  567 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
  568 {
  569         struct vfsoptlist *optlist;
  570         struct vfsopt *opt, *tmp_opt;
  571         char *fstype, *fspath, *errmsg;
  572         int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
  573         bool autoro;
  574 
  575         errmsg = fspath = NULL;
  576         errmsg_len = fspathlen = 0;
  577         errmsg_pos = -1;
  578         autoro = default_autoro;
  579 
  580         error = vfs_buildopts(fsoptions, &optlist);
  581         if (error)
  582                 return (error);
  583 
  584         if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
  585                 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
  586 
  587         /*
  588          * We need these two options before the others,
  589          * and they are mandatory for any filesystem.
  590          * Ensure they are NUL terminated as well.
  591          */
  592         fstypelen = 0;
  593         error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
  594         if (error || fstype[fstypelen - 1] != '\0') {
  595                 error = EINVAL;
  596                 if (errmsg != NULL)
  597                         strncpy(errmsg, "Invalid fstype", errmsg_len);
  598                 goto bail;
  599         }
  600         fspathlen = 0;
  601         error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
  602         if (error || fspath[fspathlen - 1] != '\0') {
  603                 error = EINVAL;
  604                 if (errmsg != NULL)
  605                         strncpy(errmsg, "Invalid fspath", errmsg_len);
  606                 goto bail;
  607         }
  608 
  609         /*
  610          * We need to see if we have the "update" option
  611          * before we call vfs_domount(), since vfs_domount() has special
  612          * logic based on MNT_UPDATE.  This is very important
  613          * when we want to update the root filesystem.
  614          */
  615         TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
  616                 if (strcmp(opt->name, "update") == 0) {
  617                         fsflags |= MNT_UPDATE;
  618                         vfs_freeopt(optlist, opt);
  619                 }
  620                 else if (strcmp(opt->name, "async") == 0)
  621                         fsflags |= MNT_ASYNC;
  622                 else if (strcmp(opt->name, "force") == 0) {
  623                         fsflags |= MNT_FORCE;
  624                         vfs_freeopt(optlist, opt);
  625                 }
  626                 else if (strcmp(opt->name, "reload") == 0) {
  627                         fsflags |= MNT_RELOAD;
  628                         vfs_freeopt(optlist, opt);
  629                 }
  630                 else if (strcmp(opt->name, "multilabel") == 0)
  631                         fsflags |= MNT_MULTILABEL;
  632                 else if (strcmp(opt->name, "noasync") == 0)
  633                         fsflags &= ~MNT_ASYNC;
  634                 else if (strcmp(opt->name, "noatime") == 0)
  635                         fsflags |= MNT_NOATIME;
  636                 else if (strcmp(opt->name, "atime") == 0) {
  637                         free(opt->name, M_MOUNT);
  638                         opt->name = strdup("nonoatime", M_MOUNT);
  639                 }
  640                 else if (strcmp(opt->name, "noclusterr") == 0)
  641                         fsflags |= MNT_NOCLUSTERR;
  642                 else if (strcmp(opt->name, "clusterr") == 0) {
  643                         free(opt->name, M_MOUNT);
  644                         opt->name = strdup("nonoclusterr", M_MOUNT);
  645                 }
  646                 else if (strcmp(opt->name, "noclusterw") == 0)
  647                         fsflags |= MNT_NOCLUSTERW;
  648                 else if (strcmp(opt->name, "clusterw") == 0) {
  649                         free(opt->name, M_MOUNT);
  650                         opt->name = strdup("nonoclusterw", M_MOUNT);
  651                 }
  652                 else if (strcmp(opt->name, "noexec") == 0)
  653                         fsflags |= MNT_NOEXEC;
  654                 else if (strcmp(opt->name, "exec") == 0) {
  655                         free(opt->name, M_MOUNT);
  656                         opt->name = strdup("nonoexec", M_MOUNT);
  657                 }
  658                 else if (strcmp(opt->name, "nosuid") == 0)
  659                         fsflags |= MNT_NOSUID;
  660                 else if (strcmp(opt->name, "suid") == 0) {
  661                         free(opt->name, M_MOUNT);
  662                         opt->name = strdup("nonosuid", M_MOUNT);
  663                 }
  664                 else if (strcmp(opt->name, "nosymfollow") == 0)
  665                         fsflags |= MNT_NOSYMFOLLOW;
  666                 else if (strcmp(opt->name, "symfollow") == 0) {
  667                         free(opt->name, M_MOUNT);
  668                         opt->name = strdup("nonosymfollow", M_MOUNT);
  669                 }
  670                 else if (strcmp(opt->name, "noro") == 0) {
  671                         fsflags &= ~MNT_RDONLY;
  672                         autoro = false;
  673                 }
  674                 else if (strcmp(opt->name, "rw") == 0) {
  675                         fsflags &= ~MNT_RDONLY;
  676                         autoro = false;
  677                 }
  678                 else if (strcmp(opt->name, "ro") == 0) {
  679                         fsflags |= MNT_RDONLY;
  680                         autoro = false;
  681                 }
  682                 else if (strcmp(opt->name, "rdonly") == 0) {
  683                         free(opt->name, M_MOUNT);
  684                         opt->name = strdup("ro", M_MOUNT);
  685                         fsflags |= MNT_RDONLY;
  686                         autoro = false;
  687                 }
  688                 else if (strcmp(opt->name, "autoro") == 0) {
  689                         vfs_freeopt(optlist, opt);
  690                         autoro = true;
  691                 }
  692                 else if (strcmp(opt->name, "suiddir") == 0)
  693                         fsflags |= MNT_SUIDDIR;
  694                 else if (strcmp(opt->name, "sync") == 0)
  695                         fsflags |= MNT_SYNCHRONOUS;
  696                 else if (strcmp(opt->name, "union") == 0)
  697                         fsflags |= MNT_UNION;
  698                 else if (strcmp(opt->name, "automounted") == 0) {
  699                         fsflags |= MNT_AUTOMOUNTED;
  700                         vfs_freeopt(optlist, opt);
  701                 }
  702         }
  703 
  704         /*
  705          * Be ultra-paranoid about making sure the type and fspath
  706          * variables will fit in our mp buffers, including the
  707          * terminating NUL.
  708          */
  709         if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
  710                 error = ENAMETOOLONG;
  711                 goto bail;
  712         }
  713 
  714         error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
  715 
  716         /*
  717          * See if we can mount in the read-only mode if the error code suggests
  718          * that it could be possible and the mount options allow for that.
  719          * Never try it if "[no]{ro|rw}" has been explicitly requested and not
  720          * overridden by "autoro".
  721          */
  722         if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) {
  723                 printf("%s: R/W mount failed, possibly R/O media,"
  724                     " trying R/O mount\n", __func__);
  725                 fsflags |= MNT_RDONLY;
  726                 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
  727         }
  728 bail:
  729         /* copyout the errmsg */
  730         if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
  731             && errmsg_len > 0 && errmsg != NULL) {
  732                 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
  733                         bcopy(errmsg,
  734                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
  735                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
  736                 } else {
  737                         copyout(errmsg,
  738                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
  739                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
  740                 }
  741         }
  742 
  743         if (optlist != NULL)
  744                 vfs_freeopts(optlist);
  745         return (error);
  746 }
  747 
  748 /*
  749  * Old mount API.
  750  */
  751 #ifndef _SYS_SYSPROTO_H_
  752 struct mount_args {
  753         char    *type;
  754         char    *path;
  755         int     flags;
  756         caddr_t data;
  757 };
  758 #endif
  759 /* ARGSUSED */
  760 int
  761 sys_mount(struct thread *td, struct mount_args *uap)
  762 {
  763         char *fstype;
  764         struct vfsconf *vfsp = NULL;
  765         struct mntarg *ma = NULL;
  766         uint64_t flags;
  767         int error;
  768 
  769         /*
  770          * Mount flags are now 64-bits. On 32-bit architectures only
  771          * 32-bits are passed in, but from here on everything handles
  772          * 64-bit flags correctly.
  773          */
  774         flags = uap->flags;
  775 
  776         AUDIT_ARG_FFLAGS(flags);
  777 
  778         /*
  779          * Filter out MNT_ROOTFS.  We do not want clients of mount() in
  780          * userspace to set this flag, but we must filter it out if we want
  781          * MNT_UPDATE on the root file system to work.
  782          * MNT_ROOTFS should only be set by the kernel when mounting its
  783          * root file system.
  784          */
  785         flags &= ~MNT_ROOTFS;
  786 
  787         fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
  788         error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
  789         if (error) {
  790                 free(fstype, M_TEMP);
  791                 return (error);
  792         }
  793 
  794         AUDIT_ARG_TEXT(fstype);
  795         vfsp = vfs_byname_kld(fstype, td, &error);
  796         free(fstype, M_TEMP);
  797         if (vfsp == NULL)
  798                 return (ENOENT);
  799         if (vfsp->vfc_vfsops->vfs_cmount == NULL)
  800                 return (EOPNOTSUPP);
  801 
  802         ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
  803         ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
  804         ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
  805         ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
  806         ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
  807 
  808         error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags);
  809         return (error);
  810 }
  811 
  812 /*
  813  * vfs_domount_first(): first file system mount (not update)
  814  */
  815 static int
  816 vfs_domount_first(
  817         struct thread *td,              /* Calling thread. */
  818         struct vfsconf *vfsp,           /* File system type. */
  819         char *fspath,                   /* Mount path. */
  820         struct vnode *vp,               /* Vnode to be covered. */
  821         uint64_t fsflags,               /* Flags common to all filesystems. */
  822         struct vfsoptlist **optlist     /* Options local to the filesystem. */
  823         )
  824 {
  825         struct vattr va;
  826         struct mount *mp;
  827         struct vnode *newdp;
  828         int error;
  829 
  830         ASSERT_VOP_ELOCKED(vp, __func__);
  831         KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
  832 
  833         /*
  834          * If the user is not root, ensure that they own the directory
  835          * onto which we are attempting to mount.
  836          */
  837         error = VOP_GETATTR(vp, &va, td->td_ucred);
  838         if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
  839                 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN, 0);
  840         if (error == 0)
  841                 error = vinvalbuf(vp, V_SAVE, 0, 0);
  842         if (error == 0 && vp->v_type != VDIR)
  843                 error = ENOTDIR;
  844         if (error == 0) {
  845                 VI_LOCK(vp);
  846                 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
  847                         vp->v_iflag |= VI_MOUNT;
  848                 else
  849                         error = EBUSY;
  850                 VI_UNLOCK(vp);
  851         }
  852         if (error != 0) {
  853                 vput(vp);
  854                 return (error);
  855         }
  856         VOP_UNLOCK(vp, 0);
  857 
  858         /* Allocate and initialize the filesystem. */
  859         mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
  860         /* XXXMAC: pass to vfs_mount_alloc? */
  861         mp->mnt_optnew = *optlist;
  862         /* Set the mount level flags. */
  863         mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY));
  864 
  865         /*
  866          * Mount the filesystem.
  867          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
  868          * get.  No freeing of cn_pnbuf.
  869          */
  870         error = VFS_MOUNT(mp);
  871         if (error != 0) {
  872                 vfs_unbusy(mp);
  873                 mp->mnt_vnodecovered = NULL;
  874                 vfs_mount_destroy(mp);
  875                 VI_LOCK(vp);
  876                 vp->v_iflag &= ~VI_MOUNT;
  877                 VI_UNLOCK(vp);
  878                 vrele(vp);
  879                 return (error);
  880         }
  881 
  882         if (mp->mnt_opt != NULL)
  883                 vfs_freeopts(mp->mnt_opt);
  884         mp->mnt_opt = mp->mnt_optnew;
  885         *optlist = NULL;
  886         (void)VFS_STATFS(mp, &mp->mnt_stat);
  887 
  888         /*
  889          * Prevent external consumers of mount options from reading mnt_optnew.
  890          */
  891         mp->mnt_optnew = NULL;
  892 
  893         MNT_ILOCK(mp);
  894         if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
  895             (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
  896                 mp->mnt_kern_flag |= MNTK_ASYNC;
  897         else
  898                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
  899         MNT_IUNLOCK(mp);
  900 
  901         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  902         cache_purge(vp);
  903         VI_LOCK(vp);
  904         vp->v_iflag &= ~VI_MOUNT;
  905         VI_UNLOCK(vp);
  906         vp->v_mountedhere = mp;
  907         /* Place the new filesystem at the end of the mount list. */
  908         mtx_lock(&mountlist_mtx);
  909         TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
  910         mtx_unlock(&mountlist_mtx);
  911         vfs_event_signal(NULL, VQ_MOUNT, 0);
  912         if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
  913                 panic("mount: lost mount");
  914         VOP_UNLOCK(vp, 0);
  915         EVENTHANDLER_INVOKE(vfs_mounted, mp, newdp, td);
  916         VOP_UNLOCK(newdp, 0);
  917         mountcheckdirs(vp, newdp);
  918         vrele(newdp);
  919         if ((mp->mnt_flag & MNT_RDONLY) == 0)
  920                 vfs_allocate_syncvnode(mp);
  921         vfs_unbusy(mp);
  922         return (0);
  923 }
  924 
  925 /*
  926  * vfs_domount_update(): update of mounted file system
  927  */
  928 static int
  929 vfs_domount_update(
  930         struct thread *td,              /* Calling thread. */
  931         struct vnode *vp,               /* Mount point vnode. */
  932         uint64_t fsflags,               /* Flags common to all filesystems. */
  933         struct vfsoptlist **optlist     /* Options local to the filesystem. */
  934         )
  935 {
  936         struct export_args export;
  937         void *bufp;
  938         struct mount *mp;
  939         int error, export_error, len;
  940         uint64_t flag;
  941 
  942         ASSERT_VOP_ELOCKED(vp, __func__);
  943         KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
  944         mp = vp->v_mount;
  945 
  946         if ((vp->v_vflag & VV_ROOT) == 0) {
  947                 if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
  948                     == 0)
  949                         error = EXDEV;
  950                 else
  951                         error = EINVAL;
  952                 vput(vp);
  953                 return (error);
  954         }
  955 
  956         /*
  957          * We only allow the filesystem to be reloaded if it
  958          * is currently mounted read-only.
  959          */
  960         flag = mp->mnt_flag;
  961         if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
  962                 vput(vp);
  963                 return (EOPNOTSUPP);    /* Needs translation */
  964         }
  965         /*
  966          * Only privileged root, or (if MNT_USER is set) the user that
  967          * did the original mount is permitted to update it.
  968          */
  969         error = vfs_suser(mp, td);
  970         if (error != 0) {
  971                 vput(vp);
  972                 return (error);
  973         }
  974         if (vfs_busy(mp, MBF_NOWAIT)) {
  975                 vput(vp);
  976                 return (EBUSY);
  977         }
  978         VI_LOCK(vp);
  979         if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
  980                 VI_UNLOCK(vp);
  981                 vfs_unbusy(mp);
  982                 vput(vp);
  983                 return (EBUSY);
  984         }
  985         vp->v_iflag |= VI_MOUNT;
  986         VI_UNLOCK(vp);
  987         VOP_UNLOCK(vp, 0);
  988 
  989         MNT_ILOCK(mp);
  990         if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
  991                 MNT_IUNLOCK(mp);
  992                 error = EBUSY;
  993                 goto end;
  994         }
  995         mp->mnt_flag &= ~MNT_UPDATEMASK;
  996         mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
  997             MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
  998         if ((mp->mnt_flag & MNT_ASYNC) == 0)
  999                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
 1000         MNT_IUNLOCK(mp);
 1001         mp->mnt_optnew = *optlist;
 1002         vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
 1003 
 1004         /*
 1005          * Mount the filesystem.
 1006          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
 1007          * get.  No freeing of cn_pnbuf.
 1008          */
 1009         error = VFS_MOUNT(mp);
 1010 
 1011         export_error = 0;
 1012         /* Process the export option. */
 1013         if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
 1014             &len) == 0) {
 1015                 /* Assume that there is only 1 ABI for each length. */
 1016                 switch (len) {
 1017                 case (sizeof(struct oexport_args)):
 1018                         bzero(&export, sizeof(export));
 1019                         /* FALLTHROUGH */
 1020                 case (sizeof(export)):
 1021                         bcopy(bufp, &export, len);
 1022                         export_error = vfs_export(mp, &export);
 1023                         break;
 1024                 default:
 1025                         export_error = EINVAL;
 1026                         break;
 1027                 }
 1028         }
 1029 
 1030         MNT_ILOCK(mp);
 1031         if (error == 0) {
 1032                 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
 1033                     MNT_SNAPSHOT);
 1034         } else {
 1035                 /*
 1036                  * If we fail, restore old mount flags. MNT_QUOTA is special,
 1037                  * because it is not part of MNT_UPDATEMASK, but it could have
 1038                  * changed in the meantime if quotactl(2) was called.
 1039                  * All in all we want current value of MNT_QUOTA, not the old
 1040                  * one.
 1041                  */
 1042                 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
 1043         }
 1044         if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
 1045             (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
 1046                 mp->mnt_kern_flag |= MNTK_ASYNC;
 1047         else
 1048                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
 1049         MNT_IUNLOCK(mp);
 1050 
 1051         if (error != 0)
 1052                 goto end;
 1053 
 1054         if (mp->mnt_opt != NULL)
 1055                 vfs_freeopts(mp->mnt_opt);
 1056         mp->mnt_opt = mp->mnt_optnew;
 1057         *optlist = NULL;
 1058         (void)VFS_STATFS(mp, &mp->mnt_stat);
 1059         /*
 1060          * Prevent external consumers of mount options from reading
 1061          * mnt_optnew.
 1062          */
 1063         mp->mnt_optnew = NULL;
 1064 
 1065         if ((mp->mnt_flag & MNT_RDONLY) == 0)
 1066                 vfs_allocate_syncvnode(mp);
 1067         else
 1068                 vfs_deallocate_syncvnode(mp);
 1069 end:
 1070         vfs_unbusy(mp);
 1071         VI_LOCK(vp);
 1072         vp->v_iflag &= ~VI_MOUNT;
 1073         VI_UNLOCK(vp);
 1074         vrele(vp);
 1075         return (error != 0 ? error : export_error);
 1076 }
 1077 
 1078 /*
 1079  * vfs_domount(): actually attempt a filesystem mount.
 1080  */
 1081 static int
 1082 vfs_domount(
 1083         struct thread *td,              /* Calling thread. */
 1084         const char *fstype,             /* Filesystem type. */
 1085         char *fspath,                   /* Mount path. */
 1086         uint64_t fsflags,               /* Flags common to all filesystems. */
 1087         struct vfsoptlist **optlist     /* Options local to the filesystem. */
 1088         )
 1089 {
 1090         struct vfsconf *vfsp;
 1091         struct nameidata nd;
 1092         struct vnode *vp;
 1093         char *pathbuf;
 1094         int error;
 1095 
 1096         /*
 1097          * Be ultra-paranoid about making sure the type and fspath
 1098          * variables will fit in our mp buffers, including the
 1099          * terminating NUL.
 1100          */
 1101         if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
 1102                 return (ENAMETOOLONG);
 1103 
 1104         if (jailed(td->td_ucred) || usermount == 0) {
 1105                 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
 1106                         return (error);
 1107         }
 1108 
 1109         /*
 1110          * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
 1111          */
 1112         if (fsflags & MNT_EXPORTED) {
 1113                 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
 1114                 if (error)
 1115                         return (error);
 1116         }
 1117         if (fsflags & MNT_SUIDDIR) {
 1118                 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
 1119                 if (error)
 1120                         return (error);
 1121         }
 1122         /*
 1123          * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
 1124          */
 1125         if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
 1126                 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
 1127                         fsflags |= MNT_NOSUID | MNT_USER;
 1128         }
 1129 
 1130         /* Load KLDs before we lock the covered vnode to avoid reversals. */
 1131         vfsp = NULL;
 1132         if ((fsflags & MNT_UPDATE) == 0) {
 1133                 /* Don't try to load KLDs if we're mounting the root. */
 1134                 if (fsflags & MNT_ROOTFS)
 1135                         vfsp = vfs_byname(fstype);
 1136                 else
 1137                         vfsp = vfs_byname_kld(fstype, td, &error);
 1138                 if (vfsp == NULL)
 1139                         return (ENODEV);
 1140                 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
 1141                         return (EPERM);
 1142         }
 1143 
 1144         /*
 1145          * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
 1146          */
 1147         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
 1148             UIO_SYSSPACE, fspath, td);
 1149         error = namei(&nd);
 1150         if (error != 0)
 1151                 return (error);
 1152         NDFREE(&nd, NDF_ONLY_PNBUF);
 1153         vp = nd.ni_vp;
 1154         if ((fsflags & MNT_UPDATE) == 0) {
 1155                 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
 1156                 strcpy(pathbuf, fspath);
 1157                 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
 1158                 /* debug.disablefullpath == 1 results in ENODEV */
 1159                 if (error == 0 || error == ENODEV) {
 1160                         error = vfs_domount_first(td, vfsp, pathbuf, vp,
 1161                             fsflags, optlist);
 1162                 }
 1163                 free(pathbuf, M_TEMP);
 1164         } else
 1165                 error = vfs_domount_update(td, vp, fsflags, optlist);
 1166 
 1167         return (error);
 1168 }
 1169 
 1170 /*
 1171  * Unmount a filesystem.
 1172  *
 1173  * Note: unmount takes a path to the vnode mounted on as argument, not
 1174  * special file (as before).
 1175  */
 1176 #ifndef _SYS_SYSPROTO_H_
 1177 struct unmount_args {
 1178         char    *path;
 1179         int     flags;
 1180 };
 1181 #endif
 1182 /* ARGSUSED */
 1183 int
 1184 sys_unmount(struct thread *td, struct unmount_args *uap)
 1185 {
 1186         struct nameidata nd;
 1187         struct mount *mp;
 1188         char *pathbuf;
 1189         int error, id0, id1;
 1190 
 1191         AUDIT_ARG_VALUE(uap->flags);
 1192         if (jailed(td->td_ucred) || usermount == 0) {
 1193                 error = priv_check(td, PRIV_VFS_UNMOUNT);
 1194                 if (error)
 1195                         return (error);
 1196         }
 1197 
 1198         pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
 1199         error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
 1200         if (error) {
 1201                 free(pathbuf, M_TEMP);
 1202                 return (error);
 1203         }
 1204         if (uap->flags & MNT_BYFSID) {
 1205                 AUDIT_ARG_TEXT(pathbuf);
 1206                 /* Decode the filesystem ID. */
 1207                 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
 1208                         free(pathbuf, M_TEMP);
 1209                         return (EINVAL);
 1210                 }
 1211 
 1212                 mtx_lock(&mountlist_mtx);
 1213                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
 1214                         if (mp->mnt_stat.f_fsid.val[0] == id0 &&
 1215                             mp->mnt_stat.f_fsid.val[1] == id1) {
 1216                                 vfs_ref(mp);
 1217                                 break;
 1218                         }
 1219                 }
 1220                 mtx_unlock(&mountlist_mtx);
 1221         } else {
 1222                 /*
 1223                  * Try to find global path for path argument.
 1224                  */
 1225                 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
 1226                     UIO_SYSSPACE, pathbuf, td);
 1227                 if (namei(&nd) == 0) {
 1228                         NDFREE(&nd, NDF_ONLY_PNBUF);
 1229                         error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
 1230                             MNAMELEN);
 1231                         if (error == 0 || error == ENODEV)
 1232                                 vput(nd.ni_vp);
 1233                 }
 1234                 mtx_lock(&mountlist_mtx);
 1235                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
 1236                         if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
 1237                                 vfs_ref(mp);
 1238                                 break;
 1239                         }
 1240                 }
 1241                 mtx_unlock(&mountlist_mtx);
 1242         }
 1243         free(pathbuf, M_TEMP);
 1244         if (mp == NULL) {
 1245                 /*
 1246                  * Previously we returned ENOENT for a nonexistent path and
 1247                  * EINVAL for a non-mountpoint.  We cannot tell these apart
 1248                  * now, so in the !MNT_BYFSID case return the more likely
 1249                  * EINVAL for compatibility.
 1250                  */
 1251                 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
 1252         }
 1253 
 1254         /*
 1255          * Don't allow unmounting the root filesystem.
 1256          */
 1257         if (mp->mnt_flag & MNT_ROOTFS) {
 1258                 vfs_rel(mp);
 1259                 return (EINVAL);
 1260         }
 1261         error = dounmount(mp, uap->flags, td);
 1262         return (error);
 1263 }
 1264 
 1265 /*
 1266  * Return error if any of the vnodes, ignoring the root vnode
 1267  * and the syncer vnode, have non-zero usecount.
 1268  *
 1269  * This function is purely advisory - it can return false positives
 1270  * and negatives.
 1271  */
 1272 static int
 1273 vfs_check_usecounts(struct mount *mp)
 1274 {
 1275         struct vnode *vp, *mvp;
 1276 
 1277         MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 1278                 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
 1279                     vp->v_usecount != 0) {
 1280                         VI_UNLOCK(vp);
 1281                         MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 1282                         return (EBUSY);
 1283                 }
 1284                 VI_UNLOCK(vp);
 1285         }
 1286 
 1287         return (0);
 1288 }
 1289 
 1290 static void
 1291 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
 1292 {
 1293 
 1294         mtx_assert(MNT_MTX(mp), MA_OWNED);
 1295         mp->mnt_kern_flag &= ~mntkflags;
 1296         if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
 1297                 mp->mnt_kern_flag &= ~MNTK_MWAIT;
 1298                 wakeup(mp);
 1299         }
 1300         MNT_IUNLOCK(mp);
 1301         if (coveredvp != NULL) {
 1302                 VOP_UNLOCK(coveredvp, 0);
 1303                 vdrop(coveredvp);
 1304         }
 1305         vn_finished_write(mp);
 1306 }
 1307 
 1308 /*
 1309  * Do the actual filesystem unmount.
 1310  */
 1311 int
 1312 dounmount(struct mount *mp, int flags, struct thread *td)
 1313 {
 1314         struct vnode *coveredvp, *fsrootvp;
 1315         int error;
 1316         uint64_t async_flag;
 1317         int mnt_gen_r;
 1318 
 1319         if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
 1320                 mnt_gen_r = mp->mnt_gen;
 1321                 VI_LOCK(coveredvp);
 1322                 vholdl(coveredvp);
 1323                 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
 1324                 /*
 1325                  * Check for mp being unmounted while waiting for the
 1326                  * covered vnode lock.
 1327                  */
 1328                 if (coveredvp->v_mountedhere != mp ||
 1329                     coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
 1330                         VOP_UNLOCK(coveredvp, 0);
 1331                         vdrop(coveredvp);
 1332                         vfs_rel(mp);
 1333                         return (EBUSY);
 1334                 }
 1335         }
 1336 
 1337         /*
 1338          * Only privileged root, or (if MNT_USER is set) the user that did the
 1339          * original mount is permitted to unmount this filesystem.
 1340          */
 1341         error = vfs_suser(mp, td);
 1342         if (error != 0) {
 1343                 if (coveredvp != NULL) {
 1344                         VOP_UNLOCK(coveredvp, 0);
 1345                         vdrop(coveredvp);
 1346                 }
 1347                 vfs_rel(mp);
 1348                 return (error);
 1349         }
 1350 
 1351         vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
 1352         MNT_ILOCK(mp);
 1353         if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
 1354             (mp->mnt_flag & MNT_UPDATE) != 0 ||
 1355             !TAILQ_EMPTY(&mp->mnt_uppers)) {
 1356                 dounmount_cleanup(mp, coveredvp, 0);
 1357                 return (EBUSY);
 1358         }
 1359         mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
 1360         if (flags & MNT_NONBUSY) {
 1361                 MNT_IUNLOCK(mp);
 1362                 error = vfs_check_usecounts(mp);
 1363                 MNT_ILOCK(mp);
 1364                 if (error != 0) {
 1365                         dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT |
 1366                             MNTK_NOINSMNTQ);
 1367                         return (error);
 1368                 }
 1369         }
 1370         /* Allow filesystems to detect that a forced unmount is in progress. */
 1371         if (flags & MNT_FORCE) {
 1372                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
 1373                 MNT_IUNLOCK(mp);
 1374                 /*
 1375                  * Must be done after setting MNTK_UNMOUNTF and before
 1376                  * waiting for mnt_lockref to become 0.
 1377                  */
 1378                 VFS_PURGE(mp);
 1379                 MNT_ILOCK(mp);
 1380         }
 1381         error = 0;
 1382         if (mp->mnt_lockref) {
 1383                 mp->mnt_kern_flag |= MNTK_DRAINING;
 1384                 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
 1385                     "mount drain", 0);
 1386         }
 1387         MNT_IUNLOCK(mp);
 1388         KASSERT(mp->mnt_lockref == 0,
 1389             ("%s: invalid lock refcount in the drain path @ %s:%d",
 1390             __func__, __FILE__, __LINE__));
 1391         KASSERT(error == 0,
 1392             ("%s: invalid return value for msleep in the drain path @ %s:%d",
 1393             __func__, __FILE__, __LINE__));
 1394 
 1395         if (mp->mnt_flag & MNT_EXPUBLIC)
 1396                 vfs_setpublicfs(NULL, NULL, NULL);
 1397 
 1398         /*
 1399          * From now, we can claim that the use reference on the
 1400          * coveredvp is ours, and the ref can be released only by
 1401          * successfull unmount by us, or left for later unmount
 1402          * attempt.  The previously acquired hold reference is no
 1403          * longer needed to protect the vnode from reuse.
 1404          */
 1405         if (coveredvp != NULL)
 1406                 vdrop(coveredvp);
 1407 
 1408         vfs_msync(mp, MNT_WAIT);
 1409         MNT_ILOCK(mp);
 1410         async_flag = mp->mnt_flag & MNT_ASYNC;
 1411         mp->mnt_flag &= ~MNT_ASYNC;
 1412         mp->mnt_kern_flag &= ~MNTK_ASYNC;
 1413         MNT_IUNLOCK(mp);
 1414         cache_purgevfs(mp, false); /* remove cache entries for this file sys */
 1415         vfs_deallocate_syncvnode(mp);
 1416         /*
 1417          * For forced unmounts, move process cdir/rdir refs on the fs root
 1418          * vnode to the covered vnode.  For non-forced unmounts we want
 1419          * such references to cause an EBUSY error.
 1420          */
 1421         if ((flags & MNT_FORCE) &&
 1422             VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
 1423                 if (mp->mnt_vnodecovered != NULL &&
 1424                     (mp->mnt_flag & MNT_IGNORE) == 0)
 1425                         mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
 1426                 if (fsrootvp == rootvnode) {
 1427                         vrele(rootvnode);
 1428                         rootvnode = NULL;
 1429                 }
 1430                 vput(fsrootvp);
 1431         }
 1432         if ((mp->mnt_flag & MNT_RDONLY) != 0 || (flags & MNT_FORCE) != 0 ||
 1433             (error = VFS_SYNC(mp, MNT_WAIT)) == 0)
 1434                 error = VFS_UNMOUNT(mp, flags);
 1435         vn_finished_write(mp);
 1436         /*
 1437          * If we failed to flush the dirty blocks for this mount point,
 1438          * undo all the cdir/rdir and rootvnode changes we made above.
 1439          * Unless we failed to do so because the device is reporting that
 1440          * it doesn't exist anymore.
 1441          */
 1442         if (error && error != ENXIO) {
 1443                 if ((flags & MNT_FORCE) &&
 1444                     VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
 1445                         if (mp->mnt_vnodecovered != NULL &&
 1446                             (mp->mnt_flag & MNT_IGNORE) == 0)
 1447                                 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
 1448                         if (rootvnode == NULL) {
 1449                                 rootvnode = fsrootvp;
 1450                                 vref(rootvnode);
 1451                         }
 1452                         vput(fsrootvp);
 1453                 }
 1454                 MNT_ILOCK(mp);
 1455                 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
 1456                 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
 1457                         MNT_IUNLOCK(mp);
 1458                         vfs_allocate_syncvnode(mp);
 1459                         MNT_ILOCK(mp);
 1460                 }
 1461                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
 1462                 mp->mnt_flag |= async_flag;
 1463                 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
 1464                     (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
 1465                         mp->mnt_kern_flag |= MNTK_ASYNC;
 1466                 if (mp->mnt_kern_flag & MNTK_MWAIT) {
 1467                         mp->mnt_kern_flag &= ~MNTK_MWAIT;
 1468                         wakeup(mp);
 1469                 }
 1470                 MNT_IUNLOCK(mp);
 1471                 if (coveredvp)
 1472                         VOP_UNLOCK(coveredvp, 0);
 1473                 return (error);
 1474         }
 1475         mtx_lock(&mountlist_mtx);
 1476         TAILQ_REMOVE(&mountlist, mp, mnt_list);
 1477         mtx_unlock(&mountlist_mtx);
 1478         EVENTHANDLER_INVOKE(vfs_unmounted, mp, td);
 1479         if (coveredvp != NULL) {
 1480                 coveredvp->v_mountedhere = NULL;
 1481                 VOP_UNLOCK(coveredvp, 0);
 1482         }
 1483         vfs_event_signal(NULL, VQ_UNMOUNT, 0);
 1484         if (mp == rootdevmp)
 1485                 rootdevmp = NULL;
 1486         vfs_mount_destroy(mp);
 1487         return (0);
 1488 }
 1489 
 1490 /*
 1491  * Report errors during filesystem mounting.
 1492  */
 1493 void
 1494 vfs_mount_error(struct mount *mp, const char *fmt, ...)
 1495 {
 1496         struct vfsoptlist *moptlist = mp->mnt_optnew;
 1497         va_list ap;
 1498         int error, len;
 1499         char *errmsg;
 1500 
 1501         error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
 1502         if (error || errmsg == NULL || len <= 0)
 1503                 return;
 1504 
 1505         va_start(ap, fmt);
 1506         vsnprintf(errmsg, (size_t)len, fmt, ap);
 1507         va_end(ap);
 1508 }
 1509 
 1510 void
 1511 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
 1512 {
 1513         va_list ap;
 1514         int error, len;
 1515         char *errmsg;
 1516 
 1517         error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
 1518         if (error || errmsg == NULL || len <= 0)
 1519                 return;
 1520 
 1521         va_start(ap, fmt);
 1522         vsnprintf(errmsg, (size_t)len, fmt, ap);
 1523         va_end(ap);
 1524 }
 1525 
 1526 /*
 1527  * ---------------------------------------------------------------------
 1528  * Functions for querying mount options/arguments from filesystems.
 1529  */
 1530 
 1531 /*
 1532  * Check that no unknown options are given
 1533  */
 1534 int
 1535 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
 1536 {
 1537         struct vfsopt *opt;
 1538         char errmsg[255];
 1539         const char **t, *p, *q;
 1540         int ret = 0;
 1541 
 1542         TAILQ_FOREACH(opt, opts, link) {
 1543                 p = opt->name;
 1544                 q = NULL;
 1545                 if (p[0] == 'n' && p[1] == 'o')
 1546                         q = p + 2;
 1547                 for(t = global_opts; *t != NULL; t++) {
 1548                         if (strcmp(*t, p) == 0)
 1549                                 break;
 1550                         if (q != NULL) {
 1551                                 if (strcmp(*t, q) == 0)
 1552                                         break;
 1553                         }
 1554                 }
 1555                 if (*t != NULL)
 1556                         continue;
 1557                 for(t = legal; *t != NULL; t++) {
 1558                         if (strcmp(*t, p) == 0)
 1559                                 break;
 1560                         if (q != NULL) {
 1561                                 if (strcmp(*t, q) == 0)
 1562                                         break;
 1563                         }
 1564                 }
 1565                 if (*t != NULL)
 1566                         continue;
 1567                 snprintf(errmsg, sizeof(errmsg),
 1568                     "mount option <%s> is unknown", p);
 1569                 ret = EINVAL;
 1570         }
 1571         if (ret != 0) {
 1572                 TAILQ_FOREACH(opt, opts, link) {
 1573                         if (strcmp(opt->name, "errmsg") == 0) {
 1574                                 strncpy((char *)opt->value, errmsg, opt->len);
 1575                                 break;
 1576                         }
 1577                 }
 1578                 if (opt == NULL)
 1579                         printf("%s\n", errmsg);
 1580         }
 1581         return (ret);
 1582 }
 1583 
 1584 /*
 1585  * Get a mount option by its name.
 1586  *
 1587  * Return 0 if the option was found, ENOENT otherwise.
 1588  * If len is non-NULL it will be filled with the length
 1589  * of the option. If buf is non-NULL, it will be filled
 1590  * with the address of the option.
 1591  */
 1592 int
 1593 vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len)
 1594 {
 1595         struct vfsopt *opt;
 1596 
 1597         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
 1598 
 1599         TAILQ_FOREACH(opt, opts, link) {
 1600                 if (strcmp(name, opt->name) == 0) {
 1601                         opt->seen = 1;
 1602                         if (len != NULL)
 1603                                 *len = opt->len;
 1604                         if (buf != NULL)
 1605                                 *buf = opt->value;
 1606                         return (0);
 1607                 }
 1608         }
 1609         return (ENOENT);
 1610 }
 1611 
 1612 int
 1613 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
 1614 {
 1615         struct vfsopt *opt;
 1616 
 1617         if (opts == NULL)
 1618                 return (-1);
 1619 
 1620         TAILQ_FOREACH(opt, opts, link) {
 1621                 if (strcmp(name, opt->name) == 0) {
 1622                         opt->seen = 1;
 1623                         return (opt->pos);
 1624                 }
 1625         }
 1626         return (-1);
 1627 }
 1628 
 1629 int
 1630 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
 1631 {
 1632         char *opt_value, *vtp;
 1633         quad_t iv;
 1634         int error, opt_len;
 1635 
 1636         error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
 1637         if (error != 0)
 1638                 return (error);
 1639         if (opt_len == 0 || opt_value == NULL)
 1640                 return (EINVAL);
 1641         if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
 1642                 return (EINVAL);
 1643         iv = strtoq(opt_value, &vtp, 0);
 1644         if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
 1645                 return (EINVAL);
 1646         if (iv < 0)
 1647                 return (EINVAL);
 1648         switch (vtp[0]) {
 1649         case 't':
 1650         case 'T':
 1651                 iv *= 1024;
 1652         case 'g':
 1653         case 'G':
 1654                 iv *= 1024;
 1655         case 'm':
 1656         case 'M':
 1657                 iv *= 1024;
 1658         case 'k':
 1659         case 'K':
 1660                 iv *= 1024;
 1661         case '\0':
 1662                 break;
 1663         default:
 1664                 return (EINVAL);
 1665         }
 1666         *value = iv;
 1667 
 1668         return (0);
 1669 }
 1670 
 1671 char *
 1672 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
 1673 {
 1674         struct vfsopt *opt;
 1675 
 1676         *error = 0;
 1677         TAILQ_FOREACH(opt, opts, link) {
 1678                 if (strcmp(name, opt->name) != 0)
 1679                         continue;
 1680                 opt->seen = 1;
 1681                 if (opt->len == 0 ||
 1682                     ((char *)opt->value)[opt->len - 1] != '\0') {
 1683                         *error = EINVAL;
 1684                         return (NULL);
 1685                 }
 1686                 return (opt->value);
 1687         }
 1688         *error = ENOENT;
 1689         return (NULL);
 1690 }
 1691 
 1692 int
 1693 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
 1694         uint64_t val)
 1695 {
 1696         struct vfsopt *opt;
 1697 
 1698         TAILQ_FOREACH(opt, opts, link) {
 1699                 if (strcmp(name, opt->name) == 0) {
 1700                         opt->seen = 1;
 1701                         if (w != NULL)
 1702                                 *w |= val;
 1703                         return (1);
 1704                 }
 1705         }
 1706         if (w != NULL)
 1707                 *w &= ~val;
 1708         return (0);
 1709 }
 1710 
 1711 int
 1712 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
 1713 {
 1714         va_list ap;
 1715         struct vfsopt *opt;
 1716         int ret;
 1717 
 1718         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
 1719 
 1720         TAILQ_FOREACH(opt, opts, link) {
 1721                 if (strcmp(name, opt->name) != 0)
 1722                         continue;
 1723                 opt->seen = 1;
 1724                 if (opt->len == 0 || opt->value == NULL)
 1725                         return (0);
 1726                 if (((char *)opt->value)[opt->len - 1] != '\0')
 1727                         return (0);
 1728                 va_start(ap, fmt);
 1729                 ret = vsscanf(opt->value, fmt, ap);
 1730                 va_end(ap);
 1731                 return (ret);
 1732         }
 1733         return (0);
 1734 }
 1735 
 1736 int
 1737 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
 1738 {
 1739         struct vfsopt *opt;
 1740 
 1741         TAILQ_FOREACH(opt, opts, link) {
 1742                 if (strcmp(name, opt->name) != 0)
 1743                         continue;
 1744                 opt->seen = 1;
 1745                 if (opt->value == NULL)
 1746                         opt->len = len;
 1747                 else {
 1748                         if (opt->len != len)
 1749                                 return (EINVAL);
 1750                         bcopy(value, opt->value, len);
 1751                 }
 1752                 return (0);
 1753         }
 1754         return (ENOENT);
 1755 }
 1756 
 1757 int
 1758 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
 1759 {
 1760         struct vfsopt *opt;
 1761 
 1762         TAILQ_FOREACH(opt, opts, link) {
 1763                 if (strcmp(name, opt->name) != 0)
 1764                         continue;
 1765                 opt->seen = 1;
 1766                 if (opt->value == NULL)
 1767                         opt->len = len;
 1768                 else {
 1769                         if (opt->len < len)
 1770                                 return (EINVAL);
 1771                         opt->len = len;
 1772                         bcopy(value, opt->value, len);
 1773                 }
 1774                 return (0);
 1775         }
 1776         return (ENOENT);
 1777 }
 1778 
 1779 int
 1780 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
 1781 {
 1782         struct vfsopt *opt;
 1783 
 1784         TAILQ_FOREACH(opt, opts, link) {
 1785                 if (strcmp(name, opt->name) != 0)
 1786                         continue;
 1787                 opt->seen = 1;
 1788                 if (opt->value == NULL)
 1789                         opt->len = strlen(value) + 1;
 1790                 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
 1791                         return (EINVAL);
 1792                 return (0);
 1793         }
 1794         return (ENOENT);
 1795 }
 1796 
 1797 /*
 1798  * Find and copy a mount option.
 1799  *
 1800  * The size of the buffer has to be specified
 1801  * in len, if it is not the same length as the
 1802  * mount option, EINVAL is returned.
 1803  * Returns ENOENT if the option is not found.
 1804  */
 1805 int
 1806 vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len)
 1807 {
 1808         struct vfsopt *opt;
 1809 
 1810         KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
 1811 
 1812         TAILQ_FOREACH(opt, opts, link) {
 1813                 if (strcmp(name, opt->name) == 0) {
 1814                         opt->seen = 1;
 1815                         if (len != opt->len)
 1816                                 return (EINVAL);
 1817                         bcopy(opt->value, dest, opt->len);
 1818                         return (0);
 1819                 }
 1820         }
 1821         return (ENOENT);
 1822 }
 1823 
 1824 int
 1825 __vfs_statfs(struct mount *mp, struct statfs *sbp)
 1826 {
 1827         int error;
 1828 
 1829         error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
 1830         if (sbp != &mp->mnt_stat)
 1831                 *sbp = mp->mnt_stat;
 1832         return (error);
 1833 }
 1834 
 1835 void
 1836 vfs_mountedfrom(struct mount *mp, const char *from)
 1837 {
 1838 
 1839         bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
 1840         strlcpy(mp->mnt_stat.f_mntfromname, from,
 1841             sizeof mp->mnt_stat.f_mntfromname);
 1842 }
 1843 
 1844 /*
 1845  * ---------------------------------------------------------------------
 1846  * This is the api for building mount args and mounting filesystems from
 1847  * inside the kernel.
 1848  *
 1849  * The API works by accumulation of individual args.  First error is
 1850  * latched.
 1851  *
 1852  * XXX: should be documented in new manpage kernel_mount(9)
 1853  */
 1854 
 1855 /* A memory allocation which must be freed when we are done */
 1856 struct mntaarg {
 1857         SLIST_ENTRY(mntaarg)    next;
 1858 };
 1859 
 1860 /* The header for the mount arguments */
 1861 struct mntarg {
 1862         struct iovec *v;
 1863         int len;
 1864         int error;
 1865         SLIST_HEAD(, mntaarg)   list;
 1866 };
 1867 
 1868 /*
 1869  * Add a boolean argument.
 1870  *
 1871  * flag is the boolean value.
 1872  * name must start with "no".
 1873  */
 1874 struct mntarg *
 1875 mount_argb(struct mntarg *ma, int flag, const char *name)
 1876 {
 1877 
 1878         KASSERT(name[0] == 'n' && name[1] == 'o',
 1879             ("mount_argb(...,%s): name must start with 'no'", name));
 1880 
 1881         return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
 1882 }
 1883 
 1884 /*
 1885  * Add an argument printf style
 1886  */
 1887 struct mntarg *
 1888 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
 1889 {
 1890         va_list ap;
 1891         struct mntaarg *maa;
 1892         struct sbuf *sb;
 1893         int len;
 1894 
 1895         if (ma == NULL) {
 1896                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
 1897                 SLIST_INIT(&ma->list);
 1898         }
 1899         if (ma->error)
 1900                 return (ma);
 1901 
 1902         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
 1903             M_MOUNT, M_WAITOK);
 1904         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
 1905         ma->v[ma->len].iov_len = strlen(name) + 1;
 1906         ma->len++;
 1907 
 1908         sb = sbuf_new_auto();
 1909         va_start(ap, fmt);
 1910         sbuf_vprintf(sb, fmt, ap);
 1911         va_end(ap);
 1912         sbuf_finish(sb);
 1913         len = sbuf_len(sb) + 1;
 1914         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
 1915         SLIST_INSERT_HEAD(&ma->list, maa, next);
 1916         bcopy(sbuf_data(sb), maa + 1, len);
 1917         sbuf_delete(sb);
 1918 
 1919         ma->v[ma->len].iov_base = maa + 1;
 1920         ma->v[ma->len].iov_len = len;
 1921         ma->len++;
 1922 
 1923         return (ma);
 1924 }
 1925 
 1926 /*
 1927  * Add an argument which is a userland string.
 1928  */
 1929 struct mntarg *
 1930 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
 1931 {
 1932         struct mntaarg *maa;
 1933         char *tbuf;
 1934 
 1935         if (val == NULL)
 1936                 return (ma);
 1937         if (ma == NULL) {
 1938                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
 1939                 SLIST_INIT(&ma->list);
 1940         }
 1941         if (ma->error)
 1942                 return (ma);
 1943         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
 1944         SLIST_INSERT_HEAD(&ma->list, maa, next);
 1945         tbuf = (void *)(maa + 1);
 1946         ma->error = copyinstr(val, tbuf, len, NULL);
 1947         return (mount_arg(ma, name, tbuf, -1));
 1948 }
 1949 
 1950 /*
 1951  * Plain argument.
 1952  *
 1953  * If length is -1, treat value as a C string.
 1954  */
 1955 struct mntarg *
 1956 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
 1957 {
 1958 
 1959         if (ma == NULL) {
 1960                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
 1961                 SLIST_INIT(&ma->list);
 1962         }
 1963         if (ma->error)
 1964                 return (ma);
 1965 
 1966         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
 1967             M_MOUNT, M_WAITOK);
 1968         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
 1969         ma->v[ma->len].iov_len = strlen(name) + 1;
 1970         ma->len++;
 1971 
 1972         ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
 1973         if (len < 0)
 1974                 ma->v[ma->len].iov_len = strlen(val) + 1;
 1975         else
 1976                 ma->v[ma->len].iov_len = len;
 1977         ma->len++;
 1978         return (ma);
 1979 }
 1980 
 1981 /*
 1982  * Free a mntarg structure
 1983  */
 1984 static void
 1985 free_mntarg(struct mntarg *ma)
 1986 {
 1987         struct mntaarg *maa;
 1988 
 1989         while (!SLIST_EMPTY(&ma->list)) {
 1990                 maa = SLIST_FIRST(&ma->list);
 1991                 SLIST_REMOVE_HEAD(&ma->list, next);
 1992                 free(maa, M_MOUNT);
 1993         }
 1994         free(ma->v, M_MOUNT);
 1995         free(ma, M_MOUNT);
 1996 }
 1997 
 1998 /*
 1999  * Mount a filesystem
 2000  */
 2001 int
 2002 kernel_mount(struct mntarg *ma, uint64_t flags)
 2003 {
 2004         struct uio auio;
 2005         int error;
 2006 
 2007         KASSERT(ma != NULL, ("kernel_mount NULL ma"));
 2008         KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
 2009         KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
 2010 
 2011         auio.uio_iov = ma->v;
 2012         auio.uio_iovcnt = ma->len;
 2013         auio.uio_segflg = UIO_SYSSPACE;
 2014 
 2015         error = ma->error;
 2016         if (!error)
 2017                 error = vfs_donmount(curthread, flags, &auio);
 2018         free_mntarg(ma);
 2019         return (error);
 2020 }
 2021 
 2022 /*
 2023  * A printflike function to mount a filesystem.
 2024  */
 2025 int
 2026 kernel_vmount(int flags, ...)
 2027 {
 2028         struct mntarg *ma = NULL;
 2029         va_list ap;
 2030         const char *cp;
 2031         const void *vp;
 2032         int error;
 2033 
 2034         va_start(ap, flags);
 2035         for (;;) {
 2036                 cp = va_arg(ap, const char *);
 2037                 if (cp == NULL)
 2038                         break;
 2039                 vp = va_arg(ap, const void *);
 2040                 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
 2041         }
 2042         va_end(ap);
 2043 
 2044         error = kernel_mount(ma, flags);
 2045         return (error);
 2046 }
 2047 
 2048 void
 2049 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
 2050 {
 2051 
 2052         bcopy(oexp, exp, sizeof(*oexp));
 2053         exp->ex_numsecflavors = 0;
 2054 }

Cache object: bf62ce41199ac9edd2e1d9de9bce7b1f


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