qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Fam Zheng <famz@redhat.com>, Kevin Wolf <kwolf@redhat.com>,
	Max Reitz <mreitz@redhat.com>,
	qemu-block@nongnu.org
Subject: [Qemu-devel] [PATCH] nvme: Support image creation
Date: Wed, 13 Jun 2018 15:46:55 +0800	[thread overview]
Message-ID: <20180613074655.16289-1-famz@redhat.com> (raw)

Similar to the host_device's implementation, we check the requested
length against the namespace size.

Truncation is necessary to make qcow2 creation work.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block/nvme.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/block/nvme.c b/block/nvme.c
index 6f71122bf5..ec3d18e790 100644
--- a/block/nvme.c
+++ b/block/nvme.c
@@ -21,6 +21,7 @@
 #include "qemu/option.h"
 #include "qemu/vfio-helpers.h"
 #include "block/block_int.h"
+#include "sysemu/block-backend.h"
 #include "trace.h"
 
 #include "block/nvme.h"
@@ -1154,6 +1155,73 @@ static void nvme_unregister_buf(BlockDriverState *bs, void *host)
     qemu_vfio_dma_unmap(s->vfio, host);
 }
 
+static QemuOptsList nvme_create_opts = {
+    .name = "nvme-create-opts",
+    .head = QTAILQ_HEAD_INITIALIZER(nvme_create_opts.head),
+    .desc = {
+        {
+            .name = BLOCK_OPT_SIZE,
+            .type = QEMU_OPT_SIZE,
+            .help = "Virtual disk size"
+        },
+        { /* end of list */ }
+    }
+};
+
+static int coroutine_fn nvme_co_create_opts(const char *filename, QemuOpts *opts,
+                                            Error **errp)
+{
+    int ret = 0;
+    BlockDriverState *bs = NULL;
+    int64_t size;
+
+    if (strncmp(filename, "nvme://", strlen("nvme://"))) {
+        error_setg(errp, "Invalid filename (must start with \"nvme://\")");
+        ret = -EINVAL;
+        goto out;
+    }
+
+    bs = bdrv_open(filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, errp);
+    if (!bs) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
+
+    if (size < 0 || bdrv_getlength(bs) < size) {
+        error_setg(errp, "Invalid image size");
+        ret = -EINVAL;
+    }
+
+out:
+    bdrv_unref(bs);
+    /* Hold breath for a little while before letting image format creation run.
+     * The problem is when testing with Intel P3700, the controller doesn't
+     * like the immediate open after close, as a result, nvme_init() will fail.
+     * This works around that.
+     **/
+    g_usleep(2000000);
+    return ret;
+}
+
+static int nvme_truncate(BlockDriverState *bs, int64_t offset,
+                         PreallocMode prealloc, Error **errp)
+{
+    if (prealloc != PREALLOC_MODE_OFF) {
+        error_setg(errp, "Preallocation mode '%s' unsupported",
+                   PreallocMode_str(prealloc));
+        return -ENOTSUP;
+    }
+
+    if (offset > nvme_getlength(bs)) {
+        error_setg(errp, "Cannot grow device files");
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
 static BlockDriver bdrv_nvme = {
     .format_name              = "nvme",
     .protocol_name            = "nvme",
@@ -1180,6 +1248,10 @@ static BlockDriver bdrv_nvme = {
 
     .bdrv_register_buf        = nvme_register_buf,
     .bdrv_unregister_buf      = nvme_unregister_buf,
+
+    .create_opts              = &nvme_create_opts,
+    .bdrv_co_create_opts      = nvme_co_create_opts,
+    .bdrv_truncate            = nvme_truncate,
 };
 
 static void bdrv_nvme_init(void)
-- 
2.17.0

             reply	other threads:[~2018-06-13  7:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-13  7:46 Fam Zheng [this message]
2018-06-13  8:06 ` [Qemu-devel] [PATCH] nvme: Support image creation Kevin Wolf
2018-06-13  9:17   ` Fam Zheng

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=20180613074655.16289-1-famz@redhat.com \
    --to=famz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@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).