[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 02/28] preallocate: Factor out preallocate_truncate_to_real_size()
From: |
Kevin Wolf |
Subject: |
[PULL 02/28] preallocate: Factor out preallocate_truncate_to_real_size() |
Date: |
Fri, 15 Sep 2023 16:43:18 +0200 |
It's essentially the same code in preallocate_check_perm() and
preallocate_close(), except that the latter ignores errors.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-ID: <20230911094620.45040-3-kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/preallocate.c | 48 +++++++++++++++++++++------------------------
1 file changed, 22 insertions(+), 26 deletions(-)
diff --git a/block/preallocate.c b/block/preallocate.c
index 3d0f621003..3173d80534 100644
--- a/block/preallocate.c
+++ b/block/preallocate.c
@@ -162,26 +162,39 @@ static int preallocate_open(BlockDriverState *bs, QDict
*options, int flags,
return 0;
}
-static void preallocate_close(BlockDriverState *bs)
+static int preallocate_truncate_to_real_size(BlockDriverState *bs, Error
**errp)
{
- int ret;
BDRVPreallocateState *s = bs->opaque;
-
- if (s->data_end < 0) {
- return;
- }
+ int ret;
if (s->file_end < 0) {
s->file_end = bdrv_getlength(bs->file->bs);
if (s->file_end < 0) {
- return;
+ error_setg_errno(errp, -s->file_end, "Failed to get file length");
+ return s->file_end;
}
}
if (s->data_end < s->file_end) {
ret = bdrv_truncate(bs->file, s->data_end, true, PREALLOC_MODE_OFF, 0,
NULL);
- s->file_end = ret < 0 ? ret : s->data_end;
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Failed to drop preallocation");
+ s->file_end = ret;
+ return ret;
+ }
+ s->file_end = s->data_end;
+ }
+
+ return 0;
+}
+
+static void preallocate_close(BlockDriverState *bs)
+{
+ BDRVPreallocateState *s = bs->opaque;
+
+ if (s->data_end >= 0) {
+ preallocate_truncate_to_real_size(bs, NULL);
}
}
@@ -473,24 +486,7 @@ static int preallocate_check_perm(BlockDriverState *bs,
* We should truncate in check_perm, as in set_perm bs->file->perm will
* be already changed, and we should not violate it.
*/
- if (s->file_end < 0) {
- s->file_end = bdrv_getlength(bs->file->bs);
- if (s->file_end < 0) {
- error_setg(errp, "Failed to get file length");
- return s->file_end;
- }
- }
-
- if (s->data_end < s->file_end) {
- int ret = bdrv_truncate(bs->file, s->data_end, true,
- PREALLOC_MODE_OFF, 0, NULL);
- if (ret < 0) {
- error_setg(errp, "Failed to drop preallocation");
- s->file_end = ret;
- return ret;
- }
- s->file_end = s->data_end;
- }
+ return preallocate_truncate_to_real_size(bs, errp);
}
return 0;
--
2.41.0
- [PULL 00/28] Block layer patches, Kevin Wolf, 2023/09/15
- [PULL 02/28] preallocate: Factor out preallocate_truncate_to_real_size(),
Kevin Wolf <=
- [PULL 01/28] block: Remove unused BlockReopenQueueEntry.perms_checked, Kevin Wolf, 2023/09/15
- [PULL 06/28] block-coroutine-wrapper: Add no_co_wrapper_bdrv_wrlock functions, Kevin Wolf, 2023/09/15
- [PULL 03/28] preallocate: Don't poll during permission updates, Kevin Wolf, 2023/09/15
- [PULL 10/28] block: Mark bdrv_attach_child_common() GRAPH_WRLOCK, Kevin Wolf, 2023/09/15
- [PULL 04/28] block: Take AioContext lock for bdrv_append() more consistently, Kevin Wolf, 2023/09/15
- [PULL 05/28] block: Introduce bdrv_schedule_unref(), Kevin Wolf, 2023/09/15
- [PULL 12/28] block: Mark bdrv_attach_child() GRAPH_WRLOCK, Kevin Wolf, 2023/09/15
- [PULL 08/28] block: Mark bdrv_replace_child_noperm() GRAPH_WRLOCK, Kevin Wolf, 2023/09/15
- [PULL 18/28] block: Take graph rdlock in bdrv_change_aio_context(), Kevin Wolf, 2023/09/15
- [PULL 17/28] block: Take graph rdlock in bdrv_drop_intermediate(), Kevin Wolf, 2023/09/15