Skip to content

Commit

Permalink
block: Add errp to b{lk,drv}_truncate()
Browse files Browse the repository at this point in the history
For one thing, this allows us to drop the error message generation from
qemu-img.c and blockdev.c and instead have it unified in
bdrv_truncate().

Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20170328205129.15138-3-mreitz@redhat.com
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
  • Loading branch information
XanClic committed Apr 28, 2017
1 parent 55b9392 commit ed3d2ec
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 90 deletions.
16 changes: 12 additions & 4 deletions block.c
Expand Up @@ -3307,27 +3307,35 @@ int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
/**
* Truncate file to 'offset' bytes (needed only for file protocols)
*/
int bdrv_truncate(BdrvChild *child, int64_t offset)
int bdrv_truncate(BdrvChild *child, int64_t offset, Error **errp)
{
BlockDriverState *bs = child->bs;
BlockDriver *drv = bs->drv;
int ret;

assert(child->perm & BLK_PERM_RESIZE);

if (!drv)
if (!drv) {
error_setg(errp, "No medium inserted");
return -ENOMEDIUM;
if (!drv->bdrv_truncate)
}
if (!drv->bdrv_truncate) {
error_setg(errp, "Image format driver does not support resize");
return -ENOTSUP;
if (bs->read_only)
}
if (bs->read_only) {
error_setg(errp, "Image is read-only");
return -EACCES;
}

ret = drv->bdrv_truncate(bs, offset);
if (ret == 0) {
ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
bdrv_dirty_bitmap_truncate(bs);
bdrv_parent_cb_resize(bs);
++bs->write_gen;
} else {
error_setg_errno(errp, -ret, "Failed to resize image");
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion block/blkdebug.c
Expand Up @@ -661,7 +661,7 @@ static int64_t blkdebug_getlength(BlockDriverState *bs)

static int blkdebug_truncate(BlockDriverState *bs, int64_t offset)
{
return bdrv_truncate(bs->file, offset);
return bdrv_truncate(bs->file, offset, NULL);
}

static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
Expand Down
5 changes: 3 additions & 2 deletions block/block-backend.c
Expand Up @@ -1746,13 +1746,14 @@ int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
BDRV_REQ_WRITE_COMPRESSED);
}

int blk_truncate(BlockBackend *blk, int64_t offset)
int blk_truncate(BlockBackend *blk, int64_t offset, Error **errp)
{
if (!blk_is_available(blk)) {
error_setg(errp, "No medium inserted");
return -ENOMEDIUM;
}

return bdrv_truncate(blk->root, offset);
return bdrv_truncate(blk->root, offset, errp);
}

static void blk_pdiscard_entry(void *opaque)
Expand Down
5 changes: 3 additions & 2 deletions block/commit.c
Expand Up @@ -151,7 +151,7 @@ static void coroutine_fn commit_run(void *opaque)
}

if (base_len < s->common.len) {
ret = blk_truncate(s->base, s->common.len);
ret = blk_truncate(s->base, s->common.len, NULL);
if (ret) {
goto out;
}
Expand Down Expand Up @@ -511,8 +511,9 @@ int bdrv_commit(BlockDriverState *bs)
* grow the backing file image if possible. If not possible,
* we must return an error */
if (length > backing_length) {
ret = blk_truncate(backing, length);
ret = blk_truncate(backing, length, &local_err);
if (ret < 0) {
error_report_err(local_err);
goto ro_cleanup;
}
}
Expand Down
2 changes: 1 addition & 1 deletion block/crypto.c
Expand Up @@ -389,7 +389,7 @@ static int block_crypto_truncate(BlockDriverState *bs, int64_t offset)

offset += payload_offset;

return bdrv_truncate(bs->file, offset);
return bdrv_truncate(bs->file, offset, NULL);
}

static void block_crypto_close(BlockDriverState *bs)
Expand Down
2 changes: 1 addition & 1 deletion block/mirror.c
Expand Up @@ -724,7 +724,7 @@ static void coroutine_fn mirror_run(void *opaque)
}

if (s->bdev_length > base_length) {
ret = blk_truncate(s->target, s->bdev_length);
ret = blk_truncate(s->target, s->bdev_length, NULL);
if (ret < 0) {
goto immediate_exit;
}
Expand Down
13 changes: 8 additions & 5 deletions block/parallels.c
Expand Up @@ -223,7 +223,8 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
space << BDRV_SECTOR_BITS, 0);
} else {
ret = bdrv_truncate(bs->file,
(s->data_end + space) << BDRV_SECTOR_BITS);
(s->data_end + space) << BDRV_SECTOR_BITS,
NULL);
}
if (ret < 0) {
return ret;
Expand Down Expand Up @@ -456,8 +457,10 @@ static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res,
size - res->image_end_offset);
res->leaks += count;
if (fix & BDRV_FIX_LEAKS) {
ret = bdrv_truncate(bs->file, res->image_end_offset);
Error *local_err = NULL;
ret = bdrv_truncate(bs->file, res->image_end_offset, &local_err);
if (ret < 0) {
error_report_err(local_err);
res->check_errors++;
return ret;
}
Expand Down Expand Up @@ -504,7 +507,7 @@ static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)

blk_set_allow_write_beyond_eof(file, true);

ret = blk_truncate(file, 0);
ret = blk_truncate(file, 0, errp);
if (ret < 0) {
goto exit;
}
Expand Down Expand Up @@ -696,7 +699,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
}

if (!(flags & BDRV_O_RESIZE) || !bdrv_has_zero_init(bs->file->bs) ||
bdrv_truncate(bs->file, bdrv_getlength(bs->file->bs)) != 0) {
bdrv_truncate(bs->file, bdrv_getlength(bs->file->bs), NULL) != 0) {
s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
}

Expand Down Expand Up @@ -739,7 +742,7 @@ static void parallels_close(BlockDriverState *bs)
}

if (bs->open_flags & BDRV_O_RDWR) {
bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS);
bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, NULL);
}

g_free(s->bat_dirty_bmap);
Expand Down
6 changes: 3 additions & 3 deletions block/qcow.c
Expand Up @@ -473,7 +473,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
/* round to cluster size */
cluster_offset = (cluster_offset + s->cluster_size - 1) &
~(s->cluster_size - 1);
bdrv_truncate(bs->file, cluster_offset + s->cluster_size);
bdrv_truncate(bs->file, cluster_offset + s->cluster_size, NULL);
/* if encrypted, we must initialize the cluster
content which won't be written */
if (bs->encrypted &&
Expand Down Expand Up @@ -833,7 +833,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)

blk_set_allow_write_beyond_eof(qcow_blk, true);

ret = blk_truncate(qcow_blk, 0);
ret = blk_truncate(qcow_blk, 0, errp);
if (ret < 0) {
goto exit;
}
Expand Down Expand Up @@ -916,7 +916,7 @@ static int qcow_make_empty(BlockDriverState *bs)
if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table,
l1_length) < 0)
return -1;
ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, NULL);
if (ret < 0)
return ret;

Expand Down
5 changes: 4 additions & 1 deletion block/qcow2-refcount.c
Expand Up @@ -1728,14 +1728,17 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,

if (fix & BDRV_FIX_ERRORS) {
int64_t new_nb_clusters;
Error *local_err = NULL;

if (offset > INT64_MAX - s->cluster_size) {
ret = -EINVAL;
goto resize_fail;
}

ret = bdrv_truncate(bs->file, offset + s->cluster_size);
ret = bdrv_truncate(bs->file, offset + s->cluster_size,
&local_err);
if (ret < 0) {
error_report_err(local_err);
goto resize_fail;
}
size = bdrv_getlength(bs->file->bs);
Expand Down
14 changes: 9 additions & 5 deletions block/qcow2.c
Expand Up @@ -2294,9 +2294,9 @@ 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 */
ret = blk_truncate(blk, total_size);
ret = blk_truncate(blk, total_size, errp);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not resize image");
error_prepend(errp, "Could not resize image: ");
goto out;
}

Expand Down Expand Up @@ -2584,7 +2584,7 @@ qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
/* align end of file to a sector boundary to ease reading with
sector based I/Os */
cluster_offset = bdrv_getlength(bs->file->bs);
return bdrv_truncate(bs->file, cluster_offset);
return bdrv_truncate(bs->file, cluster_offset, NULL);
}

buf = qemu_blockalign(bs, s->cluster_size);
Expand Down Expand Up @@ -2674,6 +2674,7 @@ qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
static int make_completely_empty(BlockDriverState *bs)
{
BDRVQcow2State *s = bs->opaque;
Error *local_err = NULL;
int ret, l1_clusters;
int64_t offset;
uint64_t *new_reftable = NULL;
Expand Down Expand Up @@ -2798,8 +2799,10 @@ static int make_completely_empty(BlockDriverState *bs)
goto fail;
}

ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size);
ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size,
&local_err);
if (ret < 0) {
error_report_err(local_err);
goto fail;
}

Expand Down Expand Up @@ -3273,9 +3276,10 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
return ret;
}

ret = blk_truncate(blk, new_size);
ret = blk_truncate(blk, new_size, &local_err);
blk_unref(blk);
if (ret < 0) {
error_report_err(local_err);
return ret;
}
}
Expand Down
2 changes: 1 addition & 1 deletion block/qed.c
Expand Up @@ -635,7 +635,7 @@ static int qed_create(const char *filename, uint32_t cluster_size,
blk_set_allow_write_beyond_eof(blk, true);

/* File must start empty and grow, check truncate is supported */
ret = blk_truncate(blk, 0);
ret = blk_truncate(blk, 0, errp);
if (ret < 0) {
goto out;
}
Expand Down
2 changes: 1 addition & 1 deletion block/raw-format.c
Expand Up @@ -341,7 +341,7 @@ static int raw_truncate(BlockDriverState *bs, int64_t offset)

s->size = offset;
offset += s->offset;
return bdrv_truncate(bs->file, offset);
return bdrv_truncate(bs->file, offset, NULL);
}

static int raw_media_changed(BlockDriverState *bs)
Expand Down
4 changes: 2 additions & 2 deletions block/vdi.c
Expand Up @@ -832,9 +832,9 @@ static int vdi_create(const char *filename, QemuOpts *opts, Error **errp)
}

if (image_type == VDI_TYPE_STATIC) {
ret = blk_truncate(blk, offset + blocks * block_size);
ret = blk_truncate(blk, offset + blocks * block_size, errp);
if (ret < 0) {
error_setg(errp, "Failed to statically allocate %s", filename);
error_prepend(errp, "Failed to statically allocate %s", filename);
goto exit;
}
}
Expand Down
2 changes: 1 addition & 1 deletion block/vhdx-log.c
Expand Up @@ -548,7 +548,7 @@ static int vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s,
if (new_file_size % (1024*1024)) {
/* round up to nearest 1MB boundary */
new_file_size = ((new_file_size >> 20) + 1) << 20;
bdrv_truncate(bs->file, new_file_size);
bdrv_truncate(bs->file, new_file_size, NULL);
}
}
qemu_vfree(desc_entries);
Expand Down
10 changes: 3 additions & 7 deletions block/vhdx.c
Expand Up @@ -1171,7 +1171,7 @@ static int vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s,
/* per the spec, the address for a block is in units of 1MB */
*new_offset = ROUND_UP(*new_offset, 1024 * 1024);

return bdrv_truncate(bs->file, *new_offset + s->block_size);
return bdrv_truncate(bs->file, *new_offset + s->block_size, NULL);
}

/*
Expand Down Expand Up @@ -1607,17 +1607,13 @@ static int vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
if (type == VHDX_TYPE_DYNAMIC) {
/* All zeroes, so we can just extend the file - the end of the BAT
* is the furthest thing we have written yet */
ret = blk_truncate(blk, data_file_offset);
ret = blk_truncate(blk, data_file_offset, errp);
if (ret < 0) {
error_setg_errno(errp, -ret,
"Failed to resize the underlying file");
goto exit;
}
} else if (type == VHDX_TYPE_FIXED) {
ret = blk_truncate(blk, data_file_offset + image_size);
ret = blk_truncate(blk, data_file_offset + image_size, errp);
if (ret < 0) {
error_setg_errno(errp, -ret,
"Failed to resize the underlying file");
goto exit;
}
} else {
Expand Down
13 changes: 3 additions & 10 deletions block/vmdk.c
Expand Up @@ -1714,10 +1714,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
blk_set_allow_write_beyond_eof(blk, true);

if (flat) {
ret = blk_truncate(blk, filesize);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not truncate file");
}
ret = blk_truncate(blk, filesize, errp);
goto exit;
}
magic = cpu_to_be32(VMDK4_MAGIC);
Expand Down Expand Up @@ -1780,9 +1777,8 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
goto exit;
}

ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9);
ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9, errp);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not truncate file");
goto exit;
}

Expand Down Expand Up @@ -2090,10 +2086,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
* for description file */
if (desc_offset == 0) {
ret = blk_truncate(new_blk, desc_len);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not truncate file");
}
ret = blk_truncate(new_blk, desc_len, errp);
}
exit:
if (new_blk) {
Expand Down
13 changes: 7 additions & 6 deletions block/vpc.c
Expand Up @@ -851,20 +851,21 @@ static int create_dynamic_disk(BlockBackend *blk, uint8_t *buf,
}

static int create_fixed_disk(BlockBackend *blk, uint8_t *buf,
int64_t total_size)
int64_t total_size, Error **errp)
{
int ret;

/* Add footer to total size */
total_size += HEADER_SIZE;

ret = blk_truncate(blk, total_size);
ret = blk_truncate(blk, total_size, errp);
if (ret < 0) {
return ret;
}

ret = blk_pwrite(blk, total_size - HEADER_SIZE, buf, HEADER_SIZE, 0);
if (ret < 0) {
error_setg_errno(errp, -ret, "Unable to write VHD header");
return ret;
}

Expand Down Expand Up @@ -996,11 +997,11 @@ static int vpc_create(const char *filename, QemuOpts *opts, Error **errp)

if (disk_type == VHD_DYNAMIC) {
ret = create_dynamic_disk(blk, buf, total_sectors);
if (ret < 0) {
error_setg(errp, "Unable to create or write VHD header");
}
} else {
ret = create_fixed_disk(blk, buf, total_size);
}
if (ret < 0) {
error_setg(errp, "Unable to create or write VHD header");
ret = create_fixed_disk(blk, buf, total_size, errp);
}

out:
Expand Down

0 comments on commit ed3d2ec

Please sign in to comment.