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