qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-block@nongnu.org, pbonzini@redhat.com, alex@alex.org.uk
Subject: [Qemu-devel] [PATCH v4 07/11] nbd: Clean up ioctl handling of qemu-nbd -c
Date: Wed, 11 May 2016 16:39:40 -0600	[thread overview]
Message-ID: <1463006384-7734-8-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1463006384-7734-1-git-send-email-eblake@redhat.com>

The kernel ioctl() interface into NBD is limited to 'unsigned long';
we MUST pass in input with that type (and not int or size_t, as
there may be platform ABIs where the wrong types promote incorrectly
through var-args).  Furthermore, on 32-bit platforms, the kernel
is limited to a maximum export size of 2T (our BLKSIZE of 512 times
a SIZE_BLOCKS constrained by 32 bit unsigned long).

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 nbd/client.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/nbd/client.c b/nbd/client.c
index ae9fdd4..f1afa49 100644
--- a/nbd/client.c
+++ b/nbd/client.c
@@ -593,9 +593,15 @@ fail:
 #ifdef __linux__
 int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size)
 {
+    unsigned long sectors = size / BDRV_SECTOR_SIZE;
+    if (size / BDRV_SECTOR_SIZE != sectors) {
+        LOG("Export size %lld too large for 32-bit kernel", (long long) size);
+        return -E2BIG;
+    }
+
     TRACE("Setting NBD socket");

-    if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) {
+    if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
         int serrno = errno;
         LOG("Failed to set NBD socket");
         return -serrno;
@@ -603,21 +609,25 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size)

     TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);

-    if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {
+    if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) {
         int serrno = errno;
         LOG("Failed setting NBD block size");
         return -serrno;
     }

-    TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));
+    TRACE("Setting size to %lu block(s)", sectors);
+    if (size % BDRV_SECTOR_SIZE) {
+        TRACE("Ignoring trailing %d bytes of export",
+              (int) (size % BDRV_SECTOR_SIZE));
+    }

-    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) {
+    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
         int serrno = errno;
         LOG("Failed setting size (in blocks)");
         return -serrno;
     }

-    if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
+    if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) {
         if (errno == ENOTTY) {
             int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
             TRACE("Setting readonly attribute");
-- 
2.5.5

  parent reply	other threads:[~2016-05-11 22:40 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-11 22:39 [Qemu-devel] [PATCH v4 00/11] nbd: tighter protocol compliance Eric Blake
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 01/11] nbd: Use BDRV_REQ_FUA for better FUA where supported Eric Blake
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 02/11] nbd: More debug typo fixes, use correct formats Eric Blake
2016-06-13 12:04   ` Paolo Bonzini
2016-06-13 12:21     ` Eric Blake
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 03/11] nbd: Quit server after any write error Eric Blake
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 04/11] nbd: Improve server handling of bogus commands Eric Blake
2016-06-13 12:10   ` Paolo Bonzini
2016-06-13 12:25     ` Eric Blake
2016-06-13 21:41       ` Alex Bligh
2016-06-14 13:32         ` Paolo Bonzini
2016-06-14 15:02           ` Alex Bligh
2016-06-14 15:11             ` Paolo Bonzini
2016-06-14 15:59               ` Alex Bligh
2016-06-14 22:05                 ` Paolo Bonzini
2016-06-15  7:05             ` [Qemu-devel] [Nbd] " Wouter Verhelst
2016-06-15  8:03               ` Wouter Verhelst
2016-06-15  8:52                 ` Alex Bligh
2016-06-15  9:18                   ` Paolo Bonzini
2016-06-15 10:27                     ` Alex Bligh
2016-06-15 10:34                       ` Paolo Bonzini
2016-06-15 12:13                       ` Wouter Verhelst
2016-06-15  7:02         ` Wouter Verhelst
2016-06-13 12:19   ` [Qemu-devel] " Paolo Bonzini
2016-06-13 16:54     ` Eric Blake
2016-06-14 18:24     ` Eric Blake
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 05/11] nbd: Reject unknown request flags Eric Blake
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 06/11] nbd: Group all Linux-specific ioctl code in one place Eric Blake
2016-05-11 22:39 ` Eric Blake [this message]
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 08/11] nbd: Limit nbdflags to 16 bits Eric Blake
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 09/11] nbd: Add qemu-nbd -D for human-readable description Eric Blake
2016-05-12  7:47   ` Daniel P. Berrange
2016-05-12 15:38     ` Eric Blake
2016-05-12 15:51       ` Daniel P. Berrange
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 10/11] nbd: Detect servers that send unexpected error values Eric Blake
2016-05-11 22:39 ` [Qemu-devel] [PATCH v4 11/11] nbd: Avoid magic number for NBD max name size Eric Blake
2016-05-12 16:04 ` [Qemu-devel] [PATCH v4 00/11] nbd: tighter protocol compliance Alex Bligh
2016-06-01 23:02 ` Eric Blake
2016-06-13 16:28 ` Paolo Bonzini
2016-06-13 16:49   ` Eric Blake
2016-06-14 16:31     ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1463006384-7734-8-git-send-email-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=alex@alex.org.uk \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).