copy_range ignores these limitations, let's improve it. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsement...@virtuozzo.com> --- block/io.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-)
diff --git a/block/io.c b/block/io.c index 06305c6ea6..a5efb2200f 100644 --- a/block/io.c +++ b/block/io.c @@ -3005,11 +3005,24 @@ static int coroutine_fn bdrv_co_copy_range_internal( { BdrvTrackedRequest req; int ret; + uint32_t align = MAX(src->bs->bl.request_alignment, + dst->bs->bl.request_alignment); + uint32_t max_transfer = + QEMU_ALIGN_DOWN(MIN_NON_ZERO(MIN_NON_ZERO(src->bs->bl.max_transfer, + dst->bs->bl.max_transfer), + INT_MAX), align); /* TODO We can support BDRV_REQ_NO_FALLBACK here */ assert(!(read_flags & BDRV_REQ_NO_FALLBACK)); assert(!(write_flags & BDRV_REQ_NO_FALLBACK)); + if (max_transfer == 0 && bytes > 0) { + /* + * For example, if source max_transfer is smaller than target alignment. + */ + return -ENOTSUP; + } + if (!dst || !dst->bs) { return -ENOMEDIUM; } @@ -3031,7 +3044,10 @@ static int coroutine_fn bdrv_co_copy_range_internal( if (!src->bs->drv->bdrv_co_copy_range_from || !dst->bs->drv->bdrv_co_copy_range_to - || src->bs->encrypted || dst->bs->encrypted) { + || src->bs->encrypted || dst->bs->encrypted || + !QEMU_IS_ALIGNED(src_offset, src->bs->bl.request_alignment) || + !QEMU_IS_ALIGNED(dst_offset, dst->bs->bl.request_alignment) || + !QEMU_IS_ALIGNED(bytes, align)) { return -ENOTSUP; } @@ -3046,11 +3062,22 @@ static int coroutine_fn bdrv_co_copy_range_internal( wait_serialising_requests(&req); } - ret = src->bs->drv->bdrv_co_copy_range_from(src->bs, - src, src_offset, - dst, dst_offset, - bytes, - read_flags, write_flags); + while (bytes) { + int num = MIN(bytes, max_transfer); + + ret = src->bs->drv->bdrv_co_copy_range_from(src->bs, + src, src_offset, + dst, dst_offset, + num, + read_flags, + write_flags); + if (ret < 0) { + break; + } + bytes -= num; + src_offset += num; + dst_offset += num; + } tracked_request_end(&req); bdrv_dec_in_flight(src->bs); @@ -3060,12 +3087,17 @@ static int coroutine_fn bdrv_co_copy_range_internal( BDRV_TRACKED_WRITE); ret = bdrv_co_write_req_prepare(dst, dst_offset, bytes, &req, write_flags); - if (!ret) { + while (!ret && bytes) { + int num = MIN(bytes, max_transfer); + ret = dst->bs->drv->bdrv_co_copy_range_to(dst->bs, src, src_offset, dst, dst_offset, - bytes, + num, read_flags, write_flags); + bytes -= num; + src_offset += num; + dst_offset += num; } bdrv_co_write_req_finish(dst, dst_offset, bytes, &req, ret); tracked_request_end(&req); -- 2.18.0