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
|
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <linux/fs.h>
#include <sys/stat.h>
#include "nvme.h"
#include "nvme-ioctl.h"
#include "nvme-print.h"
#include "nvme-status.h"
#define CREATE_CMD
#include "zns.h"
static const char *namespace_id = "Namespace identifier to use";
static int id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Send an ZNS specific Identify Controller command to "\
"the given device and report information about the specified "\
"controller in various formats.";
enum nvme_print_flags flags;
struct nvme_zns_id_ctrl ctrl;
int fd, err = -1;
struct config {
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_END()
};
fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
return errno;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
err = nvme_zns_identify_ctrl(fd, &ctrl);
if (!err)
nvme_show_zns_id_ctrl(&ctrl, flags);
else if (err > 0)
nvme_show_status(err);
else
perror("zns identify controller");
close_fd:
close(fd);
return nvme_status_to_errno(err, false);
}
static int id_ns(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Send an ZNS specific Identify Namespace command to "\
"the given device and report information about the specified "\
"namespace in varios formats.";
const char *vendor_specific = "dump binary vendor fields";
const char *human_readable = "show identify in readable format";
enum nvme_print_flags flags;
struct nvme_zns_id_ns ns;
struct nvme_id_ns id_ns;
int fd, err = -1;
struct config {
char *output_format;
__u32 namespace_id;
int human_readable;
int vendor_specific;
};
struct config cfg = {
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_FLAG("vendor-specific", 'v', &cfg.vendor_specific, vendor_specific),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable),
OPT_END()
};
fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
return errno;
flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.vendor_specific)
flags |= VS;
if (cfg.human_readable)
flags |= VERBOSE;
if (!cfg.namespace_id) {
err = cfg.namespace_id = nvme_get_nsid(fd);
if (err < 0) {
perror("get-namespace-id");
goto close_fd;
}
}
err = nvme_identify_ns(fd, cfg.namespace_id, false, &id_ns);
if (err) {
nvme_show_status(err);
goto close_fd;
}
err = nvme_zns_identify_ns(fd, cfg.namespace_id, &ns);
if (!err)
nvme_show_zns_id_ns(&ns, &id_ns, flags);
else if (err > 0)
nvme_show_status(err);
else
perror("zns identify namespace");
close_fd:
close(fd);
return nvme_status_to_errno(err, false);
}
static int __zns_mgmt_send(int fd, __u32 namespace_id, __u64 zslba,
bool select_all, __u32 timeout, enum nvme_zns_send_action zsa,
__u32 data_len, void *buf)
{
int err;
err = nvme_zns_mgmt_send(fd, namespace_id, zslba, select_all, timeout, zsa,
data_len, buf);
close(fd);
return err;
}
static int zns_mgmt_send(int argc, char **argv, struct command *cmd, struct plugin *plugin,
const char *desc, enum nvme_zns_send_action zsa)
{
const char *zslba = "starting LBA of the zone for this command";
const char *select_all = "send command to all zones";
const char *timeout = "timeout value, in milliseconds";
int err, fd;
char *command;
struct config {
__u64 zslba;
__u32 namespace_id;
bool select_all;
__u32 timeout;
};
struct config cfg = {};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_SUFFIX("start-lba", 's', &cfg.zslba, zslba),
OPT_FLAG("select-all", 'a', &cfg.select_all, select_all),
OPT_UINT("timeout", 't', &cfg.timeout, timeout),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
return errno;
err = asprintf(&command, "%s-%s", plugin->name, cmd->name);
if (err < 0)
goto close_fd;
if (!cfg.namespace_id) {
err = cfg.namespace_id = nvme_get_nsid(fd);
if (err < 0) {
perror("get-namespace-id");
goto free;
}
}
err = __zns_mgmt_send(fd, cfg.namespace_id, cfg.zslba,
cfg.select_all, cfg.timeout, zsa, 0, NULL);
if (!err)
printf("%s: Success, action:%d zone:%"PRIx64" all:%d nsid:%d\n",
command, zsa, (uint64_t)cfg.zslba, (int)cfg.select_all,
cfg.namespace_id);
else if (err > 0)
nvme_show_status(err);
else
perror(desc);
free:
free(command);
close_fd:
close(fd);
return nvme_status_to_errno(err, false);
}
static int get_zdes_bytes(int fd, __u32 nsid)
{
struct nvme_zns_id_ns ns;
struct nvme_id_ns id_ns;
__u8 lbaf;
int err;
err = nvme_identify_ns(fd, nsid, false, &id_ns);
if (err > 0) {
nvme_show_status(err);
return -1;
} else if (err < 0) {
perror("identify namespace");
return -1;
}
err = nvme_zns_identify_ns(fd, nsid, &ns);
if (err > 0) {
nvme_show_status(err);
return -1;
} else if (err < 0) {
perror("zns identify namespace");
return -1;
}
lbaf = id_ns.flbas & NVME_NS_FLBAS_LBA_MASK;
return ns.lbafe[lbaf].zdes << 6;
}
static int zone_mgmt_send(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Zone Management Send";
const char *zslba = "starting LBA of the zone for this command";
const char *select_all = "send command to all zones";
const char *zsa = "zone send action";
const char *data_len = "buffer length if data required";
const char *data = "optional file for data (default stdin)";
const char *timeout = "timeout value, in milliseconds";
int fd, ffd = STDIN_FILENO, err = -1;
void *buf = NULL;
struct config {
__u64 zslba;
__u32 namespace_id;
bool select_all;
__u8 zsa;
int data_len;
char *file;
__u32 timeout;
};
struct config cfg = {};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_SUFFIX("start-lba", 's', &cfg.zslba, zslba),
OPT_FLAG("select-all", 'a', &cfg.select_all, select_all),
OPT_BYTE("zsa", 'z', &cfg.zsa, zsa),
OPT_UINT("data-len", 'l', &cfg.data_len, data_len),
OPT_FILE("data", 'd', &cfg.file, data),
OPT_UINT("timeout", 't', &cfg.timeout, timeout),
OPT_END()
};
fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
return errno;
if (!cfg.namespace_id) {
err = cfg.namespace_id = nvme_get_nsid(fd);
if (err < 0) {
perror("get-namespace-id");
goto close_fd;
}
}
if (!cfg.zsa) {
fprintf(stderr, "zone send action must be specified\n");
err = -EINVAL;
goto close_fd;
}
if (cfg.zsa == NVME_ZNS_ZSA_SET_DESC_EXT) {
if(!cfg.data_len) {
cfg.data_len = get_zdes_bytes(fd, cfg.namespace_id);
if (!cfg.data_len || cfg.data_len < 0) {
fprintf(stderr,
"Zone Descriptor Extensions are not supported\n");
goto close_fd;
} else if (cfg.data_len < 0) {
err = cfg.data_len;
goto close_fd;
}
}
if (posix_memalign(&buf, getpagesize(), cfg.data_len)) {
fprintf(stderr, "can not allocate feature payload\n");
goto close_fd;
}
memset(buf, 0, cfg.data_len);
if (cfg.file) {
ffd = open(cfg.file, O_RDONLY);
if (ffd < 0) {
perror(cfg.file);
goto free;
}
}
err = read(ffd, (void *)buf, cfg.data_len);
if (err < 0) {
perror("read");
goto close_ffd;
}
} else {
if (cfg.file || cfg.data_len) {
fprintf(stderr,
"data, data_len only valid with set extended descriptor\n");
err = -EINVAL;
goto close_fd;
}
}
err = __zns_mgmt_send(fd, cfg.namespace_id, cfg.zslba, cfg.select_all,
cfg.timeout, cfg.zsa, cfg.data_len, buf);
if (!err)
printf("zone-mgmt-send: Success, action:%d zone:%"PRIx64" "
"all:%d nsid:%d\n",
cfg.zsa, (uint64_t)cfg.zslba, (int)cfg.select_all,
cfg.namespace_id);
else if (err > 0)
nvme_show_status(err);
else
perror("zns zone-mgmt-send");
close_ffd:
if (cfg.file)
close(ffd);
free:
free(buf);
close_fd:
close(fd);
return nvme_status_to_errno(err, false);
}
static int close_zone(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Close zones\n";
return zns_mgmt_send(argc, argv, cmd, plugin, desc, NVME_ZNS_ZSA_CLOSE);
}
static int finish_zone(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Finish zones\n";
return zns_mgmt_send(argc, argv, cmd, plugin, desc, NVME_ZNS_ZSA_FINISH);
}
static int open_zone(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Open zones\n";
return zns_mgmt_send(argc, argv, cmd, plugin, desc, NVME_ZNS_ZSA_OPEN);
}
static int reset_zone(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Reset zones\n";
return zns_mgmt_send(argc, argv, cmd, plugin, desc, NVME_ZNS_ZSA_RESET);
}
static int offline_zone(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Offline zones\n";
return zns_mgmt_send(argc, argv, cmd, plugin, desc, NVME_ZNS_ZSA_OFFLINE);
}
static int set_zone_desc(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Set Zone Descriptor Extension\n";
const char *zslba = "starting LBA of the zone for this command";
const char *data = "optional file for zone extention data (default stdin)";
const char *timeout = "timeout value, in milliseconds";
int fd, ffd = STDIN_FILENO, err;
void *buf = NULL;
__u32 data_len;
struct config {
__u64 zslba;
__u32 namespace_id;
char *file;
__u32 timeout;
};
struct config cfg = {};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_SUFFIX("start-lba", 's', &cfg.zslba, zslba),
OPT_FILE("data", 'd', &cfg.file, data),
OPT_UINT("timeout", 't', &cfg.timeout, timeout),
OPT_END()
};
fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
return errno;
if (!cfg.namespace_id) {
err = cfg.namespace_id = nvme_get_nsid(fd);
if (err < 0) {
perror("get-namespace-id");
goto close_fd;
}
}
data_len = get_zdes_bytes(fd, cfg.namespace_id);
if (!data_len || data_len < 0) {
fprintf(stderr,
"zone format does not provide descriptor extention\n");
errno = EINVAL;
err = -1;
goto close_fd;
}
buf = calloc(1, data_len);
if (!buf) {
perror("could not alloc memory for zone desc");
err = -ENOMEM;
goto close_fd;
}
if (cfg.file) {
ffd = open(cfg.file, O_RDONLY);
if (ffd < 0) {
perror(cfg.file);
err = -1;
goto free;
}
}
err = read(ffd, (void *)buf, data_len);
if (err < 0) {
perror("read");
goto close_ffd;
}
err = __zns_mgmt_send(fd, cfg.namespace_id, cfg.zslba, 0, cfg.timeout,
NVME_ZNS_ZSA_SET_DESC_EXT, data_len, buf);
if (!err)
printf("set-zone-desc: Success, zone:%"PRIx64" nsid:%d\n",
(uint64_t)cfg.zslba, cfg.namespace_id);
else if (err > 0)
nvme_show_status(err);
else
perror("zns set-zone-desc");
close_ffd:
if (cfg.file)
close(ffd);
free:
free(buf);
close_fd:
close(fd);
return nvme_status_to_errno(err, false);
}
static int zone_mgmt_recv(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Zone Management Receive";
const char *zslba = "starting LBA of the zone";
const char *zra = "Zone Receive Action";
const char *zrasf = "Zone Receive Action Specific Field(Reporting Options)";
const char *partial = "Zone Receive Action Specific Features(Partial Report)";
const char *data_len = "length of data in bytes";
enum nvme_print_flags flags;
int fd, err = -1;
void *data = NULL;
struct config {
char *output_format;
__u64 zslba;
__u32 namespace_id;
__u8 zra;
__u8 zrasf;
bool partial;
__u32 data_len;
};
struct config cfg = {
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_SUFFIX("start-lba", 's', &cfg.zslba, zslba),
OPT_BYTE("zra", 'z', &cfg.zra, zra),
OPT_BYTE("zrasf", 'S', &cfg.zrasf, zrasf),
OPT_FLAG("partial", 'p', &cfg.partial, partial),
OPT_UINT("data-len", 'l', &cfg.data_len, data_len),
OPT_END()
};
fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
return errno;
flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (!cfg.namespace_id) {
err = cfg.namespace_id = nvme_get_nsid(fd);
if (err < 0) {
perror("get-namespace-id");
goto close_fd;
}
}
if (cfg.zra == NVME_ZNS_ZRA_REPORT_ZONES && !cfg.data_len) {
fprintf(stderr, "error: data len is needed for NVME_ZRA_ZONE_REPORT\n");
err = -EINVAL;
goto close_fd;
}
if (cfg.data_len) {
data = calloc(1, cfg.data_len);
if (!data) {
perror("could not alloc memory for zone mgmt receive data");
err = -ENOMEM;
goto close_fd;
}
}
err = nvme_zns_mgmt_recv(fd, cfg.namespace_id, cfg.zslba, cfg.zra,
cfg.zrasf, cfg.partial, cfg.data_len, data);
if (!err)
printf("zone-mgmt-recv: Success, action:%d zone:%"PRIx64" nsid:%d\n",
cfg.zra, (uint64_t)cfg.zslba, cfg.namespace_id);
else if (err > 0)
nvme_show_status(err);
else
perror("zns zone-mgmt-recv");
free(data);
close_fd:
close(fd);
return nvme_status_to_errno(err, false);
}
static int report_zones(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve the Report Zones data structure";
const char *zslba = "starting LBA of the zone";
const char *num_descs = "number of descriptors to retrieve (default: all of them)";
const char *state = "state of zones to list";
const char *ext = "set to use the extended report zones";
const char *part = "set to use the partial report";
const char *verbose = "show report zones verbosity";
enum nvme_print_flags flags;
int fd, zdes = 0, err = -1;
__u32 report_size;
void *report;
bool huge = false;
struct nvme_zone_report *buff;
unsigned int nr_zones_chunks = 1024, /* 1024 entries * 64 bytes per entry = 64k byte transfer */
nr_zones_retrieved = 0,
nr_zones,
offset,
log_len;
int total_nr_zones = 0;
struct nvme_zns_id_ns id_zns;
struct nvme_id_ns id_ns;
uint8_t lbaf;
__le64 zsze;
struct config {
char *output_format;
__u64 zslba;
__u32 namespace_id;
int num_descs;
int state;
int verbose;
bool extended;
bool partial;
};
struct config cfg = {
.output_format = "normal",
.num_descs = -1,
};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_SUFFIX("start-lba", 's', &cfg.zslba, zslba),
OPT_UINT("descs", 'd', &cfg.num_descs, num_descs),
OPT_UINT("state", 'S', &cfg.state, state),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("verbose", 'v', &cfg.verbose, verbose),
OPT_FLAG("extended", 'e', &cfg.extended, ext),
OPT_FLAG("partial", 'p', &cfg.partial, part),
OPT_END()
};
fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
return errno;
flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.verbose)
flags |= VERBOSE;
if (!cfg.namespace_id) {
err = cfg.namespace_id = nvme_get_nsid(fd);
if (err < 0) {
perror("get-namespace-id");
goto close_fd;
}
}
if (cfg.extended) {
zdes = get_zdes_bytes(fd, cfg.namespace_id);
if (zdes < 0) {
err = zdes;
goto close_fd;
}
}
err = nvme_identify_ns(fd, cfg.namespace_id, false, &id_ns);
if (err) {
nvme_show_status(err);
goto close_fd;
}
err = nvme_zns_identify_ns(fd, cfg.namespace_id, &id_zns);
if (!err) {
/* get zsze field from zns id ns data - needed for offset calculation */
lbaf = id_ns.flbas & NVME_NS_FLBAS_LBA_MASK;
zsze = le64_to_cpu(id_zns.lbafe[lbaf].zsze);
}
else {
nvme_show_status(err);
goto close_fd;
}
log_len = sizeof(struct nvme_zone_report);
buff = calloc(1, log_len);
if (!buff) {
err = -ENOMEM;
goto close_fd;
}
err = nvme_zns_report_zones(fd, cfg.namespace_id, 0,
0, cfg.state, 0, log_len, buff);
if (err > 0) {
nvme_show_status(err);
goto free_buff;
}
else if (err < 0) {
perror("zns report-zones");
goto free_buff;
}
total_nr_zones = le64_to_cpu(buff->nr_zones);
if (cfg.num_descs == -1) {
cfg.num_descs = total_nr_zones;
}
nr_zones = cfg.num_descs;
if (nr_zones < nr_zones_chunks)
nr_zones_chunks = nr_zones;
log_len = sizeof(struct nvme_zone_report) + ((sizeof(struct nvme_zns_desc) * nr_zones_chunks) + (nr_zones_chunks * zdes));
report_size = log_len;
report = nvme_alloc(report_size, &huge);
if (!report) {
perror("alloc");
err = -ENOMEM;
goto close_fd;
}
offset = cfg.zslba;
printf("nr_zones: %"PRIu64"\n", (uint64_t)le64_to_cpu(total_nr_zones));
while (nr_zones_retrieved < nr_zones) {
if (nr_zones_retrieved >= nr_zones)
break;
if (nr_zones_retrieved + nr_zones_chunks > nr_zones) {
nr_zones_chunks = nr_zones - nr_zones_retrieved;
log_len = sizeof(struct nvme_zone_report) + ((sizeof(struct nvme_zns_desc) * nr_zones_chunks) + (nr_zones_chunks * zdes));
}
err = nvme_zns_report_zones(fd, cfg.namespace_id, offset,
cfg.extended, cfg.state, cfg.partial, log_len, report);
if (err > 0) {
nvme_show_status(err);
break;
}
if (!err)
nvme_show_zns_report_zones(report, nr_zones_chunks, zdes,
log_len, flags);
nr_zones_retrieved += nr_zones_chunks;
offset = (nr_zones_retrieved * zsze);
}
nvme_free(report, huge);
free_buff:
free(buff);
close_fd:
close(fd);
return nvme_status_to_errno(err, false);
}
static int zone_append(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "The zone append command is used to write to a zone "\
"using the slba of the zone, and the write will be appended from the "\
"write pointer of the zone";
const char *zslba = "starting LBA of the zone";
const char *data = "file containing data to write";
const char *metadata = "file with metadata to be written";
const char *limited_retry = "limit media access attempts";
const char *fua = "force unit access";
const char *prinfo = "protection information action and checks field";
const char *piremap = "protection information remap (for type 1 PI)";
const char *ref_tag = "reference tag (for end to end PI)";
const char *lbat = "logical block application tag (for end to end PI)";
const char *lbatm = "logical block application tag mask (for end to end PI)";
const char *metadata_size = "size of metadata in bytes";
const char *data_size = "size of data in bytes";
const char *latency = "output latency statistics";
int err = -1, fd, dfd = STDIN_FILENO, mfd = STDIN_FILENO;
unsigned int lba_size, meta_size;
void *buf = NULL, *mbuf = NULL;
__u16 nblocks, control = 0;
__u64 result;
struct timeval start_time, end_time;
struct nvme_id_ns ns;
struct config {
char *data;
char *metadata;
__u64 zslba;
__u64 data_size;
__u64 metadata_size;
int limited_retry;
int fua;
__u32 namespace_id;
__u32 ref_tag;
__u16 lbat;
__u16 lbatm;
__u8 prinfo;
int piremap;
int latency;
};
struct config cfg = {};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_SUFFIX("zslba", 's', &cfg.zslba, zslba),
OPT_SUFFIX("data-size", 'z', &cfg.data_size, data_size),
OPT_SUFFIX("metadata-size", 'y', &cfg.metadata_size, metadata_size),
OPT_FILE("data", 'd', &cfg.data, data),
OPT_FILE("metadata", 'M', &cfg.metadata, metadata),
OPT_FLAG("limited-retry", 'l', &cfg.limited_retry, limited_retry),
OPT_FLAG("force-unit-access", 'f', &cfg.fua, fua),
OPT_UINT("ref-tag", 'r', &cfg.ref_tag, ref_tag),
OPT_SHRT("app-tag-mask", 'm', &cfg.lbatm, lbatm),
OPT_SHRT("app-tag", 'a', &cfg.lbat, lbat),
OPT_BYTE("prinfo", 'p', &cfg.prinfo, prinfo),
OPT_FLAG("piremap", 'P', &cfg.piremap, piremap),
OPT_FLAG("latency", 't', &cfg.latency, latency),
OPT_END()
};
fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
return errno;
if (!cfg.data_size) {
fprintf(stderr, "Append size not provided\n");
errno = EINVAL;
goto close_fd;
}
if (!cfg.namespace_id) {
err = cfg.namespace_id = nvme_get_nsid(fd);
if (err < 0) {
perror("get-namespace-id");
goto close_fd;
}
}
err = nvme_identify_ns(fd, cfg.namespace_id, false, &ns);
if (err) {
nvme_show_status(err);
goto close_fd;
}
lba_size = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds;
if (cfg.data_size & (lba_size - 1)) {
fprintf(stderr,
"Data size:%#"PRIx64" not aligned to lba size:%#x\n",
(uint64_t)cfg.data_size, lba_size);
errno = EINVAL;
goto close_ns;
}
meta_size = ns.lbaf[(ns.flbas & 0x0f)].ms;
if (meta_size && !(meta_size == 8 && (cfg.prinfo & 0x8)) &&
(!cfg.metadata_size || cfg.metadata_size % meta_size)) {
fprintf(stderr,
"Metadata size:%#"PRIx64" not aligned to metadata size:%#x\n",
(uint64_t)cfg.metadata_size, meta_size);
errno = EINVAL;
goto close_ns;
}
if (cfg.prinfo > 0xf) {
fprintf(stderr, "Invalid value for prinfo:%#x\n", cfg.prinfo);
errno = EINVAL;
goto close_ns;
}
if (cfg.data) {
dfd = open(cfg.data, O_RDONLY);
if (dfd < 0) {
perror(cfg.data);
goto close_ns;
}
}
if (posix_memalign(&buf, getpagesize(), cfg.data_size)) {
fprintf(stderr, "No memory for data size:%"PRIx64"\n",
(uint64_t)cfg.data_size);
goto close_dfd;
}
memset(buf, 0, cfg.data_size);
err = read(dfd, buf, cfg.data_size);
if (err < 0) {
perror("read-data");
goto free_data;
}
if (cfg.metadata) {
mfd = open(cfg.metadata, O_RDONLY);
if (mfd < 0) {
perror(cfg.metadata);
err = -1;
goto close_dfd;
}
}
if (cfg.metadata_size) {
if (posix_memalign(&mbuf, getpagesize(), meta_size)) {
fprintf(stderr, "No memory for metadata size:%d\n",
meta_size);
err = -1;
goto close_mfd;
}
memset(mbuf, 0, cfg.metadata_size);
err = read(mfd, mbuf, cfg.metadata_size);
if (err < 0) {
perror("read-metadata");
goto free_meta;
}
}
nblocks = (cfg.data_size / lba_size) - 1;
control |= (cfg.prinfo << 10);
if (cfg.limited_retry)
control |= NVME_RW_LR;
if (cfg.fua)
control |= NVME_RW_FUA;
if (cfg.piremap)
control |= NVME_RW_PIREMAP;
gettimeofday(&start_time, NULL);
err = nvme_zns_append(fd, cfg.namespace_id, cfg.zslba, nblocks,
control, cfg.ref_tag, cfg.lbat, cfg.lbatm,
cfg.data_size, buf, cfg.metadata_size, mbuf,
&result);
gettimeofday(&end_time, NULL);
if (cfg.latency)
printf(" latency: zone append: %llu us\n",
elapsed_utime(start_time, end_time));
if (!err)
printf("Success appended data to LBA %"PRIx64"\n", (uint64_t)result);
else if (err > 0)
nvme_show_status(err);
else
perror("zns zone-append");
free_meta:
free(mbuf);
close_mfd:
if (cfg.metadata)
close(mfd);
free_data:
free(buf);
close_dfd:
if (cfg.data)
close(dfd);
close_ns:
close_fd:
close(fd);
return nvme_status_to_errno(err, false);
}
static int changed_zone_list(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve Changed Zone log for the given device";
const char *rae = "retain an asynchronous event";
struct nvme_zns_changed_zone_log log;
enum nvme_print_flags flags;
int fd, err = -1;
struct config {
char *output_format;
__u32 namespace_id;
bool rae;
};
struct config cfg = {
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("rae", 'r', &cfg.rae, rae),
OPT_END()
};
fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
return errno;
flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (!cfg.namespace_id) {
err = cfg.namespace_id = nvme_get_nsid(fd);
if (err < 0) {
perror("get-namespace-id");
goto close_fd;
}
}
err = nvme_get_log(fd, cfg.namespace_id, NVME_LOG_ZONE_CHANGED_LIST,
cfg.rae, NVME_NO_LOG_LSP, sizeof(log), &log);
if (!err)
nvme_show_zns_changed(&log, flags);
else if (err > 0)
nvme_show_status(err);
else
perror("zns changed-zone-list");
close_fd:
close(fd);
return nvme_status_to_errno(err, false);
}
|