1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include <zlib.h>
27
28#include "block/block-io.h"
29#include "qapi/error.h"
30#include "qcow2.h"
31#include "qemu/bswap.h"
32#include "qemu/memalign.h"
33#include "trace.h"
34
35int coroutine_fn qcow2_shrink_l1_table(BlockDriverState *bs,
36 uint64_t exact_size)
37{
38 BDRVQcow2State *s = bs->opaque;
39 int new_l1_size, i, ret;
40
41 if (exact_size >= s->l1_size) {
42 return 0;
43 }
44
45 new_l1_size = exact_size;
46
47#ifdef DEBUG_ALLOC2
48 fprintf(stderr, "shrink l1_table from %d to %d\n", s->l1_size, new_l1_size);
49#endif
50
51 BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_WRITE_TABLE);
52 ret = bdrv_co_pwrite_zeroes(bs->file,
53 s->l1_table_offset + new_l1_size * L1E_SIZE,
54 (s->l1_size - new_l1_size) * L1E_SIZE, 0);
55 if (ret < 0) {
56 goto fail;
57 }
58
59 ret = bdrv_co_flush(bs->file->bs);
60 if (ret < 0) {
61 goto fail;
62 }
63
64 BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_FREE_L2_CLUSTERS);
65 for (i = s->l1_size - 1; i > new_l1_size - 1; i--) {
66 if ((s->l1_table[i] & L1E_OFFSET_MASK) == 0) {
67 continue;
68 }
69 qcow2_free_clusters(bs, s->l1_table[i] & L1E_OFFSET_MASK,
70 s->cluster_size, QCOW2_DISCARD_ALWAYS);
71 s->l1_table[i] = 0;
72 }
73 return 0;
74
75fail:
76
77
78
79
80
81 memset(s->l1_table + new_l1_size, 0,
82 (s->l1_size - new_l1_size) * L1E_SIZE);
83 return ret;
84}
85
86int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
87 bool exact_size)
88{
89 BDRVQcow2State *s = bs->opaque;
90 int new_l1_size2, ret, i;
91 uint64_t *new_l1_table;
92 int64_t old_l1_table_offset, old_l1_size;
93 int64_t new_l1_table_offset, new_l1_size;
94 uint8_t data[12];
95
96 if (min_size <= s->l1_size)
97 return 0;
98
99
100
101
102 if (min_size > INT_MAX / L1E_SIZE) {
103 return -EFBIG;
104 }
105
106 if (exact_size) {
107 new_l1_size = min_size;
108 } else {
109
110 new_l1_size = s->l1_size;
111 if (new_l1_size == 0) {
112 new_l1_size = 1;
113 }
114 while (min_size > new_l1_size) {
115 new_l1_size = DIV_ROUND_UP(new_l1_size * 3, 2);
116 }
117 }
118
119 QEMU_BUILD_BUG_ON(QCOW_MAX_L1_SIZE > INT_MAX);
120 if (new_l1_size > QCOW_MAX_L1_SIZE / L1E_SIZE) {
121 return -EFBIG;
122 }
123
124#ifdef DEBUG_ALLOC2
125 fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n",
126 s->l1_size, new_l1_size);
127#endif
128
129 new_l1_size2 = L1E_SIZE * new_l1_size;
130 new_l1_table = qemu_try_blockalign(bs->file->bs, new_l1_size2);
131 if (new_l1_table == NULL) {
132 return -ENOMEM;
133 }
134 memset(new_l1_table, 0, new_l1_size2);
135
136 if (s->l1_size) {
137 memcpy(new_l1_table, s->l1_table, s->l1_size * L1E_SIZE);
138 }
139
140
141 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
142 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
143 if (new_l1_table_offset < 0) {
144 qemu_vfree(new_l1_table);
145 return new_l1_table_offset;
146 }
147
148 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
149 if (ret < 0) {
150 goto fail;
151 }
152
153
154
155 ret = qcow2_pre_write_overlap_check(bs, 0, new_l1_table_offset,
156 new_l1_size2, false);
157 if (ret < 0) {
158 goto fail;
159 }
160
161 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
162 for(i = 0; i < s->l1_size; i++)
163 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
164 ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_size2,
165 new_l1_table, 0);
166 if (ret < 0)
167 goto fail;
168 for(i = 0; i < s->l1_size; i++)
169 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
170
171
172 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
173 stl_be_p(data, new_l1_size);
174 stq_be_p(data + 4, new_l1_table_offset);
175 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size),
176 sizeof(data), data, 0);
177 if (ret < 0) {
178 goto fail;
179 }
180 qemu_vfree(s->l1_table);
181 old_l1_table_offset = s->l1_table_offset;
182 s->l1_table_offset = new_l1_table_offset;
183 s->l1_table = new_l1_table;
184 old_l1_size = s->l1_size;
185 s->l1_size = new_l1_size;
186 qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * L1E_SIZE,
187 QCOW2_DISCARD_OTHER);
188 return 0;
189 fail:
190 qemu_vfree(new_l1_table);
191 qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2,
192 QCOW2_DISCARD_OTHER);
193 return ret;
194}
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210static int l2_load(BlockDriverState *bs, uint64_t offset,
211 uint64_t l2_offset, uint64_t **l2_slice)
212{
213 BDRVQcow2State *s = bs->opaque;
214 int start_of_slice = l2_entry_size(s) *
215 (offset_to_l2_index(s, offset) - offset_to_l2_slice_index(s, offset));
216
217 return qcow2_cache_get(bs, s->l2_table_cache, l2_offset + start_of_slice,
218 (void **)l2_slice);
219}
220
221
222
223
224
225
226int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
227{
228 BDRVQcow2State *s = bs->opaque;
229 int l1_start_index;
230 int i, ret;
231 int bufsize = MAX(L1E_SIZE,
232 MIN(bs->file->bs->bl.request_alignment, s->cluster_size));
233 int nentries = bufsize / L1E_SIZE;
234 g_autofree uint64_t *buf = g_try_new0(uint64_t, nentries);
235
236 if (buf == NULL) {
237 return -ENOMEM;
238 }
239
240 l1_start_index = QEMU_ALIGN_DOWN(l1_index, nentries);
241 for (i = 0; i < MIN(nentries, s->l1_size - l1_start_index); i++) {
242 buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
243 }
244
245 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
246 s->l1_table_offset + L1E_SIZE * l1_start_index, bufsize, false);
247 if (ret < 0) {
248 return ret;
249 }
250
251 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
252 ret = bdrv_pwrite_sync(bs->file,
253 s->l1_table_offset + L1E_SIZE * l1_start_index,
254 bufsize, buf, 0);
255 if (ret < 0) {
256 return ret;
257 }
258
259 return 0;
260}
261
262
263
264
265
266
267
268
269
270
271
272static int l2_allocate(BlockDriverState *bs, int l1_index)
273{
274 BDRVQcow2State *s = bs->opaque;
275 uint64_t old_l2_offset;
276 uint64_t *l2_slice = NULL;
277 unsigned slice, slice_size2, n_slices;
278 int64_t l2_offset;
279 int ret;
280
281 old_l2_offset = s->l1_table[l1_index];
282
283 trace_qcow2_l2_allocate(bs, l1_index);
284
285
286
287 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * l2_entry_size(s));
288 if (l2_offset < 0) {
289 ret = l2_offset;
290 goto fail;
291 }
292
293
294 assert((l2_offset & L1E_OFFSET_MASK) == l2_offset);
295
296
297 if (l2_offset == 0) {
298 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
299 "allocation of L2 table at offset 0");
300 ret = -EIO;
301 goto fail;
302 }
303
304 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
305 if (ret < 0) {
306 goto fail;
307 }
308
309
310
311 slice_size2 = s->l2_slice_size * l2_entry_size(s);
312 n_slices = s->cluster_size / slice_size2;
313
314 trace_qcow2_l2_allocate_get_empty(bs, l1_index);
315 for (slice = 0; slice < n_slices; slice++) {
316 ret = qcow2_cache_get_empty(bs, s->l2_table_cache,
317 l2_offset + slice * slice_size2,
318 (void **) &l2_slice);
319 if (ret < 0) {
320 goto fail;
321 }
322
323 if ((old_l2_offset & L1E_OFFSET_MASK) == 0) {
324
325 memset(l2_slice, 0, slice_size2);
326 } else {
327 uint64_t *old_slice;
328 uint64_t old_l2_slice_offset =
329 (old_l2_offset & L1E_OFFSET_MASK) + slice * slice_size2;
330
331
332 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
333 ret = qcow2_cache_get(bs, s->l2_table_cache, old_l2_slice_offset,
334 (void **) &old_slice);
335 if (ret < 0) {
336 goto fail;
337 }
338
339 memcpy(l2_slice, old_slice, slice_size2);
340
341 qcow2_cache_put(s->l2_table_cache, (void **) &old_slice);
342 }
343
344
345 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
346
347 trace_qcow2_l2_allocate_write_l2(bs, l1_index);
348 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
349 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
350 }
351
352 ret = qcow2_cache_flush(bs, s->l2_table_cache);
353 if (ret < 0) {
354 goto fail;
355 }
356
357
358 trace_qcow2_l2_allocate_write_l1(bs, l1_index);
359 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
360 ret = qcow2_write_l1_entry(bs, l1_index);
361 if (ret < 0) {
362 goto fail;
363 }
364
365 trace_qcow2_l2_allocate_done(bs, l1_index, 0);
366 return 0;
367
368fail:
369 trace_qcow2_l2_allocate_done(bs, l1_index, ret);
370 if (l2_slice != NULL) {
371 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
372 }
373 s->l1_table[l1_index] = old_l2_offset;
374 if (l2_offset > 0) {
375 qcow2_free_clusters(bs, l2_offset, s->l2_size * l2_entry_size(s),
376 QCOW2_DISCARD_ALWAYS);
377 }
378 return ret;
379}
380
381
382
383
384
385
386
387
388
389
390
391
392
393static int qcow2_get_subcluster_range_type(BlockDriverState *bs,
394 uint64_t l2_entry,
395 uint64_t l2_bitmap,
396 unsigned sc_from,
397 QCow2SubclusterType *type)
398{
399 BDRVQcow2State *s = bs->opaque;
400 uint32_t val;
401
402 *type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, sc_from);
403
404 if (*type == QCOW2_SUBCLUSTER_INVALID) {
405 return -EINVAL;
406 } else if (!has_subclusters(s) || *type == QCOW2_SUBCLUSTER_COMPRESSED) {
407 return s->subclusters_per_cluster - sc_from;
408 }
409
410 switch (*type) {
411 case QCOW2_SUBCLUSTER_NORMAL:
412 val = l2_bitmap | QCOW_OFLAG_SUB_ALLOC_RANGE(0, sc_from);
413 return cto32(val) - sc_from;
414
415 case QCOW2_SUBCLUSTER_ZERO_PLAIN:
416 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
417 val = (l2_bitmap | QCOW_OFLAG_SUB_ZERO_RANGE(0, sc_from)) >> 32;
418 return cto32(val) - sc_from;
419
420 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
421 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
422 val = ((l2_bitmap >> 32) | l2_bitmap)
423 & ~QCOW_OFLAG_SUB_ALLOC_RANGE(0, sc_from);
424 return ctz32(val) - sc_from;
425
426 default:
427 g_assert_not_reached();
428 }
429}
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444static int count_contiguous_subclusters(BlockDriverState *bs, int nb_clusters,
445 unsigned sc_index, uint64_t *l2_slice,
446 unsigned *l2_index)
447{
448 BDRVQcow2State *s = bs->opaque;
449 int i, count = 0;
450 bool check_offset = false;
451 uint64_t expected_offset = 0;
452 QCow2SubclusterType expected_type = QCOW2_SUBCLUSTER_NORMAL, type;
453
454 assert(*l2_index + nb_clusters <= s->l2_slice_size);
455
456 for (i = 0; i < nb_clusters; i++) {
457 unsigned first_sc = (i == 0) ? sc_index : 0;
458 uint64_t l2_entry = get_l2_entry(s, l2_slice, *l2_index + i);
459 uint64_t l2_bitmap = get_l2_bitmap(s, l2_slice, *l2_index + i);
460 int ret = qcow2_get_subcluster_range_type(bs, l2_entry, l2_bitmap,
461 first_sc, &type);
462 if (ret < 0) {
463 *l2_index += i;
464 return -EIO;
465 }
466 if (i == 0) {
467 if (type == QCOW2_SUBCLUSTER_COMPRESSED) {
468
469 return ret;
470 }
471 expected_type = type;
472 expected_offset = l2_entry & L2E_OFFSET_MASK;
473 check_offset = (type == QCOW2_SUBCLUSTER_NORMAL ||
474 type == QCOW2_SUBCLUSTER_ZERO_ALLOC ||
475 type == QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC);
476 } else if (type != expected_type) {
477 break;
478 } else if (check_offset) {
479 expected_offset += s->cluster_size;
480 if (expected_offset != (l2_entry & L2E_OFFSET_MASK)) {
481 break;
482 }
483 }
484 count += ret;
485
486 if (first_sc + ret < s->subclusters_per_cluster) {
487 break;
488 }
489 }
490
491 return count;
492}
493
494static int coroutine_fn GRAPH_RDLOCK
495do_perform_cow_read(BlockDriverState *bs, uint64_t src_cluster_offset,
496 unsigned offset_in_cluster, QEMUIOVector *qiov)
497{
498 int ret;
499
500 if (qiov->size == 0) {
501 return 0;
502 }
503
504 BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
505
506 if (!bs->drv) {
507 return -ENOMEDIUM;
508 }
509
510
511
512
513
514
515
516 assert(src_cluster_offset <= INT64_MAX);
517 assert(src_cluster_offset + offset_in_cluster <= INT64_MAX);
518
519 assert((uint64_t)qiov->size <= INT64_MAX);
520 bdrv_check_qiov_request(src_cluster_offset + offset_in_cluster, qiov->size,
521 qiov, 0, &error_abort);
522
523
524
525
526
527 ret = bs->drv->bdrv_co_preadv_part(bs,
528 src_cluster_offset + offset_in_cluster,
529 qiov->size, qiov, 0, 0);
530 if (ret < 0) {
531 return ret;
532 }
533
534 return 0;
535}
536
537static int coroutine_fn GRAPH_RDLOCK
538do_perform_cow_write(BlockDriverState *bs, uint64_t cluster_offset,
539 unsigned offset_in_cluster, QEMUIOVector *qiov)
540{
541 BDRVQcow2State *s = bs->opaque;
542 int ret;
543
544 if (qiov->size == 0) {
545 return 0;
546 }
547
548 ret = qcow2_pre_write_overlap_check(bs, 0,
549 cluster_offset + offset_in_cluster, qiov->size, true);
550 if (ret < 0) {
551 return ret;
552 }
553
554 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
555 ret = bdrv_co_pwritev(s->data_file, cluster_offset + offset_in_cluster,
556 qiov->size, qiov, 0);
557 if (ret < 0) {
558 return ret;
559 }
560
561 return 0;
562}
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585int qcow2_get_host_offset(BlockDriverState *bs, uint64_t offset,
586 unsigned int *bytes, uint64_t *host_offset,
587 QCow2SubclusterType *subcluster_type)
588{
589 BDRVQcow2State *s = bs->opaque;
590 unsigned int l2_index, sc_index;
591 uint64_t l1_index, l2_offset, *l2_slice, l2_entry, l2_bitmap;
592 int sc;
593 unsigned int offset_in_cluster;
594 uint64_t bytes_available, bytes_needed, nb_clusters;
595 QCow2SubclusterType type;
596 int ret;
597
598 offset_in_cluster = offset_into_cluster(s, offset);
599 bytes_needed = (uint64_t) *bytes + offset_in_cluster;
600
601
602
603
604 bytes_available =
605 ((uint64_t) (s->l2_slice_size - offset_to_l2_slice_index(s, offset)))
606 << s->cluster_bits;
607
608 if (bytes_needed > bytes_available) {
609 bytes_needed = bytes_available;
610 }
611
612 *host_offset = 0;
613
614
615
616 l1_index = offset_to_l1_index(s, offset);
617 if (l1_index >= s->l1_size) {
618 type = QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
619 goto out;
620 }
621
622 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
623 if (!l2_offset) {
624 type = QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
625 goto out;
626 }
627
628 if (offset_into_cluster(s, l2_offset)) {
629 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
630 " unaligned (L1 index: %#" PRIx64 ")",
631 l2_offset, l1_index);
632 return -EIO;
633 }
634
635
636
637 ret = l2_load(bs, offset, l2_offset, &l2_slice);
638 if (ret < 0) {
639 return ret;
640 }
641
642
643
644 l2_index = offset_to_l2_slice_index(s, offset);
645 sc_index = offset_to_sc_index(s, offset);
646 l2_entry = get_l2_entry(s, l2_slice, l2_index);
647 l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index);
648
649 nb_clusters = size_to_clusters(s, bytes_needed);
650
651
652
653 assert(nb_clusters <= INT_MAX);
654
655 type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, sc_index);
656 if (s->qcow_version < 3 && (type == QCOW2_SUBCLUSTER_ZERO_PLAIN ||
657 type == QCOW2_SUBCLUSTER_ZERO_ALLOC)) {
658 qcow2_signal_corruption(bs, true, -1, -1, "Zero cluster entry found"
659 " in pre-v3 image (L2 offset: %#" PRIx64
660 ", L2 index: %#x)", l2_offset, l2_index);
661 ret = -EIO;
662 goto fail;
663 }
664 switch (type) {
665 case QCOW2_SUBCLUSTER_INVALID:
666 break;
667 case QCOW2_SUBCLUSTER_COMPRESSED:
668 if (has_data_file(bs)) {
669 qcow2_signal_corruption(bs, true, -1, -1, "Compressed cluster "
670 "entry found in image with external data "
671 "file (L2 offset: %#" PRIx64 ", L2 index: "
672 "%#x)", l2_offset, l2_index);
673 ret = -EIO;
674 goto fail;
675 }
676 *host_offset = l2_entry;
677 break;
678 case QCOW2_SUBCLUSTER_ZERO_PLAIN:
679 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
680 break;
681 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
682 case QCOW2_SUBCLUSTER_NORMAL:
683 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC: {
684 uint64_t host_cluster_offset = l2_entry & L2E_OFFSET_MASK;
685 *host_offset = host_cluster_offset + offset_in_cluster;
686 if (offset_into_cluster(s, host_cluster_offset)) {
687 qcow2_signal_corruption(bs, true, -1, -1,
688 "Cluster allocation offset %#"
689 PRIx64 " unaligned (L2 offset: %#" PRIx64
690 ", L2 index: %#x)", host_cluster_offset,
691 l2_offset, l2_index);
692 ret = -EIO;
693 goto fail;
694 }
695 if (has_data_file(bs) && *host_offset != offset) {
696 qcow2_signal_corruption(bs, true, -1, -1,
697 "External data file host cluster offset %#"
698 PRIx64 " does not match guest cluster "
699 "offset: %#" PRIx64
700 ", L2 index: %#x)", host_cluster_offset,
701 offset - offset_in_cluster, l2_index);
702 ret = -EIO;
703 goto fail;
704 }
705 break;
706 }
707 default:
708 abort();
709 }
710
711 sc = count_contiguous_subclusters(bs, nb_clusters, sc_index,
712 l2_slice, &l2_index);
713 if (sc < 0) {
714 qcow2_signal_corruption(bs, true, -1, -1, "Invalid cluster entry found "
715 " (L2 offset: %#" PRIx64 ", L2 index: %#x)",
716 l2_offset, l2_index);
717 ret = -EIO;
718 goto fail;
719 }
720 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
721
722 bytes_available = ((int64_t)sc + sc_index) << s->subcluster_bits;
723
724out:
725 if (bytes_available > bytes_needed) {
726 bytes_available = bytes_needed;
727 }
728
729
730
731
732 assert(bytes_available - offset_in_cluster <= UINT_MAX);
733 *bytes = bytes_available - offset_in_cluster;
734
735 *subcluster_type = type;
736
737 return 0;
738
739fail:
740 qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
741 return ret;
742}
743
744
745
746
747
748
749
750
751
752
753
754static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
755 uint64_t **new_l2_slice,
756 int *new_l2_index)
757{
758 BDRVQcow2State *s = bs->opaque;
759 unsigned int l2_index;
760 uint64_t l1_index, l2_offset;
761 uint64_t *l2_slice = NULL;
762 int ret;
763
764
765
766 l1_index = offset_to_l1_index(s, offset);
767 if (l1_index >= s->l1_size) {
768 ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
769 if (ret < 0) {
770 return ret;
771 }
772 }
773
774 assert(l1_index < s->l1_size);
775 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
776 if (offset_into_cluster(s, l2_offset)) {
777 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
778 " unaligned (L1 index: %#" PRIx64 ")",
779 l2_offset, l1_index);
780 return -EIO;
781 }
782
783 if (!(s->l1_table[l1_index] & QCOW_OFLAG_COPIED)) {
784
785 ret = l2_allocate(bs, l1_index);
786 if (ret < 0) {
787 return ret;
788 }
789
790
791 if (l2_offset) {
792 qcow2_free_clusters(bs, l2_offset, s->l2_size * l2_entry_size(s),
793 QCOW2_DISCARD_OTHER);
794 }
795
796
797 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
798 assert(offset_into_cluster(s, l2_offset) == 0);
799 }
800
801
802 ret = l2_load(bs, offset, l2_offset, &l2_slice);
803 if (ret < 0) {
804 return ret;
805 }
806
807
808
809 l2_index = offset_to_l2_slice_index(s, offset);
810
811 *new_l2_slice = l2_slice;
812 *new_l2_index = l2_index;
813
814 return 0;
815}
816
817
818
819
820
821
822
823
824
825
826int coroutine_fn qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
827 uint64_t offset,
828 int compressed_size,
829 uint64_t *host_offset)
830{
831 BDRVQcow2State *s = bs->opaque;
832 int l2_index, ret;
833 uint64_t *l2_slice;
834 int64_t cluster_offset;
835 int nb_csectors;
836
837 if (has_data_file(bs)) {
838 return 0;
839 }
840
841 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
842 if (ret < 0) {
843 return ret;
844 }
845
846
847
848 cluster_offset = get_l2_entry(s, l2_slice, l2_index);
849 if (cluster_offset & L2E_OFFSET_MASK) {
850 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
851 return -EIO;
852 }
853
854 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
855 if (cluster_offset < 0) {
856 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
857 return cluster_offset;
858 }
859
860 nb_csectors =
861 (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
862 (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
863
864
865 assert((cluster_offset & s->cluster_offset_mask) == cluster_offset);
866 assert((nb_csectors & s->csize_mask) == nb_csectors);
867
868 cluster_offset |= QCOW_OFLAG_COMPRESSED |
869 ((uint64_t)nb_csectors << s->csize_shift);
870
871
872
873
874
875 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
876 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
877 set_l2_entry(s, l2_slice, l2_index, cluster_offset);
878 if (has_subclusters(s)) {
879 set_l2_bitmap(s, l2_slice, l2_index, 0);
880 }
881 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
882
883 *host_offset = cluster_offset & s->cluster_offset_mask;
884 return 0;
885}
886
887static int coroutine_fn GRAPH_RDLOCK
888perform_cow(BlockDriverState *bs, QCowL2Meta *m)
889{
890 BDRVQcow2State *s = bs->opaque;
891 Qcow2COWRegion *start = &m->cow_start;
892 Qcow2COWRegion *end = &m->cow_end;
893 unsigned buffer_size;
894 unsigned data_bytes = end->offset - (start->offset + start->nb_bytes);
895 bool merge_reads;
896 uint8_t *start_buffer, *end_buffer;
897 QEMUIOVector qiov;
898 int ret;
899
900 assert(start->nb_bytes <= UINT_MAX - end->nb_bytes);
901 assert(start->nb_bytes + end->nb_bytes <= UINT_MAX - data_bytes);
902 assert(start->offset + start->nb_bytes <= end->offset);
903
904 if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
905 return 0;
906 }
907
908
909
910
911 merge_reads = start->nb_bytes && end->nb_bytes && data_bytes <= 16384;
912 if (merge_reads) {
913 buffer_size = start->nb_bytes + data_bytes + end->nb_bytes;
914 } else {
915
916
917
918 size_t align = bdrv_opt_mem_align(bs);
919 assert(align > 0 && align <= UINT_MAX);
920 assert(QEMU_ALIGN_UP(start->nb_bytes, align) <=
921 UINT_MAX - end->nb_bytes);
922 buffer_size = QEMU_ALIGN_UP(start->nb_bytes, align) + end->nb_bytes;
923 }
924
925
926
927 start_buffer = qemu_try_blockalign(bs, buffer_size);
928 if (start_buffer == NULL) {
929 return -ENOMEM;
930 }
931
932 end_buffer = start_buffer + buffer_size - end->nb_bytes;
933
934 qemu_iovec_init(&qiov, 2 + (m->data_qiov ?
935 qemu_iovec_subvec_niov(m->data_qiov,
936 m->data_qiov_offset,
937 data_bytes)
938 : 0));
939
940 qemu_co_mutex_unlock(&s->lock);
941
942
943
944 if (merge_reads) {
945 qemu_iovec_add(&qiov, start_buffer, buffer_size);
946 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
947 } else {
948 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
949 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
950 if (ret < 0) {
951 goto fail;
952 }
953
954 qemu_iovec_reset(&qiov);
955 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
956 ret = do_perform_cow_read(bs, m->offset, end->offset, &qiov);
957 }
958 if (ret < 0) {
959 goto fail;
960 }
961
962
963 if (bs->encrypted) {
964 ret = qcow2_co_encrypt(bs,
965 m->alloc_offset + start->offset,
966 m->offset + start->offset,
967 start_buffer, start->nb_bytes);
968 if (ret < 0) {
969 goto fail;
970 }
971
972 ret = qcow2_co_encrypt(bs,
973 m->alloc_offset + end->offset,
974 m->offset + end->offset,
975 end_buffer, end->nb_bytes);
976 if (ret < 0) {
977 goto fail;
978 }
979 }
980
981
982
983 if (m->data_qiov) {
984 qemu_iovec_reset(&qiov);
985 if (start->nb_bytes) {
986 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
987 }
988 qemu_iovec_concat(&qiov, m->data_qiov, m->data_qiov_offset, data_bytes);
989 if (end->nb_bytes) {
990 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
991 }
992
993
994
995 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
996 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
997 } else {
998
999 qemu_iovec_reset(&qiov);
1000 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
1001 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
1002 if (ret < 0) {
1003 goto fail;
1004 }
1005
1006 qemu_iovec_reset(&qiov);
1007 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
1008 ret = do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov);
1009 }
1010
1011fail:
1012 qemu_co_mutex_lock(&s->lock);
1013
1014
1015
1016
1017
1018
1019 if (ret == 0) {
1020 qcow2_cache_depends_on_flush(s->l2_table_cache);
1021 }
1022
1023 qemu_vfree(start_buffer);
1024 qemu_iovec_destroy(&qiov);
1025 return ret;
1026}
1027
1028int coroutine_fn qcow2_alloc_cluster_link_l2(BlockDriverState *bs,
1029 QCowL2Meta *m)
1030{
1031 BDRVQcow2State *s = bs->opaque;
1032 int i, j = 0, l2_index, ret;
1033 uint64_t *old_cluster, *l2_slice;
1034 uint64_t cluster_offset = m->alloc_offset;
1035
1036 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters);
1037 assert(m->nb_clusters > 0);
1038
1039 old_cluster = g_try_new(uint64_t, m->nb_clusters);
1040 if (old_cluster == NULL) {
1041 ret = -ENOMEM;
1042 goto err;
1043 }
1044
1045
1046 ret = perform_cow(bs, m);
1047 if (ret < 0) {
1048 goto err;
1049 }
1050
1051
1052 if (s->use_lazy_refcounts) {
1053 qcow2_mark_dirty(bs);
1054 }
1055 if (qcow2_need_accurate_refcounts(s)) {
1056 qcow2_cache_set_dependency(bs, s->l2_table_cache,
1057 s->refcount_block_cache);
1058 }
1059
1060 ret = get_cluster_table(bs, m->offset, &l2_slice, &l2_index);
1061 if (ret < 0) {
1062 goto err;
1063 }
1064 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1065
1066 assert(l2_index + m->nb_clusters <= s->l2_slice_size);
1067 assert(m->cow_end.offset + m->cow_end.nb_bytes <=
1068 m->nb_clusters << s->cluster_bits);
1069 for (i = 0; i < m->nb_clusters; i++) {
1070 uint64_t offset = cluster_offset + ((uint64_t)i << s->cluster_bits);
1071
1072
1073
1074
1075
1076
1077 if (get_l2_entry(s, l2_slice, l2_index + i) != 0) {
1078 old_cluster[j++] = get_l2_entry(s, l2_slice, l2_index + i);
1079 }
1080
1081
1082 assert((offset & L2E_OFFSET_MASK) == offset);
1083
1084 set_l2_entry(s, l2_slice, l2_index + i, offset | QCOW_OFLAG_COPIED);
1085
1086
1087 if (has_subclusters(s) && !m->prealloc) {
1088 uint64_t l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index + i);
1089 unsigned written_from = m->cow_start.offset;
1090 unsigned written_to = m->cow_end.offset + m->cow_end.nb_bytes;
1091 int first_sc, last_sc;
1092
1093 written_from = MAX(written_from, i << s->cluster_bits);
1094 written_to = MIN(written_to, (i + 1) << s->cluster_bits);
1095 assert(written_from < written_to);
1096 first_sc = offset_to_sc_index(s, written_from);
1097 last_sc = offset_to_sc_index(s, written_to - 1);
1098 l2_bitmap |= QCOW_OFLAG_SUB_ALLOC_RANGE(first_sc, last_sc + 1);
1099 l2_bitmap &= ~QCOW_OFLAG_SUB_ZERO_RANGE(first_sc, last_sc + 1);
1100 set_l2_bitmap(s, l2_slice, l2_index + i, l2_bitmap);
1101 }
1102 }
1103
1104
1105 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1106
1107
1108
1109
1110
1111
1112
1113 if (!m->keep_old_clusters && j != 0) {
1114 for (i = 0; i < j; i++) {
1115 qcow2_free_any_cluster(bs, old_cluster[i], QCOW2_DISCARD_NEVER);
1116 }
1117 }
1118
1119 ret = 0;
1120err:
1121 g_free(old_cluster);
1122 return ret;
1123 }
1124
1125
1126
1127
1128
1129void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m)
1130{
1131 BDRVQcow2State *s = bs->opaque;
1132 if (!has_data_file(bs) && !m->keep_old_clusters) {
1133 qcow2_free_clusters(bs, m->alloc_offset,
1134 m->nb_clusters << s->cluster_bits,
1135 QCOW2_DISCARD_NEVER);
1136 }
1137}
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159static int calculate_l2_meta(BlockDriverState *bs, uint64_t host_cluster_offset,
1160 uint64_t guest_offset, unsigned bytes,
1161 uint64_t *l2_slice, QCowL2Meta **m, bool keep_old)
1162{
1163 BDRVQcow2State *s = bs->opaque;
1164 int sc_index, l2_index = offset_to_l2_slice_index(s, guest_offset);
1165 uint64_t l2_entry, l2_bitmap;
1166 unsigned cow_start_from, cow_end_to;
1167 unsigned cow_start_to = offset_into_cluster(s, guest_offset);
1168 unsigned cow_end_from = cow_start_to + bytes;
1169 unsigned nb_clusters = size_to_clusters(s, cow_end_from);
1170 QCowL2Meta *old_m = *m;
1171 QCow2SubclusterType type;
1172 int i;
1173 bool skip_cow = keep_old;
1174
1175 assert(nb_clusters <= s->l2_slice_size - l2_index);
1176
1177
1178 for (i = 0; i < nb_clusters; i++) {
1179 l2_entry = get_l2_entry(s, l2_slice, l2_index + i);
1180 l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index + i);
1181 if (skip_cow) {
1182 unsigned write_from = MAX(cow_start_to, i << s->cluster_bits);
1183 unsigned write_to = MIN(cow_end_from, (i + 1) << s->cluster_bits);
1184 int first_sc = offset_to_sc_index(s, write_from);
1185 int last_sc = offset_to_sc_index(s, write_to - 1);
1186 int cnt = qcow2_get_subcluster_range_type(bs, l2_entry, l2_bitmap,
1187 first_sc, &type);
1188
1189 if (type != QCOW2_SUBCLUSTER_NORMAL || first_sc + cnt <= last_sc) {
1190 skip_cow = false;
1191 }
1192 } else {
1193
1194 type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, 0);
1195 }
1196 if (type == QCOW2_SUBCLUSTER_INVALID) {
1197 int l1_index = offset_to_l1_index(s, guest_offset);
1198 uint64_t l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
1199 qcow2_signal_corruption(bs, true, -1, -1, "Invalid cluster "
1200 "entry found (L2 offset: %#" PRIx64
1201 ", L2 index: %#x)",
1202 l2_offset, l2_index + i);
1203 return -EIO;
1204 }
1205 }
1206
1207 if (skip_cow) {
1208 return 0;
1209 }
1210
1211
1212 l2_entry = get_l2_entry(s, l2_slice, l2_index);
1213 l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index);
1214 sc_index = offset_to_sc_index(s, guest_offset);
1215 type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, sc_index);
1216
1217 if (!keep_old) {
1218 switch (type) {
1219 case QCOW2_SUBCLUSTER_COMPRESSED:
1220 cow_start_from = 0;
1221 break;
1222 case QCOW2_SUBCLUSTER_NORMAL:
1223 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
1224 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
1225 if (has_subclusters(s)) {
1226
1227 uint32_t alloc_bitmap = l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC;
1228 cow_start_from =
1229 MIN(sc_index, ctz32(alloc_bitmap)) << s->subcluster_bits;
1230 } else {
1231 cow_start_from = 0;
1232 }
1233 break;
1234 case QCOW2_SUBCLUSTER_ZERO_PLAIN:
1235 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
1236 cow_start_from = sc_index << s->subcluster_bits;
1237 break;
1238 default:
1239 g_assert_not_reached();
1240 }
1241 } else {
1242 switch (type) {
1243 case QCOW2_SUBCLUSTER_NORMAL:
1244 cow_start_from = cow_start_to;
1245 break;
1246 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
1247 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
1248 cow_start_from = sc_index << s->subcluster_bits;
1249 break;
1250 default:
1251 g_assert_not_reached();
1252 }
1253 }
1254
1255
1256 l2_index += nb_clusters - 1;
1257 l2_entry = get_l2_entry(s, l2_slice, l2_index);
1258 l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index);
1259 sc_index = offset_to_sc_index(s, guest_offset + bytes - 1);
1260 type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, sc_index);
1261
1262 if (!keep_old) {
1263 switch (type) {
1264 case QCOW2_SUBCLUSTER_COMPRESSED:
1265 cow_end_to = ROUND_UP(cow_end_from, s->cluster_size);
1266 break;
1267 case QCOW2_SUBCLUSTER_NORMAL:
1268 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
1269 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
1270 cow_end_to = ROUND_UP(cow_end_from, s->cluster_size);
1271 if (has_subclusters(s)) {
1272
1273 uint32_t alloc_bitmap = l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC;
1274 cow_end_to -=
1275 MIN(s->subclusters_per_cluster - sc_index - 1,
1276 clz32(alloc_bitmap)) << s->subcluster_bits;
1277 }
1278 break;
1279 case QCOW2_SUBCLUSTER_ZERO_PLAIN:
1280 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
1281 cow_end_to = ROUND_UP(cow_end_from, s->subcluster_size);
1282 break;
1283 default:
1284 g_assert_not_reached();
1285 }
1286 } else {
1287 switch (type) {
1288 case QCOW2_SUBCLUSTER_NORMAL:
1289 cow_end_to = cow_end_from;
1290 break;
1291 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
1292 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
1293 cow_end_to = ROUND_UP(cow_end_from, s->subcluster_size);
1294 break;
1295 default:
1296 g_assert_not_reached();
1297 }
1298 }
1299
1300 *m = g_malloc0(sizeof(**m));
1301 **m = (QCowL2Meta) {
1302 .next = old_m,
1303
1304 .alloc_offset = host_cluster_offset,
1305 .offset = start_of_cluster(s, guest_offset),
1306 .nb_clusters = nb_clusters,
1307
1308 .keep_old_clusters = keep_old,
1309
1310 .cow_start = {
1311 .offset = cow_start_from,
1312 .nb_bytes = cow_start_to - cow_start_from,
1313 },
1314 .cow_end = {
1315 .offset = cow_end_from,
1316 .nb_bytes = cow_end_to - cow_end_from,
1317 },
1318 };
1319
1320 qemu_co_queue_init(&(*m)->dependent_requests);
1321 QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);
1322
1323 return 0;
1324}
1325
1326
1327
1328
1329
1330
1331static bool cluster_needs_new_alloc(BlockDriverState *bs, uint64_t l2_entry)
1332{
1333 switch (qcow2_get_cluster_type(bs, l2_entry)) {
1334 case QCOW2_CLUSTER_NORMAL:
1335 case QCOW2_CLUSTER_ZERO_ALLOC:
1336 if (l2_entry & QCOW_OFLAG_COPIED) {
1337 return false;
1338 }
1339
1340 case QCOW2_CLUSTER_UNALLOCATED:
1341 case QCOW2_CLUSTER_COMPRESSED:
1342 case QCOW2_CLUSTER_ZERO_PLAIN:
1343 return true;
1344 default:
1345 abort();
1346 }
1347}
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362static int count_single_write_clusters(BlockDriverState *bs, int nb_clusters,
1363 uint64_t *l2_slice, int l2_index,
1364 bool new_alloc)
1365{
1366 BDRVQcow2State *s = bs->opaque;
1367 uint64_t l2_entry = get_l2_entry(s, l2_slice, l2_index);
1368 uint64_t expected_offset = l2_entry & L2E_OFFSET_MASK;
1369 int i;
1370
1371 for (i = 0; i < nb_clusters; i++) {
1372 l2_entry = get_l2_entry(s, l2_slice, l2_index + i);
1373 if (cluster_needs_new_alloc(bs, l2_entry) != new_alloc) {
1374 break;
1375 }
1376 if (!new_alloc) {
1377 if (expected_offset != (l2_entry & L2E_OFFSET_MASK)) {
1378 break;
1379 }
1380 expected_offset += s->cluster_size;
1381 }
1382 }
1383
1384 assert(i <= nb_clusters);
1385 return i;
1386}
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402static int coroutine_fn handle_dependencies(BlockDriverState *bs,
1403 uint64_t guest_offset,
1404 uint64_t *cur_bytes, QCowL2Meta **m)
1405{
1406 BDRVQcow2State *s = bs->opaque;
1407 QCowL2Meta *old_alloc;
1408 uint64_t bytes = *cur_bytes;
1409
1410 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
1411
1412 uint64_t start = guest_offset;
1413 uint64_t end = start + bytes;
1414 uint64_t old_start = start_of_cluster(s, l2meta_cow_start(old_alloc));
1415 uint64_t old_end = ROUND_UP(l2meta_cow_end(old_alloc), s->cluster_size);
1416
1417 if (end <= old_start || start >= old_end) {
1418
1419 continue;
1420 }
1421
1422 if (old_alloc->keep_old_clusters &&
1423 (end <= l2meta_cow_start(old_alloc) ||
1424 start >= l2meta_cow_end(old_alloc)))
1425 {
1426
1427
1428
1429
1430 continue;
1431 }
1432
1433
1434
1435 if (start < old_start) {
1436
1437 bytes = old_start - start;
1438 } else {
1439 bytes = 0;
1440 }
1441
1442
1443
1444
1445
1446
1447
1448 if (bytes == 0 && *m) {
1449 *cur_bytes = 0;
1450 return 0;
1451 }
1452
1453 if (bytes == 0) {
1454
1455
1456
1457
1458 qemu_co_queue_wait(&old_alloc->dependent_requests, &s->lock);
1459 return -EAGAIN;
1460 }
1461 }
1462
1463
1464
1465 *cur_bytes = bytes;
1466
1467 return 0;
1468}
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492static int coroutine_fn handle_copied(BlockDriverState *bs,
1493 uint64_t guest_offset, uint64_t *host_offset, uint64_t *bytes,
1494 QCowL2Meta **m)
1495{
1496 BDRVQcow2State *s = bs->opaque;
1497 int l2_index;
1498 uint64_t l2_entry, cluster_offset;
1499 uint64_t *l2_slice;
1500 uint64_t nb_clusters;
1501 unsigned int keep_clusters;
1502 int ret;
1503
1504 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset,
1505 *bytes);
1506
1507 assert(*host_offset == INV_OFFSET || offset_into_cluster(s, guest_offset)
1508 == offset_into_cluster(s, *host_offset));
1509
1510
1511
1512
1513
1514 nb_clusters =
1515 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1516
1517 l2_index = offset_to_l2_slice_index(s, guest_offset);
1518 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1519
1520 nb_clusters = MIN(nb_clusters, BDRV_REQUEST_MAX_BYTES >> s->cluster_bits);
1521
1522
1523 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
1524 if (ret < 0) {
1525 return ret;
1526 }
1527
1528 l2_entry = get_l2_entry(s, l2_slice, l2_index);
1529 cluster_offset = l2_entry & L2E_OFFSET_MASK;
1530
1531 if (!cluster_needs_new_alloc(bs, l2_entry)) {
1532 if (offset_into_cluster(s, cluster_offset)) {
1533 qcow2_signal_corruption(bs, true, -1, -1, "%s cluster offset "
1534 "%#" PRIx64 " unaligned (guest offset: %#"
1535 PRIx64 ")", l2_entry & QCOW_OFLAG_ZERO ?
1536 "Preallocated zero" : "Data",
1537 cluster_offset, guest_offset);
1538 ret = -EIO;
1539 goto out;
1540 }
1541
1542
1543 if (*host_offset != INV_OFFSET && cluster_offset != *host_offset) {
1544 *bytes = 0;
1545 ret = 0;
1546 goto out;
1547 }
1548
1549
1550 keep_clusters = count_single_write_clusters(bs, nb_clusters, l2_slice,
1551 l2_index, false);
1552 assert(keep_clusters <= nb_clusters);
1553
1554 *bytes = MIN(*bytes,
1555 keep_clusters * s->cluster_size
1556 - offset_into_cluster(s, guest_offset));
1557 assert(*bytes != 0);
1558
1559 ret = calculate_l2_meta(bs, cluster_offset, guest_offset,
1560 *bytes, l2_slice, m, true);
1561 if (ret < 0) {
1562 goto out;
1563 }
1564
1565 ret = 1;
1566 } else {
1567 ret = 0;
1568 }
1569
1570
1571out:
1572 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1573
1574
1575
1576 if (ret > 0) {
1577 *host_offset = cluster_offset + offset_into_cluster(s, guest_offset);
1578 }
1579
1580 return ret;
1581}
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset,
1603 uint64_t *host_offset, uint64_t *nb_clusters)
1604{
1605 BDRVQcow2State *s = bs->opaque;
1606
1607 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset,
1608 *host_offset, *nb_clusters);
1609
1610 if (has_data_file(bs)) {
1611 assert(*host_offset == INV_OFFSET ||
1612 *host_offset == start_of_cluster(s, guest_offset));
1613 *host_offset = start_of_cluster(s, guest_offset);
1614 return 0;
1615 }
1616
1617
1618 trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
1619 if (*host_offset == INV_OFFSET) {
1620 int64_t cluster_offset =
1621 qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size);
1622 if (cluster_offset < 0) {
1623 return cluster_offset;
1624 }
1625 *host_offset = cluster_offset;
1626 return 0;
1627 } else {
1628 int64_t ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters);
1629 if (ret < 0) {
1630 return ret;
1631 }
1632 *nb_clusters = ret;
1633 return 0;
1634 }
1635}
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658static int coroutine_fn handle_alloc(BlockDriverState *bs,
1659 uint64_t guest_offset, uint64_t *host_offset, uint64_t *bytes,
1660 QCowL2Meta **m)
1661{
1662 BDRVQcow2State *s = bs->opaque;
1663 int l2_index;
1664 uint64_t *l2_slice;
1665 uint64_t nb_clusters;
1666 int ret;
1667
1668 uint64_t alloc_cluster_offset;
1669
1670 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset,
1671 *bytes);
1672 assert(*bytes > 0);
1673
1674
1675
1676
1677
1678 nb_clusters =
1679 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1680
1681 l2_index = offset_to_l2_slice_index(s, guest_offset);
1682 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1683
1684 nb_clusters = MIN(nb_clusters, BDRV_REQUEST_MAX_BYTES >> s->cluster_bits);
1685
1686
1687 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
1688 if (ret < 0) {
1689 return ret;
1690 }
1691
1692 nb_clusters = count_single_write_clusters(bs, nb_clusters,
1693 l2_slice, l2_index, true);
1694
1695
1696
1697
1698 assert(nb_clusters > 0);
1699
1700
1701 alloc_cluster_offset = *host_offset == INV_OFFSET ? INV_OFFSET :
1702 start_of_cluster(s, *host_offset);
1703 ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,
1704 &nb_clusters);
1705 if (ret < 0) {
1706 goto out;
1707 }
1708
1709
1710 if (nb_clusters == 0) {
1711 *bytes = 0;
1712 ret = 0;
1713 goto out;
1714 }
1715
1716 assert(alloc_cluster_offset != INV_OFFSET);
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732 uint64_t requested_bytes = *bytes + offset_into_cluster(s, guest_offset);
1733 int avail_bytes = nb_clusters << s->cluster_bits;
1734 int nb_bytes = MIN(requested_bytes, avail_bytes);
1735
1736 *host_offset = alloc_cluster_offset + offset_into_cluster(s, guest_offset);
1737 *bytes = MIN(*bytes, nb_bytes - offset_into_cluster(s, guest_offset));
1738 assert(*bytes != 0);
1739
1740 ret = calculate_l2_meta(bs, alloc_cluster_offset, guest_offset, *bytes,
1741 l2_slice, m, false);
1742 if (ret < 0) {
1743 goto out;
1744 }
1745
1746 ret = 1;
1747
1748out:
1749 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1750 return ret;
1751}
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780int coroutine_fn qcow2_alloc_host_offset(BlockDriverState *bs, uint64_t offset,
1781 unsigned int *bytes,
1782 uint64_t *host_offset,
1783 QCowL2Meta **m)
1784{
1785 BDRVQcow2State *s = bs->opaque;
1786 uint64_t start, remaining;
1787 uint64_t cluster_offset;
1788 uint64_t cur_bytes;
1789 int ret;
1790
1791 trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, *bytes);
1792
1793again:
1794 start = offset;
1795 remaining = *bytes;
1796 cluster_offset = INV_OFFSET;
1797 *host_offset = INV_OFFSET;
1798 cur_bytes = 0;
1799 *m = NULL;
1800
1801 while (true) {
1802
1803 if (*host_offset == INV_OFFSET && cluster_offset != INV_OFFSET) {
1804 *host_offset = cluster_offset;
1805 }
1806
1807 assert(remaining >= cur_bytes);
1808
1809 start += cur_bytes;
1810 remaining -= cur_bytes;
1811
1812 if (cluster_offset != INV_OFFSET) {
1813 cluster_offset += cur_bytes;
1814 }
1815
1816 if (remaining == 0) {
1817 break;
1818 }
1819
1820 cur_bytes = remaining;
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840 ret = handle_dependencies(bs, start, &cur_bytes, m);
1841 if (ret == -EAGAIN) {
1842
1843
1844
1845 assert(*m == NULL);
1846 goto again;
1847 } else if (ret < 0) {
1848 return ret;
1849 } else if (cur_bytes == 0) {
1850 break;
1851 } else {
1852
1853
1854
1855 }
1856
1857
1858
1859
1860 ret = handle_copied(bs, start, &cluster_offset, &cur_bytes, m);
1861 if (ret < 0) {
1862 return ret;
1863 } else if (ret) {
1864 continue;
1865 } else if (cur_bytes == 0) {
1866 break;
1867 }
1868
1869
1870
1871
1872
1873 ret = handle_alloc(bs, start, &cluster_offset, &cur_bytes, m);
1874 if (ret < 0) {
1875 return ret;
1876 } else if (ret) {
1877 continue;
1878 } else {
1879 assert(cur_bytes == 0);
1880 break;
1881 }
1882 }
1883
1884 *bytes -= remaining;
1885 assert(*bytes > 0);
1886 assert(*host_offset != INV_OFFSET);
1887 assert(offset_into_cluster(s, *host_offset) ==
1888 offset_into_cluster(s, offset));
1889
1890 return 0;
1891}
1892
1893
1894
1895
1896
1897
1898static int discard_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1899 uint64_t nb_clusters,
1900 enum qcow2_discard_type type, bool full_discard)
1901{
1902 BDRVQcow2State *s = bs->opaque;
1903 uint64_t *l2_slice;
1904 int l2_index;
1905 int ret;
1906 int i;
1907
1908 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1909 if (ret < 0) {
1910 return ret;
1911 }
1912
1913
1914 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1915 assert(nb_clusters <= INT_MAX);
1916
1917 for (i = 0; i < nb_clusters; i++) {
1918 uint64_t old_l2_entry = get_l2_entry(s, l2_slice, l2_index + i);
1919 uint64_t old_l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index + i);
1920 uint64_t new_l2_entry = old_l2_entry;
1921 uint64_t new_l2_bitmap = old_l2_bitmap;
1922 QCow2ClusterType cluster_type =
1923 qcow2_get_cluster_type(bs, old_l2_entry);
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938 if (full_discard) {
1939 new_l2_entry = new_l2_bitmap = 0;
1940 } else if (bs->backing || qcow2_cluster_is_allocated(cluster_type)) {
1941 if (has_subclusters(s)) {
1942 new_l2_entry = 0;
1943 new_l2_bitmap = QCOW_L2_BITMAP_ALL_ZEROES;
1944 } else {
1945 new_l2_entry = s->qcow_version >= 3 ? QCOW_OFLAG_ZERO : 0;
1946 }
1947 }
1948
1949 if (old_l2_entry == new_l2_entry && old_l2_bitmap == new_l2_bitmap) {
1950 continue;
1951 }
1952
1953
1954 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1955 set_l2_entry(s, l2_slice, l2_index + i, new_l2_entry);
1956 if (has_subclusters(s)) {
1957 set_l2_bitmap(s, l2_slice, l2_index + i, new_l2_bitmap);
1958 }
1959
1960 qcow2_free_any_cluster(bs, old_l2_entry, type);
1961 }
1962
1963 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1964
1965 return nb_clusters;
1966}
1967
1968int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
1969 uint64_t bytes, enum qcow2_discard_type type,
1970 bool full_discard)
1971{
1972 BDRVQcow2State *s = bs->opaque;
1973 uint64_t end_offset = offset + bytes;
1974 uint64_t nb_clusters;
1975 int64_t cleared;
1976 int ret;
1977
1978
1979 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1980 assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1981 end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1982
1983 nb_clusters = size_to_clusters(s, bytes);
1984
1985 s->cache_discards = true;
1986
1987
1988 while (nb_clusters > 0) {
1989 cleared = discard_in_l2_slice(bs, offset, nb_clusters, type,
1990 full_discard);
1991 if (cleared < 0) {
1992 ret = cleared;
1993 goto fail;
1994 }
1995
1996 nb_clusters -= cleared;
1997 offset += (cleared * s->cluster_size);
1998 }
1999
2000 ret = 0;
2001fail:
2002 s->cache_discards = false;
2003 qcow2_process_discards(bs, ret);
2004
2005 return ret;
2006}
2007
2008
2009
2010
2011
2012
2013static int zero_in_l2_slice(BlockDriverState *bs, uint64_t offset,
2014 uint64_t nb_clusters, int flags)
2015{
2016 BDRVQcow2State *s = bs->opaque;
2017 uint64_t *l2_slice;
2018 int l2_index;
2019 int ret;
2020 int i;
2021
2022 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
2023 if (ret < 0) {
2024 return ret;
2025 }
2026
2027
2028 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
2029 assert(nb_clusters <= INT_MAX);
2030
2031 for (i = 0; i < nb_clusters; i++) {
2032 uint64_t old_l2_entry = get_l2_entry(s, l2_slice, l2_index + i);
2033 uint64_t old_l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index + i);
2034 QCow2ClusterType type = qcow2_get_cluster_type(bs, old_l2_entry);
2035 bool unmap = (type == QCOW2_CLUSTER_COMPRESSED) ||
2036 ((flags & BDRV_REQ_MAY_UNMAP) && qcow2_cluster_is_allocated(type));
2037 uint64_t new_l2_entry = unmap ? 0 : old_l2_entry;
2038 uint64_t new_l2_bitmap = old_l2_bitmap;
2039
2040 if (has_subclusters(s)) {
2041 new_l2_bitmap = QCOW_L2_BITMAP_ALL_ZEROES;
2042 } else {
2043 new_l2_entry |= QCOW_OFLAG_ZERO;
2044 }
2045
2046 if (old_l2_entry == new_l2_entry && old_l2_bitmap == new_l2_bitmap) {
2047 continue;
2048 }
2049
2050
2051 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
2052 set_l2_entry(s, l2_slice, l2_index + i, new_l2_entry);
2053 if (has_subclusters(s)) {
2054 set_l2_bitmap(s, l2_slice, l2_index + i, new_l2_bitmap);
2055 }
2056
2057
2058 if (unmap) {
2059 qcow2_free_any_cluster(bs, old_l2_entry, QCOW2_DISCARD_REQUEST);
2060 }
2061 }
2062
2063 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
2064
2065 return nb_clusters;
2066}
2067
2068static int zero_l2_subclusters(BlockDriverState *bs, uint64_t offset,
2069 unsigned nb_subclusters)
2070{
2071 BDRVQcow2State *s = bs->opaque;
2072 uint64_t *l2_slice;
2073 uint64_t old_l2_bitmap, l2_bitmap;
2074 int l2_index, ret, sc = offset_to_sc_index(s, offset);
2075
2076
2077 assert(nb_subclusters > 0 && nb_subclusters < s->subclusters_per_cluster);
2078 assert(sc + nb_subclusters <= s->subclusters_per_cluster);
2079 assert(offset_into_subcluster(s, offset) == 0);
2080
2081 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
2082 if (ret < 0) {
2083 return ret;
2084 }
2085
2086 switch (qcow2_get_cluster_type(bs, get_l2_entry(s, l2_slice, l2_index))) {
2087 case QCOW2_CLUSTER_COMPRESSED:
2088 ret = -ENOTSUP;
2089 goto out;
2090 case QCOW2_CLUSTER_NORMAL:
2091 case QCOW2_CLUSTER_UNALLOCATED:
2092 break;
2093 default:
2094 g_assert_not_reached();
2095 }
2096
2097 old_l2_bitmap = l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index);
2098
2099 l2_bitmap |= QCOW_OFLAG_SUB_ZERO_RANGE(sc, sc + nb_subclusters);
2100 l2_bitmap &= ~QCOW_OFLAG_SUB_ALLOC_RANGE(sc, sc + nb_subclusters);
2101
2102 if (old_l2_bitmap != l2_bitmap) {
2103 set_l2_bitmap(s, l2_slice, l2_index, l2_bitmap);
2104 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
2105 }
2106
2107 ret = 0;
2108out:
2109 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
2110
2111 return ret;
2112}
2113
2114int coroutine_fn qcow2_subcluster_zeroize(BlockDriverState *bs, uint64_t offset,
2115 uint64_t bytes, int flags)
2116{
2117 BDRVQcow2State *s = bs->opaque;
2118 uint64_t end_offset = offset + bytes;
2119 uint64_t nb_clusters;
2120 unsigned head, tail;
2121 int64_t cleared;
2122 int ret;
2123
2124
2125
2126 if (data_file_is_raw(bs)) {
2127 assert(has_data_file(bs));
2128 ret = bdrv_co_pwrite_zeroes(s->data_file, offset, bytes, flags);
2129 if (ret < 0) {
2130 return ret;
2131 }
2132 }
2133
2134
2135 assert(offset_into_subcluster(s, offset) == 0);
2136 assert(offset_into_subcluster(s, end_offset) == 0 ||
2137 end_offset >= bs->total_sectors << BDRV_SECTOR_BITS);
2138
2139
2140
2141
2142
2143 if (s->qcow_version < 3) {
2144 if (!bs->backing) {
2145 return qcow2_cluster_discard(bs, offset, bytes,
2146 QCOW2_DISCARD_REQUEST, false);
2147 }
2148 return -ENOTSUP;
2149 }
2150
2151 head = MIN(end_offset, ROUND_UP(offset, s->cluster_size)) - offset;
2152 offset += head;
2153
2154 tail = (end_offset >= bs->total_sectors << BDRV_SECTOR_BITS) ? 0 :
2155 end_offset - MAX(offset, start_of_cluster(s, end_offset));
2156 end_offset -= tail;
2157
2158 s->cache_discards = true;
2159
2160 if (head) {
2161 ret = zero_l2_subclusters(bs, offset - head,
2162 size_to_subclusters(s, head));
2163 if (ret < 0) {
2164 goto fail;
2165 }
2166 }
2167
2168
2169 nb_clusters = size_to_clusters(s, end_offset - offset);
2170
2171 while (nb_clusters > 0) {
2172 cleared = zero_in_l2_slice(bs, offset, nb_clusters, flags);
2173 if (cleared < 0) {
2174 ret = cleared;
2175 goto fail;
2176 }
2177
2178 nb_clusters -= cleared;
2179 offset += (cleared * s->cluster_size);
2180 }
2181
2182 if (tail) {
2183 ret = zero_l2_subclusters(bs, end_offset, size_to_subclusters(s, tail));
2184 if (ret < 0) {
2185 goto fail;
2186 }
2187 }
2188
2189 ret = 0;
2190fail:
2191 s->cache_discards = false;
2192 qcow2_process_discards(bs, ret);
2193
2194 return ret;
2195}
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
2206 int l1_size, int64_t *visited_l1_entries,
2207 int64_t l1_entries,
2208 BlockDriverAmendStatusCB *status_cb,
2209 void *cb_opaque)
2210{
2211 BDRVQcow2State *s = bs->opaque;
2212 bool is_active_l1 = (l1_table == s->l1_table);
2213 uint64_t *l2_slice = NULL;
2214 unsigned slice, slice_size2, n_slices;
2215 int ret;
2216 int i, j;
2217
2218
2219 assert(!has_subclusters(s));
2220
2221 slice_size2 = s->l2_slice_size * l2_entry_size(s);
2222 n_slices = s->cluster_size / slice_size2;
2223
2224 if (!is_active_l1) {
2225
2226
2227 l2_slice = qemu_try_blockalign(bs->file->bs, slice_size2);
2228 if (l2_slice == NULL) {
2229 return -ENOMEM;
2230 }
2231 }
2232
2233 for (i = 0; i < l1_size; i++) {
2234 uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
2235 uint64_t l2_refcount;
2236
2237 if (!l2_offset) {
2238
2239 (*visited_l1_entries)++;
2240 if (status_cb) {
2241 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
2242 }
2243 continue;
2244 }
2245
2246 if (offset_into_cluster(s, l2_offset)) {
2247 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
2248 PRIx64 " unaligned (L1 index: %#x)",
2249 l2_offset, i);
2250 ret = -EIO;
2251 goto fail;
2252 }
2253
2254 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
2255 &l2_refcount);
2256 if (ret < 0) {
2257 goto fail;
2258 }
2259
2260 for (slice = 0; slice < n_slices; slice++) {
2261 uint64_t slice_offset = l2_offset + slice * slice_size2;
2262 bool l2_dirty = false;
2263 if (is_active_l1) {
2264
2265 ret = qcow2_cache_get(bs, s->l2_table_cache, slice_offset,
2266 (void **)&l2_slice);
2267 } else {
2268
2269 ret = bdrv_pread(bs->file, slice_offset, slice_size2,
2270 l2_slice, 0);
2271 }
2272 if (ret < 0) {
2273 goto fail;
2274 }
2275
2276 for (j = 0; j < s->l2_slice_size; j++) {
2277 uint64_t l2_entry = get_l2_entry(s, l2_slice, j);
2278 int64_t offset = l2_entry & L2E_OFFSET_MASK;
2279 QCow2ClusterType cluster_type =
2280 qcow2_get_cluster_type(bs, l2_entry);
2281
2282 if (cluster_type != QCOW2_CLUSTER_ZERO_PLAIN &&
2283 cluster_type != QCOW2_CLUSTER_ZERO_ALLOC) {
2284 continue;
2285 }
2286
2287 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
2288 if (!bs->backing) {
2289
2290
2291
2292
2293
2294 set_l2_entry(s, l2_slice, j, 0);
2295 l2_dirty = true;
2296 continue;
2297 }
2298
2299 offset = qcow2_alloc_clusters(bs, s->cluster_size);
2300 if (offset < 0) {
2301 ret = offset;
2302 goto fail;
2303 }
2304
2305
2306 assert((offset & L2E_OFFSET_MASK) == offset);
2307
2308 if (l2_refcount > 1) {
2309
2310
2311 ret = qcow2_update_cluster_refcount(
2312 bs, offset >> s->cluster_bits,
2313 refcount_diff(1, l2_refcount), false,
2314 QCOW2_DISCARD_OTHER);
2315 if (ret < 0) {
2316 qcow2_free_clusters(bs, offset, s->cluster_size,
2317 QCOW2_DISCARD_OTHER);
2318 goto fail;
2319 }
2320 }
2321 }
2322
2323 if (offset_into_cluster(s, offset)) {
2324 int l2_index = slice * s->l2_slice_size + j;
2325 qcow2_signal_corruption(
2326 bs, true, -1, -1,
2327 "Cluster allocation offset "
2328 "%#" PRIx64 " unaligned (L2 offset: %#"
2329 PRIx64 ", L2 index: %#x)", offset,
2330 l2_offset, l2_index);
2331 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
2332 qcow2_free_clusters(bs, offset, s->cluster_size,
2333 QCOW2_DISCARD_ALWAYS);
2334 }
2335 ret = -EIO;
2336 goto fail;
2337 }
2338
2339 ret = qcow2_pre_write_overlap_check(bs, 0, offset,
2340 s->cluster_size, true);
2341 if (ret < 0) {
2342 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
2343 qcow2_free_clusters(bs, offset, s->cluster_size,
2344 QCOW2_DISCARD_ALWAYS);
2345 }
2346 goto fail;
2347 }
2348
2349 ret = bdrv_pwrite_zeroes(s->data_file, offset,
2350 s->cluster_size, 0);
2351 if (ret < 0) {
2352 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
2353 qcow2_free_clusters(bs, offset, s->cluster_size,
2354 QCOW2_DISCARD_ALWAYS);
2355 }
2356 goto fail;
2357 }
2358
2359 if (l2_refcount == 1) {
2360 set_l2_entry(s, l2_slice, j, offset | QCOW_OFLAG_COPIED);
2361 } else {
2362 set_l2_entry(s, l2_slice, j, offset);
2363 }
2364
2365
2366
2367
2368 l2_dirty = true;
2369 }
2370
2371 if (is_active_l1) {
2372 if (l2_dirty) {
2373 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
2374 qcow2_cache_depends_on_flush(s->l2_table_cache);
2375 }
2376 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
2377 } else {
2378 if (l2_dirty) {
2379 ret = qcow2_pre_write_overlap_check(
2380 bs, QCOW2_OL_INACTIVE_L2 | QCOW2_OL_ACTIVE_L2,
2381 slice_offset, slice_size2, false);
2382 if (ret < 0) {
2383 goto fail;
2384 }
2385
2386 ret = bdrv_pwrite(bs->file, slice_offset, slice_size2,
2387 l2_slice, 0);
2388 if (ret < 0) {
2389 goto fail;
2390 }
2391 }
2392 }
2393 }
2394
2395 (*visited_l1_entries)++;
2396 if (status_cb) {
2397 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
2398 }
2399 }
2400
2401 ret = 0;
2402
2403fail:
2404 if (l2_slice) {
2405 if (!is_active_l1) {
2406 qemu_vfree(l2_slice);
2407 } else {
2408 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
2409 }
2410 }
2411 return ret;
2412}
2413
2414
2415
2416
2417
2418
2419
2420int qcow2_expand_zero_clusters(BlockDriverState *bs,
2421 BlockDriverAmendStatusCB *status_cb,
2422 void *cb_opaque)
2423{
2424 BDRVQcow2State *s = bs->opaque;
2425 uint64_t *l1_table = NULL;
2426 int64_t l1_entries = 0, visited_l1_entries = 0;
2427 int ret;
2428 int i, j;
2429
2430 if (status_cb) {
2431 l1_entries = s->l1_size;
2432 for (i = 0; i < s->nb_snapshots; i++) {
2433 l1_entries += s->snapshots[i].l1_size;
2434 }
2435 }
2436
2437 ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
2438 &visited_l1_entries, l1_entries,
2439 status_cb, cb_opaque);
2440 if (ret < 0) {
2441 goto fail;
2442 }
2443
2444
2445
2446
2447
2448
2449
2450
2451 ret = qcow2_cache_empty(bs, s->l2_table_cache);
2452 if (ret < 0) {
2453 goto fail;
2454 }
2455
2456 for (i = 0; i < s->nb_snapshots; i++) {
2457 int l1_size2;
2458 uint64_t *new_l1_table;
2459 Error *local_err = NULL;
2460
2461 ret = qcow2_validate_table(bs, s->snapshots[i].l1_table_offset,
2462 s->snapshots[i].l1_size, L1E_SIZE,
2463 QCOW_MAX_L1_SIZE, "Snapshot L1 table",
2464 &local_err);
2465 if (ret < 0) {
2466 error_report_err(local_err);
2467 goto fail;
2468 }
2469
2470 l1_size2 = s->snapshots[i].l1_size * L1E_SIZE;
2471 new_l1_table = g_try_realloc(l1_table, l1_size2);
2472
2473 if (!new_l1_table) {
2474 ret = -ENOMEM;
2475 goto fail;
2476 }
2477
2478 l1_table = new_l1_table;
2479
2480 ret = bdrv_pread(bs->file, s->snapshots[i].l1_table_offset, l1_size2,
2481 l1_table, 0);
2482 if (ret < 0) {
2483 goto fail;
2484 }
2485
2486 for (j = 0; j < s->snapshots[i].l1_size; j++) {
2487 be64_to_cpus(&l1_table[j]);
2488 }
2489
2490 ret = expand_zero_clusters_in_l1(bs, l1_table, s->snapshots[i].l1_size,
2491 &visited_l1_entries, l1_entries,
2492 status_cb, cb_opaque);
2493 if (ret < 0) {
2494 goto fail;
2495 }
2496 }
2497
2498 ret = 0;
2499
2500fail:
2501 g_free(l1_table);
2502 return ret;
2503}
2504
2505void qcow2_parse_compressed_l2_entry(BlockDriverState *bs, uint64_t l2_entry,
2506 uint64_t *coffset, int *csize)
2507{
2508 BDRVQcow2State *s = bs->opaque;
2509 int nb_csectors;
2510
2511 assert(qcow2_get_cluster_type(bs, l2_entry) == QCOW2_CLUSTER_COMPRESSED);
2512
2513 *coffset = l2_entry & s->cluster_offset_mask;
2514
2515 nb_csectors = ((l2_entry >> s->csize_shift) & s->csize_mask) + 1;
2516 *csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE -
2517 (*coffset & (QCOW2_COMPRESSED_SECTOR_SIZE - 1));
2518}
2519