qapi: Improve specificity of type/member descriptions
[qemu/armbru.git] / hw / block / block.c
blob9f52ee6e728e34f8779a30e2afe7628b29e2dde9
1 /*
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.
8 */
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)
28 int ret;
29 int64_t bytes, offset = 0;
30 BlockDriverState *bs = blk_bs(blk);
32 for (;;) {
33 bytes = MIN(size - offset, BDRV_REQUEST_MAX_SECTORS);
34 if (bytes <= 0) {
35 return 0;
37 ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
38 if (ret < 0) {
39 return ret;
41 if (!(ret & BDRV_BLOCK_ZERO)) {
42 ret = blk_pread(blk, offset, bytes, (uint8_t *) buf + offset, 0);
43 if (ret < 0) {
44 return ret;
47 offset += bytes;
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
59 * errors.
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,
65 Error **errp)
67 int64_t blk_len;
68 int ret;
70 blk_len = blk_getlength(blk);
71 if (blk_len < 0) {
72 error_setg_errno(errp, -blk_len,
73 "can't get size of block backend");
74 return false;
76 if (blk_len != size) {
77 error_setg(errp, "device requires %" HWADDR_PRIu " bytes, "
78 "block backend provides %" PRIu64 " bytes",
79 size, blk_len);
80 return false;
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);
91 if (ret < 0) {
92 error_setg_errno(errp, -ret, "can't read block backend");
93 return false;
95 return true;
98 bool blkconf_blocksizes(BlockConf *conf, Error **errp)
100 BlockBackend *blk = conf->blk;
101 BlockSizes blocksizes;
102 BlockDriverState *bs;
103 bool use_blocksizes;
104 bool use_bs;
106 switch (conf->backend_defaults) {
107 case ON_OFF_AUTO_AUTO:
108 use_blocksizes = !blk_probe_blocksizes(blk, &blocksizes);
109 use_bs = false;
110 break;
112 case ON_OFF_AUTO_ON:
113 use_blocksizes = !blk_probe_blocksizes(blk, &blocksizes);
114 bs = blk_bs(blk);
115 use_bs = bs;
116 break;
118 case ON_OFF_AUTO_OFF:
119 use_blocksizes = false;
120 use_bs = false;
121 break;
123 default:
124 abort();
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;
131 } else {
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;
138 } else {
139 conf->logical_block_size = BDRV_SECTOR_SIZE;
142 if (use_bs) {
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) {
156 error_setg(errp,
157 "logical_block_size > physical_block_size not supported");
158 return false;
161 if (!QEMU_IS_ALIGNED(conf->min_io_size, conf->logical_block_size)) {
162 error_setg(errp,
163 "min_io_size must be a multiple of logical_block_size");
164 return false;
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",
173 UINT16_MAX);
174 return false;
177 if (!QEMU_IS_ALIGNED(conf->opt_io_size, conf->logical_block_size)) {
178 error_setg(errp,
179 "opt_io_size must be a multiple of logical_block_size");
180 return false;
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");
188 return false;
191 return true;
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;
200 bool wce;
201 int ret;
203 perm = BLK_PERM_CONSISTENT_READ;
204 if (!readonly) {
205 perm |= BLK_PERM_WRITE;
208 shared_perm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED;
209 if (resizable) {
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);
217 if (ret < 0) {
218 return false;
221 switch (conf->wce) {
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;
225 default:
226 abort();
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);
244 return true;
247 bool blkconf_geometry(BlockConf *conf, int *ptrans,
248 unsigned cyls_max, unsigned heads_max, unsigned secs_max,
249 Error **errp)
251 if (!conf->cyls && !conf->heads && !conf->secs) {
252 hd_geometry_guess(conf->blk,
253 &conf->cyls, &conf->heads, &conf->secs,
254 ptrans);
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);
261 return false;
263 if (conf->heads < 1 || conf->heads > heads_max) {
264 error_setg(errp, "heads must be between 1 and %u", heads_max);
265 return false;
267 if (conf->secs < 1 || conf->secs > secs_max) {
268 error_setg(errp, "secs must be between 1 and %u", secs_max);
269 return false;
272 return true;