block: bdrv_img_create(): propagate errors
v2 cover block: bdrv_img_create(): propagate errors
By adding error propagation to bdrv_img_create() we improve error reporting in qmp_transaction() and simplify qemu-img.c:img_create() a bit.
Please, check individual patches for details.
o v2
- Fix small inconsistencies
- Drop patches that moved the printing of the "Formatting" message to qemu-img
Luiz Capitulino (5):
block: bdrv_img_create(): add Error ** argument
qemu-img: img_create(): pass Error object to bdrv_img_create()
qemu-img: img_create(): drop unneeded goto and ret variable
qmp: qmp_transaction(): pass Error object to bdrv_img_create()
block: bdrv_img_create(): drop unused error handling code
Paolo Bonzini (1):
error: add error_set_errno and error_setg_errno
block.c | 60 +++++++++++++++++++++++++----------------------------------- block.h | 6 +++--- blockdev.c | 13 +++++++------ error.c | 28 ++++++++++++++++++++++++++++ error.h | 9 +++++++++ qemu-img.c | 19 ++++++++++--------- 6 files changed, 82 insertions(+), 53 deletions(-)
Signature · 1 line
1.7.12.315.g682ce8b
PATCH v2 1/6 error: add error_set_errno and error_setg_errno
From: Paolo Bonzini <[email protected]>
These functions help maintaining homogeneous formatting of error messages that include strerror values.
---
This patch is also in a pull request from Paolo to Anthony. We can just merge it along with this series and git will do the right thing when merging it in master.
error.c | 28 ++++++++++++++++++++++++++++ error.h | 9 +++++++++ 2 files changed, 37 insertions(+)
error.c +28 -0
@@ -43,6 +43,34 @@ void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...) *errp = err; } +void error_set_errno (Error **errp,int os_errno,ErrorClass err_class, +const char *fmt, ...) +{ +Error *err; +char *msg1; +va_list ap; + +if (errp ==NULL ) { +return ; + } +assert (*errp ==NULL ); + + err =g_malloc0 (sizeof (*err)); + +va_start (ap, fmt); + msg1 =g_strdup_vprintf (fmt, ap); +if (os_errno !=0 ) { + err->msg =g_strdup_printf ("%s: %s" , msg1,strerror (os_errno)); +g_free (msg1); + }else { + err->msg = msg1; + } +va_end (ap); + err->err_class = err_class; + + *errp = err; +} +Error *error_copy (const Error *err) {Error *err_new;
error.h +9 -0
@@ -30,10 +30,19 @@ typedef struct Error Error; void error_set(Error **err, ErrorClass err_class, const char *fmt, ...) GCC_FMT_ATTR(3, 4);/** +* Set an indirect pointer to an error given a ErrorClass value and a +* printf-style human message, followed by a strerror() string if +* @os_error is not zero. +*/ +void error_set_errno(Error **err, int os_error, ErrorClass err_class, const char *fmt, ...) GCC_FMT_ATTR(4, 5); + +/** * Same as error_set(), but sets a generic error */ #define error_setg (err, fmt, ...) \ error_set(err, ERROR_CLASS_GENERIC_ERROR, fmt, ## __VA_ARGS__) +#define error_setg_errno (err, os_error, fmt, ...) \ + error_set_errno(err, os_error, ERROR_CLASS_GENERIC_ERROR, fmt, ## __VA_ARGS__)/** * Returns true if an indirect pointer to an error is pointing to a valid
Signature · 1 line
1.7.12.315.g682ce8b
PATCH v2 2/6 block: bdrv_img_create(): add Error ** argument
In reply to Luiz CapitulinoThis commit adds an Error ** argument to bdrv_img_create() and set it appropriately on error.
Callers of bdrv_img_create() pass NULL for the new argument and still rely on bdrv_img_create()'s return value. Next commits will change callers to use the Error object instead.
--- block.c | 22 +++++++++++++++++++++- block.h | 2 +- blockdev.c | 2 +- qemu-img.c | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-)
block.c +21 -1
@@ -4294,7 +4294,7 @@ bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)int bdrv_img_create (const char *filename,const char *fmt,const char *base_filename,const char *base_fmt, -char *options,uint64_t img_size,int flags) +char *options,uint64_t img_size,int flags,Error **errp) {QEMUOptionParameter *param =NULL , *create_options =NULL ;QEMUOptionParameter *backing_fmt, *backing_file, *size;
@@ -4307,6 +4307,7 @@ int bdrv_img_create(const char *filename, const char *fmt, drv =bdrv_find_format (fmt);if (!drv) {error_report ("Unknown file format '%s'" , fmt); +error_setg (errp,"Unknown file format '%s'" , fmt); ret = -EINVAL ; goto out; }
@@ -4314,6 +4315,7 @@ int bdrv_img_create(const char *filename, const char *fmt, proto_drv =bdrv_find_protocol (filename);if (!proto_drv) {error_report ("Unknown protocol '%s'" , filename); +error_setg (errp,"Unknown protocol '%s'" , filename); ret = -EINVAL ; goto out; }
@@ -4333,6 +4335,7 @@ int bdrv_img_create(const char *filename, const char *fmt, param =parse_option_parameters (options, create_options, param);if (param ==NULL ) {error_report ("Invalid options for file format '%s'." , fmt); +error_setg (errp,"Invalid options for file format '%s'." , fmt); ret = -EINVAL ; goto out; }
@@ -4343,6 +4346,8 @@ int bdrv_img_create(const char *filename, const char *fmt, base_filename)) {error_report ("Backing file not supported for file format '%s'" , fmt); +error_setg (errp,"Backing file not supported for file format '%s'" , + fmt); ret = -EINVAL ; goto out; }
@@ -4352,6 +4357,8 @@ int bdrv_img_create(const char *filename, const char *fmt,if (set_option_parameter (param,BLOCK_OPT_BACKING_FMT , base_fmt)) {error_report ("Backing file format not supported for file " "format '%s'" , fmt); +error_setg (errp,"Backing file format not supported for file " +"format '%s'" , fmt); ret = -EINVAL ; goto out; }
@@ -4362,6 +4369,8 @@ int bdrv_img_create(const char *filename, const char *fmt,if (!strcmp (filename, backing_file->value.s)) {error_report ("Error: Trying to create an image with the " "same filename as the backing file" ); +error_setg (errp,"Error: Trying to create an image with the " +"same filename as the backing file" ); ret = -EINVAL ; goto out; }
@@ -4373,6 +4382,8 @@ int bdrv_img_create(const char *filename, const char *fmt,if (!backing_drv) {error_report ("Unknown backing file format '%s'" , backing_fmt->value.s); +error_setg (errp,"Unknown backing file format '%s'" , + backing_fmt->value.s); ret = -EINVAL ; goto out; }
@@ -4396,6 +4407,8 @@ int bdrv_img_create(const char *filename, const char *fmt, ret =bdrv_open (bs, backing_file->value.s, back_flags, backing_drv);if (ret <0 ) {error_report ("Could not open '%s'" , backing_file->value.s); +error_setg_errno (errp, -ret,"Could not open '%s'" , + backing_file->value.s); goto out; }bdrv_get_geometry (bs, &size);
@@ -4405,6 +4418,7 @@ int bdrv_img_create(const char *filename, const char *fmt,set_option_parameter (param,BLOCK_OPT_SIZE , buf); }else {error_report ("Image creation needs a size parameter" ); +error_setg (errp,"Image creation needs a size parameter" ); ret = -EINVAL ; goto out; }
@@ -4420,12 +4434,18 @@ int bdrv_img_create(const char *filename, const char *fmt,if (ret == -ENOTSUP ) {error_report ("Formatting or formatting option not supported for " "file format '%s'" , fmt); +error_setg (errp,"Formatting or formatting option not supported for " +"file format '%s'" , fmt); }else if (ret == -EFBIG ) {error_report ("The image size is too large for file format '%s'" , fmt); +error_setg (errp,"The image size is too large for file format '%s'" , + fmt); }else {error_report ("%s: error while creating %s: %s" , filename, fmt,strerror (-ret)); +error_setg (errp,"%s: error while creating %s: %s" , filename, fmt, +strerror (-ret)); } }
block.h +1 -1
@@ -341,7 +341,7 @@ int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,int bdrv_img_create (const char *filename,const char *fmt,const char *base_filename,const char *base_fmt, -char *options,uint64_t img_size,int flags); +char *options,uint64_t img_size,int flags,Error **errp);void bdrv_set_buffer_alignment (BlockDriverState *bs,int align);void *qemu_blockalign (BlockDriverState *bs,size_t size);
blockdev.c +1 -1
@@ -783,7 +783,7 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp) ret =bdrv_img_create (new_image_file, format, states->old_bs->filename, states->old_bs->drv->format_name, -NULL ,-1 , flags); +NULL ,-1 , flags,NULL );if (ret) {error_set (errp,QERR_OPEN_FILE_FAILED , new_image_file); goto delete_and_fail;
qemu-img.c +1 -1
@@ -362,7 +362,7 @@ static int img_create(int argc, char **argv) } ret =bdrv_img_create (filename, fmt, base_filename, base_fmt, - options, img_size,BDRV_O_FLAGS ); + options, img_size,BDRV_O_FLAGS ,NULL ); out:if (ret) {return 1 ;
Signature · 1 line
1.7.12.315.g682ce8b
PATCH v2 3/6 qemu-img: img_create(): pass Error object to bdrv_img_create()
In reply to Luiz Capitulino--- qemu-img.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
qemu-img.c +9 -2
@@ -301,6 +301,7 @@ static int img_create(int argc, char **argv)const char *filename;const char *base_filename =NULL ;char *options =NULL ; +Error *local_err =NULL ;for (;;) { c =getopt (argc, argv,"F:b:f:he6o:" );
@@ -361,8 +362,14 @@ static int img_create(int argc, char **argv) goto out; } - ret =bdrv_img_create (filename, fmt, base_filename, base_fmt, - options, img_size,BDRV_O_FLAGS ,NULL ); +bdrv_img_create (filename, fmt, base_filename, base_fmt, + options, img_size,BDRV_O_FLAGS , &local_err); +if (error_is_set (&local_err)) { +error_report ("%s" ,error_get_pretty (local_err)); +error_free (local_err); + ret =-1 ; + } + out:if (ret) {return 1 ;
Signature · 1 line
1.7.12.315.g682ce8b
PATCH v2 4/6 qemu-img: img_create(): drop unneeded goto and ret variable
In reply to Luiz Capitulino--- qemu-img.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
qemu-img.c +4 -10
@@ -294,7 +294,7 @@ static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,static int img_create (int argc,char **argv) { -int c, ret =0 ; +int c;uint64_t img_size =-1 ;const char *fmt ="raw" ;const char *base_fmt =NULL ;
@@ -351,15 +351,13 @@ static int img_create(int argc, char **argv)error_report ("Invalid image size specified! You may use k, M, G or " "T suffixes for " );error_report ("kilobytes, megabytes, gigabytes and terabytes." ); - ret =-1 ; - goto out; +return 1 ; } img_size = (uint64_t )sval; }if (options &&is_help_option (options)) { - ret =print_block_option_help (filename, fmt); - goto out; +return print_block_option_help (filename, fmt); } bdrv_img_create(filename, fmt, base_filename, base_fmt,
@@ -367,13 +365,9 @@ static int img_create(int argc, char **argv)if (error_is_set (&local_err)) {error_report ("%s" ,error_get_pretty (local_err));error_free (local_err); - ret =-1 ; - } - -out: -if (ret) {return 1 ; } +return 0 ; }
Signature · 1 line
1.7.12.315.g682ce8b
PATCH v2 5/6 qmp: qmp_transaction(): pass Error object to bdrv_img_create()
In reply to Luiz Capitulino--- blockdev.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
blockdev.c +7 -6
@@ -701,6 +701,7 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)int ret =0 ;BlockdevActionList *dev_entry = dev_list;BlkTransactionStates *states, *next; +Error *local_err =NULL ; QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;QSIMPLEQ_INIT (&snap_bdrv_states);
@@ -780,12 +781,12 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)/* create new image w/backing file */ if (mode !=NEW_IMAGE_MODE_EXISTING ) { - ret =bdrv_img_create (new_image_file, format, - states->old_bs->filename, - states->old_bs->drv->format_name, -NULL ,-1 , flags,NULL ); -if (ret) { -error_set (errp,QERR_OPEN_FILE_FAILED , new_image_file); +bdrv_img_create (new_image_file, format, + states->old_bs->filename, + states->old_bs->drv->format_name, +NULL ,-1 , flags, &local_err); +if (error_is_set (&local_err)) { +error_propagate (errp, local_err); goto delete_and_fail; } }
Signature · 1 line
1.7.12.315.g682ce8b
PATCH v2 6/6 block: bdrv_img_create(): drop unused error handling code
In reply to Luiz Capitulino--- block.c | 40 +++++----------------------------------- block.h | 6 +++--- 2 files changed, 8 insertions(+), 38 deletions(-)
block.c +5 -35
@@ -4292,9 +4292,9 @@ bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) bs->total_time_ns[cookie->type] +=get_clock () - cookie->start_time_ns; } -int bdrv_img_create (const char *filename,const char *fmt, -const char *base_filename,const char *base_fmt, -char *options,uint64_t img_size,int flags,Error **errp) +void bdrv_img_create (const char *filename,const char *fmt, +const char *base_filename,const char *base_fmt, +char *options,uint64_t img_size,int flags,Error **errp) {QEMUOptionParameter *param =NULL , *create_options =NULL ;QEMUOptionParameter *backing_fmt, *backing_file, *size;
@@ -4306,18 +4306,14 @@ int bdrv_img_create(const char *filename, const char *fmt,/* Find driver and parse its options */ drv =bdrv_find_format (fmt);if (!drv) { -error_report ("Unknown file format '%s'" , fmt);error_setg (errp,"Unknown file format '%s'" , fmt); - ret = -EINVAL ; - goto out; +return ; } proto_drv =bdrv_find_protocol (filename);if (!proto_drv) { -error_report ("Unknown protocol '%s'" , filename);error_setg (errp,"Unknown protocol '%s'" , filename); - ret = -EINVAL ; - goto out; +return ; } create_options = append_option_parameters(create_options,
@@ -4334,9 +4330,7 @@ int bdrv_img_create(const char *filename, const char *fmt,if (options) { param =parse_option_parameters (options, create_options, param);if (param ==NULL ) { -error_report ("Invalid options for file format '%s'." , fmt);error_setg (errp,"Invalid options for file format '%s'." , fmt); - ret = -EINVAL ; goto out; } }
@@ -4344,22 +4338,16 @@ int bdrv_img_create(const char *filename, const char *fmt,if (base_filename) {if (set_option_parameter (param,BLOCK_OPT_BACKING_FILE , base_filename)) { -error_report ("Backing file not supported for file format '%s'" , - fmt);error_setg (errp,"Backing file not supported for file format '%s'" , fmt); - ret = -EINVAL ; goto out; } }if (base_fmt) {if (set_option_parameter (param,BLOCK_OPT_BACKING_FMT , base_fmt)) { -error_report ("Backing file format not supported for file " -"format '%s'" , fmt);error_setg (errp,"Backing file format not supported for file " "format '%s'" , fmt); - ret = -EINVAL ; goto out; } }
@@ -4367,11 +4355,8 @@ int bdrv_img_create(const char *filename, const char *fmt, backing_file =get_option_parameter (param,BLOCK_OPT_BACKING_FILE );if (backing_file && backing_file->value.s) {if (!strcmp (filename, backing_file->value.s)) { -error_report ("Error: Trying to create an image with the " -"same filename as the backing file" );error_setg (errp,"Error: Trying to create an image with the " "same filename as the backing file" ); - ret = -EINVAL ; goto out; } }
@@ -4380,11 +4365,8 @@ int bdrv_img_create(const char *filename, const char *fmt,if (backing_fmt && backing_fmt->value.s) { backing_drv =bdrv_find_format (backing_fmt->value.s);if (!backing_drv) { -error_report ("Unknown backing file format '%s'" , - backing_fmt->value.s);error_setg (errp,"Unknown backing file format '%s'" , backing_fmt->value.s); - ret = -EINVAL ; goto out; } }
@@ -4406,7 +4388,6 @@ int bdrv_img_create(const char *filename, const char *fmt, ret =bdrv_open (bs, backing_file->value.s, back_flags, backing_drv);if (ret <0 ) { -error_report ("Could not open '%s'" , backing_file->value.s);error_setg_errno (errp, -ret,"Could not open '%s'" , backing_file->value.s); goto out;
@@ -4417,9 +4398,7 @@ int bdrv_img_create(const char *filename, const char *fmt,snprintf (buf,sizeof (buf),"%" PRId64, size);set_option_parameter (param,BLOCK_OPT_SIZE , buf); }else { -error_report ("Image creation needs a size parameter" );error_setg (errp,"Image creation needs a size parameter" ); - ret = -EINVAL ; goto out; } }
@@ -4429,21 +4408,14 @@ int bdrv_img_create(const char *filename, const char *fmt,puts ("" ); ret =bdrv_create (drv, filename, param); -if (ret <0 ) {if (ret == -ENOTSUP ) { -error_report ("Formatting or formatting option not supported for " -"file format '%s'" , fmt);error_setg (errp,"Formatting or formatting option not supported for " "file format '%s'" , fmt); }else if (ret == -EFBIG ) { -error_report ("The image size is too large for file format '%s'" , - fmt);error_setg (errp,"The image size is too large for file format '%s'" , fmt); }else { -error_report ("%s: error while creating %s: %s" , filename, fmt, -strerror (-ret));error_setg (errp,"%s: error while creating %s: %s" , filename, fmt,strerror (-ret)); }
@@ -4456,6 +4428,4 @@ out:if (bs) {bdrv_delete (bs); } - -return ret; }
block.h +3 -3
@@ -339,9 +339,9 @@ int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,int bdrv_load_vmstate (BlockDriverState *bs,uint8_t *buf,int64_t pos,int size); -int bdrv_img_create (const char *filename,const char *fmt, -const char *base_filename,const char *base_fmt, -char *options,uint64_t img_size,int flags,Error **errp); +void bdrv_img_create (const char *filename,const char *fmt, +const char *base_filename,const char *base_fmt, +char *options,uint64_t img_size,int flags,Error **errp);void bdrv_set_buffer_alignment (BlockDriverState *bs,int align);void *qemu_blockalign (BlockDriverState *bs,size_t size);
Signature · 1 line
1.7.12.315.g682ce8b
Quoted 4 lines · On Fri, 19 Oct 2012 11:27:59 -0300 Luiz Capitulino <[email protected]>…
By adding error propagation to
bdrv_img_create()we improve error reporting inqmp_transaction()and simplify qemu-img.c:img_create() a bit.Please, check individual patches for details.
Kevin, is this in your review queue?
Am 02.11.2012 14:25, schrieb Luiz Capitulino:
Quoted 9 lines
Quoted 4 lines · On Fri, 19 Oct 2012 11:27:59 -0300 Luiz Capitulino <[email protected]>…
By adding error propagation to
bdrv_img_create()we improve error reporting inqmp_transaction()and simplify qemu-img.c:img_create() a bit.Please, check individual patches for details.
Kevin, is this in your review queue?
Yes, it is. With KVM Forum and lots of other patch series, no promises though.
Kevin
Quoted 7 lines · On Fri, 02 Nov 2012 14:40:03 +0100 Kevin Wolf <[email protected]> wrote:
Am 02.11.2012 14:25, schrieb Luiz Capitulino:
Quoted 6 lines
Quoted 3 lines · On Fri, 19 Oct 2012 11:27:59 -0300 Luiz Capitulino <[email protected]>…
By adding error propagation to
bdrv_img_create()we improve error reporting inqmp_transaction()and simplify qemu-img.c:img_create() a bit.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Please, check individual patches for details.
Kevin, is this in your review queue?
Yes, it is. With KVM Forum and lots of other patch series, no promises though.
Sure, just wanted to know if you were aware about it.
Quoted 10 lines · On Fri, 2 Nov 2012 11:42:32 -0200 Luiz Capitulino <[email protected]>…
Quoted 7 lines · On Fri, 02 Nov 2012 14:40:03 +0100 Kevin Wolf <[email protected]> wrote:
Am 02.11.2012 14:25, schrieb Luiz Capitulino:
Quoted 6 lines
Quoted 3 lines · On Fri, 19 Oct 2012 11:27:59 -0300 Luiz Capitulino <[email protected]>…
By adding error propagation to
bdrv_img_create()we improve error reporting inqmp_transaction()and simplify qemu-img.c:img_create() a bit.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Please, check individual patches for details.
Kevin, is this in your review queue?
Yes, it is. With KVM Forum and lots of other patch series, no promises though.
Sure, just wanted to know if you were aware about it.
Still in your queue? :)
Am 29.11.2012 17:34, schrieb Luiz Capitulino:
Quoted 15 lines
Quoted 12 lines · On Fri, 2 Nov 2012 11:42:32 -0200 Luiz Capitulino <[email protected]>…
Quoted 9 lines · On Fri, 02 Nov 2012 14:40:03 +0100 Kevin Wolf <[email protected]> wrote:
Am 02.11.2012 14:25, schrieb Luiz Capitulino:
Quoted 8 lines
Quoted 4 lines · On Fri, 19 Oct 2012 11:27:59 -0300 Luiz Capitulino <[email protected]>…
By adding error propagation to
bdrv_img_create()we improve error reporting inqmp_transaction()and simplify qemu-img.c:img_create() a bit.Please, check individual patches for details.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Kevin, is this in your review queue?
Yes, it is. With KVM Forum and lots of other patch series, no promises though.
Sure, just wanted to know if you were aware about it.
Still in your queue? :)
Patches look good in principle, but the bdrv_img_create() calls in qmp_drive_mirror() aren't covered yet. Please do a quick respin for these.
Kevin
Quoted 21 lines · On Fri, 30 Nov 2012 12:39:34 +0100 Kevin Wolf <[email protected]> wrote:
Am 29.11.2012 17:34, schrieb Luiz Capitulino:
Quoted 20 lines
Quoted 17 lines · On Fri, 2 Nov 2012 11:42:32 -0200 Luiz Capitulino <[email protected]>…
Quoted 13 lines · On Fri, 02 Nov 2012 14:40:03 +0100 Kevin Wolf <[email protected]> wrote:
Am 02.11.2012 14:25, schrieb Luiz Capitulino:
Quoted 9 lines
Quoted 4 lines · On Fri, 19 Oct 2012 11:27:59 -0300 Luiz Capitulino <[email protected]>…
By adding error propagation to
bdrv_img_create()we improve error reporting inqmp_transaction()and simplify qemu-img.c:img_create() a bit.Please, check individual patches for details.
Kevin, is this in your review queue?
Yes, it is. With KVM Forum and lots of other patch series, no promises though.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Sure, just wanted to know if you were aware about it.
Still in your queue? :)
Patches look good in principle, but the
bdrv_img_create()calls inqmp_drive_mirror()aren't covered yet. Please do a quick respin for these.
Sure.