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