From nobody Tue Feb 10 02:00:55 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=1662788542; cv=none; d=zohomail.com; s=zohoarc; b=iWPC8xstl9AmkLVRbNtQbVwVpWmX3t3oYDSGdW33PNwkGYFBRg10xr9YK1sfLHlLlCdvkv+gtBa4nofgIGXcxy0uVca+7k2JWZ1REOhfudFoRzNjvlxJzhfeSVFjBBGBvWj6VYvWuBhKDBFRhBDlUkbktYl86VJvfe2JYTpqkic= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1662788542; 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=HK20SnxvC2yqSmScJhmV/FP8kW87JPGEWe5pmoXhJX0=; b=mBE8uSSNpMcm82zW1fIEqDQdEuO5ILDXAfjFryDK+lFMKqR9n+V9iaqI6YcVN5ZtfiyR8V3i7ALCMSnWKxILlsxdI3j1wHVZyHypc/S7Es0pbf+J3A18cGM5x6mj+hJSfFXPV8air9zXDOOK7c1x3st4RZjFVg6elTL9qGJDk9o= 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 16627885425201008.4263825614323; Fri, 9 Sep 2022 22:42:22 -0700 (PDT) Received: from localhost ([::1]:42792 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oWtG1-0003Ar-4y for importer@patchew.org; Sat, 10 Sep 2022 01:42:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49946) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oWt2t-0002CM-EF; Sat, 10 Sep 2022 01:28:47 -0400 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]:45848) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oWt2r-0004ne-K8; Sat, 10 Sep 2022 01:28:47 -0400 Received: by mail-pg1-x536.google.com with SMTP id h188so3390069pgc.12; Fri, 09 Sep 2022 22:28:44 -0700 (PDT) Received: from roots.. ([183.221.93.83]) by smtp.gmail.com with ESMTPSA id y6-20020aa793c6000000b00540d75197e5sm731994pff.47.2022.09.09.22.28.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Sep 2022 22:28:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=HK20SnxvC2yqSmScJhmV/FP8kW87JPGEWe5pmoXhJX0=; b=OHVNbR9xUEWIcymjz8JD5l7XbCSdVSIUoqWIw9zuySOPYjL7kprGxVHfvDvyLVt0C9 cP0+66JOvKsLKrIWr2yDQ8aycUuh9oqLNXVfEjHkQW4iN9JncF+znJENeldzAKlyxKNr 5Ft88HZm+1073U1iTFqv+FJ2zcTP2qjG+d95TeZEbCefBmoCmfC7hnoqiUsr16jhNapJ i8TsIysMq+WFAb8KgVE7NDH4RugcjDWJCS0XRLId6kaebrpRmo2uw0PRXWr1VqEOGb8r h2i8b0l2Fjuq/prQQ2ps4Y/w6mgQYmOfCvaN5jkPRDUfvZy4bLAR+TxFyKmvGKYnY1hl RyOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=HK20SnxvC2yqSmScJhmV/FP8kW87JPGEWe5pmoXhJX0=; b=4jYCE+WyhsWbXt9uUKCrQwkmjyqCzFzG/045HmMtukdX8YdGYc2FwbSqQcc7jmgFrk uOa60dHJ9HQEortJRb0LDMCLOmAiABTaS0B3IhqUnBtJbwjX8MBCeHPoOvbAAyzX3yx4 vhaSYOgfsCd0RFTIT0aRzMhDvbUrJoPzE1pNhGE+NQt3kkyBCL42CP3mRbIpc8+rucN8 J7R6frGlUZ93JXMjMHz6J2mncO2mdE9DALM776TCaJAEzJE89eUIndCPvziLrFyx5NVo bepigp5VNQVkh1u3sAWsAMmHMtttpDFVS3j1+D0cZ+0jDNewue8V9bnvPe2wR76K941y P88g== X-Gm-Message-State: ACgBeo3V3ytGTyId4L8hMuGuDmM9iiuhq2i2THBHOFB9ww23NDL1K+Pn GSZ6IctK7n2ZYo9+e7c306mJ7eUqlTTE/8DL X-Google-Smtp-Source: AA6agR7kkOgUgHRBf01vf2P2JBuuo79SZfxiPo+W675ZqwEstzByE8M1OZVL6TY0l6ANHslCjXUCcg== X-Received: by 2002:a63:81c8:0:b0:438:919e:8256 with SMTP id t191-20020a6381c8000000b00438919e8256mr4495500pgd.39.1662787723488; Fri, 09 Sep 2022 22:28:43 -0700 (PDT) From: Sam Li To: qemu-devel@nongnu.org Cc: dmitry.fomichev@wdc.com, Markus Armbruster , Eric Blake , qemu-block@nongnu.org, Stefan Hajnoczi , Kevin Wolf , Fam Zheng , damien.lemoal@opensource.wdc.com, hare@suse.de, Hanna Reitz , Sam Li Subject: [PATCH v9 2/7] file-posix: introduce helper functions for sysfs attributes Date: Sat, 10 Sep 2022 13:27:54 +0800 Message-Id: <20220910052759.27517-3-faithilikerun@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220910052759.27517-1-faithilikerun@gmail.com> References: <20220910052759.27517-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::536; envelope-from=faithilikerun@gmail.com; helo=mail-pg1-x536.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, T_SCC_BODY_TEXT_LINE=-0.01 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" X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1662788543914100001 Content-Type: text/plain; charset="utf-8" Use get_sysfs_str_val() to get the string value of device zoned model. Then get_sysfs_zoned_model() can convert it to BlockZoneModel type of QEMU. Use get_sysfs_long_val() to get the long value of zoned device information. Signed-off-by: Sam Li Reviewed-by: Hannes Reinecke Reviewed-by: Stefan Hajnoczi Reviewed-by: Damien Le Moal --- block/file-posix.c | 121 ++++++++++++++++++++++--------- include/block/block_int-common.h | 3 + 2 files changed, 88 insertions(+), 36 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 48cd096624..0a8b4b426e 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1210,66 +1210,109 @@ static int hdev_get_max_hw_transfer(int fd, struct= stat *st) #endif } =20 -static int hdev_get_max_segments(int fd, struct stat *st) -{ +/* + * Get a sysfs attribute value as character string. + */ +static int get_sysfs_str_val(struct stat *st, const char *attribute, + char **val) { #ifdef CONFIG_LINUX - char buf[32]; - const char *end; - char *sysfspath =3D NULL; + g_autofree char *sysfspath =3D NULL; int ret; - int sysfd =3D -1; - long max_segments; + size_t len; =20 - if (S_ISCHR(st->st_mode)) { - if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) =3D=3D 0) { - return ret; - } + if (!S_ISBLK(st->st_mode)) { return -ENOTSUP; } =20 - if (!S_ISBLK(st->st_mode)) { - return -ENOTSUP; + sysfspath =3D g_strdup_printf("/sys/dev/block/%u:%u/queue/%s", + major(st->st_rdev), minor(st->st_rdev), + attribute); + ret =3D g_file_get_contents(sysfspath, val, &len, NULL); + if (ret =3D=3D -1) { + return -ENOENT; } =20 - sysfspath =3D g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments= ", - major(st->st_rdev), minor(st->st_rdev)); - sysfd =3D open(sysfspath, O_RDONLY); - if (sysfd =3D=3D -1) { - ret =3D -errno; - goto out; + /* The file is ended with '\n' */ + char *p; + p =3D *val; + if (*(p + len - 1) =3D=3D '\n') { + *(p + len - 1) =3D '\0'; } - do { - ret =3D read(sysfd, buf, sizeof(buf) - 1); - } while (ret =3D=3D -1 && errno =3D=3D EINTR); + return ret; +#else + return -ENOTSUP; +#endif +} + +static int get_sysfs_zoned_model(struct stat *st, BlockZoneModel *zoned) { + g_autofree char *val =3D NULL; + int ret; + + ret =3D get_sysfs_str_val(st, "zoned", &val); if (ret < 0) { - ret =3D -errno; - goto out; - } else if (ret =3D=3D 0) { - ret =3D -EIO; - goto out; + return ret; } - buf[ret] =3D 0; - /* The file is ended with '\n', pass 'end' to accept that. */ - ret =3D qemu_strtol(buf, &end, 10, &max_segments); - if (ret =3D=3D 0 && end && *end =3D=3D '\n') { - ret =3D max_segments; + + if (strcmp(val, "host-managed") =3D=3D 0) { + *zoned =3D BLK_Z_HM; + } else if (strcmp(val, "host-aware") =3D=3D 0) { + *zoned =3D BLK_Z_HA; + } else if (strcmp(val, "none") =3D=3D 0) { + *zoned =3D BLK_Z_NONE; + } else { + return -ENOTSUP; } + return 0; +} =20 -out: - if (sysfd !=3D -1) { - close(sysfd); +/* + * Get a sysfs attribute value as a long integer. + */ +static long get_sysfs_long_val(struct stat *st, const char *attribute) { +#ifdef CONFIG_LINUX + g_autofree char *str =3D NULL; + const char *end; + long val; + int ret; + + ret =3D get_sysfs_str_val(st, attribute, &str); + if (ret < 0) { + return ret; + } + + /* The file is ended with '\n', pass 'end' to accept that. */ + ret =3D qemu_strtol(str, &end, 10, &val); + if (ret =3D=3D 0 && end && *end =3D=3D '\0') { + ret =3D val; } - g_free(sysfspath); return ret; #else return -ENOTSUP; #endif } =20 +static int hdev_get_max_segments(int fd, struct stat *st) { +#ifdef CONFIG_LINUX + int ret; + + if (S_ISCHR(st->st_mode)) { + if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) =3D=3D 0) { + return ret; + } + return -ENOTSUP; + } + return get_sysfs_long_val(st, "max_segments"); +#else + return -ENOTSUP; +#endif +} + static void raw_refresh_limits(BlockDriverState *bs, Error **errp) { BDRVRawState *s =3D bs->opaque; struct stat st; + int ret; + BlockZoneModel zoned; =20 s->needs_alignment =3D raw_needs_alignment(bs); raw_probe_alignment(bs, s->fd, errp); @@ -1307,6 +1350,12 @@ static void raw_refresh_limits(BlockDriverState *bs,= Error **errp) bs->bl.max_hw_iov =3D ret; } } + + ret =3D get_sysfs_zoned_model(&st, &zoned); + if (ret < 0) { + zoned =3D BLK_Z_NONE; + } + bs->bl.zoned =3D zoned; } =20 static int check_for_dasd(int fd) diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 8947abab76..7f7863cc9e 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -825,6 +825,9 @@ typedef struct BlockLimits { =20 /* maximum number of iovec elements */ int max_iov; + + /* device zone model */ + BlockZoneModel zoned; } BlockLimits; =20 typedef struct BdrvOpBlocker BdrvOpBlocker; --=20 2.37.3