]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-cluster.c
qcow2: Move sync out of qcow2_alloc_clusters
[mirror_qemu.git] / block / qcow2-cluster.c
CommitLineData
45aba42f
KW
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
ed6ccf0f 31int qcow2_grow_l1_table(BlockDriverState *bs, int min_size)
45aba42f
KW
32{
33 BDRVQcowState *s = bs->opaque;
34 int new_l1_size, new_l1_size2, ret, i;
35 uint64_t *new_l1_table;
5d757b56 36 int64_t new_l1_table_offset;
45aba42f
KW
37 uint8_t data[12];
38
39 new_l1_size = s->l1_size;
40 if (min_size <= new_l1_size)
41 return 0;
d191d12d
SW
42 if (new_l1_size == 0) {
43 new_l1_size = 1;
44 }
45aba42f
KW
45 while (min_size > new_l1_size) {
46 new_l1_size = (new_l1_size * 3 + 1) / 2;
47 }
48#ifdef DEBUG_ALLOC2
49 printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
50#endif
51
52 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
3f6a3ee5 53 new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
45aba42f
KW
54 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
55
56 /* write new table (align to cluster) */
66f82cee 57 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
ed6ccf0f 58 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
5d757b56
KW
59 if (new_l1_table_offset < 0) {
60 qemu_free(new_l1_table);
61 return new_l1_table_offset;
62 }
29216ed1 63 bdrv_flush(bs->file);
45aba42f 64
66f82cee 65 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
45aba42f
KW
66 for(i = 0; i < s->l1_size; i++)
67 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
8b3b7206
KW
68 ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_table, new_l1_size2);
69 if (ret < 0)
45aba42f
KW
70 goto fail;
71 for(i = 0; i < s->l1_size; i++)
72 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
73
74 /* set new table */
66f82cee 75 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
45aba42f
KW
76 cpu_to_be32w((uint32_t*)data, new_l1_size);
77 cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
8b3b7206
KW
78 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size), data,sizeof(data));
79 if (ret < 0) {
45aba42f 80 goto fail;
fb8fa77c 81 }
45aba42f 82 qemu_free(s->l1_table);
ed6ccf0f 83 qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
45aba42f
KW
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:
fb8fa77c
KW
89 qemu_free(new_l1_table);
90 qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2);
8b3b7206 91 return ret;
45aba42f
KW
92}
93
ed6ccf0f 94void qcow2_l2_cache_reset(BlockDriverState *bs)
45aba42f
KW
95{
96 BDRVQcowState *s = bs->opaque;
97
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));
101}
102
103static inline int l2_cache_new_entry(BlockDriverState *bs)
104{
105 BDRVQcowState *s = bs->opaque;
106 uint32_t min_count;
107 int min_index, i;
108
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;
116 }
117 }
118 return min_index;
119}
120
121/*
122 * seek_l2_table
123 *
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
130 *
131 */
132
133static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
134{
135 int i, j;
136
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;
143 }
144 }
145 return s->l2_cache + (i << s->l2_bits);
146 }
147 }
148 return NULL;
149}
150
151/*
152 * l2_load
153 *
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.
156 *
157 * Returns a pointer to the L2 table on success, or NULL if the read from
158 * the image file failed.
159 */
160
55c17e98
KW
161static int l2_load(BlockDriverState *bs, uint64_t l2_offset,
162 uint64_t **l2_table)
45aba42f
KW
163{
164 BDRVQcowState *s = bs->opaque;
165 int min_index;
55c17e98 166 int ret;
45aba42f
KW
167
168 /* seek if the table for the given offset is in the cache */
169
55c17e98
KW
170 *l2_table = seek_l2_table(s, l2_offset);
171 if (*l2_table != NULL) {
172 return 0;
173 }
45aba42f
KW
174
175 /* not found: load a new entry in the least used one */
176
177 min_index = l2_cache_new_entry(bs);
55c17e98 178 *l2_table = s->l2_cache + (min_index << s->l2_bits);
8252278a 179
66f82cee 180 BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
55c17e98
KW
181 ret = bdrv_pread(bs->file, l2_offset, *l2_table,
182 s->l2_size * sizeof(uint64_t));
183 if (ret < 0) {
184 return ret;
185 }
186
45aba42f
KW
187 s->l2_cache_offsets[min_index] = l2_offset;
188 s->l2_cache_counts[min_index] = 1;
189
55c17e98 190 return 0;
45aba42f
KW
191}
192
6583e3c7
KW
193/*
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)
196 */
197#define L1_ENTRIES_PER_SECTOR (512 / 8)
66f82cee 198static int write_l1_entry(BlockDriverState *bs, int l1_index)
6583e3c7 199{
66f82cee 200 BDRVQcowState *s = bs->opaque;
6583e3c7
KW
201 uint64_t buf[L1_ENTRIES_PER_SECTOR];
202 int l1_start_index;
f7defcb6 203 int i, ret;
6583e3c7
KW
204
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]);
208 }
209
66f82cee 210 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
8b3b7206 211 ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset + 8 * l1_start_index,
f7defcb6
KW
212 buf, sizeof(buf));
213 if (ret < 0) {
214 return ret;
6583e3c7
KW
215 }
216
217 return 0;
218}
219
45aba42f
KW
220/*
221 * l2_allocate
222 *
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.
227 *
228 */
229
c46e1167 230static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
45aba42f
KW
231{
232 BDRVQcowState *s = bs->opaque;
233 int min_index;
6583e3c7 234 uint64_t old_l2_offset;
f4f0d391
KW
235 uint64_t *l2_table;
236 int64_t l2_offset;
c46e1167 237 int ret;
45aba42f
KW
238
239 old_l2_offset = s->l1_table[l1_index];
240
241 /* allocate a new l2 entry */
242
ed6ccf0f 243 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
5d757b56 244 if (l2_offset < 0) {
c46e1167 245 return l2_offset;
5d757b56 246 }
29216ed1 247 bdrv_flush(bs->file);
45aba42f 248
45aba42f
KW
249 /* allocate a new entry in the l2 cache */
250
251 min_index = l2_cache_new_entry(bs);
252 l2_table = s->l2_cache + (min_index << s->l2_bits);
253
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 */
66f82cee
KW
259 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
260 ret = bdrv_pread(bs->file, old_l2_offset, l2_table,
c46e1167
KW
261 s->l2_size * sizeof(uint64_t));
262 if (ret < 0) {
175e1152 263 goto fail;
c46e1167 264 }
45aba42f
KW
265 }
266 /* write the l2 table to the file */
66f82cee 267 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
8b3b7206 268 ret = bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
c46e1167
KW
269 s->l2_size * sizeof(uint64_t));
270 if (ret < 0) {
175e1152
KW
271 goto fail;
272 }
273
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;
c46e1167 279 }
45aba42f
KW
280
281 /* update the l2 cache entry */
282
283 s->l2_cache_offsets[min_index] = l2_offset;
284 s->l2_cache_counts[min_index] = 1;
285
c46e1167
KW
286 *table = l2_table;
287 return 0;
175e1152
KW
288
289fail:
68dba0bf 290 s->l1_table[l1_index] = old_l2_offset;
175e1152
KW
291 qcow2_l2_cache_reset(bs);
292 return ret;
45aba42f
KW
293}
294
295static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
296 uint64_t *l2_table, uint64_t start, uint64_t mask)
297{
298 int i;
299 uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
300
301 if (!offset)
302 return 0;
303
304 for (i = start; i < start + nb_clusters; i++)
80ee15a6 305 if (offset + (uint64_t) i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
45aba42f
KW
306 break;
307
308 return (i - start);
309}
310
311static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
312{
313 int i = 0;
314
315 while(nb_clusters-- && l2_table[i] == 0)
316 i++;
317
318 return i;
319}
320
321/* The crypt function is compatible with the linux cryptoloop
322 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
323 supported */
ed6ccf0f
KW
324void 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)
45aba42f
KW
328{
329 union {
330 uint64_t ll[2];
331 uint8_t b[16];
332 } ivec;
333 int i;
334
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;
343 }
344}
345
346
72ecf02d
KW
347static int qcow_read(BlockDriverState *bs, int64_t sector_num,
348 uint8_t *buf, int nb_sectors)
45aba42f
KW
349{
350 BDRVQcowState *s = bs->opaque;
351 int ret, index_in_cluster, n, n1;
352 uint64_t cluster_offset;
353
354 while (nb_sectors > 0) {
355 n = nb_sectors;
1c46efaa
KW
356
357 ret = qcow2_get_cluster_offset(bs, sector_num << 9, &n,
358 &cluster_offset);
359 if (ret < 0) {
360 return ret;
361 }
362
45aba42f
KW
363 index_in_cluster = sector_num & (s->cluster_sectors - 1);
364 if (!cluster_offset) {
365 if (bs->backing_hd) {
366 /* read from the base image */
ed6ccf0f 367 n1 = qcow2_backing_read1(bs->backing_hd, sector_num, buf, n);
45aba42f 368 if (n1 > 0) {
66f82cee 369 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING);
45aba42f
KW
370 ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
371 if (ret < 0)
372 return -1;
373 }
374 } else {
375 memset(buf, 0, 512 * n);
376 }
377 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
66f82cee 378 if (qcow2_decompress_cluster(bs, cluster_offset) < 0)
45aba42f
KW
379 return -1;
380 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
381 } else {
66f82cee
KW
382 BLKDBG_EVENT(bs->file, BLKDBG_READ);
383 ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);
45aba42f
KW
384 if (ret != n * 512)
385 return -1;
386 if (s->crypt_method) {
ed6ccf0f 387 qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
45aba42f
KW
388 &s->aes_decrypt_key);
389 }
390 }
391 nb_sectors -= n;
392 sector_num += n;
393 buf += n * 512;
394 }
395 return 0;
396}
397
398static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
399 uint64_t cluster_offset, int n_start, int n_end)
400{
401 BDRVQcowState *s = bs->opaque;
402 int n, ret;
403
404 n = n_end - n_start;
405 if (n <= 0)
406 return 0;
66f82cee 407 BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
72ecf02d 408 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
45aba42f
KW
409 if (ret < 0)
410 return ret;
411 if (s->crypt_method) {
ed6ccf0f 412 qcow2_encrypt_sectors(s, start_sect + n_start,
45aba42f
KW
413 s->cluster_data,
414 s->cluster_data, n, 1,
415 &s->aes_encrypt_key);
416 }
66f82cee 417 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
8b3b7206
KW
418 ret = bdrv_write_sync(bs->file, (cluster_offset >> 9) + n_start,
419 s->cluster_data, n);
45aba42f
KW
420 if (ret < 0)
421 return ret;
422 return 0;
423}
424
425
426/*
427 * get_cluster_offset
428 *
1c46efaa
KW
429 * For a given offset of the disk image, find the cluster offset in
430 * qcow2 file. The offset is stored in *cluster_offset.
45aba42f
KW
431 *
432 * on entry, *num is the number of contiguous clusters we'd like to
433 * access following offset.
434 *
435 * on exit, *num is the number of contiguous clusters we can read.
436 *
1c46efaa
KW
437 * Return 0, if the offset is found
438 * Return -errno, otherwise.
45aba42f
KW
439 *
440 */
441
1c46efaa
KW
442int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
443 int *num, uint64_t *cluster_offset)
45aba42f
KW
444{
445 BDRVQcowState *s = bs->opaque;
80ee15a6 446 unsigned int l1_index, l2_index;
1c46efaa 447 uint64_t l2_offset, *l2_table;
45aba42f 448 int l1_bits, c;
80ee15a6
KW
449 unsigned int index_in_cluster, nb_clusters;
450 uint64_t nb_available, nb_needed;
55c17e98 451 int ret;
45aba42f
KW
452
453 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
454 nb_needed = *num + index_in_cluster;
455
456 l1_bits = s->l2_bits + s->cluster_bits;
457
458 /* compute how many bytes there are between the offset and
459 * the end of the l1 entry
460 */
461
80ee15a6 462 nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
45aba42f
KW
463
464 /* compute the number of available sectors */
465
466 nb_available = (nb_available >> 9) + index_in_cluster;
467
468 if (nb_needed > nb_available) {
469 nb_needed = nb_available;
470 }
471
1c46efaa 472 *cluster_offset = 0;
45aba42f
KW
473
474 /* seek the the l2 offset in the l1 table */
475
476 l1_index = offset >> l1_bits;
477 if (l1_index >= s->l1_size)
478 goto out;
479
480 l2_offset = s->l1_table[l1_index];
481
482 /* seek the l2 table of the given l2 offset */
483
484 if (!l2_offset)
485 goto out;
486
487 /* load the l2 table in memory */
488
489 l2_offset &= ~QCOW_OFLAG_COPIED;
55c17e98
KW
490 ret = l2_load(bs, l2_offset, &l2_table);
491 if (ret < 0) {
492 return ret;
1c46efaa 493 }
45aba42f
KW
494
495 /* find the cluster offset for the given disk offset */
496
497 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
1c46efaa 498 *cluster_offset = be64_to_cpu(l2_table[l2_index]);
45aba42f
KW
499 nb_clusters = size_to_clusters(s, nb_needed << 9);
500
1c46efaa 501 if (!*cluster_offset) {
45aba42f
KW
502 /* how many empty clusters ? */
503 c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
504 } else {
505 /* how many allocated clusters ? */
506 c = count_contiguous_clusters(nb_clusters, s->cluster_size,
507 &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
508 }
509
510 nb_available = (c * s->cluster_sectors);
511out:
512 if (nb_available > nb_needed)
513 nb_available = nb_needed;
514
515 *num = nb_available - index_in_cluster;
516
1c46efaa
KW
517 *cluster_offset &=~QCOW_OFLAG_COPIED;
518 return 0;
45aba42f
KW
519}
520
521/*
522 * get_cluster_table
523 *
524 * for a given disk offset, load (and allocate if needed)
525 * the l2 table.
526 *
527 * the l2 table offset in the qcow2 file and the cluster index
528 * in the l2 table are given to the caller.
529 *
1e3e8f1a 530 * Returns 0 on success, -errno in failure case
45aba42f 531 */
45aba42f
KW
532static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
533 uint64_t **new_l2_table,
534 uint64_t *new_l2_offset,
535 int *new_l2_index)
536{
537 BDRVQcowState *s = bs->opaque;
80ee15a6 538 unsigned int l1_index, l2_index;
c46e1167
KW
539 uint64_t l2_offset;
540 uint64_t *l2_table = NULL;
80ee15a6 541 int ret;
45aba42f
KW
542
543 /* seek the the l2 offset in the l1 table */
544
545 l1_index = offset >> (s->l2_bits + s->cluster_bits);
546 if (l1_index >= s->l1_size) {
ed6ccf0f 547 ret = qcow2_grow_l1_table(bs, l1_index + 1);
1e3e8f1a
KW
548 if (ret < 0) {
549 return ret;
550 }
45aba42f
KW
551 }
552 l2_offset = s->l1_table[l1_index];
553
554 /* seek the l2 table of the given l2 offset */
555
556 if (l2_offset & QCOW_OFLAG_COPIED) {
557 /* load the l2 table in memory */
558 l2_offset &= ~QCOW_OFLAG_COPIED;
55c17e98
KW
559 ret = l2_load(bs, l2_offset, &l2_table);
560 if (ret < 0) {
561 return ret;
1e3e8f1a 562 }
45aba42f
KW
563 } else {
564 if (l2_offset)
ed6ccf0f 565 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
c46e1167
KW
566 ret = l2_allocate(bs, l1_index, &l2_table);
567 if (ret < 0) {
568 return ret;
1e3e8f1a 569 }
45aba42f
KW
570 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
571 }
572
573 /* find the cluster offset for the given disk offset */
574
575 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
576
577 *new_l2_table = l2_table;
578 *new_l2_offset = l2_offset;
579 *new_l2_index = l2_index;
580
1e3e8f1a 581 return 0;
45aba42f
KW
582}
583
584/*
585 * alloc_compressed_cluster_offset
586 *
587 * For a given offset of the disk image, return cluster offset in
588 * qcow2 file.
589 *
590 * If the offset is not found, allocate a new compressed cluster.
591 *
592 * Return the cluster offset if successful,
593 * Return 0, otherwise.
594 *
595 */
596
ed6ccf0f
KW
597uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
598 uint64_t offset,
599 int compressed_size)
45aba42f
KW
600{
601 BDRVQcowState *s = bs->opaque;
602 int l2_index, ret;
f4f0d391
KW
603 uint64_t l2_offset, *l2_table;
604 int64_t cluster_offset;
45aba42f
KW
605 int nb_csectors;
606
607 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
1e3e8f1a 608 if (ret < 0) {
45aba42f 609 return 0;
1e3e8f1a 610 }
45aba42f
KW
611
612 cluster_offset = be64_to_cpu(l2_table[l2_index]);
613 if (cluster_offset & QCOW_OFLAG_COPIED)
614 return cluster_offset & ~QCOW_OFLAG_COPIED;
615
616 if (cluster_offset)
ed6ccf0f 617 qcow2_free_any_clusters(bs, cluster_offset, 1);
45aba42f 618
ed6ccf0f 619 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
5d757b56
KW
620 if (cluster_offset < 0) {
621 return 0;
622 }
623
45aba42f
KW
624 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
625 (cluster_offset >> 9);
626
627 cluster_offset |= QCOW_OFLAG_COMPRESSED |
628 ((uint64_t)nb_csectors << s->csize_shift);
629
630 /* update L2 table */
631
632 /* compressed clusters never have the copied flag */
633
66f82cee 634 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
45aba42f 635 l2_table[l2_index] = cpu_to_be64(cluster_offset);
8b3b7206 636 if (bdrv_pwrite_sync(bs->file,
45aba42f
KW
637 l2_offset + l2_index * sizeof(uint64_t),
638 l2_table + l2_index,
8b3b7206 639 sizeof(uint64_t)) < 0)
45aba42f
KW
640 return 0;
641
642 return cluster_offset;
643}
644
4c1612d9
KW
645/*
646 * Write L2 table updates to disk, writing whole sectors to avoid a
647 * read-modify-write in bdrv_pwrite
648 */
649#define L2_ENTRIES_PER_SECTOR (512 / 8)
66f82cee 650static int write_l2_entries(BlockDriverState *bs, uint64_t *l2_table,
4c1612d9
KW
651 uint64_t l2_offset, int l2_index, int num)
652{
653 int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1);
654 int start_offset = (8 * l2_index) & ~511;
655 int end_offset = (8 * (l2_index + num) + 511) & ~511;
656 size_t len = end_offset - start_offset;
79a31189 657 int ret;
4c1612d9 658
66f82cee 659 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
7ec5e6a4 660 ret = bdrv_pwrite(bs->file, l2_offset + start_offset,
79a31189
KW
661 &l2_table[l2_start_index], len);
662 if (ret < 0) {
663 return ret;
4c1612d9
KW
664 }
665
666 return 0;
667}
668
148da7ea 669int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
45aba42f
KW
670{
671 BDRVQcowState *s = bs->opaque;
672 int i, j = 0, l2_index, ret;
673 uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
148da7ea 674 uint64_t cluster_offset = m->cluster_offset;
45aba42f
KW
675
676 if (m->nb_clusters == 0)
677 return 0;
678
679 old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
680
681 /* copy content of unmodified sectors */
682 start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
683 if (m->n_start) {
684 ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
685 if (ret < 0)
686 goto err;
687 }
688
689 if (m->nb_available & (s->cluster_sectors - 1)) {
690 uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
691 ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
692 m->nb_available - end, s->cluster_sectors);
693 if (ret < 0)
694 goto err;
695 }
696
45aba42f 697 /* update L2 table */
1e3e8f1a
KW
698 ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index);
699 if (ret < 0) {
45aba42f 700 goto err;
1e3e8f1a 701 }
45aba42f
KW
702
703 for (i = 0; i < m->nb_clusters; i++) {
704 /* if two concurrent writes happen to the same unallocated cluster
705 * each write allocates separate cluster and writes data concurrently.
706 * The first one to complete updates l2 table with pointer to its
707 * cluster the second one has to do RMW (which is done above by
708 * copy_sectors()), update l2 table with its cluster pointer and free
709 * old cluster. This is what this loop does */
710 if(l2_table[l2_index + i] != 0)
711 old_cluster[j++] = l2_table[l2_index + i];
712
713 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
714 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
715 }
716
66f82cee 717 ret = write_l2_entries(bs, l2_table, l2_offset, l2_index, m->nb_clusters);
c835d00f 718 if (ret < 0) {
1b7c801b 719 qcow2_l2_cache_reset(bs);
45aba42f 720 goto err;
4c1612d9 721 }
45aba42f 722
7ec5e6a4
KW
723 /*
724 * If this was a COW, we need to decrease the refcount of the old cluster.
725 * Also flush bs->file to get the right order for L2 and refcount update.
726 */
727 if (j != 0) {
728 bdrv_flush(bs->file);
729 for (i = 0; i < j; i++) {
730 qcow2_free_any_clusters(bs,
731 be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
732 }
733 }
45aba42f
KW
734
735 ret = 0;
736err:
737 qemu_free(old_cluster);
738 return ret;
739 }
740
741/*
742 * alloc_cluster_offset
743 *
148da7ea 744 * For a given offset of the disk image, return cluster offset in qcow2 file.
45aba42f
KW
745 * If the offset is not found, allocate a new cluster.
746 *
148da7ea
KW
747 * If the cluster was already allocated, m->nb_clusters is set to 0,
748 * m->depends_on is set to NULL and the other fields in m are meaningless.
749 *
750 * If the cluster is newly allocated, m->nb_clusters is set to the number of
751 * contiguous clusters that have been allocated. This may be 0 if the request
752 * conflict with another write request in flight; in this case, m->depends_on
753 * is set and the remaining fields of m are meaningless.
45aba42f 754 *
148da7ea
KW
755 * If m->nb_clusters is non-zero, the other fields of m are valid and contain
756 * information about the first allocated cluster.
757 *
758 * Return 0 on success and -errno in error cases
45aba42f 759 */
f4f0d391
KW
760int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
761 int n_start, int n_end, int *num, QCowL2Meta *m)
45aba42f
KW
762{
763 BDRVQcowState *s = bs->opaque;
764 int l2_index, ret;
5d757b56
KW
765 uint64_t l2_offset, *l2_table;
766 int64_t cluster_offset;
80ee15a6 767 unsigned int nb_clusters, i = 0;
f214978a 768 QCowL2Meta *old_alloc;
45aba42f
KW
769
770 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
1e3e8f1a 771 if (ret < 0) {
148da7ea 772 return ret;
1e3e8f1a 773 }
45aba42f
KW
774
775 nb_clusters = size_to_clusters(s, n_end << 9);
776
777 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
778
779 cluster_offset = be64_to_cpu(l2_table[l2_index]);
780
781 /* We keep all QCOW_OFLAG_COPIED clusters */
782
783 if (cluster_offset & QCOW_OFLAG_COPIED) {
784 nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
785 &l2_table[l2_index], 0, 0);
786
787 cluster_offset &= ~QCOW_OFLAG_COPIED;
788 m->nb_clusters = 0;
148da7ea 789 m->depends_on = NULL;
45aba42f
KW
790
791 goto out;
792 }
793
794 /* for the moment, multiple compressed clusters are not managed */
795
796 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
797 nb_clusters = 1;
798
799 /* how many available clusters ? */
800
801 while (i < nb_clusters) {
802 i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
803 &l2_table[l2_index], i, 0);
4805bb66 804 if ((i >= nb_clusters) || be64_to_cpu(l2_table[l2_index + i])) {
45aba42f 805 break;
4805bb66 806 }
45aba42f
KW
807
808 i += count_contiguous_free_clusters(nb_clusters - i,
809 &l2_table[l2_index + i]);
4805bb66
KW
810 if (i >= nb_clusters) {
811 break;
812 }
45aba42f
KW
813
814 cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
815
816 if ((cluster_offset & QCOW_OFLAG_COPIED) ||
817 (cluster_offset & QCOW_OFLAG_COMPRESSED))
818 break;
819 }
4805bb66 820 assert(i <= nb_clusters);
45aba42f
KW
821 nb_clusters = i;
822
f214978a
KW
823 /*
824 * Check if there already is an AIO write request in flight which allocates
825 * the same cluster. In this case we need to wait until the previous
826 * request has completed and updated the L2 table accordingly.
827 */
72cf2d4f 828 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
f214978a
KW
829
830 uint64_t end_offset = offset + nb_clusters * s->cluster_size;
831 uint64_t old_offset = old_alloc->offset;
832 uint64_t old_end_offset = old_alloc->offset +
833 old_alloc->nb_clusters * s->cluster_size;
834
835 if (end_offset < old_offset || offset > old_end_offset) {
836 /* No intersection */
837 } else {
838 if (offset < old_offset) {
839 /* Stop at the start of a running allocation */
840 nb_clusters = (old_offset - offset) >> s->cluster_bits;
841 } else {
842 nb_clusters = 0;
843 }
844
845 if (nb_clusters == 0) {
846 /* Set dependency and wait for a callback */
847 m->depends_on = old_alloc;
848 m->nb_clusters = 0;
849 *num = 0;
850 return 0;
851 }
852 }
853 }
854
855 if (!nb_clusters) {
856 abort();
857 }
858
72cf2d4f 859 QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
f214978a 860
45aba42f
KW
861 /* allocate a new cluster */
862
ed6ccf0f 863 cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
5d757b56 864 if (cluster_offset < 0) {
c644db3d 865 QLIST_REMOVE(m, next_in_flight);
5d757b56
KW
866 return cluster_offset;
867 }
29216ed1 868 bdrv_flush(bs->file);
45aba42f
KW
869
870 /* save info needed for meta data update */
871 m->offset = offset;
872 m->n_start = n_start;
873 m->nb_clusters = nb_clusters;
874
875out:
876 m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
148da7ea 877 m->cluster_offset = cluster_offset;
45aba42f
KW
878
879 *num = m->nb_available - n_start;
880
148da7ea 881 return 0;
45aba42f
KW
882}
883
884static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
885 const uint8_t *buf, int buf_size)
886{
887 z_stream strm1, *strm = &strm1;
888 int ret, out_len;
889
890 memset(strm, 0, sizeof(*strm));
891
892 strm->next_in = (uint8_t *)buf;
893 strm->avail_in = buf_size;
894 strm->next_out = out_buf;
895 strm->avail_out = out_buf_size;
896
897 ret = inflateInit2(strm, -12);
898 if (ret != Z_OK)
899 return -1;
900 ret = inflate(strm, Z_FINISH);
901 out_len = strm->next_out - out_buf;
902 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
903 out_len != out_buf_size) {
904 inflateEnd(strm);
905 return -1;
906 }
907 inflateEnd(strm);
908 return 0;
909}
910
66f82cee 911int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
45aba42f 912{
66f82cee 913 BDRVQcowState *s = bs->opaque;
45aba42f
KW
914 int ret, csize, nb_csectors, sector_offset;
915 uint64_t coffset;
916
917 coffset = cluster_offset & s->cluster_offset_mask;
918 if (s->cluster_cache_offset != coffset) {
919 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
920 sector_offset = coffset & 511;
921 csize = nb_csectors * 512 - sector_offset;
66f82cee
KW
922 BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
923 ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data, nb_csectors);
45aba42f
KW
924 if (ret < 0) {
925 return -1;
926 }
927 if (decompress_buffer(s->cluster_cache, s->cluster_size,
928 s->cluster_data + sector_offset, csize) < 0) {
929 return -1;
930 }
931 s->cluster_cache_offset = coffset;
932 }
933 return 0;
934}