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