From nobody Mon Feb 9 18:18:52 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1680622431; cv=none; d=zohomail.com; s=zohoarc; b=j5MFHpahhlPnhDlyHowykQMvgDK8puJ2rfQjDNRR8ncgshg8qXYX5tKQVthmXPPpZA6PdRnEqtU1Ieml/dQlg3wl84qUGWShFsx19jp8gJAWmFjRA/heCXWHfyuULFMjTGFdyAzSQZzyTZiE4SGiVRcj+DVnImQcqmiytQ48S5U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680622431; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=PDwFnbwAQ628E24lv/sil+v21rnO8HvTED6y6PCZ2dg=; b=CK7m+X5tXp+R/42a6VNJIC4mhNyBmn+8KF8aRkKwjmd2hKO5WPqaucH5aCF3f1ozcvtuk7IPMJMHZqqU0QlKOAu24Etd1a3XVSTUJIP/URskHdP8yNpbCsLDJhuKEaxT5CsNqscrCXzxtjFS9DFMTBEg/QdPTHn33ib6vN1Kr2c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680622431677890.967367028558; Tue, 4 Apr 2023 08:33:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pjieY-0004Ve-Qt; Tue, 04 Apr 2023 11:32:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pjieX-0004VA-Gn; Tue, 04 Apr 2023 11:32:57 -0400 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pjieU-0005gX-Kr; Tue, 04 Apr 2023 11:32:56 -0400 Received: by mail-pf1-x435.google.com with SMTP id cm5so15882094pfb.0; Tue, 04 Apr 2023 08:32:53 -0700 (PDT) Received: from fedlinux.. ([106.84.131.166]) by smtp.gmail.com with ESMTPSA id p11-20020a62ab0b000000b0059085684b54sm9231519pff.140.2023.04.04.08.32.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 08:32:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680622372; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PDwFnbwAQ628E24lv/sil+v21rnO8HvTED6y6PCZ2dg=; b=lTCCeKIJI6HMj6akX9A6EGTuh2rGu9NmXzl10LG/1hJ6ER5S+HDe/7K4RsrJE/6wZu 7VBuKi48JjVSBGe2jlw9yy6f2O0CdIap50QWwF3BK11OcYNrX3v8Fd11g8P1R9OoDLN0 rA906zhPaQM1A8fDty+VNFO9E6j6VAOEoIldm1kqdqRB210nAqRabVeIQOdHeArNLQje p7BHTlUYuOBYhkQauzohLPJF8bYi+z/jHlQz5WvStZJEzup+LpARXywxFWwJ5VbG5pry F43Mugeg+NMZdJ0baqrA/gLL2waL0t44Fu/EbxjixMl2SQVK1LVnD8Ith04Vf5XXmSKu PmOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680622372; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PDwFnbwAQ628E24lv/sil+v21rnO8HvTED6y6PCZ2dg=; b=m/LPyI/3b2W0ct8Br06SXiI0+APxDCt5Ml2exc23YwwPpkl4sjUo8rA6yvHJkKn0fp DgtV0TfR1p+DvSp1VupT3f9BAQHEff1w9uJrctrsZhHVtlWWiacmjtLk57Zb2kVl6wf5 0CF2Ocr6ed3HqmW8R85W/2sTaFP8USa+B6+9VqYo3yo56vLdC26FApjvYzb5socIHCsX z5+aZ4Mi8k3hwubxTN9xUoG83yHQhoJZ4uSwpO6gkn9ynhQUvnuGpkBJYO1A42bAYOQ0 AsEO1HZWWmdUMhpt6XoaK4Tnx1ceVhXcvDjqoxvcgagov/vNKOeibx6/sQ5B8W6mwjDF +Zgw== X-Gm-Message-State: AAQBX9d46zK8H8KYFJtFra9uyrRokDDrs2F228Mh7+SwRTUipS2Dhl+6 HtWf4kI8S5vgcFqx6nVSXZfijJUp6BCnjb+JlCk= X-Google-Smtp-Source: AKy350bEKRQH3qKBiTjmUTjW6K7/xzA0cY2C/2b3KnbA8GYue69mJ4eTJ1J7JNx5L2Sqo9ctQ3a4RA== X-Received: by 2002:aa7:950d:0:b0:62d:d045:392 with SMTP id b13-20020aa7950d000000b0062dd0450392mr2847156pfp.32.1680622372301; Tue, 04 Apr 2023 08:32:52 -0700 (PDT) From: Sam Li To: qemu-devel@nongnu.org Cc: Fam Zheng , hare@suse.de, Hanna Reitz , dmitry.fomichev@wdc.com, Julia Suvorova , Stefano Garzarella , damien.lemoal@opensource.wdc.com, Stefan Hajnoczi , Aarushi Mehta , Kevin Wolf , qemu-block@nongnu.org, Sam Li Subject: [PATCH v8 1/4] file-posix: add tracking of the zone write pointers Date: Tue, 4 Apr 2023 23:32:36 +0800 Message-Id: <20230404153239.32234-2-faithilikerun@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230404153239.32234-1-faithilikerun@gmail.com> References: <20230404153239.32234-1-faithilikerun@gmail.com> 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::435; envelope-from=faithilikerun@gmail.com; helo=mail-pf1-x435.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1680622433235100001 Content-Type: text/plain; charset="utf-8" Since Linux doesn't have a user API to issue zone append operations to zoned devices from user space, the file-posix driver is modified to add zone append emulation using regular writes. To do this, the file-posix driver tracks the wp location of all zones of the device. It uses an array of uint64_t. The most significant bit of each wp location indicates if the zone type is conventional zones. The zones wp can be changed due to the following operations issued: - zone reset: change the wp to the start offset of that zone - zone finish: change to the end location of that zone - write to a zone - zone append Signed-off-by: Sam Li --- block/file-posix.c | 168 ++++++++++++++++++++++++++++++- include/block/block-common.h | 14 +++ include/block/block_int-common.h | 5 + 3 files changed, 184 insertions(+), 3 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 65efe5147e..bc58f7193b 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1324,6 +1324,88 @@ static int hdev_get_max_segments(int fd, struct stat= *st) #endif } =20 +#if defined(CONFIG_BLKZONED) +/* + * If the reset_all flag is true, then the wps of zone whose state is + * not readonly or offline should be all reset to the start sector. + * Else, take the real wp of the device. + */ +static int get_zones_wp(int fd, BlockZoneWps *wps, int64_t offset, + unsigned int nrz, bool reset_all) +{ + struct blk_zone *blkz; + size_t rep_size; + uint64_t sector =3D offset >> BDRV_SECTOR_BITS; + int ret, n =3D 0, i =3D 0; + rep_size =3D sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_= zone); + g_autofree struct blk_zone_report *rep =3D NULL; + + rep =3D g_malloc(rep_size); + blkz =3D (struct blk_zone *)(rep + 1); + while (n < nrz) { + memset(rep, 0, rep_size); + rep->sector =3D sector; + rep->nr_zones =3D nrz - n; + + do { + ret =3D ioctl(fd, BLKREPORTZONE, rep); + } while (ret !=3D 0 && errno =3D=3D EINTR); + if (ret !=3D 0) { + error_report("%d: ioctl BLKREPORTZONE at %" PRId64 " failed %d= ", + fd, offset, errno); + return -errno; + } + + if (!rep->nr_zones) { + break; + } + + for (i =3D 0; i < rep->nr_zones; i++, n++) { + /* + * The wp tracking cares only about sequential writes required= and + * sequential write preferred zones so that the wp can advance= to + * the right location. + * Use the most significant bit of the wp location to indicate= the + * zone type: 0 for SWR/SWP zones and 1 for conventional zones. + */ + if (blkz[i].type =3D=3D BLK_ZONE_TYPE_CONVENTIONAL) { + wps->wp[i] &=3D 1ULL << 63; + } else { + switch(blkz[i].cond) { + case BLK_ZONE_COND_FULL: + case BLK_ZONE_COND_READONLY: + /* Zone not writable */ + wps->wp[i] =3D (blkz[i].start + blkz[i].len) << BDRV_S= ECTOR_BITS; + break; + case BLK_ZONE_COND_OFFLINE: + /* Zone not writable nor readable */ + wps->wp[i] =3D (blkz[i].start) << BDRV_SECTOR_BITS; + break; + default: + if (reset_all) { + wps->wp[i] =3D blkz[i].start << BDRV_SECTOR_BITS; + } else { + wps->wp[i] =3D blkz[i].wp << BDRV_SECTOR_BITS; + } + break; + } + } + } + sector =3D blkz[i - 1].start + blkz[i - 1].len; + } + + return 0; +} + +static void update_zones_wp(int fd, BlockZoneWps *wps, int64_t offset, + unsigned int nrz) +{ + if (get_zones_wp(fd, wps, offset, nrz, 0) < 0) { + error_report("update zone wp failed"); + } +} +#endif + static void raw_refresh_limits(BlockDriverState *bs, Error **errp) { BDRVRawState *s =3D bs->opaque; @@ -1413,6 +1495,23 @@ static void raw_refresh_limits(BlockDriverState *bs,= Error **errp) if (ret >=3D 0) { bs->bl.max_active_zones =3D ret; } + + ret =3D get_sysfs_long_val(&st, "physical_block_size"); + if (ret >=3D 0) { + bs->bl.write_granularity =3D ret; + } + + /* The refresh_limits() function can be called multiple times. */ + bs->wps =3D NULL; + bs->wps =3D g_malloc(sizeof(BlockZoneWps) + + sizeof(int64_t) * bs->bl.nr_zones); + ret =3D get_zones_wp(s->fd, bs->wps, 0, bs->bl.nr_zones, 0); + if (ret < 0) { + error_setg_errno(errp, -ret, "report wps failed"); + bs->wps =3D NULL; + return; + } + qemu_co_mutex_init(&bs->wps->colock); return; } out: @@ -2338,9 +2437,15 @@ static int coroutine_fn raw_co_prw(BlockDriverState = *bs, uint64_t offset, { BDRVRawState *s =3D bs->opaque; RawPosixAIOData acb; + int ret; =20 if (fd_open(bs) < 0) return -EIO; +#if defined(CONFIG_BLKZONED) + if (type & QEMU_AIO_WRITE && bs->wps) { + QEMU_LOCK_GUARD(&bs->wps->colock); + } +#endif =20 /* * When using O_DIRECT, the request must be aligned to be able to use @@ -2354,14 +2459,16 @@ static int coroutine_fn raw_co_prw(BlockDriverState= *bs, uint64_t offset, } else if (s->use_linux_io_uring) { LuringState *aio =3D aio_get_linux_io_uring(bdrv_get_aio_context(b= s)); assert(qiov->size =3D=3D bytes); - return luring_co_submit(bs, aio, s->fd, offset, qiov, type); + ret =3D luring_co_submit(bs, aio, s->fd, offset, qiov, type); + goto out; #endif #ifdef CONFIG_LINUX_AIO } else if (s->use_linux_aio) { LinuxAioState *aio =3D aio_get_linux_aio(bdrv_get_aio_context(bs)); assert(qiov->size =3D=3D bytes); - return laio_co_submit(bs, aio, s->fd, offset, qiov, type, + ret =3D laio_co_submit(bs, aio, s->fd, offset, qiov, type, s->aio_max_batch); + goto out; #endif } =20 @@ -2378,7 +2485,29 @@ static int coroutine_fn raw_co_prw(BlockDriverState = *bs, uint64_t offset, }; =20 assert(qiov->size =3D=3D bytes); - return raw_thread_pool_submit(bs, handle_aiocb_rw, &acb); + ret =3D raw_thread_pool_submit(bs, handle_aiocb_rw, &acb); + +out: +#if defined(CONFIG_BLKZONED) + BlockZoneWps *wps =3D bs->wps; + if (ret =3D=3D 0) { + if (type & QEMU_AIO_WRITE && wps && bs->bl.zone_size) { + uint64_t *wp =3D &wps->wp[offset / bs->bl.zone_size]; + if (!BDRV_ZT_IS_CONV(*wp)) { + /* Advance the wp if needed */ + if (offset + bytes > *wp) { + *wp =3D offset + bytes; + } + } + } + } else { + if (type & QEMU_AIO_WRITE) { + update_zones_wp(s->fd, bs->wps, 0, 1); + } + } + +#endif + return ret; } =20 static int coroutine_fn raw_co_preadv(BlockDriverState *bs, int64_t offset, @@ -2486,6 +2615,9 @@ static void raw_close(BlockDriverState *bs) BDRVRawState *s =3D bs->opaque; =20 if (s->fd >=3D 0) { +#if defined(CONFIG_BLKZONED) + g_free(bs->wps); +#endif qemu_close(s->fd); s->fd =3D -1; } @@ -3283,6 +3415,7 @@ static int coroutine_fn raw_co_zone_mgmt(BlockDriverS= tate *bs, BlockZoneOp op, const char *op_name; unsigned long zo; int ret; + BlockZoneWps *wps =3D bs->wps; int64_t capacity =3D bs->total_sectors << BDRV_SECTOR_BITS; =20 zone_size =3D bs->bl.zone_size; @@ -3300,6 +3433,15 @@ static int coroutine_fn raw_co_zone_mgmt(BlockDriver= State *bs, BlockZoneOp op, return -EINVAL; } =20 + QEMU_LOCK_GUARD(&wps->colock); + uint32_t i =3D offset / bs->bl.zone_size; + uint32_t nrz =3D len / bs->bl.zone_size; + uint64_t *wp =3D &wps->wp[i]; + if (BDRV_ZT_IS_CONV(*wp) && len !=3D capacity) { + error_report("zone mgmt operations are not allowed for conventiona= l zones"); + return -EIO; + } + switch (op) { case BLK_ZO_OPEN: op_name =3D "BLKOPENZONE"; @@ -3337,8 +3479,28 @@ static int coroutine_fn raw_co_zone_mgmt(BlockDriver= State *bs, BlockZoneOp op, len >> BDRV_SECTOR_BITS); ret =3D raw_thread_pool_submit(bs, handle_aiocb_zone_mgmt, &acb); if (ret !=3D 0) { + update_zones_wp(s->fd, wps, offset, i); ret =3D -errno; error_report("ioctl %s failed %d", op_name, ret); + return ret; + } + + if (zo =3D=3D BLKRESETZONE && len =3D=3D capacity) { + ret =3D get_zones_wp(s->fd, wps, 0, bs->bl.nr_zones, 1); + if (ret < 0) { + error_report("reporting single wp failed"); + return ret; + } + } else if (zo =3D=3D BLKRESETZONE) { + for (int j =3D 0; j < nrz; ++j) { + wp[j] =3D offset + j * zone_size; + } + } else if (zo =3D=3D BLKFINISHZONE) { + for (int j =3D 0; j < nrz; ++j) { + /* The zoned device allows the last zone smaller that the + * zone size. */ + wp[j] =3D MIN(offset + (j + 1) * zone_size, offset + len); + } } =20 return ret; diff --git a/include/block/block-common.h b/include/block/block-common.h index 1576fcf2ed..93196229ac 100644 --- a/include/block/block-common.h +++ b/include/block/block-common.h @@ -118,6 +118,14 @@ typedef struct BlockZoneDescriptor { BlockZoneState state; } BlockZoneDescriptor; =20 +/* + * Track write pointers of a zone in bytes. + */ +typedef struct BlockZoneWps { + CoMutex colock; + uint64_t wp[]; +} BlockZoneWps; + typedef struct BlockDriverInfo { /* in bytes, 0 if irrelevant */ int cluster_size; @@ -240,6 +248,12 @@ typedef enum { #define BDRV_SECTOR_BITS 9 #define BDRV_SECTOR_SIZE (1ULL << BDRV_SECTOR_BITS) =20 +/* + * Get the first most significant bit of wp. If it is zero, then + * the zone type is SWR. + */ +#define BDRV_ZT_IS_CONV(wp) (wp & (1ULL << 63)) + #define BDRV_REQUEST_MAX_SECTORS MIN_CONST(SIZE_MAX >> BDRV_SECTOR_BITS, \ INT_MAX >> BDRV_SECTOR_BITS) #define BDRV_REQUEST_MAX_BYTES (BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BI= TS) diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 1bd2aef4d5..b34a7f175d 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -884,6 +884,8 @@ typedef struct BlockLimits { =20 /* maximum number of active zones */ int64_t max_active_zones; + + int64_t write_granularity; } BlockLimits; =20 typedef struct BdrvOpBlocker BdrvOpBlocker; @@ -1245,6 +1247,9 @@ struct BlockDriverState { CoMutex bsc_modify_lock; /* Always non-NULL, but must only be dereferenced under an RCU read gu= ard */ BdrvBlockStatusCache *block_status_cache; + + /* array of write pointers' location of each zone in the zoned device.= */ + BlockZoneWps *wps; }; =20 struct BlockBackendRootState { --=20 2.39.2