git: 069fb41af5bc - releng/13.2 - nvmecontrol: Fix default ns create parameters.

From: Alexander Motin <mav_at_FreeBSD.org>
Date: Tue, 14 Mar 2023 13:58:55 UTC
The branch releng/13.2 has been updated by mav:

URL: https://cgit.FreeBSD.org/src/commit/?id=069fb41af5bca224caf6f96b50fed55fa7755c5b

commit 069fb41af5bca224caf6f96b50fed55fa7755c5b
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2023-03-09 15:19:00 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2023-03-14 13:58:02 +0000

    nvmecontrol: Fix default ns create parameters.
    
    Instead of passing 0xff's for all unset parameters, prefer reasonable
    defaults.  It is much easier to use it this was without specs in hand.
    
    PR:     269912
    Approved by:    re (cperciva)
    (cherry picked from commit 20dc2c4d11ddaa980c491116b65c51ab522028e9)
    (cherry picked from commit de0b02cb7b112947191af96dd34e7a95b760019c)
---
 sbin/nvmecontrol/ns.c | 56 ++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/sbin/nvmecontrol/ns.c b/sbin/nvmecontrol/ns.c
index 763c12aa11af..040918caf9c5 100644
--- a/sbin/nvmecontrol/ns.c
+++ b/sbin/nvmecontrol/ns.c
@@ -577,30 +577,48 @@ nscreate(const struct cmd *f, int argc, char *argv[])
 	    NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
 		errx(EX_UNAVAILABLE, "controller does not support namespace management");
 
-	/* Allow namespaces sharing if Multi-Path I/O is supported. */
-	if (create_opt.nmic == NONE) {
-		create_opt.nmic = cd.mic ? (NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK <<
-		     NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT) : 0;
-	}
-
 	memset(&nsdata, 0, sizeof(nsdata));
 	nsdata.nsze = create_opt.nsze;
 	nsdata.ncap = create_opt.cap;
-	if (create_opt.flbas == NONE)
-		nsdata.flbas = ((create_opt.lbaf & NVME_NS_DATA_FLBAS_FORMAT_MASK)
-		    << NVME_NS_DATA_FLBAS_FORMAT_SHIFT) |
-		    ((create_opt.mset & NVME_NS_DATA_FLBAS_EXTENDED_MASK)
-			<< NVME_NS_DATA_FLBAS_EXTENDED_SHIFT);
-	else
+	if (create_opt.flbas != NONE) {
 		nsdata.flbas = create_opt.flbas;
-	if (create_opt.dps == NONE)
-		nsdata.dps = ((create_opt.pi & NVME_NS_DATA_DPS_MD_START_MASK)
-		    << NVME_NS_DATA_DPS_MD_START_SHIFT) |
-		    ((create_opt.pil & NVME_NS_DATA_DPS_PIT_MASK)
-			<< NVME_NS_DATA_DPS_PIT_SHIFT);
-	else
+	} else {
+		/* Default to the first format, whatever it is. */
+		nsdata.flbas = 0;
+		if (create_opt.lbaf != NONE) {
+			nsdata.flbas |= (create_opt.lbaf &
+			    NVME_NS_DATA_FLBAS_FORMAT_MASK)
+			    << NVME_NS_DATA_FLBAS_FORMAT_SHIFT;
+		}
+		if (create_opt.mset != NONE) {
+			nsdata.flbas |= (create_opt.mset &
+			    NVME_NS_DATA_FLBAS_EXTENDED_MASK)
+			    << NVME_NS_DATA_FLBAS_EXTENDED_SHIFT;
+		}
+	}
+	if (create_opt.dps != NONE) {
 		nsdata.dps = create_opt.dps;
-	nsdata.nmic = create_opt.nmic;
+	} else {
+		/* Default to protection disabled. */
+		nsdata.dps = 0;
+		if (create_opt.pi != NONE) {
+			nsdata.dps |= (create_opt.pi &
+			    NVME_NS_DATA_DPS_MD_START_MASK)
+			    << NVME_NS_DATA_DPS_MD_START_SHIFT;
+		}
+		if (create_opt.pil != NONE) {
+			nsdata.dps |= (create_opt.pil &
+			    NVME_NS_DATA_DPS_PIT_MASK)
+			    << NVME_NS_DATA_DPS_PIT_SHIFT;
+		}
+	}
+	if (create_opt.nmic != NONE) {
+		nsdata.nmic = create_opt.nmic;
+	} else {
+		/* Allow namespaces sharing if Multi-Path I/O is supported. */
+		nsdata.nmic = cd.mic ? (NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK <<
+		     NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT) : 0;
+	}
 	nvme_namespace_data_swapbytes(&nsdata);
 
 	memset(&pt, 0, sizeof(pt));