[Qemu-devel] [PATCH for-2.10 14/16] block/qcow2: falloc/full preallocating growth

Max Reitz posted 16 patches 8 years, 7 months ago
There is a newer version of this series
[Qemu-devel] [PATCH for-2.10 14/16] block/qcow2: falloc/full preallocating growth
Posted by Max Reitz 8 years, 7 months ago
Implement the preallocation modes falloc and full for growing qcow2
images.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/qcow2.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/block/qcow2.c b/block/qcow2.c
index 80fb815b15..b6b08d70da 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2604,7 +2604,9 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset,
     int64_t new_l1_size;
     int ret;
 
-    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA) {
+    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
+        prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
+    {
         error_setg(errp, "Unsupported preallocation mode '%s'",
                    PreallocMode_lookup[prealloc]);
         return -ENOTSUP;
@@ -2649,6 +2651,38 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset,
         }
         break;
 
+    case PREALLOC_MODE_FALLOC:
+    case PREALLOC_MODE_FULL:
+    {
+        uint64_t additional_size = qcow2_calc_size_usage(bs, old_length, offset,
+                                                         s->cluster_bits,
+                                                         s->refcount_order);
+        int64_t old_file_length = bdrv_getlength(bs->file->bs);
+        if (old_file_length < 0) {
+            error_setg(errp, "Failed to inquire file length");
+            return old_file_length;
+        }
+
+        old_file_length = ROUND_UP(old_file_length, s->cluster_size);
+
+        ret = bdrv_truncate(bs->file, old_file_length + additional_size,
+                            prealloc, errp);
+        if (ret < 0) {
+            error_prepend(errp, "Failed to resize underlying file");
+            return ret;
+        }
+
+        /* Ensure that preallocation is done in the preallocated area */
+        s->free_cluster_index = old_file_length / s->cluster_size;
+        ret = preallocate(bs, old_length, offset);
+        if (ret < 0) {
+            error_setg_errno(errp, -ret, "Preallocation failed, image may now "
+                             "occupy more space than necessary");
+            return ret;
+        }
+        break;
+    }
+
     default:
         g_assert_not_reached();
     }
-- 
2.12.0


Re: [Qemu-devel] [Qemu-block] [PATCH for-2.10 14/16] block/qcow2: falloc/full preallocating growth
Posted by Stefan Hajnoczi 8 years, 7 months ago
On Mon, Mar 13, 2017 at 10:41:15PM +0100, Max Reitz wrote:
> Implement the preallocation modes falloc and full for growing qcow2
> images.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  block/qcow2.c | 36 +++++++++++++++++++++++++++++++++++-
>  1 file changed, 35 insertions(+), 1 deletion(-)
> 
> diff --git a/block/qcow2.c b/block/qcow2.c
> index 80fb815b15..b6b08d70da 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c
> @@ -2604,7 +2604,9 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset,
>      int64_t new_l1_size;
>      int ret;
>  
> -    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA) {
> +    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
> +        prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)

Now all cases are covered so this if statement can be dropped.  If you
are worried about new preallocation modes being added in the future then
the error_setg() can be moved into the switch statement's default case.
Re: [Qemu-devel] [Qemu-block] [PATCH for-2.10 14/16] block/qcow2: falloc/full preallocating growth
Posted by Max Reitz 8 years, 7 months ago
On 20.03.2017 12:29, Stefan Hajnoczi wrote:
> On Mon, Mar 13, 2017 at 10:41:15PM +0100, Max Reitz wrote:
>> Implement the preallocation modes falloc and full for growing qcow2
>> images.
>>
>> Signed-off-by: Max Reitz <mreitz@redhat.com>
>> ---
>>  block/qcow2.c | 36 +++++++++++++++++++++++++++++++++++-
>>  1 file changed, 35 insertions(+), 1 deletion(-)
>>
>> diff --git a/block/qcow2.c b/block/qcow2.c
>> index 80fb815b15..b6b08d70da 100644
>> --- a/block/qcow2.c
>> +++ b/block/qcow2.c
>> @@ -2604,7 +2604,9 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset,
>>      int64_t new_l1_size;
>>      int ret;
>>  
>> -    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA) {
>> +    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
>> +        prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
> 
> Now all cases are covered so this if statement can be dropped.  If you
> are worried about new preallocation modes being added in the future then
> the error_setg() can be moved into the switch statement's default case.

No, because the switch comes after we have already grown the L1 table.
That wouldn't be so nice.

Max

Re: [Qemu-devel] [Qemu-block] [PATCH for-2.10 14/16] block/qcow2: falloc/full preallocating growth
Posted by Stefan Hajnoczi 8 years, 7 months ago
On Mon, Mar 20, 2017 at 04:15:59PM +0100, Max Reitz wrote:
> On 20.03.2017 12:29, Stefan Hajnoczi wrote:
> > On Mon, Mar 13, 2017 at 10:41:15PM +0100, Max Reitz wrote:
> >> Implement the preallocation modes falloc and full for growing qcow2
> >> images.
> >>
> >> Signed-off-by: Max Reitz <mreitz@redhat.com>
> >> ---
> >>  block/qcow2.c | 36 +++++++++++++++++++++++++++++++++++-
> >>  1 file changed, 35 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/block/qcow2.c b/block/qcow2.c
> >> index 80fb815b15..b6b08d70da 100644
> >> --- a/block/qcow2.c
> >> +++ b/block/qcow2.c
> >> @@ -2604,7 +2604,9 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset,
> >>      int64_t new_l1_size;
> >>      int ret;
> >>  
> >> -    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA) {
> >> +    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
> >> +        prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
> > 
> > Now all cases are covered so this if statement can be dropped.  If you
> > are worried about new preallocation modes being added in the future then
> > the error_setg() can be moved into the switch statement's default case.
> 
> No, because the switch comes after we have already grown the L1 table.
> That wouldn't be so nice.

Oops :)

Stefan