From patchwork Mon May 8 04:55:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sam Li X-Patchwork-Id: 1778236 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=20221208 header.b=LMQ9o5QY; 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 4QF8CS2R44z214X for ; Mon, 8 May 2023 14:56:50 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pvsuj-00018c-Ki; Mon, 08 May 2023 00:55:57 -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 1pvsui-00017t-2v; Mon, 08 May 2023 00:55:56 -0400 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pvsug-0005VU-B5; Mon, 08 May 2023 00:55:55 -0400 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1aae46e62e9so28395825ad.2; Sun, 07 May 2023 21:55:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683521752; x=1686113752; 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=vLJHiZfMCqd1hJDFnNOvsbwumv03fXU04zOtqhLQfSk=; b=LMQ9o5QY+aqSw5aqvZamY4eRfUVhj2KPP7ul0Vivi4B/wrDeHJqQmu8k5WzvjVIIul rFyP/bpQuHM60Mxb6AuRqkbz4cSJBsDb3ehCl4IH1MLFowNtL5AUdTuQ/dR6kL5dLJ5m x8HXGECAbfbrG4RlIO1VY8eY8KdC4djiaOAci5RMg/XNX4sIN4kH9d460lnvEDGDjY8Q xk6/jkmK53toO2ki5Zbj5kbxbmn8ToJ22mh509baAKTuVaTZnIq5x/v5Ud6Ou41VJHzr ewE68TEcYxD5eUedKy1S/oHSXUgfGqwOE8qTY7gMX7gI+htAoZaiRQV3MNmudbEOe0TK oL5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683521752; x=1686113752; 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=vLJHiZfMCqd1hJDFnNOvsbwumv03fXU04zOtqhLQfSk=; b=jHGYBY/y1lQZXQS5+HohUg9ifw45R05V0Z7ay3o73voAlvUieWT2gQ0BzEwuL7Gz5V aO+Kdh4llms6EXrlC6hyLGZzKwMb5vaQzO4Zd13P0jsZDiP2esXzVcnlA4J1CBXeWfzz vuj2h4SjNvhV1RNGOnyj8wjQbDMvUxIMnxUyucPkoEDMVKseBbV9z28BAL5S7+xoGG7g GuASy6jBM0PYZKJwRn78D01Nwl2eOkfXGbprWNPchQa1eYDWlDuMtmSJCvfkqMpRawvY L6iUevV34ivS4UMqJu4jPd9fhFAeGLURnpmUS0UGA3+Z+YpYB2KgzA5RmU8aekBkQ8Br I3Xg== X-Gm-Message-State: AC+VfDwisHUdwUkCHURAm5vjmPC3tTNpSelawDAKNG0KFw+/mm9w0XUH xM3gYA8K77jvY8GhZoXN4tZkKt7wQU2vl8fS X-Google-Smtp-Source: ACHHUZ47ZdXuVhkFR8XoR5+KJNsBeuOsGN+C8acS0P/DfMjXgONvN1/9T9S5iOVl/RsZpYPgec/t9A== X-Received: by 2002:a17:902:c40b:b0:1aa:fd48:f5e2 with SMTP id k11-20020a170902c40b00b001aafd48f5e2mr12298739plk.32.1683521751836; Sun, 07 May 2023 21:55:51 -0700 (PDT) Received: from fedlinux.. ([106.84.128.239]) by smtp.gmail.com with ESMTPSA id bg12-20020a1709028e8c00b0019ef86c2574sm6007112plb.270.2023.05.07.21.55.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 May 2023 21:55:51 -0700 (PDT) From: Sam Li To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi , qemu-block@nongnu.org, Hanna Reitz , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Paolo Bonzini , dmitry.fomichev@wdc.com, Fam Zheng , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Kevin Wolf , Thomas Huth , dlemoal@kernel.org, hare@suse.de, =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Sam Li , Damien Le Moal Subject: [PATCH v20 2/8] block/file-posix: introduce helper functions for sysfs attributes Date: Mon, 8 May 2023 12:55:27 +0800 Message-Id: <20230508045533.175575-3-faithilikerun@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230508045533.175575-1-faithilikerun@gmail.com> References: <20230508045533.175575-1-faithilikerun@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=faithilikerun@gmail.com; helo=mail-pl1-x635.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.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, FREEMAIL_REPLY=1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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 Acked-by: Kevin Wolf Message-id: 20230324090605.28361-3-faithilikerun@gmail.com [Adjust commit message prefix as suggested by Philippe Mathieu-Daudé . --Stefan] Signed-off-by: Stefan Hajnoczi --- block/file-posix.c | 127 ++++++++++++++++++++++--------- include/block/block_int-common.h | 3 + 2 files changed, 96 insertions(+), 34 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index c7b723368e..97c597a2a0 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1202,60 +1202,117 @@ 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. + */ #ifdef CONFIG_LINUX - char buf[32]; - const char *end; - char *sysfspath = NULL; +static int get_sysfs_str_val(struct stat *st, const char *attribute, + char **val) { + 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'; } - ret = RETRY_ON_EINTR(read(sysfd, buf, sizeof(buf) - 1)); + return ret; +} +#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. + */ +#ifdef CONFIG_LINUX +static long get_sysfs_long_val(struct stat *st, const char *attribute) +{ + 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; +} +#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_zoned_limits(BlockDriverState *bs, struct stat *st, + Error **errp) +{ + BlockZoneModel zoned; + int ret; + + bs->bl.zoned = BLK_Z_NONE; + + ret = get_sysfs_zoned_model(st, &zoned); + if (ret < 0 || zoned == BLK_Z_NONE) { + return; + } + bs->bl.zoned = zoned; +} + static void raw_refresh_limits(BlockDriverState *bs, Error **errp) { BDRVRawState *s = bs->opaque; @@ -1297,6 +1354,8 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp) bs->bl.max_hw_iov = ret; } } + + raw_refresh_zoned_limits(bs, &st, errp); } static int check_for_dasd(int fd) diff --git a/include/block/block_int-common.h b/include/block/block_int-common.h index 013d419444..150dc6f68f 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -861,6 +861,9 @@ typedef struct BlockLimits { * an explicit monitor command to load the disk inside the guest). */ bool has_variable_length; + + /* device zone model */ + BlockZoneModel zoned; } BlockLimits; typedef struct BdrvOpBlocker BdrvOpBlocker;