99
106
static void blk_root_change_media(BdrvChild *child, bool load);
100
107
static void blk_root_resize(BdrvChild *child);
109
static char *blk_root_get_parent_desc(BdrvChild *child)
111
BlockBackend *blk = child->opaque;
115
return g_strdup(blk->name);
118
dev_id = blk_get_attached_dev_id(blk);
122
/* TODO Callback into the BB owner for something more detailed */
124
return g_strdup("a block device");
102
128
static const char *blk_root_get_name(BdrvChild *child)
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,
114
141
.drained_begin = blk_root_drained_begin,
115
142
.drained_end = blk_root_drained_end,
119
146
* Create a new BlockBackend with a reference count of one.
120
* Store an error through @errp on failure, unless it's null.
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().
121
154
* Return the new BlockBackend on success, null on failure.
123
BlockBackend *blk_new(void)
156
BlockBackend *blk_new(uint64_t perm, uint64_t shared_perm)
125
158
BlockBackend *blk;
127
160
blk = g_new0(BlockBackend, 1);
163
blk->shared_perm = shared_perm;
129
164
blk_set_enable_write_cache(blk, true);
131
166
qemu_co_queue_init(&blk->public.throttled_reqs[0]);
156
191
BlockBackend *blk;
157
192
BlockDriverState *bs;
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.
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;
207
if (flags & BDRV_O_RESIZE) {
208
perm |= BLK_PERM_RESIZE;
211
blk = blk_new(perm, BLK_PERM_ALL);
160
212
bs = bdrv_open(filename, reference, options, flags, errp);
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);
496
557
* Associates a new BlockDriverState with @blk.
498
void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
559
int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
561
blk->root = bdrv_root_attach_child(bs, "root", &child_root,
562
blk->perm, blk->shared_perm, blk, errp);
563
if (blk->root == NULL) {
501
blk->root = bdrv_root_attach_child(bs, "root", &child_root, blk);
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));
578
* Sets the permission bitmasks that the user of the BlockBackend needs.
580
int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm,
585
if (blk->root && !blk->disable_perm) {
586
ret = bdrv_child_try_set_perm(blk->root, perm, shared_perm, errp);
593
blk->shared_perm = shared_perm;
598
void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)
601
*shared_perm = blk->shared_perm;
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).
609
* If an error is returned, the VM cannot be allowed to be resumed.
611
void blk_resume_after_migration(Error **errp)
614
Error *local_err = NULL;
616
for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
617
if (!blk->disable_perm) {
621
blk->disable_perm = false;
623
blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
625
error_propagate(errp, local_err);
626
blk->disable_perm = true;
510
632
static int blk_do_attach_dev(BlockBackend *blk, void *dev)
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;
517
647
blk->legacy_dev = false;
518
648
blk_iostatus_reset(blk);
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
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);
617
749
blk->dev_ops = ops;
618
750
blk->dev_opaque = opaque;
752
/* Are we currently quiesced? Should we enforce this right now? */
753
if (blk->quiesce_counter && ops->drained_begin) {
754
ops->drained_begin(opaque);
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.
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.
764
* If @load is false, notify of media eject. This can never fail.
625
766
* Also send DEVICE_TRAY_MOVED events as appropriate.
627
void blk_dev_change_media_cb(BlockBackend *blk, bool load)
768
void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp)
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;
632
774
assert(!blk->legacy_dev);
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);
779
assert(load == true);
780
error_propagate(errp, local_err);
636
783
tray_is_open = blk_dev_is_tray_open(blk);
638
785
if (tray_was_open != tray_is_open) {
897
1043
.ret = NOT_DONE,
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 */
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);
904
1055
return rwco.ret;
1005
1155
acb->has_returned = false;
1007
1157
co = qemu_coroutine_create(co_entry, acb);
1008
qemu_coroutine_enter(co);
1158
bdrv_coroutine_enter(blk_bs(blk), co);
1010
1160
acb->has_returned = true;
1011
1161
if (acb->rwco.ret != NOT_DONE) {
1769
1919
BlockBackend *blk = child->opaque;
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);
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. */
1779
1935
static void blk_root_drained_end(BdrvChild *child)
1781
1937
BlockBackend *blk = child->opaque;
1938
assert(blk->quiesce_counter);
1783
1940
assert(blk->public.io_limits_disabled);
1784
1941
--blk->public.io_limits_disabled;
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);