From patchwork Mon Dec 19 07:29:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Li X-Patchwork-Id: 1717171 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=QYs2NOYw; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NbBGH0gqwz1ydc for ; Mon, 19 Dec 2022 18:31:19 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p7Aan-0001JL-23; Mon, 19 Dec 2022 02:29:45 -0500 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 1p7Aal-0001J3-74; Mon, 19 Dec 2022 02:29:43 -0500 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p7Aaj-00036z-Ce; Mon, 19 Dec 2022 02:29:42 -0500 Received: by mail-pf1-x42c.google.com with SMTP id t18so5575104pfq.13; Sun, 18 Dec 2022 23:29:40 -0800 (PST) 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 :message-id:reply-to; bh=MlcDGmAU18RkE+OnLhtYCV6jx+QzKtfdj07+/quau8k=; b=QYs2NOYwT9lE5Dne1MWlrlarris6GeqjejDKPyJZs+n0oUBOc7yloOHuI4O6szct18 P/GIKFJmQLMf+tobrmO0bzbxMLQoov4XrPBQFevhpCcXWj14JoSiUG0Lf7xFcCVnZ1Ax thITlB2apvBTxUaSAciY0O6+KYlrsZOnETnbP/42Aj7msgn4r29unIFsbajMwsUsEWiA pie5WaHFIZ8HSr7kmN98J6NAMV4riqW/fpDrSDDh0AEGBKvYLDVqsHf9KuFhkHdiqwKN AlPaHfjIUyDHClfVP2W/WFS7GYp5Is5rtZf4YCOCWF7HStOuehjlRuEAXho4cAW9g1QO FEMw== 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:message-id:reply-to; bh=MlcDGmAU18RkE+OnLhtYCV6jx+QzKtfdj07+/quau8k=; b=2clCVHzAV/6gD80LwH1f4vrYh0pBPE4vyrW9ear52xWPqxwYswO7Vf1BPtDu4Le1PL csuXKZqMrQw6e+XloTM7QSY0zsn4BFHuJfKf3invo73N+oUCB2dHJ9bX6glN5lED9LMv qq4t4lB/Sd9BeMi18rBQqi6wwYzc3DumC/H1jftB/vqDvxSZFjKFmnrgBrQf1+6Yleh0 i6Xd6v8pD9y1q/RplVeafyJrngdLAq1nWguycNVYrchXDZ3/JWZlQcgLOq/L6e1M4vFp GarDiZ4XHUNynR8XoZ8YDQZ+TeDu1b/DT7tIYiQSrBlL9wzVjW9I9ofUcAMRYEHvcU8t TMdg== X-Gm-Message-State: AFqh2kqJgOAOpD+6Q87G147vcDVAsY9XnmhpQUfV4gBb2U3O2i2Q9AzQ 1GbpHqEQmZx3XoeNh1rWqHx243Qh1Vx8N2N4 X-Google-Smtp-Source: AMrXdXvUpqGCMff3qYR1EnArQrJxUEeLQqrV/AIlPgIO5nfRPtRVi5HlDZb8JrbqzO2rKsNO07UT9g== X-Received: by 2002:aa7:8502:0:b0:572:8b7d:f350 with SMTP id v2-20020aa78502000000b005728b7df350mr8897774pfn.26.1671434979687; Sun, 18 Dec 2022 23:29:39 -0800 (PST) Received: from fedlinux.. ([106.84.132.155]) by smtp.gmail.com with ESMTPSA id h12-20020a62830c000000b00577c5915138sm5765623pfe.128.2022.12.18.23.29.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Dec 2022 23:29:39 -0800 (PST) From: Sam Li To: qemu-devel@nongnu.org Cc: hare@suse.de, Hanna Reitz , Kevin Wolf , dmitry.fomichev@wdc.com, Paolo Bonzini , damien.lemoal@opensource.wdc.com, Fam Zheng , Thomas Huth , =?utf-8?q?Marc-?= =?utf-8?q?Andr=C3=A9_Lureau?= , =?utf-8?q?Phil?= =?utf-8?q?ippe_Mathieu-Daud=C3=A9?= , =?utf-8?q?Daniel_P?= =?utf-8?q?=2E_Berrang=C3=A9?= , Eric Blake , Markus Armbruster , Stefan Hajnoczi , qemu-block@nongnu.org, Sam Li Subject: [PATCH v14 2/8] file-posix: introduce helper functions for sysfs attributes Date: Mon, 19 Dec 2022 15:29:13 +0800 Message-Id: <20221219072919.6204-3-faithilikerun@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221219072919.6204-1-faithilikerun@gmail.com> References: <20221219072919.6204-1-faithilikerun@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=faithilikerun@gmail.com; helo=mail-pf1-x42c.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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 Reviewed-by: Dmitry Fomichev --- block/file-posix.c | 124 ++++++++++++++++++++++--------- include/block/block_int-common.h | 3 + 2 files changed, 91 insertions(+), 36 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index b9647c5ffc..676dd65c65 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1201,66 +1201,112 @@ static int hdev_get_max_hw_transfer(int fd, struct stat *st) #endif } -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 = NULL; + g_autofree char *sysfspath = NULL; int ret; - int sysfd = -1; - long max_segments; + size_t len; - if (S_ISCHR(st->st_mode)) { - if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) { - return ret; - } + if (!S_ISBLK(st->st_mode)) { return -ENOTSUP; } - if (!S_ISBLK(st->st_mode)) { - return -ENOTSUP; + sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/%s", + major(st->st_rdev), minor(st->st_rdev), + attribute); + ret = g_file_get_contents(sysfspath, val, &len, NULL); + if (ret == -1) { + return -ENOENT; } - sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments", - major(st->st_rdev), minor(st->st_rdev)); - sysfd = open(sysfspath, O_RDONLY); - if (sysfd == -1) { - ret = -errno; - goto out; + /* The file is ended with '\n' */ + char *p; + p = *val; + if (*(p + len - 1) == '\n') { + *(p + len - 1) = '\0'; } - do { - ret = read(sysfd, buf, sizeof(buf) - 1); - } while (ret == -1 && errno == EINTR); + return ret; +#else + return -ENOTSUP; +#endif +} + +static int get_sysfs_zoned_model(struct stat *st, BlockZoneModel *zoned) +{ + g_autofree char *val = NULL; + int ret; + + ret = get_sysfs_str_val(st, "zoned", &val); if (ret < 0) { - ret = -errno; - goto out; - } else if (ret == 0) { - ret = -EIO; - goto out; + return ret; } - buf[ret] = 0; - /* The file is ended with '\n', pass 'end' to accept that. */ - ret = qemu_strtol(buf, &end, 10, &max_segments); - if (ret == 0 && end && *end == '\n') { - ret = max_segments; + + if (strcmp(val, "host-managed") == 0) { + *zoned = BLK_Z_HM; + } else if (strcmp(val, "host-aware") == 0) { + *zoned = BLK_Z_HA; + } else if (strcmp(val, "none") == 0) { + *zoned = BLK_Z_NONE; + } else { + return -ENOTSUP; } + return 0; +} -out: - if (sysfd != -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 = NULL; + const char *end; + long val; + int ret; + + ret = get_sysfs_str_val(st, attribute, &str); + if (ret < 0) { + return ret; + } + + /* The file is ended with '\n', pass 'end' to accept that. */ + ret = qemu_strtol(str, &end, 10, &val); + if (ret == 0 && end && *end == '\0') { + ret = val; } - g_free(sysfspath); return ret; #else return -ENOTSUP; #endif } +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) == 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 = bs->opaque; struct stat st; + int ret; + BlockZoneModel zoned; s->needs_alignment = raw_needs_alignment(bs); raw_probe_alignment(bs, s->fd, errp); @@ -1298,6 +1344,12 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp) bs->bl.max_hw_iov = ret; } } + + ret = get_sysfs_zoned_model(&st, &zoned); + if (ret < 0) { + zoned = BLK_Z_NONE; + } + bs->bl.zoned = zoned; } static int check_for_dasd(int fd) diff --git a/include/block/block_int-common.h b/include/block/block_int-common.h index c34c525fa6..4941c24488 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -840,6 +840,9 @@ typedef struct BlockLimits { /* maximum number of iovec elements */ int max_iov; + + /* device zone model */ + BlockZoneModel zoned; } BlockLimits; typedef struct BdrvOpBlocker BdrvOpBlocker;