Blame SOURCES/kvm-nbd-server-Favor-u-int64_t-over-off_t.patch

383d26
From 7d033c0f2aa0c3a2c2c2b85cba48cf898284f66b Mon Sep 17 00:00:00 2001
383d26
From: John Snow <jsnow@redhat.com>
383d26
Date: Wed, 27 Mar 2019 17:22:40 +0100
383d26
Subject: [PATCH 102/163] nbd/server: Favor [u]int64_t over off_t
383d26
383d26
RH-Author: John Snow <jsnow@redhat.com>
383d26
Message-id: <20190327172308.31077-28-jsnow@redhat.com>
383d26
Patchwork-id: 85192
383d26
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 27/55] nbd/server: Favor [u]int64_t over off_t
383d26
Bugzilla: 1691009
383d26
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
383d26
RH-Acked-by: Max Reitz <mreitz@redhat.com>
383d26
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
383d26
From: Eric Blake <eblake@redhat.com>
383d26
383d26
Although our compile-time environment is set up so that we always
383d26
support long files with 64-bit off_t, we have no guarantee whether
383d26
off_t is the same type as int64_t.  This requires casts when
383d26
printing values, and prevents us from directly using qemu_strtoi64()
383d26
(which will be done in the next patch). Let's just flip to uint64_t
383d26
where possible, and stick to int64_t for detecting failure of
383d26
blk_getlength(); we also keep the assertions added in the previous
383d26
patch that the resulting values fit in 63 bits.  The overflow check
383d26
in nbd_co_receive_request() was already sane (request->from is
383d26
validated to fit in 63 bits, and request->len is 32 bits, so the
383d26
addition can't overflow 64 bits), but rewrite it in a form easier
383d26
to recognize as a typical overflow check.
383d26
383d26
Rename the variable 'description' to keep line lengths reasonable.
383d26
383d26
Suggested-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
383d26
Signed-off-by: Eric Blake <eblake@redhat.com>
383d26
Message-Id: <20190117193658.16413-7-eblake@redhat.com>
383d26
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
383d26
(cherry picked from commit 9d26dfcbab62746b3e66ec7784d75c13ff499669)
383d26
Signed-off-by: John Snow <jsnow@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 include/block/nbd.h |  4 ++--
383d26
 nbd/server.c        | 18 +++++++++---------
383d26
 qemu-nbd.c          | 29 +++++++++++------------------
383d26
 3 files changed, 22 insertions(+), 29 deletions(-)
383d26
383d26
diff --git a/include/block/nbd.h b/include/block/nbd.h
383d26
index 1971b55..24be957 100644
383d26
--- a/include/block/nbd.h
383d26
+++ b/include/block/nbd.h
383d26
@@ -294,8 +294,8 @@ int nbd_errno_to_system_errno(int err);
383d26
 typedef struct NBDExport NBDExport;
383d26
 typedef struct NBDClient NBDClient;
383d26
 
383d26
-NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
383d26
-                          const char *name, const char *description,
383d26
+NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset,
383d26
+                          uint64_t size, const char *name, const char *desc,
383d26
                           const char *bitmap, uint16_t nbdflags,
383d26
                           void (*close)(NBDExport *), bool writethrough,
383d26
                           BlockBackend *on_eject_blk, Error **errp);
383d26
diff --git a/nbd/server.c b/nbd/server.c
383d26
index 51ee809..cb0d563 100644
383d26
--- a/nbd/server.c
383d26
+++ b/nbd/server.c
383d26
@@ -77,8 +77,8 @@ struct NBDExport {
383d26
     BlockBackend *blk;
383d26
     char *name;
383d26
     char *description;
383d26
-    off_t dev_offset;
383d26
-    off_t size;
383d26
+    uint64_t dev_offset;
383d26
+    uint64_t size;
383d26
     uint16_t nbdflags;
383d26
     QTAILQ_HEAD(, NBDClient) clients;
383d26
     QTAILQ_ENTRY(NBDExport) next;
383d26
@@ -1455,8 +1455,8 @@ static void nbd_eject_notifier(Notifier *n, void *data)
383d26
     nbd_export_close(exp);
383d26
 }
383d26
 
383d26
-NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
383d26
-                          const char *name, const char *description,
383d26
+NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset,
383d26
+                          uint64_t size, const char *name, const char *desc,
383d26
                           const char *bitmap, uint16_t nbdflags,
383d26
                           void (*close)(NBDExport *), bool writethrough,
383d26
                           BlockBackend *on_eject_blk, Error **errp)
383d26
@@ -1495,12 +1495,12 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
383d26
     exp->refcount = 1;
383d26
     QTAILQ_INIT(&exp->clients);
383d26
     exp->blk = blk;
383d26
-    assert(dev_offset >= 0 && dev_offset <= INT64_MAX);
383d26
+    assert(dev_offset <= INT64_MAX);
383d26
     exp->dev_offset = dev_offset;
383d26
     exp->name = g_strdup(name);
383d26
-    exp->description = g_strdup(description);
383d26
+    exp->description = g_strdup(desc);
383d26
     exp->nbdflags = nbdflags;
383d26
-    assert(size >= 0 && size <= INT64_MAX - dev_offset);
383d26
+    assert(size <= INT64_MAX - dev_offset);
383d26
     exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
383d26
 
383d26
     if (bitmap) {
383d26
@@ -2130,10 +2130,10 @@ static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
383d26
         return -EROFS;
383d26
     }
383d26
     if (request->from > client->exp->size ||
383d26
-        request->from + request->len > client->exp->size) {
383d26
+        request->len > client->exp->size - request->from) {
383d26
         error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
383d26
                    ", Size: %" PRIu64, request->from, request->len,
383d26
-                   (uint64_t)client->exp->size);
383d26
+                   client->exp->size);
383d26
         return (request->type == NBD_CMD_WRITE ||
383d26
                 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
383d26
     }
383d26
diff --git a/qemu-nbd.c b/qemu-nbd.c
383d26
index 5c90c5e..598caa6 100644
383d26
--- a/qemu-nbd.c
383d26
+++ b/qemu-nbd.c
383d26
@@ -176,7 +176,7 @@ static void read_partition(uint8_t *p, struct partition_record *r)
383d26
 }
383d26
 
383d26
 static int find_partition(BlockBackend *blk, int partition,
383d26
-                          off_t *offset, off_t *size)
383d26
+                          uint64_t *offset, uint64_t *size)
383d26
 {
383d26
     struct partition_record mbr[4];
383d26
     uint8_t data[MBR_SIZE];
383d26
@@ -500,14 +500,14 @@ int main(int argc, char **argv)
383d26
 {
383d26
     BlockBackend *blk;
383d26
     BlockDriverState *bs;
383d26
-    off_t dev_offset = 0;
383d26
+    uint64_t dev_offset = 0;
383d26
     uint16_t nbdflags = 0;
383d26
     bool disconnect = false;
383d26
     const char *bindto = NULL;
383d26
     const char *port = NULL;
383d26
     char *sockpath = NULL;
383d26
     char *device = NULL;
383d26
-    off_t fd_size;
383d26
+    int64_t fd_size;
383d26
     QemuOpts *sn_opts = NULL;
383d26
     const char *sn_id_or_name = NULL;
383d26
     const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:x:T:D:B:";
383d26
@@ -665,10 +665,6 @@ int main(int argc, char **argv)
383d26
                 error_report("Invalid offset `%s'", optarg);
383d26
                 exit(EXIT_FAILURE);
383d26
             }
383d26
-            if (dev_offset < 0) {
383d26
-                error_report("Offset must be positive `%s'", optarg);
383d26
-                exit(EXIT_FAILURE);
383d26
-            }
383d26
             break;
383d26
         case 'l':
383d26
             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
383d26
@@ -1005,15 +1001,14 @@ int main(int argc, char **argv)
383d26
     }
383d26
 
383d26
     if (dev_offset >= fd_size) {
383d26
-        error_report("Offset (%lld) has to be smaller than the image size "
383d26
-                     "(%lld)",
383d26
-                     (long long int)dev_offset, (long long int)fd_size);
383d26
+        error_report("Offset (%" PRIu64 ") has to be smaller than the image "
383d26
+                     "size (%" PRId64 ")", dev_offset, fd_size);
383d26
         exit(EXIT_FAILURE);
383d26
     }
383d26
     fd_size -= dev_offset;
383d26
 
383d26
     if (partition != -1) {
383d26
-        off_t limit;
383d26
+        uint64_t limit;
383d26
 
383d26
         if (dev_offset) {
383d26
             error_report("Cannot request partition and offset together");
383d26
@@ -1027,15 +1022,13 @@ int main(int argc, char **argv)
383d26
         }
383d26
         /*
383d26
          * MBR partition limits are (32-bit << 9); this assert lets
383d26
-         * the compiler know that we have two positive values that
383d26
-         * can't overflow 64 bits.
383d26
+         * the compiler know that we can't overflow 64 bits.
383d26
          */
383d26
-        assert(dev_offset >= 0 && dev_offset + limit >= dev_offset);
383d26
+        assert(dev_offset + limit >= dev_offset);
383d26
         if (dev_offset + limit > fd_size) {
383d26
-            error_report("Discovered partition %d at offset %lld size %lld, "
383d26
-                         "but size exceeds file length %lld", partition,
383d26
-                         (long long int) dev_offset, (long long int) limit,
383d26
-                         (long long int) fd_size);
383d26
+            error_report("Discovered partition %d at offset %" PRIu64
383d26
+                         " size %" PRIu64 ", but size exceeds file length %"
383d26
+                         PRId64, partition, dev_offset, limit, fd_size);
383d26
             exit(EXIT_FAILURE);
383d26
         }
383d26
         fd_size = limit;
383d26
-- 
383d26
1.8.3.1
383d26