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.h"
28#include "qemu-option.h"
29#include "qemu-queue.h"
30#include "qemu-coroutine.h"
31#include "qemu-timer.h"
32#include "qapi-types.h"
33
34#define BLOCK_FLAG_ENCRYPT 1
35#define BLOCK_FLAG_COMPAT6 4
36
37#define BLOCK_IO_LIMIT_READ 0
38#define BLOCK_IO_LIMIT_WRITE 1
39#define BLOCK_IO_LIMIT_TOTAL 2
40
41#define BLOCK_IO_SLICE_TIME 100000000
42#define NANOSECONDS_PER_SECOND 1000000000.0
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
55typedef struct BdrvTrackedRequest BdrvTrackedRequest;
56
57typedef struct BlockIOLimit {
58 int64_t bps[3];
59 int64_t iops[3];
60} BlockIOLimit;
61
62typedef struct BlockIOBaseValue {
63 uint64_t bytes[2];
64 uint64_t ios[2];
65} BlockIOBaseValue;
66
67typedef struct BlockJob BlockJob;
68
69
70
71
72
73
74typedef struct BlockJobType {
75
76 size_t instance_size;
77
78
79 const char *job_type;
80
81
82 void (*set_speed)(BlockJob *job, int64_t speed, Error **errp);
83} BlockJobType;
84
85
86
87
88
89
90struct BlockJob {
91
92 const BlockJobType *job_type;
93
94
95 BlockDriverState *bs;
96
97
98
99
100
101 Coroutine *co;
102
103
104
105
106
107
108
109 bool cancelled;
110
111
112
113
114
115
116 bool busy;
117
118
119 int64_t offset;
120
121
122 int64_t len;
123
124
125 int64_t speed;
126
127
128 BlockDriverCompletionFunc *cb;
129
130
131 void *opaque;
132};
133
134struct BlockDriver {
135 const char *format_name;
136 int instance_size;
137 int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
138 int (*bdrv_probe_device)(const char *filename);
139 int (*bdrv_open)(BlockDriverState *bs, int flags);
140 int (*bdrv_file_open)(BlockDriverState *bs, const char *filename, int flags);
141 int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
142 uint8_t *buf, int nb_sectors);
143 int (*bdrv_write)(BlockDriverState *bs, int64_t sector_num,
144 const uint8_t *buf, int nb_sectors);
145 void (*bdrv_close)(BlockDriverState *bs);
146 void (*bdrv_rebind)(BlockDriverState *bs);
147 int (*bdrv_create)(const char *filename, QEMUOptionParameter *options);
148 int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
149 int (*bdrv_make_empty)(BlockDriverState *bs);
150
151 BlockDriverAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
152 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
153 BlockDriverCompletionFunc *cb, void *opaque);
154 BlockDriverAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
155 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
156 BlockDriverCompletionFunc *cb, void *opaque);
157 BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
158 BlockDriverCompletionFunc *cb, void *opaque);
159 BlockDriverAIOCB *(*bdrv_aio_discard)(BlockDriverState *bs,
160 int64_t sector_num, int nb_sectors,
161 BlockDriverCompletionFunc *cb, void *opaque);
162
163 int coroutine_fn (*bdrv_co_readv)(BlockDriverState *bs,
164 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
165 int coroutine_fn (*bdrv_co_writev)(BlockDriverState *bs,
166 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
167
168
169
170
171
172
173 int coroutine_fn (*bdrv_co_write_zeroes)(BlockDriverState *bs,
174 int64_t sector_num, int nb_sectors);
175 int coroutine_fn (*bdrv_co_discard)(BlockDriverState *bs,
176 int64_t sector_num, int nb_sectors);
177 int coroutine_fn (*bdrv_co_is_allocated)(BlockDriverState *bs,
178 int64_t sector_num, int nb_sectors, int *pnum);
179
180
181
182
183 void (*bdrv_invalidate_cache)(BlockDriverState *bs);
184
185
186
187
188
189 int coroutine_fn (*bdrv_co_flush_to_disk)(BlockDriverState *bs);
190
191
192
193
194
195
196 int coroutine_fn (*bdrv_co_flush_to_os)(BlockDriverState *bs);
197
198 const char *protocol_name;
199 int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset);
200 int64_t (*bdrv_getlength)(BlockDriverState *bs);
201 int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs);
202 int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
203 const uint8_t *buf, int nb_sectors);
204
205 int (*bdrv_snapshot_create)(BlockDriverState *bs,
206 QEMUSnapshotInfo *sn_info);
207 int (*bdrv_snapshot_goto)(BlockDriverState *bs,
208 const char *snapshot_id);
209 int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
210 int (*bdrv_snapshot_list)(BlockDriverState *bs,
211 QEMUSnapshotInfo **psn_info);
212 int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
213 const char *snapshot_name);
214 int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
215
216 int (*bdrv_save_vmstate)(BlockDriverState *bs, const uint8_t *buf,
217 int64_t pos, int size);
218 int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
219 int64_t pos, int size);
220
221 int (*bdrv_change_backing_file)(BlockDriverState *bs,
222 const char *backing_file, const char *backing_fmt);
223
224
225 int (*bdrv_is_inserted)(BlockDriverState *bs);
226 int (*bdrv_media_changed)(BlockDriverState *bs);
227 void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag);
228 void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);
229
230
231 int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
232 BlockDriverAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
233 unsigned long int req, void *buf,
234 BlockDriverCompletionFunc *cb, void *opaque);
235
236
237 QEMUOptionParameter *create_options;
238
239
240
241
242
243
244 int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result);
245
246 void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
247
248
249
250
251
252 int (*bdrv_has_zero_init)(BlockDriverState *bs);
253
254 QLIST_ENTRY(BlockDriver) list;
255};
256
257
258
259
260
261
262
263struct BlockDriverState {
264 int64_t total_sectors;
265
266 int read_only;
267 int keep_read_only;
268 int open_flags;
269 int encrypted;
270 int valid_key;
271 int sg;
272 int copy_on_read;
273
274
275 BlockDriver *drv;
276 void *opaque;
277
278 void *dev;
279
280 const BlockDevOps *dev_ops;
281 void *dev_opaque;
282
283 char filename[1024];
284 char backing_file[1024];
285
286 char backing_format[16];
287 int is_temporary;
288
289 BlockDriverState *backing_hd;
290 BlockDriverState *file;
291
292
293 unsigned int copy_on_read_in_flight;
294
295
296 int64_t slice_time;
297 int64_t slice_start;
298 int64_t slice_end;
299 BlockIOLimit io_limits;
300 BlockIOBaseValue io_base;
301 CoQueue throttled_reqs;
302 QEMUTimer *block_timer;
303 bool io_limits_enabled;
304
305
306 uint64_t nr_bytes[BDRV_MAX_IOTYPE];
307 uint64_t nr_ops[BDRV_MAX_IOTYPE];
308 uint64_t total_time_ns[BDRV_MAX_IOTYPE];
309 uint64_t wr_highest_sector;
310
311
312 int growable;
313
314
315 int buffer_alignment;
316
317
318 int enable_write_cache;
319
320
321
322 int cyls, heads, secs, translation;
323 BlockErrorAction on_read_error, on_write_error;
324 bool iostatus_enabled;
325 BlockDeviceIoStatus iostatus;
326 char device_name[32];
327 unsigned long *dirty_bitmap;
328 int64_t dirty_count;
329 int in_use;
330 QTAILQ_ENTRY(BlockDriverState) list;
331
332 QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
333
334
335 BlockJob *job;
336};
337
338int get_tmp_filename(char *filename, int size);
339
340void bdrv_set_io_limits(BlockDriverState *bs,
341 BlockIOLimit *io_limits);
342
343#ifdef _WIN32
344int is_windows_drive(const char *filename);
345#endif
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs,
366 int64_t speed, BlockDriverCompletionFunc *cb,
367 void *opaque, Error **errp);
368
369
370
371
372
373
374
375
376
377
378void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns);
379
380
381
382
383
384
385
386
387
388void block_job_complete(BlockJob *job, int ret);
389
390
391
392
393
394
395
396
397
398
399void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
400
401
402
403
404
405
406
407void block_job_cancel(BlockJob *job);
408
409
410
411
412
413
414
415bool block_job_is_cancelled(BlockJob *job);
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431int block_job_cancel_sync(BlockJob *job);
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451void stream_start(BlockDriverState *bs, BlockDriverState *base,
452 const char *base_id, int64_t speed,
453 BlockDriverCompletionFunc *cb,
454 void *opaque, Error **errp);
455
456#endif
457