unsigned int l1_index;
unsigned int l2_index;
unsigned int l2_offset;
- int valid;
+ bool new_allocation;
uint32_t *l2_cache_entry;
} VmdkMetaData;
typedef struct VmdkGrainMarker {
uint64_t lba;
uint32_t size;
- uint8_t data[0];
+ uint8_t data[];
} QEMU_PACKED VmdkGrainMarker;
enum {
}
static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
- const char *desc_file_path, QDict *options,
- Error **errp)
+ QDict *options, Error **errp)
{
int ret;
int matches;
const char *p, *np;
int64_t sectors = 0;
int64_t flat_offset;
+ char *desc_file_dir = NULL;
char *extent_path;
BdrvChild *extent_file;
+ BdrvChildRole extent_role;
BDRVVmdkState *s = bs->opaque;
VmdkExtent *extent;
char extent_opt_prefix[32];
continue;
}
- if (!path_is_absolute(fname) && !path_has_protocol(fname) &&
- !desc_file_path[0])
- {
- bdrv_refresh_filename(bs->file->bs);
- error_setg(errp, "Cannot use relative extent paths with VMDK "
- "descriptor file '%s'", bs->file->bs->filename);
- return -EINVAL;
- }
+ if (path_is_absolute(fname)) {
+ extent_path = g_strdup(fname);
+ } else {
+ if (!desc_file_dir) {
+ desc_file_dir = bdrv_dirname(bs->file->bs, errp);
+ if (!desc_file_dir) {
+ bdrv_refresh_filename(bs->file->bs);
+ error_prepend(errp, "Cannot use relative paths with VMDK "
+ "descriptor file '%s': ",
+ bs->file->bs->filename);
+ ret = -EINVAL;
+ goto out;
+ }
+ }
- extent_path = path_combine(desc_file_path, fname);
+ extent_path = g_strconcat(desc_file_dir, fname, NULL);
+ }
ret = snprintf(extent_opt_prefix, 32, "extents.%d", s->num_extents);
assert(ret < 32);
+ extent_role = BDRV_CHILD_DATA;
+ if (strcmp(type, "FLAT") != 0 && strcmp(type, "VMFS") != 0) {
+ /* non-flat extents have metadata */
+ extent_role |= BDRV_CHILD_METADATA;
+ }
+
extent_file = bdrv_open_child(extent_path, options, extent_opt_prefix,
- bs, &child_file, false, &local_err);
+ bs, &child_of_bds, extent_role, false,
+ &local_err);
g_free(extent_path);
if (local_err) {
error_propagate(errp, local_err);
- return -EINVAL;
+ ret = -EINVAL;
+ goto out;
}
/* save to extents array */
0, 0, 0, 0, 0, &extent, errp);
if (ret < 0) {
bdrv_unref_child(bs, extent_file);
- return ret;
+ goto out;
}
extent->flat_start_offset = flat_offset << 9;
} else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
g_free(buf);
if (ret) {
bdrv_unref_child(bs, extent_file);
- return ret;
+ goto out;
}
extent = &s->extents[s->num_extents - 1];
} else if (!strcmp(type, "SESPARSE")) {
ret = vmdk_open_se_sparse(bs, extent_file, bs->open_flags, errp);
if (ret) {
bdrv_unref_child(bs, extent_file);
- return ret;
+ goto out;
}
extent = &s->extents[s->num_extents - 1];
} else {
error_setg(errp, "Unsupported extent type '%s'", type);
bdrv_unref_child(bs, extent_file);
- return -ENOTSUP;
+ ret = -ENOTSUP;
+ goto out;
}
extent->type = g_strdup(type);
}
- return 0;
+
+ ret = 0;
+ goto out;
invalid:
np = next_line(p);
np--;
}
error_setg(errp, "Invalid extent line: %.*s", (int)(np - p), p);
- return -EINVAL;
+ ret = -EINVAL;
+
+out:
+ g_free(desc_file_dir);
+ return ret;
}
static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
}
s->create_type = g_strdup(ct);
s->desc_offset = 0;
- ret = vmdk_parse_extents(buf, bs, bs->file->bs->exact_filename, options,
- errp);
+ ret = vmdk_parse_extents(buf, bs, options, errp);
exit:
return ret;
}
uint32_t magic;
Error *local_err = NULL;
- bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
- false, errp);
+ bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
+ BDRV_CHILD_IMAGE, false, errp);
if (!bs->file) {
return -EINVAL;
}
s->desc_offset = 0x200;
break;
default:
+ /* No data in the descriptor file */
+ bs->file->role &= ~BDRV_CHILD_DATA;
+
+ /* Must succeed because we have given up permissions if anything */
+ bdrv_child_refresh_perms(bs, bs->file, &error_abort);
+
ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
break;
}
* get_whole_cluster
*
* Copy backing file's cluster that covers @sector_num, otherwise write zero,
- * to the cluster at @cluster_sector_num.
+ * to the cluster at @cluster_sector_num. If @zeroed is true, we're overwriting
+ * a zeroed cluster in the current layer and must not copy data from the
+ * backing file.
*
* If @skip_start_sector < @skip_end_sector, the relative range
* [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
uint64_t cluster_offset,
uint64_t offset,
uint64_t skip_start_bytes,
- uint64_t skip_end_bytes)
+ uint64_t skip_end_bytes,
+ bool zeroed)
{
int ret = VMDK_OK;
int64_t cluster_bytes;
uint8_t *whole_grain;
+ bool copy_from_backing;
/* For COW, align request sector_num to cluster start */
cluster_bytes = extent->cluster_sectors << BDRV_SECTOR_BITS;
offset = QEMU_ALIGN_DOWN(offset, cluster_bytes);
whole_grain = qemu_blockalign(bs, cluster_bytes);
+ copy_from_backing = bs->backing && !zeroed;
- if (!bs->backing) {
+ if (!copy_from_backing) {
memset(whole_grain, 0, skip_start_bytes);
memset(whole_grain + skip_end_bytes, 0, cluster_bytes - skip_end_bytes);
}
/* Read backing data before skip range */
if (skip_start_bytes > 0) {
- if (bs->backing) {
+ if (copy_from_backing) {
/* qcow2 emits this on bs->file instead of bs->backing */
BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
ret = bdrv_pread(bs->backing, offset, whole_grain,
}
/* Read backing data after skip range */
if (skip_end_bytes < cluster_bytes) {
- if (bs->backing) {
+ if (copy_from_backing) {
/* qcow2 emits this on bs->file instead of bs->backing */
BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
ret = bdrv_pread(bs->backing, offset + skip_end_bytes,
offset = cpu_to_le32(offset);
/* update L2 table */
BLKDBG_EVENT(extent->file, BLKDBG_L2_UPDATE);
- if (bdrv_pwrite_sync(extent->file,
+ if (bdrv_pwrite(extent->file,
((int64_t)m_data->l2_offset * 512)
+ (m_data->l2_index * sizeof(offset)),
&offset, sizeof(offset)) < 0) {
/* update backup L2 table */
if (extent->l1_backup_table_offset != 0) {
m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
- if (bdrv_pwrite_sync(extent->file,
+ if (bdrv_pwrite(extent->file,
((int64_t)m_data->l2_offset * 512)
+ (m_data->l2_index * sizeof(offset)),
&offset, sizeof(offset)) < 0) {
return VMDK_ERROR;
}
}
+ if (bdrv_flush(extent->file->bs) < 0) {
+ return VMDK_ERROR;
+ }
if (m_data->l2_cache_entry) {
*m_data->l2_cache_entry = offset;
}
unsigned int l2_size_bytes = extent->l2_size * extent->entry_size;
if (m_data) {
- m_data->valid = 0;
+ m_data->new_allocation = false;
}
if (extent->flat) {
*cluster_offset = extent->flat_start_offset;
extent->l2_cache_counts[min_index] = 1;
found:
l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
+ if (m_data) {
+ m_data->l1_index = l1_index;
+ m_data->l2_index = l2_index;
+ m_data->l2_offset = l2_offset;
+ m_data->l2_cache_entry = ((uint32_t *)l2_table) + l2_index;
+ }
if (extent->sesparse) {
cluster_sector = le64_to_cpu(((uint64_t *)l2_table)[l2_index]);
* or inappropriate VM shutdown.
*/
ret = get_whole_cluster(bs, extent, cluster_sector * BDRV_SECTOR_SIZE,
- offset, skip_start_bytes, skip_end_bytes);
+ offset, skip_start_bytes, skip_end_bytes,
+ zeroed);
if (ret) {
return ret;
}
if (m_data) {
- m_data->valid = 1;
- m_data->l1_index = l1_index;
- m_data->l2_index = l2_index;
- m_data->l2_offset = l2_offset;
- m_data->l2_cache_entry = ((uint32_t *)l2_table) + l2_index;
+ m_data->new_allocation = true;
}
}
*cluster_offset = cluster_sector << BDRV_SECTOR_BITS;
if (!extent->compressed) {
ret |= BDRV_BLOCK_OFFSET_VALID;
*map = cluster_offset + index_in_cluster;
+ if (extent->flat) {
+ ret |= BDRV_BLOCK_RECURSE;
+ }
}
*file = extent->file->bs;
break;
if (extent->compressed) {
void *compressed_data;
+ /* Only whole clusters */
+ if (offset_in_cluster ||
+ n_bytes > (extent->cluster_sectors * SECTOR_SIZE) ||
+ (n_bytes < (extent->cluster_sectors * SECTOR_SIZE) &&
+ offset + n_bytes != extent->end_sector * SECTOR_SIZE))
+ {
+ ret = -EINVAL;
+ goto out;
+ }
+
if (!extent->has_marker) {
ret = -EINVAL;
goto out;
error_report("Could not write to allocated cluster"
" for streamOptimized");
return -EIO;
- } else {
+ } else if (!zeroed) {
/* allocate */
ret = get_cluster_offset(bs, extent, &m_data, offset,
true, &cluster_offset, 0, 0);
offset_in_cluster == 0 &&
n_bytes >= extent->cluster_sectors * BDRV_SECTOR_SIZE) {
n_bytes = extent->cluster_sectors * BDRV_SECTOR_SIZE;
- if (!zero_dry_run) {
+ if (!zero_dry_run && ret != VMDK_ZEROED) {
/* update L2 tables */
if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED)
!= VMDK_OK) {
if (ret) {
return ret;
}
- if (m_data.valid) {
+ if (m_data.new_allocation) {
/* update L2 tables */
if (vmdk_L2update(extent, &m_data,
cluster_offset >> BDRV_SECTOR_BITS)
return length;
}
length = QEMU_ALIGN_UP(length, BDRV_SECTOR_SIZE);
- ret = bdrv_truncate(s->extents[i].file, length,
- PREALLOC_MODE_OFF, NULL);
+ ret = bdrv_truncate(s->extents[i].file, length, false,
+ PREALLOC_MODE_OFF, 0, NULL);
if (ret < 0) {
return ret;
}
int gd_buf_size;
if (flat) {
- ret = blk_truncate(blk, filesize, PREALLOC_MODE_OFF, errp);
+ ret = blk_truncate(blk, filesize, false, PREALLOC_MODE_OFF, 0, errp);
goto exit;
}
magic = cpu_to_be32(VMDK4_MAGIC);
goto exit;
}
- ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9,
- PREALLOC_MODE_OFF, errp);
+ ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9, false,
+ PREALLOC_MODE_OFF, 0, errp);
if (ret < 0) {
goto exit;
}
BlockBackend *blk = NULL;
Error *local_err = NULL;
- ret = bdrv_create_file(filename, opts, &local_err);
+ ret = bdrv_create_file(filename, opts, errp);
if (ret < 0) {
- error_propagate(errp, local_err);
goto exit;
}
/* bdrv_pwrite write padding zeros to align to sector, we don't need that
* for description file */
if (desc_offset == 0) {
- ret = blk_truncate(blk, desc_len, PREALLOC_MODE_OFF, errp);
+ ret = blk_truncate(blk, desc_len, false, PREALLOC_MODE_OFF, 0, errp);
if (ret < 0) {
goto exit;
}
return blk;
}
-static int coroutine_fn vmdk_co_create_opts(const char *filename, QemuOpts *opts,
+static int coroutine_fn vmdk_co_create_opts(BlockDriver *drv,
+ const char *filename,
+ QemuOpts *opts,
Error **errp)
{
Error *local_err = NULL;
if (!bs) {
return NULL;
}
- blk = blk_new(bdrv_get_aio_context(bs),
- BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_PERM_RESIZE,
- BLK_PERM_ALL);
- if (blk_insert_bs(blk, bs, errp)) {
- bdrv_unref(bs);
+ blk = blk_new_with_bs(bs,
+ BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_PERM_RESIZE,
+ BLK_PERM_ALL, errp);
+ if (!blk) {
return NULL;
}
blk_set_allow_write_beyond_eof(blk, true);
.bdrv_open = vmdk_open,
.bdrv_co_check = vmdk_co_check,
.bdrv_reopen_prepare = vmdk_reopen_prepare,
- .bdrv_child_perm = bdrv_format_default_perms,
+ .bdrv_child_perm = bdrv_default_perms,
.bdrv_co_preadv = vmdk_co_preadv,
.bdrv_co_pwritev = vmdk_co_pwritev,
.bdrv_co_pwritev_compressed = vmdk_co_pwritev_compressed,
.bdrv_get_info = vmdk_get_info,
.bdrv_gather_child_options = vmdk_gather_child_options,
+ .is_format = true,
.supports_backing = true,
.create_opts = &vmdk_create_opts,
};