Merge branch 'upstream-merge'
[qemu-kvm/markmc.git] / block / qcow2-cluster.c
blobe444e53e136a5cb98ff99386686a397cd8632f6e
1 /*
2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include <zlib.h>
27 #include "qemu-common.h"
28 #include "block_int.h"
29 #include "block/qcow2.h"
31 int qcow2_grow_l1_table(BlockDriverState *bs, int min_size)
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];
39 new_l1_size = s->l1_size;
40 if (min_size <= new_l1_size)
41 return 0;
42 while (min_size > new_l1_size) {
43 new_l1_size = (new_l1_size * 3 + 1) / 2;
45 #ifdef DEBUG_ALLOC2
46 printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
47 #endif
49 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
50 new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
51 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
53 /* write new table (align to cluster) */
54 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
56 for(i = 0; i < s->l1_size; i++)
57 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
58 ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
59 if (ret != new_l1_size2)
60 goto fail;
61 for(i = 0; i < s->l1_size; i++)
62 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
64 /* set new table */
65 cpu_to_be32w((uint32_t*)data, new_l1_size);
66 cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
67 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data,
68 sizeof(data)) != sizeof(data))
69 goto fail;
70 qemu_free(s->l1_table);
71 qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
72 s->l1_table_offset = new_l1_table_offset;
73 s->l1_table = new_l1_table;
74 s->l1_size = new_l1_size;
75 return 0;
76 fail:
77 qemu_free(s->l1_table);
78 return -EIO;
81 void qcow2_l2_cache_reset(BlockDriverState *bs)
83 BDRVQcowState *s = bs->opaque;
85 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
86 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
87 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
90 static inline int l2_cache_new_entry(BlockDriverState *bs)
92 BDRVQcowState *s = bs->opaque;
93 uint32_t min_count;
94 int min_index, i;
96 /* find a new entry in the least used one */
97 min_index = 0;
98 min_count = 0xffffffff;
99 for(i = 0; i < L2_CACHE_SIZE; i++) {
100 if (s->l2_cache_counts[i] < min_count) {
101 min_count = s->l2_cache_counts[i];
102 min_index = i;
105 return min_index;
109 * seek_l2_table
111 * seek l2_offset in the l2_cache table
112 * if not found, return NULL,
113 * if found,
114 * increments the l2 cache hit count of the entry,
115 * if counter overflow, divide by two all counters
116 * return the pointer to the l2 cache entry
120 static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
122 int i, j;
124 for(i = 0; i < L2_CACHE_SIZE; i++) {
125 if (l2_offset == s->l2_cache_offsets[i]) {
126 /* increment the hit count */
127 if (++s->l2_cache_counts[i] == 0xffffffff) {
128 for(j = 0; j < L2_CACHE_SIZE; j++) {
129 s->l2_cache_counts[j] >>= 1;
132 return s->l2_cache + (i << s->l2_bits);
135 return NULL;
139 * l2_load
141 * Loads a L2 table into memory. If the table is in the cache, the cache
142 * is used; otherwise the L2 table is loaded from the image file.
144 * Returns a pointer to the L2 table on success, or NULL if the read from
145 * the image file failed.
148 static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
150 BDRVQcowState *s = bs->opaque;
151 int min_index;
152 uint64_t *l2_table;
154 /* seek if the table for the given offset is in the cache */
156 l2_table = seek_l2_table(s, l2_offset);
157 if (l2_table != NULL)
158 return l2_table;
160 /* not found: load a new entry in the least used one */
162 min_index = l2_cache_new_entry(bs);
163 l2_table = s->l2_cache + (min_index << s->l2_bits);
164 if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
165 s->l2_size * sizeof(uint64_t))
166 return NULL;
167 s->l2_cache_offsets[min_index] = l2_offset;
168 s->l2_cache_counts[min_index] = 1;
170 return l2_table;
174 * Writes one sector of the L1 table to the disk (can't update single entries
175 * and we really don't want bdrv_pread to perform a read-modify-write)
177 #define L1_ENTRIES_PER_SECTOR (512 / 8)
178 static int write_l1_entry(BDRVQcowState *s, int l1_index)
180 uint64_t buf[L1_ENTRIES_PER_SECTOR];
181 int l1_start_index;
182 int i;
184 l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
185 for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) {
186 buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
189 if (bdrv_pwrite(s->hd, s->l1_table_offset + 8 * l1_start_index,
190 buf, sizeof(buf)) != sizeof(buf))
192 return -1;
195 return 0;
199 * l2_allocate
201 * Allocate a new l2 entry in the file. If l1_index points to an already
202 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
203 * table) copy the contents of the old L2 table into the newly allocated one.
204 * Otherwise the new table is initialized with zeros.
208 static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
210 BDRVQcowState *s = bs->opaque;
211 int min_index;
212 uint64_t old_l2_offset;
213 uint64_t *l2_table, l2_offset;
215 old_l2_offset = s->l1_table[l1_index];
217 /* allocate a new l2 entry */
219 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
221 /* update the L1 entry */
223 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
224 if (write_l1_entry(s, l1_index) < 0) {
225 return NULL;
228 /* allocate a new entry in the l2 cache */
230 min_index = l2_cache_new_entry(bs);
231 l2_table = s->l2_cache + (min_index << s->l2_bits);
233 if (old_l2_offset == 0) {
234 /* if there was no old l2 table, clear the new table */
235 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
236 } else {
237 /* if there was an old l2 table, read it from the disk */
238 if (bdrv_pread(s->hd, old_l2_offset,
239 l2_table, s->l2_size * sizeof(uint64_t)) !=
240 s->l2_size * sizeof(uint64_t))
241 return NULL;
243 /* write the l2 table to the file */
244 if (bdrv_pwrite(s->hd, l2_offset,
245 l2_table, s->l2_size * sizeof(uint64_t)) !=
246 s->l2_size * sizeof(uint64_t))
247 return NULL;
249 /* update the l2 cache entry */
251 s->l2_cache_offsets[min_index] = l2_offset;
252 s->l2_cache_counts[min_index] = 1;
254 return l2_table;
257 static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
258 uint64_t *l2_table, uint64_t start, uint64_t mask)
260 int i;
261 uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
263 if (!offset)
264 return 0;
266 for (i = start; i < start + nb_clusters; i++)
267 if (offset + (uint64_t) i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
268 break;
270 return (i - start);
273 static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
275 int i = 0;
277 while(nb_clusters-- && l2_table[i] == 0)
278 i++;
280 return i;
283 /* The crypt function is compatible with the linux cryptoloop
284 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
285 supported */
286 void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
287 uint8_t *out_buf, const uint8_t *in_buf,
288 int nb_sectors, int enc,
289 const AES_KEY *key)
291 union {
292 uint64_t ll[2];
293 uint8_t b[16];
294 } ivec;
295 int i;
297 for(i = 0; i < nb_sectors; i++) {
298 ivec.ll[0] = cpu_to_le64(sector_num);
299 ivec.ll[1] = 0;
300 AES_cbc_encrypt(in_buf, out_buf, 512, key,
301 ivec.b, enc);
302 sector_num++;
303 in_buf += 512;
304 out_buf += 512;
309 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
310 uint8_t *buf, int nb_sectors)
312 BDRVQcowState *s = bs->opaque;
313 int ret, index_in_cluster, n, n1;
314 uint64_t cluster_offset;
316 while (nb_sectors > 0) {
317 n = nb_sectors;
318 cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, &n);
319 index_in_cluster = sector_num & (s->cluster_sectors - 1);
320 if (!cluster_offset) {
321 if (bs->backing_hd) {
322 /* read from the base image */
323 n1 = qcow2_backing_read1(bs->backing_hd, sector_num, buf, n);
324 if (n1 > 0) {
325 ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
326 if (ret < 0)
327 return -1;
329 } else {
330 memset(buf, 0, 512 * n);
332 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
333 if (qcow2_decompress_cluster(s, cluster_offset) < 0)
334 return -1;
335 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
336 } else {
337 ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
338 if (ret != n * 512)
339 return -1;
340 if (s->crypt_method) {
341 qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
342 &s->aes_decrypt_key);
345 nb_sectors -= n;
346 sector_num += n;
347 buf += n * 512;
349 return 0;
352 static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
353 uint64_t cluster_offset, int n_start, int n_end)
355 BDRVQcowState *s = bs->opaque;
356 int n, ret;
358 n = n_end - n_start;
359 if (n <= 0)
360 return 0;
361 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
362 if (ret < 0)
363 return ret;
364 if (s->crypt_method) {
365 qcow2_encrypt_sectors(s, start_sect + n_start,
366 s->cluster_data,
367 s->cluster_data, n, 1,
368 &s->aes_encrypt_key);
370 ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
371 s->cluster_data, n);
372 if (ret < 0)
373 return ret;
374 return 0;
379 * get_cluster_offset
381 * For a given offset of the disk image, return cluster offset in
382 * qcow2 file.
384 * on entry, *num is the number of contiguous clusters we'd like to
385 * access following offset.
387 * on exit, *num is the number of contiguous clusters we can read.
389 * Return 1, if the offset is found
390 * Return 0, otherwise.
394 uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
395 int *num)
397 BDRVQcowState *s = bs->opaque;
398 unsigned int l1_index, l2_index;
399 uint64_t l2_offset, *l2_table, cluster_offset;
400 int l1_bits, c;
401 unsigned int index_in_cluster, nb_clusters;
402 uint64_t nb_available, nb_needed;
404 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
405 nb_needed = *num + index_in_cluster;
407 l1_bits = s->l2_bits + s->cluster_bits;
409 /* compute how many bytes there are between the offset and
410 * the end of the l1 entry
413 nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
415 /* compute the number of available sectors */
417 nb_available = (nb_available >> 9) + index_in_cluster;
419 if (nb_needed > nb_available) {
420 nb_needed = nb_available;
423 cluster_offset = 0;
425 /* seek the the l2 offset in the l1 table */
427 l1_index = offset >> l1_bits;
428 if (l1_index >= s->l1_size)
429 goto out;
431 l2_offset = s->l1_table[l1_index];
433 /* seek the l2 table of the given l2 offset */
435 if (!l2_offset)
436 goto out;
438 /* load the l2 table in memory */
440 l2_offset &= ~QCOW_OFLAG_COPIED;
441 l2_table = l2_load(bs, l2_offset);
442 if (l2_table == NULL)
443 return 0;
445 /* find the cluster offset for the given disk offset */
447 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
448 cluster_offset = be64_to_cpu(l2_table[l2_index]);
449 nb_clusters = size_to_clusters(s, nb_needed << 9);
451 if (!cluster_offset) {
452 /* how many empty clusters ? */
453 c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
454 } else {
455 /* how many allocated clusters ? */
456 c = count_contiguous_clusters(nb_clusters, s->cluster_size,
457 &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
460 nb_available = (c * s->cluster_sectors);
461 out:
462 if (nb_available > nb_needed)
463 nb_available = nb_needed;
465 *num = nb_available - index_in_cluster;
467 return cluster_offset & ~QCOW_OFLAG_COPIED;
471 * get_cluster_table
473 * for a given disk offset, load (and allocate if needed)
474 * the l2 table.
476 * the l2 table offset in the qcow2 file and the cluster index
477 * in the l2 table are given to the caller.
481 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
482 uint64_t **new_l2_table,
483 uint64_t *new_l2_offset,
484 int *new_l2_index)
486 BDRVQcowState *s = bs->opaque;
487 unsigned int l1_index, l2_index;
488 uint64_t l2_offset, *l2_table;
489 int ret;
491 /* seek the the l2 offset in the l1 table */
493 l1_index = offset >> (s->l2_bits + s->cluster_bits);
494 if (l1_index >= s->l1_size) {
495 ret = qcow2_grow_l1_table(bs, l1_index + 1);
496 if (ret < 0)
497 return 0;
499 l2_offset = s->l1_table[l1_index];
501 /* seek the l2 table of the given l2 offset */
503 if (l2_offset & QCOW_OFLAG_COPIED) {
504 /* load the l2 table in memory */
505 l2_offset &= ~QCOW_OFLAG_COPIED;
506 l2_table = l2_load(bs, l2_offset);
507 if (l2_table == NULL)
508 return 0;
509 } else {
510 if (l2_offset)
511 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
512 l2_table = l2_allocate(bs, l1_index);
513 if (l2_table == NULL)
514 return 0;
515 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
518 /* find the cluster offset for the given disk offset */
520 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
522 *new_l2_table = l2_table;
523 *new_l2_offset = l2_offset;
524 *new_l2_index = l2_index;
526 return 1;
530 * alloc_compressed_cluster_offset
532 * For a given offset of the disk image, return cluster offset in
533 * qcow2 file.
535 * If the offset is not found, allocate a new compressed cluster.
537 * Return the cluster offset if successful,
538 * Return 0, otherwise.
542 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
543 uint64_t offset,
544 int compressed_size)
546 BDRVQcowState *s = bs->opaque;
547 int l2_index, ret;
548 uint64_t l2_offset, *l2_table, cluster_offset;
549 int nb_csectors;
551 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
552 if (ret == 0)
553 return 0;
555 cluster_offset = be64_to_cpu(l2_table[l2_index]);
556 if (cluster_offset & QCOW_OFLAG_COPIED)
557 return cluster_offset & ~QCOW_OFLAG_COPIED;
559 if (cluster_offset)
560 qcow2_free_any_clusters(bs, cluster_offset, 1);
562 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
563 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
564 (cluster_offset >> 9);
566 cluster_offset |= QCOW_OFLAG_COMPRESSED |
567 ((uint64_t)nb_csectors << s->csize_shift);
569 /* update L2 table */
571 /* compressed clusters never have the copied flag */
573 l2_table[l2_index] = cpu_to_be64(cluster_offset);
574 if (bdrv_pwrite(s->hd,
575 l2_offset + l2_index * sizeof(uint64_t),
576 l2_table + l2_index,
577 sizeof(uint64_t)) != sizeof(uint64_t))
578 return 0;
580 return cluster_offset;
584 * Write L2 table updates to disk, writing whole sectors to avoid a
585 * read-modify-write in bdrv_pwrite
587 #define L2_ENTRIES_PER_SECTOR (512 / 8)
588 static int write_l2_entries(BDRVQcowState *s, uint64_t *l2_table,
589 uint64_t l2_offset, int l2_index, int num)
591 int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1);
592 int start_offset = (8 * l2_index) & ~511;
593 int end_offset = (8 * (l2_index + num) + 511) & ~511;
594 size_t len = end_offset - start_offset;
596 if (bdrv_pwrite(s->hd, l2_offset + start_offset, &l2_table[l2_start_index],
597 len) != len)
599 return -1;
602 return 0;
605 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
606 QCowL2Meta *m)
608 BDRVQcowState *s = bs->opaque;
609 int i, j = 0, l2_index, ret;
610 uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
612 if (m->nb_clusters == 0)
613 return 0;
615 old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
617 /* copy content of unmodified sectors */
618 start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
619 if (m->n_start) {
620 ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
621 if (ret < 0)
622 goto err;
625 if (m->nb_available & (s->cluster_sectors - 1)) {
626 uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
627 ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
628 m->nb_available - end, s->cluster_sectors);
629 if (ret < 0)
630 goto err;
633 ret = -EIO;
634 /* update L2 table */
635 if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index))
636 goto err;
638 for (i = 0; i < m->nb_clusters; i++) {
639 /* if two concurrent writes happen to the same unallocated cluster
640 * each write allocates separate cluster and writes data concurrently.
641 * The first one to complete updates l2 table with pointer to its
642 * cluster the second one has to do RMW (which is done above by
643 * copy_sectors()), update l2 table with its cluster pointer and free
644 * old cluster. This is what this loop does */
645 if(l2_table[l2_index + i] != 0)
646 old_cluster[j++] = l2_table[l2_index + i];
648 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
649 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
652 if (write_l2_entries(s, l2_table, l2_offset, l2_index, m->nb_clusters) < 0) {
653 ret = -1;
654 goto err;
657 for (i = 0; i < j; i++)
658 qcow2_free_any_clusters(bs,
659 be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
661 ret = 0;
662 err:
663 qemu_free(old_cluster);
664 return ret;
668 * alloc_cluster_offset
670 * For a given offset of the disk image, return cluster offset in
671 * qcow2 file.
673 * If the offset is not found, allocate a new cluster.
675 * Return the cluster offset if successful,
676 * Return 0, otherwise.
680 uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
681 uint64_t offset,
682 int n_start, int n_end,
683 int *num, QCowL2Meta *m)
685 BDRVQcowState *s = bs->opaque;
686 int l2_index, ret;
687 uint64_t l2_offset, *l2_table, cluster_offset;
688 unsigned int nb_clusters, i = 0;
689 QCowL2Meta *old_alloc;
691 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
692 if (ret == 0)
693 return 0;
695 nb_clusters = size_to_clusters(s, n_end << 9);
697 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
699 cluster_offset = be64_to_cpu(l2_table[l2_index]);
701 /* We keep all QCOW_OFLAG_COPIED clusters */
703 if (cluster_offset & QCOW_OFLAG_COPIED) {
704 nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
705 &l2_table[l2_index], 0, 0);
707 cluster_offset &= ~QCOW_OFLAG_COPIED;
708 m->nb_clusters = 0;
710 goto out;
713 /* for the moment, multiple compressed clusters are not managed */
715 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
716 nb_clusters = 1;
718 /* how many available clusters ? */
720 while (i < nb_clusters) {
721 i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
722 &l2_table[l2_index], i, 0);
724 if(be64_to_cpu(l2_table[l2_index + i]))
725 break;
727 i += count_contiguous_free_clusters(nb_clusters - i,
728 &l2_table[l2_index + i]);
730 cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
732 if ((cluster_offset & QCOW_OFLAG_COPIED) ||
733 (cluster_offset & QCOW_OFLAG_COMPRESSED))
734 break;
736 nb_clusters = i;
739 * Check if there already is an AIO write request in flight which allocates
740 * the same cluster. In this case we need to wait until the previous
741 * request has completed and updated the L2 table accordingly.
743 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
745 uint64_t end_offset = offset + nb_clusters * s->cluster_size;
746 uint64_t old_offset = old_alloc->offset;
747 uint64_t old_end_offset = old_alloc->offset +
748 old_alloc->nb_clusters * s->cluster_size;
750 if (end_offset < old_offset || offset > old_end_offset) {
751 /* No intersection */
752 } else {
753 if (offset < old_offset) {
754 /* Stop at the start of a running allocation */
755 nb_clusters = (old_offset - offset) >> s->cluster_bits;
756 } else {
757 nb_clusters = 0;
760 if (nb_clusters == 0) {
761 /* Set dependency and wait for a callback */
762 m->depends_on = old_alloc;
763 m->nb_clusters = 0;
764 *num = 0;
765 return 0;
770 if (!nb_clusters) {
771 abort();
774 QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
776 /* allocate a new cluster */
778 cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
780 /* save info needed for meta data update */
781 m->offset = offset;
782 m->n_start = n_start;
783 m->nb_clusters = nb_clusters;
785 out:
786 m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
788 *num = m->nb_available - n_start;
790 return cluster_offset;
793 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
794 const uint8_t *buf, int buf_size)
796 z_stream strm1, *strm = &strm1;
797 int ret, out_len;
799 memset(strm, 0, sizeof(*strm));
801 strm->next_in = (uint8_t *)buf;
802 strm->avail_in = buf_size;
803 strm->next_out = out_buf;
804 strm->avail_out = out_buf_size;
806 ret = inflateInit2(strm, -12);
807 if (ret != Z_OK)
808 return -1;
809 ret = inflate(strm, Z_FINISH);
810 out_len = strm->next_out - out_buf;
811 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
812 out_len != out_buf_size) {
813 inflateEnd(strm);
814 return -1;
816 inflateEnd(strm);
817 return 0;
820 int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
822 int ret, csize, nb_csectors, sector_offset;
823 uint64_t coffset;
825 coffset = cluster_offset & s->cluster_offset_mask;
826 if (s->cluster_cache_offset != coffset) {
827 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
828 sector_offset = coffset & 511;
829 csize = nb_csectors * 512 - sector_offset;
830 ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
831 if (ret < 0) {
832 return -1;
834 if (decompress_buffer(s->cluster_cache, s->cluster_size,
835 s->cluster_data + sector_offset, csize) < 0) {
836 return -1;
838 s->cluster_cache_offset = coffset;
840 return 0;