The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/uncompress/g_uncompress.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2010-2012 Aleksandr Rybalko
    3  * Copyright (c) 2004 Max Khon
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 /*
   29  * GEOM UNCOMPRESS module - simple decompression module for use with read-only
   30  * copressed images maked by mkuzip(8) or mkulzma(8) utilities.
   31  *
   32  * To enable module in kernel config, put this line:
   33  * device       geom_uncompress
   34  */
   35 
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD: releng/10.3/sys/geom/uncompress/g_uncompress.c 289958 2015-10-25 22:38:17Z ngie $");
   38 
   39 #include <sys/param.h>
   40 #include <sys/bio.h>
   41 #include <sys/endian.h>
   42 #include <sys/errno.h>
   43 #include <sys/kernel.h>
   44 #include <sys/lock.h>
   45 #include <sys/mutex.h>
   46 #include <sys/malloc.h>
   47 #include <sys/systm.h>
   48 
   49 #include <geom/geom.h>
   50 
   51 #include <net/zlib.h>
   52 #include <contrib/xz-embedded/linux/include/linux/xz.h>
   53 
   54 #ifdef GEOM_UNCOMPRESS_DEBUG
   55 #define DPRINTF(a)      printf a
   56 extern int g_debugflags;
   57 #else
   58 #define DPRINTF(a)
   59 #endif
   60 
   61 static MALLOC_DEFINE(M_GEOM_UNCOMPRESS, "geom_uncompress",
   62     "GEOM UNCOMPRESS data structures");
   63 
   64 #define UNCOMPRESS_CLASS_NAME   "UNCOMPRESS"
   65 #define GEOM_UZIP_MAJVER '2'
   66 #define GEOM_ULZMA_MAJVER '3'
   67 
   68 /*
   69  * Maximum allowed valid block size (to prevent foot-shooting)
   70  */
   71 #define MAX_BLKSZ       (MAXPHYS)
   72 
   73 /*
   74  * Integer values (block size, number of blocks, offsets)
   75  * are stored in big-endian (network) order on disk and struct cloop_header
   76  * and in native order in struct g_uncompress_softc
   77  */
   78 
   79 #define CLOOP_MAGIC_LEN 128
   80 static char CLOOP_MAGIC_START[] = "#!/bin/sh\n";
   81 
   82 struct cloop_header {
   83         char magic[CLOOP_MAGIC_LEN];    /* cloop magic */
   84         uint32_t blksz;                 /* block size */
   85         uint32_t nblocks;               /* number of blocks */
   86 };
   87 
   88 struct g_uncompress_softc {
   89         uint32_t blksz;                 /* block size */
   90         uint32_t nblocks;               /* number of blocks */
   91         uint64_t *offsets;
   92         enum {
   93                 GEOM_UZIP = 1,
   94                 GEOM_ULZMA
   95         } type;
   96 
   97         struct mtx last_mtx;
   98         uint32_t last_blk;              /* last blk no */
   99         char *last_buf;                 /* last blk data */
  100         int req_total;                  /* total requests */
  101         int req_cached;                 /* cached requests */
  102 
  103         /* XZ decoder structs */
  104         struct xz_buf *b;
  105         struct xz_dec *s;
  106         z_stream *zs;
  107 };
  108 
  109 static void
  110 g_uncompress_softc_free(struct g_uncompress_softc *sc, struct g_geom *gp)
  111 {
  112 
  113         if (gp != NULL) {
  114                 DPRINTF(("%s: %d requests, %d cached\n",
  115                     gp->name, sc->req_total, sc->req_cached));
  116         }
  117         if (sc->offsets != NULL) {
  118                 free(sc->offsets, M_GEOM_UNCOMPRESS);
  119                 sc->offsets = NULL;
  120         }
  121 
  122         switch (sc->type) {
  123         case GEOM_ULZMA:
  124                 if (sc->b) {
  125                         free(sc->b, M_GEOM_UNCOMPRESS);
  126                         sc->b = 0;
  127                 }
  128                 if (sc->s) {
  129                         xz_dec_end(sc->s);
  130                         sc->s = 0;
  131                 }
  132                 break;
  133         case GEOM_UZIP:
  134                 if (sc->zs) {
  135                         inflateEnd(sc->zs);
  136                         free(sc->zs, M_GEOM_UNCOMPRESS);
  137                         sc->zs = 0;
  138                 }
  139                 break;
  140         }
  141 
  142         mtx_destroy(&sc->last_mtx);
  143         free(sc->last_buf, M_GEOM_UNCOMPRESS);
  144         free(sc, M_GEOM_UNCOMPRESS);
  145 }
  146 
  147 static void *
  148 z_alloc(void *nil, u_int type, u_int size)
  149 {
  150         void *ptr;
  151 
  152         ptr = malloc(type * size, M_GEOM_UNCOMPRESS, M_NOWAIT);
  153 
  154         return (ptr);
  155 }
  156 
  157 static void
  158 z_free(void *nil, void *ptr)
  159 {
  160 
  161         free(ptr, M_GEOM_UNCOMPRESS);
  162 }
  163 
  164 static void
  165 g_uncompress_done(struct bio *bp)
  166 {
  167         struct g_uncompress_softc *sc;
  168         struct g_provider *pp, *pp2;
  169         struct g_consumer *cp;
  170         struct g_geom *gp;
  171         struct bio *bp2;
  172         uint32_t start_blk, i;
  173         off_t iolen, pos, upos;
  174         size_t bsize;
  175         int err;
  176 
  177         err = 0;
  178         bp2 = bp->bio_parent;
  179         pp = bp2->bio_to;
  180         gp = pp->geom;
  181         cp = LIST_FIRST(&gp->consumer);
  182         pp2 = cp->provider;
  183         sc = gp->softc;
  184         DPRINTF(("%s: done\n", gp->name));
  185 
  186         bp2->bio_error = bp->bio_error;
  187         if (bp2->bio_error != 0)
  188                 goto done;
  189 
  190         /*
  191          * Example:
  192          * Uncompressed block size = 65536
  193          * User request: 65540-261632
  194          * (4 uncompressed blocks -4B at start, -512B at end)
  195          *
  196          * We have 512(secsize)*63(nsec) = 32256B at offset 1024
  197          * From:  1024  bp->bio_offset = 1024
  198          * To:   33280  bp->bio_length = 33280 - 1024 = 32256
  199          * Compressed blocks: 0-1020, 1020-1080, 1080-4845, 4845-12444,
  200          *      12444-33210, 33210-44100, ...
  201          *
  202          * Get start_blk from user request:
  203          * start_blk = bp2->bio_offset / 65536 = 65540/65536 = 1
  204          * bsize (block size of parent) = pp2->sectorsize (Now is 4B)
  205          * pos(in stream from parent) = sc->offsets[start_blk] % bsize =
  206          *    = sc->offsets[1] % 4 = 1020 % 4 = 0
  207          */
  208 
  209         /*
  210          * Uncompress data.
  211          */
  212         start_blk = bp2->bio_offset / sc->blksz;
  213         bsize = pp2->sectorsize;
  214         iolen = bp->bio_completed;
  215         pos = sc->offsets[start_blk] % bsize;
  216         upos = 0;
  217 
  218         DPRINTF(("%s: done: bio_length %jd bio_completed %jd start_blk %d, "
  219             "pos %jd, upos %jd (%jd, %d, %zu)\n",
  220             gp->name, (intmax_t)bp->bio_length, (intmax_t)bp->bio_completed,
  221             start_blk, (intmax_t)pos, (intmax_t)upos,
  222             (intmax_t)bp2->bio_offset, sc->blksz, bsize));
  223 
  224         for (i = start_blk; upos < bp2->bio_length; i++) {
  225                 off_t len, ulen, uoff;
  226 
  227                 uoff = i == start_blk ? bp2->bio_offset % sc->blksz : 0;
  228                 ulen = MIN(sc->blksz - uoff, bp2->bio_length - upos);
  229                 len = sc->offsets[i + 1] - sc->offsets[i];
  230 
  231                 DPRINTF((
  232                     "%s: done: inflate block %d, start %ju, end %ju len %jd\n",
  233                     gp->name, i, (uintmax_t)sc->offsets[i],
  234                     (uintmax_t)sc->offsets[i + 1], (intmax_t)len));
  235 
  236                 if (len == 0) {
  237                         /* All zero block: no cache update */
  238                         bzero(bp2->bio_data + upos, ulen);
  239                         upos += ulen;
  240                         bp2->bio_completed += ulen;
  241                         continue;
  242                 }
  243                 if (len > iolen) {
  244                         DPRINTF(("%s: done: early termination: len (%jd) > "
  245                             "iolen (%jd)\n",
  246                             gp->name, (intmax_t)len, (intmax_t)iolen));
  247                         break;
  248                 }
  249                 mtx_lock(&sc->last_mtx);
  250 
  251 #ifdef GEOM_UNCOMPRESS_DEBUG
  252                 if (g_debugflags & 32)
  253                         hexdump(bp->bio_data + pos, len, 0, 0);
  254 #endif
  255 
  256                 switch (sc->type) {
  257                 case GEOM_ULZMA:
  258                         sc->b->in = bp->bio_data + pos;
  259                         sc->b->out = sc->last_buf;
  260                         sc->b->in_pos = sc->b->out_pos = 0;
  261                         sc->b->in_size = len;
  262                         sc->b->out_size = (size_t)-1;
  263 
  264                         err = (xz_dec_run(sc->s, sc->b) != XZ_STREAM_END) ?
  265                             1 : 0;
  266                         /* TODO decoder recovery, if needed */
  267                         break;
  268                 case GEOM_UZIP:
  269                         sc->zs->next_in = bp->bio_data + pos;
  270                         sc->zs->avail_in = len;
  271                         sc->zs->next_out = sc->last_buf;
  272                         sc->zs->avail_out = sc->blksz;
  273 
  274                         err = (inflate(sc->zs, Z_FINISH) != Z_STREAM_END) ?
  275                             1 : 0;
  276                         if ((err) || (inflateReset(sc->zs) != Z_OK))
  277                                 printf("%s: UZIP decoder reset failed\n",
  278                                      gp->name);
  279                         break;
  280                 }
  281 
  282                 if (err) {
  283                         sc->last_blk = -1;
  284                         mtx_unlock(&sc->last_mtx);
  285                         DPRINTF(("%s: done: inflate failed, code=%d\n",
  286                             gp->name, err));
  287                         bp2->bio_error = EIO;
  288                         goto done;
  289                 }
  290 
  291 #ifdef GEOM_UNCOMPRESS_DEBUG
  292                 if (g_debugflags & 32)
  293                         hexdump(sc->last_buf, sc->b->out_size, 0, 0);
  294 #endif
  295 
  296                 sc->last_blk = i;
  297                 DPRINTF(("%s: done: inflated \n", gp->name));
  298                 memcpy(bp2->bio_data + upos, sc->last_buf + uoff, ulen);
  299                 mtx_unlock(&sc->last_mtx);
  300 
  301                 pos += len;
  302                 iolen -= len;
  303                 upos += ulen;
  304                 bp2->bio_completed += ulen;
  305         }
  306 
  307 done:
  308         /*
  309          * Finish processing the request.
  310          */
  311         DPRINTF(("%s: done: (%d, %jd, %ld)\n",
  312             gp->name, bp2->bio_error, (intmax_t)bp2->bio_completed,
  313             bp2->bio_resid));
  314         free(bp->bio_data, M_GEOM_UNCOMPRESS);
  315         g_destroy_bio(bp);
  316         g_io_deliver(bp2, bp2->bio_error);
  317 }
  318 
  319 static void
  320 g_uncompress_start(struct bio *bp)
  321 {
  322         struct g_uncompress_softc *sc;
  323         struct g_provider *pp, *pp2;
  324         struct g_consumer *cp;
  325         struct g_geom *gp;
  326         struct bio *bp2;
  327         uint32_t start_blk, end_blk;
  328         size_t bsize;
  329 
  330         pp = bp->bio_to;
  331         gp = pp->geom;
  332         DPRINTF(("%s: start (%d:%s) to %s off=%jd len=%jd\n",
  333             gp->name, bp->bio_cmd,
  334             (bp->bio_cmd == BIO_READ) ? "BIO_READ" : "NOTSUPPORTED",
  335             pp->name, (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length));
  336 
  337         if (bp->bio_cmd != BIO_READ) {
  338                 g_io_deliver(bp, EOPNOTSUPP);
  339                 return;
  340         }
  341 
  342         cp = LIST_FIRST(&gp->consumer);
  343         pp2 = cp->provider;
  344         sc = gp->softc;
  345 
  346         start_blk = bp->bio_offset / sc->blksz;
  347         end_blk   = howmany(bp->bio_offset + bp->bio_length, sc->blksz);
  348         KASSERT(start_blk < sc->nblocks, ("start_blk out of range"));
  349         KASSERT(end_blk <= sc->nblocks, ("end_blk out of range"));
  350 
  351         sc->req_total++;
  352         if (start_blk + 1 == end_blk) {
  353                 mtx_lock(&sc->last_mtx);
  354                 if (start_blk == sc->last_blk) {
  355                         off_t uoff;
  356 
  357                         uoff = bp->bio_offset % sc->blksz;
  358                         KASSERT(bp->bio_length <= sc->blksz - uoff,
  359                             ("cached data error"));
  360                         memcpy(bp->bio_data, sc->last_buf + uoff,
  361                             bp->bio_length);
  362                         sc->req_cached++;
  363                         mtx_unlock(&sc->last_mtx);
  364 
  365                         DPRINTF(("%s: start: cached 0 + %jd, %jd + 0 + %jd\n",
  366                             gp->name, (intmax_t)bp->bio_length, (intmax_t)uoff,
  367                             (intmax_t)bp->bio_length));
  368                         bp->bio_completed = bp->bio_length;
  369                         g_io_deliver(bp, 0);
  370                         return;
  371                 }
  372                 mtx_unlock(&sc->last_mtx);
  373         }
  374 
  375         bp2 = g_clone_bio(bp);
  376         if (bp2 == NULL) {
  377                 g_io_deliver(bp, ENOMEM);
  378                 return;
  379         }
  380         DPRINTF(("%s: start (%d..%d), %s: %d + %jd, %s: %d + %jd\n",
  381             gp->name, start_blk, end_blk,
  382             pp->name, pp->sectorsize, (intmax_t)pp->mediasize,
  383             pp2->name, pp2->sectorsize, (intmax_t)pp2->mediasize));
  384         bsize = pp2->sectorsize;
  385         bp2->bio_done = g_uncompress_done;
  386         bp2->bio_offset = rounddown(sc->offsets[start_blk], bsize);
  387         while (1) {
  388                 bp2->bio_length = roundup(sc->offsets[end_blk], bsize) -
  389                     bp2->bio_offset;
  390                 if (bp2->bio_length < MAXPHYS)
  391                         break;
  392 
  393                 end_blk--;
  394                 DPRINTF((
  395                     "%s: bio_length (%jd) > MAXPHYS: lowering end_blk to %u\n",
  396                     gp->name, (intmax_t)bp2->bio_length, end_blk));
  397         }
  398         DPRINTF(("%s: start %jd + %jd -> %ju + %ju -> %jd + %jd\n",
  399             gp->name,
  400             (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length,
  401             (uintmax_t)sc->offsets[start_blk],
  402             (uintmax_t)sc->offsets[end_blk] - sc->offsets[start_blk],
  403             (intmax_t)bp2->bio_offset, (intmax_t)bp2->bio_length));
  404         bp2->bio_data = malloc(bp2->bio_length, M_GEOM_UNCOMPRESS, M_NOWAIT);
  405         if (bp2->bio_data == NULL) {
  406                 g_destroy_bio(bp2);
  407                 g_io_deliver(bp, ENOMEM);
  408                 return;
  409         }
  410 
  411         g_io_request(bp2, cp);
  412         DPRINTF(("%s: start ok\n", gp->name));
  413 }
  414 
  415 static void
  416 g_uncompress_orphan(struct g_consumer *cp)
  417 {
  418         struct g_geom *gp;
  419 
  420         g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->provider->name);
  421         g_topology_assert();
  422 
  423         gp = cp->geom;
  424         g_uncompress_softc_free(gp->softc, gp);
  425         gp->softc = NULL;
  426         g_wither_geom(gp, ENXIO);
  427 }
  428 
  429 static int
  430 g_uncompress_access(struct g_provider *pp, int dr, int dw, int de)
  431 {
  432         struct g_consumer *cp;
  433         struct g_geom *gp;
  434 
  435         gp = pp->geom;
  436         cp = LIST_FIRST(&gp->consumer);
  437         KASSERT (cp != NULL, ("g_uncompress_access but no consumer"));
  438 
  439         if (cp->acw + dw > 0)
  440                 return (EROFS);
  441 
  442         return (g_access(cp, dr, dw, de));
  443 }
  444 
  445 static void
  446 g_uncompress_spoiled(struct g_consumer *cp)
  447 {
  448         struct g_geom *gp;
  449 
  450         gp = cp->geom;
  451         g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name);
  452         g_topology_assert();
  453 
  454         g_uncompress_softc_free(gp->softc, gp);
  455         gp->softc = NULL;
  456         g_wither_geom(gp, ENXIO);
  457 }
  458 
  459 static struct g_geom *
  460 g_uncompress_taste(struct g_class *mp, struct g_provider *pp, int flags)
  461 {
  462         struct cloop_header *header;
  463         struct g_uncompress_softc *sc;
  464         struct g_provider *pp2;
  465         struct g_consumer *cp;
  466         struct g_geom *gp;
  467         uint64_t *offsets;
  468         uint32_t i, r, total, total_offsets, type;
  469         uint8_t *buf;
  470         int error;
  471 
  472         g_trace(G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name);
  473         g_topology_assert();
  474 
  475         /* Skip providers that are already open for writing. */
  476         if (pp->acw > 0)
  477                 return (NULL);
  478 
  479         buf = NULL;
  480 
  481         /*
  482          * Create geom instance.
  483          */
  484         gp = g_new_geomf(mp, "%s.uncompress", pp->name);
  485         cp = g_new_consumer(gp);
  486         error = g_attach(cp, pp);
  487         if (error == 0)
  488                 error = g_access(cp, 1, 0, 0);
  489         if (error) {
  490                 g_detach(cp);
  491                 g_destroy_consumer(cp);
  492                 g_destroy_geom(gp);
  493                 return (NULL);
  494         }
  495         g_topology_unlock();
  496 
  497         /*
  498          * Read cloop header, look for CLOOP magic, perform
  499          * other validity checks.
  500          */
  501         DPRINTF(("%s: media sectorsize %u, mediasize %jd\n",
  502             gp->name, pp->sectorsize, (intmax_t)pp->mediasize));
  503         total = roundup(sizeof(struct cloop_header), pp->sectorsize);
  504         buf = g_read_data(cp, 0, total, NULL);
  505         if (buf == NULL)
  506                 goto err;
  507         header = (struct cloop_header *) buf;
  508         if (strncmp(header->magic, CLOOP_MAGIC_START,
  509             sizeof(CLOOP_MAGIC_START) - 1) != 0) {
  510                 DPRINTF(("%s: no CLOOP magic\n", gp->name));
  511                 goto err;
  512         }
  513 
  514         switch (header->magic[0x0b]) {
  515         case 'L':
  516                 type = GEOM_ULZMA;
  517                 if (header->magic[0x0c] < GEOM_ULZMA_MAJVER) {
  518                         DPRINTF(("%s: image version too old\n", gp->name));
  519                         goto err;
  520                 }
  521                 DPRINTF(("%s: GEOM_ULZMA image found\n", gp->name));
  522                 break;
  523         case 'V':
  524                 type = GEOM_UZIP;
  525                 if (header->magic[0x0c] < GEOM_UZIP_MAJVER) {
  526                         DPRINTF(("%s: image version too old\n", gp->name));
  527                         goto err;
  528                 }
  529                 DPRINTF(("%s: GEOM_UZIP image found\n", gp->name));
  530                 break;
  531         default:
  532                 DPRINTF(("%s: unsupported image type\n", gp->name));
  533                 goto err;
  534         }
  535 
  536         DPRINTF(("%s: found CLOOP magic\n", gp->name));
  537         /*
  538          * Initialize softc and read offsets.
  539          */
  540         sc = malloc(sizeof(*sc), M_GEOM_UNCOMPRESS, M_WAITOK | M_ZERO);
  541         gp->softc = sc;
  542         sc->type = type;
  543         sc->blksz = ntohl(header->blksz);
  544         sc->nblocks = ntohl(header->nblocks);
  545         if (sc->blksz % 4 != 0) {
  546                 printf("%s: block size (%u) should be multiple of 4.\n",
  547                     gp->name, sc->blksz);
  548                 goto err;
  549         }
  550         if (sc->blksz > MAX_BLKSZ) {
  551                 printf("%s: block size (%u) should not be larger than %d.\n",
  552                     gp->name, sc->blksz, MAX_BLKSZ);
  553         }
  554         total_offsets = sc->nblocks + 1;
  555         if (sizeof(struct cloop_header) +
  556             total_offsets * sizeof(uint64_t) > pp->mediasize) {
  557                 printf("%s: media too small for %u blocks\n",
  558                     gp->name, sc->nblocks);
  559                 goto err;
  560         }
  561         g_free(buf);
  562 
  563         sc->offsets = malloc(total_offsets * sizeof(uint64_t),
  564             M_GEOM_UNCOMPRESS, M_WAITOK | M_ZERO);
  565         total = roundup((sizeof(struct cloop_header) +
  566             total_offsets * sizeof(uint64_t)), pp->sectorsize);
  567 #define RSZ     ((total - r) > MAXPHYS ? MAXPHYS: (total - r))
  568         for (r = 0, i = 0; r < total; r += MAXPHYS) {
  569                 buf = g_read_data(cp, r, RSZ, &error);
  570                 if (buf == NULL) {
  571                         free(sc->offsets, M_GEOM_UNCOMPRESS);
  572                         goto err;
  573                 }
  574                 offsets = (uint64_t *)buf;
  575                 if (r == 0)
  576                         offsets +=
  577                             sizeof(struct cloop_header) / sizeof(uint64_t);
  578                 for (; i < total_offsets && offsets < (uint64_t *)(buf + RSZ);
  579                     i++, offsets++)
  580                         sc->offsets[i] = be64toh(*offsets);
  581                 g_free(buf);
  582         }
  583 #undef RSZ
  584         buf = NULL;
  585         DPRINTF(("%s: done reading offsets\n", gp->name));
  586         mtx_init(&sc->last_mtx, "geom_uncompress cache", NULL, MTX_DEF);
  587         sc->last_blk = -1;
  588         sc->last_buf = malloc(sc->blksz, M_GEOM_UNCOMPRESS, M_WAITOK);
  589         sc->req_total = 0;
  590         sc->req_cached = 0;
  591 
  592         switch (sc->type) {
  593         case GEOM_ULZMA:
  594                 xz_crc32_init();
  595                 sc->s = xz_dec_init(XZ_SINGLE, 0);
  596                 sc->b = (struct xz_buf*)malloc(sizeof(struct xz_buf),
  597                     M_GEOM_UNCOMPRESS, M_WAITOK);
  598                 break;
  599         case GEOM_UZIP:
  600                 sc->zs = (z_stream *)malloc(sizeof(z_stream),
  601                     M_GEOM_UNCOMPRESS, M_WAITOK);
  602                 sc->zs->zalloc = z_alloc;
  603                 sc->zs->zfree = z_free;
  604                 if (inflateInit(sc->zs) != Z_OK) {
  605                         goto err;
  606                 }
  607                 break;
  608         }
  609 
  610         g_topology_lock();
  611         pp2 = g_new_providerf(gp, "%s", gp->name);
  612         pp2->sectorsize = 512;
  613         pp2->mediasize = (off_t)sc->nblocks * sc->blksz;
  614         if (pp->stripesize > 0) {
  615                 pp2->stripesize = pp->stripesize;
  616                 pp2->stripeoffset = pp->stripeoffset;
  617         }
  618         g_error_provider(pp2, 0);
  619         g_access(cp, -1, 0, 0);
  620 
  621         DPRINTF(("%s: taste ok (%d, %jd), (%d, %d), %x\n",
  622             gp->name,
  623             pp2->sectorsize, (intmax_t)pp2->mediasize,
  624             pp2->stripeoffset, pp2->stripesize, pp2->flags));
  625         DPRINTF(("%s: %u x %u blocks\n", gp->name, sc->nblocks, sc->blksz));
  626         return (gp);
  627 
  628 err:
  629         g_topology_lock();
  630         g_access(cp, -1, 0, 0);
  631         if (buf != NULL)
  632                 g_free(buf);
  633         if (gp->softc != NULL) {
  634                 g_uncompress_softc_free(gp->softc, NULL);
  635                 gp->softc = NULL;
  636         }
  637         g_detach(cp);
  638         g_destroy_consumer(cp);
  639         g_destroy_geom(gp);
  640 
  641         return (NULL);
  642 }
  643 
  644 static int
  645 g_uncompress_destroy_geom(struct gctl_req *req, struct g_class *mp,
  646                           struct g_geom *gp)
  647 {
  648         struct g_provider *pp;
  649 
  650         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, gp->name);
  651         g_topology_assert();
  652 
  653         if (gp->softc == NULL) {
  654                 DPRINTF(("%s(%s): gp->softc == NULL\n", __func__, gp->name));
  655                 return (ENXIO);
  656         }
  657 
  658         KASSERT(gp != NULL, ("NULL geom"));
  659         pp = LIST_FIRST(&gp->provider);
  660         KASSERT(pp != NULL, ("NULL provider"));
  661         if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
  662                 return (EBUSY);
  663 
  664         g_uncompress_softc_free(gp->softc, gp);
  665         gp->softc = NULL;
  666         g_wither_geom(gp, ENXIO);
  667 
  668         return (0);
  669 }
  670 
  671 static struct g_class g_uncompress_class = {
  672         .name = UNCOMPRESS_CLASS_NAME,
  673         .version = G_VERSION,
  674         .taste = g_uncompress_taste,
  675         .destroy_geom = g_uncompress_destroy_geom,
  676 
  677         .start = g_uncompress_start,
  678         .orphan = g_uncompress_orphan,
  679         .access = g_uncompress_access,
  680         .spoiled = g_uncompress_spoiled,
  681 };
  682 
  683 DECLARE_GEOM_CLASS(g_uncompress_class, g_uncompress);
  684 MODULE_DEPEND(g_uncompress, zlib, 1, 1, 1);

Cache object: 1fcc111c0b6945fc987781aabde8bc1b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.