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