[PATCH 3/6] preallocate: rework preallocate_truncate_to_real_size()

Denis V. Lunev via posted 6 patches 4 weeks ago
[PATCH 3/6] preallocate: rework preallocate_truncate_to_real_size()
Posted by Denis V. Lunev via 4 weeks ago
The filter is not enabled if s->data_end is negative. In this case it
would be useless completely to initialize s->file_end inside
preallocate_truncate_to_real_size() without setting s->data_end.
Here are we are going to reset the state and disable the filter as
we are either in the process of switching to the read-only state or
the driver is being closed.

Now the driver is disabled unconditionally even on the error and this is
pretty much correct. In the worst case the image would be a bit longer
and that is all.

The patch also adds redundant check for bs->open_flags into this helper
for the convinience.

Signed-off-by: Denis V. Lunev <den@openvz.org>
CC: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
CC: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
CC: Kevin Wolf <kwolf@redhat.com>
CC: Hanna Reitz <hreitz@redhat.com>
---
 block/preallocate.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/block/preallocate.c b/block/preallocate.c
index d0ed56eecb..65c1ff5d78 100644
--- a/block/preallocate.c
+++ b/block/preallocate.c
@@ -179,12 +179,11 @@ preallocate_truncate_to_real_size(BlockDriverState *bs, Error **errp)
     BDRVPreallocateState *s = bs->opaque;
     int ret;
 
-    if (s->file_end < 0) {
-        s->file_end = bdrv_getlength(bs->file->bs);
-        if (s->file_end < 0) {
-            error_setg_errno(errp, -s->file_end, "Failed to get file length");
-            return s->file_end;
-        }
+    if (!(bs->open_flags & BDRV_O_RDWR)) {
+        return 0;
+    }
+    if (s->data_end < 0) {
+        return 0;
     }
 
     if (s->data_end < s->file_end) {
@@ -192,11 +191,9 @@ preallocate_truncate_to_real_size(BlockDriverState *bs, Error **errp)
                             NULL);
         if (ret < 0) {
             error_setg_errno(errp, -ret, "Failed to drop preallocation");
-            s->file_end = ret;
-            return ret;
         }
-        s->file_end = s->data_end;
     }
+    s->data_end = s->file_end = s->zero_start = -EINVAL;
 
     return 0;
 }
@@ -211,9 +208,7 @@ static void preallocate_close(BlockDriverState *bs)
     qemu_bh_cancel(s->drop_resize_bh);
     qemu_bh_delete(s->drop_resize_bh);
 
-    if (s->data_end >= 0) {
-        preallocate_truncate_to_real_size(bs, NULL);
-    }
+    preallocate_truncate_to_real_size(bs, NULL);
 }
 
 
@@ -528,8 +523,6 @@ preallocate_drop_resize(BlockDriverState *bs, Error **errp)
      * change the child, so mark all states invalid. We'll regain control if a
      * parent requests write access again.
      */
-    s->data_end = s->file_end = s->zero_start = -EINVAL;
-
     bdrv_child_refresh_perms(bs, bs->file, NULL);
 
     return 0;
-- 
2.45.2