]> git.proxmox.com Git - mirror_qemu.git/blame - block/block-backend.c
block: Use blk_co_pwritev() for blk_write()
[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
a8823a3b
KW
710static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
711 unsigned int bytes, QEMUIOVector *qiov,
712 BdrvRequestFlags flags)
713{
714 int ret = blk_check_byte_request(blk, offset, bytes);
715 if (ret < 0) {
716 return ret;
717 }
718
719 return bdrv_co_do_pwritev(blk_bs(blk), offset, bytes, qiov, flags);
720}
721
1bf1cbc9
KW
722typedef struct BlkRwCo {
723 BlockBackend *blk;
724 int64_t offset;
725 QEMUIOVector *qiov;
726 int ret;
727 BdrvRequestFlags flags;
728} BlkRwCo;
729
730static void blk_read_entry(void *opaque)
731{
732 BlkRwCo *rwco = opaque;
733
734 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
735 rwco->qiov, rwco->flags);
736}
737
a8823a3b
KW
738static void blk_write_entry(void *opaque)
739{
740 BlkRwCo *rwco = opaque;
741
742 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
743 rwco->qiov, rwco->flags);
744}
745
746static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
747 int nb_sectors, CoroutineEntry co_entry)
1bf1cbc9
KW
748{
749 AioContext *aio_context;
750 QEMUIOVector qiov;
751 struct iovec iov;
752 Coroutine *co;
753 BlkRwCo rwco;
754
755 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
756 return -EINVAL;
757 }
758
759 iov = (struct iovec) {
760 .iov_base = buf,
761 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
762 };
763 qemu_iovec_init_external(&qiov, &iov, 1);
764
765 rwco = (BlkRwCo) {
766 .blk = blk,
767 .offset = sector_num << BDRV_SECTOR_BITS,
768 .qiov = &qiov,
769 .ret = NOT_DONE,
770 };
771
a8823a3b 772 co = qemu_coroutine_create(co_entry);
1bf1cbc9
KW
773 qemu_coroutine_enter(co, &rwco);
774
775 aio_context = blk_get_aio_context(blk);
776 while (rwco.ret == NOT_DONE) {
777 aio_poll(aio_context, true);
778 }
779
780 return rwco.ret;
4be74634
MA
781}
782
a8823a3b
KW
783int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
784 int nb_sectors)
785{
786 return blk_rw(blk, sector_num, buf, nb_sectors, blk_read_entry);
787}
788
4be74634
MA
789int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
790 int nb_sectors)
791{
e7f7d676
HR
792 int ret = blk_check_request(blk, sector_num, nb_sectors);
793 if (ret < 0) {
794 return ret;
795 }
796
f21d96d0 797 return bdrv_read_unthrottled(blk_bs(blk), sector_num, buf, nb_sectors);
4be74634
MA
798}
799
800int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
801 int nb_sectors)
802{
a8823a3b 803 return blk_rw(blk, sector_num, (uint8_t*) buf, nb_sectors, blk_write_entry);
4be74634
MA
804}
805
0df89e8e
KW
806int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
807 int nb_sectors, BdrvRequestFlags flags)
808{
809 int ret = blk_check_request(blk, sector_num, nb_sectors);
810 if (ret < 0) {
811 return ret;
812 }
813
f21d96d0 814 return bdrv_write_zeroes(blk_bs(blk), sector_num, nb_sectors, flags);
0df89e8e
KW
815}
816
e7f7d676
HR
817static void error_callback_bh(void *opaque)
818{
819 struct BlockBackendAIOCB *acb = opaque;
820 qemu_bh_delete(acb->bh);
821 acb->common.cb(acb->common.opaque, acb->ret);
822 qemu_aio_unref(acb);
823}
824
ca78ecfa
PL
825BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
826 BlockCompletionFunc *cb,
827 void *opaque, int ret)
e7f7d676
HR
828{
829 struct BlockBackendAIOCB *acb;
830 QEMUBH *bh;
831
832 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
4981bdec 833 acb->blk = blk;
e7f7d676
HR
834 acb->ret = ret;
835
836 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
837 acb->bh = bh;
838 qemu_bh_schedule(bh);
839
840 return &acb->common;
841}
842
4be74634
MA
843BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
844 int nb_sectors, BdrvRequestFlags flags,
845 BlockCompletionFunc *cb, void *opaque)
846{
e7f7d676
HR
847 int ret = blk_check_request(blk, sector_num, nb_sectors);
848 if (ret < 0) {
ca78ecfa 849 return blk_abort_aio_request(blk, cb, opaque, ret);
e7f7d676
HR
850 }
851
f21d96d0 852 return bdrv_aio_write_zeroes(blk_bs(blk), sector_num, nb_sectors, flags,
4be74634
MA
853 cb, opaque);
854}
855
856int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
857{
e7f7d676
HR
858 int ret = blk_check_byte_request(blk, offset, count);
859 if (ret < 0) {
860 return ret;
861 }
862
f21d96d0 863 return bdrv_pread(blk_bs(blk), offset, buf, count);
4be74634
MA
864}
865
866int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
867{
e7f7d676
HR
868 int ret = blk_check_byte_request(blk, offset, count);
869 if (ret < 0) {
870 return ret;
871 }
872
f21d96d0 873 return bdrv_pwrite(blk_bs(blk), offset, buf, count);
4be74634
MA
874}
875
876int64_t blk_getlength(BlockBackend *blk)
877{
c09ba36c
HR
878 if (!blk_is_available(blk)) {
879 return -ENOMEDIUM;
880 }
881
f21d96d0 882 return bdrv_getlength(blk_bs(blk));
4be74634
MA
883}
884
885void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
886{
f21d96d0 887 if (!blk_bs(blk)) {
a46fc9c9
HR
888 *nb_sectors_ptr = 0;
889 } else {
f21d96d0 890 bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
a46fc9c9 891 }
4be74634
MA
892}
893
1ef01253
HR
894int64_t blk_nb_sectors(BlockBackend *blk)
895{
c09ba36c
HR
896 if (!blk_is_available(blk)) {
897 return -ENOMEDIUM;
898 }
899
f21d96d0 900 return bdrv_nb_sectors(blk_bs(blk));
1ef01253
HR
901}
902
4be74634
MA
903BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
904 QEMUIOVector *iov, int nb_sectors,
905 BlockCompletionFunc *cb, void *opaque)
906{
e7f7d676
HR
907 int ret = blk_check_request(blk, sector_num, nb_sectors);
908 if (ret < 0) {
ca78ecfa 909 return blk_abort_aio_request(blk, cb, opaque, ret);
e7f7d676
HR
910 }
911
f21d96d0 912 return bdrv_aio_readv(blk_bs(blk), sector_num, iov, nb_sectors, cb, opaque);
4be74634
MA
913}
914
915BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
916 QEMUIOVector *iov, int nb_sectors,
917 BlockCompletionFunc *cb, void *opaque)
918{
e7f7d676
HR
919 int ret = blk_check_request(blk, sector_num, nb_sectors);
920 if (ret < 0) {
ca78ecfa 921 return blk_abort_aio_request(blk, cb, opaque, ret);
e7f7d676
HR
922 }
923
f21d96d0 924 return bdrv_aio_writev(blk_bs(blk), sector_num, iov, nb_sectors, cb, opaque);
4be74634
MA
925}
926
927BlockAIOCB *blk_aio_flush(BlockBackend *blk,
928 BlockCompletionFunc *cb, void *opaque)
929{
c09ba36c 930 if (!blk_is_available(blk)) {
ca78ecfa 931 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
c09ba36c
HR
932 }
933
f21d96d0 934 return bdrv_aio_flush(blk_bs(blk), cb, opaque);
4be74634
MA
935}
936
937BlockAIOCB *blk_aio_discard(BlockBackend *blk,
938 int64_t sector_num, int nb_sectors,
939 BlockCompletionFunc *cb, void *opaque)
940{
e7f7d676
HR
941 int ret = blk_check_request(blk, sector_num, nb_sectors);
942 if (ret < 0) {
ca78ecfa 943 return blk_abort_aio_request(blk, cb, opaque, ret);
e7f7d676
HR
944 }
945
f21d96d0 946 return bdrv_aio_discard(blk_bs(blk), sector_num, nb_sectors, cb, opaque);
4be74634
MA
947}
948
949void blk_aio_cancel(BlockAIOCB *acb)
950{
951 bdrv_aio_cancel(acb);
952}
953
954void blk_aio_cancel_async(BlockAIOCB *acb)
955{
956 bdrv_aio_cancel_async(acb);
957}
958
959int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
960{
e7f7d676
HR
961 int i, ret;
962
963 for (i = 0; i < num_reqs; i++) {
964 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
965 if (ret < 0) {
966 return ret;
967 }
968 }
969
f21d96d0 970 return bdrv_aio_multiwrite(blk_bs(blk), reqs, num_reqs);
4be74634
MA
971}
972
973int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
974{
c09ba36c
HR
975 if (!blk_is_available(blk)) {
976 return -ENOMEDIUM;
977 }
978
f21d96d0 979 return bdrv_ioctl(blk_bs(blk), req, buf);
4be74634
MA
980}
981
982BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
983 BlockCompletionFunc *cb, void *opaque)
984{
c09ba36c 985 if (!blk_is_available(blk)) {
ca78ecfa 986 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
c09ba36c
HR
987 }
988
f21d96d0 989 return bdrv_aio_ioctl(blk_bs(blk), req, buf, cb, opaque);
4be74634
MA
990}
991
2bb0dce7
HR
992int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
993{
e7f7d676
HR
994 int ret = blk_check_request(blk, sector_num, nb_sectors);
995 if (ret < 0) {
996 return ret;
997 }
998
f21d96d0 999 return bdrv_co_discard(blk_bs(blk), sector_num, nb_sectors);
2bb0dce7
HR
1000}
1001
1002int blk_co_flush(BlockBackend *blk)
1003{
c09ba36c
HR
1004 if (!blk_is_available(blk)) {
1005 return -ENOMEDIUM;
1006 }
1007
f21d96d0 1008 return bdrv_co_flush(blk_bs(blk));
2bb0dce7
HR
1009}
1010
4be74634
MA
1011int blk_flush(BlockBackend *blk)
1012{
c09ba36c
HR
1013 if (!blk_is_available(blk)) {
1014 return -ENOMEDIUM;
1015 }
1016
f21d96d0 1017 return bdrv_flush(blk_bs(blk));
4be74634
MA
1018}
1019
97b0385a
AY
1020void blk_drain(BlockBackend *blk)
1021{
f21d96d0
KW
1022 if (blk_bs(blk)) {
1023 bdrv_drain(blk_bs(blk));
a46fc9c9 1024 }
97b0385a
AY
1025}
1026
4be74634
MA
1027void blk_drain_all(void)
1028{
1029 bdrv_drain_all();
1030}
1031
373340b2
HR
1032void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1033 BlockdevOnError on_write_error)
1034{
1035 blk->on_read_error = on_read_error;
1036 blk->on_write_error = on_write_error;
1037}
1038
4be74634
MA
1039BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
1040{
373340b2 1041 return is_read ? blk->on_read_error : blk->on_write_error;
4be74634
MA
1042}
1043
1044BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
1045 int error)
1046{
373340b2
HR
1047 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
1048
1049 switch (on_err) {
1050 case BLOCKDEV_ON_ERROR_ENOSPC:
1051 return (error == ENOSPC) ?
1052 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1053 case BLOCKDEV_ON_ERROR_STOP:
1054 return BLOCK_ERROR_ACTION_STOP;
1055 case BLOCKDEV_ON_ERROR_REPORT:
1056 return BLOCK_ERROR_ACTION_REPORT;
1057 case BLOCKDEV_ON_ERROR_IGNORE:
1058 return BLOCK_ERROR_ACTION_IGNORE;
1059 default:
1060 abort();
1061 }
4be74634
MA
1062}
1063
373340b2
HR
1064static void send_qmp_error_event(BlockBackend *blk,
1065 BlockErrorAction action,
1066 bool is_read, int error)
1067{
1068 IoOperationType optype;
1069
1070 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
1071 qapi_event_send_block_io_error(blk_name(blk), optype, action,
1072 blk_iostatus_is_enabled(blk),
1073 error == ENOSPC, strerror(error),
1074 &error_abort);
1075}
1076
1077/* This is done by device models because, while the block layer knows
1078 * about the error, it does not know whether an operation comes from
1079 * the device or the block layer (from a job, for example).
1080 */
4be74634
MA
1081void blk_error_action(BlockBackend *blk, BlockErrorAction action,
1082 bool is_read, int error)
1083{
373340b2
HR
1084 assert(error >= 0);
1085
1086 if (action == BLOCK_ERROR_ACTION_STOP) {
1087 /* First set the iostatus, so that "info block" returns an iostatus
1088 * that matches the events raised so far (an additional error iostatus
1089 * is fine, but not a lost one).
1090 */
1091 blk_iostatus_set_err(blk, error);
1092
1093 /* Then raise the request to stop the VM and the event.
1094 * qemu_system_vmstop_request_prepare has two effects. First,
1095 * it ensures that the STOP event always comes after the
1096 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1097 * can observe the STOP event and do a "cont" before the STOP
1098 * event is issued, the VM will not stop. In this case, vm_start()
1099 * also ensures that the STOP/RESUME pair of events is emitted.
1100 */
1101 qemu_system_vmstop_request_prepare();
1102 send_qmp_error_event(blk, action, is_read, error);
1103 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1104 } else {
1105 send_qmp_error_event(blk, action, is_read, error);
1106 }
4be74634
MA
1107}
1108
1109int blk_is_read_only(BlockBackend *blk)
1110{
f21d96d0
KW
1111 BlockDriverState *bs = blk_bs(blk);
1112
1113 if (bs) {
1114 return bdrv_is_read_only(bs);
061959e8
HR
1115 } else {
1116 return blk->root_state.read_only;
1117 }
4be74634
MA
1118}
1119
1120int blk_is_sg(BlockBackend *blk)
1121{
f21d96d0
KW
1122 BlockDriverState *bs = blk_bs(blk);
1123
1124 if (!bs) {
a46fc9c9
HR
1125 return 0;
1126 }
1127
f21d96d0 1128 return bdrv_is_sg(bs);
4be74634
MA
1129}
1130
1131int blk_enable_write_cache(BlockBackend *blk)
1132{
f21d96d0
KW
1133 BlockDriverState *bs = blk_bs(blk);
1134
1135 if (bs) {
1136 return bdrv_enable_write_cache(bs);
061959e8
HR
1137 } else {
1138 return !!(blk->root_state.open_flags & BDRV_O_CACHE_WB);
1139 }
4be74634
MA
1140}
1141
1142void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1143{
f21d96d0
KW
1144 BlockDriverState *bs = blk_bs(blk);
1145
1146 if (bs) {
1147 bdrv_set_enable_write_cache(bs, wce);
061959e8
HR
1148 } else {
1149 if (wce) {
1150 blk->root_state.open_flags |= BDRV_O_CACHE_WB;
1151 } else {
1152 blk->root_state.open_flags &= ~BDRV_O_CACHE_WB;
1153 }
1154 }
4be74634
MA
1155}
1156
2bb0dce7
HR
1157void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1158{
f21d96d0
KW
1159 BlockDriverState *bs = blk_bs(blk);
1160
1161 if (!bs) {
c09ba36c
HR
1162 error_setg(errp, "Device '%s' has no medium", blk->name);
1163 return;
1164 }
1165
f21d96d0 1166 bdrv_invalidate_cache(bs, errp);
2bb0dce7
HR
1167}
1168
e031f750 1169bool blk_is_inserted(BlockBackend *blk)
4be74634 1170{
f21d96d0
KW
1171 BlockDriverState *bs = blk_bs(blk);
1172
1173 return bs && bdrv_is_inserted(bs);
db0284f8
HR
1174}
1175
1176bool blk_is_available(BlockBackend *blk)
1177{
1178 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
4be74634
MA
1179}
1180
1181void blk_lock_medium(BlockBackend *blk, bool locked)
1182{
f21d96d0
KW
1183 BlockDriverState *bs = blk_bs(blk);
1184
1185 if (bs) {
1186 bdrv_lock_medium(bs, locked);
a46fc9c9 1187 }
4be74634
MA
1188}
1189
1190void blk_eject(BlockBackend *blk, bool eject_flag)
1191{
f21d96d0
KW
1192 BlockDriverState *bs = blk_bs(blk);
1193
1194 if (bs) {
1195 bdrv_eject(bs, eject_flag);
a46fc9c9 1196 }
4be74634
MA
1197}
1198
1199int blk_get_flags(BlockBackend *blk)
1200{
f21d96d0
KW
1201 BlockDriverState *bs = blk_bs(blk);
1202
1203 if (bs) {
1204 return bdrv_get_flags(bs);
061959e8
HR
1205 } else {
1206 return blk->root_state.open_flags;
1207 }
4be74634
MA
1208}
1209
454057b7
PL
1210int blk_get_max_transfer_length(BlockBackend *blk)
1211{
f21d96d0
KW
1212 BlockDriverState *bs = blk_bs(blk);
1213
1214 if (bs) {
1215 return bs->bl.max_transfer_length;
a46fc9c9
HR
1216 } else {
1217 return 0;
1218 }
454057b7
PL
1219}
1220
648296e0
SH
1221int blk_get_max_iov(BlockBackend *blk)
1222{
f21d96d0 1223 return blk->root->bs->bl.max_iov;
648296e0
SH
1224}
1225
4be74634
MA
1226void blk_set_guest_block_size(BlockBackend *blk, int align)
1227{
68e9ec01 1228 blk->guest_block_size = align;
4be74634
MA
1229}
1230
f1c17521
PB
1231void *blk_try_blockalign(BlockBackend *blk, size_t size)
1232{
f21d96d0 1233 return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
f1c17521
PB
1234}
1235
4be74634
MA
1236void *blk_blockalign(BlockBackend *blk, size_t size)
1237{
f21d96d0 1238 return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
4be74634
MA
1239}
1240
1241bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1242{
f21d96d0
KW
1243 BlockDriverState *bs = blk_bs(blk);
1244
1245 if (!bs) {
a46fc9c9
HR
1246 return false;
1247 }
1248
f21d96d0 1249 return bdrv_op_is_blocked(bs, op, errp);
4be74634
MA
1250}
1251
1252void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1253{
f21d96d0
KW
1254 BlockDriverState *bs = blk_bs(blk);
1255
1256 if (bs) {
1257 bdrv_op_unblock(bs, op, reason);
a46fc9c9 1258 }
4be74634
MA
1259}
1260
1261void blk_op_block_all(BlockBackend *blk, Error *reason)
1262{
f21d96d0
KW
1263 BlockDriverState *bs = blk_bs(blk);
1264
1265 if (bs) {
1266 bdrv_op_block_all(bs, reason);
a46fc9c9 1267 }
4be74634
MA
1268}
1269
1270void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1271{
f21d96d0
KW
1272 BlockDriverState *bs = blk_bs(blk);
1273
1274 if (bs) {
1275 bdrv_op_unblock_all(bs, reason);
a46fc9c9 1276 }
4be74634
MA
1277}
1278
1279AioContext *blk_get_aio_context(BlockBackend *blk)
1280{
f21d96d0
KW
1281 BlockDriverState *bs = blk_bs(blk);
1282
1283 if (bs) {
1284 return bdrv_get_aio_context(bs);
4981bdec
HR
1285 } else {
1286 return qemu_get_aio_context();
1287 }
1288}
1289
1290static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1291{
1292 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1293 return blk_get_aio_context(blk_acb->blk);
4be74634
MA
1294}
1295
1296void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1297{
f21d96d0
KW
1298 BlockDriverState *bs = blk_bs(blk);
1299
1300 if (bs) {
1301 bdrv_set_aio_context(bs, new_context);
a46fc9c9 1302 }
4be74634
MA
1303}
1304
2019ba0a
HR
1305void blk_add_aio_context_notifier(BlockBackend *blk,
1306 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1307 void (*detach_aio_context)(void *opaque), void *opaque)
1308{
f21d96d0
KW
1309 BlockDriverState *bs = blk_bs(blk);
1310
1311 if (bs) {
1312 bdrv_add_aio_context_notifier(bs, attached_aio_context,
a46fc9c9
HR
1313 detach_aio_context, opaque);
1314 }
2019ba0a
HR
1315}
1316
1317void blk_remove_aio_context_notifier(BlockBackend *blk,
1318 void (*attached_aio_context)(AioContext *,
1319 void *),
1320 void (*detach_aio_context)(void *),
1321 void *opaque)
1322{
f21d96d0
KW
1323 BlockDriverState *bs = blk_bs(blk);
1324
1325 if (bs) {
1326 bdrv_remove_aio_context_notifier(bs, attached_aio_context,
a46fc9c9
HR
1327 detach_aio_context, opaque);
1328 }
2019ba0a
HR
1329}
1330
3301f6c6
HR
1331void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1332{
1333 notifier_list_add(&blk->remove_bs_notifiers, notify);
1334}
1335
1336void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1337{
1338 notifier_list_add(&blk->insert_bs_notifiers, notify);
1339}
1340
4be74634
MA
1341void blk_io_plug(BlockBackend *blk)
1342{
f21d96d0
KW
1343 BlockDriverState *bs = blk_bs(blk);
1344
1345 if (bs) {
1346 bdrv_io_plug(bs);
a46fc9c9 1347 }
4be74634
MA
1348}
1349
1350void blk_io_unplug(BlockBackend *blk)
1351{
f21d96d0
KW
1352 BlockDriverState *bs = blk_bs(blk);
1353
1354 if (bs) {
1355 bdrv_io_unplug(bs);
a46fc9c9 1356 }
4be74634
MA
1357}
1358
1359BlockAcctStats *blk_get_stats(BlockBackend *blk)
1360{
7f0e9da6 1361 return &blk->stats;
4be74634
MA
1362}
1363
1364void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1365 BlockCompletionFunc *cb, void *opaque)
1366{
1367 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1368}
1ef01253
HR
1369
1370int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
1371 int nb_sectors, BdrvRequestFlags flags)
1372{
e7f7d676
HR
1373 int ret = blk_check_request(blk, sector_num, nb_sectors);
1374 if (ret < 0) {
1375 return ret;
1376 }
1377
f21d96d0 1378 return bdrv_co_write_zeroes(blk_bs(blk), sector_num, nb_sectors, flags);
1ef01253
HR
1379}
1380
1381int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1382 const uint8_t *buf, int nb_sectors)
1383{
e7f7d676
HR
1384 int ret = blk_check_request(blk, sector_num, nb_sectors);
1385 if (ret < 0) {
1386 return ret;
1387 }
1388
f21d96d0 1389 return bdrv_write_compressed(blk_bs(blk), sector_num, buf, nb_sectors);
1ef01253
HR
1390}
1391
1392int blk_truncate(BlockBackend *blk, int64_t offset)
1393{
c09ba36c
HR
1394 if (!blk_is_available(blk)) {
1395 return -ENOMEDIUM;
1396 }
1397
f21d96d0 1398 return bdrv_truncate(blk_bs(blk), offset);
1ef01253
HR
1399}
1400
1401int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1402{
e7f7d676
HR
1403 int ret = blk_check_request(blk, sector_num, nb_sectors);
1404 if (ret < 0) {
1405 return ret;
1406 }
1407
f21d96d0 1408 return bdrv_discard(blk_bs(blk), sector_num, nb_sectors);
1ef01253
HR
1409}
1410
1411int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1412 int64_t pos, int size)
1413{
c09ba36c
HR
1414 if (!blk_is_available(blk)) {
1415 return -ENOMEDIUM;
1416 }
1417
f21d96d0 1418 return bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
1ef01253
HR
1419}
1420
1421int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1422{
c09ba36c
HR
1423 if (!blk_is_available(blk)) {
1424 return -ENOMEDIUM;
1425 }
1426
f21d96d0 1427 return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
1ef01253 1428}
f0272c4d
ET
1429
1430int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1431{
c09ba36c
HR
1432 if (!blk_is_available(blk)) {
1433 return -ENOMEDIUM;
1434 }
1435
f21d96d0 1436 return bdrv_probe_blocksizes(blk_bs(blk), bsz);
f0272c4d
ET
1437}
1438
1439int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1440{
c09ba36c
HR
1441 if (!blk_is_available(blk)) {
1442 return -ENOMEDIUM;
1443 }
1444
f21d96d0 1445 return bdrv_probe_geometry(blk_bs(blk), geo);
f0272c4d 1446}
281d22d8
HR
1447
1448/*
1449 * Updates the BlockBackendRootState object with data from the currently
1450 * attached BlockDriverState.
1451 */
1452void blk_update_root_state(BlockBackend *blk)
1453{
f21d96d0 1454 assert(blk->root);
281d22d8 1455
f21d96d0
KW
1456 blk->root_state.open_flags = blk->root->bs->open_flags;
1457 blk->root_state.read_only = blk->root->bs->read_only;
1458 blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
281d22d8
HR
1459
1460 if (blk->root_state.throttle_group) {
1461 g_free(blk->root_state.throttle_group);
1462 throttle_group_unref(blk->root_state.throttle_state);
1463 }
f21d96d0
KW
1464 if (blk->root->bs->throttle_state) {
1465 const char *name = throttle_group_get_name(blk->root->bs);
281d22d8
HR
1466 blk->root_state.throttle_group = g_strdup(name);
1467 blk->root_state.throttle_state = throttle_group_incref(name);
1468 } else {
1469 blk->root_state.throttle_group = NULL;
1470 blk->root_state.throttle_state = NULL;
1471 }
1472}
1473
38cb18f5
HR
1474/*
1475 * Applies the information in the root state to the given BlockDriverState. This
1476 * does not include the flags which have to be specified for bdrv_open(), use
1477 * blk_get_open_flags_from_root_state() to inquire them.
1478 */
1479void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
1480{
1481 bs->detect_zeroes = blk->root_state.detect_zeroes;
1482 if (blk->root_state.throttle_group) {
1483 bdrv_io_limits_enable(bs, blk->root_state.throttle_group);
1484 }
1485}
1486
1487/*
1488 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1489 * supposed to inherit the root state.
1490 */
1491int blk_get_open_flags_from_root_state(BlockBackend *blk)
1492{
1493 int bs_flags;
1494
1495 bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1496 bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1497
1498 return bs_flags;
1499}
1500
281d22d8
HR
1501BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1502{
1503 return &blk->root_state;
1504}
1393f212
HR
1505
1506int blk_commit_all(void)
1507{
fe1a9cbc
HR
1508 BlockBackend *blk = NULL;
1509
1510 while ((blk = blk_all_next(blk)) != NULL) {
1511 AioContext *aio_context = blk_get_aio_context(blk);
1512
1513 aio_context_acquire(aio_context);
f21d96d0
KW
1514 if (blk_is_inserted(blk) && blk->root->bs->backing) {
1515 int ret = bdrv_commit(blk->root->bs);
fe1a9cbc
HR
1516 if (ret < 0) {
1517 aio_context_release(aio_context);
1518 return ret;
1519 }
1520 }
1521 aio_context_release(aio_context);
1522 }
1523 return 0;
1524}
1525
1526int blk_flush_all(void)
1527{
1528 BlockBackend *blk = NULL;
1529 int result = 0;
1530
1531 while ((blk = blk_all_next(blk)) != NULL) {
1532 AioContext *aio_context = blk_get_aio_context(blk);
1533 int ret;
1534
1535 aio_context_acquire(aio_context);
1536 if (blk_is_inserted(blk)) {
1537 ret = blk_flush(blk);
1538 if (ret < 0 && !result) {
1539 result = ret;
1540 }
1541 }
1542 aio_context_release(aio_context);
1543 }
1544
1545 return result;
1393f212 1546}