[Qemu-devel] [PATCH 5/8] block: Evaluate @exact in protocol drivers

Max Reitz posted 8 patches 6 years, 1 month ago
Maintainers: "Denis V. Lunev" <den@openvz.org>, Stefan Hajnoczi <stefanha@redhat.com>, Kevin Wolf <kwolf@redhat.com>, John Snow <jsnow@redhat.com>, Fam Zheng <fam@euphon.net>, Max Reitz <mreitz@redhat.com>, Peter Lieven <pl@kamp.de>, Markus Armbruster <armbru@redhat.com>, Ronnie Sahlberg <ronniesahlberg@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>, Jeff Cody <codyprime@gmail.com>, "Richard W.M. Jones" <rjones@redhat.com>, Jason Dillaman <dillaman@redhat.com>, Liu Yuan <namei.unix@gmail.com>, Stefan Weil <sw@weilnetz.de>
[Qemu-devel] [PATCH 5/8] block: Evaluate @exact in protocol drivers
Posted by Max Reitz 6 years, 1 month ago
We have two protocol drivers that return success when trying to shrink a
block device even though they cannot shrink it.  This behavior is now
only allowed with exact=false, so they should return an error with
exact=true.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/file-posix.c | 8 +++++++-
 block/iscsi.c      | 7 ++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/block/file-posix.c b/block/file-posix.c
index d8755c5fac..a85f3486d1 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -2028,6 +2028,7 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
     }
 
     if (S_ISREG(st.st_mode)) {
+        /* Always resizes to the exact @offset */
         return raw_regular_truncate(bs, s->fd, offset, prealloc, errp);
     }
 
@@ -2038,7 +2039,12 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
     }
 
     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
-        if (offset > raw_getlength(bs)) {
+        int64_t cur_length = raw_getlength(bs);
+
+        if (offset != cur_length && exact) {
+            error_setg(errp, "Cannot resize device files");
+            return -ENOTSUP;
+        } else if (offset > cur_length) {
             error_setg(errp, "Cannot grow device files");
             return -EINVAL;
         }
diff --git a/block/iscsi.c b/block/iscsi.c
index a90426270a..cc2072ff6c 100644
--- a/block/iscsi.c
+++ b/block/iscsi.c
@@ -2126,6 +2126,7 @@ static int coroutine_fn iscsi_co_truncate(BlockDriverState *bs, int64_t offset,
                                           Error **errp)
 {
     IscsiLun *iscsilun = bs->opaque;
+    int64_t cur_length;
     Error *local_err = NULL;
 
     if (prealloc != PREALLOC_MODE_OFF) {
@@ -2145,7 +2146,11 @@ static int coroutine_fn iscsi_co_truncate(BlockDriverState *bs, int64_t offset,
         return -EIO;
     }
 
-    if (offset > iscsi_getlength(bs)) {
+    cur_length = iscsi_getlength(bs);
+    if (offset != cur_length && exact) {
+        error_setg(errp, "Cannot resize iSCSI devices");
+        return -ENOTSUP;
+    } else if (offset > cur_length) {
         error_setg(errp, "Cannot grow iSCSI devices");
         return -EINVAL;
     }
-- 
2.21.0


Re: [Qemu-devel] [PATCH 5/8] block: Evaluate @exact in protocol drivers
Posted by Maxim Levitsky 6 years, 1 month ago
On Wed, 2019-09-18 at 11:51 +0200, Max Reitz wrote:
> We have two protocol drivers that return success when trying to shrink a
> block device even though they cannot shrink it.  This behavior is now
> only allowed with exact=false, so they should return an error with
> exact=true.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  block/file-posix.c | 8 +++++++-
>  block/iscsi.c      | 7 ++++++-
>  2 files changed, 13 insertions(+), 2 deletions(-)
> 
> diff --git a/block/file-posix.c b/block/file-posix.c
> index d8755c5fac..a85f3486d1 100644
> --- a/block/file-posix.c
> +++ b/block/file-posix.c
> @@ -2028,6 +2028,7 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
>      }
>  
>      if (S_ISREG(st.st_mode)) {
> +        /* Always resizes to the exact @offset */
>          return raw_regular_truncate(bs, s->fd, offset, prealloc, errp);
>      }
>  
> @@ -2038,7 +2039,12 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
>      }
>  
>      if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
> -        if (offset > raw_getlength(bs)) {
> +        int64_t cur_length = raw_getlength(bs);
> +
> +        if (offset != cur_length && exact) {
> +            error_setg(errp, "Cannot resize device files");
> +            return -ENOTSUP;
> +        } else if (offset > cur_length) {
>              error_setg(errp, "Cannot grow device files");
>              return -EINVAL;
>          }
> diff --git a/block/iscsi.c b/block/iscsi.c
> index a90426270a..cc2072ff6c 100644
> --- a/block/iscsi.c
> +++ b/block/iscsi.c
> @@ -2126,6 +2126,7 @@ static int coroutine_fn iscsi_co_truncate(BlockDriverState *bs, int64_t offset,
>                                            Error **errp)
>  {
>      IscsiLun *iscsilun = bs->opaque;
> +    int64_t cur_length;
>      Error *local_err = NULL;
>  
>      if (prealloc != PREALLOC_MODE_OFF) {
> @@ -2145,7 +2146,11 @@ static int coroutine_fn iscsi_co_truncate(BlockDriverState *bs, int64_t offset,
>          return -EIO;
>      }
>  
> -    if (offset > iscsi_getlength(bs)) {
> +    cur_length = iscsi_getlength(bs);
> +    if (offset != cur_length && exact) {
> +        error_setg(errp, "Cannot resize iSCSI devices");
> +        return -ENOTSUP;
> +    } else if (offset > cur_length) {
>          error_setg(errp, "Cannot grow iSCSI devices");
>          return -EINVAL;
>      }

Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
Best regards,
	Maxim Levitsky