]>
Commit | Line | Data |
---|---|---|
26f54e9a MA |
1 | /* |
2 | * QEMU Block backends | |
3 | * | |
60cb2fa7 | 4 | * Copyright (C) 2014-2016 Red Hat, Inc. |
26f54e9a MA |
5 | * |
6 | * Authors: | |
7 | * Markus Armbruster <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" |
13d4ff07 | 18 | #include "hw/qdev-core.h" |
18e46a03 | 19 | #include "sysemu/blockdev.h" |
54d31236 | 20 | #include "sysemu/runstate.h" |
e4ec5ad4 PD |
21 | #include "sysemu/sysemu.h" |
22 | #include "sysemu/replay.h" | |
e688df6b | 23 | #include "qapi/error.h" |
9af23989 | 24 | #include "qapi/qapi-events-block.h" |
f348b6d1 | 25 | #include "qemu/id.h" |
db725815 | 26 | #include "qemu/main-loop.h" |
922a01a0 | 27 | #include "qemu/option.h" |
1e98fefd | 28 | #include "trace.h" |
5f7772c4 | 29 | #include "migration/misc.h" |
a7f53e26 MA |
30 | |
31 | /* Number of coroutines to reserve per attached device model */ | |
32 | #define COROUTINE_POOL_RESERVATION 64 | |
26f54e9a | 33 | |
1bf1cbc9 KW |
34 | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ |
35 | ||
4981bdec HR |
36 | static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb); |
37 | ||
d03654ea SH |
38 | typedef struct BlockBackendAioNotifier { |
39 | void (*attached_aio_context)(AioContext *new_context, void *opaque); | |
40 | void (*detach_aio_context)(void *opaque); | |
41 | void *opaque; | |
42 | QLIST_ENTRY(BlockBackendAioNotifier) list; | |
43 | } BlockBackendAioNotifier; | |
44 | ||
26f54e9a MA |
45 | struct BlockBackend { |
46 | char *name; | |
47 | int refcnt; | |
f21d96d0 | 48 | BdrvChild *root; |
d861ab3a | 49 | AioContext *ctx; |
26f8b3a8 | 50 | DriveInfo *legacy_dinfo; /* null unless created by drive_new() */ |
2cf22d6a | 51 | QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */ |
9492b0b9 | 52 | QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */ |
f2cd875d | 53 | BlockBackendPublic public; |
a7f53e26 | 54 | |
d09ea2d2 | 55 | DeviceState *dev; /* attached device model, if any */ |
a7f53e26 MA |
56 | const BlockDevOps *dev_ops; |
57 | void *dev_opaque; | |
68e9ec01 HR |
58 | |
59 | /* the block size for which the guest device expects atomicity */ | |
60 | int guest_block_size; | |
7f0e9da6 | 61 | |
281d22d8 HR |
62 | /* If the BDS tree is removed, some of its options are stored here (which |
63 | * can be used to restore those options in the new BDS on insert) */ | |
64 | BlockBackendRootState root_state; | |
65 | ||
bfd18d1e KW |
66 | bool enable_write_cache; |
67 | ||
7f0e9da6 HR |
68 | /* I/O stats (display with "info blockstats"). */ |
69 | BlockAcctStats stats; | |
373340b2 HR |
70 | |
71 | BlockdevOnError on_read_error, on_write_error; | |
72 | bool iostatus_enabled; | |
73 | BlockDeviceIoStatus iostatus; | |
3301f6c6 | 74 | |
981776b3 KW |
75 | uint64_t perm; |
76 | uint64_t shared_perm; | |
d35ff5e6 | 77 | bool disable_perm; |
981776b3 | 78 | |
980b0f94 | 79 | bool allow_aio_context_change; |
c10c9d96 KW |
80 | bool allow_write_beyond_eof; |
81 | ||
3301f6c6 | 82 | NotifierList remove_bs_notifiers, insert_bs_notifiers; |
d03654ea | 83 | QLIST_HEAD(, BlockBackendAioNotifier) aio_notifiers; |
f4d9cc88 JS |
84 | |
85 | int quiesce_counter; | |
cf312932 KW |
86 | CoQueue queued_requests; |
87 | bool disable_request_queuing; | |
88 | ||
5f7772c4 | 89 | VMChangeStateEntry *vmsh; |
ca2e2144 | 90 | bool force_allow_inactivate; |
33f2a757 SH |
91 | |
92 | /* Number of in-flight aio requests. BlockDriverState also counts | |
93 | * in-flight requests but aio requests can exist even when blk->root is | |
94 | * NULL, so we cannot rely on its counter for that case. | |
95 | * Accessed with atomic ops. | |
96 | */ | |
97 | unsigned int in_flight; | |
26f54e9a MA |
98 | }; |
99 | ||
e7f7d676 HR |
100 | typedef struct BlockBackendAIOCB { |
101 | BlockAIOCB common; | |
4981bdec | 102 | BlockBackend *blk; |
e7f7d676 HR |
103 | int ret; |
104 | } BlockBackendAIOCB; | |
105 | ||
106 | static const AIOCBInfo block_backend_aiocb_info = { | |
4981bdec | 107 | .get_aio_context = blk_aiocb_get_aio_context, |
e7f7d676 HR |
108 | .aiocb_size = sizeof(BlockBackendAIOCB), |
109 | }; | |
110 | ||
8fb3c76c | 111 | static void drive_info_del(DriveInfo *dinfo); |
7c8eece4 | 112 | static BlockBackend *bdrv_first_blk(BlockDriverState *bs); |
8fb3c76c | 113 | |
2cf22d6a HR |
114 | /* All BlockBackends */ |
115 | static QTAILQ_HEAD(, BlockBackend) block_backends = | |
116 | QTAILQ_HEAD_INITIALIZER(block_backends); | |
117 | ||
9492b0b9 HR |
118 | /* All BlockBackends referenced by the monitor and which are iterated through by |
119 | * blk_next() */ | |
120 | static QTAILQ_HEAD(, BlockBackend) monitor_block_backends = | |
121 | QTAILQ_HEAD_INITIALIZER(monitor_block_backends); | |
26f54e9a | 122 | |
3cdc69d3 | 123 | static void blk_root_inherit_options(BdrvChildRole role, bool parent_is_format, |
272c02ea | 124 | int *child_flags, QDict *child_options, |
f21d96d0 KW |
125 | int parent_flags, QDict *parent_options) |
126 | { | |
127 | /* We're not supposed to call this function for root nodes */ | |
128 | abort(); | |
129 | } | |
c2066af0 | 130 | static void blk_root_drained_begin(BdrvChild *child); |
fe5258a5 | 131 | static bool blk_root_drained_poll(BdrvChild *child); |
e037c09c | 132 | static void blk_root_drained_end(BdrvChild *child, int *drained_end_counter); |
f21d96d0 | 133 | |
5c8cab48 KW |
134 | static void blk_root_change_media(BdrvChild *child, bool load); |
135 | static void blk_root_resize(BdrvChild *child); | |
136 | ||
38475269 KW |
137 | static bool blk_root_can_set_aio_ctx(BdrvChild *child, AioContext *ctx, |
138 | GSList **ignore, Error **errp); | |
139 | static void blk_root_set_aio_ctx(BdrvChild *child, AioContext *ctx, | |
140 | GSList **ignore); | |
141 | ||
b5411555 KW |
142 | static char *blk_root_get_parent_desc(BdrvChild *child) |
143 | { | |
144 | BlockBackend *blk = child->opaque; | |
145 | char *dev_id; | |
146 | ||
147 | if (blk->name) { | |
148 | return g_strdup(blk->name); | |
149 | } | |
150 | ||
151 | dev_id = blk_get_attached_dev_id(blk); | |
152 | if (*dev_id) { | |
153 | return dev_id; | |
154 | } else { | |
155 | /* TODO Callback into the BB owner for something more detailed */ | |
156 | g_free(dev_id); | |
157 | return g_strdup("a block device"); | |
158 | } | |
159 | } | |
160 | ||
4c265bf9 KW |
161 | static const char *blk_root_get_name(BdrvChild *child) |
162 | { | |
163 | return blk_name(child->opaque); | |
164 | } | |
165 | ||
5f7772c4 FZ |
166 | static void blk_vm_state_changed(void *opaque, int running, RunState state) |
167 | { | |
168 | Error *local_err = NULL; | |
169 | BlockBackend *blk = opaque; | |
170 | ||
171 | if (state == RUN_STATE_INMIGRATE) { | |
172 | return; | |
173 | } | |
174 | ||
175 | qemu_del_vm_change_state_handler(blk->vmsh); | |
176 | blk->vmsh = NULL; | |
177 | blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err); | |
178 | if (local_err) { | |
179 | error_report_err(local_err); | |
180 | } | |
181 | } | |
182 | ||
4417ab7a KW |
183 | /* |
184 | * Notifies the user of the BlockBackend that migration has completed. qdev | |
185 | * devices can tighten their permissions in response (specifically revoke | |
186 | * shared write permissions that we needed for storage migration). | |
187 | * | |
188 | * If an error is returned, the VM cannot be allowed to be resumed. | |
189 | */ | |
190 | static void blk_root_activate(BdrvChild *child, Error **errp) | |
191 | { | |
192 | BlockBackend *blk = child->opaque; | |
193 | Error *local_err = NULL; | |
194 | ||
195 | if (!blk->disable_perm) { | |
196 | return; | |
197 | } | |
198 | ||
199 | blk->disable_perm = false; | |
200 | ||
5f7772c4 FZ |
201 | blk_set_perm(blk, blk->perm, BLK_PERM_ALL, &local_err); |
202 | if (local_err) { | |
203 | error_propagate(errp, local_err); | |
204 | blk->disable_perm = true; | |
205 | return; | |
206 | } | |
207 | ||
208 | if (runstate_check(RUN_STATE_INMIGRATE)) { | |
209 | /* Activation can happen when migration process is still active, for | |
210 | * example when nbd_server_add is called during non-shared storage | |
211 | * migration. Defer the shared_perm update to migration completion. */ | |
212 | if (!blk->vmsh) { | |
213 | blk->vmsh = qemu_add_vm_change_state_handler(blk_vm_state_changed, | |
214 | blk); | |
215 | } | |
216 | return; | |
217 | } | |
218 | ||
4417ab7a KW |
219 | blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err); |
220 | if (local_err) { | |
221 | error_propagate(errp, local_err); | |
222 | blk->disable_perm = true; | |
223 | return; | |
224 | } | |
225 | } | |
226 | ||
ca2e2144 FZ |
227 | void blk_set_force_allow_inactivate(BlockBackend *blk) |
228 | { | |
229 | blk->force_allow_inactivate = true; | |
230 | } | |
231 | ||
c16de8f5 FZ |
232 | static bool blk_can_inactivate(BlockBackend *blk) |
233 | { | |
ca2e2144 | 234 | /* If it is a guest device, inactivate is ok. */ |
c16de8f5 FZ |
235 | if (blk->dev || blk_name(blk)[0]) { |
236 | return true; | |
237 | } | |
238 | ||
ca2e2144 FZ |
239 | /* Inactivating means no more writes to the image can be done, |
240 | * even if those writes would be changes invisible to the | |
241 | * guest. For block job BBs that satisfy this, we can just allow | |
242 | * it. This is the case for mirror job source, which is required | |
243 | * by libvirt non-shared block migration. */ | |
244 | if (!(blk->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED))) { | |
245 | return true; | |
246 | } | |
247 | ||
248 | return blk->force_allow_inactivate; | |
c16de8f5 FZ |
249 | } |
250 | ||
cfa1a572 KW |
251 | static int blk_root_inactivate(BdrvChild *child) |
252 | { | |
253 | BlockBackend *blk = child->opaque; | |
254 | ||
255 | if (blk->disable_perm) { | |
256 | return 0; | |
257 | } | |
258 | ||
c16de8f5 | 259 | if (!blk_can_inactivate(blk)) { |
cfa1a572 KW |
260 | return -EPERM; |
261 | } | |
262 | ||
263 | blk->disable_perm = true; | |
264 | if (blk->root) { | |
265 | bdrv_child_try_set_perm(blk->root, 0, BLK_PERM_ALL, &error_abort); | |
266 | } | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
d03654ea SH |
271 | static void blk_root_attach(BdrvChild *child) |
272 | { | |
273 | BlockBackend *blk = child->opaque; | |
274 | BlockBackendAioNotifier *notifier; | |
275 | ||
276 | trace_blk_root_attach(child, blk, child->bs); | |
277 | ||
278 | QLIST_FOREACH(notifier, &blk->aio_notifiers, list) { | |
279 | bdrv_add_aio_context_notifier(child->bs, | |
280 | notifier->attached_aio_context, | |
281 | notifier->detach_aio_context, | |
282 | notifier->opaque); | |
283 | } | |
284 | } | |
285 | ||
286 | static void blk_root_detach(BdrvChild *child) | |
287 | { | |
288 | BlockBackend *blk = child->opaque; | |
289 | BlockBackendAioNotifier *notifier; | |
290 | ||
291 | trace_blk_root_detach(child, blk, child->bs); | |
292 | ||
293 | QLIST_FOREACH(notifier, &blk->aio_notifiers, list) { | |
294 | bdrv_remove_aio_context_notifier(child->bs, | |
295 | notifier->attached_aio_context, | |
296 | notifier->detach_aio_context, | |
297 | notifier->opaque); | |
298 | } | |
299 | } | |
300 | ||
bd86fb99 | 301 | static const BdrvChildClass child_root = { |
c2066af0 KW |
302 | .inherit_options = blk_root_inherit_options, |
303 | ||
5c8cab48 KW |
304 | .change_media = blk_root_change_media, |
305 | .resize = blk_root_resize, | |
4c265bf9 | 306 | .get_name = blk_root_get_name, |
b5411555 | 307 | .get_parent_desc = blk_root_get_parent_desc, |
5c8cab48 | 308 | |
c2066af0 | 309 | .drained_begin = blk_root_drained_begin, |
fe5258a5 | 310 | .drained_poll = blk_root_drained_poll, |
c2066af0 | 311 | .drained_end = blk_root_drained_end, |
4417ab7a KW |
312 | |
313 | .activate = blk_root_activate, | |
cfa1a572 | 314 | .inactivate = blk_root_inactivate, |
d03654ea SH |
315 | |
316 | .attach = blk_root_attach, | |
317 | .detach = blk_root_detach, | |
38475269 KW |
318 | |
319 | .can_set_aio_ctx = blk_root_can_set_aio_ctx, | |
320 | .set_aio_ctx = blk_root_set_aio_ctx, | |
f21d96d0 KW |
321 | }; |
322 | ||
26f54e9a | 323 | /* |
efaa7c4e | 324 | * Create a new BlockBackend with a reference count of one. |
6d0eb64d KW |
325 | * |
326 | * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions | |
327 | * to request for a block driver node that is attached to this BlockBackend. | |
328 | * @shared_perm is a bitmask which describes which permissions may be granted | |
329 | * to other users of the attached node. | |
330 | * Both sets of permissions can be changed later using blk_set_perm(). | |
331 | * | |
26f54e9a MA |
332 | * Return the new BlockBackend on success, null on failure. |
333 | */ | |
d861ab3a | 334 | BlockBackend *blk_new(AioContext *ctx, uint64_t perm, uint64_t shared_perm) |
26f54e9a MA |
335 | { |
336 | BlockBackend *blk; | |
337 | ||
26f54e9a | 338 | blk = g_new0(BlockBackend, 1); |
26f54e9a | 339 | blk->refcnt = 1; |
d861ab3a | 340 | blk->ctx = ctx; |
6d0eb64d KW |
341 | blk->perm = perm; |
342 | blk->shared_perm = shared_perm; | |
0c3169df KW |
343 | blk_set_enable_write_cache(blk, true); |
344 | ||
cb53460b KW |
345 | blk->on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
346 | blk->on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; | |
347 | ||
9caa6f3d | 348 | block_acct_init(&blk->stats); |
27ccdd52 | 349 | |
cf312932 | 350 | qemu_co_queue_init(&blk->queued_requests); |
3301f6c6 HR |
351 | notifier_list_init(&blk->remove_bs_notifiers); |
352 | notifier_list_init(&blk->insert_bs_notifiers); | |
d03654ea | 353 | QLIST_INIT(&blk->aio_notifiers); |
27ccdd52 | 354 | |
2cf22d6a | 355 | QTAILQ_INSERT_TAIL(&block_backends, blk, link); |
26f54e9a MA |
356 | return blk; |
357 | } | |
358 | ||
a3aeeab5 EB |
359 | /* |
360 | * Create a new BlockBackend connected to an existing BlockDriverState. | |
361 | * | |
362 | * @perm is a bitmasks of BLK_PERM_* constants which describes the | |
363 | * permissions to request for @bs that is attached to this | |
364 | * BlockBackend. @shared_perm is a bitmask which describes which | |
365 | * permissions may be granted to other users of the attached node. | |
366 | * Both sets of permissions can be changed later using blk_set_perm(). | |
367 | * | |
368 | * Return the new BlockBackend on success, null on failure. | |
369 | */ | |
370 | BlockBackend *blk_new_with_bs(BlockDriverState *bs, uint64_t perm, | |
371 | uint64_t shared_perm, Error **errp) | |
372 | { | |
373 | BlockBackend *blk = blk_new(bdrv_get_aio_context(bs), perm, shared_perm); | |
374 | ||
375 | if (blk_insert_bs(blk, bs, errp) < 0) { | |
376 | blk_unref(blk); | |
377 | return NULL; | |
378 | } | |
379 | return blk; | |
380 | } | |
381 | ||
7e7d56d9 | 382 | /* |
28eb9b12 | 383 | * Creates a new BlockBackend, opens a new BlockDriverState, and connects both. |
d861ab3a | 384 | * The new BlockBackend is in the main AioContext. |
ca49a4fd HR |
385 | * |
386 | * Just as with bdrv_open(), after having called this function the reference to | |
387 | * @options belongs to the block layer (even on failure). | |
388 | * | |
389 | * TODO: Remove @filename and @flags; it should be possible to specify a whole | |
390 | * BDS tree just by specifying the @options QDict (or @reference, | |
391 | * alternatively). At the time of adding this function, this is not possible, | |
392 | * though, so callers of this function have to be able to specify @filename and | |
393 | * @flags. | |
394 | */ | |
efaa7c4e HR |
395 | BlockBackend *blk_new_open(const char *filename, const char *reference, |
396 | QDict *options, int flags, Error **errp) | |
ca49a4fd HR |
397 | { |
398 | BlockBackend *blk; | |
28eb9b12 | 399 | BlockDriverState *bs; |
1f4ad7d3 | 400 | uint64_t perm = 0; |
c62d32f5 KW |
401 | |
402 | /* blk_new_open() is mainly used in .bdrv_create implementations and the | |
403 | * tools where sharing isn't a concern because the BDS stays private, so we | |
404 | * just request permission according to the flags. | |
405 | * | |
406 | * The exceptions are xen_disk and blockdev_init(); in these cases, the | |
407 | * caller of blk_new_open() doesn't make use of the permissions, but they | |
408 | * shouldn't hurt either. We can still share everything here because the | |
409 | * guest devices will add their own blockers if they can't share. */ | |
1f4ad7d3 KW |
410 | if ((flags & BDRV_O_NO_IO) == 0) { |
411 | perm |= BLK_PERM_CONSISTENT_READ; | |
412 | if (flags & BDRV_O_RDWR) { | |
413 | perm |= BLK_PERM_WRITE; | |
414 | } | |
c62d32f5 KW |
415 | } |
416 | if (flags & BDRV_O_RESIZE) { | |
417 | perm |= BLK_PERM_RESIZE; | |
418 | } | |
ca49a4fd | 419 | |
d861ab3a | 420 | blk = blk_new(qemu_get_aio_context(), perm, BLK_PERM_ALL); |
5b363937 HR |
421 | bs = bdrv_open(filename, reference, options, flags, errp); |
422 | if (!bs) { | |
ca49a4fd HR |
423 | blk_unref(blk); |
424 | return NULL; | |
425 | } | |
426 | ||
258b7765 | 427 | blk->root = bdrv_root_attach_child(bs, "root", &child_root, 0, blk->ctx, |
50bfbe93 FZ |
428 | perm, BLK_PERM_ALL, blk, errp); |
429 | if (!blk->root) { | |
50bfbe93 FZ |
430 | blk_unref(blk); |
431 | return NULL; | |
432 | } | |
72e775c7 | 433 | |
ca49a4fd HR |
434 | return blk; |
435 | } | |
436 | ||
26f54e9a MA |
437 | static void blk_delete(BlockBackend *blk) |
438 | { | |
439 | assert(!blk->refcnt); | |
e5e78550 | 440 | assert(!blk->name); |
a7f53e26 | 441 | assert(!blk->dev); |
022cdc9f | 442 | if (blk->public.throttle_group_member.throttle_state) { |
1606e4cf EB |
443 | blk_io_limits_disable(blk); |
444 | } | |
f21d96d0 | 445 | if (blk->root) { |
13855c6b | 446 | blk_remove_bs(blk); |
7e7d56d9 | 447 | } |
5f7772c4 FZ |
448 | if (blk->vmsh) { |
449 | qemu_del_vm_change_state_handler(blk->vmsh); | |
450 | blk->vmsh = NULL; | |
451 | } | |
3301f6c6 HR |
452 | assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers)); |
453 | assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers)); | |
d03654ea | 454 | assert(QLIST_EMPTY(&blk->aio_notifiers)); |
2cf22d6a | 455 | QTAILQ_REMOVE(&block_backends, blk, link); |
18e46a03 | 456 | drive_info_del(blk->legacy_dinfo); |
979e9b03 | 457 | block_acct_cleanup(&blk->stats); |
26f54e9a MA |
458 | g_free(blk); |
459 | } | |
460 | ||
8fb3c76c MA |
461 | static void drive_info_del(DriveInfo *dinfo) |
462 | { | |
463 | if (!dinfo) { | |
464 | return; | |
465 | } | |
466 | qemu_opts_del(dinfo->opts); | |
8fb3c76c MA |
467 | g_free(dinfo); |
468 | } | |
469 | ||
f636ae85 AG |
470 | int blk_get_refcnt(BlockBackend *blk) |
471 | { | |
472 | return blk ? blk->refcnt : 0; | |
473 | } | |
474 | ||
26f54e9a MA |
475 | /* |
476 | * Increment @blk's reference count. | |
477 | * @blk must not be null. | |
478 | */ | |
479 | void blk_ref(BlockBackend *blk) | |
480 | { | |
5ca9d21b | 481 | assert(blk->refcnt > 0); |
26f54e9a MA |
482 | blk->refcnt++; |
483 | } | |
484 | ||
485 | /* | |
486 | * Decrement @blk's reference count. | |
487 | * If this drops it to zero, destroy @blk. | |
488 | * For convenience, do nothing if @blk is null. | |
489 | */ | |
490 | void blk_unref(BlockBackend *blk) | |
491 | { | |
492 | if (blk) { | |
493 | assert(blk->refcnt > 0); | |
5ca9d21b KW |
494 | if (blk->refcnt > 1) { |
495 | blk->refcnt--; | |
496 | } else { | |
497 | blk_drain(blk); | |
498 | /* blk_drain() cannot resurrect blk, nobody held a reference */ | |
499 | assert(blk->refcnt == 1); | |
500 | blk->refcnt = 0; | |
26f54e9a MA |
501 | blk_delete(blk); |
502 | } | |
503 | } | |
504 | } | |
505 | ||
2cf22d6a HR |
506 | /* |
507 | * Behaves similarly to blk_next() but iterates over all BlockBackends, even the | |
508 | * ones which are hidden (i.e. are not referenced by the monitor). | |
509 | */ | |
a429b9b5 | 510 | BlockBackend *blk_all_next(BlockBackend *blk) |
2cf22d6a HR |
511 | { |
512 | return blk ? QTAILQ_NEXT(blk, link) | |
513 | : QTAILQ_FIRST(&block_backends); | |
514 | } | |
515 | ||
d8da3cef HR |
516 | void blk_remove_all_bs(void) |
517 | { | |
74d1b8fc | 518 | BlockBackend *blk = NULL; |
d8da3cef | 519 | |
2cf22d6a | 520 | while ((blk = blk_all_next(blk)) != NULL) { |
d8da3cef HR |
521 | AioContext *ctx = blk_get_aio_context(blk); |
522 | ||
523 | aio_context_acquire(ctx); | |
f21d96d0 | 524 | if (blk->root) { |
d8da3cef HR |
525 | blk_remove_bs(blk); |
526 | } | |
527 | aio_context_release(ctx); | |
528 | } | |
529 | } | |
530 | ||
26f54e9a | 531 | /* |
9492b0b9 | 532 | * Return the monitor-owned BlockBackend after @blk. |
26f54e9a MA |
533 | * If @blk is null, return the first one. |
534 | * Else, return @blk's next sibling, which may be null. | |
535 | * | |
536 | * To iterate over all BlockBackends, do | |
537 | * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { | |
538 | * ... | |
539 | * } | |
540 | */ | |
541 | BlockBackend *blk_next(BlockBackend *blk) | |
542 | { | |
9492b0b9 HR |
543 | return blk ? QTAILQ_NEXT(blk, monitor_link) |
544 | : QTAILQ_FIRST(&monitor_block_backends); | |
26f54e9a MA |
545 | } |
546 | ||
7c8eece4 KW |
547 | /* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by |
548 | * the monitor or attached to a BlockBackend */ | |
88be7b4b | 549 | BlockDriverState *bdrv_next(BdrvNextIterator *it) |
7c8eece4 | 550 | { |
5e003f17 HR |
551 | BlockDriverState *bs, *old_bs; |
552 | ||
553 | /* Must be called from the main loop */ | |
554 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); | |
981f4f57 | 555 | |
7c8eece4 KW |
556 | /* First, return all root nodes of BlockBackends. In order to avoid |
557 | * returning a BDS twice when multiple BBs refer to it, we only return it | |
558 | * if the BB is the first one in the parent list of the BDS. */ | |
559 | if (it->phase == BDRV_NEXT_BACKEND_ROOTS) { | |
5e003f17 HR |
560 | BlockBackend *old_blk = it->blk; |
561 | ||
562 | old_bs = old_blk ? blk_bs(old_blk) : NULL; | |
563 | ||
7c8eece4 KW |
564 | do { |
565 | it->blk = blk_all_next(it->blk); | |
88be7b4b KW |
566 | bs = it->blk ? blk_bs(it->blk) : NULL; |
567 | } while (it->blk && (bs == NULL || bdrv_first_blk(bs) != it->blk)); | |
7c8eece4 | 568 | |
5e003f17 HR |
569 | if (it->blk) { |
570 | blk_ref(it->blk); | |
571 | } | |
572 | blk_unref(old_blk); | |
573 | ||
88be7b4b | 574 | if (bs) { |
5e003f17 HR |
575 | bdrv_ref(bs); |
576 | bdrv_unref(old_bs); | |
88be7b4b | 577 | return bs; |
7c8eece4 KW |
578 | } |
579 | it->phase = BDRV_NEXT_MONITOR_OWNED; | |
5e003f17 HR |
580 | } else { |
581 | old_bs = it->bs; | |
7c8eece4 KW |
582 | } |
583 | ||
584 | /* Then return the monitor-owned BDSes without a BB attached. Ignore all | |
585 | * BDSes that are attached to a BlockBackend here; they have been handled | |
586 | * by the above block already */ | |
981f4f57 | 587 | do { |
7c8eece4 | 588 | it->bs = bdrv_next_monitor_owned(it->bs); |
88be7b4b KW |
589 | bs = it->bs; |
590 | } while (bs && bdrv_has_blk(bs)); | |
591 | ||
5e003f17 HR |
592 | if (bs) { |
593 | bdrv_ref(bs); | |
594 | } | |
595 | bdrv_unref(old_bs); | |
596 | ||
88be7b4b KW |
597 | return bs; |
598 | } | |
599 | ||
5e003f17 | 600 | static void bdrv_next_reset(BdrvNextIterator *it) |
88be7b4b KW |
601 | { |
602 | *it = (BdrvNextIterator) { | |
603 | .phase = BDRV_NEXT_BACKEND_ROOTS, | |
604 | }; | |
5e003f17 | 605 | } |
981f4f57 | 606 | |
5e003f17 HR |
607 | BlockDriverState *bdrv_first(BdrvNextIterator *it) |
608 | { | |
609 | bdrv_next_reset(it); | |
88be7b4b | 610 | return bdrv_next(it); |
981f4f57 HR |
611 | } |
612 | ||
5e003f17 HR |
613 | /* Must be called when aborting a bdrv_next() iteration before |
614 | * bdrv_next() returns NULL */ | |
615 | void bdrv_next_cleanup(BdrvNextIterator *it) | |
616 | { | |
617 | /* Must be called from the main loop */ | |
618 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); | |
619 | ||
620 | if (it->phase == BDRV_NEXT_BACKEND_ROOTS) { | |
621 | if (it->blk) { | |
622 | bdrv_unref(blk_bs(it->blk)); | |
623 | blk_unref(it->blk); | |
624 | } | |
625 | } else { | |
626 | bdrv_unref(it->bs); | |
627 | } | |
628 | ||
629 | bdrv_next_reset(it); | |
630 | } | |
631 | ||
e5e78550 HR |
632 | /* |
633 | * Add a BlockBackend into the list of backends referenced by the monitor, with | |
634 | * the given @name acting as the handle for the monitor. | |
635 | * Strictly for use by blockdev.c. | |
636 | * | |
637 | * @name must not be null or empty. | |
638 | * | |
639 | * Returns true on success and false on failure. In the latter case, an Error | |
640 | * object is returned through @errp. | |
641 | */ | |
642 | bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp) | |
643 | { | |
644 | assert(!blk->name); | |
645 | assert(name && name[0]); | |
646 | ||
647 | if (!id_wellformed(name)) { | |
648 | error_setg(errp, "Invalid device name"); | |
649 | return false; | |
650 | } | |
651 | if (blk_by_name(name)) { | |
652 | error_setg(errp, "Device with id '%s' already exists", name); | |
653 | return false; | |
654 | } | |
655 | if (bdrv_find_node(name)) { | |
656 | error_setg(errp, | |
657 | "Device name '%s' conflicts with an existing node name", | |
658 | name); | |
659 | return false; | |
660 | } | |
661 | ||
662 | blk->name = g_strdup(name); | |
663 | QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link); | |
664 | return true; | |
665 | } | |
666 | ||
667 | /* | |
668 | * Remove a BlockBackend from the list of backends referenced by the monitor. | |
669 | * Strictly for use by blockdev.c. | |
670 | */ | |
671 | void monitor_remove_blk(BlockBackend *blk) | |
672 | { | |
673 | if (!blk->name) { | |
674 | return; | |
675 | } | |
676 | ||
677 | QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link); | |
678 | g_free(blk->name); | |
679 | blk->name = NULL; | |
680 | } | |
681 | ||
26f54e9a | 682 | /* |
7e7d56d9 | 683 | * Return @blk's name, a non-null string. |
e5e78550 | 684 | * Returns an empty string iff @blk is not referenced by the monitor. |
26f54e9a | 685 | */ |
0731a50f | 686 | const char *blk_name(const BlockBackend *blk) |
26f54e9a | 687 | { |
e5e78550 | 688 | return blk->name ?: ""; |
26f54e9a MA |
689 | } |
690 | ||
691 | /* | |
692 | * Return the BlockBackend with name @name if it exists, else null. | |
693 | * @name must not be null. | |
694 | */ | |
695 | BlockBackend *blk_by_name(const char *name) | |
696 | { | |
74d1b8fc | 697 | BlockBackend *blk = NULL; |
26f54e9a MA |
698 | |
699 | assert(name); | |
74d1b8fc | 700 | while ((blk = blk_next(blk)) != NULL) { |
26f54e9a MA |
701 | if (!strcmp(name, blk->name)) { |
702 | return blk; | |
703 | } | |
704 | } | |
705 | return NULL; | |
706 | } | |
7e7d56d9 MA |
707 | |
708 | /* | |
709 | * Return the BlockDriverState attached to @blk if any, else null. | |
710 | */ | |
711 | BlockDriverState *blk_bs(BlockBackend *blk) | |
712 | { | |
f21d96d0 | 713 | return blk->root ? blk->root->bs : NULL; |
7e7d56d9 MA |
714 | } |
715 | ||
7c8eece4 | 716 | static BlockBackend *bdrv_first_blk(BlockDriverState *bs) |
dde33812 KW |
717 | { |
718 | BdrvChild *child; | |
719 | QLIST_FOREACH(child, &bs->parents, next_parent) { | |
bd86fb99 | 720 | if (child->klass == &child_root) { |
7c8eece4 | 721 | return child->opaque; |
dde33812 KW |
722 | } |
723 | } | |
724 | ||
7c8eece4 KW |
725 | return NULL; |
726 | } | |
727 | ||
728 | /* | |
729 | * Returns true if @bs has an associated BlockBackend. | |
730 | */ | |
731 | bool bdrv_has_blk(BlockDriverState *bs) | |
732 | { | |
733 | return bdrv_first_blk(bs) != NULL; | |
dde33812 KW |
734 | } |
735 | ||
b6c1bae5 KW |
736 | /* |
737 | * Returns true if @bs has only BlockBackends as parents. | |
738 | */ | |
739 | bool bdrv_is_root_node(BlockDriverState *bs) | |
740 | { | |
741 | BdrvChild *c; | |
742 | ||
743 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
bd86fb99 | 744 | if (c->klass != &child_root) { |
b6c1bae5 KW |
745 | return false; |
746 | } | |
747 | } | |
748 | ||
749 | return true; | |
750 | } | |
751 | ||
18e46a03 MA |
752 | /* |
753 | * Return @blk's DriveInfo if any, else null. | |
754 | */ | |
755 | DriveInfo *blk_legacy_dinfo(BlockBackend *blk) | |
756 | { | |
757 | return blk->legacy_dinfo; | |
758 | } | |
759 | ||
760 | /* | |
761 | * Set @blk's DriveInfo to @dinfo, and return it. | |
762 | * @blk must not have a DriveInfo set already. | |
763 | * No other BlockBackend may have the same DriveInfo set. | |
764 | */ | |
765 | DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo) | |
766 | { | |
767 | assert(!blk->legacy_dinfo); | |
768 | return blk->legacy_dinfo = dinfo; | |
769 | } | |
770 | ||
771 | /* | |
772 | * Return the BlockBackend with DriveInfo @dinfo. | |
773 | * It must exist. | |
774 | */ | |
775 | BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo) | |
776 | { | |
74d1b8fc | 777 | BlockBackend *blk = NULL; |
18e46a03 | 778 | |
74d1b8fc | 779 | while ((blk = blk_next(blk)) != NULL) { |
18e46a03 MA |
780 | if (blk->legacy_dinfo == dinfo) { |
781 | return blk; | |
782 | } | |
783 | } | |
784 | abort(); | |
785 | } | |
786 | ||
f2cd875d KW |
787 | /* |
788 | * Returns a pointer to the publicly accessible fields of @blk. | |
789 | */ | |
790 | BlockBackendPublic *blk_get_public(BlockBackend *blk) | |
791 | { | |
792 | return &blk->public; | |
793 | } | |
794 | ||
795 | /* | |
796 | * Returns a BlockBackend given the associated @public fields. | |
797 | */ | |
798 | BlockBackend *blk_by_public(BlockBackendPublic *public) | |
799 | { | |
800 | return container_of(public, BlockBackend, public); | |
801 | } | |
802 | ||
1c95f7e1 HR |
803 | /* |
804 | * Disassociates the currently associated BlockDriverState from @blk. | |
805 | */ | |
806 | void blk_remove_bs(BlockBackend *blk) | |
807 | { | |
c89bcf3a | 808 | ThrottleGroupMember *tgm = &blk->public.throttle_group_member; |
632a7735 | 809 | BlockDriverState *bs; |
022cdc9f | 810 | |
3301f6c6 | 811 | notifier_list_notify(&blk->remove_bs_notifiers, blk); |
c89bcf3a | 812 | if (tgm->throttle_state) { |
632a7735 ZL |
813 | bs = blk_bs(blk); |
814 | bdrv_drained_begin(bs); | |
c89bcf3a AG |
815 | throttle_group_detach_aio_context(tgm); |
816 | throttle_group_attach_aio_context(tgm, qemu_get_aio_context()); | |
632a7735 | 817 | bdrv_drained_end(bs); |
a5614993 | 818 | } |
1c95f7e1 | 819 | |
7ca7f0f6 KW |
820 | blk_update_root_state(blk); |
821 | ||
f45280cb GK |
822 | /* bdrv_root_unref_child() will cause blk->root to become stale and may |
823 | * switch to a completion coroutine later on. Let's drain all I/O here | |
824 | * to avoid that and a potential QEMU crash. | |
825 | */ | |
826 | blk_drain(blk); | |
f21d96d0 KW |
827 | bdrv_root_unref_child(blk->root); |
828 | blk->root = NULL; | |
1c95f7e1 HR |
829 | } |
830 | ||
0c3c36d6 HR |
831 | /* |
832 | * Associates a new BlockDriverState with @blk. | |
833 | */ | |
d7086422 | 834 | int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp) |
0c3c36d6 | 835 | { |
c89bcf3a | 836 | ThrottleGroupMember *tgm = &blk->public.throttle_group_member; |
b441dc71 | 837 | bdrv_ref(bs); |
258b7765 | 838 | blk->root = bdrv_root_attach_child(bs, "root", &child_root, 0, blk->ctx, |
d7086422 KW |
839 | blk->perm, blk->shared_perm, blk, errp); |
840 | if (blk->root == NULL) { | |
841 | return -EPERM; | |
842 | } | |
3301f6c6 HR |
843 | |
844 | notifier_list_notify(&blk->insert_bs_notifiers, blk); | |
c89bcf3a AG |
845 | if (tgm->throttle_state) { |
846 | throttle_group_detach_aio_context(tgm); | |
847 | throttle_group_attach_aio_context(tgm, bdrv_get_aio_context(bs)); | |
7ca7f0f6 | 848 | } |
d7086422 KW |
849 | |
850 | return 0; | |
0c3c36d6 HR |
851 | } |
852 | ||
981776b3 KW |
853 | /* |
854 | * Sets the permission bitmasks that the user of the BlockBackend needs. | |
855 | */ | |
856 | int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm, | |
857 | Error **errp) | |
858 | { | |
859 | int ret; | |
860 | ||
d35ff5e6 | 861 | if (blk->root && !blk->disable_perm) { |
981776b3 KW |
862 | ret = bdrv_child_try_set_perm(blk->root, perm, shared_perm, errp); |
863 | if (ret < 0) { | |
864 | return ret; | |
865 | } | |
866 | } | |
867 | ||
868 | blk->perm = perm; | |
869 | blk->shared_perm = shared_perm; | |
870 | ||
871 | return 0; | |
872 | } | |
873 | ||
887354bd KW |
874 | void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm) |
875 | { | |
876 | *perm = blk->perm; | |
877 | *shared_perm = blk->shared_perm; | |
878 | } | |
879 | ||
d09ea2d2 TH |
880 | /* |
881 | * Attach device model @dev to @blk. | |
882 | * Return 0 on success, -EBUSY when a device model is attached already. | |
883 | */ | |
884 | int blk_attach_dev(BlockBackend *blk, DeviceState *dev) | |
4be74634 | 885 | { |
a7f53e26 MA |
886 | if (blk->dev) { |
887 | return -EBUSY; | |
888 | } | |
d35ff5e6 KW |
889 | |
890 | /* While migration is still incoming, we don't need to apply the | |
891 | * permissions of guest device BlockBackends. We might still have a block | |
892 | * job or NBD server writing to the image for storage migration. */ | |
893 | if (runstate_check(RUN_STATE_INMIGRATE)) { | |
894 | blk->disable_perm = true; | |
895 | } | |
896 | ||
84ebe375 | 897 | blk_ref(blk); |
a7f53e26 | 898 | blk->dev = dev; |
373340b2 | 899 | blk_iostatus_reset(blk); |
d35ff5e6 | 900 | |
a7f53e26 | 901 | return 0; |
4be74634 MA |
902 | } |
903 | ||
a7f53e26 MA |
904 | /* |
905 | * Detach device model @dev from @blk. | |
906 | * @dev must be currently attached to @blk. | |
907 | */ | |
d09ea2d2 | 908 | void blk_detach_dev(BlockBackend *blk, DeviceState *dev) |
4be74634 | 909 | { |
a7f53e26 MA |
910 | assert(blk->dev == dev); |
911 | blk->dev = NULL; | |
912 | blk->dev_ops = NULL; | |
913 | blk->dev_opaque = NULL; | |
68e9ec01 | 914 | blk->guest_block_size = 512; |
981776b3 | 915 | blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort); |
84ebe375 | 916 | blk_unref(blk); |
4be74634 MA |
917 | } |
918 | ||
a7f53e26 MA |
919 | /* |
920 | * Return the device model attached to @blk if any, else null. | |
921 | */ | |
d09ea2d2 | 922 | DeviceState *blk_get_attached_dev(BlockBackend *blk) |
a7f53e26 MA |
923 | { |
924 | return blk->dev; | |
925 | } | |
926 | ||
2d76e724 KW |
927 | /* Return the qdev ID, or if no ID is assigned the QOM path, of the block |
928 | * device attached to the BlockBackend. */ | |
77beef83 | 929 | char *blk_get_attached_dev_id(BlockBackend *blk) |
2d76e724 | 930 | { |
d09ea2d2 | 931 | DeviceState *dev = blk->dev; |
2d76e724 KW |
932 | |
933 | if (!dev) { | |
934 | return g_strdup(""); | |
935 | } else if (dev->id) { | |
936 | return g_strdup(dev->id); | |
937 | } | |
602414d1 LM |
938 | |
939 | return object_get_canonical_path(OBJECT(dev)) ?: g_strdup(""); | |
2d76e724 KW |
940 | } |
941 | ||
1c89e1fa KW |
942 | /* |
943 | * Return the BlockBackend which has the device model @dev attached if it | |
944 | * exists, else null. | |
945 | * | |
946 | * @dev must not be null. | |
947 | */ | |
948 | BlockBackend *blk_by_dev(void *dev) | |
949 | { | |
950 | BlockBackend *blk = NULL; | |
951 | ||
952 | assert(dev != NULL); | |
953 | while ((blk = blk_all_next(blk)) != NULL) { | |
954 | if (blk->dev == dev) { | |
955 | return blk; | |
956 | } | |
957 | } | |
958 | return NULL; | |
959 | } | |
960 | ||
a7f53e26 MA |
961 | /* |
962 | * Set @blk's device model callbacks to @ops. | |
963 | * @opaque is the opaque argument to pass to the callbacks. | |
964 | * This is for use by device models. | |
965 | */ | |
966 | void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, | |
967 | void *opaque) | |
968 | { | |
969 | blk->dev_ops = ops; | |
970 | blk->dev_opaque = opaque; | |
f4d9cc88 JS |
971 | |
972 | /* Are we currently quiesced? Should we enforce this right now? */ | |
973 | if (blk->quiesce_counter && ops->drained_begin) { | |
974 | ops->drained_begin(opaque); | |
975 | } | |
a7f53e26 MA |
976 | } |
977 | ||
978 | /* | |
979 | * Notify @blk's attached device model of media change. | |
39829a01 KW |
980 | * |
981 | * If @load is true, notify of media load. This action can fail, meaning that | |
982 | * the medium cannot be loaded. @errp is set then. | |
983 | * | |
984 | * If @load is false, notify of media eject. This can never fail. | |
985 | * | |
a7f53e26 MA |
986 | * Also send DEVICE_TRAY_MOVED events as appropriate. |
987 | */ | |
39829a01 | 988 | void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp) |
a7f53e26 MA |
989 | { |
990 | if (blk->dev_ops && blk->dev_ops->change_media_cb) { | |
f1f57066 | 991 | bool tray_was_open, tray_is_open; |
39829a01 | 992 | Error *local_err = NULL; |
a7f53e26 | 993 | |
f1f57066 | 994 | tray_was_open = blk_dev_is_tray_open(blk); |
39829a01 KW |
995 | blk->dev_ops->change_media_cb(blk->dev_opaque, load, &local_err); |
996 | if (local_err) { | |
997 | assert(load == true); | |
998 | error_propagate(errp, local_err); | |
999 | return; | |
1000 | } | |
f1f57066 HR |
1001 | tray_is_open = blk_dev_is_tray_open(blk); |
1002 | ||
1003 | if (tray_was_open != tray_is_open) { | |
2d76e724 | 1004 | char *id = blk_get_attached_dev_id(blk); |
3ab72385 | 1005 | qapi_event_send_device_tray_moved(blk_name(blk), id, tray_is_open); |
2d76e724 | 1006 | g_free(id); |
a7f53e26 MA |
1007 | } |
1008 | } | |
1009 | } | |
1010 | ||
5c8cab48 KW |
1011 | static void blk_root_change_media(BdrvChild *child, bool load) |
1012 | { | |
39829a01 | 1013 | blk_dev_change_media_cb(child->opaque, load, NULL); |
5c8cab48 KW |
1014 | } |
1015 | ||
a7f53e26 MA |
1016 | /* |
1017 | * Does @blk's attached device model have removable media? | |
1018 | * %true if no device model is attached. | |
1019 | */ | |
1020 | bool blk_dev_has_removable_media(BlockBackend *blk) | |
1021 | { | |
1022 | return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb); | |
1023 | } | |
1024 | ||
8f3a73bc HR |
1025 | /* |
1026 | * Does @blk's attached device model have a tray? | |
1027 | */ | |
1028 | bool blk_dev_has_tray(BlockBackend *blk) | |
1029 | { | |
1030 | return blk->dev_ops && blk->dev_ops->is_tray_open; | |
1031 | } | |
1032 | ||
a7f53e26 MA |
1033 | /* |
1034 | * Notify @blk's attached device model of a media eject request. | |
1035 | * If @force is true, the medium is about to be yanked out forcefully. | |
1036 | */ | |
1037 | void blk_dev_eject_request(BlockBackend *blk, bool force) | |
4be74634 | 1038 | { |
a7f53e26 MA |
1039 | if (blk->dev_ops && blk->dev_ops->eject_request_cb) { |
1040 | blk->dev_ops->eject_request_cb(blk->dev_opaque, force); | |
1041 | } | |
4be74634 MA |
1042 | } |
1043 | ||
a7f53e26 MA |
1044 | /* |
1045 | * Does @blk's attached device model have a tray, and is it open? | |
1046 | */ | |
1047 | bool blk_dev_is_tray_open(BlockBackend *blk) | |
4be74634 | 1048 | { |
8f3a73bc | 1049 | if (blk_dev_has_tray(blk)) { |
a7f53e26 MA |
1050 | return blk->dev_ops->is_tray_open(blk->dev_opaque); |
1051 | } | |
1052 | return false; | |
1053 | } | |
1054 | ||
1055 | /* | |
1056 | * Does @blk's attached device model have the medium locked? | |
1057 | * %false if the device model has no such lock. | |
1058 | */ | |
1059 | bool blk_dev_is_medium_locked(BlockBackend *blk) | |
1060 | { | |
1061 | if (blk->dev_ops && blk->dev_ops->is_medium_locked) { | |
1062 | return blk->dev_ops->is_medium_locked(blk->dev_opaque); | |
1063 | } | |
1064 | return false; | |
1065 | } | |
1066 | ||
1067 | /* | |
1068 | * Notify @blk's attached device model of a backend size change. | |
1069 | */ | |
5c8cab48 | 1070 | static void blk_root_resize(BdrvChild *child) |
a7f53e26 | 1071 | { |
5c8cab48 KW |
1072 | BlockBackend *blk = child->opaque; |
1073 | ||
a7f53e26 MA |
1074 | if (blk->dev_ops && blk->dev_ops->resize_cb) { |
1075 | blk->dev_ops->resize_cb(blk->dev_opaque); | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | void blk_iostatus_enable(BlockBackend *blk) | |
1080 | { | |
373340b2 HR |
1081 | blk->iostatus_enabled = true; |
1082 | blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK; | |
1083 | } | |
1084 | ||
1085 | /* The I/O status is only enabled if the drive explicitly | |
1086 | * enables it _and_ the VM is configured to stop on errors */ | |
1087 | bool blk_iostatus_is_enabled(const BlockBackend *blk) | |
1088 | { | |
1089 | return (blk->iostatus_enabled && | |
1090 | (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || | |
1091 | blk->on_write_error == BLOCKDEV_ON_ERROR_STOP || | |
1092 | blk->on_read_error == BLOCKDEV_ON_ERROR_STOP)); | |
1093 | } | |
1094 | ||
1095 | BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk) | |
1096 | { | |
1097 | return blk->iostatus; | |
1098 | } | |
1099 | ||
1100 | void blk_iostatus_disable(BlockBackend *blk) | |
1101 | { | |
1102 | blk->iostatus_enabled = false; | |
1103 | } | |
1104 | ||
1105 | void blk_iostatus_reset(BlockBackend *blk) | |
1106 | { | |
1107 | if (blk_iostatus_is_enabled(blk)) { | |
1108 | blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK; | |
373340b2 HR |
1109 | } |
1110 | } | |
1111 | ||
1112 | void blk_iostatus_set_err(BlockBackend *blk, int error) | |
1113 | { | |
1114 | assert(blk_iostatus_is_enabled(blk)); | |
1115 | if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { | |
1116 | blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : | |
1117 | BLOCK_DEVICE_IO_STATUS_FAILED; | |
1118 | } | |
4be74634 MA |
1119 | } |
1120 | ||
c10c9d96 KW |
1121 | void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow) |
1122 | { | |
1123 | blk->allow_write_beyond_eof = allow; | |
1124 | } | |
1125 | ||
980b0f94 KW |
1126 | void blk_set_allow_aio_context_change(BlockBackend *blk, bool allow) |
1127 | { | |
1128 | blk->allow_aio_context_change = allow; | |
1129 | } | |
1130 | ||
cf312932 KW |
1131 | void blk_set_disable_request_queuing(BlockBackend *blk, bool disable) |
1132 | { | |
1133 | blk->disable_request_queuing = disable; | |
1134 | } | |
1135 | ||
e7f7d676 HR |
1136 | static int blk_check_byte_request(BlockBackend *blk, int64_t offset, |
1137 | size_t size) | |
1138 | { | |
1139 | int64_t len; | |
1140 | ||
1141 | if (size > INT_MAX) { | |
1142 | return -EIO; | |
1143 | } | |
1144 | ||
c09ba36c | 1145 | if (!blk_is_available(blk)) { |
e7f7d676 HR |
1146 | return -ENOMEDIUM; |
1147 | } | |
1148 | ||
e7f7d676 HR |
1149 | if (offset < 0) { |
1150 | return -EIO; | |
1151 | } | |
1152 | ||
c10c9d96 KW |
1153 | if (!blk->allow_write_beyond_eof) { |
1154 | len = blk_getlength(blk); | |
1155 | if (len < 0) { | |
1156 | return len; | |
1157 | } | |
1158 | ||
1159 | if (offset > len || len - offset < size) { | |
1160 | return -EIO; | |
1161 | } | |
e7f7d676 HR |
1162 | } |
1163 | ||
1164 | return 0; | |
1165 | } | |
1166 | ||
7f16476f | 1167 | /* To be called between exactly one pair of blk_inc/dec_in_flight() */ |
cf312932 KW |
1168 | static void coroutine_fn blk_wait_while_drained(BlockBackend *blk) |
1169 | { | |
7f16476f KW |
1170 | assert(blk->in_flight > 0); |
1171 | ||
cf312932 | 1172 | if (blk->quiesce_counter && !blk->disable_request_queuing) { |
7f16476f | 1173 | blk_dec_in_flight(blk); |
cf312932 | 1174 | qemu_co_queue_wait(&blk->queued_requests, NULL); |
7f16476f | 1175 | blk_inc_in_flight(blk); |
cf312932 KW |
1176 | } |
1177 | } | |
1178 | ||
fbb92b67 KW |
1179 | /* To be called between exactly one pair of blk_inc/dec_in_flight() */ |
1180 | static int coroutine_fn | |
1181 | blk_do_preadv(BlockBackend *blk, int64_t offset, unsigned int bytes, | |
1182 | QEMUIOVector *qiov, BdrvRequestFlags flags) | |
4be74634 | 1183 | { |
1e98fefd | 1184 | int ret; |
cf312932 | 1185 | BlockDriverState *bs; |
1e98fefd | 1186 | |
cf312932 | 1187 | blk_wait_while_drained(blk); |
1e98fefd | 1188 | |
cf312932 KW |
1189 | /* Call blk_bs() only after waiting, the graph may have changed */ |
1190 | bs = blk_bs(blk); | |
99723548 | 1191 | trace_blk_co_preadv(blk, bs, offset, bytes, flags); |
1e98fefd KW |
1192 | |
1193 | ret = blk_check_byte_request(blk, offset, bytes); | |
e7f7d676 HR |
1194 | if (ret < 0) { |
1195 | return ret; | |
1196 | } | |
1197 | ||
99723548 PB |
1198 | bdrv_inc_in_flight(bs); |
1199 | ||
441565b2 | 1200 | /* throttling disk I/O */ |
022cdc9f MP |
1201 | if (blk->public.throttle_group_member.throttle_state) { |
1202 | throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member, | |
1203 | bytes, false); | |
441565b2 KW |
1204 | } |
1205 | ||
99723548 PB |
1206 | ret = bdrv_co_preadv(blk->root, offset, bytes, qiov, flags); |
1207 | bdrv_dec_in_flight(bs); | |
1208 | return ret; | |
1bf1cbc9 KW |
1209 | } |
1210 | ||
fbb92b67 KW |
1211 | int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset, |
1212 | unsigned int bytes, QEMUIOVector *qiov, | |
1213 | BdrvRequestFlags flags) | |
1214 | { | |
1215 | int ret; | |
1216 | ||
1217 | blk_inc_in_flight(blk); | |
1218 | ret = blk_do_preadv(blk, offset, bytes, qiov, flags); | |
1219 | blk_dec_in_flight(blk); | |
1220 | ||
1221 | return ret; | |
1222 | } | |
1223 | ||
1224 | /* To be called between exactly one pair of blk_inc/dec_in_flight() */ | |
1225 | static int coroutine_fn | |
1226 | blk_do_pwritev_part(BlockBackend *blk, int64_t offset, unsigned int bytes, | |
1227 | QEMUIOVector *qiov, size_t qiov_offset, | |
1228 | BdrvRequestFlags flags) | |
a8823a3b | 1229 | { |
bfd18d1e | 1230 | int ret; |
cf312932 | 1231 | BlockDriverState *bs; |
bfd18d1e | 1232 | |
cf312932 | 1233 | blk_wait_while_drained(blk); |
bfd18d1e | 1234 | |
cf312932 KW |
1235 | /* Call blk_bs() only after waiting, the graph may have changed */ |
1236 | bs = blk_bs(blk); | |
99723548 | 1237 | trace_blk_co_pwritev(blk, bs, offset, bytes, flags); |
1e98fefd | 1238 | |
bfd18d1e | 1239 | ret = blk_check_byte_request(blk, offset, bytes); |
a8823a3b KW |
1240 | if (ret < 0) { |
1241 | return ret; | |
1242 | } | |
1243 | ||
99723548 | 1244 | bdrv_inc_in_flight(bs); |
441565b2 | 1245 | /* throttling disk I/O */ |
022cdc9f MP |
1246 | if (blk->public.throttle_group_member.throttle_state) { |
1247 | throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member, | |
1248 | bytes, true); | |
441565b2 KW |
1249 | } |
1250 | ||
bfd18d1e KW |
1251 | if (!blk->enable_write_cache) { |
1252 | flags |= BDRV_REQ_FUA; | |
1253 | } | |
1254 | ||
b3016864 VSO |
1255 | ret = bdrv_co_pwritev_part(blk->root, offset, bytes, qiov, qiov_offset, |
1256 | flags); | |
99723548 PB |
1257 | bdrv_dec_in_flight(bs); |
1258 | return ret; | |
a8823a3b KW |
1259 | } |
1260 | ||
fbb92b67 KW |
1261 | int coroutine_fn blk_co_pwritev_part(BlockBackend *blk, int64_t offset, |
1262 | unsigned int bytes, | |
1263 | QEMUIOVector *qiov, size_t qiov_offset, | |
1264 | BdrvRequestFlags flags) | |
1265 | { | |
1266 | int ret; | |
1267 | ||
1268 | blk_inc_in_flight(blk); | |
1269 | ret = blk_do_pwritev_part(blk, offset, bytes, qiov, qiov_offset, flags); | |
1270 | blk_dec_in_flight(blk); | |
1271 | ||
1272 | return ret; | |
1273 | } | |
1274 | ||
b3016864 VSO |
1275 | int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset, |
1276 | unsigned int bytes, QEMUIOVector *qiov, | |
1277 | BdrvRequestFlags flags) | |
1278 | { | |
1279 | return blk_co_pwritev_part(blk, offset, bytes, qiov, 0, flags); | |
1280 | } | |
1281 | ||
1bf1cbc9 KW |
1282 | typedef struct BlkRwCo { |
1283 | BlockBackend *blk; | |
1284 | int64_t offset; | |
c060332c | 1285 | void *iobuf; |
1bf1cbc9 KW |
1286 | int ret; |
1287 | BdrvRequestFlags flags; | |
1288 | } BlkRwCo; | |
1289 | ||
1290 | static void blk_read_entry(void *opaque) | |
1291 | { | |
1292 | BlkRwCo *rwco = opaque; | |
c060332c | 1293 | QEMUIOVector *qiov = rwco->iobuf; |
1bf1cbc9 | 1294 | |
fbb92b67 | 1295 | rwco->ret = blk_do_preadv(rwco->blk, rwco->offset, qiov->size, |
c060332c | 1296 | qiov, rwco->flags); |
4720cbee | 1297 | aio_wait_kick(); |
1bf1cbc9 KW |
1298 | } |
1299 | ||
a8823a3b KW |
1300 | static void blk_write_entry(void *opaque) |
1301 | { | |
1302 | BlkRwCo *rwco = opaque; | |
c060332c | 1303 | QEMUIOVector *qiov = rwco->iobuf; |
a8823a3b | 1304 | |
fbb92b67 KW |
1305 | rwco->ret = blk_do_pwritev_part(rwco->blk, rwco->offset, qiov->size, |
1306 | qiov, 0, rwco->flags); | |
4720cbee | 1307 | aio_wait_kick(); |
a8823a3b KW |
1308 | } |
1309 | ||
a55d3fba KW |
1310 | static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf, |
1311 | int64_t bytes, CoroutineEntry co_entry, | |
1312 | BdrvRequestFlags flags) | |
1bf1cbc9 | 1313 | { |
ae5a9592 VSO |
1314 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes); |
1315 | BlkRwCo rwco = { | |
1bf1cbc9 | 1316 | .blk = blk, |
a55d3fba | 1317 | .offset = offset, |
c060332c | 1318 | .iobuf = &qiov, |
fc1453cd | 1319 | .flags = flags, |
1bf1cbc9 KW |
1320 | .ret = NOT_DONE, |
1321 | }; | |
1322 | ||
fbb92b67 | 1323 | blk_inc_in_flight(blk); |
35f106e6 PB |
1324 | if (qemu_in_coroutine()) { |
1325 | /* Fast-path if already in coroutine context */ | |
1326 | co_entry(&rwco); | |
1327 | } else { | |
1328 | Coroutine *co = qemu_coroutine_create(co_entry, &rwco); | |
e92f0e19 | 1329 | bdrv_coroutine_enter(blk_bs(blk), co); |
35f106e6 PB |
1330 | BDRV_POLL_WHILE(blk_bs(blk), rwco.ret == NOT_DONE); |
1331 | } | |
fbb92b67 | 1332 | blk_dec_in_flight(blk); |
1bf1cbc9 KW |
1333 | |
1334 | return rwco.ret; | |
4be74634 MA |
1335 | } |
1336 | ||
d004bd52 | 1337 | int blk_pwrite_zeroes(BlockBackend *blk, int64_t offset, |
f5a5ca79 | 1338 | int bytes, BdrvRequestFlags flags) |
0df89e8e | 1339 | { |
f5a5ca79 | 1340 | return blk_prw(blk, offset, NULL, bytes, blk_write_entry, |
983a1600 | 1341 | flags | BDRV_REQ_ZERO_WRITE); |
0df89e8e KW |
1342 | } |
1343 | ||
720ff280 KW |
1344 | int blk_make_zero(BlockBackend *blk, BdrvRequestFlags flags) |
1345 | { | |
1346 | return bdrv_make_zero(blk->root, flags); | |
1347 | } | |
1348 | ||
c90e2a9c | 1349 | void blk_inc_in_flight(BlockBackend *blk) |
33f2a757 SH |
1350 | { |
1351 | atomic_inc(&blk->in_flight); | |
1352 | } | |
1353 | ||
c90e2a9c | 1354 | void blk_dec_in_flight(BlockBackend *blk) |
33f2a757 SH |
1355 | { |
1356 | atomic_dec(&blk->in_flight); | |
cfe29d82 | 1357 | aio_wait_kick(); |
33f2a757 SH |
1358 | } |
1359 | ||
e7f7d676 HR |
1360 | static void error_callback_bh(void *opaque) |
1361 | { | |
1362 | struct BlockBackendAIOCB *acb = opaque; | |
99723548 | 1363 | |
33f2a757 | 1364 | blk_dec_in_flight(acb->blk); |
e7f7d676 HR |
1365 | acb->common.cb(acb->common.opaque, acb->ret); |
1366 | qemu_aio_unref(acb); | |
1367 | } | |
1368 | ||
ca78ecfa PL |
1369 | BlockAIOCB *blk_abort_aio_request(BlockBackend *blk, |
1370 | BlockCompletionFunc *cb, | |
1371 | void *opaque, int ret) | |
e7f7d676 HR |
1372 | { |
1373 | struct BlockBackendAIOCB *acb; | |
e7f7d676 | 1374 | |
33f2a757 | 1375 | blk_inc_in_flight(blk); |
e7f7d676 | 1376 | acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque); |
4981bdec | 1377 | acb->blk = blk; |
e7f7d676 HR |
1378 | acb->ret = ret; |
1379 | ||
e4ec5ad4 PD |
1380 | replay_bh_schedule_oneshot_event(blk_get_aio_context(blk), |
1381 | error_callback_bh, acb); | |
e7f7d676 HR |
1382 | return &acb->common; |
1383 | } | |
1384 | ||
57d6a428 KW |
1385 | typedef struct BlkAioEmAIOCB { |
1386 | BlockAIOCB common; | |
1387 | BlkRwCo rwco; | |
7fa84cd8 | 1388 | int bytes; |
57d6a428 | 1389 | bool has_returned; |
57d6a428 KW |
1390 | } BlkAioEmAIOCB; |
1391 | ||
1392 | static const AIOCBInfo blk_aio_em_aiocb_info = { | |
1393 | .aiocb_size = sizeof(BlkAioEmAIOCB), | |
1394 | }; | |
1395 | ||
1396 | static void blk_aio_complete(BlkAioEmAIOCB *acb) | |
1397 | { | |
57d6a428 KW |
1398 | if (acb->has_returned) { |
1399 | acb->common.cb(acb->common.opaque, acb->rwco.ret); | |
46aaf2a5 | 1400 | blk_dec_in_flight(acb->rwco.blk); |
57d6a428 KW |
1401 | qemu_aio_unref(acb); |
1402 | } | |
1403 | } | |
1404 | ||
1405 | static void blk_aio_complete_bh(void *opaque) | |
1406 | { | |
fffb6e12 | 1407 | BlkAioEmAIOCB *acb = opaque; |
fffb6e12 PB |
1408 | assert(acb->has_returned); |
1409 | blk_aio_complete(acb); | |
57d6a428 KW |
1410 | } |
1411 | ||
7fa84cd8 | 1412 | static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes, |
c060332c | 1413 | void *iobuf, CoroutineEntry co_entry, |
57d6a428 KW |
1414 | BdrvRequestFlags flags, |
1415 | BlockCompletionFunc *cb, void *opaque) | |
1416 | { | |
1417 | BlkAioEmAIOCB *acb; | |
1418 | Coroutine *co; | |
1419 | ||
33f2a757 | 1420 | blk_inc_in_flight(blk); |
57d6a428 KW |
1421 | acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque); |
1422 | acb->rwco = (BlkRwCo) { | |
1423 | .blk = blk, | |
1424 | .offset = offset, | |
c060332c | 1425 | .iobuf = iobuf, |
57d6a428 KW |
1426 | .flags = flags, |
1427 | .ret = NOT_DONE, | |
1428 | }; | |
7fa84cd8 | 1429 | acb->bytes = bytes; |
57d6a428 KW |
1430 | acb->has_returned = false; |
1431 | ||
0b8b8753 | 1432 | co = qemu_coroutine_create(co_entry, acb); |
e92f0e19 | 1433 | bdrv_coroutine_enter(blk_bs(blk), co); |
57d6a428 KW |
1434 | |
1435 | acb->has_returned = true; | |
1436 | if (acb->rwco.ret != NOT_DONE) { | |
e4ec5ad4 PD |
1437 | replay_bh_schedule_oneshot_event(blk_get_aio_context(blk), |
1438 | blk_aio_complete_bh, acb); | |
57d6a428 KW |
1439 | } |
1440 | ||
1441 | return &acb->common; | |
1442 | } | |
1443 | ||
1444 | static void blk_aio_read_entry(void *opaque) | |
1445 | { | |
1446 | BlkAioEmAIOCB *acb = opaque; | |
1447 | BlkRwCo *rwco = &acb->rwco; | |
c060332c | 1448 | QEMUIOVector *qiov = rwco->iobuf; |
57d6a428 | 1449 | |
c060332c | 1450 | assert(qiov->size == acb->bytes); |
fbb92b67 | 1451 | rwco->ret = blk_do_preadv(rwco->blk, rwco->offset, acb->bytes, |
c060332c | 1452 | qiov, rwco->flags); |
57d6a428 KW |
1453 | blk_aio_complete(acb); |
1454 | } | |
1455 | ||
1456 | static void blk_aio_write_entry(void *opaque) | |
1457 | { | |
1458 | BlkAioEmAIOCB *acb = opaque; | |
1459 | BlkRwCo *rwco = &acb->rwco; | |
c060332c | 1460 | QEMUIOVector *qiov = rwco->iobuf; |
57d6a428 | 1461 | |
c060332c | 1462 | assert(!qiov || qiov->size == acb->bytes); |
fbb92b67 KW |
1463 | rwco->ret = blk_do_pwritev_part(rwco->blk, rwco->offset, acb->bytes, |
1464 | qiov, 0, rwco->flags); | |
57d6a428 KW |
1465 | blk_aio_complete(acb); |
1466 | } | |
1467 | ||
d004bd52 EB |
1468 | BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset, |
1469 | int count, BdrvRequestFlags flags, | |
1470 | BlockCompletionFunc *cb, void *opaque) | |
4be74634 | 1471 | { |
983a1600 EB |
1472 | return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry, |
1473 | flags | BDRV_REQ_ZERO_WRITE, cb, opaque); | |
4be74634 MA |
1474 | } |
1475 | ||
1476 | int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count) | |
1477 | { | |
a55d3fba | 1478 | int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0); |
e7f7d676 HR |
1479 | if (ret < 0) { |
1480 | return ret; | |
1481 | } | |
a55d3fba | 1482 | return count; |
4be74634 MA |
1483 | } |
1484 | ||
8341f00d EB |
1485 | int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count, |
1486 | BdrvRequestFlags flags) | |
4be74634 | 1487 | { |
8341f00d EB |
1488 | int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry, |
1489 | flags); | |
e7f7d676 HR |
1490 | if (ret < 0) { |
1491 | return ret; | |
1492 | } | |
a55d3fba | 1493 | return count; |
4be74634 MA |
1494 | } |
1495 | ||
1496 | int64_t blk_getlength(BlockBackend *blk) | |
1497 | { | |
c09ba36c HR |
1498 | if (!blk_is_available(blk)) { |
1499 | return -ENOMEDIUM; | |
1500 | } | |
1501 | ||
f21d96d0 | 1502 | return bdrv_getlength(blk_bs(blk)); |
4be74634 MA |
1503 | } |
1504 | ||
1505 | void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr) | |
1506 | { | |
f21d96d0 | 1507 | if (!blk_bs(blk)) { |
a46fc9c9 HR |
1508 | *nb_sectors_ptr = 0; |
1509 | } else { | |
f21d96d0 | 1510 | bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr); |
a46fc9c9 | 1511 | } |
4be74634 MA |
1512 | } |
1513 | ||
1ef01253 HR |
1514 | int64_t blk_nb_sectors(BlockBackend *blk) |
1515 | { | |
c09ba36c HR |
1516 | if (!blk_is_available(blk)) { |
1517 | return -ENOMEDIUM; | |
1518 | } | |
1519 | ||
f21d96d0 | 1520 | return bdrv_nb_sectors(blk_bs(blk)); |
1ef01253 HR |
1521 | } |
1522 | ||
60cb2fa7 EB |
1523 | BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset, |
1524 | QEMUIOVector *qiov, BdrvRequestFlags flags, | |
1525 | BlockCompletionFunc *cb, void *opaque) | |
1526 | { | |
1527 | return blk_aio_prwv(blk, offset, qiov->size, qiov, | |
1528 | blk_aio_read_entry, flags, cb, opaque); | |
1529 | } | |
1530 | ||
60cb2fa7 EB |
1531 | BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset, |
1532 | QEMUIOVector *qiov, BdrvRequestFlags flags, | |
1533 | BlockCompletionFunc *cb, void *opaque) | |
1534 | { | |
1535 | return blk_aio_prwv(blk, offset, qiov->size, qiov, | |
1536 | blk_aio_write_entry, flags, cb, opaque); | |
1537 | } | |
1538 | ||
4be74634 MA |
1539 | void blk_aio_cancel(BlockAIOCB *acb) |
1540 | { | |
1541 | bdrv_aio_cancel(acb); | |
1542 | } | |
1543 | ||
1544 | void blk_aio_cancel_async(BlockAIOCB *acb) | |
1545 | { | |
1546 | bdrv_aio_cancel_async(acb); | |
1547 | } | |
1548 | ||
fbb92b67 KW |
1549 | /* To be called between exactly one pair of blk_inc/dec_in_flight() */ |
1550 | static int coroutine_fn | |
1551 | blk_do_ioctl(BlockBackend *blk, unsigned long int req, void *buf) | |
4be74634 | 1552 | { |
cf312932 KW |
1553 | blk_wait_while_drained(blk); |
1554 | ||
c09ba36c HR |
1555 | if (!blk_is_available(blk)) { |
1556 | return -ENOMEDIUM; | |
1557 | } | |
1558 | ||
48af776a KW |
1559 | return bdrv_co_ioctl(blk_bs(blk), req, buf); |
1560 | } | |
1561 | ||
1562 | static void blk_ioctl_entry(void *opaque) | |
1563 | { | |
1564 | BlkRwCo *rwco = opaque; | |
c060332c DS |
1565 | QEMUIOVector *qiov = rwco->iobuf; |
1566 | ||
fbb92b67 | 1567 | rwco->ret = blk_do_ioctl(rwco->blk, rwco->offset, qiov->iov[0].iov_base); |
4720cbee | 1568 | aio_wait_kick(); |
48af776a KW |
1569 | } |
1570 | ||
1571 | int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf) | |
1572 | { | |
1573 | return blk_prw(blk, req, buf, 0, blk_ioctl_entry, 0); | |
1574 | } | |
1575 | ||
1576 | static void blk_aio_ioctl_entry(void *opaque) | |
1577 | { | |
1578 | BlkAioEmAIOCB *acb = opaque; | |
1579 | BlkRwCo *rwco = &acb->rwco; | |
1580 | ||
fbb92b67 | 1581 | rwco->ret = blk_do_ioctl(rwco->blk, rwco->offset, rwco->iobuf); |
c060332c | 1582 | |
48af776a | 1583 | blk_aio_complete(acb); |
4be74634 MA |
1584 | } |
1585 | ||
1586 | BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf, | |
1587 | BlockCompletionFunc *cb, void *opaque) | |
1588 | { | |
c060332c | 1589 | return blk_aio_prwv(blk, req, 0, buf, blk_aio_ioctl_entry, 0, cb, opaque); |
4be74634 MA |
1590 | } |
1591 | ||
fbb92b67 KW |
1592 | /* To be called between exactly one pair of blk_inc/dec_in_flight() */ |
1593 | static int coroutine_fn | |
1594 | blk_do_pdiscard(BlockBackend *blk, int64_t offset, int bytes) | |
2bb0dce7 | 1595 | { |
cf312932 KW |
1596 | int ret; |
1597 | ||
1598 | blk_wait_while_drained(blk); | |
1599 | ||
1600 | ret = blk_check_byte_request(blk, offset, bytes); | |
e7f7d676 HR |
1601 | if (ret < 0) { |
1602 | return ret; | |
1603 | } | |
1604 | ||
0b9fd3f4 | 1605 | return bdrv_co_pdiscard(blk->root, offset, bytes); |
2bb0dce7 HR |
1606 | } |
1607 | ||
564806c5 KW |
1608 | static void blk_aio_pdiscard_entry(void *opaque) |
1609 | { | |
1610 | BlkAioEmAIOCB *acb = opaque; | |
1611 | BlkRwCo *rwco = &acb->rwco; | |
1612 | ||
fbb92b67 | 1613 | rwco->ret = blk_do_pdiscard(rwco->blk, rwco->offset, acb->bytes); |
564806c5 KW |
1614 | blk_aio_complete(acb); |
1615 | } | |
1616 | ||
1617 | BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk, | |
1618 | int64_t offset, int bytes, | |
1619 | BlockCompletionFunc *cb, void *opaque) | |
1620 | { | |
1621 | return blk_aio_prwv(blk, offset, bytes, NULL, blk_aio_pdiscard_entry, 0, | |
1622 | cb, opaque); | |
1623 | } | |
1624 | ||
fbb92b67 KW |
1625 | int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset, int bytes) |
1626 | { | |
1627 | int ret; | |
1628 | ||
1629 | blk_inc_in_flight(blk); | |
1630 | ret = blk_do_pdiscard(blk, offset, bytes); | |
1631 | blk_dec_in_flight(blk); | |
1632 | ||
1633 | return ret; | |
1634 | } | |
1635 | ||
564806c5 KW |
1636 | static void blk_pdiscard_entry(void *opaque) |
1637 | { | |
1638 | BlkRwCo *rwco = opaque; | |
1639 | QEMUIOVector *qiov = rwco->iobuf; | |
1640 | ||
fbb92b67 | 1641 | rwco->ret = blk_do_pdiscard(rwco->blk, rwco->offset, qiov->size); |
564806c5 KW |
1642 | aio_wait_kick(); |
1643 | } | |
1644 | ||
1645 | int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes) | |
1646 | { | |
1647 | return blk_prw(blk, offset, NULL, bytes, blk_pdiscard_entry, 0); | |
1648 | } | |
1649 | ||
fbb92b67 KW |
1650 | /* To be called between exactly one pair of blk_inc/dec_in_flight() */ |
1651 | static int coroutine_fn blk_do_flush(BlockBackend *blk) | |
2bb0dce7 | 1652 | { |
cf312932 KW |
1653 | blk_wait_while_drained(blk); |
1654 | ||
c09ba36c HR |
1655 | if (!blk_is_available(blk)) { |
1656 | return -ENOMEDIUM; | |
1657 | } | |
1658 | ||
f21d96d0 | 1659 | return bdrv_co_flush(blk_bs(blk)); |
2bb0dce7 HR |
1660 | } |
1661 | ||
564806c5 KW |
1662 | static void blk_aio_flush_entry(void *opaque) |
1663 | { | |
1664 | BlkAioEmAIOCB *acb = opaque; | |
1665 | BlkRwCo *rwco = &acb->rwco; | |
1666 | ||
fbb92b67 | 1667 | rwco->ret = blk_do_flush(rwco->blk); |
564806c5 KW |
1668 | blk_aio_complete(acb); |
1669 | } | |
1670 | ||
1671 | BlockAIOCB *blk_aio_flush(BlockBackend *blk, | |
1672 | BlockCompletionFunc *cb, void *opaque) | |
1673 | { | |
1674 | return blk_aio_prwv(blk, 0, 0, NULL, blk_aio_flush_entry, 0, cb, opaque); | |
1675 | } | |
1676 | ||
fbb92b67 KW |
1677 | int coroutine_fn blk_co_flush(BlockBackend *blk) |
1678 | { | |
1679 | int ret; | |
1680 | ||
1681 | blk_inc_in_flight(blk); | |
1682 | ret = blk_do_flush(blk); | |
1683 | blk_dec_in_flight(blk); | |
1684 | ||
1685 | return ret; | |
1686 | } | |
1687 | ||
be07a889 | 1688 | static void blk_flush_entry(void *opaque) |
4be74634 | 1689 | { |
be07a889 | 1690 | BlkRwCo *rwco = opaque; |
fbb92b67 | 1691 | rwco->ret = blk_do_flush(rwco->blk); |
4720cbee | 1692 | aio_wait_kick(); |
be07a889 | 1693 | } |
c09ba36c | 1694 | |
be07a889 KW |
1695 | int blk_flush(BlockBackend *blk) |
1696 | { | |
1697 | return blk_prw(blk, 0, NULL, 0, blk_flush_entry, 0); | |
4be74634 MA |
1698 | } |
1699 | ||
97b0385a AY |
1700 | void blk_drain(BlockBackend *blk) |
1701 | { | |
33f2a757 SH |
1702 | BlockDriverState *bs = blk_bs(blk); |
1703 | ||
1704 | if (bs) { | |
1705 | bdrv_drained_begin(bs); | |
1706 | } | |
1707 | ||
1708 | /* We may have -ENOMEDIUM completions in flight */ | |
cfe29d82 KW |
1709 | AIO_WAIT_WHILE(blk_get_aio_context(blk), |
1710 | atomic_mb_read(&blk->in_flight) > 0); | |
33f2a757 SH |
1711 | |
1712 | if (bs) { | |
1713 | bdrv_drained_end(bs); | |
a46fc9c9 | 1714 | } |
97b0385a AY |
1715 | } |
1716 | ||
4be74634 MA |
1717 | void blk_drain_all(void) |
1718 | { | |
33f2a757 SH |
1719 | BlockBackend *blk = NULL; |
1720 | ||
1721 | bdrv_drain_all_begin(); | |
1722 | ||
1723 | while ((blk = blk_all_next(blk)) != NULL) { | |
1724 | AioContext *ctx = blk_get_aio_context(blk); | |
1725 | ||
1726 | aio_context_acquire(ctx); | |
1727 | ||
1728 | /* We may have -ENOMEDIUM completions in flight */ | |
cfe29d82 | 1729 | AIO_WAIT_WHILE(ctx, atomic_mb_read(&blk->in_flight) > 0); |
33f2a757 SH |
1730 | |
1731 | aio_context_release(ctx); | |
1732 | } | |
1733 | ||
1734 | bdrv_drain_all_end(); | |
4be74634 MA |
1735 | } |
1736 | ||
373340b2 HR |
1737 | void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error, |
1738 | BlockdevOnError on_write_error) | |
1739 | { | |
1740 | blk->on_read_error = on_read_error; | |
1741 | blk->on_write_error = on_write_error; | |
1742 | } | |
1743 | ||
4be74634 MA |
1744 | BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read) |
1745 | { | |
373340b2 | 1746 | return is_read ? blk->on_read_error : blk->on_write_error; |
4be74634 MA |
1747 | } |
1748 | ||
1749 | BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read, | |
1750 | int error) | |
1751 | { | |
373340b2 HR |
1752 | BlockdevOnError on_err = blk_get_on_error(blk, is_read); |
1753 | ||
1754 | switch (on_err) { | |
1755 | case BLOCKDEV_ON_ERROR_ENOSPC: | |
1756 | return (error == ENOSPC) ? | |
1757 | BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT; | |
1758 | case BLOCKDEV_ON_ERROR_STOP: | |
1759 | return BLOCK_ERROR_ACTION_STOP; | |
1760 | case BLOCKDEV_ON_ERROR_REPORT: | |
1761 | return BLOCK_ERROR_ACTION_REPORT; | |
1762 | case BLOCKDEV_ON_ERROR_IGNORE: | |
1763 | return BLOCK_ERROR_ACTION_IGNORE; | |
8c398252 | 1764 | case BLOCKDEV_ON_ERROR_AUTO: |
373340b2 HR |
1765 | default: |
1766 | abort(); | |
1767 | } | |
4be74634 MA |
1768 | } |
1769 | ||
373340b2 HR |
1770 | static void send_qmp_error_event(BlockBackend *blk, |
1771 | BlockErrorAction action, | |
1772 | bool is_read, int error) | |
1773 | { | |
1774 | IoOperationType optype; | |
bfe1a14c | 1775 | BlockDriverState *bs = blk_bs(blk); |
373340b2 HR |
1776 | |
1777 | optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE; | |
bfe1a14c KW |
1778 | qapi_event_send_block_io_error(blk_name(blk), !!bs, |
1779 | bs ? bdrv_get_node_name(bs) : NULL, optype, | |
2bf7e10f | 1780 | action, blk_iostatus_is_enabled(blk), |
3ab72385 | 1781 | error == ENOSPC, strerror(error)); |
373340b2 HR |
1782 | } |
1783 | ||
1784 | /* This is done by device models because, while the block layer knows | |
1785 | * about the error, it does not know whether an operation comes from | |
1786 | * the device or the block layer (from a job, for example). | |
1787 | */ | |
4be74634 MA |
1788 | void blk_error_action(BlockBackend *blk, BlockErrorAction action, |
1789 | bool is_read, int error) | |
1790 | { | |
373340b2 HR |
1791 | assert(error >= 0); |
1792 | ||
1793 | if (action == BLOCK_ERROR_ACTION_STOP) { | |
1794 | /* First set the iostatus, so that "info block" returns an iostatus | |
1795 | * that matches the events raised so far (an additional error iostatus | |
1796 | * is fine, but not a lost one). | |
1797 | */ | |
1798 | blk_iostatus_set_err(blk, error); | |
1799 | ||
1800 | /* Then raise the request to stop the VM and the event. | |
1801 | * qemu_system_vmstop_request_prepare has two effects. First, | |
1802 | * it ensures that the STOP event always comes after the | |
1803 | * BLOCK_IO_ERROR event. Second, it ensures that even if management | |
1804 | * can observe the STOP event and do a "cont" before the STOP | |
1805 | * event is issued, the VM will not stop. In this case, vm_start() | |
1806 | * also ensures that the STOP/RESUME pair of events is emitted. | |
1807 | */ | |
1808 | qemu_system_vmstop_request_prepare(); | |
1809 | send_qmp_error_event(blk, action, is_read, error); | |
1810 | qemu_system_vmstop_request(RUN_STATE_IO_ERROR); | |
1811 | } else { | |
1812 | send_qmp_error_event(blk, action, is_read, error); | |
1813 | } | |
4be74634 MA |
1814 | } |
1815 | ||
96710565 | 1816 | bool blk_is_read_only(BlockBackend *blk) |
4be74634 | 1817 | { |
f21d96d0 KW |
1818 | BlockDriverState *bs = blk_bs(blk); |
1819 | ||
1820 | if (bs) { | |
1821 | return bdrv_is_read_only(bs); | |
061959e8 HR |
1822 | } else { |
1823 | return blk->root_state.read_only; | |
1824 | } | |
4be74634 MA |
1825 | } |
1826 | ||
96710565 | 1827 | bool blk_is_sg(BlockBackend *blk) |
4be74634 | 1828 | { |
f21d96d0 KW |
1829 | BlockDriverState *bs = blk_bs(blk); |
1830 | ||
1831 | if (!bs) { | |
96710565 | 1832 | return false; |
a46fc9c9 HR |
1833 | } |
1834 | ||
f21d96d0 | 1835 | return bdrv_is_sg(bs); |
4be74634 MA |
1836 | } |
1837 | ||
96710565 | 1838 | bool blk_enable_write_cache(BlockBackend *blk) |
4be74634 | 1839 | { |
bfd18d1e | 1840 | return blk->enable_write_cache; |
4be74634 MA |
1841 | } |
1842 | ||
1843 | void blk_set_enable_write_cache(BlockBackend *blk, bool wce) | |
1844 | { | |
bfd18d1e | 1845 | blk->enable_write_cache = wce; |
4be74634 MA |
1846 | } |
1847 | ||
2bb0dce7 HR |
1848 | void blk_invalidate_cache(BlockBackend *blk, Error **errp) |
1849 | { | |
f21d96d0 KW |
1850 | BlockDriverState *bs = blk_bs(blk); |
1851 | ||
1852 | if (!bs) { | |
c09ba36c HR |
1853 | error_setg(errp, "Device '%s' has no medium", blk->name); |
1854 | return; | |
1855 | } | |
1856 | ||
f21d96d0 | 1857 | bdrv_invalidate_cache(bs, errp); |
2bb0dce7 HR |
1858 | } |
1859 | ||
e031f750 | 1860 | bool blk_is_inserted(BlockBackend *blk) |
4be74634 | 1861 | { |
f21d96d0 KW |
1862 | BlockDriverState *bs = blk_bs(blk); |
1863 | ||
1864 | return bs && bdrv_is_inserted(bs); | |
db0284f8 HR |
1865 | } |
1866 | ||
1867 | bool blk_is_available(BlockBackend *blk) | |
1868 | { | |
1869 | return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk); | |
4be74634 MA |
1870 | } |
1871 | ||
1872 | void blk_lock_medium(BlockBackend *blk, bool locked) | |
1873 | { | |
f21d96d0 KW |
1874 | BlockDriverState *bs = blk_bs(blk); |
1875 | ||
1876 | if (bs) { | |
1877 | bdrv_lock_medium(bs, locked); | |
a46fc9c9 | 1878 | } |
4be74634 MA |
1879 | } |
1880 | ||
1881 | void blk_eject(BlockBackend *blk, bool eject_flag) | |
1882 | { | |
f21d96d0 | 1883 | BlockDriverState *bs = blk_bs(blk); |
2d76e724 KW |
1884 | char *id; |
1885 | ||
f21d96d0 KW |
1886 | if (bs) { |
1887 | bdrv_eject(bs, eject_flag); | |
a46fc9c9 | 1888 | } |
c47ee043 JS |
1889 | |
1890 | /* Whether or not we ejected on the backend, | |
1891 | * the frontend experienced a tray event. */ | |
1892 | id = blk_get_attached_dev_id(blk); | |
1893 | qapi_event_send_device_tray_moved(blk_name(blk), id, | |
3ab72385 | 1894 | eject_flag); |
c47ee043 | 1895 | g_free(id); |
4be74634 MA |
1896 | } |
1897 | ||
1898 | int blk_get_flags(BlockBackend *blk) | |
1899 | { | |
f21d96d0 KW |
1900 | BlockDriverState *bs = blk_bs(blk); |
1901 | ||
1902 | if (bs) { | |
1903 | return bdrv_get_flags(bs); | |
061959e8 HR |
1904 | } else { |
1905 | return blk->root_state.open_flags; | |
1906 | } | |
4be74634 MA |
1907 | } |
1908 | ||
4841211e EB |
1909 | /* Returns the minimum request alignment, in bytes; guaranteed nonzero */ |
1910 | uint32_t blk_get_request_alignment(BlockBackend *blk) | |
1911 | { | |
1912 | BlockDriverState *bs = blk_bs(blk); | |
1913 | return bs ? bs->bl.request_alignment : BDRV_SECTOR_SIZE; | |
1914 | } | |
1915 | ||
5def6b80 EB |
1916 | /* Returns the maximum transfer length, in bytes; guaranteed nonzero */ |
1917 | uint32_t blk_get_max_transfer(BlockBackend *blk) | |
454057b7 | 1918 | { |
f21d96d0 | 1919 | BlockDriverState *bs = blk_bs(blk); |
5def6b80 | 1920 | uint32_t max = 0; |
f21d96d0 KW |
1921 | |
1922 | if (bs) { | |
5def6b80 | 1923 | max = bs->bl.max_transfer; |
a46fc9c9 | 1924 | } |
5def6b80 | 1925 | return MIN_NON_ZERO(max, INT_MAX); |
454057b7 PL |
1926 | } |
1927 | ||
648296e0 SH |
1928 | int blk_get_max_iov(BlockBackend *blk) |
1929 | { | |
f21d96d0 | 1930 | return blk->root->bs->bl.max_iov; |
648296e0 SH |
1931 | } |
1932 | ||
4be74634 MA |
1933 | void blk_set_guest_block_size(BlockBackend *blk, int align) |
1934 | { | |
68e9ec01 | 1935 | blk->guest_block_size = align; |
4be74634 MA |
1936 | } |
1937 | ||
f1c17521 PB |
1938 | void *blk_try_blockalign(BlockBackend *blk, size_t size) |
1939 | { | |
f21d96d0 | 1940 | return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size); |
f1c17521 PB |
1941 | } |
1942 | ||
4be74634 MA |
1943 | void *blk_blockalign(BlockBackend *blk, size_t size) |
1944 | { | |
f21d96d0 | 1945 | return qemu_blockalign(blk ? blk_bs(blk) : NULL, size); |
4be74634 MA |
1946 | } |
1947 | ||
1948 | bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp) | |
1949 | { | |
f21d96d0 KW |
1950 | BlockDriverState *bs = blk_bs(blk); |
1951 | ||
1952 | if (!bs) { | |
a46fc9c9 HR |
1953 | return false; |
1954 | } | |
1955 | ||
f21d96d0 | 1956 | return bdrv_op_is_blocked(bs, op, errp); |
4be74634 MA |
1957 | } |
1958 | ||
1959 | void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason) | |
1960 | { | |
f21d96d0 KW |
1961 | BlockDriverState *bs = blk_bs(blk); |
1962 | ||
1963 | if (bs) { | |
1964 | bdrv_op_unblock(bs, op, reason); | |
a46fc9c9 | 1965 | } |
4be74634 MA |
1966 | } |
1967 | ||
1968 | void blk_op_block_all(BlockBackend *blk, Error *reason) | |
1969 | { | |
f21d96d0 KW |
1970 | BlockDriverState *bs = blk_bs(blk); |
1971 | ||
1972 | if (bs) { | |
1973 | bdrv_op_block_all(bs, reason); | |
a46fc9c9 | 1974 | } |
4be74634 MA |
1975 | } |
1976 | ||
1977 | void blk_op_unblock_all(BlockBackend *blk, Error *reason) | |
1978 | { | |
f21d96d0 KW |
1979 | BlockDriverState *bs = blk_bs(blk); |
1980 | ||
1981 | if (bs) { | |
1982 | bdrv_op_unblock_all(bs, reason); | |
a46fc9c9 | 1983 | } |
4be74634 MA |
1984 | } |
1985 | ||
1986 | AioContext *blk_get_aio_context(BlockBackend *blk) | |
1987 | { | |
d861ab3a KW |
1988 | BlockDriverState *bs = blk_bs(blk); |
1989 | ||
d861ab3a | 1990 | if (bs) { |
132ada80 KW |
1991 | AioContext *ctx = bdrv_get_aio_context(blk_bs(blk)); |
1992 | assert(ctx == blk->ctx); | |
d861ab3a KW |
1993 | } |
1994 | ||
1995 | return blk->ctx; | |
4981bdec HR |
1996 | } |
1997 | ||
1998 | static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb) | |
1999 | { | |
2000 | BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb); | |
2001 | return blk_get_aio_context(blk_acb->blk); | |
4be74634 MA |
2002 | } |
2003 | ||
97896a48 KW |
2004 | static int blk_do_set_aio_context(BlockBackend *blk, AioContext *new_context, |
2005 | bool update_root_node, Error **errp) | |
4be74634 | 2006 | { |
f21d96d0 | 2007 | BlockDriverState *bs = blk_bs(blk); |
c61791fc | 2008 | ThrottleGroupMember *tgm = &blk->public.throttle_group_member; |
97896a48 | 2009 | int ret; |
f21d96d0 KW |
2010 | |
2011 | if (bs) { | |
97896a48 KW |
2012 | if (update_root_node) { |
2013 | ret = bdrv_child_try_set_aio_context(bs, new_context, blk->root, | |
2014 | errp); | |
2015 | if (ret < 0) { | |
2016 | return ret; | |
2017 | } | |
2018 | } | |
c61791fc | 2019 | if (tgm->throttle_state) { |
dc868fb0 | 2020 | bdrv_drained_begin(bs); |
c61791fc MP |
2021 | throttle_group_detach_aio_context(tgm); |
2022 | throttle_group_attach_aio_context(tgm, new_context); | |
dc868fb0 | 2023 | bdrv_drained_end(bs); |
7ca7f0f6 | 2024 | } |
a46fc9c9 | 2025 | } |
97896a48 | 2026 | |
d861ab3a | 2027 | blk->ctx = new_context; |
97896a48 | 2028 | return 0; |
4be74634 MA |
2029 | } |
2030 | ||
97896a48 KW |
2031 | int blk_set_aio_context(BlockBackend *blk, AioContext *new_context, |
2032 | Error **errp) | |
38475269 | 2033 | { |
97896a48 | 2034 | return blk_do_set_aio_context(blk, new_context, true, errp); |
38475269 KW |
2035 | } |
2036 | ||
2037 | static bool blk_root_can_set_aio_ctx(BdrvChild *child, AioContext *ctx, | |
2038 | GSList **ignore, Error **errp) | |
2039 | { | |
2040 | BlockBackend *blk = child->opaque; | |
2041 | ||
980b0f94 KW |
2042 | if (blk->allow_aio_context_change) { |
2043 | return true; | |
2044 | } | |
2045 | ||
38475269 KW |
2046 | /* Only manually created BlockBackends that are not attached to anything |
2047 | * can change their AioContext without updating their user. */ | |
2048 | if (!blk->name || blk->dev) { | |
2049 | /* TODO Add BB name/QOM path */ | |
2050 | error_setg(errp, "Cannot change iothread of active block backend"); | |
2051 | return false; | |
2052 | } | |
2053 | ||
2054 | return true; | |
2055 | } | |
2056 | ||
2057 | static void blk_root_set_aio_ctx(BdrvChild *child, AioContext *ctx, | |
2058 | GSList **ignore) | |
2059 | { | |
2060 | BlockBackend *blk = child->opaque; | |
97896a48 | 2061 | blk_do_set_aio_context(blk, ctx, false, &error_abort); |
38475269 KW |
2062 | } |
2063 | ||
2019ba0a HR |
2064 | void blk_add_aio_context_notifier(BlockBackend *blk, |
2065 | void (*attached_aio_context)(AioContext *new_context, void *opaque), | |
2066 | void (*detach_aio_context)(void *opaque), void *opaque) | |
2067 | { | |
d03654ea | 2068 | BlockBackendAioNotifier *notifier; |
f21d96d0 KW |
2069 | BlockDriverState *bs = blk_bs(blk); |
2070 | ||
d03654ea SH |
2071 | notifier = g_new(BlockBackendAioNotifier, 1); |
2072 | notifier->attached_aio_context = attached_aio_context; | |
2073 | notifier->detach_aio_context = detach_aio_context; | |
2074 | notifier->opaque = opaque; | |
2075 | QLIST_INSERT_HEAD(&blk->aio_notifiers, notifier, list); | |
2076 | ||
f21d96d0 KW |
2077 | if (bs) { |
2078 | bdrv_add_aio_context_notifier(bs, attached_aio_context, | |
a46fc9c9 HR |
2079 | detach_aio_context, opaque); |
2080 | } | |
2019ba0a HR |
2081 | } |
2082 | ||
2083 | void blk_remove_aio_context_notifier(BlockBackend *blk, | |
2084 | void (*attached_aio_context)(AioContext *, | |
2085 | void *), | |
2086 | void (*detach_aio_context)(void *), | |
2087 | void *opaque) | |
2088 | { | |
d03654ea | 2089 | BlockBackendAioNotifier *notifier; |
f21d96d0 KW |
2090 | BlockDriverState *bs = blk_bs(blk); |
2091 | ||
2092 | if (bs) { | |
2093 | bdrv_remove_aio_context_notifier(bs, attached_aio_context, | |
a46fc9c9 HR |
2094 | detach_aio_context, opaque); |
2095 | } | |
d03654ea SH |
2096 | |
2097 | QLIST_FOREACH(notifier, &blk->aio_notifiers, list) { | |
2098 | if (notifier->attached_aio_context == attached_aio_context && | |
2099 | notifier->detach_aio_context == detach_aio_context && | |
2100 | notifier->opaque == opaque) { | |
2101 | QLIST_REMOVE(notifier, list); | |
2102 | g_free(notifier); | |
2103 | return; | |
2104 | } | |
2105 | } | |
2106 | ||
2107 | abort(); | |
2019ba0a HR |
2108 | } |
2109 | ||
3301f6c6 HR |
2110 | void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify) |
2111 | { | |
2112 | notifier_list_add(&blk->remove_bs_notifiers, notify); | |
2113 | } | |
2114 | ||
2115 | void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify) | |
2116 | { | |
2117 | notifier_list_add(&blk->insert_bs_notifiers, notify); | |
2118 | } | |
2119 | ||
4be74634 MA |
2120 | void blk_io_plug(BlockBackend *blk) |
2121 | { | |
f21d96d0 KW |
2122 | BlockDriverState *bs = blk_bs(blk); |
2123 | ||
2124 | if (bs) { | |
2125 | bdrv_io_plug(bs); | |
a46fc9c9 | 2126 | } |
4be74634 MA |
2127 | } |
2128 | ||
2129 | void blk_io_unplug(BlockBackend *blk) | |
2130 | { | |
f21d96d0 KW |
2131 | BlockDriverState *bs = blk_bs(blk); |
2132 | ||
2133 | if (bs) { | |
2134 | bdrv_io_unplug(bs); | |
a46fc9c9 | 2135 | } |
4be74634 MA |
2136 | } |
2137 | ||
2138 | BlockAcctStats *blk_get_stats(BlockBackend *blk) | |
2139 | { | |
7f0e9da6 | 2140 | return &blk->stats; |
4be74634 MA |
2141 | } |
2142 | ||
2143 | void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk, | |
2144 | BlockCompletionFunc *cb, void *opaque) | |
2145 | { | |
2146 | return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque); | |
2147 | } | |
1ef01253 | 2148 | |
d004bd52 | 2149 | int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset, |
f5a5ca79 | 2150 | int bytes, BdrvRequestFlags flags) |
1ef01253 | 2151 | { |
f5a5ca79 | 2152 | return blk_co_pwritev(blk, offset, bytes, NULL, |
16aaf975 | 2153 | flags | BDRV_REQ_ZERO_WRITE); |
1ef01253 HR |
2154 | } |
2155 | ||
fe5c1355 PB |
2156 | int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf, |
2157 | int count) | |
1ef01253 | 2158 | { |
35fadca8 PB |
2159 | return blk_prw(blk, offset, (void *) buf, count, blk_write_entry, |
2160 | BDRV_REQ_WRITE_COMPRESSED); | |
1ef01253 HR |
2161 | } |
2162 | ||
c80d8b06 | 2163 | int blk_truncate(BlockBackend *blk, int64_t offset, bool exact, |
8c6242b6 | 2164 | PreallocMode prealloc, BdrvRequestFlags flags, Error **errp) |
1ef01253 | 2165 | { |
c09ba36c | 2166 | if (!blk_is_available(blk)) { |
ed3d2ec9 | 2167 | error_setg(errp, "No medium inserted"); |
c09ba36c HR |
2168 | return -ENOMEDIUM; |
2169 | } | |
2170 | ||
8c6242b6 | 2171 | return bdrv_truncate(blk->root, offset, exact, prealloc, flags, errp); |
1ef01253 HR |
2172 | } |
2173 | ||
1ef01253 HR |
2174 | int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, |
2175 | int64_t pos, int size) | |
2176 | { | |
bfd18d1e KW |
2177 | int ret; |
2178 | ||
c09ba36c HR |
2179 | if (!blk_is_available(blk)) { |
2180 | return -ENOMEDIUM; | |
2181 | } | |
2182 | ||
bfd18d1e KW |
2183 | ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size); |
2184 | if (ret < 0) { | |
2185 | return ret; | |
2186 | } | |
2187 | ||
2188 | if (ret == size && !blk->enable_write_cache) { | |
2189 | ret = bdrv_flush(blk_bs(blk)); | |
2190 | } | |
2191 | ||
2192 | return ret < 0 ? ret : size; | |
1ef01253 HR |
2193 | } |
2194 | ||
2195 | int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size) | |
2196 | { | |
c09ba36c HR |
2197 | if (!blk_is_available(blk)) { |
2198 | return -ENOMEDIUM; | |
2199 | } | |
2200 | ||
f21d96d0 | 2201 | return bdrv_load_vmstate(blk_bs(blk), buf, pos, size); |
1ef01253 | 2202 | } |
f0272c4d ET |
2203 | |
2204 | int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz) | |
2205 | { | |
c09ba36c HR |
2206 | if (!blk_is_available(blk)) { |
2207 | return -ENOMEDIUM; | |
2208 | } | |
2209 | ||
f21d96d0 | 2210 | return bdrv_probe_blocksizes(blk_bs(blk), bsz); |
f0272c4d ET |
2211 | } |
2212 | ||
2213 | int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo) | |
2214 | { | |
c09ba36c HR |
2215 | if (!blk_is_available(blk)) { |
2216 | return -ENOMEDIUM; | |
2217 | } | |
2218 | ||
f21d96d0 | 2219 | return bdrv_probe_geometry(blk_bs(blk), geo); |
f0272c4d | 2220 | } |
281d22d8 HR |
2221 | |
2222 | /* | |
2223 | * Updates the BlockBackendRootState object with data from the currently | |
2224 | * attached BlockDriverState. | |
2225 | */ | |
2226 | void blk_update_root_state(BlockBackend *blk) | |
2227 | { | |
f21d96d0 | 2228 | assert(blk->root); |
281d22d8 | 2229 | |
f21d96d0 KW |
2230 | blk->root_state.open_flags = blk->root->bs->open_flags; |
2231 | blk->root_state.read_only = blk->root->bs->read_only; | |
2232 | blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes; | |
281d22d8 HR |
2233 | } |
2234 | ||
38cb18f5 | 2235 | /* |
b85114f8 KW |
2236 | * Returns the detect-zeroes setting to be used for bdrv_open() of a |
2237 | * BlockDriverState which is supposed to inherit the root state. | |
38cb18f5 | 2238 | */ |
b85114f8 | 2239 | bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk) |
38cb18f5 | 2240 | { |
b85114f8 | 2241 | return blk->root_state.detect_zeroes; |
38cb18f5 HR |
2242 | } |
2243 | ||
2244 | /* | |
2245 | * Returns the flags to be used for bdrv_open() of a BlockDriverState which is | |
2246 | * supposed to inherit the root state. | |
2247 | */ | |
2248 | int blk_get_open_flags_from_root_state(BlockBackend *blk) | |
2249 | { | |
2250 | int bs_flags; | |
2251 | ||
2252 | bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR; | |
2253 | bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR; | |
2254 | ||
2255 | return bs_flags; | |
2256 | } | |
2257 | ||
281d22d8 HR |
2258 | BlockBackendRootState *blk_get_root_state(BlockBackend *blk) |
2259 | { | |
2260 | return &blk->root_state; | |
2261 | } | |
1393f212 HR |
2262 | |
2263 | int blk_commit_all(void) | |
2264 | { | |
fe1a9cbc HR |
2265 | BlockBackend *blk = NULL; |
2266 | ||
2267 | while ((blk = blk_all_next(blk)) != NULL) { | |
2268 | AioContext *aio_context = blk_get_aio_context(blk); | |
2269 | ||
2270 | aio_context_acquire(aio_context); | |
f21d96d0 KW |
2271 | if (blk_is_inserted(blk) && blk->root->bs->backing) { |
2272 | int ret = bdrv_commit(blk->root->bs); | |
fe1a9cbc HR |
2273 | if (ret < 0) { |
2274 | aio_context_release(aio_context); | |
2275 | return ret; | |
2276 | } | |
2277 | } | |
2278 | aio_context_release(aio_context); | |
2279 | } | |
2280 | return 0; | |
2281 | } | |
2282 | ||
97148076 KW |
2283 | |
2284 | /* throttling disk I/O limits */ | |
2285 | void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg) | |
2286 | { | |
022cdc9f | 2287 | throttle_group_config(&blk->public.throttle_group_member, cfg); |
97148076 KW |
2288 | } |
2289 | ||
2290 | void blk_io_limits_disable(BlockBackend *blk) | |
2291 | { | |
48bf7ea8 AG |
2292 | BlockDriverState *bs = blk_bs(blk); |
2293 | ThrottleGroupMember *tgm = &blk->public.throttle_group_member; | |
2294 | assert(tgm->throttle_state); | |
2295 | if (bs) { | |
2296 | bdrv_drained_begin(bs); | |
2297 | } | |
2298 | throttle_group_unregister_tgm(tgm); | |
2299 | if (bs) { | |
2300 | bdrv_drained_end(bs); | |
2301 | } | |
97148076 KW |
2302 | } |
2303 | ||
2304 | /* should be called before blk_set_io_limits if a limit is set */ | |
2305 | void blk_io_limits_enable(BlockBackend *blk, const char *group) | |
2306 | { | |
022cdc9f | 2307 | assert(!blk->public.throttle_group_member.throttle_state); |
c61791fc MP |
2308 | throttle_group_register_tgm(&blk->public.throttle_group_member, |
2309 | group, blk_get_aio_context(blk)); | |
97148076 KW |
2310 | } |
2311 | ||
2312 | void blk_io_limits_update_group(BlockBackend *blk, const char *group) | |
2313 | { | |
2314 | /* this BB is not part of any group */ | |
022cdc9f | 2315 | if (!blk->public.throttle_group_member.throttle_state) { |
97148076 KW |
2316 | return; |
2317 | } | |
2318 | ||
2319 | /* this BB is a part of the same group than the one we want */ | |
022cdc9f MP |
2320 | if (!g_strcmp0(throttle_group_get_name(&blk->public.throttle_group_member), |
2321 | group)) { | |
97148076 KW |
2322 | return; |
2323 | } | |
2324 | ||
2325 | /* need to change the group this bs belong to */ | |
2326 | blk_io_limits_disable(blk); | |
2327 | blk_io_limits_enable(blk, group); | |
2328 | } | |
c2066af0 KW |
2329 | |
2330 | static void blk_root_drained_begin(BdrvChild *child) | |
2331 | { | |
2332 | BlockBackend *blk = child->opaque; | |
2333 | ||
f4d9cc88 JS |
2334 | if (++blk->quiesce_counter == 1) { |
2335 | if (blk->dev_ops && blk->dev_ops->drained_begin) { | |
2336 | blk->dev_ops->drained_begin(blk->dev_opaque); | |
2337 | } | |
2338 | } | |
2339 | ||
36fe1331 KW |
2340 | /* Note that blk->root may not be accessible here yet if we are just |
2341 | * attaching to a BlockDriverState that is drained. Use child instead. */ | |
2342 | ||
022cdc9f MP |
2343 | if (atomic_fetch_inc(&blk->public.throttle_group_member.io_limits_disabled) == 0) { |
2344 | throttle_group_restart_tgm(&blk->public.throttle_group_member); | |
c2066af0 KW |
2345 | } |
2346 | } | |
2347 | ||
fe5258a5 KW |
2348 | static bool blk_root_drained_poll(BdrvChild *child) |
2349 | { | |
2350 | BlockBackend *blk = child->opaque; | |
2351 | assert(blk->quiesce_counter); | |
2352 | return !!blk->in_flight; | |
2353 | } | |
2354 | ||
e037c09c | 2355 | static void blk_root_drained_end(BdrvChild *child, int *drained_end_counter) |
c2066af0 KW |
2356 | { |
2357 | BlockBackend *blk = child->opaque; | |
f4d9cc88 | 2358 | assert(blk->quiesce_counter); |
c2066af0 | 2359 | |
022cdc9f MP |
2360 | assert(blk->public.throttle_group_member.io_limits_disabled); |
2361 | atomic_dec(&blk->public.throttle_group_member.io_limits_disabled); | |
f4d9cc88 JS |
2362 | |
2363 | if (--blk->quiesce_counter == 0) { | |
2364 | if (blk->dev_ops && blk->dev_ops->drained_end) { | |
2365 | blk->dev_ops->drained_end(blk->dev_opaque); | |
2366 | } | |
cf312932 KW |
2367 | while (qemu_co_enter_next(&blk->queued_requests, NULL)) { |
2368 | /* Resume all queued requests */ | |
2369 | } | |
f4d9cc88 | 2370 | } |
c2066af0 | 2371 | } |
23d0ba93 FZ |
2372 | |
2373 | void blk_register_buf(BlockBackend *blk, void *host, size_t size) | |
2374 | { | |
2375 | bdrv_register_buf(blk_bs(blk), host, size); | |
2376 | } | |
2377 | ||
2378 | void blk_unregister_buf(BlockBackend *blk, void *host) | |
2379 | { | |
2380 | bdrv_unregister_buf(blk_bs(blk), host); | |
2381 | } | |
b5679fa4 FZ |
2382 | |
2383 | int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in, | |
2384 | BlockBackend *blk_out, int64_t off_out, | |
67b51fb9 VSO |
2385 | int bytes, BdrvRequestFlags read_flags, |
2386 | BdrvRequestFlags write_flags) | |
b5679fa4 FZ |
2387 | { |
2388 | int r; | |
2389 | r = blk_check_byte_request(blk_in, off_in, bytes); | |
2390 | if (r) { | |
2391 | return r; | |
2392 | } | |
2393 | r = blk_check_byte_request(blk_out, off_out, bytes); | |
2394 | if (r) { | |
2395 | return r; | |
2396 | } | |
2397 | return bdrv_co_copy_range(blk_in->root, off_in, | |
2398 | blk_out->root, off_out, | |
67b51fb9 | 2399 | bytes, read_flags, write_flags); |
b5679fa4 | 2400 | } |
5d3b4e99 VSO |
2401 | |
2402 | const BdrvChild *blk_root(BlockBackend *blk) | |
2403 | { | |
2404 | return blk->root; | |
2405 | } | |
2b7bbdbd HR |
2406 | |
2407 | int blk_make_empty(BlockBackend *blk, Error **errp) | |
2408 | { | |
2409 | if (!blk_is_available(blk)) { | |
2410 | error_setg(errp, "No medium inserted"); | |
2411 | return -ENOMEDIUM; | |
2412 | } | |
2413 | ||
2414 | return bdrv_make_empty(blk->root, errp); | |
2415 | } |