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