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