lsi53c895a: avoid a write only variable
[qemu/cris-port.git] / block / qcow2-cluster.c
blobfb4224a669ebeeff8e8b6545008aaf93fd6e2f0f
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 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
58 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
59 if (new_l1_table_offset < 0) {
60 qemu_free(new_l1_table);
61 return new_l1_table_offset;
63 bdrv_flush(bs->file);
65 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
66 for(i = 0; i < s->l1_size; i++)
67 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
68 ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_table, new_l1_size2);
69 if (ret < 0)
70 goto fail;
71 for(i = 0; i < s->l1_size; i++)
72 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
74 /* set new table */
75 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
76 cpu_to_be32w((uint32_t*)data, new_l1_size);
77 cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
78 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size), data,sizeof(data));
79 if (ret < 0) {
80 goto fail;
82 qemu_free(s->l1_table);
83 qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
84 s->l1_table_offset = new_l1_table_offset;
85 s->l1_table = new_l1_table;
86 s->l1_size = new_l1_size;
87 return 0;
88 fail:
89 qemu_free(new_l1_table);
90 qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2);
91 return ret;
94 void qcow2_l2_cache_reset(BlockDriverState *bs)
96 BDRVQcowState *s = bs->opaque;
98 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
99 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
100 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
103 static inline int l2_cache_new_entry(BlockDriverState *bs)
105 BDRVQcowState *s = bs->opaque;
106 uint32_t min_count;
107 int min_index, i;
109 /* find a new entry in the least used one */
110 min_index = 0;
111 min_count = 0xffffffff;
112 for(i = 0; i < L2_CACHE_SIZE; i++) {
113 if (s->l2_cache_counts[i] < min_count) {
114 min_count = s->l2_cache_counts[i];
115 min_index = i;
118 return min_index;
122 * seek_l2_table
124 * seek l2_offset in the l2_cache table
125 * if not found, return NULL,
126 * if found,
127 * increments the l2 cache hit count of the entry,
128 * if counter overflow, divide by two all counters
129 * return the pointer to the l2 cache entry
133 static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
135 int i, j;
137 for(i = 0; i < L2_CACHE_SIZE; i++) {
138 if (l2_offset == s->l2_cache_offsets[i]) {
139 /* increment the hit count */
140 if (++s->l2_cache_counts[i] == 0xffffffff) {
141 for(j = 0; j < L2_CACHE_SIZE; j++) {
142 s->l2_cache_counts[j] >>= 1;
145 return s->l2_cache + (i << s->l2_bits);
148 return NULL;
152 * l2_load
154 * Loads a L2 table into memory. If the table is in the cache, the cache
155 * is used; otherwise the L2 table is loaded from the image file.
157 * Returns a pointer to the L2 table on success, or NULL if the read from
158 * the image file failed.
161 static int l2_load(BlockDriverState *bs, uint64_t l2_offset,
162 uint64_t **l2_table)
164 BDRVQcowState *s = bs->opaque;
165 int min_index;
166 int ret;
168 /* seek if the table for the given offset is in the cache */
170 *l2_table = seek_l2_table(s, l2_offset);
171 if (*l2_table != NULL) {
172 return 0;
175 /* not found: load a new entry in the least used one */
177 min_index = l2_cache_new_entry(bs);
178 *l2_table = s->l2_cache + (min_index << s->l2_bits);
180 BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
181 ret = bdrv_pread(bs->file, l2_offset, *l2_table,
182 s->l2_size * sizeof(uint64_t));
183 if (ret < 0) {
184 return ret;
187 s->l2_cache_offsets[min_index] = l2_offset;
188 s->l2_cache_counts[min_index] = 1;
190 return 0;
194 * Writes one sector of the L1 table to the disk (can't update single entries
195 * and we really don't want bdrv_pread to perform a read-modify-write)
197 #define L1_ENTRIES_PER_SECTOR (512 / 8)
198 static int write_l1_entry(BlockDriverState *bs, int l1_index)
200 BDRVQcowState *s = bs->opaque;
201 uint64_t buf[L1_ENTRIES_PER_SECTOR];
202 int l1_start_index;
203 int i, ret;
205 l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
206 for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) {
207 buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
210 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
211 ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset + 8 * l1_start_index,
212 buf, sizeof(buf));
213 if (ret < 0) {
214 return ret;
217 return 0;
221 * l2_allocate
223 * Allocate a new l2 entry in the file. If l1_index points to an already
224 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
225 * table) copy the contents of the old L2 table into the newly allocated one.
226 * Otherwise the new table is initialized with zeros.
230 static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
232 BDRVQcowState *s = bs->opaque;
233 int min_index;
234 uint64_t old_l2_offset;
235 uint64_t *l2_table;
236 int64_t l2_offset;
237 int ret;
239 old_l2_offset = s->l1_table[l1_index];
241 /* allocate a new l2 entry */
243 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
244 if (l2_offset < 0) {
245 return l2_offset;
247 bdrv_flush(bs->file);
249 /* allocate a new entry in the l2 cache */
251 min_index = l2_cache_new_entry(bs);
252 l2_table = s->l2_cache + (min_index << s->l2_bits);
254 if (old_l2_offset == 0) {
255 /* if there was no old l2 table, clear the new table */
256 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
257 } else {
258 /* if there was an old l2 table, read it from the disk */
259 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
260 ret = bdrv_pread(bs->file, old_l2_offset, l2_table,
261 s->l2_size * sizeof(uint64_t));
262 if (ret < 0) {
263 goto fail;
266 /* write the l2 table to the file */
267 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
268 ret = bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
269 s->l2_size * sizeof(uint64_t));
270 if (ret < 0) {
271 goto fail;
274 /* update the L1 entry */
275 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
276 ret = write_l1_entry(bs, l1_index);
277 if (ret < 0) {
278 goto fail;
281 /* update the l2 cache entry */
283 s->l2_cache_offsets[min_index] = l2_offset;
284 s->l2_cache_counts[min_index] = 1;
286 *table = l2_table;
287 return 0;
289 fail:
290 s->l1_table[l1_index] = old_l2_offset;
291 qcow2_l2_cache_reset(bs);
292 return ret;
295 static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
296 uint64_t *l2_table, uint64_t start, uint64_t mask)
298 int i;
299 uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
301 if (!offset)
302 return 0;
304 for (i = start; i < start + nb_clusters; i++)
305 if (offset + (uint64_t) i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
306 break;
308 return (i - start);
311 static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
313 int i = 0;
315 while(nb_clusters-- && l2_table[i] == 0)
316 i++;
318 return i;
321 /* The crypt function is compatible with the linux cryptoloop
322 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
323 supported */
324 void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
325 uint8_t *out_buf, const uint8_t *in_buf,
326 int nb_sectors, int enc,
327 const AES_KEY *key)
329 union {
330 uint64_t ll[2];
331 uint8_t b[16];
332 } ivec;
333 int i;
335 for(i = 0; i < nb_sectors; i++) {
336 ivec.ll[0] = cpu_to_le64(sector_num);
337 ivec.ll[1] = 0;
338 AES_cbc_encrypt(in_buf, out_buf, 512, key,
339 ivec.b, enc);
340 sector_num++;
341 in_buf += 512;
342 out_buf += 512;
347 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
348 uint8_t *buf, int nb_sectors)
350 BDRVQcowState *s = bs->opaque;
351 int ret, index_in_cluster, n, n1;
352 uint64_t cluster_offset;
353 struct iovec iov;
354 QEMUIOVector qiov;
356 while (nb_sectors > 0) {
357 n = nb_sectors;
359 ret = qcow2_get_cluster_offset(bs, sector_num << 9, &n,
360 &cluster_offset);
361 if (ret < 0) {
362 return ret;
365 index_in_cluster = sector_num & (s->cluster_sectors - 1);
366 if (!cluster_offset) {
367 if (bs->backing_hd) {
368 /* read from the base image */
369 iov.iov_base = buf;
370 iov.iov_len = n * 512;
371 qemu_iovec_init_external(&qiov, &iov, 1);
373 n1 = qcow2_backing_read1(bs->backing_hd, &qiov, sector_num, n);
374 if (n1 > 0) {
375 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING);
376 ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
377 if (ret < 0)
378 return -1;
380 } else {
381 memset(buf, 0, 512 * n);
383 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
384 if (qcow2_decompress_cluster(bs, cluster_offset) < 0)
385 return -1;
386 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
387 } else {
388 BLKDBG_EVENT(bs->file, BLKDBG_READ);
389 ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);
390 if (ret != n * 512)
391 return -1;
392 if (s->crypt_method) {
393 qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
394 &s->aes_decrypt_key);
397 nb_sectors -= n;
398 sector_num += n;
399 buf += n * 512;
401 return 0;
404 static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
405 uint64_t cluster_offset, int n_start, int n_end)
407 BDRVQcowState *s = bs->opaque;
408 int n, ret;
410 n = n_end - n_start;
411 if (n <= 0)
412 return 0;
413 BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
414 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
415 if (ret < 0)
416 return ret;
417 if (s->crypt_method) {
418 qcow2_encrypt_sectors(s, start_sect + n_start,
419 s->cluster_data,
420 s->cluster_data, n, 1,
421 &s->aes_encrypt_key);
423 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
424 ret = bdrv_write(bs->file, (cluster_offset >> 9) + n_start,
425 s->cluster_data, n);
426 if (ret < 0)
427 return ret;
428 return 0;
433 * get_cluster_offset
435 * For a given offset of the disk image, find the cluster offset in
436 * qcow2 file. The offset is stored in *cluster_offset.
438 * on entry, *num is the number of contiguous clusters we'd like to
439 * access following offset.
441 * on exit, *num is the number of contiguous clusters we can read.
443 * Return 0, if the offset is found
444 * Return -errno, otherwise.
448 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
449 int *num, uint64_t *cluster_offset)
451 BDRVQcowState *s = bs->opaque;
452 unsigned int l1_index, l2_index;
453 uint64_t l2_offset, *l2_table;
454 int l1_bits, c;
455 unsigned int index_in_cluster, nb_clusters;
456 uint64_t nb_available, nb_needed;
457 int ret;
459 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
460 nb_needed = *num + index_in_cluster;
462 l1_bits = s->l2_bits + s->cluster_bits;
464 /* compute how many bytes there are between the offset and
465 * the end of the l1 entry
468 nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
470 /* compute the number of available sectors */
472 nb_available = (nb_available >> 9) + index_in_cluster;
474 if (nb_needed > nb_available) {
475 nb_needed = nb_available;
478 *cluster_offset = 0;
480 /* seek the the l2 offset in the l1 table */
482 l1_index = offset >> l1_bits;
483 if (l1_index >= s->l1_size)
484 goto out;
486 l2_offset = s->l1_table[l1_index];
488 /* seek the l2 table of the given l2 offset */
490 if (!l2_offset)
491 goto out;
493 /* load the l2 table in memory */
495 l2_offset &= ~QCOW_OFLAG_COPIED;
496 ret = l2_load(bs, l2_offset, &l2_table);
497 if (ret < 0) {
498 return ret;
501 /* find the cluster offset for the given disk offset */
503 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
504 *cluster_offset = be64_to_cpu(l2_table[l2_index]);
505 nb_clusters = size_to_clusters(s, nb_needed << 9);
507 if (!*cluster_offset) {
508 /* how many empty clusters ? */
509 c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
510 } else {
511 /* how many allocated clusters ? */
512 c = count_contiguous_clusters(nb_clusters, s->cluster_size,
513 &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
516 nb_available = (c * s->cluster_sectors);
517 out:
518 if (nb_available > nb_needed)
519 nb_available = nb_needed;
521 *num = nb_available - index_in_cluster;
523 *cluster_offset &=~QCOW_OFLAG_COPIED;
524 return 0;
528 * get_cluster_table
530 * for a given disk offset, load (and allocate if needed)
531 * the l2 table.
533 * the l2 table offset in the qcow2 file and the cluster index
534 * in the l2 table are given to the caller.
536 * Returns 0 on success, -errno in failure case
538 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
539 uint64_t **new_l2_table,
540 uint64_t *new_l2_offset,
541 int *new_l2_index)
543 BDRVQcowState *s = bs->opaque;
544 unsigned int l1_index, l2_index;
545 uint64_t l2_offset;
546 uint64_t *l2_table = NULL;
547 int ret;
549 /* seek the the l2 offset in the l1 table */
551 l1_index = offset >> (s->l2_bits + s->cluster_bits);
552 if (l1_index >= s->l1_size) {
553 ret = qcow2_grow_l1_table(bs, l1_index + 1);
554 if (ret < 0) {
555 return ret;
558 l2_offset = s->l1_table[l1_index];
560 /* seek the l2 table of the given l2 offset */
562 if (l2_offset & QCOW_OFLAG_COPIED) {
563 /* load the l2 table in memory */
564 l2_offset &= ~QCOW_OFLAG_COPIED;
565 ret = l2_load(bs, l2_offset, &l2_table);
566 if (ret < 0) {
567 return ret;
569 } else {
570 if (l2_offset)
571 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
572 ret = l2_allocate(bs, l1_index, &l2_table);
573 if (ret < 0) {
574 return ret;
576 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
579 /* find the cluster offset for the given disk offset */
581 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
583 *new_l2_table = l2_table;
584 *new_l2_offset = l2_offset;
585 *new_l2_index = l2_index;
587 return 0;
591 * alloc_compressed_cluster_offset
593 * For a given offset of the disk image, return cluster offset in
594 * qcow2 file.
596 * If the offset is not found, allocate a new compressed cluster.
598 * Return the cluster offset if successful,
599 * Return 0, otherwise.
603 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
604 uint64_t offset,
605 int compressed_size)
607 BDRVQcowState *s = bs->opaque;
608 int l2_index, ret;
609 uint64_t l2_offset, *l2_table;
610 int64_t cluster_offset;
611 int nb_csectors;
613 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
614 if (ret < 0) {
615 return 0;
618 cluster_offset = be64_to_cpu(l2_table[l2_index]);
619 if (cluster_offset & QCOW_OFLAG_COPIED)
620 return cluster_offset & ~QCOW_OFLAG_COPIED;
622 if (cluster_offset)
623 qcow2_free_any_clusters(bs, cluster_offset, 1);
625 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
626 if (cluster_offset < 0) {
627 return 0;
630 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
631 (cluster_offset >> 9);
633 cluster_offset |= QCOW_OFLAG_COMPRESSED |
634 ((uint64_t)nb_csectors << s->csize_shift);
636 /* update L2 table */
638 /* compressed clusters never have the copied flag */
640 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
641 l2_table[l2_index] = cpu_to_be64(cluster_offset);
642 if (bdrv_pwrite_sync(bs->file,
643 l2_offset + l2_index * sizeof(uint64_t),
644 l2_table + l2_index,
645 sizeof(uint64_t)) < 0)
646 return 0;
648 return cluster_offset;
652 * Write L2 table updates to disk, writing whole sectors to avoid a
653 * read-modify-write in bdrv_pwrite
655 #define L2_ENTRIES_PER_SECTOR (512 / 8)
656 static int write_l2_entries(BlockDriverState *bs, uint64_t *l2_table,
657 uint64_t l2_offset, int l2_index, int num)
659 int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1);
660 int start_offset = (8 * l2_index) & ~511;
661 int end_offset = (8 * (l2_index + num) + 511) & ~511;
662 size_t len = end_offset - start_offset;
663 int ret;
665 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
666 ret = bdrv_pwrite(bs->file, l2_offset + start_offset,
667 &l2_table[l2_start_index], len);
668 if (ret < 0) {
669 return ret;
672 return 0;
675 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
677 BDRVQcowState *s = bs->opaque;
678 int i, j = 0, l2_index, ret;
679 uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
680 uint64_t cluster_offset = m->cluster_offset;
682 if (m->nb_clusters == 0)
683 return 0;
685 old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
687 /* copy content of unmodified sectors */
688 start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
689 if (m->n_start) {
690 ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
691 if (ret < 0)
692 goto err;
695 if (m->nb_available & (s->cluster_sectors - 1)) {
696 uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
697 ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
698 m->nb_available - end, s->cluster_sectors);
699 if (ret < 0)
700 goto err;
703 /* update L2 table */
704 ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index);
705 if (ret < 0) {
706 goto err;
709 for (i = 0; i < m->nb_clusters; i++) {
710 /* if two concurrent writes happen to the same unallocated cluster
711 * each write allocates separate cluster and writes data concurrently.
712 * The first one to complete updates l2 table with pointer to its
713 * cluster the second one has to do RMW (which is done above by
714 * copy_sectors()), update l2 table with its cluster pointer and free
715 * old cluster. This is what this loop does */
716 if(l2_table[l2_index + i] != 0)
717 old_cluster[j++] = l2_table[l2_index + i];
719 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
720 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
724 * Before we update the L2 table to actually point to the new cluster, we
725 * need to be sure that the refcounts have been increased and COW was
726 * handled.
728 bdrv_flush(bs->file);
730 ret = write_l2_entries(bs, l2_table, l2_offset, l2_index, m->nb_clusters);
731 if (ret < 0) {
732 qcow2_l2_cache_reset(bs);
733 goto err;
737 * If this was a COW, we need to decrease the refcount of the old cluster.
738 * Also flush bs->file to get the right order for L2 and refcount update.
740 if (j != 0) {
741 bdrv_flush(bs->file);
742 for (i = 0; i < j; i++) {
743 qcow2_free_any_clusters(bs,
744 be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
748 ret = 0;
749 err:
750 qemu_free(old_cluster);
751 return ret;
755 * alloc_cluster_offset
757 * For a given offset of the disk image, return cluster offset in qcow2 file.
758 * If the offset is not found, allocate a new cluster.
760 * If the cluster was already allocated, m->nb_clusters is set to 0,
761 * m->depends_on is set to NULL and the other fields in m are meaningless.
763 * If the cluster is newly allocated, m->nb_clusters is set to the number of
764 * contiguous clusters that have been allocated. This may be 0 if the request
765 * conflict with another write request in flight; in this case, m->depends_on
766 * is set and the remaining fields of m are meaningless.
768 * If m->nb_clusters is non-zero, the other fields of m are valid and contain
769 * information about the first allocated cluster.
771 * Return 0 on success and -errno in error cases
773 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
774 int n_start, int n_end, int *num, QCowL2Meta *m)
776 BDRVQcowState *s = bs->opaque;
777 int l2_index, ret;
778 uint64_t l2_offset, *l2_table;
779 int64_t cluster_offset;
780 unsigned int nb_clusters, i = 0;
781 QCowL2Meta *old_alloc;
783 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
784 if (ret < 0) {
785 return ret;
788 nb_clusters = size_to_clusters(s, n_end << 9);
790 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
792 cluster_offset = be64_to_cpu(l2_table[l2_index]);
794 /* We keep all QCOW_OFLAG_COPIED clusters */
796 if (cluster_offset & QCOW_OFLAG_COPIED) {
797 nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
798 &l2_table[l2_index], 0, 0);
800 cluster_offset &= ~QCOW_OFLAG_COPIED;
801 m->nb_clusters = 0;
802 m->depends_on = NULL;
804 goto out;
807 /* for the moment, multiple compressed clusters are not managed */
809 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
810 nb_clusters = 1;
812 /* how many available clusters ? */
814 while (i < nb_clusters) {
815 i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
816 &l2_table[l2_index], i, 0);
817 if ((i >= nb_clusters) || be64_to_cpu(l2_table[l2_index + i])) {
818 break;
821 i += count_contiguous_free_clusters(nb_clusters - i,
822 &l2_table[l2_index + i]);
823 if (i >= nb_clusters) {
824 break;
827 cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
829 if ((cluster_offset & QCOW_OFLAG_COPIED) ||
830 (cluster_offset & QCOW_OFLAG_COMPRESSED))
831 break;
833 assert(i <= nb_clusters);
834 nb_clusters = i;
837 * Check if there already is an AIO write request in flight which allocates
838 * the same cluster. In this case we need to wait until the previous
839 * request has completed and updated the L2 table accordingly.
841 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
843 uint64_t end_offset = offset + nb_clusters * s->cluster_size;
844 uint64_t old_offset = old_alloc->offset;
845 uint64_t old_end_offset = old_alloc->offset +
846 old_alloc->nb_clusters * s->cluster_size;
848 if (end_offset < old_offset || offset > old_end_offset) {
849 /* No intersection */
850 } else {
851 if (offset < old_offset) {
852 /* Stop at the start of a running allocation */
853 nb_clusters = (old_offset - offset) >> s->cluster_bits;
854 } else {
855 nb_clusters = 0;
858 if (nb_clusters == 0) {
859 /* Set dependency and wait for a callback */
860 m->depends_on = old_alloc;
861 m->nb_clusters = 0;
862 *num = 0;
863 return 0;
868 if (!nb_clusters) {
869 abort();
872 QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
874 /* allocate a new cluster */
876 cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
877 if (cluster_offset < 0) {
878 QLIST_REMOVE(m, next_in_flight);
879 return cluster_offset;
882 /* save info needed for meta data update */
883 m->offset = offset;
884 m->n_start = n_start;
885 m->nb_clusters = nb_clusters;
887 out:
888 m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
889 m->cluster_offset = cluster_offset;
891 *num = m->nb_available - n_start;
893 return 0;
896 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
897 const uint8_t *buf, int buf_size)
899 z_stream strm1, *strm = &strm1;
900 int ret, out_len;
902 memset(strm, 0, sizeof(*strm));
904 strm->next_in = (uint8_t *)buf;
905 strm->avail_in = buf_size;
906 strm->next_out = out_buf;
907 strm->avail_out = out_buf_size;
909 ret = inflateInit2(strm, -12);
910 if (ret != Z_OK)
911 return -1;
912 ret = inflate(strm, Z_FINISH);
913 out_len = strm->next_out - out_buf;
914 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
915 out_len != out_buf_size) {
916 inflateEnd(strm);
917 return -1;
919 inflateEnd(strm);
920 return 0;
923 int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
925 BDRVQcowState *s = bs->opaque;
926 int ret, csize, nb_csectors, sector_offset;
927 uint64_t coffset;
929 coffset = cluster_offset & s->cluster_offset_mask;
930 if (s->cluster_cache_offset != coffset) {
931 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
932 sector_offset = coffset & 511;
933 csize = nb_csectors * 512 - sector_offset;
934 BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
935 ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data, nb_csectors);
936 if (ret < 0) {
937 return -1;
939 if (decompress_buffer(s->cluster_cache, s->cluster_size,
940 s->cluster_data + sector_offset, csize) < 0) {
941 return -1;
943 s->cluster_cache_offset = coffset;
945 return 0;