]> git.proxmox.com Git - qemu.git/blob - include/block/block_int.h
block: Add bdrv_get_specific_info
[qemu.git] / include / block / block_int.h
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_H
25 #define BLOCK_INT_H
26
27 #include "block/block.h"
28 #include "qemu/option.h"
29 #include "qemu/queue.h"
30 #include "block/coroutine.h"
31 #include "qemu/timer.h"
32 #include "qapi-types.h"
33 #include "qapi/qmp/qerror.h"
34 #include "monitor/monitor.h"
35 #include "qemu/hbitmap.h"
36 #include "block/snapshot.h"
37 #include "qemu/main-loop.h"
38 #include "qemu/throttle.h"
39
40 #define BLOCK_FLAG_ENCRYPT 1
41 #define BLOCK_FLAG_COMPAT6 4
42 #define BLOCK_FLAG_LAZY_REFCOUNTS 8
43
44 #define BLOCK_OPT_SIZE "size"
45 #define BLOCK_OPT_ENCRYPT "encryption"
46 #define BLOCK_OPT_COMPAT6 "compat6"
47 #define BLOCK_OPT_BACKING_FILE "backing_file"
48 #define BLOCK_OPT_BACKING_FMT "backing_fmt"
49 #define BLOCK_OPT_CLUSTER_SIZE "cluster_size"
50 #define BLOCK_OPT_TABLE_SIZE "table_size"
51 #define BLOCK_OPT_PREALLOC "preallocation"
52 #define BLOCK_OPT_SUBFMT "subformat"
53 #define BLOCK_OPT_COMPAT_LEVEL "compat"
54 #define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts"
55 #define BLOCK_OPT_ADAPTER_TYPE "adapter_type"
56
57 typedef struct BdrvTrackedRequest {
58 BlockDriverState *bs;
59 int64_t sector_num;
60 int nb_sectors;
61 bool is_write;
62 QLIST_ENTRY(BdrvTrackedRequest) list;
63 Coroutine *co; /* owner, used for deadlock detection */
64 CoQueue wait_queue; /* coroutines blocked on this request */
65 } BdrvTrackedRequest;
66
67 struct BlockDriver {
68 const char *format_name;
69 int instance_size;
70 int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
71 int (*bdrv_probe_device)(const char *filename);
72
73 /* Any driver implementing this callback is expected to be able to handle
74 * NULL file names in its .bdrv_open() implementation */
75 void (*bdrv_parse_filename)(const char *filename, QDict *options, Error **errp);
76 /* Drivers not implementing bdrv_parse_filename nor bdrv_open should have
77 * this field set to true, except ones that are defined only by their
78 * child's bs.
79 * An example of the last type will be the quorum block driver.
80 */
81 bool bdrv_needs_filename;
82
83 /* For handling image reopen for split or non-split files */
84 int (*bdrv_reopen_prepare)(BDRVReopenState *reopen_state,
85 BlockReopenQueue *queue, Error **errp);
86 void (*bdrv_reopen_commit)(BDRVReopenState *reopen_state);
87 void (*bdrv_reopen_abort)(BDRVReopenState *reopen_state);
88
89 int (*bdrv_open)(BlockDriverState *bs, QDict *options, int flags,
90 Error **errp);
91 int (*bdrv_file_open)(BlockDriverState *bs, QDict *options, int flags,
92 Error **errp);
93 int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
94 uint8_t *buf, int nb_sectors);
95 int (*bdrv_write)(BlockDriverState *bs, int64_t sector_num,
96 const uint8_t *buf, int nb_sectors);
97 void (*bdrv_close)(BlockDriverState *bs);
98 void (*bdrv_rebind)(BlockDriverState *bs);
99 int (*bdrv_create)(const char *filename, QEMUOptionParameter *options,
100 Error **errp);
101 int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
102 int (*bdrv_make_empty)(BlockDriverState *bs);
103 /* aio */
104 BlockDriverAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
105 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
106 BlockDriverCompletionFunc *cb, void *opaque);
107 BlockDriverAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
108 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
109 BlockDriverCompletionFunc *cb, void *opaque);
110 BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
111 BlockDriverCompletionFunc *cb, void *opaque);
112 BlockDriverAIOCB *(*bdrv_aio_discard)(BlockDriverState *bs,
113 int64_t sector_num, int nb_sectors,
114 BlockDriverCompletionFunc *cb, void *opaque);
115
116 int coroutine_fn (*bdrv_co_readv)(BlockDriverState *bs,
117 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
118 int coroutine_fn (*bdrv_co_writev)(BlockDriverState *bs,
119 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
120 /*
121 * Efficiently zero a region of the disk image. Typically an image format
122 * would use a compact metadata representation to implement this. This
123 * function pointer may be NULL and .bdrv_co_writev() will be called
124 * instead.
125 */
126 int coroutine_fn (*bdrv_co_write_zeroes)(BlockDriverState *bs,
127 int64_t sector_num, int nb_sectors);
128 int coroutine_fn (*bdrv_co_discard)(BlockDriverState *bs,
129 int64_t sector_num, int nb_sectors);
130 int64_t coroutine_fn (*bdrv_co_get_block_status)(BlockDriverState *bs,
131 int64_t sector_num, int nb_sectors, int *pnum);
132
133 /*
134 * Invalidate any cached meta-data.
135 */
136 void (*bdrv_invalidate_cache)(BlockDriverState *bs);
137
138 /*
139 * Flushes all data that was already written to the OS all the way down to
140 * the disk (for example raw-posix calls fsync()).
141 */
142 int coroutine_fn (*bdrv_co_flush_to_disk)(BlockDriverState *bs);
143
144 /*
145 * Flushes all internal caches to the OS. The data may still sit in a
146 * writeback cache of the host OS, but it will survive a crash of the qemu
147 * process.
148 */
149 int coroutine_fn (*bdrv_co_flush_to_os)(BlockDriverState *bs);
150
151 const char *protocol_name;
152 int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset);
153 int64_t (*bdrv_getlength)(BlockDriverState *bs);
154 int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs);
155 int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
156 const uint8_t *buf, int nb_sectors);
157
158 int (*bdrv_snapshot_create)(BlockDriverState *bs,
159 QEMUSnapshotInfo *sn_info);
160 int (*bdrv_snapshot_goto)(BlockDriverState *bs,
161 const char *snapshot_id);
162 int (*bdrv_snapshot_delete)(BlockDriverState *bs,
163 const char *snapshot_id,
164 const char *name,
165 Error **errp);
166 int (*bdrv_snapshot_list)(BlockDriverState *bs,
167 QEMUSnapshotInfo **psn_info);
168 int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
169 const char *snapshot_name);
170 int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
171 ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
172
173 int (*bdrv_save_vmstate)(BlockDriverState *bs, QEMUIOVector *qiov,
174 int64_t pos);
175 int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
176 int64_t pos, int size);
177
178 int (*bdrv_change_backing_file)(BlockDriverState *bs,
179 const char *backing_file, const char *backing_fmt);
180
181 /* removable device specific */
182 int (*bdrv_is_inserted)(BlockDriverState *bs);
183 int (*bdrv_media_changed)(BlockDriverState *bs);
184 void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag);
185 void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);
186
187 /* to control generic scsi devices */
188 int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
189 BlockDriverAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
190 unsigned long int req, void *buf,
191 BlockDriverCompletionFunc *cb, void *opaque);
192
193 /* List of options for creating images, terminated by name == NULL */
194 QEMUOptionParameter *create_options;
195
196
197 /*
198 * Returns 0 for completed check, -errno for internal errors.
199 * The check results are stored in result.
200 */
201 int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result,
202 BdrvCheckMode fix);
203
204 int (*bdrv_amend_options)(BlockDriverState *bs,
205 QEMUOptionParameter *options);
206
207 void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
208
209 /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */
210 int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event,
211 const char *tag);
212 int (*bdrv_debug_resume)(BlockDriverState *bs, const char *tag);
213 bool (*bdrv_debug_is_suspended)(BlockDriverState *bs, const char *tag);
214
215 /*
216 * Returns 1 if newly created images are guaranteed to contain only
217 * zeros, 0 otherwise.
218 */
219 int (*bdrv_has_zero_init)(BlockDriverState *bs);
220
221 QLIST_ENTRY(BlockDriver) list;
222 };
223
224 /*
225 * Note: the function bdrv_append() copies and swaps contents of
226 * BlockDriverStates, so if you add new fields to this struct, please
227 * inspect bdrv_append() to determine if the new fields need to be
228 * copied as well.
229 */
230 struct BlockDriverState {
231 int64_t total_sectors; /* if we are reading a disk image, give its
232 size in sectors */
233 int read_only; /* if true, the media is read only */
234 int open_flags; /* flags used to open the file, re-used for re-open */
235 int encrypted; /* if true, the media is encrypted */
236 int valid_key; /* if true, a valid encryption key has been set */
237 int sg; /* if true, the device is a /dev/sg* */
238 int copy_on_read; /* if true, copy read backing sectors into image
239 note this is a reference count */
240
241 BlockDriver *drv; /* NULL means no media */
242 void *opaque;
243
244 void *dev; /* attached device model, if any */
245 /* TODO change to DeviceState when all users are qdevified */
246 const BlockDevOps *dev_ops;
247 void *dev_opaque;
248
249 char filename[1024];
250 char backing_file[1024]; /* if non zero, the image is a diff of
251 this file image */
252 char backing_format[16]; /* if non-zero and backing_file exists */
253 int is_temporary;
254
255 BlockDriverState *backing_hd;
256 BlockDriverState *file;
257
258 NotifierList close_notifiers;
259
260 /* Callback before write request is processed */
261 NotifierWithReturnList before_write_notifiers;
262
263 /* number of in-flight copy-on-read requests */
264 unsigned int copy_on_read_in_flight;
265
266 /* I/O throttling */
267 ThrottleState throttle_state;
268 CoQueue throttled_reqs[2];
269 bool io_limits_enabled;
270
271 /* I/O stats (display with "info blockstats"). */
272 uint64_t nr_bytes[BDRV_MAX_IOTYPE];
273 uint64_t nr_ops[BDRV_MAX_IOTYPE];
274 uint64_t total_time_ns[BDRV_MAX_IOTYPE];
275 uint64_t wr_highest_sector;
276
277 /* Whether the disk can expand beyond total_sectors */
278 int growable;
279
280 /* Whether produces zeros when read beyond eof */
281 bool zero_beyond_eof;
282
283 /* the memory alignment required for the buffers handled by this driver */
284 int buffer_alignment;
285
286 /* do we need to tell the quest if we have a volatile write cache? */
287 int enable_write_cache;
288
289 /* NOTE: the following infos are only hints for real hardware
290 drivers. They are not used by the block driver */
291 BlockdevOnError on_read_error, on_write_error;
292 bool iostatus_enabled;
293 BlockDeviceIoStatus iostatus;
294 char device_name[32];
295 HBitmap *dirty_bitmap;
296 int refcnt;
297 int in_use; /* users other than guest access, eg. block migration */
298 QTAILQ_ENTRY(BlockDriverState) list;
299
300 QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
301
302 /* long-running background operation */
303 BlockJob *job;
304
305 QDict *options;
306 };
307
308 int get_tmp_filename(char *filename, int size);
309
310 void bdrv_set_io_limits(BlockDriverState *bs,
311 ThrottleConfig *cfg);
312
313
314 /**
315 * bdrv_add_before_write_notifier:
316 *
317 * Register a callback that is invoked before write requests are processed but
318 * after any throttling or waiting for overlapping requests.
319 */
320 void bdrv_add_before_write_notifier(BlockDriverState *bs,
321 NotifierWithReturn *notifier);
322
323 /**
324 * bdrv_get_aio_context:
325 *
326 * Returns: the currently bound #AioContext
327 */
328 AioContext *bdrv_get_aio_context(BlockDriverState *bs);
329
330 #ifdef _WIN32
331 int is_windows_drive(const char *filename);
332 #endif
333 void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
334 enum MonitorEvent ev,
335 BlockErrorAction action, bool is_read);
336
337 /**
338 * stream_start:
339 * @bs: Block device to operate on.
340 * @base: Block device that will become the new base, or %NULL to
341 * flatten the whole backing file chain onto @bs.
342 * @base_id: The file name that will be written to @bs as the new
343 * backing file if the job completes. Ignored if @base is %NULL.
344 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
345 * @on_error: The action to take upon error.
346 * @cb: Completion function for the job.
347 * @opaque: Opaque pointer value passed to @cb.
348 * @errp: Error object.
349 *
350 * Start a streaming operation on @bs. Clusters that are unallocated
351 * in @bs, but allocated in any image between @base and @bs (both
352 * exclusive) will be written to @bs. At the end of a successful
353 * streaming job, the backing file of @bs will be changed to
354 * @base_id in the written image and to @base in the live BlockDriverState.
355 */
356 void stream_start(BlockDriverState *bs, BlockDriverState *base,
357 const char *base_id, int64_t speed, BlockdevOnError on_error,
358 BlockDriverCompletionFunc *cb,
359 void *opaque, Error **errp);
360
361 /**
362 * commit_start:
363 * @bs: Top Block device
364 * @base: Block device that will be written into, and become the new top
365 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
366 * @on_error: The action to take upon error.
367 * @cb: Completion function for the job.
368 * @opaque: Opaque pointer value passed to @cb.
369 * @errp: Error object.
370 *
371 */
372 void commit_start(BlockDriverState *bs, BlockDriverState *base,
373 BlockDriverState *top, int64_t speed,
374 BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
375 void *opaque, Error **errp);
376
377 /*
378 * mirror_start:
379 * @bs: Block device to operate on.
380 * @target: Block device to write to.
381 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
382 * @granularity: The chosen granularity for the dirty bitmap.
383 * @buf_size: The amount of data that can be in flight at one time.
384 * @mode: Whether to collapse all images in the chain to the target.
385 * @on_source_error: The action to take upon error reading from the source.
386 * @on_target_error: The action to take upon error writing to the target.
387 * @cb: Completion function for the job.
388 * @opaque: Opaque pointer value passed to @cb.
389 * @errp: Error object.
390 *
391 * Start a mirroring operation on @bs. Clusters that are allocated
392 * in @bs will be written to @bs until the job is cancelled or
393 * manually completed. At the end of a successful mirroring job,
394 * @bs will be switched to read from @target.
395 */
396 void mirror_start(BlockDriverState *bs, BlockDriverState *target,
397 int64_t speed, int64_t granularity, int64_t buf_size,
398 MirrorSyncMode mode, BlockdevOnError on_source_error,
399 BlockdevOnError on_target_error,
400 BlockDriverCompletionFunc *cb,
401 void *opaque, Error **errp);
402
403 /*
404 * backup_start:
405 * @bs: Block device to operate on.
406 * @target: Block device to write to.
407 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
408 * @sync_mode: What parts of the disk image should be copied to the destination.
409 * @on_source_error: The action to take upon error reading from the source.
410 * @on_target_error: The action to take upon error writing to the target.
411 * @cb: Completion function for the job.
412 * @opaque: Opaque pointer value passed to @cb.
413 *
414 * Start a backup operation on @bs. Clusters in @bs are written to @target
415 * until the job is cancelled or manually completed.
416 */
417 void backup_start(BlockDriverState *bs, BlockDriverState *target,
418 int64_t speed, MirrorSyncMode sync_mode,
419 BlockdevOnError on_source_error,
420 BlockdevOnError on_target_error,
421 BlockDriverCompletionFunc *cb, void *opaque,
422 Error **errp);
423
424 #endif /* BLOCK_INT_H */