From mboxrd@z Thu Jan 1 00:00:00 1970 From: Wen Congyang Subject: [PATCH v10 09/31] libxc/migration: export read_record for common use Date: Mon, 22 Feb 2016 10:52:13 +0800 Message-ID: <1456109555-28299-10-git-send-email-wency@cn.fujitsu.com> References: <1456109555-28299-1-git-send-email-wency@cn.fujitsu.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1456109555-28299-1-git-send-email-wency@cn.fujitsu.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen devel , Konrad Rzeszutek Wilk , Andrew Cooper , Ian Campbell , Ian Jackson , Wei Liu Cc: Lars Kurth , Changlong Xie , Wen Congyang , Gui Jianfeng , Jiang Yunhong , Dong Eddie , Shriram Rajagopalan , Yang Hongyang List-Id: xen-devel@lists.xenproject.org read_record() could be used by primary to read dirty bitmap record sent by secondary under COLO. When used by save side, we need to pass the backchannel fd instead of ctx->fd to read_record(), so we added a fd param to it. No functional changes. Signed-off-by: Yang Hongyang CC: Andrew Cooper Signed-off-by: Wen Congyang --- tools/libxc/xc_sr_common.c | 49 +++++++++++++++++++++++++++++++++++ tools/libxc/xc_sr_common.h | 14 ++++++++++ tools/libxc/xc_sr_restore.c | 63 +-------------------------------------------- 3 files changed, 64 insertions(+), 62 deletions(-) diff --git a/tools/libxc/xc_sr_common.c b/tools/libxc/xc_sr_common.c index 3313a90..b228a15 100644 --- a/tools/libxc/xc_sr_common.c +++ b/tools/libxc/xc_sr_common.c @@ -89,6 +89,55 @@ int write_split_record(struct xc_sr_context *ctx, struct xc_sr_record *rec, return -1; } +int read_record(struct xc_sr_context *ctx, int fd, struct xc_sr_record *rec) +{ + xc_interface *xch = ctx->xch; + struct xc_sr_rhdr rhdr; + size_t datasz; + + if ( read_exact(fd, &rhdr, sizeof(rhdr)) ) + { + PERROR("Failed to read Record Header from stream"); + return -1; + } + else if ( rhdr.length > REC_LENGTH_MAX ) + { + ERROR("Record (0x%08x, %s) length %#x exceeds max (%#x)", rhdr.type, + rec_type_to_str(rhdr.type), rhdr.length, REC_LENGTH_MAX); + return -1; + } + + datasz = ROUNDUP(rhdr.length, REC_ALIGN_ORDER); + + if ( datasz ) + { + rec->data = malloc(datasz); + + if ( !rec->data ) + { + ERROR("Unable to allocate %zu bytes for record data (0x%08x, %s)", + datasz, rhdr.type, rec_type_to_str(rhdr.type)); + return -1; + } + + if ( read_exact(fd, rec->data, datasz) ) + { + free(rec->data); + rec->data = NULL; + PERROR("Failed to read %zu bytes of data for record (0x%08x, %s)", + datasz, rhdr.type, rec_type_to_str(rhdr.type)); + return -1; + } + } + else + rec->data = NULL; + + rec->type = rhdr.type; + rec->length = rhdr.length; + + return 0; +}; + static void __attribute__((unused)) build_assertions(void) { XC_BUILD_BUG_ON(sizeof(struct xc_sr_ihdr) != 24); diff --git a/tools/libxc/xc_sr_common.h b/tools/libxc/xc_sr_common.h index 66f595f..5d9f497 100644 --- a/tools/libxc/xc_sr_common.h +++ b/tools/libxc/xc_sr_common.h @@ -382,6 +382,20 @@ static inline int write_record(struct xc_sr_context *ctx, } /* + * Reads a record from the stream, and fills in the record structure. + * + * Returns 0 on success and non-0 on failure. + * + * On success, the records type and size shall be valid. + * - If size is 0, data shall be NULL. + * - If size is non-0, data shall be a buffer allocated by malloc() which must + * be passed to free() by the caller. + * + * On failure, the contents of the record structure are undefined. + */ +int read_record(struct xc_sr_context *ctx, int fd, struct xc_sr_record *rec); + +/* * This would ideally be private in restore.c, but is needed by * x86_pv_localise_page() if we receive pagetables frames ahead of the * contents of the frames they point at. diff --git a/tools/libxc/xc_sr_restore.c b/tools/libxc/xc_sr_restore.c index e24560e..aef9bca 100644 --- a/tools/libxc/xc_sr_restore.c +++ b/tools/libxc/xc_sr_restore.c @@ -69,67 +69,6 @@ static int read_headers(struct xc_sr_context *ctx) } /* - * Reads a record from the stream, and fills in the record structure. - * - * Returns 0 on success and non-0 on failure. - * - * On success, the records type and size shall be valid. - * - If size is 0, data shall be NULL. - * - If size is non-0, data shall be a buffer allocated by malloc() which must - * be passed to free() by the caller. - * - * On failure, the contents of the record structure are undefined. - */ -static int read_record(struct xc_sr_context *ctx, struct xc_sr_record *rec) -{ - xc_interface *xch = ctx->xch; - struct xc_sr_rhdr rhdr; - size_t datasz; - - if ( read_exact(ctx->fd, &rhdr, sizeof(rhdr)) ) - { - PERROR("Failed to read Record Header from stream"); - return -1; - } - else if ( rhdr.length > REC_LENGTH_MAX ) - { - ERROR("Record (0x%08x, %s) length %#x exceeds max (%#x)", rhdr.type, - rec_type_to_str(rhdr.type), rhdr.length, REC_LENGTH_MAX); - return -1; - } - - datasz = ROUNDUP(rhdr.length, REC_ALIGN_ORDER); - - if ( datasz ) - { - rec->data = malloc(datasz); - - if ( !rec->data ) - { - ERROR("Unable to allocate %zu bytes for record data (0x%08x, %s)", - datasz, rhdr.type, rec_type_to_str(rhdr.type)); - return -1; - } - - if ( read_exact(ctx->fd, rec->data, datasz) ) - { - free(rec->data); - rec->data = NULL; - PERROR("Failed to read %zu bytes of data for record (0x%08x, %s)", - datasz, rhdr.type, rec_type_to_str(rhdr.type)); - return -1; - } - } - else - rec->data = NULL; - - rec->type = rhdr.type; - rec->length = rhdr.length; - - return 0; -}; - -/* * Is a pfn populated? */ static bool pfn_is_populated(const struct xc_sr_context *ctx, xen_pfn_t pfn) @@ -650,7 +589,7 @@ static int restore(struct xc_sr_context *ctx) do { - rc = read_record(ctx, &rec); + rc = read_record(ctx, ctx->fd, &rec); if ( rc ) { if ( ctx->restore.buffer_all_records ) -- 2.5.0