X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=block-qcow.c;h=1fabc494dd016ebe07d05e87d1ff1769f0080ad2;hb=bcebc4a2e92679c5d10f7b5d20269de2ef3d782c;hp=9ad7061d3c354c785eaace7b55f099ea3994095e;hpb=e7b81015e5b8d03c13670940a80c9be0eeb816f6;p=qemu diff --git a/block-qcow.c b/block-qcow.c index 9ad7061..1fabc49 100644 --- a/block-qcow.c +++ b/block-qcow.c @@ -1,8 +1,8 @@ /* * Block driver for the QCOW format - * + * * Copyright (c) 2004-2006 Fabrice Bellard - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#include "vl.h" +#include "qemu-common.h" #include "block_int.h" #include #include "aes.h" @@ -80,10 +80,10 @@ static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset); static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) { const QCowHeader *cow_header = (const void *)buf; - + if (buf_size >= sizeof(QCowHeader) && be32_to_cpu(cow_header->magic) == QCOW_MAGIC && - be32_to_cpu(cow_header->version) == QCOW_VERSION) + be32_to_cpu(cow_header->version) == QCOW_VERSION) return 100; else return 0; @@ -108,7 +108,7 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags) be64_to_cpus(&header.size); be32_to_cpus(&header.crypt_method); be64_to_cpus(&header.l1_table_offset); - + if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) goto fail; if (header.size <= 1 || header.cluster_bits < 9) @@ -134,7 +134,7 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags) s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t)); if (!s->l1_table) goto fail; - if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != + 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; for(i = 0;i < s->l1_size; i++) { @@ -151,7 +151,7 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags) if (!s->cluster_data) goto fail; s->cluster_cache_offset = -1; - + /* read the backing file name */ if (header.backing_file_offset != 0) { len = header.backing_file_size; @@ -177,7 +177,7 @@ static int qcow_set_key(BlockDriverState *bs, const char *key) BDRVQcowState *s = bs->opaque; uint8_t keybuf[16]; int len, i; - + memset(keybuf, 0, 16); len = strlen(key); if (len > 16) @@ -231,7 +231,7 @@ static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num, for(i = 0; i < nb_sectors; i++) { ivec.ll[0] = cpu_to_le64(sector_num); ivec.ll[1] = 0; - AES_cbc_encrypt(in_buf, out_buf, 512, key, + AES_cbc_encrypt(in_buf, out_buf, 512, key, ivec.b, enc); sector_num++; in_buf += 512; @@ -248,7 +248,7 @@ static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num, * * 2 to allocate a compressed cluster of size * 'compressed_size'. 'compressed_size' must be > 0 and < - * cluster_size + * cluster_size * * return 0 if not allocated. */ @@ -262,7 +262,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, uint64_t l2_offset, *l2_table, cluster_offset, tmp; uint32_t min_count; int new_l2_table; - + l1_index = offset >> (s->l2_bits + s->cluster_bits); l2_offset = s->l1_table[l1_index]; new_l2_table = 0; @@ -276,7 +276,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, /* update the L1 entry */ s->l1_table[l1_index] = l2_offset; tmp = cpu_to_be64(l2_offset); - if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp), + if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp)) return 0; new_l2_table = 1; @@ -309,7 +309,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, s->l2_size * sizeof(uint64_t)) return 0; } else { - if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != + if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != s->l2_size * sizeof(uint64_t)) return 0; } @@ -318,7 +318,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, found: l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); cluster_offset = be64_to_cpu(l2_table[l2_index]); - if (!cluster_offset || + if (!cluster_offset || ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) { if (!allocate) return 0; @@ -331,54 +331,54 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, if (decompress_cluster(s, cluster_offset) < 0) return 0; cluster_offset = bdrv_getlength(s->hd); - cluster_offset = (cluster_offset + s->cluster_size - 1) & + cluster_offset = (cluster_offset + s->cluster_size - 1) & ~(s->cluster_size - 1); /* write the cluster content */ - if (bdrv_pwrite(s->hd, cluster_offset, s->cluster_cache, s->cluster_size) != + if (bdrv_pwrite(s->hd, cluster_offset, s->cluster_cache, s->cluster_size) != s->cluster_size) return -1; } else { cluster_offset = bdrv_getlength(s->hd); if (allocate == 1) { /* round to cluster size */ - cluster_offset = (cluster_offset + s->cluster_size - 1) & + cluster_offset = (cluster_offset + s->cluster_size - 1) & ~(s->cluster_size - 1); bdrv_truncate(s->hd, cluster_offset + s->cluster_size); /* if encrypted, we must initialize the cluster content which won't be written */ - if (s->crypt_method && + if (s->crypt_method && (n_end - n_start) < s->cluster_sectors) { uint64_t start_sect; start_sect = (offset & ~(s->cluster_size - 1)) >> 9; - memset(s->cluster_data + 512, 0xaa, 512); + memset(s->cluster_data + 512, 0x00, 512); for(i = 0; i < s->cluster_sectors; i++) { if (i < n_start || i >= n_end) { - encrypt_sectors(s, start_sect + i, - s->cluster_data, + encrypt_sectors(s, start_sect + i, + s->cluster_data, s->cluster_data + 512, 1, 1, &s->aes_encrypt_key); - if (bdrv_pwrite(s->hd, cluster_offset + i * 512, + if (bdrv_pwrite(s->hd, cluster_offset + i * 512, s->cluster_data, 512) != 512) return -1; } } } - } else { - cluster_offset |= QCOW_OFLAG_COMPRESSED | + } else if (allocate == 2) { + cluster_offset |= QCOW_OFLAG_COMPRESSED | (uint64_t)compressed_size << (63 - s->cluster_bits); } } /* update L2 table */ tmp = cpu_to_be64(cluster_offset); l2_table[l2_index] = tmp; - if (bdrv_pwrite(s->hd, + if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp)) return 0; } return cluster_offset; } -static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, +static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum) { BDRVQcowState *s = bs->opaque; @@ -420,7 +420,7 @@ static int decompress_buffer(uint8_t *out_buf, int out_buf_size, inflateEnd(strm); return 0; } - + static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) { int ret, csize; @@ -431,7 +431,7 @@ static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) csize = cluster_offset >> (63 - s->cluster_bits); csize &= (s->cluster_size - 1); ret = bdrv_pread(s->hd, coffset, s->cluster_data, csize); - if (ret != csize) + if (ret != csize) return -1; if (decompress_buffer(s->cluster_cache, s->cluster_size, s->cluster_data, csize) < 0) { @@ -444,13 +444,13 @@ static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) #if 0 -static int qcow_read(BlockDriverState *bs, int64_t sector_num, +static int qcow_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors) { BDRVQcowState *s = bs->opaque; int ret, index_in_cluster, n; uint64_t cluster_offset; - + while (nb_sectors > 0) { cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); index_in_cluster = sector_num & (s->cluster_sectors - 1); @@ -472,10 +472,10 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num, memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n); } else { ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); - if (ret != n * 512) + if (ret != n * 512) return -1; if (s->crypt_method) { - encrypt_sectors(s, sector_num, buf, buf, n, 0, + encrypt_sectors(s, sector_num, buf, buf, n, 0, &s->aes_decrypt_key); } } @@ -487,32 +487,32 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num, } #endif -static int qcow_write(BlockDriverState *bs, int64_t sector_num, +static int qcow_write(BlockDriverState *bs, int64_t sector_num, const uint8_t *buf, int nb_sectors) { BDRVQcowState *s = bs->opaque; int ret, index_in_cluster, n; uint64_t cluster_offset; - + while (nb_sectors > 0) { index_in_cluster = sector_num & (s->cluster_sectors - 1); n = s->cluster_sectors - index_in_cluster; if (n > nb_sectors) n = nb_sectors; - cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0, - index_in_cluster, + cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0, + index_in_cluster, index_in_cluster + n); if (!cluster_offset) return -1; if (s->crypt_method) { encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1, &s->aes_encrypt_key); - ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, + ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, s->cluster_data, n * 512); } else { ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); } - if (ret != n * 512) + if (ret != n * 512) return -1; nb_sectors -= n; sector_num += n; @@ -522,231 +522,207 @@ static int qcow_write(BlockDriverState *bs, int64_t sector_num, return 0; } -typedef struct { +typedef struct QCowAIOCB { + BlockDriverAIOCB common; int64_t sector_num; uint8_t *buf; int nb_sectors; int n; uint64_t cluster_offset; - uint8_t *cluster_data; + uint8_t *cluster_data; BlockDriverAIOCB *hd_aiocb; - BlockDriverAIOCB *backing_hd_aiocb; } QCowAIOCB; -static void qcow_aio_delete(BlockDriverAIOCB *acb); - -static int qcow_aio_new(BlockDriverAIOCB *acb) -{ - BlockDriverState *bs = acb->bs; - BDRVQcowState *s = bs->opaque; - QCowAIOCB *acb1; - acb1 = qemu_mallocz(sizeof(QCowAIOCB)); - if (!acb1) - return -1; - acb->opaque = acb1; - acb1->hd_aiocb = bdrv_aio_new(s->hd); - if (!acb1->hd_aiocb) - goto fail; - if (bs->backing_hd) { - acb1->backing_hd_aiocb = bdrv_aio_new(bs->backing_hd); - if (!acb1->backing_hd_aiocb) - goto fail; - } - return 0; - fail: - qcow_aio_delete(acb); - return -1; -} - static void qcow_aio_read_cb(void *opaque, int ret) { - BlockDriverAIOCB *acb = opaque; - BlockDriverState *bs = acb->bs; + QCowAIOCB *acb = opaque; + BlockDriverState *bs = acb->common.bs; BDRVQcowState *s = bs->opaque; - QCowAIOCB *acb1 = acb->opaque; int index_in_cluster; + acb->hd_aiocb = NULL; if (ret < 0) { fail: - acb->cb(acb->cb_opaque, ret); + acb->common.cb(acb->common.opaque, ret); + qemu_aio_release(acb); return; } redo: /* post process the read buffer */ - if (!acb1->cluster_offset) { + if (!acb->cluster_offset) { /* nothing to do */ - } else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) { + } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { /* nothing to do */ } else { if (s->crypt_method) { - encrypt_sectors(s, acb1->sector_num, acb1->buf, acb1->buf, - acb1->n, 0, + encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, + acb->n, 0, &s->aes_decrypt_key); } } - acb1->nb_sectors -= acb1->n; - acb1->sector_num += acb1->n; - acb1->buf += acb1->n * 512; + acb->nb_sectors -= acb->n; + acb->sector_num += acb->n; + acb->buf += acb->n * 512; - if (acb1->nb_sectors == 0) { + if (acb->nb_sectors == 0) { /* request completed */ - acb->cb(acb->cb_opaque, 0); + acb->common.cb(acb->common.opaque, 0); + qemu_aio_release(acb); return; } - + /* prepare next AIO request */ - acb1->cluster_offset = get_cluster_offset(bs, - acb1->sector_num << 9, - 0, 0, 0, 0); - index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1); - acb1->n = s->cluster_sectors - index_in_cluster; - if (acb1->n > acb1->nb_sectors) - acb1->n = acb1->nb_sectors; - - if (!acb1->cluster_offset) { + acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, + 0, 0, 0, 0); + index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); + acb->n = s->cluster_sectors - index_in_cluster; + if (acb->n > acb->nb_sectors) + acb->n = acb->nb_sectors; + + if (!acb->cluster_offset) { if (bs->backing_hd) { /* read from the base image */ - ret = bdrv_aio_read(acb1->backing_hd_aiocb, acb1->sector_num, - acb1->buf, acb1->n, qcow_aio_read_cb, acb); - if (ret < 0) + acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, + acb->sector_num, acb->buf, acb->n, qcow_aio_read_cb, acb); + if (acb->hd_aiocb == NULL) goto fail; } else { /* Note: in this case, no need to wait */ - memset(acb1->buf, 0, 512 * acb1->n); + memset(acb->buf, 0, 512 * acb->n); goto redo; } - } else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) { + } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { /* add AIO support for compressed blocks ? */ - if (decompress_cluster(s, acb1->cluster_offset) < 0) + if (decompress_cluster(s, acb->cluster_offset) < 0) goto fail; - memcpy(acb1->buf, - s->cluster_cache + index_in_cluster * 512, 512 * acb1->n); + memcpy(acb->buf, + s->cluster_cache + index_in_cluster * 512, 512 * acb->n); goto redo; } else { - if ((acb1->cluster_offset & 511) != 0) { + if ((acb->cluster_offset & 511) != 0) { ret = -EIO; goto fail; } - ret = bdrv_aio_read(acb1->hd_aiocb, - (acb1->cluster_offset >> 9) + index_in_cluster, - acb1->buf, acb1->n, qcow_aio_read_cb, acb); - if (ret < 0) + acb->hd_aiocb = bdrv_aio_read(s->hd, + (acb->cluster_offset >> 9) + index_in_cluster, + acb->buf, acb->n, qcow_aio_read_cb, acb); + if (acb->hd_aiocb == NULL) goto fail; } } -static int qcow_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, - uint8_t *buf, int nb_sectors) +static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs, + int64_t sector_num, uint8_t *buf, int nb_sectors, + BlockDriverCompletionFunc *cb, void *opaque) { - QCowAIOCB *acb1 = acb->opaque; - - acb1->sector_num = sector_num; - acb1->buf = buf; - acb1->nb_sectors = nb_sectors; - acb1->n = 0; - acb1->cluster_offset = 0; + QCowAIOCB *acb; + + acb = qemu_aio_get(bs, cb, opaque); + if (!acb) + return NULL; + acb->hd_aiocb = NULL; + acb->sector_num = sector_num; + acb->buf = buf; + acb->nb_sectors = nb_sectors; + acb->n = 0; + acb->cluster_offset = 0; qcow_aio_read_cb(acb, 0); - return 0; + return &acb->common; } static void qcow_aio_write_cb(void *opaque, int ret) { - BlockDriverAIOCB *acb = opaque; - BlockDriverState *bs = acb->bs; + QCowAIOCB *acb = opaque; + BlockDriverState *bs = acb->common.bs; BDRVQcowState *s = bs->opaque; - QCowAIOCB *acb1 = acb->opaque; int index_in_cluster; uint64_t cluster_offset; const uint8_t *src_buf; - + + acb->hd_aiocb = NULL; + if (ret < 0) { fail: - acb->cb(acb->cb_opaque, ret); + acb->common.cb(acb->common.opaque, ret); + qemu_aio_release(acb); return; } - acb1->nb_sectors -= acb1->n; - acb1->sector_num += acb1->n; - acb1->buf += acb1->n * 512; + acb->nb_sectors -= acb->n; + acb->sector_num += acb->n; + acb->buf += acb->n * 512; - if (acb1->nb_sectors == 0) { + if (acb->nb_sectors == 0) { /* request completed */ - acb->cb(acb->cb_opaque, 0); + acb->common.cb(acb->common.opaque, 0); + qemu_aio_release(acb); return; } - - index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1); - acb1->n = s->cluster_sectors - index_in_cluster; - if (acb1->n > acb1->nb_sectors) - acb1->n = acb1->nb_sectors; - cluster_offset = get_cluster_offset(bs, acb1->sector_num << 9, 1, 0, - index_in_cluster, - index_in_cluster + acb1->n); + + index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); + acb->n = s->cluster_sectors - index_in_cluster; + if (acb->n > acb->nb_sectors) + acb->n = acb->nb_sectors; + cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, 1, 0, + index_in_cluster, + index_in_cluster + acb->n); if (!cluster_offset || (cluster_offset & 511) != 0) { ret = -EIO; goto fail; } if (s->crypt_method) { - if (!acb1->cluster_data) { - acb1->cluster_data = qemu_mallocz(s->cluster_size); - if (!acb1->cluster_data) { + if (!acb->cluster_data) { + acb->cluster_data = qemu_mallocz(s->cluster_size); + if (!acb->cluster_data) { ret = -ENOMEM; goto fail; } } - encrypt_sectors(s, acb1->sector_num, acb1->cluster_data, acb1->buf, - acb1->n, 1, &s->aes_encrypt_key); - src_buf = acb1->cluster_data; + encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, + acb->n, 1, &s->aes_encrypt_key); + src_buf = acb->cluster_data; } else { - src_buf = acb1->buf; + src_buf = acb->buf; } - ret = bdrv_aio_write(acb1->hd_aiocb, - (cluster_offset >> 9) + index_in_cluster, - src_buf, acb1->n, - qcow_aio_write_cb, acb); - if (ret < 0) + acb->hd_aiocb = bdrv_aio_write(s->hd, + (cluster_offset >> 9) + index_in_cluster, + src_buf, acb->n, + qcow_aio_write_cb, acb); + if (acb->hd_aiocb == NULL) goto fail; } -static int qcow_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, - const uint8_t *buf, int nb_sectors) +static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs, + int64_t sector_num, const uint8_t *buf, int nb_sectors, + BlockDriverCompletionFunc *cb, void *opaque) { - QCowAIOCB *acb1 = acb->opaque; - BlockDriverState *bs = acb->bs; BDRVQcowState *s = bs->opaque; - + QCowAIOCB *acb; + s->cluster_cache_offset = -1; /* disable compressed cache */ - acb1->sector_num = sector_num; - acb1->buf = (uint8_t *)buf; - acb1->nb_sectors = nb_sectors; - acb1->n = 0; - - qcow_aio_write_cb(acb, 0); - return 0; -} + acb = qemu_aio_get(bs, cb, opaque); + if (!acb) + return NULL; + acb->hd_aiocb = NULL; + acb->sector_num = sector_num; + acb->buf = (uint8_t *)buf; + acb->nb_sectors = nb_sectors; + acb->n = 0; -static void qcow_aio_cancel(BlockDriverAIOCB *acb) -{ - QCowAIOCB *acb1 = acb->opaque; - if (acb1->hd_aiocb) - bdrv_aio_cancel(acb1->hd_aiocb); - if (acb1->backing_hd_aiocb) - bdrv_aio_cancel(acb1->backing_hd_aiocb); + qcow_aio_write_cb(acb, 0); + return &acb->common; } -static void qcow_aio_delete(BlockDriverAIOCB *acb) +static void qcow_aio_cancel(BlockDriverAIOCB *blockacb) { - QCowAIOCB *acb1 = acb->opaque; - if (acb1->hd_aiocb) - bdrv_aio_delete(acb1->hd_aiocb); - if (acb1->backing_hd_aiocb) - bdrv_aio_delete(acb1->backing_hd_aiocb); - qemu_free(acb1->cluster_data); - qemu_free(acb1); + QCowAIOCB *acb = (QCowAIOCB *)blockacb; + if (acb->hd_aiocb) + bdrv_aio_cancel(acb->hd_aiocb); + qemu_aio_release(acb); } static void qcow_close(BlockDriverState *bs) @@ -776,11 +752,15 @@ static int qcow_create(const char *filename, int64_t total_size, header_size = sizeof(header); backing_filename_len = 0; if (backing_file) { - header.backing_file_offset = cpu_to_be64(header_size); - backing_filename_len = strlen(backing_file); - header.backing_file_size = cpu_to_be32(backing_filename_len); - header_size += backing_filename_len; - header.mtime = cpu_to_be32(0); + if (strcmp(backing_file, "fat:")) { + header.backing_file_offset = cpu_to_be64(header_size); + backing_filename_len = strlen(backing_file); + header.backing_file_size = cpu_to_be32(backing_filename_len); + header_size += backing_filename_len; + } else { + /* special backing file for vvfat */ + backing_file = NULL; + } header.cluster_bits = 9; /* 512 byte cluster to avoid copying unmodifyed sectors */ header.l2_bits = 12; /* 32 KB L2 tables */ @@ -793,12 +773,12 @@ static int qcow_create(const char *filename, int64_t total_size, l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift; header.l1_table_offset = cpu_to_be64(header_size); - if (flags) { + if (flags & BLOCK_FLAG_ENCRYPT) { header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); } else { header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); } - + /* write all the data */ write(fd, &header, sizeof(header)); if (backing_file) { @@ -813,7 +793,7 @@ static int qcow_create(const char *filename, int64_t total_size, return 0; } -int qcow_make_empty(BlockDriverState *bs) +static int qcow_make_empty(BlockDriverState *bs) { BDRVQcowState *s = bs->opaque; uint32_t l1_length = s->l1_size * sizeof(uint64_t); @@ -833,18 +813,10 @@ int qcow_make_empty(BlockDriverState *bs) return 0; } -int qcow_get_cluster_size(BlockDriverState *bs) -{ - BDRVQcowState *s = bs->opaque; - if (bs->drv != &bdrv_qcow) - return -1; - return s->cluster_size; -} - /* XXX: put compressed sectors first, then all the cluster aligned tables to avoid losing bytes in alignment */ -int qcow_compress_cluster(BlockDriverState *bs, int64_t sector_num, - const uint8_t *buf) +static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, + const uint8_t *buf, int nb_sectors) { BDRVQcowState *s = bs->opaque; z_stream strm; @@ -852,8 +824,8 @@ int qcow_compress_cluster(BlockDriverState *bs, int64_t sector_num, uint8_t *out_buf; uint64_t cluster_offset; - if (bs->drv != &bdrv_qcow) - return -1; + if (nb_sectors != s->cluster_sectors) + return -EINVAL; out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); if (!out_buf) @@ -862,7 +834,7 @@ int qcow_compress_cluster(BlockDriverState *bs, int64_t sector_num, /* best compression, small window, no zlib header */ memset(&strm, 0, sizeof(strm)); ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, - Z_DEFLATED, -12, + Z_DEFLATED, -12, 9, Z_DEFAULT_STRATEGY); if (ret != 0) { qemu_free(out_buf); @@ -888,7 +860,7 @@ int qcow_compress_cluster(BlockDriverState *bs, int64_t sector_num, /* could not compress: write normal cluster */ qcow_write(bs, sector_num, buf, s->cluster_sectors); } else { - cluster_offset = get_cluster_offset(bs, sector_num << 9, 2, + cluster_offset = get_cluster_offset(bs, sector_num << 9, 2, out_len, 0, 0); cluster_offset &= s->cluster_offset_mask; if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) { @@ -896,7 +868,7 @@ int qcow_compress_cluster(BlockDriverState *bs, int64_t sector_num, return -1; } } - + qemu_free(out_buf); return 0; } @@ -907,25 +879,28 @@ static void qcow_flush(BlockDriverState *bs) bdrv_flush(s->hd); } +static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) +{ + BDRVQcowState *s = bs->opaque; + bdi->cluster_size = s->cluster_size; + return 0; +} + BlockDriver bdrv_qcow = { - "qcow", - sizeof(BDRVQcowState), - qcow_probe, - qcow_open, - NULL, - NULL, - qcow_close, - qcow_create, - qcow_flush, - qcow_is_allocated, - qcow_set_key, - qcow_make_empty, - - .bdrv_aio_new = qcow_aio_new, - .bdrv_aio_read = qcow_aio_read, - .bdrv_aio_write = qcow_aio_write, - .bdrv_aio_cancel = qcow_aio_cancel, - .bdrv_aio_delete = qcow_aio_delete, + .format_name = "qcow", + .instance_size = sizeof(BDRVQcowState), + .bdrv_probe = qcow_probe, + .bdrv_open = qcow_open, + .bdrv_close = qcow_close, + .bdrv_create = qcow_create, + .bdrv_flush = qcow_flush, + .bdrv_is_allocated = qcow_is_allocated, + .bdrv_set_key = qcow_set_key, + .bdrv_make_empty = qcow_make_empty, + .bdrv_aio_read = qcow_aio_read, + .bdrv_aio_write = qcow_aio_write, + .bdrv_aio_cancel = qcow_aio_cancel, + .aiocb_size = sizeof(QCowAIOCB), + .bdrv_write_compressed = qcow_write_compressed, + .bdrv_get_info = qcow_get_info, }; - -