]> git.proxmox.com Git - mirror_qemu.git/blame - include/block/block-io.h
block: Fix deadlocks in bdrv_graph_wrunlock()
[mirror_qemu.git] / include / block / block-io.h
CommitLineData
3b491a90
EGE
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#ifndef BLOCK_IO_H
25#define BLOCK_IO_H
26
e2c1c34f
MA
27#include "block/aio-wait.h"
28#include "block/block-common.h"
29#include "qemu/coroutine.h"
30#include "qemu/iov.h"
3b491a90
EGE
31
32/*
33 * I/O API functions. These functions are thread-safe, and therefore
34 * can run in any thread as long as the thread has called
35 * aio_context_acquire/release().
36 *
37 * These functions can only call functions from I/O and Common categories,
38 * but can be invoked by GS, "I/O or GS" and I/O APIs.
39 *
40 * All functions in this category must use the macro
41 * IO_CODE();
42 * to catch when they are accidentally called by the wrong API.
43 */
44
90830f59
EGE
45int co_wrapper_mixed_bdrv_rdlock
46bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset, int64_t bytes,
47 BdrvRequestFlags flags);
48
3b491a90 49int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags);
90830f59
EGE
50
51int co_wrapper_mixed_bdrv_rdlock
52bdrv_pread(BdrvChild *child, int64_t offset, int64_t bytes, void *buf,
53 BdrvRequestFlags flags);
54
55int co_wrapper_mixed_bdrv_rdlock
56bdrv_pwrite(BdrvChild *child, int64_t offset,int64_t bytes,
57 const void *buf, BdrvRequestFlags flags);
58
59int co_wrapper_mixed_bdrv_rdlock
60bdrv_pwrite_sync(BdrvChild *child, int64_t offset, int64_t bytes,
61 const void *buf, BdrvRequestFlags flags);
62
b24a4c41
KW
63int coroutine_fn GRAPH_RDLOCK
64bdrv_co_pwrite_sync(BdrvChild *child, int64_t offset, int64_t bytes,
65 const void *buf, BdrvRequestFlags flags);
66
3b491a90
EGE
67/*
68 * Efficiently zero a region of the disk image. Note that this is a regular
69 * I/O request like read or write and should have a reasonable size. This
70 * function is not suitable for zeroing the entire image in a single request
71 * because it may allocate memory for the entire region.
72 */
abaf8b75
KW
73int coroutine_fn GRAPH_RDLOCK
74bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset, int64_t bytes,
75 BdrvRequestFlags flags);
3b491a90 76
c2b8e315
KW
77int coroutine_fn GRAPH_RDLOCK
78bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
79 PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
3b491a90 80
8ab8140a 81int64_t coroutine_fn GRAPH_RDLOCK bdrv_co_nb_sectors(BlockDriverState *bs);
81f730d4 82int64_t coroutine_mixed_fn bdrv_nb_sectors(BlockDriverState *bs);
c86422c5 83
8ab8140a
KW
84int64_t coroutine_fn GRAPH_RDLOCK bdrv_co_getlength(BlockDriverState *bs);
85int64_t co_wrapper_mixed_bdrv_rdlock bdrv_getlength(BlockDriverState *bs);
c86422c5 86
de335638
EGE
87int64_t coroutine_fn GRAPH_RDLOCK
88bdrv_co_get_allocated_file_size(BlockDriverState *bs);
89
90int64_t co_wrapper_bdrv_rdlock
91bdrv_get_allocated_file_size(BlockDriverState *bs);
82618d7b 92
3b491a90
EGE
93BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts,
94 BlockDriverState *in_bs, Error **errp);
d8fbf9aa 95
48aef794
KW
96int coroutine_fn GRAPH_RDLOCK
97bdrv_co_delete_file(BlockDriverState *bs, Error **errp);
98
99void coroutine_fn GRAPH_RDLOCK
100bdrv_co_delete_file_noerr(BlockDriverState *bs);
3b491a90
EGE
101
102
103/* async block I/O */
3b491a90
EGE
104void bdrv_aio_cancel_async(BlockAIOCB *acb);
105
106/* sg packet commands */
26c518ab
KW
107int coroutine_fn GRAPH_RDLOCK
108bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf);
3b491a90
EGE
109
110/* Ensure contents are flushed to disk. */
88095349 111int coroutine_fn GRAPH_RDLOCK bdrv_co_flush(BlockDriverState *bs);
3b491a90 112
9a5a1c62
EGE
113int coroutine_fn GRAPH_RDLOCK bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
114 int64_t bytes);
115
6d43eaa3
SL
116/* Report zone information of zone block device. */
117int coroutine_fn GRAPH_RDLOCK bdrv_co_zone_report(BlockDriverState *bs,
118 int64_t offset,
119 unsigned int *nr_zones,
120 BlockZoneDescriptor *zones);
121int coroutine_fn GRAPH_RDLOCK bdrv_co_zone_mgmt(BlockDriverState *bs,
122 BlockZoneOp op,
123 int64_t offset, int64_t len);
4751d09a
SL
124int coroutine_fn GRAPH_RDLOCK bdrv_co_zone_append(BlockDriverState *bs,
125 int64_t *offset,
126 QEMUIOVector *qiov,
127 BdrvRequestFlags flags);
6d43eaa3 128
3b491a90 129bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs);
1b88457e
PB
130
131int coroutine_fn GRAPH_RDLOCK
132bdrv_co_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes,
133 int64_t *pnum, int64_t *map, BlockDriverState **file);
134int co_wrapper_mixed_bdrv_rdlock
135bdrv_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes,
136 int64_t *pnum, int64_t *map, BlockDriverState **file);
7b52a921 137
7ff9579e
KW
138int coroutine_fn GRAPH_RDLOCK
139bdrv_co_block_status_above(BlockDriverState *bs, BlockDriverState *base,
140 int64_t offset, int64_t bytes, int64_t *pnum,
141 int64_t *map, BlockDriverState **file);
1b88457e
PB
142int co_wrapper_mixed_bdrv_rdlock
143bdrv_block_status_above(BlockDriverState *bs, BlockDriverState *base,
144 int64_t offset, int64_t bytes, int64_t *pnum,
145 int64_t *map, BlockDriverState **file);
7b52a921 146
7ff9579e
KW
147int coroutine_fn GRAPH_RDLOCK
148bdrv_co_is_allocated(BlockDriverState *bs, int64_t offset, int64_t bytes,
149 int64_t *pnum);
578ffa9f
PB
150int co_wrapper_mixed_bdrv_rdlock
151bdrv_is_allocated(BlockDriverState *bs, int64_t offset,
152 int64_t bytes, int64_t *pnum);
7b52a921 153
7ff9579e
KW
154int coroutine_fn GRAPH_RDLOCK
155bdrv_co_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
156 bool include_base, int64_t offset, int64_t bytes,
157 int64_t *pnum);
578ffa9f
PB
158int co_wrapper_mixed_bdrv_rdlock
159bdrv_is_allocated_above(BlockDriverState *bs, BlockDriverState *base,
160 bool include_base, int64_t offset,
161 int64_t bytes, int64_t *pnum);
7b52a921 162
abaf8b75
KW
163int coroutine_fn GRAPH_RDLOCK
164bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, int64_t bytes);
3b491a90 165
018f9dea
KW
166int GRAPH_RDLOCK
167bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg,
168 Error **errp);
169
3b491a90
EGE
170bool bdrv_is_read_only(BlockDriverState *bs);
171bool bdrv_is_writable(BlockDriverState *bs);
172bool bdrv_is_sg(BlockDriverState *bs);
15aee7ac 173int bdrv_get_flags(BlockDriverState *bs);
1e97be91 174
c73ff92c
EGE
175bool coroutine_fn GRAPH_RDLOCK bdrv_co_is_inserted(BlockDriverState *bs);
176bool co_wrapper_bdrv_rdlock bdrv_is_inserted(BlockDriverState *bs);
1e97be91 177
79a292e5
KW
178void coroutine_fn GRAPH_RDLOCK
179bdrv_co_lock_medium(BlockDriverState *bs, bool locked);
180
181void coroutine_fn GRAPH_RDLOCK
182bdrv_co_eject(BlockDriverState *bs, bool eject_flag);
2531b390 183
3b491a90
EGE
184const char *bdrv_get_format_name(BlockDriverState *bs);
185
f5a3a270 186bool GRAPH_RDLOCK bdrv_supports_compressed_writes(BlockDriverState *bs);
3b491a90 187const char *bdrv_get_node_name(const BlockDriverState *bs);
4026f1c4
KW
188
189const char * GRAPH_RDLOCK
190bdrv_get_device_name(const BlockDriverState *bs);
191
192const char * GRAPH_RDLOCK
193bdrv_get_device_or_node_name(const BlockDriverState *bs);
3d47eb0a 194
a00e70c0
EGE
195int coroutine_fn GRAPH_RDLOCK
196bdrv_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
197
198int co_wrapper_mixed_bdrv_rdlock
199bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
3d47eb0a 200
3574499a
KW
201ImageInfoSpecific * GRAPH_RDLOCK
202bdrv_get_specific_info(BlockDriverState *bs, Error **errp);
203
3b491a90 204BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs);
fc6b211f
AD
205void bdrv_round_to_subclusters(BlockDriverState *bs,
206 int64_t offset, int64_t bytes,
207 int64_t *cluster_offset,
208 int64_t *cluster_bytes);
3b491a90
EGE
209
210void bdrv_get_backing_filename(BlockDriverState *bs,
211 char *filename, int filename_size);
212
e2dd2737
KW
213int coroutine_fn GRAPH_RDLOCK
214bdrv_co_change_backing_file(BlockDriverState *bs, const char *backing_file,
215 const char *backing_fmt, bool warn);
216
217int co_wrapper_bdrv_rdlock
218bdrv_change_backing_file(BlockDriverState *bs, const char *backing_file,
219 const char *backing_fmt, bool warn);
220
3b491a90
EGE
221int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
222 int64_t pos, int size);
223
224int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
225 int64_t pos, int size);
226
227/*
228 * Returns the alignment in bytes that is required so that no bounce buffer
229 * is required throughout the stack
230 */
231size_t bdrv_min_mem_align(BlockDriverState *bs);
232/* Returns optimal alignment in bytes for bounce buffer */
233size_t bdrv_opt_mem_align(BlockDriverState *bs);
234void *qemu_blockalign(BlockDriverState *bs, size_t size);
235void *qemu_blockalign0(BlockDriverState *bs, size_t size);
236void *qemu_try_blockalign(BlockDriverState *bs, size_t size);
237void *qemu_try_blockalign0(BlockDriverState *bs, size_t size);
3b491a90
EGE
238
239void bdrv_enable_copy_on_read(BlockDriverState *bs);
240void bdrv_disable_copy_on_read(BlockDriverState *bs);
241
cb2bfaa4
EGE
242void coroutine_fn GRAPH_RDLOCK
243bdrv_co_debug_event(BlockDriverState *bs, BlkdebugEvent event);
244
245void co_wrapper_mixed_bdrv_rdlock
246bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event);
3b491a90 247
17362398
PB
248#define BLKDBG_CO_EVENT(child, evt) \
249 do { \
250 if (child) { \
251 bdrv_co_debug_event(child->bs, evt); \
252 } \
253 } while (0)
254
3b491a90
EGE
255#define BLKDBG_EVENT(child, evt) \
256 do { \
257 if (child) { \
258 bdrv_debug_event(child->bs, evt); \
259 } \
260 } while (0)
261
262/**
263 * bdrv_get_aio_context:
264 *
265 * Returns: the currently bound #AioContext
266 */
267AioContext *bdrv_get_aio_context(BlockDriverState *bs);
268
d5f8d79c
HR
269AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c);
270
3b491a90
EGE
271/**
272 * Move the current coroutine to the AioContext of @bs and return the old
273 * AioContext of the coroutine. Increase bs->in_flight so that draining @bs
274 * will wait for the operation to proceed until the corresponding
275 * bdrv_co_leave().
276 *
277 * Consequently, you can't call drain inside a bdrv_co_enter/leave() section as
278 * this will deadlock.
279 */
280AioContext *coroutine_fn bdrv_co_enter(BlockDriverState *bs);
281
282/**
283 * Ends a section started by bdrv_co_enter(). Move the current coroutine back
284 * to old_ctx and decrease bs->in_flight again.
285 */
286void coroutine_fn bdrv_co_leave(BlockDriverState *bs, AioContext *old_ctx);
287
3b491a90
EGE
288AioContext *child_of_bds_get_parent_aio_context(BdrvChild *c);
289
167f748d
KW
290bool coroutine_fn GRAPH_RDLOCK
291bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
292 uint32_t granularity, Error **errp);
293bool co_wrapper_bdrv_rdlock
294bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
295 uint32_t granularity, Error **errp);
3b491a90
EGE
296
297/**
298 *
299 * bdrv_co_copy_range:
300 *
301 * Do offloaded copy between two children. If the operation is not implemented
302 * by the driver, or if the backend storage doesn't support it, a negative
303 * error code will be returned.
304 *
305 * Note: block layer doesn't emulate or fallback to a bounce buffer approach
306 * because usually the caller shouldn't attempt offloaded copy any more (e.g.
307 * calling copy_file_range(2)) after the first error, thus it should fall back
308 * to a read+write path in the caller level.
309 *
310 * @src: Source child to copy data from
311 * @src_offset: offset in @src image to read data
312 * @dst: Destination child to copy data to
313 * @dst_offset: offset in @dst image to write data
314 * @bytes: number of bytes to copy
315 * @flags: request flags. Supported flags:
316 * BDRV_REQ_ZERO_WRITE - treat the @src range as zero data and do zero
317 * write on @dst as if bdrv_co_pwrite_zeroes is
318 * called. Used to simplify caller code, or
319 * during BlockDriver.bdrv_co_copy_range_from()
320 * recursion.
321 * BDRV_REQ_NO_SERIALISING - do not serialize with other overlapping
322 * requests currently in flight.
323 *
324 * Returns: 0 if succeeded; negative error code if failed.
325 **/
742bf09b
EGE
326int coroutine_fn GRAPH_RDLOCK
327bdrv_co_copy_range(BdrvChild *src, int64_t src_offset,
328 BdrvChild *dst, int64_t dst_offset,
329 int64_t bytes, BdrvRequestFlags read_flags,
330 BdrvRequestFlags write_flags);
3b491a90 331
3b491a90
EGE
332/*
333 * "I/O or GS" API functions. These functions can run without
334 * the BQL, but only in one specific iothread/main loop.
335 *
336 * More specifically, these functions use BDRV_POLL_WHILE(bs), which
337 * requires the caller to be either in the main thread and hold
338 * the BlockdriverState (bs) AioContext lock, or directly in the
339 * home thread that runs the bs AioContext. Calling them from
340 * another thread in another AioContext would cause deadlocks.
341 *
342 * Therefore, these functions are not proper I/O, because they
343 * can't run in *any* iothreads, but only in a specific one.
344 *
345 * These functions can call any function from I/O, Common and this
346 * categories, but must be invoked only by other "I/O or GS" and GS APIs.
347 *
348 * All functions in this category must use the macro
349 * IO_OR_GS_CODE();
350 * to catch when they are accidentally called by the wrong API.
351 */
352
353#define BDRV_POLL_WHILE(bs, cond) ({ \
354 BlockDriverState *bs_ = (bs); \
384a48fb 355 IO_OR_GS_CODE(); \
3b491a90
EGE
356 AIO_WAIT_WHILE(bdrv_get_aio_context(bs_), \
357 cond); })
358
359void bdrv_drain(BlockDriverState *bs);
3b491a90 360
90830f59 361int co_wrapper_mixed_bdrv_rdlock
3b491a90
EGE
362bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
363 PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
364
90830f59
EGE
365int co_wrapper_mixed_bdrv_rdlock
366bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix);
3b491a90
EGE
367
368/* Invalidate any cached metadata used by image formats */
90830f59
EGE
369int co_wrapper_mixed_bdrv_rdlock
370bdrv_invalidate_cache(BlockDriverState *bs, Error **errp);
371
372int co_wrapper_mixed_bdrv_rdlock bdrv_flush(BlockDriverState *bs);
373
374int co_wrapper_mixed_bdrv_rdlock
375bdrv_pdiscard(BdrvChild *child, int64_t offset, int64_t bytes);
376
377int co_wrapper_mixed_bdrv_rdlock
3b491a90 378bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
90830f59
EGE
379
380int co_wrapper_mixed_bdrv_rdlock
3b491a90
EGE
381bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
382
383/**
384 * bdrv_parent_drained_begin_single:
385 *
606ed756 386 * Begin a quiesced section for the parent of @c.
3b491a90 387 */
d05ab380 388void GRAPH_RDLOCK bdrv_parent_drained_begin_single(BdrvChild *c);
3b491a90 389
23987471
KW
390/**
391 * bdrv_parent_drained_poll_single:
392 *
393 * Returns true if there is any pending activity to cease before @c can be
394 * called quiesced, false otherwise.
395 */
d05ab380 396bool GRAPH_RDLOCK bdrv_parent_drained_poll_single(BdrvChild *c);
23987471 397
3b491a90
EGE
398/**
399 * bdrv_parent_drained_end_single:
400 *
401 * End a quiesced section for the parent of @c.
3b491a90 402 */
d05ab380 403void GRAPH_RDLOCK bdrv_parent_drained_end_single(BdrvChild *c);
3b491a90
EGE
404
405/**
406 * bdrv_drain_poll:
407 *
299403ae 408 * Poll for pending requests in @bs and its parents (except for @ignore_parent).
3b491a90
EGE
409 *
410 * If @ignore_bds_parents is true, parents that are BlockDriverStates must
411 * ignore the drain request because they will be drained separately (used for
412 * drain_all).
413 *
414 * This is part of bdrv_drained_begin.
415 */
d05ab380
EGE
416bool GRAPH_RDLOCK
417bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent,
418 bool ignore_bds_parents);
3b491a90
EGE
419
420/**
421 * bdrv_drained_begin:
422 *
423 * Begin a quiesced section for exclusive access to the BDS, by disabling
424 * external request sources including NBD server, block jobs, and device model.
425 *
d05ab380
EGE
426 * This function can only be invoked by the main loop or a coroutine
427 * (regardless of the AioContext where it is running).
428 * If the coroutine is running in an Iothread AioContext, this function will
429 * just schedule a BH to run in the main loop.
430 * However, it cannot be directly called by an Iothread.
431 *
3b491a90
EGE
432 * This function can be recursive.
433 */
434void bdrv_drained_begin(BlockDriverState *bs);
435
436/**
437 * bdrv_do_drained_begin_quiesce:
438 *
439 * Quiesces a BDS like bdrv_drained_begin(), but does not wait for already
440 * running requests to complete.
441 */
a82a3bd1 442void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent);
3b491a90 443
3b491a90
EGE
444/**
445 * bdrv_drained_end:
446 *
447 * End a quiescent section started by bdrv_drained_begin().
d05ab380
EGE
448 *
449 * This function can only be invoked by the main loop or a coroutine
450 * (regardless of the AioContext where it is running).
451 * If the coroutine is running in an Iothread AioContext, this function will
452 * just schedule a BH to run in the main loop.
453 * However, it cannot be directly called by an Iothread.
3b491a90
EGE
454 */
455void bdrv_drained_end(BlockDriverState *bs);
456
3b491a90 457#endif /* BLOCK_IO_H */