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