Skip to contentSkip to conversation

ndctl, lib: refactor badblocks retrieval routines

PATCH In review Vishal Verma 2 participants 6 messages Oct 3, 2018 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

PATCH v2 1/5 ndctl, lib: refactor badblocks retrieval routines

From: Dan Williams <[email protected]>

The kernel provides two locations in sysfs that contain badblocks information. The only one ndctl currently uses is region based badblocks. The problem with this is that this requires root privileges to calculate namespace-offset badblocks. Since the kernel already provides a world readable location for namespace badblocks, we can fall back to that.

Refactor the badblocks retrieval routines to use a bb_iter structure that contains information for what badblocks file in sysfs to use, to prepare for a direct namespace-badblocks fallback.

[vishal] remove an error print triggered by opening an invalid file

---
 ndctl/lib/libndctl.c | 141 +++++++++++++++++++++++--------------------
 ndctl/lib/private.h  |   6 ++
 2 files changed, 82 insertions(+), 65 deletions(-)

No changes since v1

ndctl/lib/libndctl.c +76 -65
index 481b110..d58be47 100644
@@ -178,8 +178,7 @@ struct ndctl_region {
 		int state;
 		unsigned long long cookie;
 	} iset;
-	FILE *badblocks;
-	struct badblock bb;
+	struct badblocks_iter bb_iter;
 	enum ndctl_persistence_domain persistence_domain;
 	/* file descriptor for deep flush sysfs entry */
 	int flush_fd;
@@ -376,6 +375,77 @@ NDCTL_EXPORT struct ndctl_ctx *ndctl_ref(struct ndctl_ctx *ctx)
 	return ctx;
 }
 
+static void badblocks_iter_free(struct badblocks_iter *bb_iter)
+{
+	if (bb_iter->file)
+		fclose(bb_iter->file);
+}
+
+static int badblocks_iter_init(struct badblocks_iter *bb_iter, const char *path)
+{
+	char *bb_path;
+	int rc = 0;
+
+	/* if the file is already open */
+	if (bb_iter->file) {
+		fclose(bb_iter->file);
+		bb_iter->file = NULL;
+	}
+
+	if (asprintf(&bb_path, "%s/badblocks", path) < 0)
+		return -errno;
+
+	bb_iter->file = fopen(bb_path, "re");
+	if (!bb_iter->file) {
+		rc = -errno;
+		free(bb_path);
+		return rc;
+	}
+
+	free(bb_path);
+	return rc;
+}
+
+static struct badblock *badblocks_iter_next(struct badblocks_iter *bb_iter)
+{
+	int rc;
+	char *buf = NULL;
+	size_t rlen = 0;
+
+	if (!bb_iter->file)
+		return NULL;
+
+	rc = getline(&buf, &rlen, bb_iter->file);
+	if (rc == -1) {
+		free(buf);
+		return NULL;
+	}
+
+	rc = sscanf(buf, "%llu %u", &bb_iter->bb.offset, &bb_iter->bb.len);
+	free(buf);
+	if (rc != 2) {
+		fclose(bb_iter->file);
+		bb_iter->file = NULL;
+		bb_iter->bb.offset = 0;
+		bb_iter->bb.len = 0;
+		return NULL;
+	}
+
+	return &bb_iter->bb;
+}
+
+static struct badblock *badblocks_iter_first(struct badblocks_iter *bb_iter,
+		struct ndctl_ctx *ctx, const char *path)
+{
+	int rc;
+
+	rc = badblocks_iter_init(bb_iter, path);
+	if (rc < 0)
+		return NULL;
+
+	return badblocks_iter_next(bb_iter);
+}
+
 static void free_namespace(struct ndctl_namespace *ndns, struct list_head *head)
 {
 	struct ndctl_bb *bb, *next;
@@ -511,8 +581,7 @@ static void free_region(struct ndctl_region *region)
 	kmod_module_unref(region->module);
 	free(region->region_buf);
 	free(region->region_path);
-	if (region->badblocks)
-		fclose(region->badblocks);
+	badblocks_iter_free(&region->bb_iter);
 	if (region->flush_fd > 0)
 		close(region->flush_fd);
 	free(region);
@@ -2253,73 +2322,15 @@ NDCTL_EXPORT int ndctl_region_get_numa_node(struct ndctl_region *region)
 	return region->numa_node;
 }
 
-static int regions_badblocks_init(struct ndctl_region *region)
-{
-	struct ndctl_ctx *ctx = ndctl_region_get_ctx(region);
-	char *bb_path;
-	int rc = 0;
-
-	/* if the file is already open */
-	if (region->badblocks) {
-		fclose(region->badblocks);
-		region->badblocks = NULL;
-	}
-
-	if (asprintf(&bb_path, "%s/badblocks",
-				region->region_path) < 0) {
-		rc = -errno;
-		err(ctx, "region badblocks path allocation failure\n");
-		return rc;
-	}
-
-	region->badblocks = fopen(bb_path, "re");
-	if (!region->badblocks) {
-		rc = -errno;
-		free(bb_path);
-		return rc;
-	}
-
-	free(bb_path);
-	return rc;
-}
-
 NDCTL_EXPORT struct badblock *ndctl_region_get_next_badblock(struct ndctl_region *region)
 {
-	int rc;
-	char *buf = NULL;
-	size_t rlen = 0;
-
-	if (!region->badblocks)
-		return NULL;
-
-	rc = getline(&buf, &rlen, region->badblocks);
-	if (rc == -1) {
-		free(buf);
-		return NULL;
-	}
-
-	rc = sscanf(buf, "%llu %u", &region->bb.offset, &region->bb.len);
-	free(buf);
-	if (rc != 2) {
-		fclose(region->badblocks);
-		region->badblocks = NULL;
-		region->bb.offset = 0;
-		region->bb.len = 0;
-		return NULL;
-	}
-
-	return &region->bb;
+	return badblocks_iter_next(&region->bb_iter);
 }
 
 NDCTL_EXPORT struct badblock *ndctl_region_get_first_badblock(struct ndctl_region *region)
 {
-	int rc;
-
-	rc = regions_badblocks_init(region);
-	if (rc < 0)
-		return NULL;
-
-	return ndctl_region_get_next_badblock(region);
+	return badblocks_iter_first(&region->bb_iter,
+			ndctl_region_get_ctx(region), region->region_path);
 }
 
 NDCTL_EXPORT enum ndctl_persistence_domain
ndctl/lib/private.h +6 -0
index b6e438a..d8ed614 100644
@@ -185,6 +185,11 @@ struct ndctl_lbasize {
 	int num;
 };
 
+struct badblocks_iter {
+	struct badblock bb;
+	FILE *file;
+};
+
 /**
  * struct ndctl_namespace - device claimed by the nd_blk or nd_pmem driver
  * @module: kernel module
@@ -209,6 +214,7 @@ struct ndctl_namespace {
 	int generation;
 	unsigned long long resource, size;
 	enum ndctl_namespace_mode enforce_mode;
+	struct badblocks_iter bb_iter;
 	char *alt_name;
 	uuid_t uuid;
 	struct ndctl_lbasize lbasize;
Signature · 6 lines
2.17.1

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

PATCH v2 2/5 ndctl, lib: add APIs for retrieving namespace badblocks

From: Dan Williams <[email protected]>

Use the new bb_iter mechanism to provide APIs that retrieve the namespace badblocks from the kernel's block device representation in sysfs. This paves the way for a namespace based fallback if region-badblocks are unavailable.

[vishal] add the new global symbols to libndctl.sym [vishal] fix the badblocks path to remove the trailing 'badblocks'

---
 ndctl/lib/libndctl.c   | 42 ++++++++++++++++++++++++++++++++++++++++++
 ndctl/lib/libndctl.sym |  6 ++++++
 ndctl/libndctl.h       |  6 ++++++
 3 files changed, 54 insertions(+)

No changes since v1

ndctl/lib/libndctl.c +42 -0
index d58be47..d341100 100644
@@ -3610,6 +3610,48 @@ NDCTL_EXPORT int ndctl_namespace_is_enabled(struct ndctl_namespace *ndns)
 	return is_enabled(ndctl_namespace_get_bus(ndns), path);
 }
 
+NDCTL_EXPORT struct badblock *ndctl_namespace_get_next_badblock(
+		struct ndctl_namespace *ndns)
+{
+	return badblocks_iter_next(&ndns->bb_iter);
+}
+
+NDCTL_EXPORT struct badblock *ndctl_namespace_get_first_badblock(
+		struct ndctl_namespace *ndns)
+{
+	struct ndctl_btt *btt = ndctl_namespace_get_btt(ndns);
+	struct ndctl_pfn *pfn = ndctl_namespace_get_pfn(ndns);
+	struct ndctl_dax *dax = ndctl_namespace_get_dax(ndns);
+	struct ndctl_ctx *ctx = ndctl_namespace_get_ctx(ndns);
+	const char *dev = ndctl_namespace_get_devname(ndns);
+	char path[SYSFS_ATTR_SIZE];
+	ssize_t len = sizeof(path);
+	const char *bdev;
+
+	if (btt || dax) {
+		dbg(ctx, "%s: badblocks not supported for %s\n", dev,
+				btt ? "btt" : "device-dax");
+		return NULL;
+	}
+
+	if (pfn)
+		bdev = ndctl_pfn_get_block_device(pfn);
+	else
+		bdev = ndctl_namespace_get_block_device(ndns);
+
+	if (!bdev) {
+		dbg(ctx, "%s: failed to determine block device\n", dev);
+		return NULL;
+	}
+
+	if (snprintf(path, len, "/sys/block/%s", bdev) >= len) {
+		err(ctx, "%s: buffer too small!\n", dev);
+		return NULL;
+	}
+
+	return badblocks_iter_first(&ndns->bb_iter, ctx, path);
+}
+
 static int ndctl_bind(struct ndctl_ctx *ctx, struct kmod_module *module,
 		const char *devname)
 {
ndctl/lib/libndctl.sym +6 -0
index a6849ee..185ee4a 100644
@@ -378,3 +378,9 @@ global:
 	ndctl_region_get_max_available_extent;
 	ndctl_cmd_smart_inject_ctrl_temperature;
 } LIBNDCTL_16;
+
+LIBNDCTL_18 {
+global:
+	ndctl_namespace_get_first_badblock;
+	ndctl_namespace_get_next_badblock;
+} LIBNDCTL_17;
ndctl/libndctl.h +6 -0
index 83d6c6c..500c7de 100644
@@ -439,6 +439,12 @@ struct ndctl_namespace *ndctl_namespace_get_next(struct ndctl_namespace *ndns);
 	     ndns != NULL; \
 	     ndns = _ndns, \
 	     _ndns = _ndns ? ndctl_namespace_get_next(_ndns) : NULL)
+struct badblock *ndctl_namespace_get_first_badblock(struct ndctl_namespace *ndns);
+struct badblock *ndctl_namespace_get_next_badblock(struct ndctl_namespace *ndns);
+#define ndctl_namespace_badblock_foreach(ndns, badblock) \
+        for (badblock = ndctl_namespace_get_first_badblock(ndns); \
+             badblock != NULL; \
+             badblock = ndctl_namespace_get_next_badblock(ndns))
 struct ndctl_ctx *ndctl_namespace_get_ctx(struct ndctl_namespace *ndns);
 struct ndctl_bus *ndctl_namespace_get_bus(struct ndctl_namespace *ndns);
 struct ndctl_region *ndctl_namespace_get_region(struct ndctl_namespace *ndns);
Signature · 6 lines
2.17.1

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

PATCH v2 3/5 util/json: fix an error check for region resource

In reply to Vishal Verma

The return type of ndctl_region_get_resource() is 'unsigned long long', and therefore the error checking for it should be done against ULLONG_MAX. Fix an instance where we were checking against ULONG_MAX.

---
 util/json.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/json.c +1 -1
index 2d15859..30b56c3 100644
@@ -470,7 +470,7 @@ struct json_object *util_region_badblocks_to_json(struct ndctl_region *region,
 
 		/* get start address of region */
 		addr = ndctl_region_get_resource(region);
-		if (addr == ULONG_MAX)
+		if (addr == ULLONG_MAX)
 			goto err_array;
 
 		/* get address of bad block */
Signature · 6 lines
2.17.1

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

PATCH v2 4/5 util/json: Add comments around re-checking the UTIL_JSON_MEDIA_ERRORS flag

In reply to Vishal Verma

Add comments to clarify that in the various ndctl_*_babdlock_foreach loops where we compose the json output, the additional check for UTIL_JSON_MEDIA_ERRORS is valid because even if we're not printing media errors, we still want the badblock_count to be updated, which requires a full badblocks traversal.

---
 util/json.c | 2 ++
 1 file changed, 2 insertions(+)
util/json.c +2 -0
index 30b56c3..702ccf4 100644
@@ -465,6 +465,7 @@ struct json_object *util_region_badblocks_to_json(struct ndctl_region *region,
 
 		bbs += bb->len;
 
+		/* recheck so we can still get the badblocks_count from above */
 		if (!(flags & UTIL_JSON_MEDIA_ERRORS))
 			continue;
 
@@ -554,6 +555,7 @@ static struct json_object *dev_badblocks_to_json(struct ndctl_region *region,
 
 		bbs += len;
 
+		/* recheck so we can still get the badblocks_count from above */
 		if (!(flags & UTIL_JSON_MEDIA_ERRORS))
 			continue;
 
Signature · 6 lines
2.17.1

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
Reviewed-by on v2 4/5
#4 lore raw

Re: [ndctl PATCH v2 4/5] util/json: Add comments around re-checking the UTIL_JSON_MEDIA_ERRORS flag

Quoted 8 lines · Vishal Verma

Add comments to clarify that in the various ndctl_*_babdlock_foreach loops where we compose the json output, the additional check for UTIL_JSON_MEDIA_ERRORS is valid because even if we're not printing media errors, we still want the badblock_count to be updated, which requires a full badblocks traversal.

...and everything else looks good too in this series.
_______________________________________________
Linux-nvdimm mailing list [email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author maintainer
#5 lore raw

PATCH v2 5/5 util/json: add a util_namespace_badblocks_to_json() helper

In reply to Vishal Verma

When ndctl list --media-errors is invoked, we rely upon the 'region' badblocks provided by the kernel, and reference them against the namespace start to report namespace badblocks. This can fail for non-root users, as the region and namespace resource information is root-only. However the kernel also provides block-device badblocks information in sysfs, and that is world readable. Provide a new helper to retrieve this information, and use it as a fallback when the region based badblocks are unavailable.

Note that when falling back to the namespace based badblocks, we are unable to retrieve the DIMM on which the badblocks are present, since we still need root privileges for both getting the resource information, and for the address translation command.

---
 util/json.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 60 insertions(+), 3 deletions(-)
util/json.c +60 -3
index 702ccf4..5c3424e 100644
@@ -509,6 +509,56 @@ struct json_object *util_region_badblocks_to_json(struct ndctl_region *region,
 	return NULL;
 }
 
+static struct json_object *util_namespace_badblocks_to_json(
+			struct ndctl_namespace *ndns,
+			unsigned int *bb_count, unsigned long flags)
+{
+	struct json_object *jbb = NULL, *jbbs = NULL, *jobj;
+	struct badblock *bb;
+	int bbs = 0;
+
+	if (flags & UTIL_JSON_MEDIA_ERRORS) {
+		jbbs = json_object_new_array();
+		if (!jbbs)
+			return NULL;
+	} else
+		return NULL;
+
+	ndctl_namespace_badblock_foreach(ndns, bb) {
+		bbs += bb->len;
+
+		/* recheck so we can still get the badblocks_count from above */
+		if (!(flags & UTIL_JSON_MEDIA_ERRORS))
+			continue;
+
+		jbb = json_object_new_object();
+		if (!jbb)
+			goto err_array;
+
+		jobj = json_object_new_int64(bb->offset);
+		if (!jobj)
+			goto err;
+		json_object_object_add(jbb, "offset", jobj);
+
+		jobj = json_object_new_int(bb->len);
+		if (!jobj)
+			goto err;
+		json_object_object_add(jbb, "length", jobj);
+		json_object_array_add(jbbs, jbb);
+	}
+
+	*bb_count = bbs;
+
+	if (bbs)
+		return jbbs;
+
+ err:
+	json_object_put(jbb);
+ err_array:
+	json_object_put(jbbs);
+	return NULL;
+}
+
 static struct json_object *dev_badblocks_to_json(struct ndctl_region *region,
 		unsigned long long dev_begin, unsigned long long dev_size,
 		unsigned int *bb_count, unsigned long flags)
@@ -599,8 +649,11 @@ static struct json_object *util_pfn_badblocks_to_json(struct ndctl_pfn *pfn,
 	unsigned long long pfn_begin, pfn_size;
 
 	pfn_begin = ndctl_pfn_get_resource(pfn);
-	if (pfn_begin == ULLONG_MAX)
-		return NULL;
+	if (pfn_begin == ULLONG_MAX) {
+		struct ndctl_namespace *ndns = ndctl_pfn_get_namespace(pfn);
+
+		return util_namespace_badblocks_to_json(ndns, bb_count, flags);
+	}
 
 	pfn_size = ndctl_pfn_get_size(pfn);
 	if (pfn_size == ULLONG_MAX)
@@ -879,10 +932,14 @@ struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns,
 		jbbs = util_dax_badblocks_to_json(dax, &bb_count, flags);
 	else if (btt)
 		util_btt_badblocks_to_json(btt, &bb_count);
-	else
+	else {
 		jbbs = util_region_badblocks_to_json(
 				ndctl_namespace_get_region(ndns), &bb_count,
 				flags);
+		if (!jbbs)
+			jbbs = util_namespace_badblocks_to_json(ndns, &bb_count,
+					flags);
+	}
 
 	if (bb_count) {
 		jobj = json_object_new_int(bb_count);
Signature · 6 lines
2.17.1

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