mirror of
https://github.com/xemu-project/xemu.git
synced 2024-11-27 21:40:49 +00:00
block: introduce BDRV_MAX_LENGTH
We are going to modify block layer to work with 64bit requests. And first step is moving to int64_t type for both offset and bytes arguments in all block request related functions. It's mostly safe (when widening signed or unsigned int to int64_t), but switching from uint64_t is questionable. So, let's first establish the set of requests we want to work with. First signed int64_t should be enough, as off_t is signed anyway. Then, obviously offset + bytes should not overflow. And most interesting: (offset + bytes) being aligned up should not overflow as well. Aligned to what alignment? First thing that comes in mind is bs->bl.request_alignment, as we align up request to this alignment. But there is another thing: look at bdrv_mark_request_serialising(). It aligns request up to some given alignment. And this parameter may be bdrv_get_cluster_size(), which is often a lot greater than bs->bl.request_alignment. Note also, that bdrv_mark_request_serialising() uses signed int64_t for calculations. So, actually, we already depend on some restrictions. Happily, bdrv_get_cluster_size() returns int and bs->bl.request_alignment has 32bit unsigned type, but defined to be a power of 2 less than INT_MAX. So, we may establish, that INT_MAX is absolute maximum for any kind of alignment that may occur with the request. Note, that bdrv_get_cluster_size() is not documented to return power of 2, still bdrv_mark_request_serialising() behaves like it is. Also, backup uses bdi.cluster_size and is not prepared to it not being power of 2. So, let's establish that Qemu supports only power-of-2 clusters and alignments. So, alignment can't be greater than 2^30. Finally to be safe with calculations, to not calculate different maximums for different nodes (depending on cluster size and request_alignment), let's simply set QEMU_ALIGN_DOWN(INT64_MAX, 2^30) as absolute maximum bytes length for Qemu. Actually, it's not much less than INT64_MAX. OK, then, let's apply it to block/io. Let's consider all block/io entry points of offset/bytes: 4 bytes/offset interface functions: bdrv_co_preadv_part(), bdrv_co_pwritev_part(), bdrv_co_copy_range_internal() and bdrv_co_pdiscard() and we check them all with bdrv_check_request(). We also have one entry point with only offset: bdrv_co_truncate(). Check the offset. And one public structure: BdrvTrackedRequest. Happily, it has only three external users: file-posix.c: adopted by this patch write-threshold.c: only read fields test-write-threshold.c: sets obviously small constant values Better is to make the structure private and add corresponding interfaces.. Still it's not obvious what kind of interface is needed for file-posix.c. Let's keep it public but add corresponding assertions. After this patch we'll convert functions in block/io.c to int64_t bytes and offset parameters. We can assume that offset/bytes pair always satisfy new restrictions, and make corresponding assertions where needed. If we reach some offset/bytes point in block/io.c missing bdrv_check_request() it is considered a bug. As well, if block/io.c modifies a offset/bytes request, expanding it more then aligning up to request_alignment, it's a bug too. For all io requests except for discard we keep for now old restriction of 32bit request length. iotest 206 output error message changed, as now test disk size is larger than new limit. Add one more test case with new maximum disk size to cover too-big-L1 case. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Message-Id: <20201203222713.13507-5-vsementsov@virtuozzo.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
f4dad307ef
commit
8b1170012b
17
block.c
17
block.c
@ -962,6 +962,11 @@ int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bs->total_sectors = hint;
|
bs->total_sectors = hint;
|
||||||
|
|
||||||
|
if (bs->total_sectors * BDRV_SECTOR_SIZE > BDRV_MAX_LENGTH) {
|
||||||
|
return -EFBIG;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -5535,6 +5540,7 @@ void bdrv_get_backing_filename(BlockDriverState *bs,
|
|||||||
|
|
||||||
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
BlockDriver *drv = bs->drv;
|
BlockDriver *drv = bs->drv;
|
||||||
/* if bs->drv == NULL, bs is closed, so there's nothing to do here */
|
/* if bs->drv == NULL, bs is closed, so there's nothing to do here */
|
||||||
if (!drv) {
|
if (!drv) {
|
||||||
@ -5548,7 +5554,16 @@ int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
|||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
memset(bdi, 0, sizeof(*bdi));
|
memset(bdi, 0, sizeof(*bdi));
|
||||||
return drv->bdrv_get_info(bs, bdi);
|
ret = drv->bdrv_get_info(bs, bdi);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bdi->cluster_size > BDRV_MAX_ALIGNMENT) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs,
|
ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs,
|
||||||
|
@ -2926,7 +2926,6 @@ raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes,
|
|||||||
#ifdef CONFIG_FALLOCATE
|
#ifdef CONFIG_FALLOCATE
|
||||||
if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
|
if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
|
||||||
BdrvTrackedRequest *req;
|
BdrvTrackedRequest *req;
|
||||||
uint64_t end;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is a workaround for a bug in the Linux XFS driver,
|
* This is a workaround for a bug in the Linux XFS driver,
|
||||||
@ -2950,8 +2949,9 @@ raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes,
|
|||||||
assert(req->offset <= offset);
|
assert(req->offset <= offset);
|
||||||
assert(req->offset + req->bytes >= offset + bytes);
|
assert(req->offset + req->bytes >= offset + bytes);
|
||||||
|
|
||||||
end = INT64_MAX & -(uint64_t)bs->bl.request_alignment;
|
req->bytes = BDRV_MAX_LENGTH - req->offset;
|
||||||
req->bytes = end - req->offset;
|
|
||||||
|
assert(bdrv_check_request(req->offset, req->bytes) == 0);
|
||||||
|
|
||||||
bdrv_mark_request_serialising(req, bs->bl.request_alignment);
|
bdrv_mark_request_serialising(req, bs->bl.request_alignment);
|
||||||
}
|
}
|
||||||
|
51
block/io.c
51
block/io.c
@ -176,6 +176,13 @@ void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)
|
|||||||
/* Then let the driver override it */
|
/* Then let the driver override it */
|
||||||
if (drv->bdrv_refresh_limits) {
|
if (drv->bdrv_refresh_limits) {
|
||||||
drv->bdrv_refresh_limits(bs, errp);
|
drv->bdrv_refresh_limits(bs, errp);
|
||||||
|
if (*errp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bs->bl.request_alignment > BDRV_MAX_ALIGNMENT) {
|
||||||
|
error_setg(errp, "Driver requires too large request alignment");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -884,13 +891,31 @@ static bool coroutine_fn bdrv_wait_serialising_requests(BdrvTrackedRequest *self
|
|||||||
return waited;
|
return waited;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int bdrv_check_byte_request(int64_t offset, size_t size)
|
int bdrv_check_request(int64_t offset, int64_t bytes)
|
||||||
{
|
{
|
||||||
if (size > BDRV_REQUEST_MAX_BYTES) {
|
if (offset < 0 || bytes < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offset < 0) {
|
if (bytes > BDRV_MAX_LENGTH) {
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offset > BDRV_MAX_LENGTH - bytes) {
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int bdrv_check_request32(int64_t offset, int64_t bytes)
|
||||||
|
{
|
||||||
|
int ret = bdrv_check_request(offset, bytes);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bytes > BDRV_REQUEST_MAX_BYTES) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1641,7 +1666,7 @@ int coroutine_fn bdrv_co_preadv_part(BdrvChild *child,
|
|||||||
return -ENOMEDIUM;
|
return -ENOMEDIUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_check_byte_request(offset, bytes);
|
ret = bdrv_check_request32(offset, bytes);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -2057,7 +2082,7 @@ int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child,
|
|||||||
return -ENOMEDIUM;
|
return -ENOMEDIUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_check_byte_request(offset, bytes);
|
ret = bdrv_check_request32(offset, bytes);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -2787,8 +2812,9 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
|
|||||||
return -EPERM;
|
return -EPERM;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offset < 0 || bytes < 0 || bytes > INT64_MAX - offset) {
|
ret = bdrv_check_request(offset, bytes);
|
||||||
return -EIO;
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Do nothing if disabled. */
|
/* Do nothing if disabled. */
|
||||||
@ -3047,7 +3073,7 @@ static int coroutine_fn bdrv_co_copy_range_internal(
|
|||||||
if (!dst || !dst->bs || !bdrv_is_inserted(dst->bs)) {
|
if (!dst || !dst->bs || !bdrv_is_inserted(dst->bs)) {
|
||||||
return -ENOMEDIUM;
|
return -ENOMEDIUM;
|
||||||
}
|
}
|
||||||
ret = bdrv_check_byte_request(dst_offset, bytes);
|
ret = bdrv_check_request32(dst_offset, bytes);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -3058,7 +3084,7 @@ static int coroutine_fn bdrv_co_copy_range_internal(
|
|||||||
if (!src || !src->bs || !bdrv_is_inserted(src->bs)) {
|
if (!src || !src->bs || !bdrv_is_inserted(src->bs)) {
|
||||||
return -ENOMEDIUM;
|
return -ENOMEDIUM;
|
||||||
}
|
}
|
||||||
ret = bdrv_check_byte_request(src_offset, bytes);
|
ret = bdrv_check_request32(src_offset, bytes);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -3188,6 +3214,13 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
|
|||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = bdrv_check_request(offset, 0);
|
||||||
|
if (ret < 0) {
|
||||||
|
error_setg(errp, "Required too big image size, it must be not greater "
|
||||||
|
"than %" PRId64, BDRV_MAX_LENGTH);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
old_size = bdrv_getlength(bs);
|
old_size = bdrv_getlength(bs);
|
||||||
if (old_size < 0) {
|
if (old_size < 0) {
|
||||||
error_setg_errno(errp, -old_size, "Failed to get old image size");
|
error_setg_errno(errp, -old_size, "Failed to get old image size");
|
||||||
|
@ -142,6 +142,16 @@ typedef struct HDGeometry {
|
|||||||
INT_MAX >> BDRV_SECTOR_BITS)
|
INT_MAX >> BDRV_SECTOR_BITS)
|
||||||
#define BDRV_REQUEST_MAX_BYTES (BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS)
|
#define BDRV_REQUEST_MAX_BYTES (BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We want allow aligning requests and disk length up to any 32bit alignment
|
||||||
|
* and don't afraid of overflow.
|
||||||
|
* To achieve it, and in the same time use some pretty number as maximum disk
|
||||||
|
* size, let's define maximum "length" (a limit for any offset/bytes request and
|
||||||
|
* for disk size) to be the greatest power of 2 less than INT64_MAX.
|
||||||
|
*/
|
||||||
|
#define BDRV_MAX_ALIGNMENT (1L << 30)
|
||||||
|
#define BDRV_MAX_LENGTH (QEMU_ALIGN_DOWN(INT64_MAX, BDRV_MAX_ALIGNMENT))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Allocation status flags for bdrv_block_status() and friends.
|
* Allocation status flags for bdrv_block_status() and friends.
|
||||||
*
|
*
|
||||||
|
@ -70,6 +70,12 @@ enum BdrvTrackedRequestType {
|
|||||||
BDRV_TRACKED_TRUNCATE,
|
BDRV_TRACKED_TRUNCATE,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* That is not quite good that BdrvTrackedRequest structure is public,
|
||||||
|
* as block/io.c is very careful about incoming offset/bytes being
|
||||||
|
* correct. Be sure to assert bdrv_check_request() succeeded after any
|
||||||
|
* modification of BdrvTrackedRequest object out of block/io.c
|
||||||
|
*/
|
||||||
typedef struct BdrvTrackedRequest {
|
typedef struct BdrvTrackedRequest {
|
||||||
BlockDriverState *bs;
|
BlockDriverState *bs;
|
||||||
int64_t offset;
|
int64_t offset;
|
||||||
@ -87,6 +93,8 @@ typedef struct BdrvTrackedRequest {
|
|||||||
struct BdrvTrackedRequest *waiting_for;
|
struct BdrvTrackedRequest *waiting_for;
|
||||||
} BdrvTrackedRequest;
|
} BdrvTrackedRequest;
|
||||||
|
|
||||||
|
int bdrv_check_request(int64_t offset, int64_t bytes);
|
||||||
|
|
||||||
struct BlockDriver {
|
struct BlockDriver {
|
||||||
const char *format_name;
|
const char *format_name;
|
||||||
int instance_size;
|
int instance_size;
|
||||||
|
@ -202,7 +202,7 @@ with iotests.FilePath('t.qcow2') as disk_path, \
|
|||||||
|
|
||||||
vm.launch()
|
vm.launch()
|
||||||
for size in [ 1234, 18446744073709551104, 9223372036854775808,
|
for size in [ 1234, 18446744073709551104, 9223372036854775808,
|
||||||
9223372036854775296 ]:
|
9223372036854775296, 9223372035781033984 ]:
|
||||||
vm.blockdev_create({ 'driver': imgfmt,
|
vm.blockdev_create({ 'driver': imgfmt,
|
||||||
'file': 'node0',
|
'file': 'node0',
|
||||||
'size': size })
|
'size': size })
|
||||||
|
@ -180,6 +180,12 @@ Job failed: Could not resize image: Image size cannot be negative
|
|||||||
|
|
||||||
{"execute": "blockdev-create", "arguments": {"job-id": "job0", "options": {"driver": "qcow2", "file": "node0", "size": 9223372036854775296}}}
|
{"execute": "blockdev-create", "arguments": {"job-id": "job0", "options": {"driver": "qcow2", "file": "node0", "size": 9223372036854775296}}}
|
||||||
{"return": {}}
|
{"return": {}}
|
||||||
|
Job failed: Could not resize image: Required too big image size, it must be not greater than 9223372035781033984
|
||||||
|
{"execute": "job-dismiss", "arguments": {"id": "job0"}}
|
||||||
|
{"return": {}}
|
||||||
|
|
||||||
|
{"execute": "blockdev-create", "arguments": {"job-id": "job0", "options": {"driver": "qcow2", "file": "node0", "size": 9223372035781033984}}}
|
||||||
|
{"return": {}}
|
||||||
Job failed: Could not resize image: Failed to grow the L1 table: File too large
|
Job failed: Could not resize image: Failed to grow the L1 table: File too large
|
||||||
{"execute": "job-dismiss", "arguments": {"id": "job0"}}
|
{"execute": "job-dismiss", "arguments": {"id": "job0"}}
|
||||||
{"return": {}}
|
{"return": {}}
|
||||||
|
@ -64,6 +64,8 @@ static void test_threshold_not_trigger(void)
|
|||||||
req.offset = 1024;
|
req.offset = 1024;
|
||||||
req.bytes = 1024;
|
req.bytes = 1024;
|
||||||
|
|
||||||
|
assert(bdrv_check_request(req.offset, req.bytes) == 0);
|
||||||
|
|
||||||
bdrv_write_threshold_set(&bs, threshold);
|
bdrv_write_threshold_set(&bs, threshold);
|
||||||
amount = bdrv_write_threshold_exceeded(&bs, &req);
|
amount = bdrv_write_threshold_exceeded(&bs, &req);
|
||||||
g_assert_cmpuint(amount, ==, 0);
|
g_assert_cmpuint(amount, ==, 0);
|
||||||
@ -82,6 +84,8 @@ static void test_threshold_trigger(void)
|
|||||||
req.offset = (4 * 1024 * 1024) - 1024;
|
req.offset = (4 * 1024 * 1024) - 1024;
|
||||||
req.bytes = 2 * 1024;
|
req.bytes = 2 * 1024;
|
||||||
|
|
||||||
|
assert(bdrv_check_request(req.offset, req.bytes) == 0);
|
||||||
|
|
||||||
bdrv_write_threshold_set(&bs, threshold);
|
bdrv_write_threshold_set(&bs, threshold);
|
||||||
amount = bdrv_write_threshold_exceeded(&bs, &req);
|
amount = bdrv_write_threshold_exceeded(&bs, &req);
|
||||||
g_assert_cmpuint(amount, >=, 1024);
|
g_assert_cmpuint(amount, >=, 1024);
|
||||||
|
Loading…
Reference in New Issue
Block a user