xref: /freebsd/sys/geom/uzip/g_uzip.c (revision 3b8f08459569bf0faa21473e5cec2491e95c9349)
1 /*-
2  * Copyright (c) 2004 Max Khon
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/endian.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 #include <sys/sysctl.h>
40 
41 #include <geom/geom.h>
42 #include <net/zlib.h>
43 
44 FEATURE(geom_uzip, "GEOM uzip read-only compressed disks support");
45 
46 #undef GEOM_UZIP_DEBUG
47 #ifdef GEOM_UZIP_DEBUG
48 #define DPRINTF(a)	printf a
49 #else
50 #define DPRINTF(a)
51 #endif
52 
53 static MALLOC_DEFINE(M_GEOM_UZIP, "geom_uzip", "GEOM UZIP data structures");
54 
55 #define UZIP_CLASS_NAME	"UZIP"
56 
57 /*
58  * Maximum allowed valid block size (to prevent foot-shooting)
59  */
60 #define MAX_BLKSZ	(MAXPHYS - MAXPHYS / 1000 - 12)
61 
62 /*
63  * Integer values (block size, number of blocks, offsets)
64  * are stored in big-endian (network) order on disk and struct cloop_header
65  * and in native order in struct g_uzip_softc
66  */
67 
68 #define CLOOP_MAGIC_LEN 128
69 static char CLOOP_MAGIC_START[] = "#!/bin/sh\n";
70 
71 struct cloop_header {
72 	char magic[CLOOP_MAGIC_LEN];	/* cloop magic */
73 	uint32_t blksz;			/* block size */
74 	uint32_t nblocks;		/* number of blocks */
75 };
76 
77 struct g_uzip_softc {
78 	uint32_t blksz;			/* block size */
79 	uint32_t nblocks;		/* number of blocks */
80 	uint64_t *offsets;
81 
82 	struct mtx last_mtx;
83 	uint32_t last_blk;		/* last blk no */
84 	char *last_buf;			/* last blk data */
85 	int req_total;			/* total requests */
86 	int req_cached;			/* cached requests */
87 };
88 
89 static void
90 g_uzip_softc_free(struct g_uzip_softc *sc, struct g_geom *gp)
91 {
92 	if (gp != NULL) {
93 		printf("%s: %d requests, %d cached\n",
94 		    gp->name, sc->req_total, sc->req_cached);
95 	}
96 	if (sc->offsets != NULL)
97 		free(sc->offsets, M_GEOM_UZIP);
98 	mtx_destroy(&sc->last_mtx);
99 	free(sc->last_buf, M_GEOM_UZIP);
100 	free(sc, M_GEOM_UZIP);
101 }
102 
103 static void *
104 z_alloc(void *nil, u_int type, u_int size)
105 {
106 	void *ptr;
107 
108 	ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT);
109 	return ptr;
110 }
111 
112 static void
113 z_free(void *nil, void *ptr)
114 {
115 	free(ptr, M_GEOM_UZIP);
116 }
117 
118 static void
119 g_uzip_done(struct bio *bp)
120 {
121 	int err;
122 	struct bio *bp2;
123 	z_stream zs;
124 	struct g_provider *pp, *pp2;
125 	struct g_consumer *cp;
126 	struct g_geom *gp;
127 	struct g_uzip_softc *sc;
128 	off_t iolen, pos, upos;
129 	uint32_t start_blk, i;
130 	size_t bsize;
131 
132 	bp2 = bp->bio_parent;
133 	pp = bp2->bio_to;
134 	gp = pp->geom;
135 	cp = LIST_FIRST(&gp->consumer);
136 	pp2 = cp->provider;
137 	sc = gp->softc;
138 	DPRINTF(("%s: done\n", gp->name));
139 
140 	bp2->bio_error = bp->bio_error;
141 	if (bp2->bio_error != 0)
142 		goto done;
143 
144 	/*
145 	 * Uncompress data.
146 	 */
147 	zs.zalloc = z_alloc;
148 	zs.zfree = z_free;
149 	err = inflateInit(&zs);
150 	if (err != Z_OK) {
151 		bp2->bio_error = EIO;
152 		goto done;
153 	}
154 	start_blk = bp2->bio_offset / sc->blksz;
155 	bsize = pp2->sectorsize;
156 	iolen = bp->bio_completed;
157 	pos = sc->offsets[start_blk] % bsize;
158 	upos = 0;
159 	DPRINTF(("%s: done: start_blk %d, pos %jd, upos %jd, iolen %jd "
160 	    "(%jd, %d, %zd)\n",
161 	    gp->name, start_blk, (intmax_t)pos, (intmax_t)upos,
162 	    (intmax_t)iolen, (intmax_t)bp2->bio_offset, sc->blksz, bsize));
163 	for (i = start_blk; upos < bp2->bio_length; i++) {
164 		off_t len, ulen, uoff;
165 
166 		uoff = i == start_blk ? bp2->bio_offset % sc->blksz : 0;
167 		ulen = MIN(sc->blksz - uoff, bp2->bio_length - upos);
168 		len = sc->offsets[i + 1] - sc->offsets[i];
169 
170 		if (len == 0) {
171 			/* All zero block: no cache update */
172 			bzero(bp2->bio_data + upos, ulen);
173 			upos += ulen;
174 			bp2->bio_completed += ulen;
175 			continue;
176 		}
177 		if (len > iolen) {
178 			DPRINTF(("%s: done: early termination: len (%jd) > "
179 			    "iolen (%jd)\n",
180 			    gp->name, (intmax_t)len, (intmax_t)iolen));
181 			break;
182 		}
183 		zs.next_in = bp->bio_data + pos;
184 		zs.avail_in = len;
185 		zs.next_out = sc->last_buf;
186 		zs.avail_out = sc->blksz;
187 		mtx_lock(&sc->last_mtx);
188 		err = inflate(&zs, Z_FINISH);
189 		if (err != Z_STREAM_END) {
190 			sc->last_blk = -1;
191 			mtx_unlock(&sc->last_mtx);
192 			DPRINTF(("%s: done: inflate failed (%jd + %jd -> %jd + %jd + %jd)\n",
193 			    gp->name, (intmax_t)pos, (intmax_t)len,
194 			    (intmax_t)uoff, (intmax_t)upos, (intmax_t)ulen));
195 			inflateEnd(&zs);
196 			bp2->bio_error = EIO;
197 			goto done;
198 		}
199 		sc->last_blk = i;
200 		DPRINTF(("%s: done: inflated %jd + %jd -> %jd + %jd + %jd\n",
201 		    gp->name, (intmax_t)pos, (intmax_t)len, (intmax_t)uoff,
202 		    (intmax_t)upos, (intmax_t)ulen));
203 		memcpy(bp2->bio_data + upos, sc->last_buf + uoff, ulen);
204 		mtx_unlock(&sc->last_mtx);
205 
206 		pos += len;
207 		iolen -= len;
208 		upos += ulen;
209 		bp2->bio_completed += ulen;
210 		err = inflateReset(&zs);
211 		if (err != Z_OK) {
212 			inflateEnd(&zs);
213 			bp2->bio_error = EIO;
214 			goto done;
215 		}
216 	}
217 	err = inflateEnd(&zs);
218 	if (err != Z_OK) {
219 		bp2->bio_error = EIO;
220 		goto done;
221 	}
222 
223 done:
224 	/*
225 	 * Finish processing the request.
226 	 */
227 	DPRINTF(("%s: done: (%d, %jd, %ld)\n",
228 	    gp->name, bp2->bio_error, (intmax_t)bp2->bio_completed,
229 	    bp2->bio_resid));
230 	free(bp->bio_data, M_GEOM_UZIP);
231 	g_destroy_bio(bp);
232 	g_io_deliver(bp2, bp2->bio_error);
233 }
234 
235 static void
236 g_uzip_start(struct bio *bp)
237 {
238 	struct bio *bp2;
239 	struct g_provider *pp, *pp2;
240 	struct g_geom *gp;
241 	struct g_consumer *cp;
242 	struct g_uzip_softc *sc;
243 	uint32_t start_blk, end_blk;
244 	size_t bsize;
245 
246 	pp = bp->bio_to;
247 	gp = pp->geom;
248 	DPRINTF(("%s: start (%d)\n", gp->name, bp->bio_cmd));
249 
250 	if (bp->bio_cmd != BIO_READ) {
251 		g_io_deliver(bp, EOPNOTSUPP);
252 		return;
253 	}
254 
255 	cp = LIST_FIRST(&gp->consumer);
256 	pp2 = cp->provider;
257 	sc = gp->softc;
258 
259 	start_blk = bp->bio_offset / sc->blksz;
260 	end_blk = (bp->bio_offset + bp->bio_length + sc->blksz - 1) / sc->blksz;
261 	KASSERT(start_blk < sc->nblocks,
262 		("start_blk out of range"));
263 	KASSERT(end_blk <= sc->nblocks,
264 		("end_blk out of range"));
265 
266 	sc->req_total++;
267 	if (start_blk + 1 == end_blk) {
268 		mtx_lock(&sc->last_mtx);
269 		if (start_blk == sc->last_blk) {
270 			off_t uoff;
271 
272 			uoff = bp->bio_offset % sc->blksz;
273 			KASSERT(bp->bio_length <= sc->blksz - uoff,
274 			    ("cached data error"));
275 			memcpy(bp->bio_data, sc->last_buf + uoff,
276 			    bp->bio_length);
277 			sc->req_cached++;
278 			mtx_unlock(&sc->last_mtx);
279 
280 			DPRINTF(("%s: start: cached 0 + %jd, %jd + 0 + %jd\n",
281 			    gp->name, (intmax_t)bp->bio_length, (intmax_t)uoff,
282 			    (intmax_t)bp->bio_length));
283 			bp->bio_completed = bp->bio_length;
284 			g_io_deliver(bp, 0);
285 			return;
286 		}
287 		mtx_unlock(&sc->last_mtx);
288 	}
289 
290 	bp2 = g_clone_bio(bp);
291 	if (bp2 == NULL) {
292 		g_io_deliver(bp, ENOMEM);
293 		return;
294 	}
295 	bp2->bio_done = g_uzip_done;
296 	DPRINTF(("%s: start (%d..%d), %s: %d + %jd, %s: %d + %jd\n",
297 	    gp->name, start_blk, end_blk,
298 	    pp->name, pp->sectorsize, (intmax_t)pp->mediasize,
299 	    pp2->name, pp2->sectorsize, (intmax_t)pp2->mediasize));
300 	bsize = pp2->sectorsize;
301 	bp2->bio_offset = sc->offsets[start_blk] - sc->offsets[start_blk] % bsize;
302 	while (1) {
303 		bp2->bio_length = sc->offsets[end_blk] - bp2->bio_offset;
304 		bp2->bio_length = (bp2->bio_length + bsize - 1) / bsize * bsize;
305 		if (bp2->bio_length < MAXPHYS)
306 			break;
307 
308 		end_blk--;
309 		DPRINTF(("%s: bio_length (%jd) > MAXPHYS: lowering end_blk "
310 		    "to %u\n", gp->name, (intmax_t)bp2->bio_length, end_blk));
311 	}
312 	DPRINTF(("%s: start %jd + %jd -> %ju + %ju -> %jd + %jd\n",
313 	    gp->name,
314 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length,
315 	    (uintmax_t)sc->offsets[start_blk],
316 	    (uintmax_t)sc->offsets[end_blk] - sc->offsets[start_blk],
317 	    (intmax_t)bp2->bio_offset, (intmax_t)bp2->bio_length));
318 	bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UZIP, M_NOWAIT);
319 	if (bp2->bio_data == NULL) {
320 		g_destroy_bio(bp2);
321 		g_io_deliver(bp, ENOMEM);
322 		return;
323 	}
324 
325 	g_io_request(bp2, cp);
326 	DPRINTF(("%s: start ok\n", gp->name));
327 }
328 
329 static void
330 g_uzip_orphan(struct g_consumer *cp)
331 {
332 	struct g_geom *gp;
333 
334 	g_trace(G_T_TOPOLOGY, "g_uzip_orphan(%p/%s)", cp, cp->provider->name);
335 	g_topology_assert();
336 
337 	gp = cp->geom;
338 	g_uzip_softc_free(gp->softc, gp);
339 	gp->softc = NULL;
340 	g_wither_geom(gp, ENXIO);
341 }
342 
343 static int
344 g_uzip_access(struct g_provider *pp, int dr, int dw, int de)
345 {
346 	struct g_geom *gp;
347 	struct g_consumer *cp;
348 
349 	gp = pp->geom;
350 	cp = LIST_FIRST(&gp->consumer);
351 	KASSERT (cp != NULL, ("g_uzip_access but no consumer"));
352 
353 	if (cp->acw + dw > 0)
354 		return EROFS;
355 
356 	return (g_access(cp, dr, dw, de));
357 }
358 
359 static void
360 g_uzip_spoiled(struct g_consumer *cp)
361 {
362 	struct g_geom *gp;
363 
364 	gp = cp->geom;
365 	g_trace(G_T_TOPOLOGY, "g_uzip_spoiled(%p/%s)", cp, gp->name);
366 	g_topology_assert();
367 
368 	g_uzip_softc_free(gp->softc, gp);
369 	gp->softc = NULL;
370 	g_wither_geom(gp, ENXIO);
371 }
372 
373 static struct g_geom *
374 g_uzip_taste(struct g_class *mp, struct g_provider *pp, int flags)
375 {
376 	int error;
377 	uint32_t i, total_offsets, offsets_read, blk;
378 	void *buf;
379 	struct cloop_header *header;
380 	struct g_consumer *cp;
381 	struct g_geom *gp;
382 	struct g_provider *pp2;
383 	struct g_uzip_softc *sc;
384 
385 	g_trace(G_T_TOPOLOGY, "g_uzip_taste(%s,%s)", mp->name, pp->name);
386 	g_topology_assert();
387 
388 	/* Skip providers that are already open for writing. */
389 	if (pp->acw > 0)
390 		return (NULL);
391 
392 	buf = NULL;
393 
394 	/*
395 	 * Create geom instance.
396 	 */
397 	gp = g_new_geomf(mp, "%s.uzip", pp->name);
398 	cp = g_new_consumer(gp);
399 	error = g_attach(cp, pp);
400 	if (error == 0)
401 		error = g_access(cp, 1, 0, 0);
402 	if (error) {
403 		g_detach(cp);
404 		g_destroy_consumer(cp);
405 		g_destroy_geom(gp);
406 		return (NULL);
407 	}
408 	g_topology_unlock();
409 
410 	/*
411 	 * Read cloop header, look for CLOOP magic, perform
412 	 * other validity checks.
413 	 */
414 	DPRINTF(("%s: media sectorsize %u, mediasize %jd\n",
415 	    gp->name, pp->sectorsize, (intmax_t)pp->mediasize));
416 	buf = g_read_data(cp, 0, pp->sectorsize, NULL);
417 	if (buf == NULL)
418 		goto err;
419 	header = (struct cloop_header *) buf;
420 	if (strncmp(header->magic, CLOOP_MAGIC_START,
421 		    sizeof(CLOOP_MAGIC_START) - 1) != 0) {
422 		DPRINTF(("%s: no CLOOP magic\n", gp->name));
423 		goto err;
424 	}
425 	if (header->magic[0x0b] != 'V' || header->magic[0x0c] < '2') {
426 		DPRINTF(("%s: image version too old\n", gp->name));
427 		goto err;
428 	}
429 
430 	/*
431 	 * Initialize softc and read offsets.
432 	 */
433 	sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO);
434 	gp->softc = sc;
435 	sc->blksz = ntohl(header->blksz);
436 	sc->nblocks = ntohl(header->nblocks);
437 	if (sc->blksz % 512 != 0) {
438 		printf("%s: block size (%u) should be multiple of 512.\n",
439 		    gp->name, sc->blksz);
440 		goto err;
441 	}
442 	if (sc->blksz > MAX_BLKSZ) {
443 		printf("%s: block size (%u) should not be larger than %d.\n",
444 		    gp->name, sc->blksz, MAX_BLKSZ);
445 	}
446 	total_offsets = sc->nblocks + 1;
447 	if (sizeof(struct cloop_header) +
448 	    total_offsets * sizeof(uint64_t) > pp->mediasize) {
449 		printf("%s: media too small for %u blocks\n",
450 		       gp->name, sc->nblocks);
451 		goto err;
452 	}
453 	sc->offsets = malloc(
454 	    total_offsets * sizeof(uint64_t), M_GEOM_UZIP, M_WAITOK);
455 	offsets_read = MIN(total_offsets,
456 	    (pp->sectorsize - sizeof(*header)) / sizeof(uint64_t));
457 	for (i = 0; i < offsets_read; i++)
458 		sc->offsets[i] = be64toh(((uint64_t *) (header + 1))[i]);
459 	DPRINTF(("%s: %u offsets in the first sector\n",
460 	       gp->name, offsets_read));
461 	for (blk = 1; offsets_read < total_offsets; blk++) {
462 		uint32_t nread;
463 
464 		free(buf, M_GEOM);
465 		buf = g_read_data(
466 		    cp, blk * pp->sectorsize, pp->sectorsize, NULL);
467 		if (buf == NULL)
468 			goto err;
469 		nread = MIN(total_offsets - offsets_read,
470 		     pp->sectorsize / sizeof(uint64_t));
471 		DPRINTF(("%s: %u offsets read from sector %d\n",
472 		    gp->name, nread, blk));
473 		for (i = 0; i < nread; i++) {
474 			sc->offsets[offsets_read + i] =
475 			    be64toh(((uint64_t *) buf)[i]);
476 		}
477 		offsets_read += nread;
478 	}
479 	DPRINTF(("%s: done reading offsets\n", gp->name));
480 	mtx_init(&sc->last_mtx, "geom_uzip cache", NULL, MTX_DEF);
481 	sc->last_blk = -1;
482 	sc->last_buf = malloc(sc->blksz, M_GEOM_UZIP, M_WAITOK);
483 	sc->req_total = 0;
484 	sc->req_cached = 0;
485 
486 	g_topology_lock();
487 	pp2 = g_new_providerf(gp, "%s", gp->name);
488 	pp2->sectorsize = 512;
489 	pp2->mediasize = (off_t)sc->nblocks * sc->blksz;
490         pp2->stripesize = pp->stripesize;
491         pp2->stripeoffset = pp->stripeoffset;
492 	g_error_provider(pp2, 0);
493 	g_access(cp, -1, 0, 0);
494 
495 	DPRINTF(("%s: taste ok (%d, %jd), (%d, %d), %x\n",
496 	    gp->name,
497 	    pp2->sectorsize, (intmax_t)pp2->mediasize,
498 	    pp2->stripeoffset, pp2->stripesize, pp2->flags));
499 	printf("%s: %u x %u blocks\n",
500 	       gp->name, sc->nblocks, sc->blksz);
501 	return (gp);
502 
503 err:
504 	g_topology_lock();
505 	g_access(cp, -1, 0, 0);
506 	if (buf != NULL)
507 		free(buf, M_GEOM);
508 	if (gp->softc != NULL) {
509 		g_uzip_softc_free(gp->softc, NULL);
510 		gp->softc = NULL;
511 	}
512 	g_detach(cp);
513 	g_destroy_consumer(cp);
514 	g_destroy_geom(gp);
515 	return (NULL);
516 }
517 
518 static int
519 g_uzip_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
520 {
521 	struct g_provider *pp;
522 
523 	g_trace(G_T_TOPOLOGY, "g_uzip_destroy_geom(%s, %s)", mp->name, gp->name);
524 	g_topology_assert();
525 
526 	if (gp->softc == NULL) {
527 		printf("%s(%s): gp->softc == NULL\n", __func__, gp->name);
528 		return (ENXIO);
529 	}
530 
531 	KASSERT(gp != NULL, ("NULL geom"));
532 	pp = LIST_FIRST(&gp->provider);
533 	KASSERT(pp != NULL, ("NULL provider"));
534 	if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
535 		return (EBUSY);
536 
537 	g_uzip_softc_free(gp->softc, gp);
538 	gp->softc = NULL;
539 	g_wither_geom(gp, ENXIO);
540 	return (0);
541 }
542 
543 static struct g_class g_uzip_class = {
544 	.name = UZIP_CLASS_NAME,
545 	.version = G_VERSION,
546 	.taste = g_uzip_taste,
547 	.destroy_geom = g_uzip_destroy_geom,
548 
549 	.start = g_uzip_start,
550 	.orphan = g_uzip_orphan,
551 	.access = g_uzip_access,
552 	.spoiled = g_uzip_spoiled,
553 };
554 
555 DECLARE_GEOM_CLASS(g_uzip_class, g_uzip);
556 MODULE_DEPEND(g_uzip, zlib, 1, 1, 1);
557