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