From patchwork Wed Mar 10 15:39:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 12128247 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58E9BC433DB for ; Wed, 10 Mar 2021 15:41:41 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C38F964F95 for ; Wed, 10 Mar 2021 15:41:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C38F964F95 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:40140 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lK0xv-00038Y-Jw for qemu-devel@archiver.kernel.org; Wed, 10 Mar 2021 10:41:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:56612) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lK0vq-0001aS-Sq; Wed, 10 Mar 2021 10:39:30 -0500 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]:39547) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lK0vn-0000vH-JO; Wed, 10 Mar 2021 10:39:30 -0500 Received: by mail-pj1-x1031.google.com with SMTP id lr10-20020a17090b4b8ab02900dd61b95c5eso5178908pjb.4; Wed, 10 Mar 2021 07:39:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Wg+mhNkNB4lUrMjlZW3lEf2FPRUUmtAYl68S49LJAb0=; b=ht/nlzaiFBGh893w5PzQGpUSxpxJu3NT3QKxYOHpmdDkOi+MMIrHt17SZNHbP5nDTA yFZfEPFykAUDQEMAqeUyUSU+H+lSVR8zp63y8Im50izXTH637A+YYWzmkSAdVLZxWDDI LkBiIXRVKkI40TVL00VVNV3DCAZkvP97vy7Dx9BnwykeLxpN0HzYoLfcpcAMMW5s/Dcs 6ChWF6iALvxs0H3MeEp6hy9irOrdCGe+cgE4oIpjRDAFMDDnmGqzxax9h611ECfphzV6 q1UnD5V51prrJNscvhcwvQgckjPoNi4EQ9K21TkSa7yfXA6kf7c7crZLF5TNA0K5rIAa iP6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Wg+mhNkNB4lUrMjlZW3lEf2FPRUUmtAYl68S49LJAb0=; b=caRO8Dcr8gZsF8K8go09t+0S+lFOUmYDeDIv8GCg3g1CEKn5MbWBcCPLGqMJgbSM2l APe+qBxm5cF/MvRb0FFbxxM3Ew+0yOYaQLGJbwV9tR7JQRML3FtpypSQZIzfjhJLneK8 06Orf9qAKrZAJKwUkLkqgdiymIN7AIqi9NmvVIXtsHuQJrj0YX05UJho8G0yiaJjDxR/ 8/wdAR4/CwTxTCyiK3Iecyj/pJZp6E8siXWt2Few+zUnrH1XbhxYtbcUcsCXrqqb0nv3 fCbwjbNHQAhrmjykg6e7JK7eSQ2KPwahN0xiINdxDmtvu2ieTnznh17/dFlx7eb4MoTo FEnw== X-Gm-Message-State: AOAM530amNWKCaaBfdlTyg3vrN4yTIzTt2GGrB/IuyQQnYPd0Csc6aLr +LmcXPxEKywNyCUOkRAI/PRyFgfVYvcexQ== X-Google-Smtp-Source: ABdhPJz63CMQ/B9ofsGVWSWCvZIZHD6yp6qKCWqzii7BaEGlfC6BEin9+WDiXYLOGs8pYiViW1BJew== X-Received: by 2002:a17:90a:d3d1:: with SMTP id d17mr4066243pjw.21.1615390765461; Wed, 10 Mar 2021 07:39:25 -0800 (PST) Received: from localhost.localdomain ([2400:4050:c360:8200:681d:e6e5:d1b0:3153]) by smtp.gmail.com with ESMTPSA id s12sm16324388pgj.70.2021.03.10.07.39.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Mar 2021 07:39:24 -0800 (PST) From: Akihiko Odaki To: Subject: [PATCH v3 1/2] block/file-posix: Optimize for macOS Date: Thu, 11 Mar 2021 00:39:15 +0900 Message-Id: <20210310153916.64256-1-akihiko.odaki@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=akihiko.odaki@gmail.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: Kevin Wolf , Fam Zheng , pkrempa@redhat.com, Akihiko Odaki , qemu-block@nongnu.org, Markus Armbruster , qemu-devel@nongnu.org, Max Reitz , Konstantin Nazarov , Stefan Hajnoczi , John Snow , dgilbert@redhat.com Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" This commit introduces "punch hole" operation and optimizes transfer block size for macOS. This commit introduces two additional members, discard_granularity and opt_io to BlockSizes type in include/block/block.h. Also, the members of the type are now optional. Set -1 to discard_granularity and 0 to other members for the default values. Thanks to Konstantin Nazarov for detailed analysis of a flaw in an old version of this change: https://gist.github.com/akihikodaki/87df4149e7ca87f18dc56807ec5a1bc5#gistcomment-3654667 Signed-off-by: Akihiko Odaki --- block/file-posix.c | 40 ++++++++++++++++++++++++++++++++++++++-- block/nvme.c | 2 ++ block/raw-format.c | 4 +++- hw/block/block.c | 12 ++++++++++-- include/block/block.h | 2 ++ 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 05079b40cae..21bdaf969c5 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -44,6 +44,7 @@ #if defined(__APPLE__) && (__MACH__) #include #include +#include #include #include #include @@ -1292,6 +1293,8 @@ static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) if (check_for_dasd(s->fd) < 0) { return -ENOTSUP; } + bsz->opt_io = 0; + bsz->discard_granularity = -1; ret = probe_logical_blocksize(s->fd, &bsz->log); if (ret < 0) { return ret; @@ -1586,6 +1589,7 @@ out: } } +G_GNUC_UNUSED static int translate_err(int err) { if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP || @@ -1795,16 +1799,27 @@ static int handle_aiocb_discard(void *opaque) } } while (errno == EINTR); - ret = -errno; + ret = translate_err(-errno); #endif } else { #ifdef CONFIG_FALLOCATE_PUNCH_HOLE ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, aiocb->aio_offset, aiocb->aio_nbytes); + ret = translate_err(-errno); +#elif defined(__APPLE__) && (__MACH__) + fpunchhole_t fpunchhole; + fpunchhole.fp_flags = 0; + fpunchhole.reserved = 0; + fpunchhole.fp_offset = aiocb->aio_offset; + fpunchhole.fp_length = aiocb->aio_nbytes; + if (fcntl(s->fd, F_PUNCHHOLE, &fpunchhole) == -1) { + ret = errno == ENODEV ? -ENOTSUP : -errno; + } else { + ret = 0; + } #endif } - ret = translate_err(ret); if (ret == -ENOTSUP) { s->has_discard = false; } @@ -2113,6 +2128,26 @@ static int raw_co_flush_to_disk(BlockDriverState *bs) return raw_thread_pool_submit(bs, handle_aiocb_flush, &acb); } +static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) +{ +#if defined(__APPLE__) && (__MACH__) + BDRVRawState *s = bs->opaque; + struct statfs buf; + + if (!fstatfs(s->fd, &buf)) { + bsz->phys = 0; + bsz->log = 0; + bsz->opt_io = buf.f_iosize; + bsz->discard_granularity = buf.f_bsize; + return 0; + } + + return -errno; +#else + return -ENOTSUP; +#endif +} + static void raw_aio_attach_aio_context(BlockDriverState *bs, AioContext *new_context) { @@ -3247,6 +3282,7 @@ BlockDriver bdrv_file = { .bdrv_refresh_limits = raw_refresh_limits, .bdrv_io_plug = raw_aio_plug, .bdrv_io_unplug = raw_aio_unplug, + .bdrv_probe_blocksizes = raw_probe_blocksizes, .bdrv_attach_aio_context = raw_aio_attach_aio_context, .bdrv_co_truncate = raw_co_truncate, diff --git a/block/nvme.c b/block/nvme.c index 2b5421e7aa6..1845d07577b 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -989,6 +989,8 @@ static int nvme_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) uint32_t blocksize = nvme_get_blocksize(bs); bsz->phys = blocksize; bsz->log = blocksize; + bsz->opt_io = 0; + bsz->discard_granularity = -1; return 0; } diff --git a/block/raw-format.c b/block/raw-format.c index 7717578ed6a..847df11f2ae 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -507,6 +507,7 @@ static int raw_probe(const uint8_t *buf, int buf_size, const char *filename) static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) { BDRVRawState *s = bs->opaque; + uint32_t size; int ret; ret = bdrv_probe_blocksizes(bs->file->bs, bsz); @@ -514,7 +515,8 @@ static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) return ret; } - if (!QEMU_IS_ALIGNED(s->offset, MAX(bsz->log, bsz->phys))) { + size = MAX(bsz->log, bsz->phys); + if (size && !QEMU_IS_ALIGNED(s->offset, size)) { return -ENOTSUP; } diff --git a/hw/block/block.c b/hw/block/block.c index 1e34573da71..c907e5a7722 100644 --- a/hw/block/block.c +++ b/hw/block/block.c @@ -70,19 +70,27 @@ bool blkconf_blocksizes(BlockConf *conf, Error **errp) backend_ret = blk_probe_blocksizes(blk, &blocksizes); /* fill in detected values if they are not defined via qemu command line */ if (!conf->physical_block_size) { - if (!backend_ret) { + if (!backend_ret && blocksizes.phys) { conf->physical_block_size = blocksizes.phys; } else { conf->physical_block_size = BDRV_SECTOR_SIZE; } } if (!conf->logical_block_size) { - if (!backend_ret) { + if (!backend_ret && blocksizes.log) { conf->logical_block_size = blocksizes.log; } else { conf->logical_block_size = BDRV_SECTOR_SIZE; } } + if (!backend_ret) { + if (!conf->opt_io_size) { + conf->opt_io_size = blocksizes.opt_io; + } + if (conf->discard_granularity == -1) { + conf->discard_granularity = blocksizes.discard_granularity; + } + } if (conf->logical_block_size > conf->physical_block_size) { error_setg(errp, diff --git a/include/block/block.h b/include/block/block.h index b3f6e509d49..d12471a6cc4 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -93,6 +93,8 @@ typedef enum { typedef struct BlockSizes { uint32_t phys; uint32_t log; + uint32_t discard_granularity; + uint32_t opt_io; } BlockSizes; typedef struct HDGeometry { From patchwork Wed Mar 10 15:39:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 12128249 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8A54DC433E0 for ; Wed, 10 Mar 2021 15:41:41 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2F38B64F60 for ; Wed, 10 Mar 2021 15:41:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2F38B64F60 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:40220 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lK0xw-0003AL-6J for qemu-devel@archiver.kernel.org; Wed, 10 Mar 2021 10:41:40 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:56636) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lK0vv-0001f6-0O; Wed, 10 Mar 2021 10:39:35 -0500 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]:50737) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lK0vt-0000xF-6w; Wed, 10 Mar 2021 10:39:34 -0500 Received: by mail-pj1-x1036.google.com with SMTP id gb6so1828266pjb.0; Wed, 10 Mar 2021 07:39:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=a4qd/bBN/3ZRggdH+NcenLbXsMn80TQHVruC1T64rwQ=; b=Cy/daR+EjzCVZfzr9D98/p8D4xJOFQ45aXoKPsEvXAiRnQMkmqNAW1xcwM6APQ+7qs 642fnYovjog1pYkzcmg+k4Rs1MDeD23gG48D/vPDO7MzX/tL7l6ZqgLaGMcoIhbkfsYJ rHxUALRJVBcrboajBYwFggJ5T0ZeateQgESHfsGJbyQzemuCLZbA0gAnH8GnvTyb7heT ESfNGO/MRblfvZtqF0abqm+knXKBJEqklj4S/6wFGZCRG5Xdw6ZheebSlUtGE4yTe14X /jjKwCXF4tzCWbHYzT2oM2JX9wAOFYDwkFgSGY61vyupcRE6suiSstrwfkGzyS8GNH78 kSAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a4qd/bBN/3ZRggdH+NcenLbXsMn80TQHVruC1T64rwQ=; b=O5/1f3H8iNOpo018qTETD3MRklgJDqRXvKS0uPFMhB+5aqFnu/Lsw8PwmpsZloszj2 9iZ5LErBxn6yXNMKJRo7QEF0no8ZEcG/ORViEb3gaigL7BnDocEv0/WFRCTTxRUpnELA NCEsLoU7M3U+ypXGKzqjQ0L12yCVKMLXaUBnILoMptjT4AFBX2fBp90m+RTKe5Sr2YdH Y1PjpX11Seh/W3nO4BjKhx+ILPTVOsKQPYzuCAo1xpYSeayWO2T3N/3cM62L0kMo7qzB JQ6nW7kIh45qtZbGfkfMWsyQbvzMFtFYsid2acYG8Ddpt8y7zHdFIJP9MEd2pcBCyBYc q25A== X-Gm-Message-State: AOAM533PPBPjWwXUJMJyp4n6OhD740EZWIkY8odtUyMvoQQpwpN2P/Mg YgGuTU9QdMcDWaIBs8gl8norjlvAC1SOvQ== X-Google-Smtp-Source: ABdhPJxRjyfP1nbJLCDgITZGtQ+hSQkBhs/VuG/e5/8vZszaqX/PMl6FCHpEQdx2g5m6qFNBUvCKZA== X-Received: by 2002:a17:90a:6385:: with SMTP id f5mr4217115pjj.91.1615390771036; Wed, 10 Mar 2021 07:39:31 -0800 (PST) Received: from localhost.localdomain ([2400:4050:c360:8200:681d:e6e5:d1b0:3153]) by smtp.gmail.com with ESMTPSA id s12sm16324388pgj.70.2021.03.10.07.39.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Mar 2021 07:39:30 -0800 (PST) From: Akihiko Odaki To: Subject: [PATCH v3 2/2] block: Add backend_defaults property Date: Thu, 11 Mar 2021 00:39:16 +0900 Message-Id: <20210310153916.64256-2-akihiko.odaki@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210310153916.64256-1-akihiko.odaki@gmail.com> References: <20210310153916.64256-1-akihiko.odaki@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=akihiko.odaki@gmail.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: Kevin Wolf , Fam Zheng , pkrempa@redhat.com, Akihiko Odaki , qemu-block@nongnu.org, Markus Armbruster , qemu-devel@nongnu.org, Max Reitz , Konstantin Nazarov , Stefan Hajnoczi , John Snow , dgilbert@redhat.com Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" backend_defaults property allow users to control if default block properties should be decided with backend information. If it is off, any backend information will be discarded, which is suitable if you plan to perform live migration to a different disk backend. If it is on, a block device may utilize backend information more aggressively. By default, it is auto, which uses backend information from physical devices and ignores one from file. It is consistent with the older versions, and should be aligned with the user expectation that a file backend is more independent of host than a physical device backend. Signed-off-by: Akihiko Odaki --- block/file-posix.c | 2 ++ block/nvme.c | 1 + hw/block/block.c | 27 ++++++++++++++++++++++----- include/block/block.h | 1 + include/hw/block/block.h | 3 +++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 21bdaf969c5..2631bd972a5 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1293,6 +1293,7 @@ static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) if (check_for_dasd(s->fd) < 0) { return -ENOTSUP; } + bsz->file = false; bsz->opt_io = 0; bsz->discard_granularity = -1; ret = probe_logical_blocksize(s->fd, &bsz->log); @@ -2135,6 +2136,7 @@ static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) struct statfs buf; if (!fstatfs(s->fd, &buf)) { + bsz->file = true; bsz->phys = 0; bsz->log = 0; bsz->opt_io = buf.f_iosize; diff --git a/block/nvme.c b/block/nvme.c index 1845d07577b..8438c8a2b90 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -987,6 +987,7 @@ static uint32_t nvme_get_blocksize(BlockDriverState *bs) static int nvme_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) { uint32_t blocksize = nvme_get_blocksize(bs); + bsz->file = false; bsz->phys = blocksize; bsz->log = blocksize; bsz->opt_io = 0; diff --git a/hw/block/block.c b/hw/block/block.c index c907e5a7722..d42cc40a1c8 100644 --- a/hw/block/block.c +++ b/hw/block/block.c @@ -65,25 +65,42 @@ bool blkconf_blocksizes(BlockConf *conf, Error **errp) { BlockBackend *blk = conf->blk; BlockSizes blocksizes; - int backend_ret; + bool use_blocksizes; + + switch (conf->backend_defaults) { + case ON_OFF_AUTO_AUTO: + use_blocksizes = !blk_probe_blocksizes(blk, &blocksizes) && + !blocksizes.file; + break; + + case ON_OFF_AUTO_ON: + use_blocksizes = !blk_probe_blocksizes(blk, &blocksizes); + break; + + case ON_OFF_AUTO_OFF: + use_blocksizes = false; + break; + + default: + abort(); + } - backend_ret = blk_probe_blocksizes(blk, &blocksizes); /* fill in detected values if they are not defined via qemu command line */ if (!conf->physical_block_size) { - if (!backend_ret && blocksizes.phys) { + if (use_blocksizes && blocksizes.phys) { conf->physical_block_size = blocksizes.phys; } else { conf->physical_block_size = BDRV_SECTOR_SIZE; } } if (!conf->logical_block_size) { - if (!backend_ret && blocksizes.log) { + if (use_blocksizes && blocksizes.log) { conf->logical_block_size = blocksizes.log; } else { conf->logical_block_size = BDRV_SECTOR_SIZE; } } - if (!backend_ret) { + if (use_blocksizes) { if (!conf->opt_io_size) { conf->opt_io_size = blocksizes.opt_io; } diff --git a/include/block/block.h b/include/block/block.h index d12471a6cc4..ccc3ec5c847 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -91,6 +91,7 @@ typedef enum { } BdrvRequestFlags; typedef struct BlockSizes { + bool file; uint32_t phys; uint32_t log; uint32_t discard_granularity; diff --git a/include/hw/block/block.h b/include/hw/block/block.h index c172cbe65f1..5902c0440a5 100644 --- a/include/hw/block/block.h +++ b/include/hw/block/block.h @@ -19,6 +19,7 @@ typedef struct BlockConf { BlockBackend *blk; + OnOffAuto backend_defaults; uint32_t physical_block_size; uint32_t logical_block_size; uint32_t min_io_size; @@ -48,6 +49,8 @@ static inline unsigned int get_physical_block_exp(BlockConf *conf) } #define DEFINE_BLOCK_PROPERTIES_BASE(_state, _conf) \ + DEFINE_PROP_ON_OFF_AUTO("backend_defaults", _state, \ + _conf.backend_defaults, ON_OFF_AUTO_AUTO), \ DEFINE_PROP_BLOCKSIZE("logical_block_size", _state, \ _conf.logical_block_size), \ DEFINE_PROP_BLOCKSIZE("physical_block_size", _state, \