Skip to contentSkip to conversation

libndctl: set errno for routines that don't return an error status

PATCH In review Vishal Verma 2 participants 2 messages Oct 4, 2018 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

PATCH v2 1/1 libndctl: set errno for routines that don't return an error status

For routines that return a UINT_MAX or UL{L}ONG_MAX, there isn't a way to get any information as to what went wrong. Set errno in such routines so that the callers can get some additional context about the error.

---
 ndctl/lib/dimm.c     |  8 +++--
 ndctl/lib/hpe1.c     | 79 +++++++++++++++++++++++++++++++++++++-------
 ndctl/lib/inject.c   |  2 ++
 ndctl/lib/intel.c    | 45 +++++++++++++++++++++----
 ndctl/lib/libndctl.c | 54 ++++++++++++++++++++++--------
 ndctl/lib/msft.c     | 27 ++++++++++++---
 6 files changed, 177 insertions(+), 38 deletions(-)

v2: s/EOVERFLOW/ENOMEM/ (Dan)

ndctl/lib/dimm.c +6 -2
index b3e032e..5e41734 100644
@@ -565,17 +565,21 @@ NDCTL_EXPORT unsigned long ndctl_dimm_get_available_labels(
 {
 	struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
 	char *path = dimm->dimm_buf;
-	int len = dimm->buf_len;
+	int rc, len = dimm->buf_len;
 	char buf[20];
 
 	if (snprintf(path, len, "%s/available_slots", dimm->dimm_path) >= len) {
 		err(ctx, "%s: buffer too small!\n",
 				ndctl_dimm_get_devname(dimm));
+		errno = ENOMEM;
 		return ULONG_MAX;
 	}
 
-	if (sysfs_read_attr(ctx, path, buf) < 0)
+	rc = sysfs_read_attr(ctx, path, buf);
+	if (rc < 0) {
+		errno = -rc;
 		return ULONG_MAX;
+	}
 
 	return strtoul(buf, NULL, 0);
 }
ndctl/lib/hpe1.c +67 -12

168 lines not on this page · raw message

ndctl/lib/inject.c +2 -0
index 268c5cd..2b0702e 100644
@@ -498,6 +498,7 @@ NDCTL_EXPORT unsigned long long ndctl_bb_get_block(struct ndctl_bb *bb)
 {
 	if (bb)
 		return bb->block;
+	errno = EINVAL;
 	return ULLONG_MAX;
 }
 
@@ -505,5 +506,6 @@ NDCTL_EXPORT unsigned long long ndctl_bb_get_count(struct ndctl_bb *bb)
 {
 	if (bb)
 		return bb->count;
+	errno = EINVAL;
 	return ULLONG_MAX;
 }
ndctl/lib/intel.c +38 -7

94 lines not on this page · raw message

ndctl/lib/libndctl.c +41 -13

162 lines not on this page · raw message

ndctl/lib/msft.c +23 -4
index e15bc07..19453cd 100644
@@ -77,8 +77,13 @@ static int msft_smart_valid(struct ndctl_cmd *cmd)
 
 static unsigned int msft_cmd_smart_get_flags(struct ndctl_cmd *cmd)
 {
-	if (msft_smart_valid(cmd) < 0)
+	int rc;
+
+	rc = msft_smart_valid(cmd);
+	if (rc < 0) {
+		errno = -rc;
 		return UINT_MAX;
+	}
 
 	/* below health data can be retrieved via MSFT _DSM function 11 */
 	return NDN_MSFT_SMART_HEALTH_VALID |
@@ -103,9 +108,13 @@ static unsigned int msft_cmd_smart_get_health(struct ndctl_cmd *cmd)
 {
 	unsigned int health;
 	unsigned int num;
+	int rc;
 
-	if (msft_smart_valid(cmd) < 0)
+	rc = msft_smart_valid(cmd);
+	if (rc < 0) {
+		errno = -rc;
 		return UINT_MAX;
+	}
 
 	num = num_set_bit_health(CMD_MSFT_SMART(cmd)->health);
 	if (num == 0)
@@ -122,16 +131,26 @@ static unsigned int msft_cmd_smart_get_health(struct ndctl_cmd *cmd)
 
 static unsigned int msft_cmd_smart_get_media_temperature(struct ndctl_cmd *cmd)
 {
-	if (msft_smart_valid(cmd) < 0)
+	int rc;
+
+	rc = msft_smart_valid(cmd);
+	if (rc < 0) {
+		errno = -rc;
 		return UINT_MAX;
+	}
 
 	return CMD_MSFT_SMART(cmd)->temp * 16;
 }
 
 static unsigned int msft_cmd_smart_get_life_used(struct ndctl_cmd *cmd)
 {
-	if (msft_smart_valid(cmd) < 0)
+	int rc;
+
+	rc = msft_smart_valid(cmd);
+	if (rc < 0) {
+		errno = -rc;
 		return UINT_MAX;
+	}
 
 	return 100 - CMD_MSFT_SMART(cmd)->nvm_lifetime;
 }
Signature · 6 lines
2.17.1

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Vishal Verma

For routines that return a UINT_MAX or UL{L}ONG_MAX, there isn't a way
to get any information as to what went wrong. Set errno in such routines
so that the callers can get some additional context about the error.

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