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