2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Max Khon
5 * Copyright (c) 2014 Juniper Networks, Inc.
6 * Copyright (c) 2006-2016 Maxim Sobolev <sobomax@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/endian.h>
37 #include <sys/errno.h>
38 #include <sys/kernel.h>
40 #include <sys/mutex.h>
41 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
44 #include <sys/kthread.h>
46 #include <geom/geom.h>
48 #include <geom/uzip/g_uzip.h>
49 #include <geom/uzip/g_uzip_cloop.h>
50 #include <geom/uzip/g_uzip_softc.h>
51 #include <geom/uzip/g_uzip_dapi.h>
52 #include <geom/uzip/g_uzip_zlib.h>
53 #include <geom/uzip/g_uzip_lzma.h>
54 #include <geom/uzip/g_uzip_wrkthr.h>
58 MALLOC_DEFINE(M_GEOM_UZIP, "geom_uzip", "GEOM UZIP data structures");
60 FEATURE(geom_uzip, "GEOM read-only compressed disks support");
66 unsigned char padded:1;
67 #define BLEN_UNDEF UINT32_MAX
71 #define ABS(a) ((a) < 0 ? -(a) : (a))
74 #define BLK_IN_RANGE(mcn, bcn, ilen) \
75 (((bcn) != BLEN_UNDEF) && ( \
76 ((ilen) >= 0 && (mcn >= bcn) && (mcn <= ((intmax_t)(bcn) + (ilen)))) || \
77 ((ilen) < 0 && (mcn <= bcn) && (mcn >= ((intmax_t)(bcn) + (ilen)))) \
80 #ifdef GEOM_UZIP_DEBUG
81 # define GEOM_UZIP_DBG_DEFAULT 3
83 # define GEOM_UZIP_DBG_DEFAULT 0
87 #define GUZ_DBG_INFO 2
91 #define GUZ_DEV_SUFX ".uzip"
92 #define GUZ_DEV_NAME(p) (p GUZ_DEV_SUFX)
94 static char g_uzip_attach_to[MAXPATHLEN] = {"*"};
95 static char g_uzip_noattach_to[MAXPATHLEN] = {GUZ_DEV_NAME("*")};
96 TUNABLE_STR("kern.geom.uzip.attach_to", g_uzip_attach_to,
97 sizeof(g_uzip_attach_to));
98 TUNABLE_STR("kern.geom.uzip.noattach_to", g_uzip_noattach_to,
99 sizeof(g_uzip_noattach_to));
101 SYSCTL_DECL(_kern_geom);
102 SYSCTL_NODE(_kern_geom, OID_AUTO, uzip, CTLFLAG_RW, 0, "GEOM_UZIP stuff");
103 static u_int g_uzip_debug = GEOM_UZIP_DBG_DEFAULT;
104 SYSCTL_UINT(_kern_geom_uzip, OID_AUTO, debug, CTLFLAG_RWTUN, &g_uzip_debug, 0,
105 "Debug level (0-4)");
106 static u_int g_uzip_debug_block = BLEN_UNDEF;
107 SYSCTL_UINT(_kern_geom_uzip, OID_AUTO, debug_block, CTLFLAG_RWTUN,
108 &g_uzip_debug_block, 0, "Debug operations around specific cluster#");
110 #define DPRINTF(lvl, a) \
111 if ((lvl) <= g_uzip_debug) { \
114 #define DPRINTF_BLK(lvl, cn, a) \
115 if ((lvl) <= g_uzip_debug || \
116 BLK_IN_RANGE(cn, g_uzip_debug_block, 8) || \
117 BLK_IN_RANGE(cn, g_uzip_debug_block, -8)) { \
120 #define DPRINTF_BRNG(lvl, bcn, ecn, a) \
121 KASSERT(bcn < ecn, ("DPRINTF_BRNG: invalid range (%ju, %ju)", \
122 (uintmax_t)bcn, (uintmax_t)ecn)); \
123 if (((lvl) <= g_uzip_debug) || \
124 BLK_IN_RANGE(g_uzip_debug_block, bcn, \
125 (intmax_t)ecn - (intmax_t)bcn)) { \
129 #define UZIP_CLASS_NAME "UZIP"
132 * Maximum allowed valid block size (to prevent foot-shooting)
134 #define MAX_BLKSZ (MAXPHYS)
136 static char CLOOP_MAGIC_START[] = "#!/bin/sh\n";
138 static void g_uzip_read_done(struct bio *bp);
139 static void g_uzip_do(struct g_uzip_softc *, struct bio *bp);
142 g_uzip_softc_free(struct g_uzip_softc *sc, struct g_geom *gp)
146 DPRINTF(GUZ_DBG_INFO, ("%s: %d requests, %d cached\n",
147 gp->name, sc->req_total, sc->req_cached));
150 mtx_lock(&sc->queue_mtx);
151 sc->wrkthr_flags |= GUZ_SHUTDOWN;
153 while (!(sc->wrkthr_flags & GUZ_EXITING)) {
154 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "guzfree",
157 mtx_unlock(&sc->queue_mtx);
159 sc->dcp->free(sc->dcp);
160 free(sc->toc, M_GEOM_UZIP);
161 mtx_destroy(&sc->queue_mtx);
162 mtx_destroy(&sc->last_mtx);
163 free(sc->last_buf, M_GEOM_UZIP);
164 free(sc, M_GEOM_UZIP);
168 g_uzip_cached(struct g_geom *gp, struct bio *bp)
170 struct g_uzip_softc *sc;
172 size_t blk, blkofs, usz;
175 ofs = bp->bio_offset + bp->bio_completed;
176 blk = ofs / sc->blksz;
177 mtx_lock(&sc->last_mtx);
178 if (blk == sc->last_blk) {
179 blkofs = ofs % sc->blksz;
180 usz = sc->blksz - blkofs;
181 if (bp->bio_resid < usz)
183 memcpy(bp->bio_data + bp->bio_completed, sc->last_buf + blkofs,
186 mtx_unlock(&sc->last_mtx);
188 DPRINTF(GUZ_DBG_IO, ("%s/%s: %p: offset=%jd: got %jd bytes "
189 "from cache\n", __func__, gp->name, bp, (intmax_t)ofs,
192 bp->bio_completed += usz;
193 bp->bio_resid -= usz;
195 if (bp->bio_resid == 0) {
200 mtx_unlock(&sc->last_mtx);
205 #define BLK_ENDS(sc, bi) ((sc)->toc[(bi)].offset + \
206 (sc)->toc[(bi)].blen)
208 #define BLK_IS_CONT(sc, bi) (BLK_ENDS((sc), (bi) - 1) == \
209 (sc)->toc[(bi)].offset)
210 #define BLK_IS_NIL(sc, bi) ((sc)->toc[(bi)].blen == 0)
212 #define TOFF_2_BOFF(sc, pp, bi) ((sc)->toc[(bi)].offset - \
213 (sc)->toc[(bi)].offset % (pp)->sectorsize)
214 #define TLEN_2_BLEN(sc, pp, bp, ei) roundup(BLK_ENDS((sc), (ei)) - \
215 (bp)->bio_offset, (pp)->sectorsize)
218 g_uzip_request(struct g_geom *gp, struct bio *bp)
220 struct g_uzip_softc *sc;
222 struct g_consumer *cp;
223 struct g_provider *pp;
224 off_t ofs, start_blk_ofs;
225 size_t i, start_blk, end_blk, zsize;
227 if (g_uzip_cached(gp, bp) != 0)
232 cp = LIST_FIRST(&gp->consumer);
235 ofs = bp->bio_offset + bp->bio_completed;
236 start_blk = ofs / sc->blksz;
237 KASSERT(start_blk < sc->nblocks, ("start_blk out of range"));
238 end_blk = howmany(ofs + bp->bio_resid, sc->blksz);
239 KASSERT(end_blk <= sc->nblocks, ("end_blk out of range"));
241 for (; BLK_IS_NIL(sc, start_blk) && start_blk < end_blk; start_blk++) {
242 /* Fill in any leading Nil blocks */
243 start_blk_ofs = ofs % sc->blksz;
244 zsize = MIN(sc->blksz - start_blk_ofs, bp->bio_resid);
245 DPRINTF_BLK(GUZ_DBG_IO, start_blk, ("%s/%s: %p/%ju: "
246 "filling %ju zero bytes\n", __func__, gp->name, gp,
247 (uintmax_t)bp->bio_completed, (uintmax_t)zsize));
248 bzero(bp->bio_data + bp->bio_completed, zsize);
249 bp->bio_completed += zsize;
250 bp->bio_resid -= zsize;
254 if (start_blk == end_blk) {
255 KASSERT(bp->bio_resid == 0, ("bp->bio_resid is invalid"));
257 * No non-Nil data is left, complete request immediately.
259 DPRINTF(GUZ_DBG_IO, ("%s/%s: %p: all done returning %ju "
260 "bytes\n", __func__, gp->name, gp,
261 (uintmax_t)bp->bio_completed));
266 for (i = start_blk + 1; i < end_blk; i++) {
267 /* Trim discontinuous areas if any */
268 if (!BLK_IS_CONT(sc, i)) {
274 DPRINTF_BRNG(GUZ_DBG_IO, start_blk, end_blk, ("%s/%s: %p: "
275 "start=%u (%ju[%jd]), end=%u (%ju)\n", __func__, gp->name, bp,
276 (u_int)start_blk, (uintmax_t)sc->toc[start_blk].offset,
277 (intmax_t)sc->toc[start_blk].blen,
278 (u_int)end_blk, (uintmax_t)BLK_ENDS(sc, end_blk - 1)));
280 bp2 = g_clone_bio(bp);
282 g_io_deliver(bp, ENOMEM);
285 bp2->bio_done = g_uzip_read_done;
287 bp2->bio_offset = TOFF_2_BOFF(sc, pp, start_blk);
289 bp2->bio_length = TLEN_2_BLEN(sc, pp, bp2, end_blk - 1);
290 if (bp2->bio_length <= MAXPHYS) {
293 if (end_blk == (start_blk + 1)) {
299 DPRINTF(GUZ_DBG_IO, ("%s/%s: bp2->bio_length = %jd, "
300 "bp2->bio_offset = %jd\n", __func__, gp->name,
301 (intmax_t)bp2->bio_length, (intmax_t)bp2->bio_offset));
303 bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UZIP, M_NOWAIT);
304 if (bp2->bio_data == NULL) {
306 g_io_deliver(bp, ENOMEM);
310 DPRINTF_BRNG(GUZ_DBG_IO, start_blk, end_blk, ("%s/%s: %p: "
311 "reading %jd bytes from offset %jd\n", __func__, gp->name, bp,
312 (intmax_t)bp2->bio_length, (intmax_t)bp2->bio_offset));
314 g_io_request(bp2, cp);
319 g_uzip_read_done(struct bio *bp)
323 struct g_uzip_softc *sc;
325 bp2 = bp->bio_parent;
326 gp = bp2->bio_to->geom;
329 mtx_lock(&sc->queue_mtx);
330 bioq_disksort(&sc->bio_queue, bp);
331 mtx_unlock(&sc->queue_mtx);
336 g_uzip_memvcmp(const void *memory, unsigned char val, size_t size)
340 mm = (const u_char *)memory;
341 return (*mm == val) && memcmp(mm, mm + 1, size - 1) == 0;
345 g_uzip_do(struct g_uzip_softc *sc, struct bio *bp)
348 struct g_provider *pp;
349 struct g_consumer *cp;
353 size_t blk, blkofs, len, ulen, firstblk;
356 bp2 = bp->bio_parent;
357 gp = bp2->bio_to->geom;
359 cp = LIST_FIRST(&gp->consumer);
362 bp2->bio_error = bp->bio_error;
363 if (bp2->bio_error != 0)
366 /* Make sure there's forward progress. */
367 if (bp->bio_completed == 0) {
368 bp2->bio_error = ECANCELED;
372 ofs = bp2->bio_offset + bp2->bio_completed;
373 firstblk = blk = ofs / sc->blksz;
374 blkofs = ofs % sc->blksz;
375 data = bp->bio_data + sc->toc[blk].offset % pp->sectorsize;
376 data2 = bp2->bio_data + bp2->bio_completed;
377 while (bp->bio_completed && bp2->bio_resid) {
378 if (blk > firstblk && !BLK_IS_CONT(sc, blk)) {
379 DPRINTF_BLK(GUZ_DBG_IO, blk, ("%s/%s: %p: backref'ed "
380 "cluster #%u requested, looping around\n",
381 __func__, gp->name, bp2, (u_int)blk));
384 ulen = MIN(sc->blksz - blkofs, bp2->bio_resid);
385 len = sc->toc[blk].blen;
386 DPRINTF(GUZ_DBG_IO, ("%s/%s: %p/%ju: data2=%p, ulen=%u, "
387 "data=%p, len=%u\n", __func__, gp->name, gp,
388 bp->bio_completed, data2, (u_int)ulen, data, (u_int)len));
390 /* All zero block: no cache update */
393 } else if (len <= bp->bio_completed) {
394 mtx_lock(&sc->last_mtx);
395 err = sc->dcp->decompress(sc->dcp, gp->name, data,
397 if (err != 0 && sc->toc[blk].last != 0) {
399 * Last block decompression has failed, check
400 * if it's just zero padding.
402 if (g_uzip_memvcmp(data, '\0', len) == 0) {
403 sc->toc[blk].blen = 0;
405 mtx_unlock(&sc->last_mtx);
412 mtx_unlock(&sc->last_mtx);
413 bp2->bio_error = EILSEQ;
414 DPRINTF(GUZ_DBG_ERR, ("%s/%s: decompress"
415 "(%p, %ju, %ju) failed\n", __func__,
416 gp->name, sc->dcp, (uintmax_t)blk,
421 memcpy(data2, sc->last_buf + blkofs, ulen);
422 mtx_unlock(&sc->last_mtx);
423 err = sc->dcp->rewind(sc->dcp, gp->name);
425 bp2->bio_error = EILSEQ;
426 DPRINTF(GUZ_DBG_ERR, ("%s/%s: rewind(%p) "
427 "failed\n", __func__, gp->name, sc->dcp));
435 bp2->bio_completed += ulen;
436 bp2->bio_resid -= ulen;
437 bp->bio_completed -= len;
443 /* Finish processing the request. */
444 free(bp->bio_data, M_GEOM_UZIP);
446 if (bp2->bio_error != 0 || bp2->bio_resid == 0)
447 g_io_deliver(bp2, bp2->bio_error);
449 g_uzip_request(gp, bp2);
453 g_uzip_start(struct bio *bp)
455 struct g_provider *pp;
457 struct g_uzip_softc *sc;
462 DPRINTF(GUZ_DBG_IO, ("%s/%s: %p: cmd=%d, offset=%jd, length=%jd, "
463 "buffer=%p\n", __func__, gp->name, bp, bp->bio_cmd,
464 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length, bp->bio_data));
469 if (bp->bio_cmd == BIO_GETATTR) {
471 struct g_consumer *cp;
473 struct g_provider *pp;
475 /* pass on MNT:* requests and ignore others */
476 if (strncmp(bp->bio_attribute, "MNT:", 4) == 0) {
477 bp2 = g_clone_bio(bp);
479 g_io_deliver(bp, ENOMEM);
482 bp2->bio_done = g_std_done;
485 cp = LIST_FIRST(&gp->consumer);
486 g_io_request(bp2, cp);
490 if (bp->bio_cmd != BIO_READ) {
491 g_io_deliver(bp, EOPNOTSUPP);
495 bp->bio_resid = bp->bio_length;
496 bp->bio_completed = 0;
498 g_uzip_request(gp, bp);
502 g_uzip_orphan(struct g_consumer *cp)
506 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->provider->name);
510 g_uzip_softc_free(gp->softc, gp);
512 g_wither_geom(gp, ENXIO);
516 g_uzip_access(struct g_provider *pp, int dr, int dw, int de)
519 struct g_consumer *cp;
522 cp = LIST_FIRST(&gp->consumer);
523 KASSERT (cp != NULL, ("g_uzip_access but no consumer"));
525 if (cp->acw + dw > 0)
528 return (g_access(cp, dr, dw, de));
532 g_uzip_spoiled(struct g_consumer *cp)
536 G_VALID_CONSUMER(cp);
538 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name);
541 g_uzip_softc_free(gp->softc, gp);
543 g_wither_geom(gp, ENXIO);
547 g_uzip_parse_toc(struct g_uzip_softc *sc, struct g_provider *pp,
550 uint32_t i, j, backref_to;
551 uint64_t max_offset, min_offset;
552 struct g_uzip_blk *last_blk;
554 min_offset = sizeof(struct cloop_header) +
555 (sc->nblocks + 1) * sizeof(uint64_t);
556 max_offset = sc->toc[0].offset - 1;
557 last_blk = &sc->toc[0];
558 for (i = 0; i < sc->nblocks; i++) {
559 /* First do some bounds checking */
560 if ((sc->toc[i].offset < min_offset) ||
561 (sc->toc[i].offset > pp->mediasize)) {
564 DPRINTF_BLK(GUZ_DBG_IO, i, ("%s: cluster #%u "
565 "offset=%ju max_offset=%ju\n", gp->name,
566 (u_int)i, (uintmax_t)sc->toc[i].offset,
567 (uintmax_t)max_offset));
568 backref_to = BLEN_UNDEF;
569 if (sc->toc[i].offset < max_offset) {
571 * For the backref'ed blocks search already parsed
572 * TOC entries for the matching offset and copy the
573 * size from matched entry.
575 for (j = 0; j <= i; j++) {
576 if (sc->toc[j].offset == sc->toc[i].offset &&
577 !BLK_IS_NIL(sc, j)) {
583 DPRINTF(GUZ_DBG_ERR, ("%s: cannot match "
584 "backref'ed offset at cluster #%u\n",
588 sc->toc[i].blen = sc->toc[j].blen;
591 last_blk = &sc->toc[i];
593 * For the "normal blocks" seek forward until we hit
594 * block whose offset is larger than ours and assume
595 * it's going to be the next one.
597 for (j = i + 1; j < sc->nblocks; j++) {
598 if (sc->toc[j].offset > max_offset) {
602 sc->toc[i].blen = sc->toc[j].offset -
604 if (BLK_ENDS(sc, i) > pp->mediasize) {
605 DPRINTF(GUZ_DBG_ERR, ("%s: cluster #%u "
606 "extends past media boundary (%ju > %ju)\n",
608 (uintmax_t)BLK_ENDS(sc, i),
609 (intmax_t)pp->mediasize));
612 KASSERT(max_offset <= sc->toc[i].offset, (
613 "%s: max_offset is incorrect: %ju",
614 gp->name, (uintmax_t)max_offset));
615 max_offset = BLK_ENDS(sc, i) - 1;
617 DPRINTF_BLK(GUZ_DBG_TOC, i, ("%s: cluster #%u, original %u "
618 "bytes, in %u bytes", gp->name, i, sc->blksz,
620 if (backref_to != BLEN_UNDEF) {
621 DPRINTF_BLK(GUZ_DBG_TOC, i, (" (->#%u)",
624 DPRINTF_BLK(GUZ_DBG_TOC, i, ("\n"));
627 /* Do a second pass to validate block lengths */
628 for (i = 0; i < sc->nblocks; i++) {
629 if (sc->toc[i].blen > sc->dcp->max_blen) {
630 if (sc->toc[i].last == 0) {
631 DPRINTF(GUZ_DBG_ERR, ("%s: cluster #%u "
632 "length (%ju) exceeds "
633 "max_blen (%ju)\n", gp->name, i,
634 (uintmax_t)sc->toc[i].blen,
635 (uintmax_t)sc->dcp->max_blen));
638 DPRINTF(GUZ_DBG_INFO, ("%s: cluster #%u extra "
639 "padding is detected, trimmed to %ju\n",
640 gp->name, i, (uintmax_t)sc->dcp->max_blen));
641 sc->toc[i].blen = sc->dcp->max_blen;
642 sc->toc[i].padded = 1;
648 DPRINTF(GUZ_DBG_ERR, ("%s: cluster #%u: invalid offset %ju, "
649 "min_offset=%ju mediasize=%jd\n", gp->name, (u_int)i,
650 sc->toc[i].offset, min_offset, pp->mediasize));
654 static struct g_geom *
655 g_uzip_taste(struct g_class *mp, struct g_provider *pp, int flags)
658 uint32_t i, total_offsets, offsets_read, blk;
660 struct cloop_header *header;
661 struct g_consumer *cp;
663 struct g_provider *pp2;
664 struct g_uzip_softc *sc;
670 g_trace(G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name);
673 /* Skip providers that are already open for writing. */
677 if ((fnmatch(g_uzip_attach_to, pp->name, 0) != 0) ||
678 (fnmatch(g_uzip_noattach_to, pp->name, 0) == 0)) {
679 DPRINTF(GUZ_DBG_INFO, ("%s(%s,%s), ignoring\n", __func__,
680 mp->name, pp->name));
687 * Create geom instance.
689 gp = g_new_geomf(mp, GUZ_DEV_NAME("%s"), pp->name);
690 cp = g_new_consumer(gp);
691 error = g_attach(cp, pp);
693 error = g_access(cp, 1, 0, 0);
700 * Read cloop header, look for CLOOP magic, perform
701 * other validity checks.
703 DPRINTF(GUZ_DBG_INFO, ("%s: media sectorsize %u, mediasize %jd\n",
704 gp->name, pp->sectorsize, (intmax_t)pp->mediasize));
705 buf = g_read_data(cp, 0, pp->sectorsize, NULL);
708 header = (struct cloop_header *) buf;
709 if (strncmp(header->magic, CLOOP_MAGIC_START,
710 sizeof(CLOOP_MAGIC_START) - 1) != 0) {
711 DPRINTF(GUZ_DBG_ERR, ("%s: no CLOOP magic\n", gp->name));
715 switch (header->magic[CLOOP_OFS_COMPR]) {
716 case CLOOP_COMP_LZMA:
717 case CLOOP_COMP_LZMA_DDP:
719 if (header->magic[CLOOP_OFS_VERSN] < CLOOP_MINVER_LZMA) {
720 DPRINTF(GUZ_DBG_ERR, ("%s: image version too old\n",
724 DPRINTF(GUZ_DBG_INFO, ("%s: GEOM_UZIP_LZMA image found\n",
727 case CLOOP_COMP_LIBZ:
728 case CLOOP_COMP_LIBZ_DDP:
730 if (header->magic[CLOOP_OFS_VERSN] < CLOOP_MINVER_ZLIB) {
731 DPRINTF(GUZ_DBG_ERR, ("%s: image version too old\n",
735 DPRINTF(GUZ_DBG_INFO, ("%s: GEOM_UZIP_ZLIB image found\n",
739 DPRINTF(GUZ_DBG_ERR, ("%s: unsupported image type\n",
745 * Initialize softc and read offsets.
747 sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO);
749 sc->blksz = ntohl(header->blksz);
750 sc->nblocks = ntohl(header->nblocks);
751 if (sc->blksz % 512 != 0) {
752 printf("%s: block size (%u) should be multiple of 512.\n",
753 gp->name, sc->blksz);
756 if (sc->blksz > MAX_BLKSZ) {
757 printf("%s: block size (%u) should not be larger than %d.\n",
758 gp->name, sc->blksz, MAX_BLKSZ);
760 total_offsets = sc->nblocks + 1;
761 if (sizeof(struct cloop_header) +
762 total_offsets * sizeof(uint64_t) > pp->mediasize) {
763 printf("%s: media too small for %u blocks\n",
764 gp->name, sc->nblocks);
767 sc->toc = malloc(total_offsets * sizeof(struct g_uzip_blk),
768 M_GEOM_UZIP, M_WAITOK | M_ZERO);
769 offsets_read = MIN(total_offsets,
770 (pp->sectorsize - sizeof(*header)) / sizeof(uint64_t));
771 for (i = 0; i < offsets_read; i++) {
772 sc->toc[i].offset = be64toh(((uint64_t *) (header + 1))[i]);
773 sc->toc[i].blen = BLEN_UNDEF;
775 DPRINTF(GUZ_DBG_INFO, ("%s: %u offsets in the first sector\n",
776 gp->name, offsets_read));
777 for (blk = 1; offsets_read < total_offsets; blk++) {
782 cp, blk * pp->sectorsize, pp->sectorsize, NULL);
785 nread = MIN(total_offsets - offsets_read,
786 pp->sectorsize / sizeof(uint64_t));
787 DPRINTF(GUZ_DBG_TOC, ("%s: %u offsets read from sector %d\n",
788 gp->name, nread, blk));
789 for (i = 0; i < nread; i++) {
790 sc->toc[offsets_read + i].offset =
791 be64toh(((uint64_t *) buf)[i]);
792 sc->toc[offsets_read + i].blen = BLEN_UNDEF;
794 offsets_read += nread;
799 DPRINTF(GUZ_DBG_INFO, ("%s: done reading %u block offsets from %u "
800 "sectors\n", gp->name, offsets_read, blk));
801 if (sc->nblocks != offsets_read) {
802 DPRINTF(GUZ_DBG_ERR, ("%s: read %s offsets than expected "
803 "blocks\n", gp->name,
804 sc->nblocks < offsets_read ? "more" : "less"));
808 if (type == G_UZIP) {
809 sc->dcp = g_uzip_zlib_ctor(sc->blksz);
811 sc->dcp = g_uzip_lzma_ctor(sc->blksz);
813 if (sc->dcp == NULL) {
818 * "Fake" last+1 block, to make it easier for the TOC parser to
819 * iterate without making the last element a special case.
821 sc->toc[sc->nblocks].offset = pp->mediasize;
822 /* Massage TOC (table of contents), make sure it is sound */
823 if (g_uzip_parse_toc(sc, pp, gp) != 0) {
824 DPRINTF(GUZ_DBG_ERR, ("%s: TOC error\n", gp->name));
827 mtx_init(&sc->last_mtx, "geom_uzip cache", NULL, MTX_DEF);
828 mtx_init(&sc->queue_mtx, "geom_uzip wrkthread", NULL, MTX_DEF);
829 bioq_init(&sc->bio_queue);
831 sc->last_buf = malloc(sc->blksz, M_GEOM_UZIP, M_WAITOK);
835 sc->uzip_do = &g_uzip_do;
837 error = kproc_create(g_uzip_wrkthr, sc, &sc->procp, 0, 0, "%s",
844 pp2 = g_new_providerf(gp, "%s", gp->name);
845 pp2->sectorsize = 512;
846 pp2->mediasize = (off_t)sc->nblocks * sc->blksz;
847 pp2->stripesize = pp->stripesize;
848 pp2->stripeoffset = pp->stripeoffset;
849 g_error_provider(pp2, 0);
850 g_access(cp, -1, 0, 0);
852 DPRINTF(GUZ_DBG_INFO, ("%s: taste ok (%d, %ju), (%ju, %ju), %x\n",
853 gp->name, pp2->sectorsize, (uintmax_t)pp2->mediasize,
854 (uintmax_t)pp2->stripeoffset, (uintmax_t)pp2->stripesize, pp2->flags));
855 DPRINTF(GUZ_DBG_INFO, ("%s: %u x %u blocks\n", gp->name, sc->nblocks,
860 free(sc->last_buf, M_GEOM);
861 mtx_destroy(&sc->queue_mtx);
862 mtx_destroy(&sc->last_mtx);
864 sc->dcp->free(sc->dcp);
866 free(sc->toc, M_GEOM);
868 free(gp->softc, M_GEOM_UZIP);
875 g_access(cp, -1, 0, 0);
878 g_destroy_consumer(cp);
885 g_uzip_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
887 struct g_provider *pp;
889 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, gp->name);
892 if (gp->softc == NULL) {
893 DPRINTF(GUZ_DBG_ERR, ("%s(%s): gp->softc == NULL\n", __func__,
898 KASSERT(gp != NULL, ("NULL geom"));
899 pp = LIST_FIRST(&gp->provider);
900 KASSERT(pp != NULL, ("NULL provider"));
901 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
904 g_uzip_softc_free(gp->softc, gp);
906 g_wither_geom(gp, ENXIO);
911 static struct g_class g_uzip_class = {
912 .name = UZIP_CLASS_NAME,
913 .version = G_VERSION,
914 .taste = g_uzip_taste,
915 .destroy_geom = g_uzip_destroy_geom,
917 .start = g_uzip_start,
918 .orphan = g_uzip_orphan,
919 .access = g_uzip_access,
920 .spoiled = g_uzip_spoiled,
923 DECLARE_GEOM_CLASS(g_uzip_class, g_uzip);
924 MODULE_DEPEND(g_uzip, zlib, 1, 1, 1);
925 MODULE_VERSION(geom_uzip, 0);