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 = bdrv_pread(bs->file, offset, bytes, 43 (uint8_t *) buf + offset, 0); 44 if (ret < 0) { 45 return ret; 46 } 47 } 48 offset += bytes; 49 } 50 } 51 52 /* 53 * Read the entire contents of @blk into @buf. 54 * @blk's contents must be @size bytes, and @size must be at most 55 * BDRV_REQUEST_MAX_BYTES. 56 * On success, return true. 57 * On failure, store an error through @errp and return false. 58 * Note that the error messages do not identify the block backend. 59 * TODO Since callers don't either, this can result in confusing 60 * errors. 61 * This function not intended for actual block devices, which read on 62 * demand. It's for things like memory devices that (ab)use a block 63 * backend to provide persistence. 64 */ 65 bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size, 66 Error **errp) 67 { 68 int64_t blk_len; 69 int ret; 70 71 blk_len = blk_getlength(blk); 72 if (blk_len < 0) { 73 error_setg_errno(errp, -blk_len, 74 "can't get size of block backend"); 75 return false; 76 } 77 if (blk_len != size) { 78 error_setg(errp, "device requires %" HWADDR_PRIu " bytes, " 79 "block backend provides %" PRIu64 " bytes", 80 size, blk_len); 81 return false; 82 } 83 84 /* 85 * We could loop for @size > BDRV_REQUEST_MAX_BYTES, but if we 86 * ever get to the point we want to read *gigabytes* here, we 87 * should probably rework the device to be more like an actual 88 * block device and read only on demand. 89 */ 90 assert(size <= BDRV_REQUEST_MAX_BYTES); 91 ret = blk_pread_nonzeroes(blk, size, buf); 92 if (ret < 0) { 93 error_setg_errno(errp, -ret, "can't read block backend"); 94 return false; 95 } 96 return true; 97 } 98 99 bool blkconf_blocksizes(BlockConf *conf, Error **errp) 100 { 101 BlockBackend *blk = conf->blk; 102 BlockSizes blocksizes; 103 BlockDriverState *bs; 104 bool use_blocksizes; 105 bool use_bs; 106 107 switch (conf->backend_defaults) { 108 case ON_OFF_AUTO_AUTO: 109 use_blocksizes = !blk_probe_blocksizes(blk, &blocksizes); 110 use_bs = false; 111 break; 112 113 case ON_OFF_AUTO_ON: 114 use_blocksizes = !blk_probe_blocksizes(blk, &blocksizes); 115 bs = blk_bs(blk); 116 use_bs = bs; 117 break; 118 119 case ON_OFF_AUTO_OFF: 120 use_blocksizes = false; 121 use_bs = false; 122 break; 123 124 default: 125 abort(); 126 } 127 128 /* fill in detected values if they are not defined via qemu command line */ 129 if (!conf->physical_block_size) { 130 if (use_blocksizes) { 131 conf->physical_block_size = blocksizes.phys; 132 } else { 133 conf->physical_block_size = BDRV_SECTOR_SIZE; 134 } 135 } 136 if (!conf->logical_block_size) { 137 if (use_blocksizes) { 138 conf->logical_block_size = blocksizes.log; 139 } else { 140 conf->logical_block_size = BDRV_SECTOR_SIZE; 141 } 142 } 143 if (use_bs) { 144 if (!conf->opt_io_size) { 145 conf->opt_io_size = bs->bl.opt_transfer; 146 } 147 if (conf->discard_granularity == -1) { 148 if (bs->bl.pdiscard_alignment) { 149 conf->discard_granularity = bs->bl.pdiscard_alignment; 150 } else if (bs->bl.request_alignment != 1) { 151 conf->discard_granularity = bs->bl.request_alignment; 152 } 153 } 154 } 155 156 if (conf->logical_block_size > conf->physical_block_size) { 157 error_setg(errp, 158 "logical_block_size > physical_block_size not supported"); 159 return false; 160 } 161 162 if (!QEMU_IS_ALIGNED(conf->min_io_size, conf->logical_block_size)) { 163 error_setg(errp, 164 "min_io_size must be a multiple of logical_block_size"); 165 return false; 166 } 167 168 /* 169 * all devices which support min_io_size (scsi and virtio-blk) expose it to 170 * the guest as a uint16_t in units of logical blocks 171 */ 172 if (conf->min_io_size / conf->logical_block_size > UINT16_MAX) { 173 error_setg(errp, "min_io_size must not exceed %u logical blocks", 174 UINT16_MAX); 175 return false; 176 } 177 178 if (!QEMU_IS_ALIGNED(conf->opt_io_size, conf->logical_block_size)) { 179 error_setg(errp, 180 "opt_io_size must be a multiple of logical_block_size"); 181 return false; 182 } 183 184 if (conf->discard_granularity != -1 && 185 !QEMU_IS_ALIGNED(conf->discard_granularity, 186 conf->logical_block_size)) { 187 error_setg(errp, "discard_granularity must be " 188 "a multiple of logical_block_size"); 189 return false; 190 } 191 192 return true; 193 } 194 195 bool blkconf_apply_backend_options(BlockConf *conf, bool readonly, 196 bool resizable, Error **errp) 197 { 198 BlockBackend *blk = conf->blk; 199 BlockdevOnError rerror, werror; 200 uint64_t perm, shared_perm; 201 bool wce; 202 int ret; 203 204 perm = BLK_PERM_CONSISTENT_READ; 205 if (!readonly) { 206 perm |= BLK_PERM_WRITE; 207 } 208 209 shared_perm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED; 210 if (resizable) { 211 shared_perm |= BLK_PERM_RESIZE; 212 } 213 if (conf->share_rw) { 214 shared_perm |= BLK_PERM_WRITE; 215 } 216 217 ret = blk_set_perm(blk, perm, shared_perm, errp); 218 if (ret < 0) { 219 return false; 220 } 221 222 switch (conf->wce) { 223 case ON_OFF_AUTO_ON: wce = true; break; 224 case ON_OFF_AUTO_OFF: wce = false; break; 225 case ON_OFF_AUTO_AUTO: wce = blk_enable_write_cache(blk); break; 226 default: 227 abort(); 228 } 229 230 rerror = conf->rerror; 231 if (rerror == BLOCKDEV_ON_ERROR_AUTO) { 232 rerror = blk_get_on_error(blk, true); 233 } 234 235 werror = conf->werror; 236 if (werror == BLOCKDEV_ON_ERROR_AUTO) { 237 werror = blk_get_on_error(blk, false); 238 } 239 240 blk_set_enable_write_cache(blk, wce); 241 blk_set_on_error(blk, rerror, werror); 242 243 block_acct_setup(blk_get_stats(blk), conf->account_invalid, 244 conf->account_failed); 245 return true; 246 } 247 248 bool blkconf_geometry(BlockConf *conf, int *ptrans, 249 unsigned cyls_max, unsigned heads_max, unsigned secs_max, 250 Error **errp) 251 { 252 if (!conf->cyls && !conf->heads && !conf->secs) { 253 hd_geometry_guess(conf->blk, 254 &conf->cyls, &conf->heads, &conf->secs, 255 ptrans); 256 } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) { 257 *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs); 258 } 259 if (conf->cyls || conf->heads || conf->secs) { 260 if (conf->cyls < 1 || conf->cyls > cyls_max) { 261 error_setg(errp, "cyls must be between 1 and %u", cyls_max); 262 return false; 263 } 264 if (conf->heads < 1 || conf->heads > heads_max) { 265 error_setg(errp, "heads must be between 1 and %u", heads_max); 266 return false; 267 } 268 if (conf->secs < 1 || conf->secs > secs_max) { 269 error_setg(errp, "secs must be between 1 and %u", secs_max); 270 return false; 271 } 272 } 273 return true; 274 } 275