]> git.proxmox.com Git - mirror_qemu.git/blame - block/block-backend.c
block: Prepare remaining BB functions for NULL BDS
[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);
26f54e9a
MA
179 g_free(blk);
180}
181
8fb3c76c
MA
182static void drive_info_del(DriveInfo *dinfo)
183{
184 if (!dinfo) {
185 return;
186 }
187 qemu_opts_del(dinfo->opts);
8fb3c76c
MA
188 g_free(dinfo->serial);
189 g_free(dinfo);
190}
191
26f54e9a
MA
192/*
193 * Increment @blk's reference count.
194 * @blk must not be null.
195 */
196void blk_ref(BlockBackend *blk)
197{
198 blk->refcnt++;
199}
200
201/*
202 * Decrement @blk's reference count.
203 * If this drops it to zero, destroy @blk.
204 * For convenience, do nothing if @blk is null.
205 */
206void blk_unref(BlockBackend *blk)
207{
208 if (blk) {
209 assert(blk->refcnt > 0);
210 if (!--blk->refcnt) {
211 blk_delete(blk);
212 }
213 }
214}
215
216/*
217 * Return the BlockBackend after @blk.
218 * If @blk is null, return the first one.
219 * Else, return @blk's next sibling, which may be null.
220 *
221 * To iterate over all BlockBackends, do
222 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
223 * ...
224 * }
225 */
226BlockBackend *blk_next(BlockBackend *blk)
227{
228 return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
229}
230
231/*
7e7d56d9
MA
232 * Return @blk's name, a non-null string.
233 * Wart: the name is empty iff @blk has been hidden with
3e5a50d6 234 * blk_hide_on_behalf_of_hmp_drive_del().
26f54e9a
MA
235 */
236const char *blk_name(BlockBackend *blk)
237{
238 return blk->name;
239}
240
241/*
242 * Return the BlockBackend with name @name if it exists, else null.
243 * @name must not be null.
244 */
245BlockBackend *blk_by_name(const char *name)
246{
247 BlockBackend *blk;
248
249 assert(name);
250 QTAILQ_FOREACH(blk, &blk_backends, link) {
251 if (!strcmp(name, blk->name)) {
252 return blk;
253 }
254 }
255 return NULL;
256}
7e7d56d9
MA
257
258/*
259 * Return the BlockDriverState attached to @blk if any, else null.
260 */
261BlockDriverState *blk_bs(BlockBackend *blk)
262{
263 return blk->bs;
264}
265
a2d61900
KW
266/*
267 * Changes the BlockDriverState attached to @blk
268 */
269void blk_set_bs(BlockBackend *blk, BlockDriverState *bs)
270{
271 bdrv_ref(bs);
272
273 if (blk->bs) {
274 blk->bs->blk = NULL;
275 bdrv_unref(blk->bs);
276 }
277 assert(bs->blk == NULL);
278
279 blk->bs = bs;
280 bs->blk = blk;
281}
282
18e46a03
MA
283/*
284 * Return @blk's DriveInfo if any, else null.
285 */
286DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
287{
288 return blk->legacy_dinfo;
289}
290
291/*
292 * Set @blk's DriveInfo to @dinfo, and return it.
293 * @blk must not have a DriveInfo set already.
294 * No other BlockBackend may have the same DriveInfo set.
295 */
296DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
297{
298 assert(!blk->legacy_dinfo);
299 return blk->legacy_dinfo = dinfo;
300}
301
302/*
303 * Return the BlockBackend with DriveInfo @dinfo.
304 * It must exist.
305 */
306BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
307{
308 BlockBackend *blk;
309
310 QTAILQ_FOREACH(blk, &blk_backends, link) {
311 if (blk->legacy_dinfo == dinfo) {
312 return blk;
313 }
314 }
315 abort();
316}
317
7e7d56d9
MA
318/*
319 * Hide @blk.
320 * @blk must not have been hidden already.
321 * Make attached BlockDriverState, if any, anonymous.
322 * Once hidden, @blk is invisible to all functions that don't receive
323 * it as argument. For example, blk_by_name() won't return it.
324 * Strictly for use by do_drive_del().
325 * TODO get rid of it!
326 */
3e5a50d6 327void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk)
7e7d56d9
MA
328{
329 QTAILQ_REMOVE(&blk_backends, blk, link);
330 blk->name[0] = 0;
331 if (blk->bs) {
332 bdrv_make_anon(blk->bs);
333 }
334}
4be74634 335
a7f53e26
MA
336/*
337 * Attach device model @dev to @blk.
338 * Return 0 on success, -EBUSY when a device model is attached already.
339 */
4be74634 340int blk_attach_dev(BlockBackend *blk, void *dev)
a7f53e26 341/* TODO change to DeviceState *dev when all users are qdevified */
4be74634 342{
a7f53e26
MA
343 if (blk->dev) {
344 return -EBUSY;
345 }
84ebe375 346 blk_ref(blk);
a7f53e26 347 blk->dev = dev;
373340b2 348 blk_iostatus_reset(blk);
a7f53e26 349 return 0;
4be74634
MA
350}
351
a7f53e26
MA
352/*
353 * Attach device model @dev to @blk.
354 * @blk must not have a device model attached already.
355 * TODO qdevified devices don't use this, remove when devices are qdevified
356 */
4be74634
MA
357void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
358{
a7f53e26
MA
359 if (blk_attach_dev(blk, dev) < 0) {
360 abort();
361 }
4be74634
MA
362}
363
a7f53e26
MA
364/*
365 * Detach device model @dev from @blk.
366 * @dev must be currently attached to @blk.
367 */
4be74634 368void blk_detach_dev(BlockBackend *blk, void *dev)
a7f53e26 369/* TODO change to DeviceState *dev when all users are qdevified */
4be74634 370{
a7f53e26
MA
371 assert(blk->dev == dev);
372 blk->dev = NULL;
373 blk->dev_ops = NULL;
374 blk->dev_opaque = NULL;
68e9ec01 375 blk->guest_block_size = 512;
84ebe375 376 blk_unref(blk);
4be74634
MA
377}
378
a7f53e26
MA
379/*
380 * Return the device model attached to @blk if any, else null.
381 */
4be74634 382void *blk_get_attached_dev(BlockBackend *blk)
a7f53e26
MA
383/* TODO change to return DeviceState * when all users are qdevified */
384{
385 return blk->dev;
386}
387
388/*
389 * Set @blk's device model callbacks to @ops.
390 * @opaque is the opaque argument to pass to the callbacks.
391 * This is for use by device models.
392 */
393void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
394 void *opaque)
395{
396 blk->dev_ops = ops;
397 blk->dev_opaque = opaque;
398}
399
400/*
401 * Notify @blk's attached device model of media change.
402 * If @load is true, notify of media load.
403 * Else, notify of media eject.
404 * Also send DEVICE_TRAY_MOVED events as appropriate.
405 */
406void blk_dev_change_media_cb(BlockBackend *blk, bool load)
407{
408 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
409 bool tray_was_closed = !blk_dev_is_tray_open(blk);
410
411 blk->dev_ops->change_media_cb(blk->dev_opaque, load);
412 if (tray_was_closed) {
413 /* tray open */
414 qapi_event_send_device_tray_moved(blk_name(blk),
415 true, &error_abort);
416 }
417 if (load) {
418 /* tray close */
419 qapi_event_send_device_tray_moved(blk_name(blk),
420 false, &error_abort);
421 }
422 }
423}
424
425/*
426 * Does @blk's attached device model have removable media?
427 * %true if no device model is attached.
428 */
429bool blk_dev_has_removable_media(BlockBackend *blk)
430{
431 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
432}
433
434/*
435 * Notify @blk's attached device model of a media eject request.
436 * If @force is true, the medium is about to be yanked out forcefully.
437 */
438void blk_dev_eject_request(BlockBackend *blk, bool force)
4be74634 439{
a7f53e26
MA
440 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
441 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
442 }
4be74634
MA
443}
444
a7f53e26
MA
445/*
446 * Does @blk's attached device model have a tray, and is it open?
447 */
448bool blk_dev_is_tray_open(BlockBackend *blk)
4be74634 449{
a7f53e26
MA
450 if (blk->dev_ops && blk->dev_ops->is_tray_open) {
451 return blk->dev_ops->is_tray_open(blk->dev_opaque);
452 }
453 return false;
454}
455
456/*
457 * Does @blk's attached device model have the medium locked?
458 * %false if the device model has no such lock.
459 */
460bool blk_dev_is_medium_locked(BlockBackend *blk)
461{
462 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
463 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
464 }
465 return false;
466}
467
468/*
469 * Notify @blk's attached device model of a backend size change.
470 */
471void blk_dev_resize_cb(BlockBackend *blk)
472{
473 if (blk->dev_ops && blk->dev_ops->resize_cb) {
474 blk->dev_ops->resize_cb(blk->dev_opaque);
475 }
476}
477
478void blk_iostatus_enable(BlockBackend *blk)
479{
373340b2
HR
480 blk->iostatus_enabled = true;
481 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
482}
483
484/* The I/O status is only enabled if the drive explicitly
485 * enables it _and_ the VM is configured to stop on errors */
486bool blk_iostatus_is_enabled(const BlockBackend *blk)
487{
488 return (blk->iostatus_enabled &&
489 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
490 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
491 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
492}
493
494BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
495{
496 return blk->iostatus;
497}
498
499void blk_iostatus_disable(BlockBackend *blk)
500{
501 blk->iostatus_enabled = false;
502}
503
504void blk_iostatus_reset(BlockBackend *blk)
505{
506 if (blk_iostatus_is_enabled(blk)) {
507 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
508 if (blk->bs && blk->bs->job) {
509 block_job_iostatus_reset(blk->bs->job);
510 }
511 }
512}
513
514void blk_iostatus_set_err(BlockBackend *blk, int error)
515{
516 assert(blk_iostatus_is_enabled(blk));
517 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
518 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
519 BLOCK_DEVICE_IO_STATUS_FAILED;
520 }
4be74634
MA
521}
522
e7f7d676
HR
523static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
524 size_t size)
525{
526 int64_t len;
527
528 if (size > INT_MAX) {
529 return -EIO;
530 }
531
c09ba36c 532 if (!blk_is_available(blk)) {
e7f7d676
HR
533 return -ENOMEDIUM;
534 }
535
536 len = blk_getlength(blk);
537 if (len < 0) {
538 return len;
539 }
540
541 if (offset < 0) {
542 return -EIO;
543 }
544
545 if (offset > len || len - offset < size) {
546 return -EIO;
547 }
548
549 return 0;
550}
551
552static int blk_check_request(BlockBackend *blk, int64_t sector_num,
553 int nb_sectors)
554{
555 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
556 return -EIO;
557 }
558
559 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
560 return -EIO;
561 }
562
563 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
564 nb_sectors * BDRV_SECTOR_SIZE);
565}
566
4be74634
MA
567int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
568 int nb_sectors)
569{
e7f7d676
HR
570 int ret = blk_check_request(blk, sector_num, nb_sectors);
571 if (ret < 0) {
572 return ret;
573 }
574
4be74634
MA
575 return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
576}
577
578int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
579 int nb_sectors)
580{
e7f7d676
HR
581 int ret = blk_check_request(blk, sector_num, nb_sectors);
582 if (ret < 0) {
583 return ret;
584 }
585
4be74634
MA
586 return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
587}
588
589int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
590 int nb_sectors)
591{
e7f7d676
HR
592 int ret = blk_check_request(blk, sector_num, nb_sectors);
593 if (ret < 0) {
594 return ret;
595 }
596
4be74634
MA
597 return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
598}
599
0df89e8e
KW
600int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
601 int nb_sectors, BdrvRequestFlags flags)
602{
603 int ret = blk_check_request(blk, sector_num, nb_sectors);
604 if (ret < 0) {
605 return ret;
606 }
607
608 return bdrv_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
609}
610
e7f7d676
HR
611static void error_callback_bh(void *opaque)
612{
613 struct BlockBackendAIOCB *acb = opaque;
614 qemu_bh_delete(acb->bh);
615 acb->common.cb(acb->common.opaque, acb->ret);
616 qemu_aio_unref(acb);
617}
618
619static BlockAIOCB *abort_aio_request(BlockBackend *blk, BlockCompletionFunc *cb,
620 void *opaque, int ret)
621{
622 struct BlockBackendAIOCB *acb;
623 QEMUBH *bh;
624
625 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
4981bdec 626 acb->blk = blk;
e7f7d676
HR
627 acb->ret = ret;
628
629 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
630 acb->bh = bh;
631 qemu_bh_schedule(bh);
632
633 return &acb->common;
634}
635
4be74634
MA
636BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
637 int nb_sectors, BdrvRequestFlags flags,
638 BlockCompletionFunc *cb, void *opaque)
639{
e7f7d676
HR
640 int ret = blk_check_request(blk, sector_num, nb_sectors);
641 if (ret < 0) {
642 return abort_aio_request(blk, cb, opaque, ret);
643 }
644
4be74634
MA
645 return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
646 cb, opaque);
647}
648
649int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
650{
e7f7d676
HR
651 int ret = blk_check_byte_request(blk, offset, count);
652 if (ret < 0) {
653 return ret;
654 }
655
4be74634
MA
656 return bdrv_pread(blk->bs, offset, buf, count);
657}
658
659int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
660{
e7f7d676
HR
661 int ret = blk_check_byte_request(blk, offset, count);
662 if (ret < 0) {
663 return ret;
664 }
665
4be74634
MA
666 return bdrv_pwrite(blk->bs, offset, buf, count);
667}
668
669int64_t blk_getlength(BlockBackend *blk)
670{
c09ba36c
HR
671 if (!blk_is_available(blk)) {
672 return -ENOMEDIUM;
673 }
674
4be74634
MA
675 return bdrv_getlength(blk->bs);
676}
677
678void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
679{
a46fc9c9
HR
680 if (!blk->bs) {
681 *nb_sectors_ptr = 0;
682 } else {
683 bdrv_get_geometry(blk->bs, nb_sectors_ptr);
684 }
4be74634
MA
685}
686
1ef01253
HR
687int64_t blk_nb_sectors(BlockBackend *blk)
688{
c09ba36c
HR
689 if (!blk_is_available(blk)) {
690 return -ENOMEDIUM;
691 }
692
1ef01253
HR
693 return bdrv_nb_sectors(blk->bs);
694}
695
4be74634
MA
696BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
697 QEMUIOVector *iov, int nb_sectors,
698 BlockCompletionFunc *cb, void *opaque)
699{
e7f7d676
HR
700 int ret = blk_check_request(blk, sector_num, nb_sectors);
701 if (ret < 0) {
702 return abort_aio_request(blk, cb, opaque, ret);
703 }
704
4be74634
MA
705 return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
706}
707
708BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
709 QEMUIOVector *iov, int nb_sectors,
710 BlockCompletionFunc *cb, void *opaque)
711{
e7f7d676
HR
712 int ret = blk_check_request(blk, sector_num, nb_sectors);
713 if (ret < 0) {
714 return abort_aio_request(blk, cb, opaque, ret);
715 }
716
4be74634
MA
717 return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
718}
719
720BlockAIOCB *blk_aio_flush(BlockBackend *blk,
721 BlockCompletionFunc *cb, void *opaque)
722{
c09ba36c
HR
723 if (!blk_is_available(blk)) {
724 return abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
725 }
726
4be74634
MA
727 return bdrv_aio_flush(blk->bs, cb, opaque);
728}
729
730BlockAIOCB *blk_aio_discard(BlockBackend *blk,
731 int64_t sector_num, int nb_sectors,
732 BlockCompletionFunc *cb, void *opaque)
733{
e7f7d676
HR
734 int ret = blk_check_request(blk, sector_num, nb_sectors);
735 if (ret < 0) {
736 return abort_aio_request(blk, cb, opaque, ret);
737 }
738
4be74634
MA
739 return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
740}
741
742void blk_aio_cancel(BlockAIOCB *acb)
743{
744 bdrv_aio_cancel(acb);
745}
746
747void blk_aio_cancel_async(BlockAIOCB *acb)
748{
749 bdrv_aio_cancel_async(acb);
750}
751
752int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
753{
e7f7d676
HR
754 int i, ret;
755
756 for (i = 0; i < num_reqs; i++) {
757 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
758 if (ret < 0) {
759 return ret;
760 }
761 }
762
4be74634
MA
763 return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
764}
765
766int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
767{
c09ba36c
HR
768 if (!blk_is_available(blk)) {
769 return -ENOMEDIUM;
770 }
771
4be74634
MA
772 return bdrv_ioctl(blk->bs, req, buf);
773}
774
775BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
776 BlockCompletionFunc *cb, void *opaque)
777{
c09ba36c
HR
778 if (!blk_is_available(blk)) {
779 return abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
780 }
781
4be74634
MA
782 return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
783}
784
2bb0dce7
HR
785int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
786{
e7f7d676
HR
787 int ret = blk_check_request(blk, sector_num, nb_sectors);
788 if (ret < 0) {
789 return ret;
790 }
791
2bb0dce7
HR
792 return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
793}
794
795int blk_co_flush(BlockBackend *blk)
796{
c09ba36c
HR
797 if (!blk_is_available(blk)) {
798 return -ENOMEDIUM;
799 }
800
2bb0dce7
HR
801 return bdrv_co_flush(blk->bs);
802}
803
4be74634
MA
804int blk_flush(BlockBackend *blk)
805{
c09ba36c
HR
806 if (!blk_is_available(blk)) {
807 return -ENOMEDIUM;
808 }
809
4be74634
MA
810 return bdrv_flush(blk->bs);
811}
812
813int blk_flush_all(void)
814{
815 return bdrv_flush_all();
816}
817
97b0385a
AY
818void blk_drain(BlockBackend *blk)
819{
a46fc9c9
HR
820 if (blk->bs) {
821 bdrv_drain(blk->bs);
822 }
97b0385a
AY
823}
824
4be74634
MA
825void blk_drain_all(void)
826{
827 bdrv_drain_all();
828}
829
373340b2
HR
830void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
831 BlockdevOnError on_write_error)
832{
833 blk->on_read_error = on_read_error;
834 blk->on_write_error = on_write_error;
835}
836
4be74634
MA
837BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
838{
373340b2 839 return is_read ? blk->on_read_error : blk->on_write_error;
4be74634
MA
840}
841
842BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
843 int error)
844{
373340b2
HR
845 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
846
847 switch (on_err) {
848 case BLOCKDEV_ON_ERROR_ENOSPC:
849 return (error == ENOSPC) ?
850 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
851 case BLOCKDEV_ON_ERROR_STOP:
852 return BLOCK_ERROR_ACTION_STOP;
853 case BLOCKDEV_ON_ERROR_REPORT:
854 return BLOCK_ERROR_ACTION_REPORT;
855 case BLOCKDEV_ON_ERROR_IGNORE:
856 return BLOCK_ERROR_ACTION_IGNORE;
857 default:
858 abort();
859 }
4be74634
MA
860}
861
373340b2
HR
862static void send_qmp_error_event(BlockBackend *blk,
863 BlockErrorAction action,
864 bool is_read, int error)
865{
866 IoOperationType optype;
867
868 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
869 qapi_event_send_block_io_error(blk_name(blk), optype, action,
870 blk_iostatus_is_enabled(blk),
871 error == ENOSPC, strerror(error),
872 &error_abort);
873}
874
875/* This is done by device models because, while the block layer knows
876 * about the error, it does not know whether an operation comes from
877 * the device or the block layer (from a job, for example).
878 */
4be74634
MA
879void blk_error_action(BlockBackend *blk, BlockErrorAction action,
880 bool is_read, int error)
881{
373340b2
HR
882 assert(error >= 0);
883
884 if (action == BLOCK_ERROR_ACTION_STOP) {
885 /* First set the iostatus, so that "info block" returns an iostatus
886 * that matches the events raised so far (an additional error iostatus
887 * is fine, but not a lost one).
888 */
889 blk_iostatus_set_err(blk, error);
890
891 /* Then raise the request to stop the VM and the event.
892 * qemu_system_vmstop_request_prepare has two effects. First,
893 * it ensures that the STOP event always comes after the
894 * BLOCK_IO_ERROR event. Second, it ensures that even if management
895 * can observe the STOP event and do a "cont" before the STOP
896 * event is issued, the VM will not stop. In this case, vm_start()
897 * also ensures that the STOP/RESUME pair of events is emitted.
898 */
899 qemu_system_vmstop_request_prepare();
900 send_qmp_error_event(blk, action, is_read, error);
901 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
902 } else {
903 send_qmp_error_event(blk, action, is_read, error);
904 }
4be74634
MA
905}
906
907int blk_is_read_only(BlockBackend *blk)
908{
061959e8
HR
909 if (blk->bs) {
910 return bdrv_is_read_only(blk->bs);
911 } else {
912 return blk->root_state.read_only;
913 }
4be74634
MA
914}
915
916int blk_is_sg(BlockBackend *blk)
917{
a46fc9c9
HR
918 if (!blk->bs) {
919 return 0;
920 }
921
4be74634
MA
922 return bdrv_is_sg(blk->bs);
923}
924
925int blk_enable_write_cache(BlockBackend *blk)
926{
061959e8
HR
927 if (blk->bs) {
928 return bdrv_enable_write_cache(blk->bs);
929 } else {
930 return !!(blk->root_state.open_flags & BDRV_O_CACHE_WB);
931 }
4be74634
MA
932}
933
934void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
935{
061959e8
HR
936 if (blk->bs) {
937 bdrv_set_enable_write_cache(blk->bs, wce);
938 } else {
939 if (wce) {
940 blk->root_state.open_flags |= BDRV_O_CACHE_WB;
941 } else {
942 blk->root_state.open_flags &= ~BDRV_O_CACHE_WB;
943 }
944 }
4be74634
MA
945}
946
2bb0dce7
HR
947void blk_invalidate_cache(BlockBackend *blk, Error **errp)
948{
c09ba36c
HR
949 if (!blk->bs) {
950 error_setg(errp, "Device '%s' has no medium", blk->name);
951 return;
952 }
953
2bb0dce7
HR
954 bdrv_invalidate_cache(blk->bs, errp);
955}
956
e031f750 957bool blk_is_inserted(BlockBackend *blk)
4be74634 958{
db0284f8
HR
959 return blk->bs && bdrv_is_inserted(blk->bs);
960}
961
962bool blk_is_available(BlockBackend *blk)
963{
964 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
4be74634
MA
965}
966
967void blk_lock_medium(BlockBackend *blk, bool locked)
968{
a46fc9c9
HR
969 if (blk->bs) {
970 bdrv_lock_medium(blk->bs, locked);
971 }
4be74634
MA
972}
973
974void blk_eject(BlockBackend *blk, bool eject_flag)
975{
a46fc9c9
HR
976 if (blk->bs) {
977 bdrv_eject(blk->bs, eject_flag);
978 }
4be74634
MA
979}
980
981int blk_get_flags(BlockBackend *blk)
982{
061959e8
HR
983 if (blk->bs) {
984 return bdrv_get_flags(blk->bs);
985 } else {
986 return blk->root_state.open_flags;
987 }
4be74634
MA
988}
989
454057b7
PL
990int blk_get_max_transfer_length(BlockBackend *blk)
991{
a46fc9c9
HR
992 if (blk->bs) {
993 return blk->bs->bl.max_transfer_length;
994 } else {
995 return 0;
996 }
454057b7
PL
997}
998
4be74634
MA
999void blk_set_guest_block_size(BlockBackend *blk, int align)
1000{
68e9ec01 1001 blk->guest_block_size = align;
4be74634
MA
1002}
1003
1004void *blk_blockalign(BlockBackend *blk, size_t size)
1005{
1006 return qemu_blockalign(blk ? blk->bs : NULL, size);
1007}
1008
1009bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1010{
a46fc9c9
HR
1011 if (!blk->bs) {
1012 return false;
1013 }
1014
4be74634
MA
1015 return bdrv_op_is_blocked(blk->bs, op, errp);
1016}
1017
1018void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1019{
a46fc9c9
HR
1020 if (blk->bs) {
1021 bdrv_op_unblock(blk->bs, op, reason);
1022 }
4be74634
MA
1023}
1024
1025void blk_op_block_all(BlockBackend *blk, Error *reason)
1026{
a46fc9c9
HR
1027 if (blk->bs) {
1028 bdrv_op_block_all(blk->bs, reason);
1029 }
4be74634
MA
1030}
1031
1032void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1033{
a46fc9c9
HR
1034 if (blk->bs) {
1035 bdrv_op_unblock_all(blk->bs, reason);
1036 }
4be74634
MA
1037}
1038
1039AioContext *blk_get_aio_context(BlockBackend *blk)
1040{
4981bdec
HR
1041 if (blk->bs) {
1042 return bdrv_get_aio_context(blk->bs);
1043 } else {
1044 return qemu_get_aio_context();
1045 }
1046}
1047
1048static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1049{
1050 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1051 return blk_get_aio_context(blk_acb->blk);
4be74634
MA
1052}
1053
1054void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1055{
a46fc9c9
HR
1056 if (blk->bs) {
1057 bdrv_set_aio_context(blk->bs, new_context);
1058 }
4be74634
MA
1059}
1060
2019ba0a
HR
1061void blk_add_aio_context_notifier(BlockBackend *blk,
1062 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1063 void (*detach_aio_context)(void *opaque), void *opaque)
1064{
a46fc9c9
HR
1065 if (blk->bs) {
1066 bdrv_add_aio_context_notifier(blk->bs, attached_aio_context,
1067 detach_aio_context, opaque);
1068 }
2019ba0a
HR
1069}
1070
1071void blk_remove_aio_context_notifier(BlockBackend *blk,
1072 void (*attached_aio_context)(AioContext *,
1073 void *),
1074 void (*detach_aio_context)(void *),
1075 void *opaque)
1076{
a46fc9c9
HR
1077 if (blk->bs) {
1078 bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context,
1079 detach_aio_context, opaque);
1080 }
2019ba0a
HR
1081}
1082
2c28b21f
HR
1083void blk_add_close_notifier(BlockBackend *blk, Notifier *notify)
1084{
a46fc9c9
HR
1085 if (blk->bs) {
1086 bdrv_add_close_notifier(blk->bs, notify);
1087 }
2c28b21f
HR
1088}
1089
4be74634
MA
1090void blk_io_plug(BlockBackend *blk)
1091{
a46fc9c9
HR
1092 if (blk->bs) {
1093 bdrv_io_plug(blk->bs);
1094 }
4be74634
MA
1095}
1096
1097void blk_io_unplug(BlockBackend *blk)
1098{
a46fc9c9
HR
1099 if (blk->bs) {
1100 bdrv_io_unplug(blk->bs);
1101 }
4be74634
MA
1102}
1103
1104BlockAcctStats *blk_get_stats(BlockBackend *blk)
1105{
7f0e9da6 1106 return &blk->stats;
4be74634
MA
1107}
1108
1109void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1110 BlockCompletionFunc *cb, void *opaque)
1111{
1112 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1113}
1ef01253
HR
1114
1115int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
1116 int nb_sectors, BdrvRequestFlags flags)
1117{
e7f7d676
HR
1118 int ret = blk_check_request(blk, sector_num, nb_sectors);
1119 if (ret < 0) {
1120 return ret;
1121 }
1122
1ef01253
HR
1123 return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
1124}
1125
1126int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1127 const uint8_t *buf, int nb_sectors)
1128{
e7f7d676
HR
1129 int ret = blk_check_request(blk, sector_num, nb_sectors);
1130 if (ret < 0) {
1131 return ret;
1132 }
1133
1ef01253
HR
1134 return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
1135}
1136
1137int blk_truncate(BlockBackend *blk, int64_t offset)
1138{
c09ba36c
HR
1139 if (!blk_is_available(blk)) {
1140 return -ENOMEDIUM;
1141 }
1142
1ef01253
HR
1143 return bdrv_truncate(blk->bs, offset);
1144}
1145
1146int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1147{
e7f7d676
HR
1148 int ret = blk_check_request(blk, sector_num, nb_sectors);
1149 if (ret < 0) {
1150 return ret;
1151 }
1152
1ef01253
HR
1153 return bdrv_discard(blk->bs, sector_num, nb_sectors);
1154}
1155
1156int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1157 int64_t pos, int size)
1158{
c09ba36c
HR
1159 if (!blk_is_available(blk)) {
1160 return -ENOMEDIUM;
1161 }
1162
1ef01253
HR
1163 return bdrv_save_vmstate(blk->bs, buf, pos, size);
1164}
1165
1166int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1167{
c09ba36c
HR
1168 if (!blk_is_available(blk)) {
1169 return -ENOMEDIUM;
1170 }
1171
1ef01253
HR
1172 return bdrv_load_vmstate(blk->bs, buf, pos, size);
1173}
f0272c4d
ET
1174
1175int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1176{
c09ba36c
HR
1177 if (!blk_is_available(blk)) {
1178 return -ENOMEDIUM;
1179 }
1180
f0272c4d
ET
1181 return bdrv_probe_blocksizes(blk->bs, bsz);
1182}
1183
1184int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1185{
c09ba36c
HR
1186 if (!blk_is_available(blk)) {
1187 return -ENOMEDIUM;
1188 }
1189
f0272c4d
ET
1190 return bdrv_probe_geometry(blk->bs, geo);
1191}
281d22d8
HR
1192
1193/*
1194 * Updates the BlockBackendRootState object with data from the currently
1195 * attached BlockDriverState.
1196 */
1197void blk_update_root_state(BlockBackend *blk)
1198{
1199 assert(blk->bs);
1200
1201 blk->root_state.open_flags = blk->bs->open_flags;
1202 blk->root_state.read_only = blk->bs->read_only;
1203 blk->root_state.detect_zeroes = blk->bs->detect_zeroes;
1204
1205 if (blk->root_state.throttle_group) {
1206 g_free(blk->root_state.throttle_group);
1207 throttle_group_unref(blk->root_state.throttle_state);
1208 }
1209 if (blk->bs->throttle_state) {
1210 const char *name = throttle_group_get_name(blk->bs);
1211 blk->root_state.throttle_group = g_strdup(name);
1212 blk->root_state.throttle_state = throttle_group_incref(name);
1213 } else {
1214 blk->root_state.throttle_group = NULL;
1215 blk->root_state.throttle_state = NULL;
1216 }
1217}
1218
1219BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1220{
1221 return &blk->root_state;
1222}