]> git.proxmox.com Git - mirror_qemu.git/blame - include/block/block-global-state.h
block: Mark bdrv_replace_node() GRAPH_WRLOCK
[mirror_qemu.git] / include / block / block-global-state.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_GLOBAL_STATE_H
25#define BLOCK_GLOBAL_STATE_H
26
e2c1c34f
MA
27#include "block/block-common.h"
28#include "qemu/coroutine.h"
29#include "qemu/transactions.h"
3b491a90
EGE
30
31/*
32 * Global state (GS) API. These functions run under the BQL.
33 *
34 * If a function modifies the graph, it also uses drain and/or
35 * aio_context_acquire/release to be sure it has unique access.
36 * aio_context locking is needed together with BQL because of
37 * the thread-safe I/O API that concurrently runs and accesses
38 * the graph without the BQL.
39 *
40 * It is important to note that not all of these functions are
41 * necessarily limited to running under the BQL, but they would
42 * require additional auditing and many small thread-safety changes
43 * to move them into the I/O API. Often it's not worth doing that
44 * work since the APIs are only used with the BQL held at the
45 * moment, so they have been placed in the GS API (for now).
46 *
47 * These functions can call any function from this and other categories
48 * (I/O, "I/O or GS", Common), but must be invoked only by other GS APIs.
49 *
50 * All functions in this header must use the macro
51 * GLOBAL_STATE_CODE();
52 * to catch when they are accidentally called without the BQL.
53 */
54
55void bdrv_init(void);
56BlockDriver *bdrv_find_protocol(const char *filename,
57 bool allow_protocol_prefix,
58 Error **errp);
59BlockDriver *bdrv_find_format(const char *format_name);
741443eb 60
4db7ba3b 61int coroutine_fn GRAPH_UNLOCKED
4ec8df01
KW
62bdrv_co_create(BlockDriver *drv, const char *filename, QemuOpts *opts,
63 Error **errp);
741443eb 64
4db7ba3b
KW
65int co_wrapper bdrv_create(BlockDriver *drv, const char *filename,
66 QemuOpts *opts, Error **errp);
4ec8df01 67
4db7ba3b 68int coroutine_fn GRAPH_UNLOCKED
4ec8df01 69bdrv_co_create_file(const char *filename, QemuOpts *opts, Error **errp);
3b491a90
EGE
70
71BlockDriverState *bdrv_new(void);
72int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
73 Error **errp);
ccd6a379
KW
74
75int GRAPH_WRLOCK
76bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, Error **errp);
77
3b491a90
EGE
78int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,
79 Error **errp);
80BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *node_options,
81 int flags, Error **errp);
82int bdrv_drop_filter(BlockDriverState *bs, Error **errp);
83
4bee90e9
KW
84BdrvChild * no_coroutine_fn
85bdrv_open_child(const char *filename, QDict *options, const char *bdref_key,
86 BlockDriverState *parent, const BdrvChildClass *child_class,
87 BdrvChildRole child_role, bool allow_none, Error **errp);
88
89BdrvChild * coroutine_fn no_co_wrapper
90bdrv_co_open_child(const char *filename, QDict *options, const char *bdref_key,
91 BlockDriverState *parent, const BdrvChildClass *child_class,
92 BdrvChildRole child_role, bool allow_none, Error **errp);
93
83930780
VSO
94int bdrv_open_file_child(const char *filename,
95 QDict *options, const char *bdref_key,
96 BlockDriverState *parent, Error **errp);
4bee90e9
KW
97
98BlockDriverState * no_coroutine_fn
99bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp);
100
101BlockDriverState * coroutine_fn no_co_wrapper
102bdrv_co_open_blockdev_ref(BlockdevRef *ref, Error **errp);
103
3b491a90
EGE
104int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
105 Error **errp);
d0f9fd94
KW
106int GRAPH_WRLOCK
107bdrv_set_backing_hd_drained(BlockDriverState *bs, BlockDriverState *backing_hd,
108 Error **errp);
109
3b491a90
EGE
110int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
111 const char *bdref_key, Error **errp);
4bee90e9
KW
112
113BlockDriverState * no_coroutine_fn
114bdrv_open(const char *filename, const char *reference, QDict *options,
115 int flags, Error **errp);
116
117BlockDriverState * coroutine_fn no_co_wrapper
118bdrv_co_open(const char *filename, const char *reference,
119 QDict *options, int flags, Error **errp);
120
3b491a90
EGE
121BlockDriverState *bdrv_new_open_driver_opts(BlockDriver *drv,
122 const char *node_name,
123 QDict *options, int flags,
124 Error **errp);
125BlockDriverState *bdrv_new_open_driver(BlockDriver *drv, const char *node_name,
126 int flags, Error **errp);
127BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
128 BlockDriverState *bs, QDict *options,
129 bool keep_old_opts);
130void bdrv_reopen_queue_free(BlockReopenQueue *bs_queue);
131int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp);
132int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,
133 Error **errp);
134int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
135 Error **errp);
136BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
137 const char *backing_file);
b7cfc7d5 138void GRAPH_RDLOCK bdrv_refresh_filename(BlockDriverState *bs);
e19b157f
KW
139
140void GRAPH_RDLOCK
141bdrv_refresh_limits(BlockDriverState *bs, Transaction *tran, Error **errp);
142
3b491a90 143int bdrv_commit(BlockDriverState *bs);
0bb79c97 144int GRAPH_RDLOCK bdrv_make_empty(BdrvChild *c, Error **errp);
3b491a90
EGE
145int bdrv_change_backing_file(BlockDriverState *bs, const char *backing_file,
146 const char *backing_fmt, bool warn);
147void bdrv_register(BlockDriver *bdrv);
148int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base,
149 const char *backing_file_str);
ad74751f
KW
150
151BlockDriverState * GRAPH_RDLOCK
152bdrv_find_overlay(BlockDriverState *active, BlockDriverState *bs);
153
154BlockDriverState * GRAPH_RDLOCK bdrv_find_base(BlockDriverState *bs);
9275fc72
KW
155
156int GRAPH_RDLOCK
157bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base,
158 Error **errp);
159void GRAPH_RDLOCK
160bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base);
3b491a90
EGE
161
162/*
163 * The units of offset and total_work_size may be chosen arbitrarily by the
164 * block driver; total_work_size may change during the course of the amendment
165 * operation
166 */
167typedef void BlockDriverAmendStatusCB(BlockDriverState *bs, int64_t offset,
168 int64_t total_work_size, void *opaque);
bd131d67
KW
169int GRAPH_RDLOCK
170bdrv_amend_options(BlockDriverState *bs_new, QemuOpts *opts,
171 BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
172 bool force, Error **errp);
3b491a90
EGE
173
174/* check if a named node can be replaced when doing drive-mirror */
533c6e4e
KW
175BlockDriverState * GRAPH_RDLOCK
176check_to_replace_node(BlockDriverState *parent_bs, const char *node_name,
177 Error **errp);
3b491a90 178
2b3912f1
KW
179int no_coroutine_fn GRAPH_RDLOCK
180bdrv_activate(BlockDriverState *bs, Error **errp);
da4afaff 181
2b3912f1 182int coroutine_fn no_co_wrapper_bdrv_rdlock
da4afaff
KW
183bdrv_co_activate(BlockDriverState *bs, Error **errp);
184
3b491a90
EGE
185void bdrv_activate_all(Error **errp);
186int bdrv_inactivate_all(void);
187
188int bdrv_flush_all(void);
189void bdrv_close_all(void);
190void bdrv_drain_all_begin(void);
da0bd744 191void bdrv_drain_all_begin_nopoll(void);
3b491a90
EGE
192void bdrv_drain_all_end(void);
193void bdrv_drain_all(void);
194
652b0dd8
SH
195void bdrv_aio_cancel(BlockAIOCB *acb);
196
3b491a90 197int bdrv_has_zero_init_1(BlockDriverState *bs);
06717986 198int coroutine_mixed_fn GRAPH_RDLOCK bdrv_has_zero_init(BlockDriverState *bs);
3b491a90
EGE
199BlockDriverState *bdrv_find_node(const char *node_name);
200BlockDeviceInfoList *bdrv_named_nodes_list(bool flat, Error **errp);
15f3f1fe 201XDbgBlockGraph * GRAPH_RDLOCK bdrv_get_xdbg_block_graph(Error **errp);
3b491a90
EGE
202BlockDriverState *bdrv_lookup_bs(const char *device,
203 const char *node_name,
204 Error **errp);
79bb7627
KW
205bool GRAPH_RDLOCK
206bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base);
207
3b491a90
EGE
208BlockDriverState *bdrv_next_node(BlockDriverState *bs);
209BlockDriverState *bdrv_next_all_states(BlockDriverState *bs);
210
211typedef struct BdrvNextIterator {
212 enum {
213 BDRV_NEXT_BACKEND_ROOTS,
214 BDRV_NEXT_MONITOR_OWNED,
215 } phase;
216 BlockBackend *blk;
217 BlockDriverState *bs;
218} BdrvNextIterator;
219
2b3912f1
KW
220BlockDriverState * GRAPH_RDLOCK bdrv_first(BdrvNextIterator *it);
221BlockDriverState * GRAPH_RDLOCK bdrv_next(BdrvNextIterator *it);
3b491a90
EGE
222void bdrv_next_cleanup(BdrvNextIterator *it);
223
224BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs);
225void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
226 void *opaque, bool read_only);
b7cfc7d5
KW
227
228char * GRAPH_RDLOCK
229bdrv_get_full_backing_filename(BlockDriverState *bs, Error **errp);
230
231char * GRAPH_RDLOCK bdrv_dirname(BlockDriverState *bs, Error **errp);
3b491a90
EGE
232
233void bdrv_img_create(const char *filename, const char *fmt,
234 const char *base_filename, const char *base_fmt,
235 char *options, uint64_t img_size, int flags,
236 bool quiet, Error **errp);
237
238void bdrv_ref(BlockDriverState *bs);
b2ab5f54
KW
239void no_coroutine_fn bdrv_unref(BlockDriverState *bs);
240void coroutine_fn no_co_wrapper bdrv_co_unref(BlockDriverState *bs);
ac2ae233 241void GRAPH_WRLOCK bdrv_schedule_unref(BlockDriverState *bs);
32a8aba3
KW
242
243void GRAPH_WRLOCK
244bdrv_unref_child(BlockDriverState *parent, BdrvChild *child);
245
246void coroutine_fn no_co_wrapper_bdrv_wrlock
247bdrv_co_unref_child(BlockDriverState *parent, BdrvChild *child);
afdaeb9e
KW
248
249BdrvChild * GRAPH_WRLOCK
250bdrv_attach_child(BlockDriverState *parent_bs,
251 BlockDriverState *child_bs,
252 const char *child_name,
253 const BdrvChildClass *child_class,
254 BdrvChildRole child_role,
255 Error **errp);
3b491a90 256
277f2007
KW
257bool GRAPH_RDLOCK
258bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp);
259
3b491a90
EGE
260void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason);
261void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason);
262void bdrv_op_block_all(BlockDriverState *bs, Error *reason);
263void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason);
264bool bdrv_op_blocker_is_empty(BlockDriverState *bs);
265
266int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
267 const char *tag);
268int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag);
269int bdrv_debug_resume(BlockDriverState *bs, const char *tag);
270bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag);
271
272/**
273 * Locks the AioContext of @bs if it's not the current AioContext. This avoids
274 * double locking which could lead to deadlocks: This is a coroutine_fn, so we
275 * know we already own the lock of the current AioContext.
276 *
277 * May only be called in the main thread.
278 */
279void coroutine_fn bdrv_co_lock(BlockDriverState *bs);
280
281/**
282 * Unlocks the AioContext of @bs if it's not the current AioContext.
283 */
284void coroutine_fn bdrv_co_unlock(BlockDriverState *bs);
285
7e8c182f 286bool bdrv_child_change_aio_context(BdrvChild *c, AioContext *ctx,
e08cc001 287 GHashTable *visited, Transaction *tran,
7e8c182f 288 Error **errp);
a41cfda1
EGE
289int bdrv_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
290 BdrvChild *ignore_child, Error **errp);
3b491a90 291
221caadc 292int GRAPH_RDLOCK bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz);
3b491a90
EGE
293int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo);
294
9def6082
KW
295void GRAPH_WRLOCK
296bdrv_add_child(BlockDriverState *parent, BlockDriverState *child, Error **errp);
297
298void GRAPH_WRLOCK
299bdrv_del_child(BlockDriverState *parent, BdrvChild *child, Error **errp);
3b491a90
EGE
300
301/**
302 *
303 * bdrv_register_buf/bdrv_unregister_buf:
304 *
305 * Register/unregister a buffer for I/O. For example, VFIO drivers are
306 * interested to know the memory areas that would later be used for I/O, so
307 * that they can prepare IOMMU mapping etc., to get better performance.
4f384011
SH
308 *
309 * Buffers must not overlap and they must be unregistered with the same <host,
310 * size> values that they were registered with.
f4ec04ba
SH
311 *
312 * Returns: true on success, false on failure
3b491a90 313 */
f4ec04ba
SH
314bool bdrv_register_buf(BlockDriverState *bs, void *host, size_t size,
315 Error **errp);
4f384011 316void bdrv_unregister_buf(BlockDriverState *bs, void *host, size_t size);
3b491a90
EGE
317
318void bdrv_cancel_in_flight(BlockDriverState *bs);
319
320#endif /* BLOCK_GLOBAL_STATE_H */