forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.c
More file actions
3021 lines (2514 loc) · 68.4 KB
/
tree.c
File metadata and controls
3021 lines (2514 loc) · 68.4 KB
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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* This file is part of libnvme.
* Copyright (c) 2020 Western Digital Corporation or its affiliates.
*
* Authors: Keith Busch <[email protected]>
* Chaitanya Kulkarni <[email protected]>
*/
#include <dirent.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <libgen.h>
#include <unistd.h>
#include <ifaddrs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ccan/endian/endian.h>
#include <ccan/list/list.h>
#include "cleanup.h"
#include "ioctl.h"
#include "linux.h"
#include "filters.h"
#include "tree.h"
#include "filters.h"
#include "util.h"
#include "fabrics.h"
#include "log.h"
#include "private.h"
/**
* struct candidate_args - Used to look for a controller matching these parameters
* @transport: Transport type: loop, fc, rdma, tcp
* @traddr: Transport address (destination address)
* @trsvcid: Transport service ID
* @subsysnqn: Subsystem NQN
* @host_traddr: Host transport address (source address)
* @host_iface: Host interface for connection (tcp only)
* @iface_list: Interface list (tcp only)
* @addreq: Address comparison function (for traddr, host-traddr)
* @well_known_nqn: Set to "true" when @subsysnqn is the well-known NQN
*/
struct candidate_args {
const char *transport;
const char *traddr;
const char *trsvcid;
const char *subsysnqn;
const char *host_traddr;
const char *host_iface;
const struct ifaddrs *iface_list;
bool (*addreq)(const char *, const char *);
bool well_known_nqn;
};
typedef bool (*ctrl_match_t)(struct nvme_ctrl *c, struct candidate_args *candidate);
static void __nvme_free_host(nvme_host_t h);
static void __nvme_free_ctrl(nvme_ctrl_t c);
static int nvme_subsystem_scan_namespace(struct nvme_global_ctx *ctx,
struct nvme_subsystem *s, char *name);
static int nvme_init_subsystem(nvme_subsystem_t s, const char *name);
static int nvme_scan_subsystem(struct nvme_global_ctx *ctx, const char *name);
static int nvme_ctrl_scan_namespace(struct nvme_global_ctx *ctx, struct nvme_ctrl *c,
char *name);
static int nvme_ctrl_scan_path(struct nvme_global_ctx *ctx, struct nvme_ctrl *c, char *name);
/**
* Compare two C strings and handle NULL pointers gracefully.
* Return true if both pointers are equal (including both set to NULL).
* Return false if one and only one of the two pointers is NULL.
* Perform string comparisong only if both pointers are not NULL and
* return true if both strings are the same, false otherwise.
*/
static bool streq0(const char *s1, const char *s2)
{
if (s1 == s2)
return true;
if (!s1 || !s2)
return false;
return !strcmp(s1, s2);
}
/**
* Same as streq0() but ignore the case of the characters.
*/
static bool streqcase0(const char *s1, const char *s2)
{
if (s1 == s2)
return true;
if (!s1 || !s2)
return false;
return !strcasecmp(s1, s2);
}
struct dirents {
struct dirent **ents;
int num;
};
static void cleanup_dirents(struct dirents *ents)
{
while (ents->num > 0)
free(ents->ents[--ents->num]);
free(ents->ents);
}
#define _cleanup_dirents_ __cleanup__(cleanup_dirents)
static char *nvme_hostid_from_hostnqn(const char *hostnqn)
{
const char *uuid;
uuid = strstr(hostnqn, "uuid:");
if (!uuid)
return NULL;
return strdup(uuid + strlen("uuid:"));
}
int nvme_host_get_ids(struct nvme_global_ctx *ctx,
const char *hostnqn_arg, const char *hostid_arg,
char **hostnqn, char **hostid)
{
_cleanup_free_ char *nqn = NULL;
_cleanup_free_ char *hid = NULL;
_cleanup_free_ char *hnqn = NULL;
nvme_host_t h;
/* command line argumments */
if (hostid_arg)
hid = strdup(hostid_arg);
if (hostnqn_arg)
hnqn = strdup(hostnqn_arg);
/* JSON config: assume the first entry is the default host */
h = nvme_first_host(ctx);
if (h) {
if (!hid)
hid = xstrdup(nvme_host_get_hostid(h));
if (!hnqn)
hnqn = xstrdup(nvme_host_get_hostnqn(h));
}
/* /etc/nvme/hostid and/or /etc/nvme/hostnqn */
if (!hid)
hid = nvmf_hostid_from_file();
if (!hnqn)
hnqn = nvmf_hostnqn_from_file();
/* incomplete configuration, thus derive hostid from hostnqn */
if (!hid && hnqn)
hid = nvme_hostid_from_hostnqn(hnqn);
/*
* fallback to use either DMI information or device-tree. If all
* fails generate one
*/
if (!hid) {
hid = nvmf_hostid_generate();
if (!hid)
return -ENOMEM;
nvme_msg(ctx, LOG_DEBUG,
"warning: using auto generated hostid and hostnqn\n");
}
/* incomplete configuration, thus derive hostnqn from hostid */
if (!hnqn) {
hnqn = nvmf_hostnqn_generate_from_hostid(hid);
if (!hnqn)
return -ENOMEM;
}
/* sanity checks */
nqn = nvme_hostid_from_hostnqn(hnqn);
if (nqn && strcmp(nqn, hid)) {
nvme_msg(ctx, LOG_DEBUG,
"warning: use hostid '%s' which does not match uuid in hostnqn '%s'\n",
hid, hnqn);
}
*hostid = hid;
*hostnqn = hnqn;
hid = NULL;
hnqn = NULL;
return 0;
}
int nvme_host_get(struct nvme_global_ctx *ctx, const char *hostnqn,
const char *hostid, nvme_host_t *host)
{
_cleanup_free_ char *hnqn = NULL;
_cleanup_free_ char *hid = NULL;
struct nvme_host *h;
int err;
err = nvme_host_get_ids(ctx, hostnqn, hostid, &hnqn, &hid);
if (err)
return err;
h = nvme_lookup_host(ctx, hnqn, hid);
if (!h)
return -ENOMEM;
nvme_host_set_hostsymname(h, NULL);
*host = h;
return 0;
}
static void nvme_filter_subsystem(struct nvme_global_ctx *ctx, nvme_subsystem_t s,
nvme_scan_filter_t f, void *f_args)
{
if (f(s, NULL, NULL, f_args))
return;
nvme_msg(ctx, LOG_DEBUG, "filter out subsystem %s\n",
nvme_subsystem_get_name(s));
nvme_free_subsystem(s);
}
static void nvme_filter_ns(struct nvme_global_ctx *ctx, nvme_ns_t n,
nvme_scan_filter_t f, void *f_args)
{
if (f(NULL, NULL, n, f_args))
return;
nvme_msg(ctx, LOG_DEBUG, "filter out namespace %s\n",
nvme_ns_get_name(n));
nvme_free_ns(n);
}
static void nvme_filter_ctrl(struct nvme_global_ctx *ctx, nvme_ctrl_t c,
nvme_scan_filter_t f, void *f_args)
{
if (f(NULL, c, NULL, f_args))
return;
nvme_msg(ctx, LOG_DEBUG, "filter out controller %s\n",
nvme_ctrl_get_name(c));
nvme_free_ctrl(c);
}
static void nvme_filter_tree(struct nvme_global_ctx *ctx, nvme_scan_filter_t f, void *f_args)
{
nvme_host_t h, _h;
nvme_subsystem_t s, _s;
nvme_ns_t n, _n;
nvme_path_t p, _p;
nvme_ctrl_t c, _c;
if (!f)
return;
nvme_for_each_host_safe(ctx, h, _h) {
nvme_for_each_subsystem_safe(h, s, _s) {
nvme_subsystem_for_each_ctrl_safe(s, c, _c)
nvme_filter_ctrl(ctx, c, f, f_args);
nvme_subsystem_for_each_ns_safe(s, n, _n) {
nvme_namespace_for_each_path_safe(n, p, _p) {
nvme_filter_ctrl(ctx, nvme_path_get_ctrl(p),
f, f_args);
}
nvme_filter_ns(ctx, n, f, f_args);
}
nvme_filter_subsystem(ctx, s, f, f_args);
}
}
}
int nvme_scan_topology(struct nvme_global_ctx *ctx, nvme_scan_filter_t f, void *f_args)
{
_cleanup_dirents_ struct dirents subsys = {}, ctrls = {};
int i, ret;
if (!ctx)
return 0;
ctrls.num = nvme_scan_ctrls(&ctrls.ents);
if (ctrls.num < 0) {
nvme_msg(ctx, LOG_DEBUG, "failed to scan ctrls: %s\n",
nvme_strerror(-ctrls.num));
return ctrls.num;
}
for (i = 0; i < ctrls.num; i++) {
nvme_ctrl_t c;
ret = nvme_scan_ctrl(ctx, ctrls.ents[i]->d_name, &c);
if (ret < 0) {
nvme_msg(ctx, LOG_DEBUG, "failed to scan ctrl %s: %s\n",
ctrls.ents[i]->d_name, nvme_strerror(-ret));
continue;
}
}
subsys.num = nvme_scan_subsystems(&subsys.ents);
if (subsys.num < 0) {
nvme_msg(ctx, LOG_DEBUG, "failed to scan subsystems: %s\n",
nvme_strerror(-subsys.num));
return subsys.num;
}
for (i = 0; i < subsys.num; i++) {
ret = nvme_scan_subsystem(ctx, subsys.ents[i]->d_name);
if (ret < 0) {
nvme_msg(ctx, LOG_DEBUG,
"failed to scan subsystem %s: %s\n",
subsys.ents[i]->d_name, nvme_strerror(-ret));
}
}
/*
* Filter the tree after it has been fully populated and
* updated
*/
nvme_filter_tree(ctx, f, f_args);
return 0;
}
struct nvme_global_ctx *nvme_create_global_ctx(FILE *fp, int log_level)
{
struct nvme_global_ctx *ctx;
int fd;
ctx = calloc(1, sizeof(*ctx));
if (!ctx)
return NULL;
if (fp) {
fd = fileno(fp);
if (fd < 0) {
free(ctx);
return NULL;
}
} else
fd = STDERR_FILENO;
ctx->log.fd = fd;
ctx->log.level = log_level;
list_head_init(&ctx->hosts);
list_head_init(&ctx->endpoints);
return ctx;
}
int nvme_read_config(struct nvme_global_ctx *ctx, const char *config_file)
{
int err;
if (!ctx || !config_file)
return -ENODEV;
ctx->config_file = strdup(config_file);
if (!ctx->config_file)
return -ENOMEM;
err = json_read_config(ctx, config_file);
/*
* The json configuration file is optional,
* so ignore errors when opening the file.
*/
if (err < 0 && err != -EPROTO)
return 0;
return err;
}
int nvme_scan(const char *config_file, struct nvme_global_ctx **ctxp)
{
struct nvme_global_ctx *ctx =
nvme_create_global_ctx(NULL, DEFAULT_LOGLEVEL);
int ret;
if (!ctx)
return -ENOMEM;
ret = nvme_scan_topology(ctx, NULL, NULL);
if (ret)
goto err;
if (config_file) {
ret = nvme_read_config(ctx, config_file);
if (ret)
goto err;
}
*ctxp = ctx;
return 0;
err:
nvme_free_global_ctx(ctx);
return ret;
}
int nvme_dump_config(struct nvme_global_ctx *ctx, const char *config_file)
{
return json_update_config(ctx, config_file);
}
int nvme_dump_tree(struct nvme_global_ctx *ctx)
{
return json_dump_tree(ctx);
}
const char *nvme_get_application(struct nvme_global_ctx *ctx)
{
return ctx->application;
}
void nvme_set_application(struct nvme_global_ctx *ctx, const char *a)
{
free(ctx->application);
ctx->application = NULL;
if (a)
ctx->application = strdup(a);
}
void nvme_skip_namespaces(struct nvme_global_ctx *ctx)
{
ctx->create_only = true;
}
nvme_host_t nvme_first_host(struct nvme_global_ctx *ctx)
{
return list_top(&ctx->hosts, struct nvme_host, entry);
}
nvme_host_t nvme_next_host(struct nvme_global_ctx *ctx, nvme_host_t h)
{
return h ? list_next(&ctx->hosts, h, entry) : NULL;
}
struct nvme_global_ctx *nvme_host_get_global_ctx(nvme_host_t h)
{
return h->ctx;
}
const char *nvme_host_get_hostnqn(nvme_host_t h)
{
return h->hostnqn;
}
const char *nvme_host_get_hostid(nvme_host_t h)
{
return h->hostid;
}
const char *nvme_host_get_hostsymname(nvme_host_t h)
{
return h->hostsymname;
}
void nvme_host_set_hostsymname(nvme_host_t h, const char *hostsymname)
{
free(h->hostsymname);
h->hostsymname = NULL;
if (hostsymname)
h->hostsymname = strdup(hostsymname);
}
const char *nvme_host_get_dhchap_key(nvme_host_t h)
{
return h->dhchap_key;
}
void nvme_host_set_dhchap_key(nvme_host_t h, const char *key)
{
free(h->dhchap_key);
h->dhchap_key = NULL;
if (key)
h->dhchap_key = strdup(key);
}
void nvme_host_set_pdc_enabled(nvme_host_t h, bool enabled)
{
h->pdc_enabled_valid = true;
h->pdc_enabled = enabled;
}
bool nvme_host_is_pdc_enabled(nvme_host_t h, bool fallback)
{
if (h->pdc_enabled_valid)
return h->pdc_enabled;
return fallback;
}
nvme_subsystem_t nvme_first_subsystem(nvme_host_t h)
{
return list_top(&h->subsystems, struct nvme_subsystem, entry);
}
nvme_subsystem_t nvme_next_subsystem(nvme_host_t h, nvme_subsystem_t s)
{
return s ? list_next(&h->subsystems, s, entry) : NULL;
}
void nvme_refresh_topology(struct nvme_global_ctx *ctx)
{
struct nvme_host *h, *_h;
nvme_for_each_host_safe(ctx, h, _h)
__nvme_free_host(h);
nvme_scan_topology(ctx, NULL, NULL);
}
void nvme_free_global_ctx(struct nvme_global_ctx *ctx)
{
struct nvme_host *h, *_h;
if (!ctx)
return;
freeifaddrs(ctx->ifaddrs_cache); /* NULL-safe */
ctx->ifaddrs_cache = NULL;
free(ctx->options);
nvme_for_each_host_safe(ctx, h, _h)
__nvme_free_host(h);
free(ctx->config_file);
free(ctx->application);
free(ctx);
}
void nvme_root_release_fds(struct nvme_global_ctx *ctx)
{
struct nvme_host *h, *_h;
nvme_for_each_host_safe(ctx, h, _h)
nvme_host_release_fds(h);
}
const char *nvme_subsystem_get_nqn(nvme_subsystem_t s)
{
return s->subsysnqn;
}
const char *nvme_subsystem_get_sysfs_dir(nvme_subsystem_t s)
{
return s->sysfs_dir;
}
const char *nvme_subsystem_get_name(nvme_subsystem_t s)
{
return s->name;
}
const char *nvme_subsystem_get_type(nvme_subsystem_t s)
{
return s->subsystype;
}
const char *nvme_subsystem_get_application(nvme_subsystem_t s)
{
return s->application;
}
void nvme_subsystem_set_application(nvme_subsystem_t s, const char *a)
{
free(s->application);
s->application = NULL;
if (a)
s->application = strdup(a);
}
const char *nvme_subsystem_get_iopolicy(nvme_subsystem_t s)
{
return s->iopolicy;
}
const char *nvme_subsystem_get_model(nvme_subsystem_t s)
{
return s->model;
}
const char *nvme_subsystem_get_serial(nvme_subsystem_t s)
{
return s->serial;
}
const char *nvme_subsystem_get_fw_rev(nvme_subsystem_t s)
{
return s->firmware;
}
nvme_ctrl_t nvme_subsystem_first_ctrl(nvme_subsystem_t s)
{
return list_top(&s->ctrls, struct nvme_ctrl, entry);
}
nvme_ctrl_t nvme_subsystem_next_ctrl(nvme_subsystem_t s, nvme_ctrl_t c)
{
return c ? list_next(&s->ctrls, c, entry) : NULL;
}
nvme_host_t nvme_subsystem_get_host(nvme_subsystem_t s)
{
return s->h;
}
nvme_ns_t nvme_subsystem_first_ns(nvme_subsystem_t s)
{
return list_top(&s->namespaces, struct nvme_ns, entry);
}
nvme_ns_t nvme_subsystem_next_ns(nvme_subsystem_t s, nvme_ns_t n)
{
return n ? list_next(&s->namespaces, n, entry) : NULL;
}
nvme_path_t nvme_namespace_first_path(nvme_ns_t ns)
{
return list_top(&ns->head->paths, struct nvme_path, nentry);
}
nvme_path_t nvme_namespace_next_path(nvme_ns_t ns, nvme_path_t p)
{
return p ? list_next(&ns->head->paths, p, nentry) : NULL;
}
static void __nvme_free_ns(struct nvme_ns *n)
{
list_del_init(&n->entry);
nvme_ns_release_transport_handle(n);
free(n->generic_name);
free(n->name);
free(n->sysfs_dir);
free(n->head->sysfs_dir);
free(n->head);
free(n);
}
/* Stub for SWIG */
void nvme_free_ns(struct nvme_ns *n)
{
__nvme_free_ns(n);
}
static void __nvme_free_subsystem(struct nvme_subsystem *s)
{
struct nvme_ctrl *c, *_c;
struct nvme_ns *n, *_n;
list_del_init(&s->entry);
nvme_subsystem_for_each_ctrl_safe(s, c, _c)
__nvme_free_ctrl(c);
nvme_subsystem_for_each_ns_safe(s, n, _n)
__nvme_free_ns(n);
free(s->name);
free(s->sysfs_dir);
free(s->subsysnqn);
free(s->model);
free(s->serial);
free(s->firmware);
free(s->subsystype);
free(s->application);
free(s->iopolicy);
free(s);
}
void nvme_subsystem_release_fds(struct nvme_subsystem *s)
{
struct nvme_ctrl *c, *_c;
struct nvme_ns *n, *_n;
nvme_subsystem_for_each_ctrl_safe(s, c, _c)
nvme_ctrl_release_transport_handle(c);
nvme_subsystem_for_each_ns_safe(s, n, _n)
nvme_ns_release_transport_handle(n);
}
/*
* Stub for SWIG
*/
void nvme_free_subsystem(nvme_subsystem_t s)
{
}
struct nvme_subsystem *nvme_alloc_subsystem(struct nvme_host *h,
const char *name,
const char *subsysnqn)
{
struct nvme_subsystem *s;
s = calloc(1, sizeof(*s));
if (!s)
return NULL;
s->h = h;
s->subsysnqn = strdup(subsysnqn);
if (name)
nvme_init_subsystem(s, name);
list_head_init(&s->ctrls);
list_head_init(&s->namespaces);
list_node_init(&s->entry);
list_add_tail(&h->subsystems, &s->entry);
return s;
}
struct nvme_subsystem *nvme_lookup_subsystem(struct nvme_host *h,
const char *name,
const char *subsysnqn)
{
struct nvme_subsystem *s;
nvme_for_each_subsystem(h, s) {
if (subsysnqn && s->subsysnqn &&
strcmp(s->subsysnqn, subsysnqn))
continue;
if (name && s->name &&
strcmp(s->name, name))
continue;
if (h->ctx->application) {
if (!s->application)
continue;
if (strcmp(h->ctx->application, s->application))
continue;
}
return s;
}
return nvme_alloc_subsystem(h, name, subsysnqn);
}
int nvme_subsystem_get(struct nvme_global_ctx *ctx,
struct nvme_host *h, const char *name,
const char *subsysnqn, struct nvme_subsystem **subsys)
{
struct nvme_subsystem *s;
s = nvme_lookup_subsystem(h, name, subsysnqn);
if (!s)
return -ENOMEM;
*subsys = s;
return 0;
}
static void __nvme_free_host(struct nvme_host *h)
{
struct nvme_subsystem *s, *_s;
list_del_init(&h->entry);
nvme_for_each_subsystem_safe(h, s, _s)
__nvme_free_subsystem(s);
free(h->hostnqn);
free(h->hostid);
free(h->dhchap_key);
nvme_host_set_hostsymname(h, NULL);
free(h);
}
void nvme_host_release_fds(struct nvme_host *h)
{
struct nvme_subsystem *s, *_s;
nvme_for_each_subsystem_safe(h, s, _s)
nvme_subsystem_release_fds(s);
}
/* Stub for SWIG */
void nvme_free_host(struct nvme_host *h)
{
__nvme_free_host(h);
}
static int nvme_create_host(struct nvme_global_ctx *ctx, const char *hostnqn,
const char *hostid, struct nvme_host **host)
{
struct nvme_host *h;
h = calloc(1, sizeof(*h));
if (!h)
return -ENOMEM;
h->hostnqn = strdup(hostnqn);
if (hostid)
h->hostid = strdup(hostid);
list_head_init(&h->subsystems);
list_node_init(&h->entry);
h->ctx = ctx;
list_add_tail(&ctx->hosts, &h->entry);
*host = h;
return 0;
}
struct nvme_host *nvme_lookup_host(struct nvme_global_ctx *ctx,
const char *hostnqn, const char *hostid)
{
struct nvme_host *h;
if (!hostnqn)
return NULL;
nvme_for_each_host(ctx, h) {
if (strcmp(h->hostnqn, hostnqn))
continue;
if (hostid && (!h->hostid ||
strcmp(h->hostid, hostid)))
continue;
return h;
}
if (nvme_create_host(ctx, hostnqn, hostid, &h))
return NULL;
return h;
}
static int nvme_subsystem_scan_namespaces(struct nvme_global_ctx *ctx, nvme_subsystem_t s)
{
_cleanup_dirents_ struct dirents namespaces = {};
int i, ret;
if (ctx->create_only) {
nvme_msg(ctx, LOG_DEBUG,
"skipping namespace scan for subsys %s\n",
s->subsysnqn);
return 0;
}
namespaces.num = nvme_scan_subsystem_namespaces(s, &namespaces.ents);
if (namespaces.num < 0) {
nvme_msg(ctx, LOG_DEBUG,
"failed to scan namespaces for subsys %s: %s\n",
s->subsysnqn, nvme_strerror(-namespaces.num));
return namespaces.num;
}
for (i = 0; i < namespaces.num; i++) {
ret = nvme_subsystem_scan_namespace(ctx, s,
namespaces.ents[i]->d_name);
if (ret < 0)
nvme_msg(ctx, LOG_DEBUG,
"failed to scan namespace %s: %s\n",
namespaces.ents[i]->d_name, nvme_strerror(-ret));
}
return 0;
}
static int nvme_init_subsystem(nvme_subsystem_t s, const char *name)
{
char *path;
if (asprintf(&path, "%s/%s", nvme_subsys_sysfs_dir(), name) < 0)
return -ENOMEM;
s->model = nvme_get_attr(path, "model");
if (!s->model)
s->model = strdup("undefined");
s->serial = nvme_get_attr(path, "serial");
s->firmware = nvme_get_attr(path, "firmware_rev");
s->subsystype = nvme_get_attr(path, "subsystype");
if (!s->subsystype) {
if (!strcmp(s->subsysnqn, NVME_DISC_SUBSYS_NAME))
s->subsystype = strdup("discovery");
else
s->subsystype = strdup("nvm");
}
s->name = strdup(name);
s->sysfs_dir = (char *)path;
if (s->h->ctx->application)
s->application = strdup(s->h->ctx->application);
s->iopolicy = nvme_get_attr(path, "iopolicy");
return 0;
}
static int nvme_scan_subsystem(struct nvme_global_ctx *ctx, const char *name)
{
struct nvme_subsystem *s = NULL, *_s;
_cleanup_free_ char *path = NULL, *subsysnqn = NULL;
nvme_host_t h = NULL;
int ret;
nvme_msg(ctx, LOG_DEBUG, "scan subsystem %s\n", name);
ret = asprintf(&path, "%s/%s", nvme_subsys_sysfs_dir(), name);
if (ret < 0)
return -ENOMEM;
subsysnqn = nvme_get_attr(path, "subsysnqn");
if (!subsysnqn)
return -ENODEV;
nvme_for_each_host(ctx, h) {
nvme_for_each_subsystem(h, _s) {
/*
* We are always called after nvme_scan_ctrl(),
* so any subsystem we're interested at _must_
* have a name.
*/
if (!_s->name)
continue;
if (strcmp(_s->name, name))
continue;
if (!nvme_subsystem_scan_namespaces(ctx, _s))
return -EINVAL;
s = _s;
}
}
if (!s) {
/*
* Subsystem with non-matching controller. odd.
* Create a subsystem with the default host
* and hope for the best.
*/
nvme_msg(ctx, LOG_DEBUG, "creating detached subsystem '%s'\n",
name);
ret = nvme_host_get(ctx, NULL, NULL, &h);
if (ret)
return ret;
s = nvme_alloc_subsystem(h, name, subsysnqn);
if (!s)
return -ENOMEM;
if (!nvme_subsystem_scan_namespaces(ctx, s))
return -EINVAL;
} else if (strcmp(s->subsysnqn, subsysnqn)) {
nvme_msg(ctx, LOG_DEBUG, "NQN mismatch for subsystem '%s'\n",
name);
return -EINVAL;
}
return 0;
}
nvme_ctrl_t nvme_path_get_ctrl(nvme_path_t p)
{
return p->c;
}
nvme_ns_t nvme_path_get_ns(nvme_path_t p)
{
return p->n;
}
const char *nvme_path_get_sysfs_dir(nvme_path_t p)
{
return p->sysfs_dir;
}
const char *nvme_path_get_name(nvme_path_t p)
{
return p->name;
}
int nvme_path_get_queue_depth(nvme_path_t p)
{
_cleanup_free_ char *queue_depth = NULL;
queue_depth = nvme_get_path_attr(p, "queue_depth");
if (queue_depth) {
sscanf(queue_depth, "%d", &p->queue_depth);
}
return p->queue_depth;
}
const char *nvme_path_get_ana_state(nvme_path_t p)
{
return p->ana_state;
}
const char *nvme_path_get_numa_nodes(nvme_path_t p)
{
return p->numa_nodes;
}
void nvme_free_path(struct nvme_path *p)
{
list_del_init(&p->entry);
list_del_init(&p->nentry);
free(p->name);
free(p->sysfs_dir);
free(p->ana_state);
free(p->numa_nodes);
free(p);
}
static int nvme_ctrl_scan_path(struct nvme_global_ctx *ctx, struct nvme_ctrl *c, char *name)
{