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