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