Skip to content

Commit 609841a

Browse files
ebblakekevmw
authored andcommitted
qcow: Switch to a byte-based driver
We are gradually moving away from sector-based interfaces, towards byte-based. The qcow driver is now ready to fully utilize the byte-based callback interface, as long as we override the default alignment to still be 512 (needed at least for asserts present because of encryption, but easier to do everywhere than to audit which sub-sector requests are handled correctly, especially since we no longer recommend qcow for new disk images). Signed-off-by: Eric Blake <[email protected]> Reviewed-by: Jeff Cody <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent d1326a7 commit 609841a

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

block/qcow.c

+20-15
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ typedef struct QCowHeader {
7070
typedef struct BDRVQcowState {
7171
int cluster_bits;
7272
int cluster_size;
73-
int cluster_sectors;
7473
int l2_bits;
7574
int l2_size;
7675
unsigned int l1_size;
@@ -235,7 +234,6 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
235234
}
236235
s->cluster_bits = header.cluster_bits;
237236
s->cluster_size = 1 << s->cluster_bits;
238-
s->cluster_sectors = 1 << (s->cluster_bits - 9);
239237
s->l2_bits = header.l2_bits;
240238
s->l2_size = 1 << s->l2_bits;
241239
bs->total_sectors = header.size / 512;
@@ -613,8 +611,18 @@ static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
613611
return 0;
614612
}
615613

616-
static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
617-
int nb_sectors, QEMUIOVector *qiov)
614+
static void qcow_refresh_limits(BlockDriverState *bs, Error **errp)
615+
{
616+
/* At least encrypted images require 512-byte alignment. Apply the
617+
* limit universally, rather than just on encrypted images, as
618+
* it's easier to let the block layer handle rounding than to
619+
* audit this code further. */
620+
bs->bl.request_alignment = BDRV_SECTOR_SIZE;
621+
}
622+
623+
static coroutine_fn int qcow_co_preadv(BlockDriverState *bs, uint64_t offset,
624+
uint64_t bytes, QEMUIOVector *qiov,
625+
int flags)
618626
{
619627
BDRVQcowState *s = bs->opaque;
620628
int offset_in_cluster;
@@ -624,9 +632,8 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
624632
QEMUIOVector hd_qiov;
625633
uint8_t *buf;
626634
void *orig_buf;
627-
int64_t offset = sector_num * BDRV_SECTOR_SIZE;
628-
int64_t bytes = nb_sectors * BDRV_SECTOR_SIZE;
629635

636+
assert(!flags);
630637
if (qiov->niov > 1) {
631638
buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
632639
if (buf == NULL) {
@@ -718,9 +725,9 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
718725
return ret;
719726
}
720727

721-
static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
722-
int nb_sectors, QEMUIOVector *qiov,
723-
int flags)
728+
static coroutine_fn int qcow_co_pwritev(BlockDriverState *bs, uint64_t offset,
729+
uint64_t bytes, QEMUIOVector *qiov,
730+
int flags)
724731
{
725732
BDRVQcowState *s = bs->opaque;
726733
int offset_in_cluster;
@@ -730,8 +737,6 @@ static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
730737
QEMUIOVector hd_qiov;
731738
uint8_t *buf;
732739
void *orig_buf;
733-
int64_t offset = sector_num * BDRV_SECTOR_SIZE;
734-
int64_t bytes = nb_sectors * BDRV_SECTOR_SIZE;
735740

736741
assert(!flags);
737742
s->cluster_cache_offset = -1; /* disable compressed cache */
@@ -1104,8 +1109,7 @@ qcow_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
11041109

11051110
if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
11061111
/* could not compress: write normal cluster */
1107-
ret = qcow_co_writev(bs, offset >> BDRV_SECTOR_BITS,
1108-
bytes >> BDRV_SECTOR_BITS, qiov, 0);
1112+
ret = qcow_co_pwritev(bs, offset, bytes, qiov, 0);
11091113
if (ret < 0) {
11101114
goto fail;
11111115
}
@@ -1190,9 +1194,10 @@ static BlockDriver bdrv_qcow = {
11901194
.bdrv_co_create_opts = qcow_co_create_opts,
11911195
.bdrv_has_zero_init = bdrv_has_zero_init_1,
11921196
.supports_backing = true,
1197+
.bdrv_refresh_limits = qcow_refresh_limits,
11931198

1194-
.bdrv_co_readv = qcow_co_readv,
1195-
.bdrv_co_writev = qcow_co_writev,
1199+
.bdrv_co_preadv = qcow_co_preadv,
1200+
.bdrv_co_pwritev = qcow_co_pwritev,
11961201
.bdrv_co_block_status = qcow_co_block_status,
11971202

11981203
.bdrv_make_empty = qcow_make_empty,

0 commit comments

Comments
 (0)