1#ifndef BLOCK_H
2#define BLOCK_H
3
4#include "block/aio.h"
5#include "qemu-common.h"
6#include "qemu/option.h"
7#include "block/coroutine.h"
8#include "qapi/qmp/qobject.h"
9#include "qapi-types.h"
10
11
12typedef struct BlockDriver BlockDriver;
13typedef struct BlockJob BlockJob;
14
15typedef struct BlockDriverInfo {
16
17 int cluster_size;
18
19 int64_t vm_state_offset;
20 bool is_dirty;
21
22
23
24
25 bool unallocated_blocks_are_zero;
26
27
28
29
30
31
32
33
34
35
36 bool can_write_zeroes_with_unmap;
37} BlockDriverInfo;
38
39typedef struct BlockFragInfo {
40 uint64_t allocated_clusters;
41 uint64_t total_clusters;
42 uint64_t fragmented_clusters;
43 uint64_t compressed_clusters;
44} BlockFragInfo;
45
46
47typedef struct BlockDevOps {
48
49
50
51
52
53
54
55 void (*change_media_cb)(void *opaque, bool load);
56
57
58
59
60
61
62
63
64 void (*eject_request_cb)(void *opaque, bool force);
65
66
67
68
69 bool (*is_tray_open)(void *opaque);
70
71
72
73
74 bool (*is_medium_locked)(void *opaque);
75
76
77
78 void (*resize_cb)(void *opaque);
79} BlockDevOps;
80
81typedef enum {
82 BDRV_REQ_COPY_ON_READ = 0x1,
83 BDRV_REQ_ZERO_WRITE = 0x2,
84
85
86
87
88
89
90 BDRV_REQ_MAY_UNMAP = 0x4,
91} BdrvRequestFlags;
92
93#define BDRV_O_RDWR 0x0002
94#define BDRV_O_SNAPSHOT 0x0008
95#define BDRV_O_NOCACHE 0x0020
96#define BDRV_O_CACHE_WB 0x0040
97#define BDRV_O_NATIVE_AIO 0x0080
98#define BDRV_O_NO_BACKING 0x0100
99#define BDRV_O_NO_FLUSH 0x0200
100#define BDRV_O_COPY_ON_READ 0x0400
101#define BDRV_O_INCOMING 0x0800
102#define BDRV_O_CHECK 0x1000
103#define BDRV_O_ALLOW_RDWR 0x2000
104#define BDRV_O_UNMAP 0x4000
105#define BDRV_O_PROTOCOL 0x8000
106
107
108
109#define BDRV_O_CACHE_MASK (BDRV_O_NOCACHE | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH)
110
111#define BDRV_SECTOR_BITS 9
112#define BDRV_SECTOR_SIZE (1ULL << BDRV_SECTOR_BITS)
113#define BDRV_SECTOR_MASK ~(BDRV_SECTOR_SIZE - 1)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139#define BDRV_BLOCK_DATA 1
140#define BDRV_BLOCK_ZERO 2
141#define BDRV_BLOCK_OFFSET_VALID 4
142#define BDRV_BLOCK_RAW 8
143#define BDRV_BLOCK_OFFSET_MASK BDRV_SECTOR_MASK
144
145typedef enum {
146 BDRV_ACTION_REPORT, BDRV_ACTION_IGNORE, BDRV_ACTION_STOP
147} BlockErrorAction;
148
149typedef QSIMPLEQ_HEAD(BlockReopenQueue, BlockReopenQueueEntry) BlockReopenQueue;
150
151typedef struct BDRVReopenState {
152 BlockDriverState *bs;
153 int flags;
154 void *opaque;
155} BDRVReopenState;
156
157
158void bdrv_iostatus_enable(BlockDriverState *bs);
159void bdrv_iostatus_reset(BlockDriverState *bs);
160void bdrv_iostatus_disable(BlockDriverState *bs);
161bool bdrv_iostatus_is_enabled(const BlockDriverState *bs);
162void bdrv_iostatus_set_err(BlockDriverState *bs, int error);
163void bdrv_info_print(Monitor *mon, const QObject *data);
164void bdrv_info(Monitor *mon, QObject **ret_data);
165void bdrv_stats_print(Monitor *mon, const QObject *data);
166void bdrv_info_stats(Monitor *mon, QObject **ret_data);
167
168
169void bdrv_io_limits_enable(BlockDriverState *bs);
170void bdrv_io_limits_disable(BlockDriverState *bs);
171
172void bdrv_init(void);
173void bdrv_init_with_whitelist(void);
174BlockDriver *bdrv_find_protocol(const char *filename,
175 bool allow_protocol_prefix);
176BlockDriver *bdrv_find_format(const char *format_name);
177BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
178 bool readonly);
179int bdrv_create(BlockDriver *drv, const char* filename,
180 QEMUOptionParameter *options, Error **errp);
181int bdrv_create_file(const char* filename, QEMUOptionParameter *options,
182 Error **errp);
183BlockDriverState *bdrv_new(const char *device_name);
184void bdrv_make_anon(BlockDriverState *bs);
185void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old);
186void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top);
187int bdrv_parse_cache_flags(const char *mode, int *flags);
188int bdrv_parse_discard_flags(const char *mode, int *flags);
189int bdrv_open_image(BlockDriverState **pbs, const char *filename,
190 QDict *options, const char *bdref_key, int flags,
191 bool allow_none, Error **errp);
192int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
193void bdrv_append_temp_snapshot(BlockDriverState *bs, Error **errp);
194int bdrv_open(BlockDriverState **pbs, const char *filename,
195 const char *reference, QDict *options, int flags,
196 BlockDriver *drv, Error **errp);
197BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
198 BlockDriverState *bs, int flags);
199int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp);
200int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp);
201int bdrv_reopen_prepare(BDRVReopenState *reopen_state,
202 BlockReopenQueue *queue, Error **errp);
203void bdrv_reopen_commit(BDRVReopenState *reopen_state);
204void bdrv_reopen_abort(BDRVReopenState *reopen_state);
205void bdrv_close(BlockDriverState *bs);
206void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify);
207int bdrv_attach_dev(BlockDriverState *bs, void *dev);
208void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev);
209void bdrv_detach_dev(BlockDriverState *bs, void *dev);
210void *bdrv_get_attached_dev(BlockDriverState *bs);
211void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
212 void *opaque);
213void bdrv_dev_eject_request(BlockDriverState *bs, bool force);
214bool bdrv_dev_has_removable_media(BlockDriverState *bs);
215bool bdrv_dev_is_tray_open(BlockDriverState *bs);
216bool bdrv_dev_is_medium_locked(BlockDriverState *bs);
217int bdrv_read(BlockDriverState *bs, int64_t sector_num,
218 uint8_t *buf, int nb_sectors);
219int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
220 uint8_t *buf, int nb_sectors);
221int bdrv_write(BlockDriverState *bs, int64_t sector_num,
222 const uint8_t *buf, int nb_sectors);
223int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
224 int nb_sectors, BdrvRequestFlags flags);
225BlockDriverAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs, int64_t sector_num,
226 int nb_sectors, BdrvRequestFlags flags,
227 BlockDriverCompletionFunc *cb, void *opaque);
228int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags);
229int bdrv_pread(BlockDriverState *bs, int64_t offset,
230 void *buf, int count);
231int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
232 const void *buf, int count);
233int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov);
234int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
235 const void *buf, int count);
236int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
237 int nb_sectors, QEMUIOVector *qiov);
238int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
239 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
240int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
241 int nb_sectors, QEMUIOVector *qiov);
242
243
244
245
246
247
248int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
249 int nb_sectors, BdrvRequestFlags flags);
250BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
251 const char *backing_file);
252int bdrv_get_backing_file_depth(BlockDriverState *bs);
253int bdrv_truncate(BlockDriverState *bs, int64_t offset);
254int64_t bdrv_getlength(BlockDriverState *bs);
255int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
256void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr);
257int bdrv_refresh_limits(BlockDriverState *bs);
258int bdrv_commit(BlockDriverState *bs);
259int bdrv_commit_all(void);
260int bdrv_change_backing_file(BlockDriverState *bs,
261 const char *backing_file, const char *backing_fmt);
262void bdrv_register(BlockDriver *bdrv);
263int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
264 BlockDriverState *base);
265BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
266 BlockDriverState *bs);
267BlockDriverState *bdrv_find_base(BlockDriverState *bs);
268
269
270typedef struct BdrvCheckResult {
271 int corruptions;
272 int leaks;
273 int check_errors;
274 int corruptions_fixed;
275 int leaks_fixed;
276 int64_t image_end_offset;
277 BlockFragInfo bfi;
278} BdrvCheckResult;
279
280typedef enum {
281 BDRV_FIX_LEAKS = 1,
282 BDRV_FIX_ERRORS = 2,
283} BdrvCheckMode;
284
285int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix);
286
287int bdrv_amend_options(BlockDriverState *bs_new, QEMUOptionParameter *options);
288
289
290bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
291 BlockDriverState *candidate);
292bool bdrv_is_first_non_filter(BlockDriverState *candidate);
293
294
295typedef void BlockDriverDirtyHandler(BlockDriverState *bs, int64_t sector,
296 int sector_num);
297BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
298 QEMUIOVector *iov, int nb_sectors,
299 BlockDriverCompletionFunc *cb, void *opaque);
300BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
301 QEMUIOVector *iov, int nb_sectors,
302 BlockDriverCompletionFunc *cb, void *opaque);
303BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
304 BlockDriverCompletionFunc *cb, void *opaque);
305BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs,
306 int64_t sector_num, int nb_sectors,
307 BlockDriverCompletionFunc *cb, void *opaque);
308void bdrv_aio_cancel(BlockDriverAIOCB *acb);
309
310typedef struct BlockRequest {
311
312 int64_t sector;
313 int nb_sectors;
314 int flags;
315 QEMUIOVector *qiov;
316 BlockDriverCompletionFunc *cb;
317 void *opaque;
318
319
320 int error;
321} BlockRequest;
322
323int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs,
324 int num_reqs);
325
326
327int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf);
328BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
329 unsigned long int req, void *buf,
330 BlockDriverCompletionFunc *cb, void *opaque);
331
332
333void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp);
334void bdrv_invalidate_cache_all(Error **errp);
335
336void bdrv_clear_incoming_migration_all(void);
337
338
339int bdrv_flush(BlockDriverState *bs);
340int coroutine_fn bdrv_co_flush(BlockDriverState *bs);
341int bdrv_flush_all(void);
342void bdrv_close_all(void);
343void bdrv_drain_all(void);
344
345int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
346int bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
347int bdrv_has_zero_init_1(BlockDriverState *bs);
348int bdrv_has_zero_init(BlockDriverState *bs);
349bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs);
350bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs);
351int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num,
352 int nb_sectors, int *pnum);
353int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
354 int *pnum);
355int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
356 int64_t sector_num, int nb_sectors, int *pnum);
357
358void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error,
359 BlockdevOnError on_write_error);
360BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read);
361BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error);
362void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action,
363 bool is_read, int error);
364int bdrv_is_read_only(BlockDriverState *bs);
365int bdrv_is_sg(BlockDriverState *bs);
366int bdrv_enable_write_cache(BlockDriverState *bs);
367void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce);
368int bdrv_is_inserted(BlockDriverState *bs);
369int bdrv_media_changed(BlockDriverState *bs);
370void bdrv_lock_medium(BlockDriverState *bs, bool locked);
371void bdrv_eject(BlockDriverState *bs, bool eject_flag);
372const char *bdrv_get_format_name(BlockDriverState *bs);
373BlockDriverState *bdrv_find(const char *name);
374BlockDriverState *bdrv_find_node(const char *node_name);
375BlockDeviceInfoList *bdrv_named_nodes_list(void);
376BlockDriverState *bdrv_lookup_bs(const char *device,
377 const char *node_name,
378 Error **errp);
379BlockDriverState *bdrv_next(BlockDriverState *bs);
380void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs),
381 void *opaque);
382int bdrv_is_encrypted(BlockDriverState *bs);
383int bdrv_key_required(BlockDriverState *bs);
384int bdrv_set_key(BlockDriverState *bs, const char *key);
385int bdrv_query_missing_keys(void);
386void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
387 void *opaque);
388const char *bdrv_get_device_name(BlockDriverState *bs);
389int bdrv_get_flags(BlockDriverState *bs);
390int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
391 const uint8_t *buf, int nb_sectors);
392int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
393ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs);
394void bdrv_round_to_clusters(BlockDriverState *bs,
395 int64_t sector_num, int nb_sectors,
396 int64_t *cluster_sector_num,
397 int *cluster_nb_sectors);
398
399const char *bdrv_get_encrypted_filename(BlockDriverState *bs);
400void bdrv_get_backing_filename(BlockDriverState *bs,
401 char *filename, int filename_size);
402void bdrv_get_full_backing_filename(BlockDriverState *bs,
403 char *dest, size_t sz);
404int bdrv_is_snapshot(BlockDriverState *bs);
405
406int path_is_absolute(const char *path);
407void path_combine(char *dest, int dest_size,
408 const char *base_path,
409 const char *filename);
410
411int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
412int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
413 int64_t pos, int size);
414
415int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
416 int64_t pos, int size);
417
418void bdrv_img_create(const char *filename, const char *fmt,
419 const char *base_filename, const char *base_fmt,
420 char *options, uint64_t img_size, int flags,
421 Error **errp, bool quiet);
422
423
424
425size_t bdrv_opt_mem_align(BlockDriverState *bs);
426void bdrv_set_guest_block_size(BlockDriverState *bs, int align);
427void *qemu_blockalign(BlockDriverState *bs, size_t size);
428bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov);
429
430struct HBitmapIter;
431typedef struct BdrvDirtyBitmap BdrvDirtyBitmap;
432BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity);
433void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap);
434BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs);
435int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector);
436void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors);
437void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors);
438void bdrv_dirty_iter_init(BlockDriverState *bs,
439 BdrvDirtyBitmap *bitmap, struct HBitmapIter *hbi);
440int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap);
441
442void bdrv_enable_copy_on_read(BlockDriverState *bs);
443void bdrv_disable_copy_on_read(BlockDriverState *bs);
444
445void bdrv_ref(BlockDriverState *bs);
446void bdrv_unref(BlockDriverState *bs);
447void bdrv_set_in_use(BlockDriverState *bs, int in_use);
448int bdrv_in_use(BlockDriverState *bs);
449
450#ifdef CONFIG_LINUX_AIO
451int raw_get_aio_fd(BlockDriverState *bs);
452#else
453static inline int raw_get_aio_fd(BlockDriverState *bs)
454{
455 return -ENOTSUP;
456}
457#endif
458
459enum BlockAcctType {
460 BDRV_ACCT_READ,
461 BDRV_ACCT_WRITE,
462 BDRV_ACCT_FLUSH,
463 BDRV_MAX_IOTYPE,
464};
465
466typedef struct BlockAcctCookie {
467 int64_t bytes;
468 int64_t start_time_ns;
469 enum BlockAcctType type;
470} BlockAcctCookie;
471
472void bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
473 int64_t bytes, enum BlockAcctType type);
474void bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie);
475
476typedef enum {
477 BLKDBG_L1_UPDATE,
478
479 BLKDBG_L1_GROW_ALLOC_TABLE,
480 BLKDBG_L1_GROW_WRITE_TABLE,
481 BLKDBG_L1_GROW_ACTIVATE_TABLE,
482
483 BLKDBG_L2_LOAD,
484 BLKDBG_L2_UPDATE,
485 BLKDBG_L2_UPDATE_COMPRESSED,
486 BLKDBG_L2_ALLOC_COW_READ,
487 BLKDBG_L2_ALLOC_WRITE,
488
489 BLKDBG_READ_AIO,
490 BLKDBG_READ_BACKING_AIO,
491 BLKDBG_READ_COMPRESSED,
492
493 BLKDBG_WRITE_AIO,
494 BLKDBG_WRITE_COMPRESSED,
495
496 BLKDBG_VMSTATE_LOAD,
497 BLKDBG_VMSTATE_SAVE,
498
499 BLKDBG_COW_READ,
500 BLKDBG_COW_WRITE,
501
502 BLKDBG_REFTABLE_LOAD,
503 BLKDBG_REFTABLE_GROW,
504 BLKDBG_REFTABLE_UPDATE,
505
506 BLKDBG_REFBLOCK_LOAD,
507 BLKDBG_REFBLOCK_UPDATE,
508 BLKDBG_REFBLOCK_UPDATE_PART,
509 BLKDBG_REFBLOCK_ALLOC,
510 BLKDBG_REFBLOCK_ALLOC_HOOKUP,
511 BLKDBG_REFBLOCK_ALLOC_WRITE,
512 BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS,
513 BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE,
514 BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE,
515
516 BLKDBG_CLUSTER_ALLOC,
517 BLKDBG_CLUSTER_ALLOC_BYTES,
518 BLKDBG_CLUSTER_FREE,
519
520 BLKDBG_FLUSH_TO_OS,
521 BLKDBG_FLUSH_TO_DISK,
522
523 BLKDBG_PWRITEV_RMW_HEAD,
524 BLKDBG_PWRITEV_RMW_AFTER_HEAD,
525 BLKDBG_PWRITEV_RMW_TAIL,
526 BLKDBG_PWRITEV_RMW_AFTER_TAIL,
527 BLKDBG_PWRITEV,
528 BLKDBG_PWRITEV_ZERO,
529 BLKDBG_PWRITEV_DONE,
530
531 BLKDBG_EVENT_MAX,
532} BlkDebugEvent;
533
534#define BLKDBG_EVENT(bs, evt) bdrv_debug_event(bs, evt)
535void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event);
536
537int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
538 const char *tag);
539int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag);
540int bdrv_debug_resume(BlockDriverState *bs, const char *tag);
541bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag);
542
543#endif
544