From nobody Thu Apr 18 06:07:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1528876136319613.4548113686031; Wed, 13 Jun 2018 00:48:56 -0700 (PDT) Received: from localhost ([::1]:60470 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fT0WV-0006cb-JT for importer@patchew.org; Wed, 13 Jun 2018 03:48:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44475) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fT0Ul-0005iU-PZ for qemu-devel@nongnu.org; Wed, 13 Jun 2018 03:47:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fT0Uk-0006lp-Ou for qemu-devel@nongnu.org; Wed, 13 Jun 2018 03:47:07 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:37090 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fT0Ug-0006h1-6A; Wed, 13 Jun 2018 03:47:02 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B215D4000D0B; Wed, 13 Jun 2018 07:47:01 +0000 (UTC) Received: from lemon.usersys.redhat.com (ovpn-12-100.pek2.redhat.com [10.72.12.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id B1C2C20345E2; Wed, 13 Jun 2018 07:46:58 +0000 (UTC) From: Fam Zheng To: qemu-devel@nongnu.org Date: Wed, 13 Jun 2018 15:46:55 +0800 Message-Id: <20180613074655.16289-1-famz@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 13 Jun 2018 07:47:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 13 Jun 2018 07:47:01 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'famz@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH] nvme: Support image creation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 --- 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" =20 #include "block/nvme.h" @@ -1154,6 +1155,73 @@ static void nvme_unregister_buf(BlockDriverState *bs= , void *host) qemu_vfio_dma_unmap(s->vfio, host); } =20 +static QemuOptsList nvme_create_opts =3D { + .name =3D "nvme-create-opts", + .head =3D QTAILQ_HEAD_INITIALIZER(nvme_create_opts.head), + .desc =3D { + { + .name =3D BLOCK_OPT_SIZE, + .type =3D QEMU_OPT_SIZE, + .help =3D "Virtual disk size" + }, + { /* end of list */ } + } +}; + +static int coroutine_fn nvme_co_create_opts(const char *filename, QemuOpts= *opts, + Error **errp) +{ + int ret =3D 0; + BlockDriverState *bs =3D NULL; + int64_t size; + + if (strncmp(filename, "nvme://", strlen("nvme://"))) { + error_setg(errp, "Invalid filename (must start with \"nvme://\")"); + ret =3D -EINVAL; + goto out; + } + + bs =3D bdrv_open(filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, = errp); + if (!bs) { + ret =3D -EINVAL; + goto out; + } + + size =3D qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + + if (size < 0 || bdrv_getlength(bs) < size) { + error_setg(errp, "Invalid image size"); + ret =3D -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 !=3D 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 =3D { .format_name =3D "nvme", .protocol_name =3D "nvme", @@ -1180,6 +1248,10 @@ static BlockDriver bdrv_nvme =3D { =20 .bdrv_register_buf =3D nvme_register_buf, .bdrv_unregister_buf =3D nvme_unregister_buf, + + .create_opts =3D &nvme_create_opts, + .bdrv_co_create_opts =3D nvme_co_create_opts, + .bdrv_truncate =3D nvme_truncate, }; =20 static void bdrv_nvme_init(void) --=20 2.17.0