diff mbox series

block: allocate aligned write buffer for 'truncate -m full'

Message ID 20231211105559.316897-1-andrey.drobyshev@virtuozzo.com
State New
Headers show
Series block: allocate aligned write buffer for 'truncate -m full' | expand

Commit Message

Andrey Drobyshev Dec. 11, 2023, 10:55 a.m. UTC
In case we're truncating an image opened with O_DIRECT, we might get
-EINVAL on write with unaligned buffer.  In particular, when running
iotests/298 with '-nocache' we get:

qemu-io: Failed to resize underlying file: Could not write zeros for
preallocation: Invalid argument

Let's just allocate the buffer using qemu_blockalign0() instead.

Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
---
 block/file-posix.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Denis V. Lunev Dec. 11, 2023, 11:27 a.m. UTC | #1
On 12/11/23 11:55, Andrey Drobyshev wrote:
> In case we're truncating an image opened with O_DIRECT, we might get
> -EINVAL on write with unaligned buffer.  In particular, when running
> iotests/298 with '-nocache' we get:
>
> qemu-io: Failed to resize underlying file: Could not write zeros for
> preallocation: Invalid argument
>
> Let's just allocate the buffer using qemu_blockalign0() instead.
>
> Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
> ---
>   block/file-posix.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/block/file-posix.c b/block/file-posix.c
> index b862406c71..cee8de510b 100644
> --- a/block/file-posix.c
> +++ b/block/file-posix.c
> @@ -2354,7 +2354,7 @@ static int handle_aiocb_truncate(void *opaque)
>               goto out;
>           }
>   
> -        buf = g_malloc0(65536);
> +        buf = qemu_blockalign0(aiocb->bs, 65536);
>   
>           seek_result = lseek(fd, current_length, SEEK_SET);
>           if (seek_result < 0) {
> @@ -2413,7 +2413,7 @@ out:
>           }
>       }
>   
> -    g_free(buf);
> +    qemu_vfree(buf);
>       return result;
>   }
>   
Reviewed-by: Denis V. Lunev <den@openvz.org>
Andrey Drobyshev Jan. 11, 2024, 12:53 p.m. UTC | #2
On 12/11/23 13:27, Denis V. Lunev wrote:
> On 12/11/23 11:55, Andrey Drobyshev wrote:
>> In case we're truncating an image opened with O_DIRECT, we might get
>> -EINVAL on write with unaligned buffer.  In particular, when running
>> iotests/298 with '-nocache' we get:
>>
>> qemu-io: Failed to resize underlying file: Could not write zeros for
>> preallocation: Invalid argument
>>
>> Let's just allocate the buffer using qemu_blockalign0() instead.
>>
>> Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
>> ---
>>   block/file-posix.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/block/file-posix.c b/block/file-posix.c
>> index b862406c71..cee8de510b 100644
>> --- a/block/file-posix.c
>> +++ b/block/file-posix.c
>> @@ -2354,7 +2354,7 @@ static int handle_aiocb_truncate(void *opaque)
>>               goto out;
>>           }
>>   -        buf = g_malloc0(65536);
>> +        buf = qemu_blockalign0(aiocb->bs, 65536);
>>             seek_result = lseek(fd, current_length, SEEK_SET);
>>           if (seek_result < 0) {
>> @@ -2413,7 +2413,7 @@ out:
>>           }
>>       }
>>   -    g_free(buf);
>> +    qemu_vfree(buf);
>>       return result;
>>   }
>>   
> Reviewed-by: Denis V. Lunev <den@openvz.org>

Ping
Andrey Drobyshev Jan. 25, 2024, 4:37 p.m. UTC | #3
On 1/11/24 14:53, Andrey Drobyshev wrote:
> On 12/11/23 13:27, Denis V. Lunev wrote:
>> On 12/11/23 11:55, Andrey Drobyshev wrote:
>>> In case we're truncating an image opened with O_DIRECT, we might get
>>> -EINVAL on write with unaligned buffer.  In particular, when running
>>> iotests/298 with '-nocache' we get:
>>>
>>> qemu-io: Failed to resize underlying file: Could not write zeros for
>>> preallocation: Invalid argument
>>>
>>> Let's just allocate the buffer using qemu_blockalign0() instead.
>>>
>>> Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
>>> ---
>>>   block/file-posix.c | 4 ++--
>>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/block/file-posix.c b/block/file-posix.c
>>> index b862406c71..cee8de510b 100644
>>> --- a/block/file-posix.c
>>> +++ b/block/file-posix.c
>>> @@ -2354,7 +2354,7 @@ static int handle_aiocb_truncate(void *opaque)
>>>               goto out;
>>>           }
>>>   -        buf = g_malloc0(65536);
>>> +        buf = qemu_blockalign0(aiocb->bs, 65536);
>>>             seek_result = lseek(fd, current_length, SEEK_SET);
>>>           if (seek_result < 0) {
>>> @@ -2413,7 +2413,7 @@ out:
>>>           }
>>>       }
>>>   -    g_free(buf);
>>> +    qemu_vfree(buf);
>>>       return result;
>>>   }
>>>   
>> Reviewed-by: Denis V. Lunev <den@openvz.org>
> 
> Ping

Ping
Vladimir Sementsov-Ogievskiy Jan. 25, 2024, 4:46 p.m. UTC | #4
On 11.12.23 13:55, Andrey Drobyshev wrote:
> In case we're truncating an image opened with O_DIRECT, we might get
> -EINVAL on write with unaligned buffer.  In particular, when running
> iotests/298 with '-nocache' we get:
> 
> qemu-io: Failed to resize underlying file: Could not write zeros for
> preallocation: Invalid argument
> 
> Let's just allocate the buffer using qemu_blockalign0() instead.
> 
> Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>

Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>

I also suggest to use QEMU_AUTO_VFREE (keep my r-b if you do).

> ---
>   block/file-posix.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/block/file-posix.c b/block/file-posix.c
> index b862406c71..cee8de510b 100644
> --- a/block/file-posix.c
> +++ b/block/file-posix.c
> @@ -2354,7 +2354,7 @@ static int handle_aiocb_truncate(void *opaque)
>               goto out;
>           }
>   
> -        buf = g_malloc0(65536);
> +        buf = qemu_blockalign0(aiocb->bs, 65536);
>   
>           seek_result = lseek(fd, current_length, SEEK_SET);
>           if (seek_result < 0) {
> @@ -2413,7 +2413,7 @@ out:
>           }
>       }
>   
> -    g_free(buf);
> +    qemu_vfree(buf);
>       return result;
>   }
>
Andrey Drobyshev Feb. 8, 2024, 6:32 p.m. UTC | #5
On 1/25/24 18:46, Vladimir Sementsov-Ogievskiy wrote:
> On 11.12.23 13:55, Andrey Drobyshev wrote:
>> In case we're truncating an image opened with O_DIRECT, we might get
>> -EINVAL on write with unaligned buffer.  In particular, when running
>> iotests/298 with '-nocache' we get:
>>
>> qemu-io: Failed to resize underlying file: Could not write zeros for
>> preallocation: Invalid argument
>>
>> Let's just allocate the buffer using qemu_blockalign0() instead.
>>
>> Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
> 
> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> 
> I also suggest to use QEMU_AUTO_VFREE (keep my r-b if you do).
> 
>> ---
>>   block/file-posix.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/block/file-posix.c b/block/file-posix.c
>> index b862406c71..cee8de510b 100644
>> --- a/block/file-posix.c
>> +++ b/block/file-posix.c
>> @@ -2354,7 +2354,7 @@ static int handle_aiocb_truncate(void *opaque)
>>               goto out;
>>           }
>>   -        buf = g_malloc0(65536);
>> +        buf = qemu_blockalign0(aiocb->bs, 65536);
>>             seek_result = lseek(fd, current_length, SEEK_SET);
>>           if (seek_result < 0) {
>> @@ -2413,7 +2413,7 @@ out:
>>           }
>>       }
>>   -    g_free(buf);
>> +    qemu_vfree(buf);
>>       return result;
>>   }
>>   
> 

Yet another ping, just checking if any of the block maintainers is
interested
diff mbox series

Patch

diff --git a/block/file-posix.c b/block/file-posix.c
index b862406c71..cee8de510b 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -2354,7 +2354,7 @@  static int handle_aiocb_truncate(void *opaque)
             goto out;
         }
 
-        buf = g_malloc0(65536);
+        buf = qemu_blockalign0(aiocb->bs, 65536);
 
         seek_result = lseek(fd, current_length, SEEK_SET);
         if (seek_result < 0) {
@@ -2413,7 +2413,7 @@  out:
         }
     }
 
-    g_free(buf);
+    qemu_vfree(buf);
     return result;
 }