From: Wen Congyang Date: Wed, 15 Jul 2015 09:18:35 +0000 (+0800) Subject: libxc/migration: export read_record for common use X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~1434 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=d34ef46bc48d6173cb32f4278ea4d61ea02bb3ea;p=xen.git libxc/migration: export read_record for common use read_record() could be used by primary to read dirty bitmap record sent by secondary under COLO. When used by xc 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. CC: Andrew Cooper Signed-off-by: Yang Hongyang Signed-off-by: Wen Congyang Signed-off-by: Changlong Xie Acked-by: Ian Jackson --- diff --git a/tools/libxc/xc_sr_common.c b/tools/libxc/xc_sr_common.c index 3313a907cb..b228a1550e 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 e7568b59d3..c990664d7c 100644 --- a/tools/libxc/xc_sr_common.h +++ b/tools/libxc/xc_sr_common.h @@ -371,6 +371,20 @@ static inline int write_record(struct xc_sr_context *ctx, return write_split_record(ctx, rec, NULL, 0); } +/* + * 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 diff --git a/tools/libxc/xc_sr_restore.c b/tools/libxc/xc_sr_restore.c index 2b9a0ea830..3e4ca7ffdd 100644 --- a/tools/libxc/xc_sr_restore.c +++ b/tools/libxc/xc_sr_restore.c @@ -68,67 +68,6 @@ static int read_headers(struct xc_sr_context *ctx) return 0; } -/* - * 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? */ @@ -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 )