Blame SOURCES/0129-ndctl-libndctl-Update-nvdimm-flags-after-smart-injec.patch

26ccd9
From c5ccbf29c54b5a3d9cb1c138c06c8d5ac3ee80c2 Mon Sep 17 00:00:00 2001
26ccd9
From: Vaibhav Jain <vaibhav@linux.ibm.com>
26ccd9
Date: Tue, 22 Feb 2022 17:45:19 +0530
26ccd9
Subject: [PATCH 129/217] ndctl,libndctl: Update nvdimm flags after
26ccd9
 smart-inject
26ccd9
26ccd9
Presently after performing an inject-smart command the nvdimm flags reported are
26ccd9
out of date as shown below where no 'smart_notify' or 'flush_fail' flags were
26ccd9
reported even though they are set after injecting the smart error:
26ccd9
26ccd9
$ sudo ndctl inject-smart -fU nmem0
26ccd9
[
26ccd9
  {
26ccd9
    "dev":"nmem0",
26ccd9
    "health":{
26ccd9
      "health_state":"fatal",
26ccd9
      "shutdown_state":"dirty",
26ccd9
      "shutdown_count":0
26ccd9
    }
26ccd9
  }
26ccd9
]
26ccd9
$ sudo cat /sys/class/nd/ndctl0/device/nmem0/papr/flags
26ccd9
flush_fail smart_notify
26ccd9
26ccd9
This happens because nvdimm flags are only parsed once during its probe
26ccd9
and not refreshed even after a inject-smart operation makes them out of
26ccd9
date. To fix this the patch forces an update of nvdimm flags via newly
26ccd9
introduced export from libndctl named ndctl_dimm_refresh_flags() thats called
26ccd9
from dimm_inject_smart() after inject-smart command is successfully
26ccd9
submitted. This ensures that correct nvdimm flags are displayed later in that
26ccd9
function. With this implemented correct nvdimm flags are reported after a
26ccd9
inject-smart operation:
26ccd9
26ccd9
$ sudo ndctl inject-smart -fU nmem0
26ccd9
[
26ccd9
  {
26ccd9
    "dev":"nmem0",
26ccd9
    "flag_failed_flush":true,
26ccd9
    "flag_smart_event":true,
26ccd9
    "health":{
26ccd9
      "health_state":"fatal",
26ccd9
      "shutdown_state":"dirty",
26ccd9
      "shutdown_count":0
26ccd9
    }
26ccd9
  }
26ccd9
]
26ccd9
26ccd9
The patch refactors populate_dimm_attributes() to move the nvdimm flags
26ccd9
parsing code to the newly introduced ndctl_dimm_refresh_flags()
26ccd9
export. Since reading nvdimm flags requires constructing path using
26ccd9
'bus_prefix' which is only available during add_dimm(), the patch
26ccd9
introduces a new member 'struct ndctl_dimm.bus_prefix' to cache its
26ccd9
value. During ndctl_dimm_refresh_flags() the cached bus_prefix is used to
26ccd9
read the contents of the nvdimm flag file and pass it on to the appropriate
26ccd9
flag parsing function. Finally dimm_inject_smart() is updated to issue call to
26ccd9
ndctl_dimm_refresh_flags() before generating json output of the nvdimm status
26ccd9
26ccd9
Link: https://lore.kernel.org/r/20220222121519.1674117-1-vaibhav@linux.ibm.com
26ccd9
Signed-off-by: Vaibhav Jain <vaibhav@linux.ibm.com>
26ccd9
Signed-off-by: Shivaprasad G Bhat <sbhat@linux.ibm.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 ndctl/inject-smart.c   |  4 +++
26ccd9
 ndctl/lib/libndctl.c   | 55 +++++++++++++++++++++++++++++++-----------
26ccd9
 ndctl/lib/libndctl.sym |  4 +++
26ccd9
 ndctl/lib/private.h    |  1 +
26ccd9
 ndctl/libndctl.h       |  1 +
26ccd9
 5 files changed, 51 insertions(+), 14 deletions(-)
26ccd9
26ccd9
diff -up ndctl-71.1/ndctl/inject-smart.c.orig ndctl-71.1/ndctl/inject-smart.c
26ccd9
--- ndctl-71.1/ndctl/inject-smart.c.orig	2022-10-07 16:40:24.610615979 -0400
26ccd9
+++ ndctl-71.1/ndctl/inject-smart.c	2022-10-07 16:40:35.031651459 -0400
26ccd9
@@ -467,6 +467,10 @@ static int dimm_inject_smart(struct ndct
26ccd9
 		jdimms = json_object_new_array();
26ccd9
 		if (!jdimms)
26ccd9
 			goto out;
26ccd9
+
26ccd9
+		/* Ensure the dimm flags are upto date before reporting them */
26ccd9
+		ndctl_dimm_refresh_flags(dimm);
26ccd9
+
26ccd9
 		jdimm = util_dimm_to_json(dimm, sctx.flags);
26ccd9
 		if (!jdimm)
26ccd9
 			goto out;
26ccd9
diff -up ndctl-71.1/ndctl/lib/libndctl.c.orig ndctl-71.1/ndctl/lib/libndctl.c
26ccd9
--- ndctl-71.1/ndctl/lib/libndctl.c.orig	2022-10-07 16:40:24.608615972 -0400
26ccd9
+++ ndctl-71.1/ndctl/lib/libndctl.c	2022-10-07 16:40:35.032651462 -0400
26ccd9
@@ -588,6 +588,7 @@ static void free_dimm(struct ndctl_dimm
26ccd9
 	free(dimm->unique_id);
26ccd9
 	free(dimm->dimm_buf);
26ccd9
 	free(dimm->dimm_path);
26ccd9
+	free(dimm->bus_prefix);
26ccd9
 	if (dimm->module)
26ccd9
 		kmod_module_unref(dimm->module);
26ccd9
 	if (dimm->health_eventfd > -1)
26ccd9
@@ -1645,14 +1646,34 @@ static enum ndctl_fwa_result fwa_result_
26ccd9
 	return NDCTL_FWA_RESULT_INVALID;
26ccd9
 }
26ccd9
 
26ccd9
+NDCTL_EXPORT void ndctl_dimm_refresh_flags(struct ndctl_dimm *dimm)
26ccd9
+{
26ccd9
+	struct ndctl_ctx *ctx = dimm->bus->ctx;
26ccd9
+	char *path = dimm->dimm_buf;
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+
26ccd9
+	/* Construct path to dimm flags sysfs file */
26ccd9
+	sprintf(path, "%s/%s/flags", dimm->dimm_path, dimm->bus_prefix);
26ccd9
+
26ccd9
+	if (sysfs_read_attr(ctx, path, buf) < 0)
26ccd9
+		return;
26ccd9
+
26ccd9
+	/* Reset the flags */
26ccd9
+	dimm->flags.flags = 0;
26ccd9
+	if (ndctl_bus_has_nfit(dimm->bus))
26ccd9
+		parse_nfit_mem_flags(dimm, buf);
26ccd9
+	else if (ndctl_bus_is_papr_scm(dimm->bus))
26ccd9
+		parse_papr_flags(dimm, buf);
26ccd9
+}
26ccd9
+
26ccd9
 static int populate_dimm_attributes(struct ndctl_dimm *dimm,
26ccd9
-				    const char *dimm_base,
26ccd9
-				    const char *bus_prefix)
26ccd9
+				    const char *dimm_base)
26ccd9
 {
26ccd9
 	int i, rc = -1;
26ccd9
 	char buf[SYSFS_ATTR_SIZE];
26ccd9
 	struct ndctl_ctx *ctx = dimm->bus->ctx;
26ccd9
 	char *path = calloc(1, strlen(dimm_base) + 100);
26ccd9
+	const char *bus_prefix = dimm->bus_prefix;
26ccd9
 
26ccd9
 	if (!path)
26ccd9
 		return -ENOMEM;
26ccd9
@@ -1736,16 +1757,10 @@ static int populate_dimm_attributes(stru
26ccd9
 	}
26ccd9
 
26ccd9
 	sprintf(path, "%s/%s/flags", dimm_base, bus_prefix);
26ccd9
-	if (sysfs_read_attr(ctx, path, buf) == 0) {
26ccd9
-		if (ndctl_bus_has_nfit(dimm->bus))
26ccd9
-			parse_nfit_mem_flags(dimm, buf);
26ccd9
-		else if (ndctl_bus_is_papr_scm(dimm->bus)) {
26ccd9
-			dimm->cmd_family = NVDIMM_FAMILY_PAPR;
26ccd9
-			parse_papr_flags(dimm, buf);
26ccd9
-		}
26ccd9
-	}
26ccd9
-
26ccd9
 	dimm->health_eventfd = open(path, O_RDONLY|O_CLOEXEC);
26ccd9
+
26ccd9
+	ndctl_dimm_refresh_flags(dimm);
26ccd9
+
26ccd9
 	rc = 0;
26ccd9
  err_read:
26ccd9
 
26ccd9
@@ -1801,8 +1816,9 @@ static int add_papr_dimm(struct ndctl_di
26ccd9
 
26ccd9
 		rc = 0;
26ccd9
 	} else if (strcmp(buf, "nvdimm_test") == 0) {
26ccd9
+		dimm->cmd_family = NVDIMM_FAMILY_PAPR;
26ccd9
 		/* probe via common populate_dimm_attributes() */
26ccd9
-		rc = populate_dimm_attributes(dimm, dimm_base, "papr");
26ccd9
+		rc = populate_dimm_attributes(dimm, dimm_base);
26ccd9
 	}
26ccd9
 out:
26ccd9
 	free(path);
26ccd9
@@ -1899,9 +1915,20 @@ static void *add_dimm(void *parent, int
26ccd9
 	dimm->formats = formats;
26ccd9
 	/* Check if the given dimm supports nfit */
26ccd9
 	if (ndctl_bus_has_nfit(bus)) {
26ccd9
-		rc = populate_dimm_attributes(dimm, dimm_base, "nfit");
26ccd9
+		dimm->bus_prefix = strdup("nfit");
26ccd9
+		if (!dimm->bus_prefix) {
26ccd9
+			rc = -ENOMEM;
26ccd9
+			goto out;
26ccd9
+		}
26ccd9
+		rc =  populate_dimm_attributes(dimm, dimm_base);
26ccd9
+
26ccd9
 	} else if (ndctl_bus_has_of_node(bus)) {
26ccd9
-		rc = add_papr_dimm(dimm, dimm_base);
26ccd9
+		dimm->bus_prefix = strdup("papr");
26ccd9
+		if (!dimm->bus_prefix) {
26ccd9
+			rc = -ENOMEM;
26ccd9
+			goto out;
26ccd9
+		}
26ccd9
+		rc =  add_papr_dimm(dimm, dimm_base);
26ccd9
 	}
26ccd9
 
26ccd9
 	if (rc == -ENODEV) {
26ccd9
diff -up ndctl-71.1/ndctl/lib/libndctl.sym.orig ndctl-71.1/ndctl/lib/libndctl.sym
26ccd9
--- ndctl-71.1/ndctl/lib/libndctl.sym.orig	2022-10-07 16:40:24.344615073 -0400
26ccd9
+++ ndctl-71.1/ndctl/lib/libndctl.sym	2022-10-07 16:40:35.032651462 -0400
26ccd9
@@ -456,3 +456,7 @@ LIBNDCTL_26 {
26ccd9
 	ndctl_bus_nfit_translate_spa;
26ccd9
 	ndctl_dimm_sizeof_namespace_index;
26ccd9
 } LIBNDCTL_25;
26ccd9
+
26ccd9
+LIBNDCTL_27 {
26ccd9
+	ndctl_dimm_refresh_flags;
26ccd9
+} LIBNDCTL_26;
26ccd9
diff -up ndctl-71.1/ndctl/lib/private.h.orig ndctl-71.1/ndctl/lib/private.h
26ccd9
--- ndctl-71.1/ndctl/lib/private.h.orig	2022-10-07 16:40:24.455615451 -0400
26ccd9
+++ ndctl-71.1/ndctl/lib/private.h	2022-10-07 16:40:35.032651462 -0400
26ccd9
@@ -75,6 +75,7 @@ struct ndctl_dimm {
26ccd9
 	char *unique_id;
26ccd9
 	char *dimm_path;
26ccd9
 	char *dimm_buf;
26ccd9
+	char *bus_prefix;
26ccd9
 	int health_eventfd;
26ccd9
 	int buf_len;
26ccd9
 	int id;
26ccd9
diff -up ndctl-71.1/ndctl/libndctl.h.orig ndctl-71.1/ndctl/libndctl.h
26ccd9
--- ndctl-71.1/ndctl/libndctl.h.orig	2022-10-07 16:40:24.611615982 -0400
26ccd9
+++ ndctl-71.1/ndctl/libndctl.h	2022-10-07 16:40:35.033651466 -0400
26ccd9
@@ -221,6 +221,7 @@ int ndctl_dimm_is_active(struct ndctl_di
26ccd9
 int ndctl_dimm_is_enabled(struct ndctl_dimm *dimm);
26ccd9
 int ndctl_dimm_disable(struct ndctl_dimm *dimm);
26ccd9
 int ndctl_dimm_enable(struct ndctl_dimm *dimm);
26ccd9
+void ndctl_dimm_refresh_flags(struct ndctl_dimm *dimm);
26ccd9
 
26ccd9
 struct ndctl_cmd;
26ccd9
 struct ndctl_cmd *ndctl_bus_cmd_new_ars_cap(struct ndctl_bus *bus,