2
0

block.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #ifndef BLOCK_H
  2. #define BLOCK_H
  3. #include "qemu-aio.h"
  4. #include "qemu-common.h"
  5. #include "qemu-option.h"
  6. #include "qemu-coroutine.h"
  7. #include "qobject.h"
  8. /* block.c */
  9. typedef struct BlockDriver BlockDriver;
  10. typedef struct BlockDriverInfo {
  11. /* in bytes, 0 if irrelevant */
  12. int cluster_size;
  13. /* offset at which the VM state can be saved (0 if not possible) */
  14. int64_t vm_state_offset;
  15. bool is_dirty;
  16. } BlockDriverInfo;
  17. typedef struct BlockFragInfo {
  18. uint64_t allocated_clusters;
  19. uint64_t total_clusters;
  20. uint64_t fragmented_clusters;
  21. } BlockFragInfo;
  22. typedef struct QEMUSnapshotInfo {
  23. char id_str[128]; /* unique snapshot id */
  24. /* the following fields are informative. They are not needed for
  25. the consistency of the snapshot */
  26. char name[256]; /* user chosen name */
  27. uint64_t vm_state_size; /* VM state info size */
  28. uint32_t date_sec; /* UTC date of the snapshot */
  29. uint32_t date_nsec;
  30. uint64_t vm_clock_nsec; /* VM clock relative to boot */
  31. } QEMUSnapshotInfo;
  32. /* Callbacks for block device models */
  33. typedef struct BlockDevOps {
  34. /*
  35. * Runs when virtual media changed (monitor commands eject, change)
  36. * Argument load is true on load and false on eject.
  37. * Beware: doesn't run when a host device's physical media
  38. * changes. Sure would be useful if it did.
  39. * Device models with removable media must implement this callback.
  40. */
  41. void (*change_media_cb)(void *opaque, bool load);
  42. /*
  43. * Runs when an eject request is issued from the monitor, the tray
  44. * is closed, and the medium is locked.
  45. * Device models that do not implement is_medium_locked will not need
  46. * this callback. Device models that can lock the medium or tray might
  47. * want to implement the callback and unlock the tray when "force" is
  48. * true, even if they do not support eject requests.
  49. */
  50. void (*eject_request_cb)(void *opaque, bool force);
  51. /*
  52. * Is the virtual tray open?
  53. * Device models implement this only when the device has a tray.
  54. */
  55. bool (*is_tray_open)(void *opaque);
  56. /*
  57. * Is the virtual medium locked into the device?
  58. * Device models implement this only when device has such a lock.
  59. */
  60. bool (*is_medium_locked)(void *opaque);
  61. /*
  62. * Runs when the size changed (e.g. monitor command block_resize)
  63. */
  64. void (*resize_cb)(void *opaque);
  65. } BlockDevOps;
  66. #define BDRV_O_RDWR 0x0002
  67. #define BDRV_O_SNAPSHOT 0x0008 /* open the file read only and save writes in a snapshot */
  68. #define BDRV_O_NOCACHE 0x0020 /* do not use the host page cache */
  69. #define BDRV_O_CACHE_WB 0x0040 /* use write-back caching */
  70. #define BDRV_O_NATIVE_AIO 0x0080 /* use native AIO instead of the thread pool */
  71. #define BDRV_O_NO_BACKING 0x0100 /* don't open the backing file */
  72. #define BDRV_O_NO_FLUSH 0x0200 /* disable flushing on this disk */
  73. #define BDRV_O_COPY_ON_READ 0x0400 /* copy read backing sectors into image */
  74. #define BDRV_O_INCOMING 0x0800 /* consistency hint for incoming migration */
  75. #define BDRV_O_CACHE_MASK (BDRV_O_NOCACHE | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH)
  76. #define BDRV_SECTOR_BITS 9
  77. #define BDRV_SECTOR_SIZE (1ULL << BDRV_SECTOR_BITS)
  78. #define BDRV_SECTOR_MASK ~(BDRV_SECTOR_SIZE - 1)
  79. typedef enum {
  80. BLOCK_ERR_REPORT, BLOCK_ERR_IGNORE, BLOCK_ERR_STOP_ENOSPC,
  81. BLOCK_ERR_STOP_ANY
  82. } BlockErrorAction;
  83. typedef enum {
  84. BDRV_ACTION_REPORT, BDRV_ACTION_IGNORE, BDRV_ACTION_STOP
  85. } BlockQMPEventAction;
  86. void bdrv_iostatus_enable(BlockDriverState *bs);
  87. void bdrv_iostatus_reset(BlockDriverState *bs);
  88. void bdrv_iostatus_disable(BlockDriverState *bs);
  89. bool bdrv_iostatus_is_enabled(const BlockDriverState *bs);
  90. void bdrv_iostatus_set_err(BlockDriverState *bs, int error);
  91. void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
  92. BlockQMPEventAction action, int is_read);
  93. void bdrv_info_print(Monitor *mon, const QObject *data);
  94. void bdrv_info(Monitor *mon, QObject **ret_data);
  95. void bdrv_stats_print(Monitor *mon, const QObject *data);
  96. void bdrv_info_stats(Monitor *mon, QObject **ret_data);
  97. /* disk I/O throttling */
  98. void bdrv_io_limits_enable(BlockDriverState *bs);
  99. void bdrv_io_limits_disable(BlockDriverState *bs);
  100. bool bdrv_io_limits_enabled(BlockDriverState *bs);
  101. void bdrv_init(void);
  102. void bdrv_init_with_whitelist(void);
  103. BlockDriver *bdrv_find_protocol(const char *filename);
  104. BlockDriver *bdrv_find_format(const char *format_name);
  105. BlockDriver *bdrv_find_whitelisted_format(const char *format_name);
  106. int bdrv_create(BlockDriver *drv, const char* filename,
  107. QEMUOptionParameter *options);
  108. int bdrv_create_file(const char* filename, QEMUOptionParameter *options);
  109. BlockDriverState *bdrv_new(const char *device_name);
  110. void bdrv_make_anon(BlockDriverState *bs);
  111. void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top);
  112. void bdrv_delete(BlockDriverState *bs);
  113. int bdrv_parse_cache_flags(const char *mode, int *flags);
  114. int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);
  115. int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
  116. BlockDriver *drv);
  117. void bdrv_close(BlockDriverState *bs);
  118. int bdrv_attach_dev(BlockDriverState *bs, void *dev);
  119. void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev);
  120. void bdrv_detach_dev(BlockDriverState *bs, void *dev);
  121. void *bdrv_get_attached_dev(BlockDriverState *bs);
  122. void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
  123. void *opaque);
  124. void bdrv_dev_eject_request(BlockDriverState *bs, bool force);
  125. bool bdrv_dev_has_removable_media(BlockDriverState *bs);
  126. bool bdrv_dev_is_tray_open(BlockDriverState *bs);
  127. bool bdrv_dev_is_medium_locked(BlockDriverState *bs);
  128. int bdrv_read(BlockDriverState *bs, int64_t sector_num,
  129. uint8_t *buf, int nb_sectors);
  130. int bdrv_write(BlockDriverState *bs, int64_t sector_num,
  131. const uint8_t *buf, int nb_sectors);
  132. int bdrv_pread(BlockDriverState *bs, int64_t offset,
  133. void *buf, int count);
  134. int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
  135. const void *buf, int count);
  136. int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
  137. const void *buf, int count);
  138. int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
  139. int nb_sectors, QEMUIOVector *qiov);
  140. int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
  141. int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
  142. int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
  143. int nb_sectors, QEMUIOVector *qiov);
  144. /*
  145. * Efficiently zero a region of the disk image. Note that this is a regular
  146. * I/O request like read or write and should have a reasonable size. This
  147. * function is not suitable for zeroing the entire image in a single request
  148. * because it may allocate memory for the entire region.
  149. */
  150. int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
  151. int nb_sectors);
  152. int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num,
  153. int nb_sectors, int *pnum);
  154. BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
  155. const char *backing_file);
  156. int bdrv_truncate(BlockDriverState *bs, int64_t offset);
  157. int64_t bdrv_getlength(BlockDriverState *bs);
  158. int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
  159. void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr);
  160. void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs);
  161. int bdrv_commit(BlockDriverState *bs);
  162. int bdrv_commit_all(void);
  163. int bdrv_change_backing_file(BlockDriverState *bs,
  164. const char *backing_file, const char *backing_fmt);
  165. void bdrv_register(BlockDriver *bdrv);
  166. typedef struct BdrvCheckResult {
  167. int corruptions;
  168. int leaks;
  169. int check_errors;
  170. BlockFragInfo bfi;
  171. } BdrvCheckResult;
  172. int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res);
  173. /* async block I/O */
  174. typedef void BlockDriverDirtyHandler(BlockDriverState *bs, int64_t sector,
  175. int sector_num);
  176. BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
  177. QEMUIOVector *iov, int nb_sectors,
  178. BlockDriverCompletionFunc *cb, void *opaque);
  179. BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
  180. QEMUIOVector *iov, int nb_sectors,
  181. BlockDriverCompletionFunc *cb, void *opaque);
  182. BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
  183. BlockDriverCompletionFunc *cb, void *opaque);
  184. BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs,
  185. int64_t sector_num, int nb_sectors,
  186. BlockDriverCompletionFunc *cb, void *opaque);
  187. void bdrv_aio_cancel(BlockDriverAIOCB *acb);
  188. typedef struct BlockRequest {
  189. /* Fields to be filled by multiwrite caller */
  190. int64_t sector;
  191. int nb_sectors;
  192. QEMUIOVector *qiov;
  193. BlockDriverCompletionFunc *cb;
  194. void *opaque;
  195. /* Filled by multiwrite implementation */
  196. int error;
  197. } BlockRequest;
  198. int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs,
  199. int num_reqs);
  200. /* sg packet commands */
  201. int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf);
  202. BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
  203. unsigned long int req, void *buf,
  204. BlockDriverCompletionFunc *cb, void *opaque);
  205. /* Invalidate any cached metadata used by image formats */
  206. void bdrv_invalidate_cache(BlockDriverState *bs);
  207. void bdrv_invalidate_cache_all(void);
  208. void bdrv_clear_incoming_migration_all(void);
  209. /* Ensure contents are flushed to disk. */
  210. int bdrv_flush(BlockDriverState *bs);
  211. int coroutine_fn bdrv_co_flush(BlockDriverState *bs);
  212. void bdrv_flush_all(void);
  213. void bdrv_close_all(void);
  214. void bdrv_drain_all(void);
  215. int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
  216. int bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
  217. int bdrv_has_zero_init(BlockDriverState *bs);
  218. int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
  219. int *pnum);
  220. #define BIOS_ATA_TRANSLATION_AUTO 0
  221. #define BIOS_ATA_TRANSLATION_NONE 1
  222. #define BIOS_ATA_TRANSLATION_LBA 2
  223. #define BIOS_ATA_TRANSLATION_LARGE 3
  224. #define BIOS_ATA_TRANSLATION_RECHS 4
  225. void bdrv_set_geometry_hint(BlockDriverState *bs,
  226. int cyls, int heads, int secs);
  227. void bdrv_set_translation_hint(BlockDriverState *bs, int translation);
  228. void bdrv_get_geometry_hint(BlockDriverState *bs,
  229. int *pcyls, int *pheads, int *psecs);
  230. typedef enum FDriveType {
  231. FDRIVE_DRV_144 = 0x00, /* 1.44 MB 3"5 drive */
  232. FDRIVE_DRV_288 = 0x01, /* 2.88 MB 3"5 drive */
  233. FDRIVE_DRV_120 = 0x02, /* 1.2 MB 5"25 drive */
  234. FDRIVE_DRV_NONE = 0x03, /* No drive connected */
  235. } FDriveType;
  236. typedef enum FDriveRate {
  237. FDRIVE_RATE_500K = 0x00, /* 500 Kbps */
  238. FDRIVE_RATE_300K = 0x01, /* 300 Kbps */
  239. FDRIVE_RATE_250K = 0x02, /* 250 Kbps */
  240. FDRIVE_RATE_1M = 0x03, /* 1 Mbps */
  241. } FDriveRate;
  242. void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
  243. int *max_track, int *last_sect,
  244. FDriveType drive_in, FDriveType *drive,
  245. FDriveRate *rate);
  246. int bdrv_get_translation_hint(BlockDriverState *bs);
  247. void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
  248. BlockErrorAction on_write_error);
  249. BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read);
  250. int bdrv_is_read_only(BlockDriverState *bs);
  251. int bdrv_is_sg(BlockDriverState *bs);
  252. int bdrv_enable_write_cache(BlockDriverState *bs);
  253. int bdrv_is_inserted(BlockDriverState *bs);
  254. int bdrv_media_changed(BlockDriverState *bs);
  255. void bdrv_lock_medium(BlockDriverState *bs, bool locked);
  256. void bdrv_eject(BlockDriverState *bs, bool eject_flag);
  257. void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
  258. BlockDriverState *bdrv_find(const char *name);
  259. BlockDriverState *bdrv_next(BlockDriverState *bs);
  260. void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs),
  261. void *opaque);
  262. int bdrv_is_encrypted(BlockDriverState *bs);
  263. int bdrv_key_required(BlockDriverState *bs);
  264. int bdrv_set_key(BlockDriverState *bs, const char *key);
  265. int bdrv_query_missing_keys(void);
  266. void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
  267. void *opaque);
  268. const char *bdrv_get_device_name(BlockDriverState *bs);
  269. int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
  270. const uint8_t *buf, int nb_sectors);
  271. int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
  272. const char *bdrv_get_encrypted_filename(BlockDriverState *bs);
  273. void bdrv_get_backing_filename(BlockDriverState *bs,
  274. char *filename, int filename_size);
  275. void bdrv_get_full_backing_filename(BlockDriverState *bs,
  276. char *dest, size_t sz);
  277. int bdrv_can_snapshot(BlockDriverState *bs);
  278. int bdrv_is_snapshot(BlockDriverState *bs);
  279. BlockDriverState *bdrv_snapshots(void);
  280. int bdrv_snapshot_create(BlockDriverState *bs,
  281. QEMUSnapshotInfo *sn_info);
  282. int bdrv_snapshot_goto(BlockDriverState *bs,
  283. const char *snapshot_id);
  284. int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
  285. int bdrv_snapshot_list(BlockDriverState *bs,
  286. QEMUSnapshotInfo **psn_info);
  287. int bdrv_snapshot_load_tmp(BlockDriverState *bs,
  288. const char *snapshot_name);
  289. char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
  290. char *get_human_readable_size(char *buf, int buf_size, int64_t size);
  291. int path_is_absolute(const char *path);
  292. void path_combine(char *dest, int dest_size,
  293. const char *base_path,
  294. const char *filename);
  295. int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
  296. int64_t pos, int size);
  297. int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
  298. int64_t pos, int size);
  299. int bdrv_img_create(const char *filename, const char *fmt,
  300. const char *base_filename, const char *base_fmt,
  301. char *options, uint64_t img_size, int flags);
  302. void bdrv_set_buffer_alignment(BlockDriverState *bs, int align);
  303. void *qemu_blockalign(BlockDriverState *bs, size_t size);
  304. #define BDRV_SECTORS_PER_DIRTY_CHUNK 2048
  305. void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable);
  306. int bdrv_get_dirty(BlockDriverState *bs, int64_t sector);
  307. void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
  308. int nr_sectors);
  309. int64_t bdrv_get_dirty_count(BlockDriverState *bs);
  310. void bdrv_enable_copy_on_read(BlockDriverState *bs);
  311. void bdrv_disable_copy_on_read(BlockDriverState *bs);
  312. void bdrv_set_in_use(BlockDriverState *bs, int in_use);
  313. int bdrv_in_use(BlockDriverState *bs);
  314. enum BlockAcctType {
  315. BDRV_ACCT_READ,
  316. BDRV_ACCT_WRITE,
  317. BDRV_ACCT_FLUSH,
  318. BDRV_MAX_IOTYPE,
  319. };
  320. typedef struct BlockAcctCookie {
  321. int64_t bytes;
  322. int64_t start_time_ns;
  323. enum BlockAcctType type;
  324. } BlockAcctCookie;
  325. void bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
  326. int64_t bytes, enum BlockAcctType type);
  327. void bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie);
  328. typedef enum {
  329. BLKDBG_L1_UPDATE,
  330. BLKDBG_L1_GROW_ALLOC_TABLE,
  331. BLKDBG_L1_GROW_WRITE_TABLE,
  332. BLKDBG_L1_GROW_ACTIVATE_TABLE,
  333. BLKDBG_L2_LOAD,
  334. BLKDBG_L2_UPDATE,
  335. BLKDBG_L2_UPDATE_COMPRESSED,
  336. BLKDBG_L2_ALLOC_COW_READ,
  337. BLKDBG_L2_ALLOC_WRITE,
  338. BLKDBG_READ,
  339. BLKDBG_READ_AIO,
  340. BLKDBG_READ_BACKING,
  341. BLKDBG_READ_BACKING_AIO,
  342. BLKDBG_READ_COMPRESSED,
  343. BLKDBG_WRITE_AIO,
  344. BLKDBG_WRITE_COMPRESSED,
  345. BLKDBG_VMSTATE_LOAD,
  346. BLKDBG_VMSTATE_SAVE,
  347. BLKDBG_COW_READ,
  348. BLKDBG_COW_WRITE,
  349. BLKDBG_REFTABLE_LOAD,
  350. BLKDBG_REFTABLE_GROW,
  351. BLKDBG_REFBLOCK_LOAD,
  352. BLKDBG_REFBLOCK_UPDATE,
  353. BLKDBG_REFBLOCK_UPDATE_PART,
  354. BLKDBG_REFBLOCK_ALLOC,
  355. BLKDBG_REFBLOCK_ALLOC_HOOKUP,
  356. BLKDBG_REFBLOCK_ALLOC_WRITE,
  357. BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS,
  358. BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE,
  359. BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE,
  360. BLKDBG_CLUSTER_ALLOC,
  361. BLKDBG_CLUSTER_ALLOC_BYTES,
  362. BLKDBG_CLUSTER_FREE,
  363. BLKDBG_EVENT_MAX,
  364. } BlkDebugEvent;
  365. #define BLKDBG_EVENT(bs, evt) bdrv_debug_event(bs, evt)
  366. void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event);
  367. /* Convenience for block device models */
  368. typedef struct BlockConf {
  369. BlockDriverState *bs;
  370. uint16_t physical_block_size;
  371. uint16_t logical_block_size;
  372. uint16_t min_io_size;
  373. uint32_t opt_io_size;
  374. int32_t bootindex;
  375. uint32_t discard_granularity;
  376. } BlockConf;
  377. static inline unsigned int get_physical_block_exp(BlockConf *conf)
  378. {
  379. unsigned int exp = 0, size;
  380. for (size = conf->physical_block_size;
  381. size > conf->logical_block_size;
  382. size >>= 1) {
  383. exp++;
  384. }
  385. return exp;
  386. }
  387. #define DEFINE_BLOCK_PROPERTIES(_state, _conf) \
  388. DEFINE_PROP_DRIVE("drive", _state, _conf.bs), \
  389. DEFINE_PROP_BLOCKSIZE("logical_block_size", _state, \
  390. _conf.logical_block_size, 512), \
  391. DEFINE_PROP_BLOCKSIZE("physical_block_size", _state, \
  392. _conf.physical_block_size, 512), \
  393. DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 0), \
  394. DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0), \
  395. DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1), \
  396. DEFINE_PROP_UINT32("discard_granularity", _state, \
  397. _conf.discard_granularity, 0)
  398. #endif