Skip to content

Commit e05b4d2

Browse files
martin-gpyigaw
authored andcommitted
netapp-smdevices: print single device output too
In addition to printing info of all smdevices on the host, provide an option to print the output of the specified device alone. Signed-off-by: Martin George <marting@netapp.com> Tested-by: Clayton Skaggs <claytons@netapp.com>
1 parent e2025d6 commit e05b4d2

File tree

1 file changed

+85
-9
lines changed

1 file changed

+85
-9
lines changed

plugins/netapp/netapp-nvme.c

+85-9
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static void netapp_ontapdevice_json(struct json_object *devices, char *devname,
252252
}
253253

254254
static void netapp_smdevices_print_regular(struct smdevice_info *devices,
255-
int count, int format)
255+
int count, int format, const char *devname)
256256
{
257257
int i, slta;
258258
char array_label[ARRAY_LABEL_LEN / 2 + 1];
@@ -281,6 +281,36 @@ static void netapp_smdevices_print_regular(struct smdevice_info *devices,
281281
}
282282

283283
for (i = 0; i < count; i++) {
284+
if (devname && !strcmp(devname, basename(devices[i].dev))) {
285+
/* found the device, fetch info for that alone */
286+
nvme_id_ns_flbas_to_lbaf_inuse(devices[i].ns.flbas,
287+
&lba_index);
288+
unsigned long long lba = 1ULL <<
289+
devices[i].ns.lbaf[lba_index].ds;
290+
double nsze = le64_to_cpu(devices[i].ns.nsze) * lba;
291+
const char *s_suffix = suffix_si_get(&nsze);
292+
char size[128];
293+
294+
sprintf(size, "%.2f%sB", nsze, s_suffix);
295+
netapp_convert_string(array_label,
296+
(char *)&devices[i].ctrl.vs[20],
297+
ARRAY_LABEL_LEN / 2);
298+
slta = devices[i].ctrl.vs[0] & 0x1;
299+
netapp_convert_string(volume_label,
300+
(char *)devices[i].ns.vs,
301+
VOLUME_LABEL_LEN / 2);
302+
netapp_nguid_to_str(nguid_str, devices[i].ns.nguid);
303+
304+
printf(formatstr, devices[i].dev, array_label,
305+
volume_label, devices[i].nsid,
306+
nguid_str,
307+
slta ? 'A' : 'B', "unknown", size);
308+
return;
309+
}
310+
}
311+
312+
for (i = 0; i < count; i++) {
313+
/* fetch info for all devices */
284314
nvme_id_ns_flbas_to_lbaf_inuse(devices[i].ns.flbas, &lba_index);
285315
unsigned long long lba = 1ULL <<
286316
devices[i].ns.lbaf[lba_index].ds;
@@ -294,17 +324,17 @@ static void netapp_smdevices_print_regular(struct smdevice_info *devices,
294324
ARRAY_LABEL_LEN / 2);
295325
slta = devices[i].ctrl.vs[0] & 0x1;
296326
netapp_convert_string(volume_label, (char *)devices[i].ns.vs,
297-
VOLUME_LABEL_LEN / 2);
327+
VOLUME_LABEL_LEN / 2);
298328
netapp_nguid_to_str(nguid_str, devices[i].ns.nguid);
329+
299330
printf(formatstr, devices[i].dev, array_label,
300331
volume_label, devices[i].nsid, nguid_str,
301332
slta ? 'A' : 'B', "unknown", size);
302-
}
303333
}
304334
}
305335

306336
static void netapp_smdevices_print_json(struct smdevice_info *devices,
307-
int count)
337+
int count, const char *devname)
308338
{
309339
struct json_object *root = NULL;
310340
struct json_object *json_devices = NULL;
@@ -319,6 +349,36 @@ static void netapp_smdevices_print_json(struct smdevice_info *devices,
319349
json_devices = json_create_array();
320350

321351
for (i = 0; i < count; i++) {
352+
if (devname && !strcmp(devname, basename(devices[i].dev))) {
353+
/* found the device, fetch info for that alone */
354+
nvme_id_ns_flbas_to_lbaf_inuse(devices[i].ns.flbas,
355+
&lba_index);
356+
unsigned long long lba = 1ULL <<
357+
devices[i].ns.lbaf[lba_index].ds;
358+
double nsze = le64_to_cpu(devices[i].ns.nsze) * lba;
359+
const char *s_suffix = suffix_si_get(&nsze);
360+
char size[128];
361+
362+
sprintf(size, "%.2f%sB", nsze, s_suffix);
363+
netapp_convert_string(array_label,
364+
(char *)&devices[i].ctrl.vs[20],
365+
ARRAY_LABEL_LEN / 2);
366+
slta = devices[i].ctrl.vs[0] & 0x1;
367+
netapp_convert_string(volume_label,
368+
(char *)devices[i].ns.vs,
369+
VOLUME_LABEL_LEN / 2);
370+
netapp_nguid_to_str(nguid_str, devices[i].ns.nguid);
371+
netapp_smdevice_json(json_devices, devices[i].dev,
372+
array_label, volume_label,
373+
devices[i].nsid, nguid_str,
374+
slta ? "A" : "B", "unknown", size, lba,
375+
le64_to_cpu(devices[i].ns.nsze));
376+
goto out;
377+
}
378+
}
379+
380+
for (i = 0; i < count; i++) {
381+
/* fetch info for all devices */
322382
nvme_id_ns_flbas_to_lbaf_inuse(devices[i].ns.flbas, &lba_index);
323383
unsigned long long lba = 1ULL <<
324384
devices[i].ns.lbaf[lba_index].ds;
@@ -331,15 +391,17 @@ static void netapp_smdevices_print_json(struct smdevice_info *devices,
331391
(char *)&devices[i].ctrl.vs[20],
332392
ARRAY_LABEL_LEN / 2);
333393
slta = devices[i].ctrl.vs[0] & 0x1;
334-
netapp_convert_string(volume_label, (char *)devices[i].ns.vs,
394+
netapp_convert_string(volume_label,
395+
(char *)devices[i].ns.vs,
335396
VOLUME_LABEL_LEN / 2);
336397
netapp_nguid_to_str(nguid_str, devices[i].ns.nguid);
337398
netapp_smdevice_json(json_devices, devices[i].dev,
338399
array_label, volume_label, devices[i].nsid,
339-
nguid_str, slta ? "A" : "B", "unknown", size,
340-
lba, le64_to_cpu(devices[i].ns.nsze));
400+
nguid_str, slta ? "A" : "B", "unknown",
401+
size, lba, le64_to_cpu(devices[i].ns.nsze));
341402
}
342403

404+
out:
343405
/* complete the json output */
344406
json_object_add_value_array(root, "SMdevices", json_devices);
345407
json_print_object(root, NULL);
@@ -618,6 +680,7 @@ static int netapp_smdevices(int argc, char **argv, struct command *command,
618680
int num, i, fd, ret, fmt;
619681
struct smdevice_info *smdevices;
620682
char path[264];
683+
char *devname = NULL;
621684
int num_smdevices = 0;
622685

623686
struct config {
@@ -649,6 +712,18 @@ static int netapp_smdevices(int argc, char **argv, struct command *command,
649712
return num;
650713
}
651714

715+
if (optind < argc)
716+
devname = basename(argv[optind++]);
717+
718+
if (devname) {
719+
int subsys_num, nsid;
720+
721+
if (sscanf(devname, "nvme%dn%d", &subsys_num, &nsid) != 2) {
722+
fprintf(stderr, "Invalid device name %s\n", devname);
723+
return -EINVAL;
724+
}
725+
}
726+
652727
smdevices = calloc(num, sizeof(*smdevices));
653728
if (!smdevices) {
654729
fprintf(stderr, "Unable to allocate memory for devices.\n");
@@ -673,9 +748,10 @@ static int netapp_smdevices(int argc, char **argv, struct command *command,
673748
if (num_smdevices) {
674749
if (fmt == NNORMAL || fmt == NCOLUMN)
675750
netapp_smdevices_print_regular(smdevices,
676-
num_smdevices, fmt);
751+
num_smdevices, fmt, devname);
677752
else if (fmt == NJSON)
678-
netapp_smdevices_print_json(smdevices, num_smdevices);
753+
netapp_smdevices_print_json(smdevices,
754+
num_smdevices, devname);
679755
}
680756

681757
for (i = 0; i < num; i++)

0 commit comments

Comments
 (0)