block: Add flags to bdrv(_co)_truncate()
Now that block drivers can support flags for .bdrv_co_truncate, expose the parameter in the node level interfaces bdrv_co_truncate() and bdrv_truncate(). Signed-off-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Reviewed-by: Alberto Garcia <berto@igalia.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Message-Id: <20200424125448.63318-3-kwolf@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
92b92799dc
commit
7b8e485742
@ -2144,7 +2144,7 @@ int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,
|
|||||||
return -ENOMEDIUM;
|
return -ENOMEDIUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bdrv_truncate(blk->root, offset, exact, prealloc, errp);
|
return bdrv_truncate(blk->root, offset, exact, prealloc, 0, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
|
int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
|
||||||
|
@ -313,7 +313,7 @@ block_crypto_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
|
|||||||
|
|
||||||
offset += payload_offset;
|
offset += payload_offset;
|
||||||
|
|
||||||
return bdrv_co_truncate(bs->file, offset, exact, prealloc, errp);
|
return bdrv_co_truncate(bs->file, offset, exact, prealloc, 0, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void block_crypto_close(BlockDriverState *bs)
|
static void block_crypto_close(BlockDriverState *bs)
|
||||||
|
12
block/io.c
12
block/io.c
@ -3339,12 +3339,12 @@ static void bdrv_parent_cb_resize(BlockDriverState *bs)
|
|||||||
* 'offset' bytes in length.
|
* 'offset' bytes in length.
|
||||||
*/
|
*/
|
||||||
int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
|
int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
|
||||||
PreallocMode prealloc, Error **errp)
|
PreallocMode prealloc, BdrvRequestFlags flags,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs = child->bs;
|
BlockDriverState *bs = child->bs;
|
||||||
BlockDriver *drv = bs->drv;
|
BlockDriver *drv = bs->drv;
|
||||||
BdrvTrackedRequest req;
|
BdrvTrackedRequest req;
|
||||||
BdrvRequestFlags flags = 0;
|
|
||||||
int64_t old_size, new_bytes;
|
int64_t old_size, new_bytes;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
@ -3402,7 +3402,7 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
|
|||||||
}
|
}
|
||||||
ret = drv->bdrv_co_truncate(bs, offset, exact, prealloc, flags, errp);
|
ret = drv->bdrv_co_truncate(bs, offset, exact, prealloc, flags, errp);
|
||||||
} else if (bs->file && drv->is_filter) {
|
} else if (bs->file && drv->is_filter) {
|
||||||
ret = bdrv_co_truncate(bs->file, offset, exact, prealloc, errp);
|
ret = bdrv_co_truncate(bs->file, offset, exact, prealloc, flags, errp);
|
||||||
} else {
|
} else {
|
||||||
error_setg(errp, "Image format driver does not support resize");
|
error_setg(errp, "Image format driver does not support resize");
|
||||||
ret = -ENOTSUP;
|
ret = -ENOTSUP;
|
||||||
@ -3435,6 +3435,7 @@ typedef struct TruncateCo {
|
|||||||
int64_t offset;
|
int64_t offset;
|
||||||
bool exact;
|
bool exact;
|
||||||
PreallocMode prealloc;
|
PreallocMode prealloc;
|
||||||
|
BdrvRequestFlags flags;
|
||||||
Error **errp;
|
Error **errp;
|
||||||
int ret;
|
int ret;
|
||||||
} TruncateCo;
|
} TruncateCo;
|
||||||
@ -3443,12 +3444,12 @@ static void coroutine_fn bdrv_truncate_co_entry(void *opaque)
|
|||||||
{
|
{
|
||||||
TruncateCo *tco = opaque;
|
TruncateCo *tco = opaque;
|
||||||
tco->ret = bdrv_co_truncate(tco->child, tco->offset, tco->exact,
|
tco->ret = bdrv_co_truncate(tco->child, tco->offset, tco->exact,
|
||||||
tco->prealloc, tco->errp);
|
tco->prealloc, tco->flags, tco->errp);
|
||||||
aio_wait_kick();
|
aio_wait_kick();
|
||||||
}
|
}
|
||||||
|
|
||||||
int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
|
int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
|
||||||
PreallocMode prealloc, Error **errp)
|
PreallocMode prealloc, BdrvRequestFlags flags, Error **errp)
|
||||||
{
|
{
|
||||||
Coroutine *co;
|
Coroutine *co;
|
||||||
TruncateCo tco = {
|
TruncateCo tco = {
|
||||||
@ -3456,6 +3457,7 @@ int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
|
|||||||
.offset = offset,
|
.offset = offset,
|
||||||
.exact = exact,
|
.exact = exact,
|
||||||
.prealloc = prealloc,
|
.prealloc = prealloc,
|
||||||
|
.flags = flags,
|
||||||
.errp = errp,
|
.errp = errp,
|
||||||
.ret = NOT_DONE,
|
.ret = NOT_DONE,
|
||||||
};
|
};
|
||||||
|
@ -203,7 +203,7 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
|
|||||||
} else {
|
} else {
|
||||||
ret = bdrv_truncate(bs->file,
|
ret = bdrv_truncate(bs->file,
|
||||||
(s->data_end + space) << BDRV_SECTOR_BITS,
|
(s->data_end + space) << BDRV_SECTOR_BITS,
|
||||||
false, PREALLOC_MODE_OFF, NULL);
|
false, PREALLOC_MODE_OFF, 0, NULL);
|
||||||
}
|
}
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
@ -493,7 +493,7 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
|
|||||||
* That means we have to pass exact=true.
|
* That means we have to pass exact=true.
|
||||||
*/
|
*/
|
||||||
ret = bdrv_truncate(bs->file, res->image_end_offset, true,
|
ret = bdrv_truncate(bs->file, res->image_end_offset, true,
|
||||||
PREALLOC_MODE_OFF, &local_err);
|
PREALLOC_MODE_OFF, 0, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_report_err(local_err);
|
error_report_err(local_err);
|
||||||
res->check_errors++;
|
res->check_errors++;
|
||||||
@ -889,7 +889,7 @@ static void parallels_close(BlockDriverState *bs)
|
|||||||
|
|
||||||
/* errors are ignored, so we might as well pass exact=true */
|
/* errors are ignored, so we might as well pass exact=true */
|
||||||
bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, true,
|
bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, true,
|
||||||
PREALLOC_MODE_OFF, NULL);
|
PREALLOC_MODE_OFF, 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(s->bat_dirty_bmap);
|
g_free(s->bat_dirty_bmap);
|
||||||
|
@ -480,7 +480,7 @@ static int get_cluster_offset(BlockDriverState *bs,
|
|||||||
return -E2BIG;
|
return -E2BIG;
|
||||||
}
|
}
|
||||||
ret = bdrv_truncate(bs->file, cluster_offset + s->cluster_size,
|
ret = bdrv_truncate(bs->file, cluster_offset + s->cluster_size,
|
||||||
false, PREALLOC_MODE_OFF, NULL);
|
false, PREALLOC_MODE_OFF, 0, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -1035,7 +1035,7 @@ static int qcow_make_empty(BlockDriverState *bs)
|
|||||||
l1_length) < 0)
|
l1_length) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, false,
|
ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, false,
|
||||||
PREALLOC_MODE_OFF, NULL);
|
PREALLOC_MODE_OFF, 0, NULL);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
@ -2018,7 +2018,7 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
|
|||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_truncate(bs->file, offset + s->cluster_size, false,
|
ret = bdrv_truncate(bs->file, offset + s->cluster_size, false,
|
||||||
PREALLOC_MODE_OFF, &local_err);
|
PREALLOC_MODE_OFF, 0, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_report_err(local_err);
|
error_report_err(local_err);
|
||||||
goto resize_fail;
|
goto resize_fail;
|
||||||
|
@ -3095,7 +3095,7 @@ static int coroutine_fn preallocate_co(BlockDriverState *bs, uint64_t offset,
|
|||||||
mode = PREALLOC_MODE_OFF;
|
mode = PREALLOC_MODE_OFF;
|
||||||
}
|
}
|
||||||
ret = bdrv_co_truncate(s->data_file, host_offset + cur_bytes, false,
|
ret = bdrv_co_truncate(s->data_file, host_offset + cur_bytes, false,
|
||||||
mode, errp);
|
mode, 0, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -4061,7 +4061,7 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
|
|||||||
* always fulfilled, so there is no need to pass it on.)
|
* always fulfilled, so there is no need to pass it on.)
|
||||||
*/
|
*/
|
||||||
bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size,
|
bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size,
|
||||||
false, PREALLOC_MODE_OFF, &local_err);
|
false, PREALLOC_MODE_OFF, 0, &local_err);
|
||||||
if (local_err) {
|
if (local_err) {
|
||||||
warn_reportf_err(local_err,
|
warn_reportf_err(local_err,
|
||||||
"Failed to truncate the tail of the image: ");
|
"Failed to truncate the tail of the image: ");
|
||||||
@ -4083,7 +4083,8 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
|
|||||||
* file should be resized to the exact target size, too,
|
* file should be resized to the exact target size, too,
|
||||||
* so we pass @exact here.
|
* so we pass @exact here.
|
||||||
*/
|
*/
|
||||||
ret = bdrv_co_truncate(s->data_file, offset, exact, prealloc, errp);
|
ret = bdrv_co_truncate(s->data_file, offset, exact, prealloc, 0,
|
||||||
|
errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@ -4169,7 +4170,8 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
|
|||||||
new_file_size = allocation_start +
|
new_file_size = allocation_start +
|
||||||
nb_new_data_clusters * s->cluster_size;
|
nb_new_data_clusters * s->cluster_size;
|
||||||
/* Image file grows, so @exact does not matter */
|
/* Image file grows, so @exact does not matter */
|
||||||
ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc, errp);
|
ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc, 0,
|
||||||
|
errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_prepend(errp, "Failed to resize underlying file: ");
|
error_prepend(errp, "Failed to resize underlying file: ");
|
||||||
qcow2_free_clusters(bs, allocation_start,
|
qcow2_free_clusters(bs, allocation_start,
|
||||||
@ -4348,7 +4350,8 @@ qcow2_co_pwritev_compressed_part(BlockDriverState *bs,
|
|||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
return bdrv_co_truncate(bs->file, len, false, PREALLOC_MODE_OFF, NULL);
|
return bdrv_co_truncate(bs->file, len, false, PREALLOC_MODE_OFF, 0,
|
||||||
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offset_into_cluster(s, offset)) {
|
if (offset_into_cluster(s, offset)) {
|
||||||
@ -4563,7 +4566,7 @@ static int make_completely_empty(BlockDriverState *bs)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size, false,
|
ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size, false,
|
||||||
PREALLOC_MODE_OFF, &local_err);
|
PREALLOC_MODE_OFF, 0, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_report_err(local_err);
|
error_report_err(local_err);
|
||||||
goto fail;
|
goto fail;
|
||||||
|
@ -387,7 +387,7 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
|
|||||||
|
|
||||||
s->size = offset;
|
s->size = offset;
|
||||||
offset += s->offset;
|
offset += s->offset;
|
||||||
return bdrv_co_truncate(bs->file, offset, exact, prealloc, errp);
|
return bdrv_co_truncate(bs->file, offset, exact, prealloc, 0, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void raw_eject(BlockDriverState *bs, bool eject_flag)
|
static void raw_eject(BlockDriverState *bs, bool eject_flag)
|
||||||
|
@ -558,7 +558,7 @@ static int vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s,
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
ret = bdrv_truncate(bs->file, new_file_size, false,
|
ret = bdrv_truncate(bs->file, new_file_size, false,
|
||||||
PREALLOC_MODE_OFF, NULL);
|
PREALLOC_MODE_OFF, 0, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -1264,7 +1264,7 @@ static int vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s,
|
|||||||
}
|
}
|
||||||
|
|
||||||
return bdrv_truncate(bs->file, *new_offset + s->block_size, false,
|
return bdrv_truncate(bs->file, *new_offset + s->block_size, false,
|
||||||
PREALLOC_MODE_OFF, NULL);
|
PREALLOC_MODE_OFF, 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -2077,7 +2077,7 @@ vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
|
|||||||
}
|
}
|
||||||
length = QEMU_ALIGN_UP(length, BDRV_SECTOR_SIZE);
|
length = QEMU_ALIGN_UP(length, BDRV_SECTOR_SIZE);
|
||||||
ret = bdrv_truncate(s->extents[i].file, length, false,
|
ret = bdrv_truncate(s->extents[i].file, length, false,
|
||||||
PREALLOC_MODE_OFF, NULL);
|
PREALLOC_MODE_OFF, 0, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -339,9 +339,10 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
|
|||||||
void bdrv_refresh_filename(BlockDriverState *bs);
|
void bdrv_refresh_filename(BlockDriverState *bs);
|
||||||
|
|
||||||
int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
|
int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
|
||||||
PreallocMode prealloc, Error **errp);
|
PreallocMode prealloc, BdrvRequestFlags flags,
|
||||||
|
Error **errp);
|
||||||
int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
|
int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
|
||||||
PreallocMode prealloc, Error **errp);
|
PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
|
||||||
|
|
||||||
int64_t bdrv_nb_sectors(BlockDriverState *bs);
|
int64_t bdrv_nb_sectors(BlockDriverState *bs);
|
||||||
int64_t bdrv_getlength(BlockDriverState *bs);
|
int64_t bdrv_getlength(BlockDriverState *bs);
|
||||||
|
@ -186,18 +186,18 @@ static void test_sync_op_truncate(BdrvChild *c)
|
|||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
/* Normal success path */
|
/* Normal success path */
|
||||||
ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, NULL);
|
ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, 0, NULL);
|
||||||
g_assert_cmpint(ret, ==, 0);
|
g_assert_cmpint(ret, ==, 0);
|
||||||
|
|
||||||
/* Early error: Negative offset */
|
/* Early error: Negative offset */
|
||||||
ret = bdrv_truncate(c, -2, false, PREALLOC_MODE_OFF, NULL);
|
ret = bdrv_truncate(c, -2, false, PREALLOC_MODE_OFF, 0, NULL);
|
||||||
g_assert_cmpint(ret, ==, -EINVAL);
|
g_assert_cmpint(ret, ==, -EINVAL);
|
||||||
|
|
||||||
/* Error: Read-only image */
|
/* Error: Read-only image */
|
||||||
c->bs->read_only = true;
|
c->bs->read_only = true;
|
||||||
c->bs->open_flags &= ~BDRV_O_RDWR;
|
c->bs->open_flags &= ~BDRV_O_RDWR;
|
||||||
|
|
||||||
ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, NULL);
|
ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, 0, NULL);
|
||||||
g_assert_cmpint(ret, ==, -EACCES);
|
g_assert_cmpint(ret, ==, -EACCES);
|
||||||
|
|
||||||
c->bs->read_only = false;
|
c->bs->read_only = false;
|
||||||
|
Loading…
Reference in New Issue
Block a user