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"
35 #define BLOCK_FLAG_ENCRYPT 1
36 #define BLOCK_FLAG_COMPAT6 4
37 #define BLOCK_FLAG_LAZY_REFCOUNTS 8
39 #define BLOCK_IO_LIMIT_READ 0
40 #define BLOCK_IO_LIMIT_WRITE 1
41 #define BLOCK_IO_LIMIT_TOTAL 2
43 #define BLOCK_IO_SLICE_TIME 100000000
44 #define NANOSECONDS_PER_SECOND 1000000000.0
46 #define BLOCK_OPT_SIZE "size"
47 #define BLOCK_OPT_ENCRYPT "encryption"
48 #define BLOCK_OPT_COMPAT6 "compat6"
49 #define BLOCK_OPT_BACKING_FILE "backing_file"
50 #define BLOCK_OPT_BACKING_FMT "backing_fmt"
51 #define BLOCK_OPT_CLUSTER_SIZE "cluster_size"
52 #define BLOCK_OPT_TABLE_SIZE "table_size"
53 #define BLOCK_OPT_PREALLOC "preallocation"
54 #define BLOCK_OPT_SUBFMT "subformat"
55 #define BLOCK_OPT_COMPAT_LEVEL "compat"
56 #define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts"
58 typedef struct BdrvTrackedRequest BdrvTrackedRequest
;
60 typedef struct BlockIOLimit
{
65 typedef struct BlockIOBaseValue
{
71 const char *format_name
;
73 int (*bdrv_probe
)(const uint8_t *buf
, int buf_size
, const char *filename
);
74 int (*bdrv_probe_device
)(const char *filename
);
76 /* For handling image reopen for split or non-split files */
77 int (*bdrv_reopen_prepare
)(BDRVReopenState
*reopen_state
,
78 BlockReopenQueue
*queue
, Error
**errp
);
79 void (*bdrv_reopen_commit
)(BDRVReopenState
*reopen_state
);
80 void (*bdrv_reopen_abort
)(BDRVReopenState
*reopen_state
);
82 int (*bdrv_open
)(BlockDriverState
*bs
, int flags
);
83 int (*bdrv_file_open
)(BlockDriverState
*bs
, const char *filename
, int flags
);
84 int (*bdrv_read
)(BlockDriverState
*bs
, int64_t sector_num
,
85 uint8_t *buf
, int nb_sectors
);
86 int (*bdrv_write
)(BlockDriverState
*bs
, int64_t sector_num
,
87 const uint8_t *buf
, int nb_sectors
);
88 void (*bdrv_close
)(BlockDriverState
*bs
);
89 void (*bdrv_rebind
)(BlockDriverState
*bs
);
90 int (*bdrv_create
)(const char *filename
, QEMUOptionParameter
*options
);
91 int (*bdrv_set_key
)(BlockDriverState
*bs
, const char *key
);
92 int (*bdrv_make_empty
)(BlockDriverState
*bs
);
94 BlockDriverAIOCB
*(*bdrv_aio_readv
)(BlockDriverState
*bs
,
95 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
96 BlockDriverCompletionFunc
*cb
, void *opaque
);
97 BlockDriverAIOCB
*(*bdrv_aio_writev
)(BlockDriverState
*bs
,
98 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
99 BlockDriverCompletionFunc
*cb
, void *opaque
);
100 BlockDriverAIOCB
*(*bdrv_aio_flush
)(BlockDriverState
*bs
,
101 BlockDriverCompletionFunc
*cb
, void *opaque
);
102 BlockDriverAIOCB
*(*bdrv_aio_discard
)(BlockDriverState
*bs
,
103 int64_t sector_num
, int nb_sectors
,
104 BlockDriverCompletionFunc
*cb
, void *opaque
);
106 int coroutine_fn (*bdrv_co_readv
)(BlockDriverState
*bs
,
107 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
108 int coroutine_fn (*bdrv_co_writev
)(BlockDriverState
*bs
,
109 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
111 * Efficiently zero a region of the disk image. Typically an image format
112 * would use a compact metadata representation to implement this. This
113 * function pointer may be NULL and .bdrv_co_writev() will be called
116 int coroutine_fn (*bdrv_co_write_zeroes
)(BlockDriverState
*bs
,
117 int64_t sector_num
, int nb_sectors
);
118 int coroutine_fn (*bdrv_co_discard
)(BlockDriverState
*bs
,
119 int64_t sector_num
, int nb_sectors
);
120 int coroutine_fn (*bdrv_co_is_allocated
)(BlockDriverState
*bs
,
121 int64_t sector_num
, int nb_sectors
, int *pnum
);
124 * Invalidate any cached meta-data.
126 void (*bdrv_invalidate_cache
)(BlockDriverState
*bs
);
129 * Flushes all data that was already written to the OS all the way down to
130 * the disk (for example raw-posix calls fsync()).
132 int coroutine_fn (*bdrv_co_flush_to_disk
)(BlockDriverState
*bs
);
135 * Flushes all internal caches to the OS. The data may still sit in a
136 * writeback cache of the host OS, but it will survive a crash of the qemu
139 int coroutine_fn (*bdrv_co_flush_to_os
)(BlockDriverState
*bs
);
141 const char *protocol_name
;
142 int (*bdrv_truncate
)(BlockDriverState
*bs
, int64_t offset
);
143 int64_t (*bdrv_getlength
)(BlockDriverState
*bs
);
144 int64_t (*bdrv_get_allocated_file_size
)(BlockDriverState
*bs
);
145 int (*bdrv_write_compressed
)(BlockDriverState
*bs
, int64_t sector_num
,
146 const uint8_t *buf
, int nb_sectors
);
148 int (*bdrv_snapshot_create
)(BlockDriverState
*bs
,
149 QEMUSnapshotInfo
*sn_info
);
150 int (*bdrv_snapshot_goto
)(BlockDriverState
*bs
,
151 const char *snapshot_id
);
152 int (*bdrv_snapshot_delete
)(BlockDriverState
*bs
, const char *snapshot_id
);
153 int (*bdrv_snapshot_list
)(BlockDriverState
*bs
,
154 QEMUSnapshotInfo
**psn_info
);
155 int (*bdrv_snapshot_load_tmp
)(BlockDriverState
*bs
,
156 const char *snapshot_name
);
157 int (*bdrv_get_info
)(BlockDriverState
*bs
, BlockDriverInfo
*bdi
);
159 int (*bdrv_save_vmstate
)(BlockDriverState
*bs
, const uint8_t *buf
,
160 int64_t pos
, int size
);
161 int (*bdrv_load_vmstate
)(BlockDriverState
*bs
, uint8_t *buf
,
162 int64_t pos
, int size
);
164 int (*bdrv_change_backing_file
)(BlockDriverState
*bs
,
165 const char *backing_file
, const char *backing_fmt
);
167 /* removable device specific */
168 int (*bdrv_is_inserted
)(BlockDriverState
*bs
);
169 int (*bdrv_media_changed
)(BlockDriverState
*bs
);
170 void (*bdrv_eject
)(BlockDriverState
*bs
, bool eject_flag
);
171 void (*bdrv_lock_medium
)(BlockDriverState
*bs
, bool locked
);
173 /* to control generic scsi devices */
174 int (*bdrv_ioctl
)(BlockDriverState
*bs
, unsigned long int req
, void *buf
);
175 BlockDriverAIOCB
*(*bdrv_aio_ioctl
)(BlockDriverState
*bs
,
176 unsigned long int req
, void *buf
,
177 BlockDriverCompletionFunc
*cb
, void *opaque
);
179 /* List of options for creating images, terminated by name == NULL */
180 QEMUOptionParameter
*create_options
;
184 * Returns 0 for completed check, -errno for internal errors.
185 * The check results are stored in result.
187 int (*bdrv_check
)(BlockDriverState
* bs
, BdrvCheckResult
*result
,
190 void (*bdrv_debug_event
)(BlockDriverState
*bs
, BlkDebugEvent event
);
193 * Returns 1 if newly created images are guaranteed to contain only
194 * zeros, 0 otherwise.
196 int (*bdrv_has_zero_init
)(BlockDriverState
*bs
);
198 QLIST_ENTRY(BlockDriver
) list
;
202 * Note: the function bdrv_append() copies and swaps contents of
203 * BlockDriverStates, so if you add new fields to this struct, please
204 * inspect bdrv_append() to determine if the new fields need to be
207 struct BlockDriverState
{
208 int64_t total_sectors
; /* if we are reading a disk image, give its
210 int read_only
; /* if true, the media is read only */
211 int open_flags
; /* flags used to open the file, re-used for re-open */
212 int encrypted
; /* if true, the media is encrypted */
213 int valid_key
; /* if true, a valid encryption key has been set */
214 int sg
; /* if true, the device is a /dev/sg* */
215 int copy_on_read
; /* if true, copy read backing sectors into image
216 note this is a reference count */
218 BlockDriver
*drv
; /* NULL means no media */
221 void *dev
; /* attached device model, if any */
222 /* TODO change to DeviceState when all users are qdevified */
223 const BlockDevOps
*dev_ops
;
227 char backing_file
[1024]; /* if non zero, the image is a diff of
229 char backing_format
[16]; /* if non-zero and backing_file exists */
232 BlockDriverState
*backing_hd
;
233 BlockDriverState
*file
;
235 /* number of in-flight copy-on-read requests */
236 unsigned int copy_on_read_in_flight
;
238 /* the time for latest disk I/O */
242 BlockIOLimit io_limits
;
243 BlockIOBaseValue io_base
;
244 CoQueue throttled_reqs
;
245 QEMUTimer
*block_timer
;
246 bool io_limits_enabled
;
248 /* I/O stats (display with "info blockstats"). */
249 uint64_t nr_bytes
[BDRV_MAX_IOTYPE
];
250 uint64_t nr_ops
[BDRV_MAX_IOTYPE
];
251 uint64_t total_time_ns
[BDRV_MAX_IOTYPE
];
252 uint64_t wr_highest_sector
;
254 /* Whether the disk can expand beyond total_sectors */
257 /* the memory alignment required for the buffers handled by this driver */
258 int buffer_alignment
;
260 /* do we need to tell the quest if we have a volatile write cache? */
261 int enable_write_cache
;
263 /* NOTE: the following infos are only hints for real hardware
264 drivers. They are not used by the block driver */
265 BlockErrorAction on_read_error
, on_write_error
;
266 bool iostatus_enabled
;
267 BlockDeviceIoStatus iostatus
;
268 char device_name
[32];
269 unsigned long *dirty_bitmap
;
271 int in_use
; /* users other than guest access, eg. block migration */
272 QTAILQ_ENTRY(BlockDriverState
) list
;
274 QLIST_HEAD(, BdrvTrackedRequest
) tracked_requests
;
276 /* long-running background operation */
281 int get_tmp_filename(char *filename
, int size
);
283 void bdrv_set_io_limits(BlockDriverState
*bs
,
284 BlockIOLimit
*io_limits
);
287 int is_windows_drive(const char *filename
);
292 * @bs: Block device to operate on.
293 * @base: Block device that will become the new base, or %NULL to
294 * flatten the whole backing file chain onto @bs.
295 * @base_id: The file name that will be written to @bs as the new
296 * backing file if the job completes. Ignored if @base is %NULL.
297 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
298 * @cb: Completion function for the job.
299 * @opaque: Opaque pointer value passed to @cb.
300 * @errp: Error object.
302 * Start a streaming operation on @bs. Clusters that are unallocated
303 * in @bs, but allocated in any image between @base and @bs (both
304 * exclusive) will be written to @bs. At the end of a successful
305 * streaming job, the backing file of @bs will be changed to
306 * @base_id in the written image and to @base in the live BlockDriverState.
308 void stream_start(BlockDriverState
*bs
, BlockDriverState
*base
,
309 const char *base_id
, int64_t speed
,
310 BlockDriverCompletionFunc
*cb
,
311 void *opaque
, Error
**errp
);
315 * @bs: Top Block device
316 * @base: Block device that will be written into, and become the new top
317 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
318 * @on_error: The action to take upon error.
319 * @cb: Completion function for the job.
320 * @opaque: Opaque pointer value passed to @cb.
321 * @errp: Error object.
324 void commit_start(BlockDriverState
*bs
, BlockDriverState
*base
,
325 BlockDriverState
*top
, int64_t speed
,
326 BlockErrorAction on_error
, BlockDriverCompletionFunc
*cb
,
327 void *opaque
, Error
**errp
);
329 #endif /* BLOCK_INT_H */