1133640Sfjoe/*- 2133640Sfjoe * Copyright (c) 2004 Max Khon 3133640Sfjoe * All rights reserved. 4133640Sfjoe * 5133640Sfjoe * Redistribution and use in source and binary forms, with or without 6133640Sfjoe * modification, are permitted provided that the following conditions 7133640Sfjoe * are met: 8133640Sfjoe * 1. Redistributions of source code must retain the above copyright 9133640Sfjoe * notice, this list of conditions and the following disclaimer. 10133640Sfjoe * 2. Redistributions in binary form must reproduce the above copyright 11133640Sfjoe * notice, this list of conditions and the following disclaimer in the 12133640Sfjoe * documentation and/or other materials provided with the distribution. 13133640Sfjoe * 14133640Sfjoe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15133640Sfjoe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16133640Sfjoe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17133640Sfjoe * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18133640Sfjoe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19133640Sfjoe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20133640Sfjoe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21133640Sfjoe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22133640Sfjoe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23133640Sfjoe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24133640Sfjoe * SUCH DAMAGE. 25133640Sfjoe */ 26133640Sfjoe 27133640Sfjoe#include <sys/cdefs.h> 28133640Sfjoe__FBSDID("$FreeBSD$"); 29133640Sfjoe 30133640Sfjoe#include <sys/param.h> 31133640Sfjoe#include <sys/bio.h> 32133640Sfjoe#include <sys/endian.h> 33133640Sfjoe#include <sys/errno.h> 34133640Sfjoe#include <sys/kernel.h> 35133640Sfjoe#include <sys/lock.h> 36133640Sfjoe#include <sys/mutex.h> 37133640Sfjoe#include <sys/malloc.h> 38133640Sfjoe#include <sys/systm.h> 39219029Snetchild#include <sys/sysctl.h> 40133640Sfjoe 41133640Sfjoe#include <geom/geom.h> 42133640Sfjoe#include <net/zlib.h> 43133640Sfjoe 44219029SnetchildFEATURE(geom_uzip, "GEOM uzip read-only compressed disks support"); 45219029Snetchild 46133640Sfjoe#undef GEOM_UZIP_DEBUG 47133640Sfjoe#ifdef GEOM_UZIP_DEBUG 48133640Sfjoe#define DPRINTF(a) printf a 49133640Sfjoe#else 50133640Sfjoe#define DPRINTF(a) 51133640Sfjoe#endif 52133640Sfjoe 53249132Smavstatic MALLOC_DEFINE(M_GEOM_UZIP, "geom_uzip", "GEOM UZIP data structures"); 54133640Sfjoe 55133640Sfjoe#define UZIP_CLASS_NAME "UZIP" 56133640Sfjoe 57133640Sfjoe/* 58133640Sfjoe * Maximum allowed valid block size (to prevent foot-shooting) 59133640Sfjoe */ 60133640Sfjoe#define MAX_BLKSZ (MAXPHYS - MAXPHYS / 1000 - 12) 61133640Sfjoe 62133640Sfjoe/* 63133640Sfjoe * Integer values (block size, number of blocks, offsets) 64133640Sfjoe * are stored in big-endian (network) order on disk and struct cloop_header 65133640Sfjoe * and in native order in struct g_uzip_softc 66133640Sfjoe */ 67133640Sfjoe 68133640Sfjoe#define CLOOP_MAGIC_LEN 128 69133640Sfjoestatic char CLOOP_MAGIC_START[] = "#!/bin/sh\n"; 70133640Sfjoe 71133640Sfjoestruct cloop_header { 72133640Sfjoe char magic[CLOOP_MAGIC_LEN]; /* cloop magic */ 73133640Sfjoe uint32_t blksz; /* block size */ 74133640Sfjoe uint32_t nblocks; /* number of blocks */ 75133640Sfjoe}; 76133640Sfjoe 77133640Sfjoestruct g_uzip_softc { 78133640Sfjoe uint32_t blksz; /* block size */ 79133640Sfjoe uint32_t nblocks; /* number of blocks */ 80133640Sfjoe uint64_t *offsets; 81133640Sfjoe 82133640Sfjoe struct mtx last_mtx; 83133640Sfjoe uint32_t last_blk; /* last blk no */ 84133640Sfjoe char *last_buf; /* last blk data */ 85133640Sfjoe int req_total; /* total requests */ 86133640Sfjoe int req_cached; /* cached requests */ 87133640Sfjoe}; 88133640Sfjoe 89133640Sfjoestatic void 90133640Sfjoeg_uzip_softc_free(struct g_uzip_softc *sc, struct g_geom *gp) 91133640Sfjoe{ 92133640Sfjoe if (gp != NULL) { 93133640Sfjoe printf("%s: %d requests, %d cached\n", 94133640Sfjoe gp->name, sc->req_total, sc->req_cached); 95133640Sfjoe } 96133640Sfjoe if (sc->offsets != NULL) 97133640Sfjoe free(sc->offsets, M_GEOM_UZIP); 98133640Sfjoe mtx_destroy(&sc->last_mtx); 99133640Sfjoe free(sc->last_buf, M_GEOM_UZIP); 100133640Sfjoe free(sc, M_GEOM_UZIP); 101133640Sfjoe} 102133640Sfjoe 103133640Sfjoestatic void * 104133640Sfjoez_alloc(void *nil, u_int type, u_int size) 105133640Sfjoe{ 106133640Sfjoe void *ptr; 107133640Sfjoe 108133640Sfjoe ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT); 109133640Sfjoe return ptr; 110133640Sfjoe} 111133640Sfjoe 112133640Sfjoestatic void 113133640Sfjoez_free(void *nil, void *ptr) 114133640Sfjoe{ 115133640Sfjoe free(ptr, M_GEOM_UZIP); 116133640Sfjoe} 117133640Sfjoe 118133640Sfjoestatic void 119133640Sfjoeg_uzip_done(struct bio *bp) 120133640Sfjoe{ 121133640Sfjoe int err; 122133640Sfjoe struct bio *bp2; 123133640Sfjoe z_stream zs; 124133640Sfjoe struct g_provider *pp, *pp2; 125133640Sfjoe struct g_consumer *cp; 126133640Sfjoe struct g_geom *gp; 127133640Sfjoe struct g_uzip_softc *sc; 128133640Sfjoe off_t pos, upos; 129133640Sfjoe uint32_t start_blk, i; 130133640Sfjoe size_t bsize; 131133640Sfjoe 132133640Sfjoe bp2 = bp->bio_parent; 133133640Sfjoe pp = bp2->bio_to; 134133640Sfjoe gp = pp->geom; 135133640Sfjoe cp = LIST_FIRST(&gp->consumer); 136133640Sfjoe pp2 = cp->provider; 137133640Sfjoe sc = gp->softc; 138133640Sfjoe DPRINTF(("%s: done\n", gp->name)); 139133640Sfjoe 140133640Sfjoe bp2->bio_error = bp->bio_error; 141133640Sfjoe if (bp2->bio_error != 0) 142133640Sfjoe goto done; 143133640Sfjoe 144133640Sfjoe /* 145133640Sfjoe * Uncompress data. 146133640Sfjoe */ 147133640Sfjoe zs.zalloc = z_alloc; 148133640Sfjoe zs.zfree = z_free; 149133640Sfjoe err = inflateInit(&zs); 150133640Sfjoe if (err != Z_OK) { 151133640Sfjoe bp2->bio_error = EIO; 152133640Sfjoe goto done; 153133640Sfjoe } 154133640Sfjoe start_blk = bp2->bio_offset / sc->blksz; 155133640Sfjoe bsize = pp2->sectorsize; 156133640Sfjoe pos = sc->offsets[start_blk] % bsize; 157133640Sfjoe upos = 0; 158133640Sfjoe DPRINTF(("%s: done: start_blk %d, pos %lld, upos %lld (%lld, %d, %d)\n", 159133640Sfjoe gp->name, start_blk, pos, upos, 160133640Sfjoe bp2->bio_offset, sc->blksz, bsize)); 161133640Sfjoe for (i = start_blk; upos < bp2->bio_length; i++) { 162133640Sfjoe off_t len, ulen, uoff; 163133640Sfjoe 164133640Sfjoe uoff = i == start_blk ? bp2->bio_offset % sc->blksz : 0; 165133640Sfjoe ulen = MIN(sc->blksz - uoff, bp2->bio_length - upos); 166133640Sfjoe len = sc->offsets[i + 1] - sc->offsets[i]; 167133640Sfjoe 168168999Ssimokawa if (len == 0) { 169168999Ssimokawa /* All zero block: no cache update */ 170168999Ssimokawa bzero(bp2->bio_data + upos, ulen); 171168999Ssimokawa upos += ulen; 172168999Ssimokawa bp2->bio_completed += ulen; 173168999Ssimokawa continue; 174168999Ssimokawa } 175133640Sfjoe zs.next_in = bp->bio_data + pos; 176133640Sfjoe zs.avail_in = len; 177133640Sfjoe zs.next_out = sc->last_buf; 178133640Sfjoe zs.avail_out = sc->blksz; 179133640Sfjoe mtx_lock(&sc->last_mtx); 180133640Sfjoe err = inflate(&zs, Z_FINISH); 181133640Sfjoe if (err != Z_STREAM_END) { 182133640Sfjoe sc->last_blk = -1; 183133640Sfjoe mtx_unlock(&sc->last_mtx); 184133640Sfjoe DPRINTF(("%s: done: inflate failed (%lld + %lld -> %lld + %lld + %lld)\n", 185133640Sfjoe gp->name, pos, len, uoff, upos, ulen)); 186133640Sfjoe inflateEnd(&zs); 187133640Sfjoe bp2->bio_error = EIO; 188133640Sfjoe goto done; 189133640Sfjoe } 190133640Sfjoe sc->last_blk = i; 191133640Sfjoe DPRINTF(("%s: done: inflated %lld + %lld -> %lld + %lld + %lld\n", 192133640Sfjoe gp->name, 193133640Sfjoe pos, len, 194133640Sfjoe uoff, upos, ulen)); 195133640Sfjoe memcpy(bp2->bio_data + upos, sc->last_buf + uoff, ulen); 196133640Sfjoe mtx_unlock(&sc->last_mtx); 197133640Sfjoe 198133640Sfjoe pos += len; 199133640Sfjoe upos += ulen; 200133640Sfjoe bp2->bio_completed += ulen; 201133640Sfjoe err = inflateReset(&zs); 202133640Sfjoe if (err != Z_OK) { 203133640Sfjoe inflateEnd(&zs); 204133640Sfjoe bp2->bio_error = EIO; 205133640Sfjoe goto done; 206133640Sfjoe } 207133640Sfjoe } 208133640Sfjoe err = inflateEnd(&zs); 209133640Sfjoe if (err != Z_OK) { 210133640Sfjoe bp2->bio_error = EIO; 211133640Sfjoe goto done; 212133640Sfjoe } 213133640Sfjoe 214133640Sfjoedone: 215133640Sfjoe /* 216133640Sfjoe * Finish processing the request. 217133640Sfjoe */ 218133640Sfjoe DPRINTF(("%s: done: (%d, %lld, %ld)\n", 219133640Sfjoe gp->name, bp2->bio_error, bp2->bio_completed, bp2->bio_resid)); 220133640Sfjoe free(bp->bio_data, M_GEOM_UZIP); 221133640Sfjoe g_destroy_bio(bp); 222133640Sfjoe g_io_deliver(bp2, bp2->bio_error); 223133640Sfjoe} 224133640Sfjoe 225133640Sfjoestatic void 226133640Sfjoeg_uzip_start(struct bio *bp) 227133640Sfjoe{ 228133640Sfjoe struct bio *bp2; 229133640Sfjoe struct g_provider *pp, *pp2; 230133640Sfjoe struct g_geom *gp; 231133640Sfjoe struct g_consumer *cp; 232133640Sfjoe struct g_uzip_softc *sc; 233133640Sfjoe uint32_t start_blk, end_blk; 234133640Sfjoe size_t bsize; 235133640Sfjoe 236133640Sfjoe pp = bp->bio_to; 237133640Sfjoe gp = pp->geom; 238133640Sfjoe DPRINTF(("%s: start (%d)\n", gp->name, bp->bio_cmd)); 239133640Sfjoe 240133640Sfjoe if (bp->bio_cmd != BIO_READ) { 241133640Sfjoe g_io_deliver(bp, EOPNOTSUPP); 242133640Sfjoe return; 243133640Sfjoe } 244133640Sfjoe 245133640Sfjoe cp = LIST_FIRST(&gp->consumer); 246133640Sfjoe pp2 = cp->provider; 247133640Sfjoe sc = gp->softc; 248133640Sfjoe 249133640Sfjoe start_blk = bp->bio_offset / sc->blksz; 250133640Sfjoe end_blk = (bp->bio_offset + bp->bio_length + sc->blksz - 1) / sc->blksz; 251150735Sfjoe KASSERT(start_blk < sc->nblocks, 252133640Sfjoe ("start_blk out of range")); 253150735Sfjoe KASSERT(end_blk <= sc->nblocks, 254133640Sfjoe ("end_blk out of range")); 255133640Sfjoe 256133640Sfjoe sc->req_total++; 257133640Sfjoe if (start_blk + 1 == end_blk) { 258133640Sfjoe mtx_lock(&sc->last_mtx); 259133640Sfjoe if (start_blk == sc->last_blk) { 260133640Sfjoe off_t uoff; 261133640Sfjoe 262133640Sfjoe uoff = bp->bio_offset % sc->blksz; 263133640Sfjoe KASSERT(bp->bio_length <= sc->blksz - uoff, 264133640Sfjoe ("cached data error")); 265133640Sfjoe memcpy(bp->bio_data, sc->last_buf + uoff, 266133640Sfjoe bp->bio_length); 267133640Sfjoe sc->req_cached++; 268133640Sfjoe mtx_unlock(&sc->last_mtx); 269133640Sfjoe 270133640Sfjoe DPRINTF(("%s: start: cached 0 + %lld, %lld + 0 + %lld\n", 271133640Sfjoe gp->name, bp->bio_length, uoff, bp->bio_length)); 272133640Sfjoe bp->bio_completed = bp->bio_length; 273133640Sfjoe g_io_deliver(bp, 0); 274133640Sfjoe return; 275133640Sfjoe } 276133640Sfjoe mtx_unlock(&sc->last_mtx); 277133640Sfjoe } 278133640Sfjoe 279133640Sfjoe bp2 = g_clone_bio(bp); 280133640Sfjoe if (bp2 == NULL) { 281133640Sfjoe g_io_deliver(bp, ENOMEM); 282133640Sfjoe return; 283133640Sfjoe } 284133640Sfjoe bp2->bio_done = g_uzip_done; 285133640Sfjoe DPRINTF(("%s: start (%d..%d), %s: %d + %lld, %s: %d + %lld\n", 286133640Sfjoe gp->name, start_blk, end_blk, 287133640Sfjoe pp->name, pp->sectorsize, pp->mediasize, 288133640Sfjoe pp2->name, pp2->sectorsize, pp2->mediasize)); 289133640Sfjoe bsize = pp2->sectorsize; 290133640Sfjoe bp2->bio_offset = sc->offsets[start_blk] - sc->offsets[start_blk] % bsize; 291133640Sfjoe bp2->bio_length = sc->offsets[end_blk] - bp2->bio_offset; 292133640Sfjoe bp2->bio_length = (bp2->bio_length + bsize - 1) / bsize * bsize; 293133640Sfjoe DPRINTF(("%s: start %lld + %lld -> %lld + %lld -> %lld + %lld\n", 294133640Sfjoe gp->name, 295133640Sfjoe bp->bio_offset, bp->bio_length, 296133640Sfjoe sc->offsets[start_blk], sc->offsets[end_blk] - sc->offsets[start_blk], 297133640Sfjoe bp2->bio_offset, bp2->bio_length)); 298133640Sfjoe bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UZIP, M_NOWAIT); 299133640Sfjoe if (bp2->bio_data == NULL) { 300154540Spjd g_destroy_bio(bp2); 301133640Sfjoe g_io_deliver(bp, ENOMEM); 302133640Sfjoe return; 303133640Sfjoe } 304133640Sfjoe 305133640Sfjoe g_io_request(bp2, cp); 306133640Sfjoe DPRINTF(("%s: start ok\n", gp->name)); 307133640Sfjoe} 308133640Sfjoe 309133640Sfjoestatic void 310133640Sfjoeg_uzip_orphan(struct g_consumer *cp) 311133640Sfjoe{ 312133640Sfjoe struct g_geom *gp; 313133640Sfjoe 314133640Sfjoe g_trace(G_T_TOPOLOGY, "g_uzip_orphan(%p/%s)", cp, cp->provider->name); 315133640Sfjoe g_topology_assert(); 316133640Sfjoe 317133640Sfjoe gp = cp->geom; 318133640Sfjoe g_uzip_softc_free(gp->softc, gp); 319133640Sfjoe gp->softc = NULL; 320249148Smav g_wither_geom(gp, ENXIO); 321133640Sfjoe} 322133640Sfjoe 323133640Sfjoestatic int 324133640Sfjoeg_uzip_access(struct g_provider *pp, int dr, int dw, int de) 325133640Sfjoe{ 326133640Sfjoe struct g_geom *gp; 327133640Sfjoe struct g_consumer *cp; 328133640Sfjoe 329133640Sfjoe gp = pp->geom; 330133640Sfjoe cp = LIST_FIRST(&gp->consumer); 331133640Sfjoe KASSERT (cp != NULL, ("g_uzip_access but no consumer")); 332133640Sfjoe 333150735Sfjoe if (cp->acw + dw > 0) 334150735Sfjoe return EROFS; 335150735Sfjoe 336133640Sfjoe return (g_access(cp, dr, dw, de)); 337133640Sfjoe} 338133640Sfjoe 339133640Sfjoestatic void 340133640Sfjoeg_uzip_spoiled(struct g_consumer *cp) 341133640Sfjoe{ 342133640Sfjoe struct g_geom *gp; 343133640Sfjoe 344133640Sfjoe gp = cp->geom; 345133640Sfjoe g_trace(G_T_TOPOLOGY, "g_uzip_spoiled(%p/%s)", cp, gp->name); 346133640Sfjoe g_topology_assert(); 347133640Sfjoe 348133640Sfjoe g_uzip_softc_free(gp->softc, gp); 349133640Sfjoe gp->softc = NULL; 350133640Sfjoe g_wither_geom(gp, ENXIO); 351133640Sfjoe} 352133640Sfjoe 353133640Sfjoestatic struct g_geom * 354133640Sfjoeg_uzip_taste(struct g_class *mp, struct g_provider *pp, int flags) 355133640Sfjoe{ 356133640Sfjoe int error; 357133640Sfjoe uint32_t i, total_offsets, offsets_read, blk; 358133640Sfjoe void *buf; 359133640Sfjoe struct cloop_header *header; 360133640Sfjoe struct g_consumer *cp; 361133640Sfjoe struct g_geom *gp; 362133640Sfjoe struct g_provider *pp2; 363133640Sfjoe struct g_uzip_softc *sc; 364133640Sfjoe 365133640Sfjoe g_trace(G_T_TOPOLOGY, "g_uzip_taste(%s,%s)", mp->name, pp->name); 366133640Sfjoe g_topology_assert(); 367197898Spjd 368197898Spjd /* Skip providers that are already open for writing. */ 369197898Spjd if (pp->acw > 0) 370197898Spjd return (NULL); 371197898Spjd 372133640Sfjoe buf = NULL; 373133640Sfjoe 374133640Sfjoe /* 375133640Sfjoe * Create geom instance. 376133640Sfjoe */ 377133640Sfjoe gp = g_new_geomf(mp, "%s.uzip", pp->name); 378133640Sfjoe cp = g_new_consumer(gp); 379133640Sfjoe error = g_attach(cp, pp); 380133640Sfjoe if (error == 0) 381133640Sfjoe error = g_access(cp, 1, 0, 0); 382133640Sfjoe if (error) { 383133640Sfjoe g_detach(cp); 384133640Sfjoe g_destroy_consumer(cp); 385133640Sfjoe g_destroy_geom(gp); 386133640Sfjoe return (NULL); 387133640Sfjoe } 388133640Sfjoe g_topology_unlock(); 389133640Sfjoe 390133640Sfjoe /* 391133640Sfjoe * Read cloop header, look for CLOOP magic, perform 392133640Sfjoe * other validity checks. 393133640Sfjoe */ 394133640Sfjoe DPRINTF(("%s: media sectorsize %u, mediasize %lld\n", 395133640Sfjoe gp->name, pp->sectorsize, pp->mediasize)); 396152971Ssobomax buf = g_read_data(cp, 0, pp->sectorsize, NULL); 397152967Ssobomax if (buf == NULL) 398133640Sfjoe goto err; 399133640Sfjoe header = (struct cloop_header *) buf; 400133640Sfjoe if (strncmp(header->magic, CLOOP_MAGIC_START, 401133640Sfjoe sizeof(CLOOP_MAGIC_START) - 1) != 0) { 402133640Sfjoe DPRINTF(("%s: no CLOOP magic\n", gp->name)); 403133640Sfjoe goto err; 404133640Sfjoe } 405133640Sfjoe if (header->magic[0x0b] != 'V' || header->magic[0x0c] < '2') { 406133640Sfjoe DPRINTF(("%s: image version too old\n", gp->name)); 407133640Sfjoe goto err; 408133640Sfjoe } 409133640Sfjoe 410133640Sfjoe /* 411133640Sfjoe * Initialize softc and read offsets. 412133640Sfjoe */ 413137936Sfjoe sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO); 414133640Sfjoe gp->softc = sc; 415133640Sfjoe sc->blksz = ntohl(header->blksz); 416133640Sfjoe sc->nblocks = ntohl(header->nblocks); 417133640Sfjoe if (sc->blksz % 512 != 0) { 418133640Sfjoe printf("%s: block size (%u) should be multiple of 512.\n", 419133640Sfjoe gp->name, sc->blksz); 420133640Sfjoe goto err; 421133640Sfjoe } 422133640Sfjoe if (sc->blksz > MAX_BLKSZ) { 423133640Sfjoe printf("%s: block size (%u) should not be larger than %d.\n", 424133640Sfjoe gp->name, sc->blksz, MAX_BLKSZ); 425133640Sfjoe } 426133640Sfjoe total_offsets = sc->nblocks + 1; 427133640Sfjoe if (sizeof(struct cloop_header) + 428133640Sfjoe total_offsets * sizeof(uint64_t) > pp->mediasize) { 429133640Sfjoe printf("%s: media too small for %u blocks\n", 430133640Sfjoe gp->name, sc->nblocks); 431133640Sfjoe goto err; 432133640Sfjoe } 433133640Sfjoe sc->offsets = malloc( 434133640Sfjoe total_offsets * sizeof(uint64_t), M_GEOM_UZIP, M_WAITOK); 435133640Sfjoe offsets_read = MIN(total_offsets, 436133640Sfjoe (pp->sectorsize - sizeof(*header)) / sizeof(uint64_t)); 437133640Sfjoe for (i = 0; i < offsets_read; i++) 438133640Sfjoe sc->offsets[i] = be64toh(((uint64_t *) (header + 1))[i]); 439133640Sfjoe DPRINTF(("%s: %u offsets in the first sector\n", 440133640Sfjoe gp->name, offsets_read)); 441133640Sfjoe for (blk = 1; offsets_read < total_offsets; blk++) { 442133640Sfjoe uint32_t nread; 443133640Sfjoe 444135461Sfjoe free(buf, M_GEOM); 445133640Sfjoe buf = g_read_data( 446152971Ssobomax cp, blk * pp->sectorsize, pp->sectorsize, NULL); 447152967Ssobomax if (buf == NULL) 448133640Sfjoe goto err; 449133640Sfjoe nread = MIN(total_offsets - offsets_read, 450133640Sfjoe pp->sectorsize / sizeof(uint64_t)); 451133640Sfjoe DPRINTF(("%s: %u offsets read from sector %d\n", 452133640Sfjoe gp->name, nread, blk)); 453133640Sfjoe for (i = 0; i < nread; i++) { 454133640Sfjoe sc->offsets[offsets_read + i] = 455133640Sfjoe be64toh(((uint64_t *) buf)[i]); 456133640Sfjoe } 457133640Sfjoe offsets_read += nread; 458133640Sfjoe } 459133640Sfjoe DPRINTF(("%s: done reading offsets\n", gp->name)); 460133640Sfjoe mtx_init(&sc->last_mtx, "geom_uzip cache", NULL, MTX_DEF); 461133640Sfjoe sc->last_blk = -1; 462133640Sfjoe sc->last_buf = malloc(sc->blksz, M_GEOM_UZIP, M_WAITOK); 463133640Sfjoe sc->req_total = 0; 464133640Sfjoe sc->req_cached = 0; 465133640Sfjoe 466133640Sfjoe g_topology_lock(); 467133640Sfjoe pp2 = g_new_providerf(gp, "%s", gp->name); 468133640Sfjoe pp2->sectorsize = 512; 469168445Ssimokawa pp2->mediasize = (off_t)sc->nblocks * sc->blksz; 470133640Sfjoe pp2->flags = pp->flags & G_PF_CANDELETE; 471201645Smav pp2->stripesize = pp->stripesize; 472201645Smav pp2->stripeoffset = pp->stripeoffset; 473133640Sfjoe g_error_provider(pp2, 0); 474133640Sfjoe g_access(cp, -1, 0, 0); 475133640Sfjoe 476133640Sfjoe DPRINTF(("%s: taste ok (%d, %lld), (%d, %d), %x\n", 477133640Sfjoe gp->name, 478133640Sfjoe pp2->sectorsize, pp2->mediasize, 479133640Sfjoe pp2->stripeoffset, pp2->stripesize, pp2->flags)); 480133640Sfjoe printf("%s: %u x %u blocks\n", 481133640Sfjoe gp->name, sc->nblocks, sc->blksz); 482133640Sfjoe return (gp); 483133640Sfjoe 484133640Sfjoeerr: 485133640Sfjoe g_topology_lock(); 486133640Sfjoe g_access(cp, -1, 0, 0); 487133640Sfjoe if (buf != NULL) 488135461Sfjoe free(buf, M_GEOM); 489133640Sfjoe if (gp->softc != NULL) { 490133640Sfjoe g_uzip_softc_free(gp->softc, NULL); 491133640Sfjoe gp->softc = NULL; 492133640Sfjoe } 493133640Sfjoe g_detach(cp); 494133640Sfjoe g_destroy_consumer(cp); 495133640Sfjoe g_destroy_geom(gp); 496133640Sfjoe return (NULL); 497133640Sfjoe} 498133640Sfjoe 499133640Sfjoestatic int 500133640Sfjoeg_uzip_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 501133640Sfjoe{ 502133640Sfjoe struct g_provider *pp; 503133640Sfjoe 504133640Sfjoe g_trace(G_T_TOPOLOGY, "g_uzip_destroy_geom(%s, %s)", mp->name, gp->name); 505133640Sfjoe g_topology_assert(); 506133640Sfjoe 507133640Sfjoe if (gp->softc == NULL) { 508133640Sfjoe printf("%s(%s): gp->softc == NULL\n", __func__, gp->name); 509133640Sfjoe return (ENXIO); 510133640Sfjoe } 511133640Sfjoe 512133640Sfjoe KASSERT(gp != NULL, ("NULL geom")); 513133640Sfjoe pp = LIST_FIRST(&gp->provider); 514133640Sfjoe KASSERT(pp != NULL, ("NULL provider")); 515133640Sfjoe if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) 516133640Sfjoe return (EBUSY); 517133640Sfjoe 518133640Sfjoe g_uzip_softc_free(gp->softc, gp); 519133640Sfjoe gp->softc = NULL; 520133640Sfjoe g_wither_geom(gp, ENXIO); 521133640Sfjoe return (0); 522133640Sfjoe} 523133640Sfjoe 524133640Sfjoestatic struct g_class g_uzip_class = { 525133640Sfjoe .name = UZIP_CLASS_NAME, 526133640Sfjoe .version = G_VERSION, 527133640Sfjoe .taste = g_uzip_taste, 528133640Sfjoe .destroy_geom = g_uzip_destroy_geom, 529133640Sfjoe 530133640Sfjoe .start = g_uzip_start, 531133640Sfjoe .orphan = g_uzip_orphan, 532133640Sfjoe .access = g_uzip_access, 533133640Sfjoe .spoiled = g_uzip_spoiled, 534133640Sfjoe}; 535133640Sfjoe 536154686SfjoeDECLARE_GEOM_CLASS(g_uzip_class, g_uzip); 537154686SfjoeMODULE_DEPEND(g_uzip, zlib, 1, 1, 1); 538