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