From nobody Wed Apr 16 12:46:23 2025 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1682685702; cv=none; d=zohomail.com; s=zohoarc; b=QwatuJsX8/ca1Hhw5RmcpjIPAz33a42fFG0smIIw30TY1wy4l8XDjsCTyJ+WnFXkjxhisDk0DvqvaoG0ensg+S0kJ0Vfj1TqBDThH7rQnQAyFWsoaJSkmCkse8K4lSOGY4DdrsbCGOC5QNiyhqYBUBfqJi7cgBKatzzynQyy41M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682685702; h=Content-Type: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=W0udrCJv7r8lAynaFcJv3pPNEBbKHvfiYakeUszKD0Y=; b=VbOhYnYAF9bL3SfccRkhE6ReSkHH2VrsrB249z6FpjNp+0QSADi50A46NdRUG9ucaK6cW1oGfpNSb7PHeuK2ydKV++W9Z5gsim0l2qXzrN/fPDUH5GqhzGNGVys/B+YcvY6GCBE1pF5y5FwNWDb5qAnd6WGxlT2XYBsf+jqRl5Y= 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 1682685702500901.4732133175604; Fri, 28 Apr 2023 05:41:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1psNPW-0007VL-Lr; Fri, 28 Apr 2023 08:41:15 -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 1psNOr-0006pH-GU for qemu-devel@nongnu.org; Fri, 28 Apr 2023 08:40:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1psNOk-0000Un-3W for qemu-devel@nongnu.org; Fri, 28 Apr 2023 08:40:29 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-533-FO8xTO4SMsuocpXusj82lQ-1; Fri, 28 Apr 2023 08:40:04 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3C054887412; Fri, 28 Apr 2023 12:40:02 +0000 (UTC) Received: from localhost (unknown [10.39.192.223]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7301035443; Fri, 28 Apr 2023 12:40:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682685609; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W0udrCJv7r8lAynaFcJv3pPNEBbKHvfiYakeUszKD0Y=; b=KlXkqMNlWhn9gGwr0cP8e/CgYqOr7lWZiiGonJ1oDzDfE1WC/FY7Jg0lo1OukvON+J7H06 sL5+yIVYmG+xv7SL2iMroU83knxxqGeDFkZL/uAuy8QeQSxgM5CHdSu8G5lxW804EbG9IO xoSAlvMTHT71m0uHpK86wGgTjQNL5Tc= X-MC-Unique: FO8xTO4SMsuocpXusj82lQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Thomas Huth , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Raphael Norwitz , Julia Suvorova , Cornelia Huck , Eric Blake , Paolo Bonzini , Aarushi Mehta , Stefan Hajnoczi , Richard Henderson , Kevin Wolf , Markus Armbruster , Stefano Garzarella , qemu-block@nongnu.org, Hanna Reitz , Fam Zheng , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , kvm@vger.kernel.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Sam Li , Hannes Reinecke , Damien Le Moal , Dmitry Fomichev Subject: [PULL 02/17] block/file-posix: introduce helper functions for sysfs attributes Date: Fri, 28 Apr 2023 08:39:39 -0400 Message-Id: <20230428123954.179035-3-stefanha@redhat.com> In-Reply-To: <20230428123954.179035-1-stefanha@redhat.com> References: <20230428123954.179035-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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 @redhat.com) X-ZM-MESSAGEID: 1682685703938100005 From: Sam Li 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 Signed-off-by: Stefan Hajnoczi Message-id: 20230427172019.3345-3-faithilikerun@gmail.com Message-id: 20230324090605.28361-3-faithilikerun@gmail.com [Adjust commit message prefix as suggested by Philippe Mathieu-Daud=C3=A9 . --Stefan] Signed-off-by: Stefan Hajnoczi --- include/block/block_int-common.h | 3 + block/file-posix.c | 139 ++++++++++++++++++++++--------- 2 files changed, 104 insertions(+), 38 deletions(-) diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.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; =20 typedef struct BdrvOpBlocker BdrvOpBlocker; diff --git a/block/file-posix.c b/block/file-posix.c index c7b723368e..ba15b10eee 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1202,15 +1202,93 @@ 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 + g_autofree char *sysfspath =3D NULL; + int ret; + size_t len; + + 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; + } + + /* The file is ended with '\n' */ + char *p; + p =3D *val; + if (*(p + len - 1) =3D=3D '\n') { + *(p + len - 1) =3D '\0'; + } + 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) { + return ret; + } + + 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; +} + +/* + * Get a sysfs attribute value as a long integer. + */ +static long get_sysfs_long_val(struct stat *st, const char *attribute) { #ifdef CONFIG_LINUX - char buf[32]; + g_autofree char *str =3D NULL; const char *end; - char *sysfspath =3D NULL; + 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; + } + return ret; +#else + return -ENOTSUP; +#endif +} + +static int hdev_get_max_segments(int fd, struct stat *st) +{ +#ifdef CONFIG_LINUX int ret; - int sysfd =3D -1; - long max_segments; =20 if (S_ISCHR(st->st_mode)) { if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) =3D=3D 0) { @@ -1218,44 +1296,27 @@ static int hdev_get_max_segments(int fd, struct sta= t *st) } return -ENOTSUP; } - - if (!S_ISBLK(st->st_mode)) { - return -ENOTSUP; - } - - 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; - } - ret =3D RETRY_ON_EINTR(read(sysfd, buf, sizeof(buf) - 1)); - if (ret < 0) { - ret =3D -errno; - goto out; - } else if (ret =3D=3D 0) { - ret =3D -EIO; - goto out; - } - 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; - } - -out: - if (sysfd !=3D -1) { - close(sysfd); - } - g_free(sysfspath); - return ret; + return get_sysfs_long_val(st, "max_segments"); #else return -ENOTSUP; #endif } =20 +static void raw_refresh_zoned_limits(BlockDriverState *bs, struct stat *st, + Error **errp) +{ + BlockZoneModel zoned; + int ret; + + bs->bl.zoned =3D BLK_Z_NONE; + + ret =3D get_sysfs_zoned_model(st, &zoned); + if (ret < 0 || zoned =3D=3D BLK_Z_NONE) { + return; + } + bs->bl.zoned =3D zoned; +} + static void raw_refresh_limits(BlockDriverState *bs, Error **errp) { BDRVRawState *s =3D bs->opaque; @@ -1297,6 +1358,8 @@ static void raw_refresh_limits(BlockDriverState *bs, = Error **errp) bs->bl.max_hw_iov =3D ret; } } + + raw_refresh_zoned_limits(bs, &st, errp); } =20 static int check_for_dasd(int fd) --=20 2.40.0