xref: /openbmc/qemu/hw/block/block.c (revision e8d1e0cd)
1 /*
2  * Common code for block device models
3  *
4  * Copyright (C) 2012 Red Hat, Inc.
5  *
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  */
9 
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"
17 
18 /*
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.
23  *
24  * Return 0 on success, non-zero on error.
25  */
26 static int blk_pread_nonzeroes(BlockBackend *blk, hwaddr size, void *buf)
27 {
28     int ret;
29     int64_t bytes, offset = 0;
30     BlockDriverState *bs = blk_bs(blk);
31 
32     for (;;) {
33         bytes = MIN(size - offset, BDRV_REQUEST_MAX_SECTORS);
34         if (bytes <= 0) {
35             return 0;
36         }
37         ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
38         if (ret < 0) {
39             return ret;
40         }
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;
45             }
46         }
47         offset += bytes;
48     }
49 }
50 
51 /*
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.
63  */
64 bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size,
65                                  Error **errp)
66 {
67     int64_t blk_len;
68     int ret;
69 
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;
75     }
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;
81     }
82 
83     /*
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.
88      */
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;
94     }
95     return true;
96 }
97 
98 bool blkconf_blocksizes(BlockConf *conf, Error **errp)
99 {
100     BlockBackend *blk = conf->blk;
101     BlockSizes blocksizes;
102     BlockDriverState *bs;
103     bool use_blocksizes;
104     bool use_bs;
105 
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;
111 
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;
117 
118     case ON_OFF_AUTO_OFF:
119         use_blocksizes = false;
120         use_bs = false;
121         break;
122 
123     default:
124         abort();
125     }
126 
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;
133         }
134     }
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;
140         }
141     }
142     if (use_bs) {
143         if (!conf->opt_io_size) {
144             conf->opt_io_size = bs->bl.opt_transfer;
145         }
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;
151             }
152         }
153     }
154 
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;
159     }
160 
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;
165     }
166 
167     /*
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
170      */
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;
175     }
176 
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;
181     }
182 
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;
189     }
190 
191     return true;
192 }
193 
194 bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,
195                                    bool resizable, Error **errp)
196 {
197     BlockBackend *blk = conf->blk;
198     BlockdevOnError rerror, werror;
199     uint64_t perm, shared_perm;
200     bool wce;
201     int ret;
202 
203     perm = BLK_PERM_CONSISTENT_READ;
204     if (!readonly) {
205         perm |= BLK_PERM_WRITE;
206     }
207 
208     shared_perm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED;
209     if (resizable) {
210         shared_perm |= BLK_PERM_RESIZE;
211     }
212     if (conf->share_rw) {
213         shared_perm |= BLK_PERM_WRITE;
214     }
215 
216     ret = blk_set_perm(blk, perm, shared_perm, errp);
217     if (ret < 0) {
218         return false;
219     }
220 
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();
227     }
228 
229     rerror = conf->rerror;
230     if (rerror == BLOCKDEV_ON_ERROR_AUTO) {
231         rerror = blk_get_on_error(blk, true);
232     }
233 
234     werror = conf->werror;
235     if (werror == BLOCKDEV_ON_ERROR_AUTO) {
236         werror = blk_get_on_error(blk, false);
237     }
238 
239     blk_set_enable_write_cache(blk, wce);
240     blk_set_on_error(blk, rerror, werror);
241 
242     block_acct_setup(blk_get_stats(blk), conf->account_invalid,
243                      conf->account_failed);
244     return true;
245 }
246 
247 bool blkconf_geometry(BlockConf *conf, int *ptrans,
248                       unsigned cyls_max, unsigned heads_max, unsigned secs_max,
249                       Error **errp)
250 {
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);
257     }
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;
262         }
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;
266         }
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;
270         }
271     }
272     return true;
273 }
274