2 * Common code for block device models
4 * Copyright (C) 2012 Red Hat, Inc.
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "block/block_int-common.h"
12 #include "sysemu/blockdev.h"
13 #include "sysemu/block-backend.h"
14 #include "hw/block/block.h"
15 #include "qapi/error.h"
16 #include "qapi/qapi-types-block.h"
19 * Read the non-zeroes parts of @blk into @buf
20 * Reading all of the @blk is expensive if the zeroes parts of @blk
21 * is large enough. Therefore check the block status and only write
22 * the non-zeroes block into @buf.
24 * Return 0 on success, non-zero on error.
26 static int blk_pread_nonzeroes(BlockBackend
*blk
, hwaddr size
, void *buf
)
29 int64_t bytes
, offset
= 0;
30 BlockDriverState
*bs
= blk_bs(blk
);
33 bytes
= MIN(size
- offset
, BDRV_REQUEST_MAX_SECTORS
);
37 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, NULL
, NULL
);
41 if (!(ret
& BDRV_BLOCK_ZERO
)) {
42 ret
= blk_pread(blk
, offset
, bytes
, (uint8_t *) buf
+ offset
, 0);
52 * Read the entire contents of @blk into @buf.
53 * @blk's contents must be @size bytes, and @size must be at most
54 * BDRV_REQUEST_MAX_BYTES.
55 * On success, return true.
56 * On failure, store an error through @errp and return false.
57 * Note that the error messages do not identify the block backend.
58 * TODO Since callers don't either, this can result in confusing
60 * This function not intended for actual block devices, which read on
61 * demand. It's for things like memory devices that (ab)use a block
62 * backend to provide persistence.
64 bool blk_check_size_and_read_all(BlockBackend
*blk
, void *buf
, hwaddr size
,
70 blk_len
= blk_getlength(blk
);
72 error_setg_errno(errp
, -blk_len
,
73 "can't get size of block backend");
76 if (blk_len
!= size
) {
77 error_setg(errp
, "device requires %" HWADDR_PRIu
" bytes, "
78 "block backend provides %" PRIu64
" bytes",
84 * We could loop for @size > BDRV_REQUEST_MAX_BYTES, but if we
85 * ever get to the point we want to read *gigabytes* here, we
86 * should probably rework the device to be more like an actual
87 * block device and read only on demand.
89 assert(size
<= BDRV_REQUEST_MAX_BYTES
);
90 ret
= blk_pread_nonzeroes(blk
, size
, buf
);
92 error_setg_errno(errp
, -ret
, "can't read block backend");
98 bool blkconf_blocksizes(BlockConf
*conf
, Error
**errp
)
100 BlockBackend
*blk
= conf
->blk
;
101 BlockSizes blocksizes
;
102 BlockDriverState
*bs
;
106 switch (conf
->backend_defaults
) {
107 case ON_OFF_AUTO_AUTO
:
108 use_blocksizes
= !blk_probe_blocksizes(blk
, &blocksizes
);
113 use_blocksizes
= !blk_probe_blocksizes(blk
, &blocksizes
);
118 case ON_OFF_AUTO_OFF
:
119 use_blocksizes
= false;
127 /* fill in detected values if they are not defined via qemu command line */
128 if (!conf
->physical_block_size
) {
129 if (use_blocksizes
) {
130 conf
->physical_block_size
= blocksizes
.phys
;
132 conf
->physical_block_size
= BDRV_SECTOR_SIZE
;
135 if (!conf
->logical_block_size
) {
136 if (use_blocksizes
) {
137 conf
->logical_block_size
= blocksizes
.log
;
139 conf
->logical_block_size
= BDRV_SECTOR_SIZE
;
143 if (!conf
->opt_io_size
) {
144 conf
->opt_io_size
= bs
->bl
.opt_transfer
;
146 if (conf
->discard_granularity
== -1) {
147 if (bs
->bl
.pdiscard_alignment
) {
148 conf
->discard_granularity
= bs
->bl
.pdiscard_alignment
;
149 } else if (bs
->bl
.request_alignment
!= 1) {
150 conf
->discard_granularity
= bs
->bl
.request_alignment
;
155 if (conf
->logical_block_size
> conf
->physical_block_size
) {
157 "logical_block_size > physical_block_size not supported");
161 if (!QEMU_IS_ALIGNED(conf
->min_io_size
, conf
->logical_block_size
)) {
163 "min_io_size must be a multiple of logical_block_size");
168 * all devices which support min_io_size (scsi and virtio-blk) expose it to
169 * the guest as a uint16_t in units of logical blocks
171 if (conf
->min_io_size
/ conf
->logical_block_size
> UINT16_MAX
) {
172 error_setg(errp
, "min_io_size must not exceed %u logical blocks",
177 if (!QEMU_IS_ALIGNED(conf
->opt_io_size
, conf
->logical_block_size
)) {
179 "opt_io_size must be a multiple of logical_block_size");
183 if (conf
->discard_granularity
!= -1 &&
184 !QEMU_IS_ALIGNED(conf
->discard_granularity
,
185 conf
->logical_block_size
)) {
186 error_setg(errp
, "discard_granularity must be "
187 "a multiple of logical_block_size");
194 bool blkconf_apply_backend_options(BlockConf
*conf
, bool readonly
,
195 bool resizable
, Error
**errp
)
197 BlockBackend
*blk
= conf
->blk
;
198 BlockdevOnError rerror
, werror
;
199 uint64_t perm
, shared_perm
;
203 perm
= BLK_PERM_CONSISTENT_READ
;
205 perm
|= BLK_PERM_WRITE
;
208 shared_perm
= BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE_UNCHANGED
;
210 shared_perm
|= BLK_PERM_RESIZE
;
212 if (conf
->share_rw
) {
213 shared_perm
|= BLK_PERM_WRITE
;
216 ret
= blk_set_perm(blk
, perm
, shared_perm
, errp
);
222 case ON_OFF_AUTO_ON
: wce
= true; break;
223 case ON_OFF_AUTO_OFF
: wce
= false; break;
224 case ON_OFF_AUTO_AUTO
: wce
= blk_enable_write_cache(blk
); break;
229 rerror
= conf
->rerror
;
230 if (rerror
== BLOCKDEV_ON_ERROR_AUTO
) {
231 rerror
= blk_get_on_error(blk
, true);
234 werror
= conf
->werror
;
235 if (werror
== BLOCKDEV_ON_ERROR_AUTO
) {
236 werror
= blk_get_on_error(blk
, false);
239 blk_set_enable_write_cache(blk
, wce
);
240 blk_set_on_error(blk
, rerror
, werror
);
242 block_acct_setup(blk_get_stats(blk
), conf
->account_invalid
,
243 conf
->account_failed
);
247 bool blkconf_geometry(BlockConf
*conf
, int *ptrans
,
248 unsigned cyls_max
, unsigned heads_max
, unsigned secs_max
,
251 if (!conf
->cyls
&& !conf
->heads
&& !conf
->secs
) {
252 hd_geometry_guess(conf
->blk
,
253 &conf
->cyls
, &conf
->heads
, &conf
->secs
,
255 } else if (ptrans
&& *ptrans
== BIOS_ATA_TRANSLATION_AUTO
) {
256 *ptrans
= hd_bios_chs_auto_trans(conf
->cyls
, conf
->heads
, conf
->secs
);
258 if (conf
->cyls
|| conf
->heads
|| conf
->secs
) {
259 if (conf
->cyls
< 1 || conf
->cyls
> cyls_max
) {
260 error_setg(errp
, "cyls must be between 1 and %u", cyls_max
);
263 if (conf
->heads
< 1 || conf
->heads
> heads_max
) {
264 error_setg(errp
, "heads must be between 1 and %u", heads_max
);
267 if (conf
->secs
< 1 || conf
->secs
> secs_max
) {
268 error_setg(errp
, "secs must be between 1 and %u", secs_max
);