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

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