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/accounting.h"
28#include "block/block.h"
29#include "qemu/option.h"
30#include "qemu/queue.h"
31#include "qemu/coroutine.h"
32#include "qemu/timer.h"
33#include "qapi-types.h"
34#include "qemu/hbitmap.h"
35#include "block/snapshot.h"
36#include "qemu/main-loop.h"
37#include "qemu/throttle.h"
38
39#define BLOCK_FLAG_ENCRYPT 1
40#define BLOCK_FLAG_LAZY_REFCOUNTS 8
41
42#define BLOCK_OPT_SIZE "size"
43#define BLOCK_OPT_ENCRYPT "encryption"
44#define BLOCK_OPT_COMPAT6 "compat6"
45#define BLOCK_OPT_HWVERSION "hwversion"
46#define BLOCK_OPT_BACKING_FILE "backing_file"
47#define BLOCK_OPT_BACKING_FMT "backing_fmt"
48#define BLOCK_OPT_CLUSTER_SIZE "cluster_size"
49#define BLOCK_OPT_TABLE_SIZE "table_size"
50#define BLOCK_OPT_PREALLOC "preallocation"
51#define BLOCK_OPT_SUBFMT "subformat"
52#define BLOCK_OPT_COMPAT_LEVEL "compat"
53#define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts"
54#define BLOCK_OPT_ADAPTER_TYPE "adapter_type"
55#define BLOCK_OPT_REDUNDANCY "redundancy"
56#define BLOCK_OPT_NOCOW "nocow"
57#define BLOCK_OPT_OBJECT_SIZE "object_size"
58#define BLOCK_OPT_REFCOUNT_BITS "refcount_bits"
59
60#define BLOCK_PROBE_BUF_SIZE 512
61
62enum BdrvTrackedRequestType {
63 BDRV_TRACKED_READ,
64 BDRV_TRACKED_WRITE,
65 BDRV_TRACKED_DISCARD,
66};
67
68typedef struct BdrvTrackedRequest {
69 BlockDriverState *bs;
70 int64_t offset;
71 unsigned int bytes;
72 enum BdrvTrackedRequestType type;
73
74 bool serialising;
75 int64_t overlap_offset;
76 unsigned int overlap_bytes;
77
78 QLIST_ENTRY(BdrvTrackedRequest) list;
79 Coroutine *co;
80 CoQueue wait_queue;
81
82 struct BdrvTrackedRequest *waiting_for;
83} BdrvTrackedRequest;
84
85struct BlockDriver {
86 const char *format_name;
87 int instance_size;
88
89
90 bool is_filter;
91
92
93
94
95
96
97 bool (*bdrv_recurse_is_first_non_filter)(BlockDriverState *bs,
98 BlockDriverState *candidate);
99
100 int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
101 int (*bdrv_probe_device)(const char *filename);
102
103
104
105 void (*bdrv_parse_filename)(const char *filename, QDict *options, Error **errp);
106
107
108
109
110
111 bool bdrv_needs_filename;
112
113
114 bool supports_backing;
115
116
117 int (*bdrv_reopen_prepare)(BDRVReopenState *reopen_state,
118 BlockReopenQueue *queue, Error **errp);
119 void (*bdrv_reopen_commit)(BDRVReopenState *reopen_state);
120 void (*bdrv_reopen_abort)(BDRVReopenState *reopen_state);
121 void (*bdrv_join_options)(QDict *options, QDict *old_options);
122
123 int (*bdrv_open)(BlockDriverState *bs, QDict *options, int flags,
124 Error **errp);
125 int (*bdrv_file_open)(BlockDriverState *bs, QDict *options, int flags,
126 Error **errp);
127 void (*bdrv_close)(BlockDriverState *bs);
128 int (*bdrv_create)(const char *filename, QemuOpts *opts, Error **errp);
129 int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
130 int (*bdrv_make_empty)(BlockDriverState *bs);
131
132 void (*bdrv_refresh_filename)(BlockDriverState *bs, QDict *options);
133
134
135 BlockAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
136 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
137 BlockCompletionFunc *cb, void *opaque);
138 BlockAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
139 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
140 BlockCompletionFunc *cb, void *opaque);
141 BlockAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
142 BlockCompletionFunc *cb, void *opaque);
143 BlockAIOCB *(*bdrv_aio_pdiscard)(BlockDriverState *bs,
144 int64_t offset, int count,
145 BlockCompletionFunc *cb, void *opaque);
146
147 int coroutine_fn (*bdrv_co_readv)(BlockDriverState *bs,
148 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
149 int coroutine_fn (*bdrv_co_preadv)(BlockDriverState *bs,
150 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags);
151 int coroutine_fn (*bdrv_co_writev)(BlockDriverState *bs,
152 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
153 int coroutine_fn (*bdrv_co_writev_flags)(BlockDriverState *bs,
154 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int flags);
155 int coroutine_fn (*bdrv_co_pwritev)(BlockDriverState *bs,
156 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags);
157
158
159
160
161
162
163
164 int coroutine_fn (*bdrv_co_pwrite_zeroes)(BlockDriverState *bs,
165 int64_t offset, int count, BdrvRequestFlags flags);
166 int coroutine_fn (*bdrv_co_pdiscard)(BlockDriverState *bs,
167 int64_t offset, int count);
168 int64_t coroutine_fn (*bdrv_co_get_block_status)(BlockDriverState *bs,
169 int64_t sector_num, int nb_sectors, int *pnum,
170 BlockDriverState **file);
171
172
173
174
175 void (*bdrv_invalidate_cache)(BlockDriverState *bs, Error **errp);
176 int (*bdrv_inactivate)(BlockDriverState *bs);
177
178
179
180
181
182
183 int coroutine_fn (*bdrv_co_flush)(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
201 int64_t (*bdrv_getlength)(BlockDriverState *bs);
202 bool has_variable_length;
203 int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs);
204
205 int coroutine_fn (*bdrv_co_pwritev_compressed)(BlockDriverState *bs,
206 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov);
207
208 int (*bdrv_snapshot_create)(BlockDriverState *bs,
209 QEMUSnapshotInfo *sn_info);
210 int (*bdrv_snapshot_goto)(BlockDriverState *bs,
211 const char *snapshot_id);
212 int (*bdrv_snapshot_delete)(BlockDriverState *bs,
213 const char *snapshot_id,
214 const char *name,
215 Error **errp);
216 int (*bdrv_snapshot_list)(BlockDriverState *bs,
217 QEMUSnapshotInfo **psn_info);
218 int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
219 const char *snapshot_id,
220 const char *name,
221 Error **errp);
222 int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
223 ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
224
225 int coroutine_fn (*bdrv_save_vmstate)(BlockDriverState *bs,
226 QEMUIOVector *qiov,
227 int64_t pos);
228 int coroutine_fn (*bdrv_load_vmstate)(BlockDriverState *bs,
229 QEMUIOVector *qiov,
230 int64_t pos);
231
232 int (*bdrv_change_backing_file)(BlockDriverState *bs,
233 const char *backing_file, const char *backing_fmt);
234
235
236 bool (*bdrv_is_inserted)(BlockDriverState *bs);
237 int (*bdrv_media_changed)(BlockDriverState *bs);
238 void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag);
239 void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);
240
241
242 BlockAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
243 unsigned long int req, void *buf,
244 BlockCompletionFunc *cb, void *opaque);
245 int coroutine_fn (*bdrv_co_ioctl)(BlockDriverState *bs,
246 unsigned long int req, void *buf);
247
248
249 QemuOptsList *create_opts;
250
251
252
253
254
255 int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result,
256 BdrvCheckMode fix);
257
258 int (*bdrv_amend_options)(BlockDriverState *bs, QemuOpts *opts,
259 BlockDriverAmendStatusCB *status_cb,
260 void *cb_opaque);
261
262 void (*bdrv_debug_event)(BlockDriverState *bs, BlkdebugEvent event);
263
264
265 int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event,
266 const char *tag);
267 int (*bdrv_debug_remove_breakpoint)(BlockDriverState *bs,
268 const char *tag);
269 int (*bdrv_debug_resume)(BlockDriverState *bs, const char *tag);
270 bool (*bdrv_debug_is_suspended)(BlockDriverState *bs, const char *tag);
271
272 void (*bdrv_refresh_limits)(BlockDriverState *bs, Error **errp);
273
274
275
276
277
278 int (*bdrv_has_zero_init)(BlockDriverState *bs);
279
280
281
282
283
284 void (*bdrv_detach_aio_context)(BlockDriverState *bs);
285
286
287
288
289
290 void (*bdrv_attach_aio_context)(BlockDriverState *bs,
291 AioContext *new_context);
292
293
294 void (*bdrv_io_plug)(BlockDriverState *bs);
295 void (*bdrv_io_unplug)(BlockDriverState *bs);
296
297
298
299
300
301
302 int (*bdrv_probe_blocksizes)(BlockDriverState *bs, BlockSizes *bsz);
303
304
305
306
307
308
309
310 int (*bdrv_probe_geometry)(BlockDriverState *bs, HDGeometry *geo);
311
312
313
314
315
316 void (*bdrv_drain)(BlockDriverState *bs);
317
318 void (*bdrv_add_child)(BlockDriverState *parent, BlockDriverState *child,
319 Error **errp);
320 void (*bdrv_del_child)(BlockDriverState *parent, BdrvChild *child,
321 Error **errp);
322
323 QLIST_ENTRY(BlockDriver) list;
324};
325
326typedef struct BlockLimits {
327
328
329
330
331 uint32_t request_alignment;
332
333
334
335
336
337 int32_t max_pdiscard;
338
339
340
341
342
343 uint32_t pdiscard_alignment;
344
345
346
347
348 int32_t max_pwrite_zeroes;
349
350
351
352
353
354
355 uint32_t pwrite_zeroes_alignment;
356
357
358
359
360 uint32_t opt_transfer;
361
362
363
364
365
366 uint32_t max_transfer;
367
368
369 size_t min_mem_alignment;
370
371
372 size_t opt_mem_alignment;
373
374
375 int max_iov;
376} BlockLimits;
377
378typedef struct BdrvOpBlocker BdrvOpBlocker;
379
380typedef struct BdrvAioNotifier {
381 void (*attached_aio_context)(AioContext *new_context, void *opaque);
382 void (*detach_aio_context)(void *opaque);
383
384 void *opaque;
385 bool deleted;
386
387 QLIST_ENTRY(BdrvAioNotifier) list;
388} BdrvAioNotifier;
389
390struct BdrvChildRole {
391 void (*inherit_options)(int *child_flags, QDict *child_options,
392 int parent_flags, QDict *parent_options);
393
394 void (*change_media)(BdrvChild *child, bool load);
395 void (*resize)(BdrvChild *child);
396
397
398
399
400 const char* (*get_name)(BdrvChild *child);
401
402
403
404
405
406
407
408
409
410 void (*drained_begin)(BdrvChild *child);
411 void (*drained_end)(BdrvChild *child);
412};
413
414extern const BdrvChildRole child_file;
415extern const BdrvChildRole child_format;
416
417struct BdrvChild {
418 BlockDriverState *bs;
419 char *name;
420 const BdrvChildRole *role;
421 void *opaque;
422 QLIST_ENTRY(BdrvChild) next;
423 QLIST_ENTRY(BdrvChild) next_parent;
424};
425
426
427
428
429
430
431
432struct BlockDriverState {
433 int64_t total_sectors;
434
435 int open_flags;
436 bool read_only;
437 bool encrypted;
438 bool valid_key;
439 bool sg;
440 bool probed;
441
442 int copy_on_read;
443
444
445 CoQueue flush_queue;
446 bool active_flush_req;
447 unsigned int write_gen;
448 unsigned int flushed_gen;
449
450 BlockDriver *drv;
451 void *opaque;
452
453 AioContext *aio_context;
454
455
456
457 QLIST_HEAD(, BdrvAioNotifier) aio_notifiers;
458 bool walking_aio_notifiers;
459
460 char filename[PATH_MAX];
461 char backing_file[PATH_MAX];
462
463 char backing_format[16];
464
465 QDict *full_open_options;
466 char exact_filename[PATH_MAX];
467
468 BdrvChild *backing;
469 BdrvChild *file;
470
471
472 NotifierWithReturnList before_write_notifiers;
473
474
475 unsigned int in_flight;
476 unsigned int serialising_in_flight;
477
478 bool wakeup;
479
480
481 uint64_t wr_highest_offset;
482
483
484 BlockLimits bl;
485
486
487 unsigned int supported_write_flags;
488
489
490 unsigned int supported_zero_flags;
491
492
493 char node_name[32];
494
495 QTAILQ_ENTRY(BlockDriverState) node_list;
496
497 QTAILQ_ENTRY(BlockDriverState) bs_list;
498
499 QTAILQ_ENTRY(BlockDriverState) monitor_list;
500 QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps;
501 int refcnt;
502
503 QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
504
505
506 QLIST_HEAD(, BdrvOpBlocker) op_blockers[BLOCK_OP_TYPE_MAX];
507
508
509 BlockJob *job;
510
511
512
513
514 BlockDriverState *inherits_from;
515 QLIST_HEAD(, BdrvChild) children;
516 QLIST_HEAD(, BdrvChild) parents;
517
518 QDict *options;
519 QDict *explicit_options;
520 BlockdevDetectZeroesOptions detect_zeroes;
521
522
523 Error *backing_blocker;
524
525
526 uint64_t write_threshold_offset;
527 NotifierWithReturn write_threshold_notifier;
528
529
530 unsigned io_plugged;
531 unsigned io_plug_disabled;
532
533 int quiesce_counter;
534};
535
536struct BlockBackendRootState {
537 int open_flags;
538 bool read_only;
539 BlockdevDetectZeroesOptions detect_zeroes;
540};
541
542typedef enum BlockMirrorBackingMode {
543
544
545
546
547 MIRROR_SOURCE_BACKING_CHAIN,
548
549
550 MIRROR_OPEN_BACKING_CHAIN,
551
552
553 MIRROR_LEAVE_BACKING_CHAIN,
554} BlockMirrorBackingMode;
555
556static inline BlockDriverState *backing_bs(BlockDriverState *bs)
557{
558 return bs->backing ? bs->backing->bs : NULL;
559}
560
561
562
563
564extern BlockDriver bdrv_file;
565extern BlockDriver bdrv_raw;
566extern BlockDriver bdrv_qcow2;
567
568int coroutine_fn bdrv_co_preadv(BdrvChild *child,
569 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
570 BdrvRequestFlags flags);
571int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
572 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
573 BdrvRequestFlags flags);
574
575int get_tmp_filename(char *filename, int size);
576BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
577 const char *filename);
578
579
580
581
582
583
584
585
586void bdrv_add_before_write_notifier(BlockDriverState *bs,
587 NotifierWithReturn *notifier);
588
589
590
591
592
593
594
595
596
597void bdrv_detach_aio_context(BlockDriverState *bs);
598
599
600
601
602
603
604
605
606
607void bdrv_attach_aio_context(BlockDriverState *bs,
608 AioContext *new_context);
609
610
611
612
613
614
615
616
617
618
619
620
621void bdrv_add_aio_context_notifier(BlockDriverState *bs,
622 void (*attached_aio_context)(AioContext *new_context, void *opaque),
623 void (*detach_aio_context)(void *opaque), void *opaque);
624
625
626
627
628
629
630
631
632void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
633 void (*aio_context_attached)(AioContext *,
634 void *),
635 void (*aio_context_detached)(void *),
636 void *opaque);
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651void bdrv_wakeup(BlockDriverState *bs);
652
653#ifdef _WIN32
654int is_windows_drive(const char *filename);
655#endif
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677void stream_start(const char *job_id, BlockDriverState *bs,
678 BlockDriverState *base, const char *backing_file_str,
679 int64_t speed, BlockdevOnError on_error, Error **errp);
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694void commit_start(const char *job_id, BlockDriverState *bs,
695 BlockDriverState *base, BlockDriverState *top, int64_t speed,
696 BlockdevOnError on_error, const char *backing_file_str,
697 Error **errp);
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714void commit_active_start(const char *job_id, BlockDriverState *bs,
715 BlockDriverState *base, int creation_flags,
716 int64_t speed, BlockdevOnError on_error,
717 BlockCompletionFunc *cb,
718 void *opaque, Error **errp, bool auto_complete);
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742void mirror_start(const char *job_id, BlockDriverState *bs,
743 BlockDriverState *target, const char *replaces,
744 int64_t speed, uint32_t granularity, int64_t buf_size,
745 MirrorSyncMode mode, BlockMirrorBackingMode backing_mode,
746 BlockdevOnError on_source_error,
747 BlockdevOnError on_target_error,
748 bool unmap, Error **errp);
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
771 BlockDriverState *target, int64_t speed,
772 MirrorSyncMode sync_mode,
773 BdrvDirtyBitmap *sync_bitmap,
774 bool compress,
775 BlockdevOnError on_source_error,
776 BlockdevOnError on_target_error,
777 int creation_flags,
778 BlockCompletionFunc *cb, void *opaque,
779 BlockJobTxn *txn, Error **errp);
780
781void hmp_drive_add_node(Monitor *mon, const char *optstr);
782
783BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
784 const char *child_name,
785 const BdrvChildRole *child_role,
786 void *opaque);
787void bdrv_root_unref_child(BdrvChild *child);
788
789const char *bdrv_get_parent_name(const BlockDriverState *bs);
790void blk_dev_change_media_cb(BlockBackend *blk, bool load);
791bool blk_dev_has_removable_media(BlockBackend *blk);
792bool blk_dev_has_tray(BlockBackend *blk);
793void blk_dev_eject_request(BlockBackend *blk, bool force);
794bool blk_dev_is_tray_open(BlockBackend *blk);
795bool blk_dev_is_medium_locked(BlockBackend *blk);
796
797void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, int64_t nr_sect);
798bool bdrv_requests_pending(BlockDriverState *bs);
799
800void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out);
801void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in);
802
803void bdrv_inc_in_flight(BlockDriverState *bs);
804void bdrv_dec_in_flight(BlockDriverState *bs);
805
806void blockdev_close_all_bdrv_states(void);
807
808#endif
809