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