introduce bitmap_try_new
cover introduce bitmap_try_new
this adds a new helper to handle errors when allocation a bitmap and also introduces its first user.
Peter Lieven (2):
util: introduce bitmap_try_new
block/iscsi: handle failure on malloc of the allocationmap
block/iscsi.c | 22 +++++++++++++++------- include/qemu/bitmap.h | 6 ++++++ 2 files changed, 21 insertions(+), 7 deletions(-)
Signature · 1 line
1.7.9.5
PATCH 1/2 util: introduce bitmap_try_new
regular bitmap_new simply aborts if the memory allocation fails. bitmap_try_new returns NULL on failure and allows for proper error handling.
--- include/qemu/bitmap.h | 6 ++++++ 1 file changed, 6 insertions(+)
include/qemu/bitmap.h +6 -0
@@ -94,6 +94,12 @@ static inline unsigned long *bitmap_new(long nbits)return g_malloc0 (len); } +static inline unsigned long *bitmap_try_new (long nbits) +{ +long len =BITS_TO_LONGS (nbits) *sizeof (unsigned long ); +return g_try_malloc0 (len); +} +static inline void bitmap_zero (unsigned long *dst,long nbits) {if (small_nbits (nbits)) {
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [PATCH 1/2] util: introduce bitmap_try_new
Quoted 14 lines · Peter Lieven
regular
bitmap_newsimply aborts if the memory allocation fails.bitmap_try_newreturns NULL on failure and allows for proper error handling.Signed-off-by Peter Lieven <[email protected]>--- include/qemu/bitmap.h | 6 ++++++ 1 file changed, 6 insertions(+)include/qemu/bitmap.h +0
-0
include/qemu/bitmap.h static inline unsigned long *bitmap_new(long nbits) · line 102
Quoted 9 lines
include/qemu/bitmap.h +6
-0@@ -94,6 +94,12 @@ static inline unsigned long *bitmap_new(long nbits)return g_malloc0 (len); } +static inline unsigned long *bitmap_try_new (long nbits) +{ +long len =BITS_TO_LONGS (nbits) *sizeof (unsigned long ); +return g_try_malloc0 (len); +} +
What you have works, but I personally would have reimplemented bitmap_new as the first caller of bitmap_try_new in this patch, where bitmap_new handles a NULL bitmap_try_new return by abort()ing, so that it has the same behavior. By routing the one function to use the other, we are future-proofing: if initialization of a bitmap ever needs modification, we only update bitmap_try_new, rather than copying the updates to both functions.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 1/2] util: introduce bitmap_try_new
Am 25.08.2014 um 17:09 schrieb Eric Blake:
Quoted 18 lines · Peter Lieven
Quoted 17 lines
regular
bitmap_newsimply aborts if the memory allocation fails.bitmap_try_newreturns NULL on failure and allows for proper error handling.Signed-off-by Peter Lieven <[email protected]>--- include/qemu/bitmap.h | 6 ++++++ 1 file changed, 6 insertions(+)include/qemu/bitmap.h +0
-0@@ -94,6 +94,12 @@ static inline unsigned long *bitmap_new(long nbits)return g_malloc0 (len); }
Quoted 14 lines
include/qemu/bitmap.h static inline unsigned long *bitmap_new(long nbits) · line 102
Quoted 6 lines
include/qemu/bitmap.h +6
-0+static inline unsigned long *bitmap_try_new (long nbits) +{ +long len =BITS_TO_LONGS (nbits) *sizeof (unsigned long ); +return g_try_malloc0 (len); +} +What you have works, but I personally would have reimplemented
bitmap_newas the first caller ofbitmap_try_newin this patch, wherebitmap_newhandles a NULLbitmap_try_newreturn by abort()ing, so that it has the same behavior. By routing the one function to use the other, we are future-proofing: if initialization of a bitmap ever needs modification, we only updatebitmap_try_new, rather than copying the updates to both functions.
Good point. What would be the right exit if we receive a NULL from bitmap_try_new? abort() ?
Peter
Re: [Qemu-devel] [PATCH 1/2] util: introduce bitmap_try_new
Quoted 10 lines · Peter Lieven
Quoted 9 lines
Quoted 1 line
+
What you have works, but I personally would have reimplemented
bitmap_newas the first caller ofbitmap_try_newin this patch, wherebitmap_newhandles a NULLbitmap_try_newreturn by abort()ing, so that it has the same behavior. By routing the one function to use the other, we are future-proofing: if initialization of a bitmap ever needs modification, we only updatebitmap_try_new, rather than copying the updates to both functions.
Quoted 2 lines
Good point. What would be the right exit if we receive a NULL from
bitmap_try_new?abort()?
I think that g_malloc() calls abort(), so yes, that sounds right.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 2/2 block/iscsi: handle failure on malloc of the allocationmap
In reply to Peter Lieven--- block/iscsi.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
block/iscsi.c +15 -7
@@ -325,6 +325,19 @@ static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,return 1 ; } +static unsigned long *iscsi_allocationmap_init (IscsiLun *iscsilun) +{ +unsigned long *ptr; + ptr =bitmap_try_new (DIV_ROUND_UP (sector_lun2qemu (iscsilun->num_blocks, + iscsilun), + iscsilun->cluster_sectors)); +if (ptr ==NULL ) { +error_report ("iSCSI: could not initialize allocationmap. " +"Out of memory." ); + } +return ptr; +} +static void iscsi_allocationmap_set (IscsiLun *iscsilun,int64_t sector_num,int nb_sectors) {
@@ -1413,9 +1426,7 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * iscsilun->block_size) >>BDRV_SECTOR_BITS ;if (iscsilun->lbprz && !(bs->open_flags &BDRV_O_NOCACHE )) { - iscsilun->allocationmap = -bitmap_new (DIV_ROUND_UP (bs->total_sectors, - iscsilun->cluster_sectors)); + iscsilun->allocationmap =iscsi_allocationmap_init (iscsilun); } }
@@ -1508,10 +1519,7 @@ static int iscsi_truncate(BlockDriverState *bs, int64_t offset)if (iscsilun->allocationmap !=NULL ) {g_free (iscsilun->allocationmap); - iscsilun->allocationmap = -bitmap_new (DIV_ROUND_UP (sector_lun2qemu (iscsilun->num_blocks, - iscsilun), - iscsilun->cluster_sectors)); + iscsilun->allocationmap =iscsi_allocationmap_init (iscsilun); }return 0 ;
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [PATCH 2/2] block/iscsi: handle failure on malloc of the allocationmap
Il 22/08/2014 11:26, Peter Lieven ha scritto:
Quoted 30 lines
Signed-off-by Peter Lieven <[email protected]>--- block/iscsi.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)block/iscsi.c +13
-0@@ -325,6 +325,19 @@ static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,return 1 ; } +static unsigned long *iscsi_allocationmap_init (IscsiLun *iscsilun) +{ +unsigned long *ptr; + ptr =bitmap_try_new (DIV_ROUND_UP (sector_lun2qemu (iscsilun->num_blocks, + iscsilun), + iscsilun->cluster_sectors)); +if (ptr ==NULL ) { +error_report ("iSCSI: could not initialize allocationmap. " +"Out of memory." ); + } +return ptr; +} +static void iscsi_allocationmap_set (IscsiLun *iscsilun,int64_t sector_num,int nb_sectors) {
block/iscsi.c static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, · line 1431
Quoted 9 lines
block/iscsi.c +1
-3@@ -1413,9 +1426,7 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * iscsilun->block_size) >>BDRV_SECTOR_BITS ;if (iscsilun->lbprz && !(bs->open_flags &BDRV_O_NOCACHE )) { - iscsilun->allocationmap = -bitmap_new (DIV_ROUND_UP (bs->total_sectors, - iscsilun->cluster_sectors)); + iscsilun->allocationmap =iscsi_allocationmap_init (iscsilun); } }
iscsi_open has an Error ** argument. Please pass it to iscsi_allocationmap_init and use error_setg instead of error_report.
block/iscsi.c static int iscsi_truncate(BlockDriverState *bs, int64_t offset) · line 1522
Quoted 9 lines
block/iscsi.c +1
-4@@ -1508,10 +1519,7 @@ static int iscsi_truncate(BlockDriverState *bs, int64_t offset)if (iscsilun->allocationmap !=NULL ) {g_free (iscsilun->allocationmap); - iscsilun->allocationmap = -bitmap_new (DIV_ROUND_UP (sector_lun2qemu (iscsilun->num_blocks, - iscsilun), - iscsilun->cluster_sectors)); + iscsilun->allocationmap =iscsi_allocationmap_init (iscsilun);
Here you may have to use qerror_report_err, though I guess the failure need not be fatal and you can leave the allocationmap set to NULL.
Paolo
Quoted 4 lines
block/iscsi.c +0
-0}return 0 ;
Re: [Qemu-devel] [PATCH 2/2] block/iscsi: handle failure on malloc of the allocationmap
Quoted 34 lines · Paolo Bonzini
Il 22/08/2014 11:26, Peter Lieven ha scritto:
Quoted 33 lines
Signed-off-by Peter Lieven <[email protected]>--- block/iscsi.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)block/iscsi.c +13
-0@@ -325,6 +325,19 @@ static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,return 1 ; } +static unsigned long *iscsi_allocationmap_init (IscsiLun *iscsilun) +{ +unsigned long *ptr; + ptr =bitmap_try_new (DIV_ROUND_UP (sector_lun2qemu (iscsilun->num_blocks, + iscsilun), + iscsilun->cluster_sectors)); +if (ptr ==NULL ) { +error_report ("iSCSI: could not initialize allocationmap. " +"Out of memory." ); + } +return ptr; +} +static void iscsi_allocationmap_set (IscsiLun *iscsilun,int64_t sector_num,int nb_sectors) {@@ -1413,9 +1426,7 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * iscsilun->block_size) >>BDRV_SECTOR_BITS ;if (iscsilun->lbprz && !(bs->open_flags &BDRV_O_NOCACHE )) {
Quoted 8 lines
block/iscsi.c static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, · line 1431
Quoted 6 lines
block/iscsi.c +1
-3- iscsilun->allocationmap = -bitmap_new (DIV_ROUND_UP (bs->total_sectors, - iscsilun->cluster_sectors)); + iscsilun->allocationmap =iscsi_allocationmap_init (iscsilun); } }
iscsi_openhas an Error ** argument. Please pass it toiscsi_allocationmap_initand useerror_setginstead oferror_report.
I could pass the Error argument and use error_report only if the pointer is null.
Quoted 4 lines
Quoted 4 lines
block/iscsi.c +0
-0@@ -1508,10 +1519,7 @@ static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
Quoted 9 lines
block/iscsi.c static int iscsi_truncate(BlockDriverState *bs, int64_t offset) · line 1522
Quoted 7 lines
block/iscsi.c +1
-4if (iscsilun->allocationmap !=NULL ) {g_free (iscsilun->allocationmap); - iscsilun->allocationmap = -bitmap_new (DIV_ROUND_UP (sector_lun2qemu (iscsilun->num_blocks, - iscsilun), - iscsilun->cluster_sectors)); + iscsilun->allocationmap =iscsi_allocationmap_init (iscsilun);Here you may have to use
qerror_report_err, though I guess the failure need not be fatal and you can leave the allocationmap set to NULL.
That was the plan. I would also not fail on iscsi_open or would you?
Peter
Quoted 7 lines
Paolo
Quoted 4 lines
block/iscsi.c +0
-0}return 0 ;
Signature · 16 lines
Mit freundlichen Grüßen Peter Lieven ........................................................... KAMP Netzwerkdienste GmbH Vestische Str. 89-91 | 46117 Oberhausen Tel: +49 (0) 208.89 402-50 | Fax: +49 (0) 208.89 402-40 [email protected] | http://www.kamp.de Geschäftsführer: Heiner Lante | Michael Lante Amtsgericht Duisburg | HRB Nr. 12154 USt-Id-Nr.: DE 120607556 ...........................................................
Re: [Qemu-devel] [PATCH 2/2] block/iscsi: handle failure on malloc of the allocationmap
Il 25/08/2014 13:10, Peter Lieven ha scritto:
Quoted 49 lines · Paolo Bonzini
Quoted 39 lines
Il 22/08/2014 11:26, Peter Lieven ha scritto:
Quoted 38 lines
Signed-off-by Peter Lieven <[email protected]>--- block/iscsi.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)block/iscsi.c +12
-0@@ -325,6 +325,19 @@ static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors, return 1; } +static unsigned long *iscsi_allocationmap_init(IscsiLun *iscsilun) +{ +unsigned long *ptr; + ptr =bitmap_try_new (DIV_ROUND_UP (sector_lun2qemu (iscsilun->num_blocks, + iscsilun), + iscsilun->cluster_sectors)); +if (ptr ==NULL ) { +error_report ("iSCSI: could not initialize allocationmap. " +"Out of memory." ); + } +return ptr; +} + static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t
sector_num,
intnb_sectors)
{block/iscsi.c +0
-1@@ -1413,9 +1426,7 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * iscsilun->block_size) >>BDRV_SECTOR_BITS ;if (iscsilun->lbprz && !(bs->open_flags &BDRV_O_NOCACHE )) { - iscsilun->allocationmap =Quoted 8 lines
Quoted 6 lines
block/iscsi.c +1
-2-bitmap_new (DIV_ROUND_UP (bs->total_sectors, - iscsilun->cluster_sectors)); + iscsilun->allocationmap =iscsi_allocationmap_init (iscsilun);} }
iscsi_openhas an Error ** argument. Please pass it toiscsi_allocationmap_initand useerror_setginstead oferror_report.
Quoted 2 lines
I could pass the Error argument and use
error_reportonly if the pointer is null.
No, NULL means "I don't care about errors, or I don't care about precise error messages and will use the return value to check for errors".
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
block/iscsi.c +0
-0@@ -1508,10 +1519,7 @@ static int iscsi_truncate(BlockDriverState *bs, int64_t offset)if (iscsilun->allocationmap !=NULL ) {g_free (iscsilun->allocationmap);Quoted 8 lines
block/iscsi.c static int iscsi_truncate(BlockDriverState *bs, int64_t offset) · line 1522
Quoted 6 lines
block/iscsi.c +1
-4- iscsilun->allocationmap = - bitmap_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks, - iscsilun), - iscsilun->cluster_sectors)); + iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun);Here you may have to use
qerror_report_err, though I guess the failure need not be fatal and you can leave the allocationmap set to NULL.
Quoted 1 line
That was the plan. I would also not fail on
iscsi_openor would you?
Hmm, good question. If so, the patch is correct with error_report. I thought a failure in iscsi_open would be less surprising. The question then is, do we need an option to disable allocationmap queries?
Paolo
Re: [Qemu-devel] [PATCH 2/2] block/iscsi: handle failure on malloc of the allocationmap
Quoted 66 lines · Paolo Bonzini
Il 25/08/2014 13:10, Peter Lieven ha scritto:
Quoted 48 lines · Paolo Bonzini
Quoted 39 lines
Il 22/08/2014 11:26, Peter Lieven ha scritto:
Quoted 38 lines
Signed-off-by Peter Lieven <[email protected]>--- block/iscsi.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)block/iscsi.c +12
-0@@ -325,6 +325,19 @@ static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors, return 1; } +static unsigned long *iscsi_allocationmap_init(IscsiLun *iscsilun) +{ +unsigned long *ptr; + ptr =bitmap_try_new (DIV_ROUND_UP (sector_lun2qemu (iscsilun->num_blocks, + iscsilun), + iscsilun->cluster_sectors)); +if (ptr ==NULL ) { +error_report ("iSCSI: could not initialize allocationmap. " +"Out of memory." ); + } +return ptr; +} + static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t
sector_num,
intnb_sectors)
{block/iscsi.c +0
-1@@ -1413,9 +1426,7 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * iscsilun->block_size) >>BDRV_SECTOR_BITS ;if (iscsilun->lbprz && !(bs->open_flags &BDRV_O_NOCACHE )) { - iscsilun->allocationmap =Quoted 8 lines
Quoted 6 lines
block/iscsi.c +1
-2-bitmap_new (DIV_ROUND_UP (bs->total_sectors, - iscsilun->cluster_sectors)); + iscsilun->allocationmap =iscsi_allocationmap_init (iscsilun);} }
iscsi_openhas an Error ** argument. Please pass it toiscsi_allocationmap_initand useerror_setginstead oferror_report.Quoted 2 lines
I could pass the Error argument and use
error_reportonly if the pointer is null.No, NULL means "I don't care about errors, or I don't care about precise error messages and will use the return value to check for errors".
Quoted 12 lines
Quoted 12 lines
block/iscsi.c static int iscsi_truncate(BlockDriverState · line 1523
Quoted 10 lines
block/iscsi.c +1
-4@@ -1508,10 +1519,7 @@ static int iscsi_truncate(BlockDriverState *bs, int64_t offset)if (iscsilun->allocationmap !=NULL ) {g_free (iscsilun->allocationmap); - iscsilun->allocationmap = - bitmap_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks, - iscsilun), - iscsilun->cluster_sectors)); + iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun);Here you may have to use
qerror_report_err, though I guess the failure need not be fatal and you can leave the allocationmap set to NULL.
Quoted 4 lines
Quoted 1 line
That was the plan. I would also not fail on
iscsi_openor would you?Hmm, good question. If so, the patch is correct with
error_report. I thought a failure iniscsi_openwould be less surprising. The question then is, do we need an option to disable allocationmap queries?
We have cache=none... Otherwise if allocationmap is NULL the code can already handle it.
Peter
Re: [Qemu-devel] [PATCH 2/2] block/iscsi: handle failure on malloc of the allocationmap
Il 25/08/2014 13:26, Peter Lieven ha scritto:
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Hmm, good question. If so, the patch is correct with
error_report. I thought a failure iniscsi_openwould be less surprising. The question then is, do we need an option to disable allocationmap queries?We have cache=none...
Doh, right. Then I think iscsi_open should fail if it cannot allocate the map.
Paolo