block: Add PreallocMode to blk_truncate()
blk_truncate() itself will pass that value to bdrv_truncate(), and all callers of blk_truncate() just set the parameter to PREALLOC_MODE_OFF for now. Signed-off-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Message-id: 20170613202107.10125-4-mreitz@redhat.com Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
parent
7ea37c3066
commit
3a691c50f1
@ -1773,14 +1773,15 @@ int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
|
|||||||
BDRV_REQ_WRITE_COMPRESSED);
|
BDRV_REQ_WRITE_COMPRESSED);
|
||||||
}
|
}
|
||||||
|
|
||||||
int blk_truncate(BlockBackend *blk, int64_t offset, Error **errp)
|
int blk_truncate(BlockBackend *blk, int64_t offset, PreallocMode prealloc,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
if (!blk_is_available(blk)) {
|
if (!blk_is_available(blk)) {
|
||||||
error_setg(errp, "No medium inserted");
|
error_setg(errp, "No medium inserted");
|
||||||
return -ENOMEDIUM;
|
return -ENOMEDIUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bdrv_truncate(blk->root, offset, PREALLOC_MODE_OFF, errp);
|
return bdrv_truncate(blk->root, offset, prealloc, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void blk_pdiscard_entry(void *opaque)
|
static void blk_pdiscard_entry(void *opaque)
|
||||||
|
@ -163,7 +163,7 @@ static void coroutine_fn commit_run(void *opaque)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (base_len < s->common.len) {
|
if (base_len < s->common.len) {
|
||||||
ret = blk_truncate(s->base, s->common.len, NULL);
|
ret = blk_truncate(s->base, s->common.len, PREALLOC_MODE_OFF, NULL);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
@ -521,7 +521,7 @@ int bdrv_commit(BlockDriverState *bs)
|
|||||||
* grow the backing file image if possible. If not possible,
|
* grow the backing file image if possible. If not possible,
|
||||||
* we must return an error */
|
* we must return an error */
|
||||||
if (length > backing_length) {
|
if (length > backing_length) {
|
||||||
ret = blk_truncate(backing, length, &local_err);
|
ret = blk_truncate(backing, length, PREALLOC_MODE_OFF, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_report_err(local_err);
|
error_report_err(local_err);
|
||||||
goto ro_cleanup;
|
goto ro_cleanup;
|
||||||
|
@ -739,7 +739,8 @@ static void coroutine_fn mirror_run(void *opaque)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (s->bdev_length > base_length) {
|
if (s->bdev_length > base_length) {
|
||||||
ret = blk_truncate(s->target, s->bdev_length, NULL);
|
ret = blk_truncate(s->target, s->bdev_length, PREALLOC_MODE_OFF,
|
||||||
|
NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto immediate_exit;
|
goto immediate_exit;
|
||||||
}
|
}
|
||||||
|
@ -508,7 +508,7 @@ static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
|
|
||||||
blk_set_allow_write_beyond_eof(file, true);
|
blk_set_allow_write_beyond_eof(file, true);
|
||||||
|
|
||||||
ret = blk_truncate(file, 0, errp);
|
ret = blk_truncate(file, 0, PREALLOC_MODE_OFF, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -813,7 +813,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
|
|
||||||
blk_set_allow_write_beyond_eof(qcow_blk, true);
|
blk_set_allow_write_beyond_eof(qcow_blk, true);
|
||||||
|
|
||||||
ret = blk_truncate(qcow_blk, 0, errp);
|
ret = blk_truncate(qcow_blk, 0, PREALLOC_MODE_OFF, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -2802,7 +2802,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Okay, now that we have a valid image, let's give it the right size */
|
/* Okay, now that we have a valid image, let's give it the right size */
|
||||||
ret = blk_truncate(blk, total_size, errp);
|
ret = blk_truncate(blk, total_size, PREALLOC_MODE_OFF, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_prepend(errp, "Could not resize image: ");
|
error_prepend(errp, "Could not resize image: ");
|
||||||
goto out;
|
goto out;
|
||||||
@ -3995,7 +3995,7 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = blk_truncate(blk, new_size, &local_err);
|
ret = blk_truncate(blk, new_size, PREALLOC_MODE_OFF, &local_err);
|
||||||
blk_unref(blk);
|
blk_unref(blk);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_report_err(local_err);
|
error_report_err(local_err);
|
||||||
|
@ -583,7 +583,7 @@ static int qed_create(const char *filename, uint32_t cluster_size,
|
|||||||
blk_set_allow_write_beyond_eof(blk, true);
|
blk_set_allow_write_beyond_eof(blk, true);
|
||||||
|
|
||||||
/* File must start empty and grow, check truncate is supported */
|
/* File must start empty and grow, check truncate is supported */
|
||||||
ret = blk_truncate(blk, 0, errp);
|
ret = blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
@ -832,7 +832,8 @@ static int vdi_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (image_type == VDI_TYPE_STATIC) {
|
if (image_type == VDI_TYPE_STATIC) {
|
||||||
ret = blk_truncate(blk, offset + blocks * block_size, errp);
|
ret = blk_truncate(blk, offset + blocks * block_size,
|
||||||
|
PREALLOC_MODE_OFF, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_prepend(errp, "Failed to statically allocate %s", filename);
|
error_prepend(errp, "Failed to statically allocate %s", filename);
|
||||||
goto exit;
|
goto exit;
|
||||||
|
@ -1608,12 +1608,13 @@ static int vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
|
|||||||
if (type == VHDX_TYPE_DYNAMIC) {
|
if (type == VHDX_TYPE_DYNAMIC) {
|
||||||
/* All zeroes, so we can just extend the file - the end of the BAT
|
/* All zeroes, so we can just extend the file - the end of the BAT
|
||||||
* is the furthest thing we have written yet */
|
* is the furthest thing we have written yet */
|
||||||
ret = blk_truncate(blk, data_file_offset, errp);
|
ret = blk_truncate(blk, data_file_offset, PREALLOC_MODE_OFF, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
} else if (type == VHDX_TYPE_FIXED) {
|
} else if (type == VHDX_TYPE_FIXED) {
|
||||||
ret = blk_truncate(blk, data_file_offset + image_size, errp);
|
ret = blk_truncate(blk, data_file_offset + image_size,
|
||||||
|
PREALLOC_MODE_OFF, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -1714,7 +1714,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
|
|||||||
blk_set_allow_write_beyond_eof(blk, true);
|
blk_set_allow_write_beyond_eof(blk, true);
|
||||||
|
|
||||||
if (flat) {
|
if (flat) {
|
||||||
ret = blk_truncate(blk, filesize, errp);
|
ret = blk_truncate(blk, filesize, PREALLOC_MODE_OFF, errp);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
magic = cpu_to_be32(VMDK4_MAGIC);
|
magic = cpu_to_be32(VMDK4_MAGIC);
|
||||||
@ -1777,7 +1777,8 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9, errp);
|
ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9,
|
||||||
|
PREALLOC_MODE_OFF, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
@ -2086,7 +2087,7 @@ static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
/* bdrv_pwrite write padding zeros to align to sector, we don't need that
|
/* bdrv_pwrite write padding zeros to align to sector, we don't need that
|
||||||
* for description file */
|
* for description file */
|
||||||
if (desc_offset == 0) {
|
if (desc_offset == 0) {
|
||||||
ret = blk_truncate(new_blk, desc_len, errp);
|
ret = blk_truncate(new_blk, desc_len, PREALLOC_MODE_OFF, errp);
|
||||||
}
|
}
|
||||||
exit:
|
exit:
|
||||||
if (new_blk) {
|
if (new_blk) {
|
||||||
|
@ -858,7 +858,7 @@ static int create_fixed_disk(BlockBackend *blk, uint8_t *buf,
|
|||||||
/* Add footer to total size */
|
/* Add footer to total size */
|
||||||
total_size += HEADER_SIZE;
|
total_size += HEADER_SIZE;
|
||||||
|
|
||||||
ret = blk_truncate(blk, total_size, errp);
|
ret = blk_truncate(blk, total_size, PREALLOC_MODE_OFF, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -2958,7 +2958,7 @@ void qmp_block_resize(bool has_device, const char *device,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bdrv_drained_begin(bs);
|
bdrv_drained_begin(bs);
|
||||||
ret = blk_truncate(blk, size, errp);
|
ret = blk_truncate(blk, size, PREALLOC_MODE_OFF, errp);
|
||||||
bdrv_drained_end(bs);
|
bdrv_drained_end(bs);
|
||||||
|
|
||||||
out:
|
out:
|
||||||
|
@ -223,7 +223,8 @@ int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
|
|||||||
int bytes, BdrvRequestFlags flags);
|
int bytes, BdrvRequestFlags flags);
|
||||||
int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
|
int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
|
||||||
int bytes);
|
int bytes);
|
||||||
int blk_truncate(BlockBackend *blk, int64_t offset, Error **errp);
|
int blk_truncate(BlockBackend *blk, int64_t offset, PreallocMode prealloc,
|
||||||
|
Error **errp);
|
||||||
int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes);
|
int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes);
|
||||||
int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
|
int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
|
||||||
int64_t pos, int size);
|
int64_t pos, int size);
|
||||||
|
@ -3537,7 +3537,7 @@ static int img_resize(int argc, char **argv)
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = blk_truncate(blk, total_size, &err);
|
ret = blk_truncate(blk, total_size, PREALLOC_MODE_OFF, &err);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
qprintf(quiet, "Image resized.\n");
|
qprintf(quiet, "Image resized.\n");
|
||||||
} else {
|
} else {
|
||||||
|
@ -1577,7 +1577,7 @@ static int truncate_f(BlockBackend *blk, int argc, char **argv)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = blk_truncate(blk, offset, &local_err);
|
ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_report_err(local_err);
|
error_report_err(local_err);
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user