~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to block/qcow2-refcount.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2013-10-22 22:47:07 UTC
  • mfrom: (1.8.3) (10.1.42 sid)
  • Revision ID: package-import@ubuntu.com-20131022224707-1lya34fw3k3f24tv
Tags: 1.6.0+dfsg-2ubuntu1
* Merge 1.6.0~rc0+dfsg-2exp from debian experimental.  Remaining changes:
  - debian/control
    * update maintainer
    * remove libiscsi, usb-redir, vde, vnc-jpeg, and libssh2-1-dev
      from build-deps
    * enable rbd
    * add qemu-system and qemu-common B/R to qemu-keymaps
    * add D:udev, R:qemu, R:qemu-common and B:qemu-common to
      qemu-system-common
    * qemu-system-arm, qemu-system-ppc, qemu-system-sparc:
      - add qemu-kvm to Provides
      - add qemu-common, qemu-kvm, kvm to B/R
      - remove openbios-sparc from qemu-system-sparc D
      - drop openbios-ppc and openhackware Depends to Suggests (for now)
    * qemu-system-x86:
      - add qemu-common to Breaks/Replaces.
      - add cpu-checker to Recommends.
    * qemu-user: add B/R:qemu-kvm
    * qemu-kvm:
      - add armhf armel powerpc sparc to Architecture
      - C/R/P: qemu-kvm-spice
    * add qemu-common package
    * drop qemu-slof which is not packaged in ubuntu
  - add qemu-system-common.links for tap ifup/down scripts and OVMF link.
  - qemu-system-x86.links:
    * remove pxe rom links which are in kvm-ipxe
    * add symlink for kvm.1 manpage
  - debian/rules
    * add kvm-spice symlink to qemu-kvm
    * call dh_installmodules for qemu-system-x86
    * update dh_installinit to install upstart script
    * run dh_installman (Closes: #709241) (cherrypicked from 1.5.0+dfsg-2)
  - Add qemu-utils.links for kvm-* symlinks.
  - Add qemu-system-x86.qemu-kvm.upstart and .default
  - Add qemu-system-x86.modprobe to set nesting=1
  - Add qemu-system-common.preinst to add kvm group
  - qemu-system-common.postinst: remove bad group acl if there, then have
    udev relabel /dev/kvm.
  - New linaro patches from qemu-linaro rebasing branch
  - Dropped patches:
    * xen-simplify-xen_enabled.patch
    * sparc-linux-user-fix-missing-symbols-in-.rel-.rela.plt-sections.patch
    * main_loop-do-not-set-nonblocking-if-xen_enabled.patch
    * xen_machine_pv-do-not-create-a-dummy-CPU-in-machine-.patch
    * virtio-rng-fix-crash
  - Kept patches:
    * expose_vms_qemu64cpu.patch - updated
    * linaro arm patches from qemu-linaro rebasing branch
  - New patches:
    * fix-pci-add: change CONFIG variable in ifdef to make sure that
      pci_add is defined.
* Add linaro patches
* Add experimental mach-virt patches for arm virtualization.
* qemu-system-common.install: add debian/tmp/usr/lib to install the
  qemu-bridge-helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
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);
 
32
                            int addend, enum qcow2_discard_type type);
33
33
 
34
34
 
35
35
/*********************************************************/
235
235
    } else {
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);
239
240
        if (ret < 0) {
240
241
            goto fail_block;
241
242
        }
399
400
 
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;
404
406
 
405
407
    ret = load_refcount_block(bs, new_block, (void**) refcount_block);
418
420
    return ret;
419
421
}
420
422
 
 
423
void qcow2_process_discards(BlockDriverState *bs, int ret)
 
424
{
 
425
    BDRVQcowState *s = bs->opaque;
 
426
    Qcow2DiscardRegion *d, *next;
 
427
 
 
428
    QTAILQ_FOREACH_SAFE(d, &s->discards, next, next) {
 
429
        QTAILQ_REMOVE(&s->discards, d, next);
 
430
 
 
431
        /* Discard is optional, ignore the return value */
 
432
        if (ret >= 0) {
 
433
            bdrv_discard(bs->file,
 
434
                         d->offset >> BDRV_SECTOR_BITS,
 
435
                         d->bytes >> BDRV_SECTOR_BITS);
 
436
        }
 
437
 
 
438
        g_free(d);
 
439
    }
 
440
}
 
441
 
 
442
static void update_refcount_discard(BlockDriverState *bs,
 
443
                                    uint64_t offset, uint64_t length)
 
444
{
 
445
    BDRVQcowState *s = bs->opaque;
 
446
    Qcow2DiscardRegion *d, *p, *next;
 
447
 
 
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);
 
451
 
 
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
 
455
             * time. */
 
456
            assert(d->bytes + length == new_end - new_start);
 
457
            d->offset = new_start;
 
458
            d->bytes = new_end - new_start;
 
459
            goto found;
 
460
        }
 
461
    }
 
462
 
 
463
    d = g_malloc(sizeof(*d));
 
464
    *d = (Qcow2DiscardRegion) {
 
465
        .bs     = bs,
 
466
        .offset = offset,
 
467
        .bytes  = length,
 
468
    };
 
469
    QTAILQ_INSERT_TAIL(&s->discards, d, next);
 
470
 
 
471
found:
 
472
    /* Merge discard requests if they are adjacent now */
 
473
    QTAILQ_FOREACH_SAFE(p, &s->discards, next, next) {
 
474
        if (p == d
 
475
            || p->offset > d->offset + d->bytes
 
476
            || d->offset > p->offset + p->bytes)
 
477
        {
 
478
            continue;
 
479
        }
 
480
 
 
481
        /* Still no overlap possible */
 
482
        assert(p->offset == d->offset + d->bytes
 
483
            || d->offset == p->offset + p->bytes);
 
484
 
 
485
        QTAILQ_REMOVE(&s->discards, p, next);
 
486
        d->offset = MIN(d->offset, p->offset);
 
487
        d->bytes += p->bytes;
 
488
    }
 
489
}
 
490
 
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)
424
494
{
425
495
    BDRVQcowState *s = bs->opaque;
426
496
    int64_t start, last, cluster_offset;
486
556
            s->free_cluster_index = cluster_index;
487
557
        }
488
558
        refcount_block[block_index] = cpu_to_be16(refcount);
 
559
 
 
560
        if (refcount == 0 && s->discard_passthrough[type]) {
 
561
            update_refcount_discard(bs, cluster_offset, s->cluster_size);
 
562
        }
489
563
    }
490
564
 
491
565
    ret = 0;
492
566
fail:
 
567
    if (!s->cache_discards) {
 
568
        qcow2_process_discards(bs, ret);
 
569
    }
 
570
 
493
571
    /* Write last changed block to disk */
494
572
    if (refcount_block) {
495
573
        int wret;
506
584
     */
507
585
    if (ret < 0) {
508
586
        int dummy;
509
 
        dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
 
587
        dummy = update_refcount(bs, offset, cluster_offset - offset, -addend,
 
588
                                QCOW2_DISCARD_NEVER);
510
589
        (void)dummy;
511
590
    }
512
591
 
522
601
 */
523
602
static int update_cluster_refcount(BlockDriverState *bs,
524
603
                                   int64_t cluster_index,
525
 
                                   int addend)
 
604
                                   int addend,
 
605
                                   enum qcow2_discard_type type)
526
606
{
527
607
    BDRVQcowState *s = bs->opaque;
528
608
    int ret;
529
609
 
530
 
    ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
 
610
    ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend,
 
611
                          type);
531
612
    if (ret < 0) {
532
613
        return ret;
533
614
    }
579
660
        return offset;
580
661
    }
581
662
 
582
 
    ret = update_refcount(bs, offset, size, 1);
 
663
    ret = update_refcount(bs, offset, size, 1, QCOW2_DISCARD_NEVER);
583
664
    if (ret < 0) {
584
665
        return ret;
585
666
    }
611
692
    old_free_cluster_index = s->free_cluster_index;
612
693
    s->free_cluster_index = cluster_index + i;
613
694
 
614
 
    ret = update_refcount(bs, offset, i << s->cluster_bits, 1);
 
695
    ret = update_refcount(bs, offset, i << s->cluster_bits, 1,
 
696
                          QCOW2_DISCARD_NEVER);
615
697
    if (ret < 0) {
616
698
        return ret;
617
699
    }
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);
653
736
    } else {
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;
664
748
        } else {
665
749
            s->free_byte_offset = offset;
676
760
}
677
761
 
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)
680
765
{
681
766
    int ret;
682
767
 
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);
685
770
    if (ret < 0) {
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.)
694
779
 */
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)
697
782
{
698
783
    BDRVQcowState *s = bs->opaque;
699
784
 
705
790
                           s->csize_mask) + 1;
706
791
            qcow2_free_clusters(bs,
707
792
                (l2_entry & s->cluster_offset_mask) & ~511,
708
 
                nb_csectors * 512);
 
793
                nb_csectors * 512, type);
709
794
        }
710
795
        break;
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);
714
799
        break;
715
800
    case QCOW2_CLUSTER_UNALLOCATED:
716
801
    case QCOW2_CLUSTER_ZERO:
741
826
    l1_table = NULL;
742
827
    l1_size2 = l1_size * sizeof(uint64_t);
743
828
 
 
829
    s->cache_discards = true;
 
830
 
744
831
    /* WARNING: qcow2_snapshot_goto relies on this function not using the
745
832
     * l1_table_offset when it is the current s->l1_table_offset! Be careful
746
833
     * when changing this! */
785
872
                            int ret;
786
873
                            ret = update_refcount(bs,
787
874
                                (offset & s->cluster_offset_mask) & ~511,
788
 
                                nb_csectors * 512, addend);
 
875
                                nb_csectors * 512, addend,
 
876
                                QCOW2_DISCARD_SNAPSHOT);
789
877
                            if (ret < 0) {
790
878
                                goto fail;
791
879
                            }
795
883
                    } else {
796
884
                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
797
885
                        if (addend != 0) {
798
 
                            refcount = update_cluster_refcount(bs, cluster_index, addend);
 
886
                            refcount = update_cluster_refcount(bs, cluster_index, addend,
 
887
                                                               QCOW2_DISCARD_SNAPSHOT);
799
888
                        } else {
800
889
                            refcount = get_refcount(bs, cluster_index);
801
890
                        }
827
916
 
828
917
 
829
918
            if (addend != 0) {
830
 
                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
 
919
                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend,
 
920
                                                   QCOW2_DISCARD_SNAPSHOT);
831
921
            } else {
832
922
                refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
833
923
            }
850
940
        qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
851
941
    }
852
942
 
 
943
    s->cache_discards = false;
 
944
    qcow2_process_discards(bs, ret);
 
945
 
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++) {
1253
1346
 
1254
1347
            if (num_fixed) {
1255
1348
                ret = update_refcount(bs, i << s->cluster_bits, 1,
1256
 
                                      refcount2 - refcount1);
 
1349
                                      refcount2 - refcount1,
 
1350
                                      QCOW2_DISCARD_ALWAYS);
1257
1351
                if (ret >= 0) {
1258
1352
                    (*num_fixed)++;
1259
1353
                    continue;