Skip to content

Commit d1326a7

Browse files
ebblakekevmw
authored andcommitted
qcow: Switch qcow_co_writev 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 write 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 a15312b commit d1326a7

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

block/qcow.c

+17-19
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,15 @@ static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
723723
int flags)
724724
{
725725
BDRVQcowState *s = bs->opaque;
726-
int index_in_cluster;
726+
int offset_in_cluster;
727727
uint64_t cluster_offset;
728728
int ret = 0, n;
729729
struct iovec hd_iov;
730730
QEMUIOVector hd_qiov;
731731
uint8_t *buf;
732732
void *orig_buf;
733+
int64_t offset = sector_num * BDRV_SECTOR_SIZE;
734+
int64_t bytes = nb_sectors * BDRV_SECTOR_SIZE;
733735

734736
assert(!flags);
735737
s->cluster_cache_offset = -1; /* disable compressed cache */
@@ -749,16 +751,14 @@ static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
749751

750752
qemu_co_mutex_lock(&s->lock);
751753

752-
while (nb_sectors != 0) {
753-
754-
index_in_cluster = sector_num & (s->cluster_sectors - 1);
755-
n = s->cluster_sectors - index_in_cluster;
756-
if (n > nb_sectors) {
757-
n = nb_sectors;
754+
while (bytes != 0) {
755+
offset_in_cluster = offset & (s->cluster_size - 1);
756+
n = s->cluster_size - offset_in_cluster;
757+
if (n > bytes) {
758+
n = bytes;
758759
}
759-
ret = get_cluster_offset(bs, sector_num << 9, 1, 0,
760-
index_in_cluster << 9,
761-
(index_in_cluster + n) << 9, &cluster_offset);
760+
ret = get_cluster_offset(bs, offset, 1, 0, offset_in_cluster,
761+
offset_in_cluster + n, &cluster_offset);
762762
if (ret < 0) {
763763
break;
764764
}
@@ -768,30 +768,28 @@ static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
768768
}
769769
if (bs->encrypted) {
770770
assert(s->crypto);
771-
if (qcrypto_block_encrypt(s->crypto, sector_num * BDRV_SECTOR_SIZE,
772-
buf, n * BDRV_SECTOR_SIZE, NULL) < 0) {
771+
if (qcrypto_block_encrypt(s->crypto, offset, buf, n, NULL) < 0) {
773772
ret = -EIO;
774773
break;
775774
}
776775
}
777776

778777
hd_iov.iov_base = (void *)buf;
779-
hd_iov.iov_len = n * 512;
778+
hd_iov.iov_len = n;
780779
qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
781780
qemu_co_mutex_unlock(&s->lock);
782781
BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
783-
ret = bdrv_co_writev(bs->file,
784-
(cluster_offset >> 9) + index_in_cluster,
785-
n, &hd_qiov);
782+
ret = bdrv_co_pwritev(bs->file, cluster_offset + offset_in_cluster,
783+
n, &hd_qiov, 0);
786784
qemu_co_mutex_lock(&s->lock);
787785
if (ret < 0) {
788786
break;
789787
}
790788
ret = 0;
791789

792-
nb_sectors -= n;
793-
sector_num += n;
794-
buf += n * 512;
790+
bytes -= n;
791+
offset += n;
792+
buf += n;
795793
}
796794
qemu_co_mutex_unlock(&s->lock);
797795

0 commit comments

Comments
 (0)