Skip to content

Commit a15312b

Browse files
ebblakekevmw
authored andcommitted
qcow: Switch qcow_co_readv to byte-based calls
We are gradually moving away from sector-based interfaces, towards byte-based. Make the change for the internals of the qcow driver read function, by iterating over offset/bytes instead of sector_num/nb_sectors, and with a rename of index_in_cluster and repurposing of n to track bytes instead of sectors. A later patch will then switch the qcow driver as a whole over to byte-based operation. Signed-off-by: Eric Blake <[email protected]> Reviewed-by: Jeff Cody <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent 787993a commit a15312b

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

block/qcow.c

+20-22
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,15 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
617617
int nb_sectors, QEMUIOVector *qiov)
618618
{
619619
BDRVQcowState *s = bs->opaque;
620-
int index_in_cluster;
620+
int offset_in_cluster;
621621
int ret = 0, n;
622622
uint64_t cluster_offset;
623623
struct iovec hd_iov;
624624
QEMUIOVector hd_qiov;
625625
uint8_t *buf;
626626
void *orig_buf;
627+
int64_t offset = sector_num * BDRV_SECTOR_SIZE;
628+
int64_t bytes = nb_sectors * BDRV_SECTOR_SIZE;
627629

628630
if (qiov->niov > 1) {
629631
buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
@@ -637,77 +639,73 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
637639

638640
qemu_co_mutex_lock(&s->lock);
639641

640-
while (nb_sectors != 0) {
642+
while (bytes != 0) {
641643
/* prepare next request */
642-
ret = get_cluster_offset(bs, sector_num << 9,
643-
0, 0, 0, 0, &cluster_offset);
644+
ret = get_cluster_offset(bs, offset, 0, 0, 0, 0, &cluster_offset);
644645
if (ret < 0) {
645646
break;
646647
}
647-
index_in_cluster = sector_num & (s->cluster_sectors - 1);
648-
n = s->cluster_sectors - index_in_cluster;
649-
if (n > nb_sectors) {
650-
n = nb_sectors;
648+
offset_in_cluster = offset & (s->cluster_size - 1);
649+
n = s->cluster_size - offset_in_cluster;
650+
if (n > bytes) {
651+
n = bytes;
651652
}
652653

653654
if (!cluster_offset) {
654655
if (bs->backing) {
655656
/* read from the base image */
656657
hd_iov.iov_base = (void *)buf;
657-
hd_iov.iov_len = n * 512;
658+
hd_iov.iov_len = n;
658659
qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
659660
qemu_co_mutex_unlock(&s->lock);
660661
/* qcow2 emits this on bs->file instead of bs->backing */
661662
BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
662-
ret = bdrv_co_readv(bs->backing, sector_num, n, &hd_qiov);
663+
ret = bdrv_co_preadv(bs->backing, offset, n, &hd_qiov, 0);
663664
qemu_co_mutex_lock(&s->lock);
664665
if (ret < 0) {
665666
break;
666667
}
667668
} else {
668669
/* Note: in this case, no need to wait */
669-
memset(buf, 0, 512 * n);
670+
memset(buf, 0, n);
670671
}
671672
} else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
672673
/* add AIO support for compressed blocks ? */
673674
if (decompress_cluster(bs, cluster_offset) < 0) {
674675
ret = -EIO;
675676
break;
676677
}
677-
memcpy(buf,
678-
s->cluster_cache + index_in_cluster * 512, 512 * n);
678+
memcpy(buf, s->cluster_cache + offset_in_cluster, n);
679679
} else {
680680
if ((cluster_offset & 511) != 0) {
681681
ret = -EIO;
682682
break;
683683
}
684684
hd_iov.iov_base = (void *)buf;
685-
hd_iov.iov_len = n * 512;
685+
hd_iov.iov_len = n;
686686
qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
687687
qemu_co_mutex_unlock(&s->lock);
688688
BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
689-
ret = bdrv_co_readv(bs->file,
690-
(cluster_offset >> 9) + index_in_cluster,
691-
n, &hd_qiov);
689+
ret = bdrv_co_preadv(bs->file, cluster_offset + offset_in_cluster,
690+
n, &hd_qiov, 0);
692691
qemu_co_mutex_lock(&s->lock);
693692
if (ret < 0) {
694693
break;
695694
}
696695
if (bs->encrypted) {
697696
assert(s->crypto);
698697
if (qcrypto_block_decrypt(s->crypto,
699-
sector_num * BDRV_SECTOR_SIZE, buf,
700-
n * BDRV_SECTOR_SIZE, NULL) < 0) {
698+
offset, buf, n, NULL) < 0) {
701699
ret = -EIO;
702700
break;
703701
}
704702
}
705703
}
706704
ret = 0;
707705

708-
nb_sectors -= n;
709-
sector_num += n;
710-
buf += n * 512;
706+
bytes -= n;
707+
offset += n;
708+
buf += n;
711709
}
712710

713711
qemu_co_mutex_unlock(&s->lock);

0 commit comments

Comments
 (0)