xref: /freebsd/sys/geom/uzip/g_uzip.c (revision 0572ccaa4543b0abef8ef81e384c1d04de9f3da1)
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/sysctl.h>
39 #include <sys/systm.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 
93 	if (gp != NULL) {
94 		printf("%s: %d requests, %d cached\n",
95 		    gp->name, sc->req_total, sc->req_cached);
96 	}
97 	if (sc->offsets != NULL) {
98 		free(sc->offsets, M_GEOM_UZIP);
99 		sc->offsets = NULL;
100 	}
101 	mtx_destroy(&sc->last_mtx);
102 	free(sc->last_buf, M_GEOM_UZIP);
103 	free(sc, M_GEOM_UZIP);
104 }
105 
106 static void *
107 z_alloc(void *nil, u_int type, u_int size)
108 {
109 	void *ptr;
110 
111 	ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT);
112 
113 	return (ptr);
114 }
115 
116 static void
117 z_free(void *nil, void *ptr)
118 {
119 
120 	free(ptr, M_GEOM_UZIP);
121 }
122 
123 static void
124 g_uzip_done(struct bio *bp)
125 {
126 	int err;
127 	struct bio *bp2;
128 	z_stream zs;
129 	struct g_provider *pp, *pp2;
130 	struct g_consumer *cp;
131 	struct g_geom *gp;
132 	struct g_uzip_softc *sc;
133 	off_t iolen, pos, upos;
134 	uint32_t start_blk, i;
135 	size_t bsize;
136 
137 	bp2 = bp->bio_parent;
138 	pp = bp2->bio_to;
139 	gp = pp->geom;
140 	cp = LIST_FIRST(&gp->consumer);
141 	pp2 = cp->provider;
142 	sc = gp->softc;
143 	DPRINTF(("%s: done\n", gp->name));
144 
145 	bp2->bio_error = bp->bio_error;
146 	if (bp2->bio_error != 0)
147 		goto done;
148 
149 	/*
150 	 * Uncompress data.
151 	 */
152 	zs.zalloc = z_alloc;
153 	zs.zfree = z_free;
154 	err = inflateInit(&zs);
155 	if (err != Z_OK) {
156 		bp2->bio_error = EIO;
157 		goto done;
158 	}
159 	start_blk = bp2->bio_offset / sc->blksz;
160 	bsize = pp2->sectorsize;
161 	iolen = bp->bio_completed;
162 	pos = sc->offsets[start_blk] % bsize;
163 	upos = 0;
164 	DPRINTF(("%s: done: start_blk %d, pos %jd, upos %jd, iolen %jd "
165 	    "(%jd, %d, %zd)\n",
166 	    gp->name, start_blk, (intmax_t)pos, (intmax_t)upos,
167 	    (intmax_t)iolen, (intmax_t)bp2->bio_offset, sc->blksz, bsize));
168 	for (i = start_blk; upos < bp2->bio_length; i++) {
169 		off_t len, ulen, uoff;
170 
171 		uoff = i == start_blk ? bp2->bio_offset % sc->blksz : 0;
172 		ulen = MIN(sc->blksz - uoff, bp2->bio_length - upos);
173 		len = sc->offsets[i + 1] - sc->offsets[i];
174 
175 		if (len == 0) {
176 			/* All zero block: no cache update */
177 			bzero(bp2->bio_data + upos, ulen);
178 			upos += ulen;
179 			bp2->bio_completed += ulen;
180 			continue;
181 		}
182 		if (len > iolen) {
183 			DPRINTF(("%s: done: early termination: len (%jd) > "
184 			    "iolen (%jd)\n",
185 			    gp->name, (intmax_t)len, (intmax_t)iolen));
186 			break;
187 		}
188 		zs.next_in = bp->bio_data + pos;
189 		zs.avail_in = len;
190 		zs.next_out = sc->last_buf;
191 		zs.avail_out = sc->blksz;
192 		mtx_lock(&sc->last_mtx);
193 		err = inflate(&zs, Z_FINISH);
194 		if (err != Z_STREAM_END) {
195 			sc->last_blk = -1;
196 			mtx_unlock(&sc->last_mtx);
197 			DPRINTF(("%s: done: inflate failed (%jd + %jd -> %jd + %jd + %jd)\n",
198 			    gp->name, (intmax_t)pos, (intmax_t)len,
199 			    (intmax_t)uoff, (intmax_t)upos, (intmax_t)ulen));
200 			inflateEnd(&zs);
201 			bp2->bio_error = EIO;
202 			goto done;
203 		}
204 		sc->last_blk = i;
205 		DPRINTF(("%s: done: inflated %jd + %jd -> %jd + %jd + %jd\n",
206 		    gp->name, (intmax_t)pos, (intmax_t)len, (intmax_t)uoff,
207 		    (intmax_t)upos, (intmax_t)ulen));
208 		memcpy(bp2->bio_data + upos, sc->last_buf + uoff, ulen);
209 		mtx_unlock(&sc->last_mtx);
210 
211 		pos += len;
212 		iolen -= len;
213 		upos += ulen;
214 		bp2->bio_completed += ulen;
215 		err = inflateReset(&zs);
216 		if (err != Z_OK) {
217 			inflateEnd(&zs);
218 			bp2->bio_error = EIO;
219 			goto done;
220 		}
221 	}
222 	err = inflateEnd(&zs);
223 	if (err != Z_OK) {
224 		bp2->bio_error = EIO;
225 		goto done;
226 	}
227 
228 done:
229 	/*
230 	 * Finish processing the request.
231 	 */
232 	DPRINTF(("%s: done: (%d, %jd, %ld)\n",
233 	    gp->name, bp2->bio_error, (intmax_t)bp2->bio_completed,
234 	    bp2->bio_resid));
235 	free(bp->bio_data, M_GEOM_UZIP);
236 	g_destroy_bio(bp);
237 	g_io_deliver(bp2, bp2->bio_error);
238 }
239 
240 static void
241 g_uzip_start(struct bio *bp)
242 {
243 	struct bio *bp2;
244 	struct g_provider *pp, *pp2;
245 	struct g_geom *gp;
246 	struct g_consumer *cp;
247 	struct g_uzip_softc *sc;
248 	uint32_t start_blk, end_blk;
249 	size_t bsize;
250 
251 	pp = bp->bio_to;
252 	gp = pp->geom;
253 	DPRINTF(("%s: start (%d)\n", gp->name, bp->bio_cmd));
254 
255 	if (bp->bio_cmd != BIO_READ) {
256 		g_io_deliver(bp, EOPNOTSUPP);
257 		return;
258 	}
259 
260 	cp = LIST_FIRST(&gp->consumer);
261 	pp2 = cp->provider;
262 	sc = gp->softc;
263 
264 	start_blk = bp->bio_offset / sc->blksz;
265 	end_blk = (bp->bio_offset + bp->bio_length + sc->blksz - 1) / sc->blksz;
266 	KASSERT(start_blk < sc->nblocks, ("start_blk out of range"));
267 	KASSERT(end_blk <= sc->nblocks, ("end_blk out of range"));
268 
269 	sc->req_total++;
270 	if (start_blk + 1 == end_blk) {
271 		mtx_lock(&sc->last_mtx);
272 		if (start_blk == sc->last_blk) {
273 			off_t uoff;
274 
275 			uoff = bp->bio_offset % sc->blksz;
276 			KASSERT(bp->bio_length <= sc->blksz - uoff,
277 			    ("cached data error"));
278 			memcpy(bp->bio_data, sc->last_buf + uoff,
279 			    bp->bio_length);
280 			sc->req_cached++;
281 			mtx_unlock(&sc->last_mtx);
282 
283 			DPRINTF(("%s: start: cached 0 + %jd, %jd + 0 + %jd\n",
284 			    gp->name, (intmax_t)bp->bio_length, (intmax_t)uoff,
285 			    (intmax_t)bp->bio_length));
286 			bp->bio_completed = bp->bio_length;
287 			g_io_deliver(bp, 0);
288 			return;
289 		}
290 		mtx_unlock(&sc->last_mtx);
291 	}
292 
293 	bp2 = g_clone_bio(bp);
294 	if (bp2 == NULL) {
295 		g_io_deliver(bp, ENOMEM);
296 		return;
297 	}
298 	bp2->bio_done = g_uzip_done;
299 	DPRINTF(("%s: start (%d..%d), %s: %d + %jd, %s: %d + %jd\n",
300 	    gp->name, start_blk, end_blk,
301 	    pp->name, pp->sectorsize, (intmax_t)pp->mediasize,
302 	    pp2->name, pp2->sectorsize, (intmax_t)pp2->mediasize));
303 	bsize = pp2->sectorsize;
304 	bp2->bio_offset = sc->offsets[start_blk] - sc->offsets[start_blk] % bsize;
305 	while (1) {
306 		bp2->bio_length = sc->offsets[end_blk] - bp2->bio_offset;
307 		bp2->bio_length = (bp2->bio_length + bsize - 1) / bsize * bsize;
308 		if (bp2->bio_length < MAXPHYS)
309 			break;
310 
311 		end_blk--;
312 		DPRINTF(("%s: bio_length (%jd) > MAXPHYS: lowering end_blk "
313 		    "to %u\n", gp->name, (intmax_t)bp2->bio_length, end_blk));
314 	}
315 	DPRINTF(("%s: start %jd + %jd -> %ju + %ju -> %jd + %jd\n",
316 	    gp->name,
317 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length,
318 	    (uintmax_t)sc->offsets[start_blk],
319 	    (uintmax_t)sc->offsets[end_blk] - sc->offsets[start_blk],
320 	    (intmax_t)bp2->bio_offset, (intmax_t)bp2->bio_length));
321 	bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UZIP, M_NOWAIT);
322 	if (bp2->bio_data == NULL) {
323 		g_destroy_bio(bp2);
324 		g_io_deliver(bp, ENOMEM);
325 		return;
326 	}
327 
328 	g_io_request(bp2, cp);
329 	DPRINTF(("%s: start ok\n", gp->name));
330 }
331 
332 static void
333 g_uzip_orphan(struct g_consumer *cp)
334 {
335 	struct g_geom *gp;
336 
337 	g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->provider->name);
338 	g_topology_assert();
339 
340 	gp = cp->geom;
341 	g_uzip_softc_free(gp->softc, gp);
342 	gp->softc = NULL;
343 	g_wither_geom(gp, ENXIO);
344 }
345 
346 static int
347 g_uzip_access(struct g_provider *pp, int dr, int dw, int de)
348 {
349 	struct g_geom *gp;
350 	struct g_consumer *cp;
351 
352 	gp = pp->geom;
353 	cp = LIST_FIRST(&gp->consumer);
354 	KASSERT (cp != NULL, ("g_uzip_access but no consumer"));
355 
356 	if (cp->acw + dw > 0)
357 		return (EROFS);
358 
359 	return (g_access(cp, dr, dw, de));
360 }
361 
362 static void
363 g_uzip_spoiled(struct g_consumer *cp)
364 {
365 	struct g_geom *gp;
366 
367 	gp = cp->geom;
368 	g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name);
369 	g_topology_assert();
370 
371 	g_uzip_softc_free(gp->softc, gp);
372 	gp->softc = NULL;
373 	g_wither_geom(gp, ENXIO);
374 }
375 
376 static struct g_geom *
377 g_uzip_taste(struct g_class *mp, struct g_provider *pp, int flags)
378 {
379 	int error;
380 	uint32_t i, total_offsets, offsets_read, blk;
381 	void *buf;
382 	struct cloop_header *header;
383 	struct g_consumer *cp;
384 	struct g_geom *gp;
385 	struct g_provider *pp2;
386 	struct g_uzip_softc *sc;
387 
388 	g_trace(G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name);
389 	g_topology_assert();
390 
391 	/* Skip providers that are already open for writing. */
392 	if (pp->acw > 0)
393 		return (NULL);
394 
395 	buf = NULL;
396 
397 	/*
398 	 * Create geom instance.
399 	 */
400 	gp = g_new_geomf(mp, "%s.uzip", pp->name);
401 	cp = g_new_consumer(gp);
402 	error = g_attach(cp, pp);
403 	if (error == 0)
404 		error = g_access(cp, 1, 0, 0);
405 	if (error) {
406 		g_detach(cp);
407 		g_destroy_consumer(cp);
408 		g_destroy_geom(gp);
409 		return (NULL);
410 	}
411 	g_topology_unlock();
412 
413 	/*
414 	 * Read cloop header, look for CLOOP magic, perform
415 	 * other validity checks.
416 	 */
417 	DPRINTF(("%s: media sectorsize %u, mediasize %jd\n",
418 	    gp->name, pp->sectorsize, (intmax_t)pp->mediasize));
419 	buf = g_read_data(cp, 0, pp->sectorsize, NULL);
420 	if (buf == NULL)
421 		goto err;
422 	header = (struct cloop_header *) buf;
423 	if (strncmp(header->magic, CLOOP_MAGIC_START,
424 	    sizeof(CLOOP_MAGIC_START) - 1) != 0) {
425 		DPRINTF(("%s: no CLOOP magic\n", gp->name));
426 		goto err;
427 	}
428 	if (header->magic[0x0b] != 'V' || header->magic[0x0c] < '2') {
429 		DPRINTF(("%s: image version too old\n", gp->name));
430 		goto err;
431 	}
432 
433 	/*
434 	 * Initialize softc and read offsets.
435 	 */
436 	sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO);
437 	gp->softc = sc;
438 	sc->blksz = ntohl(header->blksz);
439 	sc->nblocks = ntohl(header->nblocks);
440 	if (sc->blksz % 512 != 0) {
441 		printf("%s: block size (%u) should be multiple of 512.\n",
442 		    gp->name, sc->blksz);
443 		goto err;
444 	}
445 	if (sc->blksz > MAX_BLKSZ) {
446 		printf("%s: block size (%u) should not be larger than %d.\n",
447 		    gp->name, sc->blksz, MAX_BLKSZ);
448 	}
449 	total_offsets = sc->nblocks + 1;
450 	if (sizeof(struct cloop_header) +
451 	    total_offsets * sizeof(uint64_t) > pp->mediasize) {
452 		printf("%s: media too small for %u blocks\n",
453 		    gp->name, sc->nblocks);
454 		goto err;
455 	}
456 	sc->offsets = malloc(
457 	    total_offsets * sizeof(uint64_t), M_GEOM_UZIP, M_WAITOK);
458 	offsets_read = MIN(total_offsets,
459 	    (pp->sectorsize - sizeof(*header)) / sizeof(uint64_t));
460 	for (i = 0; i < offsets_read; i++)
461 		sc->offsets[i] = be64toh(((uint64_t *) (header + 1))[i]);
462 	DPRINTF(("%s: %u offsets in the first sector\n",
463 	       gp->name, offsets_read));
464 	for (blk = 1; offsets_read < total_offsets; blk++) {
465 		uint32_t nread;
466 
467 		free(buf, M_GEOM);
468 		buf = g_read_data(
469 		    cp, blk * pp->sectorsize, pp->sectorsize, NULL);
470 		if (buf == NULL)
471 			goto err;
472 		nread = MIN(total_offsets - offsets_read,
473 		     pp->sectorsize / sizeof(uint64_t));
474 		DPRINTF(("%s: %u offsets read from sector %d\n",
475 		    gp->name, nread, blk));
476 		for (i = 0; i < nread; i++) {
477 			sc->offsets[offsets_read + i] =
478 			    be64toh(((uint64_t *) buf)[i]);
479 		}
480 		offsets_read += nread;
481 	}
482 	free(buf, M_GEOM);
483 	DPRINTF(("%s: done reading offsets\n", gp->name));
484 	mtx_init(&sc->last_mtx, "geom_uzip cache", NULL, MTX_DEF);
485 	sc->last_blk = -1;
486 	sc->last_buf = malloc(sc->blksz, M_GEOM_UZIP, M_WAITOK);
487 	sc->req_total = 0;
488 	sc->req_cached = 0;
489 
490 	g_topology_lock();
491 	pp2 = g_new_providerf(gp, "%s", gp->name);
492 	pp2->sectorsize = 512;
493 	pp2->mediasize = (off_t)sc->nblocks * sc->blksz;
494 	pp2->stripesize = pp->stripesize;
495 	pp2->stripeoffset = pp->stripeoffset;
496 	g_error_provider(pp2, 0);
497 	g_access(cp, -1, 0, 0);
498 
499 	DPRINTF(("%s: taste ok (%d, %jd), (%d, %d), %x\n",
500 	    gp->name,
501 	    pp2->sectorsize, (intmax_t)pp2->mediasize,
502 	    pp2->stripeoffset, pp2->stripesize, pp2->flags));
503 	printf("%s: %u x %u blocks\n", gp->name, sc->nblocks, sc->blksz);
504 	return (gp);
505 
506 err:
507 	g_topology_lock();
508 	g_access(cp, -1, 0, 0);
509 	if (buf != NULL)
510 		free(buf, M_GEOM);
511 	if (gp->softc != NULL) {
512 		g_uzip_softc_free(gp->softc, NULL);
513 		gp->softc = NULL;
514 	}
515 	g_detach(cp);
516 	g_destroy_consumer(cp);
517 	g_destroy_geom(gp);
518 
519 	return (NULL);
520 }
521 
522 static int
523 g_uzip_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
524 {
525 	struct g_provider *pp;
526 
527 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, gp->name);
528 	g_topology_assert();
529 
530 	if (gp->softc == NULL) {
531 		printf("%s(%s): gp->softc == NULL\n", __func__, gp->name);
532 		return (ENXIO);
533 	}
534 
535 	KASSERT(gp != NULL, ("NULL geom"));
536 	pp = LIST_FIRST(&gp->provider);
537 	KASSERT(pp != NULL, ("NULL provider"));
538 	if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
539 		return (EBUSY);
540 
541 	g_uzip_softc_free(gp->softc, gp);
542 	gp->softc = NULL;
543 	g_wither_geom(gp, ENXIO);
544 
545 	return (0);
546 }
547 
548 static struct g_class g_uzip_class = {
549 	.name = UZIP_CLASS_NAME,
550 	.version = G_VERSION,
551 	.taste = g_uzip_taste,
552 	.destroy_geom = g_uzip_destroy_geom,
553 
554 	.start = g_uzip_start,
555 	.orphan = g_uzip_orphan,
556 	.access = g_uzip_access,
557 	.spoiled = g_uzip_spoiled,
558 };
559 
560 DECLARE_GEOM_CLASS(g_uzip_class, g_uzip);
561 MODULE_DEPEND(g_uzip, zlib, 1, 1, 1);
562