29
29
static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size);
30
30
static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
31
31
int64_t offset, int64_t length,
32
int addend, enum qcow2_discard_type type);
35
35
/*********************************************************/
236
236
/* Described somewhere else. This can recurse at most twice before we
237
237
* arrive at a block that describes itself. */
238
ret = update_refcount(bs, new_block, s->cluster_size, 1);
238
ret = update_refcount(bs, new_block, s->cluster_size, 1,
239
QCOW2_DISCARD_NEVER);
400
401
/* Free old table. Remember, we must not change free_cluster_index */
401
402
uint64_t old_free_cluster_index = s->free_cluster_index;
402
qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
403
qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t),
404
QCOW2_DISCARD_OTHER);
403
405
s->free_cluster_index = old_free_cluster_index;
405
407
ret = load_refcount_block(bs, new_block, (void**) refcount_block);
423
void qcow2_process_discards(BlockDriverState *bs, int ret)
425
BDRVQcowState *s = bs->opaque;
426
Qcow2DiscardRegion *d, *next;
428
QTAILQ_FOREACH_SAFE(d, &s->discards, next, next) {
429
QTAILQ_REMOVE(&s->discards, d, next);
431
/* Discard is optional, ignore the return value */
433
bdrv_discard(bs->file,
434
d->offset >> BDRV_SECTOR_BITS,
435
d->bytes >> BDRV_SECTOR_BITS);
442
static void update_refcount_discard(BlockDriverState *bs,
443
uint64_t offset, uint64_t length)
445
BDRVQcowState *s = bs->opaque;
446
Qcow2DiscardRegion *d, *p, *next;
448
QTAILQ_FOREACH(d, &s->discards, next) {
449
uint64_t new_start = MIN(offset, d->offset);
450
uint64_t new_end = MAX(offset + length, d->offset + d->bytes);
452
if (new_end - new_start <= length + d->bytes) {
453
/* There can't be any overlap, areas ending up here have no
454
* references any more and therefore shouldn't get freed another
456
assert(d->bytes + length == new_end - new_start);
457
d->offset = new_start;
458
d->bytes = new_end - new_start;
463
d = g_malloc(sizeof(*d));
464
*d = (Qcow2DiscardRegion) {
469
QTAILQ_INSERT_TAIL(&s->discards, d, next);
472
/* Merge discard requests if they are adjacent now */
473
QTAILQ_FOREACH_SAFE(p, &s->discards, next, next) {
475
|| p->offset > d->offset + d->bytes
476
|| d->offset > p->offset + p->bytes)
481
/* Still no overlap possible */
482
assert(p->offset == d->offset + d->bytes
483
|| d->offset == p->offset + p->bytes);
485
QTAILQ_REMOVE(&s->discards, p, next);
486
d->offset = MIN(d->offset, p->offset);
487
d->bytes += p->bytes;
421
491
/* XXX: cache several refcount block clusters ? */
422
492
static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
423
int64_t offset, int64_t length, int addend)
493
int64_t offset, int64_t length, int addend, enum qcow2_discard_type type)
425
495
BDRVQcowState *s = bs->opaque;
426
496
int64_t start, last, cluster_offset;
486
556
s->free_cluster_index = cluster_index;
488
558
refcount_block[block_index] = cpu_to_be16(refcount);
560
if (refcount == 0 && s->discard_passthrough[type]) {
561
update_refcount_discard(bs, cluster_offset, s->cluster_size);
567
if (!s->cache_discards) {
568
qcow2_process_discards(bs, ret);
493
571
/* Write last changed block to disk */
494
572
if (refcount_block) {
523
602
static int update_cluster_refcount(BlockDriverState *bs,
524
603
int64_t cluster_index,
605
enum qcow2_discard_type type)
527
607
BDRVQcowState *s = bs->opaque;
530
ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
610
ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend,
649
731
if (free_in_cluster == 0)
650
732
s->free_byte_offset = 0;
651
733
if ((offset & (s->cluster_size - 1)) != 0)
652
update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
734
update_cluster_refcount(bs, offset >> s->cluster_bits, 1,
735
QCOW2_DISCARD_NEVER);
654
737
offset = qcow2_alloc_clusters(bs, s->cluster_size);
655
738
if (offset < 0) {
659
742
if ((cluster_offset + s->cluster_size) == offset) {
660
743
/* we are lucky: contiguous data */
661
744
offset = s->free_byte_offset;
662
update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
745
update_cluster_refcount(bs, offset >> s->cluster_bits, 1,
746
QCOW2_DISCARD_NEVER);
663
747
s->free_byte_offset += size;
665
749
s->free_byte_offset = offset;
678
762
void qcow2_free_clusters(BlockDriverState *bs,
679
int64_t offset, int64_t size)
763
int64_t offset, int64_t size,
764
enum qcow2_discard_type type)
683
768
BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
684
ret = update_refcount(bs, offset, size, -1);
769
ret = update_refcount(bs, offset, size, -1, type);
686
771
fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
687
772
/* TODO Remember the clusters to free them later and avoid leaking */
692
777
* Free a cluster using its L2 entry (handles clusters of all types, e.g.
693
778
* normal cluster, compressed cluster, etc.)
695
void qcow2_free_any_clusters(BlockDriverState *bs,
696
uint64_t l2_entry, int nb_clusters)
780
void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
781
int nb_clusters, enum qcow2_discard_type type)
698
783
BDRVQcowState *s = bs->opaque;
705
790
s->csize_mask) + 1;
706
791
qcow2_free_clusters(bs,
707
792
(l2_entry & s->cluster_offset_mask) & ~511,
793
nb_csectors * 512, type);
711
796
case QCOW2_CLUSTER_NORMAL:
712
797
qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK,
713
nb_clusters << s->cluster_bits);
798
nb_clusters << s->cluster_bits, type);
715
800
case QCOW2_CLUSTER_UNALLOCATED:
716
801
case QCOW2_CLUSTER_ZERO:
850
940
qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
943
s->cache_discards = false;
944
qcow2_process_discards(bs, ret);
853
946
/* Update L1 only if it isn't deleted anyway (addend = -1) */
854
947
if (ret == 0 && addend >= 0 && l1_modified) {
855
948
for (i = 0; i < l1_size; i++) {