Skip to contentSkip to conversation

libndctl: rename parse_lbasize to parse_sizes

PATCH New Oliver O'Halloran 1 participant 4 messages Jun 27, 2017 libnvdimm: non-volatile memory device

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 libndctl: rename parse_lbasize to parse_sizes

We want to re-use this code to parse the list of supported alignments for dax and PFN devices so rename the function to reflect the more generic usage.

---
 ndctl/lib/libndctl.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
ndctl/lib/libndctl.c +10 -10
index 4acebc05d6db..aa262f03303e 100644
@@ -234,12 +234,12 @@ struct ndctl_region {
 };
 
 /**
- * struct ndctl_lbasize - lbasize info for btt and blk-namespace devices
+ * struct ndctl_sizes - list of supported sector or alignments
  * @select: currently selected sector_size
  * @supported: possible sector_size options
  * @num: number of entries in @supported
  */
-struct ndctl_lbasize {
+struct ndctl_sizes {
 	int select;
 	unsigned int *supported;
 	int num;
@@ -270,7 +270,7 @@ struct ndctl_namespace {
 	unsigned long long resource, size;
 	char *alt_name;
 	uuid_t uuid;
-	struct ndctl_lbasize lbasize;
+	struct ndctl_sizes lbasize;
 	int numa_node;
 };
 
@@ -291,7 +291,7 @@ struct ndctl_btt {
 	struct ndctl_region *region;
 	struct ndctl_namespace *ndns;
 	struct list_node list;
-	struct ndctl_lbasize lbasize;
+	struct ndctl_sizes lbasize;
 	unsigned long long size;
 	char *btt_path;
 	char *btt_buf;
@@ -2855,8 +2855,8 @@ static char *get_block_device(struct ndctl_ctx *ctx, const char *block_path)
 	return bdev_name;
 }
 
-static int parse_lbasize_supported(struct ndctl_ctx *ctx, const char *devname,
-		const char *buf, struct ndctl_lbasize *lba);
+static int parse_sizes(struct ndctl_ctx *ctx, const char *devname,
+		const char *buf, struct ndctl_sizes *lba);
 
 static void *add_namespace(void *parent, int id, const char *ndns_base)
 {
@@ -2908,7 +2908,7 @@ static void *add_namespace(void *parent, int id, const char *ndns_base)
 		sprintf(path, "%s/sector_size", ndns_base);
 		if (sysfs_read_attr(ctx, path, buf) < 0)
 			goto err_read;
-		if (parse_lbasize_supported(ctx, devname, buf, &ndns->lbasize) < 0)
+		if (parse_sizes(ctx, devname, buf, &ndns->lbasize) < 0)
 			goto err_read;
 		/* fall through */
 	case ND_DEVICE_NAMESPACE_PMEM:
@@ -3738,8 +3738,8 @@ NDCTL_EXPORT int ndctl_namespace_delete(struct ndctl_namespace *ndns)
 	return 0;
 }
 
-static int parse_lbasize_supported(struct ndctl_ctx *ctx, const char *devname,
-		const char *buf, struct ndctl_lbasize *lba)
+static int parse_sizes(struct ndctl_ctx *ctx, const char *devname,
+		const char *buf, struct ndctl_sizes *lba)
 {
 	char *s = strdup(buf), *end, *field;
 	void *temp;
@@ -3830,7 +3830,7 @@ static void *add_btt(void *parent, int id, const char *btt_base)
 	sprintf(path, "%s/sector_size", btt_base);
 	if (sysfs_read_attr(ctx, path, buf) < 0)
 		goto err_read;
-	if (parse_lbasize_supported(ctx, devname, buf, &btt->lbasize) < 0)
+	if (parse_sizes(ctx, devname, buf, &btt->lbasize) < 0)
 		goto err_read;
 
 	sprintf(path, "%s/size", btt_base);
Signature · 6 lines
2.9.4

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
#1 lore raw

PATCH 2/4 libndctl: Parse supported_alignments for dax and pfn devices

Newer kernels advertise the list of alignments that they support through sysfs. This patch adds parsing inside of libndctl to determine the alignments the kernel supports for PFN and DAX regions.

---
 ndctl/lib/libndctl.c   | 69 +++++++++++++++++++++++++++++++++++++++++++++++++-
 ndctl/lib/libndctl.sym |  2 ++
 ndctl/libndctl.h.in    |  8 ++++++
 3 files changed, 78 insertions(+), 1 deletion(-)
ndctl/lib/libndctl.c +68 -1
index aa262f03303e..d21d372891a4 100644
@@ -34,6 +34,7 @@
 #include <ndctl.h>
 #endif
 
+#include <util/size.h>
 #include <util/sysfs.h>
 #include <ndctl/libndctl.h>
 #include <daxctl/libdaxctl.h>
@@ -327,6 +328,7 @@ struct ndctl_pfn {
 	int buf_len;
 	uuid_t uuid;
 	int id, generation;
+	struct ndctl_sizes alignments;
 };
 
 struct ndctl_dax {
@@ -2858,6 +2860,18 @@ static char *get_block_device(struct ndctl_ctx *ctx, const char *block_path)
 static int parse_sizes(struct ndctl_ctx *ctx, const char *devname,
 		const char *buf, struct ndctl_sizes *lba);
 
+static int sizes_contains(struct ndctl_sizes *sizes, unsigned long size)
+{
+	int i;
+
+	for (i = 0; i < sizes->num; i++)
+		if (sizes->supported[i] == size)
+			return 1;
+
+	return 0;
+}
+
+
 static void *add_namespace(void *parent, int id, const char *ndns_base)
 {
 	const char *devname = devpath_to_devname(ndns_base);
@@ -4148,6 +4162,7 @@ static void *__add_pfn(struct ndctl_pfn *pfn, const char *pfn_base)
 	char *path = calloc(1, strlen(pfn_base) + 100);
 	struct ndctl_region *region = pfn->region;
 	char buf[SYSFS_ATTR_SIZE];
+	char *end = NULL;
 
 	if (!path)
 		return NULL;
@@ -4190,7 +4205,19 @@ static void *__add_pfn(struct ndctl_pfn *pfn, const char *pfn_base)
 	if (sysfs_read_attr(ctx, path, buf) < 0)
 		pfn->align = 0;
 	else
-		pfn->align = strtoul(buf, NULL, 0);
+		pfn->align = strtoul(buf, &end, 0);
+
+
+	sprintf(path, "%s/supported_alignments", pfn_base);
+	if (sysfs_read_attr(ctx, path, buf) >= 0)
+		parse_sizes(ctx, devpath_to_devname(pfn_base),
+				buf, &pfn->alignments);
+
+	if (!pfn->alignments.supported) {
+		pfn->alignments.supported =
+			(unsigned int []) {SZ_4K, SZ_2M, SZ_1G};
+		pfn->alignments.num = 3;
+	}
 
 	sprintf(path, "%s/resource", pfn_base);
 	if (sysfs_read_attr(ctx, path, buf) < 0)
@@ -4435,6 +4462,29 @@ NDCTL_EXPORT int ndctl_pfn_set_align(struct ndctl_pfn *pfn, unsigned long align)
 	return 0;
 }
 
+NDCTL_EXPORT unsigned int ndctl_pfn_get_supported_align(
+		struct ndctl_pfn *pfn, int i)
+{
+	if (pfn->alignments.num == 0)
+		return 0;
+
+	if (i < 0 || i > pfn->alignments.num)
+		return UINT_MAX;
+	else
+		return pfn->alignments.supported[i];
+}
+
+NDCTL_EXPORT int ndctl_pfn_get_num_aligns(struct ndctl_pfn *pfn)
+{
+	return pfn->alignments.num;
+}
+
+NDCTL_EXPORT int ndctl_pfn_supports_align(struct ndctl_pfn *pfn,
+		unsigned long align)
+{
+	return sizes_contains(&pfn->alignments, align);
+}
+
 NDCTL_EXPORT int ndctl_pfn_set_namespace(struct ndctl_pfn *pfn,
 		struct ndctl_namespace *ndns)
 {
@@ -4661,6 +4711,23 @@ NDCTL_EXPORT int ndctl_dax_has_align(struct ndctl_dax *dax)
 	return ndctl_pfn_has_align(&dax->pfn);
 }
 
+NDCTL_EXPORT unsigned int ndctl_dax_get_supported_align(
+		struct ndctl_dax *dax, int i)
+{
+	return ndctl_pfn_get_supported_align(&dax->pfn, i);
+}
+
+NDCTL_EXPORT int ndctl_dax_get_num_aligns(struct ndctl_dax *dax)
+{
+	return ndctl_pfn_get_num_aligns(&dax->pfn);
+}
+
+NDCTL_EXPORT int ndctl_dax_supports_align(struct ndctl_dax *dax,
+		unsigned long align)
+{
+	return ndctl_pfn_supports_align(&dax->pfn, align);
+}
+
 NDCTL_EXPORT int ndctl_dax_set_align(struct ndctl_dax *dax, unsigned long align)
 {
 	return ndctl_pfn_set_align(&dax->pfn, align);
ndctl/lib/libndctl.sym +2 -0
index 9bc36a37a813..547d1bf133c6 100644
@@ -238,6 +238,7 @@ global:
 	ndctl_pfn_get_location;
 	ndctl_pfn_set_location;
 	ndctl_pfn_get_align;
+	ndctl_pfn_supports_align;
 	ndctl_pfn_get_size;
 	ndctl_pfn_get_resource;
 	ndctl_pfn_has_align;
@@ -266,6 +267,7 @@ global:
 	ndctl_dax_get_location;
 	ndctl_dax_set_location;
 	ndctl_dax_get_align;
+	ndctl_dax_supports_align;
 	ndctl_dax_has_align;
 	ndctl_dax_set_align;
 	ndctl_dax_set_namespace;
ndctl/libndctl.h.in +8 -0
index 2c45d2daa81b..1d0924999b03 100644
@@ -587,7 +587,11 @@ int ndctl_pfn_set_uuid(struct ndctl_pfn *pfn, uuid_t uu);
 void ndctl_pfn_get_uuid(struct ndctl_pfn *pfn, uuid_t uu);
 int ndctl_pfn_has_align(struct ndctl_pfn *pfn);
 int ndctl_pfn_set_align(struct ndctl_pfn *pfn, unsigned long align);
+int ndctl_pfn_get_num_aligns(struct ndctl_pfn *pfn);
+int ndctl_pfn_supports_align(struct ndctl_pfn *pfn, unsigned long size);
+unsigned int ndctl_pfn_get_supported_align(struct ndctl_pfn *pfn, int i);
 unsigned long ndctl_pfn_get_align(struct ndctl_pfn *pfn);
+unsigned long ndctl_pfn_def_align(struct ndctl_pfn *pfn);
 unsigned long long ndctl_pfn_get_resource(struct ndctl_pfn *pfn);
 unsigned long long ndctl_pfn_get_size(struct ndctl_pfn *pfn);
 int ndctl_pfn_set_namespace(struct ndctl_pfn *pfn, struct ndctl_namespace *ndns);
@@ -619,8 +623,12 @@ int ndctl_dax_set_uuid(struct ndctl_dax *dax, uuid_t uu);
 enum ndctl_pfn_loc ndctl_dax_get_location(struct ndctl_dax *dax);
 int ndctl_dax_set_location(struct ndctl_dax *dax, enum ndctl_pfn_loc loc);
 unsigned long ndctl_dax_get_align(struct ndctl_dax *dax);
+unsigned long ndctl_dax_def_align(struct ndctl_dax *dax);
 int ndctl_dax_has_align(struct ndctl_dax *dax);
 int ndctl_dax_set_align(struct ndctl_dax *dax, unsigned long align);
+int ndctl_dax_get_num_aligns(struct ndctl_dax *dax);
+unsigned int ndctl_dax_get_supported_align(struct ndctl_dax *dax, int i);
+int ndctl_dax_supports_align(struct ndctl_dax *dax, unsigned long size);
 int ndctl_dax_set_namespace(struct ndctl_dax *dax,
 		struct ndctl_namespace *ndns);
 struct ndctl_bus *ndctl_dax_get_bus(struct ndctl_dax *dax);
Signature · 6 lines
2.9.4

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
#2 lore raw

PATCH 3/4 libndctl: Add helpers to check supported sector sizes

In reply to Oliver O'Halloran

libndctl now has the ndctl_{dax|pfn}_supports_align() helper functions for checking if PFN and DAX regions support a given alignment. The sector size of a BTT region is similar to the region alignment so add an equivilent helper to maintain a consistent API across region types.

Adds:
ndctl_namespace_supports_sector_size()
ndctl_btt_supports_sector_size()

---
 ndctl/lib/libndctl.c   | 12 ++++++++++++
 ndctl/lib/libndctl.sym |  2 ++
 ndctl/libndctl.h.in    |  3 +++
 ndctl/namespace.c      | 30 ++++++++++++------------------
 4 files changed, 29 insertions(+), 18 deletions(-)
ndctl/lib/libndctl.c +12 -0
index d21d372891a4..9668d8c5d34e 100644
@@ -3586,6 +3586,12 @@ NDCTL_EXPORT int ndctl_namespace_get_num_sector_sizes(struct ndctl_namespace *nd
 	return ndns->lbasize.num;
 }
 
+NDCTL_EXPORT int ndctl_namespace_supports_sector_size(
+		struct ndctl_namespace *ndns, unsigned long size)
+{
+	return sizes_contains(&ndns->lbasize, size);
+}
+
 NDCTL_EXPORT int ndctl_namespace_set_sector_size(struct ndctl_namespace *ndns,
 		unsigned int sector_size)
 {
@@ -3912,6 +3918,12 @@ NDCTL_EXPORT int ndctl_btt_get_num_sector_sizes(struct ndctl_btt *btt)
 	return btt->lbasize.num;
 }
 
+NDCTL_EXPORT int ndctl_btt_supports_sector_size(
+		struct ndctl_btt *btt, unsigned long size)
+{
+	return sizes_contains(&btt->lbasize, size);
+}
+
 NDCTL_EXPORT struct ndctl_namespace *ndctl_btt_get_namespace(struct ndctl_btt *btt)
 {
 	struct ndctl_ctx *ctx = ndctl_btt_get_ctx(btt);
ndctl/lib/libndctl.sym +2 -0
index 547d1bf133c6..4039f7311222 100644
@@ -192,6 +192,7 @@ global:
 	ndctl_namespace_get_sector_size;
 	ndctl_namespace_get_num_sector_sizes;
 	ndctl_namespace_set_sector_size;
+	ndctl_namespace_supports_sector_size;
 	ndctl_namespace_get_raw_mode;
 	ndctl_namespace_set_raw_mode;
 	ndctl_namespace_get_numa_node;
@@ -204,6 +205,7 @@ global:
 	ndctl_btt_get_supported_sector_size;
 	ndctl_btt_get_sector_size;
 	ndctl_btt_get_num_sector_sizes;
+	ndctl_btt_supports_sector_size;
 	ndctl_btt_get_namespace;
 	ndctl_btt_get_uuid;
 	ndctl_btt_get_size;
ndctl/libndctl.h.in +3 -0
index 1d0924999b03..feefb9653c88 100644
@@ -513,6 +513,8 @@ int ndctl_namespace_set_size(struct ndctl_namespace *ndns,
 		unsigned long long size);
 unsigned int ndctl_namespace_get_supported_sector_size(
 		struct ndctl_namespace *ndns, int i);
+int ndctl_namespace_supports_sector_size(
+		struct ndctl_namespace *ndns, unsigned long size);
 unsigned int ndctl_namespace_get_sector_size(struct ndctl_namespace *ndns);
 int ndctl_namespace_get_num_sector_sizes(struct ndctl_namespace *ndns);
 int ndctl_namespace_set_sector_size(struct ndctl_namespace *ndns,
@@ -538,6 +540,7 @@ struct ndctl_ctx *ndctl_btt_get_ctx(struct ndctl_btt *btt);
 struct ndctl_bus *ndctl_btt_get_bus(struct ndctl_btt *btt);
 struct ndctl_region *ndctl_btt_get_region(struct ndctl_btt *btt);
 unsigned int ndctl_btt_get_id(struct ndctl_btt *btt);
+int ndctl_btt_supports_sector_size(struct ndctl_btt *btt, unsigned long size);
 unsigned int ndctl_btt_get_supported_sector_size(struct ndctl_btt *btt, int i);
 unsigned int ndctl_btt_get_sector_size(struct ndctl_btt *btt);
 int ndctl_btt_get_num_sector_sizes(struct ndctl_btt *btt);
ndctl/namespace.c +12 -18
index 895e39bb8c7d..0f402e8790a6 100644
@@ -570,25 +570,21 @@ static int validate_namespace_options(struct ndctl_region *region,
 	}
 
 	if (param.sector_size) {
-		struct ndctl_btt *btt;
-		int num, i;
+		unsigned long long size = parse_size64(param.sector_size);
 
-		p->sector_size = parse_size64(param.sector_size);
-		btt = ndctl_region_get_btt_seed(region);
 		if (p->mode == NDCTL_NS_MODE_SAFE) {
+			struct ndctl_btt *btt =
+				ndctl_region_get_btt_seed(region);
+
 			if (!btt) {
 				debug("%s: does not support 'sector' mode\n",
 						region_name);
 				return -EINVAL;
 			}
-			num = ndctl_btt_get_num_sector_sizes(btt);
-			for (i = 0; i < num; i++)
-				if (ndctl_btt_get_supported_sector_size(btt, i)
-						== p->sector_size)
-					break;
-			if (i >= num) {
+
+			if (!ndctl_btt_supports_sector_size(btt, size)) {
 				debug("%s: does not support btt sector_size %lu\n",
-						region_name, p->sector_size);
+						region_name, size);
 				return -EINVAL;
 			}
 		} else {
@@ -596,17 +592,15 @@ static int validate_namespace_options(struct ndctl_region *region,
 
 			if (!seed)
 				seed = ndctl_region_get_namespace_seed(region);
-			num = ndctl_namespace_get_num_sector_sizes(seed);
-			for (i = 0; i < num; i++)
-				if (ndctl_namespace_get_supported_sector_size(seed, i)
-						== p->sector_size)
-					break;
-			if (i >= num) {
+
+			if (!ndctl_namespace_supports_sector_size(seed, size)) {
 				debug("%s: does not support namespace sector_size %lu\n",
-						region_name, p->sector_size);
+						region_name, size);
 				return -EINVAL;
 			}
 		}
+
+		p->sector_size = size;
 	} else if (ndns) {
 		struct ndctl_btt *btt = ndctl_namespace_get_btt(ndns);
 
Signature · 6 lines
2.9.4

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
#3 lore raw

PATCH 4/4 ndctl, create-namespace: use seed device to check alignments

In reply to Oliver O'Halloran

The possible alignments for DAX and PFN regions depends on what is supported by the current kernel. Previously we have assumed that 4K, 2M and 1G alignments are valid since those are the hardware page sizes available on x86, but these are not universal. This patch reworks the alignment validation so that the valid alignments are what the seed device supports. It also uses the seed device alignment as the default alignment since this is chosen by the kernel and should be a reasonable default.

---
 ndctl/namespace.c | 79 +++++++++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 37 deletions(-)
ndctl/namespace.c +42 -37
index 0f402e8790a6..1900032cd362 100644
@@ -428,7 +428,7 @@ 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 long long size_align = SZ_4K, units = 1;
 	unsigned int ways;
 	int rc = 0;
 
@@ -491,57 +491,62 @@ static int validate_namespace_options(struct ndctl_region *region,
 
 		p->align = parse_size64(param.align);
 
-		if (p->mode == NDCTL_NS_MODE_MEMORY && p->align != SZ_2M
-				&& (!pfn || !ndctl_pfn_has_align(pfn))) {
+		if (p->mode == NDCTL_NS_MODE_MEMORY) {
+			if (!pfn) {
+				debug("%s does not support memory mode\n",
+					region_name);
+				return -EAGAIN;
+			}
+
 			/*
 			 * Initial pfn device support in the kernel
 			 * supported a 2M default alignment when
 			 * ndctl_pfn_has_align() returns false.
 			 */
-			debug("%s not support 'align' for memory mode\n",
+			if (p->align != SZ_2M && !ndctl_pfn_has_align(pfn)) {
+				debug("%s not support 'align' for memory mode\n",
 					region_name);
-			return -EAGAIN;
-		} else if (p->mode == NDCTL_NS_MODE_DAX
-				&& (!dax || !ndctl_dax_has_align(dax))) {
+				return -EAGAIN;
+			}
+
+			if (param.align_default)
+				p->align = ndctl_pfn_get_align(pfn);
+
+			if (!ndctl_pfn_supports_align(pfn, p->align)) {
+				error("unsupported alignment for memory mode: %s\n",
+					param.align);
+				return -EINVAL;
+			}
+
+			size_align = p->align;
+		} else if (p->mode == NDCTL_NS_MODE_DAX) {
 			/*
 			 * Unlike the pfn case, we require the kernel to
 			 * have 'align' support for device-dax.
 			 */
-			debug("%s not support 'align' for dax mode\n",
-					region_name);
-			return -EAGAIN;
-		} else if (!param.align_default
-				&& (p->mode == NDCTL_NS_MODE_SAFE
-					|| p->mode == NDCTL_NS_MODE_RAW)) {
-			/*
-			 * Specifying an alignment has no effect for
-			 * raw, or btt mode namespaces.
-			 */
+			if (!dax || !ndctl_dax_has_align(dax)) {
+				debug("%s not support 'align' for dax mode\n",
+						region_name);
+				return -EAGAIN;
+			}
+
+			if (param.align_default)
+				p->align = ndctl_dax_get_align(dax);
+
+			if (!ndctl_dax_supports_align(dax, p->align)) {
+				error("unsupported alignment for dax mode: %s\n",
+					param.align);
+				return -EINVAL;
+			}
+
+			size_align = p->align;
+		} else if (!param.align_default) {
+			/* in other cases the user should not supply an alignment */
 			error("%s mode does not support setting an alignment\n",
 					p->mode == NDCTL_NS_MODE_SAFE
 					? "sector" : "raw");
 			return -ENXIO;
 		}
-
-		switch (p->align) {
-		case SZ_4K:
-		case SZ_2M:
-		case SZ_1G:
-			break;
-		default:
-			error("unsupported align: %s\n", param.align);
-			return -ENXIO;
-		}
-
-		/*
-		 * 'raw' and 'sector' mode namespaces don't support an
-		 * alignment attribute.
-		 */
-		if (p->mode == NDCTL_NS_MODE_MEMORY
-				|| p->mode == NDCTL_NS_MODE_DAX)
-			size_align = p->align;
-		else
-			size_align = SZ_4K;
 	}
 
 	/* (re-)validate that the size satisfies the alignment */
Signature · 6 lines
2.9.4

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm