From owner-svn-src-head@freebsd.org Tue Mar 13 15:29:14 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id DB147F55DCA; Tue, 13 Mar 2018 15:29:13 +0000 (UTC) (envelope-from mav@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 90631830E5; Tue, 13 Mar 2018 15:29:13 +0000 (UTC) (envelope-from mav@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 869221B994; Tue, 13 Mar 2018 15:29:13 +0000 (UTC) (envelope-from mav@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w2DFTDLh075107; Tue, 13 Mar 2018 15:29:13 GMT (envelope-from mav@FreeBSD.org) Received: (from mav@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w2DFTD7b075106; Tue, 13 Mar 2018 15:29:13 GMT (envelope-from mav@FreeBSD.org) Message-Id: <201803131529.w2DFTD7b075106@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mav set sender to mav@FreeBSD.org using -f From: Alexander Motin Date: Tue, 13 Mar 2018 15:29:13 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r330846 - head/sbin/nvmecontrol X-SVN-Group: head X-SVN-Commit-Author: mav X-SVN-Commit-Paths: head/sbin/nvmecontrol X-SVN-Commit-Revision: 330846 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Mar 2018 15:29:14 -0000 Author: mav Date: Tue Mar 13 15:29:13 2018 New Revision: 330846 URL: https://svnweb.freebsd.org/changeset/base/330846 Log: Add some argument checks to be more user-friendly. MFC after: 2 weeks Sponsored by: iXsystems, Inc. Modified: head/sbin/nvmecontrol/format.c Modified: head/sbin/nvmecontrol/format.c ============================================================================== --- head/sbin/nvmecontrol/format.c Tue Mar 13 15:03:58 2018 (r330845) +++ head/sbin/nvmecontrol/format.c Tue Mar 13 15:29:13 2018 (r330846) @@ -81,9 +81,13 @@ format(int argc, char *argv[]) pil = strtol(optarg, NULL, 0); break; case 'E': + if (ses == 2) + errx(1, "-E and -C are mutually exclusive"); ses = 1; break; case 'C': + if (ses == 1) + errx(1, "-E and -C are mutually exclusive"); ses = 2; break; default: @@ -109,19 +113,7 @@ format(int argc, char *argv[]) */ if (strstr(target, NVME_NS_PREFIX) == NULL) { nsid = NVME_GLOBAL_NAMESPACE_TAG; - - /* We have no previous parameters, so use defaults. */ - if (lbaf < 0) - lbaf = 0; - if (mset < 0) - mset = 0; - if (pi < 0) - pi = 0; - if (pil < 0) - pil = 0; } else { - parse_ns_str(target, path, &nsid); - /* * We send FORMAT commands to the controller, not the namespace, * since it is an admin cmd. The namespace ID will be specified @@ -129,25 +121,34 @@ format(int argc, char *argv[]) * string to get the controller substring and namespace ID. */ close(fd); + parse_ns_str(target, path, &nsid); open_dev(path, &fd, 1, 1); + } - /* Check that controller can execute this command. */ - read_controller_data(fd, &cd); - if (((cd.fna >> NVME_CTRLR_DATA_FNA_FORMAT_ALL_SHIFT) - & NVME_CTRLR_DATA_FNA_FORMAT_ALL_MASK) && ses == 0) { - fprintf(stderr, "H/w doesn't support per-NS format\n"); - exit(1); - } else if (((cd.fna >> NVME_CTRLR_DATA_FNA_ERASE_ALL_SHIFT) - & NVME_CTRLR_DATA_FNA_ERASE_ALL_MASK) && ses != 0) { - fprintf(stderr, "H/w doesn't support per-NS erase\n"); - exit(1); - } + /* Check that controller can execute this command. */ + read_controller_data(fd, &cd); + if (((cd.oacs >> NVME_CTRLR_DATA_OACS_FORMAT_SHIFT) & + NVME_CTRLR_DATA_OACS_FORMAT_MASK) == 0) + errx(1, "controller does not support format"); + if (((cd.fna >> NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_SHIFT) & + NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_MASK) == 0 && ses == 2) + errx(1, "controller does not support cryptographic erase"); + if (nsid != NVME_GLOBAL_NAMESPACE_TAG) { + if (((cd.fna >> NVME_CTRLR_DATA_FNA_FORMAT_ALL_SHIFT) & + NVME_CTRLR_DATA_FNA_FORMAT_ALL_MASK) && ses == 0) + errx(1, "controller does not support per-NS format"); + if (((cd.fna >> NVME_CTRLR_DATA_FNA_ERASE_ALL_SHIFT) & + NVME_CTRLR_DATA_FNA_ERASE_ALL_MASK) && ses != 0) + errx(1, "controller does not support per-NS erase"); + /* Try to keep previous namespace parameters. */ read_namespace_data(fd, nsid, &nsd); if (lbaf < 0) lbaf = (nsd.flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) & NVME_NS_DATA_FLBAS_FORMAT_MASK; + if (lbaf > nsd.nlbaf) + errx(1, "LBA format is out of range"); if (mset < 0) mset = (nsd.flbas >> NVME_NS_DATA_FLBAS_EXTENDED_SHIFT) & NVME_NS_DATA_FLBAS_EXTENDED_MASK; @@ -157,6 +158,17 @@ format(int argc, char *argv[]) if (pil < 0) pil = (nsd.dps >> NVME_NS_DATA_DPS_PIT_SHIFT) & NVME_NS_DATA_DPS_PIT_MASK; + } else { + + /* We have no previous parameters, so default to zeroes. */ + if (lbaf < 0) + lbaf = 0; + if (mset < 0) + mset = 0; + if (pi < 0) + pi = 0; + if (pil < 0) + pil = 0; } memset(&pt, 0, sizeof(pt));