Statistics
| Branch: | Revision:

root / block / qcow2-cluster.c @ fb8fa77c

History | View | Annotate | Download (24.5 kB)

1
/*
2
 * Block driver for the QCOW version 2 format
3
 *
4
 * Copyright (c) 2004-2006 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
#include <zlib.h>
26

    
27
#include "qemu-common.h"
28
#include "block_int.h"
29
#include "block/qcow2.h"
30

    
31
int qcow2_grow_l1_table(BlockDriverState *bs, int min_size)
32
{
33
    BDRVQcowState *s = bs->opaque;
34
    int new_l1_size, new_l1_size2, ret, i;
35
    uint64_t *new_l1_table;
36
    uint64_t new_l1_table_offset;
37
    uint8_t data[12];
38

    
39
    new_l1_size = s->l1_size;
40
    if (min_size <= new_l1_size)
41
        return 0;
42
    if (new_l1_size == 0) {
43
        new_l1_size = 1;
44
    }
45
    while (min_size > new_l1_size) {
46
        new_l1_size = (new_l1_size * 3 + 1) / 2;
47
    }
48
#ifdef DEBUG_ALLOC2
49
    printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
50
#endif
51

    
52
    new_l1_size2 = sizeof(uint64_t) * new_l1_size;
53
    new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
54
    memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
55

    
56
    /* write new table (align to cluster) */
57
    new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
58

    
59
    for(i = 0; i < s->l1_size; i++)
60
        new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
61
    ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
62
    if (ret != new_l1_size2)
63
        goto fail;
64
    for(i = 0; i < s->l1_size; i++)
65
        new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
66

    
67
    /* set new table */
68
    cpu_to_be32w((uint32_t*)data, new_l1_size);
69
    cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
70
    ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data,sizeof(data));
71
    if (ret != sizeof(data)) {
72
        goto fail;
73
    }
74
    qemu_free(s->l1_table);
75
    qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
76
    s->l1_table_offset = new_l1_table_offset;
77
    s->l1_table = new_l1_table;
78
    s->l1_size = new_l1_size;
79
    return 0;
80
 fail:
81
    qemu_free(new_l1_table);
82
    qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2);
83
    return ret < 0 ? ret : -EIO;
84
}
85

    
86
void qcow2_l2_cache_reset(BlockDriverState *bs)
87
{
88
    BDRVQcowState *s = bs->opaque;
89

    
90
    memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
91
    memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
92
    memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
93
}
94

    
95
static inline int l2_cache_new_entry(BlockDriverState *bs)
96
{
97
    BDRVQcowState *s = bs->opaque;
98
    uint32_t min_count;
99
    int min_index, i;
100

    
101
    /* find a new entry in the least used one */
102
    min_index = 0;
103
    min_count = 0xffffffff;
104
    for(i = 0; i < L2_CACHE_SIZE; i++) {
105
        if (s->l2_cache_counts[i] < min_count) {
106
            min_count = s->l2_cache_counts[i];
107
            min_index = i;
108
        }
109
    }
110
    return min_index;
111
}
112

    
113
/*
114
 * seek_l2_table
115
 *
116
 * seek l2_offset in the l2_cache table
117
 * if not found, return NULL,
118
 * if found,
119
 *   increments the l2 cache hit count of the entry,
120
 *   if counter overflow, divide by two all counters
121
 *   return the pointer to the l2 cache entry
122
 *
123
 */
124

    
125
static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
126
{
127
    int i, j;
128

    
129
    for(i = 0; i < L2_CACHE_SIZE; i++) {
130
        if (l2_offset == s->l2_cache_offsets[i]) {
131
            /* increment the hit count */
132
            if (++s->l2_cache_counts[i] == 0xffffffff) {
133
                for(j = 0; j < L2_CACHE_SIZE; j++) {
134
                    s->l2_cache_counts[j] >>= 1;
135
                }
136
            }
137
            return s->l2_cache + (i << s->l2_bits);
138
        }
139
    }
140
    return NULL;
141
}
142

    
143
/*
144
 * l2_load
145
 *
146
 * Loads a L2 table into memory. If the table is in the cache, the cache
147
 * is used; otherwise the L2 table is loaded from the image file.
148
 *
149
 * Returns a pointer to the L2 table on success, or NULL if the read from
150
 * the image file failed.
151
 */
152

    
153
static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
154
{
155
    BDRVQcowState *s = bs->opaque;
156
    int min_index;
157
    uint64_t *l2_table;
158

    
159
    /* seek if the table for the given offset is in the cache */
160

    
161
    l2_table = seek_l2_table(s, l2_offset);
162
    if (l2_table != NULL)
163
        return l2_table;
164

    
165
    /* not found: load a new entry in the least used one */
166

    
167
    min_index = l2_cache_new_entry(bs);
168
    l2_table = s->l2_cache + (min_index << s->l2_bits);
169
    if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
170
        s->l2_size * sizeof(uint64_t))
171
        return NULL;
172
    s->l2_cache_offsets[min_index] = l2_offset;
173
    s->l2_cache_counts[min_index] = 1;
174

    
175
    return l2_table;
176
}
177

    
178
/*
179
 * Writes one sector of the L1 table to the disk (can't update single entries
180
 * and we really don't want bdrv_pread to perform a read-modify-write)
181
 */
182
#define L1_ENTRIES_PER_SECTOR (512 / 8)
183
static int write_l1_entry(BDRVQcowState *s, int l1_index)
184
{
185
    uint64_t buf[L1_ENTRIES_PER_SECTOR];
186
    int l1_start_index;
187
    int i;
188

    
189
    l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
190
    for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) {
191
        buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
192
    }
193

    
194
    if (bdrv_pwrite(s->hd, s->l1_table_offset + 8 * l1_start_index,
195
        buf, sizeof(buf)) != sizeof(buf))
196
    {
197
        return -1;
198
    }
199

    
200
    return 0;
201
}
202

    
203
/*
204
 * l2_allocate
205
 *
206
 * Allocate a new l2 entry in the file. If l1_index points to an already
207
 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
208
 * table) copy the contents of the old L2 table into the newly allocated one.
209
 * Otherwise the new table is initialized with zeros.
210
 *
211
 */
212

    
213
static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
214
{
215
    BDRVQcowState *s = bs->opaque;
216
    int min_index;
217
    uint64_t old_l2_offset;
218
    uint64_t *l2_table, l2_offset;
219

    
220
    old_l2_offset = s->l1_table[l1_index];
221

    
222
    /* allocate a new l2 entry */
223

    
224
    l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
225

    
226
    /* update the L1 entry */
227

    
228
    s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
229
    if (write_l1_entry(s, l1_index) < 0) {
230
        return NULL;
231
    }
232

    
233
    /* allocate a new entry in the l2 cache */
234

    
235
    min_index = l2_cache_new_entry(bs);
236
    l2_table = s->l2_cache + (min_index << s->l2_bits);
237

    
238
    if (old_l2_offset == 0) {
239
        /* if there was no old l2 table, clear the new table */
240
        memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
241
    } else {
242
        /* if there was an old l2 table, read it from the disk */
243
        if (bdrv_pread(s->hd, old_l2_offset,
244
                       l2_table, s->l2_size * sizeof(uint64_t)) !=
245
            s->l2_size * sizeof(uint64_t))
246
            return NULL;
247
    }
248
    /* write the l2 table to the file */
249
    if (bdrv_pwrite(s->hd, l2_offset,
250
                    l2_table, s->l2_size * sizeof(uint64_t)) !=
251
        s->l2_size * sizeof(uint64_t))
252
        return NULL;
253

    
254
    /* update the l2 cache entry */
255

    
256
    s->l2_cache_offsets[min_index] = l2_offset;
257
    s->l2_cache_counts[min_index] = 1;
258

    
259
    return l2_table;
260
}
261

    
262
static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
263
        uint64_t *l2_table, uint64_t start, uint64_t mask)
264
{
265
    int i;
266
    uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
267

    
268
    if (!offset)
269
        return 0;
270

    
271
    for (i = start; i < start + nb_clusters; i++)
272
        if (offset + (uint64_t) i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
273
            break;
274

    
275
        return (i - start);
276
}
277

    
278
static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
279
{
280
    int i = 0;
281

    
282
    while(nb_clusters-- && l2_table[i] == 0)
283
        i++;
284

    
285
    return i;
286
}
287

    
288
/* The crypt function is compatible with the linux cryptoloop
289
   algorithm for < 4 GB images. NOTE: out_buf == in_buf is
290
   supported */
291
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
292
                           uint8_t *out_buf, const uint8_t *in_buf,
293
                           int nb_sectors, int enc,
294
                           const AES_KEY *key)
295
{
296
    union {
297
        uint64_t ll[2];
298
        uint8_t b[16];
299
    } ivec;
300
    int i;
301

    
302
    for(i = 0; i < nb_sectors; i++) {
303
        ivec.ll[0] = cpu_to_le64(sector_num);
304
        ivec.ll[1] = 0;
305
        AES_cbc_encrypt(in_buf, out_buf, 512, key,
306
                        ivec.b, enc);
307
        sector_num++;
308
        in_buf += 512;
309
        out_buf += 512;
310
    }
311
}
312

    
313

    
314
static int qcow_read(BlockDriverState *bs, int64_t sector_num,
315
                     uint8_t *buf, int nb_sectors)
316
{
317
    BDRVQcowState *s = bs->opaque;
318
    int ret, index_in_cluster, n, n1;
319
    uint64_t cluster_offset;
320

    
321
    while (nb_sectors > 0) {
322
        n = nb_sectors;
323
        cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, &n);
324
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
325
        if (!cluster_offset) {
326
            if (bs->backing_hd) {
327
                /* read from the base image */
328
                n1 = qcow2_backing_read1(bs->backing_hd, sector_num, buf, n);
329
                if (n1 > 0) {
330
                    ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
331
                    if (ret < 0)
332
                        return -1;
333
                }
334
            } else {
335
                memset(buf, 0, 512 * n);
336
            }
337
        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
338
            if (qcow2_decompress_cluster(s, cluster_offset) < 0)
339
                return -1;
340
            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
341
        } else {
342
            ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
343
            if (ret != n * 512)
344
                return -1;
345
            if (s->crypt_method) {
346
                qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
347
                                &s->aes_decrypt_key);
348
            }
349
        }
350
        nb_sectors -= n;
351
        sector_num += n;
352
        buf += n * 512;
353
    }
354
    return 0;
355
}
356

    
357
static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
358
                        uint64_t cluster_offset, int n_start, int n_end)
359
{
360
    BDRVQcowState *s = bs->opaque;
361
    int n, ret;
362

    
363
    n = n_end - n_start;
364
    if (n <= 0)
365
        return 0;
366
    ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
367
    if (ret < 0)
368
        return ret;
369
    if (s->crypt_method) {
370
        qcow2_encrypt_sectors(s, start_sect + n_start,
371
                        s->cluster_data,
372
                        s->cluster_data, n, 1,
373
                        &s->aes_encrypt_key);
374
    }
375
    ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
376
                     s->cluster_data, n);
377
    if (ret < 0)
378
        return ret;
379
    return 0;
380
}
381

    
382

    
383
/*
384
 * get_cluster_offset
385
 *
386
 * For a given offset of the disk image, return cluster offset in
387
 * qcow2 file.
388
 *
389
 * on entry, *num is the number of contiguous clusters we'd like to
390
 * access following offset.
391
 *
392
 * on exit, *num is the number of contiguous clusters we can read.
393
 *
394
 * Return 1, if the offset is found
395
 * Return 0, otherwise.
396
 *
397
 */
398

    
399
uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
400
    int *num)
401
{
402
    BDRVQcowState *s = bs->opaque;
403
    unsigned int l1_index, l2_index;
404
    uint64_t l2_offset, *l2_table, cluster_offset;
405
    int l1_bits, c;
406
    unsigned int index_in_cluster, nb_clusters;
407
    uint64_t nb_available, nb_needed;
408

    
409
    index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
410
    nb_needed = *num + index_in_cluster;
411

    
412
    l1_bits = s->l2_bits + s->cluster_bits;
413

    
414
    /* compute how many bytes there are between the offset and
415
     * the end of the l1 entry
416
     */
417

    
418
    nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
419

    
420
    /* compute the number of available sectors */
421

    
422
    nb_available = (nb_available >> 9) + index_in_cluster;
423

    
424
    if (nb_needed > nb_available) {
425
        nb_needed = nb_available;
426
    }
427

    
428
    cluster_offset = 0;
429

    
430
    /* seek the the l2 offset in the l1 table */
431

    
432
    l1_index = offset >> l1_bits;
433
    if (l1_index >= s->l1_size)
434
        goto out;
435

    
436
    l2_offset = s->l1_table[l1_index];
437

    
438
    /* seek the l2 table of the given l2 offset */
439

    
440
    if (!l2_offset)
441
        goto out;
442

    
443
    /* load the l2 table in memory */
444

    
445
    l2_offset &= ~QCOW_OFLAG_COPIED;
446
    l2_table = l2_load(bs, l2_offset);
447
    if (l2_table == NULL)
448
        return 0;
449

    
450
    /* find the cluster offset for the given disk offset */
451

    
452
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
453
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
454
    nb_clusters = size_to_clusters(s, nb_needed << 9);
455

    
456
    if (!cluster_offset) {
457
        /* how many empty clusters ? */
458
        c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
459
    } else {
460
        /* how many allocated clusters ? */
461
        c = count_contiguous_clusters(nb_clusters, s->cluster_size,
462
                &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
463
    }
464

    
465
   nb_available = (c * s->cluster_sectors);
466
out:
467
    if (nb_available > nb_needed)
468
        nb_available = nb_needed;
469

    
470
    *num = nb_available - index_in_cluster;
471

    
472
    return cluster_offset & ~QCOW_OFLAG_COPIED;
473
}
474

    
475
/*
476
 * get_cluster_table
477
 *
478
 * for a given disk offset, load (and allocate if needed)
479
 * the l2 table.
480
 *
481
 * the l2 table offset in the qcow2 file and the cluster index
482
 * in the l2 table are given to the caller.
483
 *
484
 */
485

    
486
static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
487
                             uint64_t **new_l2_table,
488
                             uint64_t *new_l2_offset,
489
                             int *new_l2_index)
490
{
491
    BDRVQcowState *s = bs->opaque;
492
    unsigned int l1_index, l2_index;
493
    uint64_t l2_offset, *l2_table;
494
    int ret;
495

    
496
    /* seek the the l2 offset in the l1 table */
497

    
498
    l1_index = offset >> (s->l2_bits + s->cluster_bits);
499
    if (l1_index >= s->l1_size) {
500
        ret = qcow2_grow_l1_table(bs, l1_index + 1);
501
        if (ret < 0)
502
            return 0;
503
    }
504
    l2_offset = s->l1_table[l1_index];
505

    
506
    /* seek the l2 table of the given l2 offset */
507

    
508
    if (l2_offset & QCOW_OFLAG_COPIED) {
509
        /* load the l2 table in memory */
510
        l2_offset &= ~QCOW_OFLAG_COPIED;
511
        l2_table = l2_load(bs, l2_offset);
512
        if (l2_table == NULL)
513
            return 0;
514
    } else {
515
        if (l2_offset)
516
            qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
517
        l2_table = l2_allocate(bs, l1_index);
518
        if (l2_table == NULL)
519
            return 0;
520
        l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
521
    }
522

    
523
    /* find the cluster offset for the given disk offset */
524

    
525
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
526

    
527
    *new_l2_table = l2_table;
528
    *new_l2_offset = l2_offset;
529
    *new_l2_index = l2_index;
530

    
531
    return 1;
532
}
533

    
534
/*
535
 * alloc_compressed_cluster_offset
536
 *
537
 * For a given offset of the disk image, return cluster offset in
538
 * qcow2 file.
539
 *
540
 * If the offset is not found, allocate a new compressed cluster.
541
 *
542
 * Return the cluster offset if successful,
543
 * Return 0, otherwise.
544
 *
545
 */
546

    
547
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
548
                                               uint64_t offset,
549
                                               int compressed_size)
550
{
551
    BDRVQcowState *s = bs->opaque;
552
    int l2_index, ret;
553
    uint64_t l2_offset, *l2_table, cluster_offset;
554
    int nb_csectors;
555

    
556
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
557
    if (ret == 0)
558
        return 0;
559

    
560
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
561
    if (cluster_offset & QCOW_OFLAG_COPIED)
562
        return cluster_offset & ~QCOW_OFLAG_COPIED;
563

    
564
    if (cluster_offset)
565
        qcow2_free_any_clusters(bs, cluster_offset, 1);
566

    
567
    cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
568
    nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
569
                  (cluster_offset >> 9);
570

    
571
    cluster_offset |= QCOW_OFLAG_COMPRESSED |
572
                      ((uint64_t)nb_csectors << s->csize_shift);
573

    
574
    /* update L2 table */
575

    
576
    /* compressed clusters never have the copied flag */
577

    
578
    l2_table[l2_index] = cpu_to_be64(cluster_offset);
579
    if (bdrv_pwrite(s->hd,
580
                    l2_offset + l2_index * sizeof(uint64_t),
581
                    l2_table + l2_index,
582
                    sizeof(uint64_t)) != sizeof(uint64_t))
583
        return 0;
584

    
585
    return cluster_offset;
586
}
587

    
588
/*
589
 * Write L2 table updates to disk, writing whole sectors to avoid a
590
 * read-modify-write in bdrv_pwrite
591
 */
592
#define L2_ENTRIES_PER_SECTOR (512 / 8)
593
static int write_l2_entries(BDRVQcowState *s, uint64_t *l2_table,
594
    uint64_t l2_offset, int l2_index, int num)
595
{
596
    int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1);
597
    int start_offset = (8 * l2_index) & ~511;
598
    int end_offset = (8 * (l2_index + num) + 511) & ~511;
599
    size_t len = end_offset - start_offset;
600

    
601
    if (bdrv_pwrite(s->hd, l2_offset + start_offset, &l2_table[l2_start_index],
602
        len) != len)
603
    {
604
        return -1;
605
    }
606

    
607
    return 0;
608
}
609

    
610
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
611
    QCowL2Meta *m)
612
{
613
    BDRVQcowState *s = bs->opaque;
614
    int i, j = 0, l2_index, ret;
615
    uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
616

    
617
    if (m->nb_clusters == 0)
618
        return 0;
619

    
620
    old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
621

    
622
    /* copy content of unmodified sectors */
623
    start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
624
    if (m->n_start) {
625
        ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
626
        if (ret < 0)
627
            goto err;
628
    }
629

    
630
    if (m->nb_available & (s->cluster_sectors - 1)) {
631
        uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
632
        ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
633
                m->nb_available - end, s->cluster_sectors);
634
        if (ret < 0)
635
            goto err;
636
    }
637

    
638
    ret = -EIO;
639
    /* update L2 table */
640
    if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index))
641
        goto err;
642

    
643
    for (i = 0; i < m->nb_clusters; i++) {
644
        /* if two concurrent writes happen to the same unallocated cluster
645
         * each write allocates separate cluster and writes data concurrently.
646
         * The first one to complete updates l2 table with pointer to its
647
         * cluster the second one has to do RMW (which is done above by
648
         * copy_sectors()), update l2 table with its cluster pointer and free
649
         * old cluster. This is what this loop does */
650
        if(l2_table[l2_index + i] != 0)
651
            old_cluster[j++] = l2_table[l2_index + i];
652

    
653
        l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
654
                    (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
655
     }
656

    
657
    if (write_l2_entries(s, l2_table, l2_offset, l2_index, m->nb_clusters) < 0) {
658
        ret = -1;
659
        goto err;
660
    }
661

    
662
    for (i = 0; i < j; i++)
663
        qcow2_free_any_clusters(bs,
664
            be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
665

    
666
    ret = 0;
667
err:
668
    qemu_free(old_cluster);
669
    return ret;
670
 }
671

    
672
/*
673
 * alloc_cluster_offset
674
 *
675
 * For a given offset of the disk image, return cluster offset in
676
 * qcow2 file.
677
 *
678
 * If the offset is not found, allocate a new cluster.
679
 *
680
 * Return the cluster offset if successful,
681
 * Return 0, otherwise.
682
 *
683
 */
684

    
685
uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
686
                                    uint64_t offset,
687
                                    int n_start, int n_end,
688
                                    int *num, QCowL2Meta *m)
689
{
690
    BDRVQcowState *s = bs->opaque;
691
    int l2_index, ret;
692
    uint64_t l2_offset, *l2_table, cluster_offset;
693
    unsigned int nb_clusters, i = 0;
694
    QCowL2Meta *old_alloc;
695

    
696
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
697
    if (ret == 0)
698
        return 0;
699

    
700
    nb_clusters = size_to_clusters(s, n_end << 9);
701

    
702
    nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
703

    
704
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
705

    
706
    /* We keep all QCOW_OFLAG_COPIED clusters */
707

    
708
    if (cluster_offset & QCOW_OFLAG_COPIED) {
709
        nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
710
                &l2_table[l2_index], 0, 0);
711

    
712
        cluster_offset &= ~QCOW_OFLAG_COPIED;
713
        m->nb_clusters = 0;
714

    
715
        goto out;
716
    }
717

    
718
    /* for the moment, multiple compressed clusters are not managed */
719

    
720
    if (cluster_offset & QCOW_OFLAG_COMPRESSED)
721
        nb_clusters = 1;
722

    
723
    /* how many available clusters ? */
724

    
725
    while (i < nb_clusters) {
726
        i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
727
                &l2_table[l2_index], i, 0);
728

    
729
        if(be64_to_cpu(l2_table[l2_index + i]))
730
            break;
731

    
732
        i += count_contiguous_free_clusters(nb_clusters - i,
733
                &l2_table[l2_index + i]);
734

    
735
        cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
736

    
737
        if ((cluster_offset & QCOW_OFLAG_COPIED) ||
738
                (cluster_offset & QCOW_OFLAG_COMPRESSED))
739
            break;
740
    }
741
    nb_clusters = i;
742

    
743
    /*
744
     * Check if there already is an AIO write request in flight which allocates
745
     * the same cluster. In this case we need to wait until the previous
746
     * request has completed and updated the L2 table accordingly.
747
     */
748
    QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
749

    
750
        uint64_t end_offset = offset + nb_clusters * s->cluster_size;
751
        uint64_t old_offset = old_alloc->offset;
752
        uint64_t old_end_offset = old_alloc->offset +
753
            old_alloc->nb_clusters * s->cluster_size;
754

    
755
        if (end_offset < old_offset || offset > old_end_offset) {
756
            /* No intersection */
757
        } else {
758
            if (offset < old_offset) {
759
                /* Stop at the start of a running allocation */
760
                nb_clusters = (old_offset - offset) >> s->cluster_bits;
761
            } else {
762
                nb_clusters = 0;
763
            }
764

    
765
            if (nb_clusters == 0) {
766
                /* Set dependency and wait for a callback */
767
                m->depends_on = old_alloc;
768
                m->nb_clusters = 0;
769
                *num = 0;
770
                return 0;
771
            }
772
        }
773
    }
774

    
775
    if (!nb_clusters) {
776
        abort();
777
    }
778

    
779
    QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
780

    
781
    /* allocate a new cluster */
782

    
783
    cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
784

    
785
    /* save info needed for meta data update */
786
    m->offset = offset;
787
    m->n_start = n_start;
788
    m->nb_clusters = nb_clusters;
789

    
790
out:
791
    m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
792

    
793
    *num = m->nb_available - n_start;
794

    
795
    return cluster_offset;
796
}
797

    
798
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
799
                             const uint8_t *buf, int buf_size)
800
{
801
    z_stream strm1, *strm = &strm1;
802
    int ret, out_len;
803

    
804
    memset(strm, 0, sizeof(*strm));
805

    
806
    strm->next_in = (uint8_t *)buf;
807
    strm->avail_in = buf_size;
808
    strm->next_out = out_buf;
809
    strm->avail_out = out_buf_size;
810

    
811
    ret = inflateInit2(strm, -12);
812
    if (ret != Z_OK)
813
        return -1;
814
    ret = inflate(strm, Z_FINISH);
815
    out_len = strm->next_out - out_buf;
816
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
817
        out_len != out_buf_size) {
818
        inflateEnd(strm);
819
        return -1;
820
    }
821
    inflateEnd(strm);
822
    return 0;
823
}
824

    
825
int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
826
{
827
    int ret, csize, nb_csectors, sector_offset;
828
    uint64_t coffset;
829

    
830
    coffset = cluster_offset & s->cluster_offset_mask;
831
    if (s->cluster_cache_offset != coffset) {
832
        nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
833
        sector_offset = coffset & 511;
834
        csize = nb_csectors * 512 - sector_offset;
835
        ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
836
        if (ret < 0) {
837
            return -1;
838
        }
839
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
840
                              s->cluster_data + sector_offset, csize) < 0) {
841
            return -1;
842
        }
843
        s->cluster_cache_offset = coffset;
844
    }
845
    return 0;
846
}