The write granularity that a frontend reports is derived from the logical and the physical block size, which are properties of the device model, while the granularity that the medium requires and the write pointers that it holds belong to the backend. Nothing ties them together, and where they disagree a guest is told about a device it cannot use. A backend can require a coarser granularity than the block sizes express. A 512e host managed disk has a logical block size of 512 and requires writes to a sequential zone to be a multiple of 4096, so attaching it with a finer physical block size reports a granularity that the disk will not accept, and the guest is given a plain I/O error for a request that it was told was valid. The write pointers can disagree in the same way. They outlive any particular use of the device, while the block sizes are chosen afresh every time it is attached, so a zone written with logical_block_size=512 leaves a write pointer that logical_block_size=4096 cannot address. A zone report expresses it in 512 byte sectors, so the guest is told about a position that does not fall on a logical block boundary, which it can neither read nor write, and the zone can then only be recovered by resetting it. The reverse is harmless: a pointer laid down with a larger logical block size is still a multiple of a smaller one. Check both at realize time, along with the zone size and the zone capacity, and refuse to start otherwise. The write pointers are already held in memory by the driver, so this costs no I/O. The check uses blkconf_zone_write_granularity(), the same value that a frontend reports to its guest and validates requests against, so the three cannot disagree. Signed-off-by: Niklas Cassel <cassel@kernel.org> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Message-id: 20260921142819.2916043-8-cassel@kernel.org Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
142 lines
6.1 KiB
C
142 lines
6.1 KiB
C
/*
|
|
* Common code for block device models
|
|
*
|
|
* Copyright (C) 2012 Red Hat, Inc.
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
*
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or
|
|
* later. See the COPYING file in the top-level directory.
|
|
*/
|
|
|
|
#ifndef HW_BLOCK_H
|
|
#define HW_BLOCK_H
|
|
|
|
#include "exec/hwaddr.h"
|
|
#include "qapi/qapi-types-block-core.h"
|
|
#include "hw/core/qdev-properties-system.h"
|
|
|
|
/* Configuration */
|
|
|
|
typedef struct BlockConf {
|
|
BlockBackend *blk;
|
|
OnOffAuto backend_defaults;
|
|
uint32_t physical_block_size;
|
|
uint32_t logical_block_size;
|
|
uint32_t min_io_size;
|
|
uint32_t opt_io_size;
|
|
int32_t bootindex;
|
|
uint32_t discard_granularity;
|
|
/* geometry, not all devices use this */
|
|
uint32_t cyls, heads, secs;
|
|
uint32_t lcyls, lheads, lsecs;
|
|
OnOffAuto wce;
|
|
bool share_rw;
|
|
OnOffAuto account_invalid, account_failed;
|
|
BlockdevOnError rerror;
|
|
BlockdevOnError werror;
|
|
uint32_t num_stats_intervals;
|
|
uint32_t *stats_intervals;
|
|
} BlockConf;
|
|
|
|
static inline unsigned int get_physical_block_exp(BlockConf *conf)
|
|
{
|
|
unsigned int exp = 0, size;
|
|
|
|
for (size = conf->physical_block_size;
|
|
size > conf->logical_block_size;
|
|
size >>= 1) {
|
|
exp++;
|
|
}
|
|
|
|
return exp;
|
|
}
|
|
|
|
#define DEFAULT_BLOCK_CONF (BlockConf) { \
|
|
.bootindex = -1, \
|
|
.backend_defaults = ON_OFF_AUTO_AUTO, \
|
|
.discard_granularity = -1, \
|
|
.wce = ON_OFF_AUTO_AUTO, \
|
|
.share_rw = false, \
|
|
.account_invalid = ON_OFF_AUTO_AUTO, \
|
|
.account_failed = ON_OFF_AUTO_AUTO, \
|
|
.rerror = BLOCKDEV_ON_ERROR_AUTO, \
|
|
.werror = BLOCKDEV_ON_ERROR_AUTO, \
|
|
}
|
|
|
|
#define DEFINE_BLOCK_PROPERTIES_BASE(_state, _conf) \
|
|
DEFINE_PROP_ON_OFF_AUTO("backend_defaults", _state, \
|
|
_conf.backend_defaults, ON_OFF_AUTO_AUTO), \
|
|
DEFINE_PROP_BLOCKSIZE("logical_block_size", _state, \
|
|
_conf.logical_block_size), \
|
|
DEFINE_PROP_BLOCKSIZE("physical_block_size", _state, \
|
|
_conf.physical_block_size), \
|
|
DEFINE_PROP_SIZE32("min_io_size", _state, _conf.min_io_size, 0), \
|
|
DEFINE_PROP_SIZE32("opt_io_size", _state, _conf.opt_io_size, 0), \
|
|
DEFINE_PROP_SIZE32("discard_granularity", _state, \
|
|
_conf.discard_granularity, -1), \
|
|
DEFINE_PROP_ON_OFF_AUTO("write-cache", _state, _conf.wce, \
|
|
ON_OFF_AUTO_AUTO), \
|
|
DEFINE_PROP_BOOL("share-rw", _state, _conf.share_rw, false), \
|
|
DEFINE_PROP_ON_OFF_AUTO("account-invalid", _state, \
|
|
_conf.account_invalid, ON_OFF_AUTO_AUTO), \
|
|
DEFINE_PROP_ON_OFF_AUTO("account-failed", _state, \
|
|
_conf.account_failed, ON_OFF_AUTO_AUTO), \
|
|
DEFINE_PROP_ARRAY("stats-intervals", _state, \
|
|
_conf.num_stats_intervals, _conf.stats_intervals, \
|
|
qdev_prop_uint32, uint32_t)
|
|
|
|
#define DEFINE_BLOCK_PROPERTIES(_state, _conf) \
|
|
DEFINE_PROP_DRIVE("drive", _state, _conf.blk), \
|
|
DEFINE_BLOCK_PROPERTIES_BASE(_state, _conf)
|
|
|
|
#define DEFINE_BLOCK_CHS_PROPERTIES(_state, _conf) \
|
|
DEFINE_PROP_UINT32("cyls", _state, _conf.cyls, 0), \
|
|
DEFINE_PROP_UINT32("heads", _state, _conf.heads, 0), \
|
|
DEFINE_PROP_UINT32("secs", _state, _conf.secs, 0), \
|
|
DEFINE_PROP_UINT32("lcyls", _state, _conf.lcyls, 0), \
|
|
DEFINE_PROP_UINT32("lheads", _state, _conf.lheads, 0), \
|
|
DEFINE_PROP_UINT32("lsecs", _state, _conf.lsecs, 0)
|
|
|
|
#define DEFINE_BLOCK_ERROR_PROPERTIES(_state, _conf) \
|
|
DEFINE_PROP_BLOCKDEV_ON_ERROR("rerror", _state, _conf.rerror, \
|
|
BLOCKDEV_ON_ERROR_AUTO), \
|
|
DEFINE_PROP_BLOCKDEV_ON_ERROR("werror", _state, _conf.werror, \
|
|
BLOCKDEV_ON_ERROR_AUTO)
|
|
|
|
/* Backend access helpers */
|
|
|
|
bool blk_check_size_and_read_all(BlockBackend *blk, DeviceState *dev,
|
|
void *buf, hwaddr size, Error **errp);
|
|
|
|
/* Configuration helpers */
|
|
|
|
bool blkconf_geometry(BlockConf *conf, int *trans,
|
|
unsigned cyls_max, unsigned heads_max, unsigned secs_max,
|
|
Error **errp);
|
|
bool blkconf_blocksizes(BlockConf *conf, Error **errp);
|
|
/*
|
|
* The alignment constraint that applies to writes to a sequential zone. A guest
|
|
* cannot issue a write finer than the logical block size, and the medium cannot
|
|
* take one finer than the physical block size, so the constraint that applies
|
|
* is the larger of the two. A frontend must report this value to its guest and
|
|
* validate requests against it.
|
|
*
|
|
* It is derived from the configuration and not from the backend, so that a
|
|
* guest is not given a property of the host, which would differ after
|
|
* migration. blkconf_blocksizes() rejects a logical block size larger than the
|
|
* physical block size, so this is the physical block size in practice.
|
|
*/
|
|
uint32_t blkconf_zone_write_granularity(BlockConf *conf);
|
|
bool blkconf_check_zoned_geometry(BlockConf *conf, Error **errp);
|
|
bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,
|
|
bool resizable, Error **errp);
|
|
|
|
/* Hard disk geometry */
|
|
|
|
void hd_geometry_guess(BlockBackend *blk,
|
|
uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs,
|
|
int *ptrans);
|
|
int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs);
|
|
|
|
#endif
|