~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to block/block-backend.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
    bool iostatus_enabled;
60
60
    BlockDeviceIoStatus iostatus;
61
61
 
 
62
    uint64_t perm;
 
63
    uint64_t shared_perm;
 
64
    bool disable_perm;
 
65
 
62
66
    bool allow_write_beyond_eof;
63
67
 
64
68
    NotifierList remove_bs_notifiers, insert_bs_notifiers;
 
69
 
 
70
    int quiesce_counter;
65
71
};
66
72
 
67
73
typedef struct BlockBackendAIOCB {
77
83
 
78
84
static void drive_info_del(DriveInfo *dinfo);
79
85
static BlockBackend *bdrv_first_blk(BlockDriverState *bs);
 
86
static char *blk_get_attached_dev_id(BlockBackend *blk);
80
87
 
81
88
/* All BlockBackends */
82
89
static QTAILQ_HEAD(, BlockBackend) block_backends =
99
106
static void blk_root_change_media(BdrvChild *child, bool load);
100
107
static void blk_root_resize(BdrvChild *child);
101
108
 
 
109
static char *blk_root_get_parent_desc(BdrvChild *child)
 
110
{
 
111
    BlockBackend *blk = child->opaque;
 
112
    char *dev_id;
 
113
 
 
114
    if (blk->name) {
 
115
        return g_strdup(blk->name);
 
116
    }
 
117
 
 
118
    dev_id = blk_get_attached_dev_id(blk);
 
119
    if (*dev_id) {
 
120
        return dev_id;
 
121
    } else {
 
122
        /* TODO Callback into the BB owner for something more detailed */
 
123
        g_free(dev_id);
 
124
        return g_strdup("a block device");
 
125
    }
 
126
}
 
127
 
102
128
static const char *blk_root_get_name(BdrvChild *child)
103
129
{
104
130
    return blk_name(child->opaque);
110
136
    .change_media       = blk_root_change_media,
111
137
    .resize             = blk_root_resize,
112
138
    .get_name           = blk_root_get_name,
 
139
    .get_parent_desc    = blk_root_get_parent_desc,
113
140
 
114
141
    .drained_begin      = blk_root_drained_begin,
115
142
    .drained_end        = blk_root_drained_end,
117
144
 
118
145
/*
119
146
 * Create a new BlockBackend with a reference count of one.
120
 
 * Store an error through @errp on failure, unless it's null.
 
147
 *
 
148
 * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
 
149
 * to request for a block driver node that is attached to this BlockBackend.
 
150
 * @shared_perm is a bitmask which describes which permissions may be granted
 
151
 * to other users of the attached node.
 
152
 * Both sets of permissions can be changed later using blk_set_perm().
 
153
 *
121
154
 * Return the new BlockBackend on success, null on failure.
122
155
 */
123
 
BlockBackend *blk_new(void)
 
156
BlockBackend *blk_new(uint64_t perm, uint64_t shared_perm)
124
157
{
125
158
    BlockBackend *blk;
126
159
 
127
160
    blk = g_new0(BlockBackend, 1);
128
161
    blk->refcnt = 1;
 
162
    blk->perm = perm;
 
163
    blk->shared_perm = shared_perm;
129
164
    blk_set_enable_write_cache(blk, true);
130
165
 
131
166
    qemu_co_queue_init(&blk->public.throttled_reqs[0]);
155
190
{
156
191
    BlockBackend *blk;
157
192
    BlockDriverState *bs;
158
 
 
159
 
    blk = blk_new();
 
193
    uint64_t perm;
 
194
 
 
195
    /* blk_new_open() is mainly used in .bdrv_create implementations and the
 
196
     * tools where sharing isn't a concern because the BDS stays private, so we
 
197
     * just request permission according to the flags.
 
198
     *
 
199
     * The exceptions are xen_disk and blockdev_init(); in these cases, the
 
200
     * caller of blk_new_open() doesn't make use of the permissions, but they
 
201
     * shouldn't hurt either. We can still share everything here because the
 
202
     * guest devices will add their own blockers if they can't share. */
 
203
    perm = BLK_PERM_CONSISTENT_READ;
 
204
    if (flags & BDRV_O_RDWR) {
 
205
        perm |= BLK_PERM_WRITE;
 
206
    }
 
207
    if (flags & BDRV_O_RESIZE) {
 
208
        perm |= BLK_PERM_RESIZE;
 
209
    }
 
210
 
 
211
    blk = blk_new(perm, BLK_PERM_ALL);
160
212
    bs = bdrv_open(filename, reference, options, flags, errp);
161
213
    if (!bs) {
162
214
        blk_unref(blk);
163
215
        return NULL;
164
216
    }
165
217
 
166
 
    blk->root = bdrv_root_attach_child(bs, "root", &child_root, blk);
 
218
    blk->root = bdrv_root_attach_child(bs, "root", &child_root,
 
219
                                       perm, BLK_PERM_ALL, blk, errp);
 
220
    if (!blk->root) {
 
221
        bdrv_unref(bs);
 
222
        blk_unref(blk);
 
223
        return NULL;
 
224
    }
167
225
 
168
226
    return blk;
169
227
}
173
231
    assert(!blk->refcnt);
174
232
    assert(!blk->name);
175
233
    assert(!blk->dev);
 
234
    if (blk->public.throttle_state) {
 
235
        blk_io_limits_disable(blk);
 
236
    }
176
237
    if (blk->root) {
177
238
        blk_remove_bs(blk);
178
239
    }
495
556
/*
496
557
 * Associates a new BlockDriverState with @blk.
497
558
 */
498
 
void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
 
559
int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
499
560
{
 
561
    blk->root = bdrv_root_attach_child(bs, "root", &child_root,
 
562
                                       blk->perm, blk->shared_perm, blk, errp);
 
563
    if (blk->root == NULL) {
 
564
        return -EPERM;
 
565
    }
500
566
    bdrv_ref(bs);
501
 
    blk->root = bdrv_root_attach_child(bs, "root", &child_root, blk);
502
567
 
503
568
    notifier_list_notify(&blk->insert_bs_notifiers, blk);
504
569
    if (blk->public.throttle_state) {
505
570
        throttle_timers_attach_aio_context(
506
571
            &blk->public.throttle_timers, bdrv_get_aio_context(bs));
507
572
    }
 
573
 
 
574
    return 0;
 
575
}
 
576
 
 
577
/*
 
578
 * Sets the permission bitmasks that the user of the BlockBackend needs.
 
579
 */
 
580
int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm,
 
581
                 Error **errp)
 
582
{
 
583
    int ret;
 
584
 
 
585
    if (blk->root && !blk->disable_perm) {
 
586
        ret = bdrv_child_try_set_perm(blk->root, perm, shared_perm, errp);
 
587
        if (ret < 0) {
 
588
            return ret;
 
589
        }
 
590
    }
 
591
 
 
592
    blk->perm = perm;
 
593
    blk->shared_perm = shared_perm;
 
594
 
 
595
    return 0;
 
596
}
 
597
 
 
598
void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)
 
599
{
 
600
    *perm = blk->perm;
 
601
    *shared_perm = blk->shared_perm;
 
602
}
 
603
 
 
604
/*
 
605
 * Notifies the user of all BlockBackends that migration has completed. qdev
 
606
 * devices can tighten their permissions in response (specifically revoke
 
607
 * shared write permissions that we needed for storage migration).
 
608
 *
 
609
 * If an error is returned, the VM cannot be allowed to be resumed.
 
610
 */
 
611
void blk_resume_after_migration(Error **errp)
 
612
{
 
613
    BlockBackend *blk;
 
614
    Error *local_err = NULL;
 
615
 
 
616
    for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
 
617
        if (!blk->disable_perm) {
 
618
            continue;
 
619
        }
 
620
 
 
621
        blk->disable_perm = false;
 
622
 
 
623
        blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
 
624
        if (local_err) {
 
625
            error_propagate(errp, local_err);
 
626
            blk->disable_perm = true;
 
627
            return;
 
628
        }
 
629
    }
508
630
}
509
631
 
510
632
static int blk_do_attach_dev(BlockBackend *blk, void *dev)
512
634
    if (blk->dev) {
513
635
        return -EBUSY;
514
636
    }
 
637
 
 
638
    /* While migration is still incoming, we don't need to apply the
 
639
     * permissions of guest device BlockBackends. We might still have a block
 
640
     * job or NBD server writing to the image for storage migration. */
 
641
    if (runstate_check(RUN_STATE_INMIGRATE)) {
 
642
        blk->disable_perm = true;
 
643
    }
 
644
 
515
645
    blk_ref(blk);
516
646
    blk->dev = dev;
517
647
    blk->legacy_dev = false;
518
648
    blk_iostatus_reset(blk);
 
649
 
519
650
    return 0;
520
651
}
521
652
 
553
684
    blk->dev_ops = NULL;
554
685
    blk->dev_opaque = NULL;
555
686
    blk->guest_block_size = 512;
 
687
    blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort);
556
688
    blk_unref(blk);
557
689
}
558
690
 
610
742
                     void *opaque)
611
743
{
612
744
    /* All drivers that use blk_set_dev_ops() are qdevified and we want to keep
613
 
     * it that way, so we can assume blk->dev is a DeviceState if blk->dev_ops
614
 
     * is set. */
 
745
     * it that way, so we can assume blk->dev, if present, is a DeviceState if
 
746
     * blk->dev_ops is set. Non-device users may use dev_ops without device. */
615
747
    assert(!blk->legacy_dev);
616
748
 
617
749
    blk->dev_ops = ops;
618
750
    blk->dev_opaque = opaque;
 
751
 
 
752
    /* Are we currently quiesced? Should we enforce this right now? */
 
753
    if (blk->quiesce_counter && ops->drained_begin) {
 
754
        ops->drained_begin(opaque);
 
755
    }
619
756
}
620
757
 
621
758
/*
622
759
 * Notify @blk's attached device model of media change.
623
 
 * If @load is true, notify of media load.
624
 
 * Else, notify of media eject.
 
760
 *
 
761
 * If @load is true, notify of media load. This action can fail, meaning that
 
762
 * the medium cannot be loaded. @errp is set then.
 
763
 *
 
764
 * If @load is false, notify of media eject. This can never fail.
 
765
 *
625
766
 * Also send DEVICE_TRAY_MOVED events as appropriate.
626
767
 */
627
 
void blk_dev_change_media_cb(BlockBackend *blk, bool load)
 
768
void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp)
628
769
{
629
770
    if (blk->dev_ops && blk->dev_ops->change_media_cb) {
630
771
        bool tray_was_open, tray_is_open;
 
772
        Error *local_err = NULL;
631
773
 
632
774
        assert(!blk->legacy_dev);
633
775
 
634
776
        tray_was_open = blk_dev_is_tray_open(blk);
635
 
        blk->dev_ops->change_media_cb(blk->dev_opaque, load);
 
777
        blk->dev_ops->change_media_cb(blk->dev_opaque, load, &local_err);
 
778
        if (local_err) {
 
779
            assert(load == true);
 
780
            error_propagate(errp, local_err);
 
781
            return;
 
782
        }
636
783
        tray_is_open = blk_dev_is_tray_open(blk);
637
784
 
638
785
        if (tray_was_open != tray_is_open) {
646
793
 
647
794
static void blk_root_change_media(BdrvChild *child, bool load)
648
795
{
649
 
    blk_dev_change_media_cb(child->opaque, load);
 
796
    blk_dev_change_media_cb(child->opaque, load, NULL);
650
797
}
651
798
 
652
799
/*
880
1027
{
881
1028
    QEMUIOVector qiov;
882
1029
    struct iovec iov;
883
 
    Coroutine *co;
884
1030
    BlkRwCo rwco;
885
1031
 
886
1032
    iov = (struct iovec) {
897
1043
        .ret    = NOT_DONE,
898
1044
    };
899
1045
 
900
 
    co = qemu_coroutine_create(co_entry, &rwco);
901
 
    qemu_coroutine_enter(co);
902
 
    BDRV_POLL_WHILE(blk_bs(blk), rwco.ret == NOT_DONE);
 
1046
    if (qemu_in_coroutine()) {
 
1047
        /* Fast-path if already in coroutine context */
 
1048
        co_entry(&rwco);
 
1049
    } else {
 
1050
        Coroutine *co = qemu_coroutine_create(co_entry, &rwco);
 
1051
        bdrv_coroutine_enter(blk_bs(blk), co);
 
1052
        BDRV_POLL_WHILE(blk_bs(blk), rwco.ret == NOT_DONE);
 
1053
    }
903
1054
 
904
1055
    return rwco.ret;
905
1056
}
979
1130
static void blk_aio_complete_bh(void *opaque)
980
1131
{
981
1132
    BlkAioEmAIOCB *acb = opaque;
982
 
 
983
1133
    assert(acb->has_returned);
984
1134
    blk_aio_complete(acb);
985
1135
}
1005
1155
    acb->has_returned = false;
1006
1156
 
1007
1157
    co = qemu_coroutine_create(co_entry, acb);
1008
 
    qemu_coroutine_enter(co);
 
1158
    bdrv_coroutine_enter(blk_bs(blk), co);
1009
1159
 
1010
1160
    acb->has_returned = true;
1011
1161
    if (acb->rwco.ret != NOT_DONE) {
1602
1752
        return -ENOMEDIUM;
1603
1753
    }
1604
1754
 
1605
 
    return bdrv_truncate(blk_bs(blk), offset);
 
1755
    return bdrv_truncate(blk->root, offset);
1606
1756
}
1607
1757
 
1608
1758
static void blk_pdiscard_entry(void *opaque)
1768
1918
{
1769
1919
    BlockBackend *blk = child->opaque;
1770
1920
 
 
1921
    if (++blk->quiesce_counter == 1) {
 
1922
        if (blk->dev_ops && blk->dev_ops->drained_begin) {
 
1923
            blk->dev_ops->drained_begin(blk->dev_opaque);
 
1924
        }
 
1925
    }
 
1926
 
1771
1927
    /* Note that blk->root may not be accessible here yet if we are just
1772
1928
     * attaching to a BlockDriverState that is drained. Use child instead. */
1773
1929
 
1779
1935
static void blk_root_drained_end(BdrvChild *child)
1780
1936
{
1781
1937
    BlockBackend *blk = child->opaque;
 
1938
    assert(blk->quiesce_counter);
1782
1939
 
1783
1940
    assert(blk->public.io_limits_disabled);
1784
1941
    --blk->public.io_limits_disabled;
 
1942
 
 
1943
    if (--blk->quiesce_counter == 0) {
 
1944
        if (blk->dev_ops && blk->dev_ops->drained_end) {
 
1945
            blk->dev_ops->drained_end(blk->dev_opaque);
 
1946
        }
 
1947
    }
1785
1948
}