From nobody Mon Feb 9 23:00:16 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.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 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562171848; cv=none; d=zoho.com; s=zohoarc; b=ed1lqwPa7yg7A0I8kh7N3m4KV6uONn7exRl1W8EGU1kRQVdqyDXIMPIwRXO1ThSg0TV9fQUW5zB3qMAT3W7nwBjyW71BQcEqrG8Ut5VqGPoHdYLn0+wuvT2xVu2H/ac06Xuxz8Eo7bTAZ2/+c36Gt8u6g01JE8qI+jO2wTC1lXU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562171848; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=FXHQnQvLPmx4F66OtKQ9NWUY0qtMMhNVCmhFm7Bnl9A=; b=ZEcjc09NgYmn9Jxc9AuS44jM976M9OXYZQSjmWIVnAwW8yPBGmH3hRGiRNsU0p+uzSHv3fwGu6jgZ7YWjACLahpmfQ1dzMBiXJ6Rpd1TZnFsUsEkWbEZ6T3G49ALoF4Gq73xblElqhrZ0AEgFguOnbkyoYt+xLQazkQD3lOWTK0= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562171848124252.9626150600726; Wed, 3 Jul 2019 09:37:28 -0700 (PDT) Received: from localhost ([::1]:37540 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hiiG4-0004gH-5d for importer@patchew.org; Wed, 03 Jul 2019 12:37:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55222) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hihgb-0007rc-IF for qemu-devel@nongnu.org; Wed, 03 Jul 2019 12:00:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hihgZ-0002bA-JA for qemu-devel@nongnu.org; Wed, 03 Jul 2019 12:00:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42324) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hihgT-0002Z6-CA; Wed, 03 Jul 2019 12:00:37 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A97BB21BAA; Wed, 3 Jul 2019 16:00:31 +0000 (UTC) Received: from maximlenovopc.usersys.redhat.com (dhcp-4-67.tlv.redhat.com [10.35.4.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id B59BB183B2; Wed, 3 Jul 2019 16:00:29 +0000 (UTC) From: Maxim Levitsky To: qemu-devel@nongnu.org Date: Wed, 3 Jul 2019 18:59:42 +0300 Message-Id: <20190703155944.9637-5-mlevitsk@redhat.com> In-Reply-To: <20190703155944.9637-1-mlevitsk@redhat.com> References: <20190703155944.9637-1-mlevitsk@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 03 Jul 2019 16:00:31 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 4/6] block/nvme: add support for image creation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , qemu-block@nongnu.org, Max Reitz , Paolo Bonzini , Maxim Levitsky , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Tesed on a nvme device like that: # create preallocated qcow2 image $ qemu-img create -f qcow2 nvme://0000:06:00.0/1 10G -o preallocation=3Dmet= adata Formatting 'nvme://0000:06:00.0/1', fmt=3Dqcow2 size=3D10737418240 cluster_= size=3D65536 preallocation=3Dmetadata lazy_refcounts=3Doff refcount_bits=3D= 16 # create an empty qcow2 image $ qemu-img create -f qcow2 nvme://0000:06:00.0/1 10G -o preallocation=3Doff Formatting 'nvme://0000:06:00.0/1', fmt=3Dqcow2 size=3D10737418240 cluster_= size=3D65536 preallocation=3Doff lazy_refcounts=3Doff refcount_bits=3D16 Signed-off-by: Maxim Levitsky --- block/nvme.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/block/nvme.c b/block/nvme.c index 1f0d09349f..152d27b07f 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -1148,6 +1148,90 @@ static void nvme_aio_unplug(BlockDriverState *bs) } } =20 +static int coroutine_fn nvme_co_create_opts(const char *filename, + QemuOpts *opts, Error **errp) +{ + + int64_t total_size =3D 0; + char *buf =3D NULL; + BlockDriverState *bs; + QEMUIOVector local_qiov; + int ret =3D 0; + int64_t blocksize; + QDict *options; + Error *local_err =3D NULL; + PreallocMode prealloc; + + total_size =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), + BDRV_SECTOR_SIZE); + + buf =3D qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); + prealloc =3D qapi_enum_parse(&PreallocMode_lookup, buf, + PREALLOC_MODE_OFF, &local_err); + g_free(buf); + + if (prealloc !=3D PREALLOC_MODE_OFF) { + error_setg(errp, "Only prealloc=3Doff is supported"); + return -EINVAL; + } + + options =3D qdict_new(); + qdict_put_str(options, "driver", "nvme"); + nvme_parse_filename(filename, options, &local_err); + + if (local_err) { + error_propagate(errp, local_err); + qobject_unref(options); + return -EINVAL; + } + + bs =3D bdrv_open(NULL, NULL, options, + BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp= ); + if (bs =3D=3D NULL) { + return -EIO; + } + + if (nvme_getlength(bs) < total_size) { + error_setg(errp, "Device is too small"); + bdrv_unref(bs); + qobject_unref(options); + return -ENOSPC; + } + + blocksize =3D nvme_get_blocksize(bs); + buf =3D qemu_try_blockalign0(bs, blocksize); + qemu_iovec_init(&local_qiov, 1); + qemu_iovec_add(&local_qiov, buf, blocksize); + + ret =3D nvme_co_prw_aligned(bs, 0, blocksize, + &local_qiov, true, BDRV_REQ_FUA); + if (ret) { + error_setg(errp, "Write error to sector 0"); + } + + qemu_vfree(buf); + bdrv_unref(bs); + return ret; +} + + +static int coroutine_fn nvme_co_truncate(BlockDriverState *bs, int64_t off= set, + PreallocMode prealloc, Error **err= p) +{ + if (prealloc !=3D PREALLOC_MODE_OFF) { + error_setg(errp, "Preallocation mode '%s' unsupported nvme devices= ", + PreallocMode_str(prealloc)); + return -ENOTSUP; + } + + if (offset > nvme_getlength(bs)) { + error_setg(errp, "Cannot grow nvme devices"); + return -EINVAL; + } + + return 0; +} + static void nvme_register_buf(BlockDriverState *bs, void *host, size_t siz= e) { int ret; @@ -1169,6 +1253,7 @@ static void nvme_unregister_buf(BlockDriverState *bs,= void *host) qemu_vfio_dma_unmap(s->vfio, host); } =20 + static const char *const nvme_strong_runtime_opts[] =3D { NVME_BLOCK_OPT_DEVICE, NVME_BLOCK_OPT_NAMESPACE, @@ -1176,6 +1261,25 @@ static const char *const nvme_strong_runtime_opts[] = =3D { NULL }; =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" + }, + { + .name =3D BLOCK_OPT_PREALLOC, + .type =3D QEMU_OPT_STRING, + .help =3D "Preallocation mode (allowed values: off)", + }, + { /* end of list */ } + } +}; + static BlockDriver bdrv_nvme =3D { .format_name =3D "nvme", .protocol_name =3D "nvme", @@ -1187,6 +1291,10 @@ static BlockDriver bdrv_nvme =3D { .bdrv_getlength =3D nvme_getlength, .bdrv_probe_blocksizes =3D nvme_probe_blocksizes, =20 + .bdrv_co_create_opts =3D nvme_co_create_opts, + .bdrv_co_truncate =3D nvme_co_truncate, + .create_opts =3D &nvme_create_opts, + .bdrv_co_preadv =3D nvme_co_preadv, .bdrv_co_pwritev =3D nvme_co_pwritev, .bdrv_co_flush_to_disk =3D nvme_co_flush, --=20 2.17.2