[Midnightbsd-cvs] src [9965] trunk/sys/geom: sync with freebsd 10-stable

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sat May 26 11:16:10 EDT 2018


Revision: 9965
          http://svnweb.midnightbsd.org/src/?rev=9965
Author:   laffer1
Date:     2018-05-26 11:16:10 -0400 (Sat, 26 May 2018)
Log Message:
-----------
sync with freebsd 10-stable

Modified Paths:
--------------
    trunk/sys/geom/uzip/g_uzip.c
    trunk/sys/geom/virstor/g_virstor.c
    trunk/sys/geom/zero/g_zero.c

Added Paths:
-----------
    trunk/sys/geom/uzip/g_uzip.h
    trunk/sys/geom/uzip/g_uzip_cloop.h
    trunk/sys/geom/uzip/g_uzip_dapi.h
    trunk/sys/geom/uzip/g_uzip_lzma.c
    trunk/sys/geom/uzip/g_uzip_lzma.h
    trunk/sys/geom/uzip/g_uzip_softc.h
    trunk/sys/geom/uzip/g_uzip_wrkthr.c
    trunk/sys/geom/uzip/g_uzip_wrkthr.h
    trunk/sys/geom/uzip/g_uzip_zlib.c
    trunk/sys/geom/uzip/g_uzip_zlib.h

Modified: trunk/sys/geom/uzip/g_uzip.c
===================================================================
--- trunk/sys/geom/uzip/g_uzip.c	2018-05-26 15:13:49 UTC (rev 9964)
+++ trunk/sys/geom/uzip/g_uzip.c	2018-05-26 15:16:10 UTC (rev 9965)
@@ -1,6 +1,8 @@
 /* $MidnightBSD$ */
 /*-
  * Copyright (c) 2004 Max Khon
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/geom/uzip/g_uzip.c,v 1.12 2007/04/24 06:30:06 simokawa Exp $");
+__FBSDID("$FreeBSD: stable/10/sys/geom/uzip/g_uzip.c 303167 2016-07-21 23:49:26Z sobomax $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -36,275 +38,443 @@
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/malloc.h>
+#include <sys/sysctl.h>
 #include <sys/systm.h>
-#include <sys/sysctl.h>
+#include <sys/kthread.h>
 
 #include <geom/geom.h>
-#include <net/zlib.h>
 
-FEATURE(geom_uzip, "GEOM uzip read-only compressed disks support");
+#include <geom/uzip/g_uzip.h>
+#include <geom/uzip/g_uzip_cloop.h>
+#include <geom/uzip/g_uzip_softc.h>
+#include <geom/uzip/g_uzip_dapi.h>
+#include <geom/uzip/g_uzip_zlib.h>
+#include <geom/uzip/g_uzip_lzma.h>
+#include <geom/uzip/g_uzip_wrkthr.h>
 
-#undef GEOM_UZIP_DEBUG
+#include "opt_geom.h"
+
+MALLOC_DEFINE(M_GEOM_UZIP, "geom_uzip", "GEOM UZIP data structures");
+
+FEATURE(geom_uzip, "GEOM read-only compressed disks support");
+
+struct g_uzip_blk {
+        uint64_t offset;
+        uint32_t blen;
+        unsigned char last:1;
+        unsigned char padded:1;
+#define BLEN_UNDEF      UINT32_MAX
+};
+
+#ifndef ABS
+#define	ABS(a)			((a) < 0 ? -(a) : (a))
+#endif
+
+#define BLK_IN_RANGE(mcn, bcn, ilen)	\
+    (((bcn) != BLEN_UNDEF) && ( \
+	((ilen) >= 0 && (mcn >= bcn) && (mcn <= ((intmax_t)(bcn) + (ilen)))) || \
+	((ilen) < 0 && (mcn <= bcn) && (mcn >= ((intmax_t)(bcn) + (ilen)))) \
+    ))
+
 #ifdef GEOM_UZIP_DEBUG
-#define DPRINTF(a)	printf a
+# define GEOM_UZIP_DBG_DEFAULT	3
 #else
-#define DPRINTF(a)
+# define GEOM_UZIP_DBG_DEFAULT	0
 #endif
 
-static MALLOC_DEFINE(M_GEOM_UZIP, "geom_uzip", "GEOM UZIP data structures");
+#define	GUZ_DBG_ERR	1
+#define	GUZ_DBG_INFO	2
+#define	GUZ_DBG_IO	3
+#define	GUZ_DBG_TOC	4
 
-#define UZIP_CLASS_NAME	"UZIP"
+#define	GUZ_DEV_SUFX	".uzip"
+#define	GUZ_DEV_NAME(p)	(p GUZ_DEV_SUFX)
 
+static char g_uzip_attach_to[MAXPATHLEN] = {"*"};
+static char g_uzip_noattach_to[MAXPATHLEN] = {GUZ_DEV_NAME("*")};
+TUNABLE_STR("kern.geom.uzip.attach_to", g_uzip_attach_to,
+    sizeof(g_uzip_attach_to));
+TUNABLE_STR("kern.geom.uzip.noattach_to", g_uzip_noattach_to,
+    sizeof(g_uzip_noattach_to));
+
+SYSCTL_DECL(_kern_geom);
+SYSCTL_NODE(_kern_geom, OID_AUTO, uzip, CTLFLAG_RW, 0, "GEOM_UZIP stuff");
+static u_int g_uzip_debug = GEOM_UZIP_DBG_DEFAULT;
+SYSCTL_UINT(_kern_geom_uzip, OID_AUTO, debug, CTLFLAG_RWTUN, &g_uzip_debug, 0,
+    "Debug level (0-4)");
+static u_int g_uzip_debug_block = BLEN_UNDEF;
+SYSCTL_UINT(_kern_geom_uzip, OID_AUTO, debug_block, CTLFLAG_RWTUN,
+    &g_uzip_debug_block, 0, "Debug operations around specific cluster#");
+
+#define	DPRINTF(lvl, a)		\
+	if ((lvl) <= g_uzip_debug) { \
+		printf a; \
+	}
+#define	DPRINTF_BLK(lvl, cn, a)	\
+	if ((lvl) <= g_uzip_debug || \
+	    BLK_IN_RANGE(cn, g_uzip_debug_block, 8) || \
+	    BLK_IN_RANGE(cn, g_uzip_debug_block, -8)) { \
+		printf a; \
+	}
+#define	DPRINTF_BRNG(lvl, bcn, ecn, a) \
+	KASSERT(bcn < ecn, ("DPRINTF_BRNG: invalid range (%ju, %ju)", \
+	    (uintmax_t)bcn, (uintmax_t)ecn)); \
+	if (((lvl) <= g_uzip_debug) || \
+	    BLK_IN_RANGE(g_uzip_debug_block, bcn, \
+	     (intmax_t)ecn - (intmax_t)bcn)) { \
+		printf a; \
+	}
+
+#define	UZIP_CLASS_NAME	"UZIP"
+
 /*
  * Maximum allowed valid block size (to prevent foot-shooting)
  */
-#define MAX_BLKSZ	(MAXPHYS - MAXPHYS / 1000 - 12)
+#define	MAX_BLKSZ	(MAXPHYS)
 
-/*
- * Integer values (block size, number of blocks, offsets)
- * are stored in big-endian (network) order on disk and struct cloop_header
- * and in native order in struct g_uzip_softc
- */
-
-#define CLOOP_MAGIC_LEN 128
 static char CLOOP_MAGIC_START[] = "#!/bin/sh\n";
 
-struct cloop_header {
-	char magic[CLOOP_MAGIC_LEN];	/* cloop magic */
-	uint32_t blksz;			/* block size */
-	uint32_t nblocks;		/* number of blocks */
-};
+static void g_uzip_read_done(struct bio *bp);
+static void g_uzip_do(struct g_uzip_softc *, struct bio *bp);
 
-struct g_uzip_softc {
-	uint32_t blksz;			/* block size */
-	uint32_t nblocks;		/* number of blocks */
-	uint64_t *offsets;
-
-	struct mtx last_mtx;
-	uint32_t last_blk;		/* last blk no */
-	char *last_buf;			/* last blk data */
-	int req_total;			/* total requests */
-	int req_cached;			/* cached requests */
-};
-
 static void
 g_uzip_softc_free(struct g_uzip_softc *sc, struct g_geom *gp)
 {
+
 	if (gp != NULL) {
-		printf("%s: %d requests, %d cached\n",
-		    gp->name, sc->req_total, sc->req_cached);
+		DPRINTF(GUZ_DBG_INFO, ("%s: %d requests, %d cached\n",
+		    gp->name, sc->req_total, sc->req_cached));
 	}
-	if (sc->offsets != NULL)
-		free(sc->offsets, M_GEOM_UZIP);
+
+	mtx_lock(&sc->queue_mtx);
+	sc->wrkthr_flags |= GUZ_SHUTDOWN;
+	wakeup(sc);
+	while (!(sc->wrkthr_flags & GUZ_EXITING)) {
+		msleep(sc->procp, &sc->queue_mtx, PRIBIO, "guzfree",
+		    hz / 10);
+	}
+	mtx_unlock(&sc->queue_mtx);
+
+	sc->dcp->free(sc->dcp);
+	free(sc->toc, M_GEOM_UZIP);
+	mtx_destroy(&sc->queue_mtx);
 	mtx_destroy(&sc->last_mtx);
 	free(sc->last_buf, M_GEOM_UZIP);
 	free(sc, M_GEOM_UZIP);
 }
 
-static void *
-z_alloc(void *nil, u_int type, u_int size)
+static int
+g_uzip_cached(struct g_geom *gp, struct bio *bp)
 {
-	void *ptr;
+	struct g_uzip_softc *sc;
+	off_t ofs;
+	size_t blk, blkofs, usz;
 
-	ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT);
-	return ptr;
+	sc = gp->softc;
+	ofs = bp->bio_offset + bp->bio_completed;
+	blk = ofs / sc->blksz;
+	mtx_lock(&sc->last_mtx);
+	if (blk == sc->last_blk) {
+		blkofs = ofs % sc->blksz;
+		usz = sc->blksz - blkofs;
+		if (bp->bio_resid < usz)
+			usz = bp->bio_resid;
+		memcpy(bp->bio_data + bp->bio_completed, sc->last_buf + blkofs,
+		    usz);
+		sc->req_cached++;
+		mtx_unlock(&sc->last_mtx);
+
+		DPRINTF(GUZ_DBG_IO, ("%s/%s: %p: offset=%jd: got %jd bytes "
+		    "from cache\n", __func__, gp->name, bp, (intmax_t)ofs,
+		    (intmax_t)usz));
+
+		bp->bio_completed += usz;
+		bp->bio_resid -= usz;
+
+		if (bp->bio_resid == 0) {
+			g_io_deliver(bp, 0);
+			return (1);
+		}
+	} else
+		mtx_unlock(&sc->last_mtx);
+
+	return (0);
 }
 
+#define BLK_ENDS(sc, bi)	((sc)->toc[(bi)].offset + \
+    (sc)->toc[(bi)].blen)
+
+#define BLK_IS_CONT(sc, bi)	(BLK_ENDS((sc), (bi) - 1) == \
+    (sc)->toc[(bi)].offset)
+#define	BLK_IS_NIL(sc, bi)	((sc)->toc[(bi)].blen == 0)
+
+#define TOFF_2_BOFF(sc, pp, bi)	    ((sc)->toc[(bi)].offset - \
+    (sc)->toc[(bi)].offset % (pp)->sectorsize)
+#define TLEN_2_BLEN(sc, pp, bp, ei) ((BLK_ENDS((sc), (ei)) - \
+    (bp)->bio_offset + (pp)->sectorsize - 1) / \
+    (pp)->sectorsize * (pp)->sectorsize)
+
+static int
+g_uzip_request(struct g_geom *gp, struct bio *bp)
+{
+	struct g_uzip_softc *sc;
+	struct bio *bp2;
+	struct g_consumer *cp;
+	struct g_provider *pp;
+	off_t ofs, start_blk_ofs;
+	size_t i, start_blk, end_blk, zsize;
+
+	if (g_uzip_cached(gp, bp) != 0)
+		return (1);
+
+	sc = gp->softc;
+
+	cp = LIST_FIRST(&gp->consumer);
+	pp = cp->provider;
+
+	ofs = bp->bio_offset + bp->bio_completed;
+	start_blk = ofs / sc->blksz;
+	KASSERT(start_blk < sc->nblocks, ("start_blk out of range"));
+	end_blk = (ofs + bp->bio_resid + sc->blksz - 1) / sc->blksz;
+	KASSERT(end_blk <= sc->nblocks, ("end_blk out of range"));
+
+	for (; BLK_IS_NIL(sc, start_blk) && start_blk < end_blk; start_blk++) {
+		/* Fill in any leading Nil blocks */
+		start_blk_ofs = ofs % sc->blksz;
+		zsize = MIN(sc->blksz - start_blk_ofs, bp->bio_resid);
+		DPRINTF_BLK(GUZ_DBG_IO, start_blk, ("%s/%s: %p/%ju: "
+		    "filling %ju zero bytes\n", __func__, gp->name, gp,
+		    (uintmax_t)bp->bio_completed, (uintmax_t)zsize));
+		bzero(bp->bio_data + bp->bio_completed, zsize);
+		bp->bio_completed += zsize;
+		bp->bio_resid -= zsize;
+		ofs += zsize;
+	}
+
+	if (start_blk == end_blk) {
+		KASSERT(bp->bio_resid == 0, ("bp->bio_resid is invalid"));
+		/*
+		 * No non-Nil data is left, complete request immediately.
+		 */
+		DPRINTF(GUZ_DBG_IO, ("%s/%s: %p: all done returning %ju "
+		    "bytes\n", __func__, gp->name, gp,
+		    (uintmax_t)bp->bio_completed));
+		g_io_deliver(bp, 0);
+		return (1);
+	}
+
+	for (i = start_blk + 1; i < end_blk; i++) {
+		/* Trim discontinuous areas if any */
+		if (!BLK_IS_CONT(sc, i)) {
+			end_blk = i;
+			break;
+		}
+	}
+
+	DPRINTF_BRNG(GUZ_DBG_IO, start_blk, end_blk, ("%s/%s: %p: "
+	    "start=%u (%ju[%jd]), end=%u (%ju)\n", __func__, gp->name, bp,
+	    (u_int)start_blk, (uintmax_t)sc->toc[start_blk].offset,
+	    (intmax_t)sc->toc[start_blk].blen,
+	    (u_int)end_blk, (uintmax_t)BLK_ENDS(sc, end_blk - 1)));
+
+	bp2 = g_clone_bio(bp);
+	if (bp2 == NULL) {
+		g_io_deliver(bp, ENOMEM);
+		return (1);
+	}
+	bp2->bio_done = g_uzip_read_done;
+
+	bp2->bio_offset = TOFF_2_BOFF(sc, pp, start_blk);
+	while (1) {
+		bp2->bio_length = TLEN_2_BLEN(sc, pp, bp2, end_blk - 1);
+		if (bp2->bio_length <= MAXPHYS) {
+			break;
+		}
+		if (end_blk == (start_blk + 1)) {
+			break;
+		}
+		end_blk--;
+	}
+
+	DPRINTF(GUZ_DBG_IO, ("%s/%s: bp2->bio_length = %jd, "
+	    "bp2->bio_offset = %jd\n", __func__, gp->name,
+	    (intmax_t)bp2->bio_length, (intmax_t)bp2->bio_offset));
+
+	bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UZIP, M_NOWAIT);
+	if (bp2->bio_data == NULL) {
+		g_destroy_bio(bp2);
+		g_io_deliver(bp, ENOMEM);
+		return (1);
+	}
+
+	DPRINTF_BRNG(GUZ_DBG_IO, start_blk, end_blk, ("%s/%s: %p: "
+	    "reading %jd bytes from offset %jd\n", __func__, gp->name, bp,
+	    (intmax_t)bp2->bio_length, (intmax_t)bp2->bio_offset));
+
+	g_io_request(bp2, cp);
+	return (0);
+}
+
 static void
-z_free(void *nil, void *ptr)
+g_uzip_read_done(struct bio *bp)
 {
-	free(ptr, M_GEOM_UZIP);
+	struct bio *bp2;
+	struct g_geom *gp;
+	struct g_uzip_softc *sc;
+
+	bp2 = bp->bio_parent;
+	gp = bp2->bio_to->geom;
+	sc = gp->softc;
+
+	mtx_lock(&sc->queue_mtx);
+	bioq_disksort(&sc->bio_queue, bp);
+	mtx_unlock(&sc->queue_mtx);
+	wakeup(sc);
 }
 
+static int
+g_uzip_memvcmp(const void *memory, unsigned char val, size_t size)
+{
+	const u_char *mm;
+
+	mm = (const u_char *)memory;
+	return (*mm == val) && memcmp(mm, mm + 1, size - 1) == 0;
+}
+
 static void
-g_uzip_done(struct bio *bp)
+g_uzip_do(struct g_uzip_softc *sc, struct bio *bp)
 {
-	int err;
 	struct bio *bp2;
-	z_stream zs;
-	struct g_provider *pp, *pp2;
+	struct g_provider *pp;
 	struct g_consumer *cp;
 	struct g_geom *gp;
-	struct g_uzip_softc *sc;
-	off_t pos, upos;
-	uint32_t start_blk, i;
-	size_t bsize;
+	char *data, *data2;
+	off_t ofs;
+	size_t blk, blkofs, len, ulen, firstblk;
+	int err;
 
 	bp2 = bp->bio_parent;
-	pp = bp2->bio_to;
-	gp = pp->geom;
+	gp = bp2->bio_to->geom;
+
 	cp = LIST_FIRST(&gp->consumer);
-	pp2 = cp->provider;
-	sc = gp->softc;
-	DPRINTF(("%s: done\n", gp->name));
+	pp = cp->provider;
 
 	bp2->bio_error = bp->bio_error;
 	if (bp2->bio_error != 0)
 		goto done;
 
-	/*
-	 * Uncompress data.
-	 */
-	zs.zalloc = z_alloc;
-	zs.zfree = z_free;
-	err = inflateInit(&zs);
-	if (err != Z_OK) {
-		bp2->bio_error = EIO;
+	/* Make sure there's forward progress. */
+	if (bp->bio_completed == 0) {
+		bp2->bio_error = ECANCELED;
 		goto done;
 	}
-	start_blk = bp2->bio_offset / sc->blksz;
-	bsize = pp2->sectorsize;
-	pos = sc->offsets[start_blk] % bsize;
-	upos = 0;
-	DPRINTF(("%s: done: start_blk %d, pos %lld, upos %lld (%lld, %d, %d)\n",
-	    gp->name, start_blk, pos, upos,
-	    bp2->bio_offset, sc->blksz, bsize));
-	for (i = start_blk; upos < bp2->bio_length; i++) {
-		off_t len, ulen, uoff;
 
-		uoff = i == start_blk ? bp2->bio_offset % sc->blksz : 0;
-		ulen = MIN(sc->blksz - uoff, bp2->bio_length - upos);
-		len = sc->offsets[i + 1] - sc->offsets[i];
-
+	ofs = bp2->bio_offset + bp2->bio_completed;
+	firstblk = blk = ofs / sc->blksz;
+	blkofs = ofs % sc->blksz;
+	data = bp->bio_data + sc->toc[blk].offset % pp->sectorsize;
+	data2 = bp2->bio_data + bp2->bio_completed;
+	while (bp->bio_completed && bp2->bio_resid) {
+		if (blk > firstblk && !BLK_IS_CONT(sc, blk)) {
+			DPRINTF_BLK(GUZ_DBG_IO, blk, ("%s/%s: %p: backref'ed "
+			    "cluster #%u requested, looping around\n",
+			    __func__, gp->name, bp2, (u_int)blk));
+			goto done;
+		}
+		ulen = MIN(sc->blksz - blkofs, bp2->bio_resid);
+		len = sc->toc[blk].blen;
+		DPRINTF(GUZ_DBG_IO, ("%s/%s: %p/%ju: data2=%p, ulen=%u, "
+		    "data=%p, len=%u\n", __func__, gp->name, gp,
+		    bp->bio_completed, data2, (u_int)ulen, data, (u_int)len));
 		if (len == 0) {
 			/* All zero block: no cache update */
-			bzero(bp2->bio_data + upos, ulen);
-			upos += ulen;
-			bp2->bio_completed += ulen;
-			continue;
-		}
-		zs.next_in = bp->bio_data + pos;
-		zs.avail_in = len;
-		zs.next_out = sc->last_buf;
-		zs.avail_out = sc->blksz;
-		mtx_lock(&sc->last_mtx);
-		err = inflate(&zs, Z_FINISH);
-		if (err != Z_STREAM_END) {
-			sc->last_blk = -1;
+zero_block:
+			bzero(data2, ulen);
+		} else if (len <= bp->bio_completed) {
+			mtx_lock(&sc->last_mtx);
+			err = sc->dcp->decompress(sc->dcp, gp->name, data,
+			    len, sc->last_buf);
+			if (err != 0 && sc->toc[blk].last != 0) {
+				/*
+				 * Last block decompression has failed, check
+				 * if it's just zero padding.
+				 */
+				if (g_uzip_memvcmp(data, '\0', len) == 0) {
+					sc->toc[blk].blen = 0;
+					sc->last_blk = -1;
+					mtx_unlock(&sc->last_mtx);
+					len = 0;
+					goto zero_block;
+				}
+			}
+			if (err != 0) {
+				sc->last_blk = -1;
+				mtx_unlock(&sc->last_mtx);
+				bp2->bio_error = EILSEQ;
+				DPRINTF(GUZ_DBG_ERR, ("%s/%s: decompress"
+				    "(%p, %ju, %ju) failed\n", __func__,
+				    gp->name, sc->dcp, (uintmax_t)blk,
+				    (uintmax_t)len));
+				goto done;
+			}
+			sc->last_blk = blk;
+			memcpy(data2, sc->last_buf + blkofs, ulen);
 			mtx_unlock(&sc->last_mtx);
-			DPRINTF(("%s: done: inflate failed (%lld + %lld -> %lld + %lld + %lld)\n",
-			    gp->name, pos, len, uoff, upos, ulen));
-			inflateEnd(&zs);
-			bp2->bio_error = EIO;
-			goto done;
-		}
-		sc->last_blk = i;
-		DPRINTF(("%s: done: inflated %lld + %lld -> %lld + %lld + %lld\n",
-		    gp->name,
-		    pos, len,
-		    uoff, upos, ulen));
-		memcpy(bp2->bio_data + upos, sc->last_buf + uoff, ulen);
-		mtx_unlock(&sc->last_mtx);
+			err = sc->dcp->rewind(sc->dcp, gp->name);
+			if (err != 0) {
+				bp2->bio_error = EILSEQ;
+				DPRINTF(GUZ_DBG_ERR, ("%s/%s: rewind(%p) "
+				    "failed\n", __func__, gp->name, sc->dcp));
+				goto done;
+			}
+			data += len;
+		} else
+			break;
 
-		pos += len;
-		upos += ulen;
+		data2 += ulen;
 		bp2->bio_completed += ulen;
-		err = inflateReset(&zs);
-		if (err != Z_OK) {
-			inflateEnd(&zs);
-			bp2->bio_error = EIO;
-			goto done;
-		}
+		bp2->bio_resid -= ulen;
+		bp->bio_completed -= len;
+		blkofs = 0;
+		blk++;
 	}
-	err = inflateEnd(&zs);
-	if (err != Z_OK) {
-		bp2->bio_error = EIO;
-		goto done;
-	}
 
 done:
-	/*
-	 * Finish processing the request.
-	 */
-	DPRINTF(("%s: done: (%d, %lld, %ld)\n",
-	    gp->name, bp2->bio_error, bp2->bio_completed, bp2->bio_resid));
+	/* Finish processing the request. */
 	free(bp->bio_data, M_GEOM_UZIP);
 	g_destroy_bio(bp);
-	g_io_deliver(bp2, bp2->bio_error);
+	if (bp2->bio_error != 0 || bp2->bio_resid == 0)
+		g_io_deliver(bp2, bp2->bio_error);
+	else
+		g_uzip_request(gp, bp2);
 }
 
 static void
 g_uzip_start(struct bio *bp)
 {
-	struct bio *bp2;
-	struct g_provider *pp, *pp2;
+	struct g_provider *pp;
 	struct g_geom *gp;
-	struct g_consumer *cp;
 	struct g_uzip_softc *sc;
-	uint32_t start_blk, end_blk;
-	size_t bsize;
 
 	pp = bp->bio_to;
 	gp = pp->geom;
-	DPRINTF(("%s: start (%d)\n", gp->name, bp->bio_cmd));
 
-	if (bp->bio_cmd != BIO_READ) {
-		g_io_deliver(bp, EOPNOTSUPP);
-		return;
-	}
+	DPRINTF(GUZ_DBG_IO, ("%s/%s: %p: cmd=%d, offset=%jd, length=%jd, "
+	    "buffer=%p\n", __func__, gp->name, bp, bp->bio_cmd,
+	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length, bp->bio_data));
 
-	cp = LIST_FIRST(&gp->consumer);
-	pp2 = cp->provider;
 	sc = gp->softc;
-
-	start_blk = bp->bio_offset / sc->blksz;
-	end_blk = (bp->bio_offset + bp->bio_length + sc->blksz - 1) / sc->blksz;
-	KASSERT(start_blk < sc->nblocks,
-		("start_blk out of range"));
-	KASSERT(end_blk <= sc->nblocks,
-		("end_blk out of range"));
-
 	sc->req_total++;
-	if (start_blk + 1 == end_blk) {
-		mtx_lock(&sc->last_mtx);
-		if (start_blk == sc->last_blk) {
-			off_t uoff;
 
-			uoff = bp->bio_offset % sc->blksz;
-			KASSERT(bp->bio_length <= sc->blksz - uoff,
-			    ("cached data error"));
-			memcpy(bp->bio_data, sc->last_buf + uoff,
-			    bp->bio_length);
-			sc->req_cached++;
-			mtx_unlock(&sc->last_mtx);
-
-			DPRINTF(("%s: start: cached 0 + %lld, %lld + 0 + %lld\n",
-			    gp->name, bp->bio_length, uoff, bp->bio_length));
-			bp->bio_completed = bp->bio_length;
-			g_io_deliver(bp, 0);
-			return;
-		}
-		mtx_unlock(&sc->last_mtx);
-	}
-
-	bp2 = g_clone_bio(bp);
-	if (bp2 == NULL) {
-		g_io_deliver(bp, ENOMEM);
+	if (bp->bio_cmd != BIO_READ) {
+		g_io_deliver(bp, EOPNOTSUPP);
 		return;
 	}
-	bp2->bio_done = g_uzip_done;
-	DPRINTF(("%s: start (%d..%d), %s: %d + %lld, %s: %d + %lld\n",
-	    gp->name, start_blk, end_blk,
-	    pp->name, pp->sectorsize, pp->mediasize,
-	    pp2->name, pp2->sectorsize, pp2->mediasize));
-	bsize = pp2->sectorsize;
-	bp2->bio_offset = sc->offsets[start_blk] - sc->offsets[start_blk] % bsize;
-	bp2->bio_length = sc->offsets[end_blk] - bp2->bio_offset;
-	bp2->bio_length = (bp2->bio_length + bsize - 1) / bsize * bsize;
-	DPRINTF(("%s: start %lld + %lld -> %lld + %lld -> %lld + %lld\n",
-	    gp->name,
-	    bp->bio_offset, bp->bio_length,
-	    sc->offsets[start_blk], sc->offsets[end_blk] - sc->offsets[start_blk],
-	    bp2->bio_offset, bp2->bio_length));
-	bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UZIP, M_NOWAIT);
-	if (bp2->bio_data == NULL) {
-		g_destroy_bio(bp2);
-		g_io_deliver(bp, ENOMEM);
-		return;
-	}
 
-	g_io_request(bp2, cp);
-	DPRINTF(("%s: start ok\n", gp->name));
+	bp->bio_resid = bp->bio_length;
+	bp->bio_completed = 0;
+
+	g_uzip_request(gp, bp);
 }
 
 static void
@@ -312,7 +482,7 @@
 {
 	struct g_geom *gp;
 
-	g_trace(G_T_TOPOLOGY, "g_uzip_orphan(%p/%s)", cp, cp->provider->name);
+	g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->provider->name);
 	g_topology_assert();
 
 	gp = cp->geom;
@@ -332,7 +502,7 @@
 	KASSERT (cp != NULL, ("g_uzip_access but no consumer"));
 
 	if (cp->acw + dw > 0)
-		return EROFS;
+		return (EROFS);
 
 	return (g_access(cp, dr, dw, de));
 }
@@ -342,8 +512,9 @@
 {
 	struct g_geom *gp;
 
+	G_VALID_CONSUMER(cp);
 	gp = cp->geom;
-	g_trace(G_T_TOPOLOGY, "g_uzip_spoiled(%p/%s)", cp, gp->name);
+	g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name);
 	g_topology_assert();
 
 	g_uzip_softc_free(gp->softc, gp);
@@ -351,6 +522,114 @@
 	g_wither_geom(gp, ENXIO);
 }
 
+static int
+g_uzip_parse_toc(struct g_uzip_softc *sc, struct g_provider *pp,
+    struct g_geom *gp)
+{
+	uint32_t i, j, backref_to;
+	uint64_t max_offset, min_offset;
+	struct g_uzip_blk *last_blk;
+
+	min_offset = sizeof(struct cloop_header) +
+	    (sc->nblocks + 1) * sizeof(uint64_t);
+	max_offset = sc->toc[0].offset - 1;
+	last_blk = &sc->toc[0];
+	for (i = 0; i < sc->nblocks; i++) {
+		/* First do some bounds checking */
+		if ((sc->toc[i].offset < min_offset) ||
+		    (sc->toc[i].offset > pp->mediasize)) {
+			goto error_offset;
+		}
+		DPRINTF_BLK(GUZ_DBG_IO, i, ("%s: cluster #%u "
+		    "offset=%ju max_offset=%ju\n", gp->name,
+		    (u_int)i, (uintmax_t)sc->toc[i].offset,
+		    (uintmax_t)max_offset));
+		backref_to = BLEN_UNDEF;
+		if (sc->toc[i].offset < max_offset) {
+			/*
+			 * For the backref'ed blocks search already parsed
+			 * TOC entries for the matching offset and copy the
+			 * size from matched entry.
+			 */
+			for (j = 0; j <= i; j++) {
+                                if (sc->toc[j].offset == sc->toc[i].offset &&
+				    !BLK_IS_NIL(sc, j)) {
+                                        break;
+                                }
+                                if (j != i) {
+					continue;
+				}
+				DPRINTF(GUZ_DBG_ERR, ("%s: cannot match "
+				    "backref'ed offset at cluster #%u\n",
+				    gp->name, i));
+				return (-1);
+			}
+			sc->toc[i].blen = sc->toc[j].blen;
+			backref_to = j;
+		} else {
+			last_blk = &sc->toc[i];
+			/*
+			 * For the "normal blocks" seek forward until we hit
+			 * block whose offset is larger than ours and assume
+			 * it's going to be the next one.
+			 */
+			for (j = i + 1; j < sc->nblocks; j++) {
+				if (sc->toc[j].offset > max_offset) {
+					break;
+				}
+			}
+			sc->toc[i].blen = sc->toc[j].offset -
+			    sc->toc[i].offset;
+			if (BLK_ENDS(sc, i) > pp->mediasize) {
+				DPRINTF(GUZ_DBG_ERR, ("%s: cluster #%u "
+				    "extends past media boundary (%ju > %ju)\n",
+				    gp->name, (u_int)i,
+				    (uintmax_t)BLK_ENDS(sc, i),
+				    (intmax_t)pp->mediasize));
+				return (-1);
+			}
+			KASSERT(max_offset <= sc->toc[i].offset, (
+			    "%s: max_offset is incorrect: %ju",
+			    gp->name, (uintmax_t)max_offset));
+			max_offset = BLK_ENDS(sc, i) - 1;
+		}
+		DPRINTF_BLK(GUZ_DBG_TOC, i, ("%s: cluster #%u, original %u "
+		    "bytes, in %u bytes", gp->name, i, sc->blksz,
+		    sc->toc[i].blen));
+		if (backref_to != BLEN_UNDEF) {
+			DPRINTF_BLK(GUZ_DBG_TOC, i, (" (->#%u)",
+			    (u_int)backref_to));
+		}
+		DPRINTF_BLK(GUZ_DBG_TOC, i, ("\n"));
+	}
+	last_blk->last = 1;
+	/* Do a second pass to validate block lengths */
+	for (i = 0; i < sc->nblocks; i++) {
+		if (sc->toc[i].blen > sc->dcp->max_blen) {
+			if (sc->toc[i].last == 0) {
+				DPRINTF(GUZ_DBG_ERR, ("%s: cluster #%u "
+				    "length (%ju) exceeds "
+				    "max_blen (%ju)\n", gp->name, i,
+				    (uintmax_t)sc->toc[i].blen,
+				    (uintmax_t)sc->dcp->max_blen));
+				return (-1);
+			}
+			DPRINTF(GUZ_DBG_INFO, ("%s: cluster #%u extra "
+			    "padding is detected, trimmed to %ju\n",
+			    gp->name, i, (uintmax_t)sc->dcp->max_blen));
+			    sc->toc[i].blen = sc->dcp->max_blen;
+			sc->toc[i].padded = 1;
+		}
+	}
+	return (0);
+
+error_offset:
+	DPRINTF(GUZ_DBG_ERR, ("%s: cluster #%u: invalid offset %ju, "
+	    "min_offset=%ju mediasize=%jd\n", gp->name, (u_int)i,
+	    sc->toc[i].offset, min_offset, pp->mediasize));
+	return (-1);
+}
+
 static struct g_geom *
 g_uzip_taste(struct g_class *mp, struct g_provider *pp, int flags)
 {
@@ -362,8 +641,12 @@
 	struct g_geom *gp;
 	struct g_provider *pp2;
 	struct g_uzip_softc *sc;
+	enum {
+		G_UZIP = 1,
+		G_ULZMA
+	} type;
 
-	g_trace(G_T_TOPOLOGY, "g_uzip_taste(%s,%s)", mp->name, pp->name);
+	g_trace(G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name);
 	g_topology_assert();
 
 	/* Skip providers that are already open for writing. */
@@ -370,21 +653,25 @@
 	if (pp->acw > 0)
 		return (NULL);
 
+	if ((fnmatch(g_uzip_attach_to, pp->name, 0) != 0) ||
+	    (fnmatch(g_uzip_noattach_to, pp->name, 0) == 0)) {
+		DPRINTF(GUZ_DBG_INFO, ("%s(%s,%s), ignoring\n", __func__,
+		    mp->name, pp->name));
+		return (NULL);
+	}
+
 	buf = NULL;
 
 	/*
 	 * Create geom instance.
 	 */
-	gp = g_new_geomf(mp, "%s.uzip", pp->name);
+	gp = g_new_geomf(mp, GUZ_DEV_NAME("%s"), pp->name);
 	cp = g_new_consumer(gp);
 	error = g_attach(cp, pp);
 	if (error == 0)
 		error = g_access(cp, 1, 0, 0);
 	if (error) {
-		g_detach(cp);
-		g_destroy_consumer(cp);
-		g_destroy_geom(gp);
-		return (NULL);
+		goto e1;
 	}
 	g_topology_unlock();
 
@@ -392,22 +679,47 @@
 	 * Read cloop header, look for CLOOP magic, perform
 	 * other validity checks.
 	 */
-	DPRINTF(("%s: media sectorsize %u, mediasize %lld\n",
-	    gp->name, pp->sectorsize, pp->mediasize));
+	DPRINTF(GUZ_DBG_INFO, ("%s: media sectorsize %u, mediasize %jd\n",
+	    gp->name, pp->sectorsize, (intmax_t)pp->mediasize));
 	buf = g_read_data(cp, 0, pp->sectorsize, NULL);
 	if (buf == NULL)
-		goto err;
+		goto e2;
 	header = (struct cloop_header *) buf;
 	if (strncmp(header->magic, CLOOP_MAGIC_START,
-		    sizeof(CLOOP_MAGIC_START) - 1) != 0) {
-		DPRINTF(("%s: no CLOOP magic\n", gp->name));
-		goto err;
+	    sizeof(CLOOP_MAGIC_START) - 1) != 0) {
+		DPRINTF(GUZ_DBG_ERR, ("%s: no CLOOP magic\n", gp->name));
+		goto e3;
 	}
-	if (header->magic[0x0b] != 'V' || header->magic[0x0c] < '2') {
-		DPRINTF(("%s: image version too old\n", gp->name));
-		goto err;
-	}
 
+	switch (header->magic[CLOOP_OFS_COMPR]) {
+	case CLOOP_COMP_LZMA:
+	case CLOOP_COMP_LZMA_DDP:
+		type = G_ULZMA;
+		if (header->magic[CLOOP_OFS_VERSN] < CLOOP_MINVER_LZMA) {
+			DPRINTF(GUZ_DBG_ERR, ("%s: image version too old\n",
+			    gp->name));
+			goto e3;
+		}
+		DPRINTF(GUZ_DBG_INFO, ("%s: GEOM_UZIP_LZMA image found\n",
+		    gp->name));
+		break;
+	case CLOOP_COMP_LIBZ:
+	case CLOOP_COMP_LIBZ_DDP:
+		type = G_UZIP;
+		if (header->magic[CLOOP_OFS_VERSN] < CLOOP_MINVER_ZLIB) {
+			DPRINTF(GUZ_DBG_ERR, ("%s: image version too old\n",
+			    gp->name));
+			goto e3;
+		}
+		DPRINTF(GUZ_DBG_INFO, ("%s: GEOM_UZIP_ZLIB image found\n",
+		    gp->name));
+		break;
+	default:
+		DPRINTF(GUZ_DBG_ERR, ("%s: unsupported image type\n",
+		    gp->name));
+                goto e3;
+        }
+
 	/*
 	 * Initialize softc and read offsets.
 	 */
@@ -418,7 +730,7 @@
 	if (sc->blksz % 512 != 0) {
 		printf("%s: block size (%u) should be multiple of 512.\n",
 		    gp->name, sc->blksz);
-		goto err;
+		goto e4;
 	}
 	if (sc->blksz > MAX_BLKSZ) {
 		printf("%s: block size (%u) should not be larger than %d.\n",
@@ -428,16 +740,18 @@
 	if (sizeof(struct cloop_header) +
 	    total_offsets * sizeof(uint64_t) > pp->mediasize) {
 		printf("%s: media too small for %u blocks\n",
-		       gp->name, sc->nblocks);
-		goto err;
+		    gp->name, sc->nblocks);
+		goto e4;
 	}
-	sc->offsets = malloc(
-	    total_offsets * sizeof(uint64_t), M_GEOM_UZIP, M_WAITOK);
+	sc->toc = malloc(total_offsets * sizeof(struct g_uzip_blk),
+	    M_GEOM_UZIP, M_WAITOK | M_ZERO);
 	offsets_read = MIN(total_offsets,
 	    (pp->sectorsize - sizeof(*header)) / sizeof(uint64_t));
-	for (i = 0; i < offsets_read; i++)
-		sc->offsets[i] = be64toh(((uint64_t *) (header + 1))[i]);
-	DPRINTF(("%s: %u offsets in the first sector\n",
+	for (i = 0; i < offsets_read; i++) {
+		sc->toc[i].offset = be64toh(((uint64_t *) (header + 1))[i]);
+		sc->toc[i].blen = BLEN_UNDEF;
+	}
+	DPRINTF(GUZ_DBG_INFO, ("%s: %u offsets in the first sector\n",
 	       gp->name, offsets_read));
 	for (blk = 1; offsets_read < total_offsets; blk++) {
 		uint32_t nread;
@@ -446,54 +760,103 @@
 		buf = g_read_data(
 		    cp, blk * pp->sectorsize, pp->sectorsize, NULL);
 		if (buf == NULL)
-			goto err;
+			goto e5;
 		nread = MIN(total_offsets - offsets_read,
 		     pp->sectorsize / sizeof(uint64_t));
-		DPRINTF(("%s: %u offsets read from sector %d\n",
+		DPRINTF(GUZ_DBG_TOC, ("%s: %u offsets read from sector %d\n",
 		    gp->name, nread, blk));
 		for (i = 0; i < nread; i++) {
-			sc->offsets[offsets_read + i] =
+			sc->toc[offsets_read + i].offset =
 			    be64toh(((uint64_t *) buf)[i]);
+			sc->toc[offsets_read + i].blen = BLEN_UNDEF;
 		}
 		offsets_read += nread;
 	}
-	DPRINTF(("%s: done reading offsets\n", gp->name));
+	free(buf, M_GEOM);
+	buf = NULL;
+	offsets_read -= 1;
+	DPRINTF(GUZ_DBG_INFO, ("%s: done reading %u block offsets from %u "
+	    "sectors\n", gp->name, offsets_read, blk));
+	if (sc->nblocks != offsets_read) {
+		DPRINTF(GUZ_DBG_ERR, ("%s: read %s offsets than expected "
+		    "blocks\n", gp->name,
+		    sc->nblocks < offsets_read ? "more" : "less"));
+		goto e5;
+	}
+
+	if (type == G_UZIP) {
+		sc->dcp = g_uzip_zlib_ctor(sc->blksz);
+	} else {
+		sc->dcp = g_uzip_lzma_ctor(sc->blksz);
+	}
+	if (sc->dcp == NULL) {
+		goto e5;
+	}
+
+	/*
+	 * "Fake" last+1 block, to make it easier for the TOC parser to
+	 * iterate without making the last element a special case.
+	 */
+	sc->toc[sc->nblocks].offset = pp->mediasize;
+	/* Massage TOC (table of contents), make sure it is sound */
+	if (g_uzip_parse_toc(sc, pp, gp) != 0) {
+		DPRINTF(GUZ_DBG_ERR, ("%s: TOC error\n", gp->name));
+		goto e6;
+	}
 	mtx_init(&sc->last_mtx, "geom_uzip cache", NULL, MTX_DEF);
+	mtx_init(&sc->queue_mtx, "geom_uzip wrkthread", NULL, MTX_DEF);
+	bioq_init(&sc->bio_queue);
 	sc->last_blk = -1;
 	sc->last_buf = malloc(sc->blksz, M_GEOM_UZIP, M_WAITOK);
 	sc->req_total = 0;
 	sc->req_cached = 0;
 
+	sc->uzip_do = &g_uzip_do;
+
+	error = kproc_create(g_uzip_wrkthr, sc, &sc->procp, 0, 0, "%s",
+	    gp->name);
+	if (error != 0) {
+		goto e7;
+	}
+
 	g_topology_lock();
 	pp2 = g_new_providerf(gp, "%s", gp->name);
 	pp2->sectorsize = 512;
 	pp2->mediasize = (off_t)sc->nblocks * sc->blksz;
-        pp2->flags = pp->flags & G_PF_CANDELETE;
-        pp2->stripesize = pp->stripesize;
-        pp2->stripeoffset = pp->stripeoffset;
+	pp2->stripesize = pp->stripesize;
+	pp2->stripeoffset = pp->stripeoffset;
 	g_error_provider(pp2, 0);
 	g_access(cp, -1, 0, 0);
 
-	DPRINTF(("%s: taste ok (%d, %lld), (%d, %d), %x\n",
-	    gp->name,
-	    pp2->sectorsize, pp2->mediasize,
+	DPRINTF(GUZ_DBG_INFO, ("%s: taste ok (%d, %jd), (%d, %d), %x\n",
+	    gp->name, pp2->sectorsize, (intmax_t)pp2->mediasize,
 	    pp2->stripeoffset, pp2->stripesize, pp2->flags));
-	printf("%s: %u x %u blocks\n",
-	       gp->name, sc->nblocks, sc->blksz);
+	DPRINTF(GUZ_DBG_INFO, ("%s: %u x %u blocks\n", gp->name, sc->nblocks,
+	    sc->blksz));
 	return (gp);
 
-err:
+e7:
+	free(sc->last_buf, M_GEOM);
+	mtx_destroy(&sc->queue_mtx);
+	mtx_destroy(&sc->last_mtx);
+e6:
+	sc->dcp->free(sc->dcp);
+e5:
+	free(sc->toc, M_GEOM);
+e4:
+	free(gp->softc, M_GEOM_UZIP);
+e3:
+	if (buf != NULL) {
+		free(buf, M_GEOM);
+	}
+e2:
 	g_topology_lock();
 	g_access(cp, -1, 0, 0);
-	if (buf != NULL)
-		free(buf, M_GEOM);
-	if (gp->softc != NULL) {
-		g_uzip_softc_free(gp->softc, NULL);
-		gp->softc = NULL;
-	}
+e1:
 	g_detach(cp);
 	g_destroy_consumer(cp);
 	g_destroy_geom(gp);
+
 	return (NULL);
 }
 
@@ -502,11 +865,12 @@
 {
 	struct g_provider *pp;
 
-	g_trace(G_T_TOPOLOGY, "g_uzip_destroy_geom(%s, %s)", mp->name, gp->name);
+	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, gp->name);
 	g_topology_assert();
 
 	if (gp->softc == NULL) {
-		printf("%s(%s): gp->softc == NULL\n", __func__, gp->name);
+		DPRINTF(GUZ_DBG_ERR, ("%s(%s): gp->softc == NULL\n", __func__,
+		    gp->name));
 		return (ENXIO);
 	}
 
@@ -519,6 +883,7 @@
 	g_uzip_softc_free(gp->softc, gp);
 	gp->softc = NULL;
 	g_wither_geom(gp, ENXIO);
+
 	return (0);
 }
 

Added: trunk/sys/geom/uzip/g_uzip.h
===================================================================
--- trunk/sys/geom/uzip/g_uzip.h	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip.h	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,38 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2004 Max Khon
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/geom/uzip/g_uzip.h 303167 2016-07-21 23:49:26Z sobomax $
+ */
+#ifndef	__GEOM_G_UZIP_H__
+#define	__GEOM_G_UZIP_H__
+
+MALLOC_DECLARE(M_GEOM_UZIP);
+
+#define DEFINE_RAW_METHOD(func, rval, args...) typedef rval (*func##_t)(args)
+
+#endif	/* __GEOM_G_UZIP_H__ */


Property changes on: trunk/sys/geom/uzip/g_uzip.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/geom/uzip/g_uzip_cloop.h
===================================================================
--- trunk/sys/geom/uzip/g_uzip_cloop.h	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip_cloop.h	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,56 @@
+/* $MidnightBSD$ */
+/*
+ * Copyright (c) 2004-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/geom/uzip/g_uzip_cloop.h 303167 2016-07-21 23:49:26Z sobomax $
+ */
+
+/* CLOOP format and related constants */
+
+/*
+ * Integer values (block size, number of blocks, offsets)
+ * are stored in big-endian (network) order on disk.
+ */
+
+#define CLOOP_MAGIC_LEN 128
+#define CLOOP_OFS_COMPR 0x0b
+#define CLOOP_OFS_VERSN (CLOOP_OFS_COMPR + 1)
+
+#define CLOOP_MAJVER_2	'2'
+#define CLOOP_MAJVER_3	'3'
+
+#define	CLOOP_COMP_LIBZ		'V'
+#define	CLOOP_COMP_LIBZ_DDP	'v'
+#define	CLOOP_COMP_LZMA		'L'
+#define	CLOOP_COMP_LZMA_DDP	'l'
+
+#define	CLOOP_MINVER_LZMA	CLOOP_MAJVER_3
+#define	CLOOP_MINVER_ZLIB	CLOOP_MAJVER_2
+
+struct cloop_header {
+        char magic[CLOOP_MAGIC_LEN];    /* cloop magic */
+        uint32_t blksz;                 /* block size */
+        uint32_t nblocks;               /* number of blocks */
+};


Property changes on: trunk/sys/geom/uzip/g_uzip_cloop.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/geom/uzip/g_uzip_dapi.h
===================================================================
--- trunk/sys/geom/uzip/g_uzip_dapi.h	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip_dapi.h	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,43 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/geom/uzip/g_uzip_dapi.h 303167 2016-07-21 23:49:26Z sobomax $
+ */
+
+struct g_uzip_dapi;
+
+DEFINE_RAW_METHOD(g_uzip_dapi_decompress, int, struct g_uzip_dapi *,
+    const char *, void *, size_t, void *);
+DEFINE_RAW_METHOD(g_uzip_dapi_free, void, struct g_uzip_dapi *);
+DEFINE_RAW_METHOD(g_uzip_dapi_rewind, int, struct g_uzip_dapi *, const char *);
+
+struct g_uzip_dapi {
+	g_uzip_dapi_decompress_t decompress;
+	g_uzip_dapi_free_t free;
+	g_uzip_dapi_rewind_t rewind;
+	void *pvt;
+	int max_blen;
+};


Property changes on: trunk/sys/geom/uzip/g_uzip_dapi.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/geom/uzip/g_uzip_lzma.c
===================================================================
--- trunk/sys/geom/uzip/g_uzip_lzma.c	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip_lzma.c	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,129 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2004 Max Khon
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * Copyright (c) 2010-2012 Aleksandr Rybalko
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/sys/geom/uzip/g_uzip_lzma.c 303167 2016-07-21 23:49:26Z sobomax $");
+
+#include <sys/types.h>
+#include <sys/malloc.h>
+#include <sys/systm.h>
+
+#include <contrib/xz-embedded/linux/include/linux/xz.h>
+
+#include <geom/uzip/g_uzip.h>
+#include <geom/uzip/g_uzip_dapi.h>
+#include <geom/uzip/g_uzip_lzma.h>
+
+struct g_uzip_lzma {
+	struct g_uzip_dapi pub;
+	uint32_t blksz;
+	/* XZ decoder structs */
+	struct xz_buf b;
+	struct xz_dec *s;
+};
+
+static int g_uzip_lzma_nop(struct g_uzip_dapi *, const char *);
+
+static void
+g_uzip_lzma_free(struct g_uzip_dapi *lzpp)
+{
+	struct g_uzip_lzma *lzp;
+
+	lzp = (struct g_uzip_lzma *)lzpp->pvt;
+	if (lzp->s != NULL) {
+		xz_dec_end(lzp->s);
+		lzp->s = NULL;
+	}
+
+	free(lzp, M_GEOM_UZIP);
+}
+
+static int
+g_uzip_lzma_decompress(struct g_uzip_dapi *lzpp, const char *gp_name,
+    void *ibp, size_t ilen, void *obp)
+{
+	struct g_uzip_lzma *lzp;
+	int err;
+
+	lzp = (struct g_uzip_lzma *)lzpp->pvt;
+
+	lzp->b.in = ibp;
+	lzp->b.out = obp;
+	lzp->b.in_pos = lzp->b.out_pos = 0;
+	lzp->b.in_size = ilen;
+	lzp->b.out_size = lzp->blksz;
+	err = (xz_dec_run(lzp->s, &lzp->b) != XZ_STREAM_END) ? 1 : 0;
+	/* TODO decoder recovery, if needed */
+	if (err != 0) {
+		printf("%s: ibp=%p, obp=%p, in_pos=%jd, out_pos=%jd, "
+		    "in_size=%jd, out_size=%jd\n", __func__, ibp, obp,
+		    (intmax_t)lzp->b.in_pos, (intmax_t)lzp->b.out_pos,
+		    (intmax_t)lzp->b.in_size, (intmax_t)lzp->b.out_size);
+	}
+
+	return (err);
+}
+
+static int
+LZ4_compressBound(int isize)
+{
+
+        return (isize + (isize / 255) + 16);
+}
+
+struct g_uzip_dapi *
+g_uzip_lzma_ctor(uint32_t blksz)
+{
+	struct g_uzip_lzma *lzp;
+
+	lzp = malloc(sizeof(struct g_uzip_lzma), M_GEOM_UZIP, M_WAITOK);
+	xz_crc32_init();
+	lzp->s = xz_dec_init(XZ_SINGLE, 0);
+	if (lzp->s == NULL) {
+		goto e1;
+	}
+	lzp->blksz = blksz;
+	lzp->pub.max_blen = LZ4_compressBound(blksz);
+	lzp->pub.decompress = &g_uzip_lzma_decompress;
+	lzp->pub.free = &g_uzip_lzma_free;
+	lzp->pub.rewind = &g_uzip_lzma_nop;
+	lzp->pub.pvt = lzp;
+	return (&lzp->pub);
+e1:
+        free(lzp, M_GEOM_UZIP);
+        return (NULL);
+}
+
+static int
+g_uzip_lzma_nop(struct g_uzip_dapi *zpp, const char *gp_name)
+{
+
+        return (0);
+}


Property changes on: trunk/sys/geom/uzip/g_uzip_lzma.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/geom/uzip/g_uzip_lzma.h
===================================================================
--- trunk/sys/geom/uzip/g_uzip_lzma.h	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip_lzma.h	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,33 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2004 Max Khon
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * Copyright (c) 2010-2012 Aleksandr Rybalko
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/geom/uzip/g_uzip_lzma.h 303167 2016-07-21 23:49:26Z sobomax $
+ */
+
+struct g_uzip_dapi *g_uzip_lzma_ctor(uint32_t);


Property changes on: trunk/sys/geom/uzip/g_uzip_lzma.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/geom/uzip/g_uzip_softc.h
===================================================================
--- trunk/sys/geom/uzip/g_uzip_softc.h	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip_softc.h	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,57 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2004 Max Khon
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/geom/uzip/g_uzip_softc.h 303167 2016-07-21 23:49:26Z sobomax $
+ */
+
+struct g_uzip_softc;
+struct bio;
+
+DEFINE_RAW_METHOD(g_uzip_do, void, struct g_uzip_softc *, struct bio *);
+
+struct g_uzip_softc {
+	uint32_t blksz;                 /* block size */
+	uint32_t nblocks;               /* number of blocks */
+	struct g_uzip_blk *toc;         /* table of contents */
+
+	struct mtx last_mtx;
+	uint32_t last_blk;              /* last blk no */
+	char *last_buf;                 /* last blk data */
+	int req_total;                  /* total requests */
+	int req_cached;                 /* cached requests */
+	struct g_uzip_dapi *dcp;        /* decompressor instance */
+
+	g_uzip_do_t uzip_do;
+
+	struct proc *procp;
+	struct bio_queue_head bio_queue; 
+	struct mtx queue_mtx;
+	unsigned wrkthr_flags;
+#define	GUZ_SHUTDOWN	(0x1 << 0)
+#define	GUZ_EXITING	(0x1 << 1)
+};


Property changes on: trunk/sys/geom/uzip/g_uzip_softc.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/geom/uzip/g_uzip_wrkthr.c
===================================================================
--- trunk/sys/geom/uzip/g_uzip_wrkthr.c	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip_wrkthr.c	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,73 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/sys/geom/uzip/g_uzip_wrkthr.c 303167 2016-07-21 23:49:26Z sobomax $");
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/bio.h>
+#include <sys/systm.h>
+#include <sys/proc.h>
+#include <sys/sched.h>
+#include <sys/kthread.h>
+#include <sys/malloc.h>
+
+#include <geom/uzip/g_uzip.h>
+#include <geom/uzip/g_uzip_softc.h>
+#include <geom/uzip/g_uzip_wrkthr.h>
+
+void
+g_uzip_wrkthr(void *arg)
+{
+	struct g_uzip_softc *sc;
+	struct bio *bp;
+
+	sc = (struct g_uzip_softc *)arg;
+	thread_lock(curthread);
+	sched_prio(curthread, PRIBIO);
+	thread_unlock(curthread);
+
+	for (;;) {
+		mtx_lock(&sc->queue_mtx);
+		if (sc->wrkthr_flags & GUZ_SHUTDOWN) {
+			sc->wrkthr_flags |= GUZ_EXITING;
+			mtx_unlock(&sc->queue_mtx);
+			kproc_exit(0);
+		}
+		bp = bioq_takefirst(&sc->bio_queue);
+		if (!bp) {
+			msleep(sc, &sc->queue_mtx, PRIBIO | PDROP,
+			    "wrkwait", 0);
+			continue;
+		}
+		mtx_unlock(&sc->queue_mtx);
+		sc->uzip_do(sc, bp);
+	}
+}


Property changes on: trunk/sys/geom/uzip/g_uzip_wrkthr.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/geom/uzip/g_uzip_wrkthr.h
===================================================================
--- trunk/sys/geom/uzip/g_uzip_wrkthr.h	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip_wrkthr.h	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,31 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/geom/uzip/g_uzip_wrkthr.h 303167 2016-07-21 23:49:26Z sobomax $
+ */
+
+void g_uzip_wrkthr(void *);
+


Property changes on: trunk/sys/geom/uzip/g_uzip_wrkthr.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/geom/uzip/g_uzip_zlib.c
===================================================================
--- trunk/sys/geom/uzip/g_uzip_zlib.c	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip_zlib.c	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,146 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2004 Max Khon
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/sys/geom/uzip/g_uzip_zlib.c 303167 2016-07-21 23:49:26Z sobomax $");
+
+#include <sys/types.h>
+#include <sys/systm.h>
+#include <sys/malloc.h>
+
+#include <net/zlib.h>
+
+#include <geom/uzip/g_uzip.h>
+#include <geom/uzip/g_uzip_dapi.h>
+#include <geom/uzip/g_uzip_zlib.h>
+
+struct g_uzip_zlib {
+	uint32_t blksz;
+	struct g_uzip_dapi pub;
+	/* Zlib decoder structs */
+	z_stream zs;
+};
+
+static void *z_alloc(void *, u_int, u_int);
+static void z_free(void *, void *);
+static int g_uzip_zlib_rewind(struct g_uzip_dapi *, const char *);
+
+static void
+g_uzip_zlib_free(struct g_uzip_dapi *zpp)
+{
+	struct g_uzip_zlib *zp;
+
+	zp = (struct g_uzip_zlib *)zpp->pvt;
+	inflateEnd(&zp->zs);
+	free(zp, M_GEOM_UZIP);
+}
+
+static int
+g_uzip_zlib_decompress(struct g_uzip_dapi *zpp, const char *gp_name, void *ibp,
+    size_t ilen, void *obp)
+{
+	int err;
+	struct g_uzip_zlib *zp;
+
+	zp = (struct g_uzip_zlib *)zpp->pvt;
+
+	zp->zs.next_in = ibp;
+	zp->zs.avail_in = ilen;
+	zp->zs.next_out = obp;
+	zp->zs.avail_out = zp->blksz;
+
+	err = (inflate(&zp->zs, Z_FINISH) != Z_STREAM_END) ? 1 : 0;
+	if (err != 0) {
+		printf("%s: UZIP(zlib) inflate() failed\n", gp_name);
+	}
+	return (err);
+}
+
+static int
+g_uzip_zlib_rewind(struct g_uzip_dapi *zpp, const char *gp_name)
+{
+	int err;
+	struct g_uzip_zlib *zp;
+
+	zp = (struct g_uzip_zlib *)zpp->pvt;
+
+	err = 0;
+	if (inflateReset(&zp->zs) != Z_OK) {
+		printf("%s: UZIP(zlib) decoder reset failed\n", gp_name);
+		err = 1;
+	}
+	return (err);
+}
+
+static int
+z_compressBound(int len)
+{
+
+	return (len + (len >> 12) + (len >> 14) + 11);
+}
+
+struct g_uzip_dapi *
+g_uzip_zlib_ctor(uint32_t blksz)
+{
+	struct g_uzip_zlib *zp;
+
+	zp = malloc(sizeof(struct g_uzip_zlib), M_GEOM_UZIP, M_WAITOK);
+	zp->zs.zalloc = z_alloc;
+	zp->zs.zfree = z_free;
+	if (inflateInit(&zp->zs) != Z_OK) {
+		goto e1;
+	}
+	zp->blksz = blksz;
+	zp->pub.max_blen = z_compressBound(blksz);
+	zp->pub.decompress = &g_uzip_zlib_decompress;
+	zp->pub.free = &g_uzip_zlib_free;
+	zp->pub.rewind = &g_uzip_zlib_rewind;
+	zp->pub.pvt = (void *)zp;
+	return (&zp->pub);
+e1:
+	free(zp, M_GEOM_UZIP);
+	return (NULL);
+}
+
+static void *
+z_alloc(void *nil, u_int type, u_int size)
+{
+        void *ptr;
+
+        ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT);
+
+        return (ptr);
+}
+
+static void
+z_free(void *nil, void *ptr)
+{
+
+        free(ptr, M_GEOM_UZIP);
+}


Property changes on: trunk/sys/geom/uzip/g_uzip_zlib.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/geom/uzip/g_uzip_zlib.h
===================================================================
--- trunk/sys/geom/uzip/g_uzip_zlib.h	                        (rev 0)
+++ trunk/sys/geom/uzip/g_uzip_zlib.h	2018-05-26 15:16:10 UTC (rev 9965)
@@ -0,0 +1,34 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2004 Max Khon
+ * Copyright (c) 2014 Juniper Networks, Inc.
+ * Copyright (c) 2006-2016 Maxim Sobolev <sobomax at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/geom/uzip/g_uzip_zlib.h 303167 2016-07-21 23:49:26Z sobomax $
+ */
+
+struct g_geom;
+
+struct g_uzip_dapi *g_uzip_zlib_ctor(uint32_t);


Property changes on: trunk/sys/geom/uzip/g_uzip_zlib.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Modified: trunk/sys/geom/virstor/g_virstor.c
===================================================================
--- trunk/sys/geom/virstor/g_virstor.c	2018-05-26 15:13:49 UTC (rev 9964)
+++ trunk/sys/geom/virstor/g_virstor.c	2018-05-26 15:16:10 UTC (rev 9965)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 2006-2007 Ivan Voras <ivoras at freebsd.org>
  * All rights reserved.
@@ -30,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/geom/virstor/g_virstor.c 330737 2018-03-10 04:17:01Z asomers $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -475,7 +476,7 @@
 update_metadata(struct g_virstor_softc *sc)
 {
 	struct g_virstor_metadata md;
-	int n;
+	u_int n;
 
 	if (virstor_valid_components(sc) != sc->n_components)
 		return; /* Incomplete device */
@@ -904,11 +905,9 @@
 	LOG_MSG(LVL_DEBUG, "Component %s removed from %s", c->provider->name,
 	    sc->geom->name);
 	if (sc->provider != NULL) {
-		/* Whither, GEOM? */
-		sc->provider->flags |= G_PF_WITHER;
-		g_orphan_provider(sc->provider, ENXIO);
+		LOG_MSG(LVL_INFO, "Removing provider %s", sc->provider->name);
+		g_wither_provider(sc->provider, ENXIO);
 		sc->provider = NULL;
-		LOG_MSG(LVL_INFO, "Removing provider %s", sc->geom->name);
 	}
 
 	if (c->acr > 0 || c->acw > 0 || c->ace > 0)
@@ -932,7 +931,7 @@
 {
 	struct g_provider *pp;
 	struct g_geom *gp;
-	int n;
+	u_int n;
 
 	g_topology_assert();
 
@@ -1046,6 +1045,7 @@
 	pp = cp->provider;
 
 	buf = malloc(pp->sectorsize, M_GVIRSTOR, M_WAITOK);
+	bzero(buf, pp->sectorsize);
 	virstor_metadata_encode(md, buf);
 	g_topology_unlock();
 	error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf,

Modified: trunk/sys/geom/zero/g_zero.c
===================================================================
--- trunk/sys/geom/zero/g_zero.c	2018-05-26 15:13:49 UTC (rev 9964)
+++ trunk/sys/geom/zero/g_zero.c	2018-05-26 15:16:10 UTC (rev 9965)
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/geom/zero/g_zero.c,v 1.4 2006/02/01 12:06:01 pjd Exp $");
+__FBSDID("$FreeBSD: stable/10/sys/geom/zero/g_zero.c 260385 2014-01-07 01:32:23Z scottl $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -42,16 +42,37 @@
 
 #define	G_ZERO_CLASS_NAME	"ZERO"
 
+static int	g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS);
+
 SYSCTL_DECL(_kern_geom);
 static SYSCTL_NODE(_kern_geom, OID_AUTO, zero, CTLFLAG_RW, 0,
     "GEOM_ZERO stuff");
 static int g_zero_clear = 1;
-SYSCTL_INT(_kern_geom_zero, OID_AUTO, clear, CTLFLAG_RW, &g_zero_clear, 0,
-    "Clear read data buffer");
+SYSCTL_PROC(_kern_geom_zero, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW,
+    &g_zero_clear, 0, g_zero_clear_sysctl, "I", "Clear read data buffer");
 static int g_zero_byte = 0;
 SYSCTL_INT(_kern_geom_zero, OID_AUTO, byte, CTLFLAG_RW, &g_zero_byte, 0,
     "Byte (octet) value to clear the buffers with");
 
+static struct g_provider *gpp;
+
+static int
+g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS)
+{
+	int error;
+
+	error = sysctl_handle_int(oidp, &g_zero_clear, 0, req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+	if (gpp == NULL)
+		return (ENXIO);
+	if (g_zero_clear)
+		gpp->flags &= ~G_PF_ACCEPT_UNMAPPED;
+	else
+		gpp->flags |= G_PF_ACCEPT_UNMAPPED;
+	return (0);
+}
+
 static void
 g_zero_start(struct bio *bp)
 {
@@ -59,7 +80,7 @@
 
 	switch (bp->bio_cmd) {
 	case BIO_READ:
-		if (g_zero_clear)
+		if (g_zero_clear && (bp->bio_flags & BIO_UNMAPPED) == 0)
 			memset(bp->bio_data, g_zero_byte, bp->bio_length);
 		/* FALLTHROUGH */
 	case BIO_DELETE:
@@ -85,7 +106,10 @@
 	gp = g_new_geomf(mp, "gzero");
 	gp->start = g_zero_start;
 	gp->access = g_std_access;
-	pp = g_new_providerf(gp, "%s", gp->name);
+	gpp = pp = g_new_providerf(gp, "%s", gp->name);
+	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
+	if (!g_zero_clear)
+		pp->flags |= G_PF_ACCEPT_UNMAPPED;
 	pp->mediasize = 1152921504606846976LLU;
 	pp->sectorsize = 512;
 	g_error_provider(pp, 0);
@@ -105,6 +129,7 @@
 		return (0);
 	if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
 		return (EBUSY);
+	gpp = NULL;
 	g_wither_geom(gp, ENXIO);
 	return (0);
 }



More information about the Midnightbsd-cvs mailing list