1229537Sray/*- 2229537Sray * Copyright (c) 2010-2012 Aleksandr Rybalko 3229537Sray * Copyright (c) 2004 Max Khon 4229537Sray * All rights reserved. 5229537Sray * 6229537Sray * Redistribution and use in source and binary forms, with or without 7229537Sray * modification, are permitted provided that the following conditions 8229537Sray * are met: 9229537Sray * 1. Redistributions of source code must retain the above copyright 10229537Sray * notice, this list of conditions and the following disclaimer. 11229537Sray * 2. Redistributions in binary form must reproduce the above copyright 12229537Sray * notice, this list of conditions and the following disclaimer in the 13229537Sray * documentation and/or other materials provided with the distribution. 14229537Sray * 15229537Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16229537Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17229537Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18229537Sray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19229537Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20229537Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21229537Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22229537Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23229537Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24229537Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25229537Sray * SUCH DAMAGE. 26229537Sray */ 27229537Sray 28229537Sray/* 29229537Sray * GEOM UNCOMPRESS module - simple decompression module for use with read-only 30229537Sray * copressed images maked by mkuzip(8) or mkulzma(8) utilities. 31229537Sray * 32229537Sray * To enable module in kernel config, put this line: 33229537Sray * device geom_uncompress 34229537Sray */ 35229537Sray 36229537Sray#include <sys/cdefs.h> 37229537Sray__FBSDID("$FreeBSD: releng/10.2/sys/geom/uncompress/g_uncompress.c 283900 2015-06-02 02:05:32Z ae $"); 38229537Sray 39229537Sray#include <sys/param.h> 40229537Sray#include <sys/bio.h> 41229537Sray#include <sys/endian.h> 42229537Sray#include <sys/errno.h> 43229537Sray#include <sys/kernel.h> 44229537Sray#include <sys/lock.h> 45229537Sray#include <sys/mutex.h> 46229537Sray#include <sys/malloc.h> 47229537Sray#include <sys/systm.h> 48229537Sray 49229537Sray#include <geom/geom.h> 50229537Sray 51229537Sray#include <net/zlib.h> 52229537Sray#include <contrib/xz-embedded/linux/include/linux/xz.h> 53229537Sray 54229537Sray#ifdef GEOM_UNCOMPRESS_DEBUG 55229537Sray#define DPRINTF(a) printf a 56229537Srayextern int g_debugflags; 57229537Sray#else 58229537Sray#define DPRINTF(a) 59229537Sray#endif 60229537Sray 61229537Sraystatic MALLOC_DEFINE(M_GEOM_UNCOMPRESS, "geom_uncompress", 62229537Sray "GEOM UNCOMPRESS data structures"); 63229537Sray 64229537Sray#define UNCOMPRESS_CLASS_NAME "UNCOMPRESS" 65229537Sray#define GEOM_UZIP_MAJVER '2' 66229537Sray#define GEOM_ULZMA_MAJVER '3' 67229537Sray 68229537Sray/* 69229537Sray * Maximum allowed valid block size (to prevent foot-shooting) 70229537Sray */ 71229537Sray#define MAX_BLKSZ (MAXPHYS) 72229537Sray 73229537Sray/* 74229537Sray * Integer values (block size, number of blocks, offsets) 75229537Sray * are stored in big-endian (network) order on disk and struct cloop_header 76229537Sray * and in native order in struct g_uncompress_softc 77229537Sray */ 78229537Sray 79229537Sray#define CLOOP_MAGIC_LEN 128 80229537Sraystatic char CLOOP_MAGIC_START[] = "#!/bin/sh\n"; 81229537Sray 82229537Sraystruct cloop_header { 83229537Sray char magic[CLOOP_MAGIC_LEN]; /* cloop magic */ 84229537Sray uint32_t blksz; /* block size */ 85229537Sray uint32_t nblocks; /* number of blocks */ 86229537Sray}; 87229537Sray 88229537Sraystruct g_uncompress_softc { 89229537Sray uint32_t blksz; /* block size */ 90229537Sray uint32_t nblocks; /* number of blocks */ 91229537Sray uint64_t *offsets; 92229537Sray enum { 93229537Sray GEOM_UZIP = 1, 94229537Sray GEOM_ULZMA 95229537Sray } type; 96229537Sray 97229537Sray struct mtx last_mtx; 98229537Sray uint32_t last_blk; /* last blk no */ 99229537Sray char *last_buf; /* last blk data */ 100229537Sray int req_total; /* total requests */ 101229537Sray int req_cached; /* cached requests */ 102229537Sray 103229537Sray /* XZ decoder structs */ 104229537Sray struct xz_buf *b; 105229537Sray struct xz_dec *s; 106229537Sray z_stream *zs; 107229537Sray}; 108229537Sray 109229537Sraystatic void 110229537Srayg_uncompress_softc_free(struct g_uncompress_softc *sc, struct g_geom *gp) 111229537Sray{ 112229537Sray 113229537Sray if (gp != NULL) { 114229537Sray printf("%s: %d requests, %d cached\n", 115229537Sray gp->name, sc->req_total, sc->req_cached); 116229537Sray } 117229537Sray if (sc->offsets != NULL) { 118229537Sray free(sc->offsets, M_GEOM_UNCOMPRESS); 119266220Sloos sc->offsets = NULL; 120229537Sray } 121229537Sray 122229537Sray switch (sc->type) { 123229537Sray case GEOM_ULZMA: 124229537Sray if (sc->b) { 125229537Sray free(sc->b, M_GEOM_UNCOMPRESS); 126229537Sray sc->b = 0; 127229537Sray } 128229537Sray if (sc->s) { 129229537Sray xz_dec_end(sc->s); 130229537Sray sc->s = 0; 131229537Sray } 132229537Sray break; 133229537Sray case GEOM_UZIP: 134229537Sray if (sc->zs) { 135229537Sray inflateEnd(sc->zs); 136229537Sray free(sc->zs, M_GEOM_UNCOMPRESS); 137229537Sray sc->zs = 0; 138229537Sray } 139229537Sray break; 140229537Sray } 141229537Sray 142229537Sray mtx_destroy(&sc->last_mtx); 143229537Sray free(sc->last_buf, M_GEOM_UNCOMPRESS); 144229537Sray free(sc, M_GEOM_UNCOMPRESS); 145229537Sray} 146229537Sray 147229537Sraystatic void * 148229537Srayz_alloc(void *nil, u_int type, u_int size) 149229537Sray{ 150229537Sray void *ptr; 151229537Sray 152229537Sray ptr = malloc(type * size, M_GEOM_UNCOMPRESS, M_NOWAIT); 153266220Sloos 154229537Sray return (ptr); 155229537Sray} 156229537Sray 157229537Sraystatic void 158229537Srayz_free(void *nil, void *ptr) 159229537Sray{ 160229537Sray 161229537Sray free(ptr, M_GEOM_UNCOMPRESS); 162229537Sray} 163229537Sray 164229537Sraystatic void 165229537Srayg_uncompress_done(struct bio *bp) 166229537Sray{ 167229537Sray struct g_uncompress_softc *sc; 168229537Sray struct g_provider *pp, *pp2; 169229537Sray struct g_consumer *cp; 170229537Sray struct g_geom *gp; 171229537Sray struct bio *bp2; 172229537Sray uint32_t start_blk, i; 173266220Sloos off_t iolen, pos, upos; 174229537Sray size_t bsize; 175229537Sray int err; 176229537Sray 177229537Sray err = 0; 178229537Sray bp2 = bp->bio_parent; 179229537Sray pp = bp2->bio_to; 180229537Sray gp = pp->geom; 181229537Sray cp = LIST_FIRST(&gp->consumer); 182229537Sray pp2 = cp->provider; 183229537Sray sc = gp->softc; 184229537Sray DPRINTF(("%s: done\n", gp->name)); 185229537Sray 186229537Sray bp2->bio_error = bp->bio_error; 187229537Sray if (bp2->bio_error != 0) 188229537Sray goto done; 189229537Sray 190229537Sray /* 191229537Sray * Example: 192229537Sray * Uncompressed block size = 65536 193229537Sray * User request: 65540-261632 194229537Sray * (4 uncompressed blocks -4B at start, -512B at end) 195229537Sray * 196229537Sray * We have 512(secsize)*63(nsec) = 32256B at offset 1024 197229537Sray * From: 1024 bp->bio_offset = 1024 198229537Sray * To: 33280 bp->bio_length = 33280 - 1024 = 32256 199229537Sray * Compressed blocks: 0-1020, 1020-1080, 1080-4845, 4845-12444, 200229537Sray * 12444-33210, 33210-44100, ... 201229537Sray * 202229537Sray * Get start_blk from user request: 203229537Sray * start_blk = bp2->bio_offset / 65536 = 65540/65536 = 1 204229537Sray * bsize (block size of parent) = pp2->sectorsize (Now is 4B) 205229537Sray * pos(in stream from parent) = sc->offsets[start_blk] % bsize = 206229537Sray * = sc->offsets[1] % 4 = 1020 % 4 = 0 207229537Sray */ 208229537Sray 209229537Sray /* 210229537Sray * Uncompress data. 211229537Sray */ 212229537Sray start_blk = bp2->bio_offset / sc->blksz; 213229537Sray bsize = pp2->sectorsize; 214266220Sloos iolen = bp->bio_completed; 215229537Sray pos = sc->offsets[start_blk] % bsize; 216229537Sray upos = 0; 217229537Sray 218266220Sloos DPRINTF(("%s: done: bio_length %jd bio_completed %jd start_blk %d, " 219266220Sloos "pos %jd, upos %jd (%jd, %d, %zu)\n", 220266220Sloos gp->name, (intmax_t)bp->bio_length, (intmax_t)bp->bio_completed, 221266220Sloos start_blk, (intmax_t)pos, (intmax_t)upos, 222266220Sloos (intmax_t)bp2->bio_offset, sc->blksz, bsize)); 223229537Sray 224229537Sray for (i = start_blk; upos < bp2->bio_length; i++) { 225266220Sloos off_t len, ulen, uoff; 226229537Sray 227229537Sray uoff = i == start_blk ? bp2->bio_offset % sc->blksz : 0; 228229537Sray ulen = MIN(sc->blksz - uoff, bp2->bio_length - upos); 229266220Sloos len = sc->offsets[i + 1] - sc->offsets[i]; 230229537Sray 231266220Sloos DPRINTF(( 232266220Sloos "%s: done: inflate block %d, start %ju, end %ju len %jd\n", 233266220Sloos gp->name, i, (uintmax_t)sc->offsets[i], 234266220Sloos (uintmax_t)sc->offsets[i + 1], (intmax_t)len)); 235229537Sray 236229537Sray if (len == 0) { 237229537Sray /* All zero block: no cache update */ 238229537Sray bzero(bp2->bio_data + upos, ulen); 239229537Sray upos += ulen; 240229537Sray bp2->bio_completed += ulen; 241229537Sray continue; 242229537Sray } 243266220Sloos if (len > iolen) { 244266220Sloos DPRINTF(("%s: done: early termination: len (%jd) > " 245266220Sloos "iolen (%jd)\n", 246266220Sloos gp->name, (intmax_t)len, (intmax_t)iolen)); 247266220Sloos break; 248266220Sloos } 249229537Sray mtx_lock(&sc->last_mtx); 250229537Sray 251229537Sray#ifdef GEOM_UNCOMPRESS_DEBUG 252229537Sray if (g_debugflags & 32) 253266220Sloos hexdump(bp->bio_data + pos, len, 0, 0); 254229537Sray#endif 255229537Sray 256229537Sray switch (sc->type) { 257229537Sray case GEOM_ULZMA: 258229537Sray sc->b->in = bp->bio_data + pos; 259229537Sray sc->b->out = sc->last_buf; 260229537Sray sc->b->in_pos = sc->b->out_pos = 0; 261266220Sloos sc->b->in_size = len; 262229537Sray sc->b->out_size = (size_t)-1; 263229537Sray 264229537Sray err = (xz_dec_run(sc->s, sc->b) != XZ_STREAM_END) ? 265229537Sray 1 : 0; 266229537Sray /* TODO decoder recovery, if needed */ 267229537Sray break; 268229537Sray case GEOM_UZIP: 269229537Sray sc->zs->next_in = bp->bio_data + pos; 270266220Sloos sc->zs->avail_in = len; 271229537Sray sc->zs->next_out = sc->last_buf; 272229537Sray sc->zs->avail_out = sc->blksz; 273229537Sray 274229537Sray err = (inflate(sc->zs, Z_FINISH) != Z_STREAM_END) ? 275229537Sray 1 : 0; 276266220Sloos if ((err) || (inflateReset(sc->zs) != Z_OK)) 277229537Sray printf("%s: UZIP decoder reset failed\n", 278229537Sray gp->name); 279229537Sray break; 280229537Sray } 281229537Sray 282229537Sray if (err) { 283229537Sray sc->last_blk = -1; 284229537Sray mtx_unlock(&sc->last_mtx); 285229537Sray DPRINTF(("%s: done: inflate failed, code=%d\n", 286229537Sray gp->name, err)); 287229537Sray bp2->bio_error = EIO; 288229537Sray goto done; 289229537Sray } 290229537Sray 291229537Sray#ifdef GEOM_UNCOMPRESS_DEBUG 292229537Sray if (g_debugflags & 32) 293229537Sray hexdump(sc->last_buf, sc->b->out_size, 0, 0); 294229537Sray#endif 295229537Sray 296229537Sray sc->last_blk = i; 297229537Sray DPRINTF(("%s: done: inflated \n", gp->name)); 298229537Sray memcpy(bp2->bio_data + upos, sc->last_buf + uoff, ulen); 299229537Sray mtx_unlock(&sc->last_mtx); 300229537Sray 301229537Sray pos += len; 302266220Sloos iolen -= len; 303229537Sray upos += ulen; 304229537Sray bp2->bio_completed += ulen; 305229537Sray } 306229537Sray 307229537Sraydone: 308229537Sray /* 309229537Sray * Finish processing the request. 310229537Sray */ 311266220Sloos DPRINTF(("%s: done: (%d, %jd, %ld)\n", 312266220Sloos gp->name, bp2->bio_error, (intmax_t)bp2->bio_completed, 313266220Sloos bp2->bio_resid)); 314229537Sray free(bp->bio_data, M_GEOM_UNCOMPRESS); 315229537Sray g_destroy_bio(bp); 316229537Sray g_io_deliver(bp2, bp2->bio_error); 317229537Sray} 318229537Sray 319229537Sraystatic void 320229537Srayg_uncompress_start(struct bio *bp) 321229537Sray{ 322229537Sray struct g_uncompress_softc *sc; 323229537Sray struct g_provider *pp, *pp2; 324229537Sray struct g_consumer *cp; 325229537Sray struct g_geom *gp; 326229537Sray struct bio *bp2; 327229537Sray uint32_t start_blk, end_blk; 328229537Sray size_t bsize; 329229537Sray 330229537Sray pp = bp->bio_to; 331229537Sray gp = pp->geom; 332266220Sloos DPRINTF(("%s: start (%d:%s) to %s off=%jd len=%jd\n", 333266220Sloos gp->name, bp->bio_cmd, 334266220Sloos (bp->bio_cmd == BIO_READ) ? "BIO_READ" : "NOTSUPPORTED", 335266220Sloos pp->name, (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length)); 336229537Sray 337229537Sray if (bp->bio_cmd != BIO_READ) { 338229537Sray g_io_deliver(bp, EOPNOTSUPP); 339229537Sray return; 340229537Sray } 341229537Sray 342229537Sray cp = LIST_FIRST(&gp->consumer); 343229537Sray pp2 = cp->provider; 344229537Sray sc = gp->softc; 345229537Sray 346229537Sray start_blk = bp->bio_offset / sc->blksz; 347229537Sray end_blk = howmany(bp->bio_offset + bp->bio_length, sc->blksz); 348266220Sloos KASSERT(start_blk < sc->nblocks, ("start_blk out of range")); 349266220Sloos KASSERT(end_blk <= sc->nblocks, ("end_blk out of range")); 350229537Sray 351229537Sray sc->req_total++; 352229537Sray if (start_blk + 1 == end_blk) { 353229537Sray mtx_lock(&sc->last_mtx); 354229537Sray if (start_blk == sc->last_blk) { 355229537Sray off_t uoff; 356229537Sray 357229537Sray uoff = bp->bio_offset % sc->blksz; 358229537Sray KASSERT(bp->bio_length <= sc->blksz - uoff, 359229537Sray ("cached data error")); 360229537Sray memcpy(bp->bio_data, sc->last_buf + uoff, 361229537Sray bp->bio_length); 362229537Sray sc->req_cached++; 363229537Sray mtx_unlock(&sc->last_mtx); 364229537Sray 365266220Sloos DPRINTF(("%s: start: cached 0 + %jd, %jd + 0 + %jd\n", 366266220Sloos gp->name, (intmax_t)bp->bio_length, (intmax_t)uoff, 367266220Sloos (intmax_t)bp->bio_length)); 368229537Sray bp->bio_completed = bp->bio_length; 369229537Sray g_io_deliver(bp, 0); 370229537Sray return; 371229537Sray } 372229537Sray mtx_unlock(&sc->last_mtx); 373229537Sray } 374229537Sray 375229537Sray bp2 = g_clone_bio(bp); 376229537Sray if (bp2 == NULL) { 377229537Sray g_io_deliver(bp, ENOMEM); 378229537Sray return; 379229537Sray } 380266220Sloos DPRINTF(("%s: start (%d..%d), %s: %d + %jd, %s: %d + %jd\n", 381229537Sray gp->name, start_blk, end_blk, 382266220Sloos pp->name, pp->sectorsize, (intmax_t)pp->mediasize, 383266220Sloos pp2->name, pp2->sectorsize, (intmax_t)pp2->mediasize)); 384229537Sray bsize = pp2->sectorsize; 385229537Sray bp2->bio_done = g_uncompress_done; 386266220Sloos bp2->bio_offset = rounddown(sc->offsets[start_blk], bsize); 387266220Sloos while (1) { 388266220Sloos bp2->bio_length = roundup(sc->offsets[end_blk], bsize) - 389266220Sloos bp2->bio_offset; 390266220Sloos if (bp2->bio_length < MAXPHYS) 391266220Sloos break; 392229537Sray 393266220Sloos end_blk--; 394266220Sloos DPRINTF(( 395266220Sloos "%s: bio_length (%jd) > MAXPHYS: lowering end_blk to %u\n", 396266220Sloos gp->name, (intmax_t)bp2->bio_length, end_blk)); 397266220Sloos } 398266220Sloos DPRINTF(("%s: start %jd + %jd -> %ju + %ju -> %jd + %jd\n", 399229537Sray gp->name, 400266220Sloos (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length, 401266220Sloos (uintmax_t)sc->offsets[start_blk], 402266220Sloos (uintmax_t)sc->offsets[end_blk] - sc->offsets[start_blk], 403266220Sloos (intmax_t)bp2->bio_offset, (intmax_t)bp2->bio_length)); 404266220Sloos bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UNCOMPRESS, M_NOWAIT); 405229537Sray if (bp2->bio_data == NULL) { 406229537Sray g_destroy_bio(bp2); 407229537Sray g_io_deliver(bp, ENOMEM); 408229537Sray return; 409229537Sray } 410229537Sray 411229537Sray g_io_request(bp2, cp); 412229537Sray DPRINTF(("%s: start ok\n", gp->name)); 413229537Sray} 414229537Sray 415229537Sraystatic void 416229537Srayg_uncompress_orphan(struct g_consumer *cp) 417229537Sray{ 418229537Sray struct g_geom *gp; 419229537Sray 420266220Sloos g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->provider->name); 421229537Sray g_topology_assert(); 422229537Sray 423229537Sray gp = cp->geom; 424229537Sray g_uncompress_softc_free(gp->softc, gp); 425229537Sray gp->softc = NULL; 426238198Strasz g_wither_geom(gp, ENXIO); 427229537Sray} 428229537Sray 429229537Sraystatic int 430229537Srayg_uncompress_access(struct g_provider *pp, int dr, int dw, int de) 431229537Sray{ 432229537Sray struct g_consumer *cp; 433229537Sray struct g_geom *gp; 434229537Sray 435229537Sray gp = pp->geom; 436229537Sray cp = LIST_FIRST(&gp->consumer); 437229537Sray KASSERT (cp != NULL, ("g_uncompress_access but no consumer")); 438229537Sray 439229537Sray if (cp->acw + dw > 0) 440229537Sray return (EROFS); 441229537Sray 442229537Sray return (g_access(cp, dr, dw, de)); 443229537Sray} 444229537Sray 445229537Sraystatic void 446229537Srayg_uncompress_spoiled(struct g_consumer *cp) 447229537Sray{ 448229537Sray struct g_geom *gp; 449229537Sray 450229537Sray gp = cp->geom; 451229537Sray g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name); 452229537Sray g_topology_assert(); 453229537Sray 454229537Sray g_uncompress_softc_free(gp->softc, gp); 455229537Sray gp->softc = NULL; 456229537Sray g_wither_geom(gp, ENXIO); 457229537Sray} 458229537Sray 459229537Sraystatic struct g_geom * 460229537Srayg_uncompress_taste(struct g_class *mp, struct g_provider *pp, int flags) 461229537Sray{ 462229537Sray struct cloop_header *header; 463229537Sray struct g_uncompress_softc *sc; 464229537Sray struct g_provider *pp2; 465229537Sray struct g_consumer *cp; 466229537Sray struct g_geom *gp; 467283900Sae uint64_t *offsets; 468283900Sae uint32_t i, r, total, total_offsets, type; 469229537Sray uint8_t *buf; 470229537Sray int error; 471229537Sray 472229537Sray g_trace(G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name); 473229537Sray g_topology_assert(); 474229537Sray 475229537Sray /* Skip providers that are already open for writing. */ 476229537Sray if (pp->acw > 0) 477229537Sray return (NULL); 478229537Sray 479229537Sray buf = NULL; 480229537Sray 481229537Sray /* 482229537Sray * Create geom instance. 483229537Sray */ 484229537Sray gp = g_new_geomf(mp, "%s.uncompress", pp->name); 485229537Sray cp = g_new_consumer(gp); 486229537Sray error = g_attach(cp, pp); 487229537Sray if (error == 0) 488229537Sray error = g_access(cp, 1, 0, 0); 489229537Sray if (error) { 490229537Sray g_detach(cp); 491229537Sray g_destroy_consumer(cp); 492229537Sray g_destroy_geom(gp); 493229537Sray return (NULL); 494229537Sray } 495229537Sray g_topology_unlock(); 496229537Sray 497229537Sray /* 498229537Sray * Read cloop header, look for CLOOP magic, perform 499229537Sray * other validity checks. 500229537Sray */ 501266220Sloos DPRINTF(("%s: media sectorsize %u, mediasize %jd\n", 502266220Sloos gp->name, pp->sectorsize, (intmax_t)pp->mediasize)); 503283900Sae total = roundup(sizeof(struct cloop_header), pp->sectorsize); 504283900Sae buf = g_read_data(cp, 0, total, NULL); 505229537Sray if (buf == NULL) 506229537Sray goto err; 507229537Sray header = (struct cloop_header *) buf; 508229537Sray if (strncmp(header->magic, CLOOP_MAGIC_START, 509266220Sloos sizeof(CLOOP_MAGIC_START) - 1) != 0) { 510229537Sray DPRINTF(("%s: no CLOOP magic\n", gp->name)); 511229537Sray goto err; 512229537Sray } 513229537Sray 514229537Sray switch (header->magic[0x0b]) { 515229537Sray case 'L': 516229537Sray type = GEOM_ULZMA; 517229537Sray if (header->magic[0x0c] < GEOM_ULZMA_MAJVER) { 518229537Sray DPRINTF(("%s: image version too old\n", gp->name)); 519229537Sray goto err; 520229537Sray } 521229537Sray printf("%s: GEOM_ULZMA image found\n", gp->name); 522229537Sray break; 523229537Sray case 'V': 524229537Sray type = GEOM_UZIP; 525229537Sray if (header->magic[0x0c] < GEOM_UZIP_MAJVER) { 526229537Sray DPRINTF(("%s: image version too old\n", gp->name)); 527229537Sray goto err; 528229537Sray } 529229537Sray printf("%s: GEOM_UZIP image found\n", gp->name); 530229537Sray break; 531229537Sray default: 532229537Sray DPRINTF(("%s: unsupported image type\n", gp->name)); 533229537Sray goto err; 534229537Sray } 535229537Sray 536229537Sray DPRINTF(("%s: found CLOOP magic\n", gp->name)); 537229537Sray /* 538229537Sray * Initialize softc and read offsets. 539229537Sray */ 540229537Sray sc = malloc(sizeof(*sc), M_GEOM_UNCOMPRESS, M_WAITOK | M_ZERO); 541229537Sray gp->softc = sc; 542229537Sray sc->type = type; 543229537Sray sc->blksz = ntohl(header->blksz); 544229537Sray sc->nblocks = ntohl(header->nblocks); 545229537Sray if (sc->blksz % 4 != 0) { 546229537Sray printf("%s: block size (%u) should be multiple of 4.\n", 547229537Sray gp->name, sc->blksz); 548229537Sray goto err; 549229537Sray } 550229537Sray if (sc->blksz > MAX_BLKSZ) { 551229537Sray printf("%s: block size (%u) should not be larger than %d.\n", 552229537Sray gp->name, sc->blksz, MAX_BLKSZ); 553229537Sray } 554229537Sray total_offsets = sc->nblocks + 1; 555229537Sray if (sizeof(struct cloop_header) + 556229537Sray total_offsets * sizeof(uint64_t) > pp->mediasize) { 557229537Sray printf("%s: media too small for %u blocks\n", 558229537Sray gp->name, sc->nblocks); 559229537Sray goto err; 560229537Sray } 561283900Sae g_free(buf); 562229537Sray 563283900Sae sc->offsets = malloc(total_offsets * sizeof(uint64_t), 564283900Sae M_GEOM_UNCOMPRESS, M_WAITOK | M_ZERO); 565283900Sae total = roundup((sizeof(struct cloop_header) + 566266220Sloos total_offsets * sizeof(uint64_t)), pp->sectorsize); 567283900Sae#define RSZ ((total - r) > MAXPHYS ? MAXPHYS: (total - r)) 568283900Sae for (r = 0, i = 0; r < total; r += MAXPHYS) { 569283900Sae buf = g_read_data(cp, r, RSZ, &error); 570283900Sae if (buf == NULL) { 571283900Sae free(sc->offsets, M_GEOM_UNCOMPRESS); 572283900Sae goto err; 573283900Sae } 574283900Sae offsets = (uint64_t *)buf; 575283900Sae if (r == 0) 576283900Sae offsets += 577283900Sae sizeof(struct cloop_header) / sizeof(uint64_t); 578283900Sae for (; i < total_offsets && offsets < (uint64_t *)(buf + RSZ); 579283900Sae i++, offsets++) 580283900Sae sc->offsets[i] = be64toh(*offsets); 581283900Sae g_free(buf); 582229537Sray } 583283900Sae#undef RSZ 584282094Spfg buf = NULL; 585229537Sray DPRINTF(("%s: done reading offsets\n", gp->name)); 586229537Sray mtx_init(&sc->last_mtx, "geom_uncompress cache", NULL, MTX_DEF); 587229537Sray sc->last_blk = -1; 588229537Sray sc->last_buf = malloc(sc->blksz, M_GEOM_UNCOMPRESS, M_WAITOK); 589229537Sray sc->req_total = 0; 590229537Sray sc->req_cached = 0; 591229537Sray 592229537Sray switch (sc->type) { 593229537Sray case GEOM_ULZMA: 594229537Sray xz_crc32_init(); 595229537Sray sc->s = xz_dec_init(XZ_SINGLE, 0); 596229537Sray sc->b = (struct xz_buf*)malloc(sizeof(struct xz_buf), 597229537Sray M_GEOM_UNCOMPRESS, M_WAITOK); 598229537Sray break; 599229537Sray case GEOM_UZIP: 600229537Sray sc->zs = (z_stream *)malloc(sizeof(z_stream), 601229537Sray M_GEOM_UNCOMPRESS, M_WAITOK); 602229537Sray sc->zs->zalloc = z_alloc; 603229537Sray sc->zs->zfree = z_free; 604229537Sray if (inflateInit(sc->zs) != Z_OK) { 605229537Sray goto err; 606229537Sray } 607229537Sray break; 608229537Sray } 609229537Sray 610229537Sray g_topology_lock(); 611229537Sray pp2 = g_new_providerf(gp, "%s", gp->name); 612229537Sray pp2->sectorsize = 512; 613229537Sray pp2->mediasize = (off_t)sc->nblocks * sc->blksz; 614229537Sray if (pp->stripesize > 0) { 615229537Sray pp2->stripesize = pp->stripesize; 616229537Sray pp2->stripeoffset = pp->stripeoffset; 617229537Sray } 618229537Sray g_error_provider(pp2, 0); 619229537Sray g_access(cp, -1, 0, 0); 620229537Sray 621266220Sloos DPRINTF(("%s: taste ok (%d, %jd), (%d, %d), %x\n", 622229537Sray gp->name, 623266220Sloos pp2->sectorsize, (intmax_t)pp2->mediasize, 624229537Sray pp2->stripeoffset, pp2->stripesize, pp2->flags)); 625266220Sloos printf("%s: %u x %u blocks\n", gp->name, sc->nblocks, sc->blksz); 626229537Sray return (gp); 627229537Sray 628229537Srayerr: 629229537Sray g_topology_lock(); 630229537Sray g_access(cp, -1, 0, 0); 631229537Sray if (buf != NULL) 632283900Sae g_free(buf); 633229537Sray if (gp->softc != NULL) { 634229537Sray g_uncompress_softc_free(gp->softc, NULL); 635229537Sray gp->softc = NULL; 636229537Sray } 637229537Sray g_detach(cp); 638229537Sray g_destroy_consumer(cp); 639229537Sray g_destroy_geom(gp); 640266220Sloos 641229537Sray return (NULL); 642229537Sray} 643229537Sray 644229537Sraystatic int 645229537Srayg_uncompress_destroy_geom(struct gctl_req *req, struct g_class *mp, 646229537Sray struct g_geom *gp) 647229537Sray{ 648229537Sray struct g_provider *pp; 649229537Sray 650229537Sray g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, gp->name); 651229537Sray g_topology_assert(); 652229537Sray 653229537Sray if (gp->softc == NULL) { 654229537Sray printf("%s(%s): gp->softc == NULL\n", __func__, gp->name); 655229537Sray return (ENXIO); 656229537Sray } 657229537Sray 658229537Sray KASSERT(gp != NULL, ("NULL geom")); 659229537Sray pp = LIST_FIRST(&gp->provider); 660229537Sray KASSERT(pp != NULL, ("NULL provider")); 661229537Sray if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) 662229537Sray return (EBUSY); 663229537Sray 664229537Sray g_uncompress_softc_free(gp->softc, gp); 665229537Sray gp->softc = NULL; 666229537Sray g_wither_geom(gp, ENXIO); 667266220Sloos 668229537Sray return (0); 669229537Sray} 670229537Sray 671229537Sraystatic struct g_class g_uncompress_class = { 672229537Sray .name = UNCOMPRESS_CLASS_NAME, 673229537Sray .version = G_VERSION, 674229537Sray .taste = g_uncompress_taste, 675229537Sray .destroy_geom = g_uncompress_destroy_geom, 676229537Sray 677229537Sray .start = g_uncompress_start, 678229537Sray .orphan = g_uncompress_orphan, 679229537Sray .access = g_uncompress_access, 680229537Sray .spoiled = g_uncompress_spoiled, 681229537Sray}; 682229537Sray 683229537SrayDECLARE_GEOM_CLASS(g_uncompress_class, g_uncompress); 684266220SloosMODULE_DEPEND(g_uncompress, zlib, 1, 1, 1); 685