vmdk: make vmdk_snapshot_create return -errno

Signed-off-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Juan Quintela 2010-03-04 10:00:35 +01:00 committed by Anthony Liguori
parent b781cce53d
commit 53c2e71632
1 changed files with 58 additions and 21 deletions

View File

@ -187,6 +187,7 @@ static int vmdk_is_cid_valid(BlockDriverState *bs)
static int vmdk_snapshot_create(const char *filename, const char *backing_file) static int vmdk_snapshot_create(const char *filename, const char *backing_file)
{ {
int snp_fd, p_fd; int snp_fd, p_fd;
int ret;
uint32_t p_cid; uint32_t p_cid;
char *p_name, *gd_buf, *rgd_buf; char *p_name, *gd_buf, *rgd_buf;
const char *real_filename, *temp_str; const char *real_filename, *temp_str;
@ -211,35 +212,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); snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
if (snp_fd < 0) if (snp_fd < 0)
return -1; return -errno;
p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE); p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
if (p_fd < 0) { if (p_fd < 0) {
close(snp_fd); close(snp_fd);
return -1; return -errno;
} }
/* read the header */ /* read the header */
if (lseek(p_fd, 0x0, SEEK_SET) == -1) if (lseek(p_fd, 0x0, SEEK_SET) == -1) {
ret = -errno;
goto fail; goto fail;
if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) }
if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) {
ret = -errno;
goto fail; goto fail;
}
/* write the header */ /* write the header */
if (lseek(snp_fd, 0x0, SEEK_SET) == -1) if (lseek(snp_fd, 0x0, SEEK_SET) == -1) {
ret = -errno;
goto fail; goto fail;
if (write(snp_fd, hdr, HEADER_SIZE) == -1) }
if (write(snp_fd, hdr, HEADER_SIZE) == -1) {
ret = -errno;
goto fail; goto fail;
}
memset(&header, 0, sizeof(header)); memset(&header, 0, sizeof(header));
memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
if (ftruncate(snp_fd, header.grain_offset << 9)) if (ftruncate(snp_fd, header.grain_offset << 9)) {
ret = -errno;
goto fail; goto fail;
}
/* the descriptor offset = 0x200 */ /* the descriptor offset = 0x200 */
if (lseek(p_fd, 0x200, SEEK_SET) == -1) if (lseek(p_fd, 0x200, SEEK_SET) == -1) {
ret = -errno;
goto fail; 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; goto fail;
}
if ((p_name = strstr(p_desc,"CID")) != NULL) { if ((p_name = strstr(p_desc,"CID")) != NULL) {
p_name += sizeof("CID"); p_name += sizeof("CID");
@ -258,10 +273,14 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
(uint32_t)header.capacity, real_filename); (uint32_t)header.capacity, real_filename);
/* write the descriptor */ /* write the descriptor */
if (lseek(snp_fd, 0x200, SEEK_SET) == -1) if (lseek(snp_fd, 0x200, SEEK_SET) == -1) {
ret = -errno;
goto fail; 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; goto fail;
}
gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
@ -271,33 +290,51 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
* 512 GTE per GT, each GTE points to grain * 512 GTE per GT, each GTE points to grain
*/ */
gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE; gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
if (!gt_size) if (!gt_size) {
ret = -EINVAL;
goto fail; goto fail;
}
gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
gd_size = gde_entries * sizeof(uint32_t); gd_size = gde_entries * sizeof(uint32_t);
/* write RGD */ /* write RGD */
rgd_buf = qemu_malloc(gd_size); 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; 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; 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; 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; goto fail_rgd;
}
qemu_free(rgd_buf); qemu_free(rgd_buf);
/* write GD */ /* write GD */
gd_buf = qemu_malloc(gd_size); 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; 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; 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; 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; goto fail_gd;
}
qemu_free(gd_buf); qemu_free(gd_buf);
close(p_fd); close(p_fd);
@ -311,7 +348,7 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
fail: fail:
close(p_fd); close(p_fd);
close(snp_fd); close(snp_fd);
return -1; return ret;
} }
static void vmdk_parent_close(BlockDriverState *bs) static void vmdk_parent_close(BlockDriverState *bs)