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