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_part(bs,
456 src_cluster_offset + offset_in_cluster,
457 qiov->size, qiov, 0, 0);
458 if (ret < 0) {
459 return ret;
460 }
461
462 return 0;
463}
464
465static int coroutine_fn do_perform_cow_write(BlockDriverState *bs,
466 uint64_t cluster_offset,
467 unsigned offset_in_cluster,
468 QEMUIOVector *qiov)
469{
470 BDRVQcow2State *s = bs->opaque;
471 int ret;
472
473 if (qiov->size == 0) {
474 return 0;
475 }
476
477 ret = qcow2_pre_write_overlap_check(bs, 0,
478 cluster_offset + offset_in_cluster, qiov->size, true);
479 if (ret < 0) {
480 return ret;
481 }
482
483 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
484 ret = bdrv_co_pwritev(s->data_file, cluster_offset + offset_in_cluster,
485 qiov->size, qiov, 0);
486 if (ret < 0) {
487 return ret;
488 }
489
490 return 0;
491}
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
511 unsigned int *bytes, uint64_t *cluster_offset)
512{
513 BDRVQcow2State *s = bs->opaque;
514 unsigned int l2_index;
515 uint64_t l1_index, l2_offset, *l2_slice;
516 int c;
517 unsigned int offset_in_cluster;
518 uint64_t bytes_available, bytes_needed, nb_clusters;
519 QCow2ClusterType type;
520 int ret;
521
522 offset_in_cluster = offset_into_cluster(s, offset);
523 bytes_needed = (uint64_t) *bytes + offset_in_cluster;
524
525
526
527
528 bytes_available =
529 ((uint64_t) (s->l2_slice_size - offset_to_l2_slice_index(s, offset)))
530 << s->cluster_bits;
531
532 if (bytes_needed > bytes_available) {
533 bytes_needed = bytes_available;
534 }
535
536 *cluster_offset = 0;
537
538
539
540 l1_index = offset_to_l1_index(s, offset);
541 if (l1_index >= s->l1_size) {
542 type = QCOW2_CLUSTER_UNALLOCATED;
543 goto out;
544 }
545
546 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
547 if (!l2_offset) {
548 type = QCOW2_CLUSTER_UNALLOCATED;
549 goto out;
550 }
551
552 if (offset_into_cluster(s, l2_offset)) {
553 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
554 " unaligned (L1 index: %#" PRIx64 ")",
555 l2_offset, l1_index);
556 return -EIO;
557 }
558
559
560
561 ret = l2_load(bs, offset, l2_offset, &l2_slice);
562 if (ret < 0) {
563 return ret;
564 }
565
566
567
568 l2_index = offset_to_l2_slice_index(s, offset);
569 *cluster_offset = be64_to_cpu(l2_slice[l2_index]);
570
571 nb_clusters = size_to_clusters(s, bytes_needed);
572
573
574
575 assert(nb_clusters <= INT_MAX);
576
577 type = qcow2_get_cluster_type(bs, *cluster_offset);
578 if (s->qcow_version < 3 && (type == QCOW2_CLUSTER_ZERO_PLAIN ||
579 type == QCOW2_CLUSTER_ZERO_ALLOC)) {
580 qcow2_signal_corruption(bs, true, -1, -1, "Zero cluster entry found"
581 " in pre-v3 image (L2 offset: %#" PRIx64
582 ", L2 index: %#x)", l2_offset, l2_index);
583 ret = -EIO;
584 goto fail;
585 }
586 switch (type) {
587 case QCOW2_CLUSTER_COMPRESSED:
588 if (has_data_file(bs)) {
589 qcow2_signal_corruption(bs, true, -1, -1, "Compressed cluster "
590 "entry found in image with external data "
591 "file (L2 offset: %#" PRIx64 ", L2 index: "
592 "%#x)", l2_offset, l2_index);
593 ret = -EIO;
594 goto fail;
595 }
596
597 c = 1;
598 *cluster_offset &= L2E_COMPRESSED_OFFSET_SIZE_MASK;
599 break;
600 case QCOW2_CLUSTER_ZERO_PLAIN:
601 case QCOW2_CLUSTER_UNALLOCATED:
602
603 c = count_contiguous_clusters_unallocated(bs, nb_clusters,
604 &l2_slice[l2_index], type);
605 *cluster_offset = 0;
606 break;
607 case QCOW2_CLUSTER_ZERO_ALLOC:
608 case QCOW2_CLUSTER_NORMAL:
609
610 c = count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
611 &l2_slice[l2_index], QCOW_OFLAG_ZERO);
612 *cluster_offset &= L2E_OFFSET_MASK;
613 if (offset_into_cluster(s, *cluster_offset)) {
614 qcow2_signal_corruption(bs, true, -1, -1,
615 "Cluster allocation offset %#"
616 PRIx64 " unaligned (L2 offset: %#" PRIx64
617 ", L2 index: %#x)", *cluster_offset,
618 l2_offset, l2_index);
619 ret = -EIO;
620 goto fail;
621 }
622 if (has_data_file(bs) && *cluster_offset != offset - offset_in_cluster)
623 {
624 qcow2_signal_corruption(bs, true, -1, -1,
625 "External data file host cluster offset %#"
626 PRIx64 " does not match guest cluster "
627 "offset: %#" PRIx64
628 ", L2 index: %#x)", *cluster_offset,
629 offset - offset_in_cluster, l2_index);
630 ret = -EIO;
631 goto fail;
632 }
633 break;
634 default:
635 abort();
636 }
637
638 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
639
640 bytes_available = (int64_t)c * s->cluster_size;
641
642out:
643 if (bytes_available > bytes_needed) {
644 bytes_available = bytes_needed;
645 }
646
647
648
649
650 assert(bytes_available - offset_in_cluster <= UINT_MAX);
651 *bytes = bytes_available - offset_in_cluster;
652
653 return type;
654
655fail:
656 qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
657 return ret;
658}
659
660
661
662
663
664
665
666
667
668
669
670static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
671 uint64_t **new_l2_slice,
672 int *new_l2_index)
673{
674 BDRVQcow2State *s = bs->opaque;
675 unsigned int l2_index;
676 uint64_t l1_index, l2_offset;
677 uint64_t *l2_slice = NULL;
678 int ret;
679
680
681
682 l1_index = offset_to_l1_index(s, offset);
683 if (l1_index >= s->l1_size) {
684 ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
685 if (ret < 0) {
686 return ret;
687 }
688 }
689
690 assert(l1_index < s->l1_size);
691 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
692 if (offset_into_cluster(s, l2_offset)) {
693 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
694 " unaligned (L1 index: %#" PRIx64 ")",
695 l2_offset, l1_index);
696 return -EIO;
697 }
698
699 if (!(s->l1_table[l1_index] & QCOW_OFLAG_COPIED)) {
700
701 ret = l2_allocate(bs, l1_index);
702 if (ret < 0) {
703 return ret;
704 }
705
706
707 if (l2_offset) {
708 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
709 QCOW2_DISCARD_OTHER);
710 }
711
712
713 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
714 assert(offset_into_cluster(s, l2_offset) == 0);
715 }
716
717
718 ret = l2_load(bs, offset, l2_offset, &l2_slice);
719 if (ret < 0) {
720 return ret;
721 }
722
723
724
725 l2_index = offset_to_l2_slice_index(s, offset);
726
727 *new_l2_slice = l2_slice;
728 *new_l2_index = l2_index;
729
730 return 0;
731}
732
733
734
735
736
737
738
739
740
741
742int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
743 uint64_t offset,
744 int compressed_size,
745 uint64_t *host_offset)
746{
747 BDRVQcow2State *s = bs->opaque;
748 int l2_index, ret;
749 uint64_t *l2_slice;
750 int64_t cluster_offset;
751 int nb_csectors;
752
753 if (has_data_file(bs)) {
754 return 0;
755 }
756
757 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
758 if (ret < 0) {
759 return ret;
760 }
761
762
763
764 cluster_offset = be64_to_cpu(l2_slice[l2_index]);
765 if (cluster_offset & L2E_OFFSET_MASK) {
766 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
767 return -EIO;
768 }
769
770 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
771 if (cluster_offset < 0) {
772 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
773 return cluster_offset;
774 }
775
776 nb_csectors =
777 (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
778 (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
779
780 cluster_offset |= QCOW_OFLAG_COMPRESSED |
781 ((uint64_t)nb_csectors << s->csize_shift);
782
783
784
785
786
787 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
788 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
789 l2_slice[l2_index] = cpu_to_be64(cluster_offset);
790 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
791
792 *host_offset = cluster_offset & s->cluster_offset_mask;
793 return 0;
794}
795
796static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)
797{
798 BDRVQcow2State *s = bs->opaque;
799 Qcow2COWRegion *start = &m->cow_start;
800 Qcow2COWRegion *end = &m->cow_end;
801 unsigned buffer_size;
802 unsigned data_bytes = end->offset - (start->offset + start->nb_bytes);
803 bool merge_reads;
804 uint8_t *start_buffer, *end_buffer;
805 QEMUIOVector qiov;
806 int ret;
807
808 assert(start->nb_bytes <= UINT_MAX - end->nb_bytes);
809 assert(start->nb_bytes + end->nb_bytes <= UINT_MAX - data_bytes);
810 assert(start->offset + start->nb_bytes <= end->offset);
811
812 if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
813 return 0;
814 }
815
816
817
818
819 merge_reads = start->nb_bytes && end->nb_bytes && data_bytes <= 16384;
820 if (merge_reads) {
821 buffer_size = start->nb_bytes + data_bytes + end->nb_bytes;
822 } else {
823
824
825
826 size_t align = bdrv_opt_mem_align(bs);
827 assert(align > 0 && align <= UINT_MAX);
828 assert(QEMU_ALIGN_UP(start->nb_bytes, align) <=
829 UINT_MAX - end->nb_bytes);
830 buffer_size = QEMU_ALIGN_UP(start->nb_bytes, align) + end->nb_bytes;
831 }
832
833
834
835 start_buffer = qemu_try_blockalign(bs, buffer_size);
836 if (start_buffer == NULL) {
837 return -ENOMEM;
838 }
839
840 end_buffer = start_buffer + buffer_size - end->nb_bytes;
841
842 qemu_iovec_init(&qiov, 2 + (m->data_qiov ?
843 qemu_iovec_subvec_niov(m->data_qiov,
844 m->data_qiov_offset,
845 data_bytes)
846 : 0));
847
848 qemu_co_mutex_unlock(&s->lock);
849
850
851
852 if (merge_reads) {
853 qemu_iovec_add(&qiov, start_buffer, buffer_size);
854 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
855 } else {
856 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
857 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
858 if (ret < 0) {
859 goto fail;
860 }
861
862 qemu_iovec_reset(&qiov);
863 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
864 ret = do_perform_cow_read(bs, m->offset, end->offset, &qiov);
865 }
866 if (ret < 0) {
867 goto fail;
868 }
869
870
871 if (bs->encrypted) {
872 ret = qcow2_co_encrypt(bs,
873 m->alloc_offset + start->offset,
874 m->offset + start->offset,
875 start_buffer, start->nb_bytes);
876 if (ret < 0) {
877 goto fail;
878 }
879
880 ret = qcow2_co_encrypt(bs,
881 m->alloc_offset + end->offset,
882 m->offset + end->offset,
883 end_buffer, end->nb_bytes);
884 if (ret < 0) {
885 goto fail;
886 }
887 }
888
889
890
891 if (m->data_qiov) {
892 qemu_iovec_reset(&qiov);
893 if (start->nb_bytes) {
894 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
895 }
896 qemu_iovec_concat(&qiov, m->data_qiov, m->data_qiov_offset, data_bytes);
897 if (end->nb_bytes) {
898 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
899 }
900
901
902
903 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
904 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
905 } else {
906
907 qemu_iovec_reset(&qiov);
908 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
909 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
910 if (ret < 0) {
911 goto fail;
912 }
913
914 qemu_iovec_reset(&qiov);
915 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
916 ret = do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov);
917 }
918
919fail:
920 qemu_co_mutex_lock(&s->lock);
921
922
923
924
925
926
927 if (ret == 0) {
928 qcow2_cache_depends_on_flush(s->l2_table_cache);
929 }
930
931 qemu_vfree(start_buffer);
932 qemu_iovec_destroy(&qiov);
933 return ret;
934}
935
936int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
937{
938 BDRVQcow2State *s = bs->opaque;
939 int i, j = 0, l2_index, ret;
940 uint64_t *old_cluster, *l2_slice;
941 uint64_t cluster_offset = m->alloc_offset;
942
943 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters);
944 assert(m->nb_clusters > 0);
945
946 old_cluster = g_try_new(uint64_t, m->nb_clusters);
947 if (old_cluster == NULL) {
948 ret = -ENOMEM;
949 goto err;
950 }
951
952
953 ret = perform_cow(bs, m);
954 if (ret < 0) {
955 goto err;
956 }
957
958
959 if (s->use_lazy_refcounts) {
960 qcow2_mark_dirty(bs);
961 }
962 if (qcow2_need_accurate_refcounts(s)) {
963 qcow2_cache_set_dependency(bs, s->l2_table_cache,
964 s->refcount_block_cache);
965 }
966
967 ret = get_cluster_table(bs, m->offset, &l2_slice, &l2_index);
968 if (ret < 0) {
969 goto err;
970 }
971 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
972
973 assert(l2_index + m->nb_clusters <= s->l2_slice_size);
974 for (i = 0; i < m->nb_clusters; i++) {
975
976
977
978
979
980
981 if (l2_slice[l2_index + i] != 0) {
982 old_cluster[j++] = l2_slice[l2_index + i];
983 }
984
985 l2_slice[l2_index + i] = cpu_to_be64((cluster_offset +
986 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
987 }
988
989
990 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
991
992
993
994
995
996
997
998 if (!m->keep_old_clusters && j != 0) {
999 for (i = 0; i < j; i++) {
1000 qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1,
1001 QCOW2_DISCARD_NEVER);
1002 }
1003 }
1004
1005 ret = 0;
1006err:
1007 g_free(old_cluster);
1008 return ret;
1009 }
1010
1011
1012
1013
1014
1015void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m)
1016{
1017 BDRVQcow2State *s = bs->opaque;
1018 if (!has_data_file(bs) && !m->keep_old_clusters) {
1019 qcow2_free_clusters(bs, m->alloc_offset,
1020 m->nb_clusters << s->cluster_bits,
1021 QCOW2_DISCARD_NEVER);
1022 }
1023}
1024
1025
1026
1027
1028
1029
1030static int count_cow_clusters(BlockDriverState *bs, int nb_clusters,
1031 uint64_t *l2_slice, int l2_index)
1032{
1033 int i;
1034
1035 for (i = 0; i < nb_clusters; i++) {
1036 uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
1037 QCow2ClusterType cluster_type = qcow2_get_cluster_type(bs, l2_entry);
1038
1039 switch(cluster_type) {
1040 case QCOW2_CLUSTER_NORMAL:
1041 if (l2_entry & QCOW_OFLAG_COPIED) {
1042 goto out;
1043 }
1044 break;
1045 case QCOW2_CLUSTER_UNALLOCATED:
1046 case QCOW2_CLUSTER_COMPRESSED:
1047 case QCOW2_CLUSTER_ZERO_PLAIN:
1048 case QCOW2_CLUSTER_ZERO_ALLOC:
1049 break;
1050 default:
1051 abort();
1052 }
1053 }
1054
1055out:
1056 assert(i <= nb_clusters);
1057 return i;
1058}
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,
1075 uint64_t *cur_bytes, QCowL2Meta **m)
1076{
1077 BDRVQcow2State *s = bs->opaque;
1078 QCowL2Meta *old_alloc;
1079 uint64_t bytes = *cur_bytes;
1080
1081 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
1082
1083 uint64_t start = guest_offset;
1084 uint64_t end = start + bytes;
1085 uint64_t old_start = l2meta_cow_start(old_alloc);
1086 uint64_t old_end = l2meta_cow_end(old_alloc);
1087
1088 if (end <= old_start || start >= old_end) {
1089
1090 } else {
1091 if (start < old_start) {
1092
1093 bytes = old_start - start;
1094 } else {
1095 bytes = 0;
1096 }
1097
1098
1099
1100
1101
1102 if (bytes == 0 && *m) {
1103 *cur_bytes = 0;
1104 return 0;
1105 }
1106
1107 if (bytes == 0) {
1108
1109
1110 qemu_co_queue_wait(&old_alloc->dependent_requests, &s->lock);
1111 return -EAGAIN;
1112 }
1113 }
1114 }
1115
1116
1117
1118 *cur_bytes = bytes;
1119
1120 return 0;
1121}
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,
1146 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
1147{
1148 BDRVQcow2State *s = bs->opaque;
1149 int l2_index;
1150 uint64_t cluster_offset;
1151 uint64_t *l2_slice;
1152 uint64_t nb_clusters;
1153 unsigned int keep_clusters;
1154 int ret;
1155
1156 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset,
1157 *bytes);
1158
1159 assert(*host_offset == INV_OFFSET || offset_into_cluster(s, guest_offset)
1160 == offset_into_cluster(s, *host_offset));
1161
1162
1163
1164
1165
1166 nb_clusters =
1167 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1168
1169 l2_index = offset_to_l2_slice_index(s, guest_offset);
1170 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1171 assert(nb_clusters <= INT_MAX);
1172
1173
1174 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
1175 if (ret < 0) {
1176 return ret;
1177 }
1178
1179 cluster_offset = be64_to_cpu(l2_slice[l2_index]);
1180
1181
1182 if (qcow2_get_cluster_type(bs, cluster_offset) == QCOW2_CLUSTER_NORMAL
1183 && (cluster_offset & QCOW_OFLAG_COPIED))
1184 {
1185
1186 bool offset_matches =
1187 (cluster_offset & L2E_OFFSET_MASK) == *host_offset;
1188
1189 if (offset_into_cluster(s, cluster_offset & L2E_OFFSET_MASK)) {
1190 qcow2_signal_corruption(bs, true, -1, -1, "Data cluster offset "
1191 "%#llx unaligned (guest offset: %#" PRIx64
1192 ")", cluster_offset & L2E_OFFSET_MASK,
1193 guest_offset);
1194 ret = -EIO;
1195 goto out;
1196 }
1197
1198 if (*host_offset != INV_OFFSET && !offset_matches) {
1199 *bytes = 0;
1200 ret = 0;
1201 goto out;
1202 }
1203
1204
1205 keep_clusters =
1206 count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
1207 &l2_slice[l2_index],
1208 QCOW_OFLAG_COPIED | QCOW_OFLAG_ZERO);
1209 assert(keep_clusters <= nb_clusters);
1210
1211 *bytes = MIN(*bytes,
1212 keep_clusters * s->cluster_size
1213 - offset_into_cluster(s, guest_offset));
1214
1215 ret = 1;
1216 } else {
1217 ret = 0;
1218 }
1219
1220
1221out:
1222 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1223
1224
1225
1226 if (ret > 0) {
1227 *host_offset = (cluster_offset & L2E_OFFSET_MASK)
1228 + offset_into_cluster(s, guest_offset);
1229 }
1230
1231 return ret;
1232}
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset,
1254 uint64_t *host_offset, uint64_t *nb_clusters)
1255{
1256 BDRVQcow2State *s = bs->opaque;
1257
1258 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset,
1259 *host_offset, *nb_clusters);
1260
1261 if (has_data_file(bs)) {
1262 assert(*host_offset == INV_OFFSET ||
1263 *host_offset == start_of_cluster(s, guest_offset));
1264 *host_offset = start_of_cluster(s, guest_offset);
1265 return 0;
1266 }
1267
1268
1269 trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
1270 if (*host_offset == INV_OFFSET) {
1271 int64_t cluster_offset =
1272 qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size);
1273 if (cluster_offset < 0) {
1274 return cluster_offset;
1275 }
1276 *host_offset = cluster_offset;
1277 return 0;
1278 } else {
1279 int64_t ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters);
1280 if (ret < 0) {
1281 return ret;
1282 }
1283 *nb_clusters = ret;
1284 return 0;
1285 }
1286}
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
1309 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
1310{
1311 BDRVQcow2State *s = bs->opaque;
1312 int l2_index;
1313 uint64_t *l2_slice;
1314 uint64_t entry;
1315 uint64_t nb_clusters;
1316 int ret;
1317 bool keep_old_clusters = false;
1318
1319 uint64_t alloc_cluster_offset = INV_OFFSET;
1320
1321 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset,
1322 *bytes);
1323 assert(*bytes > 0);
1324
1325
1326
1327
1328
1329 nb_clusters =
1330 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1331
1332 l2_index = offset_to_l2_slice_index(s, guest_offset);
1333 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1334 assert(nb_clusters <= INT_MAX);
1335
1336
1337 nb_clusters = MIN(nb_clusters, INT_MAX >> s->cluster_bits);
1338
1339
1340 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
1341 if (ret < 0) {
1342 return ret;
1343 }
1344
1345 entry = be64_to_cpu(l2_slice[l2_index]);
1346 nb_clusters = count_cow_clusters(bs, nb_clusters, l2_slice, l2_index);
1347
1348
1349
1350
1351 assert(nb_clusters > 0);
1352
1353 if (qcow2_get_cluster_type(bs, entry) == QCOW2_CLUSTER_ZERO_ALLOC &&
1354 (entry & QCOW_OFLAG_COPIED) &&
1355 (*host_offset == INV_OFFSET ||
1356 start_of_cluster(s, *host_offset) == (entry & L2E_OFFSET_MASK)))
1357 {
1358 int preallocated_nb_clusters;
1359
1360 if (offset_into_cluster(s, entry & L2E_OFFSET_MASK)) {
1361 qcow2_signal_corruption(bs, true, -1, -1, "Preallocated zero "
1362 "cluster offset %#llx unaligned (guest "
1363 "offset: %#" PRIx64 ")",
1364 entry & L2E_OFFSET_MASK, guest_offset);
1365 ret = -EIO;
1366 goto fail;
1367 }
1368
1369
1370
1371
1372 preallocated_nb_clusters =
1373 count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
1374 &l2_slice[l2_index], QCOW_OFLAG_COPIED);
1375 assert(preallocated_nb_clusters > 0);
1376
1377 nb_clusters = preallocated_nb_clusters;
1378 alloc_cluster_offset = entry & L2E_OFFSET_MASK;
1379
1380
1381
1382 keep_old_clusters = true;
1383 }
1384
1385 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1386
1387 if (alloc_cluster_offset == INV_OFFSET) {
1388
1389 alloc_cluster_offset = *host_offset == INV_OFFSET ? INV_OFFSET :
1390 start_of_cluster(s, *host_offset);
1391 ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,
1392 &nb_clusters);
1393 if (ret < 0) {
1394 goto fail;
1395 }
1396
1397
1398 if (nb_clusters == 0) {
1399 *bytes = 0;
1400 return 0;
1401 }
1402
1403 assert(alloc_cluster_offset != INV_OFFSET);
1404 }
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420 uint64_t requested_bytes = *bytes + offset_into_cluster(s, guest_offset);
1421 int avail_bytes = nb_clusters << s->cluster_bits;
1422 int nb_bytes = MIN(requested_bytes, avail_bytes);
1423 QCowL2Meta *old_m = *m;
1424
1425 *m = g_malloc0(sizeof(**m));
1426
1427 **m = (QCowL2Meta) {
1428 .next = old_m,
1429
1430 .alloc_offset = alloc_cluster_offset,
1431 .offset = start_of_cluster(s, guest_offset),
1432 .nb_clusters = nb_clusters,
1433
1434 .keep_old_clusters = keep_old_clusters,
1435
1436 .cow_start = {
1437 .offset = 0,
1438 .nb_bytes = offset_into_cluster(s, guest_offset),
1439 },
1440 .cow_end = {
1441 .offset = nb_bytes,
1442 .nb_bytes = avail_bytes - nb_bytes,
1443 },
1444 };
1445 qemu_co_queue_init(&(*m)->dependent_requests);
1446 QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);
1447
1448 *host_offset = alloc_cluster_offset + offset_into_cluster(s, guest_offset);
1449 *bytes = MIN(*bytes, nb_bytes - offset_into_cluster(s, guest_offset));
1450 assert(*bytes != 0);
1451
1452 return 1;
1453
1454fail:
1455 if (*m && (*m)->nb_clusters > 0) {
1456 QLIST_REMOVE(*m, next_in_flight);
1457 }
1458 return ret;
1459}
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
1481 unsigned int *bytes, uint64_t *host_offset,
1482 QCowL2Meta **m)
1483{
1484 BDRVQcow2State *s = bs->opaque;
1485 uint64_t start, remaining;
1486 uint64_t cluster_offset;
1487 uint64_t cur_bytes;
1488 int ret;
1489
1490 trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, *bytes);
1491
1492again:
1493 start = offset;
1494 remaining = *bytes;
1495 cluster_offset = INV_OFFSET;
1496 *host_offset = INV_OFFSET;
1497 cur_bytes = 0;
1498 *m = NULL;
1499
1500 while (true) {
1501
1502 if (*host_offset == INV_OFFSET && cluster_offset != INV_OFFSET) {
1503 *host_offset = start_of_cluster(s, cluster_offset);
1504 }
1505
1506 assert(remaining >= cur_bytes);
1507
1508 start += cur_bytes;
1509 remaining -= cur_bytes;
1510
1511 if (cluster_offset != INV_OFFSET) {
1512 cluster_offset += cur_bytes;
1513 }
1514
1515 if (remaining == 0) {
1516 break;
1517 }
1518
1519 cur_bytes = remaining;
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539 ret = handle_dependencies(bs, start, &cur_bytes, m);
1540 if (ret == -EAGAIN) {
1541
1542
1543
1544 assert(*m == NULL);
1545 goto again;
1546 } else if (ret < 0) {
1547 return ret;
1548 } else if (cur_bytes == 0) {
1549 break;
1550 } else {
1551
1552
1553
1554 }
1555
1556
1557
1558
1559 ret = handle_copied(bs, start, &cluster_offset, &cur_bytes, m);
1560 if (ret < 0) {
1561 return ret;
1562 } else if (ret) {
1563 continue;
1564 } else if (cur_bytes == 0) {
1565 break;
1566 }
1567
1568
1569
1570
1571
1572 ret = handle_alloc(bs, start, &cluster_offset, &cur_bytes, m);
1573 if (ret < 0) {
1574 return ret;
1575 } else if (ret) {
1576 continue;
1577 } else {
1578 assert(cur_bytes == 0);
1579 break;
1580 }
1581 }
1582
1583 *bytes -= remaining;
1584 assert(*bytes > 0);
1585 assert(*host_offset != INV_OFFSET);
1586
1587 return 0;
1588}
1589
1590
1591
1592
1593
1594
1595static int discard_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1596 uint64_t nb_clusters,
1597 enum qcow2_discard_type type, bool full_discard)
1598{
1599 BDRVQcow2State *s = bs->opaque;
1600 uint64_t *l2_slice;
1601 int l2_index;
1602 int ret;
1603 int i;
1604
1605 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1606 if (ret < 0) {
1607 return ret;
1608 }
1609
1610
1611 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1612 assert(nb_clusters <= INT_MAX);
1613
1614 for (i = 0; i < nb_clusters; i++) {
1615 uint64_t old_l2_entry;
1616
1617 old_l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632 switch (qcow2_get_cluster_type(bs, old_l2_entry)) {
1633 case QCOW2_CLUSTER_UNALLOCATED:
1634 if (full_discard || !bs->backing) {
1635 continue;
1636 }
1637 break;
1638
1639 case QCOW2_CLUSTER_ZERO_PLAIN:
1640 if (!full_discard) {
1641 continue;
1642 }
1643 break;
1644
1645 case QCOW2_CLUSTER_ZERO_ALLOC:
1646 case QCOW2_CLUSTER_NORMAL:
1647 case QCOW2_CLUSTER_COMPRESSED:
1648 break;
1649
1650 default:
1651 abort();
1652 }
1653
1654
1655 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1656 if (!full_discard && s->qcow_version >= 3) {
1657 l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
1658 } else {
1659 l2_slice[l2_index + i] = cpu_to_be64(0);
1660 }
1661
1662
1663 qcow2_free_any_clusters(bs, old_l2_entry, 1, type);
1664 }
1665
1666 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1667
1668 return nb_clusters;
1669}
1670
1671int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
1672 uint64_t bytes, enum qcow2_discard_type type,
1673 bool full_discard)
1674{
1675 BDRVQcow2State *s = bs->opaque;
1676 uint64_t end_offset = offset + bytes;
1677 uint64_t nb_clusters;
1678 int64_t cleared;
1679 int ret;
1680
1681
1682 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1683 assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1684 end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1685
1686 nb_clusters = size_to_clusters(s, bytes);
1687
1688 s->cache_discards = true;
1689
1690
1691 while (nb_clusters > 0) {
1692 cleared = discard_in_l2_slice(bs, offset, nb_clusters, type,
1693 full_discard);
1694 if (cleared < 0) {
1695 ret = cleared;
1696 goto fail;
1697 }
1698
1699 nb_clusters -= cleared;
1700 offset += (cleared * s->cluster_size);
1701 }
1702
1703 ret = 0;
1704fail:
1705 s->cache_discards = false;
1706 qcow2_process_discards(bs, ret);
1707
1708 return ret;
1709}
1710
1711
1712
1713
1714
1715
1716static int zero_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1717 uint64_t nb_clusters, int flags)
1718{
1719 BDRVQcow2State *s = bs->opaque;
1720 uint64_t *l2_slice;
1721 int l2_index;
1722 int ret;
1723 int i;
1724 bool unmap = !!(flags & BDRV_REQ_MAY_UNMAP);
1725
1726 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1727 if (ret < 0) {
1728 return ret;
1729 }
1730
1731
1732 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
1733 assert(nb_clusters <= INT_MAX);
1734
1735 for (i = 0; i < nb_clusters; i++) {
1736 uint64_t old_offset;
1737 QCow2ClusterType cluster_type;
1738
1739 old_offset = be64_to_cpu(l2_slice[l2_index + i]);
1740
1741
1742
1743
1744
1745 cluster_type = qcow2_get_cluster_type(bs, old_offset);
1746 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN ||
1747 (cluster_type == QCOW2_CLUSTER_ZERO_ALLOC && !unmap)) {
1748 continue;
1749 }
1750
1751 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1752 if (cluster_type == QCOW2_CLUSTER_COMPRESSED || unmap) {
1753 l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
1754 qcow2_free_any_clusters(bs, old_offset, 1, QCOW2_DISCARD_REQUEST);
1755 } else {
1756 l2_slice[l2_index + i] |= cpu_to_be64(QCOW_OFLAG_ZERO);
1757 }
1758 }
1759
1760 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1761
1762 return nb_clusters;
1763}
1764
1765int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
1766 uint64_t bytes, int flags)
1767{
1768 BDRVQcow2State *s = bs->opaque;
1769 uint64_t end_offset = offset + bytes;
1770 uint64_t nb_clusters;
1771 int64_t cleared;
1772 int ret;
1773
1774
1775
1776 if (data_file_is_raw(bs)) {
1777 assert(has_data_file(bs));
1778 ret = bdrv_co_pwrite_zeroes(s->data_file, offset, bytes, flags);
1779 if (ret < 0) {
1780 return ret;
1781 }
1782 }
1783
1784
1785 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1786 assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1787 end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1788
1789
1790 if (s->qcow_version < 3) {
1791 return -ENOTSUP;
1792 }
1793
1794
1795 nb_clusters = size_to_clusters(s, bytes);
1796
1797 s->cache_discards = true;
1798
1799 while (nb_clusters > 0) {
1800 cleared = zero_in_l2_slice(bs, offset, nb_clusters, flags);
1801 if (cleared < 0) {
1802 ret = cleared;
1803 goto fail;
1804 }
1805
1806 nb_clusters -= cleared;
1807 offset += (cleared * s->cluster_size);
1808 }
1809
1810 ret = 0;
1811fail:
1812 s->cache_discards = false;
1813 qcow2_process_discards(bs, ret);
1814
1815 return ret;
1816}
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
1827 int l1_size, int64_t *visited_l1_entries,
1828 int64_t l1_entries,
1829 BlockDriverAmendStatusCB *status_cb,
1830 void *cb_opaque)
1831{
1832 BDRVQcow2State *s = bs->opaque;
1833 bool is_active_l1 = (l1_table == s->l1_table);
1834 uint64_t *l2_slice = NULL;
1835 unsigned slice, slice_size2, n_slices;
1836 int ret;
1837 int i, j;
1838
1839 slice_size2 = s->l2_slice_size * sizeof(uint64_t);
1840 n_slices = s->cluster_size / slice_size2;
1841
1842 if (!is_active_l1) {
1843
1844
1845 l2_slice = qemu_try_blockalign(bs->file->bs, slice_size2);
1846 if (l2_slice == NULL) {
1847 return -ENOMEM;
1848 }
1849 }
1850
1851 for (i = 0; i < l1_size; i++) {
1852 uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
1853 uint64_t l2_refcount;
1854
1855 if (!l2_offset) {
1856
1857 (*visited_l1_entries)++;
1858 if (status_cb) {
1859 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
1860 }
1861 continue;
1862 }
1863
1864 if (offset_into_cluster(s, l2_offset)) {
1865 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1866 PRIx64 " unaligned (L1 index: %#x)",
1867 l2_offset, i);
1868 ret = -EIO;
1869 goto fail;
1870 }
1871
1872 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1873 &l2_refcount);
1874 if (ret < 0) {
1875 goto fail;
1876 }
1877
1878 for (slice = 0; slice < n_slices; slice++) {
1879 uint64_t slice_offset = l2_offset + slice * slice_size2;
1880 bool l2_dirty = false;
1881 if (is_active_l1) {
1882
1883 ret = qcow2_cache_get(bs, s->l2_table_cache, slice_offset,
1884 (void **)&l2_slice);
1885 } else {
1886
1887 ret = bdrv_pread(bs->file, slice_offset, l2_slice, slice_size2);
1888 }
1889 if (ret < 0) {
1890 goto fail;
1891 }
1892
1893 for (j = 0; j < s->l2_slice_size; j++) {
1894 uint64_t l2_entry = be64_to_cpu(l2_slice[j]);
1895 int64_t offset = l2_entry & L2E_OFFSET_MASK;
1896 QCow2ClusterType cluster_type =
1897 qcow2_get_cluster_type(bs, l2_entry);
1898
1899 if (cluster_type != QCOW2_CLUSTER_ZERO_PLAIN &&
1900 cluster_type != QCOW2_CLUSTER_ZERO_ALLOC) {
1901 continue;
1902 }
1903
1904 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1905 if (!bs->backing) {
1906
1907
1908 l2_slice[j] = 0;
1909 l2_dirty = true;
1910 continue;
1911 }
1912
1913 offset = qcow2_alloc_clusters(bs, s->cluster_size);
1914 if (offset < 0) {
1915 ret = offset;
1916 goto fail;
1917 }
1918
1919 if (l2_refcount > 1) {
1920
1921
1922 ret = qcow2_update_cluster_refcount(
1923 bs, offset >> s->cluster_bits,
1924 refcount_diff(1, l2_refcount), false,
1925 QCOW2_DISCARD_OTHER);
1926 if (ret < 0) {
1927 qcow2_free_clusters(bs, offset, s->cluster_size,
1928 QCOW2_DISCARD_OTHER);
1929 goto fail;
1930 }
1931 }
1932 }
1933
1934 if (offset_into_cluster(s, offset)) {
1935 int l2_index = slice * s->l2_slice_size + j;
1936 qcow2_signal_corruption(
1937 bs, true, -1, -1,
1938 "Cluster allocation offset "
1939 "%#" PRIx64 " unaligned (L2 offset: %#"
1940 PRIx64 ", L2 index: %#x)", offset,
1941 l2_offset, l2_index);
1942 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1943 qcow2_free_clusters(bs, offset, s->cluster_size,
1944 QCOW2_DISCARD_ALWAYS);
1945 }
1946 ret = -EIO;
1947 goto fail;
1948 }
1949
1950 ret = qcow2_pre_write_overlap_check(bs, 0, offset,
1951 s->cluster_size, true);
1952 if (ret < 0) {
1953 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1954 qcow2_free_clusters(bs, offset, s->cluster_size,
1955 QCOW2_DISCARD_ALWAYS);
1956 }
1957 goto fail;
1958 }
1959
1960 ret = bdrv_pwrite_zeroes(s->data_file, offset,
1961 s->cluster_size, 0);
1962 if (ret < 0) {
1963 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1964 qcow2_free_clusters(bs, offset, s->cluster_size,
1965 QCOW2_DISCARD_ALWAYS);
1966 }
1967 goto fail;
1968 }
1969
1970 if (l2_refcount == 1) {
1971 l2_slice[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
1972 } else {
1973 l2_slice[j] = cpu_to_be64(offset);
1974 }
1975 l2_dirty = true;
1976 }
1977
1978 if (is_active_l1) {
1979 if (l2_dirty) {
1980 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
1981 qcow2_cache_depends_on_flush(s->l2_table_cache);
1982 }
1983 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1984 } else {
1985 if (l2_dirty) {
1986 ret = qcow2_pre_write_overlap_check(
1987 bs, QCOW2_OL_INACTIVE_L2 | QCOW2_OL_ACTIVE_L2,
1988 slice_offset, slice_size2, false);
1989 if (ret < 0) {
1990 goto fail;
1991 }
1992
1993 ret = bdrv_pwrite(bs->file, slice_offset,
1994 l2_slice, slice_size2);
1995 if (ret < 0) {
1996 goto fail;
1997 }
1998 }
1999 }
2000 }
2001
2002 (*visited_l1_entries)++;
2003 if (status_cb) {
2004 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
2005 }
2006 }
2007
2008 ret = 0;
2009
2010fail:
2011 if (l2_slice) {
2012 if (!is_active_l1) {
2013 qemu_vfree(l2_slice);
2014 } else {
2015 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
2016 }
2017 }
2018 return ret;
2019}
2020
2021
2022
2023
2024
2025
2026
2027int qcow2_expand_zero_clusters(BlockDriverState *bs,
2028 BlockDriverAmendStatusCB *status_cb,
2029 void *cb_opaque)
2030{
2031 BDRVQcow2State *s = bs->opaque;
2032 uint64_t *l1_table = NULL;
2033 int64_t l1_entries = 0, visited_l1_entries = 0;
2034 int ret;
2035 int i, j;
2036
2037 if (status_cb) {
2038 l1_entries = s->l1_size;
2039 for (i = 0; i < s->nb_snapshots; i++) {
2040 l1_entries += s->snapshots[i].l1_size;
2041 }
2042 }
2043
2044 ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
2045 &visited_l1_entries, l1_entries,
2046 status_cb, cb_opaque);
2047 if (ret < 0) {
2048 goto fail;
2049 }
2050
2051
2052
2053
2054
2055
2056
2057
2058 ret = qcow2_cache_empty(bs, s->l2_table_cache);
2059 if (ret < 0) {
2060 goto fail;
2061 }
2062
2063 for (i = 0; i < s->nb_snapshots; i++) {
2064 int l1_size2;
2065 uint64_t *new_l1_table;
2066 Error *local_err = NULL;
2067
2068 ret = qcow2_validate_table(bs, s->snapshots[i].l1_table_offset,
2069 s->snapshots[i].l1_size, sizeof(uint64_t),
2070 QCOW_MAX_L1_SIZE, "Snapshot L1 table",
2071 &local_err);
2072 if (ret < 0) {
2073 error_report_err(local_err);
2074 goto fail;
2075 }
2076
2077 l1_size2 = s->snapshots[i].l1_size * sizeof(uint64_t);
2078 new_l1_table = g_try_realloc(l1_table, l1_size2);
2079
2080 if (!new_l1_table) {
2081 ret = -ENOMEM;
2082 goto fail;
2083 }
2084
2085 l1_table = new_l1_table;
2086
2087 ret = bdrv_pread(bs->file, s->snapshots[i].l1_table_offset,
2088 l1_table, l1_size2);
2089 if (ret < 0) {
2090 goto fail;
2091 }
2092
2093 for (j = 0; j < s->snapshots[i].l1_size; j++) {
2094 be64_to_cpus(&l1_table[j]);
2095 }
2096
2097 ret = expand_zero_clusters_in_l1(bs, l1_table, s->snapshots[i].l1_size,
2098 &visited_l1_entries, l1_entries,
2099 status_cb, cb_opaque);
2100 if (ret < 0) {
2101 goto fail;
2102 }
2103 }
2104
2105 ret = 0;
2106
2107fail:
2108 g_free(l1_table);
2109 return ret;
2110}
2111