]>
Commit | Line | Data |
---|---|---|
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_INT_COMMON_H | |
25 | #define BLOCK_INT_COMMON_H | |
26 | ||
27 | #include "block/aio.h" | |
28 | #include "block/block-common.h" | |
29 | #include "block/block-global-state.h" | |
30 | #include "block/snapshot.h" | |
31 | #include "qemu/iov.h" | |
32 | #include "qemu/rcu.h" | |
33 | #include "qemu/stats64.h" | |
34 | ||
35 | #define BLOCK_FLAG_LAZY_REFCOUNTS 8 | |
36 | ||
37 | #define BLOCK_OPT_SIZE "size" | |
38 | #define BLOCK_OPT_ENCRYPT "encryption" | |
39 | #define BLOCK_OPT_ENCRYPT_FORMAT "encrypt.format" | |
40 | #define BLOCK_OPT_COMPAT6 "compat6" | |
41 | #define BLOCK_OPT_HWVERSION "hwversion" | |
42 | #define BLOCK_OPT_BACKING_FILE "backing_file" | |
43 | #define BLOCK_OPT_BACKING_FMT "backing_fmt" | |
44 | #define BLOCK_OPT_CLUSTER_SIZE "cluster_size" | |
45 | #define BLOCK_OPT_TABLE_SIZE "table_size" | |
46 | #define BLOCK_OPT_PREALLOC "preallocation" | |
47 | #define BLOCK_OPT_SUBFMT "subformat" | |
48 | #define BLOCK_OPT_COMPAT_LEVEL "compat" | |
49 | #define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts" | |
50 | #define BLOCK_OPT_ADAPTER_TYPE "adapter_type" | |
51 | #define BLOCK_OPT_REDUNDANCY "redundancy" | |
52 | #define BLOCK_OPT_NOCOW "nocow" | |
53 | #define BLOCK_OPT_EXTENT_SIZE_HINT "extent_size_hint" | |
54 | #define BLOCK_OPT_OBJECT_SIZE "object_size" | |
55 | #define BLOCK_OPT_REFCOUNT_BITS "refcount_bits" | |
56 | #define BLOCK_OPT_DATA_FILE "data_file" | |
57 | #define BLOCK_OPT_DATA_FILE_RAW "data_file_raw" | |
58 | #define BLOCK_OPT_COMPRESSION_TYPE "compression_type" | |
59 | #define BLOCK_OPT_EXTL2 "extended_l2" | |
60 | ||
61 | #define BLOCK_PROBE_BUF_SIZE 512 | |
62 | ||
63 | enum BdrvTrackedRequestType { | |
64 | BDRV_TRACKED_READ, | |
65 | BDRV_TRACKED_WRITE, | |
66 | BDRV_TRACKED_DISCARD, | |
67 | BDRV_TRACKED_TRUNCATE, | |
68 | }; | |
69 | ||
70 | /* | |
71 | * That is not quite good that BdrvTrackedRequest structure is public, | |
72 | * as block/io.c is very careful about incoming offset/bytes being | |
73 | * correct. Be sure to assert bdrv_check_request() succeeded after any | |
74 | * modification of BdrvTrackedRequest object out of block/io.c | |
75 | */ | |
76 | typedef struct BdrvTrackedRequest { | |
77 | BlockDriverState *bs; | |
78 | int64_t offset; | |
79 | int64_t bytes; | |
80 | enum BdrvTrackedRequestType type; | |
81 | ||
82 | bool serialising; | |
83 | int64_t overlap_offset; | |
84 | int64_t overlap_bytes; | |
85 | ||
86 | QLIST_ENTRY(BdrvTrackedRequest) list; | |
87 | Coroutine *co; /* owner, used for deadlock detection */ | |
88 | CoQueue wait_queue; /* coroutines blocked on this request */ | |
89 | ||
90 | struct BdrvTrackedRequest *waiting_for; | |
91 | } BdrvTrackedRequest; | |
92 | ||
93 | ||
94 | struct BlockDriver { | |
95 | /* | |
96 | * These fields are initialized when this object is created, | |
97 | * and are never changed afterwards. | |
98 | */ | |
99 | ||
100 | const char *format_name; | |
101 | int instance_size; | |
102 | ||
103 | /* | |
104 | * Set to true if the BlockDriver is a block filter. Block filters pass | |
105 | * certain callbacks that refer to data (see block.c) to their bs->file | |
106 | * or bs->backing (whichever one exists) if the driver doesn't implement | |
107 | * them. Drivers that do not wish to forward must implement them and return | |
108 | * -ENOTSUP. | |
109 | * Note that filters are not allowed to modify data. | |
110 | * | |
111 | * Filters generally cannot have more than a single filtered child, | |
112 | * because the data they present must at all times be the same as | |
113 | * that on their filtered child. That would be impossible to | |
114 | * achieve for multiple filtered children. | |
115 | * (And this filtered child must then be bs->file or bs->backing.) | |
116 | */ | |
117 | bool is_filter; | |
118 | /* | |
119 | * Only make sense for filter drivers, for others must be false. | |
120 | * If true, filtered child is bs->backing. Otherwise it's bs->file. | |
121 | * Two internal filters use bs->backing as filtered child and has this | |
122 | * field set to true: mirror_top and commit_top. There also two such test | |
123 | * filters in tests/unit/test-bdrv-graph-mod.c. | |
124 | * | |
125 | * Never create any more such filters! | |
126 | * | |
127 | * TODO: imagine how to deprecate this behavior and make all filters work | |
128 | * similarly using bs->file as filtered child. | |
129 | */ | |
130 | bool filtered_child_is_backing; | |
131 | ||
132 | /* | |
133 | * Set to true if the BlockDriver is a format driver. Format nodes | |
134 | * generally do not expect their children to be other format nodes | |
135 | * (except for backing files), and so format probing is disabled | |
136 | * on those children. | |
137 | */ | |
138 | bool is_format; | |
139 | ||
140 | /* | |
141 | * Drivers not implementing bdrv_parse_filename nor bdrv_open should have | |
142 | * this field set to true, except ones that are defined only by their | |
143 | * child's bs. | |
144 | * An example of the last type will be the quorum block driver. | |
145 | */ | |
146 | bool bdrv_needs_filename; | |
147 | ||
148 | /* | |
149 | * Set if a driver can support backing files. This also implies the | |
150 | * following semantics: | |
151 | * | |
152 | * - Return status 0 of .bdrv_co_block_status means that corresponding | |
153 | * blocks are not allocated in this layer of backing-chain | |
154 | * - For such (unallocated) blocks, read will: | |
155 | * - fill buffer with zeros if there is no backing file | |
156 | * - read from the backing file otherwise, where the block layer | |
157 | * takes care of reading zeros beyond EOF if backing file is short | |
158 | */ | |
159 | bool supports_backing; | |
160 | ||
161 | /* | |
162 | * Drivers setting this field must be able to work with just a plain | |
163 | * filename with '<protocol_name>:' as a prefix, and no other options. | |
164 | * Options may be extracted from the filename by implementing | |
165 | * bdrv_parse_filename. | |
166 | */ | |
167 | const char *protocol_name; | |
168 | ||
169 | /* List of options for creating images, terminated by name == NULL */ | |
170 | QemuOptsList *create_opts; | |
171 | ||
172 | /* List of options for image amend */ | |
173 | QemuOptsList *amend_opts; | |
174 | ||
175 | /* | |
176 | * If this driver supports reopening images this contains a | |
177 | * NULL-terminated list of the runtime options that can be | |
178 | * modified. If an option in this list is unspecified during | |
179 | * reopen then it _must_ be reset to its default value or return | |
180 | * an error. | |
181 | */ | |
182 | const char *const *mutable_opts; | |
183 | ||
184 | /* | |
185 | * Pointer to a NULL-terminated array of names of strong options | |
186 | * that can be specified for bdrv_open(). A strong option is one | |
187 | * that changes the data of a BDS. | |
188 | * If this pointer is NULL, the array is considered empty. | |
189 | * "filename" and "driver" are always considered strong. | |
190 | */ | |
191 | const char *const *strong_runtime_opts; | |
192 | ||
193 | ||
194 | /* | |
195 | * Global state (GS) API. These functions run under the BQL. | |
196 | * | |
197 | * See include/block/block-global-state.h for more information about | |
198 | * the GS API. | |
199 | */ | |
200 | ||
201 | /* | |
202 | * This function is invoked under BQL before .bdrv_co_amend() | |
203 | * (which in contrast does not necessarily run under the BQL) | |
204 | * to allow driver-specific initialization code that requires | |
205 | * the BQL, like setting up specific permission flags. | |
206 | */ | |
207 | int GRAPH_RDLOCK_PTR (*bdrv_amend_pre_run)( | |
208 | BlockDriverState *bs, Error **errp); | |
209 | /* | |
210 | * This function is invoked under BQL after .bdrv_co_amend() | |
211 | * to allow cleaning up what was done in .bdrv_amend_pre_run(). | |
212 | */ | |
213 | void GRAPH_RDLOCK_PTR (*bdrv_amend_clean)(BlockDriverState *bs); | |
214 | ||
215 | /* | |
216 | * Return true if @to_replace can be replaced by a BDS with the | |
217 | * same data as @bs without it affecting @bs's behavior (that is, | |
218 | * without it being visible to @bs's parents). | |
219 | */ | |
220 | bool GRAPH_RDLOCK_PTR (*bdrv_recurse_can_replace)( | |
221 | BlockDriverState *bs, BlockDriverState *to_replace); | |
222 | ||
223 | int (*bdrv_probe_device)(const char *filename); | |
224 | ||
225 | /* | |
226 | * Any driver implementing this callback is expected to be able to handle | |
227 | * NULL file names in its .bdrv_open() implementation. | |
228 | */ | |
229 | void (*bdrv_parse_filename)(const char *filename, QDict *options, | |
230 | Error **errp); | |
231 | ||
232 | /* For handling image reopen for split or non-split files. */ | |
233 | int (*bdrv_reopen_prepare)(BDRVReopenState *reopen_state, | |
234 | BlockReopenQueue *queue, Error **errp); | |
235 | void (*bdrv_reopen_commit)(BDRVReopenState *reopen_state); | |
236 | void (*bdrv_reopen_commit_post)(BDRVReopenState *reopen_state); | |
237 | void (*bdrv_reopen_abort)(BDRVReopenState *reopen_state); | |
238 | void (*bdrv_join_options)(QDict *options, QDict *old_options); | |
239 | ||
240 | int GRAPH_UNLOCKED_PTR (*bdrv_open)( | |
241 | BlockDriverState *bs, QDict *options, int flags, Error **errp); | |
242 | ||
243 | /* Protocol drivers should implement this instead of bdrv_open */ | |
244 | int GRAPH_UNLOCKED_PTR (*bdrv_file_open)( | |
245 | BlockDriverState *bs, QDict *options, int flags, Error **errp); | |
246 | void (*bdrv_close)(BlockDriverState *bs); | |
247 | ||
248 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_create)( | |
249 | BlockdevCreateOptions *opts, Error **errp); | |
250 | ||
251 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_create_opts)( | |
252 | BlockDriver *drv, const char *filename, QemuOpts *opts, Error **errp); | |
253 | ||
254 | int (*bdrv_amend_options)(BlockDriverState *bs, | |
255 | QemuOpts *opts, | |
256 | BlockDriverAmendStatusCB *status_cb, | |
257 | void *cb_opaque, | |
258 | bool force, | |
259 | Error **errp); | |
260 | ||
261 | int (*bdrv_make_empty)(BlockDriverState *bs); | |
262 | ||
263 | /* | |
264 | * Refreshes the bs->exact_filename field. If that is impossible, | |
265 | * bs->exact_filename has to be left empty. | |
266 | */ | |
267 | void (*bdrv_refresh_filename)(BlockDriverState *bs); | |
268 | ||
269 | /* | |
270 | * Gathers the open options for all children into @target. | |
271 | * A simple format driver (without backing file support) might | |
272 | * implement this function like this: | |
273 | * | |
274 | * QINCREF(bs->file->bs->full_open_options); | |
275 | * qdict_put(target, "file", bs->file->bs->full_open_options); | |
276 | * | |
277 | * If not specified, the generic implementation will simply put | |
278 | * all children's options under their respective name. | |
279 | * | |
280 | * @backing_overridden is true when bs->backing seems not to be | |
281 | * the child that would result from opening bs->backing_file. | |
282 | * Therefore, if it is true, the backing child's options should be | |
283 | * gathered; otherwise, there is no need since the backing child | |
284 | * is the one implied by the image header. | |
285 | * | |
286 | * Note that ideally this function would not be needed. Every | |
287 | * block driver which implements it is probably doing something | |
288 | * shady regarding its runtime option structure. | |
289 | */ | |
290 | void (*bdrv_gather_child_options)(BlockDriverState *bs, QDict *target, | |
291 | bool backing_overridden); | |
292 | ||
293 | /* | |
294 | * Returns an allocated string which is the directory name of this BDS: It | |
295 | * will be used to make relative filenames absolute by prepending this | |
296 | * function's return value to them. | |
297 | */ | |
298 | char *(*bdrv_dirname)(BlockDriverState *bs, Error **errp); | |
299 | ||
300 | /* | |
301 | * This informs the driver that we are no longer interested in the result | |
302 | * of in-flight requests, so don't waste the time if possible. | |
303 | * | |
304 | * One example usage is to avoid waiting for an nbd target node reconnect | |
305 | * timeout during job-cancel with force=true. | |
306 | */ | |
307 | void (*bdrv_cancel_in_flight)(BlockDriverState *bs); | |
308 | ||
309 | int (*bdrv_inactivate)(BlockDriverState *bs); | |
310 | ||
311 | int (*bdrv_snapshot_create)(BlockDriverState *bs, | |
312 | QEMUSnapshotInfo *sn_info); | |
313 | int (*bdrv_snapshot_goto)(BlockDriverState *bs, | |
314 | const char *snapshot_id); | |
315 | int (*bdrv_snapshot_delete)(BlockDriverState *bs, | |
316 | const char *snapshot_id, | |
317 | const char *name, | |
318 | Error **errp); | |
319 | int (*bdrv_snapshot_list)(BlockDriverState *bs, | |
320 | QEMUSnapshotInfo **psn_info); | |
321 | int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs, | |
322 | const char *snapshot_id, | |
323 | const char *name, | |
324 | Error **errp); | |
325 | ||
326 | int (*bdrv_change_backing_file)(BlockDriverState *bs, | |
327 | const char *backing_file, const char *backing_fmt); | |
328 | ||
329 | /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */ | |
330 | int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event, | |
331 | const char *tag); | |
332 | int (*bdrv_debug_remove_breakpoint)(BlockDriverState *bs, | |
333 | const char *tag); | |
334 | int (*bdrv_debug_resume)(BlockDriverState *bs, const char *tag); | |
335 | bool (*bdrv_debug_is_suspended)(BlockDriverState *bs, const char *tag); | |
336 | ||
337 | void GRAPH_RDLOCK_PTR (*bdrv_refresh_limits)( | |
338 | BlockDriverState *bs, Error **errp); | |
339 | ||
340 | /* | |
341 | * Returns 1 if newly created images are guaranteed to contain only | |
342 | * zeros, 0 otherwise. | |
343 | */ | |
344 | int (*bdrv_has_zero_init)(BlockDriverState *bs); | |
345 | ||
346 | /* | |
347 | * Remove fd handlers, timers, and other event loop callbacks so the event | |
348 | * loop is no longer in use. Called with no in-flight requests and in | |
349 | * depth-first traversal order with parents before child nodes. | |
350 | */ | |
351 | void (*bdrv_detach_aio_context)(BlockDriverState *bs); | |
352 | ||
353 | /* | |
354 | * Add fd handlers, timers, and other event loop callbacks so I/O requests | |
355 | * can be processed again. Called with no in-flight requests and in | |
356 | * depth-first traversal order with child nodes before parent nodes. | |
357 | */ | |
358 | void (*bdrv_attach_aio_context)(BlockDriverState *bs, | |
359 | AioContext *new_context); | |
360 | ||
361 | /** | |
362 | * Try to get @bs's logical and physical block size. | |
363 | * On success, store them in @bsz and return zero. | |
364 | * On failure, return negative errno. | |
365 | */ | |
366 | int (*bdrv_probe_blocksizes)(BlockDriverState *bs, BlockSizes *bsz); | |
367 | /** | |
368 | * Try to get @bs's geometry (cyls, heads, sectors) | |
369 | * On success, store them in @geo and return 0. | |
370 | * On failure return -errno. | |
371 | * Only drivers that want to override guest geometry implement this | |
372 | * callback; see hd_geometry_guess(). | |
373 | */ | |
374 | int (*bdrv_probe_geometry)(BlockDriverState *bs, HDGeometry *geo); | |
375 | ||
376 | void (*bdrv_add_child)(BlockDriverState *parent, BlockDriverState *child, | |
377 | Error **errp); | |
378 | void (*bdrv_del_child)(BlockDriverState *parent, BdrvChild *child, | |
379 | Error **errp); | |
380 | ||
381 | /** | |
382 | * Informs the block driver that a permission change is intended. The | |
383 | * driver checks whether the change is permissible and may take other | |
384 | * preparations for the change (e.g. get file system locks). This operation | |
385 | * is always followed either by a call to either .bdrv_set_perm or | |
386 | * .bdrv_abort_perm_update. | |
387 | * | |
388 | * Checks whether the requested set of cumulative permissions in @perm | |
389 | * can be granted for accessing @bs and whether no other users are using | |
390 | * permissions other than those given in @shared (both arguments take | |
391 | * BLK_PERM_* bitmasks). | |
392 | * | |
393 | * If both conditions are met, 0 is returned. Otherwise, -errno is returned | |
394 | * and errp is set to an error describing the conflict. | |
395 | */ | |
396 | int (*bdrv_check_perm)(BlockDriverState *bs, uint64_t perm, | |
397 | uint64_t shared, Error **errp); | |
398 | ||
399 | /** | |
400 | * Called to inform the driver that the set of cumulative set of used | |
401 | * permissions for @bs has changed to @perm, and the set of sharable | |
402 | * permission to @shared. The driver can use this to propagate changes to | |
403 | * its children (i.e. request permissions only if a parent actually needs | |
404 | * them). | |
405 | * | |
406 | * This function is only invoked after bdrv_check_perm(), so block drivers | |
407 | * may rely on preparations made in their .bdrv_check_perm implementation. | |
408 | */ | |
409 | void (*bdrv_set_perm)(BlockDriverState *bs, uint64_t perm, uint64_t shared); | |
410 | ||
411 | /* | |
412 | * Called to inform the driver that after a previous bdrv_check_perm() | |
413 | * call, the permission update is not performed and any preparations made | |
414 | * for it (e.g. taken file locks) need to be undone. | |
415 | * | |
416 | * This function can be called even for nodes that never saw a | |
417 | * bdrv_check_perm() call. It is a no-op then. | |
418 | */ | |
419 | void (*bdrv_abort_perm_update)(BlockDriverState *bs); | |
420 | ||
421 | /** | |
422 | * Returns in @nperm and @nshared the permissions that the driver for @bs | |
423 | * needs on its child @c, based on the cumulative permissions requested by | |
424 | * the parents in @parent_perm and @parent_shared. | |
425 | * | |
426 | * If @c is NULL, return the permissions for attaching a new child for the | |
427 | * given @child_class and @role. | |
428 | * | |
429 | * If @reopen_queue is non-NULL, don't return the currently needed | |
430 | * permissions, but those that will be needed after applying the | |
431 | * @reopen_queue. | |
432 | */ | |
433 | void (*bdrv_child_perm)(BlockDriverState *bs, BdrvChild *c, | |
434 | BdrvChildRole role, | |
435 | BlockReopenQueue *reopen_queue, | |
436 | uint64_t parent_perm, uint64_t parent_shared, | |
437 | uint64_t *nperm, uint64_t *nshared); | |
438 | ||
439 | /** | |
440 | * Register/unregister a buffer for I/O. For example, when the driver is | |
441 | * interested to know the memory areas that will later be used in iovs, so | |
442 | * that it can do IOMMU mapping with VFIO etc., in order to get better | |
443 | * performance. In the case of VFIO drivers, this callback is used to do | |
444 | * DMA mapping for hot buffers. | |
445 | * | |
446 | * Returns: true on success, false on failure | |
447 | */ | |
448 | bool GRAPH_RDLOCK_PTR (*bdrv_register_buf)( | |
449 | BlockDriverState *bs, void *host, size_t size, Error **errp); | |
450 | void GRAPH_RDLOCK_PTR (*bdrv_unregister_buf)( | |
451 | BlockDriverState *bs, void *host, size_t size); | |
452 | ||
453 | /* | |
454 | * This field is modified only under the BQL, and is part of | |
455 | * the global state. | |
456 | */ | |
457 | QLIST_ENTRY(BlockDriver) list; | |
458 | ||
459 | /* | |
460 | * I/O API functions. These functions are thread-safe. | |
461 | * | |
462 | * See include/block/block-io.h for more information about | |
463 | * the I/O API. | |
464 | */ | |
465 | ||
466 | int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename); | |
467 | ||
468 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_amend)( | |
469 | BlockDriverState *bs, BlockdevAmendOptions *opts, bool force, | |
470 | Error **errp); | |
471 | ||
472 | /* aio */ | |
473 | BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_preadv)(BlockDriverState *bs, | |
474 | int64_t offset, int64_t bytes, QEMUIOVector *qiov, | |
475 | BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque); | |
476 | ||
477 | BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_pwritev)(BlockDriverState *bs, | |
478 | int64_t offset, int64_t bytes, QEMUIOVector *qiov, | |
479 | BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque); | |
480 | ||
481 | BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_flush)( | |
482 | BlockDriverState *bs, BlockCompletionFunc *cb, void *opaque); | |
483 | ||
484 | BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_pdiscard)( | |
485 | BlockDriverState *bs, int64_t offset, int bytes, | |
486 | BlockCompletionFunc *cb, void *opaque); | |
487 | ||
488 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_readv)(BlockDriverState *bs, | |
489 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov); | |
490 | ||
491 | /** | |
492 | * @offset: position in bytes to read at | |
493 | * @bytes: number of bytes to read | |
494 | * @qiov: the buffers to fill with read data | |
495 | * @flags: currently unused, always 0 | |
496 | * | |
497 | * @offset and @bytes will be a multiple of 'request_alignment', | |
498 | * but the length of individual @qiov elements does not have to | |
499 | * be a multiple. | |
500 | * | |
501 | * @bytes will always equal the total size of @qiov, and will be | |
502 | * no larger than 'max_transfer'. | |
503 | * | |
504 | * The buffer in @qiov may point directly to guest memory. | |
505 | */ | |
506 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv)(BlockDriverState *bs, | |
507 | int64_t offset, int64_t bytes, QEMUIOVector *qiov, | |
508 | BdrvRequestFlags flags); | |
509 | ||
510 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_part)( | |
511 | BlockDriverState *bs, int64_t offset, int64_t bytes, | |
512 | QEMUIOVector *qiov, size_t qiov_offset, | |
513 | BdrvRequestFlags flags); | |
514 | ||
515 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_writev)(BlockDriverState *bs, | |
516 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, | |
517 | int flags); | |
518 | /** | |
519 | * @offset: position in bytes to write at | |
520 | * @bytes: number of bytes to write | |
521 | * @qiov: the buffers containing data to write | |
522 | * @flags: zero or more bits allowed by 'supported_write_flags' | |
523 | * | |
524 | * @offset and @bytes will be a multiple of 'request_alignment', | |
525 | * but the length of individual @qiov elements does not have to | |
526 | * be a multiple. | |
527 | * | |
528 | * @bytes will always equal the total size of @qiov, and will be | |
529 | * no larger than 'max_transfer'. | |
530 | * | |
531 | * The buffer in @qiov may point directly to guest memory. | |
532 | */ | |
533 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev)( | |
534 | BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov, | |
535 | BdrvRequestFlags flags); | |
536 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_part)( | |
537 | BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov, | |
538 | size_t qiov_offset, BdrvRequestFlags flags); | |
539 | ||
540 | /* | |
541 | * Efficiently zero a region of the disk image. Typically an image format | |
542 | * would use a compact metadata representation to implement this. This | |
543 | * function pointer may be NULL or return -ENOSUP and .bdrv_co_writev() | |
544 | * will be called instead. | |
545 | */ | |
546 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwrite_zeroes)( | |
547 | BlockDriverState *bs, int64_t offset, int64_t bytes, | |
548 | BdrvRequestFlags flags); | |
549 | ||
550 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard)( | |
551 | BlockDriverState *bs, int64_t offset, int64_t bytes); | |
552 | ||
553 | /* | |
554 | * Map [offset, offset + nbytes) range onto a child of @bs to copy from, | |
555 | * and invoke bdrv_co_copy_range_from(child, ...), or invoke | |
556 | * bdrv_co_copy_range_to() if @bs is the leaf child to copy data from. | |
557 | * | |
558 | * See the comment of bdrv_co_copy_range for the parameter and return value | |
559 | * semantics. | |
560 | */ | |
561 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_from)( | |
562 | BlockDriverState *bs, BdrvChild *src, int64_t offset, | |
563 | BdrvChild *dst, int64_t dst_offset, int64_t bytes, | |
564 | BdrvRequestFlags read_flags, BdrvRequestFlags write_flags); | |
565 | ||
566 | /* | |
567 | * Map [offset, offset + nbytes) range onto a child of bs to copy data to, | |
568 | * and invoke bdrv_co_copy_range_to(child, src, ...), or perform the copy | |
569 | * operation if @bs is the leaf and @src has the same BlockDriver. Return | |
570 | * -ENOTSUP if @bs is the leaf but @src has a different BlockDriver. | |
571 | * | |
572 | * See the comment of bdrv_co_copy_range for the parameter and return value | |
573 | * semantics. | |
574 | */ | |
575 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_to)( | |
576 | BlockDriverState *bs, BdrvChild *src, int64_t src_offset, | |
577 | BdrvChild *dst, int64_t dst_offset, int64_t bytes, | |
578 | BdrvRequestFlags read_flags, BdrvRequestFlags write_flags); | |
579 | ||
580 | /* | |
581 | * Building block for bdrv_block_status[_above] and | |
582 | * bdrv_is_allocated[_above]. The driver should answer only | |
583 | * according to the current layer, and should only need to set | |
584 | * BDRV_BLOCK_DATA, BDRV_BLOCK_ZERO, BDRV_BLOCK_OFFSET_VALID, | |
585 | * and/or BDRV_BLOCK_RAW; if the current layer defers to a backing | |
586 | * layer, the result should be 0 (and not BDRV_BLOCK_ZERO). See | |
587 | * block.h for the overall meaning of the bits. As a hint, the | |
588 | * flag want_zero is true if the caller cares more about precise | |
589 | * mappings (favor accurate _OFFSET_VALID/_ZERO) or false for | |
590 | * overall allocation (favor larger *pnum, perhaps by reporting | |
591 | * _DATA instead of _ZERO). The block layer guarantees input | |
592 | * clamped to bdrv_getlength() and aligned to request_alignment, | |
593 | * as well as non-NULL pnum, map, and file; in turn, the driver | |
594 | * must return an error or set pnum to an aligned non-zero value. | |
595 | * | |
596 | * Note that @bytes is just a hint on how big of a region the | |
597 | * caller wants to inspect. It is not a limit on *pnum. | |
598 | * Implementations are free to return larger values of *pnum if | |
599 | * doing so does not incur a performance penalty. | |
600 | * | |
601 | * block/io.c's bdrv_co_block_status() will utilize an unclamped | |
602 | * *pnum value for the block-status cache on protocol nodes, prior | |
603 | * to clamping *pnum for return to its caller. | |
604 | */ | |
605 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_block_status)( | |
606 | BlockDriverState *bs, | |
607 | bool want_zero, int64_t offset, int64_t bytes, int64_t *pnum, | |
608 | int64_t *map, BlockDriverState **file); | |
609 | ||
610 | /* | |
611 | * Snapshot-access API. | |
612 | * | |
613 | * Block-driver may provide snapshot-access API: special functions to access | |
614 | * some internal "snapshot". The functions are similar with normal | |
615 | * read/block_status/discard handler, but don't have any specific handling | |
616 | * in generic block-layer: no serializing, no alignment, no tracked | |
617 | * requests. So, block-driver that realizes these APIs is fully responsible | |
618 | * for synchronization between snapshot-access API and normal IO requests. | |
619 | * | |
620 | * TODO: To be able to support qcow2's internal snapshots, this API will | |
621 | * need to be extended to: | |
622 | * - be able to select a specific snapshot | |
623 | * - receive the snapshot's actual length (which may differ from bs's | |
624 | * length) | |
625 | */ | |
626 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_snapshot)( | |
627 | BlockDriverState *bs, int64_t offset, int64_t bytes, | |
628 | QEMUIOVector *qiov, size_t qiov_offset); | |
629 | ||
630 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_snapshot_block_status)( | |
631 | BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes, | |
632 | int64_t *pnum, int64_t *map, BlockDriverState **file); | |
633 | ||
634 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard_snapshot)( | |
635 | BlockDriverState *bs, int64_t offset, int64_t bytes); | |
636 | ||
637 | /* | |
638 | * Invalidate any cached meta-data. | |
639 | */ | |
640 | void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_invalidate_cache)( | |
641 | BlockDriverState *bs, Error **errp); | |
642 | ||
643 | /* | |
644 | * Flushes all data for all layers by calling bdrv_co_flush for underlying | |
645 | * layers, if needed. This function is needed for deterministic | |
646 | * synchronization of the flush finishing callback. | |
647 | */ | |
648 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush)(BlockDriverState *bs); | |
649 | ||
650 | /* Delete a created file. */ | |
651 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_delete_file)( | |
652 | BlockDriverState *bs, Error **errp); | |
653 | ||
654 | /* | |
655 | * Flushes all data that was already written to the OS all the way down to | |
656 | * the disk (for example file-posix.c calls fsync()). | |
657 | */ | |
658 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_disk)( | |
659 | BlockDriverState *bs); | |
660 | ||
661 | /* | |
662 | * Flushes all internal caches to the OS. The data may still sit in a | |
663 | * writeback cache of the host OS, but it will survive a crash of the qemu | |
664 | * process. | |
665 | */ | |
666 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_os)( | |
667 | BlockDriverState *bs); | |
668 | ||
669 | /* | |
670 | * Truncate @bs to @offset bytes using the given @prealloc mode | |
671 | * when growing. Modes other than PREALLOC_MODE_OFF should be | |
672 | * rejected when shrinking @bs. | |
673 | * | |
674 | * If @exact is true, @bs must be resized to exactly @offset. | |
675 | * Otherwise, it is sufficient for @bs (if it is a host block | |
676 | * device and thus there is no way to resize it) to be at least | |
677 | * @offset bytes in length. | |
678 | * | |
679 | * If @exact is true and this function fails but would succeed | |
680 | * with @exact = false, it should return -ENOTSUP. | |
681 | */ | |
682 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_truncate)( | |
683 | BlockDriverState *bs, int64_t offset, bool exact, | |
684 | PreallocMode prealloc, BdrvRequestFlags flags, Error **errp); | |
685 | ||
686 | int64_t coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_getlength)( | |
687 | BlockDriverState *bs); | |
688 | ||
689 | int64_t coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_get_allocated_file_size)( | |
690 | BlockDriverState *bs); | |
691 | ||
692 | BlockMeasureInfo *(*bdrv_measure)(QemuOpts *opts, BlockDriverState *in_bs, | |
693 | Error **errp); | |
694 | ||
695 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed)( | |
696 | BlockDriverState *bs, int64_t offset, int64_t bytes, | |
697 | QEMUIOVector *qiov); | |
698 | ||
699 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed_part)( | |
700 | BlockDriverState *bs, int64_t offset, int64_t bytes, | |
701 | QEMUIOVector *qiov, size_t qiov_offset); | |
702 | ||
703 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_get_info)( | |
704 | BlockDriverState *bs, BlockDriverInfo *bdi); | |
705 | ||
706 | ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs, | |
707 | Error **errp); | |
708 | BlockStatsSpecific *(*bdrv_get_specific_stats)(BlockDriverState *bs); | |
709 | ||
710 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_save_vmstate)( | |
711 | BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos); | |
712 | ||
713 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_load_vmstate)( | |
714 | BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos); | |
715 | ||
716 | /* removable device specific */ | |
717 | bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_is_inserted)( | |
718 | BlockDriverState *bs); | |
719 | void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_eject)( | |
720 | BlockDriverState *bs, bool eject_flag); | |
721 | void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_lock_medium)( | |
722 | BlockDriverState *bs, bool locked); | |
723 | ||
724 | /* to control generic scsi devices */ | |
725 | BlockAIOCB *coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_aio_ioctl)( | |
726 | BlockDriverState *bs, unsigned long int req, void *buf, | |
727 | BlockCompletionFunc *cb, void *opaque); | |
728 | ||
729 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_ioctl)( | |
730 | BlockDriverState *bs, unsigned long int req, void *buf); | |
731 | ||
732 | /* | |
733 | * Returns 0 for completed check, -errno for internal errors. | |
734 | * The check results are stored in result. | |
735 | */ | |
736 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_check)( | |
737 | BlockDriverState *bs, BdrvCheckResult *result, BdrvCheckMode fix); | |
738 | ||
739 | void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_debug_event)( | |
740 | BlockDriverState *bs, BlkdebugEvent event); | |
741 | ||
742 | /* io queue for linux-aio */ | |
743 | void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_io_plug)(BlockDriverState *bs); | |
744 | void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_io_unplug)( | |
745 | BlockDriverState *bs); | |
746 | ||
747 | /** | |
748 | * bdrv_drain_begin is called if implemented in the beginning of a | |
749 | * drain operation to drain and stop any internal sources of requests in | |
750 | * the driver. | |
751 | * bdrv_drain_end is called if implemented at the end of the drain. | |
752 | * | |
753 | * They should be used by the driver to e.g. manage scheduled I/O | |
754 | * requests, or toggle an internal state. After the end of the drain new | |
755 | * requests will continue normally. | |
756 | * | |
757 | * Implementations of both functions must not call aio_poll(). | |
758 | */ | |
759 | void (*bdrv_drain_begin)(BlockDriverState *bs); | |
760 | void (*bdrv_drain_end)(BlockDriverState *bs); | |
761 | ||
762 | bool (*bdrv_supports_persistent_dirty_bitmap)(BlockDriverState *bs); | |
763 | ||
764 | bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_can_store_new_dirty_bitmap)( | |
765 | BlockDriverState *bs, const char *name, uint32_t granularity, | |
766 | Error **errp); | |
767 | ||
768 | int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_remove_persistent_dirty_bitmap)( | |
769 | BlockDriverState *bs, const char *name, Error **errp); | |
770 | }; | |
771 | ||
772 | static inline bool TSA_NO_TSA block_driver_can_compress(BlockDriver *drv) | |
773 | { | |
774 | return drv->bdrv_co_pwritev_compressed || | |
775 | drv->bdrv_co_pwritev_compressed_part; | |
776 | } | |
777 | ||
778 | typedef struct BlockLimits { | |
779 | /* | |
780 | * Alignment requirement, in bytes, for offset/length of I/O | |
781 | * requests. Must be a power of 2 less than INT_MAX; defaults to | |
782 | * 1 for drivers with modern byte interfaces, and to 512 | |
783 | * otherwise. | |
784 | */ | |
785 | uint32_t request_alignment; | |
786 | ||
787 | /* | |
788 | * Maximum number of bytes that can be discarded at once. Must be multiple | |
789 | * of pdiscard_alignment, but need not be power of 2. May be 0 if no | |
790 | * inherent 64-bit limit. | |
791 | */ | |
792 | int64_t max_pdiscard; | |
793 | ||
794 | /* | |
795 | * Optimal alignment for discard requests in bytes. A power of 2 | |
796 | * is best but not mandatory. Must be a multiple of | |
797 | * bl.request_alignment, and must be less than max_pdiscard if | |
798 | * that is set. May be 0 if bl.request_alignment is good enough | |
799 | */ | |
800 | uint32_t pdiscard_alignment; | |
801 | ||
802 | /* | |
803 | * Maximum number of bytes that can zeroized at once. Must be multiple of | |
804 | * pwrite_zeroes_alignment. 0 means no limit. | |
805 | */ | |
806 | int64_t max_pwrite_zeroes; | |
807 | ||
808 | /* | |
809 | * Optimal alignment for write zeroes requests in bytes. A power | |
810 | * of 2 is best but not mandatory. Must be a multiple of | |
811 | * bl.request_alignment, and must be less than max_pwrite_zeroes | |
812 | * if that is set. May be 0 if bl.request_alignment is good | |
813 | * enough | |
814 | */ | |
815 | uint32_t pwrite_zeroes_alignment; | |
816 | ||
817 | /* | |
818 | * Optimal transfer length in bytes. A power of 2 is best but not | |
819 | * mandatory. Must be a multiple of bl.request_alignment, or 0 if | |
820 | * no preferred size | |
821 | */ | |
822 | uint32_t opt_transfer; | |
823 | ||
824 | /* | |
825 | * Maximal transfer length in bytes. Need not be power of 2, but | |
826 | * must be multiple of opt_transfer and bl.request_alignment, or 0 | |
827 | * for no 32-bit limit. For now, anything larger than INT_MAX is | |
828 | * clamped down. | |
829 | */ | |
830 | uint32_t max_transfer; | |
831 | ||
832 | /* | |
833 | * Maximal hardware transfer length in bytes. Applies whenever | |
834 | * transfers to the device bypass the kernel I/O scheduler, for | |
835 | * example with SG_IO. If larger than max_transfer or if zero, | |
836 | * blk_get_max_hw_transfer will fall back to max_transfer. | |
837 | */ | |
838 | uint64_t max_hw_transfer; | |
839 | ||
840 | /* | |
841 | * Maximal number of scatter/gather elements allowed by the hardware. | |
842 | * Applies whenever transfers to the device bypass the kernel I/O | |
843 | * scheduler, for example with SG_IO. If larger than max_iov | |
844 | * or if zero, blk_get_max_hw_iov will fall back to max_iov. | |
845 | */ | |
846 | int max_hw_iov; | |
847 | ||
848 | ||
849 | /* memory alignment, in bytes so that no bounce buffer is needed */ | |
850 | size_t min_mem_alignment; | |
851 | ||
852 | /* memory alignment, in bytes, for bounce buffer */ | |
853 | size_t opt_mem_alignment; | |
854 | ||
855 | /* maximum number of iovec elements */ | |
856 | int max_iov; | |
857 | ||
858 | /* | |
859 | * true if the length of the underlying file can change, and QEMU | |
860 | * is expected to adjust automatically. Mostly for CD-ROM drives, | |
861 | * whose length is zero when the tray is empty (they don't need | |
862 | * an explicit monitor command to load the disk inside the guest). | |
863 | */ | |
864 | bool has_variable_length; | |
865 | ||
866 | /* device zone model */ | |
867 | BlockZoneModel zoned; | |
868 | } BlockLimits; | |
869 | ||
870 | typedef struct BdrvOpBlocker BdrvOpBlocker; | |
871 | ||
872 | typedef struct BdrvAioNotifier { | |
873 | void (*attached_aio_context)(AioContext *new_context, void *opaque); | |
874 | void (*detach_aio_context)(void *opaque); | |
875 | ||
876 | void *opaque; | |
877 | bool deleted; | |
878 | ||
879 | QLIST_ENTRY(BdrvAioNotifier) list; | |
880 | } BdrvAioNotifier; | |
881 | ||
882 | struct BdrvChildClass { | |
883 | /* | |
884 | * If true, bdrv_replace_node() doesn't change the node this BdrvChild | |
885 | * points to. | |
886 | */ | |
887 | bool stay_at_node; | |
888 | ||
889 | /* | |
890 | * If true, the parent is a BlockDriverState and bdrv_next_all_states() | |
891 | * will return it. This information is used for drain_all, where every node | |
892 | * will be drained separately, so the drain only needs to be propagated to | |
893 | * non-BDS parents. | |
894 | */ | |
895 | bool parent_is_bds; | |
896 | ||
897 | /* | |
898 | * Global state (GS) API. These functions run under the BQL. | |
899 | * | |
900 | * See include/block/block-global-state.h for more information about | |
901 | * the GS API. | |
902 | */ | |
903 | void (*inherit_options)(BdrvChildRole role, bool parent_is_format, | |
904 | int *child_flags, QDict *child_options, | |
905 | int parent_flags, QDict *parent_options); | |
906 | void (*change_media)(BdrvChild *child, bool load); | |
907 | ||
908 | /* | |
909 | * Returns a malloced string that describes the parent of the child for a | |
910 | * human reader. This could be a node-name, BlockBackend name, qdev ID or | |
911 | * QOM path of the device owning the BlockBackend, job type and ID etc. The | |
912 | * caller is responsible for freeing the memory. | |
913 | */ | |
914 | char *(*get_parent_desc)(BdrvChild *child); | |
915 | ||
916 | /* | |
917 | * Notifies the parent that the child has been activated/inactivated (e.g. | |
918 | * when migration is completing) and it can start/stop requesting | |
919 | * permissions and doing I/O on it. | |
920 | */ | |
921 | void (*activate)(BdrvChild *child, Error **errp); | |
922 | int (*inactivate)(BdrvChild *child); | |
923 | ||
924 | void GRAPH_WRLOCK_PTR (*attach)(BdrvChild *child); | |
925 | void GRAPH_WRLOCK_PTR (*detach)(BdrvChild *child); | |
926 | ||
927 | /* | |
928 | * Notifies the parent that the filename of its child has changed (e.g. | |
929 | * because the direct child was removed from the backing chain), so that it | |
930 | * can update its reference. | |
931 | */ | |
932 | int (*update_filename)(BdrvChild *child, BlockDriverState *new_base, | |
933 | const char *filename, Error **errp); | |
934 | ||
935 | bool (*change_aio_ctx)(BdrvChild *child, AioContext *ctx, | |
936 | GHashTable *visited, Transaction *tran, | |
937 | Error **errp); | |
938 | ||
939 | /* | |
940 | * I/O API functions. These functions are thread-safe. | |
941 | * | |
942 | * See include/block/block-io.h for more information about | |
943 | * the I/O API. | |
944 | */ | |
945 | ||
946 | void (*resize)(BdrvChild *child); | |
947 | ||
948 | /* | |
949 | * Returns a name that is supposedly more useful for human users than the | |
950 | * node name for identifying the node in question (in particular, a BB | |
951 | * name), or NULL if the parent can't provide a better name. | |
952 | */ | |
953 | const char *(*get_name)(BdrvChild *child); | |
954 | ||
955 | AioContext *(*get_parent_aio_context)(BdrvChild *child); | |
956 | ||
957 | /* | |
958 | * If this pair of functions is implemented, the parent doesn't issue new | |
959 | * requests after returning from .drained_begin() until .drained_end() is | |
960 | * called. | |
961 | * | |
962 | * These functions must not change the graph (and therefore also must not | |
963 | * call aio_poll(), which could change the graph indirectly). | |
964 | * | |
965 | * Note that this can be nested. If drained_begin() was called twice, new | |
966 | * I/O is allowed only after drained_end() was called twice, too. | |
967 | */ | |
968 | void (*drained_begin)(BdrvChild *child); | |
969 | void (*drained_end)(BdrvChild *child); | |
970 | ||
971 | /* | |
972 | * Returns whether the parent has pending requests for the child. This | |
973 | * callback is polled after .drained_begin() has been called until all | |
974 | * activity on the child has stopped. | |
975 | */ | |
976 | bool (*drained_poll)(BdrvChild *child); | |
977 | }; | |
978 | ||
979 | extern const BdrvChildClass child_of_bds; | |
980 | ||
981 | struct BdrvChild { | |
982 | BlockDriverState *bs; | |
983 | char *name; | |
984 | const BdrvChildClass *klass; | |
985 | BdrvChildRole role; | |
986 | void *opaque; | |
987 | ||
988 | /** | |
989 | * Granted permissions for operating on this BdrvChild (BLK_PERM_* bitmask) | |
990 | */ | |
991 | uint64_t perm; | |
992 | ||
993 | /** | |
994 | * Permissions that can still be granted to other users of @bs while this | |
995 | * BdrvChild is still attached to it. (BLK_PERM_* bitmask) | |
996 | */ | |
997 | uint64_t shared_perm; | |
998 | ||
999 | /* | |
1000 | * This link is frozen: the child can neither be replaced nor | |
1001 | * detached from the parent. | |
1002 | */ | |
1003 | bool frozen; | |
1004 | ||
1005 | /* | |
1006 | * True if the parent of this child has been drained by this BdrvChild | |
1007 | * (through klass->drained_*). | |
1008 | * | |
1009 | * It is generally true if bs->quiesce_counter > 0. It may differ while the | |
1010 | * child is entering or leaving a drained section. | |
1011 | */ | |
1012 | bool quiesced_parent; | |
1013 | ||
1014 | QLIST_ENTRY(BdrvChild) next; | |
1015 | QLIST_ENTRY(BdrvChild) next_parent; | |
1016 | }; | |
1017 | ||
1018 | /* | |
1019 | * Allows bdrv_co_block_status() to cache one data region for a | |
1020 | * protocol node. | |
1021 | * | |
1022 | * @valid: Whether the cache is valid (should be accessed with atomic | |
1023 | * functions so this can be reset by RCU readers) | |
1024 | * @data_start: Offset where we know (or strongly assume) is data | |
1025 | * @data_end: Offset where the data region ends (which is not necessarily | |
1026 | * the start of a zeroed region) | |
1027 | */ | |
1028 | typedef struct BdrvBlockStatusCache { | |
1029 | struct rcu_head rcu; | |
1030 | ||
1031 | bool valid; | |
1032 | int64_t data_start; | |
1033 | int64_t data_end; | |
1034 | } BdrvBlockStatusCache; | |
1035 | ||
1036 | struct BlockDriverState { | |
1037 | /* | |
1038 | * Protected by big QEMU lock or read-only after opening. No special | |
1039 | * locking needed during I/O... | |
1040 | */ | |
1041 | int open_flags; /* flags used to open the file, re-used for re-open */ | |
1042 | bool encrypted; /* if true, the media is encrypted */ | |
1043 | bool sg; /* if true, the device is a /dev/sg* */ | |
1044 | bool probed; /* if true, format was probed rather than specified */ | |
1045 | bool force_share; /* if true, always allow all shared permissions */ | |
1046 | bool implicit; /* if true, this filter node was automatically inserted */ | |
1047 | ||
1048 | BlockDriver *drv; /* NULL means no media */ | |
1049 | void *opaque; | |
1050 | ||
1051 | AioContext *aio_context; /* event loop used for fd handlers, timers, etc */ | |
1052 | /* | |
1053 | * long-running tasks intended to always use the same AioContext as this | |
1054 | * BDS may register themselves in this list to be notified of changes | |
1055 | * regarding this BDS's context | |
1056 | */ | |
1057 | QLIST_HEAD(, BdrvAioNotifier) aio_notifiers; | |
1058 | bool walking_aio_notifiers; /* to make removal during iteration safe */ | |
1059 | ||
1060 | char filename[PATH_MAX]; | |
1061 | /* | |
1062 | * If not empty, this image is a diff in relation to backing_file. | |
1063 | * Note that this is the name given in the image header and | |
1064 | * therefore may or may not be equal to .backing->bs->filename. | |
1065 | * If this field contains a relative path, it is to be resolved | |
1066 | * relatively to the overlay's location. | |
1067 | */ | |
1068 | char backing_file[PATH_MAX]; | |
1069 | /* | |
1070 | * The backing filename indicated by the image header. Contrary | |
1071 | * to backing_file, if we ever open this file, auto_backing_file | |
1072 | * is replaced by the resulting BDS's filename (i.e. after a | |
1073 | * bdrv_refresh_filename() run). | |
1074 | */ | |
1075 | char auto_backing_file[PATH_MAX]; | |
1076 | char backing_format[16]; /* if non-zero and backing_file exists */ | |
1077 | ||
1078 | QDict *full_open_options; | |
1079 | char exact_filename[PATH_MAX]; | |
1080 | ||
1081 | /* I/O Limits */ | |
1082 | BlockLimits bl; | |
1083 | ||
1084 | /* | |
1085 | * Flags honored during pread | |
1086 | */ | |
1087 | BdrvRequestFlags supported_read_flags; | |
1088 | /* | |
1089 | * Flags honored during pwrite (so far: BDRV_REQ_FUA, | |
1090 | * BDRV_REQ_WRITE_UNCHANGED). | |
1091 | * If a driver does not support BDRV_REQ_WRITE_UNCHANGED, those | |
1092 | * writes will be issued as normal writes without the flag set. | |
1093 | * This is important to note for drivers that do not explicitly | |
1094 | * request a WRITE permission for their children and instead take | |
1095 | * the same permissions as their parent did (this is commonly what | |
1096 | * block filters do). Such drivers have to be aware that the | |
1097 | * parent may have taken a WRITE_UNCHANGED permission only and is | |
1098 | * issuing such requests. Drivers either must make sure that | |
1099 | * these requests do not result in plain WRITE accesses (usually | |
1100 | * by supporting BDRV_REQ_WRITE_UNCHANGED, and then forwarding | |
1101 | * every incoming write request as-is, including potentially that | |
1102 | * flag), or they have to explicitly take the WRITE permission for | |
1103 | * their children. | |
1104 | */ | |
1105 | BdrvRequestFlags supported_write_flags; | |
1106 | /* | |
1107 | * Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA, | |
1108 | * BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED) | |
1109 | */ | |
1110 | BdrvRequestFlags supported_zero_flags; | |
1111 | /* | |
1112 | * Flags honoured during truncate (so far: BDRV_REQ_ZERO_WRITE). | |
1113 | * | |
1114 | * If BDRV_REQ_ZERO_WRITE is given, the truncate operation must make sure | |
1115 | * that any added space reads as all zeros. If this can't be guaranteed, | |
1116 | * the operation must fail. | |
1117 | */ | |
1118 | BdrvRequestFlags supported_truncate_flags; | |
1119 | ||
1120 | /* the following member gives a name to every node on the bs graph. */ | |
1121 | char node_name[32]; | |
1122 | /* element of the list of named nodes building the graph */ | |
1123 | QTAILQ_ENTRY(BlockDriverState) node_list; | |
1124 | /* element of the list of all BlockDriverStates (all_bdrv_states) */ | |
1125 | QTAILQ_ENTRY(BlockDriverState) bs_list; | |
1126 | /* element of the list of monitor-owned BDS */ | |
1127 | QTAILQ_ENTRY(BlockDriverState) monitor_list; | |
1128 | int refcnt; | |
1129 | ||
1130 | /* operation blockers. Protected by BQL. */ | |
1131 | QLIST_HEAD(, BdrvOpBlocker) op_blockers[BLOCK_OP_TYPE_MAX]; | |
1132 | ||
1133 | /* | |
1134 | * The node that this node inherited default options from (and a reopen on | |
1135 | * which can affect this node by changing these defaults). This is always a | |
1136 | * parent node of this node. | |
1137 | */ | |
1138 | BlockDriverState *inherits_from; | |
1139 | ||
1140 | /* | |
1141 | * @backing and @file are some of @children or NULL. All these three fields | |
1142 | * (@file, @backing and @children) are modified only in | |
1143 | * bdrv_child_cb_attach() and bdrv_child_cb_detach(). | |
1144 | * | |
1145 | * See also comment in include/block/block.h, to learn how backing and file | |
1146 | * are connected with BdrvChildRole. | |
1147 | */ | |
1148 | QLIST_HEAD(, BdrvChild) children; | |
1149 | BdrvChild *backing; | |
1150 | BdrvChild *file; | |
1151 | ||
1152 | QLIST_HEAD(, BdrvChild) parents; | |
1153 | ||
1154 | QDict *options; | |
1155 | QDict *explicit_options; | |
1156 | BlockdevDetectZeroesOptions detect_zeroes; | |
1157 | ||
1158 | /* The error object in use for blocking operations on backing_hd */ | |
1159 | Error *backing_blocker; | |
1160 | ||
1161 | /* Protected by AioContext lock */ | |
1162 | ||
1163 | /* | |
1164 | * If we are reading a disk image, give its size in sectors. | |
1165 | * Generally read-only; it is written to by load_snapshot and | |
1166 | * save_snaphost, but the block layer is quiescent during those. | |
1167 | */ | |
1168 | int64_t total_sectors; | |
1169 | ||
1170 | /* threshold limit for writes, in bytes. "High water mark". */ | |
1171 | uint64_t write_threshold_offset; | |
1172 | ||
1173 | /* | |
1174 | * Writing to the list requires the BQL _and_ the dirty_bitmap_mutex. | |
1175 | * Reading from the list can be done with either the BQL or the | |
1176 | * dirty_bitmap_mutex. Modifying a bitmap only requires | |
1177 | * dirty_bitmap_mutex. | |
1178 | */ | |
1179 | QemuMutex dirty_bitmap_mutex; | |
1180 | QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps; | |
1181 | ||
1182 | /* Offset after the highest byte written to */ | |
1183 | Stat64 wr_highest_offset; | |
1184 | ||
1185 | /* | |
1186 | * If true, copy read backing sectors into image. Can be >1 if more | |
1187 | * than one client has requested copy-on-read. Accessed with atomic | |
1188 | * ops. | |
1189 | */ | |
1190 | int copy_on_read; | |
1191 | ||
1192 | /* | |
1193 | * number of in-flight requests; overall and serialising. | |
1194 | * Accessed with atomic ops. | |
1195 | */ | |
1196 | unsigned int in_flight; | |
1197 | unsigned int serialising_in_flight; | |
1198 | ||
1199 | /* | |
1200 | * counter for nested bdrv_io_plug. | |
1201 | * Accessed with atomic ops. | |
1202 | */ | |
1203 | unsigned io_plugged; | |
1204 | ||
1205 | /* do we need to tell the quest if we have a volatile write cache? */ | |
1206 | int enable_write_cache; | |
1207 | ||
1208 | /* Accessed with atomic ops. */ | |
1209 | int quiesce_counter; | |
1210 | ||
1211 | unsigned int write_gen; /* Current data generation */ | |
1212 | ||
1213 | /* Protected by reqs_lock. */ | |
1214 | CoMutex reqs_lock; | |
1215 | QLIST_HEAD(, BdrvTrackedRequest) tracked_requests; | |
1216 | CoQueue flush_queue; /* Serializing flush queue */ | |
1217 | bool active_flush_req; /* Flush request in flight? */ | |
1218 | ||
1219 | /* Only read/written by whoever has set active_flush_req to true. */ | |
1220 | unsigned int flushed_gen; /* Flushed write generation */ | |
1221 | ||
1222 | /* BdrvChild links to this node may never be frozen */ | |
1223 | bool never_freeze; | |
1224 | ||
1225 | /* Lock for block-status cache RCU writers */ | |
1226 | CoMutex bsc_modify_lock; | |
1227 | /* Always non-NULL, but must only be dereferenced under an RCU read guard */ | |
1228 | BdrvBlockStatusCache *block_status_cache; | |
1229 | }; | |
1230 | ||
1231 | struct BlockBackendRootState { | |
1232 | int open_flags; | |
1233 | BlockdevDetectZeroesOptions detect_zeroes; | |
1234 | }; | |
1235 | ||
1236 | typedef enum BlockMirrorBackingMode { | |
1237 | /* | |
1238 | * Reuse the existing backing chain from the source for the target. | |
1239 | * - sync=full: Set backing BDS to NULL. | |
1240 | * - sync=top: Use source's backing BDS. | |
1241 | * - sync=none: Use source as the backing BDS. | |
1242 | */ | |
1243 | MIRROR_SOURCE_BACKING_CHAIN, | |
1244 | ||
1245 | /* Open the target's backing chain completely anew */ | |
1246 | MIRROR_OPEN_BACKING_CHAIN, | |
1247 | ||
1248 | /* Do not change the target's backing BDS after job completion */ | |
1249 | MIRROR_LEAVE_BACKING_CHAIN, | |
1250 | } BlockMirrorBackingMode; | |
1251 | ||
1252 | ||
1253 | /* | |
1254 | * Essential block drivers which must always be statically linked into qemu, and | |
1255 | * which therefore can be accessed without using bdrv_find_format() | |
1256 | */ | |
1257 | extern BlockDriver bdrv_file; | |
1258 | extern BlockDriver bdrv_raw; | |
1259 | extern BlockDriver bdrv_qcow2; | |
1260 | ||
1261 | extern unsigned int bdrv_drain_all_count; | |
1262 | extern QemuOptsList bdrv_create_opts_simple; | |
1263 | ||
1264 | /* | |
1265 | * Common functions that are neither I/O nor Global State. | |
1266 | * | |
1267 | * See include/block/block-common.h for more information about | |
1268 | * the Common API. | |
1269 | */ | |
1270 | ||
1271 | static inline BlockDriverState *child_bs(BdrvChild *child) | |
1272 | { | |
1273 | return child ? child->bs : NULL; | |
1274 | } | |
1275 | ||
1276 | int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp); | |
1277 | char *create_tmp_file(Error **errp); | |
1278 | void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix, | |
1279 | QDict *options); | |
1280 | ||
1281 | ||
1282 | int bdrv_check_qiov_request(int64_t offset, int64_t bytes, | |
1283 | QEMUIOVector *qiov, size_t qiov_offset, | |
1284 | Error **errp); | |
1285 | ||
1286 | #ifdef _WIN32 | |
1287 | int is_windows_drive(const char *filename); | |
1288 | #endif | |
1289 | ||
1290 | #endif /* BLOCK_INT_COMMON_H */ |