block: Fix dst reading after tail copy offloading
v3 cover block: Fix dst reading after tail copy offloading
Based-on: [email protected]
v3: - Rebase onto Vladmir's:
[PATCH v5 0/4] fix image fleecing
on top of master, which has blklogwrites to be converted to BdrvChild for the bdrv_co_pdiscard() parameter. [Kevin]
- Add file_ prefix to file protocol trace points. [Kevin]
- Assert that BdrvTrackedRequest bytes and offset don't overflow INT64_MAX. [Kevin]
- Don't misuse req->offset/req->bytes in bdrv_co_write_req_prepare/finish. [Kevin]
- Fix stat64_max. [Kevin]
- Keep lines within 80 columns. [Kevin]
- Grammar fix in commit message and rev-by. [Eric]
Qcow2 allocates new clusters after the end of the file. If it is the destinaton of copy offloading, we must adjust dst->bs->total_sectors. Otherwise, further reads will drop to the "beyond EOF" code path and return zeroes, which problem is caught by iotests 222.
Follow the logic in the normal write code and update bs->total_sectors after I/O is done.
While at it, add a few convenient trace points to aid future debug experiences in the topic.
Fam Zheng (10):
block: Prefix file driver trace points with "file_"
block: Add copy offloading trace points
block: Use BdrvChild to discard
block: Use uint64_t for BdrvTrackedRequest byte fields
block: Extract common write req handling
block: Fix handling of image enlarging write
block: Use common req handling for discard
block: Use common req handling in copy offloading
block: Fix bdrv_co_truncate overlap check
block: Use common write req handling in truncate
block/blkdebug.c | 2 +- block/blklogwrites.c | 2 +- block/blkreplay.c | 2 +- block/block-backend.c | 2 +- block/copy-on-read.c | 2 +- block/file-posix.c | 4 +- block/file-win32.c | 2 +- block/io.c | 213 ++++++++++++++++++++++++-------------- block/iscsi.c | 3 + block/mirror.c | 2 +- block/qcow2-refcount.c | 2 +- block/raw-format.c | 2 +- block/throttle.c | 2 +- block/trace-events | 10 +- include/block/block.h | 4 +- include/block/block_int.h | 4 +- 16 files changed, 163 insertions(+), 95 deletions(-)
Signature · 1 line
2.17.1
PATCH v3 1/10 block: Prefix file driver trace points with "file_"
With in one module, trace points usually have a common prefix named after the module name. paio_submit and paio_submit_co are the only two trace points so far in the two file protocol drivers. As we are adding more, having a common prefix here is better so that trace points can be enabled with a glob. Rename them.
--- block/file-posix.c | 2 +- block/file-win32.c | 2 +- block/trace-events | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
block/file-posix.c +1 -1
@@ -1743,7 +1743,7 @@ static int paio_submit_co_full(BlockDriverState *bs, int fd,assert (qiov->size == bytes); } -trace_paio_submit_co (offset, bytes, type); +trace_file_paio_submit_co (offset, bytes, type); pool =aio_get_thread_pool (bdrv_get_aio_context (bs));return thread_pool_submit_co (pool, aio_worker, acb); }
block/file-win32.c +1 -1
@@ -162,7 +162,7 @@ static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile, acb->aio_nbytes = count; acb->aio_offset = offset; -trace_paio_submit (acb, opaque, offset, count, type); +trace_file_paio_submit (acb, opaque, offset, count, type); pool =aio_get_thread_pool (bdrv_get_aio_context (bs));return thread_pool_submit_aio (pool, aio_worker, acb, cb, opaque); }
block/trace-events +2 -2
@@ -55,8 +55,8 @@ qmp_block_stream(void *bs, void *job) "bs %p job %p" # block/file-win32.c # block/file-posix.c -paio_submit_co(int64_t offset, int count, int type) "offset %"PRId64" count %d type %d" -paio_submit(void *acb, void *opaque, int64_t offset, int count, int type) "acb %p opaque %p offset %"PRId64" count %d type %d" +file_paio_submit_co(int64_t offset, int count, int type) "offset %"PRId64" count %d type %d" +file_paio_submit(void *acb, void *opaque, int64_t offset, int count, int type) "acb %p opaque %p offset %"PRId64" count %d type %d" # block/qcow2.c qcow2_writev_start_req(void *co, int64_t offset, int bytes) "co %p offset 0x%" PRIx64 " bytes %d"
Signature · 1 line
2.17.1
PATCH v3 2/10 block: Add copy offloading trace points
In reply to Fam ZhengA few trace points that can help reveal what is happening in a copy offloading I/O path.
--- block/file-posix.c | 2 ++ block/io.c | 4 ++++ block/iscsi.c | 3 +++ block/trace-events | 6 ++++++ 4 files changed, 15 insertions(+)
block/file-posix.c +2 -0
@@ -1488,6 +1488,8 @@ static ssize_t handle_aiocb_copy_range(RawPosixAIOData *aiocb)ssize_t ret =copy_file_range (aiocb->aio_fildes, &in_off, aiocb->aio_fd2, &out_off, bytes,0 ); +trace_file_copy_file_range (aiocb->bs, aiocb->aio_fildes, in_off, + aiocb->aio_fd2, out_off, bytes,0 , ret);if (ret ==0 ) {/* No progress (e.g. when beyond EOF), let the caller fall back to * buffer I/O. */
block/io.c +4 -0
@@ -2997,6 +2997,8 @@ int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, uint64_t src_offset,BdrvRequestFlags read_flags, BdrvRequestFlags write_flags) { +trace_bdrv_co_copy_range_from (src ,src_offset ,dst ,dst_offset ,bytes , +read_flags ,write_flags );return bdrv_co_copy_range_internal (src, src_offset, dst, dst_offset, bytes, read_flags, write_flags, true); }
@@ -3011,6 +3013,8 @@ int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, uint64_t src_offset,BdrvRequestFlags read_flags, BdrvRequestFlags write_flags) { +trace_bdrv_co_copy_range_to (src ,src_offset ,dst ,dst_offset ,bytes , +read_flags ,write_flags );return bdrv_co_copy_range_internal (src, src_offset, dst, dst_offset, bytes, read_flags, write_flags, false); }
block/iscsi.c +3 -0
@@ -44,6 +44,7 @@#include "qapi/qmp/qstring.h" #include "crypto/secret.h" #include "scsi/utils.h" +#include "trace.h" /* Conflict between scsi/utils.h and libiscsi! :( */ #define SCSI_XFER_NONE ISCSI_XFER_NONE
@@ -2399,6 +2400,8 @@ retry: } out_unlock: + +trace_iscsi_xcopy (src_lun, src_offset, dst_lun, dst_offset, bytes, r);g_free (iscsi_task.task);qemu_mutex_unlock (&dst_lun->mutex);g_free (iscsi_task.err_str);
block/trace-events +6 -0
@@ -15,6 +15,8 @@ bdrv_co_preadv(void *bs, int64_t offset, int64_t nbytes, unsigned int flags) "bs bdrv_co_pwritev(void *bs, int64_t offset, int64_t nbytes, unsigned int flags) "bs %p offset %"PRId64" nbytes %"PRId64" flags 0x%x" bdrv_co_pwrite_zeroes(void *bs, int64_t offset, int count, int flags) "bs %p offset %"PRId64" count %d flags 0x%x" bdrv_co_do_copy_on_readv(void *bs, int64_t offset, unsigned int bytes, int64_t cluster_offset, int64_t cluster_bytes) "bs %p offset %"PRId64" bytes %u cluster_offset %"PRId64" cluster_bytes %"PRId64 +bdrv_co_copy_range_from(void *src, uint64_t src_offset, void *dst, uint64_t dst_offset, uint64_t bytes, int read_flags, int write_flags) "src %p offset %"PRIu64" dst %p offset %"PRIu64" bytes %"PRIu64" rw flags 0x%x 0x%x" +bdrv_co_copy_range_to(void *src, uint64_t src_offset, void *dst, uint64_t dst_offset, uint64_t bytes, int read_flags, int write_flags) "src %p offset %"PRIu64" dst %p offset %"PRIu64" bytes %"PRIu64" rw flags 0x%x 0x%x" # block/stream.c stream_one_iteration(void *s, int64_t offset, uint64_t bytes, int is_allocated) "s %p offset %" PRId64 " bytes %" PRIu64 " is_allocated %d"
@@ -57,6 +59,7 @@ qmp_block_stream(void *bs, void *job) "bs %p job %p" # block/file-posix.c file_paio_submit_co(int64_t offset, int count, int type) "offset %"PRId64" count %d type %d" file_paio_submit(void *acb, void *opaque, int64_t offset, int count, int type) "acb %p opaque %p offset %"PRId64" count %d type %d" +file_copy_file_range(void *bs, int src, int64_t src_off, int dst, int64_t dst_off, int64_t bytes, int flags, int64_t ret) "bs %p src_fd %d offset %"PRIu64" dst_fd %d offset %"PRIu64" bytes %"PRIu64" flags %d ret %"PRId64 # block/qcow2.c qcow2_writev_start_req(void *co, int64_t offset, int bytes) "co %p offset 0x%" PRIx64 " bytes %d"
@@ -150,3 +153,6 @@ nvme_free_req_queue_wait(void *q) "q %p" nvme_cmd_map_qiov(void *s, void *cmd, void *req, void *qiov, int entries) "s %p cmd %p req %p qiov %p entries %d" nvme_cmd_map_qiov_pages(void *s, int i, uint64_t page) "s %p page[%d] 0x%"PRIx64 nvme_cmd_map_qiov_iov(void *s, int i, void *page, int pages) "s %p iov[%d] %p pages %d" + +# block/iscsi.c +iscsi_xcopy(void *src_lun, uint64_t src_off, void *dst_lun, uint64_t dst_off, uint64_t bytes, int ret) "src_lun %p offset %"PRIu64" dst_lun %p offset %"PRIu64" bytes %"PRIu64" ret %d"
Signature · 1 line
2.17.1
PATCH v3 3/10 block: Use BdrvChild to discard
In reply to Fam ZhengOther I/O functions are already using a BdrvChild pointer in the API, so make discard do the same. It makes it possible to initiate the same permission checks before doing I/O, and much easier to share the helper functions for this, which will be added and used by write, truncate and copy range paths.
--- block/blkdebug.c | 2 +- block/blklogwrites.c | 2 +- block/blkreplay.c | 2 +- block/block-backend.c | 2 +- block/copy-on-read.c | 2 +- block/io.c | 18 +++++++++--------- block/mirror.c | 2 +- block/qcow2-refcount.c | 2 +- block/raw-format.c | 2 +- block/throttle.c | 2 +- include/block/block.h | 4 ++-- 11 files changed, 20 insertions(+), 20 deletions(-)
block/blkdebug.c +1 -1
@@ -625,7 +625,7 @@ static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,return err; } -return bdrv_co_pdiscard (bs->file->bs, offset, bytes); +return bdrv_co_pdiscard (bs->file, offset, bytes); } static int coroutine_fn blkdebug_co_block_status(BlockDriverState *bs,
block/blklogwrites.c +1 -1
@@ -483,7 +483,7 @@ static int coroutine_fn blk_log_writes_co_do_file_flush(BlkLogWritesFileReq *fr) static int coroutine_fnblk_log_writes_co_do_file_pdiscard (BlkLogWritesFileReq *fr) { -return bdrv_co_pdiscard (fr->bs->file->bs, fr->offset, fr->bytes); +return bdrv_co_pdiscard (fr->bs->file, fr->offset, fr->bytes); }static int coroutine_fn
block/blkreplay.c +1 -1
@@ -113,7 +113,7 @@ static int coroutine_fn blkreplay_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) { uint64_t reqid = blkreplay_next_id(); -int ret =bdrv_co_pdiscard (bs->file->bs, offset, bytes); +int ret =bdrv_co_pdiscard (bs->file, offset, bytes);block_request_create (reqid, bs,qemu_coroutine_self ());qemu_coroutine_yield ();
block/block-backend.c +1 -1
@@ -1559,7 +1559,7 @@ int blk_co_pdiscard(BlockBackend *blk, int64_t offset, int bytes)return ret; } -return bdrv_co_pdiscard (blk_bs (blk), offset, bytes); +return bdrv_co_pdiscard (blk->root, offset, bytes); }int blk_co_flush (BlockBackend *blk)
block/copy-on-read.c +1 -1
@@ -116,7 +116,7 @@ static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs, static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs,int64_t offset,int bytes) { -return bdrv_co_pdiscard (bs->file->bs, offset, bytes); +return bdrv_co_pdiscard (bs->file, offset, bytes); }
block/io.c +9 -9
@@ -2611,7 +2611,7 @@ int bdrv_flush(BlockDriverState *bs) } typedef struct DiscardCo { - BlockDriverState *bs; + BdrvChild *child; int64_t offset; int bytes; int ret;
@@ -2620,17 +2620,17 @@ static void coroutine_fn bdrv_pdiscard_co_entry(void *opaque) {DiscardCo *rwco = opaque; - rwco->ret =bdrv_co_pdiscard (rwco->bs, rwco->offset, rwco->bytes); + rwco->ret =bdrv_co_pdiscard (rwco->child, rwco->offset, rwco->bytes); } -int coroutine_fn bdrv_co_pdiscard(BlockDriverState *bs, int64_t offset, -int bytes) +int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset, int bytes) {BdrvTrackedRequest req;int max_pdiscard, ret;int head, tail, align; +BlockDriverState *bs = child->bs; -if (!bs->drv) { +if (!bs || !bs->drv) {return -ENOMEDIUM ; }
@@ -2741,11 +2741,11 @@ out:return ret; } -int bdrv_pdiscard (BlockDriverState *bs,int64_t offset,int bytes) +int bdrv_pdiscard (BdrvChild *child,int64_t offset,int bytes) {Coroutine *co;DiscardCo rwco = { - .bs = bs, + .child = child, .offset = offset, .bytes = bytes, .ret =NOT_DONE ,
@@ -2756,8 +2756,8 @@ int bdrv_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)bdrv_pdiscard_co_entry (&rwco); }else { co =qemu_coroutine_create (bdrv_pdiscard_co_entry, &rwco); -bdrv_coroutine_enter (bs, co); -BDRV_POLL_WHILE (bs, rwco.ret ==NOT_DONE ); +bdrv_coroutine_enter (child->bs, co); +BDRV_POLL_WHILE (child->bs, rwco.ret ==NOT_DONE ); }return rwco.ret;
block/mirror.c +1 -1
@@ -1333,7 +1333,7 @@ static int coroutine_fn bdrv_mirror_top_do_write(BlockDriverState *bs,break ;case MIRROR_METHOD_DISCARD : - ret =bdrv_co_pdiscard (bs->backing->bs, offset, bytes); + ret =bdrv_co_pdiscard (bs->backing, offset, bytes);break ;default :
block/qcow2-refcount.c +1 -1
@@ -734,7 +734,7 @@ void qcow2_process_discards(BlockDriverState *bs, int ret)/* Discard is optional, ignore the return value */ if (ret >=0 ) { -bdrv_pdiscard (bs->file->bs, d->offset, d->bytes); +bdrv_pdiscard (bs->file, d->offset, d->bytes); }g_free (d);
block/raw-format.c +1 -1
@@ -297,7 +297,7 @@ static int coroutine_fn raw_co_pdiscard(BlockDriverState *bs,if (ret) {return ret; } -return bdrv_co_pdiscard (bs->file->bs, offset, bytes); +return bdrv_co_pdiscard (bs->file, offset, bytes); }static int64_t raw_getlength (BlockDriverState *bs)
block/throttle.c +1 -1
@@ -149,7 +149,7 @@ static int coroutine_fn throttle_co_pdiscard(BlockDriverState *bs,ThrottleGroupMember *tgm = bs->opaque;throttle_group_co_io_limits_intercept (tgm, bytes, true); -return bdrv_co_pdiscard (bs->file->bs, offset, bytes); +return bdrv_co_pdiscard (bs->file, offset, bytes); }static int throttle_co_flush (BlockDriverState *bs)
include/block/block.h +2 -2
@@ -416,8 +416,8 @@ AioWait *bdrv_get_aio_wait(BlockDriverState *bs);bdrv_get_aio_context (bs_), \ cond); }) -int bdrv_pdiscard (BlockDriverState *bs,int64_t offset,int bytes); -int bdrv_co_pdiscard (BlockDriverState *bs,int64_t offset,int bytes); +int bdrv_pdiscard (BdrvChild *child,int64_t offset,int bytes); +int bdrv_co_pdiscard (BdrvChild *child,int64_t offset,int bytes);int bdrv_has_zero_init_1 (BlockDriverState *bs);int bdrv_has_zero_init (BlockDriverState *bs);bool bdrv_unallocated_blocks_are_zero (BlockDriverState *bs);
Signature · 1 line
2.17.1
PATCH v3 4/10 block: Use uint64_t for BdrvTrackedRequest byte fields
In reply to Fam ZhengThis matches the types used for bytes in the rest parts of block layer. In the case of bdrv_co_truncate, new_bytes can be the image size which probably doesn't fit in a 32 bit int.
--- block/io.c | 8 +++++--- include/block/block_int.h | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-)
block/io.c +5 -3
@@ -587,9 +587,11 @@ static void tracked_request_end(BdrvTrackedRequest *req)static void tracked_request_begin (BdrvTrackedRequest *req,BlockDriverState *bs,int64_t offset, -unsigned int bytes, +uint64_t bytes,enum BdrvTrackedRequestType type) { +assert (bytes <=INT64_MAX && offset <=INT64_MAX - bytes); + *req = (BdrvTrackedRequest ){ .bs = bs, .offset = offset,
@@ -611,7 +613,7 @@ static void tracked_request_begin(BdrvTrackedRequest *req,static void mark_request_serialising (BdrvTrackedRequest *req,uint64_t align) {int64_t overlap_offset = req->offset & ~(align -1 ); -unsigned int overlap_bytes =ROUND_UP (req->offset + req->bytes, align) +uint64_t overlap_bytes =ROUND_UP (req->offset + req->bytes, align) - overlap_offset;if (!req->serialising) {
@@ -667,7 +669,7 @@ static int bdrv_get_cluster_size(BlockDriverState *bs) }static bool tracked_request_overlaps (BdrvTrackedRequest *req, -int64_t offset,unsigned int bytes) +int64_t offset,uint64_t bytes) {/* aaaa bbbb */ if (offset >= req->overlap_offset + req->overlap_bytes) {
include/block/block_int.h +2 -2
@@ -69,12 +69,12 @@ enum BdrvTrackedRequestType { typedef struct BdrvTrackedRequest { BlockDriverState *bs; int64_t offset; - unsigned int bytes; + uint64_t bytes; enum BdrvTrackedRequestType type; bool serialising; int64_t overlap_offset; - unsigned int overlap_bytes; + uint64_t overlap_bytes; QLIST_ENTRY(BdrvTrackedRequest) list; Coroutine *co; /* owner, used for deadlock detection */
Signature · 1 line
2.17.1
PATCH v3 5/10 block: Extract common write req handling
In reply to Fam ZhengAs a mechanical refactoring patch, this is the first step towards unified and more correct write code paths. This is helpful because multiple BlockDriverState fields need to be updated after modifying image data, and it's hard to maintain in multiple places such as copy offload, discard and truncate.
--- block/io.c | 91 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 34 deletions(-)
block/io.c +57 -34
@@ -1553,6 +1553,61 @@ fail:return ret; } +static inline int coroutine_fn +bdrv_co_write_req_prepare (BdrvChild *child,int64_t offset,uint64_t bytes, +BdrvTrackedRequest *req,int flags) +{ +BlockDriverState *bs = child->bs; +bool waited; +int64_t end_sector =DIV_ROUND_UP (offset + bytes,BDRV_SECTOR_SIZE ); + +if (bs->read_only) { +return -EPERM ; + } + +/* BDRV_REQ_NO_SERIALISING is only for read operation */ +assert (!(flags &BDRV_REQ_NO_SERIALISING )); +assert (!(bs->open_flags &BDRV_O_INACTIVE )); +assert ((bs->open_flags &BDRV_O_NO_IO ) ==0 ); +assert (!(flags & ~BDRV_REQ_MASK )); + +if (flags &BDRV_REQ_SERIALISING ) { +mark_request_serialising (req,bdrv_get_cluster_size (bs)); + } + + waited =wait_serialising_requests (req); + +assert (!waited || !req->serialising || +is_request_serialising_and_aligned (req)); +assert (req->overlap_offset <= offset); +assert (offset + bytes <= req->overlap_offset + req->overlap_bytes); + +if (flags &BDRV_REQ_WRITE_UNCHANGED ) { +assert (child->perm & (BLK_PERM_WRITE_UNCHANGED |BLK_PERM_WRITE )); + }else { +assert (child->perm &BLK_PERM_WRITE ); + } +assert (end_sector <= bs->total_sectors || child->perm &BLK_PERM_RESIZE ); +return notifier_with_return_list_notify (&bs->before_write_notifiers, req); +} + +static inline void coroutine_fn +bdrv_co_write_req_finish (BdrvChild *child,int64_t offset,uint64_t bytes, +BdrvTrackedRequest *req,int ret) +{ +int64_t end_sector =DIV_ROUND_UP (offset + bytes,BDRV_SECTOR_SIZE ); +BlockDriverState *bs = child->bs; + +atomic_inc (&bs->write_gen); +bdrv_set_dirty (bs, offset, bytes); + +stat64_max (&bs->wr_highest_offset, offset + bytes); + +if (ret ==0 ) { + bs->total_sectors =MAX (bs->total_sectors, end_sector); + } +} +/* * Forwards an already correctly aligned write request to the BlockDriver, * after possibly fragmenting it.
@@ -1563,10 +1618,8 @@ static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child, {BlockDriverState *bs = child->bs;BlockDriver *drv = bs->drv; -bool waited;int ret; -int64_t end_sector =DIV_ROUND_UP (offset + bytes,BDRV_SECTOR_SIZE );uint64_t bytes_remaining = bytes;int max_transfer;
@@ -1582,31 +1635,10 @@ static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child,assert ((offset & (align -1 )) ==0 );assert ((bytes & (align -1 )) ==0 );assert (!qiov || bytes == qiov->size); -assert ((bs->open_flags &BDRV_O_NO_IO ) ==0 ); -assert (!(flags & ~BDRV_REQ_MASK )); max_transfer =QEMU_ALIGN_DOWN (MIN_NON_ZERO (bs->bl.max_transfer,INT_MAX ), align); -/* BDRV_REQ_NO_SERIALISING is only for read operation */ -assert (!(flags &BDRV_REQ_NO_SERIALISING )); - -if (flags &BDRV_REQ_SERIALISING ) { -mark_request_serialising (req,bdrv_get_cluster_size (bs)); - } - - waited =wait_serialising_requests (req); -assert (!waited || !req->serialising || -is_request_serialising_and_aligned (req)); -assert (req->overlap_offset <= offset); -assert (offset + bytes <= req->overlap_offset + req->overlap_bytes); -if (flags &BDRV_REQ_WRITE_UNCHANGED ) { -assert (child->perm & (BLK_PERM_WRITE_UNCHANGED |BLK_PERM_WRITE )); - }else { -assert (child->perm &BLK_PERM_WRITE ); - } -assert (end_sector <= bs->total_sectors || child->perm &BLK_PERM_RESIZE ); - - ret =notifier_with_return_list_notify (&bs->before_write_notifiers, req); + ret =bdrv_co_write_req_prepare (child, offset, bytes, req, flags); if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF && !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_pwrite_zeroes &&
@@ -1655,15 +1687,10 @@ static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child, }bdrv_debug_event (bs,BLKDBG_PWRITEV_DONE ); -atomic_inc (&bs->write_gen); -bdrv_set_dirty (bs, offset, bytes); - -stat64_max (&bs->wr_highest_offset, offset + bytes); -if (ret >=0 ) { - bs->total_sectors =MAX (bs->total_sectors, end_sector); ret =0 ; } +bdrv_co_write_req_finish (child, offset, bytes, req, ret);return ret; }
@@ -1778,10 +1805,6 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,if (!bs->drv) {return -ENOMEDIUM ; } -if (bs->read_only) { -return -EPERM ; - } -assert (!(bs->open_flags &BDRV_O_INACTIVE )); ret =bdrv_check_byte_request (bs, offset, bytes);if (ret <0 ) {
Signature · 1 line
2.17.1
PATCH v3 6/10 block: Fix handling of image enlarging write
In reply to Fam ZhengTwo problems exist when a write request that enlarges the image (i.e. write beyond EOF) finishes:
1) parent is not notified about size change;
2) dirty bitmap is not resized although we try to set the dirty bits;
Fix them just like how bdrv_co_truncate works.
--- block/io.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
block/io.c +7 -3
@@ -40,6 +40,7 @@static AioWait drain_all_aio_wait; +static void bdrv_parent_cb_resize (BlockDriverState *bs); static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,int64_t offset,int bytes,BdrvRequestFlags flags);
@@ -1599,13 +1600,16 @@ bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, uint64_t bytes,BlockDriverState *bs = child->bs;atomic_inc (&bs->write_gen); -bdrv_set_dirty (bs, offset, bytes);stat64_max (&bs->wr_highest_offset, offset + bytes); -if (ret ==0 ) { - bs->total_sectors =MAX (bs->total_sectors, end_sector); +if (ret ==0 && + end_sector > bs->total_sectors) { + bs->total_sectors = end_sector; +bdrv_parent_cb_resize (bs); +bdrv_dirty_bitmap_truncate (bs, end_sector <<BDRV_SECTOR_BITS ); } +bdrv_set_dirty (bs, offset, bytes); }/*
Signature · 1 line
2.17.1
PATCH v3 7/10 block: Use common req handling for discard
In reply to Fam ZhengReuse the new bdrv_co_write_req_prepare/finish helpers. The variation here is that discard requests don't affect bs->wr_highest_offset, and it cannot extend the image.
--- block/io.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-)
block/io.c +22 -10
@@ -1601,15 +1601,31 @@ bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, uint64_t bytes,atomic_inc (&bs->write_gen); -stat64_max (&bs->wr_highest_offset, offset + bytes); - +/* Discard cannot extend the image, but in error handling cases, such as +* when reverting a qcow2 cluster allocation, the discarded range can pass +* the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD +* here. Instead, just skip it, since semantically a discard request +* beyond EOF cannot expand the image anyway. +* */ if (ret ==0 && - end_sector > bs->total_sectors) { + end_sector > bs->total_sectors && + req->type !=BDRV_TRACKED_DISCARD ) { bs->total_sectors = end_sector;bdrv_parent_cb_resize (bs);bdrv_dirty_bitmap_truncate (bs, end_sector <<BDRV_SECTOR_BITS ); } -bdrv_set_dirty (bs, offset, bytes); +if (req->bytes) { +switch (req->type) { +case BDRV_TRACKED_WRITE : +stat64_max (&bs->wr_highest_offset, offset + bytes); +/* fall through, to set dirty bits */ +case BDRV_TRACKED_DISCARD : +bdrv_set_dirty (bs, offset, bytes); +break ; +default : +break ; + } + } }/*
@@ -2670,10 +2686,7 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset, int bytes) ret =bdrv_check_byte_request (bs, offset, bytes);if (ret <0 ) {return ret; - }else if (bs->read_only) { -return -EPERM ; } -assert (!(bs->open_flags &BDRV_O_INACTIVE ));/* Do nothing if disabled. */ if (!(bs->open_flags &BDRV_O_UNMAP )) {
@@ -2697,7 +2710,7 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset, int bytes)bdrv_inc_in_flight (bs);tracked_request_begin (&req, bs, offset, bytes,BDRV_TRACKED_DISCARD ); - ret =notifier_with_return_list_notify (&bs->before_write_notifiers, &req); + ret =bdrv_co_write_req_prepare (child, offset, bytes, &req,0 );if (ret <0 ) { goto out; }
@@ -2763,8 +2776,7 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset, int bytes) } ret =0 ; out: -atomic_inc (&bs->write_gen); -bdrv_set_dirty (bs, req.offset, req.bytes); +bdrv_co_write_req_finish (child, req.offset, req.bytes, &req, ret);tracked_request_end (&req);bdrv_dec_in_flight (bs);return ret;
Signature · 1 line
2.17.1
PATCH v3 8/10 block: Use common req handling in copy offloading
In reply to Fam ZhengThis brings the request handling logic inline with write and discard, fixing write_gen, resize_cb, dirty bitmaps and image size refreshing. The last of these issues broke iotest case 222, which is now fixed.
--- block/io.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
block/io.c +9 -13
@@ -3007,20 +3007,16 @@ static int coroutine_fn bdrv_co_copy_range_internal(bdrv_inc_in_flight (dst->bs);tracked_request_begin (&req, dst->bs, dst_offset, bytes,BDRV_TRACKED_WRITE ); - -/* BDRV_REQ_NO_SERIALISING is only for read operation */ -assert (!(write_flags &BDRV_REQ_NO_SERIALISING )); -if (write_flags &BDRV_REQ_SERIALISING ) { -mark_request_serialising (&req,bdrv_get_cluster_size (dst->bs)); + ret =bdrv_co_write_req_prepare (dst, dst_offset, bytes, &req, + write_flags); +if (!ret) { + ret = dst->bs->drv->bdrv_co_copy_range_to (dst->bs, + src, src_offset, + dst, dst_offset, + bytes, + read_flags, write_flags); } -wait_serialising_requests (&req); - - ret = dst->bs->drv->bdrv_co_copy_range_to (dst->bs, - src, src_offset, - dst, dst_offset, - bytes, - read_flags, write_flags); - +bdrv_co_write_req_finish (dst, dst_offset, bytes, &req, ret);tracked_request_end (&req);bdrv_dec_in_flight (dst->bs); }
Signature · 1 line
2.17.1
PATCH v3 9/10 block: Fix bdrv_co_truncate overlap check
In reply to Fam ZhengIf we are growing the image and potentially using preallocation for the new area, we need to make sure that no write requests are made to the "preallocated" area which is [@old_size, @offset), not [@offset, offset * 2 - @old_size).
--- block/io.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
block/io.c +2 -1
@@ -3113,7 +3113,8 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, }bdrv_inc_in_flight (bs); -tracked_request_begin (&req, bs, offset, new_bytes,BDRV_TRACKED_TRUNCATE ); +tracked_request_begin (&req, bs, offset - new_bytes, new_bytes, +BDRV_TRACKED_TRUNCATE );/* If we are growing the image and potentially using preallocation for the * new area, we need to make sure that no write requests are made to it
Signature · 1 line
2.17.1
PATCH v3 10/10 block: Use common write req handling in truncate
In reply to Fam ZhengTruncation is the last to convert from open coded req handling to reusing helpers. This time the permission check in prepare has to adapt to the new caller: it checks a different permission bit, and doesn't trigger the before write notifier.
Also, truncation should always trigger a bs->total_sectors update and in turn call parent resize_cb. Update the condition in finish helper, too.
It's intended to do a duplicated bs->read_only check before calling bdrv_co_write_req_prepare() so that we can be more informative with the error message, as bdrv_co_write_req_prepare() doesn't have Error parameter.
--- block/io.c | 57 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 21 deletions(-)
block/io.c +36 -21
@@ -1582,14 +1582,24 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, uint64_t bytes, is_request_serialising_and_aligned(req));assert (req->overlap_offset <= offset);assert (offset + bytes <= req->overlap_offset + req->overlap_bytes); - -if (flags &BDRV_REQ_WRITE_UNCHANGED ) { -assert (child->perm & (BLK_PERM_WRITE_UNCHANGED |BLK_PERM_WRITE )); - }else { -assert (child->perm &BLK_PERM_WRITE ); - }assert (end_sector <= bs->total_sectors || child->perm &BLK_PERM_RESIZE ); -return notifier_with_return_list_notify (&bs->before_write_notifiers, req); + +switch (req->type) { +case BDRV_TRACKED_WRITE : +case BDRV_TRACKED_DISCARD : +if (flags &BDRV_REQ_WRITE_UNCHANGED ) { +assert (child->perm & (BLK_PERM_WRITE_UNCHANGED |BLK_PERM_WRITE )); + }else { +assert (child->perm &BLK_PERM_WRITE ); + } +return notifier_with_return_list_notify (&bs->before_write_notifiers, + req); +case BDRV_TRACKED_TRUNCATE : +assert (child->perm &BLK_PERM_RESIZE ); +return 0 ; +default : +abort (); + } }static inline void coroutine_fn
@@ -1608,8 +1618,9 @@ bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, uint64_t bytes,* beyond EOF cannot expand the image anyway. * */ if (ret ==0 && - end_sector > bs->total_sectors && - req->type !=BDRV_TRACKED_DISCARD ) { + (req->type ==BDRV_TRACKED_TRUNCATE || + end_sector > bs->total_sectors) && + req->type !=BDRV_TRACKED_DISCARD ) { bs->total_sectors = end_sector;bdrv_parent_cb_resize (bs);bdrv_dirty_bitmap_truncate (bs, end_sector <<BDRV_SECTOR_BITS );
@@ -3088,7 +3099,6 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset,int64_t old_size, new_bytes;int ret; -assert (child->perm &BLK_PERM_RESIZE );/* if bs->drv == NULL, bs is closed, so there's nothing to do here */ if (!drv) {
@@ -3121,7 +3131,18 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset,* concurrently or they might be overwritten by preallocation. */ if (new_bytes) {mark_request_serialising (&req,1 ); -wait_serialising_requests (&req); + } +if (bs->read_only) { +error_setg (errp,"Image is read-only" ); + ret = -EACCES ; + goto out; + } + ret =bdrv_co_write_req_prepare (child, offset - new_bytes, new_bytes, &req, +0 ); +if (ret <0 ) { +error_setg_errno (errp, -ret, +"Failed to prepare request for truncation" ); + goto out; }if (!drv->bdrv_co_truncate) {
@@ -3133,13 +3154,6 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, ret = -ENOTSUP ; goto out; } -if (bs->read_only) { -error_setg (errp,"Image is read-only" ); - ret = -EACCES ; - goto out; - } - -assert (!(bs->open_flags &BDRV_O_INACTIVE )); ret = drv->bdrv_co_truncate (bs, offset, prealloc, errp);if (ret <0 ) {
@@ -3151,9 +3165,10 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, }else { offset = bs->total_sectors *BDRV_SECTOR_SIZE ; } -bdrv_dirty_bitmap_truncate (bs, offset); -bdrv_parent_cb_resize (bs); -atomic_inc (&bs->write_gen); +/* It's possible that truncation succeeded but refresh_total_sectors +* failed, but the latter doesn't affect how we should finish the request. +* Pass 0 as the last parameter so that dirty bitmaps etc. are handled. */ +bdrv_co_write_req_finish (child, offset - new_bytes, new_bytes, &req,0 ); out:tracked_request_end (&req);
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v3 07/10] block: Use common req handling for discard
In reply to Fam ZhengAm 10.07.2018 um 08:31 hat Fam Zheng geschrieben:
Quoted 15 lines
Reuse the new bdrv_co_write_req_prepare/finish helpers. The variation here is that discard requests don't affect
bs->wr_highest_offset, and it cannot extend the image.Signed-off-by Fam Zheng <[email protected]>--- block/io.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-)block/io.c +0
-0@@ -1601,15 +1601,31 @@ bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, uint64_t bytes,
block/io.c bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, uint64_t bytes, · line 1609
Quoted 10 lines
block/io.c +6
-2atomic_inc (&bs->write_gen); -stat64_max (&bs->wr_highest_offset, offset + bytes); - +/* Discard cannot extend the image, but in error handling cases, such as +* when reverting a qcow2 cluster allocation, the discarded range can pass +* the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD +* here. Instead, just skip it, since semantically a discard request +* beyond EOF cannot expand the image anyway. +* */
Oh, yet another style for the end of a multiline comment. :-)
I'll fix this while applying.
Kevin
Kevin Wolf
AppliedAm 10.07.2018 um 08:31 hat Fam Zheng geschrieben:
Quoted 20 lines
Based-on: [email protected]
v3: - Rebase onto Vladmir's:
[PATCH v5 0/4] fix image fleecing
on top of master, which has blklogwrites to be converted to BdrvChild for thebdrv_co_pdiscard()parameter. [Kevin]
- Addfile_prefix to file protocol trace points. [Kevin]
- Assert that BdrvTrackedRequest bytes and offset don't overflowINT64_MAX. [Kevin]
- Don't misuse req->offset/req->bytes in bdrv_co_write_req_prepare/finish. [Kevin]
- Fixstat64_max. [Kevin]
- Keep lines within 80 columns. [Kevin]
- Grammar fix in commit message and rev-by. [Eric]Qcow2 allocates new clusters after the end of the file. If it is the destinaton of copy offloading, we must adjust
dst->bs->total_sectors. Otherwise, further reads will drop to the "beyond EOF" code path and return zeroes, which problem is caught by iotests 222.
Quoted 5 lines
Follow the logic in the normal write code and update
bs->total_sectorsafter I/O is done.While at it, add a few convenient trace points to aid future debug experiences in the topic.
Thanks, applied to the block branch.
Kevin