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