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: Pre-initialize smart shutdown_count
Date: Tue,  7 Aug 2018 16:24:41 -0600	[thread overview]
Message-ID: <20180807222441.14246-2-keith.busch@intel.com> (raw)
In-Reply-To: <20180807222441.14246-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. This patch uses that
saved value if it is available in case the command can't be executed due
to user permissions, allowing an application to detect if an unsafe
shutdown occured during a write.

Signed-off-by: Keith Busch <keith.busch@intel.com>
---
 ndctl/lib/intel.c       | 42 +++++++++++++++++++++++++++++++++++++-----
 ndctl/util/json-smart.c |  4 ----
 2 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/ndctl/lib/intel.c b/ndctl/lib/intel.c
index 0abea1e..6df5aff 100644
--- a/ndctl/lib/intel.c
+++ b/ndctl/lib/intel.c
@@ -56,6 +56,35 @@ static struct ndctl_cmd *alloc_intel_cmd(struct ndctl_dimm *dimm,
 	return cmd;
 }
 
+/*
+ * If provided, prime the shutdown count with the saved value in case the user
+ * does not have access rights to run the smart command.
+ */
+static void intel_prime_shutdown_count(struct ndctl_dimm *dimm,
+				       struct ndctl_cmd *cmd)
+{
+	char *path = NULL, shutdown_count[16] = {};
+	int fd;
+
+	if (asprintf(&path, DEF_TMPFS_DIR "/%s/usc",
+		     ndctl_dimm_get_unique_id(dimm)) < 0)
+		return;
+
+	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);
+ close:
+	close (fd);
+ free_path:
+	free(path);
+}
+
 static struct ndctl_cmd *intel_dimm_cmd_new_smart(struct ndctl_dimm *dimm)
 {
 	struct ndctl_cmd *cmd;
@@ -67,7 +96,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;
-
+	intel_prime_shutdown_count(dimm, cmd);
 	return cmd;
 }
 
@@ -95,9 +124,6 @@ static unsigned int intel_cmd_smart_get_flags(struct ndctl_cmd *cmd)
 	unsigned int flags = 0;
 	unsigned int intel_flags;
 
-	if (intel_smart_valid(cmd) < 0)
-		return 0;
-
 	/* translate intel specific flags to libndctl api smart flags */
 	intel_flags = cmd->intel->smart.flags;
 	if (intel_flags & ND_INTEL_SMART_HEALTH_VALID)
@@ -142,13 +168,19 @@ static unsigned int intel_cmd_smart_get_health(struct ndctl_cmd *cmd)
 	return health;
 }
 
+static unsigned int intel_cmd_smart_get_shutdown_count(struct ndctl_cmd *cmd)
+{
+	if (cmd->intel->smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID)
+		return cmd->intel->smart.shutdown_count;
+	return UINT_MAX;
+}
+
 intel_smart_get_field(cmd, media_temperature)
 intel_smart_get_field(cmd, ctrl_temperature)
 intel_smart_get_field(cmd, spares)
 intel_smart_get_field(cmd, alarm_flags)
 intel_smart_get_field(cmd, life_used)
 intel_smart_get_field(cmd, shutdown_state)
-intel_smart_get_field(cmd, shutdown_count)
 intel_smart_get_field(cmd, vendor_size)
 
 static unsigned char *intel_cmd_smart_get_vendor_data(struct ndctl_cmd *cmd)
diff --git a/ndctl/util/json-smart.c b/ndctl/util/json-smart.c
index 6a1f294..5f43982 100644
--- a/ndctl/util/json-smart.c
+++ b/ndctl/util/json-smart.c
@@ -93,7 +93,6 @@ struct json_object *util_dimm_health_to_json(struct ndctl_dimm *dimm)
 		jobj = json_object_new_string("unknown");
 		if (jobj)
 			json_object_object_add(jhealth, "health_state", jobj);
-		goto out;
 	}
 
 	flags = ndctl_cmd_smart_get_flags(cmd);
@@ -189,8 +188,5 @@ struct json_object *util_dimm_health_to_json(struct ndctl_dimm *dimm)
  err:
 	json_object_put(jhealth);
 	jhealth = NULL;
- out:
-	if (cmd)
-		ndctl_cmd_unref(cmd);
 	return jhealth;
 }
-- 
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:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-07 22:24 [PATCH 1/2] Create Intel pmem shutdown latch rule Keith Busch
2018-08-07 22:24 ` Keith Busch [this message]
2018-08-07 22:26 ` Keith Busch

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=20180807222441.14246-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 \
    --subject='Re: [PATCH 2/2] ndctl, intel: Pre-initialize smart shutdown_count' \
    /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

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).