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