diff options
Diffstat (limited to 'block/vmdk.c')
-rw-r--r-- | block/vmdk.c | 296 |
1 files changed, 149 insertions, 147 deletions
diff --git a/block/vmdk.c b/block/vmdk.c index f21f02b..2d4ba42 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -76,7 +76,6 @@ typedef struct BDRVVmdkState { unsigned int cluster_sectors; uint32_t parent_cid; - int is_parent; } BDRVVmdkState; typedef struct VmdkMetaData { @@ -87,14 +86,6 @@ typedef struct VmdkMetaData { int valid; } VmdkMetaData; -typedef struct ActiveBDRVState{ - BlockDriverState *hd; // active image handler - uint64_t cluster_offset; // current write offset -}ActiveBDRVState; - -static ActiveBDRVState activeBDRV; - - static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) { uint32_t magic; @@ -117,14 +108,13 @@ static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) { - BDRVVmdkState *s = bs->opaque; char desc[DESC_SIZE]; uint32_t cid; const char *p_name, *cid_str; size_t cid_str_size; /* the descriptor offset = 0x200 */ - if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) + if (bdrv_pread(bs->file, 0x200, desc, DESC_SIZE) != DESC_SIZE) return 0; if (parent) { @@ -145,12 +135,11 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid) { - BDRVVmdkState *s = bs->opaque; char desc[DESC_SIZE], tmp_desc[DESC_SIZE]; char *p_name, *tmp_str; /* the descriptor offset = 0x200 */ - if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) + if (bdrv_pread(bs->file, 0x200, desc, DESC_SIZE) != DESC_SIZE) return -1; tmp_str = strstr(desc,"parentCID"); @@ -161,7 +150,7 @@ static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid) pstrcat(desc, sizeof(desc), tmp_desc); } - if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) + if (bdrv_pwrite_sync(bs->file, 0x200, desc, DESC_SIZE) < 0) return -1; return 0; } @@ -170,7 +159,7 @@ static int vmdk_is_cid_valid(BlockDriverState *bs) { #ifdef CHECK_CID BDRVVmdkState *s = bs->opaque; - BlockDriverState *p_bs = s->hd->backing_hd; + BlockDriverState *p_bs = bs->backing_hd; uint32_t cur_pcid; if (p_bs) { @@ -187,6 +176,7 @@ static int vmdk_is_cid_valid(BlockDriverState *bs) static int vmdk_snapshot_create(const char *filename, const char *backing_file) { int snp_fd, p_fd; + int ret; uint32_t p_cid; char *p_name, *gd_buf, *rgd_buf; const char *real_filename, *temp_str; @@ -211,34 +201,49 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file) snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644); if (snp_fd < 0) - return -1; + return -errno; p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE); if (p_fd < 0) { close(snp_fd); - return -1; + return -errno; } /* read the header */ - if (lseek(p_fd, 0x0, SEEK_SET) == -1) + if (lseek(p_fd, 0x0, SEEK_SET) == -1) { + ret = -errno; goto fail; - if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) + } + if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) { + ret = -errno; goto fail; + } /* write the header */ - if (lseek(snp_fd, 0x0, SEEK_SET) == -1) + if (lseek(snp_fd, 0x0, SEEK_SET) == -1) { + ret = -errno; goto fail; - if (write(snp_fd, hdr, HEADER_SIZE) == -1) + } + if (write(snp_fd, hdr, HEADER_SIZE) == -1) { + ret = -errno; goto fail; + } memset(&header, 0, sizeof(header)); memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC - ftruncate(snp_fd, header.grain_offset << 9); + if (ftruncate(snp_fd, header.grain_offset << 9)) { + ret = -errno; + goto fail; + } /* the descriptor offset = 0x200 */ - if (lseek(p_fd, 0x200, SEEK_SET) == -1) + if (lseek(p_fd, 0x200, SEEK_SET) == -1) { + ret = -errno; goto fail; - if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE) + } + if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE) { + ret = -errno; goto fail; + } if ((p_name = strstr(p_desc,"CID")) != NULL) { p_name += sizeof("CID"); @@ -257,10 +262,14 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file) (uint32_t)header.capacity, real_filename); /* write the descriptor */ - if (lseek(snp_fd, 0x200, SEEK_SET) == -1) + if (lseek(snp_fd, 0x200, SEEK_SET) == -1) { + ret = -errno; goto fail; - if (write(snp_fd, s_desc, strlen(s_desc)) == -1) + } + if (write(snp_fd, s_desc, strlen(s_desc)) == -1) { + ret = -errno; goto fail; + } gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table @@ -270,122 +279,100 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file) * 512 GTE per GT, each GTE points to grain */ gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE; - if (!gt_size) + if (!gt_size) { + ret = -EINVAL; goto fail; + } gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde gd_size = gde_entries * sizeof(uint32_t); /* write RGD */ rgd_buf = qemu_malloc(gd_size); - if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) + if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) { + ret = -errno; goto fail_rgd; - if (read(p_fd, rgd_buf, gd_size) != gd_size) + } + if (read(p_fd, rgd_buf, gd_size) != gd_size) { + ret = -errno; goto fail_rgd; - if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) + } + if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) { + ret = -errno; goto fail_rgd; - if (write(snp_fd, rgd_buf, gd_size) == -1) + } + if (write(snp_fd, rgd_buf, gd_size) == -1) { + ret = -errno; goto fail_rgd; - qemu_free(rgd_buf); + } /* write GD */ gd_buf = qemu_malloc(gd_size); - if (lseek(p_fd, gd_offset, SEEK_SET) == -1) + if (lseek(p_fd, gd_offset, SEEK_SET) == -1) { + ret = -errno; goto fail_gd; - if (read(p_fd, gd_buf, gd_size) != gd_size) + } + if (read(p_fd, gd_buf, gd_size) != gd_size) { + ret = -errno; goto fail_gd; - if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) + } + if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) { + ret = -errno; goto fail_gd; - if (write(snp_fd, gd_buf, gd_size) == -1) + } + if (write(snp_fd, gd_buf, gd_size) == -1) { + ret = -errno; goto fail_gd; - qemu_free(gd_buf); - - close(p_fd); - close(snp_fd); - return 0; + } + ret = 0; - fail_gd: +fail_gd: qemu_free(gd_buf); - fail_rgd: +fail_rgd: qemu_free(rgd_buf); - fail: +fail: close(p_fd); close(snp_fd); - return -1; + return ret; } -static void vmdk_parent_close(BlockDriverState *bs) +static int vmdk_parent_open(BlockDriverState *bs) { - if (bs->backing_hd) - bdrv_close(bs->backing_hd); -} - -static int parent_open = 0; -static int vmdk_parent_open(BlockDriverState *bs, const char * filename) -{ - BDRVVmdkState *s = bs->opaque; char *p_name; char desc[DESC_SIZE]; - char parent_img_name[1024]; /* the descriptor offset = 0x200 */ - if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) + if (bdrv_pread(bs->file, 0x200, desc, DESC_SIZE) != DESC_SIZE) return -1; if ((p_name = strstr(desc,"parentFileNameHint")) != NULL) { char *end_name; - struct stat file_buf; p_name += sizeof("parentFileNameHint") + 1; if ((end_name = strchr(p_name,'\"')) == NULL) return -1; - if ((end_name - p_name) > sizeof (s->hd->backing_file) - 1) + if ((end_name - p_name) > sizeof (bs->backing_file) - 1) return -1; - pstrcpy(s->hd->backing_file, end_name - p_name + 1, p_name); - if (stat(s->hd->backing_file, &file_buf) != 0) { - path_combine(parent_img_name, sizeof(parent_img_name), - filename, s->hd->backing_file); - } else { - pstrcpy(parent_img_name, sizeof(parent_img_name), - s->hd->backing_file); - } - - s->hd->backing_hd = bdrv_new(""); - if (!s->hd->backing_hd) { - failure: - bdrv_close(s->hd); - return -1; - } - parent_open = 1; - if (bdrv_open(s->hd->backing_hd, parent_img_name, BDRV_O_RDONLY) < 0) - goto failure; - parent_open = 0; + pstrcpy(bs->backing_file, end_name - p_name + 1, p_name); } return 0; } -static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) +static int vmdk_open(BlockDriverState *bs, int flags) { BDRVVmdkState *s = bs->opaque; uint32_t magic; - int l1_size, i, ret; + int l1_size, i; - if (parent_open) - // Parent must be opened as RO. - flags = BDRV_O_RDONLY; - - ret = bdrv_file_open(&s->hd, filename, flags); - if (ret < 0) - return ret; - if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic)) + if (bdrv_pread(bs->file, 0, &magic, sizeof(magic)) != sizeof(magic)) goto fail; magic = be32_to_cpu(magic); if (magic == VMDK3_MAGIC) { VMDK3Header header; - if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) + if (bdrv_pread(bs->file, sizeof(magic), &header, sizeof(header)) != sizeof(header)) goto fail; s->cluster_sectors = le32_to_cpu(header.granularity); s->l2_size = 1 << 9; @@ -397,7 +384,7 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) } else if (magic == VMDK4_MAGIC) { VMDK4Header header; - if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) + if (bdrv_pread(bs->file, sizeof(magic), &header, sizeof(header)) != sizeof(header)) goto fail; bs->total_sectors = le64_to_cpu(header.capacity); s->cluster_sectors = le64_to_cpu(header.granularity); @@ -410,13 +397,8 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9; s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9; - if (parent_open) - s->is_parent = 1; - else - s->is_parent = 0; - // try to open parent images, if exist - if (vmdk_parent_open(bs, filename) != 0) + if (vmdk_parent_open(bs) != 0) goto fail; // write the CID once after the image creation s->parent_cid = vmdk_read_cid(bs,1); @@ -427,7 +409,7 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) /* read the L1 table */ l1_size = s->l1_size * sizeof(uint32_t); s->l1_table = qemu_malloc(l1_size); - if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size) + if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, l1_size) != l1_size) goto fail; for(i = 0; i < s->l1_size; i++) { le32_to_cpus(&s->l1_table[i]); @@ -435,7 +417,7 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) if (s->l1_backup_table_offset) { s->l1_backup_table = qemu_malloc(l1_size); - if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size) + if (bdrv_pread(bs->file, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size) goto fail; for(i = 0; i < s->l1_size; i++) { le32_to_cpus(&s->l1_backup_table[i]); @@ -448,7 +430,6 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) qemu_free(s->l1_backup_table); qemu_free(s->l1_table); qemu_free(s->l2_cache); - bdrv_delete(s->hd); return -1; } @@ -458,29 +439,28 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data, static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset, uint64_t offset, int allocate) { - uint64_t parent_cluster_offset; BDRVVmdkState *s = bs->opaque; uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB // we will be here if it's first write on non-exist grain(cluster). // try to read from parent image, if exist - if (s->hd->backing_hd) { - BDRVVmdkState *ps = s->hd->backing_hd->opaque; + if (bs->backing_hd) { + int ret; if (!vmdk_is_cid_valid(bs)) return -1; - parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, NULL, offset, allocate); - - if (parent_cluster_offset) { - BDRVVmdkState *act_s = activeBDRV.hd->opaque; - - if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != ps->cluster_sectors*512) - return -1; + ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain, + s->cluster_sectors); + if (ret < 0) { + return -1; + } - //Write grain only into the active image - if (bdrv_pwrite(act_s->hd, activeBDRV.cluster_offset << 9, whole_grain, sizeof(whole_grain)) != sizeof(whole_grain)) - return -1; + //Write grain only into the active image + ret = bdrv_write(bs->file, cluster_offset, whole_grain, + s->cluster_sectors); + if (ret < 0) { + return -1; } } return 0; @@ -491,14 +471,14 @@ static int vmdk_L2update(BlockDriverState *bs, VmdkMetaData *m_data) BDRVVmdkState *s = bs->opaque; /* update L2 table */ - if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)), - &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset)) + if (bdrv_pwrite_sync(bs->file, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)), + &(m_data->offset), sizeof(m_data->offset)) < 0) return -1; /* update backup L2 table */ if (s->l1_backup_table_offset != 0) { m_data->l2_offset = s->l1_backup_table[m_data->l1_index]; - if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)), - &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset)) + if (bdrv_pwrite_sync(bs->file, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)), + &(m_data->offset), sizeof(m_data->offset)) < 0) return -1; } @@ -545,7 +525,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data, } } l2_table = s->l2_cache + (min_index * s->l2_size); - if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) != + if (bdrv_pread(bs->file, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) != s->l2_size * sizeof(uint32_t)) return 0; @@ -558,18 +538,15 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data, if (!cluster_offset) { if (!allocate) return 0; + // Avoid the L2 tables update for the images that have snapshots. - if (!s->is_parent) { - cluster_offset = bdrv_getlength(s->hd); - bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9)); - - cluster_offset >>= 9; - tmp = cpu_to_le32(cluster_offset); - l2_table[l2_index] = tmp; - // Save the active image state - activeBDRV.cluster_offset = cluster_offset; - activeBDRV.hd = bs; - } + cluster_offset = bdrv_getlength(bs->file); + bdrv_truncate(bs->file, cluster_offset + (s->cluster_sectors << 9)); + + cluster_offset >>= 9; + tmp = cpu_to_le32(cluster_offset); + l2_table[l2_index] = tmp; + /* First of all we write grain itself, to avoid race condition * that may to corrupt the image. * This problem may occur because of insufficient space on host disk @@ -621,17 +598,17 @@ static int vmdk_read(BlockDriverState *bs, int64_t sector_num, n = nb_sectors; if (!cluster_offset) { // try to read from parent image, if exist - if (s->hd->backing_hd) { + if (bs->backing_hd) { if (!vmdk_is_cid_valid(bs)) return -1; - ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n); + ret = bdrv_read(bs->backing_hd, sector_num, buf, n); if (ret < 0) return -1; } else { memset(buf, 0, 512 * n); } } else { - if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) + if(bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) return -1; } nb_sectors -= n; @@ -667,7 +644,7 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num, if (!cluster_offset) return -1; - if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) + if (bdrv_pwrite(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) return -1; if (m_data.valid) { /* update L2 tables */ @@ -715,6 +692,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options) int64_t total_size = 0; const char *backing_file = NULL; int flags = 0; + int ret; // Read out options while (options && options->name) { @@ -736,7 +714,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options) fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644); if (fd < 0) - return -1; + return -errno; magic = cpu_to_be32(VMDK4_MAGIC); memset(&header, 0, sizeof(header)); header.version = cpu_to_le32(1); @@ -771,22 +749,44 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options) header.check_bytes[3] = 0xa; /* write all the data */ - write(fd, &magic, sizeof(magic)); - write(fd, &header, sizeof(header)); + ret = qemu_write_full(fd, &magic, sizeof(magic)); + if (ret != sizeof(magic)) { + ret = -errno; + goto exit; + } + ret = qemu_write_full(fd, &header, sizeof(header)); + if (ret != sizeof(header)) { + ret = -errno; + goto exit; + } - ftruncate(fd, header.grain_offset << 9); + ret = ftruncate(fd, header.grain_offset << 9); + if (ret < 0) { + ret = -errno; + goto exit; + } /* write grain directory */ lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET); for (i = 0, tmp = header.rgd_offset + gd_size; - i < gt_count; i++, tmp += gt_size) - write(fd, &tmp, sizeof(tmp)); + i < gt_count; i++, tmp += gt_size) { + ret = qemu_write_full(fd, &tmp, sizeof(tmp)); + if (ret != sizeof(tmp)) { + ret = -errno; + goto exit; + } + } /* write backup grain directory */ lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET); for (i = 0, tmp = header.gd_offset + gd_size; - i < gt_count; i++, tmp += gt_size) - write(fd, &tmp, sizeof(tmp)); + i < gt_count; i++, tmp += gt_size) { + ret = qemu_write_full(fd, &tmp, sizeof(tmp)); + if (ret != sizeof(tmp)) { + ret = -errno; + goto exit; + } + } /* compose the descriptor */ real_filename = filename; @@ -803,10 +803,16 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options) /* write the descriptor */ lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET); - write(fd, desc, strlen(desc)); + ret = qemu_write_full(fd, desc, strlen(desc)); + if (ret != strlen(desc)) { + ret = -errno; + goto exit; + } + ret = 0; +exit: close(fd); - return 0; + return ret; } static void vmdk_close(BlockDriverState *bs) @@ -815,15 +821,11 @@ static void vmdk_close(BlockDriverState *bs) qemu_free(s->l1_table); qemu_free(s->l2_cache); - // try to close parent image, if exist - vmdk_parent_close(s->hd); - bdrv_delete(s->hd); } static void vmdk_flush(BlockDriverState *bs) { - BDRVVmdkState *s = bs->opaque; - bdrv_flush(s->hd); + bdrv_flush(bs->file); } @@ -850,7 +852,7 @@ static BlockDriver bdrv_vmdk = { .format_name = "vmdk", .instance_size = sizeof(BDRVVmdkState), .bdrv_probe = vmdk_probe, - .bdrv_open = vmdk_open, + .bdrv_open = vmdk_open, .bdrv_read = vmdk_read, .bdrv_write = vmdk_write, .bdrv_close = vmdk_close, |