From nobody Tue Nov 05 01:29:08 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Xj9jK1qnCz5cplM; Tue, 05 Nov 2024 01:29:09 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Xj9jK0lzKz4rmD; Tue, 5 Nov 2024 01:29:09 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730770149; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=S5sVZKmxdH8eT9UfH9hDeOTEKw1XkE8XmkKTXtNnerw=; b=nargsosMFbxkT/Tiiq1aVXPnSusSA+E6HIllyH0k6gRzIsb8bVCppNaczh8HPUOowi6CmS 851dGisecBzb+n+HXC87qZdmk3dDkD90nLMFQ1naIehlQEEf5FFuabR4964ykIy01U2IBQ PKJxY/TkuouoW0K7jWs4yvJancm5x3yE4Bz5lPjR5YJtIVlR9Zf75kwBQoK3r8jChE5Vq5 ZXIbUnnzDL3r4JdoF1nJ/YVxmWvRtrL/5kiCiocWjiiVXEggiHY74VBprnD5YIMcktyVDA xezMWmDI7hMVFtT56WxwQdl4juqkwpU0MLXrjKzdAtLfq57A0AKBnvRsbjrBNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730770149; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=S5sVZKmxdH8eT9UfH9hDeOTEKw1XkE8XmkKTXtNnerw=; b=mnBGnwY8cX5Xqq0FU54rG4eIbNTlME1DXehNUSejlZWV7PzeKj58erQH07Xl2h0V/9veom q1kilUiYwPfCRv3SnDY+z3qVdpGTC5waWHyTVioh+45lXxeSO0jS36/ENNUWgOrXVakzoh 1SLsO2A7QXaQeMXRE8SWhUtVf3v8BNCbC5x56WiXRs4108KI5miYZxYNCeCuemgOTIDpDz 9j6YLGP5gkTkW9rC3cFrxqN79s1JY2oo4qjTzvPd5mzc7gOJ89ybYKMrA4gSkAi3F2P2EB m2fQfpRqPhh6WgbqV8Fah1IeVEMPR/hR5l7dMvTYR41aorvr1CT1pbyR0d86iA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1730770149; a=rsa-sha256; cv=none; b=uHZB/Jds/t4ULNWtIUdbM0EG/PsUf0dpkAkH5kpwTLGwoRDEGooEhBY4NxBA0wyb3mVp9y F2DjnOZZtXxIQLbwIQ75bENAPC9cZKX+OjU1xa8ibFxB4LMm3ExSQoianx0OC7MtOhxZEv g7f8/F942RAPKgcPKWXrpiyPHbjkdmEBUYE+VfjuZ1OiXHGW3fVvCqR9dLNmCOiZOHvhdz FvN7RnxGRj2BtENqoZB8W77nrU71QfYWDiGvTqZTCXfJKlUxq5Bm6QtBTMQnVuIsMw0dIh B748aY3ilbBiPaDbz2W97GFNm1RHGM1DQ9GbsZUsC8FFGoTtwf7ENtNV+tTcUA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Xj9jJ62WNzxP6; Tue, 5 Nov 2024 01:29:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4A51T8Xd032005; Tue, 5 Nov 2024 01:29:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4A51T8Pt032002; Tue, 5 Nov 2024 01:29:08 GMT (envelope-from git) Date: Tue, 5 Nov 2024 01:29:08 GMT Message-Id: <202411050129.4A51T8Pt032002@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: 326e20fc1252 - main - nvmecontrol: Refactor devlist implementation List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 326e20fc1252577f96df0e53360507180cc9d153 Auto-Submitted: auto-generated The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=326e20fc1252577f96df0e53360507180cc9d153 commit 326e20fc1252577f96df0e53360507180cc9d153 Author: John Baldwin AuthorDate: 2024-11-05 01:28:26 +0000 Commit: John Baldwin CommitDate: 2024-11-05 01:28:26 +0000 nvmecontrol: Refactor devlist implementation Split out helper functions for scan_namespace and scan_controller. While here, replace sprintf() calls with snprintf() and avoid leaking the contoller fd if read_controller_data() fails. Reviewed by: chuck, imp Sponsored by: Chelsio Communications Differential Revision: https://reviews.freebsd.org/D47354 --- sbin/nvmecontrol/devlist.c | 99 +++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/sbin/nvmecontrol/devlist.c b/sbin/nvmecontrol/devlist.c index 2b34899d3aea..b2816339bc80 100644 --- a/sbin/nvmecontrol/devlist.c +++ b/sbin/nvmecontrol/devlist.c @@ -89,16 +89,68 @@ ns_get_sector_size(struct nvme_namespace_data *nsdata) } static void -devlist(const struct cmd *f, int argc, char *argv[]) +scan_namespace(int fd, int ctrlr, uint32_t nsid) +{ + struct nvme_namespace_data nsdata; + char name[64]; + uint8_t buf[7]; + uint64_t size; + + if (read_namespace_data(fd, nsid, &nsdata) != 0) + return; + if (nsdata.nsze == 0) + return; + snprintf(name, sizeof(name), "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr, + NVME_NS_PREFIX, nsid); + size = nsdata.nsze * (uint64_t)ns_get_sector_size(&nsdata); + if (opt.human) { + humanize_number(buf, sizeof(buf), size, "B", + HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); + printf(" %10s (%s)\n", name, buf); + } else { + printf(" %10s (%juMB)\n", name, (uintmax_t)size / 1024 / 1024); + } +} + +static bool +scan_controller(int ctrlr) { struct nvme_controller_data cdata; - struct nvme_namespace_data nsdata; char name[64]; uint8_t mn[64]; - uint8_t buf[7]; uint32_t i; - uint64_t size; - int ctrlr, fd, found, ret; + int fd, ret; + + snprintf(name, sizeof(name), "%s%d", NVME_CTRLR_PREFIX, ctrlr); + + ret = open_dev(name, &fd, 0, 0); + + if (ret == EACCES) { + warnx("could not open "_PATH_DEV"%s\n", name); + return (false); + } else if (ret != 0) + return (false); + + if (read_controller_data(fd, &cdata) != 0) { + close(fd); + return (true); + } + + nvme_strvis(mn, cdata.mn, sizeof(mn), NVME_MODEL_NUMBER_LENGTH); + printf("%6s: %s\n", name, mn); + + for (i = 0; i < cdata.nn; i++) { + scan_namespace(fd, ctrlr, i + 1); + } + + close(fd); + return (true); +} + +static void +devlist(const struct cmd *f, int argc, char *argv[]) +{ + int ctrlr, found; if (arg_parse(argc, argv, f)) return; @@ -108,41 +160,8 @@ devlist(const struct cmd *f, int argc, char *argv[]) while (ctrlr < NVME_MAX_UNIT) { ctrlr++; - sprintf(name, "%s%d", NVME_CTRLR_PREFIX, ctrlr); - - ret = open_dev(name, &fd, 0, 0); - - if (ret == EACCES) { - warnx("could not open "_PATH_DEV"%s\n", name); - continue; - } else if (ret != 0) - continue; - - found++; - if (read_controller_data(fd, &cdata)) - continue; - nvme_strvis(mn, cdata.mn, sizeof(mn), NVME_MODEL_NUMBER_LENGTH); - printf("%6s: %s\n", name, mn); - - for (i = 0; i < cdata.nn; i++) { - if (read_namespace_data(fd, i + 1, &nsdata)) - continue; - if (nsdata.nsze == 0) - continue; - sprintf(name, "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr, - NVME_NS_PREFIX, i + 1); - size = nsdata.nsze * (uint64_t)ns_get_sector_size(&nsdata); - if (opt.human) { - humanize_number(buf, sizeof(buf), size, "B", - HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); - printf(" %10s (%s)\n", name, buf); - - } else { - printf(" %10s (%juMB)\n", name, (uintmax_t)size / 1024 / 1024); - } - } - - close(fd); + if (scan_controller(ctrlr)) + found++; } if (found == 0) {