]> git.proxmox.com Git - mirror_qemu.git/blame - block/block-backend.c
Merge tag 'block-pull-request' of https://gitlab.com/stefanha/qemu into staging
[mirror_qemu.git] / block / block-backend.c
CommitLineData
26f54e9a
MA
1/*
2 * QEMU Block backends
3 *
60cb2fa7 4 * Copyright (C) 2014-2016 Red Hat, Inc.
26f54e9a
MA
5 *
6 * Authors:
7 * Markus Armbruster <armbru@redhat.com>,
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1
10 * or later. See the COPYING.LIB file in the top-level directory.
11 */
12
80c71a24 13#include "qemu/osdep.h"
26f54e9a
MA
14#include "sysemu/block-backend.h"
15#include "block/block_int.h"
373340b2 16#include "block/blockjob.h"
16d36e29 17#include "block/coroutines.h"
281d22d8 18#include "block/throttle-groups.h"
13d4ff07 19#include "hw/qdev-core.h"
18e46a03 20#include "sysemu/blockdev.h"
54d31236 21#include "sysemu/runstate.h"
e4ec5ad4 22#include "sysemu/replay.h"
e688df6b 23#include "qapi/error.h"
9af23989 24#include "qapi/qapi-events-block.h"
f348b6d1 25#include "qemu/id.h"
db725815 26#include "qemu/main-loop.h"
922a01a0 27#include "qemu/option.h"
1e98fefd 28#include "trace.h"
5f7772c4 29#include "migration/misc.h"
a7f53e26
MA
30
31/* Number of coroutines to reserve per attached device model */
32#define COROUTINE_POOL_RESERVATION 64
26f54e9a 33
1bf1cbc9
KW
34#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
35
4981bdec
HR
36static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
37
d03654ea
SH
38typedef struct BlockBackendAioNotifier {
39 void (*attached_aio_context)(AioContext *new_context, void *opaque);
40 void (*detach_aio_context)(void *opaque);
41 void *opaque;
42 QLIST_ENTRY(BlockBackendAioNotifier) list;
43} BlockBackendAioNotifier;
44
26f54e9a
MA
45struct BlockBackend {
46 char *name;
47 int refcnt;
f21d96d0 48 BdrvChild *root;
d861ab3a 49 AioContext *ctx;
26f8b3a8 50 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
2cf22d6a 51 QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */
9492b0b9 52 QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
f2cd875d 53 BlockBackendPublic public;
a7f53e26 54
d09ea2d2 55 DeviceState *dev; /* attached device model, if any */
a7f53e26
MA
56 const BlockDevOps *dev_ops;
57 void *dev_opaque;
68e9ec01 58
281d22d8
HR
59 /* If the BDS tree is removed, some of its options are stored here (which
60 * can be used to restore those options in the new BDS on insert) */
61 BlockBackendRootState root_state;
62
bfd18d1e
KW
63 bool enable_write_cache;
64
7f0e9da6
HR
65 /* I/O stats (display with "info blockstats"). */
66 BlockAcctStats stats;
373340b2
HR
67
68 BlockdevOnError on_read_error, on_write_error;
69 bool iostatus_enabled;
70 BlockDeviceIoStatus iostatus;
3301f6c6 71
981776b3
KW
72 uint64_t perm;
73 uint64_t shared_perm;
d35ff5e6 74 bool disable_perm;
981776b3 75
980b0f94 76 bool allow_aio_context_change;
c10c9d96
KW
77 bool allow_write_beyond_eof;
78
a2c4c3b1 79 /* Protected by BQL */
3301f6c6 80 NotifierList remove_bs_notifiers, insert_bs_notifiers;
d03654ea 81 QLIST_HEAD(, BlockBackendAioNotifier) aio_notifiers;
f4d9cc88
JS
82
83 int quiesce_counter;
cf312932
KW
84 CoQueue queued_requests;
85 bool disable_request_queuing;
86
5f7772c4 87 VMChangeStateEntry *vmsh;
ca2e2144 88 bool force_allow_inactivate;
33f2a757
SH
89
90 /* Number of in-flight aio requests. BlockDriverState also counts
91 * in-flight requests but aio requests can exist even when blk->root is
92 * NULL, so we cannot rely on its counter for that case.
93 * Accessed with atomic ops.
94 */
95 unsigned int in_flight;
26f54e9a
MA
96};
97
e7f7d676
HR
98typedef struct BlockBackendAIOCB {
99 BlockAIOCB common;
4981bdec 100 BlockBackend *blk;
e7f7d676
HR
101 int ret;
102} BlockBackendAIOCB;
103
104static const AIOCBInfo block_backend_aiocb_info = {
4981bdec 105 .get_aio_context = blk_aiocb_get_aio_context,
e7f7d676
HR
106 .aiocb_size = sizeof(BlockBackendAIOCB),
107};
108
8fb3c76c 109static void drive_info_del(DriveInfo *dinfo);
7c8eece4 110static BlockBackend *bdrv_first_blk(BlockDriverState *bs);
8fb3c76c 111
a2c4c3b1 112/* All BlockBackends. Protected by BQL. */
2cf22d6a
HR
113static QTAILQ_HEAD(, BlockBackend) block_backends =
114 QTAILQ_HEAD_INITIALIZER(block_backends);
115
a2c4c3b1
EGE
116/*
117 * All BlockBackends referenced by the monitor and which are iterated through by
118 * blk_next(). Protected by BQL.
119 */
9492b0b9
HR
120static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
121 QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
26f54e9a 122
3cdc69d3 123static void blk_root_inherit_options(BdrvChildRole role, bool parent_is_format,
272c02ea 124 int *child_flags, QDict *child_options,
f21d96d0
KW
125 int parent_flags, QDict *parent_options)
126{
127 /* We're not supposed to call this function for root nodes */
128 abort();
129}
c2066af0 130static void blk_root_drained_begin(BdrvChild *child);
fe5258a5 131static bool blk_root_drained_poll(BdrvChild *child);
e037c09c 132static void blk_root_drained_end(BdrvChild *child, int *drained_end_counter);
f21d96d0 133
5c8cab48
KW
134static void blk_root_change_media(BdrvChild *child, bool load);
135static void blk_root_resize(BdrvChild *child);
136
33949396
EGE
137static bool blk_root_change_aio_ctx(BdrvChild *child, AioContext *ctx,
138 GHashTable *visited, Transaction *tran,
139 Error **errp);
38475269 140
b5411555
KW
141static char *blk_root_get_parent_desc(BdrvChild *child)
142{
143 BlockBackend *blk = child->opaque;
fd240a18 144 g_autofree char *dev_id = NULL;
b5411555
KW
145
146 if (blk->name) {
fd240a18 147 return g_strdup_printf("block device '%s'", blk->name);
b5411555
KW
148 }
149
150 dev_id = blk_get_attached_dev_id(blk);
151 if (*dev_id) {
fd240a18 152 return g_strdup_printf("block device '%s'", dev_id);
b5411555
KW
153 } else {
154 /* TODO Callback into the BB owner for something more detailed */
fd240a18 155 return g_strdup("an unnamed block device");
b5411555
KW
156 }
157}
158
4c265bf9
KW
159static const char *blk_root_get_name(BdrvChild *child)
160{
161 return blk_name(child->opaque);
162}
163
538f0497 164static void blk_vm_state_changed(void *opaque, bool running, RunState state)
5f7772c4
FZ
165{
166 Error *local_err = NULL;
167 BlockBackend *blk = opaque;
168
169 if (state == RUN_STATE_INMIGRATE) {
170 return;
171 }
172
173 qemu_del_vm_change_state_handler(blk->vmsh);
174 blk->vmsh = NULL;
175 blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
176 if (local_err) {
177 error_report_err(local_err);
178 }
179}
180
4417ab7a
KW
181/*
182 * Notifies the user of the BlockBackend that migration has completed. qdev
183 * devices can tighten their permissions in response (specifically revoke
184 * shared write permissions that we needed for storage migration).
185 *
186 * If an error is returned, the VM cannot be allowed to be resumed.
187 */
188static void blk_root_activate(BdrvChild *child, Error **errp)
189{
190 BlockBackend *blk = child->opaque;
191 Error *local_err = NULL;
492a1196 192 uint64_t saved_shared_perm;
4417ab7a
KW
193
194 if (!blk->disable_perm) {
195 return;
196 }
197
198 blk->disable_perm = false;
199
492a1196
HR
200 /*
201 * blk->shared_perm contains the permissions we want to share once
202 * migration is really completely done. For now, we need to share
203 * all; but we also need to retain blk->shared_perm, which is
204 * overwritten by a successful blk_set_perm() call. Save it and
205 * restore it below.
206 */
207 saved_shared_perm = blk->shared_perm;
208
5f7772c4
FZ
209 blk_set_perm(blk, blk->perm, BLK_PERM_ALL, &local_err);
210 if (local_err) {
211 error_propagate(errp, local_err);
212 blk->disable_perm = true;
213 return;
214 }
492a1196 215 blk->shared_perm = saved_shared_perm;
5f7772c4
FZ
216
217 if (runstate_check(RUN_STATE_INMIGRATE)) {
218 /* Activation can happen when migration process is still active, for
219 * example when nbd_server_add is called during non-shared storage
220 * migration. Defer the shared_perm update to migration completion. */
221 if (!blk->vmsh) {
222 blk->vmsh = qemu_add_vm_change_state_handler(blk_vm_state_changed,
223 blk);
224 }
225 return;
226 }
227
4417ab7a
KW
228 blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
229 if (local_err) {
230 error_propagate(errp, local_err);
231 blk->disable_perm = true;
232 return;
233 }
234}
235
ca2e2144
FZ
236void blk_set_force_allow_inactivate(BlockBackend *blk)
237{
0439c5a4 238 GLOBAL_STATE_CODE();
ca2e2144
FZ
239 blk->force_allow_inactivate = true;
240}
241
c16de8f5
FZ
242static bool blk_can_inactivate(BlockBackend *blk)
243{
ca2e2144 244 /* If it is a guest device, inactivate is ok. */
c16de8f5
FZ
245 if (blk->dev || blk_name(blk)[0]) {
246 return true;
247 }
248
ca2e2144
FZ
249 /* Inactivating means no more writes to the image can be done,
250 * even if those writes would be changes invisible to the
251 * guest. For block job BBs that satisfy this, we can just allow
252 * it. This is the case for mirror job source, which is required
253 * by libvirt non-shared block migration. */
254 if (!(blk->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED))) {
255 return true;
256 }
257
258 return blk->force_allow_inactivate;
c16de8f5
FZ
259}
260
cfa1a572
KW
261static int blk_root_inactivate(BdrvChild *child)
262{
263 BlockBackend *blk = child->opaque;
264
265 if (blk->disable_perm) {
266 return 0;
267 }
268
c16de8f5 269 if (!blk_can_inactivate(blk)) {
cfa1a572
KW
270 return -EPERM;
271 }
272
273 blk->disable_perm = true;
274 if (blk->root) {
275 bdrv_child_try_set_perm(blk->root, 0, BLK_PERM_ALL, &error_abort);
276 }
277
278 return 0;
279}
280
d03654ea
SH
281static void blk_root_attach(BdrvChild *child)
282{
283 BlockBackend *blk = child->opaque;
284 BlockBackendAioNotifier *notifier;
285
286 trace_blk_root_attach(child, blk, child->bs);
287
288 QLIST_FOREACH(notifier, &blk->aio_notifiers, list) {
289 bdrv_add_aio_context_notifier(child->bs,
290 notifier->attached_aio_context,
291 notifier->detach_aio_context,
292 notifier->opaque);
293 }
294}
295
296static void blk_root_detach(BdrvChild *child)
297{
298 BlockBackend *blk = child->opaque;
299 BlockBackendAioNotifier *notifier;
300
301 trace_blk_root_detach(child, blk, child->bs);
302
303 QLIST_FOREACH(notifier, &blk->aio_notifiers, list) {
304 bdrv_remove_aio_context_notifier(child->bs,
305 notifier->attached_aio_context,
306 notifier->detach_aio_context,
307 notifier->opaque);
308 }
309}
310
3ca1f322
VSO
311static AioContext *blk_root_get_parent_aio_context(BdrvChild *c)
312{
313 BlockBackend *blk = c->opaque;
314
315 return blk_get_aio_context(blk);
316}
317
bd86fb99 318static const BdrvChildClass child_root = {
c2066af0
KW
319 .inherit_options = blk_root_inherit_options,
320
5c8cab48
KW
321 .change_media = blk_root_change_media,
322 .resize = blk_root_resize,
4c265bf9 323 .get_name = blk_root_get_name,
b5411555 324 .get_parent_desc = blk_root_get_parent_desc,
5c8cab48 325
c2066af0 326 .drained_begin = blk_root_drained_begin,
fe5258a5 327 .drained_poll = blk_root_drained_poll,
c2066af0 328 .drained_end = blk_root_drained_end,
4417ab7a
KW
329
330 .activate = blk_root_activate,
cfa1a572 331 .inactivate = blk_root_inactivate,
d03654ea
SH
332
333 .attach = blk_root_attach,
334 .detach = blk_root_detach,
38475269 335
33949396 336 .change_aio_ctx = blk_root_change_aio_ctx,
3ca1f322
VSO
337
338 .get_parent_aio_context = blk_root_get_parent_aio_context,
f21d96d0
KW
339};
340
26f54e9a 341/*
efaa7c4e 342 * Create a new BlockBackend with a reference count of one.
6d0eb64d
KW
343 *
344 * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
345 * to request for a block driver node that is attached to this BlockBackend.
346 * @shared_perm is a bitmask which describes which permissions may be granted
347 * to other users of the attached node.
348 * Both sets of permissions can be changed later using blk_set_perm().
349 *
26f54e9a
MA
350 * Return the new BlockBackend on success, null on failure.
351 */
d861ab3a 352BlockBackend *blk_new(AioContext *ctx, uint64_t perm, uint64_t shared_perm)
26f54e9a
MA
353{
354 BlockBackend *blk;
355
0439c5a4
EGE
356 GLOBAL_STATE_CODE();
357
26f54e9a 358 blk = g_new0(BlockBackend, 1);
26f54e9a 359 blk->refcnt = 1;
d861ab3a 360 blk->ctx = ctx;
6d0eb64d
KW
361 blk->perm = perm;
362 blk->shared_perm = shared_perm;
0c3169df
KW
363 blk_set_enable_write_cache(blk, true);
364
cb53460b
KW
365 blk->on_read_error = BLOCKDEV_ON_ERROR_REPORT;
366 blk->on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
367
9caa6f3d 368 block_acct_init(&blk->stats);
27ccdd52 369
cf312932 370 qemu_co_queue_init(&blk->queued_requests);
3301f6c6
HR
371 notifier_list_init(&blk->remove_bs_notifiers);
372 notifier_list_init(&blk->insert_bs_notifiers);
d03654ea 373 QLIST_INIT(&blk->aio_notifiers);
27ccdd52 374
2cf22d6a 375 QTAILQ_INSERT_TAIL(&block_backends, blk, link);
26f54e9a
MA
376 return blk;
377}
378
a3aeeab5
EB
379/*
380 * Create a new BlockBackend connected to an existing BlockDriverState.
381 *
382 * @perm is a bitmasks of BLK_PERM_* constants which describes the
383 * permissions to request for @bs that is attached to this
384 * BlockBackend. @shared_perm is a bitmask which describes which
385 * permissions may be granted to other users of the attached node.
386 * Both sets of permissions can be changed later using blk_set_perm().
387 *
388 * Return the new BlockBackend on success, null on failure.
389 */
390BlockBackend *blk_new_with_bs(BlockDriverState *bs, uint64_t perm,
391 uint64_t shared_perm, Error **errp)
392{
393 BlockBackend *blk = blk_new(bdrv_get_aio_context(bs), perm, shared_perm);
394
0439c5a4
EGE
395 GLOBAL_STATE_CODE();
396
a3aeeab5
EB
397 if (blk_insert_bs(blk, bs, errp) < 0) {
398 blk_unref(blk);
399 return NULL;
400 }
401 return blk;
402}
403
7e7d56d9 404/*
28eb9b12 405 * Creates a new BlockBackend, opens a new BlockDriverState, and connects both.
d861ab3a 406 * The new BlockBackend is in the main AioContext.
ca49a4fd
HR
407 *
408 * Just as with bdrv_open(), after having called this function the reference to
409 * @options belongs to the block layer (even on failure).
410 *
411 * TODO: Remove @filename and @flags; it should be possible to specify a whole
412 * BDS tree just by specifying the @options QDict (or @reference,
413 * alternatively). At the time of adding this function, this is not possible,
414 * though, so callers of this function have to be able to specify @filename and
415 * @flags.
416 */
efaa7c4e
HR
417BlockBackend *blk_new_open(const char *filename, const char *reference,
418 QDict *options, int flags, Error **errp)
ca49a4fd
HR
419{
420 BlockBackend *blk;
28eb9b12 421 BlockDriverState *bs;
1f4ad7d3 422 uint64_t perm = 0;
35b7f4ab 423 uint64_t shared = BLK_PERM_ALL;
c62d32f5 424
0439c5a4
EGE
425 GLOBAL_STATE_CODE();
426
35b7f4ab
KW
427 /*
428 * blk_new_open() is mainly used in .bdrv_create implementations and the
429 * tools where sharing isn't a major concern because the BDS stays private
430 * and the file is generally not supposed to be used by a second process,
431 * so we just request permission according to the flags.
c62d32f5
KW
432 *
433 * The exceptions are xen_disk and blockdev_init(); in these cases, the
434 * caller of blk_new_open() doesn't make use of the permissions, but they
435 * shouldn't hurt either. We can still share everything here because the
35b7f4ab
KW
436 * guest devices will add their own blockers if they can't share.
437 */
1f4ad7d3
KW
438 if ((flags & BDRV_O_NO_IO) == 0) {
439 perm |= BLK_PERM_CONSISTENT_READ;
440 if (flags & BDRV_O_RDWR) {
441 perm |= BLK_PERM_WRITE;
442 }
c62d32f5
KW
443 }
444 if (flags & BDRV_O_RESIZE) {
445 perm |= BLK_PERM_RESIZE;
446 }
35b7f4ab
KW
447 if (flags & BDRV_O_NO_SHARE) {
448 shared = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED;
449 }
ca49a4fd 450
35b7f4ab 451 blk = blk_new(qemu_get_aio_context(), perm, shared);
5b363937
HR
452 bs = bdrv_open(filename, reference, options, flags, errp);
453 if (!bs) {
ca49a4fd
HR
454 blk_unref(blk);
455 return NULL;
456 }
457
1f38f04e
HR
458 blk->root = bdrv_root_attach_child(bs, "root", &child_root,
459 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
35b7f4ab 460 perm, shared, blk, errp);
50bfbe93 461 if (!blk->root) {
50bfbe93
FZ
462 blk_unref(blk);
463 return NULL;
464 }
72e775c7 465
ca49a4fd
HR
466 return blk;
467}
468
26f54e9a
MA
469static void blk_delete(BlockBackend *blk)
470{
471 assert(!blk->refcnt);
e5e78550 472 assert(!blk->name);
a7f53e26 473 assert(!blk->dev);
022cdc9f 474 if (blk->public.throttle_group_member.throttle_state) {
1606e4cf
EB
475 blk_io_limits_disable(blk);
476 }
f21d96d0 477 if (blk->root) {
13855c6b 478 blk_remove_bs(blk);
7e7d56d9 479 }
5f7772c4
FZ
480 if (blk->vmsh) {
481 qemu_del_vm_change_state_handler(blk->vmsh);
482 blk->vmsh = NULL;
483 }
3301f6c6
HR
484 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
485 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
d03654ea 486 assert(QLIST_EMPTY(&blk->aio_notifiers));
2cf22d6a 487 QTAILQ_REMOVE(&block_backends, blk, link);
18e46a03 488 drive_info_del(blk->legacy_dinfo);
979e9b03 489 block_acct_cleanup(&blk->stats);
26f54e9a
MA
490 g_free(blk);
491}
492
8fb3c76c
MA
493static void drive_info_del(DriveInfo *dinfo)
494{
495 if (!dinfo) {
496 return;
497 }
498 qemu_opts_del(dinfo->opts);
8fb3c76c
MA
499 g_free(dinfo);
500}
501
f636ae85
AG
502int blk_get_refcnt(BlockBackend *blk)
503{
0439c5a4 504 GLOBAL_STATE_CODE();
f636ae85
AG
505 return blk ? blk->refcnt : 0;
506}
507
26f54e9a
MA
508/*
509 * Increment @blk's reference count.
510 * @blk must not be null.
511 */
512void blk_ref(BlockBackend *blk)
513{
5ca9d21b 514 assert(blk->refcnt > 0);
0439c5a4 515 GLOBAL_STATE_CODE();
26f54e9a
MA
516 blk->refcnt++;
517}
518
519/*
520 * Decrement @blk's reference count.
521 * If this drops it to zero, destroy @blk.
522 * For convenience, do nothing if @blk is null.
523 */
524void blk_unref(BlockBackend *blk)
525{
0439c5a4 526 GLOBAL_STATE_CODE();
26f54e9a
MA
527 if (blk) {
528 assert(blk->refcnt > 0);
5ca9d21b
KW
529 if (blk->refcnt > 1) {
530 blk->refcnt--;
531 } else {
532 blk_drain(blk);
533 /* blk_drain() cannot resurrect blk, nobody held a reference */
534 assert(blk->refcnt == 1);
535 blk->refcnt = 0;
26f54e9a
MA
536 blk_delete(blk);
537 }
538 }
539}
540
2cf22d6a
HR
541/*
542 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
543 * ones which are hidden (i.e. are not referenced by the monitor).
544 */
a429b9b5 545BlockBackend *blk_all_next(BlockBackend *blk)
2cf22d6a 546{
0439c5a4 547 GLOBAL_STATE_CODE();
2cf22d6a
HR
548 return blk ? QTAILQ_NEXT(blk, link)
549 : QTAILQ_FIRST(&block_backends);
550}
551
d8da3cef
HR
552void blk_remove_all_bs(void)
553{
74d1b8fc 554 BlockBackend *blk = NULL;
d8da3cef 555
0439c5a4
EGE
556 GLOBAL_STATE_CODE();
557
2cf22d6a 558 while ((blk = blk_all_next(blk)) != NULL) {
d8da3cef
HR
559 AioContext *ctx = blk_get_aio_context(blk);
560
561 aio_context_acquire(ctx);
f21d96d0 562 if (blk->root) {
d8da3cef
HR
563 blk_remove_bs(blk);
564 }
565 aio_context_release(ctx);
566 }
567}
568
26f54e9a 569/*
9492b0b9 570 * Return the monitor-owned BlockBackend after @blk.
26f54e9a
MA
571 * If @blk is null, return the first one.
572 * Else, return @blk's next sibling, which may be null.
573 *
574 * To iterate over all BlockBackends, do
575 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
576 * ...
577 * }
578 */
579BlockBackend *blk_next(BlockBackend *blk)
580{
0439c5a4 581 GLOBAL_STATE_CODE();
9492b0b9
HR
582 return blk ? QTAILQ_NEXT(blk, monitor_link)
583 : QTAILQ_FIRST(&monitor_block_backends);
26f54e9a
MA
584}
585
7c8eece4
KW
586/* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by
587 * the monitor or attached to a BlockBackend */
88be7b4b 588BlockDriverState *bdrv_next(BdrvNextIterator *it)
7c8eece4 589{
5e003f17
HR
590 BlockDriverState *bs, *old_bs;
591
592 /* Must be called from the main loop */
593 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
981f4f57 594
7c8eece4
KW
595 /* First, return all root nodes of BlockBackends. In order to avoid
596 * returning a BDS twice when multiple BBs refer to it, we only return it
597 * if the BB is the first one in the parent list of the BDS. */
598 if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
5e003f17
HR
599 BlockBackend *old_blk = it->blk;
600
601 old_bs = old_blk ? blk_bs(old_blk) : NULL;
602
7c8eece4
KW
603 do {
604 it->blk = blk_all_next(it->blk);
88be7b4b
KW
605 bs = it->blk ? blk_bs(it->blk) : NULL;
606 } while (it->blk && (bs == NULL || bdrv_first_blk(bs) != it->blk));
7c8eece4 607
5e003f17
HR
608 if (it->blk) {
609 blk_ref(it->blk);
610 }
611 blk_unref(old_blk);
612
88be7b4b 613 if (bs) {
5e003f17
HR
614 bdrv_ref(bs);
615 bdrv_unref(old_bs);
88be7b4b 616 return bs;
7c8eece4
KW
617 }
618 it->phase = BDRV_NEXT_MONITOR_OWNED;
5e003f17
HR
619 } else {
620 old_bs = it->bs;
7c8eece4
KW
621 }
622
623 /* Then return the monitor-owned BDSes without a BB attached. Ignore all
624 * BDSes that are attached to a BlockBackend here; they have been handled
625 * by the above block already */
981f4f57 626 do {
7c8eece4 627 it->bs = bdrv_next_monitor_owned(it->bs);
88be7b4b
KW
628 bs = it->bs;
629 } while (bs && bdrv_has_blk(bs));
630
5e003f17
HR
631 if (bs) {
632 bdrv_ref(bs);
633 }
634 bdrv_unref(old_bs);
635
88be7b4b
KW
636 return bs;
637}
638
5e003f17 639static void bdrv_next_reset(BdrvNextIterator *it)
88be7b4b
KW
640{
641 *it = (BdrvNextIterator) {
642 .phase = BDRV_NEXT_BACKEND_ROOTS,
643 };
5e003f17 644}
981f4f57 645
5e003f17
HR
646BlockDriverState *bdrv_first(BdrvNextIterator *it)
647{
0439c5a4 648 GLOBAL_STATE_CODE();
5e003f17 649 bdrv_next_reset(it);
88be7b4b 650 return bdrv_next(it);
981f4f57
HR
651}
652
5e003f17
HR
653/* Must be called when aborting a bdrv_next() iteration before
654 * bdrv_next() returns NULL */
655void bdrv_next_cleanup(BdrvNextIterator *it)
656{
657 /* Must be called from the main loop */
658 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
659
660 if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
661 if (it->blk) {
662 bdrv_unref(blk_bs(it->blk));
663 blk_unref(it->blk);
664 }
665 } else {
666 bdrv_unref(it->bs);
667 }
668
669 bdrv_next_reset(it);
670}
671
e5e78550
HR
672/*
673 * Add a BlockBackend into the list of backends referenced by the monitor, with
674 * the given @name acting as the handle for the monitor.
675 * Strictly for use by blockdev.c.
676 *
677 * @name must not be null or empty.
678 *
679 * Returns true on success and false on failure. In the latter case, an Error
680 * object is returned through @errp.
681 */
682bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
683{
684 assert(!blk->name);
685 assert(name && name[0]);
0439c5a4 686 GLOBAL_STATE_CODE();
e5e78550
HR
687
688 if (!id_wellformed(name)) {
689 error_setg(errp, "Invalid device name");
690 return false;
691 }
692 if (blk_by_name(name)) {
693 error_setg(errp, "Device with id '%s' already exists", name);
694 return false;
695 }
696 if (bdrv_find_node(name)) {
697 error_setg(errp,
698 "Device name '%s' conflicts with an existing node name",
699 name);
700 return false;
701 }
702
703 blk->name = g_strdup(name);
704 QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
705 return true;
706}
707
708/*
709 * Remove a BlockBackend from the list of backends referenced by the monitor.
710 * Strictly for use by blockdev.c.
711 */
712void monitor_remove_blk(BlockBackend *blk)
713{
0439c5a4
EGE
714 GLOBAL_STATE_CODE();
715
e5e78550
HR
716 if (!blk->name) {
717 return;
718 }
719
720 QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
721 g_free(blk->name);
722 blk->name = NULL;
723}
724
26f54e9a 725/*
7e7d56d9 726 * Return @blk's name, a non-null string.
e5e78550 727 * Returns an empty string iff @blk is not referenced by the monitor.
26f54e9a 728 */
0731a50f 729const char *blk_name(const BlockBackend *blk)
26f54e9a 730{
37868b2a 731 IO_CODE();
e5e78550 732 return blk->name ?: "";
26f54e9a
MA
733}
734
735/*
736 * Return the BlockBackend with name @name if it exists, else null.
737 * @name must not be null.
738 */
739BlockBackend *blk_by_name(const char *name)
740{
74d1b8fc 741 BlockBackend *blk = NULL;
26f54e9a 742
0439c5a4 743 GLOBAL_STATE_CODE();
26f54e9a 744 assert(name);
74d1b8fc 745 while ((blk = blk_next(blk)) != NULL) {
26f54e9a
MA
746 if (!strcmp(name, blk->name)) {
747 return blk;
748 }
749 }
750 return NULL;
751}
7e7d56d9
MA
752
753/*
754 * Return the BlockDriverState attached to @blk if any, else null.
755 */
756BlockDriverState *blk_bs(BlockBackend *blk)
757{
37868b2a 758 IO_CODE();
f21d96d0 759 return blk->root ? blk->root->bs : NULL;
7e7d56d9
MA
760}
761
7c8eece4 762static BlockBackend *bdrv_first_blk(BlockDriverState *bs)
dde33812
KW
763{
764 BdrvChild *child;
bdb73476
EGE
765
766 GLOBAL_STATE_CODE();
767
dde33812 768 QLIST_FOREACH(child, &bs->parents, next_parent) {
bd86fb99 769 if (child->klass == &child_root) {
7c8eece4 770 return child->opaque;
dde33812
KW
771 }
772 }
773
7c8eece4
KW
774 return NULL;
775}
776
777/*
778 * Returns true if @bs has an associated BlockBackend.
779 */
780bool bdrv_has_blk(BlockDriverState *bs)
781{
0439c5a4 782 GLOBAL_STATE_CODE();
7c8eece4 783 return bdrv_first_blk(bs) != NULL;
dde33812
KW
784}
785
b6c1bae5
KW
786/*
787 * Returns true if @bs has only BlockBackends as parents.
788 */
789bool bdrv_is_root_node(BlockDriverState *bs)
790{
791 BdrvChild *c;
792
0439c5a4 793 GLOBAL_STATE_CODE();
b6c1bae5 794 QLIST_FOREACH(c, &bs->parents, next_parent) {
bd86fb99 795 if (c->klass != &child_root) {
b6c1bae5
KW
796 return false;
797 }
798 }
799
800 return true;
801}
802
18e46a03
MA
803/*
804 * Return @blk's DriveInfo if any, else null.
805 */
806DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
807{
c5be7445 808 GLOBAL_STATE_CODE();
18e46a03
MA
809 return blk->legacy_dinfo;
810}
811
812/*
813 * Set @blk's DriveInfo to @dinfo, and return it.
814 * @blk must not have a DriveInfo set already.
815 * No other BlockBackend may have the same DriveInfo set.
816 */
817DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
818{
819 assert(!blk->legacy_dinfo);
c5be7445 820 GLOBAL_STATE_CODE();
18e46a03
MA
821 return blk->legacy_dinfo = dinfo;
822}
823
824/*
825 * Return the BlockBackend with DriveInfo @dinfo.
826 * It must exist.
827 */
828BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
829{
74d1b8fc 830 BlockBackend *blk = NULL;
c5be7445 831 GLOBAL_STATE_CODE();
18e46a03 832
74d1b8fc 833 while ((blk = blk_next(blk)) != NULL) {
18e46a03
MA
834 if (blk->legacy_dinfo == dinfo) {
835 return blk;
836 }
837 }
838 abort();
839}
840
f2cd875d
KW
841/*
842 * Returns a pointer to the publicly accessible fields of @blk.
843 */
844BlockBackendPublic *blk_get_public(BlockBackend *blk)
845{
0439c5a4 846 GLOBAL_STATE_CODE();
f2cd875d
KW
847 return &blk->public;
848}
849
850/*
851 * Returns a BlockBackend given the associated @public fields.
852 */
853BlockBackend *blk_by_public(BlockBackendPublic *public)
854{
0439c5a4 855 GLOBAL_STATE_CODE();
f2cd875d
KW
856 return container_of(public, BlockBackend, public);
857}
858
1c95f7e1
HR
859/*
860 * Disassociates the currently associated BlockDriverState from @blk.
861 */
862void blk_remove_bs(BlockBackend *blk)
863{
c89bcf3a 864 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
e6cada92 865 BdrvChild *root;
022cdc9f 866
0439c5a4
EGE
867 GLOBAL_STATE_CODE();
868
3301f6c6 869 notifier_list_notify(&blk->remove_bs_notifiers, blk);
c89bcf3a 870 if (tgm->throttle_state) {
1e3552db
SH
871 BlockDriverState *bs = blk_bs(blk);
872
873 /*
874 * Take a ref in case blk_bs() changes across bdrv_drained_begin(), for
875 * example, if a temporary filter node is removed by a blockjob.
876 */
877 bdrv_ref(bs);
632a7735 878 bdrv_drained_begin(bs);
c89bcf3a
AG
879 throttle_group_detach_aio_context(tgm);
880 throttle_group_attach_aio_context(tgm, qemu_get_aio_context());
632a7735 881 bdrv_drained_end(bs);
1e3552db 882 bdrv_unref(bs);
a5614993 883 }
1c95f7e1 884
7ca7f0f6
KW
885 blk_update_root_state(blk);
886
f45280cb
GK
887 /* bdrv_root_unref_child() will cause blk->root to become stale and may
888 * switch to a completion coroutine later on. Let's drain all I/O here
889 * to avoid that and a potential QEMU crash.
890 */
891 blk_drain(blk);
e6cada92 892 root = blk->root;
f21d96d0 893 blk->root = NULL;
e6cada92 894 bdrv_root_unref_child(root);
1c95f7e1
HR
895}
896
0c3c36d6
HR
897/*
898 * Associates a new BlockDriverState with @blk.
899 */
d7086422 900int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
0c3c36d6 901{
c89bcf3a 902 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
0439c5a4 903 GLOBAL_STATE_CODE();
b441dc71 904 bdrv_ref(bs);
1f38f04e
HR
905 blk->root = bdrv_root_attach_child(bs, "root", &child_root,
906 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
228ca37e 907 blk->perm, blk->shared_perm,
1f38f04e 908 blk, errp);
d7086422
KW
909 if (blk->root == NULL) {
910 return -EPERM;
911 }
3301f6c6
HR
912
913 notifier_list_notify(&blk->insert_bs_notifiers, blk);
c89bcf3a
AG
914 if (tgm->throttle_state) {
915 throttle_group_detach_aio_context(tgm);
916 throttle_group_attach_aio_context(tgm, bdrv_get_aio_context(bs));
7ca7f0f6 917 }
d7086422
KW
918
919 return 0;
0c3c36d6
HR
920}
921
ed089506
VSO
922/*
923 * Change BlockDriverState associated with @blk.
924 */
925int blk_replace_bs(BlockBackend *blk, BlockDriverState *new_bs, Error **errp)
926{
0439c5a4 927 GLOBAL_STATE_CODE();
ed089506
VSO
928 return bdrv_replace_child_bs(blk->root, new_bs, errp);
929}
930
981776b3
KW
931/*
932 * Sets the permission bitmasks that the user of the BlockBackend needs.
933 */
934int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm,
935 Error **errp)
936{
937 int ret;
0439c5a4 938 GLOBAL_STATE_CODE();
981776b3 939
d35ff5e6 940 if (blk->root && !blk->disable_perm) {
981776b3
KW
941 ret = bdrv_child_try_set_perm(blk->root, perm, shared_perm, errp);
942 if (ret < 0) {
943 return ret;
944 }
945 }
946
947 blk->perm = perm;
948 blk->shared_perm = shared_perm;
949
950 return 0;
951}
952
887354bd
KW
953void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)
954{
0439c5a4 955 GLOBAL_STATE_CODE();
887354bd
KW
956 *perm = blk->perm;
957 *shared_perm = blk->shared_perm;
958}
959
d09ea2d2
TH
960/*
961 * Attach device model @dev to @blk.
962 * Return 0 on success, -EBUSY when a device model is attached already.
963 */
964int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
4be74634 965{
0439c5a4 966 GLOBAL_STATE_CODE();
a7f53e26
MA
967 if (blk->dev) {
968 return -EBUSY;
969 }
d35ff5e6
KW
970
971 /* While migration is still incoming, we don't need to apply the
972 * permissions of guest device BlockBackends. We might still have a block
973 * job or NBD server writing to the image for storage migration. */
974 if (runstate_check(RUN_STATE_INMIGRATE)) {
975 blk->disable_perm = true;
976 }
977
84ebe375 978 blk_ref(blk);
a7f53e26 979 blk->dev = dev;
373340b2 980 blk_iostatus_reset(blk);
d35ff5e6 981
a7f53e26 982 return 0;
4be74634
MA
983}
984
a7f53e26
MA
985/*
986 * Detach device model @dev from @blk.
987 * @dev must be currently attached to @blk.
988 */
d09ea2d2 989void blk_detach_dev(BlockBackend *blk, DeviceState *dev)
4be74634 990{
a7f53e26 991 assert(blk->dev == dev);
0439c5a4 992 GLOBAL_STATE_CODE();
a7f53e26
MA
993 blk->dev = NULL;
994 blk->dev_ops = NULL;
995 blk->dev_opaque = NULL;
981776b3 996 blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort);
84ebe375 997 blk_unref(blk);
4be74634
MA
998}
999
a7f53e26
MA
1000/*
1001 * Return the device model attached to @blk if any, else null.
1002 */
d09ea2d2 1003DeviceState *blk_get_attached_dev(BlockBackend *blk)
a7f53e26 1004{
0439c5a4 1005 GLOBAL_STATE_CODE();
a7f53e26
MA
1006 return blk->dev;
1007}
1008
2d76e724
KW
1009/* Return the qdev ID, or if no ID is assigned the QOM path, of the block
1010 * device attached to the BlockBackend. */
77beef83 1011char *blk_get_attached_dev_id(BlockBackend *blk)
2d76e724 1012{
d09ea2d2 1013 DeviceState *dev = blk->dev;
37868b2a 1014 IO_CODE();
2d76e724
KW
1015
1016 if (!dev) {
1017 return g_strdup("");
1018 } else if (dev->id) {
1019 return g_strdup(dev->id);
1020 }
602414d1
LM
1021
1022 return object_get_canonical_path(OBJECT(dev)) ?: g_strdup("");
2d76e724
KW
1023}
1024
1c89e1fa
KW
1025/*
1026 * Return the BlockBackend which has the device model @dev attached if it
1027 * exists, else null.
1028 *
1029 * @dev must not be null.
1030 */
1031BlockBackend *blk_by_dev(void *dev)
1032{
1033 BlockBackend *blk = NULL;
1034
0439c5a4
EGE
1035 GLOBAL_STATE_CODE();
1036
1c89e1fa
KW
1037 assert(dev != NULL);
1038 while ((blk = blk_all_next(blk)) != NULL) {
1039 if (blk->dev == dev) {
1040 return blk;
1041 }
1042 }
1043 return NULL;
1044}
1045
a7f53e26
MA
1046/*
1047 * Set @blk's device model callbacks to @ops.
1048 * @opaque is the opaque argument to pass to the callbacks.
1049 * This is for use by device models.
1050 */
1051void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
1052 void *opaque)
1053{
0439c5a4 1054 GLOBAL_STATE_CODE();
a7f53e26
MA
1055 blk->dev_ops = ops;
1056 blk->dev_opaque = opaque;
f4d9cc88
JS
1057
1058 /* Are we currently quiesced? Should we enforce this right now? */
ac1fc3a3 1059 if (blk->quiesce_counter && ops && ops->drained_begin) {
f4d9cc88
JS
1060 ops->drained_begin(opaque);
1061 }
a7f53e26
MA
1062}
1063
1064/*
1065 * Notify @blk's attached device model of media change.
39829a01
KW
1066 *
1067 * If @load is true, notify of media load. This action can fail, meaning that
1068 * the medium cannot be loaded. @errp is set then.
1069 *
1070 * If @load is false, notify of media eject. This can never fail.
1071 *
a7f53e26
MA
1072 * Also send DEVICE_TRAY_MOVED events as appropriate.
1073 */
39829a01 1074void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp)
a7f53e26 1075{
0439c5a4 1076 GLOBAL_STATE_CODE();
a7f53e26 1077 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
f1f57066 1078 bool tray_was_open, tray_is_open;
39829a01 1079 Error *local_err = NULL;
a7f53e26 1080
f1f57066 1081 tray_was_open = blk_dev_is_tray_open(blk);
39829a01
KW
1082 blk->dev_ops->change_media_cb(blk->dev_opaque, load, &local_err);
1083 if (local_err) {
1084 assert(load == true);
1085 error_propagate(errp, local_err);
1086 return;
1087 }
f1f57066
HR
1088 tray_is_open = blk_dev_is_tray_open(blk);
1089
1090 if (tray_was_open != tray_is_open) {
2d76e724 1091 char *id = blk_get_attached_dev_id(blk);
3ab72385 1092 qapi_event_send_device_tray_moved(blk_name(blk), id, tray_is_open);
2d76e724 1093 g_free(id);
a7f53e26
MA
1094 }
1095 }
1096}
1097
5c8cab48
KW
1098static void blk_root_change_media(BdrvChild *child, bool load)
1099{
39829a01 1100 blk_dev_change_media_cb(child->opaque, load, NULL);
5c8cab48
KW
1101}
1102
a7f53e26
MA
1103/*
1104 * Does @blk's attached device model have removable media?
1105 * %true if no device model is attached.
1106 */
1107bool blk_dev_has_removable_media(BlockBackend *blk)
1108{
b4ad82aa 1109 GLOBAL_STATE_CODE();
a7f53e26
MA
1110 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
1111}
1112
8f3a73bc
HR
1113/*
1114 * Does @blk's attached device model have a tray?
1115 */
1116bool blk_dev_has_tray(BlockBackend *blk)
1117{
967d7905 1118 IO_CODE();
8f3a73bc
HR
1119 return blk->dev_ops && blk->dev_ops->is_tray_open;
1120}
1121
a7f53e26
MA
1122/*
1123 * Notify @blk's attached device model of a media eject request.
1124 * If @force is true, the medium is about to be yanked out forcefully.
1125 */
1126void blk_dev_eject_request(BlockBackend *blk, bool force)
4be74634 1127{
b4ad82aa 1128 GLOBAL_STATE_CODE();
a7f53e26
MA
1129 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
1130 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
1131 }
4be74634
MA
1132}
1133
a7f53e26
MA
1134/*
1135 * Does @blk's attached device model have a tray, and is it open?
1136 */
1137bool blk_dev_is_tray_open(BlockBackend *blk)
4be74634 1138{
967d7905 1139 IO_CODE();
8f3a73bc 1140 if (blk_dev_has_tray(blk)) {
a7f53e26
MA
1141 return blk->dev_ops->is_tray_open(blk->dev_opaque);
1142 }
1143 return false;
1144}
1145
1146/*
1147 * Does @blk's attached device model have the medium locked?
1148 * %false if the device model has no such lock.
1149 */
1150bool blk_dev_is_medium_locked(BlockBackend *blk)
1151{
b4ad82aa 1152 GLOBAL_STATE_CODE();
a7f53e26
MA
1153 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
1154 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
1155 }
1156 return false;
1157}
1158
1159/*
1160 * Notify @blk's attached device model of a backend size change.
1161 */
5c8cab48 1162static void blk_root_resize(BdrvChild *child)
a7f53e26 1163{
5c8cab48
KW
1164 BlockBackend *blk = child->opaque;
1165
a7f53e26
MA
1166 if (blk->dev_ops && blk->dev_ops->resize_cb) {
1167 blk->dev_ops->resize_cb(blk->dev_opaque);
1168 }
1169}
1170
1171void blk_iostatus_enable(BlockBackend *blk)
1172{
0439c5a4 1173 GLOBAL_STATE_CODE();
373340b2
HR
1174 blk->iostatus_enabled = true;
1175 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
1176}
1177
1178/* The I/O status is only enabled if the drive explicitly
1179 * enables it _and_ the VM is configured to stop on errors */
1180bool blk_iostatus_is_enabled(const BlockBackend *blk)
1181{
37868b2a 1182 IO_CODE();
373340b2
HR
1183 return (blk->iostatus_enabled &&
1184 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
1185 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
1186 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
1187}
1188
1189BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
1190{
0439c5a4 1191 GLOBAL_STATE_CODE();
373340b2
HR
1192 return blk->iostatus;
1193}
1194
1195void blk_iostatus_disable(BlockBackend *blk)
1196{
0439c5a4 1197 GLOBAL_STATE_CODE();
373340b2
HR
1198 blk->iostatus_enabled = false;
1199}
1200
1201void blk_iostatus_reset(BlockBackend *blk)
1202{
0439c5a4 1203 GLOBAL_STATE_CODE();
373340b2
HR
1204 if (blk_iostatus_is_enabled(blk)) {
1205 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
373340b2
HR
1206 }
1207}
1208
1209void blk_iostatus_set_err(BlockBackend *blk, int error)
1210{
37868b2a 1211 IO_CODE();
373340b2
HR
1212 assert(blk_iostatus_is_enabled(blk));
1213 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
1214 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
1215 BLOCK_DEVICE_IO_STATUS_FAILED;
1216 }
4be74634
MA
1217}
1218
c10c9d96
KW
1219void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
1220{
37868b2a 1221 IO_CODE();
c10c9d96
KW
1222 blk->allow_write_beyond_eof = allow;
1223}
1224
980b0f94
KW
1225void blk_set_allow_aio_context_change(BlockBackend *blk, bool allow)
1226{
37868b2a 1227 IO_CODE();
980b0f94
KW
1228 blk->allow_aio_context_change = allow;
1229}
1230
cf312932
KW
1231void blk_set_disable_request_queuing(BlockBackend *blk, bool disable)
1232{
37868b2a 1233 IO_CODE();
cf312932
KW
1234 blk->disable_request_queuing = disable;
1235}
1236
e7f7d676 1237static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
7242db63 1238 int64_t bytes)
e7f7d676
HR
1239{
1240 int64_t len;
1241
aa78b825 1242 if (bytes < 0) {
e7f7d676
HR
1243 return -EIO;
1244 }
1245
c09ba36c 1246 if (!blk_is_available(blk)) {
e7f7d676
HR
1247 return -ENOMEDIUM;
1248 }
1249
e7f7d676
HR
1250 if (offset < 0) {
1251 return -EIO;
1252 }
1253
c10c9d96
KW
1254 if (!blk->allow_write_beyond_eof) {
1255 len = blk_getlength(blk);
1256 if (len < 0) {
1257 return len;
1258 }
1259
7242db63 1260 if (offset > len || len - offset < bytes) {
c10c9d96
KW
1261 return -EIO;
1262 }
e7f7d676
HR
1263 }
1264
1265 return 0;
1266}
1267
7f16476f 1268/* To be called between exactly one pair of blk_inc/dec_in_flight() */
cf312932
KW
1269static void coroutine_fn blk_wait_while_drained(BlockBackend *blk)
1270{
7f16476f
KW
1271 assert(blk->in_flight > 0);
1272
cf312932 1273 if (blk->quiesce_counter && !blk->disable_request_queuing) {
7f16476f 1274 blk_dec_in_flight(blk);
cf312932 1275 qemu_co_queue_wait(&blk->queued_requests, NULL);
7f16476f 1276 blk_inc_in_flight(blk);
cf312932
KW
1277 }
1278}
1279
fbb92b67 1280/* To be called between exactly one pair of blk_inc/dec_in_flight() */
d1d3fc3d
AF
1281static int coroutine_fn
1282blk_co_do_preadv_part(BlockBackend *blk, int64_t offset, int64_t bytes,
1283 QEMUIOVector *qiov, size_t qiov_offset,
1284 BdrvRequestFlags flags)
4be74634 1285{
1e98fefd 1286 int ret;
cf312932 1287 BlockDriverState *bs;
1581a70d 1288 IO_CODE();
1e98fefd 1289
cf312932 1290 blk_wait_while_drained(blk);
1e98fefd 1291
cf312932
KW
1292 /* Call blk_bs() only after waiting, the graph may have changed */
1293 bs = blk_bs(blk);
99723548 1294 trace_blk_co_preadv(blk, bs, offset, bytes, flags);
1e98fefd
KW
1295
1296 ret = blk_check_byte_request(blk, offset, bytes);
e7f7d676
HR
1297 if (ret < 0) {
1298 return ret;
1299 }
1300
99723548
PB
1301 bdrv_inc_in_flight(bs);
1302
441565b2 1303 /* throttling disk I/O */
022cdc9f
MP
1304 if (blk->public.throttle_group_member.throttle_state) {
1305 throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1306 bytes, false);
441565b2
KW
1307 }
1308
d1d3fc3d
AF
1309 ret = bdrv_co_preadv_part(blk->root, offset, bytes, qiov, qiov_offset,
1310 flags);
99723548
PB
1311 bdrv_dec_in_flight(bs);
1312 return ret;
1bf1cbc9
KW
1313}
1314
6f675c93
AF
1315int coroutine_fn blk_co_pread(BlockBackend *blk, int64_t offset, int64_t bytes,
1316 void *buf, BdrvRequestFlags flags)
1317{
1318 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
1319 IO_OR_GS_CODE();
1320
1321 assert(bytes <= SIZE_MAX);
1322
1323 return blk_co_preadv(blk, offset, bytes, &qiov, flags);
1324}
1325
fbb92b67 1326int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
95479077 1327 int64_t bytes, QEMUIOVector *qiov,
fbb92b67
KW
1328 BdrvRequestFlags flags)
1329{
1330 int ret;
37868b2a 1331 IO_OR_GS_CODE();
fbb92b67
KW
1332
1333 blk_inc_in_flight(blk);
d1d3fc3d
AF
1334 ret = blk_co_do_preadv_part(blk, offset, bytes, qiov, 0, flags);
1335 blk_dec_in_flight(blk);
1336
1337 return ret;
1338}
1339
1340int coroutine_fn blk_co_preadv_part(BlockBackend *blk, int64_t offset,
1341 int64_t bytes, QEMUIOVector *qiov,
1342 size_t qiov_offset, BdrvRequestFlags flags)
1343{
1344 int ret;
1345 IO_OR_GS_CODE();
1346
1347 blk_inc_in_flight(blk);
1348 ret = blk_co_do_preadv_part(blk, offset, bytes, qiov, qiov_offset, flags);
fbb92b67
KW
1349 blk_dec_in_flight(blk);
1350
1351 return ret;
1352}
1353
1354/* To be called between exactly one pair of blk_inc/dec_in_flight() */
07a64aa4 1355static int coroutine_fn
70e8775e
VSO
1356blk_co_do_pwritev_part(BlockBackend *blk, int64_t offset, int64_t bytes,
1357 QEMUIOVector *qiov, size_t qiov_offset,
1358 BdrvRequestFlags flags)
a8823a3b 1359{
bfd18d1e 1360 int ret;
cf312932 1361 BlockDriverState *bs;
1581a70d 1362 IO_CODE();
bfd18d1e 1363
cf312932 1364 blk_wait_while_drained(blk);
bfd18d1e 1365
cf312932
KW
1366 /* Call blk_bs() only after waiting, the graph may have changed */
1367 bs = blk_bs(blk);
99723548 1368 trace_blk_co_pwritev(blk, bs, offset, bytes, flags);
1e98fefd 1369
bfd18d1e 1370 ret = blk_check_byte_request(blk, offset, bytes);
a8823a3b
KW
1371 if (ret < 0) {
1372 return ret;
1373 }
1374
99723548 1375 bdrv_inc_in_flight(bs);
441565b2 1376 /* throttling disk I/O */
022cdc9f
MP
1377 if (blk->public.throttle_group_member.throttle_state) {
1378 throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1379 bytes, true);
441565b2
KW
1380 }
1381
bfd18d1e
KW
1382 if (!blk->enable_write_cache) {
1383 flags |= BDRV_REQ_FUA;
1384 }
1385
b3016864
VSO
1386 ret = bdrv_co_pwritev_part(blk->root, offset, bytes, qiov, qiov_offset,
1387 flags);
99723548
PB
1388 bdrv_dec_in_flight(bs);
1389 return ret;
a8823a3b
KW
1390}
1391
fbb92b67 1392int coroutine_fn blk_co_pwritev_part(BlockBackend *blk, int64_t offset,
34460feb 1393 int64_t bytes,
fbb92b67
KW
1394 QEMUIOVector *qiov, size_t qiov_offset,
1395 BdrvRequestFlags flags)
1396{
1397 int ret;
37868b2a 1398 IO_OR_GS_CODE();
fbb92b67
KW
1399
1400 blk_inc_in_flight(blk);
70e8775e 1401 ret = blk_co_do_pwritev_part(blk, offset, bytes, qiov, qiov_offset, flags);
fbb92b67
KW
1402 blk_dec_in_flight(blk);
1403
1404 return ret;
1405}
1406
6f675c93
AF
1407int coroutine_fn blk_co_pwrite(BlockBackend *blk, int64_t offset, int64_t bytes,
1408 const void *buf, BdrvRequestFlags flags)
1409{
1410 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
1411 IO_OR_GS_CODE();
1412
1413 assert(bytes <= SIZE_MAX);
1414
1415 return blk_co_pwritev(blk, offset, bytes, &qiov, flags);
1416}
1417
b3016864 1418int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
34460feb 1419 int64_t bytes, QEMUIOVector *qiov,
b3016864
VSO
1420 BdrvRequestFlags flags)
1421{
37868b2a 1422 IO_OR_GS_CODE();
b3016864
VSO
1423 return blk_co_pwritev_part(blk, offset, bytes, qiov, 0, flags);
1424}
1425
1bf1cbc9
KW
1426typedef struct BlkRwCo {
1427 BlockBackend *blk;
1428 int64_t offset;
c060332c 1429 void *iobuf;
1bf1cbc9
KW
1430 int ret;
1431 BdrvRequestFlags flags;
1432} BlkRwCo;
1433
720ff280
KW
1434int blk_make_zero(BlockBackend *blk, BdrvRequestFlags flags)
1435{
0439c5a4 1436 GLOBAL_STATE_CODE();
720ff280
KW
1437 return bdrv_make_zero(blk->root, flags);
1438}
1439
c90e2a9c 1440void blk_inc_in_flight(BlockBackend *blk)
33f2a757 1441{
37868b2a 1442 IO_CODE();
d73415a3 1443 qatomic_inc(&blk->in_flight);
33f2a757
SH
1444}
1445
c90e2a9c 1446void blk_dec_in_flight(BlockBackend *blk)
33f2a757 1447{
37868b2a 1448 IO_CODE();
d73415a3 1449 qatomic_dec(&blk->in_flight);
cfe29d82 1450 aio_wait_kick();
33f2a757
SH
1451}
1452
e7f7d676
HR
1453static void error_callback_bh(void *opaque)
1454{
1455 struct BlockBackendAIOCB *acb = opaque;
99723548 1456
33f2a757 1457 blk_dec_in_flight(acb->blk);
e7f7d676
HR
1458 acb->common.cb(acb->common.opaque, acb->ret);
1459 qemu_aio_unref(acb);
1460}
1461
ca78ecfa
PL
1462BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
1463 BlockCompletionFunc *cb,
1464 void *opaque, int ret)
e7f7d676
HR
1465{
1466 struct BlockBackendAIOCB *acb;
37868b2a 1467 IO_CODE();
e7f7d676 1468
33f2a757 1469 blk_inc_in_flight(blk);
e7f7d676 1470 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
4981bdec 1471 acb->blk = blk;
e7f7d676
HR
1472 acb->ret = ret;
1473
e4ec5ad4
PD
1474 replay_bh_schedule_oneshot_event(blk_get_aio_context(blk),
1475 error_callback_bh, acb);
e7f7d676
HR
1476 return &acb->common;
1477}
1478
57d6a428
KW
1479typedef struct BlkAioEmAIOCB {
1480 BlockAIOCB common;
1481 BlkRwCo rwco;
a93d81c8 1482 int64_t bytes;
57d6a428 1483 bool has_returned;
57d6a428
KW
1484} BlkAioEmAIOCB;
1485
1d719ddc
SH
1486static AioContext *blk_aio_em_aiocb_get_aio_context(BlockAIOCB *acb_)
1487{
1488 BlkAioEmAIOCB *acb = container_of(acb_, BlkAioEmAIOCB, common);
1489
1490 return blk_get_aio_context(acb->rwco.blk);
1491}
1492
57d6a428
KW
1493static const AIOCBInfo blk_aio_em_aiocb_info = {
1494 .aiocb_size = sizeof(BlkAioEmAIOCB),
1d719ddc 1495 .get_aio_context = blk_aio_em_aiocb_get_aio_context,
57d6a428
KW
1496};
1497
1498static void blk_aio_complete(BlkAioEmAIOCB *acb)
1499{
57d6a428
KW
1500 if (acb->has_returned) {
1501 acb->common.cb(acb->common.opaque, acb->rwco.ret);
46aaf2a5 1502 blk_dec_in_flight(acb->rwco.blk);
57d6a428
KW
1503 qemu_aio_unref(acb);
1504 }
1505}
1506
1507static void blk_aio_complete_bh(void *opaque)
1508{
fffb6e12 1509 BlkAioEmAIOCB *acb = opaque;
fffb6e12
PB
1510 assert(acb->has_returned);
1511 blk_aio_complete(acb);
57d6a428
KW
1512}
1513
a93d81c8
VSO
1514static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset,
1515 int64_t bytes,
c060332c 1516 void *iobuf, CoroutineEntry co_entry,
57d6a428
KW
1517 BdrvRequestFlags flags,
1518 BlockCompletionFunc *cb, void *opaque)
1519{
1520 BlkAioEmAIOCB *acb;
1521 Coroutine *co;
1522
33f2a757 1523 blk_inc_in_flight(blk);
57d6a428
KW
1524 acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
1525 acb->rwco = (BlkRwCo) {
1526 .blk = blk,
1527 .offset = offset,
c060332c 1528 .iobuf = iobuf,
57d6a428
KW
1529 .flags = flags,
1530 .ret = NOT_DONE,
1531 };
7fa84cd8 1532 acb->bytes = bytes;
57d6a428
KW
1533 acb->has_returned = false;
1534
0b8b8753 1535 co = qemu_coroutine_create(co_entry, acb);
e92f0e19 1536 bdrv_coroutine_enter(blk_bs(blk), co);
57d6a428
KW
1537
1538 acb->has_returned = true;
1539 if (acb->rwco.ret != NOT_DONE) {
e4ec5ad4
PD
1540 replay_bh_schedule_oneshot_event(blk_get_aio_context(blk),
1541 blk_aio_complete_bh, acb);
57d6a428
KW
1542 }
1543
1544 return &acb->common;
1545}
1546
881a4c55 1547static void coroutine_fn blk_aio_read_entry(void *opaque)
57d6a428
KW
1548{
1549 BlkAioEmAIOCB *acb = opaque;
1550 BlkRwCo *rwco = &acb->rwco;
c060332c 1551 QEMUIOVector *qiov = rwco->iobuf;
57d6a428 1552
c060332c 1553 assert(qiov->size == acb->bytes);
d1d3fc3d
AF
1554 rwco->ret = blk_co_do_preadv_part(rwco->blk, rwco->offset, acb->bytes, qiov,
1555 0, rwco->flags);
57d6a428
KW
1556 blk_aio_complete(acb);
1557}
1558
881a4c55 1559static void coroutine_fn blk_aio_write_entry(void *opaque)
57d6a428
KW
1560{
1561 BlkAioEmAIOCB *acb = opaque;
1562 BlkRwCo *rwco = &acb->rwco;
c060332c 1563 QEMUIOVector *qiov = rwco->iobuf;
57d6a428 1564
c060332c 1565 assert(!qiov || qiov->size == acb->bytes);
70e8775e
VSO
1566 rwco->ret = blk_co_do_pwritev_part(rwco->blk, rwco->offset, acb->bytes,
1567 qiov, 0, rwco->flags);
57d6a428
KW
1568 blk_aio_complete(acb);
1569}
1570
d004bd52 1571BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
a93d81c8 1572 int64_t bytes, BdrvRequestFlags flags,
d004bd52 1573 BlockCompletionFunc *cb, void *opaque)
4be74634 1574{
37868b2a 1575 IO_CODE();
a93d81c8 1576 return blk_aio_prwv(blk, offset, bytes, NULL, blk_aio_write_entry,
983a1600 1577 flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
4be74634
MA
1578}
1579
4be74634
MA
1580int64_t blk_getlength(BlockBackend *blk)
1581{
37868b2a 1582 IO_CODE();
c09ba36c
HR
1583 if (!blk_is_available(blk)) {
1584 return -ENOMEDIUM;
1585 }
1586
f21d96d0 1587 return bdrv_getlength(blk_bs(blk));
4be74634
MA
1588}
1589
1590void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
1591{
37868b2a 1592 IO_CODE();
f21d96d0 1593 if (!blk_bs(blk)) {
a46fc9c9
HR
1594 *nb_sectors_ptr = 0;
1595 } else {
f21d96d0 1596 bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
a46fc9c9 1597 }
4be74634
MA
1598}
1599
1ef01253
HR
1600int64_t blk_nb_sectors(BlockBackend *blk)
1601{
37868b2a 1602 IO_CODE();
c09ba36c
HR
1603 if (!blk_is_available(blk)) {
1604 return -ENOMEDIUM;
1605 }
1606
f21d96d0 1607 return bdrv_nb_sectors(blk_bs(blk));
1ef01253
HR
1608}
1609
60cb2fa7
EB
1610BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
1611 QEMUIOVector *qiov, BdrvRequestFlags flags,
1612 BlockCompletionFunc *cb, void *opaque)
1613{
37868b2a 1614 IO_CODE();
a93d81c8 1615 assert((uint64_t)qiov->size <= INT64_MAX);
60cb2fa7
EB
1616 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1617 blk_aio_read_entry, flags, cb, opaque);
1618}
1619
60cb2fa7
EB
1620BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
1621 QEMUIOVector *qiov, BdrvRequestFlags flags,
1622 BlockCompletionFunc *cb, void *opaque)
1623{
37868b2a 1624 IO_CODE();
73d4a113 1625 assert((uint64_t)qiov->size <= INT64_MAX);
60cb2fa7
EB
1626 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1627 blk_aio_write_entry, flags, cb, opaque);
1628}
1629
4be74634
MA
1630void blk_aio_cancel(BlockAIOCB *acb)
1631{
0439c5a4 1632 GLOBAL_STATE_CODE();
4be74634
MA
1633 bdrv_aio_cancel(acb);
1634}
1635
1636void blk_aio_cancel_async(BlockAIOCB *acb)
1637{
37868b2a 1638 IO_CODE();
4be74634
MA
1639 bdrv_aio_cancel_async(acb);
1640}
1641
fbb92b67 1642/* To be called between exactly one pair of blk_inc/dec_in_flight() */
df02da00 1643static int coroutine_fn
70e8775e 1644blk_co_do_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
4be74634 1645{
1581a70d
EGE
1646 IO_CODE();
1647
cf312932
KW
1648 blk_wait_while_drained(blk);
1649
c09ba36c
HR
1650 if (!blk_is_available(blk)) {
1651 return -ENOMEDIUM;
1652 }
1653
48af776a
KW
1654 return bdrv_co_ioctl(blk_bs(blk), req, buf);
1655}
1656
df02da00
AF
1657int coroutine_fn blk_co_ioctl(BlockBackend *blk, unsigned long int req,
1658 void *buf)
48af776a 1659{
16d36e29 1660 int ret;
37868b2a 1661 IO_OR_GS_CODE();
c060332c 1662
16d36e29 1663 blk_inc_in_flight(blk);
df02da00 1664 ret = blk_co_do_ioctl(blk, req, buf);
16d36e29 1665 blk_dec_in_flight(blk);
48af776a 1666
16d36e29 1667 return ret;
48af776a
KW
1668}
1669
881a4c55 1670static void coroutine_fn blk_aio_ioctl_entry(void *opaque)
48af776a
KW
1671{
1672 BlkAioEmAIOCB *acb = opaque;
1673 BlkRwCo *rwco = &acb->rwco;
1674
70e8775e 1675 rwco->ret = blk_co_do_ioctl(rwco->blk, rwco->offset, rwco->iobuf);
c060332c 1676
48af776a 1677 blk_aio_complete(acb);
4be74634
MA
1678}
1679
1680BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
1681 BlockCompletionFunc *cb, void *opaque)
1682{
37868b2a 1683 IO_CODE();
c060332c 1684 return blk_aio_prwv(blk, req, 0, buf, blk_aio_ioctl_entry, 0, cb, opaque);
4be74634
MA
1685}
1686
fbb92b67 1687/* To be called between exactly one pair of blk_inc/dec_in_flight() */
07a64aa4 1688static int coroutine_fn
70e8775e 1689blk_co_do_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes)
2bb0dce7 1690{
cf312932 1691 int ret;
1581a70d 1692 IO_CODE();
cf312932
KW
1693
1694 blk_wait_while_drained(blk);
1695
1696 ret = blk_check_byte_request(blk, offset, bytes);
e7f7d676
HR
1697 if (ret < 0) {
1698 return ret;
1699 }
1700
0b9fd3f4 1701 return bdrv_co_pdiscard(blk->root, offset, bytes);
2bb0dce7
HR
1702}
1703
881a4c55 1704static void coroutine_fn blk_aio_pdiscard_entry(void *opaque)
564806c5
KW
1705{
1706 BlkAioEmAIOCB *acb = opaque;
1707 BlkRwCo *rwco = &acb->rwco;
1708
70e8775e 1709 rwco->ret = blk_co_do_pdiscard(rwco->blk, rwco->offset, acb->bytes);
564806c5
KW
1710 blk_aio_complete(acb);
1711}
1712
1713BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk,
a93d81c8 1714 int64_t offset, int64_t bytes,
564806c5
KW
1715 BlockCompletionFunc *cb, void *opaque)
1716{
37868b2a 1717 IO_CODE();
564806c5
KW
1718 return blk_aio_prwv(blk, offset, bytes, NULL, blk_aio_pdiscard_entry, 0,
1719 cb, opaque);
1720}
1721
2800637a
VSO
1722int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset,
1723 int64_t bytes)
fbb92b67
KW
1724{
1725 int ret;
37868b2a 1726 IO_OR_GS_CODE();
fbb92b67
KW
1727
1728 blk_inc_in_flight(blk);
70e8775e 1729 ret = blk_co_do_pdiscard(blk, offset, bytes);
fbb92b67
KW
1730 blk_dec_in_flight(blk);
1731
1732 return ret;
1733}
1734
fbb92b67 1735/* To be called between exactly one pair of blk_inc/dec_in_flight() */
07a64aa4 1736static int coroutine_fn blk_co_do_flush(BlockBackend *blk)
2bb0dce7 1737{
cf312932 1738 blk_wait_while_drained(blk);
1581a70d 1739 IO_CODE();
cf312932 1740
c09ba36c
HR
1741 if (!blk_is_available(blk)) {
1742 return -ENOMEDIUM;
1743 }
1744
f21d96d0 1745 return bdrv_co_flush(blk_bs(blk));
2bb0dce7
HR
1746}
1747
881a4c55 1748static void coroutine_fn blk_aio_flush_entry(void *opaque)
564806c5
KW
1749{
1750 BlkAioEmAIOCB *acb = opaque;
1751 BlkRwCo *rwco = &acb->rwco;
1752
70e8775e 1753 rwco->ret = blk_co_do_flush(rwco->blk);
564806c5
KW
1754 blk_aio_complete(acb);
1755}
1756
1757BlockAIOCB *blk_aio_flush(BlockBackend *blk,
1758 BlockCompletionFunc *cb, void *opaque)
1759{
37868b2a 1760 IO_CODE();
564806c5
KW
1761 return blk_aio_prwv(blk, 0, 0, NULL, blk_aio_flush_entry, 0, cb, opaque);
1762}
1763
fbb92b67
KW
1764int coroutine_fn blk_co_flush(BlockBackend *blk)
1765{
1766 int ret;
37868b2a 1767 IO_OR_GS_CODE();
fbb92b67
KW
1768
1769 blk_inc_in_flight(blk);
70e8775e 1770 ret = blk_co_do_flush(blk);
fbb92b67
KW
1771 blk_dec_in_flight(blk);
1772
1773 return ret;
1774}
1775
97b0385a
AY
1776void blk_drain(BlockBackend *blk)
1777{
33f2a757 1778 BlockDriverState *bs = blk_bs(blk);
0439c5a4 1779 GLOBAL_STATE_CODE();
33f2a757
SH
1780
1781 if (bs) {
1e3552db 1782 bdrv_ref(bs);
33f2a757
SH
1783 bdrv_drained_begin(bs);
1784 }
1785
1786 /* We may have -ENOMEDIUM completions in flight */
cfe29d82 1787 AIO_WAIT_WHILE(blk_get_aio_context(blk),
d73415a3 1788 qatomic_mb_read(&blk->in_flight) > 0);
33f2a757
SH
1789
1790 if (bs) {
1791 bdrv_drained_end(bs);
1e3552db 1792 bdrv_unref(bs);
a46fc9c9 1793 }
97b0385a
AY
1794}
1795
4be74634
MA
1796void blk_drain_all(void)
1797{
33f2a757
SH
1798 BlockBackend *blk = NULL;
1799
0439c5a4
EGE
1800 GLOBAL_STATE_CODE();
1801
33f2a757
SH
1802 bdrv_drain_all_begin();
1803
1804 while ((blk = blk_all_next(blk)) != NULL) {
1805 AioContext *ctx = blk_get_aio_context(blk);
1806
1807 aio_context_acquire(ctx);
1808
1809 /* We may have -ENOMEDIUM completions in flight */
d73415a3 1810 AIO_WAIT_WHILE(ctx, qatomic_mb_read(&blk->in_flight) > 0);
33f2a757
SH
1811
1812 aio_context_release(ctx);
1813 }
1814
1815 bdrv_drain_all_end();
4be74634
MA
1816}
1817
373340b2
HR
1818void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1819 BlockdevOnError on_write_error)
1820{
0439c5a4 1821 GLOBAL_STATE_CODE();
373340b2
HR
1822 blk->on_read_error = on_read_error;
1823 blk->on_write_error = on_write_error;
1824}
1825
4be74634
MA
1826BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
1827{
37868b2a 1828 IO_CODE();
373340b2 1829 return is_read ? blk->on_read_error : blk->on_write_error;
4be74634
MA
1830}
1831
1832BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
1833 int error)
1834{
373340b2 1835 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
37868b2a 1836 IO_CODE();
373340b2
HR
1837
1838 switch (on_err) {
1839 case BLOCKDEV_ON_ERROR_ENOSPC:
1840 return (error == ENOSPC) ?
1841 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1842 case BLOCKDEV_ON_ERROR_STOP:
1843 return BLOCK_ERROR_ACTION_STOP;
1844 case BLOCKDEV_ON_ERROR_REPORT:
1845 return BLOCK_ERROR_ACTION_REPORT;
1846 case BLOCKDEV_ON_ERROR_IGNORE:
1847 return BLOCK_ERROR_ACTION_IGNORE;
8c398252 1848 case BLOCKDEV_ON_ERROR_AUTO:
373340b2
HR
1849 default:
1850 abort();
1851 }
4be74634
MA
1852}
1853
373340b2
HR
1854static void send_qmp_error_event(BlockBackend *blk,
1855 BlockErrorAction action,
1856 bool is_read, int error)
1857{
1858 IoOperationType optype;
bfe1a14c 1859 BlockDriverState *bs = blk_bs(blk);
373340b2
HR
1860
1861 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
bfe1a14c
KW
1862 qapi_event_send_block_io_error(blk_name(blk), !!bs,
1863 bs ? bdrv_get_node_name(bs) : NULL, optype,
2bf7e10f 1864 action, blk_iostatus_is_enabled(blk),
3ab72385 1865 error == ENOSPC, strerror(error));
373340b2
HR
1866}
1867
1868/* This is done by device models because, while the block layer knows
1869 * about the error, it does not know whether an operation comes from
1870 * the device or the block layer (from a job, for example).
1871 */
4be74634
MA
1872void blk_error_action(BlockBackend *blk, BlockErrorAction action,
1873 bool is_read, int error)
1874{
373340b2 1875 assert(error >= 0);
37868b2a 1876 IO_CODE();
373340b2
HR
1877
1878 if (action == BLOCK_ERROR_ACTION_STOP) {
1879 /* First set the iostatus, so that "info block" returns an iostatus
1880 * that matches the events raised so far (an additional error iostatus
1881 * is fine, but not a lost one).
1882 */
1883 blk_iostatus_set_err(blk, error);
1884
1885 /* Then raise the request to stop the VM and the event.
1886 * qemu_system_vmstop_request_prepare has two effects. First,
1887 * it ensures that the STOP event always comes after the
1888 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1889 * can observe the STOP event and do a "cont" before the STOP
1890 * event is issued, the VM will not stop. In this case, vm_start()
1891 * also ensures that the STOP/RESUME pair of events is emitted.
1892 */
1893 qemu_system_vmstop_request_prepare();
1894 send_qmp_error_event(blk, action, is_read, error);
1895 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1896 } else {
1897 send_qmp_error_event(blk, action, is_read, error);
1898 }
4be74634
MA
1899}
1900
86b1cf32
KW
1901/*
1902 * Returns true if the BlockBackend can support taking write permissions
1903 * (because its root node is not read-only).
1904 */
1905bool blk_supports_write_perm(BlockBackend *blk)
4be74634 1906{
f21d96d0 1907 BlockDriverState *bs = blk_bs(blk);
0439c5a4 1908 GLOBAL_STATE_CODE();
f21d96d0
KW
1909
1910 if (bs) {
86b1cf32 1911 return !bdrv_is_read_only(bs);
061959e8 1912 } else {
260242a8 1913 return blk->root_state.open_flags & BDRV_O_RDWR;
061959e8 1914 }
4be74634
MA
1915}
1916
86b1cf32
KW
1917/*
1918 * Returns true if the BlockBackend can be written to in its current
1919 * configuration (i.e. if write permission have been requested)
1920 */
1921bool blk_is_writable(BlockBackend *blk)
1922{
37868b2a 1923 IO_CODE();
86b1cf32
KW
1924 return blk->perm & BLK_PERM_WRITE;
1925}
1926
96710565 1927bool blk_is_sg(BlockBackend *blk)
4be74634 1928{
f21d96d0 1929 BlockDriverState *bs = blk_bs(blk);
0439c5a4 1930 GLOBAL_STATE_CODE();
f21d96d0
KW
1931
1932 if (!bs) {
96710565 1933 return false;
a46fc9c9
HR
1934 }
1935
f21d96d0 1936 return bdrv_is_sg(bs);
4be74634
MA
1937}
1938
96710565 1939bool blk_enable_write_cache(BlockBackend *blk)
4be74634 1940{
37868b2a 1941 IO_CODE();
bfd18d1e 1942 return blk->enable_write_cache;
4be74634
MA
1943}
1944
1945void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1946{
be8da05b 1947 IO_CODE();
bfd18d1e 1948 blk->enable_write_cache = wce;
4be74634
MA
1949}
1950
3b717194 1951void blk_activate(BlockBackend *blk, Error **errp)
2bb0dce7 1952{
f21d96d0 1953 BlockDriverState *bs = blk_bs(blk);
0439c5a4 1954 GLOBAL_STATE_CODE();
f21d96d0
KW
1955
1956 if (!bs) {
c09ba36c
HR
1957 error_setg(errp, "Device '%s' has no medium", blk->name);
1958 return;
1959 }
1960
a94750d9 1961 bdrv_activate(bs, errp);
2bb0dce7
HR
1962}
1963
e031f750 1964bool blk_is_inserted(BlockBackend *blk)
4be74634 1965{
f21d96d0 1966 BlockDriverState *bs = blk_bs(blk);
37868b2a 1967 IO_CODE();
f21d96d0
KW
1968
1969 return bs && bdrv_is_inserted(bs);
db0284f8
HR
1970}
1971
1972bool blk_is_available(BlockBackend *blk)
1973{
37868b2a 1974 IO_CODE();
db0284f8 1975 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
4be74634
MA
1976}
1977
1978void blk_lock_medium(BlockBackend *blk, bool locked)
1979{
f21d96d0 1980 BlockDriverState *bs = blk_bs(blk);
37868b2a 1981 IO_CODE();
f21d96d0
KW
1982
1983 if (bs) {
1984 bdrv_lock_medium(bs, locked);
a46fc9c9 1985 }
4be74634
MA
1986}
1987
1988void blk_eject(BlockBackend *blk, bool eject_flag)
1989{
f21d96d0 1990 BlockDriverState *bs = blk_bs(blk);
2d76e724 1991 char *id;
37868b2a 1992 IO_CODE();
2d76e724 1993
f21d96d0
KW
1994 if (bs) {
1995 bdrv_eject(bs, eject_flag);
a46fc9c9 1996 }
c47ee043
JS
1997
1998 /* Whether or not we ejected on the backend,
1999 * the frontend experienced a tray event. */
2000 id = blk_get_attached_dev_id(blk);
2001 qapi_event_send_device_tray_moved(blk_name(blk), id,
3ab72385 2002 eject_flag);
c47ee043 2003 g_free(id);
4be74634
MA
2004}
2005
2006int blk_get_flags(BlockBackend *blk)
2007{
f21d96d0 2008 BlockDriverState *bs = blk_bs(blk);
0439c5a4 2009 GLOBAL_STATE_CODE();
f21d96d0
KW
2010
2011 if (bs) {
2012 return bdrv_get_flags(bs);
061959e8
HR
2013 } else {
2014 return blk->root_state.open_flags;
2015 }
4be74634
MA
2016}
2017
4841211e
EB
2018/* Returns the minimum request alignment, in bytes; guaranteed nonzero */
2019uint32_t blk_get_request_alignment(BlockBackend *blk)
2020{
2021 BlockDriverState *bs = blk_bs(blk);
37868b2a 2022 IO_CODE();
4841211e
EB
2023 return bs ? bs->bl.request_alignment : BDRV_SECTOR_SIZE;
2024}
2025
24b36e98
PB
2026/* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */
2027uint64_t blk_get_max_hw_transfer(BlockBackend *blk)
2028{
2029 BlockDriverState *bs = blk_bs(blk);
2030 uint64_t max = INT_MAX;
37868b2a 2031 IO_CODE();
24b36e98
PB
2032
2033 if (bs) {
2034 max = MIN_NON_ZERO(max, bs->bl.max_hw_transfer);
2035 max = MIN_NON_ZERO(max, bs->bl.max_transfer);
2036 }
2037 return ROUND_DOWN(max, blk_get_request_alignment(blk));
2038}
2039
5def6b80
EB
2040/* Returns the maximum transfer length, in bytes; guaranteed nonzero */
2041uint32_t blk_get_max_transfer(BlockBackend *blk)
454057b7 2042{
f21d96d0 2043 BlockDriverState *bs = blk_bs(blk);
b99f7fa0 2044 uint32_t max = INT_MAX;
37868b2a 2045 IO_CODE();
f21d96d0
KW
2046
2047 if (bs) {
b99f7fa0 2048 max = MIN_NON_ZERO(max, bs->bl.max_transfer);
a46fc9c9 2049 }
b99f7fa0 2050 return ROUND_DOWN(max, blk_get_request_alignment(blk));
454057b7
PL
2051}
2052
cc071629
PB
2053int blk_get_max_hw_iov(BlockBackend *blk)
2054{
37868b2a 2055 IO_CODE();
cc071629
PB
2056 return MIN_NON_ZERO(blk->root->bs->bl.max_hw_iov,
2057 blk->root->bs->bl.max_iov);
2058}
2059
648296e0
SH
2060int blk_get_max_iov(BlockBackend *blk)
2061{
37868b2a 2062 IO_CODE();
f21d96d0 2063 return blk->root->bs->bl.max_iov;
648296e0
SH
2064}
2065
f1c17521
PB
2066void *blk_try_blockalign(BlockBackend *blk, size_t size)
2067{
37868b2a 2068 IO_CODE();
f21d96d0 2069 return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
f1c17521
PB
2070}
2071
4be74634
MA
2072void *blk_blockalign(BlockBackend *blk, size_t size)
2073{
37868b2a 2074 IO_CODE();
f21d96d0 2075 return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
4be74634
MA
2076}
2077
2078bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
2079{
f21d96d0 2080 BlockDriverState *bs = blk_bs(blk);
0439c5a4 2081 GLOBAL_STATE_CODE();
f21d96d0
KW
2082
2083 if (!bs) {
a46fc9c9
HR
2084 return false;
2085 }
2086
f21d96d0 2087 return bdrv_op_is_blocked(bs, op, errp);
4be74634
MA
2088}
2089
2090void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
2091{
f21d96d0 2092 BlockDriverState *bs = blk_bs(blk);
0439c5a4 2093 GLOBAL_STATE_CODE();
f21d96d0
KW
2094
2095 if (bs) {
2096 bdrv_op_unblock(bs, op, reason);
a46fc9c9 2097 }
4be74634
MA
2098}
2099
2100void blk_op_block_all(BlockBackend *blk, Error *reason)
2101{
f21d96d0 2102 BlockDriverState *bs = blk_bs(blk);
0439c5a4 2103 GLOBAL_STATE_CODE();
f21d96d0
KW
2104
2105 if (bs) {
2106 bdrv_op_block_all(bs, reason);
a46fc9c9 2107 }
4be74634
MA
2108}
2109
2110void blk_op_unblock_all(BlockBackend *blk, Error *reason)
2111{
f21d96d0 2112 BlockDriverState *bs = blk_bs(blk);
0439c5a4 2113 GLOBAL_STATE_CODE();
f21d96d0
KW
2114
2115 if (bs) {
2116 bdrv_op_unblock_all(bs, reason);
a46fc9c9 2117 }
4be74634
MA
2118}
2119
2120AioContext *blk_get_aio_context(BlockBackend *blk)
2121{
d861ab3a 2122 BlockDriverState *bs = blk_bs(blk);
37868b2a 2123 IO_CODE();
d861ab3a 2124
d861ab3a 2125 if (bs) {
132ada80
KW
2126 AioContext *ctx = bdrv_get_aio_context(blk_bs(blk));
2127 assert(ctx == blk->ctx);
d861ab3a
KW
2128 }
2129
2130 return blk->ctx;
4981bdec
HR
2131}
2132
2133static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
2134{
2135 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
2136 return blk_get_aio_context(blk_acb->blk);
4be74634
MA
2137}
2138
97896a48
KW
2139static int blk_do_set_aio_context(BlockBackend *blk, AioContext *new_context,
2140 bool update_root_node, Error **errp)
4be74634 2141{
f21d96d0 2142 BlockDriverState *bs = blk_bs(blk);
c61791fc 2143 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
97896a48 2144 int ret;
f21d96d0
KW
2145
2146 if (bs) {
1e3552db
SH
2147 bdrv_ref(bs);
2148
97896a48 2149 if (update_root_node) {
f8be48ad
EGE
2150 /*
2151 * update_root_node MUST be false for blk_root_set_aio_ctx_commit(),
2152 * as we are already in the commit function of a transaction.
2153 */
a41cfda1 2154 ret = bdrv_try_change_aio_context(bs, new_context, blk->root, errp);
97896a48 2155 if (ret < 0) {
1e3552db 2156 bdrv_unref(bs);
97896a48
KW
2157 return ret;
2158 }
2159 }
c61791fc 2160 if (tgm->throttle_state) {
dc868fb0 2161 bdrv_drained_begin(bs);
c61791fc
MP
2162 throttle_group_detach_aio_context(tgm);
2163 throttle_group_attach_aio_context(tgm, new_context);
dc868fb0 2164 bdrv_drained_end(bs);
7ca7f0f6 2165 }
1e3552db
SH
2166
2167 bdrv_unref(bs);
a46fc9c9 2168 }
97896a48 2169
d861ab3a 2170 blk->ctx = new_context;
97896a48 2171 return 0;
4be74634
MA
2172}
2173
97896a48
KW
2174int blk_set_aio_context(BlockBackend *blk, AioContext *new_context,
2175 Error **errp)
38475269 2176{
0439c5a4 2177 GLOBAL_STATE_CODE();
97896a48 2178 return blk_do_set_aio_context(blk, new_context, true, errp);
38475269
KW
2179}
2180
33949396
EGE
2181typedef struct BdrvStateBlkRootContext {
2182 AioContext *new_ctx;
2183 BlockBackend *blk;
2184} BdrvStateBlkRootContext;
2185
2186static void blk_root_set_aio_ctx_commit(void *opaque)
2187{
2188 BdrvStateBlkRootContext *s = opaque;
2189 BlockBackend *blk = s->blk;
2190
2191 blk_do_set_aio_context(blk, s->new_ctx, false, &error_abort);
2192}
2193
2194static TransactionActionDrv set_blk_root_context = {
2195 .commit = blk_root_set_aio_ctx_commit,
2196 .clean = g_free,
2197};
2198
2199static bool blk_root_change_aio_ctx(BdrvChild *child, AioContext *ctx,
2200 GHashTable *visited, Transaction *tran,
2201 Error **errp)
38475269
KW
2202{
2203 BlockBackend *blk = child->opaque;
33949396 2204 BdrvStateBlkRootContext *s;
38475269 2205
33949396
EGE
2206 if (!blk->allow_aio_context_change) {
2207 /*
2208 * Manually created BlockBackends (those with a name) that are not
2209 * attached to anything can change their AioContext without updating
2210 * their user; return an error for others.
2211 */
2212 if (!blk->name || blk->dev) {
2213 /* TODO Add BB name/QOM path */
2214 error_setg(errp, "Cannot change iothread of active block backend");
2215 return false;
2216 }
980b0f94
KW
2217 }
2218
33949396
EGE
2219 s = g_new(BdrvStateBlkRootContext, 1);
2220 *s = (BdrvStateBlkRootContext) {
2221 .new_ctx = ctx,
2222 .blk = blk,
2223 };
38475269 2224
33949396 2225 tran_add(tran, &set_blk_root_context, s);
38475269
KW
2226 return true;
2227}
2228
2019ba0a
HR
2229void blk_add_aio_context_notifier(BlockBackend *blk,
2230 void (*attached_aio_context)(AioContext *new_context, void *opaque),
2231 void (*detach_aio_context)(void *opaque), void *opaque)
2232{
d03654ea 2233 BlockBackendAioNotifier *notifier;
f21d96d0 2234 BlockDriverState *bs = blk_bs(blk);
0439c5a4 2235 GLOBAL_STATE_CODE();
f21d96d0 2236
d03654ea
SH
2237 notifier = g_new(BlockBackendAioNotifier, 1);
2238 notifier->attached_aio_context = attached_aio_context;
2239 notifier->detach_aio_context = detach_aio_context;
2240 notifier->opaque = opaque;
2241 QLIST_INSERT_HEAD(&blk->aio_notifiers, notifier, list);
2242
f21d96d0
KW
2243 if (bs) {
2244 bdrv_add_aio_context_notifier(bs, attached_aio_context,
a46fc9c9
HR
2245 detach_aio_context, opaque);
2246 }
2019ba0a
HR
2247}
2248
2249void blk_remove_aio_context_notifier(BlockBackend *blk,
2250 void (*attached_aio_context)(AioContext *,
2251 void *),
2252 void (*detach_aio_context)(void *),
2253 void *opaque)
2254{
d03654ea 2255 BlockBackendAioNotifier *notifier;
f21d96d0
KW
2256 BlockDriverState *bs = blk_bs(blk);
2257
0439c5a4
EGE
2258 GLOBAL_STATE_CODE();
2259
f21d96d0
KW
2260 if (bs) {
2261 bdrv_remove_aio_context_notifier(bs, attached_aio_context,
a46fc9c9
HR
2262 detach_aio_context, opaque);
2263 }
d03654ea
SH
2264
2265 QLIST_FOREACH(notifier, &blk->aio_notifiers, list) {
2266 if (notifier->attached_aio_context == attached_aio_context &&
2267 notifier->detach_aio_context == detach_aio_context &&
2268 notifier->opaque == opaque) {
2269 QLIST_REMOVE(notifier, list);
2270 g_free(notifier);
2271 return;
2272 }
2273 }
2274
2275 abort();
2019ba0a
HR
2276}
2277
3301f6c6
HR
2278void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
2279{
0439c5a4 2280 GLOBAL_STATE_CODE();
3301f6c6
HR
2281 notifier_list_add(&blk->remove_bs_notifiers, notify);
2282}
2283
2284void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
2285{
0439c5a4 2286 GLOBAL_STATE_CODE();
3301f6c6
HR
2287 notifier_list_add(&blk->insert_bs_notifiers, notify);
2288}
2289
4be74634
MA
2290void blk_io_plug(BlockBackend *blk)
2291{
f21d96d0 2292 BlockDriverState *bs = blk_bs(blk);
37868b2a 2293 IO_CODE();
f21d96d0
KW
2294
2295 if (bs) {
2296 bdrv_io_plug(bs);
a46fc9c9 2297 }
4be74634
MA
2298}
2299
2300void blk_io_unplug(BlockBackend *blk)
2301{
f21d96d0 2302 BlockDriverState *bs = blk_bs(blk);
37868b2a 2303 IO_CODE();
f21d96d0
KW
2304
2305 if (bs) {
2306 bdrv_io_unplug(bs);
a46fc9c9 2307 }
4be74634
MA
2308}
2309
2310BlockAcctStats *blk_get_stats(BlockBackend *blk)
2311{
37868b2a 2312 IO_CODE();
7f0e9da6 2313 return &blk->stats;
4be74634
MA
2314}
2315
2316void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
2317 BlockCompletionFunc *cb, void *opaque)
2318{
37868b2a 2319 IO_CODE();
4be74634
MA
2320 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
2321}
1ef01253 2322
d004bd52 2323int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
34460feb 2324 int64_t bytes, BdrvRequestFlags flags)
1ef01253 2325{
37868b2a 2326 IO_OR_GS_CODE();
f5a5ca79 2327 return blk_co_pwritev(blk, offset, bytes, NULL,
16aaf975 2328 flags | BDRV_REQ_ZERO_WRITE);
1ef01253
HR
2329}
2330
2c9715fa
AF
2331int coroutine_fn blk_co_pwrite_compressed(BlockBackend *blk, int64_t offset,
2332 int64_t bytes, const void *buf)
1ef01253 2333{
06f0325c 2334 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
37868b2a 2335 IO_OR_GS_CODE();
2c9715fa
AF
2336 return blk_co_pwritev_part(blk, offset, bytes, &qiov, 0,
2337 BDRV_REQ_WRITE_COMPRESSED);
1ef01253
HR
2338}
2339
015ed252
AF
2340int coroutine_fn blk_co_truncate(BlockBackend *blk, int64_t offset, bool exact,
2341 PreallocMode prealloc, BdrvRequestFlags flags,
2342 Error **errp)
1ef01253 2343{
37868b2a 2344 IO_OR_GS_CODE();
c09ba36c 2345 if (!blk_is_available(blk)) {
ed3d2ec9 2346 error_setg(errp, "No medium inserted");
c09ba36c
HR
2347 return -ENOMEDIUM;
2348 }
2349
015ed252 2350 return bdrv_co_truncate(blk->root, offset, exact, prealloc, flags, errp);
1ef01253
HR
2351}
2352
1ef01253
HR
2353int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
2354 int64_t pos, int size)
2355{
bfd18d1e 2356 int ret;
0439c5a4 2357 GLOBAL_STATE_CODE();
bfd18d1e 2358
c09ba36c
HR
2359 if (!blk_is_available(blk)) {
2360 return -ENOMEDIUM;
2361 }
2362
bfd18d1e
KW
2363 ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
2364 if (ret < 0) {
2365 return ret;
2366 }
2367
2368 if (ret == size && !blk->enable_write_cache) {
2369 ret = bdrv_flush(blk_bs(blk));
2370 }
2371
2372 return ret < 0 ? ret : size;
1ef01253
HR
2373}
2374
2375int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
2376{
0439c5a4 2377 GLOBAL_STATE_CODE();
c09ba36c
HR
2378 if (!blk_is_available(blk)) {
2379 return -ENOMEDIUM;
2380 }
2381
f21d96d0 2382 return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
1ef01253 2383}
f0272c4d
ET
2384
2385int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
2386{
0439c5a4 2387 GLOBAL_STATE_CODE();
c09ba36c
HR
2388 if (!blk_is_available(blk)) {
2389 return -ENOMEDIUM;
2390 }
2391
f21d96d0 2392 return bdrv_probe_blocksizes(blk_bs(blk), bsz);
f0272c4d
ET
2393}
2394
2395int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
2396{
0439c5a4 2397 GLOBAL_STATE_CODE();
c09ba36c
HR
2398 if (!blk_is_available(blk)) {
2399 return -ENOMEDIUM;
2400 }
2401
f21d96d0 2402 return bdrv_probe_geometry(blk_bs(blk), geo);
f0272c4d 2403}
281d22d8
HR
2404
2405/*
2406 * Updates the BlockBackendRootState object with data from the currently
2407 * attached BlockDriverState.
2408 */
2409void blk_update_root_state(BlockBackend *blk)
2410{
0439c5a4 2411 GLOBAL_STATE_CODE();
f21d96d0 2412 assert(blk->root);
281d22d8 2413
f21d96d0 2414 blk->root_state.open_flags = blk->root->bs->open_flags;
f21d96d0 2415 blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
281d22d8
HR
2416}
2417
38cb18f5 2418/*
b85114f8
KW
2419 * Returns the detect-zeroes setting to be used for bdrv_open() of a
2420 * BlockDriverState which is supposed to inherit the root state.
38cb18f5 2421 */
b85114f8 2422bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk)
38cb18f5 2423{
0439c5a4 2424 GLOBAL_STATE_CODE();
b85114f8 2425 return blk->root_state.detect_zeroes;
38cb18f5
HR
2426}
2427
2428/*
2429 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
2430 * supposed to inherit the root state.
2431 */
2432int blk_get_open_flags_from_root_state(BlockBackend *blk)
2433{
0439c5a4 2434 GLOBAL_STATE_CODE();
260242a8 2435 return blk->root_state.open_flags;
38cb18f5
HR
2436}
2437
281d22d8
HR
2438BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
2439{
0439c5a4 2440 GLOBAL_STATE_CODE();
281d22d8
HR
2441 return &blk->root_state;
2442}
1393f212
HR
2443
2444int blk_commit_all(void)
2445{
fe1a9cbc 2446 BlockBackend *blk = NULL;
0439c5a4 2447 GLOBAL_STATE_CODE();
fe1a9cbc
HR
2448
2449 while ((blk = blk_all_next(blk)) != NULL) {
2450 AioContext *aio_context = blk_get_aio_context(blk);
9a71b9de 2451 BlockDriverState *unfiltered_bs = bdrv_skip_filters(blk_bs(blk));
fe1a9cbc
HR
2452
2453 aio_context_acquire(aio_context);
9a71b9de
HR
2454 if (blk_is_inserted(blk) && bdrv_cow_child(unfiltered_bs)) {
2455 int ret;
2456
2457 ret = bdrv_commit(unfiltered_bs);
fe1a9cbc
HR
2458 if (ret < 0) {
2459 aio_context_release(aio_context);
2460 return ret;
2461 }
2462 }
2463 aio_context_release(aio_context);
2464 }
2465 return 0;
2466}
2467
97148076
KW
2468
2469/* throttling disk I/O limits */
2470void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
2471{
0439c5a4 2472 GLOBAL_STATE_CODE();
022cdc9f 2473 throttle_group_config(&blk->public.throttle_group_member, cfg);
97148076
KW
2474}
2475
2476void blk_io_limits_disable(BlockBackend *blk)
2477{
48bf7ea8
AG
2478 BlockDriverState *bs = blk_bs(blk);
2479 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
2480 assert(tgm->throttle_state);
0439c5a4 2481 GLOBAL_STATE_CODE();
48bf7ea8 2482 if (bs) {
1e3552db 2483 bdrv_ref(bs);
48bf7ea8
AG
2484 bdrv_drained_begin(bs);
2485 }
2486 throttle_group_unregister_tgm(tgm);
2487 if (bs) {
2488 bdrv_drained_end(bs);
1e3552db 2489 bdrv_unref(bs);
48bf7ea8 2490 }
97148076
KW
2491}
2492
2493/* should be called before blk_set_io_limits if a limit is set */
2494void blk_io_limits_enable(BlockBackend *blk, const char *group)
2495{
022cdc9f 2496 assert(!blk->public.throttle_group_member.throttle_state);
0439c5a4 2497 GLOBAL_STATE_CODE();
c61791fc
MP
2498 throttle_group_register_tgm(&blk->public.throttle_group_member,
2499 group, blk_get_aio_context(blk));
97148076
KW
2500}
2501
2502void blk_io_limits_update_group(BlockBackend *blk, const char *group)
2503{
0439c5a4 2504 GLOBAL_STATE_CODE();
97148076 2505 /* this BB is not part of any group */
022cdc9f 2506 if (!blk->public.throttle_group_member.throttle_state) {
97148076
KW
2507 return;
2508 }
2509
2510 /* this BB is a part of the same group than the one we want */
022cdc9f
MP
2511 if (!g_strcmp0(throttle_group_get_name(&blk->public.throttle_group_member),
2512 group)) {
97148076
KW
2513 return;
2514 }
2515
2516 /* need to change the group this bs belong to */
2517 blk_io_limits_disable(blk);
2518 blk_io_limits_enable(blk, group);
2519}
c2066af0
KW
2520
2521static void blk_root_drained_begin(BdrvChild *child)
2522{
2523 BlockBackend *blk = child->opaque;
d73415a3 2524 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
c2066af0 2525
f4d9cc88
JS
2526 if (++blk->quiesce_counter == 1) {
2527 if (blk->dev_ops && blk->dev_ops->drained_begin) {
2528 blk->dev_ops->drained_begin(blk->dev_opaque);
2529 }
2530 }
2531
36fe1331
KW
2532 /* Note that blk->root may not be accessible here yet if we are just
2533 * attaching to a BlockDriverState that is drained. Use child instead. */
2534
d73415a3
SH
2535 if (qatomic_fetch_inc(&tgm->io_limits_disabled) == 0) {
2536 throttle_group_restart_tgm(tgm);
c2066af0
KW
2537 }
2538}
2539
fe5258a5
KW
2540static bool blk_root_drained_poll(BdrvChild *child)
2541{
2542 BlockBackend *blk = child->opaque;
095cc4d0 2543 bool busy = false;
fe5258a5 2544 assert(blk->quiesce_counter);
095cc4d0
SL
2545
2546 if (blk->dev_ops && blk->dev_ops->drained_poll) {
2547 busy = blk->dev_ops->drained_poll(blk->dev_opaque);
2548 }
2549 return busy || !!blk->in_flight;
fe5258a5
KW
2550}
2551
e037c09c 2552static void blk_root_drained_end(BdrvChild *child, int *drained_end_counter)
c2066af0
KW
2553{
2554 BlockBackend *blk = child->opaque;
f4d9cc88 2555 assert(blk->quiesce_counter);
c2066af0 2556
022cdc9f 2557 assert(blk->public.throttle_group_member.io_limits_disabled);
d73415a3 2558 qatomic_dec(&blk->public.throttle_group_member.io_limits_disabled);
f4d9cc88
JS
2559
2560 if (--blk->quiesce_counter == 0) {
2561 if (blk->dev_ops && blk->dev_ops->drained_end) {
2562 blk->dev_ops->drained_end(blk->dev_opaque);
2563 }
cf312932
KW
2564 while (qemu_co_enter_next(&blk->queued_requests, NULL)) {
2565 /* Resume all queued requests */
2566 }
f4d9cc88 2567 }
c2066af0 2568}
23d0ba93 2569
f4ec04ba 2570bool blk_register_buf(BlockBackend *blk, void *host, size_t size, Error **errp)
23d0ba93 2571{
0439c5a4 2572 GLOBAL_STATE_CODE();
f4ec04ba 2573 return bdrv_register_buf(blk_bs(blk), host, size, errp);
23d0ba93
FZ
2574}
2575
4f384011 2576void blk_unregister_buf(BlockBackend *blk, void *host, size_t size)
23d0ba93 2577{
0439c5a4 2578 GLOBAL_STATE_CODE();
4f384011 2579 bdrv_unregister_buf(blk_bs(blk), host, size);
23d0ba93 2580}
b5679fa4
FZ
2581
2582int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in,
2583 BlockBackend *blk_out, int64_t off_out,
e192179b 2584 int64_t bytes, BdrvRequestFlags read_flags,
67b51fb9 2585 BdrvRequestFlags write_flags)
b5679fa4
FZ
2586{
2587 int r;
37868b2a
EGE
2588 IO_CODE();
2589
b5679fa4
FZ
2590 r = blk_check_byte_request(blk_in, off_in, bytes);
2591 if (r) {
2592 return r;
2593 }
2594 r = blk_check_byte_request(blk_out, off_out, bytes);
2595 if (r) {
2596 return r;
2597 }
2598 return bdrv_co_copy_range(blk_in->root, off_in,
2599 blk_out->root, off_out,
67b51fb9 2600 bytes, read_flags, write_flags);
b5679fa4 2601}
5d3b4e99
VSO
2602
2603const BdrvChild *blk_root(BlockBackend *blk)
2604{
0439c5a4 2605 GLOBAL_STATE_CODE();
5d3b4e99
VSO
2606 return blk->root;
2607}
2b7bbdbd
HR
2608
2609int blk_make_empty(BlockBackend *blk, Error **errp)
2610{
0439c5a4 2611 GLOBAL_STATE_CODE();
2b7bbdbd
HR
2612 if (!blk_is_available(blk)) {
2613 error_setg(errp, "No medium inserted");
2614 return -ENOMEDIUM;
2615 }
2616
2617 return bdrv_make_empty(blk->root, errp);
2618}