Skip to content

Commit 8f29e20

Browse files
committed
feat: use nvme_show_err() helper function
It introduced to handle the negative error and the postive status codes. Then reduce the duplicated error and status codes output implementation. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent 9e36c66 commit 8f29e20

1 file changed

Lines changed: 40 additions & 45 deletions

File tree

plugins/feat/feat-nvme.c

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#define STR(x) #x
1313
#define TMT(n) "thermal management temperature " STR(n)
14+
#define show_error(err, fmt, ...) show_err(err, __func__, fmt, ##__VA_ARGS__)
1415

1516
struct perfc_config {
1617
__u32 namespace_id;
@@ -52,6 +53,22 @@ static const char *power_limit_feat = "power limit feature";
5253
static const char *power_thresh_feat = "power threshold feature";
5354
static const char *power_meas_feat = "power measurement feature";
5455

56+
static void show_err(int err, const char *func, const char *fmt, ...)
57+
{
58+
va_list ap;
59+
60+
_cleanup_free_ char *msg = NULL;
61+
62+
va_start(ap, fmt);
63+
64+
if (vasprintf(&msg, fmt, ap) > 0)
65+
nvme_show_err(msg, err);
66+
else
67+
nvme_show_err(func, err);
68+
69+
va_end(ap);
70+
}
71+
5572
static int feat_get(struct nvme_transport_handle *hdl, const __u8 fid,
5673
__u32 cdw11, __u8 sel, __u8 uidx, const char *feat)
5774
{
@@ -75,16 +92,14 @@ static int feat_get(struct nvme_transport_handle *hdl, const __u8 fid,
7592

7693
nvme_show_init();
7794

78-
if (!err) {
95+
if (err) {
96+
show_error(err, "Get %s", feat);
97+
} else {
7998
nvme_feature_show(fid, sel, result);
8099
if (NVME_CHECK(sel, GET_FEATURES_SEL, SUPPORTED))
81100
nvme_show_select_result(fid, result);
82101
else
83102
nvme_feature_show_fields(fid, result, buf);
84-
} else if (err > 0) {
85-
nvme_show_status(err);
86-
} else {
87-
nvme_show_error("Get %s: %s", feat, nvme_strerror(errno));
88103
}
89104

90105
nvme_show_finish();
@@ -104,10 +119,8 @@ static int power_mgmt_set(struct nvme_transport_handle *hdl, const __u8 fid,
104119

105120
nvme_show_init();
106121

107-
if (err > 0) {
108-
nvme_show_status(err);
109-
} else if (err < 0) {
110-
nvme_show_perror("Set %s", power_mgmt_feat);
122+
if (err) {
123+
show_error(err, "Set %s", power_mgmt_feat);
111124
} else {
112125
nvme_show_result("Set %s: 0x%04x (%s)", power_mgmt_feat, cdw11,
113126
sv ? "Save" : "Not save");
@@ -197,10 +210,8 @@ static int perfc_set(struct nvme_transport_handle *hdl, __u8 fid, __u32 cdw11,
197210

198211
nvme_show_init();
199212

200-
if (err > 0) {
201-
nvme_show_status(err);
202-
} else if (err < 0) {
203-
nvme_show_perror("Set %s", perfc_feat);
213+
if (err) {
214+
show_error(err, "Set %s", perfc_feat);
204215
} else {
205216
nvme_show_result("Set %s: 0x%04x (%s)", perfc_feat, cdw11,
206217
sv ? "Save" : "Not save");
@@ -269,10 +280,8 @@ static int hctm_set(struct nvme_transport_handle *hdl, const __u8 fid,
269280

270281
nvme_show_init();
271282

272-
if (err > 0) {
273-
nvme_show_status(err);
274-
} else if (err < 0) {
275-
nvme_show_perror("Set %s", hctm_feat);
283+
if (err) {
284+
show_error(err, "Set %s", hctm_feat);
276285
} else {
277286
nvme_show_result("Set %s: 0x%04x (%s)", hctm_feat, cdw11,
278287
sv ? "Save" : "Not save");
@@ -333,10 +342,8 @@ static int timestamp_set(struct nvme_transport_handle *hdl, const __u8 fid,
333342

334343
nvme_show_init();
335344

336-
if (err > 0) {
337-
nvme_show_status(err);
338-
} else if (err < 0) {
339-
nvme_show_perror("Set %s", timestamp_feat);
345+
if (err) {
346+
show_error(err, "Set %s", timestamp_feat);
340347
} else {
341348
nvme_show_result("Set %s: (%s)", timestamp_feat, sv ? "Save" : "Not save");
342349
nvme_feature_show_fields(fid, 0, (unsigned char *)&ts);
@@ -412,10 +419,8 @@ static int temp_thresh_set(struct nvme_transport_handle *hdl, const __u8 fid,
412419

413420
nvme_show_init();
414421

415-
if (err > 0) {
416-
nvme_show_status(err);
417-
} else if (err < 0) {
418-
nvme_show_perror("Set %s", temp_thresh_feat);
422+
if (err) {
423+
show_error(err, "Set %s", temp_thresh_feat);
419424
} else {
420425
nvme_show_result("Set %s: (%s)", temp_thresh_feat, sv ? "Save" : "Not save");
421426
nvme_feature_show_fields(fid, NVME_SET(cfg->tmpth, FEAT_TT_TMPTH) |
@@ -499,10 +504,8 @@ static int arbitration_set(struct nvme_transport_handle *hdl, const __u8 fid,
499504

500505
nvme_show_init();
501506

502-
if (err > 0) {
503-
nvme_show_status(err);
504-
} else if (err < 0) {
505-
nvme_show_perror("Set %s", arbitration_feat);
507+
if (err) {
508+
show_error(err, "Set %s", arbitration_feat);
506509
} else {
507510
nvme_show_result("Set %s: (%s)", arbitration_feat, sv ? "Save" : "Not save");
508511
nvme_feature_show_fields(fid, NVME_SET(cfg->ab, FEAT_ARBITRATION_BURST) |
@@ -558,10 +561,8 @@ static int volatile_wc_set(struct nvme_transport_handle *hdl, const __u8 fid,
558561

559562
nvme_show_init();
560563

561-
if (err > 0) {
562-
nvme_show_status(err);
563-
} else if (err < 0) {
564-
nvme_show_perror("Set %s", volatile_wc_feat);
564+
if (err) {
565+
show_error(err, "Set %s", volatile_wc_feat);
565566
} else {
566567
nvme_show_result("Set %s: 0x%04x (%s)", volatile_wc_feat, cdw11,
567568
sv ? "Save" : "Not save");
@@ -617,10 +618,8 @@ static int power_limit_set(struct nvme_transport_handle *hdl, const __u8 fid,
617618

618619
nvme_show_init();
619620

620-
if (err > 0) {
621-
nvme_show_status(err);
622-
} else if (err < 0) {
623-
nvme_show_perror("Set %s", power_limit_feat);
621+
if (err) {
622+
show_error(err, "Set %s", power_limit_feat);
624623
} else {
625624
nvme_show_result("Set %s: 0x%04x (%s)", power_limit_feat, cdw13,
626625
sv ? "Save" : "Not save");
@@ -689,10 +688,8 @@ static int power_thresh_set(struct nvme_transport_handle *hdl, const __u8 fid,
689688

690689
nvme_show_init();
691690

692-
if (err > 0) {
693-
nvme_show_status(err);
694-
} else if (err < 0) {
695-
nvme_show_perror("Set %s", power_thresh_feat);
691+
if (err) {
692+
show_error(err, "Set %s", power_thresh_feat);
696693
} else {
697694
nvme_show_result("Set %s: 0x%04x (%s)", power_thresh_feat,
698695
cdw11, sv ? "Save" : "Not save");
@@ -769,10 +766,8 @@ static int power_meas_set(struct nvme_transport_handle *hdl, const __u8 fid,
769766

770767
nvme_show_init();
771768

772-
if (err > 0) {
773-
nvme_show_status(err);
774-
} else if (err < 0) {
775-
nvme_show_perror("Set %s", power_meas_feat);
769+
if (err) {
770+
show_error(err, "Set %s", power_meas_feat);
776771
} else {
777772
nvme_show_result("Set %s: 0x%04x (%s)", power_meas_feat, cdw11,
778773
sv ? "Save" : "Not save");

0 commit comments

Comments
 (0)