From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:42648) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tcu8z-0007ES-1p for qemu-devel@nongnu.org; Mon, 26 Nov 2012 03:34:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tcu8q-0003nb-0l for qemu-devel@nongnu.org; Mon, 26 Nov 2012 03:34:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:58500) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tcu8p-0003lP-JG for qemu-devel@nongnu.org; Mon, 26 Nov 2012 03:34:11 -0500 Message-ID: <50B32979.6010905@redhat.com> Date: Mon, 26 Nov 2012 09:34:01 +0100 From: Paolo Bonzini MIME-Version: 1.0 References: <1353749244-25676-1-git-send-email-xiawenc@linux.vnet.ibm.com> <1353749244-25676-7-git-send-email-xiawenc@linux.vnet.ibm.com> In-Reply-To: <1353749244-25676-7-git-send-email-xiawenc@linux.vnet.ibm.com> Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH V11 6/7] libqblock API implement List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Wenchao Xia Cc: kwolf@redhat.com, stefanha@gmail.com, aliguori@us.ibm.com, qemu-devel@nongnu.org, blauwirbel@gmail.com Il 24/11/2012 10:27, Wenchao Xia ha scritto: > This patch contains implemention for APIs. > Important APIs: > 1 QBlockContext. This structure was used to retrieve errors, every th= read > must create one first. > 2 QBlockImage. It stands for an block image object. > 3 QBlockStaticInfo. It contains static information such as location, = backing > file, size. > 4 Sync I/O. It is similar to C file open, read, write and close opera= tions. >=20 > v11: > Moved API design out of this patch. > Spell fix. > Use a new function in libqblock-aio.c to do bdrv init and aio init, r= emoved > this section from library loading call back function, which allows map = different > aio-context to differenct QBlockContext in the future. > Renamed QBlockState to QBlockImage. > Added reference counter in QBlockImage, which is only used in new/del= ete pair > function now. With a reference count, the API should be new/ref/unref, not new/delete. The "delete" function should be internal, just call it from unref when the reference count goes to zero. Some other comments below. > Removed useless parentheses around & argument. > Move virt_size out of format unions, removed virt_size from QBlockSta= ticInfoAddr. > bdrv_read and bdrv_write, Report I/O error when block api return nega= tive value. > qb_check_allocation, fixed the length check condition and added comme= nts for > it. > qb_info_image_static_get, renamed info to p_info and info_tmp to info= , also > renamed all double pointing parameters with prefix =E2=80=9Cp_=E2=80=9D= in all API. > qb_str2fmttype and qb_fmttype2str, added parameter check. > qb_setup_info_addr, moved memset into it. > qb_info_image_static_get, added format valid check, removed variable = member_addr, > moved memset to qb_setup_info_addr. >=20 > Signed-off-by: Wenchao Xia > --- > libqblock/libqblock-aio.c | 110 ++++- > libqblock/libqblock-error.c | 57 ++ > libqblock/libqblock.c | 1191 +++++++++++++++++++++++++++++++++++= +++++++- > 3 files changed, 1349 insertions(+), 9 deletions(-) >=20 > diff --git a/libqblock/libqblock-aio.c b/libqblock/libqblock-aio.c > index 605eee8..97d7ad9 100644 > --- a/libqblock/libqblock-aio.c > +++ b/libqblock/libqblock-aio.c > @@ -11,31 +11,63 @@ > * > */ > =20 > +/* This file was only used in libqblock, codes are copied from main-lo= op.c, > + iohandler.c, compatfd.c now, it may have different implemention in th= e future. > +*/ > + > #include > =20 > +#include "libqblock-aio.h" > + > #include "qemu-common.h" > #include "qemu-aio.h" > #include "main-loop.h" > #include "compatfd.h" > =20 > -void qemu_notify_event(void) > +#include "block.h" > + > +/* Aio support, copied from main-loop.c */ > + > +static AioContext *qemu_aio_context; > + > +/* This function should only be called once now. */ > +void libqblock_aio_init(void) > { > + GSource *src; > + > + qemu_aio_context =3D aio_context_new(); > + /* bdrv_init must be called after qemu_aio_context was set. */ > + bdrv_init(); > + > + src =3D aio_get_g_source(qemu_aio_context); > + g_source_attach(src, NULL); > + g_source_unref(src); There is no need (yet) to do these three steps. Once we add an asynchronous I/O API, we can add an API to get an AioContext from a QBlockContext. This however requires support for multiple AioContexts, I think. So that's left for later. > return; > } > =20 > +void qemu_notify_event(void) > +{ > + if (!qemu_aio_context) { > + return; > + } > + aio_notify(qemu_aio_context); > +} > + > +/* Functions to operate on the main QEMU AioContext. */ > + > QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) > { > - return NULL; > + return aio_bh_new(qemu_aio_context, cb, opaque); > } > =20 > void qemu_aio_flush(void) > { > - return; > + aio_flush(qemu_aio_context); > } > =20 > bool qemu_aio_wait(void) > { > - return false; > + return aio_poll(qemu_aio_context, true); > } > =20 > #ifdef CONFIG_POSIX > @@ -45,7 +77,8 @@ void qemu_aio_set_fd_handler(int fd, > AioFlushHandler *io_flush, > void *opaque) > { > - return; > + aio_set_fd_handler(qemu_aio_context, fd, io_read, io_write, io_flu= sh, > + opaque); > } > #endif > =20 > @@ -53,22 +86,83 @@ void qemu_aio_set_event_notifier(EventNotifier *not= ifier, > EventNotifierHandler *io_read, > AioFlushEventNotifierHandler *io_flus= h) > { > - return; > + aio_set_event_notifier(qemu_aio_context, notifier, io_read, io_flu= sh); > } > =20 > + > +/* Signal fd support, copied from compatfd.c */ > + > bool qemu_signalfd_available(void) > { > +#ifdef CONFIG_SIGNALFD > + sigset_t mask; > + int fd; > + bool ok; > + sigemptyset(&mask); > + errno =3D 0; > + fd =3D syscall(SYS_signalfd, -1, &mask, _NSIG / 8); > + ok =3D (errno !=3D ENOSYS); > + if (fd >=3D 0) { > + close(fd); > + } > + return ok; > +#else > return false; > +#endif > } Please just return false. > -typedef struct IOHandlerRecord IOHandlerRecord; > =20 > +/* Fd handler support, copied from iohandler.c. */ > + > +typedef struct IOHandlerRecord { > + IOCanReadHandler *fd_read_poll; > + IOHandler *fd_read; > + IOHandler *fd_write; > + void *opaque; > + QLIST_ENTRY(IOHandlerRecord) next; > + int fd; > + bool deleted; > +} IOHandlerRecord; > + > +static QLIST_HEAD(, IOHandlerRecord) io_handlers =3D > + QLIST_HEAD_INITIALIZER(io_handlers); > + > +/* XXX: fd_read_poll should be suppressed, but an API change is > + necessary in the character devices to suppress fd_can_read(). */ > int qemu_set_fd_handler2(int fd, > IOCanReadHandler *fd_read_poll, > IOHandler *fd_read, > IOHandler *fd_write, > void *opaque) > { > + IOHandlerRecord *ioh; > + > + assert(fd >=3D 0); > + > + if (!fd_read && !fd_write) { > + QLIST_FOREACH(ioh, &io_handlers, next) { > + if (ioh->fd =3D=3D fd) { > + ioh->deleted =3D 1; > + break; > + } > + } > + } else { > + QLIST_FOREACH(ioh, &io_handlers, next) { > + if (ioh->fd =3D=3D fd) { > + goto found; > + } > + } > + ioh =3D g_malloc0(sizeof(IOHandlerRecord)); > + QLIST_INSERT_HEAD(&io_handlers, ioh, next); > + found: > + ioh->fd =3D fd; > + ioh->fd_read_poll =3D fd_read_poll; > + ioh->fd_read =3D fd_read; > + ioh->fd_write =3D fd_write; > + ioh->opaque =3D opaque; > + ioh->deleted =3D 0; > + qemu_notify_event(); > + } > return 0; > } > =20 > @@ -77,5 +171,5 @@ int qemu_set_fd_handler(int fd, > IOHandler *fd_write, > void *opaque) > { > - return 0; > + return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); > } The stubs/ version is enough for qemu_set_fd_handler2. The only use of qemu_set_fd_handler is in event_notifier-posix.c, either change it to qemu_set_fd_handler2 or add another file to stubs/. > diff --git a/libqblock/libqblock-error.c b/libqblock/libqblock-error.c > index e69de29..2a59970 100644 > --- a/libqblock/libqblock-error.c > +++ b/libqblock/libqblock-error.c > @@ -0,0 +1,57 @@ > +/* > + * QEMU block layer library > + * > + * Copyright IBM, Corp. 2012 > + * > + * Authors: > + * Wenchao Xia > + * > + * This work is licensed under the terms of the GNU LGPL, version 2 or= later. > + * See the COPYING.LIB file in the top-level directory. > + * > + */ > + > +#include "libqblock-error.h" > +#include "libqblock-internal.h" > + > +void qb_error_get_human_str(QBlockContext *context, > + char *buf, size_t buf_size) > +{ > + const char *err_ret_str; > + switch (context->err_ret) { > + case QB_ERR_INTERNAL_ERR: > + err_ret_str =3D "Internal error."; > + break; > + case QB_ERR_INVALID_PARAM: > + err_ret_str =3D "Invalid param."; > + break; > + case QB_ERR_BLOCK_OUT_OF_RANGE: > + err_ret_str =3D "request is out of image's range."; > + break; > + default: > + err_ret_str =3D "Unknown error."; > + break; > + } > + if (context =3D=3D NULL) { > + snprintf(buf, buf_size, "%s", err_ret_str); > + return; > + } > + > + if (context->err_ret =3D=3D QB_ERR_INTERNAL_ERR) { > + snprintf(buf, buf_size, "%s %s errno [%d]. strerror [%s].", > + err_ret_str, context->g_error->message, > + context->err_no, strerror(-context->err_no)); > + } else { > + snprintf(buf, buf_size, "%s %s", > + err_ret_str, context->g_error->message); > + } > + return; > +} > + > +int qb_error_get_errno(QBlockContext *context) > +{ > + if (context->err_ret =3D=3D QB_ERR_INTERNAL_ERR) { > + return context->err_no; > + } > + return 0; > +} > diff --git a/libqblock/libqblock.c b/libqblock/libqblock.c > index 4c18c41..89e3c54 100644 > --- a/libqblock/libqblock.c > +++ b/libqblock/libqblock.c > @@ -1,6 +1,1195 @@ > +/* > + * QEMU block layer library > + * > + * Copyright IBM, Corp. 2012 > + * > + * Authors: > + * Wenchao Xia > + * > + * This work is licensed under the terms of the GNU LGPL, version 2 or= later. > + * See the COPYING.LIB file in the top-level directory. > + * > + */ > + > +#include > +#include > +#include > + > #include "libqblock.h" > +#include "libqblock-internal.h" > + > +#include "block_int.h" > +#include "qemu-aio.h" > +#include "libqblock-aio.h" > + > +#define LIBQB_FILENAME_MAX 4096 > + > +typedef struct LibqblockGlobalData { > + int init_flag; > + pthread_mutex_t *mutex; > +} LibqblockGlobalData; > + > +LibqblockGlobalData libqb_global_data; > + > +typedef struct LibqbFormatStrMapping { > + const char *fmt_str; > + QBlockFormat fmt_type; > +} LibqbFormatStrMapping; > + > +LibqbFormatStrMapping libqb_fmtstr_table[] =3D { > + {"cow", QB_FMT_COW}, > + {"qed", QB_FMT_QED}, > + {"qcow", QB_FMT_QCOW}, > + {"qcow2", QB_FMT_QCOW2}, > + {"raw", QB_FMT_RAW}, > + {"rbd", QB_FMT_RBD}, > + {"sheepdog", QB_FMT_SHEEPDOG}, > + {"vdi", QB_FMT_VDI}, > + {"vmdk", QB_FMT_VMDK}, > + {"vpc", QB_FMT_VPC}, > + {NULL, 0}, > +}; > + > +__attribute__((constructor)) > +static void libqblock_init(void) > +{ > + /* Todo: add an assertion about the ABI. */ > + int ret; > + if (libqb_global_data.init_flag =3D=3D 0) { > + libqb_global_data.mutex =3D g_malloc(sizeof(pthread_mutex_t)); > + ret =3D pthread_mutex_init(libqb_global_data.mutex, NULL); > + g_assert(ret =3D=3D 0); > + libqb_global_data.init_flag =3D 1; > + } > +} > + > +const char *qb_fmttype2str(QBlockFormat fmt_type) > +{ > + int i =3D 0; > + LibqbFormatStrMapping *tb =3D libqb_fmtstr_table; > =20 > -int libqb_test(void) > + if ((fmt_type <=3D QB_FMT_NONE) || (fmt_type >=3D QB_FMT_MAX)) { > + return NULL; > + } > + while (tb[i].fmt_str !=3D NULL) { > + if (tb[i].fmt_type =3D=3D fmt_type) { > + return tb[i].fmt_str; > + } > + i++; > + } > + return NULL; > +} > + > +QBlockFormat qb_str2fmttype(const char *fmt_str) > { > + int i =3D 0; > + LibqbFormatStrMapping *tb =3D libqb_fmtstr_table; > + > + if (fmt_str =3D=3D NULL) { > + return QB_FMT_NONE; > + } > + while (tb[i].fmt_str !=3D NULL) { > + if ((strcmp(fmt_str, tb[i].fmt_str) =3D=3D 0)) { > + return tb[i].fmt_type; > + } > + i++; > + } > + return QB_FMT_NONE; > +} > + > +static void set_context_err(QBlockContext *context, int err_ret, > + const char *fmt, ...) > +{ > + va_list ap; > + > + if (context->g_error !=3D NULL) { > + g_error_free(context->g_error); > + } > + > + va_start(ap, fmt); > + context->g_error =3D g_error_new_valist(G_LIBQBLOCK_ERROR, err_ret= , fmt, ap); > + va_end(ap); > + > + context->err_ret =3D err_ret; > + if (err_ret =3D=3D QB_ERR_INTERNAL_ERR) { > + context->err_no =3D -errno; > + } else { > + context->err_no =3D 0; > + } > +} > + > +int qb_context_new(QBlockContext **p_context) > +{ > + *p_context =3D g_malloc0_n(1, sizeof(QBlockContext)); > + > + /* Following will be replaced when multi-thread aio comes. Althoug= h this > +lib is not thread safe now, add a lock here ahead is not bad. */ > + pthread_mutex_t *mutex =3D libqb_global_data.mutex; > + pthread_mutex_lock(mutex); > + static int aio_init_flag; > + if (aio_init_flag =3D=3D 0) { > + libqblock_aio_init(); > + aio_init_flag =3D 1; > + } > + pthread_mutex_unlock(mutex); > return 0; > } > + > +void qb_context_delete(QBlockContext **p_context) > +{ > + if ((*p_context)->g_error !=3D NULL) { > + g_error_free((*p_context)->g_error); > + } > + QB_FREE(*p_context); > + return; > +} > + > +int qb_image_new(QBlockContext *context, > + QBlockImage **p_qbi) > +{ > + *p_qbi =3D g_malloc0_n(1, sizeof(QBlockImage)); > + /* Assume ref_count is already set to 0 in malloc. */ > + (*p_qbi)->ref_count++; > + (*p_qbi)->bdrvs =3D bdrv_new("hda"); > + if ((*p_qbi)->bdrvs =3D=3D NULL) { > + QB_FREE(*p_qbi); > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "failed to create the driver."); > + return context->err_ret; > + } > + return 0; > +} > + > +void qb_image_delete(QBlockContext *context, > + QBlockImage **p_qbi) > +{ > + (*p_qbi)->ref_count--; > + if ((*p_qbi)->ref_count > 0) { > + return; > + } > + if ((*p_qbi)->filename !=3D NULL) { > + qb_close(context, *p_qbi); > + } > + if ((*p_qbi)->bdrvs !=3D NULL) { > + bdrv_delete((*p_qbi)->bdrvs); > + (*p_qbi)->bdrvs =3D NULL; > + } > + QB_FREE(*p_qbi); > + return; > +} > + > +int qb_loc_info_new(QBlockContext *context, > + QBlockLocationInfo **p_loc) > +{ > + *p_loc =3D g_malloc0_n(1, sizeof(QBlockLocationInfo)); > + return 0; > +} > + > +void qb_loc_info_delete(QBlockContext *context, > + QBlockLocationInfo **p_loc) > +{ > + QB_FREE(*p_loc); > +} > + > +int qb_fmt_info_new(QBlockContext *context, > + QBlockFormatInfo **p_fmt) > +{ > + *p_fmt =3D g_malloc0_n(1, sizeof(QBlockFormatInfo)); > + return 0; > +} > + > +void qb_fmt_info_delete(QBlockContext *context, > + QBlockFormatInfo **p_fmt) > +{ > + QB_FREE(*p_fmt); > +} > + > +/* return 0 if every thing is fine */ > +static int loc_check_params(QBlockContext *context, > + QBlockLocationInfo *loc) > +{ > + context->err_ret =3D 0; > + > + switch (loc->prot_type) { > + case QB_PROTO_FILE: > + if (loc->o_file.filename =3D=3D NULL) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Filename was not set."); > + goto out; > + } > + if (path_has_protocol(loc->o_file.filename) > 0) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "filename [%s] had protocol.", > + loc->o_file.filename); > + goto out; > + } > + break; > + default: > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Protocol type [%d] was not valid.", > + loc->prot_type); > + break; > + } > + > + out: > + return context->err_ret; > +} > + > +/* translate loc structure to internal filename, returned char* need f= ree, > + * assuming filename is not NULL. *filename would be set to NULL if no= valid > + * filename found. *filename must be freed later. > + * return 0 if no error with *filename set. > + */ > +static int loc2filename(QBlockContext *context, > + QBlockLocationInfo *loc, > + char **p_filename) > +{ > + context->err_ret =3D 0; > + > + if (*p_filename !=3D NULL) { > + QB_FREE(*p_filename); > + } > + switch (loc->prot_type) { > + case QB_PROTO_FILE: > + *p_filename =3D g_strdup(loc->o_file.filename); > + break; > + default: > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "protocol type [%d] is not supported.", > + loc->prot_type); > + break; > + } > + > + return context->err_ret; > +} > + > +/* translate filename to location, loc->prot_type =3D NONE if fail, fi= lename > + must be valid. loc internal char pointer must be freed later. > + * return 0 if no error. > + */ > +static int filename2loc(QBlockContext *context, > + QBlockLocationInfo *loc, > + const char *filename) > +{ > + context->err_ret =3D 0; > + > + if (path_has_protocol(filename) > 0) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Filename [%s] had protocol, not supported now.", > + filename); > + goto out; > + } > + > + loc->prot_type =3D QB_PROTO_FILE; > + switch (loc->prot_type) { > + case QB_PROTO_FILE: > + loc->o_file.filename =3D g_strdup(filename); > + break; > + default: > + break; > + } > + > + out: > + return context->err_ret; > +} > + > +/* return 0 if OK, or qblock error number */ > +static int set_backing_file_options(QBlockContext *context, > + QEMUOptionParameter *param, > + QBlockLocationInfo *loc, > + QBlockFormat *fmt) > +{ > + char *backing_filename =3D NULL; > + const char *fmtstr_backing =3D NULL; > + int ret =3D 0; > + > + if (loc =3D=3D NULL) { > + goto out; > + } > + > + ret =3D loc2filename(context, loc, &backing_filename); > + /* ret can < 0 if loc have not been set, mean user did not specify= backing > + file, so need to check return value */ > + > + ret =3D 0; > + > + if (backing_filename) { > + ret =3D set_option_parameter(param, > + BLOCK_OPT_BACKING_FILE, backing_filename); > + assert(ret =3D=3D 0); > + if (fmt =3D=3D NULL) { > + goto out; > + } > + fmtstr_backing =3D qb_fmttype2str(*fmt); > + if (fmtstr_backing) { > + ret =3D set_option_parameter(param, > + BLOCK_OPT_BACKING_FMT, fmtstr_backing)= ; > + assert(ret =3D=3D 0); > + } > + } > + > + out: > + g_free(backing_filename); > + return ret; > +} > + > +int qb_create(QBlockContext *context, > + QBlockImage *qbi, > + QBlockLocationInfo *loc, > + QBlockFormatInfo *fmt, > + int flag) > +{ > + int ret =3D 0, bd_ret; > + char *filename =3D NULL; > + BlockDriverState *bs =3D NULL; > + BlockDriver *drv =3D NULL, *backing_drv =3D NULL; > + bool tmp_bool; > + > + const char *fmtstr =3D NULL, *tmp =3D NULL; > + QEMUOptionParameter *param =3D NULL, *create_options =3D NULL; > + QEMUOptionParameter *backing_fmt, *backing_file, *size; > + QBlockFormatOptionsCOW *o_cow =3D NULL; > + QBlockFormatOptionsQED *o_qed =3D NULL; > + QBlockFormatOptionsQCOW *o_qcow =3D NULL; > + QBlockFormatOptionsQCOW2 *o_qcow2 =3D NULL; > + QBlockFormatOptionsRAW *o_raw =3D NULL; > + QBlockFormatOptionsRBD *o_rbd =3D NULL; > + QBlockFormatOptionsSD *o_sd =3D NULL; > + QBlockFormatOptionsVDI *o_vdi =3D NULL; > + QBlockFormatOptionsVMDK *o_vmdk =3D NULL; > + QBlockFormatOptionsVPC *o_vpc =3D NULL; > + > + > + /* check parameters */ > + if (flag & (~LIBQBLOCK_O_VALID_MASK)) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "invalid flag was set."); > + ret =3D context->err_ret; > + goto out; > + } > + > + if ((loc =3D=3D NULL) || (qbi =3D=3D NULL) || (fmt =3D=3D NULL)) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Got unexpected NULL pointer in parameters."= ); > + ret =3D context->err_ret; > + goto out; > + } > + > + ret =3D loc_check_params(context, loc); > + if (ret !=3D 0) { > + goto out; > + } > + > + /* internal translate */ > + ret =3D loc2filename(context, loc, &filename); > + if (ret !=3D 0) { > + goto out; > + } > + > + fmtstr =3D qb_fmttype2str(fmt->fmt_type); > + if (fmtstr =3D=3D NULL) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Got unexpected NULL pointer in parameters."); > + ret =3D context->err_ret; > + goto out; > + } > + > + drv =3D bdrv_find_format(fmtstr); > + assert(drv !=3D NULL); > + > + create_options =3D append_option_parameters(create_options, > + drv->create_options); > + param =3D parse_option_parameters("", create_options, param); > + > + bd_ret =3D set_option_parameter_int(param, > + BLOCK_OPT_SIZE, fmt->virt_size); > + assert(bd_ret =3D=3D 0); > + > + switch (fmt->fmt_type) { > + case QB_FMT_COW: > + o_cow =3D &fmt->o_cow; > + /* do not need to check loc, it may be not set */ > + ret =3D set_backing_file_options(context, param, > + &o_cow->backing_loc, NULL); > + if (ret !=3D 0) { > + goto out; > + } > + break; > + case QB_FMT_QED: > + o_qed =3D &fmt->o_qed; > + ret =3D set_backing_file_options(context, param, > + &o_qed->backing_loc, &o_qed->backing_= fmt); > + if (ret !=3D 0) { > + goto out; > + } > + bd_ret =3D set_option_parameter_int(param, > + BLOCK_OPT_CLUSTER_SIZE, o_qed->cluster= _size); > + assert(bd_ret =3D=3D 0); > + bd_ret =3D set_option_parameter_int(param, > + BLOCK_OPT_TABLE_SIZE, o_qed->table_siz= e); > + assert(bd_ret =3D=3D 0); > + break; > + case QB_FMT_QCOW: > + o_qcow =3D &fmt->o_qcow; > + ret =3D set_backing_file_options(context, param, > + &o_qcow->backing_loc, NULL); > + if (ret !=3D 0) { > + goto out; > + } > + tmp =3D o_qcow->encrypt ? "on" : "off"; > + bd_ret =3D set_option_parameter(param, BLOCK_OPT_ENCRYPT, tmp)= ; > + assert(bd_ret =3D=3D 0); > + break; > + case QB_FMT_QCOW2: > + o_qcow2 =3D &fmt->o_qcow2; > + ret =3D set_backing_file_options(context, param, > + &o_qcow2->backing_loc, &o_qcow2->backing= _fmt); > + if (ret !=3D 0) { > + goto out; > + } > + tmp =3D o_qcow2->encrypt ? "on" : "off"; > + bd_ret =3D set_option_parameter(param, BLOCK_OPT_ENCRYPT, tmp)= ; > + assert(bd_ret =3D=3D 0); > + bd_ret =3D set_option_parameter_int(param, > + BLOCK_OPT_CLUSTER_SIZE, o_qcow2->cluster= _size); > + assert(bd_ret =3D=3D 0); > + > + if (o_qcow2->cpt_lv !=3D QB_FMT_QCOW2_COMPAT_DEFAULT) { > + tmp =3D o_qcow2->cpt_lv =3D=3D QB_FMT_QCOW2_COMPAT_V0_10 ?= "0.10" : "1.1"; > + bd_ret =3D set_option_parameter(param, > + BLOCK_OPT_COMPAT_LEVEL, tmp); > + assert(bd_ret =3D=3D 0); > + } > + > + if (o_qcow2->pre_mode !=3D QB_FMT_QCOW2_PREALLOC_DEFAULT) { > + tmp =3D o_qcow2->pre_mode =3D=3D QB_FMT_QCOW2_PREALLOC_OFF= ? > + "off" : "metadata"; > + bd_ret =3D set_option_parameter(param, > + BLOCK_OPT_PREALLOC, tmp); > + assert(bd_ret =3D=3D 0); > + } > + break; > + > + case QB_FMT_RAW: > + o_raw =3D &fmt->o_raw; > + /* do nothing now. */ > + break; > + case QB_FMT_RBD: > + o_rbd =3D &fmt->o_rbd; > + bd_ret =3D set_option_parameter_int(param, > + BLOCK_OPT_CLUSTER_SIZE, o_rbd->cluster_s= ize); > + assert(bd_ret =3D=3D 0); > + break; > + case QB_FMT_SHEEPDOG: > + o_sd =3D &fmt->o_sd; > + ret =3D set_backing_file_options(context, param, > + &o_sd->backing_loc, NULL); > + if (ret !=3D 0) { > + goto out; > + } > + if (o_sd->pre_mode !=3D QB_FMT_SD_PREALLOC_DEFAULT) { > + tmp =3D o_sd->pre_mode =3D=3D QB_FMT_SD_PREALLOC_OFF ? "of= f" : "full"; > + bd_ret =3D set_option_parameter(param, > + BLOCK_OPT_PREALLOC, tmp); > + assert(bd_ret =3D=3D 0); > + } > + break; > + case QB_FMT_VDI: > + o_vdi =3D &fmt->o_vdi; > + /* following option is not always valid depends on configurati= on */ > + set_option_parameter_int(param, > + BLOCK_OPT_CLUSTER_SIZE, o_vdi->cluster_s= ize); > + if (o_vdi->pre_mode !=3D QB_FMT_VDI_PREALLOC_DEFAULT) { > + tmp_bool =3D o_sd->pre_mode =3D=3D QB_FMT_VDI_PREALLOC_MET= ADATA ? > + true : false; > + set_option_parameter_int(param, "static", tmp_bool); > + } > + break; > + case QB_FMT_VMDK: > + o_vmdk =3D &fmt->o_vmdk; > + ret =3D set_backing_file_options(context, param, > + &o_vmdk->backing_loc, NULL); > + if (ret !=3D 0) { > + goto out; > + } > + > + if (o_vmdk->cpt_lv !=3D QB_FMT_VMDK_COMPAT_DEFAULT) { > + tmp_bool =3D o_vmdk->cpt_lv =3D=3D QB_FMT_VMDK_COMPAT_VMDK= V6_TRUE ? > + true : false; > + bd_ret =3D set_option_parameter_int(param, BLOCK_OPT_COMPA= T6, > + tmp_bool); > + assert(bd_ret =3D=3D 0); > + } > + if (o_vmdk->subfmt !=3D QB_FMT_VMDK_SUBFMT_DEFAULT) { > + switch (o_vmdk->subfmt) { > + case QB_FMT_VMDK_SUBFMT_MONOLITHIC_SPARSE: > + tmp =3D "monolithicSparse"; > + break; > + case QB_FMT_VMDK_SUBFMT_MONOLITHIC_FLAT: > + tmp =3D "monolithicFlat"; > + break; > + case QB_FMT_VMDK_SUBFMT_TWOGBMAX_EXTENT_SPARSE: > + tmp =3D "twoGbMaxExtentSparse"; > + break; > + case QB_FMT_VMDK_SUBFMT_TWOGBMAX_EXTENT_FLAT: > + tmp =3D "twoGbMaxExtentFlat"; > + break; > + case QB_FMT_VMDK_SUBFMT_STREAM_OPTIMIZED: > + tmp =3D "streamOptimized"; > + break; > + default: > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "invalid VMDK sumfmt type %d was set.", o_vmdk->su= bfmt); > + ret =3D context->err_ret; > + goto out; > + break; > + } > + bd_ret =3D set_option_parameter(param, > + BLOCK_OPT_SUBFMT, tmp); > + assert(bd_ret =3D=3D 0); > + } > + break; > + case QB_FMT_VPC: > + o_vpc =3D &fmt->o_vpc; > + if (o_vpc->subfmt !=3D QB_FMT_VPC_SUBFMT_DEFAULT) { > + tmp =3D o_vpc->subfmt =3D=3D QB_FMT_VPC_SUBFMT_DYNAMIC ? > + "dynamic" : "fixed"; > + bd_ret =3D set_option_parameter(param, > + BLOCK_OPT_SUBFMT, tmp); > + assert(bd_ret =3D=3D 0); > + } > + break; > + default: > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "invalid format type %d was set.", fmt->fmt_ty= pe); > + ret =3D context->err_ret; > + goto out; > + break; > + } > + > + backing_file =3D get_option_parameter(param, BLOCK_OPT_BACKING_FIL= E); > + if (backing_file && backing_file->value.s) { > + if (!strcmp(filename, backing_file->value.s)) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Backing file is the same with new file."); > + ret =3D context->err_ret; > + goto out; > + } > + } > + > + backing_fmt =3D get_option_parameter(param, BLOCK_OPT_BACKING_FMT)= ; > + if (backing_fmt && backing_fmt->value.s) { > + backing_drv =3D bdrv_find_format(backing_fmt->value.s); > + assert(backing_drv !=3D NULL); > + } > + > + size =3D get_option_parameter(param, BLOCK_OPT_SIZE); > + if (size && size->value.n <=3D 0) { > + if (backing_file && backing_file->value.s) { > + uint64_t size; > + char buf[32]; > + int back_flags; > + > + /* backing files always opened read-only */ > + back_flags =3D > + flag & > + ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); > + > + bs =3D bdrv_new(""); > + > + ret =3D bdrv_open(bs, backing_file->value.s, > + back_flags, backing_drv); > + if (ret < 0) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Failed to open the backing file."); > + ret =3D context->err_ret; > + goto out; > + } > + bdrv_get_geometry(bs, &size); > + size *=3D BDRV_SECTOR_SIZE; > + > + snprintf(buf, sizeof(buf), "%" PRId64, size); > + set_option_parameter(param, BLOCK_OPT_SIZE, buf); > + } else { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "Neither size or backing file was not set."= ); > + ret =3D context->err_ret; > + goto out; > + } > + } > + > + bd_ret =3D bdrv_create(drv, filename, param); > + > + > + if (bd_ret < 0) { > + const char *errstr; > + if (bd_ret =3D=3D -ENOTSUP) { > + errstr =3D "formatting option not supported."; > + } else if (bd_ret =3D=3D -EFBIG) { > + errstr =3D "The image size is too large."; > + } else { > + errstr =3D "Error in creating the image."; > + } > + set_context_err(context, QB_ERR_INTERNAL_ERR, errstr); > + ret =3D context->err_ret; > + } > + > +out: > + free_option_parameters(create_options); > + free_option_parameters(param); > + g_free(filename); > + if (bs) { > + bdrv_delete(bs); > + } > + > + return ret; > +} > + > +int qb_open(QBlockContext *context, > + QBlockImage *qbi, > + QBlockLocationInfo *loc, > + QBlockFormatInfo *fmt, > + int flag) > +{ > + int ret =3D 0, bd_ret; > + BlockDriverState *bs; > + BlockDriver *bd; > + const char *fmtstr; > + char *filename =3D NULL; > + > + /* take care of user settings */ > + /* do nothing now */ > + > + /* check parameters */ > + if (flag & (~LIBQBLOCK_O_VALID_MASK)) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Invalid flag was set."); > + ret =3D context->err_ret; > + goto out; > + } > + > + if ((loc =3D=3D NULL) || (qbi =3D=3D NULL)) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Got unexpected NULL pointer in parameters."); > + ret =3D context->err_ret; > + goto out; > + } > + > + ret =3D loc_check_params(context, loc); > + if (ret !=3D 0) { > + goto out; > + } > + > + /* internal translate */ > + ret =3D loc2filename(context, loc, &filename); > + if (ret !=3D 0) { > + goto out; > + } > + > + fmtstr =3D NULL; > + bd =3D NULL; > + if (fmt !=3D NULL) { > + fmtstr =3D qb_fmttype2str(fmt->fmt_type); > + } > + > + if (fmtstr !=3D NULL) { > + bd =3D bdrv_find_format(fmtstr); > + assert(bd !=3D NULL); > + } > + > + /* do real opening */ > + bs =3D qbi->bdrvs; > + bd_ret =3D bdrv_open(bs, filename, flag, bd); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "Failed in opening with driver, bd_ret is %d.", = bd_ret); > + ret =3D context->err_ret; > + goto out; > + } > + > + if (qbi->filename !=3D NULL) { > + g_free(qbi->filename); > + } > + qbi->filename =3D g_strdup(filename); > + > + out: > + g_free(filename); > + return ret; > +} > + > +void qb_close(QBlockContext *context, > + QBlockImage *qbi) > +{ > + BlockDriverState *bs; > + > + bs =3D qbi->bdrvs; > + > + if (qbi->filename !=3D NULL) { > + QB_FREE(qbi->filename); > + bdrv_close(bs); > + } > + return; > +} > + > +int32_t qb_read(QBlockContext *context, > + QBlockImage *qbi, > + uint8_t *buf, > + uint32_t len, > + uint64_t offset) > +{ > + int bd_ret; > + BlockDriverState *bs; > + uint8_t temp_buf[BDRV_SECTOR_SIZE], *p; > + uint64_t sector_start; > + int sector_num, byte_offset, cp_len; > + uint32_t remains; > + > + context->err_ret =3D 0; > + bs =3D qbi->bdrvs; > + > + if (len <=3D 0) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Param len is less or equal to zero."); > + return context->err_ret; > + } > + > + p =3D buf; > + remains =3D len; > + > + sector_start =3D offset >> BDRV_SECTOR_BITS; > + > + byte_offset =3D offset & (~BDRV_SECTOR_MASK); > + if (byte_offset !=3D 0) { > + /* the start sector is not alligned, need to read/write this s= ector. */ > + bd_ret =3D bdrv_read(bs, sector_start, temp_buf, 1); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "I/O errors."); > + context->err_no =3D bd_ret; > + return context->err_ret; > + } > + cp_len =3D BDRV_SECTOR_SIZE - byte_offset; > + memcpy(p, temp_buf + byte_offset, cp_len); > + > + remains -=3D cp_len; > + p +=3D cp_len; > + sector_start++; > + } > + > + /* now start position is alligned. */ > + if (remains >=3D BDRV_SECTOR_SIZE) { > + sector_num =3D remains >> BDRV_SECTOR_BITS; > + bd_ret =3D bdrv_read(bs, sector_start, p, sector_num); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "I/O errors."); > + context->err_no =3D bd_ret; > + return context->err_ret; > + } > + remains -=3D sector_num << BDRV_SECTOR_BITS; > + p +=3D sector_num << BDRV_SECTOR_BITS; > + sector_start +=3D sector_num; > + } > + > + if (remains > 0) { > + /* there is some request remains, less than 1 sector */ > + bd_ret =3D bdrv_read(bs, sector_start, temp_buf, 1); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "I/O errors."); > + context->err_no =3D bd_ret; > + return context->err_ret; > + } > + memcpy(p, temp_buf, remains); > + remains -=3D remains; > + } > + > + return len-remains; > +} > + > +int32_t qb_write(QBlockContext *context, > + QBlockImage *qbi, > + const uint8_t *buf, > + uint32_t len, > + uint64_t offset) > +{ > + int bd_ret; > + BlockDriverState *bs; > + uint8_t temp_buf[BDRV_SECTOR_SIZE]; > + const uint8_t *p; > + uint64_t sector_start; > + int sector_num, byte_offset, cp_len; > + uint32_t remains; > + > + context->err_ret =3D 0; > + bs =3D qbi->bdrvs; > + > + if (len <=3D 0) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Param len is less or equal to zero."); > + return context->err_ret; > + } > + > + p =3D buf; > + remains =3D len; > + > + sector_start =3D offset >> BDRV_SECTOR_BITS; > + > + byte_offset =3D offset & (~BDRV_SECTOR_MASK); > + if (byte_offset !=3D 0) { > + /* the start sector is not alligned, need to read/write this s= ector. */ > + bd_ret =3D bdrv_read(bs, sector_start, temp_buf, 1); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "I/O errors."); > + context->err_no =3D bd_ret; > + return context->err_ret; > + } > + cp_len =3D BDRV_SECTOR_SIZE - byte_offset; > + memcpy(temp_buf + byte_offset, p, cp_len); > + bd_ret =3D bdrv_write(bs, sector_start, temp_buf, 1); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "I/O errors."); > + context->err_no =3D bd_ret; > + return context->err_ret; > + } > + remains -=3D cp_len; > + p +=3D cp_len; > + sector_start++; > + } > + > + /* now start position is alligned. */ > + if (remains >=3D BDRV_SECTOR_SIZE) { > + sector_num =3D remains >> BDRV_SECTOR_BITS; > + bd_ret =3D bdrv_write(bs, sector_start, p, sector_num); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "I/O errors."); > + context->err_no =3D bd_ret; > + return context->err_ret; > + } > + remains -=3D sector_num << BDRV_SECTOR_BITS; > + p +=3D sector_num << BDRV_SECTOR_BITS; > + sector_start +=3D sector_num; > + } > + > + if (remains > 0) { > + /* there is some request remains, less than 1 sector */ > + bd_ret =3D bdrv_read(bs, sector_start, temp_buf, 1); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "I/O errors."); > + context->err_no =3D bd_ret; > + return context->err_ret; > + } > + memcpy(temp_buf, p, remains); > + bd_ret =3D bdrv_write(bs, sector_start, temp_buf, 1); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "I/O errors."); > + context->err_no =3D bd_ret; > + return context->err_ret; > + } > + remains -=3D remains; > + } > + > + return len-remains; > +} > + > +int qb_flush(QBlockContext *context, > + QBlockImage *qbi) > +{ > + int bd_ret; > + BlockDriverState *bs; > + > + context->err_ret =3D 0; > + bs =3D qbi->bdrvs; > + bd_ret =3D bdrv_flush(bs); > + if (bd_ret < 0) { > + set_context_err(context, QB_ERR_INTERNAL_ERR, > + "Internal error."); > + } > + return context->err_ret; > +} > + > +int qb_check_allocation(QBlockContext *context, > + QBlockImage *qbi, > + uint64_t start, > + int64_t length, > + int *pstatus, > + int64_t *plength) > +{ > + int ret; > + int sector_start, sector_num, num; > + BlockDriverState *bs; > + unsigned int real_len, ret_len; > + > + context->err_ret =3D 0; > + bs =3D qbi->bdrvs; > + > + /* Now bdrv_is_allocated take nb_sectors as int with unit sector, = but this > + API take length as int64_t with unit bytes, so a check is needed t= o ensure > + no silent error will happen in translating bytes to sector later i= f length > + is too big. > + max_sectors is set to INT_MAX - 1 in case of that real_len is b= igger > + than length. > + Gcc issue? a =3D (0x7ffffffe << 9) was compiled to 0xffffffffff= fffc00, it > + should be 0xfffffffc00, so use variable instead of direct macro > + calculation. */ > + int64_t max_sectors =3D INT_MAX - 1; > + int64_t max_bytes =3D max_sectors << BDRV_SECTOR_BITS; > + > + if (length > max_bytes) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Length is too big."); > + goto out; > + } > + > + if (length <=3D 0) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Length is not valid."); > + goto out; > + } > + > + if (qbi->filename =3D=3D NULL) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Image was not opened first."); > + goto out; > + } > + > + /* translate to sector */ > + sector_start =3D start >> BDRV_SECTOR_BITS; > + real_len =3D (start & (~BDRV_SECTOR_MASK)) + length; > + sector_num =3D real_len >> BDRV_SECTOR_BITS; > + if ((real_len & (~BDRV_SECTOR_MASK)) !=3D 0) { > + sector_num++; > + } > + > + ret =3D bdrv_is_allocated(bs, sector_start, sector_num, &num); > + if ((ret =3D=3D 0) && (num =3D=3D 0)) { > + set_context_err(context, QB_ERR_BLOCK_OUT_OF_RANGE, > + "Start position was bigger than the image's siz= e."); > + goto out; > + } > + > + *pstatus =3D ret; > + ret_len =3D (num << BDRV_SECTOR_BITS) - (start & (~BDRV_SECTOR_MAS= K)); > + if (ret_len > length) { > + ret_len =3D length; > + } > + *plength =3D ret_len; > + > + out: > + return context->err_ret; > +} > + > +static void qb_setup_info_addr(const QBlockStaticInfo *info, > + QBlockStaticInfoAddr *info_addr) > +{ > + const QBlockLocationInfo *backing_loc =3D NULL; > + const bool *encrypt =3D NULL; > + const QBlockFormatInfo *fmt =3D &info->fmt; > + > + memset(info_addr, 0, sizeof(QBlockStaticInfoAddr)); > + > + switch (fmt->fmt_type) { > + case QB_FMT_COW: > + backing_loc =3D &fmt->o_cow.backing_loc; > + break; > + case QB_FMT_QED: > + backing_loc =3D &fmt->o_qed.backing_loc; > + break; > + case QB_FMT_QCOW: > + backing_loc =3D &fmt->o_qcow.backing_loc; > + encrypt =3D &fmt->o_qcow.encrypt; > + break; > + case QB_FMT_QCOW2: > + backing_loc =3D &fmt->o_qcow2.backing_loc; > + encrypt =3D &fmt->o_qcow2.encrypt; > + break; > + case QB_FMT_RAW: > + break; > + case QB_FMT_RBD: > + break; > + case QB_FMT_SHEEPDOG: > + backing_loc =3D &fmt->o_sd.backing_loc; > + break; > + case QB_FMT_VDI: > + break; > + case QB_FMT_VMDK: > + backing_loc =3D &fmt->o_vmdk.backing_loc; > + break; > + case QB_FMT_VPC: > + break; > + default: > + break; > + } > + > + info_addr->backing_loc =3D (QBlockLocationInfo *)backing_loc; > + info_addr->encrypt =3D (bool *)encrypt; > + return; > +} > + > +const uint64_t *qb_get_virt_size(const QBlockStaticInfo *info) > +{ > + return &info->fmt.virt_size; > +} > + > +const QBlockLocationInfo *qb_get_backing_loc(const QBlockStaticInfo *i= nfo) > +{ > + QBlockStaticInfoAddr addr; > + qb_setup_info_addr(info, &addr); > + return addr.backing_loc; > +} > + > +const bool *qb_get_encrypt(const QBlockStaticInfo *info) > +{ > + QBlockStaticInfoAddr addr; > + qb_setup_info_addr(info, &addr); > + return addr.encrypt; > +} > + > +int qb_info_image_static_get(QBlockContext *context, > + QBlockImage *qbi, > + QBlockStaticInfo **p_info) > +{ > + int ret =3D 0; > + BlockDriverState *bs; > + QBlockStaticInfo *info; > + QBlockStaticInfoAddr addr; > + const char *fmt_str; > + uint64_t total_sectors; > + char backing_filename[LIBQB_FILENAME_MAX]; > + > + if (qbi->filename =3D=3D NULL) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Block Image was not opened."); > + ret =3D context->err_ret; > + goto out; > + } > + > + info =3D g_malloc0_n(1, sizeof(QBlockStaticInfo)); > + > + bs =3D qbi->bdrvs; > + > + ret =3D filename2loc(context, > + &info->loc, > + qbi->filename); > + if (ret < 0) { > + goto free; > + } > + > + fmt_str =3D bdrv_get_format_name(bs); > + info->fmt.fmt_type =3D qb_str2fmttype(fmt_str); > + if (info->fmt.fmt_type =3D=3D QB_FMT_NONE) { > + set_context_err(context, QB_ERR_INVALID_PARAM, > + "Image format %s not valid.", fmt_str); > + ret =3D context->err_ret; > + goto free; > + } > + > + /* we got the format type and basic location info now, setup the s= truct > + pointer to the internal members */ > + qb_setup_info_addr(info, &addr); > + > + bdrv_get_geometry(bs, &total_sectors); > + info->fmt.virt_size =3D total_sectors * BDRV_SECTOR_SIZE; > + > + if (addr.encrypt !=3D NULL) { > + *(addr.encrypt) =3D bdrv_is_encrypted(bs); > + } > + > + bdrv_get_full_backing_filename(bs, backing_filename, > + sizeof(backing_filename)); > + if (backing_filename[0] !=3D '\0') { > + assert(addr.backing_loc !=3D NULL); > + ret =3D filename2loc(context, > + addr.backing_loc, > + backing_filename); > + if (ret < 0) { > + goto free; > + } > + } > + > + info->sector_size =3D BDRV_SECTOR_SIZE; > + *p_info =3D info; > + > + out: > + return ret; > + free: > + qb_info_image_static_delete(context, &info); > + return ret; > +} > + > +/* free locations if it has string allocated on heap. */ > +static void loc_free(QBlockLocationInfo *loc) > +{ > + switch (loc->prot_type) { > + case QB_PROTO_FILE: > + g_free((void *)(loc->o_file.filename)); > + loc->o_file.filename =3D NULL; > + break; > + default: > + break; > + } > +} > + > +/* free fmt related resoure. */ > +static void fmt_free(QBlockFormatInfo *fmt) > +{ > + switch (fmt->fmt_type) { > + case QB_FMT_COW: > + loc_free(&fmt->o_cow.backing_loc); > + break; > + case QB_FMT_QED: > + loc_free(&fmt->o_qed.backing_loc); > + break; > + case QB_FMT_QCOW: > + loc_free(&fmt->o_qcow.backing_loc); > + break; > + case QB_FMT_QCOW2: > + loc_free(&fmt->o_qcow2.backing_loc); > + break; > + case QB_FMT_RAW: > + break; > + case QB_FMT_RBD: > + break; > + case QB_FMT_SHEEPDOG: > + loc_free(&fmt->o_sd.backing_loc); > + break; > + case QB_FMT_VDI: > + break; > + case QB_FMT_VMDK: > + loc_free(&fmt->o_vmdk.backing_loc); > + break; > + case QB_FMT_VPC: > + break; > + default: > + break; > + } > + return; > +} > + > +void qb_info_image_static_delete(QBlockContext *context, > + QBlockStaticInfo **p_info) > +{ > + loc_free(&(*p_info)->loc); > + fmt_free(&(*p_info)->fmt); > + QB_FREE(*p_info); > +} > + > +QBlockLocationInfo *qb_loc_info_dup(const QBlockLocationInfo *prot) > +{ > + QBlockLocationInfo *p =3D g_malloc0_n(1, sizeof(QBlockLocationInfo= )); > + p->prot_type =3D prot->prot_type; > + switch (p->prot_type) { > + case QB_PROTO_FILE: > + p->o_file.filename =3D > + g_strdup(prot->o_file.filename); > + break; > + default: > + break; > + } > + return p; > +} >=20