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