Skip to contentSkip to conversation

respect bdrv_getlength() error code

PATCH Applied Denis V. Lunev 3 participants 9 messages Aug 4, 2017 – Aug 7, 2017 Block layer

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 block: respect error code from bdrv_getlength in handle_aiocb_write_zeroes

Original idea beyond the code in question was the following: we have failed to write zeroes with fallocate(FALLOC_FL_ZERO_RANGE) as the simplest approach and via fallocate(FALLOC_FL_PUNCH_HOLE)/fallocate(0). We have the only chance now: if the request comes beyond end of the file. Thus we should calculate file length and respect the error code from that op.

CC Markus Armbruster <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/file-posix.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
block/file-posix.c +7 -1
index cfbb236f6f..f4de022ae0 100644
@@ -1339,6 +1339,9 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
 #if defined(CONFIG_FALLOCATE) || defined(CONFIG_XFS)
     BDRVRawState *s = aiocb->bs->opaque;
 #endif
+#ifdef CONFIG_FALLOCATE
+    int64_t len;
+#endif
 
     if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
         return handle_aiocb_write_zeroes_block(aiocb);
@@ -1381,7 +1384,10 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
 #endif
 
 #ifdef CONFIG_FALLOCATE
-    if (s->has_fallocate && aiocb->aio_offset >= bdrv_getlength(aiocb->bs)) {
+    /* Last resort: we are trying to extend the file with zeroed data. This
+     * can be done via fallocate(fd, 0) */
+    len = bdrv_getlength(aiocb->bs);
+    if (s->has_fallocate && len >= 0 && aiocb->aio_offset >= len) {
         int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes);
         if (ret == 0 || ret != -ENOTSUP) {
             return ret;
Signature · 1 line
2.11.0
#3 lore raw

PATCH 2/3 parallels: respect error code of bdrv_getlength() in allocate_clusters()

In reply to Denis V. Lunev

If we can not get the file length, the state of BDS is broken completely. Return error to the caller.

CC Markus Armbruster <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/parallels.c +6 -2
index 5bbdfabb7a..6794e53c0b 100644
@@ -192,7 +192,7 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
                                  int nb_sectors, int *pnum)
 {
     BDRVParallelsState *s = bs->opaque;
-    int64_t pos, space, idx, to_allocate, i;
+    int64_t pos, space, idx, to_allocate, i, len;
 
     pos = block_status(s, sector_num, nb_sectors, pnum);
     if (pos > 0) {
@@ -214,7 +214,11 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
     assert(idx < s->bat_size && idx + to_allocate <= s->bat_size);
 
     space = to_allocate * s->tracks;
-    if (s->data_end + space > bdrv_getlength(bs->file->bs) >> BDRV_SECTOR_BITS) {
+    len = bdrv_getlength(bs->file->bs);
+    if (len < 0) {
+        return len;
+    }
+    if (s->data_end + space > (len >> BDRV_SECTOR_BITS)) {
         int ret;
         space += s->prealloc_size;
         if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
Signature · 1 line
2.11.0
#5 lore raw

PATCH 3/3 parallels: drop check that bdrv_truncate() is working

In reply to Denis V. Lunev

This would be actually strange and error prone. If truncate() nowadays will fail, there is something fatally wrong. Let's check for that during the actual work.

The only fallback case is when the file is not zero initialized. In this case we should switch to preallocation via fallocate().

CC Markus Armbruster <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
block/parallels.c +1 -3
index 6794e53c0b..e1e06d23cc 100644
@@ -703,9 +703,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail_options;
     }
 
-    if (!(flags & BDRV_O_RESIZE) || !bdrv_has_zero_init(bs->file->bs) ||
-            bdrv_truncate(bs->file, bdrv_getlength(bs->file->bs),
-                          PREALLOC_MODE_OFF, NULL) != 0) {
+    if (!bdrv_has_zero_init(bs->file->bs)) {
         s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
     }
 
Signature · 1 line
2.11.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] block: respect error code from bdrv_getlength in handle_aiocb_write_zeroes

In reply to Denis V. Lunev
Quoted 6 lines · Denis V. Lunev

Original idea beyond the code in question was the following: we have failed to write zeroes with fallocate(FALLOC_FL_ZERO_RANGE) as the simplest approach and via fallocate(FALLOC_FL_PUNCH_HOLE)/fallocate(0). We have the only chance now: if the request comes beyond end of the file. Thus we should calculate file length and respect the error code from that op.

Quoted 9 lines
CC Markus Armbruster <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/file-posix.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3] parallels: respect error code of bdrv_getlength() in allocate_clusters()

In reply to Denis V. Lunev
Quoted 12 lines · Denis V. Lunev

If we can not get the file length, the state of BDS is broken completely. Return error to the caller.

CC Markus Armbruster <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#6 lore raw

Re: [Qemu-devel] [PATCH 3/3] parallels: drop check that bdrv_truncate() is working

In reply to Denis V. Lunev
Quoted 6 lines · Denis V. Lunev

This would be actually strange and error prone. If truncate() nowadays will fail, there is something fatally wrong. Let's check for that during the actual work.

The only fallback case is when the file is not zero initialized. In this case we should switch to preallocation via fallocate().

I got confused by the commit message. Here's my attempt an an alternative, to see if I'm understanding the point of this patch:

The code was trying to truncate a file to its current length, as an optimization to help decide whether an alternative prealloc mode was useful. But it forgot to check whether bdrv_getlength() succeeded, and dealing with that failure just complicates what was supposed to be a no-op probe for optimizing later operation. We will still properly fail later when an actual truncation attempt is made and the device can't support it. So when deciding how to set prealloc_mode while opening the device, all we really need is to check just the one condition that matters - knowing whether the device is zero initialized.

Quoted 9 lines
CC Markus Armbruster <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

The code change makes sense at first glance, but I'm a bit reluctant to give R-b, since the commit message threw me off and I'm not familiar with the parallels code in the first place.

Quoted 17 lines
block/parallels.c +1 -3
index 6794e53c0b..e1e06d23cc 100644
@@ -703,9 +703,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail_options;
     }
 
-    if (!(flags & BDRV_O_RESIZE) || !bdrv_has_zero_init(bs->file->bs) ||
-            bdrv_truncate(bs->file, bdrv_getlength(bs->file->bs),
-                          PREALLOC_MODE_OFF, NULL) != 0) {
+    if (!bdrv_has_zero_init(bs->file->bs)) {
         s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
     }
 
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#7 lore raw

Re: [Qemu-devel] [PATCH 3/3] parallels: drop check that bdrv_truncate() is working

Quoted 10 lines · Eric Blake
Quoted 6 lines · Denis V. Lunev

This would be actually strange and error prone. If truncate() nowadays will fail, there is something fatally wrong. Let's check for that during the actual work.

The only fallback case is when the file is not zero initialized. In this case we should switch to preallocation via fallocate().

I got confused by the commit message. Here's my attempt an an alternative, to see if I'm understanding the point of this patch:

Quoted 10 lines

The code was trying to truncate a file to its current length, as an optimization to help decide whether an alternative prealloc mode was useful. But it forgot to check whether bdrv_getlength() succeeded, and dealing with that failure just complicates what was supposed to be a no-op probe for optimizing later operation. We will still properly fail later when an actual truncation attempt is made and the device can't support it. So when deciding how to set prealloc_mode while opening the device, all we really need is to check just the one condition that matters - knowing whether the device is zero initialized.

This is not an optimization. This is check that truncate is working for underlying BDS. 2 years ago I though that I have seen some BDSes without truncate support.

So, with this patch I have dropped this check. If the user has specified to change preallocation mode, he is responsible to be correct. If truncate is broken, this will be revealed at the first attempt to expand the file.

Quoted 28 lines
Quoted 8 lines
CC Markus Armbruster <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

The code change makes sense at first glance, but I'm a bit reluctant to give R-b, since the commit message threw me off and I'm not familiar with the parallels code in the first place.

Quoted 16 lines
block/parallels.c +1 -3
index 6794e53c0b..e1e06d23cc 100644
@@ -703,9 +703,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail_options;
     }
 
-    if (!(flags & BDRV_O_RESIZE) || !bdrv_has_zero_init(bs->file->bs) ||
-            bdrv_truncate(bs->file, bdrv_getlength(bs->file->bs),
-                          PREALLOC_MODE_OFF, NULL) != 0) {
+    if (!bdrv_has_zero_init(bs->file->bs)) {
         s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
     }
 
Applied
#8 lore raw
In reply to Denis V. Lunev

Am 04.08.2017 um 17:10 hat Denis V. Lunev geschrieben:

Quoted 3 lines

These cases were reported by Markus Armbruster <[email protected]> Patches add error checking of the bdrv_getlength() call or remove the call of that function.

Thanks, applied to the block branch.

Kevin