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