Skip to content

Commit e2025d6

Browse files
martin-gpyigaw
authored andcommitted
netapp-smdevices: segregate print routines
Segregate printing the respective normal/column and json outputs into separate routines for better readability. Signed-off-by: Martin George <marting@netapp.com> Tested-by: Clayton Skaggs <claytons@netapp.com>
1 parent 902f562 commit e2025d6

File tree

1 file changed

+66
-30
lines changed

1 file changed

+66
-30
lines changed

plugins/netapp/netapp-nvme.c

+66-30
Original file line numberDiff line numberDiff line change
@@ -251,23 +251,24 @@ static void netapp_ontapdevice_json(struct json_object *devices, char *devname,
251251
json_array_add_value_object(devices, device_attrs);
252252
}
253253

254-
static void netapp_smdevices_print(struct smdevice_info *devices, int count, int format)
254+
static void netapp_smdevices_print_regular(struct smdevice_info *devices,
255+
int count, int format)
255256
{
256-
struct json_object *root = NULL;
257-
struct json_object *json_devices = NULL;
258257
int i, slta;
259258
char array_label[ARRAY_LABEL_LEN / 2 + 1];
260259
char volume_label[VOLUME_LABEL_LEN / 2 + 1];
261260
char nguid_str[33];
261+
__u8 lba_index;
262+
263+
char *formatstr = NULL;
262264
char basestr[] =
263265
"%s, Array Name %s, Volume Name %s, NSID %d, Volume ID %s, Controller %c, Access State %s, %s\n";
264266
char columnstr[] = "%-16s %-30s %-30s %4d %32s %c %-12s %9s\n";
265-
char *formatstr = basestr; /* default to "normal" output format */
266-
__u8 lba_index;
267267

268-
if (format == NCOLUMN) {
269-
/* for column output, change output string and print column headers */
270-
formatstr = columnstr;
268+
if (format == NNORMAL)
269+
formatstr = basestr;
270+
else if (format == NCOLUMN) {
271+
/* change output string and print column headers */
271272
printf("%-16s %-30s %-30s %-4s %-32s %-4s %-12s %-9s\n",
272273
"Device", "Array Name", "Volume Name", "NSID",
273274
"Volume ID", "Ctrl", "Access State", " Size");
@@ -276,44 +277,74 @@ static void netapp_smdevices_print(struct smdevice_info *devices, int count, int
276277
"------------------------------", "----",
277278
"--------------------------------", "----",
278279
"------------", "---------");
279-
} else if (format == NJSON) {
280-
/* prepare for json output */
281-
root = json_create_object();
282-
json_devices = json_create_array();
280+
formatstr = columnstr;
283281
}
284282

285283
for (i = 0; i < count; i++) {
286284
nvme_id_ns_flbas_to_lbaf_inuse(devices[i].ns.flbas, &lba_index);
287-
unsigned long long lba = 1ULL << devices[i].ns.lbaf[lba_index].ds;
285+
unsigned long long lba = 1ULL <<
286+
devices[i].ns.lbaf[lba_index].ds;
288287
double nsze = le64_to_cpu(devices[i].ns.nsze) * lba;
289288
const char *s_suffix = suffix_si_get(&nsze);
290289
char size[128];
291290

292291
sprintf(size, "%.2f%sB", nsze, s_suffix);
293-
netapp_convert_string(array_label, (char *)&devices[i].ctrl.vs[20],
294-
ARRAY_LABEL_LEN / 2);
292+
netapp_convert_string(array_label,
293+
(char *)&devices[i].ctrl.vs[20],
294+
ARRAY_LABEL_LEN / 2);
295295
slta = devices[i].ctrl.vs[0] & 0x1;
296296
netapp_convert_string(volume_label, (char *)devices[i].ns.vs,
297297
VOLUME_LABEL_LEN / 2);
298298
netapp_nguid_to_str(nguid_str, devices[i].ns.nguid);
299-
if (format == NJSON)
300-
netapp_smdevice_json(json_devices, devices[i].dev,
301-
array_label, volume_label, devices[i].nsid,
302-
nguid_str, slta ? "A" : "B", "unknown", size,
303-
lba, le64_to_cpu(devices[i].ns.nsze));
304-
else
305-
printf(formatstr, devices[i].dev, array_label,
299+
printf(formatstr, devices[i].dev, array_label,
306300
volume_label, devices[i].nsid, nguid_str,
307301
slta ? 'A' : 'B', "unknown", size);
302+
}
308303
}
304+
}
305+
306+
static void netapp_smdevices_print_json(struct smdevice_info *devices,
307+
int count)
308+
{
309+
struct json_object *root = NULL;
310+
struct json_object *json_devices = NULL;
311+
int i, slta;
312+
char array_label[ARRAY_LABEL_LEN / 2 + 1];
313+
char volume_label[VOLUME_LABEL_LEN / 2 + 1];
314+
char nguid_str[33];
315+
__u8 lba_index;
309316

310-
if (format == NJSON) {
311-
/* complete the json output */
312-
json_object_add_value_array(root, "SMdevices", json_devices);
313-
json_print_object(root, NULL);
314-
printf("\n");
315-
json_free_object(root);
317+
/* prepare for the json output */
318+
root = json_create_object();
319+
json_devices = json_create_array();
320+
321+
for (i = 0; i < count; i++) {
322+
nvme_id_ns_flbas_to_lbaf_inuse(devices[i].ns.flbas, &lba_index);
323+
unsigned long long lba = 1ULL <<
324+
devices[i].ns.lbaf[lba_index].ds;
325+
double nsze = le64_to_cpu(devices[i].ns.nsze) * lba;
326+
const char *s_suffix = suffix_si_get(&nsze);
327+
char size[128];
328+
329+
sprintf(size, "%.2f%sB", nsze, s_suffix);
330+
netapp_convert_string(array_label,
331+
(char *)&devices[i].ctrl.vs[20],
332+
ARRAY_LABEL_LEN / 2);
333+
slta = devices[i].ctrl.vs[0] & 0x1;
334+
netapp_convert_string(volume_label, (char *)devices[i].ns.vs,
335+
VOLUME_LABEL_LEN / 2);
336+
netapp_nguid_to_str(nguid_str, devices[i].ns.nguid);
337+
netapp_smdevice_json(json_devices, devices[i].dev,
338+
array_label, volume_label, devices[i].nsid,
339+
nguid_str, slta ? "A" : "B", "unknown", size,
340+
lba, le64_to_cpu(devices[i].ns.nsze));
316341
}
342+
343+
/* complete the json output */
344+
json_object_add_value_array(root, "SMdevices", json_devices);
345+
json_print_object(root, NULL);
346+
printf("\n");
347+
json_free_object(root);
317348
}
318349

319350
static void netapp_ontapdevices_print_regular(struct ontapdevice_info *devices,
@@ -639,8 +670,13 @@ static int netapp_smdevices(int argc, char **argv, struct command *command,
639670
close(fd);
640671
}
641672

642-
if (num_smdevices)
643-
netapp_smdevices_print(smdevices, num_smdevices, fmt);
673+
if (num_smdevices) {
674+
if (fmt == NNORMAL || fmt == NCOLUMN)
675+
netapp_smdevices_print_regular(smdevices,
676+
num_smdevices, fmt);
677+
else if (fmt == NJSON)
678+
netapp_smdevices_print_json(smdevices, num_smdevices);
679+
}
644680

645681
for (i = 0; i < num; i++)
646682
free(devices[i]);

0 commit comments

Comments
 (0)