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/geom/geom_vfs.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) 2004 Poul-Henning Kamp
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include <sys/param.h>
   31 #include <sys/systm.h>
   32 #include <sys/bio.h>
   33 #include <sys/kernel.h>
   34 #include <sys/malloc.h>
   35 #include <sys/vnode.h>
   36 #include <sys/mount.h>  /* XXX Temporary for VFS_LOCK_GIANT */
   37 
   38 #include <geom/geom.h>
   39 #include <geom/geom_vfs.h>
   40 
   41 /*
   42  * subroutines for use by filesystems.
   43  *
   44  * XXX: should maybe live somewhere else ?
   45  */
   46 #include <sys/buf.h>
   47 
   48 static struct buf_ops __g_vfs_bufops = {
   49         .bop_name =     "GEOM_VFS",
   50         .bop_write =    bufwrite,
   51         .bop_strategy = g_vfs_strategy, 
   52         .bop_sync =     bufsync,        
   53         .bop_bdflush =  bufbdflush
   54 };
   55 
   56 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
   57 
   58 static g_orphan_t g_vfs_orphan;
   59 
   60 static struct g_class g_vfs_class = {
   61         .name =         "VFS",
   62         .version =      G_VERSION,
   63         .orphan =       g_vfs_orphan,
   64 };
   65 
   66 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
   67 
   68 static void
   69 g_vfs_done(struct bio *bip)
   70 {
   71         struct buf *bp;
   72         int vfslocked;
   73 
   74         /*
   75          * Provider ('bio_to') could have withered away sometime
   76          * between incrementing the 'nend' in g_io_deliver() and now,
   77          * making 'bio_to' a dangling pointer.  We cannot do that
   78          * in g_wither_geom(), as it would require going over
   79          * the 'g_bio_run_up' list, resetting the pointer.
   80          */
   81         if (bip->bio_from->provider == NULL)
   82                 bip->bio_to = NULL;
   83 
   84         if (bip->bio_error) {
   85                 printf("g_vfs_done():");
   86                 g_print_bio(bip);
   87                 printf("error = %d\n", bip->bio_error);
   88         }
   89         bp = bip->bio_caller2;
   90         bp->b_error = bip->bio_error;
   91         bp->b_ioflags = bip->bio_flags;
   92         if (bip->bio_error)
   93                 bp->b_ioflags |= BIO_ERROR;
   94         bp->b_resid = bp->b_bcount - bip->bio_completed;
   95         g_destroy_bio(bip);
   96         vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL));
   97         bufdone(bp);
   98         VFS_UNLOCK_GIANT(vfslocked);
   99 }
  100 
  101 void
  102 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
  103 {
  104         struct g_consumer *cp;
  105         struct bio *bip;
  106         int vfslocked;
  107 
  108         cp = bo->bo_private;
  109         /* G_VALID_CONSUMER(cp); We likely lack topology lock */
  110 
  111         /*
  112          * If the provider has orphaned us, just return EXIO.
  113          */
  114         if (cp->provider == NULL) {
  115                 bp->b_error = ENXIO;
  116                 bp->b_ioflags |= BIO_ERROR;
  117                 vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL));
  118                 bufdone(bp);
  119                 VFS_UNLOCK_GIANT(vfslocked);
  120                 return;
  121         }
  122 
  123         bip = g_alloc_bio();
  124         bip->bio_cmd = bp->b_iocmd;
  125         bip->bio_offset = bp->b_iooffset;
  126         bip->bio_data = bp->b_data;
  127         bip->bio_done = g_vfs_done;
  128         bip->bio_caller2 = bp;
  129         bip->bio_length = bp->b_bcount;
  130         if (bp->b_flags & B_BARRIER) {
  131                 bip->bio_flags |= BIO_ORDERED;
  132                 bp->b_flags &= ~B_BARRIER;
  133         }
  134         g_io_request(bip, cp);
  135 }
  136 
  137 static void
  138 g_vfs_orphan(struct g_consumer *cp)
  139 {
  140         struct g_geom *gp;
  141         struct bufobj *bo;
  142 
  143         g_topology_assert();
  144 
  145         gp = cp->geom;
  146         bo = gp->softc;
  147         g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name);
  148         if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
  149                 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
  150         g_detach(cp);
  151 
  152         /*
  153          * Do not destroy the geom.  Filesystem will do that during unmount.
  154          */
  155 }
  156 
  157 int
  158 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
  159 {
  160         struct g_geom *gp;
  161         struct g_provider *pp;
  162         struct g_consumer *cp;
  163         struct bufobj *bo;
  164         int vfslocked;
  165         int error;
  166 
  167         g_topology_assert();
  168 
  169         *cpp = NULL;
  170         bo = &vp->v_bufobj;
  171         if (bo->bo_private != vp)
  172                 return (EBUSY);
  173 
  174         pp = g_dev_getprovider(vp->v_rdev);
  175         if (pp == NULL)
  176                 return (ENOENT);
  177         gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
  178         cp = g_new_consumer(gp);
  179         g_attach(cp, pp);
  180         error = g_access(cp, 1, wr, 1);
  181         if (error) {
  182                 g_wither_geom(gp, ENXIO);
  183                 return (error);
  184         }
  185         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  186         vnode_create_vobject(vp, pp->mediasize, curthread);
  187         VFS_UNLOCK_GIANT(vfslocked);
  188         *cpp = cp;
  189         cp->private = vp;
  190         bo->bo_ops = g_vfs_bufops;
  191         bo->bo_private = cp;
  192         bo->bo_bsize = pp->sectorsize;
  193         gp->softc = bo;
  194 
  195         return (error);
  196 }
  197 
  198 void
  199 g_vfs_close(struct g_consumer *cp)
  200 {
  201         struct g_geom *gp;
  202         struct bufobj *bo;
  203 
  204         g_topology_assert();
  205 
  206         gp = cp->geom;
  207         bo = gp->softc;
  208         bufobj_invalbuf(bo, V_SAVE, 0, 0);
  209         bo->bo_private = cp->private;
  210         g_wither_geom_close(gp, ENXIO);
  211 }

Cache object: 171147e5b99f56d72f15bba8d7c81517


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