Blame SOURCES/0027-ndctl-dimm-Fix-label-index-block-calculations.patch

26ccd9
From 7e98977c10ad5f4baf5e3bc4d5b4b2fd733a8b7e Mon Sep 17 00:00:00 2001
26ccd9
From: Jingqi Liu <jingqi.liu@intel.com>
26ccd9
Date: Thu, 8 Jul 2021 16:14:46 +0800
26ccd9
Subject: [PATCH 027/217] ndctl/dimm: Fix label index block calculations
26ccd9
26ccd9
The following bug is caused by setting the size of Label Index Block
26ccd9
to a fixed 256 bytes.
26ccd9
26ccd9
Use the following Qemu command to start a Guest with 2MB label-size:
26ccd9
	-object memory-backend-file,id=mem1,share=on,mem-path=/dev/dax1.1,size=14G,align=2M
26ccd9
	-device nvdimm,memdev=mem1,id=nv1,label-size=2M
26ccd9
26ccd9
There is a namespace in the Guest as follows:
26ccd9
	$ ndctl list
26ccd9
	[
26ccd9
	  {
26ccd9
	    "dev":"namespace0.0",
26ccd9
	    "mode":"devdax",
26ccd9
	    "map":"dev",
26ccd9
	    "size":14780727296,
26ccd9
	    "uuid":"58ad5282-5a16-404f-b8ee-e28b4c784eb8",
26ccd9
	    "chardev":"dax0.0",
26ccd9
	    "align":2097152,
26ccd9
	    "name":"namespace0.0"
26ccd9
	  }
26ccd9
	]
26ccd9
26ccd9
Fail to read labels. The result is as follows:
26ccd9
	$ ndctl read-labels -u nmem0
26ccd9
	[
26ccd9
	]
26ccd9
	read 0 nmem
26ccd9
26ccd9
If using the following Qemu command to start the Guest with 128K
26ccd9
label-size, this label can be read correctly.
26ccd9
	-object memory-backend-file,id=mem1,share=on,mem-path=/dev/dax1.1,size=14G,align=2M
26ccd9
	-device nvdimm,memdev=mem1,id=nv1,label-size=128K
26ccd9
26ccd9
The size of a Label Index Block depends on how many label slots fit into
26ccd9
the label storage area. The minimum size of an index block is 256 bytes
26ccd9
and the size must be a multiple of 256 bytes. For a storage area of 128KB,
26ccd9
the corresponding Label Index Block size is 256 bytes. But if the label
26ccd9
storage area is not 128KB, the Label Index Block size should not be 256 bytes.
26ccd9
26ccd9
Namespace Label Index Block appears twice at the top of the label storage area.
26ccd9
Following the two index blocks, an array for storing labels takes up the
26ccd9
remainder of the label storage area.
26ccd9
26ccd9
For obtaining the size of Namespace Index Block, we also cannot rely on
26ccd9
the field of 'mysize' in this index block since it might be corrupted.
26ccd9
Similar to the linux kernel, we use sizeof_namespace_index() to get the size
26ccd9
of Namespace Index Block. Then we can also correctly calculate the starting
26ccd9
offset of the following namespace labels.
26ccd9
26ccd9
Link: https://lore.kernel.org/r/20210708081446.14323-1-jingqi.liu@intel.com
26ccd9
Suggested-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
Reviewed-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
Signed-off-by: Jingqi Liu <jingqi.liu@intel.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 ndctl/dimm.c           | 19 +++++++++++++++----
26ccd9
 ndctl/lib/dimm.c       |  5 +++++
26ccd9
 ndctl/lib/libndctl.sym |  1 +
26ccd9
 ndctl/libndctl.h       |  1 +
26ccd9
 4 files changed, 22 insertions(+), 4 deletions(-)
26ccd9
26ccd9
diff --git a/ndctl/dimm.c b/ndctl/dimm.c
26ccd9
index 09ce49e..1d2d9a2 100644
26ccd9
--- a/ndctl/dimm.c
26ccd9
+++ b/ndctl/dimm.c
26ccd9
@@ -94,13 +94,18 @@ static struct json_object *dump_label_json(struct ndctl_dimm *dimm,
26ccd9
 	struct json_object *jarray = json_object_new_array();
26ccd9
 	struct json_object *jlabel = NULL;
26ccd9
 	struct namespace_label nslabel;
26ccd9
+	unsigned int nsindex_size;
26ccd9
 	unsigned int slot = -1;
26ccd9
 	ssize_t offset;
26ccd9
 
26ccd9
 	if (!jarray)
26ccd9
 		return NULL;
26ccd9
 
26ccd9
-	for (offset = NSINDEX_ALIGN * 2; offset < size;
26ccd9
+	nsindex_size = ndctl_dimm_sizeof_namespace_index(dimm);
26ccd9
+	if (nsindex_size == 0)
26ccd9
+		return NULL;
26ccd9
+
26ccd9
+	for (offset = nsindex_size * 2; offset < size;
26ccd9
 			offset += ndctl_dimm_sizeof_namespace_label(dimm)) {
26ccd9
 		ssize_t len = min_t(ssize_t,
26ccd9
 				ndctl_dimm_sizeof_namespace_label(dimm),
26ccd9
@@ -204,17 +209,23 @@ static struct json_object *dump_label_json(struct ndctl_dimm *dimm,
26ccd9
 	return jarray;
26ccd9
 }
26ccd9
 
26ccd9
-static struct json_object *dump_index_json(struct ndctl_cmd *cmd_read, ssize_t size)
26ccd9
+static struct json_object *dump_index_json(struct ndctl_dimm *dimm,
26ccd9
+		struct ndctl_cmd *cmd_read, ssize_t size)
26ccd9
 {
26ccd9
 	struct json_object *jarray = json_object_new_array();
26ccd9
 	struct json_object *jindex = NULL;
26ccd9
 	struct namespace_index nsindex;
26ccd9
+	unsigned int nsindex_size;
26ccd9
 	ssize_t offset;
26ccd9
 
26ccd9
 	if (!jarray)
26ccd9
 		return NULL;
26ccd9
 
26ccd9
-	for (offset = 0; offset < NSINDEX_ALIGN * 2; offset += NSINDEX_ALIGN) {
26ccd9
+	nsindex_size = ndctl_dimm_sizeof_namespace_index(dimm);
26ccd9
+	if (nsindex_size == 0)
26ccd9
+		return NULL;
26ccd9
+
26ccd9
+	for (offset = 0; offset < nsindex_size * 2; offset += nsindex_size) {
26ccd9
 		ssize_t len = min_t(ssize_t, sizeof(nsindex), size - offset);
26ccd9
 		struct json_object *jobj;
26ccd9
 
26ccd9
@@ -288,7 +299,7 @@ static struct json_object *dump_json(struct ndctl_dimm *dimm,
26ccd9
 		goto err;
26ccd9
 	json_object_object_add(jdimm, "dev", jobj);
26ccd9
 
26ccd9
-	jindex = dump_index_json(cmd_read, size);
26ccd9
+	jindex = dump_index_json(dimm, cmd_read, size);
26ccd9
 	if (!jindex)
26ccd9
 		goto err;
26ccd9
 	json_object_object_add(jdimm, "index", jindex);
26ccd9
diff --git a/ndctl/lib/dimm.c b/ndctl/lib/dimm.c
26ccd9
index c045cbe..9e36e28 100644
26ccd9
--- a/ndctl/lib/dimm.c
26ccd9
+++ b/ndctl/lib/dimm.c
26ccd9
@@ -256,6 +256,11 @@ static int __label_validate(struct nvdimm_data *ndd)
26ccd9
 	return -EINVAL;
26ccd9
 }
26ccd9
 
26ccd9
+NDCTL_EXPORT unsigned int ndctl_dimm_sizeof_namespace_index(struct ndctl_dimm *dimm)
26ccd9
+{
26ccd9
+	return sizeof_namespace_index(&dimm->ndd);
26ccd9
+}
26ccd9
+
26ccd9
 /*
26ccd9
  * If the dimm labels have not been previously validated this routine
26ccd9
  * will make up a default size. Otherwise, it will pick the size based
26ccd9
diff --git a/ndctl/lib/libndctl.sym b/ndctl/lib/libndctl.sym
26ccd9
index 58afb74..5ee73b7 100644
26ccd9
--- a/ndctl/lib/libndctl.sym
26ccd9
+++ b/ndctl/lib/libndctl.sym
26ccd9
@@ -454,4 +454,5 @@ LIBNDCTL_25 {
26ccd9
 
26ccd9
 LIBNDCTL_26 {
26ccd9
 	ndctl_bus_nfit_translate_spa;
26ccd9
+	ndctl_dimm_sizeof_namespace_index;
26ccd9
 } LIBNDCTL_25;
26ccd9
diff --git a/ndctl/libndctl.h b/ndctl/libndctl.h
26ccd9
index 87d07b7..df109bb 100644
26ccd9
--- a/ndctl/libndctl.h
26ccd9
+++ b/ndctl/libndctl.h
26ccd9
@@ -337,6 +337,7 @@ int ndctl_dimm_init_labels(struct ndctl_dimm *dimm,
26ccd9
 		enum ndctl_namespace_version v);
26ccd9
 unsigned long ndctl_dimm_get_available_labels(struct ndctl_dimm *dimm);
26ccd9
 unsigned int ndctl_dimm_sizeof_namespace_label(struct ndctl_dimm *dimm);
26ccd9
+unsigned int ndctl_dimm_sizeof_namespace_index(struct ndctl_dimm *dimm);
26ccd9
 unsigned int ndctl_cmd_cfg_size_get_size(struct ndctl_cmd *cfg_size);
26ccd9
 ssize_t ndctl_cmd_cfg_read_get_data(struct ndctl_cmd *cfg_read, void *buf,
26ccd9
 		unsigned int len, unsigned int offset);
26ccd9
-- 
26ccd9
2.27.0
26ccd9