Skip to contentSkip to conversation

Create Intel pmem shutdown latch rule

PATCH In review Keith Busch 1 participant 3 messages Aug 7, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Create Intel pmem shutdown latch rule

This patch provides a udev rule and the program it runs for Intel nvdimms. The program the rule executes runs two commands to allow applications to manage unsafe shutdowns.

The first command enables the shutdown latch. Without this, the last shutdown status will remain fixed to the status set when the latch was last enabled.

The second command retrieves the unclean shutdown counts and saves it in a known location. Only root can access the health's shutdown count, so we have to stash it somewhere accessible to non-privileged users. A successful execution of the rule will write USC to the run time tmpfs location. By default, the location will be set to:

/run/ndctl/<dimm-id>/usc

A distro may change this location using the '--with-tmpfsdir=[DIR]'.

Reading the file will report the count observed when the dimm was added.

---
 .gitignore                  |   1 +
 Makefile.am                 |   3 +
 configure.ac                |  10 ++++
 contrib/80-intel-pmem.rules |   1 +
 ndctl.spec.in               |   5 +-
 ndctl/Makefile.am           |   5 ++
 ndctl/latch-shutdown.c      | 140 ++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 164 insertions(+), 1 deletion(-)
 create mode 100644 contrib/80-intel-pmem.rules
 create mode 100644 ndctl/latch-shutdown.c
.gitignore +1 -0
index 1016b3b..1601738 100644
@@ -25,6 +25,7 @@ daxctl/lib/libdaxctl.pc
 *.a
 ndctl/lib/libndctl.pc
 ndctl/ndctl
+ndctl/latch-shutdown
 rhel/
 sles/ndctl.spec
 util/log.lo
Makefile.am +3 -0
index e0c463a..9d3913e 100644
@@ -42,6 +42,9 @@ bashcompletiondir = $(BASH_COMPLETION_DIR)
 dist_bashcompletion_DATA = contrib/ndctl
 endif
 
+udevrulesdir = $(UDEVDIR)/rules.d
+dist_udevrules_DATA = contrib/80-intel-pmem.rules
+
 noinst_LIBRARIES = libccan.a
 libccan_a_SOURCES = \
 	ccan/str/str.h \
configure.ac +10 -0
index cf44260..15f336b 100644
@@ -143,7 +143,16 @@ AC_CHECK_FUNCS([ \
 	secure_getenv\
 ])
 
+AC_ARG_WITH([tmpfsdir],
+  [AS_HELP_STRING([--with-tmpfsdir=DIR], [Directory for temporary runtime files])],
+  [tmpfsdir=$withval],
+  [tmpfsdir="/run"])
+
+UDEVDIR="$(pkg-config udev --variable=udevdir)"
+AC_SUBST([UDEVDIR])
+
 my_CFLAGS="\
+-D DEF_TMPFS_DIR='\"${tmpfsdir}/ndctl\"' \
 -Wall \
 -Wchar-subscripts \
 -Wformat-security \
@@ -182,6 +191,7 @@ AC_MSG_RESULT([
 
         prefix:                 ${prefix}
         sysconfdir:             ${sysconfdir}
+        tmpfsdir:               ${tmpfsdir}
         libdir:                 ${libdir}
         includedir:             ${includedir}
 
contrib/80-intel-pmem.rules new file +1 -0
new file mode 100644
index 0000000..20fcef9
@@ -0,0 +1 @@
+ACTION=="add", KERNEL=="nmem*", RUN+="latch-shutdown $kernel"
ndctl.spec.in +4 -1
index e2c879c..25edf7e 100644
@@ -90,7 +90,7 @@ control API for these devices.
 %build
 echo %{version} > version
 ./autogen.sh
-%configure --disable-static --disable-silent-rules
+%configure --disable-static --disable-silent-rules --with-tmpfsdir=%{_tmpfilesdir}
 make %{?_smp_mflags}
 
 %install
@@ -109,6 +109,7 @@ make check
 %postun -n DAX_LNAME -p /sbin/ldconfig
 
 %define bashcompdir %(pkg-config --variable=completionsdir bash-completion)
+%define udevdir  %(pkg-config --variable=udevdir udev)
 
 %files
 %defattr(-,root,root)
@@ -116,6 +117,8 @@ make check
 %{_bindir}/ndctl
 %{_mandir}/man1/ndctl*
 %{bashcompdir}/
+%{_udevrulesdir}/80-intel-pmem.rules
+%{udevdir}/latch-shutdown
 
 %files -n daxctl
 %defattr(-,root,root)
ndctl/Makefile.am +5 -0
index 0f56871..155179b 100644
@@ -41,3 +41,8 @@ ndctl_SOURCES += ../test/libndctl.c \
 		 ../test/core.c \
 		 test.c
 endif
+
+latch_shutdowndir = $(UDEVDIR)
+latch_shutdown_PROGRAMS = latch-shutdown
+latch_shutdown_SOURCES = latch-shutdown.c
+latch_shutdown_LDADD = lib/libndctl.la
ndctl/latch-shutdown.c new file +140 -0

140 lines not on this page · raw message

Signature · 6 lines
2.14.4

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

PATCH 2/2 ndctl, intel: Pre-initialize smart shutdown_count

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.

---
 ndctl/lib/intel.c       | 42 +++++++++++++++++++++++++++++++++++++-----
 ndctl/util/json-smart.c |  4 ----
 2 files changed, 37 insertions(+), 9 deletions(-)
ndctl/lib/intel.c +37 -5
index 0abea1e..6df5aff 100644
@@ -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)
ndctl/util/json-smart.c +0 -4
index 6a1f294..5f43982 100644
@@ -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;
 }
Signature · 6 lines
2.14.4

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