From nobody Sat May 11 15:19:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1614946751; cv=none; d=zohomail.com; s=zohoarc; b=DXCu/cT+L1EAKwellAF2vh/0zgUzJ7AiJ+Yct0Z6IHni7wM6OLPZKDHG8qNAcERmG/124o6PqkDMadNPvnfcS16DJnwSiKV7u2sIeomaJh9lPTogKrcrnm7O2J+rMOGEGUegBKqdz173//fs7jXkgC8/+0oq8K1goP8F8dn7PBw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614946751; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject; bh=Wg+mhNkNB4lUrMjlZW3lEf2FPRUUmtAYl68S49LJAb0=; b=g50Ipif26Ws06yM4EcHsQTwjvGObg179vb6d6M6MEy6Elv+a52AttxlVb9TgLTheL8H6WnB7M/vIfN664l8ApQ7fcHjVtTPNHDY/ZzEXQQMUwuiiAznaB7wjaJ8kMEEpj3D3OsfpzMKPsblN6L7aFxIzHKyeG61oKBAFFPRtd5U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.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 161494675120737.22047722406751; Fri, 5 Mar 2021 04:19:11 -0800 (PST) Received: from localhost ([::1]:35590 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lI9QD-0000rA-GH for importer@patchew.org; Fri, 05 Mar 2021 07:19:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:41832) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lI9P6-0000Bk-DU; Fri, 05 Mar 2021 07:18:00 -0500 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]:38692) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lI9P4-0004Jf-Br; Fri, 05 Mar 2021 07:18:00 -0500 Received: by mail-pl1-x635.google.com with SMTP id s7so1333452plg.5; Fri, 05 Mar 2021 04:17:57 -0800 (PST) Received: from localhost.localdomain ([2400:4050:c360:8200:6076:fea8:b0ba:4fdb]) by smtp.gmail.com with ESMTPSA id j26sm2426697pfa.35.2021.03.05.04.17.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 Mar 2021 04:17:55 -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=fbMeq6BdHQWssd0sYWp7JnuREDaFsRlmil1dWIYcsxq4rU4oo1w5NjB7nRKl1aQilA PZiC5lmw3EukI6iawaHvD5hK4N9juDWmiWu750euE6WiPBfYY+BRLliPT1q8yRmJsEyH qN89p6vD1Qk5f0Qbdu/0xESSZYImFTWhvyaGedKySap98lZ2cpCipYiGERzbW1EFIXLH nmUBC2mIUD+BO0xjxKeAAOODoeLLsYDburVgMQsawSn+hgWcZzgyaNITjrR7eHx7qWMo TOSQ7a7cC0IpzPi5qjr3cO8+EKnCNMjeF7evJalfYNfepy1QWNXAxBrwVPo6Qij1c7BN UauQ== 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=dQoqMnTo131TpxboAyE1aF36ZaZ3f1nvmmrumN3YtgAXQ76cDR5rcKuDmJ+/tV6934 m07hHhf18K8fEaUvMVXYEeSjgXzFNTrzZzuQr4GxRZZekXl8hg/ma0IHk9Do0yiZ9Lb/ B8tFzW0gtzgZHXhGKMvvDKLeQIN/KGENNBClbGITKepVYP1Rw38j3AqkiB5PYp9EyrJM eVblpkM2XNknIh/Ean2fzonGsXzjVxP+qZ++WM5iQm9KkxU/5VthmJsPwcqPpOQ8usuc X0eKbAxxlng9Id0J+F/1ogn1RFMtMCchae9HeXJjLUJ4MI18SpbSM9Wy6bSFC3OQE2i1 20ZA== X-Gm-Message-State: AOAM533cO74iwbi/0dPSgN/eapxVlPA2IlplxTJ+ek2kNr0VC2CC/mc2 IFU9wEfjZ4w7xt9nlLWaG/YU5fW4xDFTzA== X-Google-Smtp-Source: ABdhPJyNPTPP2cQdtnVqwE0nw01F/zu9oFWVKri/QlAZz+aOJjL5M5RtSOW/vJylrxmHxqKlXW4b6g== X-Received: by 2002:a17:90a:1a59:: with SMTP id 25mr10188789pjl.54.1614946676192; Fri, 05 Mar 2021 04:17:56 -0800 (PST) From: Akihiko Odaki To: Subject: [PATCH v2] block/file-posix: Optimize for macOS Date: Fri, 5 Mar 2021 21:17:48 +0900 Message-Id: <20210305121748.65173-1-akihiko.odaki@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=akihiko.odaki@gmail.com; helo=mail-pl1-x635.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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" 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#gistco= mment-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 =3D 0; + bsz->discard_granularity =3D -1; ret =3D probe_logical_blocksize(s->fd, &bsz->log); if (ret < 0) { return ret; @@ -1586,6 +1589,7 @@ out: } } =20 +G_GNUC_UNUSED static int translate_err(int err) { if (err =3D=3D -ENODEV || err =3D=3D -ENOSYS || err =3D=3D -EOPNOTSUPP= || @@ -1795,16 +1799,27 @@ static int handle_aiocb_discard(void *opaque) } } while (errno =3D=3D EINTR); =20 - ret =3D -errno; + ret =3D translate_err(-errno); #endif } else { #ifdef CONFIG_FALLOCATE_PUNCH_HOLE ret =3D do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_= SIZE, aiocb->aio_offset, aiocb->aio_nbytes); + ret =3D translate_err(-errno); +#elif defined(__APPLE__) && (__MACH__) + fpunchhole_t fpunchhole; + fpunchhole.fp_flags =3D 0; + fpunchhole.reserved =3D 0; + fpunchhole.fp_offset =3D aiocb->aio_offset; + fpunchhole.fp_length =3D aiocb->aio_nbytes; + if (fcntl(s->fd, F_PUNCHHOLE, &fpunchhole) =3D=3D -1) { + ret =3D errno =3D=3D ENODEV ? -ENOTSUP : -errno; + } else { + ret =3D 0; + } #endif } =20 - ret =3D translate_err(ret); if (ret =3D=3D -ENOTSUP) { s->has_discard =3D false; } @@ -2113,6 +2128,26 @@ static int raw_co_flush_to_disk(BlockDriverState *bs) return raw_thread_pool_submit(bs, handle_aiocb_flush, &acb); } =20 +static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) +{ +#if defined(__APPLE__) && (__MACH__) + BDRVRawState *s =3D bs->opaque; + struct statfs buf; + + if (!fstatfs(s->fd, &buf)) { + bsz->phys =3D 0; + bsz->log =3D 0; + bsz->opt_io =3D buf.f_iosize; + bsz->discard_granularity =3D 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 =3D { .bdrv_refresh_limits =3D raw_refresh_limits, .bdrv_io_plug =3D raw_aio_plug, .bdrv_io_unplug =3D raw_aio_unplug, + .bdrv_probe_blocksizes =3D raw_probe_blocksizes, .bdrv_attach_aio_context =3D raw_aio_attach_aio_context, =20 .bdrv_co_truncate =3D 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 =3D nvme_get_blocksize(bs); bsz->phys =3D blocksize; bsz->log =3D blocksize; + bsz->opt_io =3D 0; + bsz->discard_granularity =3D -1; return 0; } =20 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 =3D bs->opaque; + uint32_t size; int ret; =20 ret =3D bdrv_probe_blocksizes(bs->file->bs, bsz); @@ -514,7 +515,8 @@ static int raw_probe_blocksizes(BlockDriverState *bs, B= lockSizes *bsz) return ret; } =20 - if (!QEMU_IS_ALIGNED(s->offset, MAX(bsz->log, bsz->phys))) { + size =3D MAX(bsz->log, bsz->phys); + if (size && !QEMU_IS_ALIGNED(s->offset, size)) { return -ENOTSUP; } =20 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 =3D blk_probe_blocksizes(blk, &blocksizes); /* fill in detected values if they are not defined via qemu command li= ne */ if (!conf->physical_block_size) { - if (!backend_ret) { + if (!backend_ret && blocksizes.phys) { conf->physical_block_size =3D blocksizes.phys; } else { conf->physical_block_size =3D BDRV_SECTOR_SIZE; } } if (!conf->logical_block_size) { - if (!backend_ret) { + if (!backend_ret && blocksizes.log) { conf->logical_block_size =3D blocksizes.log; } else { conf->logical_block_size =3D BDRV_SECTOR_SIZE; } } + if (!backend_ret) { + if (!conf->opt_io_size) { + conf->opt_io_size =3D blocksizes.opt_io; + } + if (conf->discard_granularity =3D=3D -1) { + conf->discard_granularity =3D blocksizes.discard_granularity; + } + } =20 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; =20 typedef struct HDGeometry { --=20 2.24.3 (Apple Git-128)