g_uzip.c revision 265193
1/*- 2 * Copyright (c) 2004 Max Khon 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: head/sys/geom/uzip/g_uzip.c 265193 2014-05-01 14:47:27Z loos $"); 29 30#include <sys/param.h> 31#include <sys/bio.h> 32#include <sys/endian.h> 33#include <sys/errno.h> 34#include <sys/kernel.h> 35#include <sys/lock.h> 36#include <sys/mutex.h> 37#include <sys/malloc.h> 38#include <sys/systm.h> 39 40#include <geom/geom.h> 41#include <net/zlib.h> 42 43FEATURE(geom_uzip, "GEOM uzip read-only compressed disks support"); 44 45#undef GEOM_UZIP_DEBUG 46#ifdef GEOM_UZIP_DEBUG 47#define DPRINTF(a) printf a 48#else 49#define DPRINTF(a) 50#endif 51 52static MALLOC_DEFINE(M_GEOM_UZIP, "geom_uzip", "GEOM UZIP data structures"); 53 54#define UZIP_CLASS_NAME "UZIP" 55 56/* 57 * Maximum allowed valid block size (to prevent foot-shooting) 58 */ 59#define MAX_BLKSZ (MAXPHYS - MAXPHYS / 1000 - 12) 60 61/* 62 * Integer values (block size, number of blocks, offsets) 63 * are stored in big-endian (network) order on disk and struct cloop_header 64 * and in native order in struct g_uzip_softc 65 */ 66 67#define CLOOP_MAGIC_LEN 128 68static char CLOOP_MAGIC_START[] = "#!/bin/sh\n"; 69 70struct cloop_header { 71 char magic[CLOOP_MAGIC_LEN]; /* cloop magic */ 72 uint32_t blksz; /* block size */ 73 uint32_t nblocks; /* number of blocks */ 74}; 75 76struct g_uzip_softc { 77 uint32_t blksz; /* block size */ 78 uint32_t nblocks; /* number of blocks */ 79 uint64_t *offsets; 80 81 struct mtx last_mtx; 82 uint32_t last_blk; /* last blk no */ 83 char *last_buf; /* last blk data */ 84 int req_total; /* total requests */ 85 int req_cached; /* cached requests */ 86}; 87 88static void 89g_uzip_softc_free(struct g_uzip_softc *sc, struct g_geom *gp) 90{ 91 92 if (gp != NULL) { 93 printf("%s: %d requests, %d cached\n", 94 gp->name, sc->req_total, sc->req_cached); 95 } 96 if (sc->offsets != NULL) { 97 free(sc->offsets, M_GEOM_UZIP); 98 sc->offsets = NULL; 99 } 100 mtx_destroy(&sc->last_mtx); 101 free(sc->last_buf, M_GEOM_UZIP); 102 free(sc, M_GEOM_UZIP); 103} 104 105static void * 106z_alloc(void *nil, u_int type, u_int size) 107{ 108 void *ptr; 109 110 ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT); 111 112 return (ptr); 113} 114 115static void 116z_free(void *nil, void *ptr) 117{ 118 119 free(ptr, M_GEOM_UZIP); 120} 121 122static void 123g_uzip_done(struct bio *bp) 124{ 125 int err; 126 struct bio *bp2; 127 z_stream zs; 128 struct g_provider *pp, *pp2; 129 struct g_consumer *cp; 130 struct g_geom *gp; 131 struct g_uzip_softc *sc; 132 off_t iolen, pos, upos; 133 uint32_t start_blk, i; 134 size_t bsize; 135 136 bp2 = bp->bio_parent; 137 pp = bp2->bio_to; 138 gp = pp->geom; 139 cp = LIST_FIRST(&gp->consumer); 140 pp2 = cp->provider; 141 sc = gp->softc; 142 DPRINTF(("%s: done\n", gp->name)); 143 144 bp2->bio_error = bp->bio_error; 145 if (bp2->bio_error != 0) 146 goto done; 147 148 /* 149 * Uncompress data. 150 */ 151 zs.zalloc = z_alloc; 152 zs.zfree = z_free; 153 err = inflateInit(&zs); 154 if (err != Z_OK) { 155 bp2->bio_error = EIO; 156 goto done; 157 } 158 start_blk = bp2->bio_offset / sc->blksz; 159 bsize = pp2->sectorsize; 160 iolen = bp->bio_completed; 161 pos = sc->offsets[start_blk] % bsize; 162 upos = 0; 163 DPRINTF(("%s: done: start_blk %d, pos %jd, upos %jd, iolen %jd " 164 "(%jd, %d, %zd)\n", 165 gp->name, start_blk, (intmax_t)pos, (intmax_t)upos, 166 (intmax_t)iolen, (intmax_t)bp2->bio_offset, sc->blksz, bsize)); 167 for (i = start_blk; upos < bp2->bio_length; i++) { 168 off_t len, ulen, uoff; 169 170 uoff = i == start_blk ? bp2->bio_offset % sc->blksz : 0; 171 ulen = MIN(sc->blksz - uoff, bp2->bio_length - upos); 172 len = sc->offsets[i + 1] - sc->offsets[i]; 173 174 if (len == 0) { 175 /* All zero block: no cache update */ 176 bzero(bp2->bio_data + upos, ulen); 177 upos += ulen; 178 bp2->bio_completed += ulen; 179 continue; 180 } 181 if (len > iolen) { 182 DPRINTF(("%s: done: early termination: len (%jd) > " 183 "iolen (%jd)\n", 184 gp->name, (intmax_t)len, (intmax_t)iolen)); 185 break; 186 } 187 zs.next_in = bp->bio_data + pos; 188 zs.avail_in = len; 189 zs.next_out = sc->last_buf; 190 zs.avail_out = sc->blksz; 191 mtx_lock(&sc->last_mtx); 192 err = inflate(&zs, Z_FINISH); 193 if (err != Z_STREAM_END) { 194 sc->last_blk = -1; 195 mtx_unlock(&sc->last_mtx); 196 DPRINTF(("%s: done: inflate failed (%jd + %jd -> %jd + %jd + %jd)\n", 197 gp->name, (intmax_t)pos, (intmax_t)len, 198 (intmax_t)uoff, (intmax_t)upos, (intmax_t)ulen)); 199 inflateEnd(&zs); 200 bp2->bio_error = EIO; 201 goto done; 202 } 203 sc->last_blk = i; 204 DPRINTF(("%s: done: inflated %jd + %jd -> %jd + %jd + %jd\n", 205 gp->name, (intmax_t)pos, (intmax_t)len, (intmax_t)uoff, 206 (intmax_t)upos, (intmax_t)ulen)); 207 memcpy(bp2->bio_data + upos, sc->last_buf + uoff, ulen); 208 mtx_unlock(&sc->last_mtx); 209 210 pos += len; 211 iolen -= len; 212 upos += ulen; 213 bp2->bio_completed += ulen; 214 err = inflateReset(&zs); 215 if (err != Z_OK) { 216 inflateEnd(&zs); 217 bp2->bio_error = EIO; 218 goto done; 219 } 220 } 221 err = inflateEnd(&zs); 222 if (err != Z_OK) { 223 bp2->bio_error = EIO; 224 goto done; 225 } 226 227done: 228 /* 229 * Finish processing the request. 230 */ 231 DPRINTF(("%s: done: (%d, %jd, %ld)\n", 232 gp->name, bp2->bio_error, (intmax_t)bp2->bio_completed, 233 bp2->bio_resid)); 234 free(bp->bio_data, M_GEOM_UZIP); 235 g_destroy_bio(bp); 236 g_io_deliver(bp2, bp2->bio_error); 237} 238 239static void 240g_uzip_start(struct bio *bp) 241{ 242 struct bio *bp2; 243 struct g_provider *pp, *pp2; 244 struct g_geom *gp; 245 struct g_consumer *cp; 246 struct g_uzip_softc *sc; 247 uint32_t start_blk, end_blk; 248 size_t bsize; 249 250 pp = bp->bio_to; 251 gp = pp->geom; 252 DPRINTF(("%s: start (%d)\n", gp->name, bp->bio_cmd)); 253 254 if (bp->bio_cmd != BIO_READ) { 255 g_io_deliver(bp, EOPNOTSUPP); 256 return; 257 } 258 259 cp = LIST_FIRST(&gp->consumer); 260 pp2 = cp->provider; 261 sc = gp->softc; 262 263 start_blk = bp->bio_offset / sc->blksz; 264 end_blk = (bp->bio_offset + bp->bio_length + sc->blksz - 1) / sc->blksz; 265 KASSERT(start_blk < sc->nblocks, ("start_blk out of range")); 266 KASSERT(end_blk <= sc->nblocks, ("end_blk out of range")); 267 268 sc->req_total++; 269 if (start_blk + 1 == end_blk) { 270 mtx_lock(&sc->last_mtx); 271 if (start_blk == sc->last_blk) { 272 off_t uoff; 273 274 uoff = bp->bio_offset % sc->blksz; 275 KASSERT(bp->bio_length <= sc->blksz - uoff, 276 ("cached data error")); 277 memcpy(bp->bio_data, sc->last_buf + uoff, 278 bp->bio_length); 279 sc->req_cached++; 280 mtx_unlock(&sc->last_mtx); 281 282 DPRINTF(("%s: start: cached 0 + %jd, %jd + 0 + %jd\n", 283 gp->name, (intmax_t)bp->bio_length, (intmax_t)uoff, 284 (intmax_t)bp->bio_length)); 285 bp->bio_completed = bp->bio_length; 286 g_io_deliver(bp, 0); 287 return; 288 } 289 mtx_unlock(&sc->last_mtx); 290 } 291 292 bp2 = g_clone_bio(bp); 293 if (bp2 == NULL) { 294 g_io_deliver(bp, ENOMEM); 295 return; 296 } 297 bp2->bio_done = g_uzip_done; 298 DPRINTF(("%s: start (%d..%d), %s: %d + %jd, %s: %d + %jd\n", 299 gp->name, start_blk, end_blk, 300 pp->name, pp->sectorsize, (intmax_t)pp->mediasize, 301 pp2->name, pp2->sectorsize, (intmax_t)pp2->mediasize)); 302 bsize = pp2->sectorsize; 303 bp2->bio_offset = sc->offsets[start_blk] - sc->offsets[start_blk] % bsize; 304 while (1) { 305 bp2->bio_length = sc->offsets[end_blk] - bp2->bio_offset; 306 bp2->bio_length = (bp2->bio_length + bsize - 1) / bsize * bsize; 307 if (bp2->bio_length < MAXPHYS) 308 break; 309 310 end_blk--; 311 DPRINTF(("%s: bio_length (%jd) > MAXPHYS: lowering end_blk " 312 "to %u\n", gp->name, (intmax_t)bp2->bio_length, end_blk)); 313 } 314 DPRINTF(("%s: start %jd + %jd -> %ju + %ju -> %jd + %jd\n", 315 gp->name, 316 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length, 317 (uintmax_t)sc->offsets[start_blk], 318 (uintmax_t)sc->offsets[end_blk] - sc->offsets[start_blk], 319 (intmax_t)bp2->bio_offset, (intmax_t)bp2->bio_length)); 320 bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UZIP, M_NOWAIT); 321 if (bp2->bio_data == NULL) { 322 g_destroy_bio(bp2); 323 g_io_deliver(bp, ENOMEM); 324 return; 325 } 326 327 g_io_request(bp2, cp); 328 DPRINTF(("%s: start ok\n", gp->name)); 329} 330 331static void 332g_uzip_orphan(struct g_consumer *cp) 333{ 334 struct g_geom *gp; 335 336 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->provider->name); 337 g_topology_assert(); 338 339 gp = cp->geom; 340 g_uzip_softc_free(gp->softc, gp); 341 gp->softc = NULL; 342 g_wither_geom(gp, ENXIO); 343} 344 345static int 346g_uzip_access(struct g_provider *pp, int dr, int dw, int de) 347{ 348 struct g_geom *gp; 349 struct g_consumer *cp; 350 351 gp = pp->geom; 352 cp = LIST_FIRST(&gp->consumer); 353 KASSERT (cp != NULL, ("g_uzip_access but no consumer")); 354 355 if (cp->acw + dw > 0) 356 return (EROFS); 357 358 return (g_access(cp, dr, dw, de)); 359} 360 361static void 362g_uzip_spoiled(struct g_consumer *cp) 363{ 364 struct g_geom *gp; 365 366 gp = cp->geom; 367 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name); 368 g_topology_assert(); 369 370 g_uzip_softc_free(gp->softc, gp); 371 gp->softc = NULL; 372 g_wither_geom(gp, ENXIO); 373} 374 375static struct g_geom * 376g_uzip_taste(struct g_class *mp, struct g_provider *pp, int flags) 377{ 378 int error; 379 uint32_t i, total_offsets, offsets_read, blk; 380 void *buf; 381 struct cloop_header *header; 382 struct g_consumer *cp; 383 struct g_geom *gp; 384 struct g_provider *pp2; 385 struct g_uzip_softc *sc; 386 387 g_trace(G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name); 388 g_topology_assert(); 389 390 /* Skip providers that are already open for writing. */ 391 if (pp->acw > 0) 392 return (NULL); 393 394 buf = NULL; 395 396 /* 397 * Create geom instance. 398 */ 399 gp = g_new_geomf(mp, "%s.uzip", pp->name); 400 cp = g_new_consumer(gp); 401 error = g_attach(cp, pp); 402 if (error == 0) 403 error = g_access(cp, 1, 0, 0); 404 if (error) { 405 g_detach(cp); 406 g_destroy_consumer(cp); 407 g_destroy_geom(gp); 408 return (NULL); 409 } 410 g_topology_unlock(); 411 412 /* 413 * Read cloop header, look for CLOOP magic, perform 414 * other validity checks. 415 */ 416 DPRINTF(("%s: media sectorsize %u, mediasize %jd\n", 417 gp->name, pp->sectorsize, (intmax_t)pp->mediasize)); 418 buf = g_read_data(cp, 0, pp->sectorsize, NULL); 419 if (buf == NULL) 420 goto err; 421 header = (struct cloop_header *) buf; 422 if (strncmp(header->magic, CLOOP_MAGIC_START, 423 sizeof(CLOOP_MAGIC_START) - 1) != 0) { 424 DPRINTF(("%s: no CLOOP magic\n", gp->name)); 425 goto err; 426 } 427 if (header->magic[0x0b] != 'V' || header->magic[0x0c] < '2') { 428 DPRINTF(("%s: image version too old\n", gp->name)); 429 goto err; 430 } 431 432 /* 433 * Initialize softc and read offsets. 434 */ 435 sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO); 436 gp->softc = sc; 437 sc->blksz = ntohl(header->blksz); 438 sc->nblocks = ntohl(header->nblocks); 439 if (sc->blksz % 512 != 0) { 440 printf("%s: block size (%u) should be multiple of 512.\n", 441 gp->name, sc->blksz); 442 goto err; 443 } 444 if (sc->blksz > MAX_BLKSZ) { 445 printf("%s: block size (%u) should not be larger than %d.\n", 446 gp->name, sc->blksz, MAX_BLKSZ); 447 } 448 total_offsets = sc->nblocks + 1; 449 if (sizeof(struct cloop_header) + 450 total_offsets * sizeof(uint64_t) > pp->mediasize) { 451 printf("%s: media too small for %u blocks\n", 452 gp->name, sc->nblocks); 453 goto err; 454 } 455 sc->offsets = malloc( 456 total_offsets * sizeof(uint64_t), M_GEOM_UZIP, M_WAITOK); 457 offsets_read = MIN(total_offsets, 458 (pp->sectorsize - sizeof(*header)) / sizeof(uint64_t)); 459 for (i = 0; i < offsets_read; i++) 460 sc->offsets[i] = be64toh(((uint64_t *) (header + 1))[i]); 461 DPRINTF(("%s: %u offsets in the first sector\n", 462 gp->name, offsets_read)); 463 for (blk = 1; offsets_read < total_offsets; blk++) { 464 uint32_t nread; 465 466 free(buf, M_GEOM); 467 buf = g_read_data( 468 cp, blk * pp->sectorsize, pp->sectorsize, NULL); 469 if (buf == NULL) 470 goto err; 471 nread = MIN(total_offsets - offsets_read, 472 pp->sectorsize / sizeof(uint64_t)); 473 DPRINTF(("%s: %u offsets read from sector %d\n", 474 gp->name, nread, blk)); 475 for (i = 0; i < nread; i++) { 476 sc->offsets[offsets_read + i] = 477 be64toh(((uint64_t *) buf)[i]); 478 } 479 offsets_read += nread; 480 } 481 DPRINTF(("%s: done reading offsets\n", gp->name)); 482 mtx_init(&sc->last_mtx, "geom_uzip cache", NULL, MTX_DEF); 483 sc->last_blk = -1; 484 sc->last_buf = malloc(sc->blksz, M_GEOM_UZIP, M_WAITOK); 485 sc->req_total = 0; 486 sc->req_cached = 0; 487 488 g_topology_lock(); 489 pp2 = g_new_providerf(gp, "%s", gp->name); 490 pp2->sectorsize = 512; 491 pp2->mediasize = (off_t)sc->nblocks * sc->blksz; 492 pp2->stripesize = pp->stripesize; 493 pp2->stripeoffset = pp->stripeoffset; 494 g_error_provider(pp2, 0); 495 g_access(cp, -1, 0, 0); 496 497 DPRINTF(("%s: taste ok (%d, %jd), (%d, %d), %x\n", 498 gp->name, 499 pp2->sectorsize, (intmax_t)pp2->mediasize, 500 pp2->stripeoffset, pp2->stripesize, pp2->flags)); 501 printf("%s: %u x %u blocks\n", gp->name, sc->nblocks, sc->blksz); 502 return (gp); 503 504err: 505 g_topology_lock(); 506 g_access(cp, -1, 0, 0); 507 if (buf != NULL) 508 free(buf, M_GEOM); 509 if (gp->softc != NULL) { 510 g_uzip_softc_free(gp->softc, NULL); 511 gp->softc = NULL; 512 } 513 g_detach(cp); 514 g_destroy_consumer(cp); 515 g_destroy_geom(gp); 516 517 return (NULL); 518} 519 520static int 521g_uzip_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 522{ 523 struct g_provider *pp; 524 525 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, gp->name); 526 g_topology_assert(); 527 528 if (gp->softc == NULL) { 529 printf("%s(%s): gp->softc == NULL\n", __func__, gp->name); 530 return (ENXIO); 531 } 532 533 KASSERT(gp != NULL, ("NULL geom")); 534 pp = LIST_FIRST(&gp->provider); 535 KASSERT(pp != NULL, ("NULL provider")); 536 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) 537 return (EBUSY); 538 539 g_uzip_softc_free(gp->softc, gp); 540 gp->softc = NULL; 541 g_wither_geom(gp, ENXIO); 542 543 return (0); 544} 545 546static struct g_class g_uzip_class = { 547 .name = UZIP_CLASS_NAME, 548 .version = G_VERSION, 549 .taste = g_uzip_taste, 550 .destroy_geom = g_uzip_destroy_geom, 551 552 .start = g_uzip_start, 553 .orphan = g_uzip_orphan, 554 .access = g_uzip_access, 555 .spoiled = g_uzip_spoiled, 556}; 557 558DECLARE_GEOM_CLASS(g_uzip_class, g_uzip); 559MODULE_DEPEND(g_uzip, zlib, 1, 1, 1); 560