14d13ab3dSPoul-Henning Kamp /*- 24d13ab3dSPoul-Henning Kamp * Copyright (c) 2004 Poul-Henning Kamp 34d13ab3dSPoul-Henning Kamp * All rights reserved. 44d13ab3dSPoul-Henning Kamp * 54d13ab3dSPoul-Henning Kamp * Redistribution and use in source and binary forms, with or without 64d13ab3dSPoul-Henning Kamp * modification, are permitted provided that the following conditions 74d13ab3dSPoul-Henning Kamp * are met: 84d13ab3dSPoul-Henning Kamp * 1. Redistributions of source code must retain the above copyright 94d13ab3dSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer. 104d13ab3dSPoul-Henning Kamp * 2. Redistributions in binary form must reproduce the above copyright 114d13ab3dSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer in the 124d13ab3dSPoul-Henning Kamp * documentation and/or other materials provided with the distribution. 134d13ab3dSPoul-Henning Kamp * 144d13ab3dSPoul-Henning Kamp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 154d13ab3dSPoul-Henning Kamp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 164d13ab3dSPoul-Henning Kamp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 174d13ab3dSPoul-Henning Kamp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 184d13ab3dSPoul-Henning Kamp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 194d13ab3dSPoul-Henning Kamp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 204d13ab3dSPoul-Henning Kamp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 214d13ab3dSPoul-Henning Kamp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 224d13ab3dSPoul-Henning Kamp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 234d13ab3dSPoul-Henning Kamp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 244d13ab3dSPoul-Henning Kamp * SUCH DAMAGE. 254d13ab3dSPoul-Henning Kamp */ 264d13ab3dSPoul-Henning Kamp 274d13ab3dSPoul-Henning Kamp #include <sys/cdefs.h> 284d13ab3dSPoul-Henning Kamp __FBSDID("$FreeBSD$"); 294d13ab3dSPoul-Henning Kamp 304d13ab3dSPoul-Henning Kamp #include <sys/param.h> 314d13ab3dSPoul-Henning Kamp #include <sys/systm.h> 324d13ab3dSPoul-Henning Kamp #include <sys/bio.h> 334d13ab3dSPoul-Henning Kamp #include <sys/kernel.h> 34ea5791d7SAlexander Motin #include <sys/lock.h> 354d13ab3dSPoul-Henning Kamp #include <sys/malloc.h> 36ea5791d7SAlexander Motin #include <sys/mutex.h> 374d13ab3dSPoul-Henning Kamp #include <sys/vnode.h> 381907e620SJeff Roberson #include <sys/mount.h> /* XXX Temporary for VFS_LOCK_GIANT */ 394d13ab3dSPoul-Henning Kamp 404d13ab3dSPoul-Henning Kamp #include <geom/geom.h> 414d13ab3dSPoul-Henning Kamp #include <geom/geom_vfs.h> 424d13ab3dSPoul-Henning Kamp 434d13ab3dSPoul-Henning Kamp /* 444d13ab3dSPoul-Henning Kamp * subroutines for use by filesystems. 454d13ab3dSPoul-Henning Kamp * 464d13ab3dSPoul-Henning Kamp * XXX: should maybe live somewhere else ? 474d13ab3dSPoul-Henning Kamp */ 484d13ab3dSPoul-Henning Kamp #include <sys/buf.h> 494d13ab3dSPoul-Henning Kamp 50ea5791d7SAlexander Motin struct g_vfs_softc { 51ea5791d7SAlexander Motin struct mtx sc_mtx; 52ea5791d7SAlexander Motin struct bufobj *sc_bo; 53ea5791d7SAlexander Motin int sc_active; 54ea5791d7SAlexander Motin int sc_orphaned; 55ea5791d7SAlexander Motin }; 56ea5791d7SAlexander Motin 574d13ab3dSPoul-Henning Kamp static struct buf_ops __g_vfs_bufops = { 584d13ab3dSPoul-Henning Kamp .bop_name = "GEOM_VFS", 594d13ab3dSPoul-Henning Kamp .bop_write = bufwrite, 604d13ab3dSPoul-Henning Kamp .bop_strategy = g_vfs_strategy, 616ef8480aSPoul-Henning Kamp .bop_sync = bufsync, 622cc7d26fSKonstantin Belousov .bop_bdflush = bufbdflush 634d13ab3dSPoul-Henning Kamp }; 644d13ab3dSPoul-Henning Kamp 654d13ab3dSPoul-Henning Kamp struct buf_ops *g_vfs_bufops = &__g_vfs_bufops; 664d13ab3dSPoul-Henning Kamp 6707e95ed6SPoul-Henning Kamp static g_orphan_t g_vfs_orphan; 6807e95ed6SPoul-Henning Kamp 694d13ab3dSPoul-Henning Kamp static struct g_class g_vfs_class = { 704d13ab3dSPoul-Henning Kamp .name = "VFS", 714d13ab3dSPoul-Henning Kamp .version = G_VERSION, 724d13ab3dSPoul-Henning Kamp .orphan = g_vfs_orphan, 734d13ab3dSPoul-Henning Kamp }; 744d13ab3dSPoul-Henning Kamp 754d13ab3dSPoul-Henning Kamp DECLARE_GEOM_CLASS(g_vfs_class, g_vfs); 764d13ab3dSPoul-Henning Kamp 774d13ab3dSPoul-Henning Kamp static void 78ea5791d7SAlexander Motin g_vfs_destroy(void *arg, int flags __unused) 79ea5791d7SAlexander Motin { 80ea5791d7SAlexander Motin struct g_consumer *cp; 81ea5791d7SAlexander Motin 82ea5791d7SAlexander Motin g_topology_assert(); 83ea5791d7SAlexander Motin cp = arg; 84ea5791d7SAlexander Motin if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 85ea5791d7SAlexander Motin g_access(cp, -cp->acr, -cp->acw, -cp->ace); 86ea5791d7SAlexander Motin g_detach(cp); 87ea5791d7SAlexander Motin if (cp->geom->softc == NULL) 88ea5791d7SAlexander Motin g_wither_geom(cp->geom, ENXIO); 89ea5791d7SAlexander Motin } 90ea5791d7SAlexander Motin 91ea5791d7SAlexander Motin static void 924d13ab3dSPoul-Henning Kamp g_vfs_done(struct bio *bip) 934d13ab3dSPoul-Henning Kamp { 94ea5791d7SAlexander Motin struct g_consumer *cp; 95ea5791d7SAlexander Motin struct g_vfs_softc *sc; 964d13ab3dSPoul-Henning Kamp struct buf *bp; 97ea5791d7SAlexander Motin int vfslocked, destroy; 984d13ab3dSPoul-Henning Kamp 99ea5791d7SAlexander Motin cp = bip->bio_from; 100ea5791d7SAlexander Motin sc = cp->geom->softc; 1014d13ab3dSPoul-Henning Kamp if (bip->bio_error) { 102f9eeb895SPoul-Henning Kamp printf("g_vfs_done():"); 1034d13ab3dSPoul-Henning Kamp g_print_bio(bip); 1044d13ab3dSPoul-Henning Kamp printf("error = %d\n", bip->bio_error); 1054d13ab3dSPoul-Henning Kamp } 1064d13ab3dSPoul-Henning Kamp bp = bip->bio_caller2; 1074d13ab3dSPoul-Henning Kamp bp->b_error = bip->bio_error; 1084d13ab3dSPoul-Henning Kamp bp->b_ioflags = bip->bio_flags; 1094d13ab3dSPoul-Henning Kamp if (bip->bio_error) 1104d13ab3dSPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 1114d13ab3dSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bip->bio_completed; 1124d13ab3dSPoul-Henning Kamp g_destroy_bio(bip); 113ea5791d7SAlexander Motin 114ea5791d7SAlexander Motin mtx_lock(&sc->sc_mtx); 115ea5791d7SAlexander Motin destroy = ((--sc->sc_active) == 0 && sc->sc_orphaned); 116ea5791d7SAlexander Motin mtx_unlock(&sc->sc_mtx); 117ea5791d7SAlexander Motin if (destroy) 118ea5791d7SAlexander Motin g_post_event(g_vfs_destroy, cp, M_WAITOK, NULL); 119ea5791d7SAlexander Motin 1201907e620SJeff Roberson vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL)); 1214d13ab3dSPoul-Henning Kamp bufdone(bp); 1221907e620SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 1234d13ab3dSPoul-Henning Kamp } 1244d13ab3dSPoul-Henning Kamp 1254d13ab3dSPoul-Henning Kamp void 1264d13ab3dSPoul-Henning Kamp g_vfs_strategy(struct bufobj *bo, struct buf *bp) 1274d13ab3dSPoul-Henning Kamp { 128ea5791d7SAlexander Motin struct g_vfs_softc *sc; 1294d13ab3dSPoul-Henning Kamp struct g_consumer *cp; 1304d13ab3dSPoul-Henning Kamp struct bio *bip; 131ce8be7b8SEdward Tomasz Napierala int vfslocked; 1324d13ab3dSPoul-Henning Kamp 1334d13ab3dSPoul-Henning Kamp cp = bo->bo_private; 134ea5791d7SAlexander Motin sc = cp->geom->softc; 1354d13ab3dSPoul-Henning Kamp 136ce8be7b8SEdward Tomasz Napierala /* 1376bccea7cSRebecca Cran * If the provider has orphaned us, just return EXIO. 138ce8be7b8SEdward Tomasz Napierala */ 139ea5791d7SAlexander Motin mtx_lock(&sc->sc_mtx); 140ea5791d7SAlexander Motin if (sc->sc_orphaned) { 141ea5791d7SAlexander Motin mtx_unlock(&sc->sc_mtx); 142ce8be7b8SEdward Tomasz Napierala bp->b_error = ENXIO; 143ce8be7b8SEdward Tomasz Napierala bp->b_ioflags |= BIO_ERROR; 144ce8be7b8SEdward Tomasz Napierala vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL)); 145ce8be7b8SEdward Tomasz Napierala bufdone(bp); 146ce8be7b8SEdward Tomasz Napierala VFS_UNLOCK_GIANT(vfslocked); 147ce8be7b8SEdward Tomasz Napierala return; 148ce8be7b8SEdward Tomasz Napierala } 149ea5791d7SAlexander Motin sc->sc_active++; 150ea5791d7SAlexander Motin mtx_unlock(&sc->sc_mtx); 151ce8be7b8SEdward Tomasz Napierala 1524d13ab3dSPoul-Henning Kamp bip = g_alloc_bio(); 1534d13ab3dSPoul-Henning Kamp bip->bio_cmd = bp->b_iocmd; 1544d13ab3dSPoul-Henning Kamp bip->bio_offset = bp->b_iooffset; 1554d13ab3dSPoul-Henning Kamp bip->bio_data = bp->b_data; 1564d13ab3dSPoul-Henning Kamp bip->bio_done = g_vfs_done; 1574d13ab3dSPoul-Henning Kamp bip->bio_caller2 = bp; 1584d13ab3dSPoul-Henning Kamp bip->bio_length = bp->b_bcount; 1594d13ab3dSPoul-Henning Kamp g_io_request(bip, cp); 1604d13ab3dSPoul-Henning Kamp } 1614d13ab3dSPoul-Henning Kamp 16207e95ed6SPoul-Henning Kamp static void 1634d13ab3dSPoul-Henning Kamp g_vfs_orphan(struct g_consumer *cp) 1644d13ab3dSPoul-Henning Kamp { 165ce8be7b8SEdward Tomasz Napierala struct g_geom *gp; 166ea5791d7SAlexander Motin struct g_vfs_softc *sc; 167ea5791d7SAlexander Motin int destroy; 168ce8be7b8SEdward Tomasz Napierala 169ce8be7b8SEdward Tomasz Napierala g_topology_assert(); 170ce8be7b8SEdward Tomasz Napierala 171ce8be7b8SEdward Tomasz Napierala gp = cp->geom; 172ce8be7b8SEdward Tomasz Napierala g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name); 173*a2fa37feSAlexander Motin sc = gp->softc; 174*a2fa37feSAlexander Motin if (sc == NULL) 175*a2fa37feSAlexander Motin return; 176ea5791d7SAlexander Motin mtx_lock(&sc->sc_mtx); 177ea5791d7SAlexander Motin sc->sc_orphaned = 1; 178ea5791d7SAlexander Motin destroy = (sc->sc_active == 0); 179ea5791d7SAlexander Motin mtx_unlock(&sc->sc_mtx); 180ea5791d7SAlexander Motin if (destroy) 181ea5791d7SAlexander Motin g_vfs_destroy(cp, 0); 1824d13ab3dSPoul-Henning Kamp 1834d13ab3dSPoul-Henning Kamp /* 18438153e80SEdward Tomasz Napierala * Do not destroy the geom. Filesystem will do that during unmount. 1854d13ab3dSPoul-Henning Kamp */ 1864d13ab3dSPoul-Henning Kamp } 1874d13ab3dSPoul-Henning Kamp 1884d13ab3dSPoul-Henning Kamp int 1894d13ab3dSPoul-Henning Kamp g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr) 1904d13ab3dSPoul-Henning Kamp { 1914d13ab3dSPoul-Henning Kamp struct g_geom *gp; 1924d13ab3dSPoul-Henning Kamp struct g_provider *pp; 1934d13ab3dSPoul-Henning Kamp struct g_consumer *cp; 194ea5791d7SAlexander Motin struct g_vfs_softc *sc; 1954d13ab3dSPoul-Henning Kamp struct bufobj *bo; 196420239c7SJeff Roberson int vfslocked; 1974d13ab3dSPoul-Henning Kamp int error; 1984d13ab3dSPoul-Henning Kamp 1994d13ab3dSPoul-Henning Kamp g_topology_assert(); 2004d13ab3dSPoul-Henning Kamp 2014d13ab3dSPoul-Henning Kamp *cpp = NULL; 2028f128ff5SAndriy Gapon bo = &vp->v_bufobj; 2038f128ff5SAndriy Gapon if (bo->bo_private != vp) 2048f128ff5SAndriy Gapon return (EBUSY); 2058f128ff5SAndriy Gapon 2064d13ab3dSPoul-Henning Kamp pp = g_dev_getprovider(vp->v_rdev); 2074d13ab3dSPoul-Henning Kamp if (pp == NULL) 2084d13ab3dSPoul-Henning Kamp return (ENOENT); 2094d13ab3dSPoul-Henning Kamp gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name); 210ea5791d7SAlexander Motin sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 211ea5791d7SAlexander Motin mtx_init(&sc->sc_mtx, "g_vfs", NULL, MTX_DEF); 212ea5791d7SAlexander Motin sc->sc_bo = bo; 213ea5791d7SAlexander Motin gp->softc = sc; 2144d13ab3dSPoul-Henning Kamp cp = g_new_consumer(gp); 2154d13ab3dSPoul-Henning Kamp g_attach(cp, pp); 2168795189cSKirk McKusick error = g_access(cp, 1, wr, wr); 2174d13ab3dSPoul-Henning Kamp if (error) { 2184d13ab3dSPoul-Henning Kamp g_wither_geom(gp, ENXIO); 2194d13ab3dSPoul-Henning Kamp return (error); 2204d13ab3dSPoul-Henning Kamp } 221420239c7SJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 222bc0fc6fcSPoul-Henning Kamp vnode_create_vobject(vp, pp->mediasize, curthread); 223420239c7SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 2244d13ab3dSPoul-Henning Kamp *cpp = cp; 2258f128ff5SAndriy Gapon cp->private = vp; 2264d13ab3dSPoul-Henning Kamp bo->bo_ops = g_vfs_bufops; 2274d13ab3dSPoul-Henning Kamp bo->bo_private = cp; 2281b4bc5f8SAndriy Gapon bo->bo_bsize = pp->sectorsize; 2294d13ab3dSPoul-Henning Kamp 2304d13ab3dSPoul-Henning Kamp return (error); 2314d13ab3dSPoul-Henning Kamp } 23284a69752SPoul-Henning Kamp 23384a69752SPoul-Henning Kamp void 2340d7935fdSAttilio Rao g_vfs_close(struct g_consumer *cp) 23584a69752SPoul-Henning Kamp { 23684a69752SPoul-Henning Kamp struct g_geom *gp; 237ea5791d7SAlexander Motin struct g_vfs_softc *sc; 23884a69752SPoul-Henning Kamp 23984a69752SPoul-Henning Kamp g_topology_assert(); 24084a69752SPoul-Henning Kamp 24184a69752SPoul-Henning Kamp gp = cp->geom; 242ea5791d7SAlexander Motin sc = gp->softc; 243ea5791d7SAlexander Motin bufobj_invalbuf(sc->sc_bo, V_SAVE, 0, 0); 244ea5791d7SAlexander Motin sc->sc_bo->bo_private = cp->private; 245ea5791d7SAlexander Motin gp->softc = NULL; 246ea5791d7SAlexander Motin mtx_destroy(&sc->sc_mtx); 247ea5791d7SAlexander Motin if (!sc->sc_orphaned || cp->provider == NULL) 24884a69752SPoul-Henning Kamp g_wither_geom_close(gp, ENXIO); 249ea5791d7SAlexander Motin g_free(sc); 25084a69752SPoul-Henning Kamp } 251