summaryrefslogtreecommitdiff
path: root/block/dictzip.c
blob: cf07da1b39c8783738f25d7cd08f8ba6ad7a6537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
 * DictZip Block driver for dictzip enabled gzip files
 *
 * Use the "dictzip" tool from the "dictd" package to create gzip files that
 * contain the extra DictZip headers.
 *
 * dictzip(1) is a compression program which creates compressed files in the
 * gzip format (see RFC 1952). However, unlike gzip(1), dictzip(1) compresses
 * the file in pieces and stores an index to the pieces in the gzip header.
 * This allows random access to the file at the granularity of the compressed
 * pieces (currently about 64kB) while maintaining good compression ratios
 * (within 5% of the expected ratio for dictionary data).
 * dictd(8) uses files stored in this format.
 *
 * For details on DictZip see http://dict.org/.
 *
 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "qemu-common.h"
#include "block/block_int.h"
#include <zlib.h>

// #define DEBUG

#ifdef DEBUG
#define dprintf(fmt, ...) do { printf("dzip: " fmt, ## __VA_ARGS__); } while (0)
#else
#define dprintf(fmt, ...) do { } while (0)
#endif

#define SECTOR_SIZE 512
#define Z_STREAM_COUNT 4
#define CACHE_COUNT 20

/* magic values */

#define GZ_MAGIC1     0x1f
#define GZ_MAGIC2     0x8b
#define DZ_MAGIC1      'R'
#define DZ_MAGIC2      'A'

#define GZ_FEXTRA     0x04      /* Optional field (random access index)    */
#define GZ_FNAME      0x08      /* Original name                           */
#define GZ_COMMENT    0x10      /* Zero-terminated, human-readable comment */
#define GZ_FHCRC      0x02      /* Header CRC16                            */

/* offsets */

#define GZ_ID            0      /* GZ_MAGIC (16bit)                        */
#define GZ_FLG           3      /* FLaGs (see above)                       */
#define GZ_XLEN         10      /* eXtra LENgth (16bit)                    */
#define GZ_SI           12      /* Subfield ID (16bit)                     */
#define GZ_VERSION      16      /* Version for subfield format             */
#define GZ_CHUNKSIZE    18      /* Chunk size (16bit)                      */
#define GZ_CHUNKCNT     20      /* Number of chunks (16bit)                */
#define GZ_RNDDATA      22      /* Random access data (16bit)              */

#define GZ_99_CHUNKSIZE 18      /* Chunk size (32bit)                      */
#define GZ_99_CHUNKCNT  22      /* Number of chunks (32bit)                */
#define GZ_99_FILESIZE  26      /* Size of unpacked file (64bit)           */
#define GZ_99_RNDDATA   34      /* Random access data (32bit)              */

struct BDRVDictZipState;

typedef struct DictZipAIOCB {
    BlockAIOCB common;
    struct BDRVDictZipState *s;
    QEMUIOVector *qiov;          /* QIOV of the original request */
    QEMUIOVector *qiov_gz;       /* QIOV of the gz subrequest */
    QEMUBH *bh;                  /* BH for cache */
    z_stream *zStream;           /* stream to use for decoding */
    int zStream_id;              /* stream id of the above pointer */
    size_t start;                /* offset into the uncompressed file */
    size_t len;                  /* uncompressed bytes to read */
    uint8_t *gzipped;            /* the gzipped data */
    uint8_t *buf;                /* cached result */
    size_t gz_len;               /* amount of gzip data */
    size_t gz_start;             /* uncompressed starting point of gzip data */
    uint64_t offset;             /* offset for "start" into the uncompressed chunk */
    int chunks_len;              /* amount of uncompressed data in all gzip data */
} DictZipAIOCB;

typedef struct dict_cache {
    size_t start;
    size_t len;
    uint8_t *buf;
} DictCache;

typedef struct BDRVDictZipState {
    BlockDriverState *hd;
    z_stream zStream[Z_STREAM_COUNT];
    DictCache cache[CACHE_COUNT];
    int cache_index;
    uint8_t  stream_in_use;
    uint64_t chunk_len;
    uint32_t chunk_cnt;
    uint16_t *chunks;
    uint32_t *chunks32;
    uint64_t *offsets;
    int64_t file_len;
} BDRVDictZipState;

static int dictzip_probe(const uint8_t *buf, int buf_size, const char *filename)
{
    if (buf_size < 2)
        return 0;

    /* We match on every gzip file */
    if ((buf[0] == GZ_MAGIC1) && (buf[1] == GZ_MAGIC2))
        return 100;

    return 0;
}

static int start_zStream(z_stream *zStream)
{
    zStream->zalloc    = NULL;
    zStream->zfree     = NULL;
    zStream->opaque    = NULL;
    zStream->next_in   = 0;
    zStream->avail_in  = 0;
    zStream->next_out  = NULL;
    zStream->avail_out = 0;

    return inflateInit2( zStream, -15 );
}

static QemuOptsList runtime_opts = {
    .name = "dzip",
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
    .desc = {
        {
            .name = "filename",
            .type = QEMU_OPT_STRING,
            .help = "URL to the dictzip file",
        },
        { /* end of list */ }
    },
};

static int dictzip_open(BlockDriverState *bs, QDict *options, int flags, Error **errp)
{
    BDRVDictZipState *s = bs->opaque;
    const char *err = "Unknown (read error?)";
    uint8_t magic[2];
    char buf[100];
    uint8_t header_flags;
    uint16_t chunk_len16;
    uint16_t chunk_cnt16;
    uint16_t header_ver;
    uint16_t tmp_short;
    uint64_t offset;
    int chunks_len;
    int headerLength = GZ_XLEN - 1;
    int rnd_offs;
    int ret;
    int i;
    QemuOpts *opts;
    Error *local_err = NULL;
    const char *filename;

    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
    qemu_opts_absorb_qdict(opts, options, &local_err);
    if (local_err != NULL) {
        error_propagate(errp, local_err);
        ret = -EINVAL;
        goto fail;
    }

    filename = qemu_opt_get(opts, "filename");

    if (!strncmp(filename, "dzip://", 7))
        filename += 7;
    else if (!strncmp(filename, "dzip:", 5))
        filename += 5;

    ret = bdrv_open(&s->hd, filename, NULL, NULL, flags | BDRV_O_PROTOCOL, NULL, &local_err);
    if (ret < 0) {
        error_propagate(errp, local_err);
        qemu_opts_del(opts);
        return ret;
    }

    /* initialize zlib streams */
    for (i = 0; i < Z_STREAM_COUNT; i++) {
        if (start_zStream( &s->zStream[i] ) != Z_OK) {
            err = s->zStream[i].msg;
            goto fail;
        }
    }

    /* gzip header */
    if (bdrv_pread(s->hd, GZ_ID, &magic, sizeof(magic)) != sizeof(magic))
        goto fail;

    if (!((magic[0] == GZ_MAGIC1) && (magic[1] == GZ_MAGIC2))) {
        err = "No gzip file";
        goto fail;
    }

    /* dzip header */
    if (bdrv_pread(s->hd, GZ_FLG, &header_flags, 1) != 1)
        goto fail;

    if (!(header_flags & GZ_FEXTRA)) {
        err = "Not a dictzip file (wrong flags)";
        goto fail;
    }

    /* extra length */
    if (bdrv_pread(s->hd, GZ_XLEN, &tmp_short, 2) != 2)
        goto fail;

    headerLength += le16_to_cpu(tmp_short) + 2;

    /* DictZip magic */
    if (bdrv_pread(s->hd, GZ_SI, &magic, 2) != 2)
        goto fail;

    if (magic[0] != DZ_MAGIC1 || magic[1] != DZ_MAGIC2) {
        err = "Not a dictzip file (missing extra magic)";
        goto fail;
    }

    /* DictZip version */
    if (bdrv_pread(s->hd, GZ_VERSION, &header_ver, 2) != 2)
        goto fail;

    header_ver = le16_to_cpu(header_ver);

    switch (header_ver) {
        case 1: /* Normal DictZip */
            /* number of chunks */
            if (bdrv_pread(s->hd, GZ_CHUNKSIZE, &chunk_len16, 2) != 2)
                goto fail;

            s->chunk_len = le16_to_cpu(chunk_len16);

            /* chunk count */
            if (bdrv_pread(s->hd, GZ_CHUNKCNT, &chunk_cnt16, 2) != 2)
                goto fail;

            s->chunk_cnt = le16_to_cpu(chunk_cnt16);
            chunks_len = sizeof(short) * s->chunk_cnt;
            rnd_offs = GZ_RNDDATA;
            break;
        case 99: /* Special Alex pigz version */
            /* number of chunks */
            if (bdrv_pread(s->hd, GZ_99_CHUNKSIZE, &s->chunk_len, 4) != 4)
                goto fail;

            dprintf("chunk len [%#x] = %d\n", GZ_99_CHUNKSIZE, s->chunk_len);
            s->chunk_len = le32_to_cpu(s->chunk_len);

            /* chunk count */
            if (bdrv_pread(s->hd, GZ_99_CHUNKCNT, &s->chunk_cnt, 4) != 4)
                goto fail;

            s->chunk_cnt = le32_to_cpu(s->chunk_cnt);

            dprintf("chunk len | count = %d | %d\n", s->chunk_len, s->chunk_cnt);

            /* file size */
            if (bdrv_pread(s->hd, GZ_99_FILESIZE, &s->file_len, 8) != 8)
                goto fail;

            s->file_len = le64_to_cpu(s->file_len);
            chunks_len = sizeof(int) * s->chunk_cnt;
            rnd_offs = GZ_99_RNDDATA;
            break;
        default:
            err = "Invalid DictZip version";
            goto fail;
    }

    /* random access data */
    s->chunks = g_malloc(chunks_len);
    if (header_ver == 99)
        s->chunks32 = (uint32_t *)s->chunks;

    if (bdrv_pread(s->hd, rnd_offs, s->chunks, chunks_len) != chunks_len)
        goto fail;

    /* orig filename */
    if (header_flags & GZ_FNAME) {
        if (bdrv_pread(s->hd, headerLength + 1, buf, sizeof(buf)) != sizeof(buf))
            goto fail;

        buf[sizeof(buf) - 1] = '\0';
        headerLength += strlen(buf) + 1;

        if (strlen(buf) == sizeof(buf))
            goto fail;

        dprintf("filename: %s\n", buf);
    }

    /* comment field */
    if (header_flags & GZ_COMMENT) {
        if (bdrv_pread(s->hd, headerLength, buf, sizeof(buf)) != sizeof(buf))
            goto fail;

        buf[sizeof(buf) - 1] = '\0';
        headerLength += strlen(buf) + 1;

        if (strlen(buf) == sizeof(buf))
            goto fail;

        dprintf("comment: %s\n", buf);
    }

    if (header_flags & GZ_FHCRC)
        headerLength += 2;

    /* uncompressed file length*/
    if (!s->file_len) {
        uint32_t file_len;

        if (bdrv_pread(s->hd, bdrv_getlength(s->hd) - 4, &file_len, 4) != 4)
            goto fail;

        s->file_len = le32_to_cpu(file_len);
    }

    /* compute offsets */
    s->offsets = g_malloc(sizeof( *s->offsets ) * s->chunk_cnt);

    for (offset = headerLength + 1, i = 0; i < s->chunk_cnt; i++) {
        s->offsets[i] = offset;
        switch (header_ver) {
        case 1:
            offset += s->chunks[i];
            break;
        case 99:
            offset += s->chunks32[i];
            break;
        }

        dprintf("chunk %#x - %#x = offset %#x -> %#x\n", i * s->chunk_len, (i+1) * s->chunk_len, s->offsets[i], offset);
    }
    qemu_opts_del(opts);

    return 0;

fail:
    fprintf(stderr, "DictZip: Error opening file: %s\n", err);
    bdrv_unref(s->hd);
    if (s->chunks)
        g_free(s->chunks);
    qemu_opts_del(opts);
    return -EINVAL;
}

/* This callback gets invoked when we have the result in cache already */
static void dictzip_cache_cb(void *opaque)
{
    DictZipAIOCB *acb = (DictZipAIOCB *)opaque;

    qemu_iovec_from_buf(acb->qiov, 0, acb->buf, acb->len);
    acb->common.cb(acb->common.opaque, 0);
    qemu_bh_delete(acb->bh);
    qemu_aio_unref(acb);
}

/* This callback gets invoked by the underlying block reader when we have
 * all compressed data. We uncompress in here. */
static void dictzip_read_cb(void *opaque, int ret)
{
    DictZipAIOCB *acb = (DictZipAIOCB *)opaque;
    struct BDRVDictZipState *s = acb->s;
    uint8_t *buf;
    DictCache *cache;
    int r;

    buf = g_malloc(acb->chunks_len);

    /* uncompress the chunk */
    acb->zStream->next_in   = acb->gzipped;
    acb->zStream->avail_in  = acb->gz_len;
    acb->zStream->next_out  = buf;
    acb->zStream->avail_out = acb->chunks_len;

    r = inflate( acb->zStream,  Z_PARTIAL_FLUSH );
    if ( (r != Z_OK) && (r != Z_STREAM_END) )
        fprintf(stderr, "Error inflating: [%d] %s\n", r, acb->zStream->msg);

    if ( r == Z_STREAM_END )
        inflateReset(acb->zStream);

    dprintf("inflating [%d] left: %d | %d bytes\n", r, acb->zStream->avail_in, acb->zStream->avail_out);
    s->stream_in_use &= ~(1 << acb->zStream_id);

    /* nofity the caller */
    qemu_iovec_from_buf(acb->qiov, 0, buf + acb->offset, acb->len);
    acb->common.cb(acb->common.opaque, 0);

    /* fill the cache */
    cache = &s->cache[s->cache_index];
    s->cache_index++;
    if (s->cache_index == CACHE_COUNT)
        s->cache_index = 0;

    cache->len = 0;
    if (cache->buf)
        g_free(cache->buf);
    cache->start = acb->gz_start;
    cache->buf = buf;
    cache->len = acb->chunks_len;

    /* free occupied ressources */
    g_free(acb->qiov_gz);
    qemu_aio_unref(acb);
}

static void dictzip_aio_cancel(BlockAIOCB *blockacb)
{
}

static const AIOCBInfo dictzip_aiocb_info = {
    .aiocb_size         = sizeof(DictZipAIOCB),
    .cancel_async       = dictzip_aio_cancel,
};

/* This is where we get a request from a caller to read something */
static BlockAIOCB *dictzip_aio_readv(BlockDriverState *bs,
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
        BlockCompletionFunc *cb, void *opaque)
{
    BDRVDictZipState *s = bs->opaque;
    DictZipAIOCB *acb;
    QEMUIOVector *qiov_gz;
    struct iovec *iov;
    uint8_t *buf;
    size_t  start = sector_num * SECTOR_SIZE;
    size_t  len = nb_sectors * SECTOR_SIZE;
    size_t  end = start + len;
    size_t  gz_start;
    size_t  gz_len;
    int64_t gz_sector_num;
    int     gz_nb_sectors;
    int     first_chunk, last_chunk;
    int     first_offset;
    int     i;

    acb = qemu_aio_get(&dictzip_aiocb_info, bs, cb, opaque);
    if (!acb)
        return NULL;

    /* Search Cache */
    for (i = 0; i < CACHE_COUNT; i++) {
        if (!s->cache[i].len)
            continue;

        if ((start >= s->cache[i].start) &&
            (end <= (s->cache[i].start + s->cache[i].len))) {
            acb->buf = s->cache[i].buf + (start - s->cache[i].start);
            acb->len = len;
            acb->qiov = qiov;
            acb->bh = qemu_bh_new(dictzip_cache_cb, acb);
            qemu_bh_schedule(acb->bh);

            return &acb->common;
        }
    }

    /* No cache, so let's decode */
    do {
        for (i = 0; i < Z_STREAM_COUNT; i++) {
            if (!(s->stream_in_use & (1 << i))) {
                s->stream_in_use |= (1 << i);
                acb->zStream_id = i;
                acb->zStream = &s->zStream[i];
                break;
            }
        }
    } while(!acb->zStream);

    /* We need to read these chunks */
    first_chunk  = start / s->chunk_len;
    first_offset = start - first_chunk * s->chunk_len;
    last_chunk   = end / s->chunk_len;

    gz_start = s->offsets[first_chunk];
    gz_len = 0;
    for (i = first_chunk; i <= last_chunk; i++) {
        if (s->chunks32)
            gz_len += s->chunks32[i];
        else
            gz_len += s->chunks[i];
    }

    gz_sector_num = gz_start / SECTOR_SIZE;
    gz_nb_sectors = (gz_len / SECTOR_SIZE);

    /* account for tail and heads */
    while ((gz_start + gz_len) > ((gz_sector_num + gz_nb_sectors) * SECTOR_SIZE))
        gz_nb_sectors++;

    /* Allocate qiov, iov and buf in one chunk so we only need to free qiov */
    qiov_gz = g_malloc0(sizeof(QEMUIOVector) + sizeof(struct iovec) +
                           (gz_nb_sectors * SECTOR_SIZE));
    iov = (struct iovec *)(((char *)qiov_gz) + sizeof(QEMUIOVector));
    buf = ((uint8_t *)iov) + sizeof(struct iovec *);

    /* Kick off the read by the backing file, so we can start decompressing */
    iov->iov_base = (void *)buf;
    iov->iov_len = gz_nb_sectors * 512;
    qemu_iovec_init_external(qiov_gz, iov, 1);

    dprintf("read %d - %d => %d - %d\n", start, end, gz_start, gz_start + gz_len);

    acb->s = s;
    acb->qiov = qiov;
    acb->qiov_gz = qiov_gz;
    acb->start = start;
    acb->len = len;
    acb->gzipped = buf + (gz_start % SECTOR_SIZE);
    acb->gz_len = gz_len;
    acb->gz_start = first_chunk * s->chunk_len;
    acb->offset = first_offset;
    acb->chunks_len = (last_chunk - first_chunk + 1) * s->chunk_len;

    return bdrv_aio_readv(s->hd, gz_sector_num, qiov_gz, gz_nb_sectors,
                          dictzip_read_cb, acb);
}

static void dictzip_close(BlockDriverState *bs)
{
    BDRVDictZipState *s = bs->opaque;
    int i;

    for (i = 0; i < CACHE_COUNT; i++) {
        if (!s->cache[i].len)
            continue;

        g_free(s->cache[i].buf);
    }

    for (i = 0; i < Z_STREAM_COUNT; i++) {
        inflateEnd(&s->zStream[i]);
    }

    if (s->chunks)
        g_free(s->chunks);

    if (s->offsets)
        g_free(s->offsets);

    dprintf("Close\n");
}

static int64_t dictzip_getlength(BlockDriverState *bs)
{
    BDRVDictZipState *s = bs->opaque;
    dprintf("getlength -> %ld\n", s->file_len);
    return s->file_len;
}

static BlockDriver bdrv_dictzip = {
    .format_name     = "dzip",
    .protocol_name   = "dzip",

    .instance_size   = sizeof(BDRVDictZipState),
    .bdrv_file_open  = dictzip_open,
    .bdrv_close      = dictzip_close,
    .bdrv_getlength  = dictzip_getlength,
    .bdrv_probe      = dictzip_probe,

    .bdrv_aio_readv  = dictzip_aio_readv,
};

static void dictzip_block_init(void)
{
    bdrv_register(&bdrv_dictzip);
}

block_init(dictzip_block_init);