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

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