nvdimm.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Keith Busch <keith.busch@intel.com>
To: Vishal Verma <vishal.l.verma@intel.com>,
	Dave Jiang <dave.jiang@intel.com>,
	linux-nvdimm@lists.01.org
Subject: [PATCH 2/2] ndctl, intel: Fallback to smart cached shutdown_count
Date: Tue,  7 Aug 2018 16:26:56 -0600	[thread overview]
Message-ID: <20180807222656.14346-2-keith.busch@intel.com> (raw)
In-Reply-To: <20180807222656.14346-1-keith.busch@intel.com>

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>
---
 ndctl/lib/intel.c    | 34 +++++++++++++++++++++++++++++++++-
 ndctl/lib/libndctl.c |  2 ++
 ndctl/lib/private.h  |  1 +
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/ndctl/lib/intel.c b/ndctl/lib/intel.c
index 0abea1e..83f8203 100644
--- a/ndctl/lib/intel.c
+++ b/ndctl/lib/intel.c
@@ -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_unique_id(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,7 +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;
 }
 
diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
index 47e005e..d94321e 100644
--- a/ndctl/lib/libndctl.c
+++ b/ndctl/lib/libndctl.c
@@ -2651,6 +2651,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;
 }
 
diff --git a/ndctl/lib/private.h b/ndctl/lib/private.h
index a94f894..5812c85 100644
--- a/ndctl/lib/private.h
+++ b/ndctl/lib/private.h
@@ -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];
-- 
2.14.4

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

  reply	other threads:[~2018-08-07 22:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-07 22:26 [PATCH 1/2] Create Intel pmem shutdown latch rule Keith Busch
2018-08-07 22:26 ` Keith Busch [this message]
2018-08-07 23:35   ` [PATCH 2/2] ndctl, intel: Fallback to smart cached shutdown_count Verma, Vishal L
2018-08-07 23:35 ` [PATCH 1/2] Create Intel pmem shutdown latch rule Verma, Vishal L
2018-08-08  0:02 ` Verma, Vishal L

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=20180807222656.14346-2-keith.busch@intel.com \
    --to=keith.busch@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=linux-nvdimm@lists.01.org \
    --cc=vishal.l.verma@intel.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).