xref: /openbmc/qemu/hw/block/block.c (revision 93dd625f)
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 "sysemu/blockdev.h"
12 #include "sysemu/block-backend.h"
13 #include "hw/block/block.h"
14 #include "qapi/error.h"
15 #include "qapi/qapi-types-block.h"
16 
17 /*
18  * Read the entire contents of @blk into @buf.
19  * @blk's contents must be @size bytes, and @size must be at most
20  * BDRV_REQUEST_MAX_BYTES.
21  * On success, return true.
22  * On failure, store an error through @errp and return false.
23  * Note that the error messages do not identify the block backend.
24  * TODO Since callers don't either, this can result in confusing
25  * errors.
26  * This function not intended for actual block devices, which read on
27  * demand.  It's for things like memory devices that (ab)use a block
28  * backend to provide persistence.
29  */
30 bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size,
31                                  Error **errp)
32 {
33     int64_t blk_len;
34     int ret;
35 
36     blk_len = blk_getlength(blk);
37     if (blk_len < 0) {
38         error_setg_errno(errp, -blk_len,
39                          "can't get size of block backend");
40         return false;
41     }
42     if (blk_len != size) {
43         error_setg(errp, "device requires %" HWADDR_PRIu " bytes, "
44                    "block backend provides %" PRIu64 " bytes",
45                    size, blk_len);
46         return false;
47     }
48 
49     /*
50      * We could loop for @size > BDRV_REQUEST_MAX_BYTES, but if we
51      * ever get to the point we want to read *gigabytes* here, we
52      * should probably rework the device to be more like an actual
53      * block device and read only on demand.
54      */
55     assert(size <= BDRV_REQUEST_MAX_BYTES);
56     ret = blk_pread(blk, 0, buf, size);
57     if (ret < 0) {
58         error_setg_errno(errp, -ret, "can't read block backend");
59         return false;
60     }
61     return true;
62 }
63 
64 bool blkconf_blocksizes(BlockConf *conf, Error **errp)
65 {
66     BlockBackend *blk = conf->blk;
67     BlockSizes blocksizes;
68     int backend_ret;
69 
70     backend_ret = blk_probe_blocksizes(blk, &blocksizes);
71     /* fill in detected values if they are not defined via qemu command line */
72     if (!conf->physical_block_size) {
73         if (!backend_ret) {
74            conf->physical_block_size = blocksizes.phys;
75         } else {
76             conf->physical_block_size = BDRV_SECTOR_SIZE;
77         }
78     }
79     if (!conf->logical_block_size) {
80         if (!backend_ret) {
81             conf->logical_block_size = blocksizes.log;
82         } else {
83             conf->logical_block_size = BDRV_SECTOR_SIZE;
84         }
85     }
86 
87     if (conf->logical_block_size > conf->physical_block_size) {
88         error_setg(errp,
89                    "logical_block_size > physical_block_size not supported");
90         return false;
91     }
92 
93     if (!QEMU_IS_ALIGNED(conf->min_io_size, conf->logical_block_size)) {
94         error_setg(errp,
95                    "min_io_size must be a multiple of logical_block_size");
96         return false;
97     }
98 
99     /*
100      * all devices which support min_io_size (scsi and virtio-blk) expose it to
101      * the guest as a uint16_t in units of logical blocks
102      */
103     if (conf->min_io_size / conf->logical_block_size > UINT16_MAX) {
104         error_setg(errp, "min_io_size must not exceed %u logical blocks",
105                    UINT16_MAX);
106         return false;
107     }
108 
109     if (!QEMU_IS_ALIGNED(conf->opt_io_size, conf->logical_block_size)) {
110         error_setg(errp,
111                    "opt_io_size must be a multiple of logical_block_size");
112         return false;
113     }
114 
115     if (conf->discard_granularity != -1 &&
116         !QEMU_IS_ALIGNED(conf->discard_granularity,
117                          conf->logical_block_size)) {
118         error_setg(errp, "discard_granularity must be "
119                    "a multiple of logical_block_size");
120         return false;
121     }
122 
123     return true;
124 }
125 
126 bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,
127                                    bool resizable, Error **errp)
128 {
129     BlockBackend *blk = conf->blk;
130     BlockdevOnError rerror, werror;
131     uint64_t perm, shared_perm;
132     bool wce;
133     int ret;
134 
135     perm = BLK_PERM_CONSISTENT_READ;
136     if (!readonly) {
137         perm |= BLK_PERM_WRITE;
138     }
139 
140     shared_perm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
141                   BLK_PERM_GRAPH_MOD;
142     if (resizable) {
143         shared_perm |= BLK_PERM_RESIZE;
144     }
145     if (conf->share_rw) {
146         shared_perm |= BLK_PERM_WRITE;
147     }
148 
149     ret = blk_set_perm(blk, perm, shared_perm, errp);
150     if (ret < 0) {
151         return false;
152     }
153 
154     switch (conf->wce) {
155     case ON_OFF_AUTO_ON:    wce = true; break;
156     case ON_OFF_AUTO_OFF:   wce = false; break;
157     case ON_OFF_AUTO_AUTO:  wce = blk_enable_write_cache(blk); break;
158     default:
159         abort();
160     }
161 
162     rerror = conf->rerror;
163     if (rerror == BLOCKDEV_ON_ERROR_AUTO) {
164         rerror = blk_get_on_error(blk, true);
165     }
166 
167     werror = conf->werror;
168     if (werror == BLOCKDEV_ON_ERROR_AUTO) {
169         werror = blk_get_on_error(blk, false);
170     }
171 
172     blk_set_enable_write_cache(blk, wce);
173     blk_set_on_error(blk, rerror, werror);
174 
175     return true;
176 }
177 
178 bool blkconf_geometry(BlockConf *conf, int *ptrans,
179                       unsigned cyls_max, unsigned heads_max, unsigned secs_max,
180                       Error **errp)
181 {
182     if (!conf->cyls && !conf->heads && !conf->secs) {
183         hd_geometry_guess(conf->blk,
184                           &conf->cyls, &conf->heads, &conf->secs,
185                           ptrans);
186     } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) {
187         *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs);
188     }
189     if (conf->cyls || conf->heads || conf->secs) {
190         if (conf->cyls < 1 || conf->cyls > cyls_max) {
191             error_setg(errp, "cyls must be between 1 and %u", cyls_max);
192             return false;
193         }
194         if (conf->heads < 1 || conf->heads > heads_max) {
195             error_setg(errp, "heads must be between 1 and %u", heads_max);
196             return false;
197         }
198         if (conf->secs < 1 || conf->secs > secs_max) {
199             error_setg(errp, "secs must be between 1 and %u", secs_max);
200             return false;
201         }
202     }
203     return true;
204 }
205