2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
28 #include "qemu-option.h"
29 #include "qemu-queue.h"
30 #include "qemu-coroutine.h"
31 #include "qemu-timer.h"
32 #include "qapi-types.h"
34 #define BLOCK_FLAG_ENCRYPT 1
35 #define BLOCK_FLAG_COMPAT6 4
36 #define BLOCK_FLAG_LAZY_REFCOUNTS 8
38 #define BLOCK_IO_LIMIT_READ 0
39 #define BLOCK_IO_LIMIT_WRITE 1
40 #define BLOCK_IO_LIMIT_TOTAL 2
42 #define BLOCK_IO_SLICE_TIME 100000000
43 #define NANOSECONDS_PER_SECOND 1000000000.0
45 #define BLOCK_OPT_SIZE "size"
46 #define BLOCK_OPT_ENCRYPT "encryption"
47 #define BLOCK_OPT_COMPAT6 "compat6"
48 #define BLOCK_OPT_BACKING_FILE "backing_file"
49 #define BLOCK_OPT_BACKING_FMT "backing_fmt"
50 #define BLOCK_OPT_CLUSTER_SIZE "cluster_size"
51 #define BLOCK_OPT_TABLE_SIZE "table_size"
52 #define BLOCK_OPT_PREALLOC "preallocation"
53 #define BLOCK_OPT_SUBFMT "subformat"
54 #define BLOCK_OPT_COMPAT_LEVEL "compat"
55 #define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts"
57 typedef struct BdrvTrackedRequest BdrvTrackedRequest
;
59 typedef struct BlockIOLimit
{
64 typedef struct BlockIOBaseValue
{
69 typedef struct BlockJob BlockJob
;
74 * A class type for block job objects.
76 typedef struct BlockJobType
{
77 /** Derived BlockJob struct size */
80 /** String describing the operation, part of query-block-jobs QMP API */
83 /** Optional callback for job types that support setting a speed limit */
84 void (*set_speed
)(BlockJob
*job
, int64_t speed
, Error
**errp
);
90 * Long-running operation on a BlockDriverState.
93 /** The job type, including the job vtable. */
94 const BlockJobType
*job_type
;
96 /** The block device on which the job is operating. */
100 * The coroutine that executes the job. If not NULL, it is
101 * reentered when busy is false and the job is cancelled.
106 * Set to true if the job should cancel itself. The flag must
107 * always be tested just before toggling the busy flag from false
108 * to true. After a job has been cancelled, it should only yield
109 * if #qemu_aio_wait will ("sooner or later") reenter the coroutine.
114 * Set to false by the job while it is in a quiescent state, where
115 * no I/O is pending and the job has yielded on any condition
116 * that is not detected by #qemu_aio_wait, such as a timer.
120 /** Offset that is published by the query-block-jobs QMP API */
123 /** Length that is published by the query-block-jobs QMP API */
126 /** Speed that was set with @block_job_set_speed. */
129 /** The completion function that will be called when the job completes. */
130 BlockDriverCompletionFunc
*cb
;
132 /** The opaque value that is passed to the completion function. */
137 const char *format_name
;
139 int (*bdrv_probe
)(const uint8_t *buf
, int buf_size
, const char *filename
);
140 int (*bdrv_probe_device
)(const char *filename
);
141 int (*bdrv_open
)(BlockDriverState
*bs
, int flags
);
142 int (*bdrv_file_open
)(BlockDriverState
*bs
, const char *filename
, int flags
);
143 int (*bdrv_read
)(BlockDriverState
*bs
, int64_t sector_num
,
144 uint8_t *buf
, int nb_sectors
);
145 int (*bdrv_write
)(BlockDriverState
*bs
, int64_t sector_num
,
146 const uint8_t *buf
, int nb_sectors
);
147 void (*bdrv_close
)(BlockDriverState
*bs
);
148 void (*bdrv_rebind
)(BlockDriverState
*bs
);
149 int (*bdrv_create
)(const char *filename
, QEMUOptionParameter
*options
);
150 int (*bdrv_set_key
)(BlockDriverState
*bs
, const char *key
);
151 int (*bdrv_make_empty
)(BlockDriverState
*bs
);
153 BlockDriverAIOCB
*(*bdrv_aio_readv
)(BlockDriverState
*bs
,
154 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
155 BlockDriverCompletionFunc
*cb
, void *opaque
);
156 BlockDriverAIOCB
*(*bdrv_aio_writev
)(BlockDriverState
*bs
,
157 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
158 BlockDriverCompletionFunc
*cb
, void *opaque
);
159 BlockDriverAIOCB
*(*bdrv_aio_flush
)(BlockDriverState
*bs
,
160 BlockDriverCompletionFunc
*cb
, void *opaque
);
161 BlockDriverAIOCB
*(*bdrv_aio_discard
)(BlockDriverState
*bs
,
162 int64_t sector_num
, int nb_sectors
,
163 BlockDriverCompletionFunc
*cb
, void *opaque
);
165 int coroutine_fn (*bdrv_co_readv
)(BlockDriverState
*bs
,
166 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
167 int coroutine_fn (*bdrv_co_writev
)(BlockDriverState
*bs
,
168 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
170 * Efficiently zero a region of the disk image. Typically an image format
171 * would use a compact metadata representation to implement this. This
172 * function pointer may be NULL and .bdrv_co_writev() will be called
175 int coroutine_fn (*bdrv_co_write_zeroes
)(BlockDriverState
*bs
,
176 int64_t sector_num
, int nb_sectors
);
177 int coroutine_fn (*bdrv_co_discard
)(BlockDriverState
*bs
,
178 int64_t sector_num
, int nb_sectors
);
179 int coroutine_fn (*bdrv_co_is_allocated
)(BlockDriverState
*bs
,
180 int64_t sector_num
, int nb_sectors
, int *pnum
);
183 * Invalidate any cached meta-data.
185 void (*bdrv_invalidate_cache
)(BlockDriverState
*bs
);
188 * Flushes all data that was already written to the OS all the way down to
189 * the disk (for example raw-posix calls fsync()).
191 int coroutine_fn (*bdrv_co_flush_to_disk
)(BlockDriverState
*bs
);
194 * Flushes all internal caches to the OS. The data may still sit in a
195 * writeback cache of the host OS, but it will survive a crash of the qemu
198 int coroutine_fn (*bdrv_co_flush_to_os
)(BlockDriverState
*bs
);
200 const char *protocol_name
;
201 int (*bdrv_truncate
)(BlockDriverState
*bs
, int64_t offset
);
202 int64_t (*bdrv_getlength
)(BlockDriverState
*bs
);
203 int64_t (*bdrv_get_allocated_file_size
)(BlockDriverState
*bs
);
204 int (*bdrv_write_compressed
)(BlockDriverState
*bs
, int64_t sector_num
,
205 const uint8_t *buf
, int nb_sectors
);
207 int (*bdrv_snapshot_create
)(BlockDriverState
*bs
,
208 QEMUSnapshotInfo
*sn_info
);
209 int (*bdrv_snapshot_goto
)(BlockDriverState
*bs
,
210 const char *snapshot_id
);
211 int (*bdrv_snapshot_delete
)(BlockDriverState
*bs
, const char *snapshot_id
);
212 int (*bdrv_snapshot_list
)(BlockDriverState
*bs
,
213 QEMUSnapshotInfo
**psn_info
);
214 int (*bdrv_snapshot_load_tmp
)(BlockDriverState
*bs
,
215 const char *snapshot_name
);
216 int (*bdrv_get_info
)(BlockDriverState
*bs
, BlockDriverInfo
*bdi
);
218 int (*bdrv_save_vmstate
)(BlockDriverState
*bs
, const uint8_t *buf
,
219 int64_t pos
, int size
);
220 int (*bdrv_load_vmstate
)(BlockDriverState
*bs
, uint8_t *buf
,
221 int64_t pos
, int size
);
223 int (*bdrv_change_backing_file
)(BlockDriverState
*bs
,
224 const char *backing_file
, const char *backing_fmt
);
226 /* removable device specific */
227 int (*bdrv_is_inserted
)(BlockDriverState
*bs
);
228 int (*bdrv_media_changed
)(BlockDriverState
*bs
);
229 void (*bdrv_eject
)(BlockDriverState
*bs
, bool eject_flag
);
230 void (*bdrv_lock_medium
)(BlockDriverState
*bs
, bool locked
);
232 /* to control generic scsi devices */
233 int (*bdrv_ioctl
)(BlockDriverState
*bs
, unsigned long int req
, void *buf
);
234 BlockDriverAIOCB
*(*bdrv_aio_ioctl
)(BlockDriverState
*bs
,
235 unsigned long int req
, void *buf
,
236 BlockDriverCompletionFunc
*cb
, void *opaque
);
238 /* List of options for creating images, terminated by name == NULL */
239 QEMUOptionParameter
*create_options
;
243 * Returns 0 for completed check, -errno for internal errors.
244 * The check results are stored in result.
246 int (*bdrv_check
)(BlockDriverState
* bs
, BdrvCheckResult
*result
,
249 void (*bdrv_debug_event
)(BlockDriverState
*bs
, BlkDebugEvent event
);
252 * Returns 1 if newly created images are guaranteed to contain only
253 * zeros, 0 otherwise.
255 int (*bdrv_has_zero_init
)(BlockDriverState
*bs
);
257 QLIST_ENTRY(BlockDriver
) list
;
261 * Note: the function bdrv_append() copies and swaps contents of
262 * BlockDriverStates, so if you add new fields to this struct, please
263 * inspect bdrv_append() to determine if the new fields need to be
266 struct BlockDriverState
{
267 int64_t total_sectors
; /* if we are reading a disk image, give its
269 int read_only
; /* if true, the media is read only */
270 int keep_read_only
; /* if true, the media was requested to stay read only */
271 int open_flags
; /* flags used to open the file, re-used for re-open */
272 int encrypted
; /* if true, the media is encrypted */
273 int valid_key
; /* if true, a valid encryption key has been set */
274 int sg
; /* if true, the device is a /dev/sg* */
275 int copy_on_read
; /* if true, copy read backing sectors into image
276 note this is a reference count */
278 BlockDriver
*drv
; /* NULL means no media */
281 void *dev
; /* attached device model, if any */
282 /* TODO change to DeviceState when all users are qdevified */
283 const BlockDevOps
*dev_ops
;
287 char backing_file
[1024]; /* if non zero, the image is a diff of
289 char backing_format
[16]; /* if non-zero and backing_file exists */
292 BlockDriverState
*backing_hd
;
293 BlockDriverState
*file
;
295 /* number of in-flight copy-on-read requests */
296 unsigned int copy_on_read_in_flight
;
298 /* the time for latest disk I/O */
302 BlockIOLimit io_limits
;
303 BlockIOBaseValue io_base
;
304 CoQueue throttled_reqs
;
305 QEMUTimer
*block_timer
;
306 bool io_limits_enabled
;
308 /* I/O stats (display with "info blockstats"). */
309 uint64_t nr_bytes
[BDRV_MAX_IOTYPE
];
310 uint64_t nr_ops
[BDRV_MAX_IOTYPE
];
311 uint64_t total_time_ns
[BDRV_MAX_IOTYPE
];
312 uint64_t wr_highest_sector
;
314 /* Whether the disk can expand beyond total_sectors */
317 /* the memory alignment required for the buffers handled by this driver */
318 int buffer_alignment
;
320 /* do we need to tell the quest if we have a volatile write cache? */
321 int enable_write_cache
;
323 /* NOTE: the following infos are only hints for real hardware
324 drivers. They are not used by the block driver */
325 BlockErrorAction on_read_error
, on_write_error
;
326 bool iostatus_enabled
;
327 BlockDeviceIoStatus iostatus
;
328 char device_name
[32];
329 unsigned long *dirty_bitmap
;
331 int in_use
; /* users other than guest access, eg. block migration */
332 QTAILQ_ENTRY(BlockDriverState
) list
;
334 QLIST_HEAD(, BdrvTrackedRequest
) tracked_requests
;
336 /* long-running background operation */
340 int get_tmp_filename(char *filename
, int size
);
342 void bdrv_set_io_limits(BlockDriverState
*bs
,
343 BlockIOLimit
*io_limits
);
346 int is_windows_drive(const char *filename
);
351 * @job_type: The class object for the newly-created job.
353 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
354 * @cb: Completion function for the job.
355 * @opaque: Opaque pointer value passed to @cb.
356 * @errp: Error object.
358 * Create a new long-running block device job and return it. The job
359 * will call @cb asynchronously when the job completes. Note that
360 * @bs may have been closed at the time the @cb it is called. If
361 * this is the case, the job may be reported as either cancelled or
364 * This function is not part of the public job interface; it should be
365 * called from a wrapper that is specific to the job type.
367 void *block_job_create(const BlockJobType
*job_type
, BlockDriverState
*bs
,
368 int64_t speed
, BlockDriverCompletionFunc
*cb
,
369 void *opaque
, Error
**errp
);
372 * block_job_sleep_ns:
373 * @job: The job that calls the function.
374 * @clock: The clock to sleep on.
375 * @ns: How many nanoseconds to stop for.
377 * Put the job to sleep (assuming that it wasn't canceled) for @ns
378 * nanoseconds. Canceling the job will interrupt the wait immediately.
380 void block_job_sleep_ns(BlockJob
*job
, QEMUClock
*clock
, int64_t ns
);
383 * block_job_complete:
384 * @job: The job being completed.
385 * @ret: The status code.
387 * Call the completion function that was registered at creation time, and
390 void block_job_complete(BlockJob
*job
, int ret
);
393 * block_job_set_speed:
394 * @job: The job to set the speed for.
395 * @speed: The new value
396 * @errp: Error object.
398 * Set a rate-limiting parameter for the job; the actual meaning may
399 * vary depending on the job type.
401 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
);
405 * @job: The job to be canceled.
407 * Asynchronously cancel the specified job.
409 void block_job_cancel(BlockJob
*job
);
412 * block_job_is_cancelled:
413 * @job: The job being queried.
415 * Returns whether the job is scheduled for cancellation.
417 bool block_job_is_cancelled(BlockJob
*job
);
421 * @job: The job to be canceled.
423 * Asynchronously cancel the job and wait for it to reach a quiescent
424 * state. Note that the completion callback will still be called
425 * asynchronously, hence it is *not* valid to call #bdrv_delete
426 * immediately after #block_job_cancel_sync. Users of block jobs
427 * will usually protect the BlockDriverState objects with a reference
428 * count, should this be a concern.
430 * Returns the return value from the job if the job actually completed
431 * during the call, or -ECANCELED if it was canceled.
433 int block_job_cancel_sync(BlockJob
*job
);
437 * @bs: Block device to operate on.
438 * @base: Block device that will become the new base, or %NULL to
439 * flatten the whole backing file chain onto @bs.
440 * @base_id: The file name that will be written to @bs as the new
441 * backing file if the job completes. Ignored if @base is %NULL.
442 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
443 * @cb: Completion function for the job.
444 * @opaque: Opaque pointer value passed to @cb.
445 * @errp: Error object.
447 * Start a streaming operation on @bs. Clusters that are unallocated
448 * in @bs, but allocated in any image between @base and @bs (both
449 * exclusive) will be written to @bs. At the end of a successful
450 * streaming job, the backing file of @bs will be changed to
451 * @base_id in the written image and to @base in the live BlockDriverState.
453 void stream_start(BlockDriverState
*bs
, BlockDriverState
*base
,
454 const char *base_id
, int64_t speed
,
455 BlockDriverCompletionFunc
*cb
,
456 void *opaque
, Error
**errp
);
458 #endif /* BLOCK_INT_H */