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