Commit a7320456f1bc "ndctl: add sector_size to 'ndctl list' output"
mishandles the case where a namespace does not specify a sector_size,
but otherwise supports a sector_size selection:

    # ndctl list --namespace=namespace1.0
    {
      "dev":"namespace1.0",
      "mode":"memory",
      "size":32763805696,
      "uuid":"7c985ba5-6d33-48bd-8fde-6c25a520abe0",
      "sector_size":-1,
      "blockdev":"pmem1",
      "numa_node":0
    }

Fix this and clean up the output to only provide a sector_size in the
non-default (i.e. != 512 bytes) case.

Fixes: a7320456f1bc ("ndctl: add sector_size to 'ndctl list' output")
Cc: Ross Zwisler <[email protected]>
Signed-off-by: Dan Williams <[email protected]>
---
 util/json.c |   31 +++++++++++++------------------
 1 file changed, 13 insertions(+), 18 deletions(-)

diff --git a/util/json.c b/util/json.c
index 17d8f135b686..e7f789b36385 100644
--- a/util/json.c
+++ b/util/json.c
@@ -646,6 +646,7 @@ struct json_object *util_namespace_to_json(struct 
ndctl_namespace *ndns,
        struct json_object *jndns = json_object_new_object();
        struct json_object *jobj, *jbbs = NULL;
        unsigned long long size = ULLONG_MAX;
+       unsigned int sector_size = UINT_MAX;
        enum ndctl_namespace_mode mode;
        const char *bdev = NULL, *name;
        unsigned int bb_count = 0;
@@ -769,29 +770,23 @@ struct json_object *util_namespace_to_json(struct 
ndctl_namespace *ndns,
        } else
                bdev = ndctl_namespace_get_block_device(ndns);
 
-       jobj = NULL;
-       if (btt) {
-               jobj = json_object_new_int(ndctl_btt_get_sector_size(btt));
-               if (!jobj)
-                       goto err;
-       } else if (!dax) {
-               unsigned int sector_size = 
ndctl_namespace_get_sector_size(ndns);
-
-               /*
-                * The kernel will default to a 512 byte sector size on PMEM
-                * namespaces that don't explicitly have a sector size. This
-                * happens because they use pre-v1.2 labels or because they
-                * don't have a label space (devtype=nd_namespace_io).
-                */
-               if (!sector_size)
-                       sector_size = 512;
+       if (btt)
+               sector_size = ndctl_btt_get_sector_size(btt);
+       else if (!dax)
+               sector_size = ndctl_namespace_get_sector_size(ndns);
 
+       /*
+        * The kernel will default to a 512 byte sector size on PMEM
+        * namespaces that don't explicitly have a sector size. This
+        * happens because they use pre-v1.2 labels or because they
+        * don't have a label space (devtype=nd_namespace_io).
+        */
+       if (sector_size < UINT_MAX && sector_size > 512) {
                jobj = json_object_new_int(sector_size);
                if (!jobj)
                        goto err;
-       }
-       if (jobj)
                json_object_object_add(jndns, "sector_size", jobj);
+       }
 
        if (bdev && bdev[0]) {
                jobj = json_object_new_string(bdev);

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

Reply via email to