All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Williams <dan.j.williams@intel.com>
To: linux-nvdimm@lists.01.org
Subject: [PATCH 9/9] ndctl, create-namespace: enforce --size must be even multiple of interleave-width
Date: Mon, 09 Jan 2017 17:08:12 -0800	[thread overview]
Message-ID: <148401049211.16217.1553285320779246446.stgit@dwillia2-desk3.amr.corp.intel.com> (raw)
In-Reply-To: <148401044568.16217.6942951063735168785.stgit@dwillia2-desk3.amr.corp.intel.com>

If we don't check the size is properly aligned then the kernel will
cause the create-namespace request to abort. When the size is not
properly aligned suggest an alternative (rounded to the same units that
user specified) and bail out early without disturbing the current
configuration.

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 ndctl/builtin-xaction-namespace.c |   34 +++++++++++++++++++++++++++++++++-
 util/size.c                       |    3 +--
 util/size.h                       |    1 +
 3 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/ndctl/builtin-xaction-namespace.c b/ndctl/builtin-xaction-namespace.c
index eb289b0e392f..5a19729b6376 100644
--- a/ndctl/builtin-xaction-namespace.c
+++ b/ndctl/builtin-xaction-namespace.c
@@ -26,6 +26,7 @@
 #include <util/filter.h>
 #include <ndctl/libndctl.h>
 #include <util/parse-options.h>
+#include <ccan/minmax/minmax.h>
 #include <ccan/array_size/array_size.h>
 
 #ifdef HAVE_NDCTL_H
@@ -398,6 +399,8 @@ static int validate_namespace_options(struct ndctl_region *region,
 		struct ndctl_namespace *ndns, struct parsed_parameters *p)
 {
 	const char *region_name = ndctl_region_get_devname(region);
+	unsigned long long size_align, units = 1;
+	unsigned int ways;
 	int rc = 0;
 
 	memset(p, 0, sizeof(*p));
@@ -408,7 +411,7 @@ static int validate_namespace_options(struct ndctl_region *region,
 	}
 
 	if (param.size)
-		p->size = parse_size64(param.size);
+		p->size = __parse_size64(param.size, &units);
 	else if (ndns)
 		p->size = ndctl_namespace_get_size(ndns);
 
@@ -487,6 +490,35 @@ static int validate_namespace_options(struct ndctl_region *region,
 	} else
 		p->align = SZ_2M;
 
+	/* (re-)validate that the size satisfies the alignment */
+	if (param.align)
+		size_align = p->align;
+	else
+		size_align = SZ_4K;
+	ways = ndctl_region_get_interleave_ways(region);
+	size_align = max(units, size_align) * ways;
+	if (p->size % size_align) {
+		char *suffix = "";
+
+		if (units == SZ_1K)
+			suffix = "K";
+		else if (units == SZ_1M)
+			suffix = "M";
+		else if (units == SZ_1G)
+			suffix = "G";
+		else if (units == SZ_1T)
+			suffix = "T";
+
+		p->size /= size_align;
+		p->size++;
+		p->size *= size_align;
+		p->size /= units;
+		error("'--size=' must align to interleave-width: %d and alignment: %ld\n"
+				"  did you intend --size=%lld%s?\n", ways, param.align
+				? p->align : SZ_4K, p->size, suffix);
+		return -EINVAL;
+	}
+
 	if (param.sector_size) {
 		struct ndctl_btt *btt;
 		int num, i;
diff --git a/util/size.c b/util/size.c
index 6ad622781a27..da76bd94aebd 100644
--- a/util/size.c
+++ b/util/size.c
@@ -2,8 +2,7 @@
 #include <limits.h>
 #include <util/size.h>
 
-static unsigned long long __parse_size64(const char *str,
-		unsigned long long *units)
+unsigned long long __parse_size64(const char *str, unsigned long long *units)
 {
 	unsigned long long val, check;
 	char *end;
diff --git a/util/size.h b/util/size.h
index 634c92621bdc..776363fd19e9 100644
--- a/util/size.h
+++ b/util/size.h
@@ -12,5 +12,6 @@
 #define SZ_1T 0x10000000000ULL
 
 unsigned long long parse_size64(const char *str);
+unsigned long long __parse_size64(const char *str, unsigned long long *units);
 
 #endif /* _NDCTL_SIZE_H_ */

_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm

      parent reply	other threads:[~2017-01-10  1:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-10  1:07 [PATCH 0/9] ndctl, create-namespace: updates and fixes Dan Williams
2017-01-10  1:07 ` [PATCH 1/9] ndctl, create-namespace: skip idle regions when scanning for capacity Dan Williams
2017-01-10  1:07 ` [PATCH 2/9] ndctl, create-namespace: skip blk regions when scanning for memory or dax mode Dan Williams
2017-01-10  1:07 ` [PATCH 3/9] ndctl, create-namespace: improve failure message Dan Williams
2017-01-10  1:07 ` [PATCH 4/9] ndctl, create-namespace: add an 'align' option Dan Williams
2017-01-10  1:07 ` [PATCH 5/9] daxctl, list: add man page Dan Williams
2017-01-10  1:07 ` [PATCH 6/9] daxctl: add daxctl to rpm spec Dan Williams
2017-01-10  1:08 ` [PATCH 7/9] ndctl: return unit size from parse_size64() Dan Williams
2017-01-10  1:08 ` [PATCH 8/9] ndctl: fix ndctl_region_get_interleave_ways(), set minimum Dan Williams
2017-01-10  1:08 ` Dan Williams [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=148401049211.16217.1553285320779246446.stgit@dwillia2-desk3.amr.corp.intel.com \
    --to=dan.j.williams@intel.com \
    --cc=linux-nvdimm@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.