Statistics
| Branch: | Revision:

root / block / qcow2-cluster.c @ 1e3e8f1a

History | View | Annotate | Download (24.6 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
 * Returns 0 on success, -errno in failure case
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 ret;
503
        }
504
    }
505
    l2_offset = s->l1_table[l1_index];
506

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

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

    
526
    /* find the cluster offset for the given disk offset */
527

    
528
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
529

    
530
    *new_l2_table = l2_table;
531
    *new_l2_offset = l2_offset;
532
    *new_l2_index = l2_index;
533

    
534
    return 0;
535
}
536

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

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

    
559
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
560
    if (ret < 0) {
561
        return 0;
562
    }
563

    
564
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
565
    if (cluster_offset & QCOW_OFLAG_COPIED)
566
        return cluster_offset & ~QCOW_OFLAG_COPIED;
567

    
568
    if (cluster_offset)
569
        qcow2_free_any_clusters(bs, cluster_offset, 1);
570

    
571
    cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
572
    nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
573
                  (cluster_offset >> 9);
574

    
575
    cluster_offset |= QCOW_OFLAG_COMPRESSED |
576
                      ((uint64_t)nb_csectors << s->csize_shift);
577

    
578
    /* update L2 table */
579

    
580
    /* compressed clusters never have the copied flag */
581

    
582
    l2_table[l2_index] = cpu_to_be64(cluster_offset);
583
    if (bdrv_pwrite(s->hd,
584
                    l2_offset + l2_index * sizeof(uint64_t),
585
                    l2_table + l2_index,
586
                    sizeof(uint64_t)) != sizeof(uint64_t))
587
        return 0;
588

    
589
    return cluster_offset;
590
}
591

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

    
605
    if (bdrv_pwrite(s->hd, l2_offset + start_offset, &l2_table[l2_start_index],
606
        len) != len)
607
    {
608
        return -1;
609
    }
610

    
611
    return 0;
612
}
613

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

    
621
    if (m->nb_clusters == 0)
622
        return 0;
623

    
624
    old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
625

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

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

    
642
    /* update L2 table */
643
    ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index);
644
    if (ret < 0) {
645
        goto err;
646
    }
647

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

    
658
        l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
659
                    (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
660
     }
661

    
662
    if (write_l2_entries(s, l2_table, l2_offset, l2_index, m->nb_clusters) < 0) {
663
        ret = -1;
664
        goto err;
665
    }
666

    
667
    for (i = 0; i < j; i++)
668
        qcow2_free_any_clusters(bs,
669
            be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
670

    
671
    ret = 0;
672
err:
673
    qemu_free(old_cluster);
674
    return ret;
675
 }
676

    
677
/*
678
 * alloc_cluster_offset
679
 *
680
 * For a given offset of the disk image, return cluster offset in
681
 * qcow2 file.
682
 *
683
 * If the offset is not found, allocate a new cluster.
684
 *
685
 * Return the cluster offset if successful,
686
 * Return 0, otherwise.
687
 *
688
 */
689

    
690
uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
691
                                    uint64_t offset,
692
                                    int n_start, int n_end,
693
                                    int *num, QCowL2Meta *m)
694
{
695
    BDRVQcowState *s = bs->opaque;
696
    int l2_index, ret;
697
    uint64_t l2_offset, *l2_table, cluster_offset;
698
    unsigned int nb_clusters, i = 0;
699
    QCowL2Meta *old_alloc;
700

    
701
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
702
    if (ret < 0) {
703
        return 0;
704
    }
705

    
706
    nb_clusters = size_to_clusters(s, n_end << 9);
707

    
708
    nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
709

    
710
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
711

    
712
    /* We keep all QCOW_OFLAG_COPIED clusters */
713

    
714
    if (cluster_offset & QCOW_OFLAG_COPIED) {
715
        nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
716
                &l2_table[l2_index], 0, 0);
717

    
718
        cluster_offset &= ~QCOW_OFLAG_COPIED;
719
        m->nb_clusters = 0;
720

    
721
        goto out;
722
    }
723

    
724
    /* for the moment, multiple compressed clusters are not managed */
725

    
726
    if (cluster_offset & QCOW_OFLAG_COMPRESSED)
727
        nb_clusters = 1;
728

    
729
    /* how many available clusters ? */
730

    
731
    while (i < nb_clusters) {
732
        i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
733
                &l2_table[l2_index], i, 0);
734

    
735
        if(be64_to_cpu(l2_table[l2_index + i]))
736
            break;
737

    
738
        i += count_contiguous_free_clusters(nb_clusters - i,
739
                &l2_table[l2_index + i]);
740

    
741
        cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
742

    
743
        if ((cluster_offset & QCOW_OFLAG_COPIED) ||
744
                (cluster_offset & QCOW_OFLAG_COMPRESSED))
745
            break;
746
    }
747
    nb_clusters = i;
748

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

    
756
        uint64_t end_offset = offset + nb_clusters * s->cluster_size;
757
        uint64_t old_offset = old_alloc->offset;
758
        uint64_t old_end_offset = old_alloc->offset +
759
            old_alloc->nb_clusters * s->cluster_size;
760

    
761
        if (end_offset < old_offset || offset > old_end_offset) {
762
            /* No intersection */
763
        } else {
764
            if (offset < old_offset) {
765
                /* Stop at the start of a running allocation */
766
                nb_clusters = (old_offset - offset) >> s->cluster_bits;
767
            } else {
768
                nb_clusters = 0;
769
            }
770

    
771
            if (nb_clusters == 0) {
772
                /* Set dependency and wait for a callback */
773
                m->depends_on = old_alloc;
774
                m->nb_clusters = 0;
775
                *num = 0;
776
                return 0;
777
            }
778
        }
779
    }
780

    
781
    if (!nb_clusters) {
782
        abort();
783
    }
784

    
785
    QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
786

    
787
    /* allocate a new cluster */
788

    
789
    cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
790

    
791
    /* save info needed for meta data update */
792
    m->offset = offset;
793
    m->n_start = n_start;
794
    m->nb_clusters = nb_clusters;
795

    
796
out:
797
    m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
798

    
799
    *num = m->nb_available - n_start;
800

    
801
    return cluster_offset;
802
}
803

    
804
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
805
                             const uint8_t *buf, int buf_size)
806
{
807
    z_stream strm1, *strm = &strm1;
808
    int ret, out_len;
809

    
810
    memset(strm, 0, sizeof(*strm));
811

    
812
    strm->next_in = (uint8_t *)buf;
813
    strm->avail_in = buf_size;
814
    strm->next_out = out_buf;
815
    strm->avail_out = out_buf_size;
816

    
817
    ret = inflateInit2(strm, -12);
818
    if (ret != Z_OK)
819
        return -1;
820
    ret = inflate(strm, Z_FINISH);
821
    out_len = strm->next_out - out_buf;
822
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
823
        out_len != out_buf_size) {
824
        inflateEnd(strm);
825
        return -1;
826
    }
827
    inflateEnd(strm);
828
    return 0;
829
}
830

    
831
int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
832
{
833
    int ret, csize, nb_csectors, sector_offset;
834
    uint64_t coffset;
835

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