fix max_discard for NBD/gluster block drivers
v2 cover fix max_discard for NBD/gluster block drivers
Changes from v1:
- switched to UINT32_MAX for NBD
- limited max_discard for gluster to SIZE_MAX on i686 only
PATCH v2 1/2 glusterfs: fix max_discard
qemu_gluster_co_discard calculates size to discard as follows
size_t size = nb_sectors * BDRV_SECTOR_SIZE;
ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.
Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.
--- block/gluster.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
block/gluster.c +11 -0
@@ -622,6 +622,13 @@ out:return ret; } +static void qemu_gluster_refresh_limits (BlockDriverState *bs,Error **errp) +{ +#if SIZE_MAX ==UINT_MAX + bs->bl.max_discard =SIZE_MAX >>BDRV_SECTOR_BITS ; +#endif +} +#ifdef CONFIG_GLUSTERFS_DISCARD static coroutine_fn int qemu_gluster_co_discard(BlockDriverState *bs,int64_t sector_num,int nb_sectors)
@@ -735,6 +742,7 @@ static BlockDriver bdrv_gluster = {#ifdef CONFIG_GLUSTERFS_DISCARD .bdrv_co_discard = qemu_gluster_co_discard, #endif + .bdrv_refresh_limits = qemu_gluster_refresh_limits, #ifdef CONFIG_GLUSTERFS_ZEROFILL .bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,#endif
@@ -762,6 +770,7 @@ static BlockDriver bdrv_gluster_tcp = {#ifdef CONFIG_GLUSTERFS_DISCARD .bdrv_co_discard = qemu_gluster_co_discard, #endif + .bdrv_refresh_limits = qemu_gluster_refresh_limits, #ifdef CONFIG_GLUSTERFS_ZEROFILL .bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,#endif
@@ -789,6 +798,7 @@ static BlockDriver bdrv_gluster_unix = {#ifdef CONFIG_GLUSTERFS_DISCARD .bdrv_co_discard = qemu_gluster_co_discard, #endif + .bdrv_refresh_limits = qemu_gluster_refresh_limits, #ifdef CONFIG_GLUSTERFS_ZEROFILL .bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,#endif
@@ -816,6 +826,7 @@ static BlockDriver bdrv_gluster_rdma = {#ifdef CONFIG_GLUSTERFS_DISCARD .bdrv_co_discard = qemu_gluster_co_discard, #endif + .bdrv_refresh_limits = qemu_gluster_refresh_limits, #ifdef CONFIG_GLUSTERFS_ZEROFILL .bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,#endif
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard
Am 02.02.2015 um 19:29 schrieb Denis V. Lunev:
Quoted 24 lines
qemu_gluster_co_discardcalculates size to discard as followssize_t size = nb_sectors * BDRV_SECTOR_SIZE; ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);
glfs_discard_asyncis declared as follows:
intglfs_discard_async(glfs_fd_t*fd,off_tlength,size_tlent,
glfs_io_cbkfn, void *data)__THROW
This is problematic on i686 assizeof(size_t)== 4.Set
bl_max_discardtoSIZE_MAX>>BDRV_SECTOR_BITSto avoid overflow on i386.Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Peter Lieven <[email protected]>--- block/gluster.c | 11 +++++++++++ 1 file changed, 11 insertions(+)block/gluster.c +0
-0
block/gluster.c out: · line 629
Quoted 8 lines
block/gluster.c +5
-0@@ -622,6 +622,13 @@ out:return ret; } +static void qemu_gluster_refresh_limits (BlockDriverState *bs,Error **errp) +{ +#if SIZE_MAX ==UINT_MAX + bs->bl.max_discard =SIZE_MAX >>BDRV_SECTOR_BITS ; +#endif
I would write:
bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
without the condition.
Peter
Quoted 37 lines
block/gluster.c +6
-0+} +#ifdef CONFIG_GLUSTERFS_DISCARD static coroutine_fn int qemu_gluster_co_discard(BlockDriverState *bs,int64_t sector_num,int nb_sectors)@@ -735,6 +742,7 @@ static BlockDriver bdrv_gluster = {#ifdef CONFIG_GLUSTERFS_DISCARD .bdrv_co_discard = qemu_gluster_co_discard, #endif + .bdrv_refresh_limits = qemu_gluster_refresh_limits, #ifdef CONFIG_GLUSTERFS_ZEROFILL .bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,#endif @@ -762,6 +770,7 @@ static BlockDriver bdrv_gluster_tcp = {#ifdef CONFIG_GLUSTERFS_DISCARD .bdrv_co_discard = qemu_gluster_co_discard, #endif + .bdrv_refresh_limits = qemu_gluster_refresh_limits, #ifdef CONFIG_GLUSTERFS_ZEROFILL .bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,#endif @@ -789,6 +798,7 @@ static BlockDriver bdrv_gluster_unix = {#ifdef CONFIG_GLUSTERFS_DISCARD .bdrv_co_discard = qemu_gluster_co_discard, #endif + .bdrv_refresh_limits = qemu_gluster_refresh_limits, #ifdef CONFIG_GLUSTERFS_ZEROFILL .bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,#endif @@ -816,6 +826,7 @@ static BlockDriver bdrv_gluster_rdma = {#ifdef CONFIG_GLUSTERFS_DISCARD .bdrv_co_discard = qemu_gluster_co_discard, #endif + .bdrv_refresh_limits = qemu_gluster_refresh_limits, #ifdef CONFIG_GLUSTERFS_ZEROFILL .bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,#endif
Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard
Quoted 35 lines · Peter Lieven
Am 02.02.2015 um 19:29 schrieb Denis V. Lunev:
Quoted 24 lines
qemu_gluster_co_discardcalculates size to discard as followssize_t size = nb_sectors * BDRV_SECTOR_SIZE; ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);
glfs_discard_asyncis declared as follows:
intglfs_discard_async(glfs_fd_t*fd,off_tlength,size_tlent,
glfs_io_cbkfn, void *data)__THROW
This is problematic on i686 assizeof(size_t)== 4.Set
bl_max_discardtoSIZE_MAX>>BDRV_SECTOR_BITSto avoid overflow on i386.Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Peter Lieven <[email protected]>--- block/gluster.c | 11 +++++++++++ 1 file changed, 11 insertions(+)block/gluster.c +0
-0block/gluster.c out: · line 629
Quoted 8 lines
block/gluster.c +5
-0@@ -622,6 +622,13 @@ out:return ret; } +static void qemu_gluster_refresh_limits (BlockDriverState *bs,Error **errp) +{ +#if SIZE_MAX ==UINT_MAX + bs->bl.max_discard =SIZE_MAX >>BDRV_SECTOR_BITS ; +#endif I would write:
Quoted 7 lines
bs->bl.max_discard=MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);without the condition.
Peter
ok, respinned
Den
PATCH v2 2/2 nbd: fix max_discard
In reply to Denis V. Lunevnbd_co_discard calls nbd_client_session_co_discard which uses uint32_t as the length in bytes of the data to discard due to the following definition:
struct nbd_request {
uint32_t magic;
uint32_t type;
uint64_t handle;
uint64_t from;
uint32_t len; <-- the length of data to be discarded, in bytes
} QEMU_PACKED;Thus we should limit bl_max_discard to UINT32_MAX >> BDRV_SECTOR_BITS to avoid overflow.
--- block/nbd.c | 8 ++++++++ 1 file changed, 8 insertions(+)
block/nbd.c +8 -0
@@ -301,6 +301,11 @@ static int nbd_co_flush(BlockDriverState *bs)return nbd_client_session_co_flush (&s->client); } +static void nbd_refresh_limits (BlockDriverState *bs,Error **errp) +{ + bs->bl.max_discard =UINT32_MAX >>BDRV_SECTOR_BITS ; +} +static int nbd_co_discard (BlockDriverState *bs,int64_t sector_num,int nb_sectors) {
@@ -396,6 +401,7 @@ static BlockDriver bdrv_nbd = { .bdrv_close = nbd_close, .bdrv_co_flush_to_os = nbd_co_flush, .bdrv_co_discard = nbd_co_discard, + .bdrv_refresh_limits = nbd_refresh_limits, .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_detach_aio_context, .bdrv_attach_aio_context = nbd_attach_aio_context,
@@ -413,6 +419,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_refresh_limits = nbd_refresh_limits, .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_detach_aio_context, .bdrv_attach_aio_context = nbd_attach_aio_context,
@@ -430,6 +437,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_refresh_limits = nbd_refresh_limits, .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_detach_aio_context, .bdrv_attach_aio_context = nbd_attach_aio_context,
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH 2/2] nbd: fix max_discard
Am 02.02.2015 um 19:29 schrieb Denis V. Lunev:
Quoted 55 lines
nbd_co_discardcallsnbd_client_session_co_discardwhich usesuint32_tas the length in bytes of the data to discard due to the following definition:struct nbd_request { uint32_t magic; uint32_t type; uint64_t handle; uint64_t from; uint32_t len; <-- the length of data to be discarded, in bytes } QEMU_PACKED;Thus we should limit
bl_max_discardtoUINT32_MAX>>BDRV_SECTOR_BITSto avoid overflow.Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Peter Lieven <[email protected]>--- block/nbd.c | 8 ++++++++ 1 file changed, 8 insertions(+)block/nbd.c +7
-0@@ -301,6 +301,11 @@ static int nbd_co_flush(BlockDriverState *bs)return nbd_client_session_co_flush (&s->client); } +static void nbd_refresh_limits (BlockDriverState *bs,Error **errp) +{ + bs->bl.max_discard =UINT32_MAX >>BDRV_SECTOR_BITS ; +} +static int nbd_co_discard (BlockDriverState *bs,int64_t sector_num,int nb_sectors) {@@ -396,6 +401,7 @@ static BlockDriver bdrv_nbd = { .bdrv_close = nbd_close, .bdrv_co_flush_to_os = nbd_co_flush, .bdrv_co_discard = nbd_co_discard, + .bdrv_refresh_limits = nbd_refresh_limits, .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_detach_aio_context, .bdrv_attach_aio_context = nbd_attach_aio_context,@@ -413,6 +419,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_refresh_limits = nbd_refresh_limits, .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_detach_aio_context, .bdrv_attach_aio_context = nbd_attach_aio_context,
block/nbd.c static BlockDriver bdrv_nbd_unix = { · line 443
Quoted 7 lines
block/nbd.c +1
-0@@ -430,6 +437,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_refresh_limits = nbd_refresh_limits, .bdrv_getlength = nbd_getlength, .bdrv_detach_aio_context = nbd_detach_aio_context, .bdrv_attach_aio_context = nbd_attach_aio_context,