block_int.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * QEMU System Emulator block driver
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #ifndef BLOCK_INT_H
  25. #define BLOCK_INT_H
  26. #include "block.h"
  27. #include "qemu-option.h"
  28. #include "qemu-queue.h"
  29. #define BLOCK_FLAG_ENCRYPT 1
  30. #define BLOCK_FLAG_COMPAT6 4
  31. #define BLOCK_OPT_SIZE "size"
  32. #define BLOCK_OPT_ENCRYPT "encryption"
  33. #define BLOCK_OPT_COMPAT6 "compat6"
  34. #define BLOCK_OPT_BACKING_FILE "backing_file"
  35. #define BLOCK_OPT_BACKING_FMT "backing_fmt"
  36. #define BLOCK_OPT_CLUSTER_SIZE "cluster_size"
  37. #define BLOCK_OPT_TABLE_SIZE "table_size"
  38. #define BLOCK_OPT_PREALLOC "preallocation"
  39. #define BLOCK_OPT_SUBFMT "subformat"
  40. typedef struct AIOPool {
  41. void (*cancel)(BlockDriverAIOCB *acb);
  42. int aiocb_size;
  43. BlockDriverAIOCB *free_aiocb;
  44. } AIOPool;
  45. struct BlockDriver {
  46. const char *format_name;
  47. int instance_size;
  48. int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
  49. int (*bdrv_probe_device)(const char *filename);
  50. int (*bdrv_open)(BlockDriverState *bs, int flags);
  51. int (*bdrv_file_open)(BlockDriverState *bs, const char *filename, int flags);
  52. int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
  53. uint8_t *buf, int nb_sectors);
  54. int (*bdrv_write)(BlockDriverState *bs, int64_t sector_num,
  55. const uint8_t *buf, int nb_sectors);
  56. void (*bdrv_close)(BlockDriverState *bs);
  57. int (*bdrv_create)(const char *filename, QEMUOptionParameter *options);
  58. int (*bdrv_flush)(BlockDriverState *bs);
  59. int (*bdrv_is_allocated)(BlockDriverState *bs, int64_t sector_num,
  60. int nb_sectors, int *pnum);
  61. int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
  62. int (*bdrv_make_empty)(BlockDriverState *bs);
  63. /* aio */
  64. BlockDriverAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
  65. int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
  66. BlockDriverCompletionFunc *cb, void *opaque);
  67. BlockDriverAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
  68. int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
  69. BlockDriverCompletionFunc *cb, void *opaque);
  70. BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
  71. BlockDriverCompletionFunc *cb, void *opaque);
  72. int (*bdrv_discard)(BlockDriverState *bs, int64_t sector_num,
  73. int nb_sectors);
  74. int (*bdrv_aio_multiwrite)(BlockDriverState *bs, BlockRequest *reqs,
  75. int num_reqs);
  76. int (*bdrv_merge_requests)(BlockDriverState *bs, BlockRequest* a,
  77. BlockRequest *b);
  78. const char *protocol_name;
  79. int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset);
  80. int64_t (*bdrv_getlength)(BlockDriverState *bs);
  81. int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs);
  82. int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
  83. const uint8_t *buf, int nb_sectors);
  84. int (*bdrv_snapshot_create)(BlockDriverState *bs,
  85. QEMUSnapshotInfo *sn_info);
  86. int (*bdrv_snapshot_goto)(BlockDriverState *bs,
  87. const char *snapshot_id);
  88. int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
  89. int (*bdrv_snapshot_list)(BlockDriverState *bs,
  90. QEMUSnapshotInfo **psn_info);
  91. int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
  92. const char *snapshot_name);
  93. int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
  94. int (*bdrv_save_vmstate)(BlockDriverState *bs, const uint8_t *buf,
  95. int64_t pos, int size);
  96. int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
  97. int64_t pos, int size);
  98. int (*bdrv_change_backing_file)(BlockDriverState *bs,
  99. const char *backing_file, const char *backing_fmt);
  100. /* removable device specific */
  101. int (*bdrv_is_inserted)(BlockDriverState *bs);
  102. int (*bdrv_media_changed)(BlockDriverState *bs);
  103. int (*bdrv_eject)(BlockDriverState *bs, int eject_flag);
  104. int (*bdrv_set_locked)(BlockDriverState *bs, int locked);
  105. /* to control generic scsi devices */
  106. int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
  107. BlockDriverAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
  108. unsigned long int req, void *buf,
  109. BlockDriverCompletionFunc *cb, void *opaque);
  110. /* List of options for creating images, terminated by name == NULL */
  111. QEMUOptionParameter *create_options;
  112. /*
  113. * Returns 0 for completed check, -errno for internal errors.
  114. * The check results are stored in result.
  115. */
  116. int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result);
  117. void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
  118. /*
  119. * Returns 1 if newly created images are guaranteed to contain only
  120. * zeros, 0 otherwise.
  121. */
  122. int (*bdrv_has_zero_init)(BlockDriverState *bs);
  123. QLIST_ENTRY(BlockDriver) list;
  124. };
  125. struct BlockDriverState {
  126. int64_t total_sectors; /* if we are reading a disk image, give its
  127. size in sectors */
  128. int read_only; /* if true, the media is read only */
  129. int keep_read_only; /* if true, the media was requested to stay read only */
  130. int open_flags; /* flags used to open the file, re-used for re-open */
  131. int removable; /* if true, the media can be removed */
  132. int locked; /* if true, the media cannot temporarily be ejected */
  133. int tray_open; /* if true, the virtual tray is open */
  134. int encrypted; /* if true, the media is encrypted */
  135. int valid_key; /* if true, a valid encryption key has been set */
  136. int sg; /* if true, the device is a /dev/sg* */
  137. /* event callback when inserting/removing */
  138. void (*change_cb)(void *opaque, int reason);
  139. void *change_opaque;
  140. BlockDriver *drv; /* NULL means no media */
  141. void *opaque;
  142. DeviceState *peer;
  143. char filename[1024];
  144. char backing_file[1024]; /* if non zero, the image is a diff of
  145. this file image */
  146. char backing_format[16]; /* if non-zero and backing_file exists */
  147. int is_temporary;
  148. int media_changed;
  149. BlockDriverState *backing_hd;
  150. BlockDriverState *file;
  151. /* async read/write emulation */
  152. void *sync_aiocb;
  153. /* I/O stats (display with "info blockstats"). */
  154. uint64_t rd_bytes;
  155. uint64_t wr_bytes;
  156. uint64_t rd_ops;
  157. uint64_t wr_ops;
  158. uint64_t wr_highest_sector;
  159. /* Whether the disk can expand beyond total_sectors */
  160. int growable;
  161. /* the memory alignment required for the buffers handled by this driver */
  162. int buffer_alignment;
  163. /* do we need to tell the quest if we have a volatile write cache? */
  164. int enable_write_cache;
  165. /* NOTE: the following infos are only hints for real hardware
  166. drivers. They are not used by the block driver */
  167. int cyls, heads, secs, translation;
  168. BlockErrorAction on_read_error, on_write_error;
  169. char device_name[32];
  170. unsigned long *dirty_bitmap;
  171. int64_t dirty_count;
  172. int in_use; /* users other than guest access, eg. block migration */
  173. QTAILQ_ENTRY(BlockDriverState) list;
  174. void *private;
  175. };
  176. #define CHANGE_MEDIA 0x01
  177. #define CHANGE_SIZE 0x02
  178. struct BlockDriverAIOCB {
  179. AIOPool *pool;
  180. BlockDriverState *bs;
  181. BlockDriverCompletionFunc *cb;
  182. void *opaque;
  183. BlockDriverAIOCB *next;
  184. };
  185. void get_tmp_filename(char *filename, int size);
  186. void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
  187. BlockDriverCompletionFunc *cb, void *opaque);
  188. void qemu_aio_release(void *p);
  189. void *qemu_blockalign(BlockDriverState *bs, size_t size);
  190. #ifdef _WIN32
  191. int is_windows_drive(const char *filename);
  192. #endif
  193. typedef struct BlockConf {
  194. BlockDriverState *bs;
  195. uint16_t physical_block_size;
  196. uint16_t logical_block_size;
  197. uint16_t min_io_size;
  198. uint32_t opt_io_size;
  199. int32_t bootindex;
  200. uint32_t discard_granularity;
  201. } BlockConf;
  202. static inline unsigned int get_physical_block_exp(BlockConf *conf)
  203. {
  204. unsigned int exp = 0, size;
  205. for (size = conf->physical_block_size;
  206. size > conf->logical_block_size;
  207. size >>= 1) {
  208. exp++;
  209. }
  210. return exp;
  211. }
  212. #define DEFINE_BLOCK_PROPERTIES(_state, _conf) \
  213. DEFINE_PROP_DRIVE("drive", _state, _conf.bs), \
  214. DEFINE_PROP_UINT16("logical_block_size", _state, \
  215. _conf.logical_block_size, 512), \
  216. DEFINE_PROP_UINT16("physical_block_size", _state, \
  217. _conf.physical_block_size, 512), \
  218. DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 0), \
  219. DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0), \
  220. DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1), \
  221. DEFINE_PROP_UINT32("discard_granularity", _state, \
  222. _conf.discard_granularity, 0)
  223. #endif /* BLOCK_INT_H */