Skip to content

Commit

Permalink
libndctl: set errno for routines that don't return an error status
Browse files Browse the repository at this point in the history
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.

Reported-by: Lukasz Dorau <[email protected]>
Cc: Dan Williams <[email protected]>
Reviewed-by: Dan Williams <[email protected]>
Signed-off-by: Vishal Verma <[email protected]>
  • Loading branch information
stellarhopper committed Oct 4, 2018
1 parent dd51f45 commit e46e0de
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 38 deletions.
8 changes: 6 additions & 2 deletions ndctl/lib/dimm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
79 changes: 67 additions & 12 deletions ndctl/lib/hpe1.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ static unsigned int hpe1_cmd_smart_get_flags(struct ndctl_cmd *cmd)
{
unsigned int hpe1flags;
unsigned int flags;
int rc;

if (hpe1_smart_valid(cmd) < 0)
rc = hpe1_smart_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

hpe1flags = CMD_HPE1_SMART(cmd)->out_valid_flags;
flags = 0;
Expand All @@ -118,9 +122,13 @@ static unsigned int hpe1_cmd_smart_get_health(struct ndctl_cmd *cmd)
{
unsigned char hpe1health;
unsigned int health;
int rc;

if (hpe1_smart_valid(cmd) < 0)
rc = hpe1_smart_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

hpe1health = CMD_HPE1_SMART(cmd)->stat_summary;
health = 0;
Expand All @@ -136,16 +144,26 @@ static unsigned int hpe1_cmd_smart_get_health(struct ndctl_cmd *cmd)

static unsigned int hpe1_cmd_smart_get_media_temperature(struct ndctl_cmd *cmd)
{
if (hpe1_smart_valid(cmd) < 0)
int rc;

rc = hpe1_smart_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

return CMD_HPE1_SMART(cmd)->curr_temp;
}

static unsigned int hpe1_cmd_smart_get_spares(struct ndctl_cmd *cmd)
{
if (hpe1_smart_valid(cmd) < 0)
int rc;

rc = hpe1_smart_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

return CMD_HPE1_SMART(cmd)->spare_blocks;
}
Expand All @@ -154,9 +172,13 @@ static unsigned int hpe1_cmd_smart_get_alarm_flags(struct ndctl_cmd *cmd)
{
unsigned int hpe1flags;
unsigned int flags;
int rc;

if (hpe1_smart_valid(cmd) < 0)
rc = hpe1_smart_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

hpe1flags = CMD_HPE1_SMART(cmd)->alarm_trips;
flags = 0;
Expand All @@ -170,18 +192,27 @@ static unsigned int hpe1_cmd_smart_get_alarm_flags(struct ndctl_cmd *cmd)

static unsigned int hpe1_cmd_smart_get_life_used(struct ndctl_cmd *cmd)
{
if (hpe1_smart_valid(cmd) < 0)
int rc;

rc = hpe1_smart_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

return CMD_HPE1_SMART(cmd)->device_life;
}

static unsigned int hpe1_cmd_smart_get_shutdown_state(struct ndctl_cmd *cmd)
{
unsigned int shutdown;
int rc;

if (hpe1_smart_valid(cmd) < 0)
rc = hpe1_smart_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

shutdown = CMD_HPE1_SMART(cmd)->last_shutdown_stat;
if (shutdown == NDN_HPE1_SMART_LASTSAVEGOOD)
Expand All @@ -192,16 +223,26 @@ static unsigned int hpe1_cmd_smart_get_shutdown_state(struct ndctl_cmd *cmd)

static unsigned int hpe1_cmd_smart_get_vendor_size(struct ndctl_cmd *cmd)
{
if (hpe1_smart_valid(cmd) < 0)
int rc;

rc = hpe1_smart_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

return CMD_HPE1_SMART(cmd)->vndr_spec_data_size;
}

static unsigned char *hpe1_cmd_smart_get_vendor_data(struct ndctl_cmd *cmd)
{
if (hpe1_smart_valid(cmd) < 0)
int rc;

rc = hpe1_smart_valid(cmd);
if (rc < 0) {
errno = -rc;
return NULL;
}

return CMD_HPE1_SMART(cmd)->vnd_spec_data;
}
Expand Down Expand Up @@ -265,9 +306,13 @@ static unsigned int hpe1_cmd_smart_threshold_get_alarm_control(struct ndctl_cmd
{
unsigned int hpe1flags;
unsigned int flags;
int rc;

if (hpe1_smart_threshold_valid(cmd) < 0)
rc = hpe1_smart_threshold_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

hpe1flags = CMD_HPE1_SMART_THRESH(cmd)->threshold_alarm_ctl;
flags = 0;
Expand All @@ -282,16 +327,26 @@ static unsigned int hpe1_cmd_smart_threshold_get_alarm_control(struct ndctl_cmd
static unsigned int hpe1_cmd_smart_threshold_get_media_temperature(
struct ndctl_cmd *cmd)
{
if (hpe1_smart_threshold_valid(cmd) < 0)
int rc;

rc = hpe1_smart_threshold_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

return CMD_HPE1_SMART_THRESH(cmd)->temp_threshold;
}

static unsigned int hpe1_cmd_smart_threshold_get_spares(struct ndctl_cmd *cmd)
{
if (hpe1_smart_threshold_valid(cmd) < 0)
int rc;

rc = hpe1_smart_threshold_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}

return CMD_HPE1_SMART_THRESH(cmd)->spare_block_threshold;
}
Expand Down
2 changes: 2 additions & 0 deletions ndctl/lib/inject.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,14 @@ NDCTL_EXPORT unsigned long long ndctl_bb_get_block(struct ndctl_bb *bb)
{
if (bb)
return bb->block;
errno = EINVAL;
return ULLONG_MAX;
}

NDCTL_EXPORT unsigned long long ndctl_bb_get_count(struct ndctl_bb *bb)
{
if (bb)
return bb->count;
errno = EINVAL;
return ULLONG_MAX;
}
45 changes: 38 additions & 7 deletions ndctl/lib/intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ static int intel_smart_valid(struct ndctl_cmd *cmd)
#define intel_smart_get_field(cmd, field) \
static unsigned int intel_cmd_smart_get_##field(struct ndctl_cmd *cmd) \
{ \
if (intel_smart_valid(cmd) < 0) \
int rc; \
rc = intel_smart_valid(cmd); \
if (rc < 0) { \
errno = -rc; \
return UINT_MAX; \
} \
return cmd->intel->smart.field; \
}

Expand Down Expand Up @@ -173,8 +177,12 @@ static int intel_smart_threshold_valid(struct ndctl_cmd *cmd)
static unsigned int intel_cmd_smart_threshold_get_##field( \
struct ndctl_cmd *cmd) \
{ \
if (intel_smart_threshold_valid(cmd) < 0) \
int rc; \
rc = intel_smart_threshold_valid(cmd); \
if (rc < 0) { \
errno = -rc; \
return UINT_MAX; \
} \
return cmd->intel->thresh.field; \
}

Expand Down Expand Up @@ -431,17 +439,25 @@ static int intel_fw_get_info_valid(struct ndctl_cmd *cmd)
static unsigned int intel_cmd_fw_info_get_##field( \
struct ndctl_cmd *cmd) \
{ \
if (intel_fw_get_info_valid(cmd) < 0) \
int rc; \
rc = intel_fw_get_info_valid(cmd); \
if (rc < 0) { \
errno = -rc; \
return UINT_MAX; \
} \
return cmd->intel->info.field; \
}

#define intel_fw_info_get_field64(cmd, field) \
static unsigned long long intel_cmd_fw_info_get_##field( \
struct ndctl_cmd *cmd) \
{ \
if (intel_fw_get_info_valid(cmd) < 0) \
int rc; \
rc = intel_fw_get_info_valid(cmd); \
if (rc < 0) { \
errno = -rc; \
return ULLONG_MAX; \
} \
return cmd->intel->info.field; \
}

Expand All @@ -454,8 +470,13 @@ intel_fw_info_get_field64(cmd, run_version);
static unsigned long long intel_cmd_fw_info_get_updated_version(
struct ndctl_cmd *cmd)
{
if (intel_fw_get_info_valid(cmd) < 0)
int rc;

rc = intel_fw_get_info_valid(cmd);
if (rc < 0) {
errno = -rc;
return ULLONG_MAX;
}
return cmd->intel->info.updated_version;

}
Expand Down Expand Up @@ -488,8 +509,13 @@ static int intel_fw_start_valid(struct ndctl_cmd *cmd)

static unsigned int intel_cmd_fw_start_get_context(struct ndctl_cmd *cmd)
{
if (intel_fw_start_valid(cmd) < 0)
int rc;

rc = intel_fw_start_valid(cmd);
if (rc < 0) {
errno = -rc;
return UINT_MAX;
}
return cmd->intel->start.context;
}

Expand Down Expand Up @@ -580,8 +606,13 @@ static int intel_fw_fquery_valid(struct ndctl_cmd *cmd)
static unsigned long long
intel_cmd_fw_fquery_get_fw_rev(struct ndctl_cmd *cmd)
{
if (intel_fw_fquery_valid(cmd) < 0)
int rc;

rc = intel_fw_fquery_valid(cmd);
if (rc < 0) {
errno = -rc;
return ULLONG_MAX;
}
return cmd->intel->fquery.updated_fw_rev;
}

Expand Down
Loading

0 comments on commit e46e0de

Please sign in to comment.