1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
57typedef 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;
64 CoQueue wait_queue;
65} BdrvTrackedRequest;
66
67struct BlockDriver {
68 const char *format_name;
69 int instance_size;
70
71
72
73
74 ExtSnapshotPerm (*bdrv_check_ext_snapshot)(BlockDriverState *bs);
75
76 int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
77 int (*bdrv_probe_device)(const char *filename);
78
79
80
81 void (*bdrv_parse_filename)(const char *filename, QDict *options, Error **errp);
82
83
84
85
86
87 bool bdrv_needs_filename;
88
89
90 int (*bdrv_reopen_prepare)(BDRVReopenState *reopen_state,
91 BlockReopenQueue *queue, Error **errp);
92 void (*bdrv_reopen_commit)(BDRVReopenState *reopen_state);
93 void (*bdrv_reopen_abort)(BDRVReopenState *reopen_state);
94
95 int (*bdrv_open)(BlockDriverState *bs, QDict *options, int flags,
96 Error **errp);
97 int (*bdrv_file_open)(BlockDriverState *bs, QDict *options, int flags,
98 Error **errp);
99 int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
100 uint8_t *buf, int nb_sectors);
101 int (*bdrv_write)(BlockDriverState *bs, int64_t sector_num,
102 const uint8_t *buf, int nb_sectors);
103 void (*bdrv_close)(BlockDriverState *bs);
104 void (*bdrv_rebind)(BlockDriverState *bs);
105 int (*bdrv_create)(const char *filename, QEMUOptionParameter *options,
106 Error **errp);
107 int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
108 int (*bdrv_make_empty)(BlockDriverState *bs);
109
110 BlockDriverAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
111 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
112 BlockDriverCompletionFunc *cb, void *opaque);
113 BlockDriverAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
114 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
115 BlockDriverCompletionFunc *cb, void *opaque);
116 BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
117 BlockDriverCompletionFunc *cb, void *opaque);
118 BlockDriverAIOCB *(*bdrv_aio_discard)(BlockDriverState *bs,
119 int64_t sector_num, int nb_sectors,
120 BlockDriverCompletionFunc *cb, void *opaque);
121
122 int coroutine_fn (*bdrv_co_readv)(BlockDriverState *bs,
123 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
124 int coroutine_fn (*bdrv_co_writev)(BlockDriverState *bs,
125 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
126
127
128
129
130
131
132 int coroutine_fn (*bdrv_co_write_zeroes)(BlockDriverState *bs,
133 int64_t sector_num, int nb_sectors);
134 int coroutine_fn (*bdrv_co_discard)(BlockDriverState *bs,
135 int64_t sector_num, int nb_sectors);
136 int64_t coroutine_fn (*bdrv_co_get_block_status)(BlockDriverState *bs,
137 int64_t sector_num, int nb_sectors, int *pnum);
138
139
140
141
142 void (*bdrv_invalidate_cache)(BlockDriverState *bs);
143
144
145
146
147
148 int coroutine_fn (*bdrv_co_flush_to_disk)(BlockDriverState *bs);
149
150
151
152
153
154
155 int coroutine_fn (*bdrv_co_flush_to_os)(BlockDriverState *bs);
156
157 const char *protocol_name;
158 int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset);
159
160 int64_t (*bdrv_getlength)(BlockDriverState *bs);
161 bool has_variable_length;
162 int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs);
163
164 int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
165 const uint8_t *buf, int nb_sectors);
166
167 int (*bdrv_snapshot_create)(BlockDriverState *bs,
168 QEMUSnapshotInfo *sn_info);
169 int (*bdrv_snapshot_goto)(BlockDriverState *bs,
170 const char *snapshot_id);
171 int (*bdrv_snapshot_delete)(BlockDriverState *bs,
172 const char *snapshot_id,
173 const char *name,
174 Error **errp);
175 int (*bdrv_snapshot_list)(BlockDriverState *bs,
176 QEMUSnapshotInfo **psn_info);
177 int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
178 const char *snapshot_name);
179 int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
180 ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
181
182 int (*bdrv_save_vmstate)(BlockDriverState *bs, QEMUIOVector *qiov,
183 int64_t pos);
184 int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
185 int64_t pos, int size);
186
187 int (*bdrv_change_backing_file)(BlockDriverState *bs,
188 const char *backing_file, const char *backing_fmt);
189
190
191 int (*bdrv_is_inserted)(BlockDriverState *bs);
192 int (*bdrv_media_changed)(BlockDriverState *bs);
193 void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag);
194 void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);
195
196
197 int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
198 BlockDriverAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
199 unsigned long int req, void *buf,
200 BlockDriverCompletionFunc *cb, void *opaque);
201
202
203 QEMUOptionParameter *create_options;
204
205
206
207
208
209
210 int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result,
211 BdrvCheckMode fix);
212
213 int (*bdrv_amend_options)(BlockDriverState *bs,
214 QEMUOptionParameter *options);
215
216 void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
217
218
219 int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event,
220 const char *tag);
221 int (*bdrv_debug_resume)(BlockDriverState *bs, const char *tag);
222 bool (*bdrv_debug_is_suspended)(BlockDriverState *bs, const char *tag);
223
224
225
226
227
228 int (*bdrv_has_zero_init)(BlockDriverState *bs);
229
230 QLIST_ENTRY(BlockDriver) list;
231};
232
233
234
235
236
237
238
239struct BlockDriverState {
240 int64_t total_sectors;
241
242 int read_only;
243 int open_flags;
244 int encrypted;
245 int valid_key;
246 int sg;
247 int copy_on_read;
248
249
250 BlockDriver *drv;
251 void *opaque;
252
253 void *dev;
254
255 const BlockDevOps *dev_ops;
256 void *dev_opaque;
257
258 char filename[1024];
259 char backing_file[1024];
260
261 char backing_format[16];
262 int is_temporary;
263
264 BlockDriverState *backing_hd;
265 BlockDriverState *file;
266
267 NotifierList close_notifiers;
268
269
270 NotifierWithReturnList before_write_notifiers;
271
272
273 unsigned int copy_on_read_in_flight;
274
275
276 ThrottleState throttle_state;
277 CoQueue throttled_reqs[2];
278 bool io_limits_enabled;
279
280
281 uint64_t nr_bytes[BDRV_MAX_IOTYPE];
282 uint64_t nr_ops[BDRV_MAX_IOTYPE];
283 uint64_t total_time_ns[BDRV_MAX_IOTYPE];
284 uint64_t wr_highest_sector;
285
286
287 int growable;
288
289
290 bool zero_beyond_eof;
291
292
293 int buffer_alignment;
294
295
296 int enable_write_cache;
297
298
299
300 BlockdevOnError on_read_error, on_write_error;
301 bool iostatus_enabled;
302 BlockDeviceIoStatus iostatus;
303 char device_name[32];
304 HBitmap *dirty_bitmap;
305 int refcnt;
306 int in_use;
307 QTAILQ_ENTRY(BlockDriverState) list;
308
309 QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
310
311
312 BlockJob *job;
313
314 QDict *options;
315};
316
317int get_tmp_filename(char *filename, int size);
318
319void bdrv_set_io_limits(BlockDriverState *bs,
320 ThrottleConfig *cfg);
321
322
323
324
325
326
327
328
329void bdrv_add_before_write_notifier(BlockDriverState *bs,
330 NotifierWithReturn *notifier);
331
332
333
334
335
336
337AioContext *bdrv_get_aio_context(BlockDriverState *bs);
338
339#ifdef _WIN32
340int is_windows_drive(const char *filename);
341#endif
342void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
343 enum MonitorEvent ev,
344 BlockErrorAction action, bool is_read);
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365void stream_start(BlockDriverState *bs, BlockDriverState *base,
366 const char *base_id, int64_t speed, BlockdevOnError on_error,
367 BlockDriverCompletionFunc *cb,
368 void *opaque, Error **errp);
369
370
371
372
373
374
375
376
377
378
379
380
381void commit_start(BlockDriverState *bs, BlockDriverState *base,
382 BlockDriverState *top, int64_t speed,
383 BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
384 void *opaque, Error **errp);
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405void mirror_start(BlockDriverState *bs, BlockDriverState *target,
406 int64_t speed, int64_t granularity, int64_t buf_size,
407 MirrorSyncMode mode, BlockdevOnError on_source_error,
408 BlockdevOnError on_target_error,
409 BlockDriverCompletionFunc *cb,
410 void *opaque, Error **errp);
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426void backup_start(BlockDriverState *bs, BlockDriverState *target,
427 int64_t speed, MirrorSyncMode sync_mode,
428 BlockdevOnError on_source_error,
429 BlockdevOnError on_target_error,
430 BlockDriverCompletionFunc *cb, void *opaque,
431 Error **errp);
432
433#endif
434