Blame SOURCES/0042-cxl-add-commands-to-read-write-and-zero-labels.patch

26ccd9
From c415cebe4b5ca50e06db78a4719f312e33106936 Mon Sep 17 00:00:00 2001
26ccd9
From: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
Date: Thu, 7 Oct 2021 02:21:35 -0600
26ccd9
Subject: [PATCH 042/217] cxl: add commands to read, write, and zero labels
26ccd9
26ccd9
Add the following cxl-cli commands: read-labels, write-labels,
26ccd9
zero-labels. They operate on a CXL memdev, or a set of memdevs, and
26ccd9
allow interacting with the label storage area (LSA) on the device.
26ccd9
26ccd9
Add man pages for the above cxl-cli commands.
26ccd9
26ccd9
Cc: Dan Williams <dan.j.williams@intel.com>
26ccd9
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 Documentation/cxl/Makefile.am            |   5 +-
26ccd9
 Documentation/cxl/cxl-read-labels.txt    |  33 +++
26ccd9
 Documentation/cxl/cxl-write-labels.txt   |  32 +++
26ccd9
 Documentation/cxl/cxl-zero-labels.txt    |  29 ++
26ccd9
 Documentation/cxl/labels-description.txt |   8 +
26ccd9
 Documentation/cxl/labels-options.txt     |  17 ++
26ccd9
 Documentation/cxl/memdev-option.txt      |   4 +
26ccd9
 cxl/Makefile.am                          |   1 +
26ccd9
 cxl/builtin.h                            |   5 +
26ccd9
 cxl/cxl.c                                |   3 +
26ccd9
 cxl/memdev.c                             | 324 +++++++++++++++++++++++
26ccd9
 11 files changed, 460 insertions(+), 1 deletion(-)
26ccd9
 create mode 100644 Documentation/cxl/cxl-read-labels.txt
26ccd9
 create mode 100644 Documentation/cxl/cxl-write-labels.txt
26ccd9
 create mode 100644 Documentation/cxl/cxl-zero-labels.txt
26ccd9
 create mode 100644 Documentation/cxl/labels-description.txt
26ccd9
 create mode 100644 Documentation/cxl/labels-options.txt
26ccd9
 create mode 100644 Documentation/cxl/memdev-option.txt
26ccd9
 create mode 100644 cxl/memdev.c
26ccd9
26ccd9
diff --git a/Documentation/cxl/Makefile.am b/Documentation/cxl/Makefile.am
26ccd9
index db98dd7..efabaa3 100644
26ccd9
--- a/Documentation/cxl/Makefile.am
26ccd9
+++ b/Documentation/cxl/Makefile.am
26ccd9
@@ -19,7 +19,10 @@ endif
26ccd9
 
26ccd9
 man1_MANS = \
26ccd9
 	cxl.1 \
26ccd9
-	cxl-list.1
26ccd9
+	cxl-list.1 \
26ccd9
+	cxl-read-labels.1 \
26ccd9
+	cxl-write-labels.1 \
26ccd9
+	cxl-zero-labels.1
26ccd9
 
26ccd9
 EXTRA_DIST = $(man1_MANS)
26ccd9
 
26ccd9
diff --git a/Documentation/cxl/cxl-read-labels.txt b/Documentation/cxl/cxl-read-labels.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..143f296
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/cxl-read-labels.txt
26ccd9
@@ -0,0 +1,33 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+cxl-read-labels(1)
26ccd9
+==================
26ccd9
+
26ccd9
+NAME
26ccd9
+----
26ccd9
+cxl-read-labels - read out the label area on a CXL memdev
26ccd9
+
26ccd9
+SYNOPSIS
26ccd9
+--------
26ccd9
+[verse]
26ccd9
+'cxl read-labels' <mem0> [<mem1>..<memN>] [<options>]
26ccd9
+
26ccd9
+include::labels-description.txt[]
26ccd9
+This command dumps the raw binary data in a memdev's label area to stdout or a
26ccd9
+file.  In the multi-memdev case the data is concatenated.
26ccd9
+
26ccd9
+OPTIONS
26ccd9
+-------
26ccd9
+include::labels-options.txt[]
26ccd9
+
26ccd9
+-o::
26ccd9
+--output::
26ccd9
+	output file
26ccd9
+
26ccd9
+include::../copyright.txt[]
26ccd9
+
26ccd9
+SEE ALSO
26ccd9
+--------
26ccd9
+linkcxl:cxl-write-labels[1],
26ccd9
+linkcxl:cxl-zero-labels[1],
26ccd9
+CXL-2.0 9.13.2
26ccd9
diff --git a/Documentation/cxl/cxl-write-labels.txt b/Documentation/cxl/cxl-write-labels.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..75f42a5
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/cxl-write-labels.txt
26ccd9
@@ -0,0 +1,32 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+cxl-write-labels(1)
26ccd9
+===================
26ccd9
+
26ccd9
+NAME
26ccd9
+----
26ccd9
+cxl-write-labels - write data to the label area on a memdev
26ccd9
+
26ccd9
+SYNOPSIS
26ccd9
+--------
26ccd9
+[verse]
26ccd9
+'cxl write-labels <mem> [-i <filename>]'
26ccd9
+
26ccd9
+include::labels-description.txt[]
26ccd9
+Read data from the input filename, or stdin, and write it to the given
26ccd9
+<mem> device. Note that the device must not be active in any region, or
26ccd9
+actively registered with the nvdimm subsystem. If it is, the kernel will
26ccd9
+not allow write access to the device's label data area.
26ccd9
+
26ccd9
+OPTIONS
26ccd9
+-------
26ccd9
+include::labels-options.txt[]
26ccd9
+-i::
26ccd9
+--input::
26ccd9
+	input file
26ccd9
+
26ccd9
+SEE ALSO
26ccd9
+--------
26ccd9
+linkcxl:cxl-read-labels[1],
26ccd9
+linkcxl:cxl-zero-labels[1],
26ccd9
+CXL-2.0 9.13.2
26ccd9
diff --git a/Documentation/cxl/cxl-zero-labels.txt b/Documentation/cxl/cxl-zero-labels.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..bf95b24
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/cxl-zero-labels.txt
26ccd9
@@ -0,0 +1,29 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+cxl-zero-labels(1)
26ccd9
+==================
26ccd9
+
26ccd9
+NAME
26ccd9
+----
26ccd9
+cxl-zero-labels - zero out the label area on a set of memdevs
26ccd9
+
26ccd9
+SYNOPSIS
26ccd9
+--------
26ccd9
+[verse]
26ccd9
+'cxl zero-labels' <mem0> [<mem1>..<memN>] [<options>]
26ccd9
+
26ccd9
+include::labels-description.txt[]
26ccd9
+This command resets the device to its default state by
26ccd9
+deleting all labels.
26ccd9
+
26ccd9
+OPTIONS
26ccd9
+-------
26ccd9
+include::labels-options.txt[]
26ccd9
+
26ccd9
+include::../copyright.txt[]
26ccd9
+
26ccd9
+SEE ALSO
26ccd9
+--------
26ccd9
+linkcxl:cxl-read-labels[1],
26ccd9
+linkcxl:cxl-write-labels[1],
26ccd9
+CXL-2.0 9.13.2
26ccd9
diff --git a/Documentation/cxl/labels-description.txt b/Documentation/cxl/labels-description.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..f60bd5d
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/labels-description.txt
26ccd9
@@ -0,0 +1,8 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+DESCRIPTION
26ccd9
+-----------
26ccd9
+The region label area is a small persistent partition of capacity
26ccd9
+available on some CXL memory devices. The label area is used to
26ccd9
+and configure or determine the set of memory devices participating
26ccd9
+in different interleave sets.
26ccd9
diff --git a/Documentation/cxl/labels-options.txt b/Documentation/cxl/labels-options.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..06fbac3
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/labels-options.txt
26ccd9
@@ -0,0 +1,17 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+<memory device(s)>::
26ccd9
+include::memdev-option.txt[]
26ccd9
+
26ccd9
+-s::
26ccd9
+--size=::
26ccd9
+	Limit the operation to the given number of bytes. A size of 0
26ccd9
+	indicates to operate over the entire label capacity.
26ccd9
+
26ccd9
+-O::
26ccd9
+--offset=::
26ccd9
+	Begin the operation at the given offset into the label area.
26ccd9
+
26ccd9
+-v::
26ccd9
+	Turn on verbose debug messages in the library (if libcxl was built with
26ccd9
+	logging and debug enabled).
26ccd9
diff --git a/Documentation/cxl/memdev-option.txt b/Documentation/cxl/memdev-option.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..e778582
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/memdev-option.txt
26ccd9
@@ -0,0 +1,4 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+A 'memX' device name, or a memdev id number. Restrict the operation to
26ccd9
+the specified memdev(s). The keyword 'all' can be specified to indicate
26ccd9
+the lack of any restriction.
26ccd9
diff --git a/cxl/Makefile.am b/cxl/Makefile.am
26ccd9
index 98606b9..da9f91d 100644
26ccd9
--- a/cxl/Makefile.am
26ccd9
+++ b/cxl/Makefile.am
26ccd9
@@ -10,6 +10,7 @@ config.h: $(srcdir)/Makefile.am
26ccd9
 cxl_SOURCES =\
26ccd9
 		cxl.c \
26ccd9
 		list.c \
26ccd9
+		memdev.c \
26ccd9
 		../util/json.c \
26ccd9
 		builtin.h
26ccd9
 
26ccd9
diff --git a/cxl/builtin.h b/cxl/builtin.h
26ccd9
index 3797f98..78eca6e 100644
26ccd9
--- a/cxl/builtin.h
26ccd9
+++ b/cxl/builtin.h
26ccd9
@@ -5,4 +5,9 @@
26ccd9
 
26ccd9
 struct cxl_ctx;
26ccd9
 int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
+int cmd_write_labels(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
+int cmd_read_labels(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
+int cmd_zero_labels(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
+int cmd_init_labels(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
+int cmd_check_labels(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 #endif /* _CXL_BUILTIN_H_ */
26ccd9
diff --git a/cxl/cxl.c b/cxl/cxl.c
26ccd9
index a7725f8..4b1661d 100644
26ccd9
--- a/cxl/cxl.c
26ccd9
+++ b/cxl/cxl.c
26ccd9
@@ -61,6 +61,9 @@ static struct cmd_struct commands[] = {
26ccd9
 	{ "version", .c_fn = cmd_version },
26ccd9
 	{ "list", .c_fn = cmd_list },
26ccd9
 	{ "help", .c_fn = cmd_help },
26ccd9
+	{ "zero-labels", .c_fn = cmd_zero_labels },
26ccd9
+	{ "read-labels", .c_fn = cmd_read_labels },
26ccd9
+	{ "write-labels", .c_fn = cmd_write_labels },
26ccd9
 };
26ccd9
 
26ccd9
 int main(int argc, const char **argv)
26ccd9
diff --git a/cxl/memdev.c b/cxl/memdev.c
26ccd9
new file mode 100644
26ccd9
index 0000000..5ee38e5
26ccd9
--- /dev/null
26ccd9
+++ b/cxl/memdev.c
26ccd9
@@ -0,0 +1,324 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+/* Copyright (C) 2020-2021 Intel Corporation. All rights reserved. */
26ccd9
+#include <stdio.h>
26ccd9
+#include <errno.h>
26ccd9
+#include <stdlib.h>
26ccd9
+#include <unistd.h>
26ccd9
+#include <limits.h>
26ccd9
+#include <util/log.h>
26ccd9
+#include <util/filter.h>
26ccd9
+#include <cxl/libcxl.h>
26ccd9
+#include <util/parse-options.h>
26ccd9
+#include <ccan/minmax/minmax.h>
26ccd9
+#include <ccan/array_size/array_size.h>
26ccd9
+
26ccd9
+struct action_context {
26ccd9
+	FILE *f_out;
26ccd9
+	FILE *f_in;
26ccd9
+};
26ccd9
+
26ccd9
+static struct parameters {
26ccd9
+	const char *outfile;
26ccd9
+	const char *infile;
26ccd9
+	unsigned len;
26ccd9
+	unsigned offset;
26ccd9
+	bool verbose;
26ccd9
+} param;
26ccd9
+
26ccd9
+#define fail(fmt, ...) \
26ccd9
+do { \
26ccd9
+	fprintf(stderr, "cxl-%s:%s:%d: " fmt, \
26ccd9
+			VERSION, __func__, __LINE__, ##__VA_ARGS__); \
26ccd9
+} while (0)
26ccd9
+
26ccd9
+#define BASE_OPTIONS() \
26ccd9
+OPT_BOOLEAN('v',"verbose", &param.verbose, "turn on debug")
26ccd9
+
26ccd9
+#define READ_OPTIONS() \
26ccd9
+OPT_STRING('o', "output", &param.outfile, "output-file", \
26ccd9
+	"filename to write label area contents")
26ccd9
+
26ccd9
+#define WRITE_OPTIONS() \
26ccd9
+OPT_STRING('i', "input", &param.infile, "input-file", \
26ccd9
+	"filename to read label area data")
26ccd9
+
26ccd9
+#define LABEL_OPTIONS() \
26ccd9
+OPT_UINTEGER('s', "size", &param.len, "number of label bytes to operate"), \
26ccd9
+OPT_UINTEGER('O', "offset", &param.offset, \
26ccd9
+	"offset into the label area to start operation")
26ccd9
+
26ccd9
+static const struct option read_options[] = {
26ccd9
+	BASE_OPTIONS(),
26ccd9
+	LABEL_OPTIONS(),
26ccd9
+	READ_OPTIONS(),
26ccd9
+	OPT_END(),
26ccd9
+};
26ccd9
+
26ccd9
+static const struct option write_options[] = {
26ccd9
+	BASE_OPTIONS(),
26ccd9
+	LABEL_OPTIONS(),
26ccd9
+	WRITE_OPTIONS(),
26ccd9
+	OPT_END(),
26ccd9
+};
26ccd9
+
26ccd9
+static const struct option zero_options[] = {
26ccd9
+	BASE_OPTIONS(),
26ccd9
+	LABEL_OPTIONS(),
26ccd9
+	OPT_END(),
26ccd9
+};
26ccd9
+
26ccd9
+static int action_zero(struct cxl_memdev *memdev, struct action_context *actx)
26ccd9
+{
26ccd9
+	size_t size;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	if (param.len)
26ccd9
+		size = param.len;
26ccd9
+	else
26ccd9
+		size = cxl_memdev_get_label_size(memdev);
26ccd9
+
26ccd9
+	if (cxl_memdev_nvdimm_bridge_active(memdev)) {
26ccd9
+		fprintf(stderr,
26ccd9
+			"%s: has active nvdimm bridge, abort label write\n",
26ccd9
+			cxl_memdev_get_devname(memdev));
26ccd9
+		return -EBUSY;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = cxl_memdev_zero_label(memdev, size, param.offset);
26ccd9
+	if (rc < 0)
26ccd9
+		fprintf(stderr, "%s: label zeroing failed: %s\n",
26ccd9
+			cxl_memdev_get_devname(memdev), strerror(-rc));
26ccd9
+
26ccd9
+	return rc;
26ccd9
+}
26ccd9
+
26ccd9
+static int action_write(struct cxl_memdev *memdev, struct action_context *actx)
26ccd9
+{
26ccd9
+	size_t size = param.len, read_len;
26ccd9
+	unsigned char *buf;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	if (cxl_memdev_nvdimm_bridge_active(memdev)) {
26ccd9
+		fprintf(stderr,
26ccd9
+			"%s: has active nvdimm bridge, abort label write\n",
26ccd9
+			cxl_memdev_get_devname(memdev));
26ccd9
+		return -EBUSY;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (!size) {
26ccd9
+		size_t label_size = cxl_memdev_get_label_size(memdev);
26ccd9
+
26ccd9
+		fseek(actx->f_in, 0L, SEEK_END);
26ccd9
+		size = ftell(actx->f_in);
26ccd9
+		fseek(actx->f_in, 0L, SEEK_SET);
26ccd9
+
26ccd9
+		if (size > label_size) {
26ccd9
+			fprintf(stderr,
26ccd9
+				"File size (%zu) greater than label area size (%zu), aborting\n",
26ccd9
+				size, label_size);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	buf = calloc(1, size);
26ccd9
+	if (!buf)
26ccd9
+		return -ENOMEM;
26ccd9
+
26ccd9
+	read_len = fread(buf, 1, size, actx->f_in);
26ccd9
+	if (read_len != size) {
26ccd9
+		rc = -ENXIO;
26ccd9
+		goto out;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = cxl_memdev_write_label(memdev, buf, size, param.offset);
26ccd9
+	if (rc < 0)
26ccd9
+		fprintf(stderr, "%s: label write failed: %s\n",
26ccd9
+			cxl_memdev_get_devname(memdev), strerror(-rc));
26ccd9
+
26ccd9
+out:
26ccd9
+	free(buf);
26ccd9
+	return rc;
26ccd9
+}
26ccd9
+
26ccd9
+static int action_read(struct cxl_memdev *memdev, struct action_context *actx)
26ccd9
+{
26ccd9
+	size_t size, write_len;
26ccd9
+	char *buf;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	if (param.len)
26ccd9
+		size = param.len;
26ccd9
+	else
26ccd9
+		size = cxl_memdev_get_label_size(memdev);
26ccd9
+
26ccd9
+	buf = calloc(1, size);
26ccd9
+	if (!buf)
26ccd9
+		return -ENOMEM;
26ccd9
+
26ccd9
+	rc = cxl_memdev_read_label(memdev, buf, size, param.offset);
26ccd9
+	if (rc < 0) {
26ccd9
+		fprintf(stderr, "%s: label read failed: %s\n",
26ccd9
+			cxl_memdev_get_devname(memdev), strerror(-rc));
26ccd9
+		goto out;
26ccd9
+	}
26ccd9
+
26ccd9
+	write_len = fwrite(buf, 1, size, actx->f_out);
26ccd9
+	if (write_len != size) {
26ccd9
+		rc = -ENXIO;
26ccd9
+		goto out;
26ccd9
+	}
26ccd9
+	fflush(actx->f_out);
26ccd9
+
26ccd9
+out:
26ccd9
+	free(buf);
26ccd9
+	return rc;
26ccd9
+}
26ccd9
+
26ccd9
+static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
26ccd9
+		int (*action)(struct cxl_memdev *memdev, struct action_context *actx),
26ccd9
+		const struct option *options, const char *usage)
26ccd9
+{
26ccd9
+	struct cxl_memdev *memdev, *single = NULL;
26ccd9
+	struct action_context actx = { 0 };
26ccd9
+	int i, rc = 0, count = 0, err = 0;
26ccd9
+	const char * const u[] = {
26ccd9
+		usage,
26ccd9
+		NULL
26ccd9
+	};
26ccd9
+	unsigned long id;
26ccd9
+
26ccd9
+	argc = parse_options(argc, argv, options, u, 0);
26ccd9
+
26ccd9
+	if (argc == 0)
26ccd9
+		usage_with_options(u, options);
26ccd9
+	for (i = 0; i < argc; i++) {
26ccd9
+		if (strcmp(argv[i], "all") == 0) {
26ccd9
+			argv[0] = "all";
26ccd9
+			argc = 1;
26ccd9
+			break;
26ccd9
+		}
26ccd9
+
26ccd9
+		if (sscanf(argv[i], "mem%lu", &id) != 1) {
26ccd9
+			fprintf(stderr, "'%s' is not a valid memdev name\n",
26ccd9
+					argv[i]);
26ccd9
+			err++;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	if (err == argc) {
26ccd9
+		usage_with_options(u, options);
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (!param.outfile)
26ccd9
+		actx.f_out = stdout;
26ccd9
+	else {
26ccd9
+		actx.f_out = fopen(param.outfile, "w+");
26ccd9
+		if (!actx.f_out) {
26ccd9
+			fprintf(stderr, "failed to open: %s: (%s)\n",
26ccd9
+					param.outfile, strerror(errno));
26ccd9
+			rc = -errno;
26ccd9
+			goto out;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	if (!param.infile) {
26ccd9
+		actx.f_in = stdin;
26ccd9
+	} else {
26ccd9
+		actx.f_in = fopen(param.infile, "r");
26ccd9
+		if (!actx.f_in) {
26ccd9
+			fprintf(stderr, "failed to open: %s: (%s)\n",
26ccd9
+					param.infile, strerror(errno));
26ccd9
+			rc = -errno;
26ccd9
+			goto out_close_fout;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	if (param.verbose)
26ccd9
+		cxl_set_log_priority(ctx, LOG_DEBUG);
26ccd9
+
26ccd9
+	rc = 0;
26ccd9
+	err = 0;
26ccd9
+	count = 0;
26ccd9
+
26ccd9
+	for (i = 0; i < argc; i++) {
26ccd9
+		if (sscanf(argv[i], "mem%lu", &id) != 1
26ccd9
+				&& strcmp(argv[i], "all") != 0)
26ccd9
+			continue;
26ccd9
+
26ccd9
+		cxl_memdev_foreach (ctx, memdev) {
26ccd9
+			if (!util_cxl_memdev_filter(memdev, argv[i]))
26ccd9
+				continue;
26ccd9
+
26ccd9
+			if (action == action_write) {
26ccd9
+				single = memdev;
26ccd9
+				rc = 0;
26ccd9
+			} else
26ccd9
+				rc = action(memdev, &actx);
26ccd9
+
26ccd9
+			if (rc == 0)
26ccd9
+				count++;
26ccd9
+			else if (rc && !err)
26ccd9
+				err = rc;
26ccd9
+		}
26ccd9
+	}
26ccd9
+	rc = err;
26ccd9
+
26ccd9
+	if (action == action_write) {
26ccd9
+		if (count > 1) {
26ccd9
+			error("write-labels only supports writing a single memdev\n");
26ccd9
+			usage_with_options(u, options);
26ccd9
+			return -EINVAL;
26ccd9
+		} else if (single) {
26ccd9
+			rc = action(single, &actx);
26ccd9
+			if (rc)
26ccd9
+				count = 0;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	if (actx.f_in != stdin)
26ccd9
+		fclose(actx.f_in);
26ccd9
+
26ccd9
+ out_close_fout:
26ccd9
+	if (actx.f_out != stdout)
26ccd9
+		fclose(actx.f_out);
26ccd9
+
26ccd9
+ out:
26ccd9
+	/*
26ccd9
+	 * count if some actions succeeded, 0 if none were attempted,
26ccd9
+	 * negative error code otherwise.
26ccd9
+	 */
26ccd9
+	if (count > 0)
26ccd9
+		return count;
26ccd9
+	return rc;
26ccd9
+}
26ccd9
+
26ccd9
+int cmd_write_labels(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
+{
26ccd9
+	int count = memdev_action(argc, argv, ctx, action_write, write_options,
26ccd9
+			"cxl write-labels <memdev> [-i <filename>]");
26ccd9
+
26ccd9
+	fprintf(stderr, "wrote %d mem%s\n", count >= 0 ? count : 0,
26ccd9
+			count > 1 ? "s" : "");
26ccd9
+	return count >= 0 ? 0 : EXIT_FAILURE;
26ccd9
+}
26ccd9
+
26ccd9
+int cmd_read_labels(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
+{
26ccd9
+	int count = memdev_action(argc, argv, ctx, action_read, read_options,
26ccd9
+			"cxl read-labels <mem0> [<mem1>..<memN>] [-o <filename>]");
26ccd9
+
26ccd9
+	fprintf(stderr, "read %d mem%s\n", count >= 0 ? count : 0,
26ccd9
+			count > 1 ? "s" : "");
26ccd9
+	return count >= 0 ? 0 : EXIT_FAILURE;
26ccd9
+}
26ccd9
+
26ccd9
+int cmd_zero_labels(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
+{
26ccd9
+	int count = memdev_action(argc, argv, ctx, action_zero, zero_options,
26ccd9
+			"cxl zero-labels <mem0> [<mem1>..<memN>] [<options>]");
26ccd9
+
26ccd9
+	fprintf(stderr, "zeroed %d mem%s\n", count >= 0 ? count : 0,
26ccd9
+			count > 1 ? "s" : "");
26ccd9
+	return count >= 0 ? 0 : EXIT_FAILURE;
26ccd9
+}
26ccd9
-- 
26ccd9
2.27.0
26ccd9