]> Git Repo - qemu.git/blame - block/qcow2-cluster.c
block: Use g_new() & friends where that makes obvious sense
[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"
737e150e 28#include "block/block_int.h"
45aba42f 29#include "block/qcow2.h"
3cce16f4 30#include "trace.h"
45aba42f 31
2cf7cfa1
KW
32int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
33 bool exact_size)
45aba42f
KW
34{
35 BDRVQcowState *s = bs->opaque;
2cf7cfa1 36 int new_l1_size2, ret, i;
45aba42f 37 uint64_t *new_l1_table;
fda74f82 38 int64_t old_l1_table_offset, old_l1_size;
2cf7cfa1 39 int64_t new_l1_table_offset, new_l1_size;
45aba42f
KW
40 uint8_t data[12];
41
72893756 42 if (min_size <= s->l1_size)
45aba42f 43 return 0;
72893756 44
b93f9950
HR
45 /* Do a sanity check on min_size before trying to calculate new_l1_size
46 * (this prevents overflows during the while loop for the calculation of
47 * new_l1_size) */
48 if (min_size > INT_MAX / sizeof(uint64_t)) {
49 return -EFBIG;
50 }
51
72893756
SH
52 if (exact_size) {
53 new_l1_size = min_size;
54 } else {
55 /* Bump size up to reduce the number of times we have to grow */
56 new_l1_size = s->l1_size;
57 if (new_l1_size == 0) {
58 new_l1_size = 1;
59 }
60 while (min_size > new_l1_size) {
61 new_l1_size = (new_l1_size * 3 + 1) / 2;
62 }
45aba42f 63 }
72893756 64
cab60de9 65 if (new_l1_size > INT_MAX / sizeof(uint64_t)) {
2cf7cfa1
KW
66 return -EFBIG;
67 }
68
45aba42f 69#ifdef DEBUG_ALLOC2
2cf7cfa1
KW
70 fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n",
71 s->l1_size, new_l1_size);
45aba42f
KW
72#endif
73
74 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
de82815d
KW
75 new_l1_table = qemu_try_blockalign(bs->file,
76 align_offset(new_l1_size2, 512));
77 if (new_l1_table == NULL) {
78 return -ENOMEM;
79 }
80 memset(new_l1_table, 0, align_offset(new_l1_size2, 512));
81
45aba42f
KW
82 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
83
84 /* write new table (align to cluster) */
66f82cee 85 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
ed6ccf0f 86 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
5d757b56 87 if (new_l1_table_offset < 0) {
de82815d 88 qemu_vfree(new_l1_table);
5d757b56
KW
89 return new_l1_table_offset;
90 }
29c1a730
KW
91
92 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
93 if (ret < 0) {
80fa3341 94 goto fail;
29c1a730 95 }
45aba42f 96
cf93980e
HR
97 /* the L1 position has not yet been updated, so these clusters must
98 * indeed be completely free */
231bb267
HR
99 ret = qcow2_pre_write_overlap_check(bs, 0, new_l1_table_offset,
100 new_l1_size2);
cf93980e
HR
101 if (ret < 0) {
102 goto fail;
103 }
104
66f82cee 105 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
45aba42f
KW
106 for(i = 0; i < s->l1_size; i++)
107 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
8b3b7206
KW
108 ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_table, new_l1_size2);
109 if (ret < 0)
45aba42f
KW
110 goto fail;
111 for(i = 0; i < s->l1_size; i++)
112 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
113
114 /* set new table */
66f82cee 115 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
45aba42f 116 cpu_to_be32w((uint32_t*)data, new_l1_size);
e4ef9f46 117 stq_be_p(data + 4, new_l1_table_offset);
8b3b7206
KW
118 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size), data,sizeof(data));
119 if (ret < 0) {
45aba42f 120 goto fail;
fb8fa77c 121 }
de82815d 122 qemu_vfree(s->l1_table);
fda74f82 123 old_l1_table_offset = s->l1_table_offset;
45aba42f
KW
124 s->l1_table_offset = new_l1_table_offset;
125 s->l1_table = new_l1_table;
fda74f82 126 old_l1_size = s->l1_size;
45aba42f 127 s->l1_size = new_l1_size;
fda74f82
HR
128 qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * sizeof(uint64_t),
129 QCOW2_DISCARD_OTHER);
45aba42f
KW
130 return 0;
131 fail:
de82815d 132 qemu_vfree(new_l1_table);
6cfcb9b8
KW
133 qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2,
134 QCOW2_DISCARD_OTHER);
8b3b7206 135 return ret;
45aba42f
KW
136}
137
45aba42f
KW
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
55c17e98
KW
148static int l2_load(BlockDriverState *bs, uint64_t l2_offset,
149 uint64_t **l2_table)
45aba42f
KW
150{
151 BDRVQcowState *s = bs->opaque;
55c17e98 152 int ret;
45aba42f 153
29c1a730 154 ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset, (void**) l2_table);
45aba42f 155
29c1a730 156 return ret;
45aba42f
KW
157}
158
6583e3c7
KW
159/*
160 * Writes one sector of the L1 table to the disk (can't update single entries
161 * and we really don't want bdrv_pread to perform a read-modify-write)
162 */
163#define L1_ENTRIES_PER_SECTOR (512 / 8)
e23e400e 164int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
6583e3c7 165{
66f82cee 166 BDRVQcowState *s = bs->opaque;
6583e3c7
KW
167 uint64_t buf[L1_ENTRIES_PER_SECTOR];
168 int l1_start_index;
f7defcb6 169 int i, ret;
6583e3c7
KW
170
171 l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
172 for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) {
173 buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
174 }
175
231bb267 176 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
cf93980e
HR
177 s->l1_table_offset + 8 * l1_start_index, sizeof(buf));
178 if (ret < 0) {
179 return ret;
180 }
181
66f82cee 182 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
8b3b7206 183 ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset + 8 * l1_start_index,
f7defcb6
KW
184 buf, sizeof(buf));
185 if (ret < 0) {
186 return ret;
6583e3c7
KW
187 }
188
189 return 0;
190}
191
45aba42f
KW
192/*
193 * l2_allocate
194 *
195 * Allocate a new l2 entry in the file. If l1_index points to an already
196 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
197 * table) copy the contents of the old L2 table into the newly allocated one.
198 * Otherwise the new table is initialized with zeros.
199 *
200 */
201
c46e1167 202static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
45aba42f
KW
203{
204 BDRVQcowState *s = bs->opaque;
6583e3c7 205 uint64_t old_l2_offset;
8585afd8 206 uint64_t *l2_table = NULL;
f4f0d391 207 int64_t l2_offset;
c46e1167 208 int ret;
45aba42f
KW
209
210 old_l2_offset = s->l1_table[l1_index];
211
3cce16f4
KW
212 trace_qcow2_l2_allocate(bs, l1_index);
213
45aba42f
KW
214 /* allocate a new l2 entry */
215
ed6ccf0f 216 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
5d757b56 217 if (l2_offset < 0) {
be0b742e
HR
218 ret = l2_offset;
219 goto fail;
5d757b56 220 }
29c1a730
KW
221
222 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
223 if (ret < 0) {
224 goto fail;
225 }
45aba42f 226
45aba42f
KW
227 /* allocate a new entry in the l2 cache */
228
3cce16f4 229 trace_qcow2_l2_allocate_get_empty(bs, l1_index);
29c1a730
KW
230 ret = qcow2_cache_get_empty(bs, s->l2_table_cache, l2_offset, (void**) table);
231 if (ret < 0) {
be0b742e 232 goto fail;
29c1a730
KW
233 }
234
235 l2_table = *table;
45aba42f 236
8e37f681 237 if ((old_l2_offset & L1E_OFFSET_MASK) == 0) {
45aba42f
KW
238 /* if there was no old l2 table, clear the new table */
239 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
240 } else {
29c1a730
KW
241 uint64_t* old_table;
242
45aba42f 243 /* if there was an old l2 table, read it from the disk */
66f82cee 244 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
8e37f681
KW
245 ret = qcow2_cache_get(bs, s->l2_table_cache,
246 old_l2_offset & L1E_OFFSET_MASK,
29c1a730
KW
247 (void**) &old_table);
248 if (ret < 0) {
249 goto fail;
250 }
251
252 memcpy(l2_table, old_table, s->cluster_size);
253
254 ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &old_table);
c46e1167 255 if (ret < 0) {
175e1152 256 goto fail;
c46e1167 257 }
45aba42f 258 }
29c1a730 259
45aba42f 260 /* write the l2 table to the file */
66f82cee 261 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
29c1a730 262
3cce16f4 263 trace_qcow2_l2_allocate_write_l2(bs, l1_index);
29c1a730
KW
264 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
265 ret = qcow2_cache_flush(bs, s->l2_table_cache);
c46e1167 266 if (ret < 0) {
175e1152
KW
267 goto fail;
268 }
269
270 /* update the L1 entry */
3cce16f4 271 trace_qcow2_l2_allocate_write_l1(bs, l1_index);
175e1152 272 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
e23e400e 273 ret = qcow2_write_l1_entry(bs, l1_index);
175e1152
KW
274 if (ret < 0) {
275 goto fail;
c46e1167 276 }
45aba42f 277
c46e1167 278 *table = l2_table;
3cce16f4 279 trace_qcow2_l2_allocate_done(bs, l1_index, 0);
c46e1167 280 return 0;
175e1152
KW
281
282fail:
3cce16f4 283 trace_qcow2_l2_allocate_done(bs, l1_index, ret);
8585afd8
HR
284 if (l2_table != NULL) {
285 qcow2_cache_put(bs, s->l2_table_cache, (void**) table);
286 }
68dba0bf 287 s->l1_table[l1_index] = old_l2_offset;
e3b21ef9
HR
288 if (l2_offset > 0) {
289 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
290 QCOW2_DISCARD_ALWAYS);
291 }
175e1152 292 return ret;
45aba42f
KW
293}
294
2bfcc4a0
KW
295/*
296 * Checks how many clusters in a given L2 table are contiguous in the image
297 * file. As soon as one of the flags in the bitmask stop_flags changes compared
298 * to the first cluster, the search is stopped and the cluster is not counted
299 * as contiguous. (This allows it, for example, to stop at the first compressed
300 * cluster which may require a different handling)
301 */
45aba42f 302static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
61653008 303 uint64_t *l2_table, uint64_t stop_flags)
45aba42f
KW
304{
305 int i;
78a52ad5 306 uint64_t mask = stop_flags | L2E_OFFSET_MASK | QCOW_OFLAG_COMPRESSED;
15684a47
HR
307 uint64_t first_entry = be64_to_cpu(l2_table[0]);
308 uint64_t offset = first_entry & mask;
45aba42f
KW
309
310 if (!offset)
311 return 0;
312
15684a47
HR
313 assert(qcow2_get_cluster_type(first_entry) != QCOW2_CLUSTER_COMPRESSED);
314
61653008 315 for (i = 0; i < nb_clusters; i++) {
2bfcc4a0
KW
316 uint64_t l2_entry = be64_to_cpu(l2_table[i]) & mask;
317 if (offset + (uint64_t) i * cluster_size != l2_entry) {
45aba42f 318 break;
2bfcc4a0
KW
319 }
320 }
45aba42f 321
61653008 322 return i;
45aba42f
KW
323}
324
325static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
326{
2bfcc4a0
KW
327 int i;
328
329 for (i = 0; i < nb_clusters; i++) {
330 int type = qcow2_get_cluster_type(be64_to_cpu(l2_table[i]));
45aba42f 331
2bfcc4a0
KW
332 if (type != QCOW2_CLUSTER_UNALLOCATED) {
333 break;
334 }
335 }
45aba42f
KW
336
337 return i;
338}
339
340/* The crypt function is compatible with the linux cryptoloop
341 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
342 supported */
ed6ccf0f
KW
343void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
344 uint8_t *out_buf, const uint8_t *in_buf,
345 int nb_sectors, int enc,
346 const AES_KEY *key)
45aba42f
KW
347{
348 union {
349 uint64_t ll[2];
350 uint8_t b[16];
351 } ivec;
352 int i;
353
354 for(i = 0; i < nb_sectors; i++) {
355 ivec.ll[0] = cpu_to_le64(sector_num);
356 ivec.ll[1] = 0;
357 AES_cbc_encrypt(in_buf, out_buf, 512, key,
358 ivec.b, enc);
359 sector_num++;
360 in_buf += 512;
361 out_buf += 512;
362 }
363}
364
aef4acb6
SH
365static int coroutine_fn copy_sectors(BlockDriverState *bs,
366 uint64_t start_sect,
367 uint64_t cluster_offset,
368 int n_start, int n_end)
45aba42f
KW
369{
370 BDRVQcowState *s = bs->opaque;
aef4acb6
SH
371 QEMUIOVector qiov;
372 struct iovec iov;
45aba42f 373 int n, ret;
1b9f1491 374
45aba42f 375 n = n_end - n_start;
1b9f1491 376 if (n <= 0) {
45aba42f 377 return 0;
1b9f1491
KW
378 }
379
aef4acb6 380 iov.iov_len = n * BDRV_SECTOR_SIZE;
de82815d
KW
381 iov.iov_base = qemu_try_blockalign(bs, iov.iov_len);
382 if (iov.iov_base == NULL) {
383 return -ENOMEM;
384 }
aef4acb6
SH
385
386 qemu_iovec_init_external(&qiov, &iov, 1);
1b9f1491 387
66f82cee 388 BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
aef4acb6 389
dba28555 390 if (!bs->drv) {
bd604369
KW
391 ret = -ENOMEDIUM;
392 goto out;
dba28555
HR
393 }
394
aef4acb6
SH
395 /* Call .bdrv_co_readv() directly instead of using the public block-layer
396 * interface. This avoids double I/O throttling and request tracking,
397 * which can lead to deadlock when block layer copy-on-read is enabled.
398 */
399 ret = bs->drv->bdrv_co_readv(bs, start_sect + n_start, n, &qiov);
1b9f1491
KW
400 if (ret < 0) {
401 goto out;
402 }
403
45aba42f 404 if (s->crypt_method) {
ed6ccf0f 405 qcow2_encrypt_sectors(s, start_sect + n_start,
aef4acb6 406 iov.iov_base, iov.iov_base, n, 1,
45aba42f
KW
407 &s->aes_encrypt_key);
408 }
1b9f1491 409
231bb267 410 ret = qcow2_pre_write_overlap_check(bs, 0,
cf93980e
HR
411 cluster_offset + n_start * BDRV_SECTOR_SIZE, n * BDRV_SECTOR_SIZE);
412 if (ret < 0) {
413 goto out;
414 }
415
66f82cee 416 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
aef4acb6 417 ret = bdrv_co_writev(bs->file, (cluster_offset >> 9) + n_start, n, &qiov);
1b9f1491
KW
418 if (ret < 0) {
419 goto out;
420 }
421
422 ret = 0;
423out:
aef4acb6 424 qemu_vfree(iov.iov_base);
1b9f1491 425 return ret;
45aba42f
KW
426}
427
428
429/*
430 * get_cluster_offset
431 *
1c46efaa
KW
432 * For a given offset of the disk image, find the cluster offset in
433 * qcow2 file. The offset is stored in *cluster_offset.
45aba42f 434 *
d57237f2 435 * on entry, *num is the number of contiguous sectors we'd like to
45aba42f
KW
436 * access following offset.
437 *
d57237f2 438 * on exit, *num is the number of contiguous sectors we can read.
45aba42f 439 *
68d000a3
KW
440 * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
441 * cases.
45aba42f 442 */
1c46efaa
KW
443int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
444 int *num, uint64_t *cluster_offset)
45aba42f
KW
445{
446 BDRVQcowState *s = bs->opaque;
2cf7cfa1
KW
447 unsigned int l2_index;
448 uint64_t l1_index, l2_offset, *l2_table;
45aba42f 449 int l1_bits, c;
80ee15a6
KW
450 unsigned int index_in_cluster, nb_clusters;
451 uint64_t nb_available, nb_needed;
55c17e98 452 int ret;
45aba42f
KW
453
454 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
455 nb_needed = *num + index_in_cluster;
456
457 l1_bits = s->l2_bits + s->cluster_bits;
458
459 /* compute how many bytes there are between the offset and
460 * the end of the l1 entry
461 */
462
80ee15a6 463 nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
45aba42f
KW
464
465 /* compute the number of available sectors */
466
467 nb_available = (nb_available >> 9) + index_in_cluster;
468
469 if (nb_needed > nb_available) {
470 nb_needed = nb_available;
471 }
472
1c46efaa 473 *cluster_offset = 0;
45aba42f
KW
474
475 /* seek the the l2 offset in the l1 table */
476
477 l1_index = offset >> l1_bits;
68d000a3
KW
478 if (l1_index >= s->l1_size) {
479 ret = QCOW2_CLUSTER_UNALLOCATED;
45aba42f 480 goto out;
68d000a3 481 }
45aba42f 482
68d000a3
KW
483 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
484 if (!l2_offset) {
485 ret = QCOW2_CLUSTER_UNALLOCATED;
45aba42f 486 goto out;
68d000a3 487 }
45aba42f
KW
488
489 /* load the l2 table in memory */
490
55c17e98
KW
491 ret = l2_load(bs, l2_offset, &l2_table);
492 if (ret < 0) {
493 return ret;
1c46efaa 494 }
45aba42f
KW
495
496 /* find the cluster offset for the given disk offset */
497
498 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
1c46efaa 499 *cluster_offset = be64_to_cpu(l2_table[l2_index]);
45aba42f
KW
500 nb_clusters = size_to_clusters(s, nb_needed << 9);
501
68d000a3
KW
502 ret = qcow2_get_cluster_type(*cluster_offset);
503 switch (ret) {
504 case QCOW2_CLUSTER_COMPRESSED:
505 /* Compressed clusters can only be processed one by one */
506 c = 1;
507 *cluster_offset &= L2E_COMPRESSED_OFFSET_SIZE_MASK;
508 break;
6377af48 509 case QCOW2_CLUSTER_ZERO:
381b487d 510 if (s->qcow_version < 3) {
8885eade 511 qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
381b487d
PB
512 return -EIO;
513 }
6377af48 514 c = count_contiguous_clusters(nb_clusters, s->cluster_size,
61653008 515 &l2_table[l2_index], QCOW_OFLAG_ZERO);
6377af48
KW
516 *cluster_offset = 0;
517 break;
68d000a3 518 case QCOW2_CLUSTER_UNALLOCATED:
45aba42f
KW
519 /* how many empty clusters ? */
520 c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
68d000a3
KW
521 *cluster_offset = 0;
522 break;
523 case QCOW2_CLUSTER_NORMAL:
45aba42f
KW
524 /* how many allocated clusters ? */
525 c = count_contiguous_clusters(nb_clusters, s->cluster_size,
61653008 526 &l2_table[l2_index], QCOW_OFLAG_ZERO);
68d000a3
KW
527 *cluster_offset &= L2E_OFFSET_MASK;
528 break;
1417d7e4
KW
529 default:
530 abort();
45aba42f
KW
531 }
532
29c1a730
KW
533 qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
534
68d000a3
KW
535 nb_available = (c * s->cluster_sectors);
536
45aba42f
KW
537out:
538 if (nb_available > nb_needed)
539 nb_available = nb_needed;
540
541 *num = nb_available - index_in_cluster;
542
68d000a3 543 return ret;
45aba42f
KW
544}
545
546/*
547 * get_cluster_table
548 *
549 * for a given disk offset, load (and allocate if needed)
550 * the l2 table.
551 *
552 * the l2 table offset in the qcow2 file and the cluster index
553 * in the l2 table are given to the caller.
554 *
1e3e8f1a 555 * Returns 0 on success, -errno in failure case
45aba42f 556 */
45aba42f
KW
557static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
558 uint64_t **new_l2_table,
45aba42f
KW
559 int *new_l2_index)
560{
561 BDRVQcowState *s = bs->opaque;
2cf7cfa1
KW
562 unsigned int l2_index;
563 uint64_t l1_index, l2_offset;
c46e1167 564 uint64_t *l2_table = NULL;
80ee15a6 565 int ret;
45aba42f
KW
566
567 /* seek the the l2 offset in the l1 table */
568
569 l1_index = offset >> (s->l2_bits + s->cluster_bits);
570 if (l1_index >= s->l1_size) {
72893756 571 ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
1e3e8f1a
KW
572 if (ret < 0) {
573 return ret;
574 }
45aba42f 575 }
8e37f681 576
2cf7cfa1 577 assert(l1_index < s->l1_size);
8e37f681 578 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
45aba42f
KW
579
580 /* seek the l2 table of the given l2 offset */
581
8e37f681 582 if (s->l1_table[l1_index] & QCOW_OFLAG_COPIED) {
45aba42f 583 /* load the l2 table in memory */
55c17e98
KW
584 ret = l2_load(bs, l2_offset, &l2_table);
585 if (ret < 0) {
586 return ret;
1e3e8f1a 587 }
45aba42f 588 } else {
16fde5f2 589 /* First allocate a new L2 table (and do COW if needed) */
c46e1167
KW
590 ret = l2_allocate(bs, l1_index, &l2_table);
591 if (ret < 0) {
592 return ret;
1e3e8f1a 593 }
16fde5f2
KW
594
595 /* Then decrease the refcount of the old table */
596 if (l2_offset) {
6cfcb9b8
KW
597 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
598 QCOW2_DISCARD_OTHER);
16fde5f2 599 }
45aba42f
KW
600 }
601
602 /* find the cluster offset for the given disk offset */
603
604 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
605
606 *new_l2_table = l2_table;
45aba42f
KW
607 *new_l2_index = l2_index;
608
1e3e8f1a 609 return 0;
45aba42f
KW
610}
611
612/*
613 * alloc_compressed_cluster_offset
614 *
615 * For a given offset of the disk image, return cluster offset in
616 * qcow2 file.
617 *
618 * If the offset is not found, allocate a new compressed cluster.
619 *
620 * Return the cluster offset if successful,
621 * Return 0, otherwise.
622 *
623 */
624
ed6ccf0f
KW
625uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
626 uint64_t offset,
627 int compressed_size)
45aba42f
KW
628{
629 BDRVQcowState *s = bs->opaque;
630 int l2_index, ret;
3948d1d4 631 uint64_t *l2_table;
f4f0d391 632 int64_t cluster_offset;
45aba42f
KW
633 int nb_csectors;
634
3948d1d4 635 ret = get_cluster_table(bs, offset, &l2_table, &l2_index);
1e3e8f1a 636 if (ret < 0) {
45aba42f 637 return 0;
1e3e8f1a 638 }
45aba42f 639
b0b6862e
KW
640 /* Compression can't overwrite anything. Fail if the cluster was already
641 * allocated. */
45aba42f 642 cluster_offset = be64_to_cpu(l2_table[l2_index]);
b0b6862e 643 if (cluster_offset & L2E_OFFSET_MASK) {
8f1efd00
KW
644 qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
645 return 0;
646 }
45aba42f 647
ed6ccf0f 648 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
5d757b56 649 if (cluster_offset < 0) {
29c1a730 650 qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
5d757b56
KW
651 return 0;
652 }
653
45aba42f
KW
654 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
655 (cluster_offset >> 9);
656
657 cluster_offset |= QCOW_OFLAG_COMPRESSED |
658 ((uint64_t)nb_csectors << s->csize_shift);
659
660 /* update L2 table */
661
662 /* compressed clusters never have the copied flag */
663
66f82cee 664 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
29c1a730 665 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
45aba42f 666 l2_table[l2_index] = cpu_to_be64(cluster_offset);
29c1a730 667 ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
79a31189 668 if (ret < 0) {
29c1a730 669 return 0;
4c1612d9
KW
670 }
671
29c1a730 672 return cluster_offset;
4c1612d9
KW
673}
674
593fb83c
KW
675static int perform_cow(BlockDriverState *bs, QCowL2Meta *m, Qcow2COWRegion *r)
676{
677 BDRVQcowState *s = bs->opaque;
678 int ret;
679
680 if (r->nb_sectors == 0) {
681 return 0;
682 }
683
684 qemu_co_mutex_unlock(&s->lock);
685 ret = copy_sectors(bs, m->offset / BDRV_SECTOR_SIZE, m->alloc_offset,
686 r->offset / BDRV_SECTOR_SIZE,
687 r->offset / BDRV_SECTOR_SIZE + r->nb_sectors);
688 qemu_co_mutex_lock(&s->lock);
689
690 if (ret < 0) {
691 return ret;
692 }
693
694 /*
695 * Before we update the L2 table to actually point to the new cluster, we
696 * need to be sure that the refcounts have been increased and COW was
697 * handled.
698 */
699 qcow2_cache_depends_on_flush(s->l2_table_cache);
700
701 return 0;
702}
703
148da7ea 704int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
45aba42f
KW
705{
706 BDRVQcowState *s = bs->opaque;
707 int i, j = 0, l2_index, ret;
593fb83c 708 uint64_t *old_cluster, *l2_table;
250196f1 709 uint64_t cluster_offset = m->alloc_offset;
45aba42f 710
3cce16f4 711 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters);
f50f88b9 712 assert(m->nb_clusters > 0);
45aba42f 713
5839e53b 714 old_cluster = g_try_new(uint64_t, m->nb_clusters);
de82815d
KW
715 if (old_cluster == NULL) {
716 ret = -ENOMEM;
717 goto err;
718 }
45aba42f
KW
719
720 /* copy content of unmodified sectors */
593fb83c
KW
721 ret = perform_cow(bs, m, &m->cow_start);
722 if (ret < 0) {
723 goto err;
45aba42f
KW
724 }
725
593fb83c
KW
726 ret = perform_cow(bs, m, &m->cow_end);
727 if (ret < 0) {
728 goto err;
29c1a730
KW
729 }
730
593fb83c 731 /* Update L2 table. */
74c4510a 732 if (s->use_lazy_refcounts) {
280d3735
KW
733 qcow2_mark_dirty(bs);
734 }
bfe8043e
SH
735 if (qcow2_need_accurate_refcounts(s)) {
736 qcow2_cache_set_dependency(bs, s->l2_table_cache,
737 s->refcount_block_cache);
738 }
280d3735 739
3948d1d4 740 ret = get_cluster_table(bs, m->offset, &l2_table, &l2_index);
1e3e8f1a 741 if (ret < 0) {
45aba42f 742 goto err;
1e3e8f1a 743 }
29c1a730 744 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
45aba42f 745
c01dbccb 746 assert(l2_index + m->nb_clusters <= s->l2_size);
45aba42f
KW
747 for (i = 0; i < m->nb_clusters; i++) {
748 /* if two concurrent writes happen to the same unallocated cluster
749 * each write allocates separate cluster and writes data concurrently.
750 * The first one to complete updates l2 table with pointer to its
751 * cluster the second one has to do RMW (which is done above by
752 * copy_sectors()), update l2 table with its cluster pointer and free
753 * old cluster. This is what this loop does */
754 if(l2_table[l2_index + i] != 0)
755 old_cluster[j++] = l2_table[l2_index + i];
756
757 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
758 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
759 }
760
9f8e668e 761
29c1a730 762 ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
c835d00f 763 if (ret < 0) {
45aba42f 764 goto err;
4c1612d9 765 }
45aba42f 766
7ec5e6a4
KW
767 /*
768 * If this was a COW, we need to decrease the refcount of the old cluster.
769 * Also flush bs->file to get the right order for L2 and refcount update.
6cfcb9b8
KW
770 *
771 * Don't discard clusters that reach a refcount of 0 (e.g. compressed
772 * clusters), the next write will reuse them anyway.
7ec5e6a4
KW
773 */
774 if (j != 0) {
7ec5e6a4 775 for (i = 0; i < j; i++) {
6cfcb9b8
KW
776 qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1,
777 QCOW2_DISCARD_NEVER);
7ec5e6a4
KW
778 }
779 }
45aba42f
KW
780
781 ret = 0;
782err:
7267c094 783 g_free(old_cluster);
45aba42f
KW
784 return ret;
785 }
786
bf319ece
KW
787/*
788 * Returns the number of contiguous clusters that can be used for an allocating
789 * write, but require COW to be performed (this includes yet unallocated space,
790 * which must copy from the backing file)
791 */
792static int count_cow_clusters(BDRVQcowState *s, int nb_clusters,
793 uint64_t *l2_table, int l2_index)
794{
143550a8 795 int i;
bf319ece 796
143550a8
KW
797 for (i = 0; i < nb_clusters; i++) {
798 uint64_t l2_entry = be64_to_cpu(l2_table[l2_index + i]);
799 int cluster_type = qcow2_get_cluster_type(l2_entry);
800
801 switch(cluster_type) {
802 case QCOW2_CLUSTER_NORMAL:
803 if (l2_entry & QCOW_OFLAG_COPIED) {
804 goto out;
805 }
bf319ece 806 break;
143550a8
KW
807 case QCOW2_CLUSTER_UNALLOCATED:
808 case QCOW2_CLUSTER_COMPRESSED:
6377af48 809 case QCOW2_CLUSTER_ZERO:
bf319ece 810 break;
143550a8
KW
811 default:
812 abort();
813 }
bf319ece
KW
814 }
815
143550a8 816out:
bf319ece
KW
817 assert(i <= nb_clusters);
818 return i;
819}
820
250196f1 821/*
226c3c26
KW
822 * Check if there already is an AIO write request in flight which allocates
823 * the same cluster. In this case we need to wait until the previous
824 * request has completed and updated the L2 table accordingly.
65eb2e35
KW
825 *
826 * Returns:
827 * 0 if there was no dependency. *cur_bytes indicates the number of
828 * bytes from guest_offset that can be read before the next
829 * dependency must be processed (or the request is complete)
830 *
831 * -EAGAIN if we had to wait for another request, previously gathered
832 * information on cluster allocation may be invalid now. The caller
833 * must start over anyway, so consider *cur_bytes undefined.
250196f1 834 */
226c3c26 835static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,
ecdd5333 836 uint64_t *cur_bytes, QCowL2Meta **m)
250196f1
KW
837{
838 BDRVQcowState *s = bs->opaque;
250196f1 839 QCowL2Meta *old_alloc;
65eb2e35 840 uint64_t bytes = *cur_bytes;
250196f1 841
250196f1
KW
842 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
843
65eb2e35
KW
844 uint64_t start = guest_offset;
845 uint64_t end = start + bytes;
846 uint64_t old_start = l2meta_cow_start(old_alloc);
847 uint64_t old_end = l2meta_cow_end(old_alloc);
250196f1 848
d9d74f41 849 if (end <= old_start || start >= old_end) {
250196f1
KW
850 /* No intersection */
851 } else {
852 if (start < old_start) {
853 /* Stop at the start of a running allocation */
65eb2e35 854 bytes = old_start - start;
250196f1 855 } else {
65eb2e35 856 bytes = 0;
250196f1
KW
857 }
858
ecdd5333
KW
859 /* Stop if already an l2meta exists. After yielding, it wouldn't
860 * be valid any more, so we'd have to clean up the old L2Metas
861 * and deal with requests depending on them before starting to
862 * gather new ones. Not worth the trouble. */
863 if (bytes == 0 && *m) {
864 *cur_bytes = 0;
865 return 0;
866 }
867
65eb2e35 868 if (bytes == 0) {
250196f1
KW
869 /* Wait for the dependency to complete. We need to recheck
870 * the free/allocated clusters when we continue. */
871 qemu_co_mutex_unlock(&s->lock);
872 qemu_co_queue_wait(&old_alloc->dependent_requests);
873 qemu_co_mutex_lock(&s->lock);
874 return -EAGAIN;
875 }
876 }
877 }
878
65eb2e35
KW
879 /* Make sure that existing clusters and new allocations are only used up to
880 * the next dependency if we shortened the request above */
881 *cur_bytes = bytes;
250196f1 882
226c3c26
KW
883 return 0;
884}
885
0af729ec
KW
886/*
887 * Checks how many already allocated clusters that don't require a copy on
888 * write there are at the given guest_offset (up to *bytes). If
889 * *host_offset is not zero, only physically contiguous clusters beginning at
890 * this host offset are counted.
891 *
411d62b0
KW
892 * Note that guest_offset may not be cluster aligned. In this case, the
893 * returned *host_offset points to exact byte referenced by guest_offset and
894 * therefore isn't cluster aligned as well.
0af729ec
KW
895 *
896 * Returns:
897 * 0: if no allocated clusters are available at the given offset.
898 * *bytes is normally unchanged. It is set to 0 if the cluster
899 * is allocated and doesn't need COW, but doesn't have the right
900 * physical offset.
901 *
902 * 1: if allocated clusters that don't require a COW are available at
903 * the requested offset. *bytes may have decreased and describes
904 * the length of the area that can be written to.
905 *
906 * -errno: in error cases
0af729ec
KW
907 */
908static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,
c53ede9f 909 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
0af729ec
KW
910{
911 BDRVQcowState *s = bs->opaque;
912 int l2_index;
913 uint64_t cluster_offset;
914 uint64_t *l2_table;
acb0467f 915 unsigned int nb_clusters;
c53ede9f 916 unsigned int keep_clusters;
0af729ec
KW
917 int ret, pret;
918
919 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset,
920 *bytes);
0af729ec 921
411d62b0
KW
922 assert(*host_offset == 0 || offset_into_cluster(s, guest_offset)
923 == offset_into_cluster(s, *host_offset));
924
acb0467f
KW
925 /*
926 * Calculate the number of clusters to look for. We stop at L2 table
927 * boundaries to keep things simple.
928 */
929 nb_clusters =
930 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
931
932 l2_index = offset_to_l2_index(s, guest_offset);
933 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
934
0af729ec
KW
935 /* Find L2 entry for the first involved cluster */
936 ret = get_cluster_table(bs, guest_offset, &l2_table, &l2_index);
937 if (ret < 0) {
938 return ret;
939 }
940
941 cluster_offset = be64_to_cpu(l2_table[l2_index]);
942
943 /* Check how many clusters are already allocated and don't need COW */
944 if (qcow2_get_cluster_type(cluster_offset) == QCOW2_CLUSTER_NORMAL
945 && (cluster_offset & QCOW_OFLAG_COPIED))
946 {
e62daaf6
KW
947 /* If a specific host_offset is required, check it */
948 bool offset_matches =
949 (cluster_offset & L2E_OFFSET_MASK) == *host_offset;
950
951 if (*host_offset != 0 && !offset_matches) {
952 *bytes = 0;
953 ret = 0;
954 goto out;
955 }
956
0af729ec 957 /* We keep all QCOW_OFLAG_COPIED clusters */
c53ede9f 958 keep_clusters =
acb0467f 959 count_contiguous_clusters(nb_clusters, s->cluster_size,
61653008 960 &l2_table[l2_index],
0af729ec 961 QCOW_OFLAG_COPIED | QCOW_OFLAG_ZERO);
c53ede9f
KW
962 assert(keep_clusters <= nb_clusters);
963
964 *bytes = MIN(*bytes,
965 keep_clusters * s->cluster_size
966 - offset_into_cluster(s, guest_offset));
0af729ec
KW
967
968 ret = 1;
969 } else {
0af729ec
KW
970 ret = 0;
971 }
972
0af729ec 973 /* Cleanup */
e62daaf6 974out:
0af729ec
KW
975 pret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
976 if (pret < 0) {
977 return pret;
978 }
979
e62daaf6
KW
980 /* Only return a host offset if we actually made progress. Otherwise we
981 * would make requirements for handle_alloc() that it can't fulfill */
982 if (ret) {
411d62b0
KW
983 *host_offset = (cluster_offset & L2E_OFFSET_MASK)
984 + offset_into_cluster(s, guest_offset);
e62daaf6
KW
985 }
986
0af729ec
KW
987 return ret;
988}
989
226c3c26
KW
990/*
991 * Allocates new clusters for the given guest_offset.
992 *
993 * At most *nb_clusters are allocated, and on return *nb_clusters is updated to
994 * contain the number of clusters that have been allocated and are contiguous
995 * in the image file.
996 *
997 * If *host_offset is non-zero, it specifies the offset in the image file at
998 * which the new clusters must start. *nb_clusters can be 0 on return in this
999 * case if the cluster at host_offset is already in use. If *host_offset is
1000 * zero, the clusters can be allocated anywhere in the image file.
1001 *
1002 * *host_offset is updated to contain the offset into the image file at which
1003 * the first allocated cluster starts.
1004 *
1005 * Return 0 on success and -errno in error cases. -EAGAIN means that the
1006 * function has been waiting for another request and the allocation must be
1007 * restarted, but the whole request should not be failed.
1008 */
1009static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset,
1010 uint64_t *host_offset, unsigned int *nb_clusters)
1011{
1012 BDRVQcowState *s = bs->opaque;
226c3c26
KW
1013
1014 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset,
1015 *host_offset, *nb_clusters);
1016
250196f1
KW
1017 /* Allocate new clusters */
1018 trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
1019 if (*host_offset == 0) {
df021791
KW
1020 int64_t cluster_offset =
1021 qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size);
1022 if (cluster_offset < 0) {
1023 return cluster_offset;
1024 }
1025 *host_offset = cluster_offset;
1026 return 0;
250196f1 1027 } else {
17a71e58 1028 int ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters);
df021791
KW
1029 if (ret < 0) {
1030 return ret;
1031 }
1032 *nb_clusters = ret;
1033 return 0;
250196f1 1034 }
250196f1
KW
1035}
1036
10f0ed8b
KW
1037/*
1038 * Allocates new clusters for an area that either is yet unallocated or needs a
1039 * copy on write. If *host_offset is non-zero, clusters are only allocated if
1040 * the new allocation can match the specified host offset.
1041 *
411d62b0
KW
1042 * Note that guest_offset may not be cluster aligned. In this case, the
1043 * returned *host_offset points to exact byte referenced by guest_offset and
1044 * therefore isn't cluster aligned as well.
10f0ed8b
KW
1045 *
1046 * Returns:
1047 * 0: if no clusters could be allocated. *bytes is set to 0,
1048 * *host_offset is left unchanged.
1049 *
1050 * 1: if new clusters were allocated. *bytes may be decreased if the
1051 * new allocation doesn't cover all of the requested area.
1052 * *host_offset is updated to contain the host offset of the first
1053 * newly allocated cluster.
1054 *
1055 * -errno: in error cases
10f0ed8b
KW
1056 */
1057static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
c37f4cd7 1058 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
10f0ed8b
KW
1059{
1060 BDRVQcowState *s = bs->opaque;
1061 int l2_index;
1062 uint64_t *l2_table;
1063 uint64_t entry;
f5bc6350 1064 unsigned int nb_clusters;
10f0ed8b
KW
1065 int ret;
1066
10f0ed8b 1067 uint64_t alloc_cluster_offset;
10f0ed8b
KW
1068
1069 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset,
1070 *bytes);
1071 assert(*bytes > 0);
1072
f5bc6350
KW
1073 /*
1074 * Calculate the number of clusters to look for. We stop at L2 table
1075 * boundaries to keep things simple.
1076 */
c37f4cd7
KW
1077 nb_clusters =
1078 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1079
f5bc6350 1080 l2_index = offset_to_l2_index(s, guest_offset);
c37f4cd7 1081 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
f5bc6350 1082
10f0ed8b
KW
1083 /* Find L2 entry for the first involved cluster */
1084 ret = get_cluster_table(bs, guest_offset, &l2_table, &l2_index);
1085 if (ret < 0) {
1086 return ret;
1087 }
1088
3b8e2e26 1089 entry = be64_to_cpu(l2_table[l2_index]);
10f0ed8b
KW
1090
1091 /* For the moment, overwrite compressed clusters one by one */
1092 if (entry & QCOW_OFLAG_COMPRESSED) {
1093 nb_clusters = 1;
1094 } else {
3b8e2e26 1095 nb_clusters = count_cow_clusters(s, nb_clusters, l2_table, l2_index);
10f0ed8b
KW
1096 }
1097
ecdd5333
KW
1098 /* This function is only called when there were no non-COW clusters, so if
1099 * we can't find any unallocated or COW clusters either, something is
1100 * wrong with our code. */
1101 assert(nb_clusters > 0);
1102
10f0ed8b
KW
1103 ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
1104 if (ret < 0) {
1105 return ret;
1106 }
1107
10f0ed8b 1108 /* Allocate, if necessary at a given offset in the image file */
411d62b0 1109 alloc_cluster_offset = start_of_cluster(s, *host_offset);
83baa9a4 1110 ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,
10f0ed8b
KW
1111 &nb_clusters);
1112 if (ret < 0) {
1113 goto fail;
1114 }
1115
83baa9a4
KW
1116 /* Can't extend contiguous allocation */
1117 if (nb_clusters == 0) {
10f0ed8b
KW
1118 *bytes = 0;
1119 return 0;
1120 }
1121
ff52aab2
HR
1122 /* !*host_offset would overwrite the image header and is reserved for "no
1123 * host offset preferred". If 0 was a valid host offset, it'd trigger the
1124 * following overlap check; do that now to avoid having an invalid value in
1125 * *host_offset. */
1126 if (!alloc_cluster_offset) {
1127 ret = qcow2_pre_write_overlap_check(bs, 0, alloc_cluster_offset,
1128 nb_clusters * s->cluster_size);
1129 assert(ret < 0);
1130 goto fail;
1131 }
1132
83baa9a4
KW
1133 /*
1134 * Save info needed for meta data update.
1135 *
1136 * requested_sectors: Number of sectors from the start of the first
1137 * newly allocated cluster to the end of the (possibly shortened
1138 * before) write request.
1139 *
1140 * avail_sectors: Number of sectors from the start of the first
1141 * newly allocated to the end of the last newly allocated cluster.
1142 *
1143 * nb_sectors: The number of sectors from the start of the first
1144 * newly allocated cluster to the end of the area that the write
1145 * request actually writes to (excluding COW at the end)
1146 */
1147 int requested_sectors =
1148 (*bytes + offset_into_cluster(s, guest_offset))
1149 >> BDRV_SECTOR_BITS;
1150 int avail_sectors = nb_clusters
1151 << (s->cluster_bits - BDRV_SECTOR_BITS);
1152 int alloc_n_start = offset_into_cluster(s, guest_offset)
1153 >> BDRV_SECTOR_BITS;
1154 int nb_sectors = MIN(requested_sectors, avail_sectors);
88c6588c 1155 QCowL2Meta *old_m = *m;
83baa9a4 1156
83baa9a4
KW
1157 *m = g_malloc0(sizeof(**m));
1158
1159 **m = (QCowL2Meta) {
88c6588c
KW
1160 .next = old_m,
1161
411d62b0 1162 .alloc_offset = alloc_cluster_offset,
83baa9a4
KW
1163 .offset = start_of_cluster(s, guest_offset),
1164 .nb_clusters = nb_clusters,
1165 .nb_available = nb_sectors,
1166
1167 .cow_start = {
1168 .offset = 0,
1169 .nb_sectors = alloc_n_start,
1170 },
1171 .cow_end = {
1172 .offset = nb_sectors * BDRV_SECTOR_SIZE,
1173 .nb_sectors = avail_sectors - nb_sectors,
1174 },
1175 };
1176 qemu_co_queue_init(&(*m)->dependent_requests);
1177 QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);
1178
411d62b0 1179 *host_offset = alloc_cluster_offset + offset_into_cluster(s, guest_offset);
83baa9a4
KW
1180 *bytes = MIN(*bytes, (nb_sectors * BDRV_SECTOR_SIZE)
1181 - offset_into_cluster(s, guest_offset));
1182 assert(*bytes != 0);
1183
10f0ed8b
KW
1184 return 1;
1185
1186fail:
1187 if (*m && (*m)->nb_clusters > 0) {
1188 QLIST_REMOVE(*m, next_in_flight);
1189 }
1190 return ret;
1191}
1192
45aba42f
KW
1193/*
1194 * alloc_cluster_offset
1195 *
250196f1
KW
1196 * For a given offset on the virtual disk, find the cluster offset in qcow2
1197 * file. If the offset is not found, allocate a new cluster.
45aba42f 1198 *
250196f1 1199 * If the cluster was already allocated, m->nb_clusters is set to 0 and
a7912369 1200 * other fields in m are meaningless.
148da7ea
KW
1201 *
1202 * If the cluster is newly allocated, m->nb_clusters is set to the number of
68d100e9
KW
1203 * contiguous clusters that have been allocated. In this case, the other
1204 * fields of m are valid and contain information about the first allocated
1205 * cluster.
45aba42f 1206 *
68d100e9
KW
1207 * If the request conflicts with another write request in flight, the coroutine
1208 * is queued and will be reentered when the dependency has completed.
148da7ea
KW
1209 *
1210 * Return 0 on success and -errno in error cases
45aba42f 1211 */
f4f0d391 1212int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
16f0587e 1213 int *num, uint64_t *host_offset, QCowL2Meta **m)
45aba42f
KW
1214{
1215 BDRVQcowState *s = bs->opaque;
710c2496 1216 uint64_t start, remaining;
250196f1 1217 uint64_t cluster_offset;
65eb2e35 1218 uint64_t cur_bytes;
710c2496 1219 int ret;
45aba42f 1220
16f0587e 1221 trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, *num);
3cce16f4 1222
16f0587e 1223 assert((offset & ~BDRV_SECTOR_MASK) == 0);
710c2496 1224
72424114 1225again:
16f0587e
HT
1226 start = offset;
1227 remaining = *num << BDRV_SECTOR_BITS;
0af729ec
KW
1228 cluster_offset = 0;
1229 *host_offset = 0;
ecdd5333
KW
1230 cur_bytes = 0;
1231 *m = NULL;
0af729ec 1232
2c3b32d2 1233 while (true) {
ecdd5333
KW
1234
1235 if (!*host_offset) {
1236 *host_offset = start_of_cluster(s, cluster_offset);
1237 }
1238
1239 assert(remaining >= cur_bytes);
1240
1241 start += cur_bytes;
1242 remaining -= cur_bytes;
1243 cluster_offset += cur_bytes;
1244
1245 if (remaining == 0) {
1246 break;
1247 }
1248
1249 cur_bytes = remaining;
1250
2c3b32d2
KW
1251 /*
1252 * Now start gathering as many contiguous clusters as possible:
1253 *
1254 * 1. Check for overlaps with in-flight allocations
1255 *
1256 * a) Overlap not in the first cluster -> shorten this request and
1257 * let the caller handle the rest in its next loop iteration.
1258 *
1259 * b) Real overlaps of two requests. Yield and restart the search
1260 * for contiguous clusters (the situation could have changed
1261 * while we were sleeping)
1262 *
1263 * c) TODO: Request starts in the same cluster as the in-flight
1264 * allocation ends. Shorten the COW of the in-fight allocation,
1265 * set cluster_offset to write to the same cluster and set up
1266 * the right synchronisation between the in-flight request and
1267 * the new one.
1268 */
ecdd5333 1269 ret = handle_dependencies(bs, start, &cur_bytes, m);
2c3b32d2 1270 if (ret == -EAGAIN) {
ecdd5333
KW
1271 /* Currently handle_dependencies() doesn't yield if we already had
1272 * an allocation. If it did, we would have to clean up the L2Meta
1273 * structs before starting over. */
1274 assert(*m == NULL);
2c3b32d2
KW
1275 goto again;
1276 } else if (ret < 0) {
1277 return ret;
ecdd5333
KW
1278 } else if (cur_bytes == 0) {
1279 break;
2c3b32d2
KW
1280 } else {
1281 /* handle_dependencies() may have decreased cur_bytes (shortened
1282 * the allocations below) so that the next dependency is processed
1283 * correctly during the next loop iteration. */
0af729ec 1284 }
710c2496 1285
2c3b32d2
KW
1286 /*
1287 * 2. Count contiguous COPIED clusters.
1288 */
1289 ret = handle_copied(bs, start, &cluster_offset, &cur_bytes, m);
1290 if (ret < 0) {
1291 return ret;
1292 } else if (ret) {
ecdd5333 1293 continue;
2c3b32d2
KW
1294 } else if (cur_bytes == 0) {
1295 break;
1296 }
060bee89 1297
2c3b32d2
KW
1298 /*
1299 * 3. If the request still hasn't completed, allocate new clusters,
1300 * considering any cluster_offset of steps 1c or 2.
1301 */
1302 ret = handle_alloc(bs, start, &cluster_offset, &cur_bytes, m);
1303 if (ret < 0) {
1304 return ret;
1305 } else if (ret) {
ecdd5333 1306 continue;
2c3b32d2
KW
1307 } else {
1308 assert(cur_bytes == 0);
1309 break;
1310 }
f5bc6350 1311 }
10f0ed8b 1312
16f0587e 1313 *num -= remaining >> BDRV_SECTOR_BITS;
710c2496
KW
1314 assert(*num > 0);
1315 assert(*host_offset != 0);
45aba42f 1316
148da7ea 1317 return 0;
45aba42f
KW
1318}
1319
1320static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
1321 const uint8_t *buf, int buf_size)
1322{
1323 z_stream strm1, *strm = &strm1;
1324 int ret, out_len;
1325
1326 memset(strm, 0, sizeof(*strm));
1327
1328 strm->next_in = (uint8_t *)buf;
1329 strm->avail_in = buf_size;
1330 strm->next_out = out_buf;
1331 strm->avail_out = out_buf_size;
1332
1333 ret = inflateInit2(strm, -12);
1334 if (ret != Z_OK)
1335 return -1;
1336 ret = inflate(strm, Z_FINISH);
1337 out_len = strm->next_out - out_buf;
1338 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
1339 out_len != out_buf_size) {
1340 inflateEnd(strm);
1341 return -1;
1342 }
1343 inflateEnd(strm);
1344 return 0;
1345}
1346
66f82cee 1347int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
45aba42f 1348{
66f82cee 1349 BDRVQcowState *s = bs->opaque;
45aba42f
KW
1350 int ret, csize, nb_csectors, sector_offset;
1351 uint64_t coffset;
1352
1353 coffset = cluster_offset & s->cluster_offset_mask;
1354 if (s->cluster_cache_offset != coffset) {
1355 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
1356 sector_offset = coffset & 511;
1357 csize = nb_csectors * 512 - sector_offset;
66f82cee
KW
1358 BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
1359 ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data, nb_csectors);
45aba42f 1360 if (ret < 0) {
8af36488 1361 return ret;
45aba42f
KW
1362 }
1363 if (decompress_buffer(s->cluster_cache, s->cluster_size,
1364 s->cluster_data + sector_offset, csize) < 0) {
8af36488 1365 return -EIO;
45aba42f
KW
1366 }
1367 s->cluster_cache_offset = coffset;
1368 }
1369 return 0;
1370}
5ea929e3
KW
1371
1372/*
1373 * This discards as many clusters of nb_clusters as possible at once (i.e.
1374 * all clusters in the same L2 table) and returns the number of discarded
1375 * clusters.
1376 */
1377static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
670df5e3 1378 unsigned int nb_clusters, enum qcow2_discard_type type)
5ea929e3
KW
1379{
1380 BDRVQcowState *s = bs->opaque;
3948d1d4 1381 uint64_t *l2_table;
5ea929e3
KW
1382 int l2_index;
1383 int ret;
1384 int i;
1385
3948d1d4 1386 ret = get_cluster_table(bs, offset, &l2_table, &l2_index);
5ea929e3
KW
1387 if (ret < 0) {
1388 return ret;
1389 }
1390
1391 /* Limit nb_clusters to one L2 table */
1392 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
1393
1394 for (i = 0; i < nb_clusters; i++) {
c883db0d 1395 uint64_t old_l2_entry;
5ea929e3 1396
c883db0d 1397 old_l2_entry = be64_to_cpu(l2_table[l2_index + i]);
a71835a0
KW
1398
1399 /*
1400 * Make sure that a discarded area reads back as zeroes for v3 images
1401 * (we cannot do it for v2 without actually writing a zero-filled
1402 * buffer). We can skip the operation if the cluster is already marked
1403 * as zero, or if it's unallocated and we don't have a backing file.
1404 *
1405 * TODO We might want to use bdrv_get_block_status(bs) here, but we're
1406 * holding s->lock, so that doesn't work today.
1407 */
c883db0d
HR
1408 switch (qcow2_get_cluster_type(old_l2_entry)) {
1409 case QCOW2_CLUSTER_UNALLOCATED:
1410 if (!bs->backing_hd) {
1411 continue;
1412 }
1413 break;
1414
1415 case QCOW2_CLUSTER_ZERO:
1416 continue;
1417
1418 case QCOW2_CLUSTER_NORMAL:
1419 case QCOW2_CLUSTER_COMPRESSED:
1420 break;
1421
1422 default:
1423 abort();
5ea929e3
KW
1424 }
1425
1426 /* First remove L2 entries */
1427 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
a71835a0
KW
1428 if (s->qcow_version >= 3) {
1429 l2_table[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
1430 } else {
1431 l2_table[l2_index + i] = cpu_to_be64(0);
1432 }
5ea929e3
KW
1433
1434 /* Then decrease the refcount */
c883db0d 1435 qcow2_free_any_clusters(bs, old_l2_entry, 1, type);
5ea929e3
KW
1436 }
1437
1438 ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
1439 if (ret < 0) {
1440 return ret;
1441 }
1442
1443 return nb_clusters;
1444}
1445
1446int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
670df5e3 1447 int nb_sectors, enum qcow2_discard_type type)
5ea929e3
KW
1448{
1449 BDRVQcowState *s = bs->opaque;
1450 uint64_t end_offset;
1451 unsigned int nb_clusters;
1452 int ret;
1453
1454 end_offset = offset + (nb_sectors << BDRV_SECTOR_BITS);
1455
1456 /* Round start up and end down */
1457 offset = align_offset(offset, s->cluster_size);
ac95acdb 1458 end_offset = start_of_cluster(s, end_offset);
5ea929e3
KW
1459
1460 if (offset > end_offset) {
1461 return 0;
1462 }
1463
1464 nb_clusters = size_to_clusters(s, end_offset - offset);
1465
0b919fae
KW
1466 s->cache_discards = true;
1467
5ea929e3
KW
1468 /* Each L2 table is handled by its own loop iteration */
1469 while (nb_clusters > 0) {
670df5e3 1470 ret = discard_single_l2(bs, offset, nb_clusters, type);
5ea929e3 1471 if (ret < 0) {
0b919fae 1472 goto fail;
5ea929e3
KW
1473 }
1474
1475 nb_clusters -= ret;
1476 offset += (ret * s->cluster_size);
1477 }
1478
0b919fae
KW
1479 ret = 0;
1480fail:
1481 s->cache_discards = false;
1482 qcow2_process_discards(bs, ret);
1483
1484 return ret;
5ea929e3 1485}
621f0589
KW
1486
1487/*
1488 * This zeroes as many clusters of nb_clusters as possible at once (i.e.
1489 * all clusters in the same L2 table) and returns the number of zeroed
1490 * clusters.
1491 */
1492static int zero_single_l2(BlockDriverState *bs, uint64_t offset,
1493 unsigned int nb_clusters)
1494{
1495 BDRVQcowState *s = bs->opaque;
1496 uint64_t *l2_table;
1497 int l2_index;
1498 int ret;
1499 int i;
1500
1501 ret = get_cluster_table(bs, offset, &l2_table, &l2_index);
1502 if (ret < 0) {
1503 return ret;
1504 }
1505
1506 /* Limit nb_clusters to one L2 table */
1507 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
1508
1509 for (i = 0; i < nb_clusters; i++) {
1510 uint64_t old_offset;
1511
1512 old_offset = be64_to_cpu(l2_table[l2_index + i]);
1513
1514 /* Update L2 entries */
1515 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
1516 if (old_offset & QCOW_OFLAG_COMPRESSED) {
1517 l2_table[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
6cfcb9b8 1518 qcow2_free_any_clusters(bs, old_offset, 1, QCOW2_DISCARD_REQUEST);
621f0589
KW
1519 } else {
1520 l2_table[l2_index + i] |= cpu_to_be64(QCOW_OFLAG_ZERO);
1521 }
1522 }
1523
1524 ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
1525 if (ret < 0) {
1526 return ret;
1527 }
1528
1529 return nb_clusters;
1530}
1531
1532int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors)
1533{
1534 BDRVQcowState *s = bs->opaque;
1535 unsigned int nb_clusters;
1536 int ret;
1537
1538 /* The zero flag is only supported by version 3 and newer */
1539 if (s->qcow_version < 3) {
1540 return -ENOTSUP;
1541 }
1542
1543 /* Each L2 table is handled by its own loop iteration */
1544 nb_clusters = size_to_clusters(s, nb_sectors << BDRV_SECTOR_BITS);
1545
0b919fae
KW
1546 s->cache_discards = true;
1547
621f0589
KW
1548 while (nb_clusters > 0) {
1549 ret = zero_single_l2(bs, offset, nb_clusters);
1550 if (ret < 0) {
0b919fae 1551 goto fail;
621f0589
KW
1552 }
1553
1554 nb_clusters -= ret;
1555 offset += (ret * s->cluster_size);
1556 }
1557
0b919fae
KW
1558 ret = 0;
1559fail:
1560 s->cache_discards = false;
1561 qcow2_process_discards(bs, ret);
1562
1563 return ret;
621f0589 1564}
32b6444d
HR
1565
1566/*
1567 * Expands all zero clusters in a specific L1 table (or deallocates them, for
1568 * non-backed non-pre-allocated zero clusters).
1569 *
1570 * expanded_clusters is a bitmap where every bit corresponds to one cluster in
1571 * the image file; a bit gets set if the corresponding cluster has been used for
1572 * zero expansion (i.e., has been filled with zeroes and is referenced from an
1573 * L2 table). nb_clusters contains the total cluster count of the image file,
1574 * i.e., the number of bits in expanded_clusters.
1575 */
1576static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
e390cf5a
HR
1577 int l1_size, uint8_t **expanded_clusters,
1578 uint64_t *nb_clusters)
32b6444d
HR
1579{
1580 BDRVQcowState *s = bs->opaque;
1581 bool is_active_l1 = (l1_table == s->l1_table);
1582 uint64_t *l2_table = NULL;
1583 int ret;
1584 int i, j;
1585
1586 if (!is_active_l1) {
1587 /* inactive L2 tables require a buffer to be stored in when loading
1588 * them from disk */
de82815d
KW
1589 l2_table = qemu_try_blockalign(bs->file, s->cluster_size);
1590 if (l2_table == NULL) {
1591 return -ENOMEM;
1592 }
32b6444d
HR
1593 }
1594
1595 for (i = 0; i < l1_size; i++) {
1596 uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
1597 bool l2_dirty = false;
1598
1599 if (!l2_offset) {
1600 /* unallocated */
1601 continue;
1602 }
1603
1604 if (is_active_l1) {
1605 /* get active L2 tables from cache */
1606 ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset,
1607 (void **)&l2_table);
1608 } else {
1609 /* load inactive L2 tables from disk */
1610 ret = bdrv_read(bs->file, l2_offset / BDRV_SECTOR_SIZE,
1611 (void *)l2_table, s->cluster_sectors);
1612 }
1613 if (ret < 0) {
1614 goto fail;
1615 }
1616
1617 for (j = 0; j < s->l2_size; j++) {
1618 uint64_t l2_entry = be64_to_cpu(l2_table[j]);
1619 int64_t offset = l2_entry & L2E_OFFSET_MASK, cluster_index;
1620 int cluster_type = qcow2_get_cluster_type(l2_entry);
320c7066 1621 bool preallocated = offset != 0;
32b6444d
HR
1622
1623 if (cluster_type == QCOW2_CLUSTER_NORMAL) {
1624 cluster_index = offset >> s->cluster_bits;
e390cf5a
HR
1625 assert((cluster_index >= 0) && (cluster_index < *nb_clusters));
1626 if ((*expanded_clusters)[cluster_index / 8] &
32b6444d
HR
1627 (1 << (cluster_index % 8))) {
1628 /* Probably a shared L2 table; this cluster was a zero
1629 * cluster which has been expanded, its refcount
1630 * therefore most likely requires an update. */
1631 ret = qcow2_update_cluster_refcount(bs, cluster_index, 1,
1632 QCOW2_DISCARD_NEVER);
1633 if (ret < 0) {
1634 goto fail;
1635 }
1636 /* Since we just increased the refcount, the COPIED flag may
1637 * no longer be set. */
1638 l2_table[j] = cpu_to_be64(l2_entry & ~QCOW_OFLAG_COPIED);
1639 l2_dirty = true;
1640 }
1641 continue;
1642 }
1643 else if (qcow2_get_cluster_type(l2_entry) != QCOW2_CLUSTER_ZERO) {
1644 continue;
1645 }
1646
320c7066 1647 if (!preallocated) {
32b6444d
HR
1648 if (!bs->backing_hd) {
1649 /* not backed; therefore we can simply deallocate the
1650 * cluster */
1651 l2_table[j] = 0;
1652 l2_dirty = true;
1653 continue;
1654 }
1655
1656 offset = qcow2_alloc_clusters(bs, s->cluster_size);
1657 if (offset < 0) {
1658 ret = offset;
1659 goto fail;
1660 }
1661 }
1662
231bb267 1663 ret = qcow2_pre_write_overlap_check(bs, 0, offset, s->cluster_size);
32b6444d 1664 if (ret < 0) {
320c7066
HR
1665 if (!preallocated) {
1666 qcow2_free_clusters(bs, offset, s->cluster_size,
1667 QCOW2_DISCARD_ALWAYS);
1668 }
32b6444d
HR
1669 goto fail;
1670 }
1671
1672 ret = bdrv_write_zeroes(bs->file, offset / BDRV_SECTOR_SIZE,
aa7bfbff 1673 s->cluster_sectors, 0);
32b6444d 1674 if (ret < 0) {
320c7066
HR
1675 if (!preallocated) {
1676 qcow2_free_clusters(bs, offset, s->cluster_size,
1677 QCOW2_DISCARD_ALWAYS);
1678 }
32b6444d
HR
1679 goto fail;
1680 }
1681
1682 l2_table[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
1683 l2_dirty = true;
1684
1685 cluster_index = offset >> s->cluster_bits;
e390cf5a
HR
1686
1687 if (cluster_index >= *nb_clusters) {
1688 uint64_t old_bitmap_size = (*nb_clusters + 7) / 8;
1689 uint64_t new_bitmap_size;
1690 /* The offset may lie beyond the old end of the underlying image
1691 * file for growable files only */
1692 assert(bs->file->growable);
1693 *nb_clusters = size_to_clusters(s, bs->file->total_sectors *
1694 BDRV_SECTOR_SIZE);
1695 new_bitmap_size = (*nb_clusters + 7) / 8;
1696 *expanded_clusters = g_realloc(*expanded_clusters,
1697 new_bitmap_size);
1698 /* clear the newly allocated space */
1699 memset(&(*expanded_clusters)[old_bitmap_size], 0,
1700 new_bitmap_size - old_bitmap_size);
1701 }
1702
1703 assert((cluster_index >= 0) && (cluster_index < *nb_clusters));
1704 (*expanded_clusters)[cluster_index / 8] |= 1 << (cluster_index % 8);
32b6444d
HR
1705 }
1706
1707 if (is_active_l1) {
1708 if (l2_dirty) {
1709 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
1710 qcow2_cache_depends_on_flush(s->l2_table_cache);
1711 }
1712 ret = qcow2_cache_put(bs, s->l2_table_cache, (void **)&l2_table);
1713 if (ret < 0) {
1714 l2_table = NULL;
1715 goto fail;
1716 }
1717 } else {
1718 if (l2_dirty) {
231bb267
HR
1719 ret = qcow2_pre_write_overlap_check(bs,
1720 QCOW2_OL_INACTIVE_L2 | QCOW2_OL_ACTIVE_L2, l2_offset,
32b6444d
HR
1721 s->cluster_size);
1722 if (ret < 0) {
1723 goto fail;
1724 }
1725
1726 ret = bdrv_write(bs->file, l2_offset / BDRV_SECTOR_SIZE,
1727 (void *)l2_table, s->cluster_sectors);
1728 if (ret < 0) {
1729 goto fail;
1730 }
1731 }
1732 }
1733 }
1734
1735 ret = 0;
1736
1737fail:
1738 if (l2_table) {
1739 if (!is_active_l1) {
1740 qemu_vfree(l2_table);
1741 } else {
1742 if (ret < 0) {
1743 qcow2_cache_put(bs, s->l2_table_cache, (void **)&l2_table);
1744 } else {
1745 ret = qcow2_cache_put(bs, s->l2_table_cache,
1746 (void **)&l2_table);
1747 }
1748 }
1749 }
1750 return ret;
1751}
1752
1753/*
1754 * For backed images, expands all zero clusters on the image. For non-backed
1755 * images, deallocates all non-pre-allocated zero clusters (and claims the
1756 * allocation for pre-allocated ones). This is important for downgrading to a
1757 * qcow2 version which doesn't yet support metadata zero clusters.
1758 */
1759int qcow2_expand_zero_clusters(BlockDriverState *bs)
1760{
1761 BDRVQcowState *s = bs->opaque;
1762 uint64_t *l1_table = NULL;
32b6444d
HR
1763 uint64_t nb_clusters;
1764 uint8_t *expanded_clusters;
1765 int ret;
1766 int i, j;
1767
e390cf5a
HR
1768 nb_clusters = size_to_clusters(s, bs->file->total_sectors *
1769 BDRV_SECTOR_SIZE);
de82815d
KW
1770 expanded_clusters = g_try_malloc0((nb_clusters + 7) / 8);
1771 if (expanded_clusters == NULL) {
1772 ret = -ENOMEM;
1773 goto fail;
1774 }
32b6444d
HR
1775
1776 ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
e390cf5a 1777 &expanded_clusters, &nb_clusters);
32b6444d
HR
1778 if (ret < 0) {
1779 goto fail;
1780 }
1781
1782 /* Inactive L1 tables may point to active L2 tables - therefore it is
1783 * necessary to flush the L2 table cache before trying to access the L2
1784 * tables pointed to by inactive L1 entries (else we might try to expand
1785 * zero clusters that have already been expanded); furthermore, it is also
1786 * necessary to empty the L2 table cache, since it may contain tables which
1787 * are now going to be modified directly on disk, bypassing the cache.
1788 * qcow2_cache_empty() does both for us. */
1789 ret = qcow2_cache_empty(bs, s->l2_table_cache);
1790 if (ret < 0) {
1791 goto fail;
1792 }
1793
1794 for (i = 0; i < s->nb_snapshots; i++) {
1795 int l1_sectors = (s->snapshots[i].l1_size * sizeof(uint64_t) +
1796 BDRV_SECTOR_SIZE - 1) / BDRV_SECTOR_SIZE;
1797
1798 l1_table = g_realloc(l1_table, l1_sectors * BDRV_SECTOR_SIZE);
1799
1800 ret = bdrv_read(bs->file, s->snapshots[i].l1_table_offset /
1801 BDRV_SECTOR_SIZE, (void *)l1_table, l1_sectors);
1802 if (ret < 0) {
1803 goto fail;
1804 }
1805
1806 for (j = 0; j < s->snapshots[i].l1_size; j++) {
1807 be64_to_cpus(&l1_table[j]);
1808 }
1809
1810 ret = expand_zero_clusters_in_l1(bs, l1_table, s->snapshots[i].l1_size,
e390cf5a 1811 &expanded_clusters, &nb_clusters);
32b6444d
HR
1812 if (ret < 0) {
1813 goto fail;
1814 }
1815 }
1816
1817 ret = 0;
1818
1819fail:
1820 g_free(expanded_clusters);
1821 g_free(l1_table);
1822 return ret;
1823}
This page took 0.653632 seconds and 4 git commands to generate.