nbd: fix block-mirror NBD target
PATCH 1/1 nbd: fix block-mirror NBD target
There is VM with 8 GB QCOW2 storage. Real size of the image file is 2 GB. 'drive-mirror' redirected to NBD target creates 8 GB image at destination. The situation is even worse as zeroes are sent through the channel.
The patch simply adds .bdrv_co_write_zeroes callback to NBD block driver which works though NBD_TRIM to avoid transfer of zeroes.
--- block/nbd-client.c | 10 ++++++++++ block/nbd-client.h | 2 ++ block/nbd.c | 9 +++++++++ 3 files changed, 21 insertions(+)
block/nbd-client.c +10 -0
@@ -344,7 +344,17 @@ int nbd_client_co_discard(BlockDriverState *bs, int64_t sector_num, }nbd_coroutine_end (client, &request);return -reply.error; +} + +int nbd_client_co_write_zeroes (BlockDriverState *bs,int64_t sector_num, +int nb_sectors,BdrvRequestFlags flags) +{ +NbdClientSession *client =nbd_get_client_session (bs); +if (!(client->nbdflags &NBD_FLAG_SEND_TRIM )) { +return -ENOTSUP ; + } +return nbd_client_co_discard (bs, sector_num, nb_sectors); }void nbd_client_detach_aio_context (BlockDriverState *bs)
block/nbd-client.h +2 -0
@@ -40,6 +40,8 @@ void nbd_client_close(BlockDriverState *bs);int nbd_client_co_discard (BlockDriverState *bs,int64_t sector_num,int nb_sectors); +int nbd_client_co_write_zeroes (BlockDriverState *bs,int64_t sector_num, +int nb_sectors,BdrvRequestFlags flags);int nbd_client_co_flush (BlockDriverState *bs);int nbd_client_co_writev (BlockDriverState *bs,int64_t sector_num,int nb_sectors,QEMUIOVector *qiov);
block/nbd.c +9 -0
@@ -318,6 +318,13 @@ static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,return nbd_client_co_discard (bs, sector_num, nb_sectors); } +static coroutine_fn int nbd_co_write_zeroes(BlockDriverState *bs, +int64_t sector_num,int nb_sectors,BdrvRequestFlags flags) +{ +return nbd_client_co_write_zeroes (bs, sector_num, nb_sectors, flags); +} + +static void nbd_close (BlockDriverState *bs) {nbd_client_close (bs);
@@ -415,6 +422,7 @@ static BlockDriver bdrv_nbd_tcp = { .bdrv_close = nbd_close, .bdrv_co_flush_to_os = nbd_co_flush, .bdrv_co_discard = nbd_co_discard, + .bdrv_co_write_zeroes = nbd_co_write_zeroes, .bdrv_refresh_limits = nbd_refresh_limits, .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_detach_aio_context,
@@ -433,6 +441,7 @@ static BlockDriver bdrv_nbd_unix = { .bdrv_close = nbd_close, .bdrv_co_flush_to_os = nbd_co_flush, .bdrv_co_discard = nbd_co_discard, + .bdrv_co_write_zeroes = nbd_co_write_zeroes, .bdrv_refresh_limits = nbd_refresh_limits, .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_detach_aio_context,
Signature · 1 line
2.5.0
Quoted 6 lines · Denis V. Lunev
There is VM with 8 GB QCOW2 storage. Real size of the image file is 2 GB. 'drive-mirror' redirected to NBD target creates 8 GB image at destination. The situation is even worse as zeroes are sent through the channel.
The patch simply adds .bdrv_co_write_zeroes callback to NBD block driver which works though
NBD_TRIMto avoid transfer of zeroes.
The specification[1] says the following about TRIM:
Quoted 3 lines
After issuing this command, a client MUST NOT make any assumptions about the contents of the export affected by this command, until overwriting it again with
NBD_CMD_WRITE.
So I don't think this is correct.
The correct solution would probably to introduce a specific command to write zeroes, or at least to use detect-zeroes on the NBD server side (but this will still lead to the zeroes being sent over the line).
Max
[1] https://github.com/yoe/nbd/blob/master/doc/proto.md
Quoted 7 lines
Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>--- block/nbd-client.c | 10 ++++++++++ block/nbd-client.h | 2 ++ block/nbd.c | 9 +++++++++ 3 files changed, 21 insertions(+)
Quoted 14 lines · Max Reitz
Quoted 6 lines · Denis V. Lunev
There is VM with 8 GB QCOW2 storage. Real size of the image file is 2 GB. 'drive-mirror' redirected to NBD target creates 8 GB image at destination. The situation is even worse as zeroes are sent through the channel.
The patch simply adds .bdrv_co_write_zeroes callback to NBD block driver which works though
NBD_TRIMto avoid transfer of zeroes.The specification[1] says the following about TRIM:
Quoted 3 lines
After issuing this command, a client MUST NOT make any assumptions about the contents of the export affected by this command, until overwriting it again with
NBD_CMD_WRITE.So I don't think this is correct.
Quoted 16 lines
The correct solution would probably to introduce a specific command to write zeroes, or at least to use detect-zeroes on the NBD server side (but this will still lead to the zeroes being sent over the line).
Max
[1] https://github.com/yoe/nbd/blob/master/doc/proto.md
Quoted 7 lines
Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>--- block/nbd-client.c | 10 ++++++++++ block/nbd-client.h | 2 ++ block/nbd.c | 9 +++++++++ 3 files changed, 21 insertions(+)
This sounds not that good. OK. At least there is correct lengthy way for this. White-outs must be supported by the protocol.
Den