g_uzip.c revision 295952
1/*-
2 * Copyright (c) 2004 Max Khon
3 * Copyright (c) 2014 Juniper Networks, Inc.
4 * Copyright (c) 2006-2016 Maxim Sobolev <sobomax@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/geom/uzip/g_uzip.c 295952 2016-02-24 02:52:49Z adrian $");
31
32#include <sys/param.h>
33#include <sys/bio.h>
34#include <sys/endian.h>
35#include <sys/errno.h>
36#include <sys/kernel.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/malloc.h>
40#include <sys/sysctl.h>
41#include <sys/systm.h>
42#include <sys/kthread.h>
43
44#include <geom/geom.h>
45
46#include <geom/uzip/g_uzip.h>
47#include <geom/uzip/g_uzip_cloop.h>
48#include <geom/uzip/g_uzip_softc.h>
49#include <geom/uzip/g_uzip_dapi.h>
50#include <geom/uzip/g_uzip_zlib.h>
51#include <geom/uzip/g_uzip_lzma.h>
52#include <geom/uzip/g_uzip_wrkthr.h>
53
54MALLOC_DEFINE(M_GEOM_UZIP, "geom_uzip", "GEOM UZIP data structures");
55
56FEATURE(geom_uzip, "GEOM read-only compressed disks support");
57
58struct g_uzip_blk {
59        uint64_t offset;
60        uint32_t blen;
61#define BLEN_UNDEF      UINT32_MAX
62};
63
64#ifndef ABS
65#define	ABS(a)			((a) < 0 ? -(a) : (a))
66#endif
67
68#define BLK_IN_RANGE(mcn, bcn, ilen)	\
69    (((bcn) != BLEN_UNDEF) && ( \
70	((ilen) >= 0 && (mcn >= bcn) && (mcn <= ((intmax_t)(bcn) + (ilen)))) || \
71	((ilen) < 0 && (mcn <= bcn) && (mcn >= ((intmax_t)(bcn) + (ilen)))) \
72    ))
73
74#ifdef GEOM_UZIP_DEBUG
75# define GEOM_UZIP_DBG_DEFAULT	3
76#else
77# define GEOM_UZIP_DBG_DEFAULT	0
78#endif
79
80#define	GUZ_DBG_ERR	1
81#define	GUZ_DBG_INFO	2
82#define	GUZ_DBG_IO	3
83#define	GUZ_DBG_TOC	4
84
85SYSCTL_DECL(_kern_geom);
86SYSCTL_NODE(_kern_geom, OID_AUTO, uzip, CTLFLAG_RW, 0, "GEOM_UZIP stuff");
87static u_int g_uzip_debug = GEOM_UZIP_DBG_DEFAULT;
88SYSCTL_UINT(_kern_geom_uzip, OID_AUTO, debug, CTLFLAG_RWTUN, &g_uzip_debug, 0,
89    "Debug level (0-4)");
90static u_int g_uzip_debug_block = BLEN_UNDEF;
91SYSCTL_UINT(_kern_geom_uzip, OID_AUTO, debug_block, CTLFLAG_RWTUN,
92    &g_uzip_debug_block, 0, "Debug operations around specific cluster#");
93
94#define	DPRINTF(lvl, a)		\
95	if ((lvl) <= g_uzip_debug) { \
96		printf a; \
97	}
98#define	DPRINTF_BLK(lvl, cn, a)	\
99	if ((lvl) <= g_uzip_debug || \
100	    BLK_IN_RANGE(cn, g_uzip_debug_block, 8) || \
101	    BLK_IN_RANGE(cn, g_uzip_debug_block, -8)) { \
102		printf a; \
103	}
104#define	DPRINTF_BRNG(lvl, bcn, ecn, a) \
105	if (bcn >= ecn) { \
106		printf("DPRINTF_BRNG: invalid range (%ju, %ju), BUG BUG " \
107		    "BUG!\n", (uintmax_t)bcn, (uintmax_t)ecn); \
108	} else if (((lvl) <= g_uzip_debug) || \
109	    BLK_IN_RANGE(g_uzip_debug_block, bcn, \
110	     (intmax_t)ecn - (intmax_t)bcn)) { \
111		printf a; \
112	}
113
114#define	UZIP_CLASS_NAME	"UZIP"
115
116/*
117 * Maximum allowed valid block size (to prevent foot-shooting)
118 */
119#define	MAX_BLKSZ	(MAXPHYS)
120
121static char CLOOP_MAGIC_START[] = "#!/bin/sh\n";
122
123static void g_uzip_read_done(struct bio *bp);
124static void g_uzip_do(struct g_uzip_softc *, struct bio *bp);
125
126static void
127g_uzip_softc_free(struct g_uzip_softc *sc, struct g_geom *gp)
128{
129
130	if (gp != NULL) {
131		DPRINTF(GUZ_DBG_INFO, ("%s: %d requests, %d cached\n",
132		    gp->name, sc->req_total, sc->req_cached));
133	}
134
135	mtx_lock(&sc->queue_mtx);
136	sc->wrkthr_flags |= GUZ_SHUTDOWN;
137	wakeup(sc);
138	while (!(sc->wrkthr_flags & GUZ_EXITING)) {
139		msleep(sc->procp, &sc->queue_mtx, PRIBIO, "guzfree",
140		    hz / 10);
141	}
142	mtx_unlock(&sc->queue_mtx);
143
144	sc->dcp->free(sc->dcp);
145	free(sc->toc, M_GEOM_UZIP);
146	mtx_destroy(&sc->queue_mtx);
147	mtx_destroy(&sc->last_mtx);
148	free(sc->last_buf, M_GEOM_UZIP);
149	free(sc, M_GEOM_UZIP);
150}
151
152static int
153g_uzip_cached(struct g_geom *gp, struct bio *bp)
154{
155	struct g_uzip_softc *sc;
156	off_t ofs;
157	size_t blk, blkofs, usz;
158
159	sc = gp->softc;
160	ofs = bp->bio_offset + bp->bio_completed;
161	blk = ofs / sc->blksz;
162	mtx_lock(&sc->last_mtx);
163	if (blk == sc->last_blk) {
164		blkofs = ofs % sc->blksz;
165		usz = sc->blksz - blkofs;
166		if (bp->bio_resid < usz)
167			usz = bp->bio_resid;
168		memcpy(bp->bio_data + bp->bio_completed, sc->last_buf + blkofs,
169		    usz);
170		sc->req_cached++;
171		mtx_unlock(&sc->last_mtx);
172
173		DPRINTF(GUZ_DBG_IO, ("%s/%s: %p: offset=%jd: got %jd bytes "
174		    "from cache\n", __func__, gp->name, bp, (intmax_t)ofs,
175		    (intmax_t)usz));
176
177		bp->bio_completed += usz;
178		bp->bio_resid -= usz;
179
180		if (bp->bio_resid == 0) {
181			g_io_deliver(bp, 0);
182			return (1);
183		}
184	} else
185		mtx_unlock(&sc->last_mtx);
186
187	return (0);
188}
189
190#define BLK_ENDS(sc, bi)	((sc)->toc[(bi)].offset + \
191    (sc)->toc[(bi)].blen)
192
193#define BLK_IS_CONT(sc, bi)	(BLK_ENDS((sc), (bi) - 1) == \
194    (sc)->toc[(bi)].offset)
195#define	BLK_IS_NIL(sc, bi)	((sc)->toc[(bi)].blen == 0)
196
197#define TOFF_2_BOFF(sc, pp, bi)	    ((sc)->toc[(bi)].offset - \
198    (sc)->toc[(bi)].offset % (pp)->sectorsize)
199#define TLEN_2_BLEN(sc, pp, bp, ei) ((BLK_ENDS((sc), (ei)) - \
200    (bp)->bio_offset + (pp)->sectorsize - 1) / \
201    (pp)->sectorsize * (pp)->sectorsize)
202
203static int
204g_uzip_request(struct g_geom *gp, struct bio *bp)
205{
206	struct g_uzip_softc *sc;
207	struct bio *bp2;
208	struct g_consumer *cp;
209	struct g_provider *pp;
210	off_t ofs, start_blk_ofs;
211	size_t i, start_blk, end_blk, zsize;
212
213	if (g_uzip_cached(gp, bp) != 0)
214		return (1);
215
216	sc = gp->softc;
217
218	cp = LIST_FIRST(&gp->consumer);
219	pp = cp->provider;
220
221	ofs = bp->bio_offset + bp->bio_completed;
222	start_blk = ofs / sc->blksz;
223	KASSERT(start_blk < sc->nblocks, ("start_blk out of range"));
224	end_blk = (ofs + bp->bio_resid + sc->blksz - 1) / sc->blksz;
225	KASSERT(end_blk <= sc->nblocks, ("end_blk out of range"));
226
227	for (; BLK_IS_NIL(sc, start_blk) && start_blk < end_blk; start_blk++) {
228		/* Fill in any leading Nil blocks */
229		start_blk_ofs = ofs % sc->blksz;
230		zsize = MIN(sc->blksz - start_blk_ofs, bp->bio_resid);
231		DPRINTF_BLK(GUZ_DBG_IO, start_blk, ("%s/%s: %p/%ju: "
232		    "filling %ju zero bytes\n", __func__, gp->name, gp,
233		    (uintmax_t)bp->bio_completed, (uintmax_t)zsize));
234		bzero(bp->bio_data + bp->bio_completed, zsize);
235		bp->bio_completed += zsize;
236		bp->bio_resid -= zsize;
237		ofs += zsize;
238	}
239
240	if (start_blk == end_blk) {
241		KASSERT(bp->bio_resid == 0, ("bp->bio_resid is invalid"));
242		/*
243		 * No non-Nil data is left, complete request immediately.
244		 */
245		DPRINTF(GUZ_DBG_IO, ("%s/%s: %p: all done returning %ju "
246		    "bytes\n", __func__, gp->name, gp,
247		    (uintmax_t)bp->bio_completed));
248		g_io_deliver(bp, 0);
249		return (1);
250	}
251
252	for (i = start_blk + 1; i < end_blk; i++) {
253		/* Trim discontinuous areas if any */
254		if (!BLK_IS_CONT(sc, i)) {
255			end_blk = i;
256			break;
257		}
258	}
259
260	DPRINTF_BRNG(GUZ_DBG_IO, start_blk, end_blk, ("%s/%s: %p: "
261	    "start=%u (%ju), end=%u (%ju)\n", __func__, gp->name, bp,
262	    (u_int)start_blk, (uintmax_t)sc->toc[start_blk].offset,
263	    (u_int)end_blk, (uintmax_t)BLK_ENDS(sc, end_blk - 1)));
264
265	bp2 = g_clone_bio(bp);
266	if (bp2 == NULL) {
267		g_io_deliver(bp, ENOMEM);
268		return (1);
269	}
270	bp2->bio_done = g_uzip_read_done;
271
272	bp2->bio_offset = TOFF_2_BOFF(sc, pp, start_blk);
273	while (1) {
274		bp2->bio_length = TLEN_2_BLEN(sc, pp, bp2, end_blk - 1);
275		if (bp2->bio_length <= MAXPHYS)
276			break;
277
278		end_blk--;
279	}
280
281	bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UZIP, M_NOWAIT);
282	if (bp2->bio_data == NULL) {
283		g_destroy_bio(bp2);
284		g_io_deliver(bp, ENOMEM);
285		return (1);
286	}
287
288	DPRINTF_BRNG(GUZ_DBG_IO, start_blk, end_blk, ("%s/%s: %p: "
289	    "reading %jd bytes from offset %jd\n", __func__, gp->name, bp,
290	    (intmax_t)bp2->bio_length, (intmax_t)bp2->bio_offset));
291
292	g_io_request(bp2, cp);
293	return (0);
294}
295
296static void
297g_uzip_read_done(struct bio *bp)
298{
299	struct bio *bp2;
300	struct g_geom *gp;
301	struct g_uzip_softc *sc;
302
303	bp2 = bp->bio_parent;
304	gp = bp2->bio_to->geom;
305	sc = gp->softc;
306
307	mtx_lock(&sc->queue_mtx);
308	bioq_disksort(&sc->bio_queue, bp);
309	mtx_unlock(&sc->queue_mtx);
310	wakeup(sc);
311}
312
313static void
314g_uzip_do(struct g_uzip_softc *sc, struct bio *bp)
315{
316	struct bio *bp2;
317	struct g_provider *pp;
318	struct g_consumer *cp;
319	struct g_geom *gp;
320	char *data, *data2;
321	off_t ofs;
322	size_t blk, blkofs, len, ulen, firstblk;
323	int err;
324
325	bp2 = bp->bio_parent;
326	gp = bp2->bio_to->geom;
327
328	cp = LIST_FIRST(&gp->consumer);
329	pp = cp->provider;
330
331	bp2->bio_error = bp->bio_error;
332	if (bp2->bio_error != 0)
333		goto done;
334
335	/* Make sure there's forward progress. */
336	if (bp->bio_completed == 0) {
337		bp2->bio_error = ECANCELED;
338		goto done;
339	}
340
341	ofs = bp2->bio_offset + bp2->bio_completed;
342	firstblk = blk = ofs / sc->blksz;
343	blkofs = ofs % sc->blksz;
344	data = bp->bio_data + sc->toc[blk].offset % pp->sectorsize;
345	data2 = bp2->bio_data + bp2->bio_completed;
346	while (bp->bio_completed && bp2->bio_resid) {
347		if (blk > firstblk && !BLK_IS_CONT(sc, blk)) {
348			DPRINTF_BLK(GUZ_DBG_IO, blk, ("%s/%s: %p: backref'ed "
349			    "cluster #%u requested, looping around\n",
350			    __func__, gp->name, bp2, (u_int)blk));
351			goto done;
352		}
353		ulen = MIN(sc->blksz - blkofs, bp2->bio_resid);
354		len = sc->toc[blk].blen;
355		DPRINTF(GUZ_DBG_IO, ("%s/%s: %p/%ju: data2=%p, ulen=%u, "
356		    "data=%p, len=%u\n", __func__, gp->name, gp,
357		    bp->bio_completed, data2, (u_int)ulen, data, (u_int)len));
358		if (len == 0) {
359			/* All zero block: no cache update */
360			bzero(data2, ulen);
361		} else if (len <= bp->bio_completed) {
362			mtx_lock(&sc->last_mtx);
363			err = sc->dcp->decompress(sc->dcp, gp->name, data,
364			    len, sc->last_buf);
365			if (err != 0) {
366				sc->last_blk = -1;
367				mtx_unlock(&sc->last_mtx);
368				bp2->bio_error = EILSEQ;
369				DPRINTF(GUZ_DBG_ERR, ("%s/%s: decompress"
370				    "(%p) failed\n", __func__, gp->name,
371				    sc->dcp));
372				goto done;
373			}
374			sc->last_blk = blk;
375			memcpy(data2, sc->last_buf + blkofs, ulen);
376			mtx_unlock(&sc->last_mtx);
377			err = sc->dcp->rewind(sc->dcp, gp->name);
378			if (err != 0) {
379				bp2->bio_error = EILSEQ;
380				DPRINTF(GUZ_DBG_ERR, ("%s/%s: rewind(%p) "
381				    "failed\n", __func__, gp->name, sc->dcp));
382				goto done;
383			}
384			data += len;
385		} else
386			break;
387
388		data2 += ulen;
389		bp2->bio_completed += ulen;
390		bp2->bio_resid -= ulen;
391		bp->bio_completed -= len;
392		blkofs = 0;
393		blk++;
394	}
395
396done:
397	/* Finish processing the request. */
398	free(bp->bio_data, M_GEOM_UZIP);
399	g_destroy_bio(bp);
400	if (bp2->bio_error != 0 || bp2->bio_resid == 0)
401		g_io_deliver(bp2, bp2->bio_error);
402	else
403		g_uzip_request(gp, bp2);
404}
405
406static void
407g_uzip_start(struct bio *bp)
408{
409	struct g_provider *pp;
410	struct g_geom *gp;
411	struct g_uzip_softc *sc;
412
413	pp = bp->bio_to;
414	gp = pp->geom;
415
416	DPRINTF(GUZ_DBG_IO, ("%s/%s: %p: cmd=%d, offset=%jd, length=%jd, "
417	    "buffer=%p\n", __func__, gp->name, bp, bp->bio_cmd,
418	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length, bp->bio_data));
419
420	sc = gp->softc;
421	sc->req_total++;
422
423	if (bp->bio_cmd != BIO_READ) {
424		g_io_deliver(bp, EOPNOTSUPP);
425		return;
426	}
427
428	bp->bio_resid = bp->bio_length;
429	bp->bio_completed = 0;
430
431	g_uzip_request(gp, bp);
432}
433
434static void
435g_uzip_orphan(struct g_consumer *cp)
436{
437	struct g_geom *gp;
438
439	g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->provider->name);
440	g_topology_assert();
441
442	gp = cp->geom;
443	g_uzip_softc_free(gp->softc, gp);
444	gp->softc = NULL;
445	g_wither_geom(gp, ENXIO);
446}
447
448static int
449g_uzip_access(struct g_provider *pp, int dr, int dw, int de)
450{
451	struct g_geom *gp;
452	struct g_consumer *cp;
453
454	gp = pp->geom;
455	cp = LIST_FIRST(&gp->consumer);
456	KASSERT (cp != NULL, ("g_uzip_access but no consumer"));
457
458	if (cp->acw + dw > 0)
459		return (EROFS);
460
461	return (g_access(cp, dr, dw, de));
462}
463
464static void
465g_uzip_spoiled(struct g_consumer *cp)
466{
467	struct g_geom *gp;
468
469	gp = cp->geom;
470	g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name);
471	g_topology_assert();
472
473	g_uzip_softc_free(gp->softc, gp);
474	gp->softc = NULL;
475	g_wither_geom(gp, ENXIO);
476}
477
478static int
479g_uzip_parse_toc(struct g_uzip_softc *sc, struct g_provider *pp,
480    struct g_geom *gp)
481{
482	uint32_t i, j, backref_to;
483	uint64_t max_offset, min_offset;
484
485	min_offset = sizeof(struct cloop_header) +
486	    (sc->nblocks + 1) * sizeof(uint64_t);
487	max_offset = sc->toc[0].offset - 1;
488	for (i = 0; i < sc->nblocks; i++) {
489		/* First do some bounds checking */
490		if ((sc->toc[i].offset < min_offset) ||
491		    (sc->toc[i].offset >= pp->mediasize)) {
492			goto error_offset;
493		}
494		DPRINTF_BLK(GUZ_DBG_IO, i, ("%s: cluster #%u "
495		    "sc->toc[i].offset=%ju max_offset=%ju\n", gp->name,
496		    (u_int)i, (uintmax_t)sc->toc[i].offset,
497		    (uintmax_t)max_offset));
498		backref_to = BLEN_UNDEF;
499		if (sc->toc[i].offset < max_offset) {
500			/*
501			 * For the backref'ed blocks search already parsed
502			 * TOC entries for the matching offset and copy the
503			 * size from matched entry.
504			 */
505			for (j = 0; j <= i; j++) {
506                                if (sc->toc[j].offset == sc->toc[i].offset &&
507				    !BLK_IS_NIL(sc, j)) {
508                                        break;
509                                }
510                                if (j != i) {
511					continue;
512				}
513				DPRINTF(GUZ_DBG_ERR, ("%s: cannot match "
514				    "backref'ed offset at cluster #%u\n",
515				    gp->name, i));
516				return (-1);
517			}
518			sc->toc[i].blen = sc->toc[j].blen;
519			backref_to = j;
520		} else {
521			/*
522			 * For the "normal blocks" seek forward until we hit
523			 * block whose offset is larger than ours and assume
524			 * it's going to be the next one.
525			 */
526			for (j = i + 1; j < sc->nblocks; j++) {
527				if (sc->toc[j].offset > max_offset) {
528					break;
529				}
530			}
531			sc->toc[i].blen = sc->toc[j].offset -
532			    sc->toc[i].offset;
533			if (BLK_ENDS(sc, i) > pp->mediasize) {
534				DPRINTF(GUZ_DBG_ERR, ("%s: cluster #%u "
535				    "extends past media boundary (%ju > %ju)\n",
536				    gp->name, (u_int)i,
537				    (uintmax_t)BLK_ENDS(sc, i),
538				    (intmax_t)pp->mediasize));
539				return (-1);
540			}
541			KASSERT(max_offset <= sc->toc[i].offset, (
542			    "%s: max_offset is incorrect: %ju",
543			    gp->name, (uintmax_t)max_offset));
544			max_offset = BLK_ENDS(sc, i) - 1;
545		}
546		DPRINTF_BLK(GUZ_DBG_TOC, i, ("%s: cluster #%u, original %u "
547		    "bytes, in %u bytes", gp->name, i, sc->blksz,
548		    sc->toc[i].blen));
549		if (backref_to != BLEN_UNDEF) {
550			DPRINTF_BLK(GUZ_DBG_TOC, i, (" (->#%u)",
551			    (u_int)backref_to));
552		}
553		DPRINTF_BLK(GUZ_DBG_TOC, i, ("\n"));
554	}
555	return (0);
556
557error_offset:
558	DPRINTF(GUZ_DBG_ERR, ("%s: cluster #%u: invalid offset %ju, "
559	    "min_offset=%ju mediasize=%jd\n", gp->name, (u_int)i,
560	    sc->toc[i].offset, min_offset, pp->mediasize));
561	return (-1);
562}
563
564static struct g_geom *
565g_uzip_taste(struct g_class *mp, struct g_provider *pp, int flags)
566{
567	int error;
568	uint32_t i, total_offsets, offsets_read, blk;
569	void *buf;
570	struct cloop_header *header;
571	struct g_consumer *cp;
572	struct g_geom *gp;
573	struct g_provider *pp2;
574	struct g_uzip_softc *sc;
575	enum {
576		GEOM_UZIP = 1,
577		GEOM_ULZMA
578	} type;
579
580	g_trace(G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name);
581	g_topology_assert();
582
583	/* Skip providers that are already open for writing. */
584	if (pp->acw > 0)
585		return (NULL);
586
587	buf = NULL;
588
589	/*
590	 * Create geom instance.
591	 */
592	gp = g_new_geomf(mp, "%s.uzip", pp->name);
593	cp = g_new_consumer(gp);
594	error = g_attach(cp, pp);
595	if (error == 0)
596		error = g_access(cp, 1, 0, 0);
597	if (error) {
598		goto e1;
599	}
600	g_topology_unlock();
601
602	/*
603	 * Read cloop header, look for CLOOP magic, perform
604	 * other validity checks.
605	 */
606	DPRINTF(GUZ_DBG_INFO, ("%s: media sectorsize %u, mediasize %jd\n",
607	    gp->name, pp->sectorsize, (intmax_t)pp->mediasize));
608	buf = g_read_data(cp, 0, pp->sectorsize, NULL);
609	if (buf == NULL)
610		goto e2;
611	header = (struct cloop_header *) buf;
612	if (strncmp(header->magic, CLOOP_MAGIC_START,
613	    sizeof(CLOOP_MAGIC_START) - 1) != 0) {
614		DPRINTF(GUZ_DBG_ERR, ("%s: no CLOOP magic\n", gp->name));
615		goto e3;
616	}
617
618	switch (header->magic[CLOOP_OFS_COMPR]) {
619	case CLOOP_COMP_LZMA:
620	case CLOOP_COMP_LZMA_DDP:
621		type = GEOM_ULZMA;
622		if (header->magic[CLOOP_OFS_VERSN] < CLOOP_MINVER_LZMA) {
623			DPRINTF(GUZ_DBG_ERR, ("%s: image version too old\n",
624			    gp->name));
625			goto e3;
626		}
627		DPRINTF(GUZ_DBG_INFO, ("%s: GEOM_UZIP_LZMA image found\n",
628		    gp->name));
629		break;
630	case CLOOP_COMP_LIBZ:
631	case CLOOP_COMP_LIBZ_DDP:
632		type = GEOM_UZIP;
633		if (header->magic[CLOOP_OFS_VERSN] < CLOOP_MINVER_ZLIB) {
634			DPRINTF(GUZ_DBG_ERR, ("%s: image version too old\n",
635			    gp->name));
636			goto e3;
637		}
638		DPRINTF(GUZ_DBG_INFO, ("%s: GEOM_UZIP_ZLIB image found\n",
639		    gp->name));
640		break;
641	default:
642		DPRINTF(GUZ_DBG_ERR, ("%s: unsupported image type\n",
643		    gp->name));
644                goto e3;
645        }
646
647	/*
648	 * Initialize softc and read offsets.
649	 */
650	sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO);
651	gp->softc = sc;
652	sc->blksz = ntohl(header->blksz);
653	sc->nblocks = ntohl(header->nblocks);
654	if (sc->blksz % 512 != 0) {
655		printf("%s: block size (%u) should be multiple of 512.\n",
656		    gp->name, sc->blksz);
657		goto e4;
658	}
659	if (sc->blksz > MAX_BLKSZ) {
660		printf("%s: block size (%u) should not be larger than %d.\n",
661		    gp->name, sc->blksz, MAX_BLKSZ);
662	}
663	total_offsets = sc->nblocks + 1;
664	if (sizeof(struct cloop_header) +
665	    total_offsets * sizeof(uint64_t) > pp->mediasize) {
666		printf("%s: media too small for %u blocks\n",
667		    gp->name, sc->nblocks);
668		goto e4;
669	}
670	sc->toc = malloc(total_offsets * sizeof(struct g_uzip_blk),
671	    M_GEOM_UZIP, M_WAITOK | M_ZERO);
672	offsets_read = MIN(total_offsets,
673	    (pp->sectorsize - sizeof(*header)) / sizeof(uint64_t));
674	for (i = 0; i < offsets_read; i++) {
675		sc->toc[i].offset = be64toh(((uint64_t *) (header + 1))[i]);
676		sc->toc[i].blen = BLEN_UNDEF;
677	}
678	DPRINTF(GUZ_DBG_INFO, ("%s: %u offsets in the first sector\n",
679	       gp->name, offsets_read));
680	for (blk = 1; offsets_read < total_offsets; blk++) {
681		uint32_t nread;
682
683		free(buf, M_GEOM);
684		buf = g_read_data(
685		    cp, blk * pp->sectorsize, pp->sectorsize, NULL);
686		if (buf == NULL)
687			goto e5;
688		nread = MIN(total_offsets - offsets_read,
689		     pp->sectorsize / sizeof(uint64_t));
690		DPRINTF(GUZ_DBG_TOC, ("%s: %u offsets read from sector %d\n",
691		    gp->name, nread, blk));
692		for (i = 0; i < nread; i++) {
693			sc->toc[offsets_read + i].offset =
694			    be64toh(((uint64_t *) buf)[i]);
695			sc->toc[offsets_read + i].blen = BLEN_UNDEF;
696		}
697		offsets_read += nread;
698	}
699	free(buf, M_GEOM);
700	buf = NULL;
701	offsets_read -= 1;
702	DPRINTF(GUZ_DBG_INFO, ("%s: done reading %u block offsets from %u "
703	    "sectors\n", gp->name, offsets_read, blk));
704	if (sc->nblocks != offsets_read) {
705		DPRINTF(GUZ_DBG_ERR, ("%s: read %s offsets than expected "
706		    "blocks\n", gp->name,
707		    sc->nblocks < offsets_read ? "more" : "less"));
708		goto e5;
709	}
710	/* Massage TOC (table of contents), make sure it is sound */
711	if (g_uzip_parse_toc(sc, pp, gp) != 0) {
712		DPRINTF(GUZ_DBG_ERR, ("%s: TOC error\n", gp->name));
713		goto e5;
714	}
715	mtx_init(&sc->last_mtx, "geom_uzip cache", NULL, MTX_DEF);
716	mtx_init(&sc->queue_mtx, "geom_uzip wrkthread", NULL, MTX_DEF);
717	bioq_init(&sc->bio_queue);
718	sc->last_blk = -1;
719	sc->last_buf = malloc(sc->blksz, M_GEOM_UZIP, M_WAITOK);
720	sc->req_total = 0;
721	sc->req_cached = 0;
722
723	if (type == GEOM_UZIP) {
724		sc->dcp = g_uzip_zlib_ctor(sc->blksz);
725	} else {
726		sc->dcp = g_uzip_lzma_ctor(sc->blksz);
727	}
728	if (sc->dcp == NULL) {
729		goto e6;
730	}
731
732	sc->uzip_do = &g_uzip_do;
733
734	error = kproc_create(g_uzip_wrkthr, sc, &sc->procp, 0, 0, "%s",
735	    gp->name);
736	if (error != 0) {
737		goto e7;
738	}
739
740	g_topology_lock();
741	pp2 = g_new_providerf(gp, "%s", gp->name);
742	pp2->sectorsize = 512;
743	pp2->mediasize = (off_t)sc->nblocks * sc->blksz;
744	pp2->stripesize = pp->stripesize;
745	pp2->stripeoffset = pp->stripeoffset;
746	g_error_provider(pp2, 0);
747	g_access(cp, -1, 0, 0);
748
749	DPRINTF(GUZ_DBG_INFO, ("%s: taste ok (%d, %jd), (%d, %d), %x\n",
750	    gp->name, pp2->sectorsize, (intmax_t)pp2->mediasize,
751	    pp2->stripeoffset, pp2->stripesize, pp2->flags));
752	DPRINTF(GUZ_DBG_INFO, ("%s: %u x %u blocks\n", gp->name, sc->nblocks,
753	    sc->blksz));
754	return (gp);
755
756e7:
757	sc->dcp->free(sc->dcp);
758e6:
759	free(sc->last_buf, M_GEOM);
760	mtx_destroy(&sc->queue_mtx);
761	mtx_destroy(&sc->last_mtx);
762e5:
763	free(sc->toc, M_GEOM);
764e4:
765	free(gp->softc, M_GEOM_UZIP);
766e3:
767	if (buf != NULL) {
768		free(buf, M_GEOM);
769	}
770e2:
771	g_topology_lock();
772	g_access(cp, -1, 0, 0);
773e1:
774	g_detach(cp);
775	g_destroy_consumer(cp);
776	g_destroy_geom(gp);
777
778	return (NULL);
779}
780
781static int
782g_uzip_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
783{
784	struct g_provider *pp;
785
786	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, gp->name);
787	g_topology_assert();
788
789	if (gp->softc == NULL) {
790		DPRINTF(GUZ_DBG_ERR, ("%s(%s): gp->softc == NULL\n", __func__,
791		    gp->name));
792		return (ENXIO);
793	}
794
795	KASSERT(gp != NULL, ("NULL geom"));
796	pp = LIST_FIRST(&gp->provider);
797	KASSERT(pp != NULL, ("NULL provider"));
798	if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
799		return (EBUSY);
800
801	g_uzip_softc_free(gp->softc, gp);
802	gp->softc = NULL;
803	g_wither_geom(gp, ENXIO);
804
805	return (0);
806}
807
808static struct g_class g_uzip_class = {
809	.name = UZIP_CLASS_NAME,
810	.version = G_VERSION,
811	.taste = g_uzip_taste,
812	.destroy_geom = g_uzip_destroy_geom,
813
814	.start = g_uzip_start,
815	.orphan = g_uzip_orphan,
816	.access = g_uzip_access,
817	.spoiled = g_uzip_spoiled,
818};
819
820DECLARE_GEOM_CLASS(g_uzip_class, g_uzip);
821MODULE_DEPEND(g_uzip, zlib, 1, 1, 1);
822