qcow2: Return 0/-errno in qcow2_alloc_cluster_offset
[qemu/aliguori-queue.git] / block / qcow2-cluster.c
blobd23d5b3aff3bcb8fb7e481b5a76d747415e88e54
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 if (new_l1_size == 0) {
43 new_l1_size = 1;
45 while (min_size > new_l1_size) {
46 new_l1_size = (new_l1_size * 3 + 1) / 2;
48 #ifdef DEBUG_ALLOC2
49 printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
50 #endif
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));
56 /* write new table (align to cluster) */
57 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
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]);
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;
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;
86 void qcow2_l2_cache_reset(BlockDriverState *bs)
88 BDRVQcowState *s = bs->opaque;
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));
95 static inline int l2_cache_new_entry(BlockDriverState *bs)
97 BDRVQcowState *s = bs->opaque;
98 uint32_t min_count;
99 int min_index, i;
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;
110 return min_index;
114 * seek_l2_table
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
125 static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
127 int i, j;
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;
137 return s->l2_cache + (i << s->l2_bits);
140 return NULL;
144 * l2_load
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.
149 * Returns a pointer to the L2 table on success, or NULL if the read from
150 * the image file failed.
153 static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
155 BDRVQcowState *s = bs->opaque;
156 int min_index;
157 uint64_t *l2_table;
159 /* seek if the table for the given offset is in the cache */
161 l2_table = seek_l2_table(s, l2_offset);
162 if (l2_table != NULL)
163 return l2_table;
165 /* not found: load a new entry in the least used one */
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;
175 return l2_table;
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)
182 #define L1_ENTRIES_PER_SECTOR (512 / 8)
183 static int write_l1_entry(BDRVQcowState *s, int l1_index)
185 uint64_t buf[L1_ENTRIES_PER_SECTOR];
186 int l1_start_index;
187 int i;
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]);
194 if (bdrv_pwrite(s->hd, s->l1_table_offset + 8 * l1_start_index,
195 buf, sizeof(buf)) != sizeof(buf))
197 return -1;
200 return 0;
204 * l2_allocate
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.
213 static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
215 BDRVQcowState *s = bs->opaque;
216 int min_index;
217 uint64_t old_l2_offset;
218 uint64_t *l2_table, l2_offset;
220 old_l2_offset = s->l1_table[l1_index];
222 /* allocate a new l2 entry */
224 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
226 /* update the L1 entry */
228 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
229 if (write_l1_entry(s, l1_index) < 0) {
230 return NULL;
233 /* allocate a new entry in the l2 cache */
235 min_index = l2_cache_new_entry(bs);
236 l2_table = s->l2_cache + (min_index << s->l2_bits);
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;
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;
254 /* update the l2 cache entry */
256 s->l2_cache_offsets[min_index] = l2_offset;
257 s->l2_cache_counts[min_index] = 1;
259 return l2_table;
262 static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
263 uint64_t *l2_table, uint64_t start, uint64_t mask)
265 int i;
266 uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
268 if (!offset)
269 return 0;
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;
275 return (i - start);
278 static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
280 int i = 0;
282 while(nb_clusters-- && l2_table[i] == 0)
283 i++;
285 return i;
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)
296 union {
297 uint64_t ll[2];
298 uint8_t b[16];
299 } ivec;
300 int i;
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;
314 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
315 uint8_t *buf, int nb_sectors)
317 BDRVQcowState *s = bs->opaque;
318 int ret, index_in_cluster, n, n1;
319 uint64_t cluster_offset;
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;
334 } else {
335 memset(buf, 0, 512 * n);
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);
350 nb_sectors -= n;
351 sector_num += n;
352 buf += n * 512;
354 return 0;
357 static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
358 uint64_t cluster_offset, int n_start, int n_end)
360 BDRVQcowState *s = bs->opaque;
361 int n, ret;
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);
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;
384 * get_cluster_offset
386 * For a given offset of the disk image, return cluster offset in
387 * qcow2 file.
389 * on entry, *num is the number of contiguous clusters we'd like to
390 * access following offset.
392 * on exit, *num is the number of contiguous clusters we can read.
394 * Return 1, if the offset is found
395 * Return 0, otherwise.
399 uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
400 int *num)
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;
409 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
410 nb_needed = *num + index_in_cluster;
412 l1_bits = s->l2_bits + s->cluster_bits;
414 /* compute how many bytes there are between the offset and
415 * the end of the l1 entry
418 nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
420 /* compute the number of available sectors */
422 nb_available = (nb_available >> 9) + index_in_cluster;
424 if (nb_needed > nb_available) {
425 nb_needed = nb_available;
428 cluster_offset = 0;
430 /* seek the the l2 offset in the l1 table */
432 l1_index = offset >> l1_bits;
433 if (l1_index >= s->l1_size)
434 goto out;
436 l2_offset = s->l1_table[l1_index];
438 /* seek the l2 table of the given l2 offset */
440 if (!l2_offset)
441 goto out;
443 /* load the l2 table in memory */
445 l2_offset &= ~QCOW_OFLAG_COPIED;
446 l2_table = l2_load(bs, l2_offset);
447 if (l2_table == NULL)
448 return 0;
450 /* find the cluster offset for the given disk offset */
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);
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);
465 nb_available = (c * s->cluster_sectors);
466 out:
467 if (nb_available > nb_needed)
468 nb_available = nb_needed;
470 *num = nb_available - index_in_cluster;
472 return cluster_offset & ~QCOW_OFLAG_COPIED;
476 * get_cluster_table
478 * for a given disk offset, load (and allocate if needed)
479 * the l2 table.
481 * the l2 table offset in the qcow2 file and the cluster index
482 * in the l2 table are given to the caller.
484 * Returns 0 on success, -errno in failure case
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)
491 BDRVQcowState *s = bs->opaque;
492 unsigned int l1_index, l2_index;
493 uint64_t l2_offset, *l2_table;
494 int ret;
496 /* seek the the l2 offset in the l1 table */
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;
505 l2_offset = s->l1_table[l1_index];
507 /* seek the l2 table of the given l2 offset */
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;
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;
523 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
526 /* find the cluster offset for the given disk offset */
528 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
530 *new_l2_table = l2_table;
531 *new_l2_offset = l2_offset;
532 *new_l2_index = l2_index;
534 return 0;
538 * alloc_compressed_cluster_offset
540 * For a given offset of the disk image, return cluster offset in
541 * qcow2 file.
543 * If the offset is not found, allocate a new compressed cluster.
545 * Return the cluster offset if successful,
546 * Return 0, otherwise.
550 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
551 uint64_t offset,
552 int compressed_size)
554 BDRVQcowState *s = bs->opaque;
555 int l2_index, ret;
556 uint64_t l2_offset, *l2_table, cluster_offset;
557 int nb_csectors;
559 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
560 if (ret < 0) {
561 return 0;
564 cluster_offset = be64_to_cpu(l2_table[l2_index]);
565 if (cluster_offset & QCOW_OFLAG_COPIED)
566 return cluster_offset & ~QCOW_OFLAG_COPIED;
568 if (cluster_offset)
569 qcow2_free_any_clusters(bs, cluster_offset, 1);
571 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
572 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
573 (cluster_offset >> 9);
575 cluster_offset |= QCOW_OFLAG_COMPRESSED |
576 ((uint64_t)nb_csectors << s->csize_shift);
578 /* update L2 table */
580 /* compressed clusters never have the copied flag */
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;
589 return cluster_offset;
593 * Write L2 table updates to disk, writing whole sectors to avoid a
594 * read-modify-write in bdrv_pwrite
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)
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;
605 if (bdrv_pwrite(s->hd, l2_offset + start_offset, &l2_table[l2_start_index],
606 len) != len)
608 return -1;
611 return 0;
614 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
616 BDRVQcowState *s = bs->opaque;
617 int i, j = 0, l2_index, ret;
618 uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
619 uint64_t cluster_offset = m->cluster_offset;
621 if (m->nb_clusters == 0)
622 return 0;
624 old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
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;
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;
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;
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];
658 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
659 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
662 if (write_l2_entries(s, l2_table, l2_offset, l2_index, m->nb_clusters) < 0) {
663 ret = -1;
664 goto err;
667 for (i = 0; i < j; i++)
668 qcow2_free_any_clusters(bs,
669 be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
671 ret = 0;
672 err:
673 qemu_free(old_cluster);
674 return ret;
678 * alloc_cluster_offset
680 * For a given offset of the disk image, return cluster offset in qcow2 file.
681 * If the offset is not found, allocate a new cluster.
683 * If the cluster was already allocated, m->nb_clusters is set to 0,
684 * m->depends_on is set to NULL and the other fields in m are meaningless.
686 * If the cluster is newly allocated, m->nb_clusters is set to the number of
687 * contiguous clusters that have been allocated. This may be 0 if the request
688 * conflict with another write request in flight; in this case, m->depends_on
689 * is set and the remaining fields of m are meaningless.
691 * If m->nb_clusters is non-zero, the other fields of m are valid and contain
692 * information about the first allocated cluster.
694 * Return 0 on success and -errno in error cases
696 uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
697 uint64_t offset,
698 int n_start, int n_end,
699 int *num, QCowL2Meta *m)
701 BDRVQcowState *s = bs->opaque;
702 int l2_index, ret;
703 uint64_t l2_offset, *l2_table, cluster_offset;
704 unsigned int nb_clusters, i = 0;
705 QCowL2Meta *old_alloc;
707 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
708 if (ret < 0) {
709 return ret;
712 nb_clusters = size_to_clusters(s, n_end << 9);
714 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
716 cluster_offset = be64_to_cpu(l2_table[l2_index]);
718 /* We keep all QCOW_OFLAG_COPIED clusters */
720 if (cluster_offset & QCOW_OFLAG_COPIED) {
721 nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
722 &l2_table[l2_index], 0, 0);
724 cluster_offset &= ~QCOW_OFLAG_COPIED;
725 m->nb_clusters = 0;
726 m->depends_on = NULL;
728 goto out;
731 /* for the moment, multiple compressed clusters are not managed */
733 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
734 nb_clusters = 1;
736 /* how many available clusters ? */
738 while (i < nb_clusters) {
739 i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
740 &l2_table[l2_index], i, 0);
742 if(be64_to_cpu(l2_table[l2_index + i]))
743 break;
745 i += count_contiguous_free_clusters(nb_clusters - i,
746 &l2_table[l2_index + i]);
748 cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
750 if ((cluster_offset & QCOW_OFLAG_COPIED) ||
751 (cluster_offset & QCOW_OFLAG_COMPRESSED))
752 break;
754 nb_clusters = i;
757 * Check if there already is an AIO write request in flight which allocates
758 * the same cluster. In this case we need to wait until the previous
759 * request has completed and updated the L2 table accordingly.
761 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
763 uint64_t end_offset = offset + nb_clusters * s->cluster_size;
764 uint64_t old_offset = old_alloc->offset;
765 uint64_t old_end_offset = old_alloc->offset +
766 old_alloc->nb_clusters * s->cluster_size;
768 if (end_offset < old_offset || offset > old_end_offset) {
769 /* No intersection */
770 } else {
771 if (offset < old_offset) {
772 /* Stop at the start of a running allocation */
773 nb_clusters = (old_offset - offset) >> s->cluster_bits;
774 } else {
775 nb_clusters = 0;
778 if (nb_clusters == 0) {
779 /* Set dependency and wait for a callback */
780 m->depends_on = old_alloc;
781 m->nb_clusters = 0;
782 *num = 0;
783 return 0;
788 if (!nb_clusters) {
789 abort();
792 QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
794 /* allocate a new cluster */
796 cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
798 /* save info needed for meta data update */
799 m->offset = offset;
800 m->n_start = n_start;
801 m->nb_clusters = nb_clusters;
803 out:
804 m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
805 m->cluster_offset = cluster_offset;
807 *num = m->nb_available - n_start;
809 return 0;
812 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
813 const uint8_t *buf, int buf_size)
815 z_stream strm1, *strm = &strm1;
816 int ret, out_len;
818 memset(strm, 0, sizeof(*strm));
820 strm->next_in = (uint8_t *)buf;
821 strm->avail_in = buf_size;
822 strm->next_out = out_buf;
823 strm->avail_out = out_buf_size;
825 ret = inflateInit2(strm, -12);
826 if (ret != Z_OK)
827 return -1;
828 ret = inflate(strm, Z_FINISH);
829 out_len = strm->next_out - out_buf;
830 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
831 out_len != out_buf_size) {
832 inflateEnd(strm);
833 return -1;
835 inflateEnd(strm);
836 return 0;
839 int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
841 int ret, csize, nb_csectors, sector_offset;
842 uint64_t coffset;
844 coffset = cluster_offset & s->cluster_offset_mask;
845 if (s->cluster_cache_offset != coffset) {
846 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
847 sector_offset = coffset & 511;
848 csize = nb_csectors * 512 - sector_offset;
849 ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
850 if (ret < 0) {
851 return -1;
853 if (decompress_buffer(s->cluster_cache, s->cluster_size,
854 s->cluster_data + sector_offset, csize) < 0) {
855 return -1;
857 s->cluster_cache_offset = coffset;
859 return 0;