On 06/03/2016 11:21 AM, Kevin Wolf wrote: > Reading from qcow2 images is now byte granularity. > > Most of the affected code in qcow2 actually gets simpler with this > change. The only exception is encryption, which is fixed on 512 bytes > blocks; in order to keep this working, bs->request_alignment is set for > encrypted images. > > Signed-off-by: Kevin Wolf <[email protected]> > --- > block/qcow2-cluster.c | 18 ++++----- > block/qcow2.c | 108 > +++++++++++++++++++++++++++----------------------- > block/qcow2.h | 2 +- > 3 files changed, 67 insertions(+), 61 deletions(-) >
> @@ -467,16 +468,16 @@ out:
> * For a given offset of the disk image, find the cluster offset in
> * qcow2 file. The offset is stored in *cluster_offset.
> *
> - * on entry, *num is the number of contiguous sectors we'd like to
> + * on entry, *bytes is the number of contiguous bytes we'd like to
maybe s/number/maximum number/
> * access following offset.
> *
> - * on exit, *num is the number of contiguous sectors we can read.
> + * on exit, *bytes is the number of contiguous bytes we can read.
maybe s/we can read/with the same cluster type/
> *
> * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
> * cases.
> */
> int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
> - int *num, uint64_t *cluster_offset)
> + unsigned int *bytes, uint64_t *cluster_offset)
> {
> BDRVQcow2State *s = bs->opaque;
> unsigned int l2_index;
> @@ -485,12 +486,9 @@ int qcow2_get_cluster_offset(BlockDriverState *bs,
> uint64_t offset,
> unsigned int offset_in_cluster, nb_clusters;
> uint64_t bytes_available, bytes_needed;
> int ret;
> - unsigned int bytes;
> -
> - bytes = *num * BDRV_SECTOR_SIZE;
One potential overflow gone...
>
> offset_in_cluster = offset_into_cluster(s, offset);
> - bytes_needed = bytes + offset_in_cluster;
> + bytes_needed = *bytes + offset_in_cluster;
...but not the other. Looks like your callers limit their input 'bytes'
to at most INT_MAX, and therefore it happens to not overflow unsigned
int in practice, but you may want an assertion?
> +++ b/block/qcow2.c
> @@ -975,6 +975,9 @@ static int qcow2_open(BlockDriverState *bs, QDict
> *options, int flags,
> }
>
> bs->encrypted = 1;
> +
> + /* Encryption works on a sector granularity */
> + bs->request_alignment = BDRV_SECTOR_SIZE;
Trivial conflict with my patch 5/5 that moves request_alignment into
BlockLimits (if we even want that, since I still have to find why my
patch makes qemu-iotests 77 hang)
>
> -static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t
> sector_num,
> - int remaining_sectors, QEMUIOVector *qiov)
> +static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t
> offset,
> + uint64_t bytes, QEMUIOVector *qiov,
> + int flags)
Wait a minute. .bdrv_co_preadv() takes uint64_t bytes, while
bdrv_co_preadv() takes only unsigned int bytes? Eww. We've got some
more scrubbing work to do. At least it is going to get easier to
universally turn on full 64-bit byte interfaces everywhere, especially
once my patches for auto-fragmenting at max_transfer_length land (which
in turn won't be posted before your conversion of bdrv_aligned_preadv()
to a byte interface). So no impact to this patch.
> {
> BDRVQcow2State *s = bs->opaque;
> - int index_in_cluster, n1;
> + int offset_in_cluster, n1;
> int ret;
> - int cur_nr_sectors; /* number of sectors in current iteration */
> + unsigned int cur_bytes; /* number of sectors in current iteration */
comment is stale now
> uint64_t cluster_offset = 0;
> uint64_t bytes_done = 0;
> QEMUIOVector hd_qiov;
> @@ -1389,26 +1402,24 @@ static coroutine_fn int
> qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
>
> qemu_co_mutex_lock(&s->lock);
>
> - while (remaining_sectors != 0) {
> + while (bytes != 0) {
>
> /* prepare next request */
> - cur_nr_sectors = remaining_sectors;
> + cur_bytes = MIN(bytes, INT_MAX);
> if (s->cipher) {
> - cur_nr_sectors = MIN(cur_nr_sectors,
> - QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
> + cur_bytes = MIN(cur_bytes,
> + QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
Again, my work on auto-fragmenting at the block layer should make it so
that we can eventually further simplify this part to just assert that
bytes doesn't exceed max_transfer_length, rather than having to fragment
it at INT_MAX ourselves.
Couple of tweaks to fix as pointed out above, but mostly looks sane.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
