2 * Copyright (c) 2004 Max Khon
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
36 #include <sys/mutex.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 #include <sys/sysctl.h>
41 #include <geom/geom.h>
44 FEATURE(geom_uzip, "GEOM uzip read-only compressed disks support");
46 #undef GEOM_UZIP_DEBUG
47 #ifdef GEOM_UZIP_DEBUG
48 #define DPRINTF(a) printf a
53 static MALLOC_DEFINE(M_GEOM_UZIP, "geom_uzip", "GEOM UZIP data structures");
55 #define UZIP_CLASS_NAME "UZIP"
58 * Maximum allowed valid block size (to prevent foot-shooting)
60 #define MAX_BLKSZ (MAXPHYS - MAXPHYS / 1000 - 12)
63 * Integer values (block size, number of blocks, offsets)
64 * are stored in big-endian (network) order on disk and struct cloop_header
65 * and in native order in struct g_uzip_softc
68 #define CLOOP_MAGIC_LEN 128
69 static char CLOOP_MAGIC_START[] = "#!/bin/sh\n";
72 char magic[CLOOP_MAGIC_LEN]; /* cloop magic */
73 uint32_t blksz; /* block size */
74 uint32_t nblocks; /* number of blocks */
78 uint32_t blksz; /* block size */
79 uint32_t nblocks; /* number of blocks */
83 uint32_t last_blk; /* last blk no */
84 char *last_buf; /* last blk data */
85 int req_total; /* total requests */
86 int req_cached; /* cached requests */
90 g_uzip_softc_free(struct g_uzip_softc *sc, struct g_geom *gp)
93 printf("%s: %d requests, %d cached\n",
94 gp->name, sc->req_total, sc->req_cached);
96 if (sc->offsets != NULL)
97 free(sc->offsets, M_GEOM_UZIP);
98 mtx_destroy(&sc->last_mtx);
99 free(sc->last_buf, M_GEOM_UZIP);
100 free(sc, M_GEOM_UZIP);
104 z_alloc(void *nil, u_int type, u_int size)
108 ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT);
113 z_free(void *nil, void *ptr)
115 free(ptr, M_GEOM_UZIP);
119 g_uzip_done(struct bio *bp)
124 struct g_provider *pp, *pp2;
125 struct g_consumer *cp;
127 struct g_uzip_softc *sc;
129 uint32_t start_blk, i;
132 bp2 = bp->bio_parent;
135 cp = LIST_FIRST(&gp->consumer);
138 DPRINTF(("%s: done\n", gp->name));
140 bp2->bio_error = bp->bio_error;
141 if (bp2->bio_error != 0)
149 err = inflateInit(&zs);
151 bp2->bio_error = EIO;
154 start_blk = bp2->bio_offset / sc->blksz;
155 bsize = pp2->sectorsize;
156 pos = sc->offsets[start_blk] % bsize;
158 DPRINTF(("%s: done: start_blk %d, pos %lld, upos %lld (%lld, %d, %d)\n",
159 gp->name, start_blk, pos, upos,
160 bp2->bio_offset, sc->blksz, bsize));
161 for (i = start_blk; upos < bp2->bio_length; i++) {
162 off_t len, ulen, uoff;
164 uoff = i == start_blk ? bp2->bio_offset % sc->blksz : 0;
165 ulen = MIN(sc->blksz - uoff, bp2->bio_length - upos);
166 len = sc->offsets[i + 1] - sc->offsets[i];
169 /* All zero block: no cache update */
170 bzero(bp2->bio_data + upos, ulen);
172 bp2->bio_completed += ulen;
175 zs.next_in = bp->bio_data + pos;
177 zs.next_out = sc->last_buf;
178 zs.avail_out = sc->blksz;
179 mtx_lock(&sc->last_mtx);
180 err = inflate(&zs, Z_FINISH);
181 if (err != Z_STREAM_END) {
183 mtx_unlock(&sc->last_mtx);
184 DPRINTF(("%s: done: inflate failed (%lld + %lld -> %lld + %lld + %lld)\n",
185 gp->name, pos, len, uoff, upos, ulen));
187 bp2->bio_error = EIO;
191 DPRINTF(("%s: done: inflated %lld + %lld -> %lld + %lld + %lld\n",
195 memcpy(bp2->bio_data + upos, sc->last_buf + uoff, ulen);
196 mtx_unlock(&sc->last_mtx);
200 bp2->bio_completed += ulen;
201 err = inflateReset(&zs);
204 bp2->bio_error = EIO;
208 err = inflateEnd(&zs);
210 bp2->bio_error = EIO;
216 * Finish processing the request.
218 DPRINTF(("%s: done: (%d, %lld, %ld)\n",
219 gp->name, bp2->bio_error, bp2->bio_completed, bp2->bio_resid));
220 free(bp->bio_data, M_GEOM_UZIP);
222 g_io_deliver(bp2, bp2->bio_error);
226 g_uzip_start(struct bio *bp)
229 struct g_provider *pp, *pp2;
231 struct g_consumer *cp;
232 struct g_uzip_softc *sc;
233 uint32_t start_blk, end_blk;
238 DPRINTF(("%s: start (%d)\n", gp->name, bp->bio_cmd));
240 if (bp->bio_cmd != BIO_READ) {
241 g_io_deliver(bp, EOPNOTSUPP);
245 cp = LIST_FIRST(&gp->consumer);
249 start_blk = bp->bio_offset / sc->blksz;
250 end_blk = (bp->bio_offset + bp->bio_length + sc->blksz - 1) / sc->blksz;
251 KASSERT(start_blk < sc->nblocks,
252 ("start_blk out of range"));
253 KASSERT(end_blk <= sc->nblocks,
254 ("end_blk out of range"));
257 if (start_blk + 1 == end_blk) {
258 mtx_lock(&sc->last_mtx);
259 if (start_blk == sc->last_blk) {
262 uoff = bp->bio_offset % sc->blksz;
263 KASSERT(bp->bio_length <= sc->blksz - uoff,
264 ("cached data error"));
265 memcpy(bp->bio_data, sc->last_buf + uoff,
268 mtx_unlock(&sc->last_mtx);
270 DPRINTF(("%s: start: cached 0 + %lld, %lld + 0 + %lld\n",
271 gp->name, bp->bio_length, uoff, bp->bio_length));
272 bp->bio_completed = bp->bio_length;
276 mtx_unlock(&sc->last_mtx);
279 bp2 = g_clone_bio(bp);
281 g_io_deliver(bp, ENOMEM);
284 bp2->bio_done = g_uzip_done;
285 DPRINTF(("%s: start (%d..%d), %s: %d + %lld, %s: %d + %lld\n",
286 gp->name, start_blk, end_blk,
287 pp->name, pp->sectorsize, pp->mediasize,
288 pp2->name, pp2->sectorsize, pp2->mediasize));
289 bsize = pp2->sectorsize;
290 bp2->bio_offset = sc->offsets[start_blk] - sc->offsets[start_blk] % bsize;
291 bp2->bio_length = sc->offsets[end_blk] - bp2->bio_offset;
292 bp2->bio_length = (bp2->bio_length + bsize - 1) / bsize * bsize;
293 DPRINTF(("%s: start %lld + %lld -> %lld + %lld -> %lld + %lld\n",
295 bp->bio_offset, bp->bio_length,
296 sc->offsets[start_blk], sc->offsets[end_blk] - sc->offsets[start_blk],
297 bp2->bio_offset, bp2->bio_length));
298 bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UZIP, M_NOWAIT);
299 if (bp2->bio_data == NULL) {
301 g_io_deliver(bp, ENOMEM);
305 g_io_request(bp2, cp);
306 DPRINTF(("%s: start ok\n", gp->name));
310 g_uzip_orphan(struct g_consumer *cp)
314 g_trace(G_T_TOPOLOGY, "g_uzip_orphan(%p/%s)", cp, cp->provider->name);
318 g_uzip_softc_free(gp->softc, gp);
320 g_wither_geom(gp, ENXIO);
324 g_uzip_access(struct g_provider *pp, int dr, int dw, int de)
327 struct g_consumer *cp;
330 cp = LIST_FIRST(&gp->consumer);
331 KASSERT (cp != NULL, ("g_uzip_access but no consumer"));
333 if (cp->acw + dw > 0)
336 return (g_access(cp, dr, dw, de));
340 g_uzip_spoiled(struct g_consumer *cp)
345 g_trace(G_T_TOPOLOGY, "g_uzip_spoiled(%p/%s)", cp, gp->name);
348 g_uzip_softc_free(gp->softc, gp);
350 g_wither_geom(gp, ENXIO);
353 static struct g_geom *
354 g_uzip_taste(struct g_class *mp, struct g_provider *pp, int flags)
357 uint32_t i, total_offsets, offsets_read, blk;
359 struct cloop_header *header;
360 struct g_consumer *cp;
362 struct g_provider *pp2;
363 struct g_uzip_softc *sc;
365 g_trace(G_T_TOPOLOGY, "g_uzip_taste(%s,%s)", mp->name, pp->name);
368 /* Skip providers that are already open for writing. */
375 * Create geom instance.
377 gp = g_new_geomf(mp, "%s.uzip", pp->name);
378 cp = g_new_consumer(gp);
379 error = g_attach(cp, pp);
381 error = g_access(cp, 1, 0, 0);
384 g_destroy_consumer(cp);
391 * Read cloop header, look for CLOOP magic, perform
392 * other validity checks.
394 DPRINTF(("%s: media sectorsize %u, mediasize %lld\n",
395 gp->name, pp->sectorsize, pp->mediasize));
396 buf = g_read_data(cp, 0, pp->sectorsize, NULL);
399 header = (struct cloop_header *) buf;
400 if (strncmp(header->magic, CLOOP_MAGIC_START,
401 sizeof(CLOOP_MAGIC_START) - 1) != 0) {
402 DPRINTF(("%s: no CLOOP magic\n", gp->name));
405 if (header->magic[0x0b] != 'V' || header->magic[0x0c] < '2') {
406 DPRINTF(("%s: image version too old\n", gp->name));
411 * Initialize softc and read offsets.
413 sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO);
415 sc->blksz = ntohl(header->blksz);
416 sc->nblocks = ntohl(header->nblocks);
417 if (sc->blksz % 512 != 0) {
418 printf("%s: block size (%u) should be multiple of 512.\n",
419 gp->name, sc->blksz);
422 if (sc->blksz > MAX_BLKSZ) {
423 printf("%s: block size (%u) should not be larger than %d.\n",
424 gp->name, sc->blksz, MAX_BLKSZ);
426 total_offsets = sc->nblocks + 1;
427 if (sizeof(struct cloop_header) +
428 total_offsets * sizeof(uint64_t) > pp->mediasize) {
429 printf("%s: media too small for %u blocks\n",
430 gp->name, sc->nblocks);
433 sc->offsets = malloc(
434 total_offsets * sizeof(uint64_t), M_GEOM_UZIP, M_WAITOK);
435 offsets_read = MIN(total_offsets,
436 (pp->sectorsize - sizeof(*header)) / sizeof(uint64_t));
437 for (i = 0; i < offsets_read; i++)
438 sc->offsets[i] = be64toh(((uint64_t *) (header + 1))[i]);
439 DPRINTF(("%s: %u offsets in the first sector\n",
440 gp->name, offsets_read));
441 for (blk = 1; offsets_read < total_offsets; blk++) {
446 cp, blk * pp->sectorsize, pp->sectorsize, NULL);
449 nread = MIN(total_offsets - offsets_read,
450 pp->sectorsize / sizeof(uint64_t));
451 DPRINTF(("%s: %u offsets read from sector %d\n",
452 gp->name, nread, blk));
453 for (i = 0; i < nread; i++) {
454 sc->offsets[offsets_read + i] =
455 be64toh(((uint64_t *) buf)[i]);
457 offsets_read += nread;
459 DPRINTF(("%s: done reading offsets\n", gp->name));
460 mtx_init(&sc->last_mtx, "geom_uzip cache", NULL, MTX_DEF);
462 sc->last_buf = malloc(sc->blksz, M_GEOM_UZIP, M_WAITOK);
467 pp2 = g_new_providerf(gp, "%s", gp->name);
468 pp2->sectorsize = 512;
469 pp2->mediasize = (off_t)sc->nblocks * sc->blksz;
470 pp2->stripesize = pp->stripesize;
471 pp2->stripeoffset = pp->stripeoffset;
472 g_error_provider(pp2, 0);
473 g_access(cp, -1, 0, 0);
475 DPRINTF(("%s: taste ok (%d, %lld), (%d, %d), %x\n",
477 pp2->sectorsize, pp2->mediasize,
478 pp2->stripeoffset, pp2->stripesize, pp2->flags));
479 printf("%s: %u x %u blocks\n",
480 gp->name, sc->nblocks, sc->blksz);
485 g_access(cp, -1, 0, 0);
488 if (gp->softc != NULL) {
489 g_uzip_softc_free(gp->softc, NULL);
493 g_destroy_consumer(cp);
499 g_uzip_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
501 struct g_provider *pp;
503 g_trace(G_T_TOPOLOGY, "g_uzip_destroy_geom(%s, %s)", mp->name, gp->name);
506 if (gp->softc == NULL) {
507 printf("%s(%s): gp->softc == NULL\n", __func__, gp->name);
511 KASSERT(gp != NULL, ("NULL geom"));
512 pp = LIST_FIRST(&gp->provider);
513 KASSERT(pp != NULL, ("NULL provider"));
514 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
517 g_uzip_softc_free(gp->softc, gp);
519 g_wither_geom(gp, ENXIO);
523 static struct g_class g_uzip_class = {
524 .name = UZIP_CLASS_NAME,
525 .version = G_VERSION,
526 .taste = g_uzip_taste,
527 .destroy_geom = g_uzip_destroy_geom,
529 .start = g_uzip_start,
530 .orphan = g_uzip_orphan,
531 .access = g_uzip_access,
532 .spoiled = g_uzip_spoiled,
535 DECLARE_GEOM_CLASS(g_uzip_class, g_uzip);
536 MODULE_DEPEND(g_uzip, zlib, 1, 1, 1);