From c090ccaec38ec15df3af8ecc867eea6ed4975dcd Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 28 Mar 2008 14:47:12 +0000 Subject: [PATCH] Fix L1 table endianess of qcow images created by tapdisk The qemu/ioemu implementation of the qcow format uses a big endian L1 table. tapdisk omits the necessary conversion, so qcow images have the wrong endianess and cannot be read by correct implementations of qcow. This patch detects broken tapdisk images and converts their L1 tables to big endian when the image file is opened in ioemu for the first time. The fixed image has a new flag EXTHDR_L1_BIG_ENDIAN set in the extended header. Note that a converted image cannot be opened by tapdisk again. Signed-off-by: Kevin Wolf Patch cross-ported to qemu xen from hg commit c02deeae743221cbe9a1ac9769d21c292e267601 Signed-off-by: Ian Jackson --- block-qcow.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/block-qcow.c b/block-qcow.c index 4798ade0..4b66d51c 100644 --- a/block-qcow.c +++ b/block-qcow.c @@ -37,6 +37,11 @@ #define QCOW_OFLAG_COMPRESSED (1LL << 63) +#define XEN_MAGIC (('X' << 24) | ('E' << 16) | ('N' << 8) | 0xfb) + +#define EXTHDR_SPARSE_FILE 0x01 +#define EXTHDR_L1_BIG_ENDIAN 0x02 + typedef struct QCowHeader { uint32_t magic; uint32_t version; @@ -50,6 +55,14 @@ typedef struct QCowHeader { uint64_t l1_table_offset; } QCowHeader; +/*Extended header for Xen enhancements*/ +typedef struct QCowHeader_ext { + uint32_t xmagic; + uint32_t cksum; + uint32_t min_cluster_alloc; + uint32_t flags; +} QCowHeader_ext; + #define L2_CACHE_SIZE 16 typedef struct BDRVQcowState { @@ -137,6 +150,51 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags) if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != s->l1_size * sizeof(uint64_t)) goto fail; + + /* Try to detect old tapdisk images. They have to be fixed because they + * don't use big endian but native endianess for the L1 table */ + if (header.backing_file_offset == 0 && s->l1_table_offset % 4096 == 0) { + + QCowHeader_ext exthdr; + uint64_t l1_bytes = s->l1_size * sizeof(uint64_t); + + if (bdrv_pread(s->hd, sizeof(header), &exthdr, sizeof(exthdr)) + != sizeof(exthdr)) + goto end_xenhdr; + + be32_to_cpus(&exthdr.xmagic); + if (exthdr.xmagic != XEN_MAGIC) + goto end_xenhdr; + + be32_to_cpus(&exthdr.flags); + if (exthdr.flags & EXTHDR_L1_BIG_ENDIAN) + goto end_xenhdr; + + /* The image is broken. Fix it. */ + fprintf(stderr, "qcow: Converting image to big endian L1 table\n"); + + for(i = 0;i < s->l1_size; i++) { + cpu_to_be64s(&s->l1_table[i]); + } + + if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, + l1_bytes) != l1_bytes) { + fprintf(stderr, "qcow: Failed to write new L1 table\n"); + goto fail; + } + + exthdr.flags |= EXTHDR_L1_BIG_ENDIAN; + cpu_to_be32s(&exthdr.flags); + + if (bdrv_pwrite(s->hd, sizeof(header), &exthdr, sizeof(exthdr)) + != sizeof(exthdr)) { + fprintf(stderr, "qcow: Failed to write extended header\n"); + goto fail; + } + } +end_xenhdr: + + /* L1 table is big endian now */ for(i = 0;i < s->l1_size; i++) { be64_to_cpus(&s->l1_table[i]); } -- 2.39.5