]> git.proxmox.com Git - mirror_qemu.git/blob - block/qcow2-cluster.c
99215fa8564167b40bdaaedac0bf6061e1d5f962
[mirror_qemu.git] / block / qcow2-cluster.c
1 /*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include <zlib.h>
26
27 #include "qemu-common.h"
28 #include "block_int.h"
29 #include "block/qcow2.h"
30
31 int qcow2_grow_l1_table(BlockDriverState *bs, int min_size)
32 {
33 BDRVQcowState *s = bs->opaque;
34 int new_l1_size, new_l1_size2, ret, i;
35 uint64_t *new_l1_table;
36 uint64_t new_l1_table_offset;
37 uint8_t data[12];
38
39 new_l1_size = s->l1_size;
40 if (min_size <= new_l1_size)
41 return 0;
42 while (min_size > new_l1_size) {
43 new_l1_size = (new_l1_size * 3 + 1) / 2;
44 }
45 #ifdef DEBUG_ALLOC2
46 printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
47 #endif
48
49 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
50 new_l1_table = qemu_mallocz(new_l1_size2);
51 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
52
53 /* write new table (align to cluster) */
54 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
55
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]);
63
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;
79 }
80
81 void qcow2_l2_cache_reset(BlockDriverState *bs)
82 {
83 BDRVQcowState *s = bs->opaque;
84
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));
88 }
89
90 static inline int l2_cache_new_entry(BlockDriverState *bs)
91 {
92 BDRVQcowState *s = bs->opaque;
93 uint32_t min_count;
94 int min_index, i;
95
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;
103 }
104 }
105 return min_index;
106 }
107
108 /*
109 * seek_l2_table
110 *
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
117 *
118 */
119
120 static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
121 {
122 int i, j;
123
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;
130 }
131 }
132 return s->l2_cache + (i << s->l2_bits);
133 }
134 }
135 return NULL;
136 }
137
138 /*
139 * l2_load
140 *
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.
143 *
144 * Returns a pointer to the L2 table on success, or NULL if the read from
145 * the image file failed.
146 */
147
148 static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
149 {
150 BDRVQcowState *s = bs->opaque;
151 int min_index;
152 uint64_t *l2_table;
153
154 /* seek if the table for the given offset is in the cache */
155
156 l2_table = seek_l2_table(s, l2_offset);
157 if (l2_table != NULL)
158 return l2_table;
159
160 /* not found: load a new entry in the least used one */
161
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;
169
170 return l2_table;
171 }
172
173 /*
174 * l2_allocate
175 *
176 * Allocate a new l2 entry in the file. If l1_index points to an already
177 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
178 * table) copy the contents of the old L2 table into the newly allocated one.
179 * Otherwise the new table is initialized with zeros.
180 *
181 */
182
183 static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
184 {
185 BDRVQcowState *s = bs->opaque;
186 int min_index;
187 uint64_t old_l2_offset, tmp;
188 uint64_t *l2_table, l2_offset;
189
190 old_l2_offset = s->l1_table[l1_index];
191
192 /* allocate a new l2 entry */
193
194 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
195
196 /* update the L1 entry */
197
198 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
199
200 tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED);
201 if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),
202 &tmp, sizeof(tmp)) != sizeof(tmp))
203 return NULL;
204
205 /* allocate a new entry in the l2 cache */
206
207 min_index = l2_cache_new_entry(bs);
208 l2_table = s->l2_cache + (min_index << s->l2_bits);
209
210 if (old_l2_offset == 0) {
211 /* if there was no old l2 table, clear the new table */
212 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
213 } else {
214 /* if there was an old l2 table, read it from the disk */
215 if (bdrv_pread(s->hd, old_l2_offset,
216 l2_table, s->l2_size * sizeof(uint64_t)) !=
217 s->l2_size * sizeof(uint64_t))
218 return NULL;
219 }
220 /* write the l2 table to the file */
221 if (bdrv_pwrite(s->hd, l2_offset,
222 l2_table, s->l2_size * sizeof(uint64_t)) !=
223 s->l2_size * sizeof(uint64_t))
224 return NULL;
225
226 /* update the l2 cache entry */
227
228 s->l2_cache_offsets[min_index] = l2_offset;
229 s->l2_cache_counts[min_index] = 1;
230
231 return l2_table;
232 }
233
234 static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
235 uint64_t *l2_table, uint64_t start, uint64_t mask)
236 {
237 int i;
238 uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
239
240 if (!offset)
241 return 0;
242
243 for (i = start; i < start + nb_clusters; i++)
244 if (offset + i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
245 break;
246
247 return (i - start);
248 }
249
250 static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
251 {
252 int i = 0;
253
254 while(nb_clusters-- && l2_table[i] == 0)
255 i++;
256
257 return i;
258 }
259
260 /* The crypt function is compatible with the linux cryptoloop
261 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
262 supported */
263 void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
264 uint8_t *out_buf, const uint8_t *in_buf,
265 int nb_sectors, int enc,
266 const AES_KEY *key)
267 {
268 union {
269 uint64_t ll[2];
270 uint8_t b[16];
271 } ivec;
272 int i;
273
274 for(i = 0; i < nb_sectors; i++) {
275 ivec.ll[0] = cpu_to_le64(sector_num);
276 ivec.ll[1] = 0;
277 AES_cbc_encrypt(in_buf, out_buf, 512, key,
278 ivec.b, enc);
279 sector_num++;
280 in_buf += 512;
281 out_buf += 512;
282 }
283 }
284
285
286 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
287 uint8_t *buf, int nb_sectors)
288 {
289 BDRVQcowState *s = bs->opaque;
290 int ret, index_in_cluster, n, n1;
291 uint64_t cluster_offset;
292
293 while (nb_sectors > 0) {
294 n = nb_sectors;
295 cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, &n);
296 index_in_cluster = sector_num & (s->cluster_sectors - 1);
297 if (!cluster_offset) {
298 if (bs->backing_hd) {
299 /* read from the base image */
300 n1 = qcow2_backing_read1(bs->backing_hd, sector_num, buf, n);
301 if (n1 > 0) {
302 ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
303 if (ret < 0)
304 return -1;
305 }
306 } else {
307 memset(buf, 0, 512 * n);
308 }
309 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
310 if (qcow2_decompress_cluster(s, cluster_offset) < 0)
311 return -1;
312 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
313 } else {
314 ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
315 if (ret != n * 512)
316 return -1;
317 if (s->crypt_method) {
318 qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
319 &s->aes_decrypt_key);
320 }
321 }
322 nb_sectors -= n;
323 sector_num += n;
324 buf += n * 512;
325 }
326 return 0;
327 }
328
329 static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
330 uint64_t cluster_offset, int n_start, int n_end)
331 {
332 BDRVQcowState *s = bs->opaque;
333 int n, ret;
334
335 n = n_end - n_start;
336 if (n <= 0)
337 return 0;
338 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
339 if (ret < 0)
340 return ret;
341 if (s->crypt_method) {
342 qcow2_encrypt_sectors(s, start_sect + n_start,
343 s->cluster_data,
344 s->cluster_data, n, 1,
345 &s->aes_encrypt_key);
346 }
347 ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
348 s->cluster_data, n);
349 if (ret < 0)
350 return ret;
351 return 0;
352 }
353
354
355 /*
356 * get_cluster_offset
357 *
358 * For a given offset of the disk image, return cluster offset in
359 * qcow2 file.
360 *
361 * on entry, *num is the number of contiguous clusters we'd like to
362 * access following offset.
363 *
364 * on exit, *num is the number of contiguous clusters we can read.
365 *
366 * Return 1, if the offset is found
367 * Return 0, otherwise.
368 *
369 */
370
371 uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
372 int *num)
373 {
374 BDRVQcowState *s = bs->opaque;
375 int l1_index, l2_index;
376 uint64_t l2_offset, *l2_table, cluster_offset;
377 int l1_bits, c;
378 int index_in_cluster, nb_available, nb_needed, nb_clusters;
379
380 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
381 nb_needed = *num + index_in_cluster;
382
383 l1_bits = s->l2_bits + s->cluster_bits;
384
385 /* compute how many bytes there are between the offset and
386 * the end of the l1 entry
387 */
388
389 nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1));
390
391 /* compute the number of available sectors */
392
393 nb_available = (nb_available >> 9) + index_in_cluster;
394
395 if (nb_needed > nb_available) {
396 nb_needed = nb_available;
397 }
398
399 cluster_offset = 0;
400
401 /* seek the the l2 offset in the l1 table */
402
403 l1_index = offset >> l1_bits;
404 if (l1_index >= s->l1_size)
405 goto out;
406
407 l2_offset = s->l1_table[l1_index];
408
409 /* seek the l2 table of the given l2 offset */
410
411 if (!l2_offset)
412 goto out;
413
414 /* load the l2 table in memory */
415
416 l2_offset &= ~QCOW_OFLAG_COPIED;
417 l2_table = l2_load(bs, l2_offset);
418 if (l2_table == NULL)
419 return 0;
420
421 /* find the cluster offset for the given disk offset */
422
423 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
424 cluster_offset = be64_to_cpu(l2_table[l2_index]);
425 nb_clusters = size_to_clusters(s, nb_needed << 9);
426
427 if (!cluster_offset) {
428 /* how many empty clusters ? */
429 c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
430 } else {
431 /* how many allocated clusters ? */
432 c = count_contiguous_clusters(nb_clusters, s->cluster_size,
433 &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
434 }
435
436 nb_available = (c * s->cluster_sectors);
437 out:
438 if (nb_available > nb_needed)
439 nb_available = nb_needed;
440
441 *num = nb_available - index_in_cluster;
442
443 return cluster_offset & ~QCOW_OFLAG_COPIED;
444 }
445
446 /*
447 * get_cluster_table
448 *
449 * for a given disk offset, load (and allocate if needed)
450 * the l2 table.
451 *
452 * the l2 table offset in the qcow2 file and the cluster index
453 * in the l2 table are given to the caller.
454 *
455 */
456
457 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
458 uint64_t **new_l2_table,
459 uint64_t *new_l2_offset,
460 int *new_l2_index)
461 {
462 BDRVQcowState *s = bs->opaque;
463 int l1_index, l2_index, ret;
464 uint64_t l2_offset, *l2_table;
465
466 /* seek the the l2 offset in the l1 table */
467
468 l1_index = offset >> (s->l2_bits + s->cluster_bits);
469 if (l1_index >= s->l1_size) {
470 ret = qcow2_grow_l1_table(bs, l1_index + 1);
471 if (ret < 0)
472 return 0;
473 }
474 l2_offset = s->l1_table[l1_index];
475
476 /* seek the l2 table of the given l2 offset */
477
478 if (l2_offset & QCOW_OFLAG_COPIED) {
479 /* load the l2 table in memory */
480 l2_offset &= ~QCOW_OFLAG_COPIED;
481 l2_table = l2_load(bs, l2_offset);
482 if (l2_table == NULL)
483 return 0;
484 } else {
485 if (l2_offset)
486 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
487 l2_table = l2_allocate(bs, l1_index);
488 if (l2_table == NULL)
489 return 0;
490 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
491 }
492
493 /* find the cluster offset for the given disk offset */
494
495 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
496
497 *new_l2_table = l2_table;
498 *new_l2_offset = l2_offset;
499 *new_l2_index = l2_index;
500
501 return 1;
502 }
503
504 /*
505 * alloc_compressed_cluster_offset
506 *
507 * For a given offset of the disk image, return cluster offset in
508 * qcow2 file.
509 *
510 * If the offset is not found, allocate a new compressed cluster.
511 *
512 * Return the cluster offset if successful,
513 * Return 0, otherwise.
514 *
515 */
516
517 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
518 uint64_t offset,
519 int compressed_size)
520 {
521 BDRVQcowState *s = bs->opaque;
522 int l2_index, ret;
523 uint64_t l2_offset, *l2_table, cluster_offset;
524 int nb_csectors;
525
526 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
527 if (ret == 0)
528 return 0;
529
530 cluster_offset = be64_to_cpu(l2_table[l2_index]);
531 if (cluster_offset & QCOW_OFLAG_COPIED)
532 return cluster_offset & ~QCOW_OFLAG_COPIED;
533
534 if (cluster_offset)
535 qcow2_free_any_clusters(bs, cluster_offset, 1);
536
537 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
538 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
539 (cluster_offset >> 9);
540
541 cluster_offset |= QCOW_OFLAG_COMPRESSED |
542 ((uint64_t)nb_csectors << s->csize_shift);
543
544 /* update L2 table */
545
546 /* compressed clusters never have the copied flag */
547
548 l2_table[l2_index] = cpu_to_be64(cluster_offset);
549 if (bdrv_pwrite(s->hd,
550 l2_offset + l2_index * sizeof(uint64_t),
551 l2_table + l2_index,
552 sizeof(uint64_t)) != sizeof(uint64_t))
553 return 0;
554
555 return cluster_offset;
556 }
557
558 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
559 QCowL2Meta *m)
560 {
561 BDRVQcowState *s = bs->opaque;
562 int i, j = 0, l2_index, ret;
563 uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
564
565 if (m->nb_clusters == 0)
566 return 0;
567
568 old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
569
570 /* copy content of unmodified sectors */
571 start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
572 if (m->n_start) {
573 ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
574 if (ret < 0)
575 goto err;
576 }
577
578 if (m->nb_available & (s->cluster_sectors - 1)) {
579 uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
580 ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
581 m->nb_available - end, s->cluster_sectors);
582 if (ret < 0)
583 goto err;
584 }
585
586 ret = -EIO;
587 /* update L2 table */
588 if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index))
589 goto err;
590
591 for (i = 0; i < m->nb_clusters; i++) {
592 /* if two concurrent writes happen to the same unallocated cluster
593 * each write allocates separate cluster and writes data concurrently.
594 * The first one to complete updates l2 table with pointer to its
595 * cluster the second one has to do RMW (which is done above by
596 * copy_sectors()), update l2 table with its cluster pointer and free
597 * old cluster. This is what this loop does */
598 if(l2_table[l2_index + i] != 0)
599 old_cluster[j++] = l2_table[l2_index + i];
600
601 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
602 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
603 }
604
605 if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(uint64_t),
606 l2_table + l2_index, m->nb_clusters * sizeof(uint64_t)) !=
607 m->nb_clusters * sizeof(uint64_t))
608 goto err;
609
610 for (i = 0; i < j; i++)
611 qcow2_free_any_clusters(bs,
612 be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
613
614 ret = 0;
615 err:
616 qemu_free(old_cluster);
617 return ret;
618 }
619
620 /*
621 * alloc_cluster_offset
622 *
623 * For a given offset of the disk image, return cluster offset in
624 * qcow2 file.
625 *
626 * If the offset is not found, allocate a new cluster.
627 *
628 * Return the cluster offset if successful,
629 * Return 0, otherwise.
630 *
631 */
632
633 uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
634 uint64_t offset,
635 int n_start, int n_end,
636 int *num, QCowL2Meta *m)
637 {
638 BDRVQcowState *s = bs->opaque;
639 int l2_index, ret;
640 uint64_t l2_offset, *l2_table, cluster_offset;
641 int nb_clusters, i = 0;
642
643 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
644 if (ret == 0)
645 return 0;
646
647 nb_clusters = size_to_clusters(s, n_end << 9);
648
649 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
650
651 cluster_offset = be64_to_cpu(l2_table[l2_index]);
652
653 /* We keep all QCOW_OFLAG_COPIED clusters */
654
655 if (cluster_offset & QCOW_OFLAG_COPIED) {
656 nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
657 &l2_table[l2_index], 0, 0);
658
659 cluster_offset &= ~QCOW_OFLAG_COPIED;
660 m->nb_clusters = 0;
661
662 goto out;
663 }
664
665 /* for the moment, multiple compressed clusters are not managed */
666
667 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
668 nb_clusters = 1;
669
670 /* how many available clusters ? */
671
672 while (i < nb_clusters) {
673 i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
674 &l2_table[l2_index], i, 0);
675
676 if(be64_to_cpu(l2_table[l2_index + i]))
677 break;
678
679 i += count_contiguous_free_clusters(nb_clusters - i,
680 &l2_table[l2_index + i]);
681
682 cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
683
684 if ((cluster_offset & QCOW_OFLAG_COPIED) ||
685 (cluster_offset & QCOW_OFLAG_COMPRESSED))
686 break;
687 }
688 nb_clusters = i;
689
690 /* allocate a new cluster */
691
692 cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
693
694 /* save info needed for meta data update */
695 m->offset = offset;
696 m->n_start = n_start;
697 m->nb_clusters = nb_clusters;
698
699 out:
700 m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
701
702 *num = m->nb_available - n_start;
703
704 return cluster_offset;
705 }
706
707 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
708 const uint8_t *buf, int buf_size)
709 {
710 z_stream strm1, *strm = &strm1;
711 int ret, out_len;
712
713 memset(strm, 0, sizeof(*strm));
714
715 strm->next_in = (uint8_t *)buf;
716 strm->avail_in = buf_size;
717 strm->next_out = out_buf;
718 strm->avail_out = out_buf_size;
719
720 ret = inflateInit2(strm, -12);
721 if (ret != Z_OK)
722 return -1;
723 ret = inflate(strm, Z_FINISH);
724 out_len = strm->next_out - out_buf;
725 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
726 out_len != out_buf_size) {
727 inflateEnd(strm);
728 return -1;
729 }
730 inflateEnd(strm);
731 return 0;
732 }
733
734 int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
735 {
736 int ret, csize, nb_csectors, sector_offset;
737 uint64_t coffset;
738
739 coffset = cluster_offset & s->cluster_offset_mask;
740 if (s->cluster_cache_offset != coffset) {
741 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
742 sector_offset = coffset & 511;
743 csize = nb_csectors * 512 - sector_offset;
744 ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
745 if (ret < 0) {
746 return -1;
747 }
748 if (decompress_buffer(s->cluster_cache, s->cluster_size,
749 s->cluster_data + sector_offset, csize) < 0) {
750 return -1;
751 }
752 s->cluster_cache_offset = coffset;
753 }
754 return 0;
755 }