diff mbox series

[ndctl,4/5] util/json: consolidate check for the UTIL_JSON_MEDIA_ERRORS flag

Message ID 20181002033140.21295-4-vishal.l.verma@intel.com (mailing list archive)
State New, archived
Headers show
Series [ndctl,1/5] ndctl, lib: refactor badblocks retrieval routines | expand

Commit Message

Vishal Verma Oct. 2, 2018, 3:31 a.m. UTC
We don't need to check for the above flag for each badblock we're
iterating over. Remove the check in the respective loops, and return
early if it is not set.

Cc: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 util/json.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

Comments

Dan Williams Oct. 3, 2018, 1:52 a.m. UTC | #1
On Mon, Oct 1, 2018 at 8:38 PM Vishal Verma <vishal.l.verma@intel.com> wrote:
>
> We don't need to check for the above flag for each badblock we're
> iterating over. Remove the check in the respective loops, and return
> early if it is not set.
>
> Cc: Dan Williams <dan.j.williams@intel.com>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
> ---
>  util/json.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
>
> diff --git a/util/json.c b/util/json.c
> index 30b56c3..4dd5fc3 100644
> --- a/util/json.c
> +++ b/util/json.c
> @@ -457,7 +457,8 @@ struct json_object *util_region_badblocks_to_json(struct ndctl_region *region,
>                 jbbs = json_object_new_array();
>                 if (!jbbs)
>                         return NULL;
> -       }
> +       } else
> +               return NULL;
>
>         ndctl_region_badblock_foreach(region, bb) {
>                 struct json_object *jdimms;
> @@ -465,9 +466,6 @@ struct json_object *util_region_badblocks_to_json(struct ndctl_region *region,
>
>                 bbs += bb->len;
>
> -               if (!(flags & UTIL_JSON_MEDIA_ERRORS))
> -                       continue;
> -

The reason for this awkwardness is to account for the total badblocks
count even when the individual badblocks are not emitted. See that
"bbs += bb->len" that will now be skipped. Perhaps at least a comment
is needed because that trickery is not obvious.
diff mbox series

Patch

diff --git a/util/json.c b/util/json.c
index 30b56c3..4dd5fc3 100644
--- a/util/json.c
+++ b/util/json.c
@@ -457,7 +457,8 @@  struct json_object *util_region_badblocks_to_json(struct ndctl_region *region,
 		jbbs = json_object_new_array();
 		if (!jbbs)
 			return NULL;
-	}
+	} else
+		return NULL;
 
 	ndctl_region_badblock_foreach(region, bb) {
 		struct json_object *jdimms;
@@ -465,9 +466,6 @@  struct json_object *util_region_badblocks_to_json(struct ndctl_region *region,
 
 		bbs += bb->len;
 
-		if (!(flags & UTIL_JSON_MEDIA_ERRORS))
-			continue;
-
 		/* get start address of region */
 		addr = ndctl_region_get_resource(region);
 		if (addr == ULLONG_MAX)
@@ -527,7 +525,8 @@  static struct json_object *dev_badblocks_to_json(struct ndctl_region *region,
 		jbbs = json_object_new_array();
 		if (!jbbs)
 			return NULL;
-	}
+	} else
+		return NULL;
 
 	ndctl_region_badblock_foreach(region, bb) {
 		unsigned long long bb_begin, bb_end, begin, end;
@@ -554,9 +553,6 @@  static struct json_object *dev_badblocks_to_json(struct ndctl_region *region,
 
 		bbs += len;
 
-		if (!(flags & UTIL_JSON_MEDIA_ERRORS))
-			continue;
-
 		jbb = json_object_new_object();
 		if (!jbb)
 			goto err_array;