~fboudra/qemu-linaro/new-upstream-release-1.2.0-2012.09-0ubuntu1

« back to all changes in this revision

Viewing changes to blockdev.c

  • Committer: Fathi Boudra
  • Author(s): Fathi Boudra
  • Date: 2012-08-21 06:47:11 UTC
  • mfrom: (0.1.16)
  • Revision ID: fathi.boudra@linaro.org-20120821064711-7yxmubp2v8a44xce
Tags: 1.1.50-2012.08-0ubuntu1
* New upstream release.
  - support emulated systems with more than 2G of memory. (LP: #1030588)
* Drop powerpc-missing-include.patch - merged upstream.
* Update debian/control: 
  - drop perl build dependency.
  - add libfdt-dev build dependency.
* Update debian/qemu-keymaps.install file.
* Update debian/rules:
  - update QEMU_CPU for ARM architecture: armv4l -> armv7l.
  - update conf_audio_drv: default to PulseAudio since PA is the default on
    Ubuntu.
  - enable KVM on ARM architecture.
  - enable flat device tree support (--enable-fdt). (LP: #1030594)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 * later.  See the COPYING file in the top-level directory.
8
8
 */
9
9
 
10
 
#include "block.h"
11
10
#include "blockdev.h"
 
11
#include "hw/block-common.h"
12
12
#include "monitor.h"
13
13
#include "qerror.h"
14
14
#include "qemu-option.h"
64
64
{
65
65
    DriveInfo *dinfo = drive_get_by_blockdev(bs);
66
66
 
 
67
    if (bs->job) {
 
68
        block_job_cancel(bs->job);
 
69
    }
67
70
    if (dinfo) {
68
71
        dinfo->auto_del = 1;
69
72
    }
275
278
{
276
279
    const char *buf;
277
280
    const char *file = NULL;
278
 
    char devname[128];
279
281
    const char *serial;
280
282
    const char *mediastr = "";
281
283
    BlockInterfaceType type;
315
317
    serial = qemu_opt_get(opts, "serial");
316
318
 
317
319
    if ((buf = qemu_opt_get(opts, "if")) != NULL) {
318
 
        pstrcpy(devname, sizeof(devname), buf);
319
320
        for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
320
321
            ;
321
322
        if (type == IF_COUNT) {
324
325
        }
325
326
    } else {
326
327
        type = default_to_scsi ? IF_SCSI : IF_IDE;
327
 
        pstrcpy(devname, sizeof(devname), if_name[type]);
328
328
    }
329
329
 
330
330
    max_devs = if_max_devs[type];
331
331
 
332
332
    if (cyls || heads || secs) {
333
 
        if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
 
333
        if (cyls < 1) {
334
334
            error_report("invalid physical cyls number");
335
335
            return NULL;
336
336
        }
337
 
        if (heads < 1 || (type == IF_IDE && heads > 16)) {
 
337
        if (heads < 1) {
338
338
            error_report("invalid physical heads number");
339
339
            return NULL;
340
340
        }
341
 
        if (secs < 1 || (type == IF_IDE && secs > 63)) {
 
341
        if (secs < 1) {
342
342
            error_report("invalid physical secs number");
343
343
            return NULL;
344
344
        }
398
398
#endif
399
399
 
400
400
    if ((buf = qemu_opt_get(opts, "format")) != NULL) {
401
 
       if (strcmp(buf, "?") == 0) {
402
 
           error_printf("Supported formats:");
403
 
           bdrv_iterate_format(bdrv_format_print, NULL);
404
 
           error_printf("\n");
405
 
           return NULL;
 
401
        if (is_help_option(buf)) {
 
402
            error_printf("Supported formats:");
 
403
            bdrv_iterate_format(bdrv_format_print, NULL);
 
404
            error_printf("\n");
 
405
            return NULL;
406
406
        }
407
407
        drv = bdrv_find_whitelisted_format(buf);
408
408
        if (!drv) {
520
520
            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
521
521
        if (max_devs)
522
522
            snprintf(dinfo->id, 32, "%s%i%s%i",
523
 
                     devname, bus_id, mediastr, unit_id);
 
523
                     if_name[type], bus_id, mediastr, unit_id);
524
524
        else
525
525
            snprintf(dinfo->id, 32, "%s%s%i",
526
 
                     devname, mediastr, unit_id);
 
526
                     if_name[type], mediastr, unit_id);
527
527
    }
528
528
    dinfo->bdrv = bdrv_new(dinfo->id);
529
529
    dinfo->devaddr = devaddr;
530
530
    dinfo->type = type;
531
531
    dinfo->bus = bus_id;
532
532
    dinfo->unit = unit_id;
 
533
    dinfo->cyls = cyls;
 
534
    dinfo->heads = heads;
 
535
    dinfo->secs = secs;
 
536
    dinfo->trans = translation;
533
537
    dinfo->opts = opts;
534
538
    dinfo->refcount = 1;
535
 
    if (serial)
536
 
        strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
 
539
    dinfo->serial = serial;
537
540
    QTAILQ_INSERT_TAIL(&drives, dinfo, next);
538
541
 
539
542
    bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
546
549
    case IF_SCSI:
547
550
    case IF_XEN:
548
551
    case IF_NONE:
549
 
        switch(media) {
550
 
        case MEDIA_DISK:
551
 
            if (cyls != 0) {
552
 
                bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
553
 
                bdrv_set_translation_hint(dinfo->bdrv, translation);
554
 
            }
555
 
            break;
556
 
        case MEDIA_CDROM:
557
 
            dinfo->media_cd = 1;
558
 
            break;
559
 
        }
 
552
        dinfo->media_cd = media == MEDIA_CDROM;
560
553
        break;
561
554
    case IF_SD:
562
555
    case IF_FLOPPY:
565
558
        break;
566
559
    case IF_VIRTIO:
567
560
        /* add virtio block device */
568
 
        opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
 
561
        opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
569
562
        if (arch_type == QEMU_ARCH_S390X) {
570
563
            qemu_opt_set(opts, "driver", "virtio-blk-s390");
571
564
        } else {
591
584
        bdrv_flags |= BDRV_O_COPY_ON_READ;
592
585
    }
593
586
 
 
587
    if (runstate_check(RUN_STATE_INMIGRATE)) {
 
588
        bdrv_flags |= BDRV_O_INCOMING;
 
589
    }
 
590
 
594
591
    if (media == MEDIA_CDROM) {
595
592
        /* CDROM is fine for any interface, don't check.  */
596
593
        ro = 1;
604
601
 
605
602
    bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
606
603
 
 
604
    if (ro && copy_on_read) {
 
605
        error_report("warning: disabling copy_on_read on readonly drive");
 
606
    }
 
607
 
607
608
    ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
608
609
    if (ret < 0) {
609
610
        error_report("could not open disk image %s: %s",
627
628
{
628
629
    const char *device = qdict_get_str(qdict, "device");
629
630
    BlockDriverState *bs;
 
631
    int ret;
630
632
 
631
633
    if (!strcmp(device, "all")) {
632
 
        bdrv_commit_all();
 
634
        ret = bdrv_commit_all();
 
635
        if (ret == -EBUSY) {
 
636
            qerror_report(QERR_DEVICE_IN_USE, device);
 
637
            return;
 
638
        }
633
639
    } else {
634
 
        int ret;
635
 
 
636
640
        bs = bdrv_find(device);
637
641
        if (!bs) {
638
642
            qerror_report(QERR_DEVICE_NOT_FOUND, device);
646
650
    }
647
651
}
648
652
 
 
653
static void blockdev_do_action(int kind, void *data, Error **errp)
 
654
{
 
655
    BlockdevAction action;
 
656
    BlockdevActionList list;
 
657
 
 
658
    action.kind = kind;
 
659
    action.data = data;
 
660
    list.value = &action;
 
661
    list.next = NULL;
 
662
    qmp_transaction(&list, errp);
 
663
}
 
664
 
649
665
void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
650
666
                                bool has_format, const char *format,
 
667
                                bool has_mode, enum NewImageMode mode,
651
668
                                Error **errp)
652
669
{
653
 
    BlockDriverState *bs;
654
 
    BlockDriver *drv, *old_drv, *proto_drv;
655
 
    int ret = 0;
656
 
    int flags;
657
 
    char old_filename[1024];
658
 
 
659
 
    bs = bdrv_find(device);
660
 
    if (!bs) {
661
 
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
662
 
        return;
663
 
    }
664
 
    if (bdrv_in_use(bs)) {
665
 
        error_set(errp, QERR_DEVICE_IN_USE, device);
666
 
        return;
667
 
    }
668
 
 
669
 
    pstrcpy(old_filename, sizeof(old_filename), bs->filename);
670
 
 
671
 
    old_drv = bs->drv;
672
 
    flags = bs->open_flags;
673
 
 
674
 
    if (!has_format) {
675
 
        format = "qcow2";
676
 
    }
677
 
 
678
 
    drv = bdrv_find_format(format);
679
 
    if (!drv) {
680
 
        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
681
 
        return;
682
 
    }
683
 
 
684
 
    proto_drv = bdrv_find_protocol(snapshot_file);
685
 
    if (!proto_drv) {
686
 
        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
687
 
        return;
688
 
    }
689
 
 
690
 
    ret = bdrv_img_create(snapshot_file, format, bs->filename,
691
 
                          bs->drv->format_name, NULL, -1, flags);
692
 
    if (ret) {
693
 
        error_set(errp, QERR_UNDEFINED_ERROR);
694
 
        return;
695
 
    }
696
 
 
697
 
    bdrv_drain_all();
698
 
    bdrv_flush(bs);
699
 
 
700
 
    bdrv_close(bs);
701
 
    ret = bdrv_open(bs, snapshot_file, flags, drv);
702
 
    /*
703
 
     * If reopening the image file we just created fails, fall back
704
 
     * and try to re-open the original image. If that fails too, we
705
 
     * are in serious trouble.
706
 
     */
707
 
    if (ret != 0) {
708
 
        ret = bdrv_open(bs, old_filename, flags, old_drv);
709
 
        if (ret != 0) {
710
 
            error_set(errp, QERR_OPEN_FILE_FAILED, old_filename);
711
 
        } else {
712
 
            error_set(errp, QERR_OPEN_FILE_FAILED, snapshot_file);
713
 
        }
714
 
    }
 
670
    BlockdevSnapshot snapshot = {
 
671
        .device = (char *) device,
 
672
        .snapshot_file = (char *) snapshot_file,
 
673
        .has_format = has_format,
 
674
        .format = (char *) format,
 
675
        .has_mode = has_mode,
 
676
        .mode = mode,
 
677
    };
 
678
    blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
 
679
                       errp);
715
680
}
716
681
 
717
682
 
718
683
/* New and old BlockDriverState structs for group snapshots */
719
 
typedef struct BlkGroupSnapshotStates {
 
684
typedef struct BlkTransactionStates {
720
685
    BlockDriverState *old_bs;
721
686
    BlockDriverState *new_bs;
722
 
    QSIMPLEQ_ENTRY(BlkGroupSnapshotStates) entry;
723
 
} BlkGroupSnapshotStates;
 
687
    QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
 
688
} BlkTransactionStates;
724
689
 
725
690
/*
726
691
 * 'Atomic' group snapshots.  The snapshots are taken as a set, and if any fail
727
692
 *  then we do not pivot any of the devices in the group, and abandon the
728
693
 *  snapshots
729
694
 */
730
 
void qmp_blockdev_group_snapshot_sync(SnapshotDevList *dev_list,
731
 
                                      Error **errp)
 
695
void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
732
696
{
733
697
    int ret = 0;
734
 
    SnapshotDevList *dev_entry = dev_list;
735
 
    SnapshotDev *dev_info = NULL;
736
 
    BlkGroupSnapshotStates *states;
737
 
    BlockDriver *proto_drv;
738
 
    BlockDriver *drv;
739
 
    int flags;
740
 
    const char *format;
741
 
    const char *snapshot_file;
 
698
    BlockdevActionList *dev_entry = dev_list;
 
699
    BlkTransactionStates *states, *next;
742
700
 
743
 
    QSIMPLEQ_HEAD(snap_bdrv_states, BlkGroupSnapshotStates) snap_bdrv_states;
 
701
    QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
744
702
    QSIMPLEQ_INIT(&snap_bdrv_states);
745
703
 
746
704
    /* drain all i/o before any snapshots */
748
706
 
749
707
    /* We don't do anything in this loop that commits us to the snapshot */
750
708
    while (NULL != dev_entry) {
 
709
        BlockdevAction *dev_info = NULL;
 
710
        BlockDriver *proto_drv;
 
711
        BlockDriver *drv;
 
712
        int flags;
 
713
        enum NewImageMode mode;
 
714
        const char *new_image_file;
 
715
        const char *device;
 
716
        const char *format = "qcow2";
 
717
 
751
718
        dev_info = dev_entry->value;
752
719
        dev_entry = dev_entry->next;
753
720
 
754
 
        states = g_malloc0(sizeof(BlkGroupSnapshotStates));
 
721
        states = g_malloc0(sizeof(BlkTransactionStates));
755
722
        QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
756
723
 
757
 
        states->old_bs = bdrv_find(dev_info->device);
758
 
 
 
724
        switch (dev_info->kind) {
 
725
        case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
 
726
            device = dev_info->blockdev_snapshot_sync->device;
 
727
            if (!dev_info->blockdev_snapshot_sync->has_mode) {
 
728
                dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
 
729
            }
 
730
            new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
 
731
            if (dev_info->blockdev_snapshot_sync->has_format) {
 
732
                format = dev_info->blockdev_snapshot_sync->format;
 
733
            }
 
734
            mode = dev_info->blockdev_snapshot_sync->mode;
 
735
            break;
 
736
        default:
 
737
            abort();
 
738
        }
 
739
 
 
740
        drv = bdrv_find_format(format);
 
741
        if (!drv) {
 
742
            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
 
743
            goto delete_and_fail;
 
744
        }
 
745
 
 
746
        states->old_bs = bdrv_find(device);
759
747
        if (!states->old_bs) {
760
 
            error_set(errp, QERR_DEVICE_NOT_FOUND, dev_info->device);
 
748
            error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 
749
            goto delete_and_fail;
 
750
        }
 
751
 
 
752
        if (!bdrv_is_inserted(states->old_bs)) {
 
753
            error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
761
754
            goto delete_and_fail;
762
755
        }
763
756
 
764
757
        if (bdrv_in_use(states->old_bs)) {
765
 
            error_set(errp, QERR_DEVICE_IN_USE, dev_info->device);
 
758
            error_set(errp, QERR_DEVICE_IN_USE, device);
766
759
            goto delete_and_fail;
767
760
        }
768
761
 
769
 
        if (!bdrv_is_read_only(states->old_bs) &&
770
 
             bdrv_is_inserted(states->old_bs)) {
771
 
 
 
762
        if (!bdrv_is_read_only(states->old_bs)) {
772
763
            if (bdrv_flush(states->old_bs)) {
773
764
                error_set(errp, QERR_IO_ERROR);
774
765
                goto delete_and_fail;
775
766
            }
776
767
        }
777
768
 
778
 
        snapshot_file = dev_info->snapshot_file;
779
 
 
780
769
        flags = states->old_bs->open_flags;
781
770
 
782
 
        if (!dev_info->has_format) {
783
 
            format = "qcow2";
784
 
        } else {
785
 
            format = dev_info->format;
786
 
        }
787
 
 
788
 
        drv = bdrv_find_format(format);
789
 
        if (!drv) {
790
 
            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
791
 
            goto delete_and_fail;
792
 
        }
793
 
 
794
 
        proto_drv = bdrv_find_protocol(snapshot_file);
 
771
        proto_drv = bdrv_find_protocol(new_image_file);
795
772
        if (!proto_drv) {
796
773
            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
797
774
            goto delete_and_fail;
798
775
        }
799
776
 
800
777
        /* create new image w/backing file */
801
 
        ret = bdrv_img_create(snapshot_file, format,
802
 
                              states->old_bs->filename,
803
 
                              drv->format_name, NULL, -1, flags);
804
 
        if (ret) {
805
 
            error_set(errp, QERR_OPEN_FILE_FAILED, snapshot_file);
806
 
            goto delete_and_fail;
 
778
        if (mode != NEW_IMAGE_MODE_EXISTING) {
 
779
            ret = bdrv_img_create(new_image_file, format,
 
780
                                  states->old_bs->filename,
 
781
                                  states->old_bs->drv->format_name,
 
782
                                  NULL, -1, flags);
 
783
            if (ret) {
 
784
                error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
 
785
                goto delete_and_fail;
 
786
            }
807
787
        }
808
788
 
809
789
        /* We will manually add the backing_hd field to the bs later */
810
790
        states->new_bs = bdrv_new("");
811
 
        ret = bdrv_open(states->new_bs, snapshot_file,
 
791
        ret = bdrv_open(states->new_bs, new_image_file,
812
792
                        flags | BDRV_O_NO_BACKING, drv);
813
793
        if (ret != 0) {
814
 
            error_set(errp, QERR_OPEN_FILE_FAILED, snapshot_file);
 
794
            error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
815
795
            goto delete_and_fail;
816
796
        }
817
797
    }
838
818
        }
839
819
    }
840
820
exit:
841
 
    QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
 
821
    QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
842
822
        g_free(states);
843
823
    }
844
824
    return;
1107
1087
}
1108
1088
 
1109
1089
void qmp_block_stream(const char *device, bool has_base,
1110
 
                      const char *base, Error **errp)
 
1090
                      const char *base, bool has_speed,
 
1091
                      int64_t speed, Error **errp)
1111
1092
{
1112
1093
    BlockDriverState *bs;
1113
1094
    BlockDriverState *base_bs = NULL;
1114
 
    int ret;
 
1095
    Error *local_err = NULL;
1115
1096
 
1116
1097
    bs = bdrv_find(device);
1117
1098
    if (!bs) {
1127
1108
        }
1128
1109
    }
1129
1110
 
1130
 
    ret = stream_start(bs, base_bs, base, block_stream_cb, bs);
1131
 
    if (ret < 0) {
1132
 
        switch (ret) {
1133
 
        case -EBUSY:
1134
 
            error_set(errp, QERR_DEVICE_IN_USE, device);
1135
 
            return;
1136
 
        default:
1137
 
            error_set(errp, QERR_NOT_SUPPORTED);
1138
 
            return;
1139
 
        }
 
1111
    stream_start(bs, base_bs, base, has_speed ? speed : 0,
 
1112
                 block_stream_cb, bs, &local_err);
 
1113
    if (error_is_set(&local_err)) {
 
1114
        error_propagate(errp, local_err);
 
1115
        return;
1140
1116
    }
1141
1117
 
1142
1118
    /* Grab a reference so hotplug does not delete the BlockDriverState from
1158
1134
    return bs->job;
1159
1135
}
1160
1136
 
1161
 
void qmp_block_job_set_speed(const char *device, int64_t value, Error **errp)
 
1137
void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
1162
1138
{
1163
1139
    BlockJob *job = find_block_job(device);
1164
1140
 
1167
1143
        return;
1168
1144
    }
1169
1145
 
1170
 
    if (block_job_set_speed(job, value) < 0) {
1171
 
        error_set(errp, QERR_NOT_SUPPORTED);
1172
 
    }
 
1146
    block_job_set_speed(job, speed, errp);
1173
1147
}
1174
1148
 
1175
1149
void qmp_block_job_cancel(const char *device, Error **errp)