@@ -56,6 +56,38 @@ static struct ndctl_cmd *alloc_intel_cmd(struct ndctl_dimm *dimm,
return cmd;
}
+/*
+ * If provided, read the cached shutdown count in case the user does not have
+ * access rights to run the smart command, and pretend the command was
+ * successful with only the shutdown_count valid.
+ */
+static int intel_smart_handle_error(struct ndctl_cmd *cmd)
+{
+ struct ndctl_dimm *dimm = cmd->dimm;
+ char *path = NULL, shutdown_count[16] = {};
+ int fd, rc = cmd->status;
+
+ if (asprintf(&path, DEF_TMPFS_DIR "/%s/usc",
+ ndctl_dimm_get_devname(dimm)) < 0)
+ return rc;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto free_path;
+
+ if (read(fd, shutdown_count, sizeof(shutdown_count)) < 0)
+ goto close;
+
+ cmd->intel->smart.flags = ND_INTEL_SMART_SHUTDOWN_COUNT_VALID;
+ cmd->intel->smart.shutdown_count = strtoull(shutdown_count, NULL, 0);
+ rc = cmd->status = 0;
+ close:
+ close (fd);
+ free_path:
+ free(path);
+ return rc;
+}
+
static struct ndctl_cmd *intel_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
{
struct ndctl_cmd *cmd;
@@ -67,6 +99,7 @@ static struct ndctl_cmd *intel_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
if (!cmd)
return NULL;
cmd->firmware_status = &cmd->intel->smart.status;
+ cmd->handle_error = intel_smart_handle_error;
return cmd;
}
@@ -2766,6 +2766,8 @@ NDCTL_EXPORT int ndctl_cmd_submit(struct ndctl_cmd *cmd)
close(fd);
out:
cmd->status = rc;
+ if (rc && cmd->handle_error)
+ rc = cmd->handle_error(cmd);
return rc;
}
@@ -255,6 +255,7 @@ struct ndctl_cmd {
int dir;
} iter;
struct ndctl_cmd *source;
+ int (*handle_error)(struct ndctl_cmd *cmd);
union {
struct nd_cmd_ars_cap ars_cap[0];
struct nd_cmd_ars_start ars_start[0];
A user space rule saves the unsafe shutdown count to a fixed location for Intel dimms so that non-root users can read it. If the smart command submission fails, this patch uses that saved value in a newly added ndctl_cmd "handle_error" callback. If the cached value is available, the error handler will override the command status to success, and set the valid flags for the shutdown count. Signed-off-by: Keith Busch <keith.busch@intel.com> --- v1 -> v2: Use nmem<X> instead of dimm-id ndctl/lib/intel.c | 33 +++++++++++++++++++++++++++++++++ ndctl/lib/libndctl.c | 2 ++ ndctl/lib/private.h | 1 + 3 files changed, 36 insertions(+)