]> git.proxmox.com Git - mirror_qemu.git/blame - block/block-backend.c
block/io: Quiesce parents between drained_begin/end
[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"
18e46a03 18#include "sysemu/blockdev.h"
373340b2 19#include "sysemu/sysemu.h"
a7f53e26 20#include "qapi-event.h"
f348b6d1 21#include "qemu/id.h"
a7f53e26
MA
22
23/* Number of coroutines to reserve per attached device model */
24#define COROUTINE_POOL_RESERVATION 64
26f54e9a 25
1bf1cbc9
KW
26#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
27
4981bdec
HR
28static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
29
26f54e9a
MA
30struct BlockBackend {
31 char *name;
32 int refcnt;
f21d96d0 33 BdrvChild *root;
26f8b3a8 34 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
2cf22d6a 35 QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */
9492b0b9 36 QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
f2cd875d 37 BlockBackendPublic public;
a7f53e26
MA
38
39 void *dev; /* attached device model, if any */
40 /* TODO change to DeviceState when all users are qdevified */
41 const BlockDevOps *dev_ops;
42 void *dev_opaque;
68e9ec01
HR
43
44 /* the block size for which the guest device expects atomicity */
45 int guest_block_size;
7f0e9da6 46
281d22d8
HR
47 /* If the BDS tree is removed, some of its options are stored here (which
48 * can be used to restore those options in the new BDS on insert) */
49 BlockBackendRootState root_state;
50
bfd18d1e
KW
51 bool enable_write_cache;
52
7f0e9da6
HR
53 /* I/O stats (display with "info blockstats"). */
54 BlockAcctStats stats;
373340b2
HR
55
56 BlockdevOnError on_read_error, on_write_error;
57 bool iostatus_enabled;
58 BlockDeviceIoStatus iostatus;
3301f6c6 59
c10c9d96
KW
60 bool allow_write_beyond_eof;
61
3301f6c6 62 NotifierList remove_bs_notifiers, insert_bs_notifiers;
26f54e9a
MA
63};
64
e7f7d676
HR
65typedef struct BlockBackendAIOCB {
66 BlockAIOCB common;
67 QEMUBH *bh;
4981bdec 68 BlockBackend *blk;
e7f7d676
HR
69 int ret;
70} BlockBackendAIOCB;
71
72static const AIOCBInfo block_backend_aiocb_info = {
4981bdec 73 .get_aio_context = blk_aiocb_get_aio_context,
e7f7d676
HR
74 .aiocb_size = sizeof(BlockBackendAIOCB),
75};
76
8fb3c76c
MA
77static void drive_info_del(DriveInfo *dinfo);
78
2cf22d6a
HR
79/* All BlockBackends */
80static QTAILQ_HEAD(, BlockBackend) block_backends =
81 QTAILQ_HEAD_INITIALIZER(block_backends);
82
9492b0b9
HR
83/* All BlockBackends referenced by the monitor and which are iterated through by
84 * blk_next() */
85static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
86 QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
26f54e9a 87
f21d96d0
KW
88static void blk_root_inherit_options(int *child_flags, QDict *child_options,
89 int parent_flags, QDict *parent_options)
90{
91 /* We're not supposed to call this function for root nodes */
92 abort();
93}
c2066af0
KW
94static void blk_root_drained_begin(BdrvChild *child);
95static void blk_root_drained_end(BdrvChild *child);
f21d96d0
KW
96
97static const BdrvChildRole child_root = {
c2066af0
KW
98 .inherit_options = blk_root_inherit_options,
99
100 .drained_begin = blk_root_drained_begin,
101 .drained_end = blk_root_drained_end,
f21d96d0
KW
102};
103
26f54e9a 104/*
efaa7c4e 105 * Create a new BlockBackend with a reference count of one.
26f54e9a
MA
106 * Store an error through @errp on failure, unless it's null.
107 * Return the new BlockBackend on success, null on failure.
108 */
efaa7c4e 109BlockBackend *blk_new(Error **errp)
26f54e9a
MA
110{
111 BlockBackend *blk;
112
26f54e9a 113 blk = g_new0(BlockBackend, 1);
26f54e9a 114 blk->refcnt = 1;
27ccdd52
KW
115 qemu_co_queue_init(&blk->public.throttled_reqs[0]);
116 qemu_co_queue_init(&blk->public.throttled_reqs[1]);
117
3301f6c6
HR
118 notifier_list_init(&blk->remove_bs_notifiers);
119 notifier_list_init(&blk->insert_bs_notifiers);
27ccdd52 120
2cf22d6a 121 QTAILQ_INSERT_TAIL(&block_backends, blk, link);
26f54e9a
MA
122 return blk;
123}
124
7e7d56d9
MA
125/*
126 * Create a new BlockBackend with a new BlockDriverState attached.
7e7d56d9
MA
127 * Otherwise just like blk_new(), which see.
128 */
efaa7c4e 129BlockBackend *blk_new_with_bs(Error **errp)
7e7d56d9
MA
130{
131 BlockBackend *blk;
132 BlockDriverState *bs;
133
efaa7c4e 134 blk = blk_new(errp);
7e7d56d9
MA
135 if (!blk) {
136 return NULL;
137 }
138
7f06d47e 139 bs = bdrv_new_root();
f21d96d0 140 blk->root = bdrv_root_attach_child(bs, "root", &child_root);
22aa8b24 141 blk->root->opaque = blk;
7e7d56d9
MA
142 bs->blk = blk;
143 return blk;
144}
145
ca49a4fd
HR
146/*
147 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
148 *
149 * Just as with bdrv_open(), after having called this function the reference to
150 * @options belongs to the block layer (even on failure).
151 *
152 * TODO: Remove @filename and @flags; it should be possible to specify a whole
153 * BDS tree just by specifying the @options QDict (or @reference,
154 * alternatively). At the time of adding this function, this is not possible,
155 * though, so callers of this function have to be able to specify @filename and
156 * @flags.
157 */
efaa7c4e
HR
158BlockBackend *blk_new_open(const char *filename, const char *reference,
159 QDict *options, int flags, Error **errp)
ca49a4fd
HR
160{
161 BlockBackend *blk;
162 int ret;
163
efaa7c4e 164 blk = blk_new_with_bs(errp);
ca49a4fd
HR
165 if (!blk) {
166 QDECREF(options);
167 return NULL;
168 }
169
f21d96d0 170 ret = bdrv_open(&blk->root->bs, filename, reference, options, flags, errp);
ca49a4fd
HR
171 if (ret < 0) {
172 blk_unref(blk);
173 return NULL;
174 }
175
72e775c7
KW
176 blk_set_enable_write_cache(blk, true);
177
ca49a4fd
HR
178 return blk;
179}
180
26f54e9a
MA
181static void blk_delete(BlockBackend *blk)
182{
183 assert(!blk->refcnt);
e5e78550 184 assert(!blk->name);
a7f53e26 185 assert(!blk->dev);
f21d96d0 186 if (blk->root) {
13855c6b 187 blk_remove_bs(blk);
7e7d56d9 188 }
3301f6c6
HR
189 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
190 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
281d22d8
HR
191 if (blk->root_state.throttle_state) {
192 g_free(blk->root_state.throttle_group);
193 throttle_group_unref(blk->root_state.throttle_state);
194 }
2cf22d6a 195 QTAILQ_REMOVE(&block_backends, blk, link);
18e46a03 196 drive_info_del(blk->legacy_dinfo);
979e9b03 197 block_acct_cleanup(&blk->stats);
26f54e9a
MA
198 g_free(blk);
199}
200
8fb3c76c
MA
201static void drive_info_del(DriveInfo *dinfo)
202{
203 if (!dinfo) {
204 return;
205 }
206 qemu_opts_del(dinfo->opts);
8fb3c76c
MA
207 g_free(dinfo->serial);
208 g_free(dinfo);
209}
210
f636ae85
AG
211int blk_get_refcnt(BlockBackend *blk)
212{
213 return blk ? blk->refcnt : 0;
214}
215
26f54e9a
MA
216/*
217 * Increment @blk's reference count.
218 * @blk must not be null.
219 */
220void blk_ref(BlockBackend *blk)
221{
222 blk->refcnt++;
223}
224
225/*
226 * Decrement @blk's reference count.
227 * If this drops it to zero, destroy @blk.
228 * For convenience, do nothing if @blk is null.
229 */
230void blk_unref(BlockBackend *blk)
231{
232 if (blk) {
233 assert(blk->refcnt > 0);
234 if (!--blk->refcnt) {
235 blk_delete(blk);
236 }
237 }
238}
239
2cf22d6a
HR
240/*
241 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
242 * ones which are hidden (i.e. are not referenced by the monitor).
243 */
244static BlockBackend *blk_all_next(BlockBackend *blk)
245{
246 return blk ? QTAILQ_NEXT(blk, link)
247 : QTAILQ_FIRST(&block_backends);
248}
249
d8da3cef
HR
250void blk_remove_all_bs(void)
251{
74d1b8fc 252 BlockBackend *blk = NULL;
d8da3cef 253
2cf22d6a 254 while ((blk = blk_all_next(blk)) != NULL) {
d8da3cef
HR
255 AioContext *ctx = blk_get_aio_context(blk);
256
257 aio_context_acquire(ctx);
f21d96d0 258 if (blk->root) {
d8da3cef
HR
259 blk_remove_bs(blk);
260 }
261 aio_context_release(ctx);
262 }
263}
264
26f54e9a 265/*
9492b0b9 266 * Return the monitor-owned BlockBackend after @blk.
26f54e9a
MA
267 * If @blk is null, return the first one.
268 * Else, return @blk's next sibling, which may be null.
269 *
270 * To iterate over all BlockBackends, do
271 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
272 * ...
273 * }
274 */
275BlockBackend *blk_next(BlockBackend *blk)
276{
9492b0b9
HR
277 return blk ? QTAILQ_NEXT(blk, monitor_link)
278 : QTAILQ_FIRST(&monitor_block_backends);
26f54e9a
MA
279}
280
981f4f57
HR
281/*
282 * Iterates over all BlockDriverStates which are attached to a BlockBackend.
283 * This function is for use by bdrv_next().
284 *
285 * @bs must be NULL or a BDS that is attached to a BB.
286 */
287BlockDriverState *blk_next_root_bs(BlockDriverState *bs)
288{
289 BlockBackend *blk;
290
291 if (bs) {
292 assert(bs->blk);
293 blk = bs->blk;
294 } else {
295 blk = NULL;
296 }
297
298 do {
299 blk = blk_all_next(blk);
f21d96d0 300 } while (blk && !blk->root);
981f4f57 301
f21d96d0 302 return blk ? blk->root->bs : NULL;
981f4f57
HR
303}
304
e5e78550
HR
305/*
306 * Add a BlockBackend into the list of backends referenced by the monitor, with
307 * the given @name acting as the handle for the monitor.
308 * Strictly for use by blockdev.c.
309 *
310 * @name must not be null or empty.
311 *
312 * Returns true on success and false on failure. In the latter case, an Error
313 * object is returned through @errp.
314 */
315bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
316{
317 assert(!blk->name);
318 assert(name && name[0]);
319
320 if (!id_wellformed(name)) {
321 error_setg(errp, "Invalid device name");
322 return false;
323 }
324 if (blk_by_name(name)) {
325 error_setg(errp, "Device with id '%s' already exists", name);
326 return false;
327 }
328 if (bdrv_find_node(name)) {
329 error_setg(errp,
330 "Device name '%s' conflicts with an existing node name",
331 name);
332 return false;
333 }
334
335 blk->name = g_strdup(name);
336 QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
337 return true;
338}
339
340/*
341 * Remove a BlockBackend from the list of backends referenced by the monitor.
342 * Strictly for use by blockdev.c.
343 */
344void monitor_remove_blk(BlockBackend *blk)
345{
346 if (!blk->name) {
347 return;
348 }
349
350 QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
351 g_free(blk->name);
352 blk->name = NULL;
353}
354
26f54e9a 355/*
7e7d56d9 356 * Return @blk's name, a non-null string.
e5e78550 357 * Returns an empty string iff @blk is not referenced by the monitor.
26f54e9a
MA
358 */
359const char *blk_name(BlockBackend *blk)
360{
e5e78550 361 return blk->name ?: "";
26f54e9a
MA
362}
363
364/*
365 * Return the BlockBackend with name @name if it exists, else null.
366 * @name must not be null.
367 */
368BlockBackend *blk_by_name(const char *name)
369{
74d1b8fc 370 BlockBackend *blk = NULL;
26f54e9a
MA
371
372 assert(name);
74d1b8fc 373 while ((blk = blk_next(blk)) != NULL) {
26f54e9a
MA
374 if (!strcmp(name, blk->name)) {
375 return blk;
376 }
377 }
378 return NULL;
379}
7e7d56d9
MA
380
381/*
382 * Return the BlockDriverState attached to @blk if any, else null.
383 */
384BlockDriverState *blk_bs(BlockBackend *blk)
385{
f21d96d0 386 return blk->root ? blk->root->bs : NULL;
7e7d56d9
MA
387}
388
18e46a03
MA
389/*
390 * Return @blk's DriveInfo if any, else null.
391 */
392DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
393{
394 return blk->legacy_dinfo;
395}
396
397/*
398 * Set @blk's DriveInfo to @dinfo, and return it.
399 * @blk must not have a DriveInfo set already.
400 * No other BlockBackend may have the same DriveInfo set.
401 */
402DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
403{
404 assert(!blk->legacy_dinfo);
405 return blk->legacy_dinfo = dinfo;
406}
407
408/*
409 * Return the BlockBackend with DriveInfo @dinfo.
410 * It must exist.
411 */
412BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
413{
74d1b8fc 414 BlockBackend *blk = NULL;
18e46a03 415
74d1b8fc 416 while ((blk = blk_next(blk)) != NULL) {
18e46a03
MA
417 if (blk->legacy_dinfo == dinfo) {
418 return blk;
419 }
420 }
421 abort();
422}
423
f2cd875d
KW
424/*
425 * Returns a pointer to the publicly accessible fields of @blk.
426 */
427BlockBackendPublic *blk_get_public(BlockBackend *blk)
428{
429 return &blk->public;
430}
431
432/*
433 * Returns a BlockBackend given the associated @public fields.
434 */
435BlockBackend *blk_by_public(BlockBackendPublic *public)
436{
437 return container_of(public, BlockBackend, public);
438}
439
1c95f7e1
HR
440/*
441 * Disassociates the currently associated BlockDriverState from @blk.
442 */
443void blk_remove_bs(BlockBackend *blk)
444{
f21d96d0 445 assert(blk->root->bs->blk == blk);
13855c6b 446
3301f6c6
HR
447 notifier_list_notify(&blk->remove_bs_notifiers, blk);
448
1c95f7e1 449 blk_update_root_state(blk);
27ccdd52 450 if (blk->public.throttle_state) {
97148076 451 blk_io_limits_disable(blk);
a5614993 452 }
1c95f7e1 453
f21d96d0
KW
454 blk->root->bs->blk = NULL;
455 bdrv_root_unref_child(blk->root);
456 blk->root = NULL;
1c95f7e1
HR
457}
458
0c3c36d6
HR
459/*
460 * Associates a new BlockDriverState with @blk.
461 */
462void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
463{
f21d96d0 464 assert(!blk->root && !bs->blk);
0c3c36d6 465 bdrv_ref(bs);
f21d96d0 466 blk->root = bdrv_root_attach_child(bs, "root", &child_root);
22aa8b24 467 blk->root->opaque = blk;
0c3c36d6 468 bs->blk = blk;
3301f6c6
HR
469
470 notifier_list_notify(&blk->insert_bs_notifiers, blk);
0c3c36d6
HR
471}
472
a7f53e26
MA
473/*
474 * Attach device model @dev to @blk.
475 * Return 0 on success, -EBUSY when a device model is attached already.
476 */
4be74634 477int blk_attach_dev(BlockBackend *blk, void *dev)
a7f53e26 478/* TODO change to DeviceState *dev when all users are qdevified */
4be74634 479{
a7f53e26
MA
480 if (blk->dev) {
481 return -EBUSY;
482 }
84ebe375 483 blk_ref(blk);
a7f53e26 484 blk->dev = dev;
373340b2 485 blk_iostatus_reset(blk);
a7f53e26 486 return 0;
4be74634
MA
487}
488
a7f53e26
MA
489/*
490 * Attach device model @dev to @blk.
491 * @blk must not have a device model attached already.
492 * TODO qdevified devices don't use this, remove when devices are qdevified
493 */
4be74634
MA
494void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
495{
a7f53e26
MA
496 if (blk_attach_dev(blk, dev) < 0) {
497 abort();
498 }
4be74634
MA
499}
500
a7f53e26
MA
501/*
502 * Detach device model @dev from @blk.
503 * @dev must be currently attached to @blk.
504 */
4be74634 505void blk_detach_dev(BlockBackend *blk, void *dev)
a7f53e26 506/* TODO change to DeviceState *dev when all users are qdevified */
4be74634 507{
a7f53e26
MA
508 assert(blk->dev == dev);
509 blk->dev = NULL;
510 blk->dev_ops = NULL;
511 blk->dev_opaque = NULL;
68e9ec01 512 blk->guest_block_size = 512;
84ebe375 513 blk_unref(blk);
4be74634
MA
514}
515
a7f53e26
MA
516/*
517 * Return the device model attached to @blk if any, else null.
518 */
4be74634 519void *blk_get_attached_dev(BlockBackend *blk)
a7f53e26
MA
520/* TODO change to return DeviceState * when all users are qdevified */
521{
522 return blk->dev;
523}
524
525/*
526 * Set @blk's device model callbacks to @ops.
527 * @opaque is the opaque argument to pass to the callbacks.
528 * This is for use by device models.
529 */
530void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
531 void *opaque)
532{
533 blk->dev_ops = ops;
534 blk->dev_opaque = opaque;
535}
536
537/*
538 * Notify @blk's attached device model of media change.
539 * If @load is true, notify of media load.
540 * Else, notify of media eject.
541 * Also send DEVICE_TRAY_MOVED events as appropriate.
542 */
543void blk_dev_change_media_cb(BlockBackend *blk, bool load)
544{
545 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
f1f57066 546 bool tray_was_open, tray_is_open;
a7f53e26 547
f1f57066 548 tray_was_open = blk_dev_is_tray_open(blk);
a7f53e26 549 blk->dev_ops->change_media_cb(blk->dev_opaque, load);
f1f57066
HR
550 tray_is_open = blk_dev_is_tray_open(blk);
551
552 if (tray_was_open != tray_is_open) {
553 qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
554 &error_abort);
a7f53e26
MA
555 }
556 }
557}
558
559/*
560 * Does @blk's attached device model have removable media?
561 * %true if no device model is attached.
562 */
563bool blk_dev_has_removable_media(BlockBackend *blk)
564{
565 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
566}
567
8f3a73bc
HR
568/*
569 * Does @blk's attached device model have a tray?
570 */
571bool blk_dev_has_tray(BlockBackend *blk)
572{
573 return blk->dev_ops && blk->dev_ops->is_tray_open;
574}
575
a7f53e26
MA
576/*
577 * Notify @blk's attached device model of a media eject request.
578 * If @force is true, the medium is about to be yanked out forcefully.
579 */
580void blk_dev_eject_request(BlockBackend *blk, bool force)
4be74634 581{
a7f53e26
MA
582 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
583 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
584 }
4be74634
MA
585}
586
a7f53e26
MA
587/*
588 * Does @blk's attached device model have a tray, and is it open?
589 */
590bool blk_dev_is_tray_open(BlockBackend *blk)
4be74634 591{
8f3a73bc 592 if (blk_dev_has_tray(blk)) {
a7f53e26
MA
593 return blk->dev_ops->is_tray_open(blk->dev_opaque);
594 }
595 return false;
596}
597
598/*
599 * Does @blk's attached device model have the medium locked?
600 * %false if the device model has no such lock.
601 */
602bool blk_dev_is_medium_locked(BlockBackend *blk)
603{
604 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
605 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
606 }
607 return false;
608}
609
610/*
611 * Notify @blk's attached device model of a backend size change.
612 */
613void blk_dev_resize_cb(BlockBackend *blk)
614{
615 if (blk->dev_ops && blk->dev_ops->resize_cb) {
616 blk->dev_ops->resize_cb(blk->dev_opaque);
617 }
618}
619
620void blk_iostatus_enable(BlockBackend *blk)
621{
373340b2
HR
622 blk->iostatus_enabled = true;
623 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
624}
625
626/* The I/O status is only enabled if the drive explicitly
627 * enables it _and_ the VM is configured to stop on errors */
628bool blk_iostatus_is_enabled(const BlockBackend *blk)
629{
630 return (blk->iostatus_enabled &&
631 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
632 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
633 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
634}
635
636BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
637{
638 return blk->iostatus;
639}
640
641void blk_iostatus_disable(BlockBackend *blk)
642{
643 blk->iostatus_enabled = false;
644}
645
646void blk_iostatus_reset(BlockBackend *blk)
647{
648 if (blk_iostatus_is_enabled(blk)) {
f21d96d0 649 BlockDriverState *bs = blk_bs(blk);
373340b2 650 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
f21d96d0
KW
651 if (bs && bs->job) {
652 block_job_iostatus_reset(bs->job);
373340b2
HR
653 }
654 }
655}
656
657void blk_iostatus_set_err(BlockBackend *blk, int error)
658{
659 assert(blk_iostatus_is_enabled(blk));
660 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
661 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
662 BLOCK_DEVICE_IO_STATUS_FAILED;
663 }
4be74634
MA
664}
665
c10c9d96
KW
666void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
667{
668 blk->allow_write_beyond_eof = allow;
669}
670
e7f7d676
HR
671static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
672 size_t size)
673{
674 int64_t len;
675
676 if (size > INT_MAX) {
677 return -EIO;
678 }
679
c09ba36c 680 if (!blk_is_available(blk)) {
e7f7d676
HR
681 return -ENOMEDIUM;
682 }
683
e7f7d676
HR
684 if (offset < 0) {
685 return -EIO;
686 }
687
c10c9d96
KW
688 if (!blk->allow_write_beyond_eof) {
689 len = blk_getlength(blk);
690 if (len < 0) {
691 return len;
692 }
693
694 if (offset > len || len - offset < size) {
695 return -EIO;
696 }
e7f7d676
HR
697 }
698
699 return 0;
700}
701
702static int blk_check_request(BlockBackend *blk, int64_t sector_num,
703 int nb_sectors)
704{
705 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
706 return -EIO;
707 }
708
709 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
710 return -EIO;
711 }
712
713 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
714 nb_sectors * BDRV_SECTOR_SIZE);
715}
716
1bf1cbc9
KW
717static int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
718 unsigned int bytes, QEMUIOVector *qiov,
719 BdrvRequestFlags flags)
4be74634 720{
1bf1cbc9 721 int ret = blk_check_byte_request(blk, offset, bytes);
e7f7d676
HR
722 if (ret < 0) {
723 return ret;
724 }
725
441565b2
KW
726 /* throttling disk I/O */
727 if (blk->public.throttle_state) {
728 throttle_group_co_io_limits_intercept(blk, bytes, false);
729 }
730
cab3a356 731 return bdrv_co_preadv(blk_bs(blk), offset, bytes, qiov, flags);
1bf1cbc9
KW
732}
733
a8823a3b
KW
734static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
735 unsigned int bytes, QEMUIOVector *qiov,
736 BdrvRequestFlags flags)
737{
bfd18d1e
KW
738 int ret;
739
740 ret = blk_check_byte_request(blk, offset, bytes);
a8823a3b
KW
741 if (ret < 0) {
742 return ret;
743 }
744
441565b2
KW
745 /* throttling disk I/O */
746 if (blk->public.throttle_state) {
747 throttle_group_co_io_limits_intercept(blk, bytes, true);
748 }
749
bfd18d1e
KW
750 if (!blk->enable_write_cache) {
751 flags |= BDRV_REQ_FUA;
752 }
753
cab3a356 754 return bdrv_co_pwritev(blk_bs(blk), offset, bytes, qiov, flags);
a8823a3b
KW
755}
756
1bf1cbc9
KW
757typedef struct BlkRwCo {
758 BlockBackend *blk;
759 int64_t offset;
760 QEMUIOVector *qiov;
761 int ret;
762 BdrvRequestFlags flags;
763} BlkRwCo;
764
765static void blk_read_entry(void *opaque)
766{
767 BlkRwCo *rwco = opaque;
768
769 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
770 rwco->qiov, rwco->flags);
771}
772
a8823a3b
KW
773static void blk_write_entry(void *opaque)
774{
775 BlkRwCo *rwco = opaque;
776
777 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
778 rwco->qiov, rwco->flags);
779}
780
a55d3fba
KW
781static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
782 int64_t bytes, CoroutineEntry co_entry,
783 BdrvRequestFlags flags)
1bf1cbc9
KW
784{
785 AioContext *aio_context;
786 QEMUIOVector qiov;
787 struct iovec iov;
788 Coroutine *co;
789 BlkRwCo rwco;
790
1bf1cbc9
KW
791 iov = (struct iovec) {
792 .iov_base = buf,
a55d3fba 793 .iov_len = bytes,
1bf1cbc9
KW
794 };
795 qemu_iovec_init_external(&qiov, &iov, 1);
796
797 rwco = (BlkRwCo) {
798 .blk = blk,
a55d3fba 799 .offset = offset,
1bf1cbc9 800 .qiov = &qiov,
fc1453cd 801 .flags = flags,
1bf1cbc9
KW
802 .ret = NOT_DONE,
803 };
804
a8823a3b 805 co = qemu_coroutine_create(co_entry);
1bf1cbc9
KW
806 qemu_coroutine_enter(co, &rwco);
807
808 aio_context = blk_get_aio_context(blk);
809 while (rwco.ret == NOT_DONE) {
810 aio_poll(aio_context, true);
811 }
812
813 return rwco.ret;
4be74634
MA
814}
815
b7d17f9f
EB
816int blk_pread_unthrottled(BlockBackend *blk, int64_t offset, uint8_t *buf,
817 int count)
4be74634 818{
5bd51196
KW
819 int ret;
820
b7d17f9f 821 ret = blk_check_byte_request(blk, offset, count);
e7f7d676
HR
822 if (ret < 0) {
823 return ret;
824 }
825
c2066af0 826 blk_root_drained_begin(blk->root);
b7d17f9f 827 ret = blk_pread(blk, offset, buf, count);
c2066af0 828 blk_root_drained_end(blk->root);
5bd51196 829 return ret;
4be74634
MA
830}
831
983a1600
EB
832int blk_write_zeroes(BlockBackend *blk, int64_t offset,
833 int count, BdrvRequestFlags flags)
0df89e8e 834{
983a1600
EB
835 return blk_prw(blk, offset, NULL, count, blk_write_entry,
836 flags | BDRV_REQ_ZERO_WRITE);
0df89e8e
KW
837}
838
e7f7d676
HR
839static void error_callback_bh(void *opaque)
840{
841 struct BlockBackendAIOCB *acb = opaque;
842 qemu_bh_delete(acb->bh);
843 acb->common.cb(acb->common.opaque, acb->ret);
844 qemu_aio_unref(acb);
845}
846
ca78ecfa
PL
847BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
848 BlockCompletionFunc *cb,
849 void *opaque, int ret)
e7f7d676
HR
850{
851 struct BlockBackendAIOCB *acb;
852 QEMUBH *bh;
853
854 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
4981bdec 855 acb->blk = blk;
e7f7d676
HR
856 acb->ret = ret;
857
858 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
859 acb->bh = bh;
860 qemu_bh_schedule(bh);
861
862 return &acb->common;
863}
864
57d6a428
KW
865typedef struct BlkAioEmAIOCB {
866 BlockAIOCB common;
867 BlkRwCo rwco;
7fa84cd8 868 int bytes;
57d6a428
KW
869 bool has_returned;
870 QEMUBH* bh;
871} BlkAioEmAIOCB;
872
873static const AIOCBInfo blk_aio_em_aiocb_info = {
874 .aiocb_size = sizeof(BlkAioEmAIOCB),
875};
876
877static void blk_aio_complete(BlkAioEmAIOCB *acb)
878{
879 if (acb->bh) {
880 assert(acb->has_returned);
881 qemu_bh_delete(acb->bh);
882 }
883 if (acb->has_returned) {
884 acb->common.cb(acb->common.opaque, acb->rwco.ret);
885 qemu_aio_unref(acb);
886 }
887}
888
889static void blk_aio_complete_bh(void *opaque)
890{
891 blk_aio_complete(opaque);
892}
893
7fa84cd8 894static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes,
57d6a428
KW
895 QEMUIOVector *qiov, CoroutineEntry co_entry,
896 BdrvRequestFlags flags,
897 BlockCompletionFunc *cb, void *opaque)
898{
899 BlkAioEmAIOCB *acb;
900 Coroutine *co;
901
902 acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
903 acb->rwco = (BlkRwCo) {
904 .blk = blk,
905 .offset = offset,
906 .qiov = qiov,
907 .flags = flags,
908 .ret = NOT_DONE,
909 };
7fa84cd8 910 acb->bytes = bytes;
57d6a428
KW
911 acb->bh = NULL;
912 acb->has_returned = false;
913
914 co = qemu_coroutine_create(co_entry);
915 qemu_coroutine_enter(co, acb);
916
917 acb->has_returned = true;
918 if (acb->rwco.ret != NOT_DONE) {
919 acb->bh = aio_bh_new(blk_get_aio_context(blk), blk_aio_complete_bh, acb);
920 qemu_bh_schedule(acb->bh);
921 }
922
923 return &acb->common;
924}
925
926static void blk_aio_read_entry(void *opaque)
927{
928 BlkAioEmAIOCB *acb = opaque;
929 BlkRwCo *rwco = &acb->rwco;
930
7fa84cd8
KW
931 assert(rwco->qiov->size == acb->bytes);
932 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, acb->bytes,
57d6a428
KW
933 rwco->qiov, rwco->flags);
934 blk_aio_complete(acb);
935}
936
937static void blk_aio_write_entry(void *opaque)
938{
939 BlkAioEmAIOCB *acb = opaque;
940 BlkRwCo *rwco = &acb->rwco;
941
7fa84cd8
KW
942 assert(!rwco->qiov || rwco->qiov->size == acb->bytes);
943 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes,
57d6a428
KW
944 rwco->qiov, rwco->flags);
945 blk_aio_complete(acb);
946}
947
983a1600
EB
948BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t offset,
949 int count, BdrvRequestFlags flags,
4be74634
MA
950 BlockCompletionFunc *cb, void *opaque)
951{
983a1600
EB
952 return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry,
953 flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
4be74634
MA
954}
955
956int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
957{
a55d3fba 958 int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
e7f7d676
HR
959 if (ret < 0) {
960 return ret;
961 }
a55d3fba 962 return count;
4be74634
MA
963}
964
8341f00d
EB
965int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
966 BdrvRequestFlags flags)
4be74634 967{
8341f00d
EB
968 int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
969 flags);
e7f7d676
HR
970 if (ret < 0) {
971 return ret;
972 }
a55d3fba 973 return count;
4be74634
MA
974}
975
976int64_t blk_getlength(BlockBackend *blk)
977{
c09ba36c
HR
978 if (!blk_is_available(blk)) {
979 return -ENOMEDIUM;
980 }
981
f21d96d0 982 return bdrv_getlength(blk_bs(blk));
4be74634
MA
983}
984
985void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
986{
f21d96d0 987 if (!blk_bs(blk)) {
a46fc9c9
HR
988 *nb_sectors_ptr = 0;
989 } else {
f21d96d0 990 bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
a46fc9c9 991 }
4be74634
MA
992}
993
1ef01253
HR
994int64_t blk_nb_sectors(BlockBackend *blk)
995{
c09ba36c
HR
996 if (!blk_is_available(blk)) {
997 return -ENOMEDIUM;
998 }
999
f21d96d0 1000 return bdrv_nb_sectors(blk_bs(blk));
1ef01253
HR
1001}
1002
60cb2fa7
EB
1003BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
1004 QEMUIOVector *qiov, BdrvRequestFlags flags,
1005 BlockCompletionFunc *cb, void *opaque)
1006{
1007 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1008 blk_aio_read_entry, flags, cb, opaque);
1009}
1010
60cb2fa7
EB
1011BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
1012 QEMUIOVector *qiov, BdrvRequestFlags flags,
1013 BlockCompletionFunc *cb, void *opaque)
1014{
1015 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1016 blk_aio_write_entry, flags, cb, opaque);
1017}
1018
4be74634
MA
1019BlockAIOCB *blk_aio_flush(BlockBackend *blk,
1020 BlockCompletionFunc *cb, void *opaque)
1021{
c09ba36c 1022 if (!blk_is_available(blk)) {
ca78ecfa 1023 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
c09ba36c
HR
1024 }
1025
f21d96d0 1026 return bdrv_aio_flush(blk_bs(blk), cb, opaque);
4be74634
MA
1027}
1028
1029BlockAIOCB *blk_aio_discard(BlockBackend *blk,
1030 int64_t sector_num, int nb_sectors,
1031 BlockCompletionFunc *cb, void *opaque)
1032{
e7f7d676
HR
1033 int ret = blk_check_request(blk, sector_num, nb_sectors);
1034 if (ret < 0) {
ca78ecfa 1035 return blk_abort_aio_request(blk, cb, opaque, ret);
e7f7d676
HR
1036 }
1037
f21d96d0 1038 return bdrv_aio_discard(blk_bs(blk), sector_num, nb_sectors, cb, opaque);
4be74634
MA
1039}
1040
1041void blk_aio_cancel(BlockAIOCB *acb)
1042{
1043 bdrv_aio_cancel(acb);
1044}
1045
1046void blk_aio_cancel_async(BlockAIOCB *acb)
1047{
1048 bdrv_aio_cancel_async(acb);
1049}
1050
1051int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
1052{
e7f7d676
HR
1053 int i, ret;
1054
1055 for (i = 0; i < num_reqs; i++) {
1056 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
1057 if (ret < 0) {
1058 return ret;
1059 }
1060 }
1061
f21d96d0 1062 return bdrv_aio_multiwrite(blk_bs(blk), reqs, num_reqs);
4be74634
MA
1063}
1064
1065int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1066{
c09ba36c
HR
1067 if (!blk_is_available(blk)) {
1068 return -ENOMEDIUM;
1069 }
1070
f21d96d0 1071 return bdrv_ioctl(blk_bs(blk), req, buf);
4be74634
MA
1072}
1073
1074BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
1075 BlockCompletionFunc *cb, void *opaque)
1076{
c09ba36c 1077 if (!blk_is_available(blk)) {
ca78ecfa 1078 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
c09ba36c
HR
1079 }
1080
f21d96d0 1081 return bdrv_aio_ioctl(blk_bs(blk), req, buf, cb, opaque);
4be74634
MA
1082}
1083
2bb0dce7
HR
1084int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1085{
e7f7d676
HR
1086 int ret = blk_check_request(blk, sector_num, nb_sectors);
1087 if (ret < 0) {
1088 return ret;
1089 }
1090
f21d96d0 1091 return bdrv_co_discard(blk_bs(blk), sector_num, nb_sectors);
2bb0dce7
HR
1092}
1093
1094int blk_co_flush(BlockBackend *blk)
1095{
c09ba36c
HR
1096 if (!blk_is_available(blk)) {
1097 return -ENOMEDIUM;
1098 }
1099
f21d96d0 1100 return bdrv_co_flush(blk_bs(blk));
2bb0dce7
HR
1101}
1102
4be74634
MA
1103int blk_flush(BlockBackend *blk)
1104{
c09ba36c
HR
1105 if (!blk_is_available(blk)) {
1106 return -ENOMEDIUM;
1107 }
1108
f21d96d0 1109 return bdrv_flush(blk_bs(blk));
4be74634
MA
1110}
1111
97b0385a
AY
1112void blk_drain(BlockBackend *blk)
1113{
f21d96d0
KW
1114 if (blk_bs(blk)) {
1115 bdrv_drain(blk_bs(blk));
a46fc9c9 1116 }
97b0385a
AY
1117}
1118
4be74634
MA
1119void blk_drain_all(void)
1120{
1121 bdrv_drain_all();
1122}
1123
373340b2
HR
1124void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1125 BlockdevOnError on_write_error)
1126{
1127 blk->on_read_error = on_read_error;
1128 blk->on_write_error = on_write_error;
1129}
1130
4be74634
MA
1131BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
1132{
373340b2 1133 return is_read ? blk->on_read_error : blk->on_write_error;
4be74634
MA
1134}
1135
1136BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
1137 int error)
1138{
373340b2
HR
1139 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
1140
1141 switch (on_err) {
1142 case BLOCKDEV_ON_ERROR_ENOSPC:
1143 return (error == ENOSPC) ?
1144 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1145 case BLOCKDEV_ON_ERROR_STOP:
1146 return BLOCK_ERROR_ACTION_STOP;
1147 case BLOCKDEV_ON_ERROR_REPORT:
1148 return BLOCK_ERROR_ACTION_REPORT;
1149 case BLOCKDEV_ON_ERROR_IGNORE:
1150 return BLOCK_ERROR_ACTION_IGNORE;
1151 default:
1152 abort();
1153 }
4be74634
MA
1154}
1155
373340b2
HR
1156static void send_qmp_error_event(BlockBackend *blk,
1157 BlockErrorAction action,
1158 bool is_read, int error)
1159{
1160 IoOperationType optype;
1161
1162 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
1163 qapi_event_send_block_io_error(blk_name(blk), optype, action,
1164 blk_iostatus_is_enabled(blk),
1165 error == ENOSPC, strerror(error),
1166 &error_abort);
1167}
1168
1169/* This is done by device models because, while the block layer knows
1170 * about the error, it does not know whether an operation comes from
1171 * the device or the block layer (from a job, for example).
1172 */
4be74634
MA
1173void blk_error_action(BlockBackend *blk, BlockErrorAction action,
1174 bool is_read, int error)
1175{
373340b2
HR
1176 assert(error >= 0);
1177
1178 if (action == BLOCK_ERROR_ACTION_STOP) {
1179 /* First set the iostatus, so that "info block" returns an iostatus
1180 * that matches the events raised so far (an additional error iostatus
1181 * is fine, but not a lost one).
1182 */
1183 blk_iostatus_set_err(blk, error);
1184
1185 /* Then raise the request to stop the VM and the event.
1186 * qemu_system_vmstop_request_prepare has two effects. First,
1187 * it ensures that the STOP event always comes after the
1188 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1189 * can observe the STOP event and do a "cont" before the STOP
1190 * event is issued, the VM will not stop. In this case, vm_start()
1191 * also ensures that the STOP/RESUME pair of events is emitted.
1192 */
1193 qemu_system_vmstop_request_prepare();
1194 send_qmp_error_event(blk, action, is_read, error);
1195 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1196 } else {
1197 send_qmp_error_event(blk, action, is_read, error);
1198 }
4be74634
MA
1199}
1200
1201int blk_is_read_only(BlockBackend *blk)
1202{
f21d96d0
KW
1203 BlockDriverState *bs = blk_bs(blk);
1204
1205 if (bs) {
1206 return bdrv_is_read_only(bs);
061959e8
HR
1207 } else {
1208 return blk->root_state.read_only;
1209 }
4be74634
MA
1210}
1211
1212int blk_is_sg(BlockBackend *blk)
1213{
f21d96d0
KW
1214 BlockDriverState *bs = blk_bs(blk);
1215
1216 if (!bs) {
a46fc9c9
HR
1217 return 0;
1218 }
1219
f21d96d0 1220 return bdrv_is_sg(bs);
4be74634
MA
1221}
1222
1223int blk_enable_write_cache(BlockBackend *blk)
1224{
bfd18d1e 1225 return blk->enable_write_cache;
4be74634
MA
1226}
1227
1228void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1229{
bfd18d1e 1230 blk->enable_write_cache = wce;
4be74634
MA
1231}
1232
2bb0dce7
HR
1233void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1234{
f21d96d0
KW
1235 BlockDriverState *bs = blk_bs(blk);
1236
1237 if (!bs) {
c09ba36c
HR
1238 error_setg(errp, "Device '%s' has no medium", blk->name);
1239 return;
1240 }
1241
f21d96d0 1242 bdrv_invalidate_cache(bs, errp);
2bb0dce7
HR
1243}
1244
e031f750 1245bool blk_is_inserted(BlockBackend *blk)
4be74634 1246{
f21d96d0
KW
1247 BlockDriverState *bs = blk_bs(blk);
1248
1249 return bs && bdrv_is_inserted(bs);
db0284f8
HR
1250}
1251
1252bool blk_is_available(BlockBackend *blk)
1253{
1254 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
4be74634
MA
1255}
1256
1257void blk_lock_medium(BlockBackend *blk, bool locked)
1258{
f21d96d0
KW
1259 BlockDriverState *bs = blk_bs(blk);
1260
1261 if (bs) {
1262 bdrv_lock_medium(bs, locked);
a46fc9c9 1263 }
4be74634
MA
1264}
1265
1266void blk_eject(BlockBackend *blk, bool eject_flag)
1267{
f21d96d0
KW
1268 BlockDriverState *bs = blk_bs(blk);
1269
1270 if (bs) {
1271 bdrv_eject(bs, eject_flag);
a46fc9c9 1272 }
4be74634
MA
1273}
1274
1275int blk_get_flags(BlockBackend *blk)
1276{
f21d96d0
KW
1277 BlockDriverState *bs = blk_bs(blk);
1278
1279 if (bs) {
1280 return bdrv_get_flags(bs);
061959e8
HR
1281 } else {
1282 return blk->root_state.open_flags;
1283 }
4be74634
MA
1284}
1285
454057b7
PL
1286int blk_get_max_transfer_length(BlockBackend *blk)
1287{
f21d96d0
KW
1288 BlockDriverState *bs = blk_bs(blk);
1289
1290 if (bs) {
1291 return bs->bl.max_transfer_length;
a46fc9c9
HR
1292 } else {
1293 return 0;
1294 }
454057b7
PL
1295}
1296
648296e0
SH
1297int blk_get_max_iov(BlockBackend *blk)
1298{
f21d96d0 1299 return blk->root->bs->bl.max_iov;
648296e0
SH
1300}
1301
4be74634
MA
1302void blk_set_guest_block_size(BlockBackend *blk, int align)
1303{
68e9ec01 1304 blk->guest_block_size = align;
4be74634
MA
1305}
1306
f1c17521
PB
1307void *blk_try_blockalign(BlockBackend *blk, size_t size)
1308{
f21d96d0 1309 return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
f1c17521
PB
1310}
1311
4be74634
MA
1312void *blk_blockalign(BlockBackend *blk, size_t size)
1313{
f21d96d0 1314 return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
4be74634
MA
1315}
1316
1317bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1318{
f21d96d0
KW
1319 BlockDriverState *bs = blk_bs(blk);
1320
1321 if (!bs) {
a46fc9c9
HR
1322 return false;
1323 }
1324
f21d96d0 1325 return bdrv_op_is_blocked(bs, op, errp);
4be74634
MA
1326}
1327
1328void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1329{
f21d96d0
KW
1330 BlockDriverState *bs = blk_bs(blk);
1331
1332 if (bs) {
1333 bdrv_op_unblock(bs, op, reason);
a46fc9c9 1334 }
4be74634
MA
1335}
1336
1337void blk_op_block_all(BlockBackend *blk, Error *reason)
1338{
f21d96d0
KW
1339 BlockDriverState *bs = blk_bs(blk);
1340
1341 if (bs) {
1342 bdrv_op_block_all(bs, reason);
a46fc9c9 1343 }
4be74634
MA
1344}
1345
1346void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1347{
f21d96d0
KW
1348 BlockDriverState *bs = blk_bs(blk);
1349
1350 if (bs) {
1351 bdrv_op_unblock_all(bs, reason);
a46fc9c9 1352 }
4be74634
MA
1353}
1354
1355AioContext *blk_get_aio_context(BlockBackend *blk)
1356{
f21d96d0
KW
1357 BlockDriverState *bs = blk_bs(blk);
1358
1359 if (bs) {
1360 return bdrv_get_aio_context(bs);
4981bdec
HR
1361 } else {
1362 return qemu_get_aio_context();
1363 }
1364}
1365
1366static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1367{
1368 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1369 return blk_get_aio_context(blk_acb->blk);
4be74634
MA
1370}
1371
1372void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1373{
f21d96d0
KW
1374 BlockDriverState *bs = blk_bs(blk);
1375
1376 if (bs) {
1377 bdrv_set_aio_context(bs, new_context);
a46fc9c9 1378 }
4be74634
MA
1379}
1380
2019ba0a
HR
1381void blk_add_aio_context_notifier(BlockBackend *blk,
1382 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1383 void (*detach_aio_context)(void *opaque), void *opaque)
1384{
f21d96d0
KW
1385 BlockDriverState *bs = blk_bs(blk);
1386
1387 if (bs) {
1388 bdrv_add_aio_context_notifier(bs, attached_aio_context,
a46fc9c9
HR
1389 detach_aio_context, opaque);
1390 }
2019ba0a
HR
1391}
1392
1393void blk_remove_aio_context_notifier(BlockBackend *blk,
1394 void (*attached_aio_context)(AioContext *,
1395 void *),
1396 void (*detach_aio_context)(void *),
1397 void *opaque)
1398{
f21d96d0
KW
1399 BlockDriverState *bs = blk_bs(blk);
1400
1401 if (bs) {
1402 bdrv_remove_aio_context_notifier(bs, attached_aio_context,
a46fc9c9
HR
1403 detach_aio_context, opaque);
1404 }
2019ba0a
HR
1405}
1406
3301f6c6
HR
1407void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1408{
1409 notifier_list_add(&blk->remove_bs_notifiers, notify);
1410}
1411
1412void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1413{
1414 notifier_list_add(&blk->insert_bs_notifiers, notify);
1415}
1416
4be74634
MA
1417void blk_io_plug(BlockBackend *blk)
1418{
f21d96d0
KW
1419 BlockDriverState *bs = blk_bs(blk);
1420
1421 if (bs) {
1422 bdrv_io_plug(bs);
a46fc9c9 1423 }
4be74634
MA
1424}
1425
1426void blk_io_unplug(BlockBackend *blk)
1427{
f21d96d0
KW
1428 BlockDriverState *bs = blk_bs(blk);
1429
1430 if (bs) {
1431 bdrv_io_unplug(bs);
a46fc9c9 1432 }
4be74634
MA
1433}
1434
1435BlockAcctStats *blk_get_stats(BlockBackend *blk)
1436{
7f0e9da6 1437 return &blk->stats;
4be74634
MA
1438}
1439
1440void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1441 BlockCompletionFunc *cb, void *opaque)
1442{
1443 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1444}
1ef01253 1445
983a1600
EB
1446int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t offset,
1447 int count, BdrvRequestFlags flags)
1ef01253 1448{
983a1600 1449 return blk_co_pwritev(blk, offset, count, NULL,
16aaf975 1450 flags | BDRV_REQ_ZERO_WRITE);
1ef01253
HR
1451}
1452
1453int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1454 const uint8_t *buf, int nb_sectors)
1455{
e7f7d676
HR
1456 int ret = blk_check_request(blk, sector_num, nb_sectors);
1457 if (ret < 0) {
1458 return ret;
1459 }
1460
f21d96d0 1461 return bdrv_write_compressed(blk_bs(blk), sector_num, buf, nb_sectors);
1ef01253
HR
1462}
1463
1464int blk_truncate(BlockBackend *blk, int64_t offset)
1465{
c09ba36c
HR
1466 if (!blk_is_available(blk)) {
1467 return -ENOMEDIUM;
1468 }
1469
f21d96d0 1470 return bdrv_truncate(blk_bs(blk), offset);
1ef01253
HR
1471}
1472
1473int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1474{
e7f7d676
HR
1475 int ret = blk_check_request(blk, sector_num, nb_sectors);
1476 if (ret < 0) {
1477 return ret;
1478 }
1479
f21d96d0 1480 return bdrv_discard(blk_bs(blk), sector_num, nb_sectors);
1ef01253
HR
1481}
1482
1483int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1484 int64_t pos, int size)
1485{
bfd18d1e
KW
1486 int ret;
1487
c09ba36c
HR
1488 if (!blk_is_available(blk)) {
1489 return -ENOMEDIUM;
1490 }
1491
bfd18d1e
KW
1492 ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
1493 if (ret < 0) {
1494 return ret;
1495 }
1496
1497 if (ret == size && !blk->enable_write_cache) {
1498 ret = bdrv_flush(blk_bs(blk));
1499 }
1500
1501 return ret < 0 ? ret : size;
1ef01253
HR
1502}
1503
1504int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1505{
c09ba36c
HR
1506 if (!blk_is_available(blk)) {
1507 return -ENOMEDIUM;
1508 }
1509
f21d96d0 1510 return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
1ef01253 1511}
f0272c4d
ET
1512
1513int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1514{
c09ba36c
HR
1515 if (!blk_is_available(blk)) {
1516 return -ENOMEDIUM;
1517 }
1518
f21d96d0 1519 return bdrv_probe_blocksizes(blk_bs(blk), bsz);
f0272c4d
ET
1520}
1521
1522int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1523{
c09ba36c
HR
1524 if (!blk_is_available(blk)) {
1525 return -ENOMEDIUM;
1526 }
1527
f21d96d0 1528 return bdrv_probe_geometry(blk_bs(blk), geo);
f0272c4d 1529}
281d22d8
HR
1530
1531/*
1532 * Updates the BlockBackendRootState object with data from the currently
1533 * attached BlockDriverState.
1534 */
1535void blk_update_root_state(BlockBackend *blk)
1536{
f21d96d0 1537 assert(blk->root);
281d22d8 1538
f21d96d0
KW
1539 blk->root_state.open_flags = blk->root->bs->open_flags;
1540 blk->root_state.read_only = blk->root->bs->read_only;
1541 blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
281d22d8
HR
1542
1543 if (blk->root_state.throttle_group) {
1544 g_free(blk->root_state.throttle_group);
1545 throttle_group_unref(blk->root_state.throttle_state);
1546 }
27ccdd52 1547 if (blk->public.throttle_state) {
49d2165d 1548 const char *name = throttle_group_get_name(blk);
281d22d8
HR
1549 blk->root_state.throttle_group = g_strdup(name);
1550 blk->root_state.throttle_state = throttle_group_incref(name);
1551 } else {
1552 blk->root_state.throttle_group = NULL;
1553 blk->root_state.throttle_state = NULL;
1554 }
1555}
1556
38cb18f5
HR
1557/*
1558 * Applies the information in the root state to the given BlockDriverState. This
1559 * does not include the flags which have to be specified for bdrv_open(), use
1560 * blk_get_open_flags_from_root_state() to inquire them.
1561 */
1562void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
1563{
1564 bs->detect_zeroes = blk->root_state.detect_zeroes;
1565 if (blk->root_state.throttle_group) {
97148076 1566 blk_io_limits_enable(blk, blk->root_state.throttle_group);
38cb18f5
HR
1567 }
1568}
1569
1570/*
1571 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1572 * supposed to inherit the root state.
1573 */
1574int blk_get_open_flags_from_root_state(BlockBackend *blk)
1575{
1576 int bs_flags;
1577
1578 bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1579 bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1580
1581 return bs_flags;
1582}
1583
281d22d8
HR
1584BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1585{
1586 return &blk->root_state;
1587}
1393f212
HR
1588
1589int blk_commit_all(void)
1590{
fe1a9cbc
HR
1591 BlockBackend *blk = NULL;
1592
1593 while ((blk = blk_all_next(blk)) != NULL) {
1594 AioContext *aio_context = blk_get_aio_context(blk);
1595
1596 aio_context_acquire(aio_context);
f21d96d0
KW
1597 if (blk_is_inserted(blk) && blk->root->bs->backing) {
1598 int ret = bdrv_commit(blk->root->bs);
fe1a9cbc
HR
1599 if (ret < 0) {
1600 aio_context_release(aio_context);
1601 return ret;
1602 }
1603 }
1604 aio_context_release(aio_context);
1605 }
1606 return 0;
1607}
1608
1609int blk_flush_all(void)
1610{
1611 BlockBackend *blk = NULL;
1612 int result = 0;
1613
1614 while ((blk = blk_all_next(blk)) != NULL) {
1615 AioContext *aio_context = blk_get_aio_context(blk);
1616 int ret;
1617
1618 aio_context_acquire(aio_context);
1619 if (blk_is_inserted(blk)) {
1620 ret = blk_flush(blk);
1621 if (ret < 0 && !result) {
1622 result = ret;
1623 }
1624 }
1625 aio_context_release(aio_context);
1626 }
1627
1628 return result;
1393f212 1629}
97148076
KW
1630
1631
1632/* throttling disk I/O limits */
1633void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
1634{
1635 throttle_group_config(blk, cfg);
1636}
1637
1638void blk_io_limits_disable(BlockBackend *blk)
1639{
1640 assert(blk->public.throttle_state);
c2066af0 1641 bdrv_drained_begin(blk_bs(blk));
97148076 1642 throttle_group_unregister_blk(blk);
c2066af0 1643 bdrv_drained_end(blk_bs(blk));
97148076
KW
1644}
1645
1646/* should be called before blk_set_io_limits if a limit is set */
1647void blk_io_limits_enable(BlockBackend *blk, const char *group)
1648{
1649 assert(!blk->public.throttle_state);
1650 throttle_group_register_blk(blk, group);
1651}
1652
1653void blk_io_limits_update_group(BlockBackend *blk, const char *group)
1654{
1655 /* this BB is not part of any group */
1656 if (!blk->public.throttle_state) {
1657 return;
1658 }
1659
1660 /* this BB is a part of the same group than the one we want */
1661 if (!g_strcmp0(throttle_group_get_name(blk), group)) {
1662 return;
1663 }
1664
1665 /* need to change the group this bs belong to */
1666 blk_io_limits_disable(blk);
1667 blk_io_limits_enable(blk, group);
1668}
c2066af0
KW
1669
1670static void blk_root_drained_begin(BdrvChild *child)
1671{
1672 BlockBackend *blk = child->opaque;
1673
1674 if (blk->public.io_limits_disabled++ == 0) {
1675 throttle_group_restart_blk(blk);
1676 }
1677}
1678
1679static void blk_root_drained_end(BdrvChild *child)
1680{
1681 BlockBackend *blk = child->opaque;
1682
1683 assert(blk->public.io_limits_disabled);
1684 --blk->public.io_limits_disabled;
1685}