📄 block-qcow2.c.svn-base
字号:
acb->common.cb(acb->common.opaque, ret); qemu_aio_release(acb); return; } redo: /* post process the read buffer */ if (!acb->cluster_offset) { /* nothing to do */ } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { /* nothing to do */ } else { if (s->crypt_method) { encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, acb->n, 0, &s->aes_decrypt_key); } } acb->nb_sectors -= acb->n; acb->sector_num += acb->n; acb->buf += acb->n * 512; if (acb->nb_sectors == 0) { /* request completed */ acb->common.cb(acb->common.opaque, 0); qemu_aio_release(acb); return; } /* prepare next AIO request */ 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 */ n1 = backing_read1(bs->backing_hd, acb->sector_num, acb->buf, acb->n); if (n1 > 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 { goto redo; } } else { /* Note: in this case, no need to wait */ memset(acb->buf, 0, 512 * acb->n); goto redo; } } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { /* add AIO support for compressed blocks ? */ if (decompress_cluster(s, acb->cluster_offset) < 0) goto fail; memcpy(acb->buf, s->cluster_cache + index_in_cluster * 512, 512 * acb->n); goto redo; } else { if ((acb->cluster_offset & 511) != 0) { ret = -EIO; goto fail; } 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 QCowAIOCB *qcow_aio_setup(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque){ 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; return acb;}static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque){ QCowAIOCB *acb; acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); if (!acb) return NULL; qcow_aio_read_cb(acb, 0); return &acb->common;}static void qcow_aio_write_cb(void *opaque, int ret){ QCowAIOCB *acb = opaque; BlockDriverState *bs = acb->common.bs; BDRVQcowState *s = bs->opaque; int index_in_cluster; uint64_t cluster_offset; const uint8_t *src_buf; acb->hd_aiocb = NULL; if (ret < 0) { fail: acb->common.cb(acb->common.opaque, ret); qemu_aio_release(acb); return; } acb->nb_sectors -= acb->n; acb->sector_num += acb->n; acb->buf += acb->n * 512; if (acb->nb_sectors == 0) { /* request completed */ acb->common.cb(acb->common.opaque, 0); qemu_aio_release(acb); return; } 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 (!acb->cluster_data) { acb->cluster_data = qemu_mallocz(s->cluster_size); if (!acb->cluster_data) { ret = -ENOMEM; goto fail; } } 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 = acb->buf; } 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 BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs, int64_t sector_num, const uint8_t *buf, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque){ BDRVQcowState *s = bs->opaque; QCowAIOCB *acb; s->cluster_cache_offset = -1; /* disable compressed cache */ acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque); if (!acb) return NULL; qcow_aio_write_cb(acb, 0); return &acb->common;}static void qcow_aio_cancel(BlockDriverAIOCB *blockacb){ QCowAIOCB *acb = (QCowAIOCB *)blockacb; if (acb->hd_aiocb) bdrv_aio_cancel(acb->hd_aiocb); qemu_aio_release(acb);}static void qcow_close(BlockDriverState *bs){ BDRVQcowState *s = bs->opaque; qemu_free(s->l1_table); qemu_free(s->l2_cache); qemu_free(s->cluster_cache); qemu_free(s->cluster_data); refcount_close(bs); bdrv_delete(s->hd);}/* XXX: use std qcow open function ? */typedef struct QCowCreateState { int cluster_size; int cluster_bits; uint16_t *refcount_block; uint64_t *refcount_table; int64_t l1_table_offset; int64_t refcount_table_offset; int64_t refcount_block_offset;} QCowCreateState;static void create_refcount_update(QCowCreateState *s, int64_t offset, int64_t size){ int refcount; int64_t start, last, cluster_offset; uint16_t *p; start = offset & ~(s->cluster_size - 1); last = (offset + size - 1) & ~(s->cluster_size - 1); for(cluster_offset = start; cluster_offset <= last; cluster_offset += s->cluster_size) { p = &s->refcount_block[cluster_offset >> s->cluster_bits]; refcount = be16_to_cpu(*p); refcount++; *p = cpu_to_be16(refcount); }}static int qcow_create(const char *filename, int64_t total_size, const char *backing_file, int flags){ int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits; QCowHeader header; uint64_t tmp, offset; QCowCreateState s1, *s = &s1; memset(s, 0, sizeof(*s)); fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); if (fd < 0) return -1; memset(&header, 0, sizeof(header)); header.magic = cpu_to_be32(QCOW_MAGIC); header.version = cpu_to_be32(QCOW_VERSION); header.size = cpu_to_be64(total_size * 512); 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; } s->cluster_bits = 12; /* 4 KB clusters */ s->cluster_size = 1 << s->cluster_bits; header.cluster_bits = cpu_to_be32(s->cluster_bits); header_size = (header_size + 7) & ~7; if (flags & BLOCK_FLAG_ENCRYPT) { header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); } else { header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); } l2_bits = s->cluster_bits - 3; shift = s->cluster_bits + l2_bits; l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift); offset = align_offset(header_size, s->cluster_size); s->l1_table_offset = offset; header.l1_table_offset = cpu_to_be64(s->l1_table_offset); header.l1_size = cpu_to_be32(l1_size); offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size); s->refcount_table = qemu_mallocz(s->cluster_size); if (!s->refcount_table) goto fail; s->refcount_block = qemu_mallocz(s->cluster_size); if (!s->refcount_block) goto fail; s->refcount_table_offset = offset; header.refcount_table_offset = cpu_to_be64(offset); header.refcount_table_clusters = cpu_to_be32(1); offset += s->cluster_size; s->refcount_table[0] = cpu_to_be64(offset); s->refcount_block_offset = offset; offset += s->cluster_size; /* update refcounts */ create_refcount_update(s, 0, header_size); create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t)); create_refcount_update(s, s->refcount_table_offset, s->cluster_size); create_refcount_update(s, s->refcount_block_offset, s->cluster_size); /* write all the data */ write(fd, &header, sizeof(header)); if (backing_file) { write(fd, backing_file, backing_filename_len); } lseek(fd, s->l1_table_offset, SEEK_SET); tmp = 0; for(i = 0;i < l1_size; i++) { write(fd, &tmp, sizeof(tmp)); } lseek(fd, s->refcount_table_offset, SEEK_SET); write(fd, s->refcount_table, s->cluster_size); lseek(fd, s->refcount_block_offset, SEEK_SET); write(fd, s->refcount_block, s->cluster_size); qemu_free(s->refcount_table); qemu_free(s->refcount_block); close(fd); return 0; fail: qemu_free(s->refcount_table); qemu_free(s->refcount_block); close(fd); return -ENOMEM;}static int qcow_make_empty(BlockDriverState *bs){#if 0 /* XXX: not correct */ BDRVQcowState *s = bs->opaque; uint32_t l1_length = s->l1_size * sizeof(uint64_t); int ret; memset(s->l1_table, 0, l1_length); if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0) return -1; ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length); if (ret < 0) return ret; l2_cache_reset(bs);#endif return 0;}/* XXX: put compressed sectors first, then all the cluster aligned tables to avoid losing bytes in alignment */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; int ret, out_len; uint8_t *out_buf; uint64_t cluster_offset; if (nb_sectors == 0) { /* align end of file to a sector boundary to ease reading with sector based I/Os */ cluster_offset = bdrv_getlength(s->hd); cluster_offset = (cluster_offset + 511) & ~511; bdrv_truncate(s->hd, cluster_offset); return 0; } if (nb_sectors != s->cluster_sectors) return -EINVAL; out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); if (!out_buf) return -ENOMEM; /* best compression, small window, no zlib header */ memset(&strm, 0, sizeof(strm)); ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -12, 9, Z_DEFAULT_STRATEGY); if (ret != 0) { qemu_free(out_buf); return -1; } strm.avail_in = s->cluster_size; strm.next_in = (uint8_t *)buf; strm.avail_out = s->cluster_size; strm.next_out = out_buf; ret = deflate(&strm, Z_FINISH); if (ret != Z_STREAM_END && ret != Z_OK) { qemu_free(out_buf); deflateEnd(&strm); return -1; } out_len = strm.next_out - out_buf; deflateEnd(&strm); if (ret != Z_STREAM_END || out_len >= s->cluster_size) { /* 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, out_len, 0, 0); cluster_offset &= s->cluster_offset_mask; if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -