Skip to contentSkip to conversation

ndctl: Create ndctl udev rules for dirty shutdown

PATCH New Keith Busch 1 participant 2 messages Aug 8, 2018

Subsystems (2)
lore
Share & tools
lore

Keith Busch

author Applied
#0 lore raw

PATCH v2 1/2 ndctl: Create ndctl udev rules for dirty shutdown

This patch provides an ndctl udev rule and the program it runs. The rule sends two ndctl commands to allow applications to manage unsafe shutdowns.

The first command acknowledges the shutdown. For nvdimms that support this, the last shutdown status will remain set to its current status until it is acknowledged.

The second command retrieves the unclean shutdown count (USC) and saves it in a known location. Only root can directly 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/nmem<X>/usc

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

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

---
v1 -> v2:

Merged to ndctl/pending

Used more generic names

Split the udev program actions, and save USC regardless of the outcome of the first action

Removed rpm spec setting configure's tmpfilesdir

Use nmem<X> instead of dimm-id for runtime directory name

Update changelog

 .gitignore             |   1 +
 Makefile.am            |   3 +
 configure.ac           |  10 ++++
 contrib/80-ndctl.rules |   3 +
 ndctl.spec.in          |   3 +
 ndctl/Makefile.am      |   5 ++
 ndctl/ndctl-udev.c     | 147 +++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 172 insertions(+)
 create mode 100644 contrib/80-ndctl.rules
 create mode 100644 ndctl/ndctl-udev.c
.gitignore +1 -0
index 0baace4..f13a7ef 100644
@@ -25,6 +25,7 @@ daxctl/lib/libdaxctl.pc
 *.a
 ndctl/lib/libndctl.pc
 ndctl/ndctl
+ndctl/ndctl-udev
 rhel/
 sles/ndctl.spec
 util/log.lo
Makefile.am +3 -0
index e0c463a..7880eef 100644
@@ -42,6 +42,9 @@ bashcompletiondir = $(BASH_COMPLETION_DIR)
 dist_bashcompletion_DATA = contrib/ndctl
 endif
 
+udevrulesdir = $(UDEVDIR)/rules.d
+dist_udevrules_DATA = contrib/80-ndctl.rules
+
 noinst_LIBRARIES = libccan.a
 libccan_a_SOURCES = \
 	ccan/str/str.h \
configure.ac +10 -0
index e242334..4441a44 100644
@@ -164,8 +164,17 @@ AS_IF([test "x$with_systemd_unit_dir" != "xno"],
 	[AC_SUBST([systemd_unitdir], [$with_systemd_unit_dir])])
 AM_CONDITIONAL([ENABLE_SYSTEMD_UNIT_DIR], [test "x$with_systemd_unit_dir" != "xno"])
 
+AC_ARG_WITH([tmpfilesdir],
+	[AS_HELP_STRING([--with-tmpfilesdir=DIR], [Directory for temporary runtime files])],
+	[tmpfilesdir=$withval],
+	[tmpfilesdir="/run"])
+
+UDEVDIR="$(pkg-config udev --variable=udevdir)"
+AC_SUBST([UDEVDIR])
+
 my_CFLAGS="\
 -D DEF_CONF_FILE='\"${sysconfdir}/ndctl/monitor.conf\"' \
+-D DEF_TMPFS_DIR='\"${tmpfilesdir}/ndctl\"' \
 -Wall \
 -Wchar-subscripts \
 -Wformat-security \
@@ -207,6 +216,7 @@ AC_MSG_RESULT([
         libdir:                 ${libdir}
         includedir:             ${includedir}
 	systemd-unit-dir:	${systemd_unitdir}
+        tmpfilesdir:            ${tmpfilesdir}
 
         compiler:               ${CC}
         cflags:                 ${CFLAGS}
contrib/80-ndctl.rules new file +3 -0
new file mode 100644
index 0000000..54788c4
@@ -0,0 +1,3 @@
+# do not edit this file, it will be overwritten on update
+
+ACTION=="add", KERNEL=="nmem*", RUN+="ndctl-udev $kernel"
ndctl.spec.in +3 -0
index 17152c1..062aafb 100644
@@ -110,6 +110,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)
@@ -119,6 +120,8 @@ make check
 %{bashcompdir}/
 %{_sysconfdir}/ndctl/monitor.conf
 %{_unitdir}/ndctl-monitor.service
+%{_udevrulesdir}/80-ndctl.rules
+%{udevdir}/ndctl-udev
 
 %files -n daxctl
 %defattr(-,root,root)
ndctl/Makefile.am +5 -0
index 0f9bb43..4b44294 100644
@@ -51,3 +51,8 @@ EXTRA_DIST += $(monitor_config_file)
 if ENABLE_SYSTEMD_UNIT_DIR
 systemd_unit_DATA = ndctl-monitor.service
 endif
+
+ndctl_udevdir = $(UDEVDIR)
+ndctl_udev_PROGRAMS = ndctl-udev
+ndctl_udev_SOURCES = ndctl-udev.c
+ndctl_udev_LDADD = lib/libndctl.la
ndctl/ndctl-udev.c new file +147 -0

147 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 v2 2/2 ndctl, intel: Fallback to smart cached 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. 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.

---
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(+)
ndctl/lib/intel.c +33 -0
index 0abea1e..00c65a5 100644
@@ -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;
 }
ndctl/lib/libndctl.c +2 -0
index e911ea2..ab47b27 100644
@@ -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;
 }
 
ndctl/lib/private.h +1 -0
index 5ddc682..c6eb39e 100644
@@ -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];
Signature · 6 lines
2.14.4

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