]>
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" |
18e46a03 | 18 | #include "sysemu/blockdev.h" |
373340b2 | 19 | #include "sysemu/sysemu.h" |
a7f53e26 | 20 | #include "qapi-event.h" |
f348b6d1 | 21 | #include "qemu/id.h" |
a7f53e26 MA |
22 | |
23 | /* Number of coroutines to reserve per attached device model */ | |
24 | #define COROUTINE_POOL_RESERVATION 64 | |
26f54e9a | 25 | |
1bf1cbc9 KW |
26 | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ |
27 | ||
4981bdec HR |
28 | static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb); |
29 | ||
26f54e9a MA |
30 | struct BlockBackend { |
31 | char *name; | |
32 | int refcnt; | |
f21d96d0 | 33 | BdrvChild *root; |
26f8b3a8 | 34 | DriveInfo *legacy_dinfo; /* null unless created by drive_new() */ |
2cf22d6a | 35 | QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */ |
9492b0b9 | 36 | QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */ |
f2cd875d | 37 | BlockBackendPublic public; |
a7f53e26 MA |
38 | |
39 | void *dev; /* attached device model, if any */ | |
40 | /* TODO change to DeviceState when all users are qdevified */ | |
41 | const BlockDevOps *dev_ops; | |
42 | void *dev_opaque; | |
68e9ec01 HR |
43 | |
44 | /* the block size for which the guest device expects atomicity */ | |
45 | int guest_block_size; | |
7f0e9da6 | 46 | |
281d22d8 HR |
47 | /* If the BDS tree is removed, some of its options are stored here (which |
48 | * can be used to restore those options in the new BDS on insert) */ | |
49 | BlockBackendRootState root_state; | |
50 | ||
bfd18d1e KW |
51 | bool enable_write_cache; |
52 | ||
7f0e9da6 HR |
53 | /* I/O stats (display with "info blockstats"). */ |
54 | BlockAcctStats stats; | |
373340b2 HR |
55 | |
56 | BlockdevOnError on_read_error, on_write_error; | |
57 | bool iostatus_enabled; | |
58 | BlockDeviceIoStatus iostatus; | |
3301f6c6 | 59 | |
c10c9d96 KW |
60 | bool allow_write_beyond_eof; |
61 | ||
3301f6c6 | 62 | NotifierList remove_bs_notifiers, insert_bs_notifiers; |
26f54e9a MA |
63 | }; |
64 | ||
e7f7d676 HR |
65 | typedef struct BlockBackendAIOCB { |
66 | BlockAIOCB common; | |
67 | QEMUBH *bh; | |
4981bdec | 68 | BlockBackend *blk; |
e7f7d676 HR |
69 | int ret; |
70 | } BlockBackendAIOCB; | |
71 | ||
72 | static const AIOCBInfo block_backend_aiocb_info = { | |
4981bdec | 73 | .get_aio_context = blk_aiocb_get_aio_context, |
e7f7d676 HR |
74 | .aiocb_size = sizeof(BlockBackendAIOCB), |
75 | }; | |
76 | ||
8fb3c76c MA |
77 | static void drive_info_del(DriveInfo *dinfo); |
78 | ||
2cf22d6a HR |
79 | /* All BlockBackends */ |
80 | static QTAILQ_HEAD(, BlockBackend) block_backends = | |
81 | QTAILQ_HEAD_INITIALIZER(block_backends); | |
82 | ||
9492b0b9 HR |
83 | /* All BlockBackends referenced by the monitor and which are iterated through by |
84 | * blk_next() */ | |
85 | static QTAILQ_HEAD(, BlockBackend) monitor_block_backends = | |
86 | QTAILQ_HEAD_INITIALIZER(monitor_block_backends); | |
26f54e9a | 87 | |
f21d96d0 KW |
88 | static void blk_root_inherit_options(int *child_flags, QDict *child_options, |
89 | int parent_flags, QDict *parent_options) | |
90 | { | |
91 | /* We're not supposed to call this function for root nodes */ | |
92 | abort(); | |
93 | } | |
94 | ||
95 | static const BdrvChildRole child_root = { | |
96 | .inherit_options = blk_root_inherit_options, | |
97 | }; | |
98 | ||
26f54e9a | 99 | /* |
efaa7c4e | 100 | * Create a new BlockBackend with a reference count of one. |
26f54e9a MA |
101 | * Store an error through @errp on failure, unless it's null. |
102 | * Return the new BlockBackend on success, null on failure. | |
103 | */ | |
efaa7c4e | 104 | BlockBackend *blk_new(Error **errp) |
26f54e9a MA |
105 | { |
106 | BlockBackend *blk; | |
107 | ||
26f54e9a | 108 | blk = g_new0(BlockBackend, 1); |
26f54e9a | 109 | blk->refcnt = 1; |
27ccdd52 KW |
110 | qemu_co_queue_init(&blk->public.throttled_reqs[0]); |
111 | qemu_co_queue_init(&blk->public.throttled_reqs[1]); | |
112 | ||
3301f6c6 HR |
113 | notifier_list_init(&blk->remove_bs_notifiers); |
114 | notifier_list_init(&blk->insert_bs_notifiers); | |
27ccdd52 | 115 | |
2cf22d6a | 116 | QTAILQ_INSERT_TAIL(&block_backends, blk, link); |
26f54e9a MA |
117 | return blk; |
118 | } | |
119 | ||
7e7d56d9 MA |
120 | /* |
121 | * Create a new BlockBackend with a new BlockDriverState attached. | |
7e7d56d9 MA |
122 | * Otherwise just like blk_new(), which see. |
123 | */ | |
efaa7c4e | 124 | BlockBackend *blk_new_with_bs(Error **errp) |
7e7d56d9 MA |
125 | { |
126 | BlockBackend *blk; | |
127 | BlockDriverState *bs; | |
128 | ||
efaa7c4e | 129 | blk = blk_new(errp); |
7e7d56d9 MA |
130 | if (!blk) { |
131 | return NULL; | |
132 | } | |
133 | ||
7f06d47e | 134 | bs = bdrv_new_root(); |
f21d96d0 | 135 | blk->root = bdrv_root_attach_child(bs, "root", &child_root); |
22aa8b24 | 136 | blk->root->opaque = blk; |
7e7d56d9 MA |
137 | bs->blk = blk; |
138 | return blk; | |
139 | } | |
140 | ||
ca49a4fd HR |
141 | /* |
142 | * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState. | |
143 | * | |
144 | * Just as with bdrv_open(), after having called this function the reference to | |
145 | * @options belongs to the block layer (even on failure). | |
146 | * | |
147 | * TODO: Remove @filename and @flags; it should be possible to specify a whole | |
148 | * BDS tree just by specifying the @options QDict (or @reference, | |
149 | * alternatively). At the time of adding this function, this is not possible, | |
150 | * though, so callers of this function have to be able to specify @filename and | |
151 | * @flags. | |
152 | */ | |
efaa7c4e HR |
153 | BlockBackend *blk_new_open(const char *filename, const char *reference, |
154 | QDict *options, int flags, Error **errp) | |
ca49a4fd HR |
155 | { |
156 | BlockBackend *blk; | |
157 | int ret; | |
158 | ||
efaa7c4e | 159 | blk = blk_new_with_bs(errp); |
ca49a4fd HR |
160 | if (!blk) { |
161 | QDECREF(options); | |
162 | return NULL; | |
163 | } | |
164 | ||
f21d96d0 | 165 | ret = bdrv_open(&blk->root->bs, filename, reference, options, flags, errp); |
ca49a4fd HR |
166 | if (ret < 0) { |
167 | blk_unref(blk); | |
168 | return NULL; | |
169 | } | |
170 | ||
72e775c7 KW |
171 | blk_set_enable_write_cache(blk, true); |
172 | ||
ca49a4fd HR |
173 | return blk; |
174 | } | |
175 | ||
26f54e9a MA |
176 | static void blk_delete(BlockBackend *blk) |
177 | { | |
178 | assert(!blk->refcnt); | |
e5e78550 | 179 | assert(!blk->name); |
a7f53e26 | 180 | assert(!blk->dev); |
f21d96d0 | 181 | if (blk->root) { |
13855c6b | 182 | blk_remove_bs(blk); |
7e7d56d9 | 183 | } |
3301f6c6 HR |
184 | assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers)); |
185 | assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers)); | |
281d22d8 HR |
186 | if (blk->root_state.throttle_state) { |
187 | g_free(blk->root_state.throttle_group); | |
188 | throttle_group_unref(blk->root_state.throttle_state); | |
189 | } | |
2cf22d6a | 190 | QTAILQ_REMOVE(&block_backends, blk, link); |
18e46a03 | 191 | drive_info_del(blk->legacy_dinfo); |
979e9b03 | 192 | block_acct_cleanup(&blk->stats); |
26f54e9a MA |
193 | g_free(blk); |
194 | } | |
195 | ||
8fb3c76c MA |
196 | static void drive_info_del(DriveInfo *dinfo) |
197 | { | |
198 | if (!dinfo) { | |
199 | return; | |
200 | } | |
201 | qemu_opts_del(dinfo->opts); | |
8fb3c76c MA |
202 | g_free(dinfo->serial); |
203 | g_free(dinfo); | |
204 | } | |
205 | ||
f636ae85 AG |
206 | int blk_get_refcnt(BlockBackend *blk) |
207 | { | |
208 | return blk ? blk->refcnt : 0; | |
209 | } | |
210 | ||
26f54e9a MA |
211 | /* |
212 | * Increment @blk's reference count. | |
213 | * @blk must not be null. | |
214 | */ | |
215 | void blk_ref(BlockBackend *blk) | |
216 | { | |
217 | blk->refcnt++; | |
218 | } | |
219 | ||
220 | /* | |
221 | * Decrement @blk's reference count. | |
222 | * If this drops it to zero, destroy @blk. | |
223 | * For convenience, do nothing if @blk is null. | |
224 | */ | |
225 | void blk_unref(BlockBackend *blk) | |
226 | { | |
227 | if (blk) { | |
228 | assert(blk->refcnt > 0); | |
229 | if (!--blk->refcnt) { | |
230 | blk_delete(blk); | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
2cf22d6a HR |
235 | /* |
236 | * Behaves similarly to blk_next() but iterates over all BlockBackends, even the | |
237 | * ones which are hidden (i.e. are not referenced by the monitor). | |
238 | */ | |
239 | static BlockBackend *blk_all_next(BlockBackend *blk) | |
240 | { | |
241 | return blk ? QTAILQ_NEXT(blk, link) | |
242 | : QTAILQ_FIRST(&block_backends); | |
243 | } | |
244 | ||
d8da3cef HR |
245 | void blk_remove_all_bs(void) |
246 | { | |
74d1b8fc | 247 | BlockBackend *blk = NULL; |
d8da3cef | 248 | |
2cf22d6a | 249 | while ((blk = blk_all_next(blk)) != NULL) { |
d8da3cef HR |
250 | AioContext *ctx = blk_get_aio_context(blk); |
251 | ||
252 | aio_context_acquire(ctx); | |
f21d96d0 | 253 | if (blk->root) { |
d8da3cef HR |
254 | blk_remove_bs(blk); |
255 | } | |
256 | aio_context_release(ctx); | |
257 | } | |
258 | } | |
259 | ||
26f54e9a | 260 | /* |
9492b0b9 | 261 | * Return the monitor-owned BlockBackend after @blk. |
26f54e9a MA |
262 | * If @blk is null, return the first one. |
263 | * Else, return @blk's next sibling, which may be null. | |
264 | * | |
265 | * To iterate over all BlockBackends, do | |
266 | * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { | |
267 | * ... | |
268 | * } | |
269 | */ | |
270 | BlockBackend *blk_next(BlockBackend *blk) | |
271 | { | |
9492b0b9 HR |
272 | return blk ? QTAILQ_NEXT(blk, monitor_link) |
273 | : QTAILQ_FIRST(&monitor_block_backends); | |
26f54e9a MA |
274 | } |
275 | ||
981f4f57 HR |
276 | /* |
277 | * Iterates over all BlockDriverStates which are attached to a BlockBackend. | |
278 | * This function is for use by bdrv_next(). | |
279 | * | |
280 | * @bs must be NULL or a BDS that is attached to a BB. | |
281 | */ | |
282 | BlockDriverState *blk_next_root_bs(BlockDriverState *bs) | |
283 | { | |
284 | BlockBackend *blk; | |
285 | ||
286 | if (bs) { | |
287 | assert(bs->blk); | |
288 | blk = bs->blk; | |
289 | } else { | |
290 | blk = NULL; | |
291 | } | |
292 | ||
293 | do { | |
294 | blk = blk_all_next(blk); | |
f21d96d0 | 295 | } while (blk && !blk->root); |
981f4f57 | 296 | |
f21d96d0 | 297 | return blk ? blk->root->bs : NULL; |
981f4f57 HR |
298 | } |
299 | ||
e5e78550 HR |
300 | /* |
301 | * Add a BlockBackend into the list of backends referenced by the monitor, with | |
302 | * the given @name acting as the handle for the monitor. | |
303 | * Strictly for use by blockdev.c. | |
304 | * | |
305 | * @name must not be null or empty. | |
306 | * | |
307 | * Returns true on success and false on failure. In the latter case, an Error | |
308 | * object is returned through @errp. | |
309 | */ | |
310 | bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp) | |
311 | { | |
312 | assert(!blk->name); | |
313 | assert(name && name[0]); | |
314 | ||
315 | if (!id_wellformed(name)) { | |
316 | error_setg(errp, "Invalid device name"); | |
317 | return false; | |
318 | } | |
319 | if (blk_by_name(name)) { | |
320 | error_setg(errp, "Device with id '%s' already exists", name); | |
321 | return false; | |
322 | } | |
323 | if (bdrv_find_node(name)) { | |
324 | error_setg(errp, | |
325 | "Device name '%s' conflicts with an existing node name", | |
326 | name); | |
327 | return false; | |
328 | } | |
329 | ||
330 | blk->name = g_strdup(name); | |
331 | QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link); | |
332 | return true; | |
333 | } | |
334 | ||
335 | /* | |
336 | * Remove a BlockBackend from the list of backends referenced by the monitor. | |
337 | * Strictly for use by blockdev.c. | |
338 | */ | |
339 | void monitor_remove_blk(BlockBackend *blk) | |
340 | { | |
341 | if (!blk->name) { | |
342 | return; | |
343 | } | |
344 | ||
345 | QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link); | |
346 | g_free(blk->name); | |
347 | blk->name = NULL; | |
348 | } | |
349 | ||
26f54e9a | 350 | /* |
7e7d56d9 | 351 | * Return @blk's name, a non-null string. |
e5e78550 | 352 | * Returns an empty string iff @blk is not referenced by the monitor. |
26f54e9a MA |
353 | */ |
354 | const char *blk_name(BlockBackend *blk) | |
355 | { | |
e5e78550 | 356 | return blk->name ?: ""; |
26f54e9a MA |
357 | } |
358 | ||
359 | /* | |
360 | * Return the BlockBackend with name @name if it exists, else null. | |
361 | * @name must not be null. | |
362 | */ | |
363 | BlockBackend *blk_by_name(const char *name) | |
364 | { | |
74d1b8fc | 365 | BlockBackend *blk = NULL; |
26f54e9a MA |
366 | |
367 | assert(name); | |
74d1b8fc | 368 | while ((blk = blk_next(blk)) != NULL) { |
26f54e9a MA |
369 | if (!strcmp(name, blk->name)) { |
370 | return blk; | |
371 | } | |
372 | } | |
373 | return NULL; | |
374 | } | |
7e7d56d9 MA |
375 | |
376 | /* | |
377 | * Return the BlockDriverState attached to @blk if any, else null. | |
378 | */ | |
379 | BlockDriverState *blk_bs(BlockBackend *blk) | |
380 | { | |
f21d96d0 | 381 | return blk->root ? blk->root->bs : NULL; |
7e7d56d9 MA |
382 | } |
383 | ||
18e46a03 MA |
384 | /* |
385 | * Return @blk's DriveInfo if any, else null. | |
386 | */ | |
387 | DriveInfo *blk_legacy_dinfo(BlockBackend *blk) | |
388 | { | |
389 | return blk->legacy_dinfo; | |
390 | } | |
391 | ||
392 | /* | |
393 | * Set @blk's DriveInfo to @dinfo, and return it. | |
394 | * @blk must not have a DriveInfo set already. | |
395 | * No other BlockBackend may have the same DriveInfo set. | |
396 | */ | |
397 | DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo) | |
398 | { | |
399 | assert(!blk->legacy_dinfo); | |
400 | return blk->legacy_dinfo = dinfo; | |
401 | } | |
402 | ||
403 | /* | |
404 | * Return the BlockBackend with DriveInfo @dinfo. | |
405 | * It must exist. | |
406 | */ | |
407 | BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo) | |
408 | { | |
74d1b8fc | 409 | BlockBackend *blk = NULL; |
18e46a03 | 410 | |
74d1b8fc | 411 | while ((blk = blk_next(blk)) != NULL) { |
18e46a03 MA |
412 | if (blk->legacy_dinfo == dinfo) { |
413 | return blk; | |
414 | } | |
415 | } | |
416 | abort(); | |
417 | } | |
418 | ||
f2cd875d KW |
419 | /* |
420 | * Returns a pointer to the publicly accessible fields of @blk. | |
421 | */ | |
422 | BlockBackendPublic *blk_get_public(BlockBackend *blk) | |
423 | { | |
424 | return &blk->public; | |
425 | } | |
426 | ||
427 | /* | |
428 | * Returns a BlockBackend given the associated @public fields. | |
429 | */ | |
430 | BlockBackend *blk_by_public(BlockBackendPublic *public) | |
431 | { | |
432 | return container_of(public, BlockBackend, public); | |
433 | } | |
434 | ||
1c95f7e1 HR |
435 | /* |
436 | * Disassociates the currently associated BlockDriverState from @blk. | |
437 | */ | |
438 | void blk_remove_bs(BlockBackend *blk) | |
439 | { | |
f21d96d0 | 440 | assert(blk->root->bs->blk == blk); |
13855c6b | 441 | |
3301f6c6 HR |
442 | notifier_list_notify(&blk->remove_bs_notifiers, blk); |
443 | ||
1c95f7e1 | 444 | blk_update_root_state(blk); |
27ccdd52 | 445 | if (blk->public.throttle_state) { |
97148076 | 446 | blk_io_limits_disable(blk); |
a5614993 | 447 | } |
1c95f7e1 | 448 | |
f21d96d0 KW |
449 | blk->root->bs->blk = NULL; |
450 | bdrv_root_unref_child(blk->root); | |
451 | blk->root = NULL; | |
1c95f7e1 HR |
452 | } |
453 | ||
0c3c36d6 HR |
454 | /* |
455 | * Associates a new BlockDriverState with @blk. | |
456 | */ | |
457 | void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs) | |
458 | { | |
f21d96d0 | 459 | assert(!blk->root && !bs->blk); |
0c3c36d6 | 460 | bdrv_ref(bs); |
f21d96d0 | 461 | blk->root = bdrv_root_attach_child(bs, "root", &child_root); |
22aa8b24 | 462 | blk->root->opaque = blk; |
0c3c36d6 | 463 | bs->blk = blk; |
3301f6c6 HR |
464 | |
465 | notifier_list_notify(&blk->insert_bs_notifiers, blk); | |
0c3c36d6 HR |
466 | } |
467 | ||
a7f53e26 MA |
468 | /* |
469 | * Attach device model @dev to @blk. | |
470 | * Return 0 on success, -EBUSY when a device model is attached already. | |
471 | */ | |
4be74634 | 472 | int blk_attach_dev(BlockBackend *blk, void *dev) |
a7f53e26 | 473 | /* TODO change to DeviceState *dev when all users are qdevified */ |
4be74634 | 474 | { |
a7f53e26 MA |
475 | if (blk->dev) { |
476 | return -EBUSY; | |
477 | } | |
84ebe375 | 478 | blk_ref(blk); |
a7f53e26 | 479 | blk->dev = dev; |
373340b2 | 480 | blk_iostatus_reset(blk); |
a7f53e26 | 481 | return 0; |
4be74634 MA |
482 | } |
483 | ||
a7f53e26 MA |
484 | /* |
485 | * Attach device model @dev to @blk. | |
486 | * @blk must not have a device model attached already. | |
487 | * TODO qdevified devices don't use this, remove when devices are qdevified | |
488 | */ | |
4be74634 MA |
489 | void blk_attach_dev_nofail(BlockBackend *blk, void *dev) |
490 | { | |
a7f53e26 MA |
491 | if (blk_attach_dev(blk, dev) < 0) { |
492 | abort(); | |
493 | } | |
4be74634 MA |
494 | } |
495 | ||
a7f53e26 MA |
496 | /* |
497 | * Detach device model @dev from @blk. | |
498 | * @dev must be currently attached to @blk. | |
499 | */ | |
4be74634 | 500 | void blk_detach_dev(BlockBackend *blk, void *dev) |
a7f53e26 | 501 | /* TODO change to DeviceState *dev when all users are qdevified */ |
4be74634 | 502 | { |
a7f53e26 MA |
503 | assert(blk->dev == dev); |
504 | blk->dev = NULL; | |
505 | blk->dev_ops = NULL; | |
506 | blk->dev_opaque = NULL; | |
68e9ec01 | 507 | blk->guest_block_size = 512; |
84ebe375 | 508 | blk_unref(blk); |
4be74634 MA |
509 | } |
510 | ||
a7f53e26 MA |
511 | /* |
512 | * Return the device model attached to @blk if any, else null. | |
513 | */ | |
4be74634 | 514 | void *blk_get_attached_dev(BlockBackend *blk) |
a7f53e26 MA |
515 | /* TODO change to return DeviceState * when all users are qdevified */ |
516 | { | |
517 | return blk->dev; | |
518 | } | |
519 | ||
520 | /* | |
521 | * Set @blk's device model callbacks to @ops. | |
522 | * @opaque is the opaque argument to pass to the callbacks. | |
523 | * This is for use by device models. | |
524 | */ | |
525 | void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, | |
526 | void *opaque) | |
527 | { | |
528 | blk->dev_ops = ops; | |
529 | blk->dev_opaque = opaque; | |
530 | } | |
531 | ||
532 | /* | |
533 | * Notify @blk's attached device model of media change. | |
534 | * If @load is true, notify of media load. | |
535 | * Else, notify of media eject. | |
536 | * Also send DEVICE_TRAY_MOVED events as appropriate. | |
537 | */ | |
538 | void blk_dev_change_media_cb(BlockBackend *blk, bool load) | |
539 | { | |
540 | if (blk->dev_ops && blk->dev_ops->change_media_cb) { | |
f1f57066 | 541 | bool tray_was_open, tray_is_open; |
a7f53e26 | 542 | |
f1f57066 | 543 | tray_was_open = blk_dev_is_tray_open(blk); |
a7f53e26 | 544 | blk->dev_ops->change_media_cb(blk->dev_opaque, load); |
f1f57066 HR |
545 | tray_is_open = blk_dev_is_tray_open(blk); |
546 | ||
547 | if (tray_was_open != tray_is_open) { | |
548 | qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open, | |
549 | &error_abort); | |
a7f53e26 MA |
550 | } |
551 | } | |
552 | } | |
553 | ||
554 | /* | |
555 | * Does @blk's attached device model have removable media? | |
556 | * %true if no device model is attached. | |
557 | */ | |
558 | bool blk_dev_has_removable_media(BlockBackend *blk) | |
559 | { | |
560 | return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb); | |
561 | } | |
562 | ||
8f3a73bc HR |
563 | /* |
564 | * Does @blk's attached device model have a tray? | |
565 | */ | |
566 | bool blk_dev_has_tray(BlockBackend *blk) | |
567 | { | |
568 | return blk->dev_ops && blk->dev_ops->is_tray_open; | |
569 | } | |
570 | ||
a7f53e26 MA |
571 | /* |
572 | * Notify @blk's attached device model of a media eject request. | |
573 | * If @force is true, the medium is about to be yanked out forcefully. | |
574 | */ | |
575 | void blk_dev_eject_request(BlockBackend *blk, bool force) | |
4be74634 | 576 | { |
a7f53e26 MA |
577 | if (blk->dev_ops && blk->dev_ops->eject_request_cb) { |
578 | blk->dev_ops->eject_request_cb(blk->dev_opaque, force); | |
579 | } | |
4be74634 MA |
580 | } |
581 | ||
a7f53e26 MA |
582 | /* |
583 | * Does @blk's attached device model have a tray, and is it open? | |
584 | */ | |
585 | bool blk_dev_is_tray_open(BlockBackend *blk) | |
4be74634 | 586 | { |
8f3a73bc | 587 | if (blk_dev_has_tray(blk)) { |
a7f53e26 MA |
588 | return blk->dev_ops->is_tray_open(blk->dev_opaque); |
589 | } | |
590 | return false; | |
591 | } | |
592 | ||
593 | /* | |
594 | * Does @blk's attached device model have the medium locked? | |
595 | * %false if the device model has no such lock. | |
596 | */ | |
597 | bool blk_dev_is_medium_locked(BlockBackend *blk) | |
598 | { | |
599 | if (blk->dev_ops && blk->dev_ops->is_medium_locked) { | |
600 | return blk->dev_ops->is_medium_locked(blk->dev_opaque); | |
601 | } | |
602 | return false; | |
603 | } | |
604 | ||
605 | /* | |
606 | * Notify @blk's attached device model of a backend size change. | |
607 | */ | |
608 | void blk_dev_resize_cb(BlockBackend *blk) | |
609 | { | |
610 | if (blk->dev_ops && blk->dev_ops->resize_cb) { | |
611 | blk->dev_ops->resize_cb(blk->dev_opaque); | |
612 | } | |
613 | } | |
614 | ||
615 | void blk_iostatus_enable(BlockBackend *blk) | |
616 | { | |
373340b2 HR |
617 | blk->iostatus_enabled = true; |
618 | blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK; | |
619 | } | |
620 | ||
621 | /* The I/O status is only enabled if the drive explicitly | |
622 | * enables it _and_ the VM is configured to stop on errors */ | |
623 | bool blk_iostatus_is_enabled(const BlockBackend *blk) | |
624 | { | |
625 | return (blk->iostatus_enabled && | |
626 | (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || | |
627 | blk->on_write_error == BLOCKDEV_ON_ERROR_STOP || | |
628 | blk->on_read_error == BLOCKDEV_ON_ERROR_STOP)); | |
629 | } | |
630 | ||
631 | BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk) | |
632 | { | |
633 | return blk->iostatus; | |
634 | } | |
635 | ||
636 | void blk_iostatus_disable(BlockBackend *blk) | |
637 | { | |
638 | blk->iostatus_enabled = false; | |
639 | } | |
640 | ||
641 | void blk_iostatus_reset(BlockBackend *blk) | |
642 | { | |
643 | if (blk_iostatus_is_enabled(blk)) { | |
f21d96d0 | 644 | BlockDriverState *bs = blk_bs(blk); |
373340b2 | 645 | blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
f21d96d0 KW |
646 | if (bs && bs->job) { |
647 | block_job_iostatus_reset(bs->job); | |
373340b2 HR |
648 | } |
649 | } | |
650 | } | |
651 | ||
652 | void blk_iostatus_set_err(BlockBackend *blk, int error) | |
653 | { | |
654 | assert(blk_iostatus_is_enabled(blk)); | |
655 | if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { | |
656 | blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : | |
657 | BLOCK_DEVICE_IO_STATUS_FAILED; | |
658 | } | |
4be74634 MA |
659 | } |
660 | ||
c10c9d96 KW |
661 | void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow) |
662 | { | |
663 | blk->allow_write_beyond_eof = allow; | |
664 | } | |
665 | ||
e7f7d676 HR |
666 | static int blk_check_byte_request(BlockBackend *blk, int64_t offset, |
667 | size_t size) | |
668 | { | |
669 | int64_t len; | |
670 | ||
671 | if (size > INT_MAX) { | |
672 | return -EIO; | |
673 | } | |
674 | ||
c09ba36c | 675 | if (!blk_is_available(blk)) { |
e7f7d676 HR |
676 | return -ENOMEDIUM; |
677 | } | |
678 | ||
e7f7d676 HR |
679 | if (offset < 0) { |
680 | return -EIO; | |
681 | } | |
682 | ||
c10c9d96 KW |
683 | if (!blk->allow_write_beyond_eof) { |
684 | len = blk_getlength(blk); | |
685 | if (len < 0) { | |
686 | return len; | |
687 | } | |
688 | ||
689 | if (offset > len || len - offset < size) { | |
690 | return -EIO; | |
691 | } | |
e7f7d676 HR |
692 | } |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
697 | static int blk_check_request(BlockBackend *blk, int64_t sector_num, | |
698 | int nb_sectors) | |
699 | { | |
700 | if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) { | |
701 | return -EIO; | |
702 | } | |
703 | ||
704 | if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) { | |
705 | return -EIO; | |
706 | } | |
707 | ||
708 | return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE, | |
709 | nb_sectors * BDRV_SECTOR_SIZE); | |
710 | } | |
711 | ||
1bf1cbc9 KW |
712 | static int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset, |
713 | unsigned int bytes, QEMUIOVector *qiov, | |
714 | BdrvRequestFlags flags) | |
4be74634 | 715 | { |
1bf1cbc9 | 716 | int ret = blk_check_byte_request(blk, offset, bytes); |
e7f7d676 HR |
717 | if (ret < 0) { |
718 | return ret; | |
719 | } | |
720 | ||
441565b2 KW |
721 | /* throttling disk I/O */ |
722 | if (blk->public.throttle_state) { | |
723 | throttle_group_co_io_limits_intercept(blk, bytes, false); | |
724 | } | |
725 | ||
cab3a356 | 726 | return bdrv_co_preadv(blk_bs(blk), offset, bytes, qiov, flags); |
1bf1cbc9 KW |
727 | } |
728 | ||
a8823a3b KW |
729 | static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset, |
730 | unsigned int bytes, QEMUIOVector *qiov, | |
731 | BdrvRequestFlags flags) | |
732 | { | |
bfd18d1e KW |
733 | int ret; |
734 | ||
735 | ret = blk_check_byte_request(blk, offset, bytes); | |
a8823a3b KW |
736 | if (ret < 0) { |
737 | return ret; | |
738 | } | |
739 | ||
441565b2 KW |
740 | /* throttling disk I/O */ |
741 | if (blk->public.throttle_state) { | |
742 | throttle_group_co_io_limits_intercept(blk, bytes, true); | |
743 | } | |
744 | ||
bfd18d1e KW |
745 | if (!blk->enable_write_cache) { |
746 | flags |= BDRV_REQ_FUA; | |
747 | } | |
748 | ||
cab3a356 | 749 | return bdrv_co_pwritev(blk_bs(blk), offset, bytes, qiov, flags); |
a8823a3b KW |
750 | } |
751 | ||
1bf1cbc9 KW |
752 | typedef struct BlkRwCo { |
753 | BlockBackend *blk; | |
754 | int64_t offset; | |
755 | QEMUIOVector *qiov; | |
756 | int ret; | |
757 | BdrvRequestFlags flags; | |
758 | } BlkRwCo; | |
759 | ||
760 | static void blk_read_entry(void *opaque) | |
761 | { | |
762 | BlkRwCo *rwco = opaque; | |
763 | ||
764 | rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size, | |
765 | rwco->qiov, rwco->flags); | |
766 | } | |
767 | ||
a8823a3b KW |
768 | static void blk_write_entry(void *opaque) |
769 | { | |
770 | BlkRwCo *rwco = opaque; | |
771 | ||
772 | rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size, | |
773 | rwco->qiov, rwco->flags); | |
774 | } | |
775 | ||
a55d3fba KW |
776 | static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf, |
777 | int64_t bytes, CoroutineEntry co_entry, | |
778 | BdrvRequestFlags flags) | |
1bf1cbc9 KW |
779 | { |
780 | AioContext *aio_context; | |
781 | QEMUIOVector qiov; | |
782 | struct iovec iov; | |
783 | Coroutine *co; | |
784 | BlkRwCo rwco; | |
785 | ||
1bf1cbc9 KW |
786 | iov = (struct iovec) { |
787 | .iov_base = buf, | |
a55d3fba | 788 | .iov_len = bytes, |
1bf1cbc9 KW |
789 | }; |
790 | qemu_iovec_init_external(&qiov, &iov, 1); | |
791 | ||
792 | rwco = (BlkRwCo) { | |
793 | .blk = blk, | |
a55d3fba | 794 | .offset = offset, |
1bf1cbc9 | 795 | .qiov = &qiov, |
fc1453cd | 796 | .flags = flags, |
1bf1cbc9 KW |
797 | .ret = NOT_DONE, |
798 | }; | |
799 | ||
a8823a3b | 800 | co = qemu_coroutine_create(co_entry); |
1bf1cbc9 KW |
801 | qemu_coroutine_enter(co, &rwco); |
802 | ||
803 | aio_context = blk_get_aio_context(blk); | |
804 | while (rwco.ret == NOT_DONE) { | |
805 | aio_poll(aio_context, true); | |
806 | } | |
807 | ||
808 | return rwco.ret; | |
4be74634 MA |
809 | } |
810 | ||
b7d17f9f EB |
811 | int blk_pread_unthrottled(BlockBackend *blk, int64_t offset, uint8_t *buf, |
812 | int count) | |
4be74634 | 813 | { |
5bd51196 KW |
814 | int ret; |
815 | ||
b7d17f9f | 816 | ret = blk_check_byte_request(blk, offset, count); |
e7f7d676 HR |
817 | if (ret < 0) { |
818 | return ret; | |
819 | } | |
820 | ||
27ccdd52 | 821 | bdrv_no_throttling_begin(blk_bs(blk)); |
b7d17f9f | 822 | ret = blk_pread(blk, offset, buf, count); |
27ccdd52 | 823 | bdrv_no_throttling_end(blk_bs(blk)); |
5bd51196 | 824 | return ret; |
4be74634 MA |
825 | } |
826 | ||
983a1600 EB |
827 | int blk_write_zeroes(BlockBackend *blk, int64_t offset, |
828 | int count, BdrvRequestFlags flags) | |
0df89e8e | 829 | { |
983a1600 EB |
830 | return blk_prw(blk, offset, NULL, count, blk_write_entry, |
831 | flags | BDRV_REQ_ZERO_WRITE); | |
0df89e8e KW |
832 | } |
833 | ||
e7f7d676 HR |
834 | static void error_callback_bh(void *opaque) |
835 | { | |
836 | struct BlockBackendAIOCB *acb = opaque; | |
837 | qemu_bh_delete(acb->bh); | |
838 | acb->common.cb(acb->common.opaque, acb->ret); | |
839 | qemu_aio_unref(acb); | |
840 | } | |
841 | ||
ca78ecfa PL |
842 | BlockAIOCB *blk_abort_aio_request(BlockBackend *blk, |
843 | BlockCompletionFunc *cb, | |
844 | void *opaque, int ret) | |
e7f7d676 HR |
845 | { |
846 | struct BlockBackendAIOCB *acb; | |
847 | QEMUBH *bh; | |
848 | ||
849 | acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque); | |
4981bdec | 850 | acb->blk = blk; |
e7f7d676 HR |
851 | acb->ret = ret; |
852 | ||
853 | bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb); | |
854 | acb->bh = bh; | |
855 | qemu_bh_schedule(bh); | |
856 | ||
857 | return &acb->common; | |
858 | } | |
859 | ||
57d6a428 KW |
860 | typedef struct BlkAioEmAIOCB { |
861 | BlockAIOCB common; | |
862 | BlkRwCo rwco; | |
7fa84cd8 | 863 | int bytes; |
57d6a428 KW |
864 | bool has_returned; |
865 | QEMUBH* bh; | |
866 | } BlkAioEmAIOCB; | |
867 | ||
868 | static const AIOCBInfo blk_aio_em_aiocb_info = { | |
869 | .aiocb_size = sizeof(BlkAioEmAIOCB), | |
870 | }; | |
871 | ||
872 | static void blk_aio_complete(BlkAioEmAIOCB *acb) | |
873 | { | |
874 | if (acb->bh) { | |
875 | assert(acb->has_returned); | |
876 | qemu_bh_delete(acb->bh); | |
877 | } | |
878 | if (acb->has_returned) { | |
879 | acb->common.cb(acb->common.opaque, acb->rwco.ret); | |
880 | qemu_aio_unref(acb); | |
881 | } | |
882 | } | |
883 | ||
884 | static void blk_aio_complete_bh(void *opaque) | |
885 | { | |
886 | blk_aio_complete(opaque); | |
887 | } | |
888 | ||
7fa84cd8 | 889 | static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes, |
57d6a428 KW |
890 | QEMUIOVector *qiov, CoroutineEntry co_entry, |
891 | BdrvRequestFlags flags, | |
892 | BlockCompletionFunc *cb, void *opaque) | |
893 | { | |
894 | BlkAioEmAIOCB *acb; | |
895 | Coroutine *co; | |
896 | ||
897 | acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque); | |
898 | acb->rwco = (BlkRwCo) { | |
899 | .blk = blk, | |
900 | .offset = offset, | |
901 | .qiov = qiov, | |
902 | .flags = flags, | |
903 | .ret = NOT_DONE, | |
904 | }; | |
7fa84cd8 | 905 | acb->bytes = bytes; |
57d6a428 KW |
906 | acb->bh = NULL; |
907 | acb->has_returned = false; | |
908 | ||
909 | co = qemu_coroutine_create(co_entry); | |
910 | qemu_coroutine_enter(co, acb); | |
911 | ||
912 | acb->has_returned = true; | |
913 | if (acb->rwco.ret != NOT_DONE) { | |
914 | acb->bh = aio_bh_new(blk_get_aio_context(blk), blk_aio_complete_bh, acb); | |
915 | qemu_bh_schedule(acb->bh); | |
916 | } | |
917 | ||
918 | return &acb->common; | |
919 | } | |
920 | ||
921 | static void blk_aio_read_entry(void *opaque) | |
922 | { | |
923 | BlkAioEmAIOCB *acb = opaque; | |
924 | BlkRwCo *rwco = &acb->rwco; | |
925 | ||
7fa84cd8 KW |
926 | assert(rwco->qiov->size == acb->bytes); |
927 | rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, acb->bytes, | |
57d6a428 KW |
928 | rwco->qiov, rwco->flags); |
929 | blk_aio_complete(acb); | |
930 | } | |
931 | ||
932 | static void blk_aio_write_entry(void *opaque) | |
933 | { | |
934 | BlkAioEmAIOCB *acb = opaque; | |
935 | BlkRwCo *rwco = &acb->rwco; | |
936 | ||
7fa84cd8 KW |
937 | assert(!rwco->qiov || rwco->qiov->size == acb->bytes); |
938 | rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes, | |
57d6a428 KW |
939 | rwco->qiov, rwco->flags); |
940 | blk_aio_complete(acb); | |
941 | } | |
942 | ||
983a1600 EB |
943 | BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t offset, |
944 | int count, BdrvRequestFlags flags, | |
4be74634 MA |
945 | BlockCompletionFunc *cb, void *opaque) |
946 | { | |
983a1600 EB |
947 | return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry, |
948 | flags | BDRV_REQ_ZERO_WRITE, cb, opaque); | |
4be74634 MA |
949 | } |
950 | ||
951 | int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count) | |
952 | { | |
a55d3fba | 953 | int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0); |
e7f7d676 HR |
954 | if (ret < 0) { |
955 | return ret; | |
956 | } | |
a55d3fba | 957 | return count; |
4be74634 MA |
958 | } |
959 | ||
8341f00d EB |
960 | int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count, |
961 | BdrvRequestFlags flags) | |
4be74634 | 962 | { |
8341f00d EB |
963 | int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry, |
964 | flags); | |
e7f7d676 HR |
965 | if (ret < 0) { |
966 | return ret; | |
967 | } | |
a55d3fba | 968 | return count; |
4be74634 MA |
969 | } |
970 | ||
971 | int64_t blk_getlength(BlockBackend *blk) | |
972 | { | |
c09ba36c HR |
973 | if (!blk_is_available(blk)) { |
974 | return -ENOMEDIUM; | |
975 | } | |
976 | ||
f21d96d0 | 977 | return bdrv_getlength(blk_bs(blk)); |
4be74634 MA |
978 | } |
979 | ||
980 | void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr) | |
981 | { | |
f21d96d0 | 982 | if (!blk_bs(blk)) { |
a46fc9c9 HR |
983 | *nb_sectors_ptr = 0; |
984 | } else { | |
f21d96d0 | 985 | bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr); |
a46fc9c9 | 986 | } |
4be74634 MA |
987 | } |
988 | ||
1ef01253 HR |
989 | int64_t blk_nb_sectors(BlockBackend *blk) |
990 | { | |
c09ba36c HR |
991 | if (!blk_is_available(blk)) { |
992 | return -ENOMEDIUM; | |
993 | } | |
994 | ||
f21d96d0 | 995 | return bdrv_nb_sectors(blk_bs(blk)); |
1ef01253 HR |
996 | } |
997 | ||
60cb2fa7 EB |
998 | BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset, |
999 | QEMUIOVector *qiov, BdrvRequestFlags flags, | |
1000 | BlockCompletionFunc *cb, void *opaque) | |
1001 | { | |
1002 | return blk_aio_prwv(blk, offset, qiov->size, qiov, | |
1003 | blk_aio_read_entry, flags, cb, opaque); | |
1004 | } | |
1005 | ||
60cb2fa7 EB |
1006 | BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset, |
1007 | QEMUIOVector *qiov, BdrvRequestFlags flags, | |
1008 | BlockCompletionFunc *cb, void *opaque) | |
1009 | { | |
1010 | return blk_aio_prwv(blk, offset, qiov->size, qiov, | |
1011 | blk_aio_write_entry, flags, cb, opaque); | |
1012 | } | |
1013 | ||
4be74634 MA |
1014 | BlockAIOCB *blk_aio_flush(BlockBackend *blk, |
1015 | BlockCompletionFunc *cb, void *opaque) | |
1016 | { | |
c09ba36c | 1017 | if (!blk_is_available(blk)) { |
ca78ecfa | 1018 | return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM); |
c09ba36c HR |
1019 | } |
1020 | ||
f21d96d0 | 1021 | return bdrv_aio_flush(blk_bs(blk), cb, opaque); |
4be74634 MA |
1022 | } |
1023 | ||
1024 | BlockAIOCB *blk_aio_discard(BlockBackend *blk, | |
1025 | int64_t sector_num, int nb_sectors, | |
1026 | BlockCompletionFunc *cb, void *opaque) | |
1027 | { | |
e7f7d676 HR |
1028 | int ret = blk_check_request(blk, sector_num, nb_sectors); |
1029 | if (ret < 0) { | |
ca78ecfa | 1030 | return blk_abort_aio_request(blk, cb, opaque, ret); |
e7f7d676 HR |
1031 | } |
1032 | ||
f21d96d0 | 1033 | return bdrv_aio_discard(blk_bs(blk), sector_num, nb_sectors, cb, opaque); |
4be74634 MA |
1034 | } |
1035 | ||
1036 | void blk_aio_cancel(BlockAIOCB *acb) | |
1037 | { | |
1038 | bdrv_aio_cancel(acb); | |
1039 | } | |
1040 | ||
1041 | void blk_aio_cancel_async(BlockAIOCB *acb) | |
1042 | { | |
1043 | bdrv_aio_cancel_async(acb); | |
1044 | } | |
1045 | ||
1046 | int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs) | |
1047 | { | |
e7f7d676 HR |
1048 | int i, ret; |
1049 | ||
1050 | for (i = 0; i < num_reqs; i++) { | |
1051 | ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors); | |
1052 | if (ret < 0) { | |
1053 | return ret; | |
1054 | } | |
1055 | } | |
1056 | ||
f21d96d0 | 1057 | return bdrv_aio_multiwrite(blk_bs(blk), reqs, num_reqs); |
4be74634 MA |
1058 | } |
1059 | ||
1060 | int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf) | |
1061 | { | |
c09ba36c HR |
1062 | if (!blk_is_available(blk)) { |
1063 | return -ENOMEDIUM; | |
1064 | } | |
1065 | ||
f21d96d0 | 1066 | return bdrv_ioctl(blk_bs(blk), req, buf); |
4be74634 MA |
1067 | } |
1068 | ||
1069 | BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf, | |
1070 | BlockCompletionFunc *cb, void *opaque) | |
1071 | { | |
c09ba36c | 1072 | if (!blk_is_available(blk)) { |
ca78ecfa | 1073 | return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM); |
c09ba36c HR |
1074 | } |
1075 | ||
f21d96d0 | 1076 | return bdrv_aio_ioctl(blk_bs(blk), req, buf, cb, opaque); |
4be74634 MA |
1077 | } |
1078 | ||
2bb0dce7 HR |
1079 | int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) |
1080 | { | |
e7f7d676 HR |
1081 | int ret = blk_check_request(blk, sector_num, nb_sectors); |
1082 | if (ret < 0) { | |
1083 | return ret; | |
1084 | } | |
1085 | ||
f21d96d0 | 1086 | return bdrv_co_discard(blk_bs(blk), sector_num, nb_sectors); |
2bb0dce7 HR |
1087 | } |
1088 | ||
1089 | int blk_co_flush(BlockBackend *blk) | |
1090 | { | |
c09ba36c HR |
1091 | if (!blk_is_available(blk)) { |
1092 | return -ENOMEDIUM; | |
1093 | } | |
1094 | ||
f21d96d0 | 1095 | return bdrv_co_flush(blk_bs(blk)); |
2bb0dce7 HR |
1096 | } |
1097 | ||
4be74634 MA |
1098 | int blk_flush(BlockBackend *blk) |
1099 | { | |
c09ba36c HR |
1100 | if (!blk_is_available(blk)) { |
1101 | return -ENOMEDIUM; | |
1102 | } | |
1103 | ||
f21d96d0 | 1104 | return bdrv_flush(blk_bs(blk)); |
4be74634 MA |
1105 | } |
1106 | ||
97b0385a AY |
1107 | void blk_drain(BlockBackend *blk) |
1108 | { | |
f21d96d0 KW |
1109 | if (blk_bs(blk)) { |
1110 | bdrv_drain(blk_bs(blk)); | |
a46fc9c9 | 1111 | } |
97b0385a AY |
1112 | } |
1113 | ||
4be74634 MA |
1114 | void blk_drain_all(void) |
1115 | { | |
1116 | bdrv_drain_all(); | |
1117 | } | |
1118 | ||
373340b2 HR |
1119 | void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error, |
1120 | BlockdevOnError on_write_error) | |
1121 | { | |
1122 | blk->on_read_error = on_read_error; | |
1123 | blk->on_write_error = on_write_error; | |
1124 | } | |
1125 | ||
4be74634 MA |
1126 | BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read) |
1127 | { | |
373340b2 | 1128 | return is_read ? blk->on_read_error : blk->on_write_error; |
4be74634 MA |
1129 | } |
1130 | ||
1131 | BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read, | |
1132 | int error) | |
1133 | { | |
373340b2 HR |
1134 | BlockdevOnError on_err = blk_get_on_error(blk, is_read); |
1135 | ||
1136 | switch (on_err) { | |
1137 | case BLOCKDEV_ON_ERROR_ENOSPC: | |
1138 | return (error == ENOSPC) ? | |
1139 | BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT; | |
1140 | case BLOCKDEV_ON_ERROR_STOP: | |
1141 | return BLOCK_ERROR_ACTION_STOP; | |
1142 | case BLOCKDEV_ON_ERROR_REPORT: | |
1143 | return BLOCK_ERROR_ACTION_REPORT; | |
1144 | case BLOCKDEV_ON_ERROR_IGNORE: | |
1145 | return BLOCK_ERROR_ACTION_IGNORE; | |
1146 | default: | |
1147 | abort(); | |
1148 | } | |
4be74634 MA |
1149 | } |
1150 | ||
373340b2 HR |
1151 | static void send_qmp_error_event(BlockBackend *blk, |
1152 | BlockErrorAction action, | |
1153 | bool is_read, int error) | |
1154 | { | |
1155 | IoOperationType optype; | |
1156 | ||
1157 | optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE; | |
1158 | qapi_event_send_block_io_error(blk_name(blk), optype, action, | |
1159 | blk_iostatus_is_enabled(blk), | |
1160 | error == ENOSPC, strerror(error), | |
1161 | &error_abort); | |
1162 | } | |
1163 | ||
1164 | /* This is done by device models because, while the block layer knows | |
1165 | * about the error, it does not know whether an operation comes from | |
1166 | * the device or the block layer (from a job, for example). | |
1167 | */ | |
4be74634 MA |
1168 | void blk_error_action(BlockBackend *blk, BlockErrorAction action, |
1169 | bool is_read, int error) | |
1170 | { | |
373340b2 HR |
1171 | assert(error >= 0); |
1172 | ||
1173 | if (action == BLOCK_ERROR_ACTION_STOP) { | |
1174 | /* First set the iostatus, so that "info block" returns an iostatus | |
1175 | * that matches the events raised so far (an additional error iostatus | |
1176 | * is fine, but not a lost one). | |
1177 | */ | |
1178 | blk_iostatus_set_err(blk, error); | |
1179 | ||
1180 | /* Then raise the request to stop the VM and the event. | |
1181 | * qemu_system_vmstop_request_prepare has two effects. First, | |
1182 | * it ensures that the STOP event always comes after the | |
1183 | * BLOCK_IO_ERROR event. Second, it ensures that even if management | |
1184 | * can observe the STOP event and do a "cont" before the STOP | |
1185 | * event is issued, the VM will not stop. In this case, vm_start() | |
1186 | * also ensures that the STOP/RESUME pair of events is emitted. | |
1187 | */ | |
1188 | qemu_system_vmstop_request_prepare(); | |
1189 | send_qmp_error_event(blk, action, is_read, error); | |
1190 | qemu_system_vmstop_request(RUN_STATE_IO_ERROR); | |
1191 | } else { | |
1192 | send_qmp_error_event(blk, action, is_read, error); | |
1193 | } | |
4be74634 MA |
1194 | } |
1195 | ||
1196 | int blk_is_read_only(BlockBackend *blk) | |
1197 | { | |
f21d96d0 KW |
1198 | BlockDriverState *bs = blk_bs(blk); |
1199 | ||
1200 | if (bs) { | |
1201 | return bdrv_is_read_only(bs); | |
061959e8 HR |
1202 | } else { |
1203 | return blk->root_state.read_only; | |
1204 | } | |
4be74634 MA |
1205 | } |
1206 | ||
1207 | int blk_is_sg(BlockBackend *blk) | |
1208 | { | |
f21d96d0 KW |
1209 | BlockDriverState *bs = blk_bs(blk); |
1210 | ||
1211 | if (!bs) { | |
a46fc9c9 HR |
1212 | return 0; |
1213 | } | |
1214 | ||
f21d96d0 | 1215 | return bdrv_is_sg(bs); |
4be74634 MA |
1216 | } |
1217 | ||
1218 | int blk_enable_write_cache(BlockBackend *blk) | |
1219 | { | |
bfd18d1e | 1220 | return blk->enable_write_cache; |
4be74634 MA |
1221 | } |
1222 | ||
1223 | void blk_set_enable_write_cache(BlockBackend *blk, bool wce) | |
1224 | { | |
bfd18d1e | 1225 | blk->enable_write_cache = wce; |
4be74634 MA |
1226 | } |
1227 | ||
2bb0dce7 HR |
1228 | void blk_invalidate_cache(BlockBackend *blk, Error **errp) |
1229 | { | |
f21d96d0 KW |
1230 | BlockDriverState *bs = blk_bs(blk); |
1231 | ||
1232 | if (!bs) { | |
c09ba36c HR |
1233 | error_setg(errp, "Device '%s' has no medium", blk->name); |
1234 | return; | |
1235 | } | |
1236 | ||
f21d96d0 | 1237 | bdrv_invalidate_cache(bs, errp); |
2bb0dce7 HR |
1238 | } |
1239 | ||
e031f750 | 1240 | bool blk_is_inserted(BlockBackend *blk) |
4be74634 | 1241 | { |
f21d96d0 KW |
1242 | BlockDriverState *bs = blk_bs(blk); |
1243 | ||
1244 | return bs && bdrv_is_inserted(bs); | |
db0284f8 HR |
1245 | } |
1246 | ||
1247 | bool blk_is_available(BlockBackend *blk) | |
1248 | { | |
1249 | return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk); | |
4be74634 MA |
1250 | } |
1251 | ||
1252 | void blk_lock_medium(BlockBackend *blk, bool locked) | |
1253 | { | |
f21d96d0 KW |
1254 | BlockDriverState *bs = blk_bs(blk); |
1255 | ||
1256 | if (bs) { | |
1257 | bdrv_lock_medium(bs, locked); | |
a46fc9c9 | 1258 | } |
4be74634 MA |
1259 | } |
1260 | ||
1261 | void blk_eject(BlockBackend *blk, bool eject_flag) | |
1262 | { | |
f21d96d0 KW |
1263 | BlockDriverState *bs = blk_bs(blk); |
1264 | ||
1265 | if (bs) { | |
1266 | bdrv_eject(bs, eject_flag); | |
a46fc9c9 | 1267 | } |
4be74634 MA |
1268 | } |
1269 | ||
1270 | int blk_get_flags(BlockBackend *blk) | |
1271 | { | |
f21d96d0 KW |
1272 | BlockDriverState *bs = blk_bs(blk); |
1273 | ||
1274 | if (bs) { | |
1275 | return bdrv_get_flags(bs); | |
061959e8 HR |
1276 | } else { |
1277 | return blk->root_state.open_flags; | |
1278 | } | |
4be74634 MA |
1279 | } |
1280 | ||
454057b7 PL |
1281 | int blk_get_max_transfer_length(BlockBackend *blk) |
1282 | { | |
f21d96d0 KW |
1283 | BlockDriverState *bs = blk_bs(blk); |
1284 | ||
1285 | if (bs) { | |
1286 | return bs->bl.max_transfer_length; | |
a46fc9c9 HR |
1287 | } else { |
1288 | return 0; | |
1289 | } | |
454057b7 PL |
1290 | } |
1291 | ||
648296e0 SH |
1292 | int blk_get_max_iov(BlockBackend *blk) |
1293 | { | |
f21d96d0 | 1294 | return blk->root->bs->bl.max_iov; |
648296e0 SH |
1295 | } |
1296 | ||
4be74634 MA |
1297 | void blk_set_guest_block_size(BlockBackend *blk, int align) |
1298 | { | |
68e9ec01 | 1299 | blk->guest_block_size = align; |
4be74634 MA |
1300 | } |
1301 | ||
f1c17521 PB |
1302 | void *blk_try_blockalign(BlockBackend *blk, size_t size) |
1303 | { | |
f21d96d0 | 1304 | return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size); |
f1c17521 PB |
1305 | } |
1306 | ||
4be74634 MA |
1307 | void *blk_blockalign(BlockBackend *blk, size_t size) |
1308 | { | |
f21d96d0 | 1309 | return qemu_blockalign(blk ? blk_bs(blk) : NULL, size); |
4be74634 MA |
1310 | } |
1311 | ||
1312 | bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp) | |
1313 | { | |
f21d96d0 KW |
1314 | BlockDriverState *bs = blk_bs(blk); |
1315 | ||
1316 | if (!bs) { | |
a46fc9c9 HR |
1317 | return false; |
1318 | } | |
1319 | ||
f21d96d0 | 1320 | return bdrv_op_is_blocked(bs, op, errp); |
4be74634 MA |
1321 | } |
1322 | ||
1323 | void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason) | |
1324 | { | |
f21d96d0 KW |
1325 | BlockDriverState *bs = blk_bs(blk); |
1326 | ||
1327 | if (bs) { | |
1328 | bdrv_op_unblock(bs, op, reason); | |
a46fc9c9 | 1329 | } |
4be74634 MA |
1330 | } |
1331 | ||
1332 | void blk_op_block_all(BlockBackend *blk, Error *reason) | |
1333 | { | |
f21d96d0 KW |
1334 | BlockDriverState *bs = blk_bs(blk); |
1335 | ||
1336 | if (bs) { | |
1337 | bdrv_op_block_all(bs, reason); | |
a46fc9c9 | 1338 | } |
4be74634 MA |
1339 | } |
1340 | ||
1341 | void blk_op_unblock_all(BlockBackend *blk, Error *reason) | |
1342 | { | |
f21d96d0 KW |
1343 | BlockDriverState *bs = blk_bs(blk); |
1344 | ||
1345 | if (bs) { | |
1346 | bdrv_op_unblock_all(bs, reason); | |
a46fc9c9 | 1347 | } |
4be74634 MA |
1348 | } |
1349 | ||
1350 | AioContext *blk_get_aio_context(BlockBackend *blk) | |
1351 | { | |
f21d96d0 KW |
1352 | BlockDriverState *bs = blk_bs(blk); |
1353 | ||
1354 | if (bs) { | |
1355 | return bdrv_get_aio_context(bs); | |
4981bdec HR |
1356 | } else { |
1357 | return qemu_get_aio_context(); | |
1358 | } | |
1359 | } | |
1360 | ||
1361 | static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb) | |
1362 | { | |
1363 | BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb); | |
1364 | return blk_get_aio_context(blk_acb->blk); | |
4be74634 MA |
1365 | } |
1366 | ||
1367 | void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) | |
1368 | { | |
f21d96d0 KW |
1369 | BlockDriverState *bs = blk_bs(blk); |
1370 | ||
1371 | if (bs) { | |
1372 | bdrv_set_aio_context(bs, new_context); | |
a46fc9c9 | 1373 | } |
4be74634 MA |
1374 | } |
1375 | ||
2019ba0a HR |
1376 | void blk_add_aio_context_notifier(BlockBackend *blk, |
1377 | void (*attached_aio_context)(AioContext *new_context, void *opaque), | |
1378 | void (*detach_aio_context)(void *opaque), void *opaque) | |
1379 | { | |
f21d96d0 KW |
1380 | BlockDriverState *bs = blk_bs(blk); |
1381 | ||
1382 | if (bs) { | |
1383 | bdrv_add_aio_context_notifier(bs, attached_aio_context, | |
a46fc9c9 HR |
1384 | detach_aio_context, opaque); |
1385 | } | |
2019ba0a HR |
1386 | } |
1387 | ||
1388 | void blk_remove_aio_context_notifier(BlockBackend *blk, | |
1389 | void (*attached_aio_context)(AioContext *, | |
1390 | void *), | |
1391 | void (*detach_aio_context)(void *), | |
1392 | void *opaque) | |
1393 | { | |
f21d96d0 KW |
1394 | BlockDriverState *bs = blk_bs(blk); |
1395 | ||
1396 | if (bs) { | |
1397 | bdrv_remove_aio_context_notifier(bs, attached_aio_context, | |
a46fc9c9 HR |
1398 | detach_aio_context, opaque); |
1399 | } | |
2019ba0a HR |
1400 | } |
1401 | ||
3301f6c6 HR |
1402 | void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify) |
1403 | { | |
1404 | notifier_list_add(&blk->remove_bs_notifiers, notify); | |
1405 | } | |
1406 | ||
1407 | void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify) | |
1408 | { | |
1409 | notifier_list_add(&blk->insert_bs_notifiers, notify); | |
1410 | } | |
1411 | ||
4be74634 MA |
1412 | void blk_io_plug(BlockBackend *blk) |
1413 | { | |
f21d96d0 KW |
1414 | BlockDriverState *bs = blk_bs(blk); |
1415 | ||
1416 | if (bs) { | |
1417 | bdrv_io_plug(bs); | |
a46fc9c9 | 1418 | } |
4be74634 MA |
1419 | } |
1420 | ||
1421 | void blk_io_unplug(BlockBackend *blk) | |
1422 | { | |
f21d96d0 KW |
1423 | BlockDriverState *bs = blk_bs(blk); |
1424 | ||
1425 | if (bs) { | |
1426 | bdrv_io_unplug(bs); | |
a46fc9c9 | 1427 | } |
4be74634 MA |
1428 | } |
1429 | ||
1430 | BlockAcctStats *blk_get_stats(BlockBackend *blk) | |
1431 | { | |
7f0e9da6 | 1432 | return &blk->stats; |
4be74634 MA |
1433 | } |
1434 | ||
1435 | void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk, | |
1436 | BlockCompletionFunc *cb, void *opaque) | |
1437 | { | |
1438 | return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque); | |
1439 | } | |
1ef01253 | 1440 | |
983a1600 EB |
1441 | int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t offset, |
1442 | int count, BdrvRequestFlags flags) | |
1ef01253 | 1443 | { |
983a1600 | 1444 | return blk_co_pwritev(blk, offset, count, NULL, |
16aaf975 | 1445 | flags | BDRV_REQ_ZERO_WRITE); |
1ef01253 HR |
1446 | } |
1447 | ||
1448 | int blk_write_compressed(BlockBackend *blk, int64_t sector_num, | |
1449 | const uint8_t *buf, int nb_sectors) | |
1450 | { | |
e7f7d676 HR |
1451 | int ret = blk_check_request(blk, sector_num, nb_sectors); |
1452 | if (ret < 0) { | |
1453 | return ret; | |
1454 | } | |
1455 | ||
f21d96d0 | 1456 | return bdrv_write_compressed(blk_bs(blk), sector_num, buf, nb_sectors); |
1ef01253 HR |
1457 | } |
1458 | ||
1459 | int blk_truncate(BlockBackend *blk, int64_t offset) | |
1460 | { | |
c09ba36c HR |
1461 | if (!blk_is_available(blk)) { |
1462 | return -ENOMEDIUM; | |
1463 | } | |
1464 | ||
f21d96d0 | 1465 | return bdrv_truncate(blk_bs(blk), offset); |
1ef01253 HR |
1466 | } |
1467 | ||
1468 | int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) | |
1469 | { | |
e7f7d676 HR |
1470 | int ret = blk_check_request(blk, sector_num, nb_sectors); |
1471 | if (ret < 0) { | |
1472 | return ret; | |
1473 | } | |
1474 | ||
f21d96d0 | 1475 | return bdrv_discard(blk_bs(blk), sector_num, nb_sectors); |
1ef01253 HR |
1476 | } |
1477 | ||
1478 | int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, | |
1479 | int64_t pos, int size) | |
1480 | { | |
bfd18d1e KW |
1481 | int ret; |
1482 | ||
c09ba36c HR |
1483 | if (!blk_is_available(blk)) { |
1484 | return -ENOMEDIUM; | |
1485 | } | |
1486 | ||
bfd18d1e KW |
1487 | ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size); |
1488 | if (ret < 0) { | |
1489 | return ret; | |
1490 | } | |
1491 | ||
1492 | if (ret == size && !blk->enable_write_cache) { | |
1493 | ret = bdrv_flush(blk_bs(blk)); | |
1494 | } | |
1495 | ||
1496 | return ret < 0 ? ret : size; | |
1ef01253 HR |
1497 | } |
1498 | ||
1499 | int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size) | |
1500 | { | |
c09ba36c HR |
1501 | if (!blk_is_available(blk)) { |
1502 | return -ENOMEDIUM; | |
1503 | } | |
1504 | ||
f21d96d0 | 1505 | return bdrv_load_vmstate(blk_bs(blk), buf, pos, size); |
1ef01253 | 1506 | } |
f0272c4d ET |
1507 | |
1508 | int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz) | |
1509 | { | |
c09ba36c HR |
1510 | if (!blk_is_available(blk)) { |
1511 | return -ENOMEDIUM; | |
1512 | } | |
1513 | ||
f21d96d0 | 1514 | return bdrv_probe_blocksizes(blk_bs(blk), bsz); |
f0272c4d ET |
1515 | } |
1516 | ||
1517 | int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo) | |
1518 | { | |
c09ba36c HR |
1519 | if (!blk_is_available(blk)) { |
1520 | return -ENOMEDIUM; | |
1521 | } | |
1522 | ||
f21d96d0 | 1523 | return bdrv_probe_geometry(blk_bs(blk), geo); |
f0272c4d | 1524 | } |
281d22d8 HR |
1525 | |
1526 | /* | |
1527 | * Updates the BlockBackendRootState object with data from the currently | |
1528 | * attached BlockDriverState. | |
1529 | */ | |
1530 | void blk_update_root_state(BlockBackend *blk) | |
1531 | { | |
f21d96d0 | 1532 | assert(blk->root); |
281d22d8 | 1533 | |
f21d96d0 KW |
1534 | blk->root_state.open_flags = blk->root->bs->open_flags; |
1535 | blk->root_state.read_only = blk->root->bs->read_only; | |
1536 | blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes; | |
281d22d8 HR |
1537 | |
1538 | if (blk->root_state.throttle_group) { | |
1539 | g_free(blk->root_state.throttle_group); | |
1540 | throttle_group_unref(blk->root_state.throttle_state); | |
1541 | } | |
27ccdd52 | 1542 | if (blk->public.throttle_state) { |
49d2165d | 1543 | const char *name = throttle_group_get_name(blk); |
281d22d8 HR |
1544 | blk->root_state.throttle_group = g_strdup(name); |
1545 | blk->root_state.throttle_state = throttle_group_incref(name); | |
1546 | } else { | |
1547 | blk->root_state.throttle_group = NULL; | |
1548 | blk->root_state.throttle_state = NULL; | |
1549 | } | |
1550 | } | |
1551 | ||
38cb18f5 HR |
1552 | /* |
1553 | * Applies the information in the root state to the given BlockDriverState. This | |
1554 | * does not include the flags which have to be specified for bdrv_open(), use | |
1555 | * blk_get_open_flags_from_root_state() to inquire them. | |
1556 | */ | |
1557 | void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs) | |
1558 | { | |
1559 | bs->detect_zeroes = blk->root_state.detect_zeroes; | |
1560 | if (blk->root_state.throttle_group) { | |
97148076 | 1561 | blk_io_limits_enable(blk, blk->root_state.throttle_group); |
38cb18f5 HR |
1562 | } |
1563 | } | |
1564 | ||
1565 | /* | |
1566 | * Returns the flags to be used for bdrv_open() of a BlockDriverState which is | |
1567 | * supposed to inherit the root state. | |
1568 | */ | |
1569 | int blk_get_open_flags_from_root_state(BlockBackend *blk) | |
1570 | { | |
1571 | int bs_flags; | |
1572 | ||
1573 | bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR; | |
1574 | bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR; | |
1575 | ||
1576 | return bs_flags; | |
1577 | } | |
1578 | ||
281d22d8 HR |
1579 | BlockBackendRootState *blk_get_root_state(BlockBackend *blk) |
1580 | { | |
1581 | return &blk->root_state; | |
1582 | } | |
1393f212 HR |
1583 | |
1584 | int blk_commit_all(void) | |
1585 | { | |
fe1a9cbc HR |
1586 | BlockBackend *blk = NULL; |
1587 | ||
1588 | while ((blk = blk_all_next(blk)) != NULL) { | |
1589 | AioContext *aio_context = blk_get_aio_context(blk); | |
1590 | ||
1591 | aio_context_acquire(aio_context); | |
f21d96d0 KW |
1592 | if (blk_is_inserted(blk) && blk->root->bs->backing) { |
1593 | int ret = bdrv_commit(blk->root->bs); | |
fe1a9cbc HR |
1594 | if (ret < 0) { |
1595 | aio_context_release(aio_context); | |
1596 | return ret; | |
1597 | } | |
1598 | } | |
1599 | aio_context_release(aio_context); | |
1600 | } | |
1601 | return 0; | |
1602 | } | |
1603 | ||
1604 | int blk_flush_all(void) | |
1605 | { | |
1606 | BlockBackend *blk = NULL; | |
1607 | int result = 0; | |
1608 | ||
1609 | while ((blk = blk_all_next(blk)) != NULL) { | |
1610 | AioContext *aio_context = blk_get_aio_context(blk); | |
1611 | int ret; | |
1612 | ||
1613 | aio_context_acquire(aio_context); | |
1614 | if (blk_is_inserted(blk)) { | |
1615 | ret = blk_flush(blk); | |
1616 | if (ret < 0 && !result) { | |
1617 | result = ret; | |
1618 | } | |
1619 | } | |
1620 | aio_context_release(aio_context); | |
1621 | } | |
1622 | ||
1623 | return result; | |
1393f212 | 1624 | } |
97148076 KW |
1625 | |
1626 | ||
1627 | /* throttling disk I/O limits */ | |
1628 | void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg) | |
1629 | { | |
1630 | throttle_group_config(blk, cfg); | |
1631 | } | |
1632 | ||
1633 | void blk_io_limits_disable(BlockBackend *blk) | |
1634 | { | |
1635 | assert(blk->public.throttle_state); | |
1636 | bdrv_no_throttling_begin(blk_bs(blk)); | |
1637 | throttle_group_unregister_blk(blk); | |
1638 | bdrv_no_throttling_end(blk_bs(blk)); | |
1639 | } | |
1640 | ||
1641 | /* should be called before blk_set_io_limits if a limit is set */ | |
1642 | void blk_io_limits_enable(BlockBackend *blk, const char *group) | |
1643 | { | |
1644 | assert(!blk->public.throttle_state); | |
1645 | throttle_group_register_blk(blk, group); | |
1646 | } | |
1647 | ||
1648 | void blk_io_limits_update_group(BlockBackend *blk, const char *group) | |
1649 | { | |
1650 | /* this BB is not part of any group */ | |
1651 | if (!blk->public.throttle_state) { | |
1652 | return; | |
1653 | } | |
1654 | ||
1655 | /* this BB is a part of the same group than the one we want */ | |
1656 | if (!g_strcmp0(throttle_group_get_name(blk), group)) { | |
1657 | return; | |
1658 | } | |
1659 | ||
1660 | /* need to change the group this bs belong to */ | |
1661 | blk_io_limits_disable(blk); | |
1662 | blk_io_limits_enable(blk, group); | |
1663 | } |