block_int.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. #include "qemu-coroutine.h"
  30. #include "qemu-timer.h"
  31. #include "qapi-types.h"
  32. #define BLOCK_FLAG_ENCRYPT 1
  33. #define BLOCK_FLAG_COMPAT6 4
  34. #define BLOCK_IO_LIMIT_READ 0
  35. #define BLOCK_IO_LIMIT_WRITE 1
  36. #define BLOCK_IO_LIMIT_TOTAL 2
  37. #define BLOCK_IO_SLICE_TIME 100000000
  38. #define NANOSECONDS_PER_SECOND 1000000000.0
  39. #define BLOCK_OPT_SIZE "size"
  40. #define BLOCK_OPT_ENCRYPT "encryption"
  41. #define BLOCK_OPT_COMPAT6 "compat6"
  42. #define BLOCK_OPT_BACKING_FILE "backing_file"
  43. #define BLOCK_OPT_BACKING_FMT "backing_fmt"
  44. #define BLOCK_OPT_CLUSTER_SIZE "cluster_size"
  45. #define BLOCK_OPT_TABLE_SIZE "table_size"
  46. #define BLOCK_OPT_PREALLOC "preallocation"
  47. #define BLOCK_OPT_SUBFMT "subformat"
  48. #define BLOCK_OPT_COMPAT_LEVEL "compat"
  49. typedef struct BdrvTrackedRequest BdrvTrackedRequest;
  50. typedef struct BlockIOLimit {
  51. int64_t bps[3];
  52. int64_t iops[3];
  53. } BlockIOLimit;
  54. typedef struct BlockIOBaseValue {
  55. uint64_t bytes[2];
  56. uint64_t ios[2];
  57. } BlockIOBaseValue;
  58. typedef struct BlockJob BlockJob;
  59. /**
  60. * BlockJobType:
  61. *
  62. * A class type for block job objects.
  63. */
  64. typedef struct BlockJobType {
  65. /** Derived BlockJob struct size */
  66. size_t instance_size;
  67. /** String describing the operation, part of query-block-jobs QMP API */
  68. const char *job_type;
  69. /** Optional callback for job types that support setting a speed limit */
  70. void (*set_speed)(BlockJob *job, int64_t speed, Error **errp);
  71. } BlockJobType;
  72. /**
  73. * BlockJob:
  74. *
  75. * Long-running operation on a BlockDriverState.
  76. */
  77. struct BlockJob {
  78. /** The job type, including the job vtable. */
  79. const BlockJobType *job_type;
  80. /** The block device on which the job is operating. */
  81. BlockDriverState *bs;
  82. /**
  83. * The coroutine that executes the job. If not NULL, it is
  84. * reentered when busy is false and the job is cancelled.
  85. */
  86. Coroutine *co;
  87. /**
  88. * Set to true if the job should cancel itself. The flag must
  89. * always be tested just before toggling the busy flag from false
  90. * to true. After a job has been cancelled, it should only yield
  91. * if #qemu_aio_wait will ("sooner or later") reenter the coroutine.
  92. */
  93. bool cancelled;
  94. /**
  95. * Set to false by the job while it is in a quiescent state, where
  96. * no I/O is pending and the job has yielded on any condition
  97. * that is not detected by #qemu_aio_wait, such as a timer.
  98. */
  99. bool busy;
  100. /** Offset that is published by the query-block-jobs QMP API */
  101. int64_t offset;
  102. /** Length that is published by the query-block-jobs QMP API */
  103. int64_t len;
  104. /** Speed that was set with @block_job_set_speed. */
  105. int64_t speed;
  106. /** The completion function that will be called when the job completes. */
  107. BlockDriverCompletionFunc *cb;
  108. /** The opaque value that is passed to the completion function. */
  109. void *opaque;
  110. };
  111. struct BlockDriver {
  112. const char *format_name;
  113. int instance_size;
  114. int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
  115. int (*bdrv_probe_device)(const char *filename);
  116. int (*bdrv_open)(BlockDriverState *bs, int flags);
  117. int (*bdrv_file_open)(BlockDriverState *bs, const char *filename, int flags);
  118. int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
  119. uint8_t *buf, int nb_sectors);
  120. int (*bdrv_write)(BlockDriverState *bs, int64_t sector_num,
  121. const uint8_t *buf, int nb_sectors);
  122. void (*bdrv_close)(BlockDriverState *bs);
  123. void (*bdrv_rebind)(BlockDriverState *bs);
  124. int (*bdrv_create)(const char *filename, QEMUOptionParameter *options);
  125. int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
  126. int (*bdrv_make_empty)(BlockDriverState *bs);
  127. /* aio */
  128. BlockDriverAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
  129. int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
  130. BlockDriverCompletionFunc *cb, void *opaque);
  131. BlockDriverAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
  132. int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
  133. BlockDriverCompletionFunc *cb, void *opaque);
  134. BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
  135. BlockDriverCompletionFunc *cb, void *opaque);
  136. BlockDriverAIOCB *(*bdrv_aio_discard)(BlockDriverState *bs,
  137. int64_t sector_num, int nb_sectors,
  138. BlockDriverCompletionFunc *cb, void *opaque);
  139. int coroutine_fn (*bdrv_co_readv)(BlockDriverState *bs,
  140. int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
  141. int coroutine_fn (*bdrv_co_writev)(BlockDriverState *bs,
  142. int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
  143. /*
  144. * Efficiently zero a region of the disk image. Typically an image format
  145. * would use a compact metadata representation to implement this. This
  146. * function pointer may be NULL and .bdrv_co_writev() will be called
  147. * instead.
  148. */
  149. int coroutine_fn (*bdrv_co_write_zeroes)(BlockDriverState *bs,
  150. int64_t sector_num, int nb_sectors);
  151. int coroutine_fn (*bdrv_co_discard)(BlockDriverState *bs,
  152. int64_t sector_num, int nb_sectors);
  153. int coroutine_fn (*bdrv_co_is_allocated)(BlockDriverState *bs,
  154. int64_t sector_num, int nb_sectors, int *pnum);
  155. /*
  156. * Invalidate any cached meta-data.
  157. */
  158. void (*bdrv_invalidate_cache)(BlockDriverState *bs);
  159. /*
  160. * Flushes all data that was already written to the OS all the way down to
  161. * the disk (for example raw-posix calls fsync()).
  162. */
  163. int coroutine_fn (*bdrv_co_flush_to_disk)(BlockDriverState *bs);
  164. /*
  165. * Flushes all internal caches to the OS. The data may still sit in a
  166. * writeback cache of the host OS, but it will survive a crash of the qemu
  167. * process.
  168. */
  169. int coroutine_fn (*bdrv_co_flush_to_os)(BlockDriverState *bs);
  170. const char *protocol_name;
  171. int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset);
  172. int64_t (*bdrv_getlength)(BlockDriverState *bs);
  173. int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs);
  174. int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
  175. const uint8_t *buf, int nb_sectors);
  176. int (*bdrv_snapshot_create)(BlockDriverState *bs,
  177. QEMUSnapshotInfo *sn_info);
  178. int (*bdrv_snapshot_goto)(BlockDriverState *bs,
  179. const char *snapshot_id);
  180. int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
  181. int (*bdrv_snapshot_list)(BlockDriverState *bs,
  182. QEMUSnapshotInfo **psn_info);
  183. int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
  184. const char *snapshot_name);
  185. int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
  186. int (*bdrv_save_vmstate)(BlockDriverState *bs, const uint8_t *buf,
  187. int64_t pos, int size);
  188. int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
  189. int64_t pos, int size);
  190. int (*bdrv_change_backing_file)(BlockDriverState *bs,
  191. const char *backing_file, const char *backing_fmt);
  192. /* removable device specific */
  193. int (*bdrv_is_inserted)(BlockDriverState *bs);
  194. int (*bdrv_media_changed)(BlockDriverState *bs);
  195. void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag);
  196. void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);
  197. /* to control generic scsi devices */
  198. int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
  199. BlockDriverAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
  200. unsigned long int req, void *buf,
  201. BlockDriverCompletionFunc *cb, void *opaque);
  202. /* List of options for creating images, terminated by name == NULL */
  203. QEMUOptionParameter *create_options;
  204. /*
  205. * Returns 0 for completed check, -errno for internal errors.
  206. * The check results are stored in result.
  207. */
  208. int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result);
  209. void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
  210. /*
  211. * Returns 1 if newly created images are guaranteed to contain only
  212. * zeros, 0 otherwise.
  213. */
  214. int (*bdrv_has_zero_init)(BlockDriverState *bs);
  215. QLIST_ENTRY(BlockDriver) list;
  216. };
  217. /*
  218. * Note: the function bdrv_append() copies and swaps contents of
  219. * BlockDriverStates, so if you add new fields to this struct, please
  220. * inspect bdrv_append() to determine if the new fields need to be
  221. * copied as well.
  222. */
  223. struct BlockDriverState {
  224. int64_t total_sectors; /* if we are reading a disk image, give its
  225. size in sectors */
  226. int read_only; /* if true, the media is read only */
  227. int keep_read_only; /* if true, the media was requested to stay read only */
  228. int open_flags; /* flags used to open the file, re-used for re-open */
  229. int encrypted; /* if true, the media is encrypted */
  230. int valid_key; /* if true, a valid encryption key has been set */
  231. int sg; /* if true, the device is a /dev/sg* */
  232. int copy_on_read; /* if true, copy read backing sectors into image
  233. note this is a reference count */
  234. BlockDriver *drv; /* NULL means no media */
  235. void *opaque;
  236. void *dev; /* attached device model, if any */
  237. /* TODO change to DeviceState when all users are qdevified */
  238. const BlockDevOps *dev_ops;
  239. void *dev_opaque;
  240. char filename[1024];
  241. char backing_file[1024]; /* if non zero, the image is a diff of
  242. this file image */
  243. char backing_format[16]; /* if non-zero and backing_file exists */
  244. int is_temporary;
  245. BlockDriverState *backing_hd;
  246. BlockDriverState *file;
  247. /* number of in-flight copy-on-read requests */
  248. unsigned int copy_on_read_in_flight;
  249. /* the time for latest disk I/O */
  250. int64_t slice_time;
  251. int64_t slice_start;
  252. int64_t slice_end;
  253. BlockIOLimit io_limits;
  254. BlockIOBaseValue io_base;
  255. CoQueue throttled_reqs;
  256. QEMUTimer *block_timer;
  257. bool io_limits_enabled;
  258. /* I/O stats (display with "info blockstats"). */
  259. uint64_t nr_bytes[BDRV_MAX_IOTYPE];
  260. uint64_t nr_ops[BDRV_MAX_IOTYPE];
  261. uint64_t total_time_ns[BDRV_MAX_IOTYPE];
  262. uint64_t wr_highest_sector;
  263. /* Whether the disk can expand beyond total_sectors */
  264. int growable;
  265. /* the memory alignment required for the buffers handled by this driver */
  266. int buffer_alignment;
  267. /* do we need to tell the quest if we have a volatile write cache? */
  268. int enable_write_cache;
  269. /* NOTE: the following infos are only hints for real hardware
  270. drivers. They are not used by the block driver */
  271. int cyls, heads, secs, translation;
  272. BlockErrorAction on_read_error, on_write_error;
  273. bool iostatus_enabled;
  274. BlockDeviceIoStatus iostatus;
  275. char device_name[32];
  276. unsigned long *dirty_bitmap;
  277. int64_t dirty_count;
  278. int in_use; /* users other than guest access, eg. block migration */
  279. QTAILQ_ENTRY(BlockDriverState) list;
  280. QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
  281. /* long-running background operation */
  282. BlockJob *job;
  283. };
  284. int get_tmp_filename(char *filename, int size);
  285. void bdrv_set_io_limits(BlockDriverState *bs,
  286. BlockIOLimit *io_limits);
  287. #ifdef _WIN32
  288. int is_windows_drive(const char *filename);
  289. #endif
  290. /**
  291. * block_job_create:
  292. * @job_type: The class object for the newly-created job.
  293. * @bs: The block
  294. * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
  295. * @cb: Completion function for the job.
  296. * @opaque: Opaque pointer value passed to @cb.
  297. * @errp: Error object.
  298. *
  299. * Create a new long-running block device job and return it. The job
  300. * will call @cb asynchronously when the job completes. Note that
  301. * @bs may have been closed at the time the @cb it is called. If
  302. * this is the case, the job may be reported as either cancelled or
  303. * completed.
  304. *
  305. * This function is not part of the public job interface; it should be
  306. * called from a wrapper that is specific to the job type.
  307. */
  308. void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs,
  309. int64_t speed, BlockDriverCompletionFunc *cb,
  310. void *opaque, Error **errp);
  311. /**
  312. * block_job_sleep_ns:
  313. * @job: The job that calls the function.
  314. * @clock: The clock to sleep on.
  315. * @ns: How many nanoseconds to stop for.
  316. *
  317. * Put the job to sleep (assuming that it wasn't canceled) for @ns
  318. * nanoseconds. Canceling the job will interrupt the wait immediately.
  319. */
  320. void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns);
  321. /**
  322. * block_job_complete:
  323. * @job: The job being completed.
  324. * @ret: The status code.
  325. *
  326. * Call the completion function that was registered at creation time, and
  327. * free @job.
  328. */
  329. void block_job_complete(BlockJob *job, int ret);
  330. /**
  331. * block_job_set_speed:
  332. * @job: The job to set the speed for.
  333. * @speed: The new value
  334. * @errp: Error object.
  335. *
  336. * Set a rate-limiting parameter for the job; the actual meaning may
  337. * vary depending on the job type.
  338. */
  339. void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
  340. /**
  341. * block_job_cancel:
  342. * @job: The job to be canceled.
  343. *
  344. * Asynchronously cancel the specified job.
  345. */
  346. void block_job_cancel(BlockJob *job);
  347. /**
  348. * block_job_is_cancelled:
  349. * @job: The job being queried.
  350. *
  351. * Returns whether the job is scheduled for cancellation.
  352. */
  353. bool block_job_is_cancelled(BlockJob *job);
  354. /**
  355. * block_job_cancel:
  356. * @job: The job to be canceled.
  357. *
  358. * Asynchronously cancel the job and wait for it to reach a quiescent
  359. * state. Note that the completion callback will still be called
  360. * asynchronously, hence it is *not* valid to call #bdrv_delete
  361. * immediately after #block_job_cancel_sync. Users of block jobs
  362. * will usually protect the BlockDriverState objects with a reference
  363. * count, should this be a concern.
  364. *
  365. * Returns the return value from the job if the job actually completed
  366. * during the call, or -ECANCELED if it was canceled.
  367. */
  368. int block_job_cancel_sync(BlockJob *job);
  369. /**
  370. * stream_start:
  371. * @bs: Block device to operate on.
  372. * @base: Block device that will become the new base, or %NULL to
  373. * flatten the whole backing file chain onto @bs.
  374. * @base_id: The file name that will be written to @bs as the new
  375. * backing file if the job completes. Ignored if @base is %NULL.
  376. * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
  377. * @cb: Completion function for the job.
  378. * @opaque: Opaque pointer value passed to @cb.
  379. * @errp: Error object.
  380. *
  381. * Start a streaming operation on @bs. Clusters that are unallocated
  382. * in @bs, but allocated in any image between @base and @bs (both
  383. * exclusive) will be written to @bs. At the end of a successful
  384. * streaming job, the backing file of @bs will be changed to
  385. * @base_id in the written image and to @base in the live BlockDriverState.
  386. */
  387. void stream_start(BlockDriverState *bs, BlockDriverState *base,
  388. const char *base_id, int64_t speed,
  389. BlockDriverCompletionFunc *cb,
  390. void *opaque, Error **errp);
  391. #endif /* BLOCK_INT_H */