forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme.c
4700 lines (4085 loc) · 128 KB
/
nvme.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* nvme.c -- NVM-Express command line utility.
*
* Copyright (c) 2014-2015, Intel Corporation.
*
* Written by Keith Busch <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* This program uses NVMe IOCTLs to run native nvme commands to a device.
*/
#include <endian.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <inttypes.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <dirent.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "nvme-print.h"
#include "nvme-ioctl.h"
#include "nvme-lightnvm.h"
#include "plugin.h"
#include "argconfig.h"
#include "fabrics.h"
#define array_len(x) ((size_t)(sizeof(x) / sizeof(x[0])))
#define min(x, y) ((x) > (y) ? (y) : (x))
#define max(x, y) ((x) > (y) ? (x) : (y))
static struct stat nvme_stat;
const char *devicename;
static const char nvme_version_string[] = NVME_VERSION;
#define CREATE_CMD
#include "nvme-builtin.h"
static struct plugin builtin = {
.commands = commands,
.name = NULL,
.desc = NULL,
.next = NULL,
.tail = &builtin,
};
static struct program nvme = {
.name = "nvme",
.version = nvme_version_string,
.usage = "<command> [<device>] [<args>]",
.desc = "The '<device>' may be either an NVMe character "\
"device (ex: /dev/nvme0) or an nvme block device "\
"(ex: /dev/nvme0n1).",
.extensions = &builtin,
};
static unsigned long long elapsed_utime(struct timeval start_time,
struct timeval end_time)
{
unsigned long long ret = (end_time.tv_sec - start_time.tv_sec) * 1000000 +
(end_time.tv_usec - start_time.tv_usec);
return ret;
}
static int open_dev(const char *dev)
{
int err, fd;
devicename = basename(dev);
err = open(dev, O_RDONLY);
if (err < 0)
goto perror;
fd = err;
err = fstat(fd, &nvme_stat);
if (err < 0)
goto perror;
if (!S_ISCHR(nvme_stat.st_mode) && !S_ISBLK(nvme_stat.st_mode)) {
fprintf(stderr, "%s is not a block or character device\n", dev);
return -ENODEV;
}
return fd;
perror:
perror(dev);
return err;
}
static int check_arg_dev(int argc, char **argv)
{
if (optind >= argc) {
errno = EINVAL;
perror(argv[0]);
return -EINVAL;
}
return 0;
}
static int get_dev(int argc, char **argv)
{
int ret;
ret = check_arg_dev(argc, argv);
if (ret)
return ret;
return open_dev((const char *)argv[optind]);
}
int parse_and_open(int argc, char **argv, const char *desc,
const struct argconfig_commandline_options *clo, void *cfg, size_t size)
{
int ret;
ret = argconfig_parse(argc, argv, desc, clo, cfg, size);
if (ret)
return ret;
ret = get_dev(argc, argv);
if (ret < 0)
argconfig_print_help(desc, clo);
return ret;
}
static const char *output_format = "Output format: normal|json|binary";
int validate_output_format(char *format)
{
if (!format)
return -EINVAL;
if (!strcmp(format, "normal"))
return NORMAL;
if (!strcmp(format, "json"))
return JSON;
if (!strcmp(format, "binary"))
return BINARY;
return -EINVAL;
}
static int get_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
struct nvme_smart_log smart_log;
const char *desc = "Retrieve SMART log for the given device "\
"(or optionally a namespace) in either decoded format "\
"(default) or binary.";
const char *namespace = "(optional) desired namespace";
const char *raw = "output in binary format";
int err, fmt, fd;
struct config {
__u32 namespace_id;
int raw_binary;
char *output_format;
};
struct config cfg = {
.namespace_id = NVME_NSID_ALL,
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
fmt = validate_output_format(cfg.output_format);
if (fmt < 0) {
err = fmt;
goto close_fd;
}
if (cfg.raw_binary)
fmt = BINARY;
err = nvme_smart_log(fd, cfg.namespace_id, &smart_log);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)&smart_log, sizeof(smart_log));
else if (fmt == JSON)
json_smart_log(&smart_log, cfg.namespace_id, devicename);
else
show_smart_log(&smart_log, cfg.namespace_id, devicename);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
perror("smart log");
close_fd:
close(fd);
return err;
}
static int get_ana_log(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
const char *desc = "Retrieve ANA log for the given device" \
"in either decoded format "\
"(default) or binary.";
void *ana_log;
int err, fmt, fd;
int groups = 0; /* Right now get all the per ANA group NSIDS */
size_t ana_log_len;
struct nvme_id_ctrl ctrl;
struct config {
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, NULL, 0);
if (fd < 0)
return fd;
fmt = validate_output_format(cfg.output_format);
if (fmt < 0) {
err = fmt;
goto close_fd;
}
memset(&ctrl, 0, sizeof (struct nvme_id_ctrl));
err = nvme_identify_ctrl(fd, &ctrl);
if (err) {
fprintf(stderr, "ERROR : nvme_identify_ctrl() failed 0x%x\n",
err);
goto close_fd;
}
ana_log_len = sizeof(struct nvme_ana_rsp_hdr) +
le32_to_cpu(ctrl.nanagrpid) * sizeof(struct nvme_ana_group_desc);
if (!(ctrl.anacap & (1 << 6)))
ana_log_len += ctrl.mnan * sizeof(__le32);
ana_log = malloc(ana_log_len);
if (!ana_log) {
perror("malloc : ");
err = -ENOMEM;
goto close_fd;
}
err = nvme_ana_log(fd, ana_log, ana_log_len, groups ? NVME_ANA_LOG_RGO : 0);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)ana_log, ana_log_len);
else if (fmt == JSON)
json_ana_log(ana_log, devicename);
else
show_ana_log(ana_log, devicename);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(err), err);
else
perror("ana-log");
free(ana_log);
close_fd:
close(fd);
return err;
}
static int get_telemetry_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve telemetry log and write to binary file";
const char *fname = "File name to save raw binary, includes header";
const char *hgen = "Have the host tell the controller to generate the report";
const char *cgen = "Gather report generated by the controller.";
const char *dgen = "Pick which telemetry data area to report. Default is all. Valid options are 1, 2, 3.";
const size_t bs = 512;
struct nvme_telemetry_log_page_hdr *hdr;
size_t full_size, offset = bs;
int err = 0, fd, output;
void *page_log;
struct config {
char *file_name;
__u32 host_gen;
int ctrl_init;
int data_area;
};
struct config cfg = {
.file_name = NULL,
.host_gen = 1,
.ctrl_init = 0,
.data_area = 3,
};
const struct argconfig_commandline_options command_line_options[] = {
{"output-file", 'o', "FILE", CFG_STRING, &cfg.file_name, required_argument, fname},
{"host-generate", 'g', "NUM", CFG_POSITIVE, &cfg.host_gen, required_argument, hgen},
{"controller-init", 'c', "", CFG_NONE, &cfg.ctrl_init, no_argument, cgen},
{"data-area", 'd', "NUM", CFG_POSITIVE, &cfg.data_area, required_argument, dgen},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
if (!cfg.file_name) {
fprintf(stderr, "Please provide an output file!\n");
err = EINVAL;
goto close_fd;
}
output = open(cfg.file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (output < 0) {
fprintf(stderr, "Failed to open output file %s: %s!\n",
cfg.file_name, strerror(errno));
err = output;
goto close_fd;
}
cfg.host_gen = !!cfg.host_gen;
hdr = malloc(bs);
page_log = malloc(bs);
if (!hdr || !page_log) {
fprintf(stderr, "Failed to allocate %zu bytes for log: %s\n",
bs, strerror(errno));
err = ENOMEM;
goto free_mem;
}
memset(hdr, 0, bs);
err = nvme_get_telemetry_log(fd, hdr, cfg.host_gen, cfg.ctrl_init, bs, 0);
if (err) {
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
fprintf(stderr, "Failed to aquire telemetry header %d!\n", err);
close(output);
goto free_mem;
}
err = write(output, (void *) hdr, bs);
if (err != bs) {
fprintf(stderr, "Failed to flush all data to file!");
goto free_mem;
}
switch (cfg.data_area) {
case 1:
full_size = (hdr->dalb1 * bs) + offset;
break;
case 2:
full_size = (hdr->dalb2 * bs) + offset;
break;
case 3:
full_size = (hdr->dalb3 * bs) + offset;
break;
default:
fprintf(stderr, "Invalid data area requested");
err = EINVAL;
goto free_mem;
}
/*
* Continuously pull data until the offset hits the end of the last
* block.
*/
while (offset != full_size) {
err = nvme_get_telemetry_log(fd, page_log, 0, cfg.ctrl_init, bs, offset);
if (err) {
fprintf(stderr, "Failed to aquire full telemetry log!\n");
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
break;
}
err = write(output, (void *) page_log, bs);
if (err != bs) {
fprintf(stderr, "Failed to flush all data to file!");
break;
}
offset += bs;
}
free_mem:
free(hdr);
free(page_log);
close_fd:
close(fd);
return err;
}
static int get_endurance_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
struct nvme_endurance_group_log endurance_log;
const char *desc = "Retrieves endurance groups log page and prints the log.";
const char *group_id = "The endurance group identifier";
int err, fd;
int fmt;
struct config {
char *output_format;
__u16 group_id;
};
struct config cfg = {
.output_format = "normal",
.group_id = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format},
{"group-id", 'g', "NUM", CFG_SHORT, &cfg.group_id, required_argument, group_id},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
fmt = validate_output_format(cfg.output_format);
if (fmt < 0) {
err = fmt;
goto close_fd;
}
err = nvme_endurance_log(fd, cfg.group_id, &endurance_log);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)&endurance_log, sizeof(endurance_log));
else if (fmt == JSON)
json_endurance_log(&endurance_log, cfg.group_id, devicename);
else
show_endurance_log(&endurance_log, cfg.group_id, devicename);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
perror("endurance log");
close_fd:
close(fd);
return err;
}
static int get_effects_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve command effects log page and print the table.";
const char *raw_binary = "show infos in binary format";
const char *human_readable = "show infos in readable format";
struct nvme_effects_log_page effects;
int err, fd;
int fmt;
unsigned int flags = 0;
struct config {
int raw_binary;
int human_readable;
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format},
{"human-readable",'H', "", CFG_NONE, &cfg.human_readable,no_argument, human_readable},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
fmt = validate_output_format(cfg.output_format);
if (fmt < 0) {
err = fmt;
goto close_fd;
}
if (cfg.raw_binary)
fmt = BINARY;
if (cfg.human_readable)
flags |= HUMAN;
err = nvme_effects_log(fd, &effects);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)&effects, sizeof(effects));
else if (fmt == JSON)
json_effects_log(&effects, devicename);
else
show_effects_log(&effects, flags);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
perror("effects log page");
close_fd:
close(fd);
return err;
}
static int get_error_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve specified number of "\
"error log entries from a given device "\
"in either decoded format (default) or binary.";
const char *log_entries = "number of entries to retrieve";
const char *raw_binary = "dump in binary format";
struct nvme_id_ctrl ctrl;
int err, fmt, fd;
struct config {
__u32 log_entries;
int raw_binary;
char *output_format;
};
struct config cfg = {
.log_entries = 64,
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"log-entries", 'e', "NUM", CFG_POSITIVE, &cfg.log_entries, required_argument, log_entries},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
fmt = validate_output_format(cfg.output_format);
if (fmt < 0) {
err = fmt;
goto close_fd;
}
if (cfg.raw_binary)
fmt = BINARY;
if (!cfg.log_entries) {
fprintf(stderr, "non-zero log-entries is required param\n");
err = EINVAL;
goto close_fd;
}
err = nvme_identify_ctrl(fd, &ctrl);
if (err < 0)
perror("identify controller");
else if (err) {
fprintf(stderr, "could not identify controller\n");
err = ENODEV;
} else {
struct nvme_error_log_page *err_log;
cfg.log_entries = min(cfg.log_entries, ctrl.elpe + 1);
err_log = calloc(cfg.log_entries, sizeof(struct nvme_error_log_page));
if (!err_log) {
fprintf(stderr, "could not alloc buffer for error log\n");
err = ENOMEM;
goto close_fd;
}
err = nvme_error_log(fd, cfg.log_entries, err_log);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)err_log, sizeof(err_log));
else if (fmt == JSON)
json_error_log(err_log, cfg.log_entries, devicename);
else
show_error_log(err_log, cfg.log_entries, devicename);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
perror("error log");
free(err_log);
}
close_fd:
close(fd);
return err;
}
static int get_fw_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve the firmware log for the "\
"specified device in either decoded format (default) or binary.";
const char *raw_binary = "use binary output";
int err, fmt, fd;
struct nvme_firmware_log_page fw_log;
struct config {
int raw_binary;
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
fmt = validate_output_format(cfg.output_format);
if (fmt < 0) {
err = fmt;
goto close_fd;
}
if (cfg.raw_binary)
fmt = BINARY;
err = nvme_fw_log(fd, &fw_log);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)&fw_log, sizeof(fw_log));
else if (fmt == JSON)
json_fw_log(&fw_log, devicename);
else
show_fw_log(&fw_log, devicename);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
perror("fw log");
close_fd:
close(fd);
return err;
}
static int get_changed_ns_list_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
struct nvme_changed_ns_list_log changed_ns_list_log;
const char *desc = "Retrieve Changed Namespaces log for the given device "\
"in either decoded format "\
"(default) or binary.";
const char *raw = "output in binary format";
int err, fmt, fd;
struct config {
int raw_binary;
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
fmt = validate_output_format(cfg.output_format);
if (fmt < 0) {
err = fmt;
goto close_fd;
}
if (cfg.raw_binary)
fmt = BINARY;
err = nvme_changed_ns_list_log(fd, &changed_ns_list_log);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)changed_ns_list_log.log, sizeof(changed_ns_list_log.log));
else if (fmt == JSON)
json_changed_ns_list_log(&changed_ns_list_log, devicename);
else
show_changed_ns_list_log(&changed_ns_list_log, devicename);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(err), err);
else
perror("changed ns list log");
close_fd:
close(fd);
return err;
}
static int get_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve desired number of bytes "\
"from a given log on a specified device in either "\
"hex-dump (default) or binary format";
const char *namespace_id = "desired namespace";
const char *log_id = "identifier of log to retrieve";
const char *log_len = "how many bytes to retrieve";
const char *aen = "result of the aen, use to override log id";
const char *lsp = "log specific field";
const char *lpo = "log page offset specifies the location within a log page from where to start returning data";
const char *rae = "retain an asynchronous event";
const char *raw_binary = "output in raw format";
int err, fd;
struct config {
__u32 namespace_id;
__u32 log_id;
__u32 log_len;
__u32 aen;
__u64 lpo;
__u8 lsp;
int rae;
int raw_binary;
};
struct config cfg = {
.namespace_id = NVME_NSID_ALL,
.log_id = 0xffffffff,
.log_len = 0,
.lpo = NVME_NO_LOG_LPO,
.lsp = NVME_NO_LOG_LSP,
.rae = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"log-id", 'i', "NUM", CFG_POSITIVE, &cfg.log_id, required_argument, log_id},
{"log-len", 'l', "NUM", CFG_POSITIVE, &cfg.log_len, required_argument, log_len},
{"aen", 'a', "NUM", CFG_POSITIVE, &cfg.aen, required_argument, aen},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"lpo", 'o', "NUM", CFG_LONG, &cfg.lpo, required_argument, lpo},
{"lsp", 's', "NUM", CFG_BYTE, &cfg.lsp, required_argument, lsp},
{"rae", 'r', "", CFG_NONE, &cfg.rae, no_argument, rae},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
if (cfg.aen) {
cfg.log_len = 4096;
cfg.log_id = (cfg.aen >> 16) & 0xff;
}
if (cfg.log_id > 0xff) {
fprintf(stderr, "Invalid log identifier: %d. Valid range: 0-255\n", cfg.log_id);
err = EINVAL;
goto close_fd;
}
if (!cfg.log_len) {
fprintf(stderr, "non-zero log-len is required param\n");
err = EINVAL;
} else {
unsigned char *log;
log = malloc(cfg.log_len);
if (!log) {
fprintf(stderr, "could not alloc buffer for log: %s\n",
strerror(errno));
err = EINVAL;
goto close_fd;
}
err = nvme_get_log13(fd, cfg.namespace_id, cfg.log_id,
cfg.lsp, cfg.lpo, 0, cfg.rae,
cfg.log_len, log);
if (!err) {
if (!cfg.raw_binary) {
printf("Device:%s log-id:%d namespace-id:%#x\n",
devicename, cfg.log_id,
cfg.namespace_id);
d(log, cfg.log_len, 16, 1);
} else
d_raw((unsigned char *)log, cfg.log_len);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
perror("log page");
free(log);
}
close_fd:
close(fd);
return err;
}
static int sanitize_log(int argc, char **argv, struct command *command, struct plugin *plugin)
{
const char *desc = "Retrieve sanitize log and show it.";
const char *raw_binary = "show infos in binary format";
const char *human_readable = "show infos in readable format";
int fd;
int ret;
int fmt;
unsigned int flags = 0;
struct nvme_sanitize_log_page sanitize_log;
struct config {
int raw_binary;
int human_readable;
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format},
{"human-readable",'H', "", CFG_NONE, &cfg.human_readable,no_argument, human_readable},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
fmt = validate_output_format(cfg.output_format);
if (fmt < 0) {
ret = fmt;
goto close_fd;
}
if (cfg.raw_binary)
fmt = BINARY;
if (cfg.human_readable)
flags |= HUMAN;
ret = nvme_sanitize_log(fd, &sanitize_log);
if (!ret) {
if (fmt == BINARY)
d_raw((unsigned char *)&sanitize_log, sizeof(sanitize_log));
else if (fmt == JSON)
json_sanitize_log(&sanitize_log, devicename);
else
show_sanitize_log(&sanitize_log, flags, devicename);
}
else if (ret > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
else
perror("sanitize status log");
close_fd:
close(fd);
return ret;
}
static int list_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Show controller list information for the subsystem the "\
"given device is part of, or optionally controllers attached to a specific namespace.";
const char *controller = "controller to display";
const char *namespace_id = "optional namespace attached to controller";
int err, i, fd;
struct nvme_controller_list *cntlist;
struct config {
__u16 cntid;
__u32 namespace_id;
};
struct config cfg = {
.cntid = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"cntid", 'c', "NUM", CFG_SHORT, &cfg.cntid, required_argument, controller},
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
if (posix_memalign((void *)&cntlist, getpagesize(), 0x1000)) {
fprintf(stderr, "can not allocate controller list payload\n");
return ENOMEM;
}
err = nvme_identify_ctrl_list(fd, cfg.namespace_id, cfg.cntid, cntlist);
if (!err) {
__u16 num = le16_to_cpu(cntlist->num);
for (i = 0; i < (min(num, 2048)); i++)
printf("[%4u]:%#x\n", i, le16_to_cpu(cntlist->identifier[i]));
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x) cntid:%d\n",
nvme_status_to_string(err), err, cfg.cntid);
else
perror("id controller list");
free(cntlist);
close(fd);
return err;
}
static int list_ns(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "For the specified device, show the "\
"namespace list in a NVMe subsystem, optionally starting with a given namespace";
const char *namespace_id = "namespace number returned list should to start after";
const char *all = "show all namespaces in the subsystem, whether attached or inactive";
int err, i, fd;
__u32 ns_list[1024];
struct config {
__u32 namespace_id;
int all;
};
struct config cfg = {
.namespace_id = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"all", 'a', "", CFG_NONE, &cfg.all, no_argument, all},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
err = nvme_identify_ns_list(fd, cfg.namespace_id, !!cfg.all, ns_list);
if (!err) {
for (i = 0; i < 1024; i++)
if (ns_list[i])
printf("[%4u]:%#x\n", i, le32_to_cpu(ns_list[i]));
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
nvme_status_to_string(err), err, cfg.namespace_id);
else
perror("id namespace list");
close(fd);
return err;
}
static int get_nsid(int fd)