~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/blktap2/drivers/block-qcow.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* block-qcow.c
 
2
 *
 
3
 * Asynchronous Qemu copy-on-write disk implementation.
 
4
 * Code based on the Qemu implementation
 
5
 * (see copyright notice below)
 
6
 *
 
7
 * (c) 2006 Andrew Warfield and Julian Chesterfield
 
8
 *
 
9
 */
 
10
 
 
11
/*
 
12
 * Block driver for the QCOW format
 
13
 * 
 
14
 * Copyright (c) 2004 Fabrice Bellard
 
15
 * 
 
16
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 
17
 * of this software and associated documentation files(the "Software"), to deal
 
18
 * in the Software without restriction, including without limitation the rights
 
19
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
20
 * copies of the Software, and to permit persons to whom the Software is
 
21
 * furnished to do so, subject to the following conditions:
 
22
 */
 
23
 
 
24
#include <errno.h>
 
25
#include <fcntl.h>
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <unistd.h>
 
29
#include <sys/statvfs.h>
 
30
#include <sys/stat.h>
 
31
#include <sys/ioctl.h>
 
32
#include <string.h>
 
33
#include <zlib.h>
 
34
#include <inttypes.h>
 
35
#include <libaio.h>
 
36
#include <openssl/md5.h>
 
37
#include <limits.h>
 
38
#include "bswap.h"
 
39
#include "aes.h"
 
40
 
 
41
#include "tapdisk.h"
 
42
#include "tapdisk-driver.h"
 
43
#include "tapdisk-interface.h"
 
44
#include "qcow.h"
 
45
#include "blk.h"
 
46
#include "atomicio.h"
 
47
 
 
48
/* *BSD has no O_LARGEFILE */
 
49
#ifndef O_LARGEFILE
 
50
#define O_LARGEFILE     0
 
51
#endif
 
52
 
 
53
#if 1
 
54
#define ASSERT(_p) \
 
55
    if ( !(_p) ) { DPRINTF("Assertion '%s' failed, line %d, file %s", #_p , \
 
56
    __LINE__, __FILE__); *(int*)0=0; }
 
57
#else
 
58
#define ASSERT(_p) ((void)0)
 
59
#endif
 
60
 
 
61
struct pending_aio {
 
62
        td_callback_t cb;
 
63
        int id;
 
64
        void *private;
 
65
        int nb_sectors;
 
66
        char *buf;
 
67
        uint64_t sector;
 
68
};
 
69
 
 
70
#undef IOCB_IDX
 
71
#define IOCB_IDX(_s, _io) ((_io) - (_s)->iocb_list)
 
72
 
 
73
#define ZERO_TEST(_b) (_b | 0x00)
 
74
 
 
75
struct qcow_request {
 
76
        td_request_t         treq;
 
77
        struct tiocb         tiocb;
 
78
        struct tdqcow_state  *state;
 
79
};
 
80
 
 
81
static int decompress_cluster(struct tdqcow_state *s, uint64_t cluster_offset);
 
82
 
 
83
#ifdef USE_GCRYPT
 
84
 
 
85
#include <gcrypt.h>
 
86
 
 
87
uint32_t gen_cksum(char *ptr, int len)
 
88
{
 
89
  int i;
 
90
  uint32_t md[4];
 
91
 
 
92
  /* Generate checksum */
 
93
  gcry_md_hash_buffer(GCRY_MD_MD5, md, ptr, len);
 
94
 
 
95
  return md[0];
 
96
}
 
97
 
 
98
#else /* use libcrypto */
 
99
 
 
100
#include <openssl/md5.h>
 
101
 
 
102
uint32_t gen_cksum(char *ptr, int len)
 
103
{
 
104
  int i;
 
105
  unsigned char *md;
 
106
  uint32_t ret;
 
107
 
 
108
  md = malloc(MD5_DIGEST_LENGTH);
 
109
  if(!md) return 0;
 
110
 
 
111
  /* Generate checksum */
 
112
  if (MD5((unsigned char *)ptr, len, md) != md)
 
113
    ret = 0;
 
114
  else
 
115
    memcpy(&ret, md, sizeof(uint32_t));
 
116
 
 
117
  free(md);
 
118
  return ret;
 
119
}
 
120
 
 
121
#endif
 
122
 
 
123
 
 
124
static void free_aio_state(struct tdqcow_state* s)
 
125
{
 
126
        free(s->aio_requests);
 
127
        free(s->aio_free_list);
 
128
}
 
129
 
 
130
static int init_aio_state(td_driver_t *driver)
 
131
{
 
132
        int i, ret;
 
133
        td_disk_info_t *bs = &(driver->info);
 
134
        struct tdqcow_state   *s  = (struct tdqcow_state *)driver->data;
 
135
        
 
136
        // A segment (i.e. a page) can span multiple clusters
 
137
        s->max_aio_reqs = ((getpagesize() / s->cluster_size) + 1) *
 
138
          MAX_SEGMENTS_PER_REQ * MAX_REQUESTS;
 
139
 
 
140
        s->aio_free_count = s->max_aio_reqs;
 
141
 
 
142
        if (!(s->aio_requests  = calloc(s->max_aio_reqs, sizeof(struct qcow_request))) || 
 
143
            !(s->aio_free_list = calloc(s->max_aio_reqs, sizeof(struct qcow_request)))) {
 
144
            DPRINTF("Failed to allocate AIO structs (max_aio_reqs = %d)\n",
 
145
                    s->max_aio_reqs);
 
146
            goto fail;
 
147
        }
 
148
 
 
149
        for (i = 0; i < s->max_aio_reqs; i++)
 
150
                s->aio_free_list[i] = &s->aio_requests[i];
 
151
 
 
152
        DPRINTF("AIO state initialised\n");
 
153
 
 
154
        return 0;
 
155
 fail:
 
156
        return -1;
 
157
}
 
158
 
 
159
int get_filesize(char *filename, uint64_t *size, struct stat *st)
 
160
{
 
161
        int fd;
 
162
        QCowHeader header;
 
163
 
 
164
        /*Set to the backing file size*/
 
165
        fd = open(filename, O_RDONLY);
 
166
        if (fd < 0)
 
167
                return -1;
 
168
        if (read(fd, &header, sizeof(header)) < sizeof(header)) {
 
169
                close(fd);
 
170
                return -1;
 
171
        }
 
172
        close(fd);
 
173
        
 
174
        be32_to_cpus(&header.magic);
 
175
        be64_to_cpus(&header.size);
 
176
        if (header.magic == QCOW_MAGIC) {
 
177
                *size = header.size >> SECTOR_SHIFT;
 
178
                return 0;
 
179
        }
 
180
 
 
181
        if(S_ISBLK(st->st_mode)) {
 
182
                fd = open(filename, O_RDONLY);
 
183
                if (fd < 0)
 
184
                        return -1;
 
185
                if (blk_getimagesize(fd, size) != 0) {
 
186
                        printf("Unable to get Block device size\n");
 
187
                        close(fd);
 
188
                        return -1;
 
189
                }
 
190
                close(fd);
 
191
        } else *size = (st->st_size >> SECTOR_SHIFT);   
 
192
        return 0;
 
193
}
 
194
 
 
195
static int qcow_set_key(struct tdqcow_state *s, const char *key)
 
196
{
 
197
        uint8_t keybuf[16];
 
198
        int len, i;
 
199
        
 
200
        memset(keybuf, 0, 16);
 
201
        len = strlen(key);
 
202
        if (len > 16)
 
203
                len = 16;
 
204
        /* XXX: we could compress the chars to 7 bits to increase
 
205
           entropy */
 
206
        for (i = 0; i < len; i++) {
 
207
                keybuf[i] = key[i];
 
208
        }
 
209
        s->crypt_method = s->crypt_method_header;
 
210
        
 
211
        if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
 
212
                return -1;
 
213
        if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
 
214
                return -1;
 
215
#if 0
 
216
        /* test */
 
217
        {
 
218
                uint8_t in[16];
 
219
                uint8_t out[16];
 
220
                uint8_t tmp[16];
 
221
                for (i=0; i<16; i++)
 
222
                        in[i] = i;
 
223
                AES_encrypt(in, tmp, &s->aes_encrypt_key);
 
224
                AES_decrypt(tmp, out, &s->aes_decrypt_key);
 
225
                for (i = 0; i < 16; i++)
 
226
                        DPRINTF(" %02x", tmp[i]);
 
227
                DPRINTF("\n");
 
228
                for (i = 0; i < 16; i++)
 
229
                        DPRINTF(" %02x", out[i]);
 
230
                DPRINTF("\n");
 
231
        }
 
232
#endif
 
233
        return 0;
 
234
}
 
235
 
 
236
void tdqcow_complete(void *arg, struct tiocb *tiocb, int err)
 
237
{
 
238
        struct qcow_request *aio = (struct qcow_request *)arg;
 
239
        struct tdqcow_state *s = aio->state;
 
240
 
 
241
        td_complete_request(aio->treq, err);
 
242
 
 
243
        s->aio_free_list[s->aio_free_count++] = aio;
 
244
}
 
245
 
 
246
static void async_read(td_driver_t *driver, td_request_t treq)
 
247
{
 
248
        int size;
 
249
        uint64_t offset;
 
250
        struct qcow_request *aio;
 
251
        struct tdqcow_state *prv;
 
252
 
 
253
        prv    = (struct tdqcow_state *)driver->data;
 
254
        size   = treq.secs * driver->info.sector_size;
 
255
        offset = treq.sec  * (uint64_t)driver->info.sector_size;
 
256
 
 
257
        if (prv->aio_free_count == 0)
 
258
                goto fail;
 
259
 
 
260
        aio        = prv->aio_free_list[--prv->aio_free_count];
 
261
        aio->treq  = treq;
 
262
        aio->state = prv;
 
263
 
 
264
        td_prep_read(&aio->tiocb, prv->fd, treq.buf,
 
265
                     size, offset, tdqcow_complete, aio);
 
266
        td_queue_tiocb(driver, &aio->tiocb);
 
267
 
 
268
        return;
 
269
 
 
270
fail:
 
271
        td_complete_request(treq, -EBUSY);
 
272
}
 
273
 
 
274
static void async_write(td_driver_t *driver, td_request_t treq)
 
275
{
 
276
        int size;
 
277
        uint64_t offset;
 
278
        struct qcow_request *aio;
 
279
        struct tdqcow_state *prv;
 
280
 
 
281
        prv     = (struct tdqcow_state *)driver->data;
 
282
        size    = treq.secs * driver->info.sector_size;
 
283
        offset  = treq.sec  * (uint64_t)driver->info.sector_size;
 
284
 
 
285
        if (prv->aio_free_count == 0)
 
286
                goto fail;
 
287
 
 
288
        aio        = prv->aio_free_list[--prv->aio_free_count];
 
289
        aio->treq  = treq;
 
290
        aio->state = prv;
 
291
 
 
292
        td_prep_write(&aio->tiocb, prv->fd, treq.buf,
 
293
                      size, offset, tdqcow_complete, aio);
 
294
        td_queue_tiocb(driver, &aio->tiocb);
 
295
 
 
296
        return;
 
297
 
 
298
fail:
 
299
        td_complete_request(treq, -EBUSY);
 
300
}
 
301
 
 
302
/* 
 
303
 * The crypt function is compatible with the linux cryptoloop
 
304
 * algorithm for < 4 GB images. NOTE: out_buf == in_buf is
 
305
 * supported .
 
306
 */
 
307
static void encrypt_sectors(struct tdqcow_state *s, int64_t sector_num,
 
308
                            uint8_t *out_buf, const uint8_t *in_buf,
 
309
                            int nb_sectors, int enc,
 
310
                            const AES_KEY *key)
 
311
{
 
312
        union {
 
313
                uint64_t ll[2];
 
314
                uint8_t b[16];
 
315
        } ivec;
 
316
        int i;
 
317
        
 
318
        for (i = 0; i < nb_sectors; i++) {
 
319
                ivec.ll[0] = cpu_to_le64(sector_num);
 
320
                ivec.ll[1] = 0;
 
321
                AES_cbc_encrypt(in_buf, out_buf, 512, key, 
 
322
                                ivec.b, enc);
 
323
                sector_num++;
 
324
                in_buf += 512;
 
325
                out_buf += 512;
 
326
        }
 
327
}
 
328
 
 
329
int qtruncate(int fd, off_t length, int sparse)
 
330
{
 
331
        int ret, i; 
 
332
        int current = 0, rem = 0;
 
333
        uint64_t sectors;
 
334
        struct stat st;
 
335
        char *buf;
 
336
 
 
337
        /* If length is greater than the current file len
 
338
         * we synchronously write zeroes to the end of the 
 
339
         * file, otherwise we truncate the length down
 
340
         */
 
341
        ret = fstat(fd, &st);
 
342
        if (ret == -1) 
 
343
                return -1;
 
344
        if (S_ISBLK(st.st_mode))
 
345
                return 0;
 
346
 
 
347
        sectors = (length + DEFAULT_SECTOR_SIZE - 1)/DEFAULT_SECTOR_SIZE;
 
348
        current = (st.st_size + DEFAULT_SECTOR_SIZE - 1)/DEFAULT_SECTOR_SIZE;
 
349
        rem     = st.st_size % DEFAULT_SECTOR_SIZE;
 
350
 
 
351
        /* If we are extending this file, we write zeros to the end --
 
352
         * this tries to ensure that the extents allocated wind up being
 
353
         * contiguous on disk.
 
354
         */
 
355
        if(st.st_size < sectors * DEFAULT_SECTOR_SIZE) {
 
356
                /*We are extending the file*/
 
357
                if ((ret = posix_memalign((void **)&buf, 
 
358
                                          512, DEFAULT_SECTOR_SIZE))) {
 
359
                        DPRINTF("posix_memalign failed: %d\n", ret);
 
360
                        return -1;
 
361
                }
 
362
                memset(buf, 0x00, DEFAULT_SECTOR_SIZE);
 
363
                if (lseek(fd, 0, SEEK_END)==-1) {
 
364
                        DPRINTF("Lseek EOF failed (%d), internal error\n",
 
365
                                errno);
 
366
                        free(buf);
 
367
                        return -1;
 
368
                }
 
369
                if (rem) {
 
370
                        ret = write(fd, buf, rem);
 
371
                        if (ret != rem) {
 
372
                                DPRINTF("write failed: ret = %d, err = %s\n",
 
373
                                        ret, strerror(errno));
 
374
                                free(buf);
 
375
                                return -1;
 
376
                        }
 
377
                }
 
378
                for (i = current; i < sectors; i++ ) {
 
379
                        ret = write(fd, buf, DEFAULT_SECTOR_SIZE);
 
380
                        if (ret != DEFAULT_SECTOR_SIZE) {
 
381
                                DPRINTF("write failed: ret = %d, err = %s\n",
 
382
                                        ret, strerror(errno));
 
383
                                free(buf);
 
384
                                return -1;
 
385
                        }
 
386
                }
 
387
                free(buf);
 
388
        } else if(sparse && (st.st_size > sectors * DEFAULT_SECTOR_SIZE))
 
389
                if (ftruncate(fd, (off_t)sectors * DEFAULT_SECTOR_SIZE)==-1) {
 
390
                        DPRINTF("Ftruncate failed (%s)\n", strerror(errno));
 
391
                        return -1;
 
392
                }
 
393
        return 0;
 
394
}
 
395
 
 
396
/* 'allocate' is:
 
397
 *
 
398
 * 0 to not allocate.
 
399
 *
 
400
 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
 
401
 * 'n_end')
 
402
 *
 
403
 * 2 to allocate a compressed cluster of size
 
404
 * 'compressed_size'. 'compressed_size' must be > 0 and <
 
405
 * cluster_size 
 
406
 *
 
407
 * return 0 if not allocated.
 
408
 */
 
409
static uint64_t get_cluster_offset(struct tdqcow_state *s,
 
410
                                   uint64_t offset, int allocate,
 
411
                                   int compressed_size,
 
412
                                   int n_start, int n_end)
 
413
{
 
414
        int min_index, i, j, l1_index, l2_index, l2_sector, l1_sector;
 
415
        char *tmp_ptr2, *l2_ptr, *l1_ptr;
 
416
        uint64_t *tmp_ptr;
 
417
        uint64_t l2_offset, *l2_table, cluster_offset, tmp;
 
418
        uint32_t min_count;
 
419
        int new_l2_table;
 
420
 
 
421
        /*Check L1 table for the extent offset*/
 
422
        l1_index = offset >> (s->l2_bits + s->cluster_bits);
 
423
        l2_offset = s->l1_table[l1_index];
 
424
        new_l2_table = 0;
 
425
        if (!l2_offset) {
 
426
                if (!allocate)
 
427
                        return 0;
 
428
                /* 
 
429
                 * allocating a new l2 entry + extent 
 
430
                 * at the end of the file, we must also
 
431
                 * update the L1 entry safely.
 
432
                 */
 
433
                l2_offset = s->fd_end;
 
434
 
 
435
                /* round to cluster size */
 
436
                l2_offset = (l2_offset + s->cluster_size - 1) 
 
437
                        & ~(s->cluster_size - 1);
 
438
 
 
439
                /* update the L1 entry */
 
440
                s->l1_table[l1_index] = l2_offset;
 
441
                
 
442
                /*Truncate file for L2 table 
 
443
                 *(initialised to zero in case we crash)*/
 
444
                if (qtruncate(s->fd, 
 
445
                              l2_offset + (s->l2_size * sizeof(uint64_t)),
 
446
                              s->sparse) != 0) {
 
447
                        DPRINTF("ERROR truncating file\n");
 
448
                        return 0;
 
449
                }
 
450
                s->fd_end = l2_offset + (s->l2_size * sizeof(uint64_t));
 
451
 
 
452
                /*Update the L1 table entry on disk
 
453
                 * (for O_DIRECT we write 4KByte blocks)*/
 
454
                l1_sector = (l1_index * sizeof(uint64_t)) >> 12;
 
455
                l1_ptr = (char *)s->l1_table + (l1_sector << 12);
 
456
 
 
457
                if (posix_memalign((void **)&tmp_ptr, 4096, 4096) != 0) {
 
458
                        DPRINTF("ERROR allocating memory for L1 table\n");
 
459
                }
 
460
                memcpy(tmp_ptr, l1_ptr, 4096);
 
461
 
 
462
                /* Convert block to write to big endian */
 
463
                for(i = 0; i < 4096 / sizeof(uint64_t); i++) {
 
464
                        cpu_to_be64s(&tmp_ptr[i]);
 
465
                }
 
466
 
 
467
                /*
 
468
                 * Issue non-asynchronous L1 write.
 
469
                 * For safety, we must ensure that
 
470
                 * entry is written before blocks.
 
471
                 */
 
472
                lseek(s->fd, s->l1_table_offset + (l1_sector << 12), SEEK_SET);
 
473
                if (write(s->fd, tmp_ptr, 4096) != 4096) {
 
474
                        free(tmp_ptr);
 
475
                        return 0;
 
476
                }
 
477
                free(tmp_ptr);
 
478
 
 
479
                new_l2_table = 1;
 
480
                goto cache_miss;
 
481
        } else if (s->min_cluster_alloc == s->l2_size) {
 
482
                /*Fast-track the request*/
 
483
                cluster_offset = l2_offset + (s->l2_size * sizeof(uint64_t));
 
484
                l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
 
485
                return cluster_offset + (l2_index * s->cluster_size);
 
486
        }
 
487
 
 
488
        /*Check to see if L2 entry is already cached*/
 
489
        for (i = 0; i < L2_CACHE_SIZE; i++) {
 
490
                if (l2_offset == s->l2_cache_offsets[i]) {
 
491
                        /* increment the hit count */
 
492
                        if (++s->l2_cache_counts[i] == 0xffffffff) {
 
493
                                for (j = 0; j < L2_CACHE_SIZE; j++) {
 
494
                                        s->l2_cache_counts[j] >>= 1;
 
495
                                }
 
496
                        }
 
497
                        l2_table = s->l2_cache + (i << s->l2_bits);
 
498
                        goto found;
 
499
                }
 
500
        }
 
501
 
 
502
cache_miss:
 
503
        /* not found: load a new entry in the least used one */
 
504
        min_index = 0;
 
505
        min_count = 0xffffffff;
 
506
        for (i = 0; i < L2_CACHE_SIZE; i++) {
 
507
                if (s->l2_cache_counts[i] < min_count) {
 
508
                        min_count = s->l2_cache_counts[i];
 
509
                        min_index = i;
 
510
                }
 
511
        }
 
512
        l2_table = s->l2_cache + (min_index << s->l2_bits);
 
513
 
 
514
        /*If extent pre-allocated, read table from disk, 
 
515
         *otherwise write new table to disk*/
 
516
        if (new_l2_table) {
 
517
                /*Should we allocate the whole extent? Adjustable parameter.*/
 
518
                if (s->cluster_alloc == s->l2_size) {
 
519
                        cluster_offset = l2_offset + 
 
520
                                (s->l2_size * sizeof(uint64_t));
 
521
                        cluster_offset = (cluster_offset + s->cluster_size - 1)
 
522
                                & ~(s->cluster_size - 1);
 
523
                        if (qtruncate(s->fd, cluster_offset + 
 
524
                                  (s->cluster_size * s->l2_size), 
 
525
                                      s->sparse) != 0) {
 
526
                                DPRINTF("ERROR truncating file\n");
 
527
                                return 0;
 
528
                        }
 
529
                        s->fd_end = cluster_offset + 
 
530
                                (s->cluster_size * s->l2_size);
 
531
                        for (i = 0; i < s->l2_size; i++) {
 
532
                                l2_table[i] = cpu_to_be64(cluster_offset + 
 
533
                                                          (i*s->cluster_size));
 
534
                        }  
 
535
                } else memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
 
536
 
 
537
                lseek(s->fd, l2_offset, SEEK_SET);
 
538
                if (write(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) !=
 
539
                   s->l2_size * sizeof(uint64_t))
 
540
                        return 0;
 
541
        } else {
 
542
                lseek(s->fd, l2_offset, SEEK_SET);
 
543
                if (read(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) != 
 
544
                    s->l2_size * sizeof(uint64_t))
 
545
                        return 0;
 
546
        }
 
547
        
 
548
        /*Update the cache entries*/ 
 
549
        s->l2_cache_offsets[min_index] = l2_offset;
 
550
        s->l2_cache_counts[min_index] = 1;
 
551
 
 
552
found:
 
553
        /*The extent is split into 's->l2_size' blocks of 
 
554
         *size 's->cluster_size'*/
 
555
        l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
 
556
        cluster_offset = be64_to_cpu(l2_table[l2_index]);
 
557
 
 
558
        if (!cluster_offset || 
 
559
            ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1) ) {
 
560
                if (!allocate)
 
561
                        return 0;
 
562
                
 
563
                if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
 
564
                    (n_end - n_start) < s->cluster_sectors) {
 
565
                        /* cluster is already allocated but compressed, we must
 
566
                           decompress it in the case it is not completely
 
567
                           overwritten */
 
568
                        if (decompress_cluster(s, cluster_offset) < 0)
 
569
                                return 0;
 
570
                        cluster_offset = lseek(s->fd, s->fd_end, SEEK_SET);
 
571
                        cluster_offset = (cluster_offset + s->cluster_size - 1)
 
572
                                & ~(s->cluster_size - 1);
 
573
                        /* write the cluster content - not asynchronous */
 
574
                        lseek(s->fd, cluster_offset, SEEK_SET);
 
575
                        if (write(s->fd, s->cluster_cache, s->cluster_size) != 
 
576
                            s->cluster_size)
 
577
                            return -1;
 
578
                } else {
 
579
                        /* allocate a new cluster */
 
580
                        cluster_offset = lseek(s->fd, s->fd_end, SEEK_SET);
 
581
                        if (allocate == 1) {
 
582
                                /* round to cluster size */
 
583
                                cluster_offset = 
 
584
                                        (cluster_offset + s->cluster_size - 1) 
 
585
                                        & ~(s->cluster_size - 1);
 
586
                                if (qtruncate(s->fd, cluster_offset + 
 
587
                                              s->cluster_size, s->sparse)!=0) {
 
588
                                        DPRINTF("ERROR truncating file\n");
 
589
                                        return 0;
 
590
                                }
 
591
                                s->fd_end = (cluster_offset + s->cluster_size);
 
592
                                /* if encrypted, we must initialize the cluster
 
593
                                   content which won't be written */
 
594
                                if (s->crypt_method && 
 
595
                                    (n_end - n_start) < s->cluster_sectors) {
 
596
                                        uint64_t start_sect;
 
597
                                        start_sect = (offset & 
 
598
                                                      ~(s->cluster_size - 1)) 
 
599
                                                              >> 9;
 
600
                                        memset(s->cluster_data + 512, 
 
601
                                               0xaa, 512);
 
602
                                        for (i = 0; i < s->cluster_sectors;i++)
 
603
                                        {
 
604
                                                if (i < n_start || i >= n_end) 
 
605
                                                {
 
606
                                                        encrypt_sectors(s, start_sect + i, 
 
607
                                                                        s->cluster_data, 
 
608
                                                                        s->cluster_data + 512, 1, 1,
 
609
                                                                        &s->aes_encrypt_key);
 
610
                                                        lseek(s->fd, cluster_offset + i * 512, SEEK_SET);
 
611
                                                        if (write(s->fd, s->cluster_data, 512) != 512)
 
612
                                                                return -1;
 
613
                                                }
 
614
                                        }
 
615
                                }
 
616
                        } else {
 
617
                                cluster_offset |= QCOW_OFLAG_COMPRESSED | 
 
618
                                        (uint64_t)compressed_size 
 
619
                                                << (63 - s->cluster_bits);
 
620
                        }
 
621
                }
 
622
                /* update L2 table */
 
623
                tmp = cpu_to_be64(cluster_offset);
 
624
                l2_table[l2_index] = tmp;
 
625
 
 
626
                /*For IO_DIRECT we write 4KByte blocks*/
 
627
                l2_sector = (l2_index * sizeof(uint64_t)) >> 12;
 
628
                l2_ptr = (char *)l2_table + (l2_sector << 12);
 
629
                
 
630
                if (posix_memalign((void **)&tmp_ptr2, 4096, 4096) != 0) {
 
631
                        DPRINTF("ERROR allocating memory for L1 table\n");
 
632
                }
 
633
                memcpy(tmp_ptr2, l2_ptr, 4096);
 
634
                lseek(s->fd, l2_offset + (l2_sector << 12), SEEK_SET);
 
635
                if (write(s->fd, tmp_ptr2, 4096) != 4096) {
 
636
                        free(tmp_ptr2);
 
637
                        return -1;
 
638
                }
 
639
                free(tmp_ptr2);
 
640
        }
 
641
        return cluster_offset;
 
642
}
 
643
 
 
644
static int qcow_is_allocated(struct tdqcow_state *s, int64_t sector_num,
 
645
                             int nb_sectors, int *pnum)
 
646
{
 
647
        int index_in_cluster, n;
 
648
        uint64_t cluster_offset;
 
649
 
 
650
        cluster_offset = get_cluster_offset(s, sector_num << 9, 0, 0, 0, 0);
 
651
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
 
652
        n = s->cluster_sectors - index_in_cluster;
 
653
        if (n > nb_sectors)
 
654
                n = nb_sectors;
 
655
        *pnum = n;
 
656
        return (cluster_offset != 0);
 
657
}
 
658
 
 
659
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
 
660
                             const uint8_t *buf, int buf_size)
 
661
{
 
662
        z_stream strm1, *strm = &strm1;
 
663
        int ret, out_len;
 
664
        
 
665
        memset(strm, 0, sizeof(*strm));
 
666
        
 
667
        strm->next_in = (uint8_t *)buf;
 
668
        strm->avail_in = buf_size;
 
669
        strm->next_out = out_buf;
 
670
        strm->avail_out = out_buf_size;
 
671
        
 
672
        ret = inflateInit2(strm, -12);
 
673
        if (ret != Z_OK)
 
674
                return -1;
 
675
        ret = inflate(strm, Z_FINISH);
 
676
        out_len = strm->next_out - out_buf;
 
677
        if ( (ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
 
678
            (out_len != out_buf_size) ) {
 
679
                inflateEnd(strm);
 
680
                return -1;
 
681
        }
 
682
        inflateEnd(strm);
 
683
        return 0;
 
684
}
 
685
                              
 
686
static int decompress_cluster(struct tdqcow_state *s, uint64_t cluster_offset)
 
687
{
 
688
        int ret, csize;
 
689
        uint64_t coffset;
 
690
 
 
691
        coffset = cluster_offset & s->cluster_offset_mask;
 
692
        if (s->cluster_cache_offset != coffset) {
 
693
                csize = cluster_offset >> (63 - s->cluster_bits);
 
694
                csize &= (s->cluster_size - 1);
 
695
                lseek(s->fd, coffset, SEEK_SET);
 
696
                ret = read(s->fd, s->cluster_data, csize);
 
697
                if (ret != csize) 
 
698
                        return -1;
 
699
                if (decompress_buffer(s->cluster_cache, s->cluster_size,
 
700
                                      s->cluster_data, csize) < 0) {
 
701
                        return -1;
 
702
                }
 
703
                s->cluster_cache_offset = coffset;
 
704
        }
 
705
        return 0;
 
706
}
 
707
 
 
708
static int
 
709
tdqcow_read_header(int fd, QCowHeader *header)
 
710
{
 
711
        int err;
 
712
        char *buf;
 
713
        struct stat st;
 
714
        size_t size, expected;
 
715
 
 
716
        memset(header, 0, sizeof(*header));
 
717
 
 
718
        err = fstat(fd, &st);
 
719
        if (err)
 
720
                return -errno;
 
721
 
 
722
        err = lseek(fd, 0, SEEK_SET);
 
723
        if (err == (off_t)-1)
 
724
                return -errno;
 
725
 
 
726
        size = (sizeof(*header) + 511) & ~511;
 
727
        err = posix_memalign((void **)&buf, 512, size);
 
728
        if (err)
 
729
                return err;
 
730
 
 
731
        expected = size;
 
732
        if (st.st_size < size)
 
733
                expected = st.st_size;
 
734
 
 
735
        errno = 0;
 
736
        err = read(fd, buf, size);
 
737
        if (err != expected) {
 
738
                err = (errno ? -errno : -EIO);
 
739
                goto out;
 
740
        }
 
741
 
 
742
        memcpy(header, buf, sizeof(*header));
 
743
        be32_to_cpus(&header->magic);
 
744
        be32_to_cpus(&header->version);
 
745
        be64_to_cpus(&header->backing_file_offset);
 
746
        be32_to_cpus(&header->backing_file_size);
 
747
        be32_to_cpus(&header->mtime);
 
748
        be64_to_cpus(&header->size);
 
749
        be32_to_cpus(&header->crypt_method);
 
750
        be64_to_cpus(&header->l1_table_offset);
 
751
 
 
752
        err = 0;
 
753
 
 
754
out:
 
755
        free(buf);
 
756
        return err;
 
757
}
 
758
 
 
759
static int
 
760
tdqcow_load_l1_table(struct tdqcow_state *s, QCowHeader *header)
 
761
{
 
762
        char *buf;
 
763
        struct stat st;
 
764
        size_t expected;
 
765
        int i, err, shift;
 
766
        QCowHeader_ext *exthdr;
 
767
        uint32_t l1_table_bytes, l1_table_block, l1_table_size;
 
768
 
 
769
        buf         = NULL;
 
770
        s->l1_table = NULL;
 
771
 
 
772
        shift = s->cluster_bits + s->l2_bits;
 
773
 
 
774
        s->l1_size = (header->size + (1LL << shift) - 1) >> shift;
 
775
        s->l1_table_offset = header->l1_table_offset;
 
776
 
 
777
        s->min_cluster_alloc = 1; /* default */
 
778
 
 
779
        l1_table_bytes = s->l1_size * sizeof(uint64_t);
 
780
        l1_table_size  = (l1_table_bytes + 4095) & ~4095;
 
781
        l1_table_block = (l1_table_bytes + s->l1_table_offset + 4095) & ~4095;
 
782
 
 
783
        DPRINTF("L1 Table offset detected: %"PRIu64", size %d (%d)\n",
 
784
                (uint64_t)s->l1_table_offset,
 
785
                (int) (s->l1_size * sizeof(uint64_t)), 
 
786
                l1_table_size);
 
787
 
 
788
        err = fstat(s->fd, &st);
 
789
        if (err) {
 
790
                err = -errno;
 
791
                goto out;
 
792
        }
 
793
 
 
794
        err = lseek(s->fd, 0, SEEK_SET);
 
795
        if (err == (off_t)-1) {
 
796
                err = -errno;
 
797
                goto out;
 
798
        }
 
799
 
 
800
        err = posix_memalign((void **)&buf, 512, l1_table_block);
 
801
        if (err) {
 
802
                buf = NULL;
 
803
                goto out;
 
804
        }
 
805
 
 
806
        err = posix_memalign((void **)&s->l1_table, 4096, l1_table_size);
 
807
        if (err) {
 
808
                s->l1_table = NULL;
 
809
                goto out;
 
810
        }
 
811
 
 
812
        memset(buf, 0, l1_table_block);
 
813
        memset(s->l1_table, 0, l1_table_size);
 
814
 
 
815
        expected = l1_table_block;
 
816
        if (st.st_size < l1_table_block)
 
817
                expected = st.st_size;
 
818
 
 
819
        errno = 0;
 
820
        err = read(s->fd, buf, l1_table_block);
 
821
        if (err != expected) {
 
822
                err = (errno ? -errno : -EIO);
 
823
                goto out;
 
824
        }
 
825
 
 
826
        memcpy(s->l1_table, buf + s->l1_table_offset, l1_table_size);
 
827
        exthdr = (QCowHeader_ext *)(buf + sizeof(QCowHeader));
 
828
 
 
829
        /* check for xen extended header */
 
830
        if (s->l1_table_offset % 4096 == 0 &&
 
831
            be32_to_cpu(exthdr->xmagic) == XEN_MAGIC) {
 
832
                uint32_t flags = be32_to_cpu(exthdr->flags);
 
833
                uint32_t cksum = be32_to_cpu(exthdr->cksum);
 
834
 
 
835
                /*
 
836
                 * Try to detect old tapdisk images. They have to be fixed
 
837
                 * because they use big endian rather than native endian for
 
838
                 * the L1 table.  After this block, the l1 table will
 
839
                 * definitely be in BIG endian.
 
840
                 */
 
841
                if (!(flags & EXTHDR_L1_BIG_ENDIAN)) {
 
842
                        DPRINTF("qcow: converting to big endian L1 table\n");
 
843
 
 
844
                        /* convert to big endian */
 
845
                        for (i = 0; i < s->l1_size; i++)
 
846
                                cpu_to_be64s(&s->l1_table[i]);
 
847
 
 
848
                        flags |= EXTHDR_L1_BIG_ENDIAN;
 
849
                        exthdr->flags = cpu_to_be32(flags);
 
850
 
 
851
                        memcpy(buf + s->l1_table_offset,
 
852
                               s->l1_table, l1_table_size);
 
853
                        
 
854
                        err = lseek(s->fd, 0, SEEK_SET);
 
855
                        if (err == (off_t)-1) {
 
856
                                err = -errno;
 
857
                                goto out;
 
858
                        }
 
859
 
 
860
                        err = atomicio(vwrite, s->fd, buf, l1_table_block);
 
861
                        if (err != l1_table_block) {
 
862
                                err = -errno;
 
863
                                goto out;
 
864
                        }
 
865
                }
 
866
 
 
867
                /* check the L1 table checksum */
 
868
                if (cksum != gen_cksum((char *)s->l1_table,
 
869
                                       s->l1_size * sizeof(uint64_t)))
 
870
                        DPRINTF("qcow: bad L1 checksum\n");
 
871
                else {
 
872
                        s->extended = 1;
 
873
                        s->sparse = (be32_to_cpu(exthdr->flags) & SPARSE_FILE);
 
874
                        s->min_cluster_alloc =
 
875
                                be32_to_cpu(exthdr->min_cluster_alloc);
 
876
                }
 
877
        }
 
878
 
 
879
        /* convert L1 table to native endian for operation */
 
880
        for (i = 0; i < s->l1_size; i++)
 
881
                be64_to_cpus(&s->l1_table[i]);
 
882
 
 
883
        err = 0;
 
884
 
 
885
out:
 
886
        if (err) {
 
887
                free(buf);
 
888
                free(s->l1_table);
 
889
                s->l1_table = NULL;
 
890
        }
 
891
        return err;
 
892
}
 
893
 
 
894
/* Open the disk file and initialize qcow state. */
 
895
int tdqcow_open (td_driver_t *driver, const char *name, td_flag_t flags)
 
896
{
 
897
        int fd, len, i, ret, size, o_flags;
 
898
        td_disk_info_t *bs = &(driver->info);
 
899
        struct tdqcow_state   *s  = (struct tdqcow_state *)driver->data;
 
900
        QCowHeader header;
 
901
        uint64_t final_cluster = 0;
 
902
 
 
903
        DPRINTF("QCOW: Opening %s\n", name);
 
904
 
 
905
        o_flags = O_DIRECT | O_LARGEFILE | 
 
906
                ((flags == TD_OPEN_RDONLY) ? O_RDONLY : O_RDWR);
 
907
        fd = open(name, o_flags);
 
908
        if (fd < 0) {
 
909
                DPRINTF("Unable to open %s (%d)\n", name, -errno);
 
910
                return -1;
 
911
        }
 
912
 
 
913
        s->fd = fd;
 
914
        s->name = strdup(name);
 
915
        if (!s->name)
 
916
                goto fail;
 
917
 
 
918
        if (tdqcow_read_header(fd, &header))
 
919
                goto fail;
 
920
 
 
921
        if (header.magic != QCOW_MAGIC)
 
922
                goto fail;
 
923
 
 
924
        switch (header.version) {
 
925
        case QCOW_VERSION:
 
926
                break;
 
927
        case 2:
 
928
          //TODO: Port qcow2 to new blktap framework.
 
929
          //            close(fd);
 
930
          //            dd->drv = &tapdisk_qcow2;
 
931
          //            return dd->drv->td_open(dd, name, flags);
 
932
          goto fail;
 
933
        default:
 
934
                goto fail;
 
935
        }
 
936
 
 
937
        if (header.size <= 1 || header.cluster_bits < 9)
 
938
                goto fail;
 
939
        if (header.crypt_method > QCOW_CRYPT_AES)
 
940
                goto fail;
 
941
        s->crypt_method_header = header.crypt_method;
 
942
        if (s->crypt_method_header)
 
943
                s->encrypted = 1;
 
944
        s->cluster_bits = header.cluster_bits;
 
945
        s->cluster_size = 1 << s->cluster_bits;
 
946
        s->cluster_sectors = 1 << (s->cluster_bits - 9);
 
947
        s->l2_bits = header.l2_bits;
 
948
        s->l2_size = 1 << s->l2_bits;
 
949
        s->cluster_alloc = s->l2_size;
 
950
        bs->size = header.size / 512;
 
951
        s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
 
952
        s->backing_file_offset = header.backing_file_offset;
 
953
        s->backing_file_size   = header.backing_file_size;
 
954
 
 
955
        /* allocate and load l1 table */
 
956
        if (tdqcow_load_l1_table(s, &header))
 
957
                goto fail;
 
958
 
 
959
        /* alloc L2 cache */
 
960
        size = s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t);
 
961
        ret = posix_memalign((void **)&s->l2_cache, 4096, size);
 
962
        if(ret != 0) goto fail;
 
963
 
 
964
        size = s->cluster_size;
 
965
        ret = posix_memalign((void **)&s->cluster_cache, 4096, size);
 
966
        if(ret != 0) goto fail;
 
967
 
 
968
        ret = posix_memalign((void **)&s->cluster_data, 4096, size);
 
969
        if(ret != 0) goto fail;
 
970
        s->cluster_cache_offset = -1;
 
971
 
 
972
        if (s->backing_file_offset != 0)
 
973
                s->cluster_alloc = 1; /*Cannot use pre-alloc*/
 
974
 
 
975
        bs->sector_size = 512;
 
976
        bs->info = 0;
 
977
 
 
978
        for(i = 0; i < s->l1_size; i++)
 
979
                if (s->l1_table[i] > final_cluster)
 
980
                        final_cluster = s->l1_table[i];
 
981
 
 
982
        if (init_aio_state(driver)!=0) {
 
983
          DPRINTF("Unable to initialise AIO state\n");
 
984
          free_aio_state(s);
 
985
          goto fail;
 
986
        }
 
987
 
 
988
        if (!final_cluster)
 
989
                s->fd_end = s->l1_table_offset +
 
990
                        ((s->l1_size * sizeof(uint64_t) + 4095) & ~4095);
 
991
        else {
 
992
                s->fd_end = lseek(fd, 0, SEEK_END);
 
993
                if (s->fd_end == (off_t)-1)
 
994
                        goto fail;
 
995
        }
 
996
 
 
997
        return 0;
 
998
        
 
999
fail:
 
1000
        DPRINTF("QCOW Open failed\n");
 
1001
 
 
1002
        free_aio_state(s);
 
1003
        free(s->l1_table);
 
1004
        free(s->l2_cache);
 
1005
        free(s->cluster_cache);
 
1006
        free(s->cluster_data);
 
1007
        close(fd);
 
1008
        return -1;
 
1009
}
 
1010
 
 
1011
void tdqcow_queue_read(td_driver_t *driver, td_request_t treq)
 
1012
{
 
1013
        struct tdqcow_state   *s  = (struct tdqcow_state *)driver->data;
 
1014
        int ret = 0, index_in_cluster, n, i;
 
1015
        uint64_t cluster_offset, sector, nb_sectors;
 
1016
        struct qcow_prv* prv;
 
1017
        td_request_t clone = treq;
 
1018
        char* buf = treq.buf;
 
1019
 
 
1020
        sector     = treq.sec;
 
1021
        nb_sectors = treq.secs;
 
1022
 
 
1023
        /*We store a local record of the request*/
 
1024
        while (nb_sectors > 0) {
 
1025
                cluster_offset = 
 
1026
                        get_cluster_offset(s, sector << 9, 0, 0, 0, 0);
 
1027
                index_in_cluster = sector & (s->cluster_sectors - 1);
 
1028
                n = s->cluster_sectors - index_in_cluster;
 
1029
                if (n > nb_sectors)
 
1030
                        n = nb_sectors;
 
1031
 
 
1032
                if (s->aio_free_count == 0) {
 
1033
                        td_complete_request(treq, -EBUSY);
 
1034
                        return;
 
1035
                }
 
1036
                
 
1037
                if(!cluster_offset) {
 
1038
            int i;
 
1039
            /* Forward entire request if possible. */
 
1040
            for(i=0; i<nb_sectors; i++)
 
1041
                if(get_cluster_offset(s, (sector+i) << 9, 0, 0, 0, 0))
 
1042
                    goto coalesce_failed;
 
1043
            treq.buf  = buf;
 
1044
            treq.sec  = sector;
 
1045
            treq.secs = nb_sectors;
 
1046
                        td_forward_request(treq);
 
1047
            return;
 
1048
coalesce_failed:            
 
1049
                        treq.buf  = buf;
 
1050
                        treq.sec  = sector;
 
1051
                        treq.secs = n;
 
1052
                        td_forward_request(treq);
 
1053
 
 
1054
                } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
 
1055
                        if (decompress_cluster(s, cluster_offset) < 0) {
 
1056
                                td_complete_request(treq, -EIO);
 
1057
                                goto done;
 
1058
                        }
 
1059
                        memcpy(buf, s->cluster_cache + index_in_cluster * 512, 
 
1060
                               512 * n);
 
1061
                        
 
1062
                        treq.buf  = buf;
 
1063
                        treq.sec  = sector;
 
1064
                        treq.secs = n;
 
1065
                        td_complete_request(treq, 0);
 
1066
                } else {
 
1067
                  clone.buf  = buf;
 
1068
                  clone.sec  = (cluster_offset>>9)+index_in_cluster;
 
1069
                  clone.secs = n;
 
1070
                  async_read(driver, clone);
 
1071
                }
 
1072
                nb_sectors -= n;
 
1073
                sector += n;
 
1074
                buf += n * 512;
 
1075
        }
 
1076
done:
 
1077
        return;
 
1078
}
 
1079
 
 
1080
void tdqcow_queue_write(td_driver_t *driver, td_request_t treq)
 
1081
{
 
1082
        struct tdqcow_state   *s  = (struct tdqcow_state *)driver->data;
 
1083
        int ret = 0, index_in_cluster, n, i;
 
1084
        uint64_t cluster_offset, sector, nb_sectors;
 
1085
        td_callback_t cb;
 
1086
        struct qcow_prv* prv;
 
1087
        char* buf = treq.buf;
 
1088
        td_request_t clone=treq;
 
1089
 
 
1090
        sector     = treq.sec;
 
1091
        nb_sectors = treq.secs;
 
1092
                   
 
1093
        /*We store a local record of the request*/
 
1094
        while (nb_sectors > 0) {
 
1095
                index_in_cluster = sector & (s->cluster_sectors - 1);
 
1096
                n = s->cluster_sectors - index_in_cluster;
 
1097
                if (n > nb_sectors)
 
1098
                        n = nb_sectors;
 
1099
 
 
1100
                if (s->aio_free_count == 0) {
 
1101
                        td_complete_request(treq, -EBUSY);
 
1102
                        return;
 
1103
                }
 
1104
 
 
1105
                cluster_offset = get_cluster_offset(s, sector << 9, 1, 0,
 
1106
                                                    index_in_cluster, 
 
1107
                                                    index_in_cluster+n);
 
1108
                if (!cluster_offset) {
 
1109
                        DPRINTF("Ooops, no write cluster offset!\n");
 
1110
                        td_complete_request(treq, -EIO);
 
1111
                        return;
 
1112
                }
 
1113
 
 
1114
                if (s->crypt_method) {
 
1115
                        encrypt_sectors(s, sector, s->cluster_data, 
 
1116
                                        (unsigned char *)buf, n, 1,
 
1117
                                        &s->aes_encrypt_key);
 
1118
 
 
1119
                        clone.buf  = buf;
 
1120
                        clone.sec  = (cluster_offset>>9) + index_in_cluster;
 
1121
                        clone.secs = n;
 
1122
                        async_write(driver, clone);
 
1123
                } else {
 
1124
                  clone.buf  = buf;
 
1125
                  clone.sec  = (cluster_offset>>9) + index_in_cluster;
 
1126
                  clone.secs = n;
 
1127
 
 
1128
                  async_write(driver, clone);
 
1129
                }
 
1130
                
 
1131
                nb_sectors -= n;
 
1132
                sector += n;
 
1133
                buf += n * 512;
 
1134
        }
 
1135
        s->cluster_cache_offset = -1; /* disable compressed cache */
 
1136
 
 
1137
        return;
 
1138
}
 
1139
 
 
1140
static int
 
1141
tdqcow_update_checksum(struct tdqcow_state *s)
 
1142
{
 
1143
        int i, fd, err;
 
1144
        uint32_t offset, cksum, out;
 
1145
 
 
1146
        if (!s->extended)
 
1147
                return 0;
 
1148
 
 
1149
        fd = open(s->name, O_WRONLY | O_LARGEFILE); /* open without O_DIRECT */
 
1150
        if (fd == -1) {
 
1151
                err = errno;
 
1152
                goto out;
 
1153
        }
 
1154
 
 
1155
        offset = sizeof(QCowHeader) + offsetof(QCowHeader_ext, cksum);
 
1156
        if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
 
1157
                err = errno;
 
1158
                goto out;
 
1159
        }
 
1160
 
 
1161
        /* convert to big endian for checksum */
 
1162
        for (i = 0; i < s->l1_size; i++)
 
1163
                cpu_to_be64s(&s->l1_table[i]);
 
1164
 
 
1165
        cksum = gen_cksum((char *)s->l1_table, s->l1_size * sizeof(uint64_t));
 
1166
 
 
1167
        /* and back again... */
 
1168
        for (i = 0; i < s->l1_size; i++)
 
1169
                be64_to_cpus(&s->l1_table[i]);
 
1170
 
 
1171
        DPRINTF("Writing cksum: %d", cksum);
 
1172
 
 
1173
        out = cpu_to_be32(cksum);
 
1174
        if (write(fd, &out, sizeof(out)) != sizeof(out)) {
 
1175
                err = errno;
 
1176
                goto out;
 
1177
        }
 
1178
 
 
1179
        err = 0;
 
1180
 
 
1181
out:
 
1182
        if (err)
 
1183
                DPRINTF("failed to update checksum: %d\n", err);
 
1184
        if (fd != -1)
 
1185
                close(fd);
 
1186
        return err;
 
1187
}
 
1188
                
 
1189
int tdqcow_close(td_driver_t *driver)
 
1190
{
 
1191
        struct tdqcow_state *s = (struct tdqcow_state *)driver->data;
 
1192
 
 
1193
        /*Update the hdr cksum*/
 
1194
        tdqcow_update_checksum(s);
 
1195
 
 
1196
        free_aio_state(s);
 
1197
        free(s->name);
 
1198
        free(s->l1_table);
 
1199
        free(s->l2_cache);
 
1200
        free(s->cluster_cache);
 
1201
        free(s->cluster_data);
 
1202
        close(s->fd);   
 
1203
        return 0;
 
1204
}
 
1205
 
 
1206
int qcow_create(const char *filename, uint64_t total_size,
 
1207
                const char *backing_file, int sparse)
 
1208
{
 
1209
        int fd, header_size, backing_filename_len, l1_size, i;
 
1210
        int shift, length, adjust, flags = 0, ret = 0;
 
1211
        QCowHeader header;
 
1212
        QCowHeader_ext exthdr;
 
1213
        char backing_filename[PATH_MAX], *ptr;
 
1214
        uint64_t tmp, size, total_length;
 
1215
        struct stat st;
 
1216
 
 
1217
        DPRINTF("Qcow_create: size %"PRIu64"\n",total_size);
 
1218
 
 
1219
        fd = open(filename, 
 
1220
                  O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
 
1221
                  0644);
 
1222
        if (fd < 0)
 
1223
                return -1;
 
1224
 
 
1225
        memset(&header, 0, sizeof(header));
 
1226
        header.magic = cpu_to_be32(QCOW_MAGIC);
 
1227
        header.version = cpu_to_be32(QCOW_VERSION);
 
1228
 
 
1229
        /*Create extended header fields*/
 
1230
        exthdr.xmagic = cpu_to_be32(XEN_MAGIC);
 
1231
 
 
1232
        header_size = sizeof(header) + sizeof(QCowHeader_ext);
 
1233
        backing_filename_len = 0;
 
1234
        size = (total_size >> SECTOR_SHIFT);
 
1235
        if (backing_file) {
 
1236
                if (strcmp(backing_file, "fat:")) {
 
1237
                        const char *p;
 
1238
                        /* XXX: this is a hack: we do not attempt to 
 
1239
                         *check for URL like syntax */
 
1240
                        p = strchr(backing_file, ':');
 
1241
                        if (p && (p - backing_file) >= 2) {
 
1242
                                /* URL like but exclude "c:" like filenames */
 
1243
                                strncpy(backing_filename, backing_file,
 
1244
                                        sizeof(backing_filename));
 
1245
                        } else {
 
1246
                                if (realpath(backing_file, backing_filename) == NULL ||
 
1247
                                    stat(backing_filename, &st) != 0) {
 
1248
                                        return -1;
 
1249
                                }
 
1250
                        }
 
1251
                        header.backing_file_offset = cpu_to_be64(header_size);
 
1252
                        backing_filename_len = strlen(backing_filename);
 
1253
                        header.backing_file_size = cpu_to_be32(
 
1254
                                backing_filename_len);
 
1255
                        header_size += backing_filename_len;
 
1256
                        
 
1257
                        /*Set to the backing file size*/
 
1258
                        if(get_filesize(backing_filename, &size, &st)) {
 
1259
                                return -1;
 
1260
                        }
 
1261
                        DPRINTF("Backing file size detected: %"PRId64" sectors" 
 
1262
                                "(total %"PRId64" [%"PRId64" MB])\n", 
 
1263
                                size, 
 
1264
                                (uint64_t)(size << SECTOR_SHIFT), 
 
1265
                                (uint64_t)(size >> 11));
 
1266
                } else {
 
1267
                        backing_file = NULL;
 
1268
                        DPRINTF("Setting file size: %"PRId64" (total %"PRId64")\n", 
 
1269
                                total_size, 
 
1270
                                (uint64_t) (total_size << SECTOR_SHIFT));
 
1271
                }
 
1272
                header.mtime = cpu_to_be32(st.st_mtime);
 
1273
                header.cluster_bits = 9; /* 512 byte cluster to avoid copying
 
1274
                                            unmodifyed sectors */
 
1275
                header.l2_bits = 12; /* 32 KB L2 tables */
 
1276
                exthdr.min_cluster_alloc = cpu_to_be32(1);
 
1277
        } else {
 
1278
                DPRINTF("Setting file size: %"PRId64" sectors" 
 
1279
                        "(total %"PRId64" [%"PRId64" MB])\n", 
 
1280
                        size, 
 
1281
                        (uint64_t) (size << SECTOR_SHIFT), 
 
1282
                        (uint64_t) (size >> 11));
 
1283
                header.cluster_bits = 12; /* 4 KB clusters */
 
1284
                header.l2_bits = 9; /* 4 KB L2 tables */
 
1285
                exthdr.min_cluster_alloc = cpu_to_be32(1 << 9);
 
1286
        }
 
1287
        /*Set the header size value*/
 
1288
        header.size = cpu_to_be64(size * 512);
 
1289
        
 
1290
        header_size = (header_size + 7) & ~7;
 
1291
        if (header_size % 4096 > 0) {
 
1292
                header_size = ((header_size >> 12) + 1) << 12;
 
1293
        }
 
1294
 
 
1295
        shift = header.cluster_bits + header.l2_bits;
 
1296
        l1_size = ((size * 512) + (1LL << shift) - 1) >> shift;
 
1297
 
 
1298
        header.l1_table_offset = cpu_to_be64(header_size);
 
1299
        DPRINTF("L1 Table offset: %d, size %d\n",
 
1300
                header_size,
 
1301
                (int)(l1_size * sizeof(uint64_t)));
 
1302
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
 
1303
 
 
1304
        ptr = calloc(1, l1_size * sizeof(uint64_t));
 
1305
        exthdr.cksum = cpu_to_be32(gen_cksum(ptr, l1_size * sizeof(uint64_t)));
 
1306
        printf("Created cksum: %d\n",exthdr.cksum);
 
1307
        free(ptr);
 
1308
 
 
1309
        /*adjust file length to system page size boundary*/
 
1310
        length = ROUNDUP(header_size + (l1_size * sizeof(uint64_t)),
 
1311
                getpagesize());
 
1312
        if (qtruncate(fd, length, 0)!=0) {
 
1313
                DPRINTF("ERROR truncating file\n");
 
1314
                return -1;
 
1315
        }
 
1316
 
 
1317
        if (sparse == 0) {
 
1318
                /*Filesize is length+l1_size*(1 << s->l2_bits)+(size*512)*/
 
1319
                total_length = length + (l1_size * (1 << 9)) + (size * 512);
 
1320
                if (qtruncate(fd, total_length, 0)!=0) {
 
1321
                        DPRINTF("ERROR truncating file\n");
 
1322
                        return -1;
 
1323
                }
 
1324
                printf("File truncated to length %"PRIu64"\n",total_length);
 
1325
        } else
 
1326
                flags = SPARSE_FILE;
 
1327
 
 
1328
        flags |= EXTHDR_L1_BIG_ENDIAN;
 
1329
        exthdr.flags = cpu_to_be32(flags);
 
1330
        
 
1331
        /* write all the data */
 
1332
        lseek(fd, 0, SEEK_SET);
 
1333
        ret += write(fd, &header, sizeof(header));
 
1334
        ret += write(fd, &exthdr, sizeof(exthdr));
 
1335
        if (backing_file)
 
1336
                ret += write(fd, backing_filename, backing_filename_len);
 
1337
 
 
1338
        lseek(fd, header_size, SEEK_SET);
 
1339
        tmp = 0;
 
1340
        for (i = 0;i < l1_size; i++) {
 
1341
                ret += write(fd, &tmp, sizeof(tmp));
 
1342
        }
 
1343
 
 
1344
        close(fd);
 
1345
 
 
1346
        return 0;
 
1347
}
 
1348
 
 
1349
static int qcow_make_empty(struct tdqcow_state *s)
 
1350
{
 
1351
        uint32_t l1_length = s->l1_size * sizeof(uint64_t);
 
1352
 
 
1353
        memset(s->l1_table, 0, l1_length);
 
1354
        lseek(s->fd, s->l1_table_offset, SEEK_SET);
 
1355
        if (write(s->fd, s->l1_table, l1_length) < 0)
 
1356
                return -1;
 
1357
        if (qtruncate(s->fd, s->l1_table_offset + l1_length, s->sparse)!=0) {
 
1358
                DPRINTF("ERROR truncating file\n");
 
1359
                return -1;
 
1360
        }
 
1361
 
 
1362
        memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
 
1363
        memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
 
1364
        memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
 
1365
 
 
1366
        return 0;
 
1367
}
 
1368
 
 
1369
static int qcow_get_cluster_size(struct tdqcow_state *s)
 
1370
{
 
1371
        return s->cluster_size;
 
1372
}
 
1373
 
 
1374
/* XXX: put compressed sectors first, then all the cluster aligned
 
1375
   tables to avoid losing bytes in alignment */
 
1376
static int qcow_compress_cluster(struct tdqcow_state *s, int64_t sector_num, 
 
1377
                          const uint8_t *buf)
 
1378
{
 
1379
        z_stream strm;
 
1380
        int ret, out_len;
 
1381
        uint8_t *out_buf;
 
1382
        uint64_t cluster_offset;
 
1383
 
 
1384
        out_buf = malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
 
1385
        if (!out_buf)
 
1386
                return -1;
 
1387
 
 
1388
        /* best compression, small window, no zlib header */
 
1389
        memset(&strm, 0, sizeof(strm));
 
1390
        ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
 
1391
                           Z_DEFLATED, -12, 
 
1392
                           9, Z_DEFAULT_STRATEGY);
 
1393
        if (ret != 0) {
 
1394
                free(out_buf);
 
1395
                return -1;
 
1396
        }
 
1397
 
 
1398
        strm.avail_in = s->cluster_size;
 
1399
        strm.next_in = (uint8_t *)buf;
 
1400
        strm.avail_out = s->cluster_size;
 
1401
        strm.next_out = out_buf;
 
1402
 
 
1403
        ret = deflate(&strm, Z_FINISH);
 
1404
        if (ret != Z_STREAM_END && ret != Z_OK) {
 
1405
                free(out_buf);
 
1406
                deflateEnd(&strm);
 
1407
                return -1;
 
1408
        }
 
1409
        out_len = strm.next_out - out_buf;
 
1410
 
 
1411
        deflateEnd(&strm);
 
1412
 
 
1413
        if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
 
1414
                /* could not compress: write normal cluster */
 
1415
                //tdqcow_queue_write(bs, sector_num, buf, s->cluster_sectors);
 
1416
        } else {
 
1417
                cluster_offset = get_cluster_offset(s, sector_num << 9, 2, 
 
1418
                                            out_len, 0, 0);
 
1419
                cluster_offset &= s->cluster_offset_mask;
 
1420
                lseek(s->fd, cluster_offset, SEEK_SET);
 
1421
                if (write(s->fd, out_buf, out_len) != out_len) {
 
1422
                        free(out_buf);
 
1423
                        return -1;
 
1424
                }
 
1425
        }
 
1426
        
 
1427
        free(out_buf);
 
1428
        return 0;
 
1429
}
 
1430
 
 
1431
static int
 
1432
tdqcow_get_image_type(const char *file, int *type)
 
1433
{
 
1434
        int fd;
 
1435
        size_t size;
 
1436
        QCowHeader header;
 
1437
 
 
1438
        fd = open(file, O_RDONLY);
 
1439
        if (fd == -1)
 
1440
                return -errno;
 
1441
 
 
1442
        size = read(fd, &header, sizeof(header));
 
1443
        close(fd);
 
1444
        if (size != sizeof(header))
 
1445
                return (errno ? -errno : -EIO);
 
1446
 
 
1447
        be32_to_cpus(&header.magic);
 
1448
        if (header.magic == QCOW_MAGIC)
 
1449
                *type = DISK_TYPE_QCOW;
 
1450
        else
 
1451
                *type = DISK_TYPE_AIO;
 
1452
 
 
1453
        return 0;
 
1454
}
 
1455
 
 
1456
int tdqcow_get_parent_id(td_driver_t *driver, td_disk_id_t *id)
 
1457
{
 
1458
        off_t off;
 
1459
        char *buf, *filename;
 
1460
        int len, secs, type, err = -EINVAL;
 
1461
        struct tdqcow_state *child  = (struct tdqcow_state *)driver->data;
 
1462
 
 
1463
        if (!child->backing_file_offset)
 
1464
                return TD_NO_PARENT;
 
1465
 
 
1466
        /* read the backing file name */
 
1467
        len  = child->backing_file_size;
 
1468
        off  = child->backing_file_offset - (child->backing_file_offset % 512);
 
1469
        secs = (len + (child->backing_file_offset - off) + 511) >> 9;
 
1470
 
 
1471
        if (posix_memalign((void **)&buf, 512, secs << 9)) 
 
1472
                return -1;
 
1473
 
 
1474
        if (lseek(child->fd, off, SEEK_SET) == (off_t)-1)
 
1475
                goto out;
 
1476
 
 
1477
        if (read(child->fd, buf, secs << 9) != secs << 9)
 
1478
                goto out;
 
1479
        filename       = buf + (child->backing_file_offset - off);
 
1480
        filename[len]  = '\0';
 
1481
 
 
1482
        if (tdqcow_get_image_type(filename, &type))
 
1483
                goto out;
 
1484
 
 
1485
        id->name       = strdup(filename);
 
1486
        id->drivertype = type;
 
1487
        err            = 0;
 
1488
 out:
 
1489
        free(buf);
 
1490
        return err;
 
1491
}
 
1492
 
 
1493
int tdqcow_validate_parent(td_driver_t *driver,
 
1494
                          td_driver_t *pdriver, td_flag_t flags)
 
1495
{
 
1496
        struct stat stats;
 
1497
        uint64_t psize, csize;
 
1498
        struct tdqcow_state *c = (struct tdqcow_state *)driver->data;
 
1499
        struct tdqcow_state *p = (struct tdqcow_state *)pdriver->data;
 
1500
        
 
1501
        if (stat(p->name, &stats))
 
1502
                return -EINVAL;
 
1503
        if (get_filesize(p->name, &psize, &stats))
 
1504
                return -EINVAL;
 
1505
 
 
1506
        if (stat(c->name, &stats))
 
1507
                return -EINVAL;
 
1508
        if (get_filesize(c->name, &csize, &stats))
 
1509
                return -EINVAL;
 
1510
 
 
1511
        if (csize != psize)
 
1512
                return -EINVAL;
 
1513
 
 
1514
        return 0;
 
1515
}
 
1516
 
 
1517
struct tap_disk tapdisk_qcow = {
 
1518
        .disk_type           = "tapdisk_qcow",
 
1519
        .flags              = 0,
 
1520
        .private_data_size   = sizeof(struct tdqcow_state),
 
1521
        .td_open             = tdqcow_open,
 
1522
        .td_close            = tdqcow_close,
 
1523
        .td_queue_read       = tdqcow_queue_read,
 
1524
        .td_queue_write      = tdqcow_queue_write,
 
1525
        .td_get_parent_id    = tdqcow_get_parent_id,
 
1526
        .td_validate_parent  = tdqcow_validate_parent,
 
1527
        .td_debug           = NULL,
 
1528
};