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/lock.h> 35 #include <sys/malloc.h> 36 #include <sys/mutex.h> 37 #include <sys/vnode.h> 38 #include <sys/mount.h> /* XXX Temporary for VFS_LOCK_GIANT */ 39 40 #include <geom/geom.h> 41 #include <geom/geom_vfs.h> 42 43 /* 44 * subroutines for use by filesystems. 45 * 46 * XXX: should maybe live somewhere else ? 47 */ 48 #include <sys/buf.h> 49 50 struct g_vfs_softc { 51 struct mtx sc_mtx; 52 struct bufobj *sc_bo; 53 int sc_active; 54 int sc_orphaned; 55 }; 56 57 static struct buf_ops __g_vfs_bufops = { 58 .bop_name = "GEOM_VFS", 59 .bop_write = bufwrite, 60 .bop_strategy = g_vfs_strategy, 61 .bop_sync = bufsync, 62 .bop_bdflush = bufbdflush 63 }; 64 65 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops; 66 67 static g_orphan_t g_vfs_orphan; 68 69 static struct g_class g_vfs_class = { 70 .name = "VFS", 71 .version = G_VERSION, 72 .orphan = g_vfs_orphan, 73 }; 74 75 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs); 76 77 static void 78 g_vfs_destroy(void *arg, int flags __unused) 79 { 80 struct g_consumer *cp; 81 82 g_topology_assert(); 83 cp = arg; 84 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 85 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 86 g_detach(cp); 87 if (cp->geom->softc == NULL) 88 g_wither_geom(cp->geom, ENXIO); 89 } 90 91 static void 92 g_vfs_done(struct bio *bip) 93 { 94 struct g_consumer *cp; 95 struct g_vfs_softc *sc; 96 struct buf *bp; 97 int vfslocked, destroy; 98 99 cp = bip->bio_from; 100 sc = cp->geom->softc; 101 if (bip->bio_error) { 102 printf("g_vfs_done():"); 103 g_print_bio(bip); 104 printf("error = %d\n", bip->bio_error); 105 } 106 bp = bip->bio_caller2; 107 bp->b_error = bip->bio_error; 108 bp->b_ioflags = bip->bio_flags; 109 if (bip->bio_error) 110 bp->b_ioflags |= BIO_ERROR; 111 bp->b_resid = bp->b_bcount - bip->bio_completed; 112 g_destroy_bio(bip); 113 114 mtx_lock(&sc->sc_mtx); 115 destroy = ((--sc->sc_active) == 0 && sc->sc_orphaned); 116 mtx_unlock(&sc->sc_mtx); 117 if (destroy) 118 g_post_event(g_vfs_destroy, cp, M_WAITOK, NULL); 119 120 vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL)); 121 bufdone(bp); 122 VFS_UNLOCK_GIANT(vfslocked); 123 } 124 125 void 126 g_vfs_strategy(struct bufobj *bo, struct buf *bp) 127 { 128 struct g_vfs_softc *sc; 129 struct g_consumer *cp; 130 struct bio *bip; 131 int vfslocked; 132 133 cp = bo->bo_private; 134 sc = cp->geom->softc; 135 136 /* 137 * If the provider has orphaned us, just return EXIO. 138 */ 139 mtx_lock(&sc->sc_mtx); 140 if (sc->sc_orphaned) { 141 mtx_unlock(&sc->sc_mtx); 142 bp->b_error = ENXIO; 143 bp->b_ioflags |= BIO_ERROR; 144 vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL)); 145 bufdone(bp); 146 VFS_UNLOCK_GIANT(vfslocked); 147 return; 148 } 149 sc->sc_active++; 150 mtx_unlock(&sc->sc_mtx); 151 152 bip = g_alloc_bio(); 153 bip->bio_cmd = bp->b_iocmd; 154 bip->bio_offset = bp->b_iooffset; 155 bip->bio_data = bp->b_data; 156 bip->bio_done = g_vfs_done; 157 bip->bio_caller2 = bp; 158 bip->bio_length = bp->b_bcount; 159 g_io_request(bip, cp); 160 } 161 162 static void 163 g_vfs_orphan(struct g_consumer *cp) 164 { 165 struct g_geom *gp; 166 struct g_vfs_softc *sc; 167 int destroy; 168 169 g_topology_assert(); 170 171 gp = cp->geom; 172 sc = gp->softc; 173 g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name); 174 mtx_lock(&sc->sc_mtx); 175 sc->sc_orphaned = 1; 176 destroy = (sc->sc_active == 0); 177 mtx_unlock(&sc->sc_mtx); 178 if (destroy) 179 g_vfs_destroy(cp, 0); 180 181 /* 182 * Do not destroy the geom. Filesystem will do that during unmount. 183 */ 184 } 185 186 int 187 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr) 188 { 189 struct g_geom *gp; 190 struct g_provider *pp; 191 struct g_consumer *cp; 192 struct g_vfs_softc *sc; 193 struct bufobj *bo; 194 int vfslocked; 195 int error; 196 197 g_topology_assert(); 198 199 *cpp = NULL; 200 bo = &vp->v_bufobj; 201 if (bo->bo_private != vp) 202 return (EBUSY); 203 204 pp = g_dev_getprovider(vp->v_rdev); 205 if (pp == NULL) 206 return (ENOENT); 207 gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name); 208 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 209 mtx_init(&sc->sc_mtx, "g_vfs", NULL, MTX_DEF); 210 sc->sc_bo = bo; 211 gp->softc = sc; 212 cp = g_new_consumer(gp); 213 g_attach(cp, pp); 214 error = g_access(cp, 1, wr, wr); 215 if (error) { 216 g_wither_geom(gp, ENXIO); 217 return (error); 218 } 219 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 220 vnode_create_vobject(vp, pp->mediasize, curthread); 221 VFS_UNLOCK_GIANT(vfslocked); 222 *cpp = cp; 223 cp->private = vp; 224 bo->bo_ops = g_vfs_bufops; 225 bo->bo_private = cp; 226 bo->bo_bsize = pp->sectorsize; 227 228 return (error); 229 } 230 231 void 232 g_vfs_close(struct g_consumer *cp) 233 { 234 struct g_geom *gp; 235 struct g_vfs_softc *sc; 236 237 g_topology_assert(); 238 239 gp = cp->geom; 240 sc = gp->softc; 241 bufobj_invalbuf(sc->sc_bo, V_SAVE, 0, 0); 242 sc->sc_bo->bo_private = cp->private; 243 gp->softc = NULL; 244 mtx_destroy(&sc->sc_mtx); 245 if (!sc->sc_orphaned || cp->provider == NULL) 246 g_wither_geom_close(gp, ENXIO); 247 g_free(sc); 248 } 249