]> Git Repo - qemu.git/blame - include/block/block_int.h
migration: Export rdma.c functions in its own file
[qemu.git] / include / block / block_int.h
CommitLineData
ea2384d3
FB
1/*
2 * QEMU System Emulator block driver
5fafdf24 3 *
ea2384d3 4 * Copyright (c) 2003 Fabrice Bellard
5fafdf24 5 *
ea2384d3
FB
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_INT_H
25#define BLOCK_INT_H
26
5e5a94b6 27#include "block/accounting.h"
737e150e 28#include "block/block.h"
1de7afc9
PB
29#include "qemu/option.h"
30#include "qemu/queue.h"
10817bf0 31#include "qemu/coroutine.h"
1de7afc9 32#include "qemu/timer.h"
b2023818 33#include "qapi-types.h"
8f0720ec 34#include "qemu/hbitmap.h"
f364ec65 35#include "block/snapshot.h"
6a1751b7 36#include "qemu/main-loop.h"
cc0681c4 37#include "qemu/throttle.h"
faf07963 38
bfe8043e 39#define BLOCK_FLAG_ENCRYPT 1
bfe8043e 40#define BLOCK_FLAG_LAZY_REFCOUNTS 8
ec36ba14 41
bfe8043e
SH
42#define BLOCK_OPT_SIZE "size"
43#define BLOCK_OPT_ENCRYPT "encryption"
44#define BLOCK_OPT_COMPAT6 "compat6"
f249924e 45#define BLOCK_OPT_HWVERSION "hwversion"
bfe8043e
SH
46#define BLOCK_OPT_BACKING_FILE "backing_file"
47#define BLOCK_OPT_BACKING_FMT "backing_fmt"
48#define BLOCK_OPT_CLUSTER_SIZE "cluster_size"
49#define BLOCK_OPT_TABLE_SIZE "table_size"
50#define BLOCK_OPT_PREALLOC "preallocation"
51#define BLOCK_OPT_SUBFMT "subformat"
52#define BLOCK_OPT_COMPAT_LEVEL "compat"
53#define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts"
7f2039f6 54#define BLOCK_OPT_ADAPTER_TYPE "adapter_type"
b3af018f 55#define BLOCK_OPT_REDUNDANCY "redundancy"
4ab15590 56#define BLOCK_OPT_NOCOW "nocow"
876eb1b0 57#define BLOCK_OPT_OBJECT_SIZE "object_size"
06d05fa7 58#define BLOCK_OPT_REFCOUNT_BITS "refcount_bits"
0e7e1989 59
7cddd372
KW
60#define BLOCK_PROBE_BUF_SIZE 512
61
ebde595c
FZ
62enum BdrvTrackedRequestType {
63 BDRV_TRACKED_READ,
64 BDRV_TRACKED_WRITE,
ebde595c
FZ
65 BDRV_TRACKED_DISCARD,
66};
67
d616b224
SH
68typedef struct BdrvTrackedRequest {
69 BlockDriverState *bs;
793ed47a
KW
70 int64_t offset;
71 unsigned int bytes;
ebde595c 72 enum BdrvTrackedRequestType type;
7327145f 73
2dbafdc0 74 bool serialising;
7327145f
KW
75 int64_t overlap_offset;
76 unsigned int overlap_bytes;
77
d616b224
SH
78 QLIST_ENTRY(BdrvTrackedRequest) list;
79 Coroutine *co; /* owner, used for deadlock detection */
80 CoQueue wait_queue; /* coroutines blocked on this request */
6460440f
KW
81
82 struct BdrvTrackedRequest *waiting_for;
d616b224
SH
83} BdrvTrackedRequest;
84
ea2384d3
FB
85struct BlockDriver {
86 const char *format_name;
87 int instance_size;
f6186f49 88
b5042a36
BC
89 /* set to true if the BlockDriver is a block filter */
90 bool is_filter;
91 /* for snapshots block filter like Quorum can implement the
92 * following recursive callback.
212a5a8f
BC
93 * It's purpose is to recurse on the filter children while calling
94 * bdrv_recurse_is_first_non_filter on them.
95 * For a sample implementation look in the future Quorum block filter.
f6186f49 96 */
212a5a8f
BC
97 bool (*bdrv_recurse_is_first_non_filter)(BlockDriverState *bs,
98 BlockDriverState *candidate);
f6186f49 99
ea2384d3 100 int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
508c7cb3 101 int (*bdrv_probe_device)(const char *filename);
c2ad1b0c
KW
102
103 /* Any driver implementing this callback is expected to be able to handle
104 * NULL file names in its .bdrv_open() implementation */
6963a30d 105 void (*bdrv_parse_filename)(const char *filename, QDict *options, Error **errp);
030be321
BC
106 /* Drivers not implementing bdrv_parse_filename nor bdrv_open should have
107 * this field set to true, except ones that are defined only by their
108 * child's bs.
109 * An example of the last type will be the quorum block driver.
110 */
111 bool bdrv_needs_filename;
e971aa12 112
8ee79e70
KW
113 /* Set if a driver can support backing files */
114 bool supports_backing;
115
e971aa12
JC
116 /* For handling image reopen for split or non-split files */
117 int (*bdrv_reopen_prepare)(BDRVReopenState *reopen_state,
118 BlockReopenQueue *queue, Error **errp);
119 void (*bdrv_reopen_commit)(BDRVReopenState *reopen_state);
120 void (*bdrv_reopen_abort)(BDRVReopenState *reopen_state);
5365f44d 121 void (*bdrv_join_options)(QDict *options, QDict *old_options);
e971aa12 122
015a1036
HR
123 int (*bdrv_open)(BlockDriverState *bs, QDict *options, int flags,
124 Error **errp);
125 int (*bdrv_file_open)(BlockDriverState *bs, QDict *options, int flags,
126 Error **errp);
e2731add 127 void (*bdrv_close)(BlockDriverState *bs);
c282e1fd 128 int (*bdrv_create)(const char *filename, QemuOpts *opts, Error **errp);
ea2384d3 129 int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
95389c86 130 int (*bdrv_make_empty)(BlockDriverState *bs);
91af7014 131
4cdd01d3 132 void (*bdrv_refresh_filename)(BlockDriverState *bs, QDict *options);
91af7014 133
83f64091 134 /* aio */
7c84b1b8 135 BlockAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
f141eafe 136 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 137 BlockCompletionFunc *cb, void *opaque);
7c84b1b8 138 BlockAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
f141eafe 139 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 140 BlockCompletionFunc *cb, void *opaque);
7c84b1b8 141 BlockAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
097310b5 142 BlockCompletionFunc *cb, void *opaque);
4da444a0
EB
143 BlockAIOCB *(*bdrv_aio_pdiscard)(BlockDriverState *bs,
144 int64_t offset, int count,
097310b5 145 BlockCompletionFunc *cb, void *opaque);
83f64091 146
da1fa91d
KW
147 int coroutine_fn (*bdrv_co_readv)(BlockDriverState *bs,
148 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
3fb06697
KW
149 int coroutine_fn (*bdrv_co_preadv)(BlockDriverState *bs,
150 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags);
da1fa91d
KW
151 int coroutine_fn (*bdrv_co_writev)(BlockDriverState *bs,
152 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
93f5e6d8
KW
153 int coroutine_fn (*bdrv_co_writev_flags)(BlockDriverState *bs,
154 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int flags);
3fb06697
KW
155 int coroutine_fn (*bdrv_co_pwritev)(BlockDriverState *bs,
156 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags);
93f5e6d8 157
f08f2dda
SH
158 /*
159 * Efficiently zero a region of the disk image. Typically an image format
160 * would use a compact metadata representation to implement this. This
465fe887
EB
161 * function pointer may be NULL or return -ENOSUP and .bdrv_co_writev()
162 * will be called instead.
f08f2dda 163 */
d05aa8bb
EB
164 int coroutine_fn (*bdrv_co_pwrite_zeroes)(BlockDriverState *bs,
165 int64_t offset, int count, BdrvRequestFlags flags);
47a5486d
EB
166 int coroutine_fn (*bdrv_co_pdiscard)(BlockDriverState *bs,
167 int64_t offset, int count);
4c41cb49
EB
168
169 /*
170 * Building block for bdrv_block_status[_above]. The driver should
171 * answer only according to the current layer, and should not
172 * set BDRV_BLOCK_ALLOCATED, but may set BDRV_BLOCK_RAW. See block.h
173 * for the meaning of _DATA, _ZERO, and _OFFSET_VALID.
174 */
b6b8a333 175 int64_t coroutine_fn (*bdrv_co_get_block_status)(BlockDriverState *bs,
67a0fd2a
FZ
176 int64_t sector_num, int nb_sectors, int *pnum,
177 BlockDriverState **file);
da1fa91d 178
0f15423c
AL
179 /*
180 * Invalidate any cached meta-data.
181 */
5a8a30db 182 void (*bdrv_invalidate_cache)(BlockDriverState *bs, Error **errp);
76b1c7fe 183 int (*bdrv_inactivate)(BlockDriverState *bs);
0f15423c 184
c32b82af
PD
185 /*
186 * Flushes all data for all layers by calling bdrv_co_flush for underlying
187 * layers, if needed. This function is needed for deterministic
188 * synchronization of the flush finishing callback.
189 */
190 int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs);
191
c68b89ac
KW
192 /*
193 * Flushes all data that was already written to the OS all the way down to
c1bb86cd 194 * the disk (for example file-posix.c calls fsync()).
c68b89ac
KW
195 */
196 int coroutine_fn (*bdrv_co_flush_to_disk)(BlockDriverState *bs);
197
eb489bb1
KW
198 /*
199 * Flushes all internal caches to the OS. The data may still sit in a
200 * writeback cache of the host OS, but it will survive a crash of the qemu
201 * process.
202 */
203 int coroutine_fn (*bdrv_co_flush_to_os)(BlockDriverState *bs);
204
83f64091 205 const char *protocol_name;
4bff28b8 206 int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset, Error **errp);
b94a2610 207
83f64091 208 int64_t (*bdrv_getlength)(BlockDriverState *bs);
b94a2610 209 bool has_variable_length;
4a1d5e1f 210 int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs);
b94a2610 211
29a298af
PB
212 int coroutine_fn (*bdrv_co_pwritev_compressed)(BlockDriverState *bs,
213 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov);
214
5fafdf24 215 int (*bdrv_snapshot_create)(BlockDriverState *bs,
faea38e7 216 QEMUSnapshotInfo *sn_info);
5fafdf24 217 int (*bdrv_snapshot_goto)(BlockDriverState *bs,
faea38e7 218 const char *snapshot_id);
a89d89d3
WX
219 int (*bdrv_snapshot_delete)(BlockDriverState *bs,
220 const char *snapshot_id,
221 const char *name,
222 Error **errp);
5fafdf24 223 int (*bdrv_snapshot_list)(BlockDriverState *bs,
faea38e7 224 QEMUSnapshotInfo **psn_info);
51ef6727 225 int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
7b4c4781
WX
226 const char *snapshot_id,
227 const char *name,
228 Error **errp);
faea38e7 229 int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
eae041fe 230 ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
83f64091 231
1a8ae822
KW
232 int coroutine_fn (*bdrv_save_vmstate)(BlockDriverState *bs,
233 QEMUIOVector *qiov,
234 int64_t pos);
235 int coroutine_fn (*bdrv_load_vmstate)(BlockDriverState *bs,
236 QEMUIOVector *qiov,
237 int64_t pos);
178e08a5 238
756e6736
KW
239 int (*bdrv_change_backing_file)(BlockDriverState *bs,
240 const char *backing_file, const char *backing_fmt);
241
19cb3738 242 /* removable device specific */
e031f750 243 bool (*bdrv_is_inserted)(BlockDriverState *bs);
19cb3738 244 int (*bdrv_media_changed)(BlockDriverState *bs);
f36f3949 245 void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag);
025e849a 246 void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);
3b46e624 247
985a03b0 248 /* to control generic scsi devices */
7c84b1b8 249 BlockAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
221f715d 250 unsigned long int req, void *buf,
097310b5 251 BlockCompletionFunc *cb, void *opaque);
16a389dc
KW
252 int coroutine_fn (*bdrv_co_ioctl)(BlockDriverState *bs,
253 unsigned long int req, void *buf);
985a03b0 254
0e7e1989 255 /* List of options for creating images, terminated by name == NULL */
83d0521a 256 QemuOptsList *create_opts;
5eb45639 257
9ac228e0
KW
258 /*
259 * Returns 0 for completed check, -errno for internal errors.
260 * The check results are stored in result.
261 */
d4a7f45e 262 int (*bdrv_check)(BlockDriverState *bs, BdrvCheckResult *result,
4534ff54 263 BdrvCheckMode fix);
e97fc193 264
77485434 265 int (*bdrv_amend_options)(BlockDriverState *bs, QemuOpts *opts,
8b13976d
HR
266 BlockDriverAmendStatusCB *status_cb,
267 void *cb_opaque);
6f176b48 268
a31939e6 269 void (*bdrv_debug_event)(BlockDriverState *bs, BlkdebugEvent event);
8b9b0cc2 270
41c695c7
KW
271 /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */
272 int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event,
273 const char *tag);
4cc70e93
FZ
274 int (*bdrv_debug_remove_breakpoint)(BlockDriverState *bs,
275 const char *tag);
41c695c7
KW
276 int (*bdrv_debug_resume)(BlockDriverState *bs, const char *tag);
277 bool (*bdrv_debug_is_suspended)(BlockDriverState *bs, const char *tag);
278
3baca891 279 void (*bdrv_refresh_limits)(BlockDriverState *bs, Error **errp);
d34682cd 280
336c1c12
KW
281 /*
282 * Returns 1 if newly created images are guaranteed to contain only
283 * zeros, 0 otherwise.
284 */
285 int (*bdrv_has_zero_init)(BlockDriverState *bs);
12c09b8c 286
dcd04228
SH
287 /* Remove fd handlers, timers, and other event loop callbacks so the event
288 * loop is no longer in use. Called with no in-flight requests and in
289 * depth-first traversal order with parents before child nodes.
290 */
291 void (*bdrv_detach_aio_context)(BlockDriverState *bs);
292
293 /* Add fd handlers, timers, and other event loop callbacks so I/O requests
294 * can be processed again. Called with no in-flight requests and in
295 * depth-first traversal order with child nodes before parent nodes.
296 */
297 void (*bdrv_attach_aio_context)(BlockDriverState *bs,
298 AioContext *new_context);
299
448ad91d
ML
300 /* io queue for linux-aio */
301 void (*bdrv_io_plug)(BlockDriverState *bs);
302 void (*bdrv_io_unplug)(BlockDriverState *bs);
448ad91d 303
892b7de8
ET
304 /**
305 * Try to get @bs's logical and physical block size.
306 * On success, store them in @bsz and return zero.
307 * On failure, return negative errno.
308 */
309 int (*bdrv_probe_blocksizes)(BlockDriverState *bs, BlockSizes *bsz);
310 /**
311 * Try to get @bs's geometry (cyls, heads, sectors)
312 * On success, store them in @geo and return 0.
313 * On failure return -errno.
314 * Only drivers that want to override guest geometry implement this
315 * callback; see hd_geometry_guess().
316 */
317 int (*bdrv_probe_geometry)(BlockDriverState *bs, HDGeometry *geo);
318
67da1dc5
FZ
319 /**
320 * Drain and stop any internal sources of requests in the driver, and
321 * remain so until next I/O callback (e.g. bdrv_co_writev) is called.
322 */
323 void (*bdrv_drain)(BlockDriverState *bs);
324
e06018ad
WC
325 void (*bdrv_add_child)(BlockDriverState *parent, BlockDriverState *child,
326 Error **errp);
327 void (*bdrv_del_child)(BlockDriverState *parent, BdrvChild *child,
328 Error **errp);
329
33a610c3
KW
330 /**
331 * Informs the block driver that a permission change is intended. The
332 * driver checks whether the change is permissible and may take other
333 * preparations for the change (e.g. get file system locks). This operation
334 * is always followed either by a call to either .bdrv_set_perm or
335 * .bdrv_abort_perm_update.
336 *
337 * Checks whether the requested set of cumulative permissions in @perm
338 * can be granted for accessing @bs and whether no other users are using
339 * permissions other than those given in @shared (both arguments take
340 * BLK_PERM_* bitmasks).
341 *
342 * If both conditions are met, 0 is returned. Otherwise, -errno is returned
343 * and errp is set to an error describing the conflict.
344 */
345 int (*bdrv_check_perm)(BlockDriverState *bs, uint64_t perm,
346 uint64_t shared, Error **errp);
347
348 /**
349 * Called to inform the driver that the set of cumulative set of used
350 * permissions for @bs has changed to @perm, and the set of sharable
351 * permission to @shared. The driver can use this to propagate changes to
352 * its children (i.e. request permissions only if a parent actually needs
353 * them).
354 *
355 * This function is only invoked after bdrv_check_perm(), so block drivers
356 * may rely on preparations made in their .bdrv_check_perm implementation.
357 */
358 void (*bdrv_set_perm)(BlockDriverState *bs, uint64_t perm, uint64_t shared);
359
360 /*
361 * Called to inform the driver that after a previous bdrv_check_perm()
362 * call, the permission update is not performed and any preparations made
363 * for it (e.g. taken file locks) need to be undone.
364 *
365 * This function can be called even for nodes that never saw a
366 * bdrv_check_perm() call. It is a no-op then.
367 */
368 void (*bdrv_abort_perm_update)(BlockDriverState *bs);
369
370 /**
371 * Returns in @nperm and @nshared the permissions that the driver for @bs
372 * needs on its child @c, based on the cumulative permissions requested by
373 * the parents in @parent_perm and @parent_shared.
374 *
375 * If @c is NULL, return the permissions for attaching a new child for the
376 * given @role.
377 */
378 void (*bdrv_child_perm)(BlockDriverState *bs, BdrvChild *c,
379 const BdrvChildRole *role,
380 uint64_t parent_perm, uint64_t parent_shared,
381 uint64_t *nperm, uint64_t *nshared);
382
8a22f02a 383 QLIST_ENTRY(BlockDriver) list;
ea2384d3
FB
384};
385
fe81c2cc 386typedef struct BlockLimits {
a5b8dd2c
EB
387 /* Alignment requirement, in bytes, for offset/length of I/O
388 * requests. Must be a power of 2 less than INT_MAX; defaults to
389 * 1 for drivers with modern byte interfaces, and to 512
390 * otherwise. */
391 uint32_t request_alignment;
392
b8d0a980
EB
393 /* Maximum number of bytes that can be discarded at once (since it
394 * is signed, it must be < 2G, if set). Must be multiple of
b9f7855a
EB
395 * pdiscard_alignment, but need not be power of 2. May be 0 if no
396 * inherent 32-bit limit */
397 int32_t max_pdiscard;
398
b8d0a980
EB
399 /* Optimal alignment for discard requests in bytes. A power of 2
400 * is best but not mandatory. Must be a multiple of
401 * bl.request_alignment, and must be less than max_pdiscard if
402 * that is set. May be 0 if bl.request_alignment is good enough */
b9f7855a 403 uint32_t pdiscard_alignment;
fe81c2cc 404
b8d0a980
EB
405 /* Maximum number of bytes that can zeroized at once (since it is
406 * signed, it must be < 2G, if set). Must be multiple of
29cc6a68 407 * pwrite_zeroes_alignment. May be 0 if no inherent 32-bit limit */
cf081fca 408 int32_t max_pwrite_zeroes;
fe81c2cc 409
b8d0a980
EB
410 /* Optimal alignment for write zeroes requests in bytes. A power
411 * of 2 is best but not mandatory. Must be a multiple of
412 * bl.request_alignment, and must be less than max_pwrite_zeroes
413 * if that is set. May be 0 if bl.request_alignment is good
414 * enough */
cf081fca 415 uint32_t pwrite_zeroes_alignment;
7337acaf 416
b8d0a980
EB
417 /* Optimal transfer length in bytes. A power of 2 is best but not
418 * mandatory. Must be a multiple of bl.request_alignment, or 0 if
419 * no preferred size */
5def6b80
EB
420 uint32_t opt_transfer;
421
b8d0a980
EB
422 /* Maximal transfer length in bytes. Need not be power of 2, but
423 * must be multiple of opt_transfer and bl.request_alignment, or 0
424 * for no 32-bit limit. For now, anything larger than INT_MAX is
425 * clamped down. */
5def6b80 426 uint32_t max_transfer;
2647fab5 427
a5b8dd2c 428 /* memory alignment, in bytes so that no bounce buffer is needed */
4196d2f0
DL
429 size_t min_mem_alignment;
430
a5b8dd2c 431 /* memory alignment, in bytes, for bounce buffer */
339064d5 432 size_t opt_mem_alignment;
bd44feb7
SH
433
434 /* maximum number of iovec elements */
435 int max_iov;
fe81c2cc
PL
436} BlockLimits;
437
fbe40ff7
FZ
438typedef struct BdrvOpBlocker BdrvOpBlocker;
439
33384421
HR
440typedef struct BdrvAioNotifier {
441 void (*attached_aio_context)(AioContext *new_context, void *opaque);
442 void (*detach_aio_context)(void *opaque);
443
444 void *opaque;
e8a095da 445 bool deleted;
33384421
HR
446
447 QLIST_ENTRY(BdrvAioNotifier) list;
448} BdrvAioNotifier;
449
f3930ed0 450struct BdrvChildRole {
5fe31c25
KW
451 /* If true, bdrv_replace_node() doesn't change the node this BdrvChild
452 * points to. */
26de9438
KW
453 bool stay_at_node;
454
8e2160e2
KW
455 void (*inherit_options)(int *child_flags, QDict *child_options,
456 int parent_flags, QDict *parent_options);
c2066af0 457
5c8cab48
KW
458 void (*change_media)(BdrvChild *child, bool load);
459 void (*resize)(BdrvChild *child);
460
4c265bf9
KW
461 /* Returns a name that is supposedly more useful for human users than the
462 * node name for identifying the node in question (in particular, a BB
463 * name), or NULL if the parent can't provide a better name. */
d4a7f45e 464 const char *(*get_name)(BdrvChild *child);
4c265bf9 465
b5411555
KW
466 /* Returns a malloced string that describes the parent of the child for a
467 * human reader. This could be a node-name, BlockBackend name, qdev ID or
468 * QOM path of the device owning the BlockBackend, job type and ID etc. The
469 * caller is responsible for freeing the memory. */
d4a7f45e 470 char *(*get_parent_desc)(BdrvChild *child);
b5411555 471
c2066af0
KW
472 /*
473 * If this pair of functions is implemented, the parent doesn't issue new
474 * requests after returning from .drained_begin() until .drained_end() is
475 * called.
476 *
477 * Note that this can be nested. If drained_begin() was called twice, new
478 * I/O is allowed only after drained_end() was called twice, too.
479 */
480 void (*drained_begin)(BdrvChild *child);
481 void (*drained_end)(BdrvChild *child);
db95dbba 482
cfa1a572
KW
483 /* Notifies the parent that the child has been activated/inactivated (e.g.
484 * when migration is completing) and it can start/stop requesting
485 * permissions and doing I/O on it. */
4417ab7a 486 void (*activate)(BdrvChild *child, Error **errp);
cfa1a572 487 int (*inactivate)(BdrvChild *child);
4417ab7a 488
db95dbba
KW
489 void (*attach)(BdrvChild *child);
490 void (*detach)(BdrvChild *child);
f3930ed0
KW
491};
492
493extern const BdrvChildRole child_file;
494extern const BdrvChildRole child_format;
91ef3825 495extern const BdrvChildRole child_backing;
f3930ed0 496
b4b059f6 497struct BdrvChild {
6e93e7c4 498 BlockDriverState *bs;
260fecf1 499 char *name;
6e93e7c4 500 const BdrvChildRole *role;
22aa8b24 501 void *opaque;
d5e6f437
KW
502
503 /**
504 * Granted permissions for operating on this BdrvChild (BLK_PERM_* bitmask)
505 */
506 uint64_t perm;
507
508 /**
509 * Permissions that can still be granted to other users of @bs while this
510 * BdrvChild is still attached to it. (BLK_PERM_* bitmask)
511 */
512 uint64_t shared_perm;
513
6e93e7c4 514 QLIST_ENTRY(BdrvChild) next;
d42a8a93 515 QLIST_ENTRY(BdrvChild) next_parent;
b4b059f6 516};
6e93e7c4 517
8802d1fd
JC
518/*
519 * Note: the function bdrv_append() copies and swaps contents of
520 * BlockDriverStates, so if you add new fields to this struct, please
521 * inspect bdrv_append() to determine if the new fields need to be
522 * copied as well.
523 */
ea2384d3 524struct BlockDriverState {
91bcea48
PB
525 /* Protected by big QEMU lock or read-only after opening. No special
526 * locking needed during I/O...
527 */
4dca4b63 528 int open_flags; /* flags used to open the file, re-used for re-open */
54115412
EB
529 bool read_only; /* if true, the media is read only */
530 bool encrypted; /* if true, the media is encrypted */
531 bool valid_key; /* if true, a valid encryption key has been set */
532 bool sg; /* if true, the device is a /dev/sg* */
533 bool probed; /* if true, format was probed rather than specified */
5a9347c6 534 bool force_share; /* if true, always allow all shared permissions */
54115412 535
19cb3738 536 BlockDriver *drv; /* NULL means no media */
ea2384d3
FB
537 void *opaque;
538
dcd04228 539 AioContext *aio_context; /* event loop used for fd handlers, timers, etc */
33384421
HR
540 /* long-running tasks intended to always use the same AioContext as this
541 * BDS may register themselves in this list to be notified of changes
542 * regarding this BDS's context */
543 QLIST_HEAD(, BdrvAioNotifier) aio_notifiers;
e8a095da 544 bool walking_aio_notifiers; /* to make removal during iteration safe */
dcd04228 545
9a29e18f
JC
546 char filename[PATH_MAX];
547 char backing_file[PATH_MAX]; /* if non zero, the image is a diff of
548 this file image */
5eb45639 549 char backing_format[16]; /* if non-zero and backing_file exists */
19cb3738 550
91af7014 551 QDict *full_open_options;
9a29e18f 552 char exact_filename[PATH_MAX];
91af7014 553
760e0063 554 BdrvChild *backing;
9a4f4c31 555 BdrvChild *file;
66f82cee 556
fe81c2cc
PL
557 /* I/O Limits */
558 BlockLimits bl;
559
4df863f3
EB
560 /* Flags honored during pwrite (so far: BDRV_REQ_FUA) */
561 unsigned int supported_write_flags;
d05aa8bb 562 /* Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA,
465fe887
EB
563 * BDRV_REQ_MAY_UNMAP) */
564 unsigned int supported_zero_flags;
c25f53b0 565
dc364f4c
BC
566 /* the following member gives a name to every node on the bs graph. */
567 char node_name[32];
568 /* element of the list of named nodes building the graph */
569 QTAILQ_ENTRY(BlockDriverState) node_list;
2c1d04e0
HR
570 /* element of the list of all BlockDriverStates (all_bdrv_states) */
571 QTAILQ_ENTRY(BlockDriverState) bs_list;
9c4218e9
HR
572 /* element of the list of monitor-owned BDS */
573 QTAILQ_ENTRY(BlockDriverState) monitor_list;
9fcb0251 574 int refcnt;
dbffbdcf 575
fbe40ff7
FZ
576 /* operation blockers */
577 QLIST_HEAD(, BdrvOpBlocker) op_blockers[BLOCK_OP_TYPE_MAX];
578
eeec61f2
SH
579 /* long-running background operation */
580 BlockJob *job;
e971aa12 581
bddcec37
KW
582 /* The node that this node inherited default options from (and a reopen on
583 * which can affect this node by changing these defaults). This is always a
584 * parent node of this node. */
585 BlockDriverState *inherits_from;
6e93e7c4 586 QLIST_HEAD(, BdrvChild) children;
d42a8a93 587 QLIST_HEAD(, BdrvChild) parents;
6e93e7c4 588
de9c0cec 589 QDict *options;
145f598e 590 QDict *explicit_options;
465bee1d 591 BlockdevDetectZeroesOptions detect_zeroes;
826b6ca0
FZ
592
593 /* The error object in use for blocking operations on backing_hd */
594 Error *backing_blocker;
e2462113 595
91bcea48
PB
596 /* Protected by AioContext lock */
597
598 /* If true, copy read backing sectors into image. Can be >1 if more
599 * than one client has requested copy-on-read.
600 */
601 int copy_on_read;
602
603 /* If we are reading a disk image, give its size in sectors.
604 * Generally read-only; it is written to by load_vmstate and save_vmstate,
605 * but the block layer is quiescent during those.
606 */
607 int64_t total_sectors;
608
609 /* Callback before write request is processed */
610 NotifierWithReturnList before_write_notifiers;
611
612 /* number of in-flight requests; overall and serialising */
613 unsigned int in_flight;
614 unsigned int serialising_in_flight;
615
616 bool wakeup;
617
618 /* Offset after the highest byte written to */
619 uint64_t wr_highest_offset;
620
e2462113
FR
621 /* threshold limit for writes, in bytes. "High water mark". */
622 uint64_t write_threshold_offset;
623 NotifierWithReturn write_threshold_notifier;
51288d79 624
8f90b5e9 625 /* counter for nested bdrv_io_plug */
6b98bd64 626 unsigned io_plugged;
6b98bd64 627
91bcea48
PB
628 QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
629 CoQueue flush_queue; /* Serializing flush queue */
630 bool active_flush_req; /* Flush request in flight? */
631 unsigned int write_gen; /* Current data generation */
632 unsigned int flushed_gen; /* Flushed write generation */
633
634 QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps;
635
636 /* do we need to tell the quest if we have a volatile write cache? */
637 int enable_write_cache;
638
51288d79 639 int quiesce_counter;
ea2384d3
FB
640};
641
281d22d8
HR
642struct BlockBackendRootState {
643 int open_flags;
644 bool read_only;
645 BlockdevDetectZeroesOptions detect_zeroes;
281d22d8
HR
646};
647
274fccee
HR
648typedef enum BlockMirrorBackingMode {
649 /* Reuse the existing backing chain from the source for the target.
650 * - sync=full: Set backing BDS to NULL.
651 * - sync=top: Use source's backing BDS.
652 * - sync=none: Use source as the backing BDS. */
653 MIRROR_SOURCE_BACKING_CHAIN,
654
655 /* Open the target's backing chain completely anew */
656 MIRROR_OPEN_BACKING_CHAIN,
657
658 /* Do not change the target's backing BDS after job completion */
659 MIRROR_LEAVE_BACKING_CHAIN,
660} BlockMirrorBackingMode;
661
760e0063
KW
662static inline BlockDriverState *backing_bs(BlockDriverState *bs)
663{
664 return bs->backing ? bs->backing->bs : NULL;
665}
666
5f535a94
HR
667
668/* Essential block drivers which must always be statically linked into qemu, and
669 * which therefore can be accessed without using bdrv_find_format() */
670extern BlockDriver bdrv_file;
671extern BlockDriver bdrv_raw;
672extern BlockDriver bdrv_qcow2;
673
a03ef88f 674int coroutine_fn bdrv_co_preadv(BdrvChild *child,
1bf1cbc9
KW
675 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
676 BdrvRequestFlags flags);
a03ef88f 677int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
a8823a3b
KW
678 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
679 BdrvRequestFlags flags);
1bf1cbc9 680
eba25057 681int get_tmp_filename(char *filename, int size);
38f3ef57
KW
682BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
683 const char *filename);
95389c86 684
03c320d8
HR
685void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix,
686 QDict *options);
687
0563e191 688
d616b224
SH
689/**
690 * bdrv_add_before_write_notifier:
691 *
692 * Register a callback that is invoked before write requests are processed but
693 * after any throttling or waiting for overlapping requests.
694 */
695void bdrv_add_before_write_notifier(BlockDriverState *bs,
696 NotifierWithReturn *notifier);
697
dcd04228
SH
698/**
699 * bdrv_detach_aio_context:
700 *
701 * May be called from .bdrv_detach_aio_context() to detach children from the
702 * current #AioContext. This is only needed by block drivers that manage their
760e0063 703 * own children. Both ->file and ->backing are automatically handled and
dcd04228
SH
704 * block drivers should not call this function on them explicitly.
705 */
706void bdrv_detach_aio_context(BlockDriverState *bs);
707
708/**
709 * bdrv_attach_aio_context:
710 *
711 * May be called from .bdrv_attach_aio_context() to attach children to the new
712 * #AioContext. This is only needed by block drivers that manage their own
760e0063 713 * children. Both ->file and ->backing are automatically handled and block
dcd04228
SH
714 * drivers should not call this function on them explicitly.
715 */
716void bdrv_attach_aio_context(BlockDriverState *bs,
717 AioContext *new_context);
718
33384421
HR
719/**
720 * bdrv_add_aio_context_notifier:
721 *
722 * If a long-running job intends to be always run in the same AioContext as a
723 * certain BDS, it may use this function to be notified of changes regarding the
724 * association of the BDS to an AioContext.
725 *
726 * attached_aio_context() is called after the target BDS has been attached to a
727 * new AioContext; detach_aio_context() is called before the target BDS is being
728 * detached from its old AioContext.
729 */
730void bdrv_add_aio_context_notifier(BlockDriverState *bs,
731 void (*attached_aio_context)(AioContext *new_context, void *opaque),
732 void (*detach_aio_context)(void *opaque), void *opaque);
733
734/**
735 * bdrv_remove_aio_context_notifier:
736 *
737 * Unsubscribe of change notifications regarding the BDS's AioContext. The
738 * parameters given here have to be the same as those given to
739 * bdrv_add_aio_context_notifier().
740 */
741void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
742 void (*aio_context_attached)(AioContext *,
743 void *),
744 void (*aio_context_detached)(void *),
745 void *opaque);
746
c9d1a561
PB
747/**
748 * bdrv_wakeup:
749 * @bs: The BlockDriverState for which an I/O operation has been completed.
750 *
751 * Wake up the main thread if it is waiting on BDRV_POLL_WHILE. During
752 * synchronous I/O on a BlockDriverState that is attached to another
753 * I/O thread, the main thread lets the I/O thread's event loop run,
754 * waiting for the I/O operation to complete. A bdrv_wakeup will wake
755 * up the main thread if necessary.
756 *
757 * Manual calls to bdrv_wakeup are rarely necessary, because
758 * bdrv_dec_in_flight already calls it.
759 */
760void bdrv_wakeup(BlockDriverState *bs);
761
508c7cb3
CH
762#ifdef _WIN32
763int is_windows_drive(const char *filename);
764#endif
765
dc534f8f
PB
766/**
767 * stream_start:
2323322e
AG
768 * @job_id: The id of the newly-created job, or %NULL to use the
769 * device name of @bs.
dc534f8f
PB
770 * @bs: Block device to operate on.
771 * @base: Block device that will become the new base, or %NULL to
772 * flatten the whole backing file chain onto @bs.
29338003
AG
773 * @backing_file_str: The file name that will be written to @bs as the
774 * the new backing file if the job completes. Ignored if @base is %NULL.
c83c66c3 775 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
1d809098 776 * @on_error: The action to take upon error.
fd7f8c65 777 * @errp: Error object.
dc534f8f
PB
778 *
779 * Start a streaming operation on @bs. Clusters that are unallocated
780 * in @bs, but allocated in any image between @base and @bs (both
781 * exclusive) will be written to @bs. At the end of a successful
782 * streaming job, the backing file of @bs will be changed to
29338003
AG
783 * @backing_file_str in the written image and to @base in the live
784 * BlockDriverState.
dc534f8f 785 */
2323322e
AG
786void stream_start(const char *job_id, BlockDriverState *bs,
787 BlockDriverState *base, const char *backing_file_str,
8254b6d9 788 int64_t speed, BlockdevOnError on_error, Error **errp);
4f1043b4 789
747ff602
JC
790/**
791 * commit_start:
fd62c609
AG
792 * @job_id: The id of the newly-created job, or %NULL to use the
793 * device name of @bs.
03544a6e
FZ
794 * @bs: Active block device.
795 * @top: Top block device to be committed.
796 * @base: Block device that will be written into, and become the new top.
747ff602
JC
797 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
798 * @on_error: The action to take upon error.
54e26900 799 * @backing_file_str: String to use as the backing file in @top's overlay
0db832f4
KW
800 * @filter_node_name: The node name that should be assigned to the filter
801 * driver that the commit job inserts into the graph above @top. NULL means
802 * that a node name should be autogenerated.
747ff602
JC
803 * @errp: Error object.
804 *
805 */
fd62c609
AG
806void commit_start(const char *job_id, BlockDriverState *bs,
807 BlockDriverState *base, BlockDriverState *top, int64_t speed,
8254b6d9 808 BlockdevOnError on_error, const char *backing_file_str,
0db832f4 809 const char *filter_node_name, Error **errp);
03544a6e
FZ
810/**
811 * commit_active_start:
fd62c609
AG
812 * @job_id: The id of the newly-created job, or %NULL to use the
813 * device name of @bs.
03544a6e
FZ
814 * @bs: Active block device to be committed.
815 * @base: Block device that will be written into, and become the new top.
47970dfb
JS
816 * @creation_flags: Flags that control the behavior of the Job lifetime.
817 * See @BlockJobCreateFlags
03544a6e
FZ
818 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
819 * @on_error: The action to take upon error.
0db832f4
KW
820 * @filter_node_name: The node name that should be assigned to the filter
821 * driver that the commit job inserts into the graph above @bs. NULL means that
822 * a node name should be autogenerated.
03544a6e
FZ
823 * @cb: Completion function for the job.
824 * @opaque: Opaque pointer value passed to @cb.
b49f7ead 825 * @auto_complete: Auto complete the job.
78bbd910 826 * @errp: Error object.
03544a6e
FZ
827 *
828 */
fd62c609 829void commit_active_start(const char *job_id, BlockDriverState *bs,
47970dfb
JS
830 BlockDriverState *base, int creation_flags,
831 int64_t speed, BlockdevOnError on_error,
0db832f4 832 const char *filter_node_name,
78bbd910
FZ
833 BlockCompletionFunc *cb, void *opaque,
834 bool auto_complete, Error **errp);
893f7eba
PB
835/*
836 * mirror_start:
71aa9867
AG
837 * @job_id: The id of the newly-created job, or %NULL to use the
838 * device name of @bs.
893f7eba
PB
839 * @bs: Block device to operate on.
840 * @target: Block device to write to.
09158f00
BC
841 * @replaces: Block graph node name to replace once the mirror is done. Can
842 * only be used when full mirroring is selected.
893f7eba 843 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
eee13dfe 844 * @granularity: The chosen granularity for the dirty bitmap.
08e4ed6c 845 * @buf_size: The amount of data that can be in flight at one time.
893f7eba 846 * @mode: Whether to collapse all images in the chain to the target.
274fccee 847 * @backing_mode: How to establish the target's backing chain after completion.
b952b558
PB
848 * @on_source_error: The action to take upon error reading from the source.
849 * @on_target_error: The action to take upon error writing to the target.
0fc9f8ea 850 * @unmap: Whether to unmap target where source sectors only contain zeroes.
6cdbceb1
KW
851 * @filter_node_name: The node name that should be assigned to the filter
852 * driver that the mirror job inserts into the graph above @bs. NULL means that
853 * a node name should be autogenerated.
893f7eba
PB
854 * @errp: Error object.
855 *
856 * Start a mirroring operation on @bs. Clusters that are allocated
e7e4f9f9 857 * in @bs will be written to @target until the job is cancelled or
893f7eba
PB
858 * manually completed. At the end of a successful mirroring job,
859 * @bs will be switched to read from @target.
860 */
71aa9867
AG
861void mirror_start(const char *job_id, BlockDriverState *bs,
862 BlockDriverState *target, const char *replaces,
5fba6c0e 863 int64_t speed, uint32_t granularity, int64_t buf_size,
274fccee
HR
864 MirrorSyncMode mode, BlockMirrorBackingMode backing_mode,
865 BlockdevOnError on_source_error,
b952b558 866 BlockdevOnError on_target_error,
6cdbceb1 867 bool unmap, const char *filter_node_name, Error **errp);
893f7eba 868
98d2c6f2 869/*
111049a4 870 * backup_job_create:
70559d49
AG
871 * @job_id: The id of the newly-created job, or %NULL to use the
872 * device name of @bs.
98d2c6f2
DM
873 * @bs: Block device to operate on.
874 * @target: Block device to write to.
875 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
fc5d3f84 876 * @sync_mode: What parts of the disk image should be copied to the destination.
4b80ab2b 877 * @sync_bitmap: The dirty bitmap if sync_mode is MIRROR_SYNC_MODE_INCREMENTAL.
98d2c6f2
DM
878 * @on_source_error: The action to take upon error reading from the source.
879 * @on_target_error: The action to take upon error writing to the target.
47970dfb
JS
880 * @creation_flags: Flags that control the behavior of the Job lifetime.
881 * See @BlockJobCreateFlags
98d2c6f2
DM
882 * @cb: Completion function for the job.
883 * @opaque: Opaque pointer value passed to @cb.
78f51fde 884 * @txn: Transaction that this job is part of (may be NULL).
98d2c6f2 885 *
111049a4 886 * Create a backup operation on @bs. Clusters in @bs are written to @target
98d2c6f2
DM
887 * until the job is cancelled or manually completed.
888 */
111049a4
JS
889BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
890 BlockDriverState *target, int64_t speed,
891 MirrorSyncMode sync_mode,
892 BdrvDirtyBitmap *sync_bitmap,
893 bool compress,
894 BlockdevOnError on_source_error,
895 BlockdevOnError on_target_error,
896 int creation_flags,
897 BlockCompletionFunc *cb, void *opaque,
898 BlockJobTxn *txn, Error **errp);
98d2c6f2 899
abb21ac3
KW
900void hmp_drive_add_node(Monitor *mon, const char *optstr);
901
f21d96d0
KW
902BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
903 const char *child_name,
36fe1331 904 const BdrvChildRole *child_role,
d5e6f437
KW
905 uint64_t perm, uint64_t shared_perm,
906 void *opaque, Error **errp);
f21d96d0
KW
907void bdrv_root_unref_child(BdrvChild *child);
908
33a610c3
KW
909int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
910 Error **errp);
911
6a1b9ee1
KW
912/* Default implementation for BlockDriver.bdrv_child_perm() that can be used by
913 * block filters: Forward CONSISTENT_READ, WRITE, WRITE_UNCHANGED and RESIZE to
914 * all children */
915void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c,
916 const BdrvChildRole *role,
917 uint64_t perm, uint64_t shared,
918 uint64_t *nperm, uint64_t *nshared);
919
6b1a044a
KW
920/* Default implementation for BlockDriver.bdrv_child_perm() that can be used by
921 * (non-raw) image formats: Like above for bs->backing, but for bs->file it
922 * requires WRITE | RESIZE for read-write images, always requires
923 * CONSISTENT_READ and doesn't share WRITE. */
924void bdrv_format_default_perms(BlockDriverState *bs, BdrvChild *c,
925 const BdrvChildRole *role,
926 uint64_t perm, uint64_t shared,
927 uint64_t *nperm, uint64_t *nshared);
33a610c3 928
1f0c461b 929const char *bdrv_get_parent_name(const BlockDriverState *bs);
39829a01 930void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp);
a7f53e26 931bool blk_dev_has_removable_media(BlockBackend *blk);
8f3a73bc 932bool blk_dev_has_tray(BlockBackend *blk);
a7f53e26
MA
933void blk_dev_eject_request(BlockBackend *blk, bool force);
934bool blk_dev_is_tray_open(BlockBackend *blk);
935bool blk_dev_is_medium_locked(BlockBackend *blk);
a7f53e26 936
6d078599 937void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, int64_t nr_sect);
439db28c 938bool bdrv_requests_pending(BlockDriverState *bs);
e0c47b6c 939
df9a681d
FZ
940void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out);
941void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in);
942
99723548
PB
943void bdrv_inc_in_flight(BlockDriverState *bs);
944void bdrv_dec_in_flight(BlockDriverState *bs);
945
9c4218e9
HR
946void blockdev_close_all_bdrv_states(void);
947
ea2384d3 948#endif /* BLOCK_INT_H */
This page took 0.847359 seconds and 4 git commands to generate.