tcg/ppc32: proper setcond implementation
[qemu/mdroth.git] / block / qcow2-cluster.c
blob4e30d161a7f319c8b2d0d3861f33dd5839047573
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 int64_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);
58 if (new_l1_table_offset < 0) {
59 qemu_free(new_l1_table);
60 return new_l1_table_offset;
63 for(i = 0; i < s->l1_size; i++)
64 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
65 ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
66 if (ret != new_l1_size2)
67 goto fail;
68 for(i = 0; i < s->l1_size; i++)
69 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
71 /* set new table */
72 cpu_to_be32w((uint32_t*)data, new_l1_size);
73 cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
74 ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data,sizeof(data));
75 if (ret != sizeof(data)) {
76 goto fail;
78 qemu_free(s->l1_table);
79 qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
80 s->l1_table_offset = new_l1_table_offset;
81 s->l1_table = new_l1_table;
82 s->l1_size = new_l1_size;
83 return 0;
84 fail:
85 qemu_free(new_l1_table);
86 qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2);
87 return ret < 0 ? ret : -EIO;
90 void qcow2_l2_cache_reset(BlockDriverState *bs)
92 BDRVQcowState *s = bs->opaque;
94 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
95 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
96 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
99 static inline int l2_cache_new_entry(BlockDriverState *bs)
101 BDRVQcowState *s = bs->opaque;
102 uint32_t min_count;
103 int min_index, i;
105 /* find a new entry in the least used one */
106 min_index = 0;
107 min_count = 0xffffffff;
108 for(i = 0; i < L2_CACHE_SIZE; i++) {
109 if (s->l2_cache_counts[i] < min_count) {
110 min_count = s->l2_cache_counts[i];
111 min_index = i;
114 return min_index;
118 * seek_l2_table
120 * seek l2_offset in the l2_cache table
121 * if not found, return NULL,
122 * if found,
123 * increments the l2 cache hit count of the entry,
124 * if counter overflow, divide by two all counters
125 * return the pointer to the l2 cache entry
129 static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
131 int i, j;
133 for(i = 0; i < L2_CACHE_SIZE; i++) {
134 if (l2_offset == s->l2_cache_offsets[i]) {
135 /* increment the hit count */
136 if (++s->l2_cache_counts[i] == 0xffffffff) {
137 for(j = 0; j < L2_CACHE_SIZE; j++) {
138 s->l2_cache_counts[j] >>= 1;
141 return s->l2_cache + (i << s->l2_bits);
144 return NULL;
148 * l2_load
150 * Loads a L2 table into memory. If the table is in the cache, the cache
151 * is used; otherwise the L2 table is loaded from the image file.
153 * Returns a pointer to the L2 table on success, or NULL if the read from
154 * the image file failed.
157 static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
159 BDRVQcowState *s = bs->opaque;
160 int min_index;
161 uint64_t *l2_table;
163 /* seek if the table for the given offset is in the cache */
165 l2_table = seek_l2_table(s, l2_offset);
166 if (l2_table != NULL)
167 return l2_table;
169 /* not found: load a new entry in the least used one */
171 min_index = l2_cache_new_entry(bs);
172 l2_table = s->l2_cache + (min_index << s->l2_bits);
173 if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
174 s->l2_size * sizeof(uint64_t))
175 return NULL;
176 s->l2_cache_offsets[min_index] = l2_offset;
177 s->l2_cache_counts[min_index] = 1;
179 return l2_table;
183 * Writes one sector of the L1 table to the disk (can't update single entries
184 * and we really don't want bdrv_pread to perform a read-modify-write)
186 #define L1_ENTRIES_PER_SECTOR (512 / 8)
187 static int write_l1_entry(BDRVQcowState *s, int l1_index)
189 uint64_t buf[L1_ENTRIES_PER_SECTOR];
190 int l1_start_index;
191 int i;
193 l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
194 for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) {
195 buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
198 if (bdrv_pwrite(s->hd, s->l1_table_offset + 8 * l1_start_index,
199 buf, sizeof(buf)) != sizeof(buf))
201 return -1;
204 return 0;
208 * l2_allocate
210 * Allocate a new l2 entry in the file. If l1_index points to an already
211 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
212 * table) copy the contents of the old L2 table into the newly allocated one.
213 * Otherwise the new table is initialized with zeros.
217 static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
219 BDRVQcowState *s = bs->opaque;
220 int min_index;
221 uint64_t old_l2_offset;
222 uint64_t *l2_table, l2_offset;
224 old_l2_offset = s->l1_table[l1_index];
226 /* allocate a new l2 entry */
228 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
229 if (l2_offset < 0) {
230 return NULL;
233 /* update the L1 entry */
235 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
236 if (write_l1_entry(s, l1_index) < 0) {
237 return NULL;
240 /* allocate a new entry in the l2 cache */
242 min_index = l2_cache_new_entry(bs);
243 l2_table = s->l2_cache + (min_index << s->l2_bits);
245 if (old_l2_offset == 0) {
246 /* if there was no old l2 table, clear the new table */
247 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
248 } else {
249 /* if there was an old l2 table, read it from the disk */
250 if (bdrv_pread(s->hd, old_l2_offset,
251 l2_table, s->l2_size * sizeof(uint64_t)) !=
252 s->l2_size * sizeof(uint64_t))
253 return NULL;
255 /* write the l2 table to the file */
256 if (bdrv_pwrite(s->hd, l2_offset,
257 l2_table, s->l2_size * sizeof(uint64_t)) !=
258 s->l2_size * sizeof(uint64_t))
259 return NULL;
261 /* update the l2 cache entry */
263 s->l2_cache_offsets[min_index] = l2_offset;
264 s->l2_cache_counts[min_index] = 1;
266 return l2_table;
269 static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
270 uint64_t *l2_table, uint64_t start, uint64_t mask)
272 int i;
273 uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
275 if (!offset)
276 return 0;
278 for (i = start; i < start + nb_clusters; i++)
279 if (offset + (uint64_t) i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
280 break;
282 return (i - start);
285 static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
287 int i = 0;
289 while(nb_clusters-- && l2_table[i] == 0)
290 i++;
292 return i;
295 /* The crypt function is compatible with the linux cryptoloop
296 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
297 supported */
298 void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
299 uint8_t *out_buf, const uint8_t *in_buf,
300 int nb_sectors, int enc,
301 const AES_KEY *key)
303 union {
304 uint64_t ll[2];
305 uint8_t b[16];
306 } ivec;
307 int i;
309 for(i = 0; i < nb_sectors; i++) {
310 ivec.ll[0] = cpu_to_le64(sector_num);
311 ivec.ll[1] = 0;
312 AES_cbc_encrypt(in_buf, out_buf, 512, key,
313 ivec.b, enc);
314 sector_num++;
315 in_buf += 512;
316 out_buf += 512;
321 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
322 uint8_t *buf, int nb_sectors)
324 BDRVQcowState *s = bs->opaque;
325 int ret, index_in_cluster, n, n1;
326 uint64_t cluster_offset;
328 while (nb_sectors > 0) {
329 n = nb_sectors;
330 cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, &n);
331 index_in_cluster = sector_num & (s->cluster_sectors - 1);
332 if (!cluster_offset) {
333 if (bs->backing_hd) {
334 /* read from the base image */
335 n1 = qcow2_backing_read1(bs->backing_hd, sector_num, buf, n);
336 if (n1 > 0) {
337 ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
338 if (ret < 0)
339 return -1;
341 } else {
342 memset(buf, 0, 512 * n);
344 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
345 if (qcow2_decompress_cluster(s, cluster_offset) < 0)
346 return -1;
347 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
348 } else {
349 ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
350 if (ret != n * 512)
351 return -1;
352 if (s->crypt_method) {
353 qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
354 &s->aes_decrypt_key);
357 nb_sectors -= n;
358 sector_num += n;
359 buf += n * 512;
361 return 0;
364 static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
365 uint64_t cluster_offset, int n_start, int n_end)
367 BDRVQcowState *s = bs->opaque;
368 int n, ret;
370 n = n_end - n_start;
371 if (n <= 0)
372 return 0;
373 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
374 if (ret < 0)
375 return ret;
376 if (s->crypt_method) {
377 qcow2_encrypt_sectors(s, start_sect + n_start,
378 s->cluster_data,
379 s->cluster_data, n, 1,
380 &s->aes_encrypt_key);
382 ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
383 s->cluster_data, n);
384 if (ret < 0)
385 return ret;
386 return 0;
391 * get_cluster_offset
393 * For a given offset of the disk image, return cluster offset in
394 * qcow2 file.
396 * on entry, *num is the number of contiguous clusters we'd like to
397 * access following offset.
399 * on exit, *num is the number of contiguous clusters we can read.
401 * Return 1, if the offset is found
402 * Return 0, otherwise.
406 uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
407 int *num)
409 BDRVQcowState *s = bs->opaque;
410 unsigned int l1_index, l2_index;
411 uint64_t l2_offset, *l2_table, cluster_offset;
412 int l1_bits, c;
413 unsigned int index_in_cluster, nb_clusters;
414 uint64_t nb_available, nb_needed;
416 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
417 nb_needed = *num + index_in_cluster;
419 l1_bits = s->l2_bits + s->cluster_bits;
421 /* compute how many bytes there are between the offset and
422 * the end of the l1 entry
425 nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
427 /* compute the number of available sectors */
429 nb_available = (nb_available >> 9) + index_in_cluster;
431 if (nb_needed > nb_available) {
432 nb_needed = nb_available;
435 cluster_offset = 0;
437 /* seek the the l2 offset in the l1 table */
439 l1_index = offset >> l1_bits;
440 if (l1_index >= s->l1_size)
441 goto out;
443 l2_offset = s->l1_table[l1_index];
445 /* seek the l2 table of the given l2 offset */
447 if (!l2_offset)
448 goto out;
450 /* load the l2 table in memory */
452 l2_offset &= ~QCOW_OFLAG_COPIED;
453 l2_table = l2_load(bs, l2_offset);
454 if (l2_table == NULL)
455 return 0;
457 /* find the cluster offset for the given disk offset */
459 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
460 cluster_offset = be64_to_cpu(l2_table[l2_index]);
461 nb_clusters = size_to_clusters(s, nb_needed << 9);
463 if (!cluster_offset) {
464 /* how many empty clusters ? */
465 c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
466 } else {
467 /* how many allocated clusters ? */
468 c = count_contiguous_clusters(nb_clusters, s->cluster_size,
469 &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
472 nb_available = (c * s->cluster_sectors);
473 out:
474 if (nb_available > nb_needed)
475 nb_available = nb_needed;
477 *num = nb_available - index_in_cluster;
479 return cluster_offset & ~QCOW_OFLAG_COPIED;
483 * get_cluster_table
485 * for a given disk offset, load (and allocate if needed)
486 * the l2 table.
488 * the l2 table offset in the qcow2 file and the cluster index
489 * in the l2 table are given to the caller.
491 * Returns 0 on success, -errno in failure case
493 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
494 uint64_t **new_l2_table,
495 uint64_t *new_l2_offset,
496 int *new_l2_index)
498 BDRVQcowState *s = bs->opaque;
499 unsigned int l1_index, l2_index;
500 uint64_t l2_offset, *l2_table;
501 int ret;
503 /* seek the the l2 offset in the l1 table */
505 l1_index = offset >> (s->l2_bits + s->cluster_bits);
506 if (l1_index >= s->l1_size) {
507 ret = qcow2_grow_l1_table(bs, l1_index + 1);
508 if (ret < 0) {
509 return ret;
512 l2_offset = s->l1_table[l1_index];
514 /* seek the l2 table of the given l2 offset */
516 if (l2_offset & QCOW_OFLAG_COPIED) {
517 /* load the l2 table in memory */
518 l2_offset &= ~QCOW_OFLAG_COPIED;
519 l2_table = l2_load(bs, l2_offset);
520 if (l2_table == NULL) {
521 return -EIO;
523 } else {
524 if (l2_offset)
525 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
526 l2_table = l2_allocate(bs, l1_index);
527 if (l2_table == NULL) {
528 return -EIO;
530 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
533 /* find the cluster offset for the given disk offset */
535 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
537 *new_l2_table = l2_table;
538 *new_l2_offset = l2_offset;
539 *new_l2_index = l2_index;
541 return 0;
545 * alloc_compressed_cluster_offset
547 * For a given offset of the disk image, return cluster offset in
548 * qcow2 file.
550 * If the offset is not found, allocate a new compressed cluster.
552 * Return the cluster offset if successful,
553 * Return 0, otherwise.
557 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
558 uint64_t offset,
559 int compressed_size)
561 BDRVQcowState *s = bs->opaque;
562 int l2_index, ret;
563 uint64_t l2_offset, *l2_table, cluster_offset;
564 int nb_csectors;
566 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
567 if (ret < 0) {
568 return 0;
571 cluster_offset = be64_to_cpu(l2_table[l2_index]);
572 if (cluster_offset & QCOW_OFLAG_COPIED)
573 return cluster_offset & ~QCOW_OFLAG_COPIED;
575 if (cluster_offset)
576 qcow2_free_any_clusters(bs, cluster_offset, 1);
578 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
579 if (cluster_offset < 0) {
580 return 0;
583 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
584 (cluster_offset >> 9);
586 cluster_offset |= QCOW_OFLAG_COMPRESSED |
587 ((uint64_t)nb_csectors << s->csize_shift);
589 /* update L2 table */
591 /* compressed clusters never have the copied flag */
593 l2_table[l2_index] = cpu_to_be64(cluster_offset);
594 if (bdrv_pwrite(s->hd,
595 l2_offset + l2_index * sizeof(uint64_t),
596 l2_table + l2_index,
597 sizeof(uint64_t)) != sizeof(uint64_t))
598 return 0;
600 return cluster_offset;
604 * Write L2 table updates to disk, writing whole sectors to avoid a
605 * read-modify-write in bdrv_pwrite
607 #define L2_ENTRIES_PER_SECTOR (512 / 8)
608 static int write_l2_entries(BDRVQcowState *s, uint64_t *l2_table,
609 uint64_t l2_offset, int l2_index, int num)
611 int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1);
612 int start_offset = (8 * l2_index) & ~511;
613 int end_offset = (8 * (l2_index + num) + 511) & ~511;
614 size_t len = end_offset - start_offset;
616 if (bdrv_pwrite(s->hd, l2_offset + start_offset, &l2_table[l2_start_index],
617 len) != len)
619 return -1;
622 return 0;
625 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
627 BDRVQcowState *s = bs->opaque;
628 int i, j = 0, l2_index, ret;
629 uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
630 uint64_t cluster_offset = m->cluster_offset;
632 if (m->nb_clusters == 0)
633 return 0;
635 old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
637 /* copy content of unmodified sectors */
638 start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
639 if (m->n_start) {
640 ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
641 if (ret < 0)
642 goto err;
645 if (m->nb_available & (s->cluster_sectors - 1)) {
646 uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
647 ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
648 m->nb_available - end, s->cluster_sectors);
649 if (ret < 0)
650 goto err;
653 /* update L2 table */
654 ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index);
655 if (ret < 0) {
656 goto err;
659 for (i = 0; i < m->nb_clusters; i++) {
660 /* if two concurrent writes happen to the same unallocated cluster
661 * each write allocates separate cluster and writes data concurrently.
662 * The first one to complete updates l2 table with pointer to its
663 * cluster the second one has to do RMW (which is done above by
664 * copy_sectors()), update l2 table with its cluster pointer and free
665 * old cluster. This is what this loop does */
666 if(l2_table[l2_index + i] != 0)
667 old_cluster[j++] = l2_table[l2_index + i];
669 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
670 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
673 if (write_l2_entries(s, l2_table, l2_offset, l2_index, m->nb_clusters) < 0) {
674 ret = -1;
675 goto err;
678 for (i = 0; i < j; i++)
679 qcow2_free_any_clusters(bs,
680 be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
682 ret = 0;
683 err:
684 qemu_free(old_cluster);
685 return ret;
689 * alloc_cluster_offset
691 * For a given offset of the disk image, return cluster offset in qcow2 file.
692 * If the offset is not found, allocate a new cluster.
694 * If the cluster was already allocated, m->nb_clusters is set to 0,
695 * m->depends_on is set to NULL and the other fields in m are meaningless.
697 * If the cluster is newly allocated, m->nb_clusters is set to the number of
698 * contiguous clusters that have been allocated. This may be 0 if the request
699 * conflict with another write request in flight; in this case, m->depends_on
700 * is set and the remaining fields of m are meaningless.
702 * If m->nb_clusters is non-zero, the other fields of m are valid and contain
703 * information about the first allocated cluster.
705 * Return 0 on success and -errno in error cases
707 uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
708 uint64_t offset,
709 int n_start, int n_end,
710 int *num, QCowL2Meta *m)
712 BDRVQcowState *s = bs->opaque;
713 int l2_index, ret;
714 uint64_t l2_offset, *l2_table;
715 int64_t cluster_offset;
716 unsigned int nb_clusters, i = 0;
717 QCowL2Meta *old_alloc;
719 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
720 if (ret < 0) {
721 return ret;
724 nb_clusters = size_to_clusters(s, n_end << 9);
726 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
728 cluster_offset = be64_to_cpu(l2_table[l2_index]);
730 /* We keep all QCOW_OFLAG_COPIED clusters */
732 if (cluster_offset & QCOW_OFLAG_COPIED) {
733 nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
734 &l2_table[l2_index], 0, 0);
736 cluster_offset &= ~QCOW_OFLAG_COPIED;
737 m->nb_clusters = 0;
738 m->depends_on = NULL;
740 goto out;
743 /* for the moment, multiple compressed clusters are not managed */
745 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
746 nb_clusters = 1;
748 /* how many available clusters ? */
750 while (i < nb_clusters) {
751 i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
752 &l2_table[l2_index], i, 0);
754 if(be64_to_cpu(l2_table[l2_index + i]))
755 break;
757 i += count_contiguous_free_clusters(nb_clusters - i,
758 &l2_table[l2_index + i]);
760 cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
762 if ((cluster_offset & QCOW_OFLAG_COPIED) ||
763 (cluster_offset & QCOW_OFLAG_COMPRESSED))
764 break;
766 nb_clusters = i;
769 * Check if there already is an AIO write request in flight which allocates
770 * the same cluster. In this case we need to wait until the previous
771 * request has completed and updated the L2 table accordingly.
773 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
775 uint64_t end_offset = offset + nb_clusters * s->cluster_size;
776 uint64_t old_offset = old_alloc->offset;
777 uint64_t old_end_offset = old_alloc->offset +
778 old_alloc->nb_clusters * s->cluster_size;
780 if (end_offset < old_offset || offset > old_end_offset) {
781 /* No intersection */
782 } else {
783 if (offset < old_offset) {
784 /* Stop at the start of a running allocation */
785 nb_clusters = (old_offset - offset) >> s->cluster_bits;
786 } else {
787 nb_clusters = 0;
790 if (nb_clusters == 0) {
791 /* Set dependency and wait for a callback */
792 m->depends_on = old_alloc;
793 m->nb_clusters = 0;
794 *num = 0;
795 return 0;
800 if (!nb_clusters) {
801 abort();
804 QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
806 /* allocate a new cluster */
808 cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
809 if (cluster_offset < 0) {
810 return cluster_offset;
813 /* save info needed for meta data update */
814 m->offset = offset;
815 m->n_start = n_start;
816 m->nb_clusters = nb_clusters;
818 out:
819 m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
820 m->cluster_offset = cluster_offset;
822 *num = m->nb_available - n_start;
824 return 0;
827 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
828 const uint8_t *buf, int buf_size)
830 z_stream strm1, *strm = &strm1;
831 int ret, out_len;
833 memset(strm, 0, sizeof(*strm));
835 strm->next_in = (uint8_t *)buf;
836 strm->avail_in = buf_size;
837 strm->next_out = out_buf;
838 strm->avail_out = out_buf_size;
840 ret = inflateInit2(strm, -12);
841 if (ret != Z_OK)
842 return -1;
843 ret = inflate(strm, Z_FINISH);
844 out_len = strm->next_out - out_buf;
845 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
846 out_len != out_buf_size) {
847 inflateEnd(strm);
848 return -1;
850 inflateEnd(strm);
851 return 0;
854 int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
856 int ret, csize, nb_csectors, sector_offset;
857 uint64_t coffset;
859 coffset = cluster_offset & s->cluster_offset_mask;
860 if (s->cluster_cache_offset != coffset) {
861 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
862 sector_offset = coffset & 511;
863 csize = nb_csectors * 512 - sector_offset;
864 ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
865 if (ret < 0) {
866 return -1;
868 if (decompress_buffer(s->cluster_cache, s->cluster_size,
869 s->cluster_data + sector_offset, csize) < 0) {
870 return -1;
872 s->cluster_cache_offset = coffset;
874 return 0;