|
|
9ae3a8 |
From aa17f7f15bf2b0af9019a6935563d1f1a98ecc05 Mon Sep 17 00:00:00 2001
|
|
|
9ae3a8 |
From: Jeffrey Cody <jcody@redhat.com>
|
|
|
9ae3a8 |
Date: Tue, 16 Sep 2014 20:11:40 +0200
|
|
|
9ae3a8 |
Subject: [PATCH 02/20] vmdk: Allow vmdk_create to work with protocol
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Message-id: <1c4328440599c6f754b823e280f9b8212ae9c62b.1410897407.git.jcody@redhat.com>
|
|
|
9ae3a8 |
Patchwork-id: 61206
|
|
|
9ae3a8 |
O-Subject: [PATCH qemu-kvm-rhel RHEL7.1 01/15] vmdk: Allow vmdk_create to work with protocol
|
|
|
9ae3a8 |
Bugzilla: 1098086
|
|
|
9ae3a8 |
RH-Acked-by: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Max Reitz <mreitz@redhat.com>
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
From: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
This improves vmdk_create to use bdrv_* functions to replace qemu_open
|
|
|
9ae3a8 |
and other fd functions. The error handling are improved as well. One
|
|
|
9ae3a8 |
difference is that bdrv_pwrite will round up buffer to sectors, so for
|
|
|
9ae3a8 |
description file, an extra bdrv_truncate is used in the end to drop
|
|
|
9ae3a8 |
inding zeros.
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Notes:
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
- A bonus bug fix is correct endian is used in initializing GD entries.
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
- ROUND_UP and DIV_ROUND_UP are used where possible.
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
I tested that new code produces exactly the same file as previously.
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
Tested-by: Peter Lieven <pl@kamp.de>
|
|
|
9ae3a8 |
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
9ae3a8 |
(cherry picked from commit 917703c179d4483d050fe112a2a81acec3d6ffd7)
|
|
|
9ae3a8 |
Signed-off-by: Jeff Cody <jcody@redhat.com>
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
9ae3a8 |
---
|
|
|
9ae3a8 |
block/vmdk.c | 164 +++++++++++++++++++++++++++++++++------------------------
|
|
|
9ae3a8 |
1 files changed, 95 insertions(+), 69 deletions(-)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
diff --git a/block/vmdk.c b/block/vmdk.c
|
|
|
9ae3a8 |
index 7532e11..4ff9531 100644
|
|
|
9ae3a8 |
--- a/block/vmdk.c
|
|
|
9ae3a8 |
+++ b/block/vmdk.c
|
|
|
9ae3a8 |
@@ -1464,23 +1464,33 @@ static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
static int vmdk_create_extent(const char *filename, int64_t filesize,
|
|
|
9ae3a8 |
- bool flat, bool compress, bool zeroed_grain)
|
|
|
9ae3a8 |
+ bool flat, bool compress, bool zeroed_grain,
|
|
|
9ae3a8 |
+ Error **errp)
|
|
|
9ae3a8 |
{
|
|
|
9ae3a8 |
int ret, i;
|
|
|
9ae3a8 |
- int fd = 0;
|
|
|
9ae3a8 |
+ BlockDriverState *bs = NULL;
|
|
|
9ae3a8 |
VMDK4Header header;
|
|
|
9ae3a8 |
- uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
|
|
|
9ae3a8 |
+ Error *local_err;
|
|
|
9ae3a8 |
+ uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
|
|
|
9ae3a8 |
+ uint32_t *gd_buf = NULL;
|
|
|
9ae3a8 |
+ int gd_buf_size;
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
- fd = qemu_open(filename,
|
|
|
9ae3a8 |
- O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
|
|
|
9ae3a8 |
- 0644);
|
|
|
9ae3a8 |
- if (fd < 0) {
|
|
|
9ae3a8 |
- return -errno;
|
|
|
9ae3a8 |
+ ret = bdrv_create_file(filename, NULL, &local_err);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_propagate(errp, local_err);
|
|
|
9ae3a8 |
+ goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_propagate(errp, local_err);
|
|
|
9ae3a8 |
+ goto exit;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
if (flat) {
|
|
|
9ae3a8 |
- ret = ftruncate(fd, filesize);
|
|
|
9ae3a8 |
+ ret = bdrv_truncate(bs, filesize);
|
|
|
9ae3a8 |
if (ret < 0) {
|
|
|
9ae3a8 |
- ret = -errno;
|
|
|
9ae3a8 |
+ error_setg(errp, "Could not truncate file");
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
@@ -1491,24 +1501,23 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
|
|
|
9ae3a8 |
| (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
|
|
|
9ae3a8 |
| (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
|
|
|
9ae3a8 |
header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
|
|
|
9ae3a8 |
- header.capacity = filesize / 512;
|
|
|
9ae3a8 |
+ header.capacity = filesize / BDRV_SECTOR_SIZE;
|
|
|
9ae3a8 |
header.granularity = 128;
|
|
|
9ae3a8 |
- header.num_gtes_per_gt = 512;
|
|
|
9ae3a8 |
+ header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
- grains = (filesize / 512 + header.granularity - 1) / header.granularity;
|
|
|
9ae3a8 |
- gt_size = ((header.num_gtes_per_gt * sizeof(uint32_t)) + 511) >> 9;
|
|
|
9ae3a8 |
- gt_count =
|
|
|
9ae3a8 |
- (grains + header.num_gtes_per_gt - 1) / header.num_gtes_per_gt;
|
|
|
9ae3a8 |
- gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
|
|
|
9ae3a8 |
+ grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
|
|
|
9ae3a8 |
+ gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
|
|
|
9ae3a8 |
+ BDRV_SECTOR_SIZE);
|
|
|
9ae3a8 |
+ gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
|
|
|
9ae3a8 |
+ gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
header.desc_offset = 1;
|
|
|
9ae3a8 |
header.desc_size = 20;
|
|
|
9ae3a8 |
header.rgd_offset = header.desc_offset + header.desc_size;
|
|
|
9ae3a8 |
- header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
|
|
|
9ae3a8 |
+ header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
|
|
|
9ae3a8 |
header.grain_offset =
|
|
|
9ae3a8 |
- ((header.gd_offset + gd_size + (gt_size * gt_count) +
|
|
|
9ae3a8 |
- header.granularity - 1) / header.granularity) *
|
|
|
9ae3a8 |
- header.granularity;
|
|
|
9ae3a8 |
+ ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
|
|
|
9ae3a8 |
+ header.granularity);
|
|
|
9ae3a8 |
/* swap endianness for all header fields */
|
|
|
9ae3a8 |
header.version = cpu_to_le32(header.version);
|
|
|
9ae3a8 |
header.flags = cpu_to_le32(header.flags);
|
|
|
9ae3a8 |
@@ -1528,48 +1537,55 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
|
|
|
9ae3a8 |
header.check_bytes[3] = 0xa;
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
/* write all the data */
|
|
|
9ae3a8 |
- ret = qemu_write_full(fd, &magic, sizeof(magic));
|
|
|
9ae3a8 |
- if (ret != sizeof(magic)) {
|
|
|
9ae3a8 |
- ret = -errno;
|
|
|
9ae3a8 |
+ ret = bdrv_pwrite(bs, 0, &magic, sizeof(magic));
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_set(errp, QERR_IO_ERROR);
|
|
|
9ae3a8 |
goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
- ret = qemu_write_full(fd, &header, sizeof(header));
|
|
|
9ae3a8 |
- if (ret != sizeof(header)) {
|
|
|
9ae3a8 |
- ret = -errno;
|
|
|
9ae3a8 |
+ ret = bdrv_pwrite(bs, sizeof(magic), &header, sizeof(header));
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_set(errp, QERR_IO_ERROR);
|
|
|
9ae3a8 |
goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
- ret = ftruncate(fd, le64_to_cpu(header.grain_offset) << 9);
|
|
|
9ae3a8 |
+ ret = bdrv_truncate(bs, le64_to_cpu(header.grain_offset) << 9);
|
|
|
9ae3a8 |
if (ret < 0) {
|
|
|
9ae3a8 |
- ret = -errno;
|
|
|
9ae3a8 |
+ error_setg(errp, "Could not truncate file");
|
|
|
9ae3a8 |
goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
/* write grain directory */
|
|
|
9ae3a8 |
- lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
|
|
|
9ae3a8 |
- for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_size;
|
|
|
9ae3a8 |
+ gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
|
|
|
9ae3a8 |
+ gd_buf = g_malloc0(gd_buf_size);
|
|
|
9ae3a8 |
+ for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
|
|
|
9ae3a8 |
i < gt_count; i++, tmp += gt_size) {
|
|
|
9ae3a8 |
- ret = qemu_write_full(fd, &tmp, sizeof(tmp));
|
|
|
9ae3a8 |
- if (ret != sizeof(tmp)) {
|
|
|
9ae3a8 |
- ret = -errno;
|
|
|
9ae3a8 |
- goto exit;
|
|
|
9ae3a8 |
- }
|
|
|
9ae3a8 |
+ gd_buf[i] = cpu_to_le32(tmp);
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
|
|
|
9ae3a8 |
+ gd_buf, gd_buf_size);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_set(errp, QERR_IO_ERROR);
|
|
|
9ae3a8 |
+ goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
/* write backup grain directory */
|
|
|
9ae3a8 |
- lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
|
|
|
9ae3a8 |
- for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_size;
|
|
|
9ae3a8 |
+ for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
|
|
|
9ae3a8 |
i < gt_count; i++, tmp += gt_size) {
|
|
|
9ae3a8 |
- ret = qemu_write_full(fd, &tmp, sizeof(tmp));
|
|
|
9ae3a8 |
- if (ret != sizeof(tmp)) {
|
|
|
9ae3a8 |
- ret = -errno;
|
|
|
9ae3a8 |
- goto exit;
|
|
|
9ae3a8 |
- }
|
|
|
9ae3a8 |
+ gd_buf[i] = cpu_to_le32(tmp);
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
|
|
|
9ae3a8 |
+ gd_buf, gd_buf_size);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_set(errp, QERR_IO_ERROR);
|
|
|
9ae3a8 |
+ goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
ret = 0;
|
|
|
9ae3a8 |
- exit:
|
|
|
9ae3a8 |
- qemu_close(fd);
|
|
|
9ae3a8 |
+exit:
|
|
|
9ae3a8 |
+ if (bs) {
|
|
|
9ae3a8 |
+ bdrv_unref(bs);
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ g_free(gd_buf);
|
|
|
9ae3a8 |
return ret;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
@@ -1616,7 +1632,9 @@ static int filename_decompose(const char *filename, char *path, char *prefix,
|
|
|
9ae3a8 |
static int vmdk_create(const char *filename, QEMUOptionParameter *options,
|
|
|
9ae3a8 |
Error **errp)
|
|
|
9ae3a8 |
{
|
|
|
9ae3a8 |
- int fd, idx = 0;
|
|
|
9ae3a8 |
+ int idx = 0;
|
|
|
9ae3a8 |
+ BlockDriverState *new_bs = NULL;
|
|
|
9ae3a8 |
+ Error *local_err;
|
|
|
9ae3a8 |
char *desc = NULL;
|
|
|
9ae3a8 |
int64_t total_size = 0, filesize;
|
|
|
9ae3a8 |
const char *adapter_type = NULL;
|
|
|
9ae3a8 |
@@ -1633,6 +1651,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
|
|
|
9ae3a8 |
uint32_t parent_cid = 0xffffffff;
|
|
|
9ae3a8 |
uint32_t number_heads = 16;
|
|
|
9ae3a8 |
bool zeroed_grain = false;
|
|
|
9ae3a8 |
+ uint32_t desc_offset = 0, desc_len;
|
|
|
9ae3a8 |
const char desc_template[] =
|
|
|
9ae3a8 |
"# Disk DescriptorFile\n"
|
|
|
9ae3a8 |
"version=1\n"
|
|
|
9ae3a8 |
@@ -1766,7 +1785,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
|
|
|
9ae3a8 |
path, desc_filename);
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
if (vmdk_create_extent(ext_filename, size,
|
|
|
9ae3a8 |
- flat, compress, zeroed_grain)) {
|
|
|
9ae3a8 |
+ flat, compress, zeroed_grain, errp)) {
|
|
|
9ae3a8 |
ret = -EINVAL;
|
|
|
9ae3a8 |
goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
@@ -1774,7 +1793,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
/* Format description line */
|
|
|
9ae3a8 |
snprintf(desc_line, sizeof(desc_line),
|
|
|
9ae3a8 |
- desc_extent_line, size / 512, desc_filename);
|
|
|
9ae3a8 |
+ desc_extent_line, size / BDRV_SECTOR_SIZE, desc_filename);
|
|
|
9ae3a8 |
g_string_append(ext_desc_lines, desc_line);
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
/* generate descriptor file */
|
|
|
9ae3a8 |
@@ -1785,36 +1804,43 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
|
|
|
9ae3a8 |
parent_desc_line,
|
|
|
9ae3a8 |
ext_desc_lines->str,
|
|
|
9ae3a8 |
(flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
|
|
|
9ae3a8 |
- total_size / (int64_t)(63 * number_heads * 512),
|
|
|
9ae3a8 |
+ total_size /
|
|
|
9ae3a8 |
+ (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
|
|
|
9ae3a8 |
number_heads,
|
|
|
9ae3a8 |
adapter_type);
|
|
|
9ae3a8 |
- if (split || flat) {
|
|
|
9ae3a8 |
- fd = qemu_open(filename,
|
|
|
9ae3a8 |
- O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
|
|
|
9ae3a8 |
- 0644);
|
|
|
9ae3a8 |
+ desc_len = strlen(desc);
|
|
|
9ae3a8 |
+ /* the descriptor offset = 0x200 */
|
|
|
9ae3a8 |
+ if (!split && !flat) {
|
|
|
9ae3a8 |
+ desc_offset = 0x200;
|
|
|
9ae3a8 |
} else {
|
|
|
9ae3a8 |
- fd = qemu_open(filename,
|
|
|
9ae3a8 |
- O_WRONLY | O_BINARY | O_LARGEFILE,
|
|
|
9ae3a8 |
- 0644);
|
|
|
9ae3a8 |
+ ret = bdrv_create_file(filename, options, &local_err);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_setg_errno(errp, -ret, "Could not create image file");
|
|
|
9ae3a8 |
+ goto exit;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
- if (fd < 0) {
|
|
|
9ae3a8 |
- ret = -errno;
|
|
|
9ae3a8 |
+ ret = bdrv_file_open(&new_bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_setg_errno(errp, -ret, "Could not write description");
|
|
|
9ae3a8 |
goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
- /* the descriptor offset = 0x200 */
|
|
|
9ae3a8 |
- if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) {
|
|
|
9ae3a8 |
- ret = -errno;
|
|
|
9ae3a8 |
- goto close_exit;
|
|
|
9ae3a8 |
+ ret = bdrv_pwrite(new_bs, desc_offset, desc, desc_len);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_setg_errno(errp, -ret, "Could not write description");
|
|
|
9ae3a8 |
+ goto exit;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
- ret = qemu_write_full(fd, desc, strlen(desc));
|
|
|
9ae3a8 |
- if (ret != strlen(desc)) {
|
|
|
9ae3a8 |
- ret = -errno;
|
|
|
9ae3a8 |
- goto close_exit;
|
|
|
9ae3a8 |
+ /* bdrv_pwrite write padding zeros to align to sector, we don't need that
|
|
|
9ae3a8 |
+ * for description file */
|
|
|
9ae3a8 |
+ if (desc_offset == 0) {
|
|
|
9ae3a8 |
+ ret = bdrv_truncate(new_bs, desc_len);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_setg(errp, "Could not truncate file");
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
- ret = 0;
|
|
|
9ae3a8 |
-close_exit:
|
|
|
9ae3a8 |
- qemu_close(fd);
|
|
|
9ae3a8 |
exit:
|
|
|
9ae3a8 |
+ if (new_bs) {
|
|
|
9ae3a8 |
+ bdrv_unref(new_bs);
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
g_free(desc);
|
|
|
9ae3a8 |
g_string_free(ext_desc_lines, true);
|
|
|
9ae3a8 |
return ret;
|
|
|
9ae3a8 |
--
|
|
|
9ae3a8 |
1.7.1
|
|
|
9ae3a8 |
|