| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/* |
| 3 |
* This file is part of libnvme. |
| 4 |
* Copyright (c) 2020 Western Digital Corporation or its affiliates. |
| 5 |
* |
| 6 |
* Authors: Keith Busch <[email protected]> |
| 7 |
* Chaitanya Kulkarni <[email protected]> |
| 8 |
*/ |
| 9 |
#include <dirent.h> |
| 10 |
#include <errno.h> |
| 11 |
#include <stdint.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
#include <fcntl.h> |
| 16 |
#include <libgen.h> |
| 17 |
#include <unistd.h> |
| 18 |
#include <ifaddrs.h> |
| 19 |
|
| 20 |
#include <sys/types.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
#include <arpa/inet.h> |
| 23 |
#include <netdb.h> |
| 24 |
|
| 25 |
#include <ccan/endian/endian.h> |
| 26 |
#include <ccan/list/list.h> |
| 27 |
|
| 28 |
#include "cleanup.h" |
| 29 |
#include "ioctl.h" |
| 30 |
#include "linux.h" |
| 31 |
#include "filters.h" |
| 32 |
#include "tree.h" |
| 33 |
#include "filters.h" |
| 34 |
#include "util.h" |
| 35 |
#include "fabrics.h" |
| 36 |
#include "log.h" |
| 37 |
#include "private.h" |
| 38 |
|
| 39 |
/** |
| 40 |
* struct candidate_args - Used to look for a controller matching these parameters |
| 41 |
* @transport: Transport type: loop, fc, rdma, tcp |
| 42 |
* @traddr: Transport address (destination address) |
| 43 |
* @trsvcid: Transport service ID |
| 44 |
* @subsysnqn: Subsystem NQN |
| 45 |
* @host_traddr: Host transport address (source address) |
| 46 |
* @host_iface: Host interface for connection (tcp only) |
| 47 |
* @iface_list: Interface list (tcp only) |
| 48 |
* @addreq: Address comparison function (for traddr, host-traddr) |
| 49 |
* @well_known_nqn: Set to "true" when @subsysnqn is the well-known NQN |
| 50 |
*/ |
| 51 |
struct candidate_args { |
| 52 |
const char *transport; |
| 53 |
const char *traddr; |
| 54 |
const char *trsvcid; |
| 55 |
const char *subsysnqn; |
| 56 |
const char *host_traddr; |
| 57 |
const char *host_iface; |
| 58 |
struct ifaddrs *iface_list; |
| 59 |
bool (*addreq)(const char *, const char *); |
| 60 |
bool well_known_nqn; |
| 61 |
}; |
| 62 |
typedef bool (*ctrl_match_t)(struct nvme_ctrl *c, struct candidate_args *candidate); |
| 63 |
|
| 64 |
static struct nvme_host *default_host; |
| 65 |
|
| 66 |
static void __nvme_free_host(nvme_host_t h); |
| 67 |
static void __nvme_free_ctrl(nvme_ctrl_t c); |
| 68 |
static int nvme_subsystem_scan_namespace(nvme_root_t r, |
| 69 |
struct nvme_subsystem *s, char *name); |
| 70 |
static int nvme_init_subsystem(nvme_subsystem_t s, const char *name); |
| 71 |
static int nvme_scan_subsystem(nvme_root_t r, const char *name); |
| 72 |
static int nvme_ctrl_scan_namespace(nvme_root_t r, struct nvme_ctrl *c, |
| 73 |
char *name); |
| 74 |
static int nvme_ctrl_scan_path(nvme_root_t r, struct nvme_ctrl *c, char *name); |
| 75 |
|
| 76 |
/** |
| 77 |
* Compare two C strings and handle NULL pointers gracefully. |
| 78 |
* Return true if both pointers are equal (including both set to NULL). |
| 79 |
* Return false if one and only one of the two pointers is NULL. |
| 80 |
* Perform string comparisong only if both pointers are not NULL and |
| 81 |
* return true if both strings are the same, false otherwise. |
| 82 |
*/ |
| 83 |
static bool streq0(const char *s1, const char *s2) |
| 84 |
{ |
| 85 |
if (s1 == s2) |
| 86 |
return true; |
| 87 |
if (!s1 || !s2) |
| 88 |
return false; |
| 89 |
return !strcmp(s1, s2); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Same as streq0() but ignore the case of the characters. |
| 94 |
*/ |
| 95 |
static bool streqcase0(const char *s1, const char *s2) |
| 96 |
{ |
| 97 |
if (s1 == s2) |
| 98 |
return true; |
| 99 |
if (!s1 || !s2) |
| 100 |
return false; |
| 101 |
return !strcasecmp(s1, s2); |
| 102 |
} |
| 103 |
|
| 104 |
struct dirents { |
| 105 |
struct dirent **ents; |
| 106 |
int num; |
| 107 |
}; |
| 108 |
|
| 109 |
static void cleanup_dirents(struct dirents *ents) |
| 110 |
{ |
| 111 |
while (ents->num > 0) |
| 112 |
free(ents->ents[--ents->num]); |
| 113 |
free(ents->ents); |
| 114 |
} |
| 115 |
|
| 116 |
#define _cleanup_dirents_ __cleanup__(cleanup_dirents) |
| 117 |
|
| 118 |
static char *nvme_hostid_from_hostnqn(const char *hostnqn) |
| 119 |
{ |
| 120 |
const char *uuid; |
| 121 |
|
| 122 |
uuid = strstr(hostnqn, "uuid:"); |
| 123 |
if (!uuid) |
| 124 |
return NULL; |
| 125 |
|
| 126 |
return strdup(uuid + strlen("uuid:")); |
| 127 |
} |
| 128 |
|
| 129 |
int nvme_host_get_ids(nvme_root_t r, |
| 130 |
char *hostnqn_arg, char *hostid_arg, |
| 131 |
char **hostnqn, char **hostid) |
| 132 |
{ |
| 133 |
_cleanup_free_ char *nqn = NULL; |
| 134 |
_cleanup_free_ char *hid = NULL; |
| 135 |
_cleanup_free_ char *hnqn = NULL; |
| 136 |
nvme_host_t h; |
| 137 |
|
| 138 |
/* command line argumments */ |
| 139 |
if (hostid_arg) |
| 140 |
hid = strdup(hostid_arg); |
| 141 |
if (hostnqn_arg) |
| 142 |
hnqn = strdup(hostnqn_arg); |
| 143 |
|
| 144 |
/* JSON config: assume the first entry is the default host */ |
| 145 |
h = nvme_first_host(r); |
| 146 |
if (h) { |
| 147 |
if (!hid) |
| 148 |
hid = strdup(nvme_host_get_hostid(h)); |
| 149 |
if (!hnqn) |
| 150 |
hnqn = strdup(nvme_host_get_hostnqn(h)); |
| 151 |
} |
| 152 |
|
| 153 |
/* /etc/nvme/hostid and/or /etc/nvme/hostnqn */ |
| 154 |
if (!hid) |
| 155 |
hid = nvmf_hostid_from_file(); |
| 156 |
if (!hnqn) |
| 157 |
hnqn = nvmf_hostnqn_from_file(); |
| 158 |
|
| 159 |
/* incomplete configuration, thus derive hostid from hostnqn */ |
| 160 |
if (!hid && hnqn) |
| 161 |
hid = nvme_hostid_from_hostnqn(hnqn); |
| 162 |
|
| 163 |
/* |
| 164 |
* fallback to use either DMI information or device-tree. If all |
| 165 |
* fails generate one |
| 166 |
*/ |
| 167 |
if (!hid) { |
| 168 |
hid = nvmf_hostid_generate(); |
| 169 |
if (!hid) { |
| 170 |
errno = -ENOMEM; |
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
nvme_msg(r, LOG_DEBUG, |
| 175 |
"warning: using auto generated hostid and hostnqn\n"); |
| 176 |
} |
| 177 |
|
| 178 |
/* incomplete configuration, thus derive hostnqn from hostid */ |
| 179 |
if (!hnqn) { |
| 180 |
hnqn = nvmf_hostnqn_generate_from_hostid(hid); |
| 181 |
if (!hnqn) { |
| 182 |
errno = -ENOMEM; |
| 183 |
return -1; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/* sanity checks */ |
| 188 |
nqn = nvme_hostid_from_hostnqn(hnqn); |
| 189 |
if (nqn && strcmp(nqn, hid)) { |
| 190 |
nvme_msg(r, LOG_DEBUG, |
| 191 |
"warning: use hostid '%s' which does not match uuid in hostnqn '%s'\n", |
| 192 |
hid, hnqn); |
| 193 |
} |
| 194 |
|
| 195 |
*hostid = hid; |
| 196 |
*hostnqn = hnqn; |
| 197 |
hid = NULL; |
| 198 |
hnqn = NULL; |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
nvme_host_t nvme_default_host(nvme_root_t r) |
| 204 |
{ |
| 205 |
_cleanup_free_ char *hostnqn = NULL; |
| 206 |
_cleanup_free_ char *hostid = NULL; |
| 207 |
struct nvme_host *h; |
| 208 |
|
| 209 |
if (nvme_host_get_ids(r, NULL, NULL, &hostnqn, &hostid)) |
| 210 |
return NULL; |
| 211 |
|
| 212 |
h = nvme_lookup_host(r, hostnqn, hostid); |
| 213 |
|
| 214 |
nvme_host_set_hostsymname(h, NULL); |
| 215 |
|
| 216 |
default_host = h; |
| 217 |
return h; |
| 218 |
} |
| 219 |
|
| 220 |
static void nvme_filter_subsystem(nvme_root_t r, nvme_subsystem_t s, |
| 221 |
nvme_scan_filter_t f, void *f_args) |
| 222 |
{ |
| 223 |
if (f(s, NULL, NULL, f_args)) |
| 224 |
return; |
| 225 |
|
| 226 |
nvme_msg(r, LOG_DEBUG, "filter out subsystem %s\n", |
| 227 |
nvme_subsystem_get_name(s)); |
| 228 |
nvme_free_subsystem(s); |
| 229 |
} |
| 230 |
|
| 231 |
static void nvme_filter_ns(nvme_root_t r, nvme_ns_t n, |
| 232 |
nvme_scan_filter_t f, void *f_args) |
| 233 |
{ |
| 234 |
if (f(NULL, NULL, n, f_args)) |
| 235 |
return; |
| 236 |
|
| 237 |
nvme_msg(r, LOG_DEBUG, "filter out namespace %s\n", |
| 238 |
nvme_ns_get_name(n)); |
| 239 |
nvme_free_ns(n); |
| 240 |
} |
| 241 |
|
| 242 |
static void nvme_filter_ctrl(nvme_root_t r, nvme_ctrl_t c, |
| 243 |
nvme_scan_filter_t f, void *f_args) |
| 244 |
{ |
| 245 |
if (f(NULL, c, NULL, f_args)) |
| 246 |
return; |
| 247 |
|
| 248 |
nvme_msg(r, LOG_DEBUG, "filter out controller %s\n", |
| 249 |
nvme_ctrl_get_name(c)); |
| 250 |
nvme_free_ctrl(c); |
| 251 |
} |
| 252 |
|
| 253 |
static void nvme_filter_tree(nvme_root_t r, nvme_scan_filter_t f, void *f_args) |
| 254 |
{ |
| 255 |
nvme_host_t h, _h; |
| 256 |
nvme_subsystem_t s, _s; |
| 257 |
nvme_ns_t n, _n; |
| 258 |
nvme_path_t p, _p; |
| 259 |
nvme_ctrl_t c, _c; |
| 260 |
|
| 261 |
if (!f) |
| 262 |
return; |
| 263 |
|
| 264 |
nvme_for_each_host_safe(r, h, _h) { |
| 265 |
nvme_for_each_subsystem_safe(h, s, _s) { |
| 266 |
nvme_subsystem_for_each_ctrl_safe(s, c, _c) |
| 267 |
nvme_filter_ctrl(r, c, f, f_args); |
| 268 |
|
| 269 |
nvme_subsystem_for_each_ns_safe(s, n, _n) { |
| 270 |
nvme_namespace_for_each_path_safe(n, p, _p) { |
| 271 |
nvme_filter_ctrl(r, nvme_path_get_ctrl(p), |
| 272 |
f, f_args); |
| 273 |
} |
| 274 |
nvme_filter_ns(r, n, f, f_args); |
| 275 |
} |
| 276 |
|
| 277 |
nvme_filter_subsystem(r, s, f, f_args); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
int nvme_scan_topology(struct nvme_root *r, nvme_scan_filter_t f, void *f_args) |
| 283 |
{ |
| 284 |
_cleanup_dirents_ struct dirents subsys = {}, ctrls = {}; |
| 285 |
int i, ret; |
| 286 |
|
| 287 |
if (!r) |
| 288 |
return 0; |
| 289 |
|
| 290 |
ctrls.num = nvme_scan_ctrls(&ctrls.ents); |
| 291 |
if (ctrls.num < 0) { |
| 292 |
nvme_msg(r, LOG_DEBUG, "failed to scan ctrls: %s\n", |
| 293 |
strerror(errno)); |
| 294 |
return ctrls.num; |
| 295 |
} |
| 296 |
|
| 297 |
for (i = 0; i < ctrls.num; i++) { |
| 298 |
nvme_ctrl_t c = nvme_scan_ctrl(r, ctrls.ents[i]->d_name); |
| 299 |
if (!c) { |
| 300 |
nvme_msg(r, LOG_DEBUG, "failed to scan ctrl %s: %s\n", |
| 301 |
ctrls.ents[i]->d_name, strerror(errno)); |
| 302 |
continue; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
subsys.num = nvme_scan_subsystems(&subsys.ents); |
| 307 |
if (subsys.num < 0) { |
| 308 |
nvme_msg(r, LOG_DEBUG, "failed to scan subsystems: %s\n", |
| 309 |
strerror(errno)); |
| 310 |
return subsys.num; |
| 311 |
} |
| 312 |
|
| 313 |
for (i = 0; i < subsys.num; i++) { |
| 314 |
ret = nvme_scan_subsystem(r, subsys.ents[i]->d_name); |
| 315 |
if (ret < 0) { |
| 316 |
nvme_msg(r, LOG_DEBUG, |
| 317 |
"failed to scan subsystem %s: %s\n", |
| 318 |
subsys.ents[i]->d_name, strerror(errno)); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
/* |
| 323 |
* Filter the tree after it has been fully populated and |
| 324 |
* updated |
| 325 |
*/ |
| 326 |
nvme_filter_tree(r, f, f_args); |
| 327 |
|
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
nvme_root_t nvme_create_root(FILE *fp, int log_level) |
| 332 |
{ |
| 333 |
struct nvme_root *r; |
| 334 |
int fd; |
| 335 |
|
| 336 |
r = calloc(1, sizeof(*r)); |
| 337 |
if (!r) { |
| 338 |
errno = ENOMEM; |
| 339 |
return NULL; |
| 340 |
} |
| 341 |
|
| 342 |
if (fp) { |
| 343 |
fd = fileno(fp); |
| 344 |
if (fd < 0) { |
| 345 |
free(r); |
| 346 |
return NULL; |
| 347 |
} |
| 348 |
} else |
| 349 |
fd = STDERR_FILENO; |
| 350 |
|
| 351 |
r->log.fd = fd; |
| 352 |
r->log.level = log_level; |
| 353 |
|
| 354 |
list_head_init(&r->hosts); |
| 355 |
list_head_init(&r->endpoints); |
| 356 |
|
| 357 |
return r; |
| 358 |
} |
| 359 |
|
| 360 |
int nvme_read_config(nvme_root_t r, const char *config_file) |
| 361 |
{ |
| 362 |
int err = -1; |
| 363 |
int tmp; |
| 364 |
|
| 365 |
if (!r || !config_file) { |
| 366 |
errno = ENODEV; |
| 367 |
return err; |
| 368 |
} |
| 369 |
|
| 370 |
r->config_file = strdup(config_file); |
| 371 |
if (!r->config_file) { |
| 372 |
errno = ENOMEM; |
| 373 |
return err; |
| 374 |
} |
| 375 |
|
| 376 |
tmp = errno; |
| 377 |
err = json_read_config(r, config_file); |
| 378 |
/* |
| 379 |
* The json configuration file is optional, |
| 380 |
* so ignore errors when opening the file. |
| 381 |
*/ |
| 382 |
if (err < 0 && errno != EPROTO) { |
| 383 |
errno = tmp; |
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
return err; |
| 388 |
} |
| 389 |
|
| 390 |
nvme_root_t nvme_scan(const char *config_file) |
| 391 |
{ |
| 392 |
nvme_root_t r = nvme_create_root(NULL, DEFAULT_LOGLEVEL); |
| 393 |
|
| 394 |
nvme_scan_topology(r, NULL, NULL); |
| 395 |
nvme_read_config(r, config_file); |
| 396 |
return r; |
| 397 |
} |
| 398 |
|
| 399 |
int nvme_update_config(nvme_root_t r) |
| 400 |
{ |
| 401 |
if (!r->modified || !r->config_file) |
| 402 |
return 0; |
| 403 |
|
| 404 |
return json_update_config(r, r->config_file); |
| 405 |
} |
| 406 |
|
| 407 |
int nvme_dump_config(nvme_root_t r) |
| 408 |
{ |
| 409 |
return json_update_config(r, NULL); |
| 410 |
} |
| 411 |
|
| 412 |
int nvme_dump_tree(nvme_root_t r) |
| 413 |
{ |
| 414 |
return json_dump_tree(r); |
| 415 |
} |
| 416 |
|
| 417 |
const char *nvme_root_get_application(nvme_root_t r) |
| 418 |
{ |
| 419 |
return r->application; |
| 420 |
} |
| 421 |
|
| 422 |
void nvme_root_set_application(nvme_root_t r, const char *a) |
| 423 |
{ |
| 424 |
free(r->application); |
| 425 |
r->application = NULL; |
| 426 |
|
| 427 |
if (a) |
| 428 |
r->application = strdup(a); |
| 429 |
} |
| 430 |
|
| 431 |
void nvme_root_skip_namespaces(nvme_root_t r) |
| 432 |
{ |
| 433 |
r->create_only = true; |
| 434 |
} |
| 435 |
|
| 436 |
nvme_host_t nvme_first_host(nvme_root_t r) |
| 437 |
{ |
| 438 |
return list_top(&r->hosts, struct nvme_host, entry); |
| 439 |
} |
| 440 |
|
| 441 |
nvme_host_t nvme_next_host(nvme_root_t r, nvme_host_t h) |
| 442 |
{ |
| 443 |
return h ? list_next(&r->hosts, h, entry) : NULL; |
| 444 |
} |
| 445 |
|
| 446 |
nvme_root_t nvme_host_get_root(nvme_host_t h) |
| 447 |
{ |
| 448 |
return h->r; |
| 449 |
} |
| 450 |
|
| 451 |
const char *nvme_host_get_hostnqn(nvme_host_t h) |
| 452 |
{ |
| 453 |
return h->hostnqn; |
| 454 |
} |
| 455 |
|
| 456 |
const char *nvme_host_get_hostid(nvme_host_t h) |
| 457 |
{ |
| 458 |
return h->hostid; |
| 459 |
} |
| 460 |
|
| 461 |
const char *nvme_host_get_hostsymname(nvme_host_t h) |
| 462 |
{ |
| 463 |
return h->hostsymname; |
| 464 |
} |
| 465 |
|
| 466 |
void nvme_host_set_hostsymname(nvme_host_t h, const char *hostsymname) |
| 467 |
{ |
| 468 |
free(h->hostsymname); |
| 469 |
h->hostsymname = NULL; |
| 470 |
|
| 471 |
if (hostsymname) |
| 472 |
h->hostsymname = strdup(hostsymname); |
| 473 |
} |
| 474 |
|
| 475 |
const char *nvme_host_get_dhchap_key(nvme_host_t h) |
| 476 |
{ |
| 477 |
return h->dhchap_key; |
| 478 |
} |
| 479 |
|
| 480 |
void nvme_host_set_dhchap_key(nvme_host_t h, const char *key) |
| 481 |
{ |
| 482 |
free(h->dhchap_key); |
| 483 |
h->dhchap_key = NULL; |
| 484 |
|
| 485 |
if (key) |
| 486 |
h->dhchap_key = strdup(key); |
| 487 |
} |
| 488 |
|
| 489 |
void nvme_host_set_pdc_enabled(nvme_host_t h, bool enabled) |
| 490 |
{ |
| 491 |
h->pdc_enabled_valid = true; |
| 492 |
h->pdc_enabled = enabled; |
| 493 |
} |
| 494 |
|
| 495 |
bool nvme_host_is_pdc_enabled(nvme_host_t h, bool fallback) |
| 496 |
{ |
| 497 |
if (h->pdc_enabled_valid) |
| 498 |
return h->pdc_enabled; |
| 499 |
return fallback; |
| 500 |
} |
| 501 |
|
| 502 |
nvme_subsystem_t nvme_first_subsystem(nvme_host_t h) |
| 503 |
{ |
| 504 |
return list_top(&h->subsystems, struct nvme_subsystem, entry); |
| 505 |
} |
| 506 |
|
| 507 |
nvme_subsystem_t nvme_next_subsystem(nvme_host_t h, nvme_subsystem_t s) |
| 508 |
{ |
| 509 |
return s ? list_next(&h->subsystems, s, entry) : NULL; |
| 510 |
} |
| 511 |
|
| 512 |
void nvme_refresh_topology(nvme_root_t r) |
| 513 |
{ |
| 514 |
struct nvme_host *h, *_h; |
| 515 |
|
| 516 |
nvme_for_each_host_safe(r, h, _h) |
| 517 |
__nvme_free_host(h); |
| 518 |
nvme_scan_topology(r, NULL, NULL); |
| 519 |
} |
| 520 |
|
| 521 |
void nvme_free_tree(nvme_root_t r) |
| 522 |
{ |
| 523 |
struct nvme_host *h, *_h; |
| 524 |
|
| 525 |
if (!r) |
| 526 |
return; |
| 527 |
|
| 528 |
free(r->options); |
| 529 |
nvme_for_each_host_safe(r, h, _h) |
| 530 |
__nvme_free_host(h); |
| 531 |
free(r->config_file); |
| 532 |
free(r->application); |
| 533 |
free(r); |
| 534 |
} |
| 535 |
|
| 536 |
void nvme_root_release_fds(nvme_root_t r) |
| 537 |
{ |
| 538 |
struct nvme_host *h, *_h; |
| 539 |
|
| 540 |
nvme_for_each_host_safe(r, h, _h) |
| 541 |
nvme_host_release_fds(h); |
| 542 |
} |
| 543 |
|
| 544 |
const char *nvme_subsystem_get_nqn(nvme_subsystem_t s) |
| 545 |
{ |
| 546 |
return s->subsysnqn; |
| 547 |
} |
| 548 |
|
| 549 |
const char *nvme_subsystem_get_sysfs_dir(nvme_subsystem_t s) |
| 550 |
{ |
| 551 |
return s->sysfs_dir; |
| 552 |
} |
| 553 |
|
| 554 |
const char *nvme_subsystem_get_name(nvme_subsystem_t s) |
| 555 |
{ |
| 556 |
return s->name; |
| 557 |
} |
| 558 |
|
| 559 |
const char *nvme_subsystem_get_type(nvme_subsystem_t s) |
| 560 |
{ |
| 561 |
return s->subsystype; |
| 562 |
} |
| 563 |
|
| 564 |
const char *nvme_subsystem_get_application(nvme_subsystem_t s) |
| 565 |
{ |
| 566 |
return s->application; |
| 567 |
} |
| 568 |
|
| 569 |
void nvme_subsystem_set_application(nvme_subsystem_t s, const char *a) |
| 570 |
{ |
| 571 |
free(s->application); |
| 572 |
s->application = NULL; |
| 573 |
|
| 574 |
if (a) |
| 575 |
s->application = strdup(a); |
| 576 |
} |
| 577 |
|
| 578 |
const char *nvme_subsystem_get_iopolicy(nvme_subsystem_t s) |
| 579 |
{ |
| 580 |
return s->iopolicy; |
| 581 |
} |
| 582 |
|
| 583 |
const char *nvme_subsystem_get_model(nvme_subsystem_t s) |
| 584 |
{ |
| 585 |
return s->model; |
| 586 |
} |
| 587 |
|
| 588 |
const char *nvme_subsystem_get_serial(nvme_subsystem_t s) |
| 589 |
{ |
| 590 |
return s->serial; |
| 591 |
} |
| 592 |
|
| 593 |
const char *nvme_subsystem_get_fw_rev(nvme_subsystem_t s) |
| 594 |
{ |
| 595 |
return s->firmware; |
| 596 |
} |
| 597 |
|
| 598 |
nvme_ctrl_t nvme_subsystem_first_ctrl(nvme_subsystem_t s) |
| 599 |
{ |
| 600 |
return list_top(&s->ctrls, struct nvme_ctrl, entry); |
| 601 |
} |
| 602 |
|
| 603 |
nvme_ctrl_t nvme_subsystem_next_ctrl(nvme_subsystem_t s, nvme_ctrl_t c) |
| 604 |
{ |
| 605 |
return c ? list_next(&s->ctrls, c, entry) : NULL; |
| 606 |
} |
| 607 |
|
| 608 |
nvme_host_t nvme_subsystem_get_host(nvme_subsystem_t s) |
| 609 |
{ |
| 610 |
return s->h; |
| 611 |
} |
| 612 |
|
| 613 |
nvme_ns_t nvme_subsystem_first_ns(nvme_subsystem_t s) |
| 614 |
{ |
| 615 |
return list_top(&s->namespaces, struct nvme_ns, entry); |
| 616 |
} |
| 617 |
|
| 618 |
nvme_ns_t nvme_subsystem_next_ns(nvme_subsystem_t s, nvme_ns_t n) |
| 619 |
{ |
| 620 |
return n ? list_next(&s->namespaces, n, entry) : NULL; |
| 621 |
} |
| 622 |
|
| 623 |
nvme_path_t nvme_namespace_first_path(nvme_ns_t ns) |
| 624 |
{ |
| 625 |
return list_top(&ns->head->paths, struct nvme_path, nentry); |
| 626 |
} |
| 627 |
|
| 628 |
nvme_path_t nvme_namespace_next_path(nvme_ns_t ns, nvme_path_t p) |
| 629 |
{ |
| 630 |
return p ? list_next(&ns->head->paths, p, nentry) : NULL; |
| 631 |
} |
| 632 |
|
| 633 |
static void __nvme_free_ns(struct nvme_ns *n) |
| 634 |
{ |
| 635 |
list_del_init(&n->entry); |
| 636 |
nvme_ns_release_fd(n); |
| 637 |
free(n->generic_name); |
| 638 |
free(n->name); |
| 639 |
free(n->sysfs_dir); |
| 640 |
free(n->head->sysfs_dir); |
| 641 |
free(n->head); |
| 642 |
free(n); |
| 643 |
} |
| 644 |
|
| 645 |
/* Stub for SWIG */ |
| 646 |
void nvme_free_ns(struct nvme_ns *n) |
| 647 |
{ |
| 648 |
__nvme_free_ns(n); |
| 649 |
} |
| 650 |
|
| 651 |
static void __nvme_free_subsystem(struct nvme_subsystem *s) |
| 652 |
{ |
| 653 |
struct nvme_ctrl *c, *_c; |
| 654 |
struct nvme_ns *n, *_n; |
| 655 |
|
| 656 |
list_del_init(&s->entry); |
| 657 |
nvme_subsystem_for_each_ctrl_safe(s, c, _c) |
| 658 |
__nvme_free_ctrl(c); |
| 659 |
|
| 660 |
nvme_subsystem_for_each_ns_safe(s, n, _n) |
| 661 |
__nvme_free_ns(n); |
| 662 |
|
| 663 |
free(s->name); |
| 664 |
free(s->sysfs_dir); |
| 665 |
free(s->subsysnqn); |
| 666 |
free(s->model); |
| 667 |
free(s->serial); |
| 668 |
free(s->firmware); |
| 669 |
free(s->subsystype); |
| 670 |
free(s->application); |
| 671 |
free(s->iopolicy); |
| 672 |
free(s); |
| 673 |
} |
| 674 |
|
| 675 |
void nvme_subsystem_release_fds(struct nvme_subsystem *s) |
| 676 |
{ |
| 677 |
struct nvme_ctrl *c, *_c; |
| 678 |
struct nvme_ns *n, *_n; |
| 679 |
|
| 680 |
nvme_subsystem_for_each_ctrl_safe(s, c, _c) |
| 681 |
nvme_ctrl_release_fd(c); |
| 682 |
|
| 683 |
nvme_subsystem_for_each_ns_safe(s, n, _n) |
| 684 |
nvme_ns_release_fd(n); |
| 685 |
} |
| 686 |
|
| 687 |
/* |
| 688 |
* Stub for SWIG |
| 689 |
*/ |
| 690 |
void nvme_free_subsystem(nvme_subsystem_t s) |
| 691 |
{ |
| 692 |
} |
| 693 |
|
| 694 |
struct nvme_subsystem *nvme_alloc_subsystem(struct nvme_host *h, |
| 695 |
const char *name, |
| 696 |
const char *subsysnqn) |
| 697 |
{ |
| 698 |
struct nvme_subsystem *s; |
| 699 |
|
| 700 |
s = calloc(1, sizeof(*s)); |
| 701 |
if (!s) |
| 702 |
return NULL; |
| 703 |
|
| 704 |
s->h = h; |
| 705 |
s->subsysnqn = strdup(subsysnqn); |
| 706 |
if (name) |
| 707 |
nvme_init_subsystem(s, name); |
| 708 |
list_head_init(&s->ctrls); |
| 709 |
list_head_init(&s->namespaces); |
| 710 |
list_node_init(&s->entry); |
| 711 |
list_add_tail(&h->subsystems, &s->entry); |
| 712 |
h->r->modified = true; |
| 713 |
return s; |
| 714 |
} |
| 715 |
|
| 716 |
struct nvme_subsystem *nvme_lookup_subsystem(struct nvme_host *h, |
| 717 |
const char *name, |
| 718 |
const char *subsysnqn) |
| 719 |
{ |
| 720 |
struct nvme_subsystem *s; |
| 721 |
|
| 722 |
nvme_for_each_subsystem(h, s) { |
| 723 |
if (subsysnqn && s->subsysnqn && |
| 724 |
strcmp(s->subsysnqn, subsysnqn)) |
| 725 |
continue; |
| 726 |
if (name && s->name && |
| 727 |
strcmp(s->name, name)) |
| 728 |
continue; |
| 729 |
if (h->r->application) { |
| 730 |
if (!s->application) |
| 731 |
continue; |
| 732 |
if (strcmp(h->r->application, s->application)) |
| 733 |
continue; |
| 734 |
} |
| 735 |
return s; |
| 736 |
} |
| 737 |
return nvme_alloc_subsystem(h, name, subsysnqn); |
| 738 |
} |
| 739 |
|
| 740 |
static void __nvme_free_host(struct nvme_host *h) |
| 741 |
{ |
| 742 |
struct nvme_subsystem *s, *_s; |
| 743 |
|
| 744 |
list_del_init(&h->entry); |
| 745 |
nvme_for_each_subsystem_safe(h, s, _s) |
| 746 |
__nvme_free_subsystem(s); |
| 747 |
free(h->hostnqn); |
| 748 |
free(h->hostid); |
| 749 |
free(h->dhchap_key); |
| 750 |
nvme_host_set_hostsymname(h, NULL); |
| 751 |
h->r->modified = true; |
| 752 |
free(h); |
| 753 |
} |
| 754 |
|
| 755 |
void nvme_host_release_fds(struct nvme_host *h) |
| 756 |
{ |
| 757 |
struct nvme_subsystem *s, *_s; |
| 758 |
|
| 759 |
nvme_for_each_subsystem_safe(h, s, _s) |
| 760 |
nvme_subsystem_release_fds(s); |
| 761 |
} |
| 762 |
|
| 763 |
/* Stub for SWIG */ |
| 764 |
void nvme_free_host(struct nvme_host *h) |
| 765 |
{ |
| 766 |
__nvme_free_host(h); |
| 767 |
} |
| 768 |
|
| 769 |
struct nvme_host *nvme_lookup_host(nvme_root_t r, const char *hostnqn, |
| 770 |
const char *hostid) |
| 771 |
{ |
| 772 |
struct nvme_host *h; |
| 773 |
|
| 774 |
if (!hostnqn) |
| 775 |
return NULL; |
| 776 |
nvme_for_each_host(r, h) { |
| 777 |
if (strcmp(h->hostnqn, hostnqn)) |
| 778 |
continue; |
| 779 |
if (hostid && (!h->hostid || |
| 780 |
strcmp(h->hostid, hostid))) |
| 781 |
continue; |
| 782 |
return h; |
| 783 |
} |
| 784 |
h = calloc(1,sizeof(*h)); |
| 785 |
if (!h) |
| 786 |
return NULL; |
| 787 |
h->hostnqn = strdup(hostnqn); |
| 788 |
if (hostid) |
| 789 |
h->hostid = strdup(hostid); |
| 790 |
list_head_init(&h->subsystems); |
| 791 |
list_node_init(&h->entry); |
| 792 |
h->r = r; |
| 793 |
list_add_tail(&r->hosts, &h->entry); |
| 794 |
r->modified = true; |
| 795 |
|
| 796 |
return h; |
| 797 |
} |
| 798 |
|
| 799 |
static int nvme_subsystem_scan_namespaces(nvme_root_t r, nvme_subsystem_t s) |
| 800 |
{ |
| 801 |
_cleanup_dirents_ struct dirents namespaces = {}; |
| 802 |
int i, ret; |
| 803 |
|
| 804 |
if (r->create_only) { |
| 805 |
nvme_msg(r, LOG_DEBUG, |
| 806 |
"skipping namespace scan for subsys %s\n", |
| 807 |
s->subsysnqn); |
| 808 |
return 0; |
| 809 |
} |
| 810 |
namespaces.num = nvme_scan_subsystem_namespaces(s, &namespaces.ents); |
| 811 |
if (namespaces.num < 0) { |
| 812 |
nvme_msg(r, LOG_DEBUG, |
| 813 |
"failed to scan namespaces for subsys %s: %s\n", |
| 814 |
s->subsysnqn, strerror(errno)); |
| 815 |
return namespaces.num; |
| 816 |
} |
| 817 |
|
| 818 |
for (i = 0; i < namespaces.num; i++) { |
| 819 |
ret = nvme_subsystem_scan_namespace(r, s, |
| 820 |
namespaces.ents[i]->d_name); |
| 821 |
if (ret < 0) |
| 822 |
nvme_msg(r, LOG_DEBUG, |
| 823 |
"failed to scan namespace %s: %s\n", |
| 824 |
namespaces.ents[i]->d_name, strerror(errno)); |
| 825 |
} |
| 826 |
|
| 827 |
return 0; |
| 828 |
} |
| 829 |
|
| 830 |
static int nvme_init_subsystem(nvme_subsystem_t s, const char *name) |
| 831 |
{ |
| 832 |
char *path; |
| 833 |
|
| 834 |
if (asprintf(&path, "%s/%s", nvme_subsys_sysfs_dir(), name) < 0) |
| 835 |
return -1; |
| 836 |
|
| 837 |
s->model = nvme_get_attr(path, "model"); |
| 838 |
if (!s->model) |
| 839 |
s->model = strdup("undefined"); |
| 840 |
s->serial = nvme_get_attr(path, "serial"); |
| 841 |
s->firmware = nvme_get_attr(path, "firmware_rev"); |
| 842 |
s->subsystype = nvme_get_attr(path, "subsystype"); |
| 843 |
if (!s->subsystype) { |
| 844 |
if (!strcmp(s->subsysnqn, NVME_DISC_SUBSYS_NAME)) |
| 845 |
s->subsystype = strdup("discovery"); |
| 846 |
else |
| 847 |
s->subsystype = strdup("nvm"); |
| 848 |
} |
| 849 |
s->name = strdup(name); |
| 850 |
s->sysfs_dir = (char *)path; |
| 851 |
if (s->h->r->application) |
| 852 |
s->application = strdup(s->h->r->application); |
| 853 |
s->iopolicy = nvme_get_attr(path, "iopolicy"); |
| 854 |
|
| 855 |
return 0; |
| 856 |
} |
| 857 |
|
| 858 |
static int nvme_scan_subsystem(struct nvme_root *r, const char *name) |
| 859 |
{ |
| 860 |
struct nvme_subsystem *s = NULL, *_s; |
| 861 |
_cleanup_free_ char *path = NULL, *subsysnqn = NULL; |
| 862 |
nvme_host_t h = NULL; |
| 863 |
int ret; |
| 864 |
|
| 865 |
nvme_msg(r, LOG_DEBUG, "scan subsystem %s\n", name); |
| 866 |
ret = asprintf(&path, "%s/%s", nvme_subsys_sysfs_dir(), name); |
| 867 |
if (ret < 0) |
| 868 |
return ret; |
| 869 |
|
| 870 |
subsysnqn = nvme_get_attr(path, "subsysnqn"); |
| 871 |
if (!subsysnqn) { |
| 872 |
errno = ENODEV; |
| 873 |
return -1; |
| 874 |
} |
| 875 |
nvme_for_each_host(r, h) { |
| 876 |
nvme_for_each_subsystem(h, _s) { |
| 877 |
/* |
| 878 |
* We are always called after nvme_scan_ctrl(), |
| 879 |
* so any subsystem we're interested at _must_ |
| 880 |
* have a name. |
| 881 |
*/ |
| 882 |
if (!_s->name) |
| 883 |
continue; |
| 884 |
if (strcmp(_s->name, name)) |
| 885 |
continue; |
| 886 |
if (!nvme_subsystem_scan_namespaces(r, _s)) { |
| 887 |
errno = EINVAL; |
| 888 |
return -1; |
| 889 |
} |
| 890 |
s = _s; |
| 891 |
} |
| 892 |
} |
| 893 |
if (!s) { |
| 894 |
/* |
| 895 |
* Subsystem with non-matching controller. odd. |
| 896 |
* Create a subsystem with the default host |
| 897 |
* and hope for the best. |
| 898 |
*/ |
| 899 |
nvme_msg(r, LOG_DEBUG, "creating detached subsystem '%s'\n", |
| 900 |
name); |
| 901 |
h = nvme_default_host(r); |
| 902 |
s = nvme_alloc_subsystem(h, name, subsysnqn); |
| 903 |
if (!s) { |
| 904 |
errno = ENOMEM; |
| 905 |
return -1; |
| 906 |
} |
| 907 |
if (!nvme_subsystem_scan_namespaces(r, s)) { |
| 908 |
errno = EINVAL; |
| 909 |
return -1; |
| 910 |
} |
| 911 |
} else if (strcmp(s->subsysnqn, subsysnqn)) { |
| 912 |
nvme_msg(r, LOG_DEBUG, "NQN mismatch for subsystem '%s'\n", |
| 913 |
name); |
| 914 |
errno = EINVAL; |
| 915 |
return -1; |
| 916 |
} |
| 917 |
|
| 918 |
return 0; |
| 919 |
} |
| 920 |
|
| 921 |
nvme_ctrl_t nvme_path_get_ctrl(nvme_path_t p) |
| 922 |
{ |
| 923 |
return p->c; |
| 924 |
} |
| 925 |
|
| 926 |
nvme_ns_t nvme_path_get_ns(nvme_path_t p) |
| 927 |
{ |
| 928 |
return p->n; |
| 929 |
} |
| 930 |
|
| 931 |
const char *nvme_path_get_sysfs_dir(nvme_path_t p) |
| 932 |
{ |
| 933 |
return p->sysfs_dir; |
| 934 |
} |
| 935 |
|
| 936 |
const char *nvme_path_get_name(nvme_path_t p) |
| 937 |
{ |
| 938 |
return p->name; |
| 939 |
} |
| 940 |
|
| 941 |
int nvme_path_get_queue_depth(nvme_path_t p) |
| 942 |
{ |
| 943 |
_cleanup_free_ char *queue_depth = NULL; |
| 944 |
|
| 945 |
queue_depth = nvme_get_path_attr(p, "queue_depth"); |
| 946 |
if (queue_depth) { |
| 947 |
sscanf(queue_depth, "%d", &p->queue_depth); |
| 948 |
} |
| 949 |
|
| 950 |
return p->queue_depth; |
| 951 |
} |
| 952 |
|
| 953 |
const char *nvme_path_get_ana_state(nvme_path_t p) |
| 954 |
{ |
| 955 |
return p->ana_state; |
| 956 |
} |
| 957 |
|
| 958 |
const char *nvme_path_get_numa_nodes(nvme_path_t p) |
| 959 |
{ |
| 960 |
return p->numa_nodes; |
| 961 |
} |
| 962 |
|
| 963 |
void nvme_free_path(struct nvme_path *p) |
| 964 |
{ |
| 965 |
list_del_init(&p->entry); |
| 966 |
list_del_init(&p->nentry); |
| 967 |
free(p->name); |
| 968 |
free(p->sysfs_dir); |
| 969 |
free(p->ana_state); |
| 970 |
free(p->numa_nodes); |
| 971 |
free(p); |
| 972 |
} |
| 973 |
|
| 974 |
static int nvme_ctrl_scan_path(nvme_root_t r, struct nvme_ctrl *c, char *name) |
| 975 |
{ |
| 976 |
struct nvme_path *p; |
| 977 |
_cleanup_free_ char *path = NULL, *grpid = NULL, *queue_depth = NULL; |
| 978 |
int ret; |
| 979 |
|
| 980 |
nvme_msg(r, LOG_DEBUG, "scan controller %s path %s\n", |
| 981 |
c->name, name); |
| 982 |
if (!c->s) { |
| 983 |
errno = ENXIO; |
| 984 |
return -1; |
| 985 |
} |
| 986 |
ret = asprintf(&path, "%s/%s", c->sysfs_dir, name); |
| 987 |
if (ret < 0) { |
| 988 |
errno = ENOMEM; |
| 989 |
return -1; |
| 990 |
} |
| 991 |
|
| 992 |
p = calloc(1, sizeof(*p)); |
| 993 |
if (!p) { |
| 994 |
errno = ENOMEM; |
| 995 |
return -1; |
| 996 |
} |
| 997 |
|
| 998 |
p->c = c; |
| 999 |
p->name = strdup(name); |
| 1000 |
p->sysfs_dir = path; |
| 1001 |
path = NULL; |
| 1002 |
p->ana_state = nvme_get_path_attr(p, "ana_state"); |
| 1003 |
if (!p->ana_state) |
| 1004 |
p->ana_state = strdup("optimized"); |
| 1005 |
|
| 1006 |
p->numa_nodes = nvme_get_path_attr(p, "numa_nodes"); |
| 1007 |
if (!p->numa_nodes) |
| 1008 |
p->numa_nodes = strdup("-1"); |
| 1009 |
|
| 1010 |
grpid = nvme_get_path_attr(p, "ana_grpid"); |
| 1011 |
if (grpid) { |
| 1012 |
sscanf(grpid, "%d", &p->grpid); |
| 1013 |
} |
| 1014 |
|
| 1015 |
queue_depth = nvme_get_path_attr(p, "queue_depth"); |
| 1016 |
if (queue_depth) { |
| 1017 |
sscanf(queue_depth, "%d", &p->queue_depth); |
| 1018 |
} |
| 1019 |
|
| 1020 |
list_node_init(&p->nentry); |
| 1021 |
list_node_init(&p->entry); |
| 1022 |
list_add_tail(&c->paths, &p->entry); |
| 1023 |
return 0; |
| 1024 |
} |
| 1025 |
|
| 1026 |
int nvme_ctrl_get_fd(nvme_ctrl_t c) |
| 1027 |
{ |
| 1028 |
if (c->fd < 0) { |
| 1029 |
c->fd = nvme_open(c->name); |
| 1030 |
if (c->fd < 0) |
| 1031 |
nvme_msg(root_from_ctrl(c), LOG_ERR, |
| 1032 |
"Failed to open ctrl %s, errno %d\n", |
| 1033 |
c->name, errno); |
| 1034 |
} |
| 1035 |
return c->fd; |
| 1036 |
} |
| 1037 |
|
| 1038 |
void nvme_ctrl_release_fd(nvme_ctrl_t c) |
| 1039 |
{ |
| 1040 |
if (c->fd < 0) |
| 1041 |
return; |
| 1042 |
|
| 1043 |
close(c->fd); |
| 1044 |
c->fd = -1; |
| 1045 |
} |
| 1046 |
|
| 1047 |
nvme_subsystem_t nvme_ctrl_get_subsystem(nvme_ctrl_t c) |
| 1048 |
{ |
| 1049 |
return c->s; |
| 1050 |
} |
| 1051 |
|
| 1052 |
const char *nvme_ctrl_get_name(nvme_ctrl_t c) |
| 1053 |
{ |
| 1054 |
return c->name; |
| 1055 |
} |
| 1056 |
|
| 1057 |
const char *nvme_ctrl_get_sysfs_dir(nvme_ctrl_t c) |
| 1058 |
{ |
| 1059 |
return c->sysfs_dir; |
| 1060 |
} |
| 1061 |
|
| 1062 |
const char *nvme_ctrl_get_subsysnqn(nvme_ctrl_t c) |
| 1063 |
{ |
| 1064 |
return c->s ? c->s->subsysnqn : c->subsysnqn; |
| 1065 |
} |
| 1066 |
|
| 1067 |
const char *nvme_ctrl_get_address(nvme_ctrl_t c) |
| 1068 |
{ |
| 1069 |
return c->address ? c->address : ""; |
| 1070 |
} |
| 1071 |
|
| 1072 |
char *nvme_ctrl_get_src_addr(nvme_ctrl_t c, char *src_addr, size_t src_addr_len) |
| 1073 |
{ |
| 1074 |
size_t l; |
| 1075 |
char *p; |
| 1076 |
|
| 1077 |
if (!c->address) |
| 1078 |
return NULL; |
| 1079 |
|
| 1080 |
p = strstr(c->address, "src_addr="); |
| 1081 |
if (!p) |
| 1082 |
return NULL; |
| 1083 |
|
| 1084 |
p += strlen("src_addr="); |
| 1085 |
l = strcspn(p, ",%"); /* % to eliminate IPv6 scope (if present) */ |
| 1086 |
if (l >= src_addr_len) { |
| 1087 |
nvme_msg(root_from_ctrl(c), LOG_ERR, |
| 1088 |
"Buffer for src_addr is too small (%zu must be > %zu)\n", |
| 1089 |
src_addr_len, l); |
| 1090 |
return NULL; |
| 1091 |
} |
| 1092 |
|
| 1093 |
strncpy(src_addr, p, l); |
| 1094 |
src_addr[l] = '\0'; |
| 1095 |
return src_addr; |
| 1096 |
} |
| 1097 |
|
| 1098 |
const char *nvme_ctrl_get_phy_slot(nvme_ctrl_t c) |
| 1099 |
{ |
| 1100 |
return c->phy_slot ? c->phy_slot : ""; |
| 1101 |
} |
| 1102 |
|
| 1103 |
const char *nvme_ctrl_get_firmware(nvme_ctrl_t c) |
| 1104 |
{ |
| 1105 |
return c->firmware; |
| 1106 |
} |
| 1107 |
|
| 1108 |
const char *nvme_ctrl_get_model(nvme_ctrl_t c) |
| 1109 |
{ |
| 1110 |
return c->model; |
| 1111 |
} |
| 1112 |
|
| 1113 |
const char *nvme_ctrl_get_state(nvme_ctrl_t c) |
| 1114 |
{ |
| 1115 |
char *state = c->state; |
| 1116 |
|
| 1117 |
c->state = nvme_get_ctrl_attr(c, "state"); |
| 1118 |
free(state); |
| 1119 |
return c->state; |
| 1120 |
} |
| 1121 |
|
| 1122 |
const char *nvme_ctrl_get_numa_node(nvme_ctrl_t c) |
| 1123 |
{ |
| 1124 |
return c->numa_node; |
| 1125 |
} |
| 1126 |
|
| 1127 |
const char *nvme_ctrl_get_queue_count(nvme_ctrl_t c) |
| 1128 |
{ |
| 1129 |
return c->queue_count; |
| 1130 |
} |
| 1131 |
|
| 1132 |
const char *nvme_ctrl_get_serial(nvme_ctrl_t c) |
| 1133 |
{ |
| 1134 |
return c->serial; |
| 1135 |
} |
| 1136 |
|
| 1137 |
const char *nvme_ctrl_get_sqsize(nvme_ctrl_t c) |
| 1138 |
{ |
| 1139 |
return c->sqsize; |
| 1140 |
} |
| 1141 |
|
| 1142 |
const char *nvme_ctrl_get_transport(nvme_ctrl_t c) |
| 1143 |
{ |
| 1144 |
return c->transport; |
| 1145 |
} |
| 1146 |
|
| 1147 |
const char *nvme_ctrl_get_traddr(nvme_ctrl_t c) |
| 1148 |
{ |
| 1149 |
return c->traddr; |
| 1150 |
} |
| 1151 |
|
| 1152 |
const char *nvme_ctrl_get_trsvcid(nvme_ctrl_t c) |
| 1153 |
{ |
| 1154 |
return c->trsvcid; |
| 1155 |
} |
| 1156 |
|
| 1157 |
const char *nvme_ctrl_get_host_traddr(nvme_ctrl_t c) |
| 1158 |
{ |
| 1159 |
return c->cfg.host_traddr; |
| 1160 |
} |
| 1161 |
|
| 1162 |
const char *nvme_ctrl_get_host_iface(nvme_ctrl_t c) |
| 1163 |
{ |
| 1164 |
return c->cfg.host_iface; |
| 1165 |
} |
| 1166 |
|
| 1167 |
struct nvme_fabrics_config *nvme_ctrl_get_config(nvme_ctrl_t c) |
| 1168 |
{ |
| 1169 |
return &c->cfg; |
| 1170 |
} |
| 1171 |
|
| 1172 |
const char *nvme_ctrl_get_dhchap_host_key(nvme_ctrl_t c) |
| 1173 |
{ |
| 1174 |
return c->dhchap_key; |
| 1175 |
} |
| 1176 |
|
| 1177 |
const char *nvme_ctrl_get_cntlid(nvme_ctrl_t c) |
| 1178 |
{ |
| 1179 |
return c->cntlid; |
| 1180 |
} |
| 1181 |
|
| 1182 |
void nvme_ctrl_set_dhchap_host_key(nvme_ctrl_t c, const char *key) |
| 1183 |
{ |
| 1184 |
free(c->dhchap_key); |
| 1185 |
c->dhchap_key = NULL; |
| 1186 |
|
| 1187 |
if (key) |
| 1188 |
c->dhchap_key = strdup(key); |
| 1189 |
} |
| 1190 |
|
| 1191 |
const char *nvme_ctrl_get_dhchap_key(nvme_ctrl_t c) |
| 1192 |
{ |
| 1193 |
return c->dhchap_ctrl_key; |
| 1194 |
} |
| 1195 |
|
| 1196 |
void nvme_ctrl_set_dhchap_key(nvme_ctrl_t c, const char *key) |
| 1197 |
{ |
| 1198 |
free(c->dhchap_ctrl_key); |
| 1199 |
c->dhchap_ctrl_key = NULL; |
| 1200 |
|
| 1201 |
if (key) |
| 1202 |
c->dhchap_ctrl_key = strdup(key); |
| 1203 |
} |
| 1204 |
|
| 1205 |
const char *nvme_ctrl_get_keyring(nvme_ctrl_t c) |
| 1206 |
{ |
| 1207 |
return c->keyring; |
| 1208 |
} |
| 1209 |
|
| 1210 |
void nvme_ctrl_set_keyring(nvme_ctrl_t c, const char *keyring) |
| 1211 |
{ |
| 1212 |
free(c->keyring); |
| 1213 |
c->keyring = NULL; |
| 1214 |
|
| 1215 |
if (keyring) |
| 1216 |
c->keyring = strdup(keyring); |
| 1217 |
} |
| 1218 |
|
| 1219 |
const char *nvme_ctrl_get_tls_key_identity(nvme_ctrl_t c) |
| 1220 |
{ |
| 1221 |
return c->tls_key_identity; |
| 1222 |
} |
| 1223 |
|
| 1224 |
void nvme_ctrl_set_tls_key_identity(nvme_ctrl_t c, const char *identity) |
| 1225 |
{ |
| 1226 |
free(c->tls_key_identity); |
| 1227 |
c->tls_key_identity = NULL; |
| 1228 |
|
| 1229 |
if (identity) |
| 1230 |
c->tls_key_identity = strdup(identity); |
| 1231 |
} |
| 1232 |
|
| 1233 |
const char *nvme_ctrl_get_tls_key(nvme_ctrl_t c) |
| 1234 |
{ |
| 1235 |
return c->tls_key; |
| 1236 |
} |
| 1237 |
|
| 1238 |
void nvme_ctrl_set_tls_key(nvme_ctrl_t c, const char *key) |
| 1239 |
{ |
| 1240 |
free(c->tls_key); |
| 1241 |
c->tls_key = NULL; |
| 1242 |
|
| 1243 |
if (key) |
| 1244 |
c->tls_key = strdup(key); |
| 1245 |
} |
| 1246 |
|
| 1247 |
void nvme_ctrl_set_discovered(nvme_ctrl_t c, bool discovered) |
| 1248 |
{ |
| 1249 |
c->discovered = discovered; |
| 1250 |
} |
| 1251 |
|
| 1252 |
bool nvme_ctrl_is_discovered(nvme_ctrl_t c) |
| 1253 |
{ |
| 1254 |
return c->discovered; |
| 1255 |
} |
| 1256 |
|
| 1257 |
void nvme_ctrl_set_persistent(nvme_ctrl_t c, bool persistent) |
| 1258 |
{ |
| 1259 |
c->persistent = persistent; |
| 1260 |
} |
| 1261 |
|
| 1262 |
bool nvme_ctrl_is_persistent(nvme_ctrl_t c) |
| 1263 |
{ |
| 1264 |
return c->persistent; |
| 1265 |
} |
| 1266 |
|
| 1267 |
void nvme_ctrl_set_discovery_ctrl(nvme_ctrl_t c, bool discovery) |
| 1268 |
{ |
| 1269 |
c->discovery_ctrl = discovery; |
| 1270 |
} |
| 1271 |
|
| 1272 |
bool nvme_ctrl_is_discovery_ctrl(nvme_ctrl_t c) |
| 1273 |
{ |
| 1274 |
return c->discovery_ctrl; |
| 1275 |
} |
| 1276 |
|
| 1277 |
void nvme_ctrl_set_unique_discovery_ctrl(nvme_ctrl_t c, bool unique) |
| 1278 |
{ |
| 1279 |
c->unique_discovery_ctrl = unique; |
| 1280 |
} |
| 1281 |
|
| 1282 |
bool nvme_ctrl_is_unique_discovery_ctrl(nvme_ctrl_t c) |
| 1283 |
{ |
| 1284 |
return c->unique_discovery_ctrl; |
| 1285 |
} |
| 1286 |
|
| 1287 |
int nvme_ctrl_identify(nvme_ctrl_t c, struct nvme_id_ctrl *id) |
| 1288 |
{ |
| 1289 |
return nvme_identify_ctrl(nvme_ctrl_get_fd(c), id); |
| 1290 |
} |
| 1291 |
|
| 1292 |
nvme_ns_t nvme_ctrl_first_ns(nvme_ctrl_t c) |
| 1293 |
{ |
| 1294 |
return list_top(&c->namespaces, struct nvme_ns, entry); |
| 1295 |
} |
| 1296 |
|
| 1297 |
nvme_ns_t nvme_ctrl_next_ns(nvme_ctrl_t c, nvme_ns_t n) |
| 1298 |
{ |
| 1299 |
return n ? list_next(&c->namespaces, n, entry) : NULL; |
| 1300 |
} |
| 1301 |
|
| 1302 |
nvme_path_t nvme_ctrl_first_path(nvme_ctrl_t c) |
| 1303 |
{ |
| 1304 |
return list_top(&c->paths, struct nvme_path, entry); |
| 1305 |
} |
| 1306 |
|
| 1307 |
nvme_path_t nvme_ctrl_next_path(nvme_ctrl_t c, nvme_path_t p) |
| 1308 |
{ |
| 1309 |
return p ? list_next(&c->paths, p, entry) : NULL; |
| 1310 |
} |
| 1311 |
|
| 1312 |
#define FREE_CTRL_ATTR(a) \ |
| 1313 |
do { free(a); (a) = NULL; } while (0) |
| 1314 |
void nvme_deconfigure_ctrl(nvme_ctrl_t c) |
| 1315 |
{ |
| 1316 |
nvme_ctrl_release_fd(c); |
| 1317 |
FREE_CTRL_ATTR(c->name); |
| 1318 |
FREE_CTRL_ATTR(c->sysfs_dir); |
| 1319 |
FREE_CTRL_ATTR(c->firmware); |
| 1320 |
FREE_CTRL_ATTR(c->model); |
| 1321 |
FREE_CTRL_ATTR(c->state); |
| 1322 |
FREE_CTRL_ATTR(c->numa_node); |
| 1323 |
FREE_CTRL_ATTR(c->queue_count); |
| 1324 |
FREE_CTRL_ATTR(c->serial); |
| 1325 |
FREE_CTRL_ATTR(c->sqsize); |
| 1326 |
FREE_CTRL_ATTR(c->dhchap_key); |
| 1327 |
FREE_CTRL_ATTR(c->dhchap_ctrl_key); |
| 1328 |
FREE_CTRL_ATTR(c->keyring); |
| 1329 |
FREE_CTRL_ATTR(c->tls_key_identity); |
| 1330 |
FREE_CTRL_ATTR(c->tls_key); |
| 1331 |
FREE_CTRL_ATTR(c->address); |
| 1332 |
FREE_CTRL_ATTR(c->dctype); |
| 1333 |
FREE_CTRL_ATTR(c->cntrltype); |
| 1334 |
FREE_CTRL_ATTR(c->cntlid); |
| 1335 |
FREE_CTRL_ATTR(c->phy_slot); |
| 1336 |
} |
| 1337 |
|
| 1338 |
int nvme_disconnect_ctrl(nvme_ctrl_t c) |
| 1339 |
{ |
| 1340 |
nvme_root_t r = c->s && c->s->h ? c->s->h->r : NULL; |
| 1341 |
int ret; |
| 1342 |
|
| 1343 |
ret = nvme_set_attr(nvme_ctrl_get_sysfs_dir(c), |
| 1344 |
"delete_controller", "1"); |
| 1345 |
if (ret < 0) { |
| 1346 |
nvme_msg(r, LOG_ERR, "%s: failed to disconnect, error %d\n", |
| 1347 |
c->name, errno); |
| 1348 |
return ret; |
| 1349 |
} |
| 1350 |
nvme_msg(r, LOG_INFO, "%s: %s disconnected\n", c->name, c->subsysnqn); |
| 1351 |
nvme_deconfigure_ctrl(c); |
| 1352 |
return 0; |
| 1353 |
} |
| 1354 |
|
| 1355 |
void nvme_unlink_ctrl(nvme_ctrl_t c) |
| 1356 |
{ |
| 1357 |
list_del_init(&c->entry); |
| 1358 |
c->s = NULL; |
| 1359 |
} |
| 1360 |
|
| 1361 |
static void __nvme_free_ctrl(nvme_ctrl_t c) |
| 1362 |
{ |
| 1363 |
struct nvme_path *p, *_p; |
| 1364 |
struct nvme_ns *n, *_n; |
| 1365 |
|
| 1366 |
nvme_unlink_ctrl(c); |
| 1367 |
|
| 1368 |
nvme_ctrl_for_each_path_safe(c, p, _p) |
| 1369 |
nvme_free_path(p); |
| 1370 |
|
| 1371 |
nvme_ctrl_for_each_ns_safe(c, n, _n) |
| 1372 |
__nvme_free_ns(n); |
| 1373 |
|
| 1374 |
nvme_deconfigure_ctrl(c); |
| 1375 |
|
| 1376 |
FREE_CTRL_ATTR(c->transport); |
| 1377 |
FREE_CTRL_ATTR(c->subsysnqn); |
| 1378 |
FREE_CTRL_ATTR(c->traddr); |
| 1379 |
FREE_CTRL_ATTR(c->cfg.host_traddr); |
| 1380 |
FREE_CTRL_ATTR(c->cfg.host_iface); |
| 1381 |
FREE_CTRL_ATTR(c->trsvcid); |
| 1382 |
free(c); |
| 1383 |
} |
| 1384 |
|
| 1385 |
void nvme_free_ctrl(nvme_ctrl_t c) |
| 1386 |
{ |
| 1387 |
__nvme_free_ctrl(c); |
| 1388 |
} |
| 1389 |
|
| 1390 |
static bool traddr_is_hostname(const char *transport, const char *traddr) |
| 1391 |
{ |
| 1392 |
char addrstr[NVMF_TRADDR_SIZE]; |
| 1393 |
|
| 1394 |
if (!traddr || !transport) |
| 1395 |
return false; |
| 1396 |
if (!strcmp(traddr, "none")) |
| 1397 |
return false; |
| 1398 |
if (strcmp(transport, "tcp") && |
| 1399 |
strcmp(transport, "rdma")) |
| 1400 |
return false; |
| 1401 |
if (inet_pton(AF_INET, traddr, addrstr) > 0 || |
| 1402 |
inet_pton(AF_INET6, traddr, addrstr) > 0) |
| 1403 |
return false; |
| 1404 |
return true; |
| 1405 |
} |
| 1406 |
|
| 1407 |
struct nvme_ctrl *nvme_create_ctrl(nvme_root_t r, |
| 1408 |
const char *subsysnqn, const char *transport, |
| 1409 |
const char *traddr, const char *host_traddr, |
| 1410 |
const char *host_iface, const char *trsvcid) |
| 1411 |
{ |
| 1412 |
struct nvme_ctrl *c; |
| 1413 |
|
| 1414 |
if (!transport) { |
| 1415 |
nvme_msg(r, LOG_ERR, "No transport specified\n"); |
| 1416 |
errno = EINVAL; |
| 1417 |
return NULL; |
| 1418 |
} |
| 1419 |
if (strncmp(transport, "loop", 4) && |
| 1420 |
strncmp(transport, "pcie", 4) && |
| 1421 |
strncmp(transport, "apple-nvme", 10) && !traddr) { |
| 1422 |
nvme_msg(r, LOG_ERR, "No transport address for '%s'\n", |
| 1423 |
transport); |
| 1424 |
errno = EINVAL; |
| 1425 |
return NULL; |
| 1426 |
} |
| 1427 |
if (!subsysnqn) { |
| 1428 |
nvme_msg(r, LOG_ERR, "No subsystem NQN specified\n"); |
| 1429 |
errno = EINVAL; |
| 1430 |
return NULL; |
| 1431 |
} |
| 1432 |
c = calloc(1, sizeof(*c)); |
| 1433 |
if (!c) { |
| 1434 |
errno = ENOMEM; |
| 1435 |
return NULL; |
| 1436 |
} |
| 1437 |
c->fd = -1; |
| 1438 |
nvmf_default_config(&c->cfg); |
| 1439 |
list_head_init(&c->namespaces); |
| 1440 |
list_head_init(&c->paths); |
| 1441 |
list_node_init(&c->entry); |
| 1442 |
c->transport = strdup(transport); |
| 1443 |
c->subsysnqn = strdup(subsysnqn); |
| 1444 |
if (traddr) |
| 1445 |
c->traddr = strdup(traddr); |
| 1446 |
if (host_traddr) { |
| 1447 |
if (traddr_is_hostname(transport, host_traddr)) |
| 1448 |
c->cfg.host_traddr = hostname2traddr(r, host_traddr); |
| 1449 |
if (!c->cfg.host_traddr) |
| 1450 |
c->cfg.host_traddr = strdup(host_traddr); |
| 1451 |
} |
| 1452 |
if (host_iface) |
| 1453 |
c->cfg.host_iface = strdup(host_iface); |
| 1454 |
if (trsvcid) |
| 1455 |
c->trsvcid = strdup(trsvcid); |
| 1456 |
|
| 1457 |
return c; |
| 1458 |
} |
| 1459 |
|
| 1460 |
/** |
| 1461 |
* _tcp_ctrl_match_host_traddr_no_src_addr() - Match host_traddr w/o src_addr |
| 1462 |
* @c: An existing controller instance |
| 1463 |
* @candidate: Candidate ctrl we're trying to match with @c. |
| 1464 |
* |
| 1465 |
* On kernels prior to 6.1 (i.e. src_addr is not available), try to match |
| 1466 |
* a candidate controller's host_traddr to that of an existing controller. |
| 1467 |
* |
| 1468 |
* This function takes an optimistic approach. In doubt, it will declare a |
| 1469 |
* match and return true. |
| 1470 |
* |
| 1471 |
* Return: true if @c->host_traddr matches @candidate->host_traddr. false otherwise. |
| 1472 |
*/ |
| 1473 |
static bool _tcp_ctrl_match_host_traddr_no_src_addr(struct nvme_ctrl *c, struct candidate_args *candidate) |
| 1474 |
{ |
| 1475 |
if (c->cfg.host_traddr) |
| 1476 |
return candidate->addreq(candidate->host_traddr, c->cfg.host_traddr); |
| 1477 |
|
| 1478 |
/* If c->cfg.host_traddr is NULL, then the controller (c) |
| 1479 |
* uses the interface's primary address as the source |
| 1480 |
* address. If c->cfg.host_iface is defined we can |
| 1481 |
* determine the primary address associated with that |
| 1482 |
* interface and compare that to the candidate->host_traddr. |
| 1483 |
*/ |
| 1484 |
if (c->cfg.host_iface) |
| 1485 |
return nvme_iface_primary_addr_matches(candidate->iface_list, |
| 1486 |
c->cfg.host_iface, |
| 1487 |
candidate->host_traddr); |
| 1488 |
|
| 1489 |
/* If both c->cfg.host_traddr and c->cfg.host_iface are |
| 1490 |
* NULL, we don't have enough information to make a |
| 1491 |
* 100% positive match. Regardless, let's be optimistic |
| 1492 |
* and assume that we have a match. |
| 1493 |
*/ |
| 1494 |
nvme_msg(root_from_ctrl(c), LOG_DEBUG, |
| 1495 |
"Not enough data, but assume %s matches candidate's host_traddr: %s\n", |
| 1496 |
nvme_ctrl_get_name(c), candidate->host_traddr); |
| 1497 |
|
| 1498 |
return true; |
| 1499 |
} |
| 1500 |
|
| 1501 |
/** |
| 1502 |
* _tcp_ctrl_match_host_iface_no_src_addr() - Match host_iface w/o src_addr |
| 1503 |
* @c: An existing controller instance |
| 1504 |
* @candidate: Candidate ctrl we're trying to match with @c. |
| 1505 |
* |
| 1506 |
* On kernels prior to 6.1 (i.e. src_addr is not available), try to match |
| 1507 |
* a candidate controller's host_iface to that of an existing controller. |
| 1508 |
* |
| 1509 |
* This function takes an optimistic approach. In doubt, it will declare a |
| 1510 |
* match and return true. |
| 1511 |
* |
| 1512 |
* Return: true if @c->host_iface matches @candidate->host_iface. false otherwise. |
| 1513 |
*/ |
| 1514 |
static bool _tcp_ctrl_match_host_iface_no_src_addr(struct nvme_ctrl *c, struct candidate_args *candidate) |
| 1515 |
{ |
| 1516 |
if (c->cfg.host_iface) |
| 1517 |
return streq0(candidate->host_iface, c->cfg.host_iface); |
| 1518 |
|
| 1519 |
/* If c->cfg.host_traddr is not NULL we can infer the controller's (c) |
| 1520 |
* interface from it and compare it to the candidate->host_iface. |
| 1521 |
*/ |
| 1522 |
if (c->cfg.host_traddr) { |
| 1523 |
const char *c_host_iface; |
| 1524 |
|
| 1525 |
c_host_iface = nvme_iface_matching_addr(candidate->iface_list, c->cfg.host_traddr); |
| 1526 |
return streq0(candidate->host_iface, c_host_iface); |
| 1527 |
} |
| 1528 |
|
| 1529 |
/* If both c->cfg.host_traddr and c->cfg.host_iface are |
| 1530 |
* NULL, we don't have enough information to make a |
| 1531 |
* 100% positive match. Regardless, let's be optimistic |
| 1532 |
* and assume that we have a match. |
| 1533 |
*/ |
| 1534 |
nvme_msg(root_from_ctrl(c), LOG_DEBUG, |
| 1535 |
"Not enough data, but assume %s matches candidate's host_iface: %s\n", |
| 1536 |
nvme_ctrl_get_name(c), candidate->host_iface); |
| 1537 |
|
| 1538 |
return true; |
| 1539 |
} |
| 1540 |
|
| 1541 |
/** |
| 1542 |
* _tcp_opt_params_match_no_src_addr() - Match optional host_traddr/host_iface w/o src_addr |
| 1543 |
* @c: An existing controller instance |
| 1544 |
* @candidate: Candidate ctrl we're trying to match with @c. |
| 1545 |
* |
| 1546 |
* Before kernel 6.1, the src_addr was not reported by the kernel which makes |
| 1547 |
* it hard to match a candidate's host_traddr and host_iface to an existing |
| 1548 |
* controller if that controller was created without specifying the |
| 1549 |
* host_traddr and/or host_iface. This function tries its best in the absense |
| 1550 |
* of a src_addr to match @c to @candidate. This may not be 100% accurate. |
| 1551 |
* Only the src_addr can provide 100% accuracy. |
| 1552 |
* |
| 1553 |
* This function takes an optimistic approach. In doubt, it will declare a |
| 1554 |
* match and return true. |
| 1555 |
* |
| 1556 |
* Return: true if @c matches @candidate. false otherwise. |
| 1557 |
*/ |
| 1558 |
static bool _tcp_opt_params_match_no_src_addr(struct nvme_ctrl *c, struct candidate_args *candidate) |
| 1559 |
{ |
| 1560 |
/* Check host_traddr only if candidate is interested */ |
| 1561 |
if (candidate->host_traddr) { |
| 1562 |
if (!_tcp_ctrl_match_host_traddr_no_src_addr(c, candidate)) |
| 1563 |
return false; |
| 1564 |
} |
| 1565 |
|
| 1566 |
/* Check host_iface only if candidate is interested */ |
| 1567 |
if (candidate->host_iface) { |
| 1568 |
if (!_tcp_ctrl_match_host_iface_no_src_addr(c, candidate)) |
| 1569 |
return false; |
| 1570 |
} |
| 1571 |
|
| 1572 |
return true; |
| 1573 |
} |
| 1574 |
|
| 1575 |
/** |
| 1576 |
* _tcp_opt_params_match() - Match optional host_traddr/host_iface |
| 1577 |
* @c: An existing controller instance |
| 1578 |
* @candidate: Candidate ctrl we're trying to match with @c. |
| 1579 |
* |
| 1580 |
* The host_traddr and host_iface are optional for TCP. When they are not |
| 1581 |
* specified, the kernel looks up the destination IP address (traddr) in the |
| 1582 |
* routing table to determine the best interface for the connection. The |
| 1583 |
* kernel then retrieves the primary IP address assigned to that interface |
| 1584 |
* and uses that as the connection’s source address. |
| 1585 |
* |
| 1586 |
* An interface’s primary address is the default source address used for |
| 1587 |
* all connections made on that interface unless host-traddr is used to |
| 1588 |
* override the default. Kernel-selected interfaces and/or source addresses |
| 1589 |
* are hidden from user-space applications unless the kernel makes that |
| 1590 |
* information available through the "src_addr" attribute in the |
| 1591 |
* sysfs (kernel 6.1 or later). |
| 1592 |
* |
| 1593 |
* Sometimes, an application may force the interface by specifying the |
| 1594 |
* "host-iface" or may force a different source address (instead of the |
| 1595 |
* primary address) by providing the "host-traddr". |
| 1596 |
* |
| 1597 |
* If the candidate specifies the host_traddr and/or host_iface but they |
| 1598 |
* do not match the existing controller's host_traddr and/or host_iface |
| 1599 |
* (they could be NULL), we may still be able to find a match by taking |
| 1600 |
* the existing controller's src_addr into consideration since that |
| 1601 |
* parameter identifies the actual source address of the connection and |
| 1602 |
* therefore can be used to infer the interface of the connection. However, |
| 1603 |
* the src_addr can only be read from the nvme device's sysfs "address" |
| 1604 |
* attribute starting with kernel 6.1 (or kernels that backported the |
| 1605 |
* src_addr patch). |
| 1606 |
* |
| 1607 |
* For legacy kernels that do not provide the src_addr we must use a |
| 1608 |
* different algorithm to match the host_traddr and host_iface, but |
| 1609 |
* it's not 100% accurate. |
| 1610 |
* |
| 1611 |
* Return: true if @c matches @candidate. false otherwise. |
| 1612 |
*/ |
| 1613 |
static bool _tcp_opt_params_match(struct nvme_ctrl *c, struct candidate_args *candidate) |
| 1614 |
{ |
| 1615 |
char *src_addr, buffer[INET6_ADDRSTRLEN]; |
| 1616 |
|
| 1617 |
/* Check if src_addr is available (kernel 6.1 or later) */ |
| 1618 |
src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer)); |
| 1619 |
if (!src_addr) |
| 1620 |
return _tcp_opt_params_match_no_src_addr(c, candidate); |
| 1621 |
|
| 1622 |
/* Check host_traddr only if candidate is interested */ |
| 1623 |
if (candidate->host_traddr && |
| 1624 |
!candidate->addreq(candidate->host_traddr, src_addr)) |
| 1625 |
return false; |
| 1626 |
|
| 1627 |
/* Check host_iface only if candidate is interested */ |
| 1628 |
if (candidate->host_iface && |
| 1629 |
!streq0(candidate->host_iface, |
| 1630 |
nvme_iface_matching_addr(candidate->iface_list, src_addr))) |
| 1631 |
return false; |
| 1632 |
|
| 1633 |
return true; |
| 1634 |
} |
| 1635 |
|
| 1636 |
/** |
| 1637 |
* _tcp_match_ctrl() - Check if controller matches candidate (TCP only) |
| 1638 |
* @c: An existing controller instance |
| 1639 |
* @candidate: Candidate ctrl we're trying to match with @c. |
| 1640 |
* |
| 1641 |
* We want to determine if an existing controller can be re-used |
| 1642 |
* for the candidate controller we're trying to instantiate. |
| 1643 |
* |
| 1644 |
* For TCP, we do not have a match if the candidate's transport, traddr, |
| 1645 |
* trsvcid are not identical to those of the the existing controller. |
| 1646 |
* These 3 parameters are mandatory for a match. |
| 1647 |
* |
| 1648 |
* The host_traddr and host_iface are optional. When the candidate does |
| 1649 |
* not specify them (both NULL), we can ignore them. Otherwise, we must |
| 1650 |
* employ advanced investigation techniques to determine if there's a match. |
| 1651 |
* |
| 1652 |
* Return: true if a match is found, false otherwise. |
| 1653 |
*/ |
| 1654 |
static bool _tcp_match_ctrl(struct nvme_ctrl *c, struct candidate_args *candidate) |
| 1655 |
{ |
| 1656 |
if (!streq0(c->transport, candidate->transport)) |
| 1657 |
return false; |
| 1658 |
|
| 1659 |
if (!streq0(c->trsvcid, candidate->trsvcid)) |
| 1660 |
return false; |
| 1661 |
|
| 1662 |
if (!candidate->addreq(c->traddr, candidate->traddr)) |
| 1663 |
return false; |
| 1664 |
|
| 1665 |
if (candidate->well_known_nqn && !nvme_ctrl_is_discovery_ctrl(c)) |
| 1666 |
return false; |
| 1667 |
|
| 1668 |
if (candidate->subsysnqn && !streq0(c->subsysnqn, candidate->subsysnqn)) |
| 1669 |
return false; |
| 1670 |
|
| 1671 |
/* Check host_traddr / host_iface only if candidate is interested */ |
| 1672 |
if ((candidate->host_iface || candidate->host_traddr) && |
| 1673 |
!_tcp_opt_params_match(c, candidate)) |
| 1674 |
return false; |
| 1675 |
|
| 1676 |
return true; |
| 1677 |
} |
| 1678 |
|
| 1679 |
/** |
| 1680 |
* _match_ctrl() - Check if controller matches candidate (non TCP transport) |
| 1681 |
* @c: An existing controller instance |
| 1682 |
* @candidate: Candidate ctrl we're trying to match with @c. |
| 1683 |
* |
| 1684 |
* We want to determine if an existing controller can be re-used |
| 1685 |
* for the candidate controller we're trying to instantiate. This function |
| 1686 |
* is used for all transports except TCP. |
| 1687 |
* |
| 1688 |
* Return: true if a match is found, false otherwise. |
| 1689 |
*/ |
| 1690 |
static bool _match_ctrl(struct nvme_ctrl *c, struct candidate_args *candidate) |
| 1691 |
{ |
| 1692 |
if (!streq0(c->transport, candidate->transport)) |
| 1693 |
return false; |
| 1694 |
|
| 1695 |
if (candidate->traddr && c->traddr && |
| 1696 |
!candidate->addreq(c->traddr, candidate->traddr)) |
| 1697 |
return false; |
| 1698 |
|
| 1699 |
if (candidate->host_traddr && c->cfg.host_traddr && |
| 1700 |
!candidate->addreq(c->cfg.host_traddr, candidate->host_traddr)) |
| 1701 |
return false; |
| 1702 |
|
| 1703 |
if (candidate->host_iface && c->cfg.host_iface && |
| 1704 |
!streq0(c->cfg.host_iface, candidate->host_iface)) |
| 1705 |
return false; |
| 1706 |
|
| 1707 |
if (candidate->trsvcid && c->trsvcid && |
| 1708 |
!streq0(c->trsvcid, candidate->trsvcid)) |
| 1709 |
return false; |
| 1710 |
|
| 1711 |
if (candidate->well_known_nqn && !nvme_ctrl_is_discovery_ctrl(c)) |
| 1712 |
return false; |
| 1713 |
|
| 1714 |
if (candidate->subsysnqn && !streq0(c->subsysnqn, candidate->subsysnqn)) |
| 1715 |
return false; |
| 1716 |
|
| 1717 |
return true; |
| 1718 |
} |
| 1719 |
/** |
| 1720 |
* _candidate_init() - Init candidate and get the matching function |
| 1721 |
* |
| 1722 |
* @candidate: Candidate struct to initialize |
| 1723 |
* @transport: Transport name |
| 1724 |
* @traddr: Transport address |
| 1725 |
* @trsvcid: Transport service identifier |
| 1726 |
* @subsysnqn: Subsystem NQN |
| 1727 |
* @host_traddr: Host transport address |
| 1728 |
* @host_iface: Host interface name |
| 1729 |
* @host_iface: Host interface name |
| 1730 |
* |
| 1731 |
* The function _candidate_free() must be called to release resources once |
| 1732 |
* the candidate object is not longer required. |
| 1733 |
* |
| 1734 |
* Return: The matching function to use when comparing an existing |
| 1735 |
* controller to the candidate controller. |
| 1736 |
*/ |
| 1737 |
static ctrl_match_t _candidate_init(struct candidate_args *candidate, |
| 1738 |
const char *transport, |
| 1739 |
const char *traddr, |
| 1740 |
const char *trsvcid, |
| 1741 |
const char *subsysnqn, |
| 1742 |
const char *host_traddr, |
| 1743 |
const char *host_iface) |
| 1744 |
{ |
| 1745 |
memset(candidate, 0, sizeof(*candidate)); |
| 1746 |
|
| 1747 |
candidate->traddr = traddr; |
| 1748 |
candidate->trsvcid = trsvcid; |
| 1749 |
candidate->transport = transport; |
| 1750 |
candidate->subsysnqn = subsysnqn; |
| 1751 |
candidate->host_iface = streqcase0(host_iface, "none") ? NULL : host_iface; |
| 1752 |
candidate->host_traddr = streqcase0(host_traddr, "none") ? NULL : host_traddr; |
| 1753 |
|
| 1754 |
if (streq0(subsysnqn, NVME_DISC_SUBSYS_NAME)) { |
| 1755 |
/* Since TP8013, the NQN of discovery controllers can be the |
| 1756 |
* well-known NQN (i.e. nqn.2014-08.org.nvmexpress.discovery) or |
| 1757 |
* a unique NQN. A DC created using the well-known NQN may later |
| 1758 |
* display a unique NQN when looked up in the sysfs. Therefore, |
| 1759 |
* ignore (i.e. set to NULL) the well-known NQN when looking for |
| 1760 |
* a match. |
| 1761 |
*/ |
| 1762 |
candidate->subsysnqn = NULL; |
| 1763 |
candidate->well_known_nqn = true; |
| 1764 |
} |
| 1765 |
|
| 1766 |
if (streq0(transport, "tcp")) { |
| 1767 |
/* For TCP we may need to access the interface map. |
| 1768 |
* Let's retrieve and cache the map. |
| 1769 |
*/ |
| 1770 |
if (getifaddrs(&candidate->iface_list) == -1) |
| 1771 |
candidate->iface_list = NULL; |
| 1772 |
|
| 1773 |
candidate->addreq = nvme_ipaddrs_eq; |
| 1774 |
return _tcp_match_ctrl; |
| 1775 |
} |
| 1776 |
|
| 1777 |
if (streq0(transport, "rdma")) { |
| 1778 |
candidate->addreq = nvme_ipaddrs_eq; |
| 1779 |
return _match_ctrl; |
| 1780 |
} |
| 1781 |
|
| 1782 |
/* All other transport types */ |
| 1783 |
candidate->addreq = streqcase0; |
| 1784 |
return _match_ctrl; |
| 1785 |
} |
| 1786 |
|
| 1787 |
/** |
| 1788 |
* _candidate_free() - Release resources allocated by _candidate_init() |
| 1789 |
* |
| 1790 |
* @candidate: data to free. |
| 1791 |
*/ |
| 1792 |
static void _candidate_free(struct candidate_args *candidate) |
| 1793 |
{ |
| 1794 |
freeifaddrs(candidate->iface_list); /* This is NULL-safe */ |
| 1795 |
} |
| 1796 |
|
| 1797 |
#define _cleanup_candidate_ __cleanup__(_candidate_free) |
| 1798 |
|
| 1799 |
nvme_ctrl_t __nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport, |
| 1800 |
const char *traddr, const char *host_traddr, |
| 1801 |
const char *host_iface, const char *trsvcid, |
| 1802 |
const char *subsysnqn, nvme_ctrl_t p) |
| 1803 |
{ |
| 1804 |
_cleanup_candidate_ struct candidate_args candidate = {}; |
| 1805 |
struct nvme_ctrl *c, *matching_c = NULL; |
| 1806 |
ctrl_match_t ctrl_match; |
| 1807 |
|
| 1808 |
/* Init candidate and get the matching function to use */ |
| 1809 |
ctrl_match = _candidate_init(&candidate, transport, traddr, trsvcid, |
| 1810 |
subsysnqn, host_traddr, host_iface); |
| 1811 |
|
| 1812 |
c = p ? nvme_subsystem_next_ctrl(s, p) : nvme_subsystem_first_ctrl(s); |
| 1813 |
for (; c != NULL; c = nvme_subsystem_next_ctrl(s, c)) { |
| 1814 |
if (ctrl_match(c, &candidate)) { |
| 1815 |
matching_c = c; |
| 1816 |
break; |
| 1817 |
} |
| 1818 |
} |
| 1819 |
|
| 1820 |
return matching_c; |
| 1821 |
} |
| 1822 |
|
| 1823 |
bool nvme_ctrl_config_match(struct nvme_ctrl *c, const char *transport, |
| 1824 |
const char *traddr, const char *trsvcid, |
| 1825 |
const char *subsysnqn, const char *host_traddr, |
| 1826 |
const char *host_iface) |
| 1827 |
{ |
| 1828 |
_cleanup_candidate_ struct candidate_args candidate = {}; |
| 1829 |
ctrl_match_t ctrl_match; |
| 1830 |
|
| 1831 |
/* Init candidate and get the matching function to use */ |
| 1832 |
ctrl_match = _candidate_init(&candidate, transport, traddr, trsvcid, |
| 1833 |
subsysnqn, host_traddr, host_iface); |
| 1834 |
|
| 1835 |
return ctrl_match(c, &candidate); |
| 1836 |
} |
| 1837 |
|
| 1838 |
nvme_ctrl_t nvme_ctrl_find(nvme_subsystem_t s, const char *transport, |
| 1839 |
const char *traddr, const char *trsvcid, |
| 1840 |
const char *subsysnqn, const char *host_traddr, |
| 1841 |
const char *host_iface) |
| 1842 |
{ |
| 1843 |
return __nvme_lookup_ctrl(s, transport, traddr, host_traddr, host_iface, |
| 1844 |
trsvcid, subsysnqn, NULL/*p*/); |
| 1845 |
} |
| 1846 |
|
| 1847 |
nvme_ctrl_t nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport, |
| 1848 |
const char *traddr, const char *host_traddr, |
| 1849 |
const char *host_iface, const char *trsvcid, |
| 1850 |
nvme_ctrl_t p) |
| 1851 |
{ |
| 1852 |
nvme_root_t r; |
| 1853 |
struct nvme_ctrl *c; |
| 1854 |
|
| 1855 |
if (!s || !transport) |
| 1856 |
return NULL; |
| 1857 |
|
| 1858 |
c = __nvme_lookup_ctrl(s, transport, traddr, host_traddr, |
| 1859 |
host_iface, trsvcid, NULL, p); |
| 1860 |
if (c) |
| 1861 |
return c; |
| 1862 |
|
| 1863 |
r = s->h ? s->h->r : NULL; |
| 1864 |
c = nvme_create_ctrl(r, s->subsysnqn, transport, traddr, |
| 1865 |
host_traddr, host_iface, trsvcid); |
| 1866 |
if (c) { |
| 1867 |
c->s = s; |
| 1868 |
list_add_tail(&s->ctrls, &c->entry); |
| 1869 |
s->h->r->modified = true; |
| 1870 |
} |
| 1871 |
return c; |
| 1872 |
} |
| 1873 |
|
| 1874 |
static int nvme_ctrl_scan_paths(nvme_root_t r, struct nvme_ctrl *c) |
| 1875 |
{ |
| 1876 |
_cleanup_dirents_ struct dirents paths = {}; |
| 1877 |
int i; |
| 1878 |
|
| 1879 |
if (r->create_only) { |
| 1880 |
nvme_msg(r, LOG_DEBUG, |
| 1881 |
"skipping path scan for ctrl %s\n", c->name); |
| 1882 |
return 0; |
| 1883 |
} |
| 1884 |
paths.num = nvme_scan_ctrl_namespace_paths(c, &paths.ents); |
| 1885 |
if (paths.num < 0) |
| 1886 |
return paths.num; |
| 1887 |
|
| 1888 |
for (i = 0; i < paths.num; i++) |
| 1889 |
nvme_ctrl_scan_path(r, c, paths.ents[i]->d_name); |
| 1890 |
|
| 1891 |
return 0; |
| 1892 |
} |
| 1893 |
|
| 1894 |
static int nvme_ctrl_scan_namespaces(nvme_root_t r, struct nvme_ctrl *c) |
| 1895 |
{ |
| 1896 |
_cleanup_dirents_ struct dirents namespaces = {}; |
| 1897 |
int i; |
| 1898 |
|
| 1899 |
if (r->create_only) { |
| 1900 |
nvme_msg(r, LOG_DEBUG, "skipping namespace scan for ctrl %s\n", |
| 1901 |
c->name); |
| 1902 |
return 0; |
| 1903 |
} |
| 1904 |
namespaces.num = nvme_scan_ctrl_namespaces(c, &namespaces.ents); |
| 1905 |
for (i = 0; i < namespaces.num; i++) |
| 1906 |
nvme_ctrl_scan_namespace(r, c, namespaces.ents[i]->d_name); |
| 1907 |
|
| 1908 |
return 0; |
| 1909 |
} |
| 1910 |
|
| 1911 |
static char *nvme_ctrl_lookup_subsystem_name(nvme_root_t r, |
| 1912 |
const char *ctrl_name) |
| 1913 |
{ |
| 1914 |
const char *subsys_dir = nvme_subsys_sysfs_dir(); |
| 1915 |
_cleanup_dirents_ struct dirents subsys = {}; |
| 1916 |
int i; |
| 1917 |
|
| 1918 |
subsys.num = nvme_scan_subsystems(&subsys.ents); |
| 1919 |
if (subsys.num < 0) |
| 1920 |
return NULL; |
| 1921 |
for (i = 0; i < subsys.num; i++) { |
| 1922 |
struct stat st; |
| 1923 |
_cleanup_free_ char *path = NULL; |
| 1924 |
|
| 1925 |
if (asprintf(&path, "%s/%s/%s", subsys_dir, |
| 1926 |
subsys.ents[i]->d_name, ctrl_name) < 0) { |
| 1927 |
errno = ENOMEM; |
| 1928 |
return NULL; |
| 1929 |
} |
| 1930 |
nvme_msg(r, LOG_DEBUG, "lookup subsystem %s\n", path); |
| 1931 |
if (stat(path, &st) < 0) { |
| 1932 |
continue; |
| 1933 |
} |
| 1934 |
return strdup(subsys.ents[i]->d_name); |
| 1935 |
} |
| 1936 |
return NULL; |
| 1937 |
} |
| 1938 |
|
| 1939 |
static char *nvme_ctrl_lookup_phy_slot(nvme_root_t r, const char *address) |
| 1940 |
{ |
| 1941 |
const char *slots_sysfs_dir = nvme_slots_sysfs_dir(); |
| 1942 |
_cleanup_free_ char *target_addr = NULL; |
| 1943 |
_cleanup_dir_ DIR *slots_dir = NULL; |
| 1944 |
int ret; |
| 1945 |
struct dirent *entry; |
| 1946 |
|
| 1947 |
if (!address) |
| 1948 |
return NULL; |
| 1949 |
|
| 1950 |
slots_dir = opendir(slots_sysfs_dir); |
| 1951 |
if (!slots_dir) { |
| 1952 |
nvme_msg(r, LOG_WARNING, "failed to open slots dir %s\n", |
| 1953 |
slots_sysfs_dir); |
| 1954 |
return NULL; |
| 1955 |
} |
| 1956 |
|
| 1957 |
target_addr = strndup(address, 10); |
| 1958 |
while ((entry = readdir(slots_dir))) { |
| 1959 |
if (entry->d_type == DT_DIR && |
| 1960 |
strncmp(entry->d_name, ".", 1) != 0 && |
| 1961 |
strncmp(entry->d_name, "..", 2) != 0) { |
| 1962 |
_cleanup_free_ char *path = NULL; |
| 1963 |
_cleanup_free_ char *addr = NULL; |
| 1964 |
|
| 1965 |
ret = asprintf(&path, "%s/%s", |
| 1966 |
slots_sysfs_dir, entry->d_name); |
| 1967 |
if (ret < 0) { |
| 1968 |
errno = ENOMEM; |
| 1969 |
return NULL; |
| 1970 |
} |
| 1971 |
addr = nvme_get_attr(path, "address"); |
| 1972 |
|
| 1973 |
/* some directories don't have an address entry */ |
| 1974 |
if (!addr) |
| 1975 |
continue; |
| 1976 |
if (strcmp(addr, target_addr) == 0) |
| 1977 |
return strdup(entry->d_name); |
| 1978 |
} |
| 1979 |
} |
| 1980 |
return NULL; |
| 1981 |
} |
| 1982 |
|
| 1983 |
static void nvme_read_sysfs_dhchap(nvme_root_t r, nvme_ctrl_t c) |
| 1984 |
{ |
| 1985 |
char *host_key, *ctrl_key; |
| 1986 |
|
| 1987 |
host_key = nvme_get_ctrl_attr(c, "dhchap_secret"); |
| 1988 |
if (host_key && !strcmp(host_key, "none")) { |
| 1989 |
free(host_key); |
| 1990 |
host_key = NULL; |
| 1991 |
} |
| 1992 |
if (host_key) { |
| 1993 |
nvme_ctrl_set_dhchap_host_key(c, NULL); |
| 1994 |
c->dhchap_key = host_key; |
| 1995 |
} |
| 1996 |
|
| 1997 |
ctrl_key = nvme_get_ctrl_attr(c, "dhchap_ctrl_secret"); |
| 1998 |
if (ctrl_key && !strcmp(ctrl_key, "none")) { |
| 1999 |
free(ctrl_key); |
| 2000 |
ctrl_key = NULL; |
| 2001 |
} |
| 2002 |
if (ctrl_key) { |
| 2003 |
nvme_ctrl_set_dhchap_key(c, NULL); |
| 2004 |
c->dhchap_ctrl_key = ctrl_key; |
| 2005 |
} |
| 2006 |
} |
| 2007 |
|
| 2008 |
static void nvme_read_sysfs_tls(nvme_root_t r, nvme_ctrl_t c) |
| 2009 |
{ |
| 2010 |
char *endptr; |
| 2011 |
long key_id; |
| 2012 |
char *key, *keyring; |
| 2013 |
|
| 2014 |
key = nvme_get_ctrl_attr(c, "tls_key"); |
| 2015 |
if (!key) { |
| 2016 |
/* tls_key is only present if --tls has been used. */ |
| 2017 |
return; |
| 2018 |
} |
| 2019 |
c->cfg.tls = true; |
| 2020 |
|
| 2021 |
keyring = nvme_get_ctrl_attr(c, "tls_keyring"); |
| 2022 |
nvme_ctrl_set_keyring(c, keyring); |
| 2023 |
free(keyring); |
| 2024 |
|
| 2025 |
/* the sysfs entry is not prefixing the id but it's in hex */ |
| 2026 |
key_id = strtol(key, &endptr, 16); |
| 2027 |
if (endptr != key) |
| 2028 |
c->cfg.tls_key = key_id; |
| 2029 |
|
| 2030 |
free(key); |
| 2031 |
|
| 2032 |
key = nvme_get_ctrl_attr(c, "tls_configured_key"); |
| 2033 |
if (!key) |
| 2034 |
return; |
| 2035 |
|
| 2036 |
/* the sysfs entry is not prefixing the id but it's in hex */ |
| 2037 |
key_id = strtol(key, &endptr, 16); |
| 2038 |
if (endptr != key) |
| 2039 |
c->cfg.tls_configured_key = key_id; |
| 2040 |
|
| 2041 |
free(key); |
| 2042 |
} |
| 2043 |
|
| 2044 |
static int nvme_reconfigure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path, |
| 2045 |
const char *name) |
| 2046 |
{ |
| 2047 |
DIR *d; |
| 2048 |
|
| 2049 |
/* |
| 2050 |
* It's necesssary to release any resources first because a ctrl |
| 2051 |
* can be reused. |
| 2052 |
*/ |
| 2053 |
nvme_ctrl_release_fd(c); |
| 2054 |
FREE_CTRL_ATTR(c->name); |
| 2055 |
FREE_CTRL_ATTR(c->sysfs_dir); |
| 2056 |
FREE_CTRL_ATTR(c->firmware); |
| 2057 |
FREE_CTRL_ATTR(c->model); |
| 2058 |
FREE_CTRL_ATTR(c->state); |
| 2059 |
FREE_CTRL_ATTR(c->numa_node); |
| 2060 |
FREE_CTRL_ATTR(c->queue_count); |
| 2061 |
FREE_CTRL_ATTR(c->serial); |
| 2062 |
FREE_CTRL_ATTR(c->sqsize); |
| 2063 |
FREE_CTRL_ATTR(c->cntrltype); |
| 2064 |
FREE_CTRL_ATTR(c->cntlid); |
| 2065 |
FREE_CTRL_ATTR(c->dctype); |
| 2066 |
FREE_CTRL_ATTR(c->phy_slot); |
| 2067 |
|
| 2068 |
d = opendir(path); |
| 2069 |
if (!d) { |
| 2070 |
nvme_msg(r, LOG_ERR, "Failed to open ctrl dir %s, error %d\n", |
| 2071 |
path, errno); |
| 2072 |
errno = ENODEV; |
| 2073 |
return -1; |
| 2074 |
} |
| 2075 |
closedir(d); |
| 2076 |
|
| 2077 |
c->name = xstrdup(name); |
| 2078 |
c->sysfs_dir = xstrdup(path); |
| 2079 |
c->firmware = nvme_get_ctrl_attr(c, "firmware_rev"); |
| 2080 |
c->model = nvme_get_ctrl_attr(c, "model"); |
| 2081 |
c->state = nvme_get_ctrl_attr(c, "state"); |
| 2082 |
c->numa_node = nvme_get_ctrl_attr(c, "numa_node"); |
| 2083 |
c->queue_count = nvme_get_ctrl_attr(c, "queue_count"); |
| 2084 |
c->serial = nvme_get_ctrl_attr(c, "serial"); |
| 2085 |
c->sqsize = nvme_get_ctrl_attr(c, "sqsize"); |
| 2086 |
c->cntrltype = nvme_get_ctrl_attr(c, "cntrltype"); |
| 2087 |
c->cntlid = nvme_get_ctrl_attr(c, "cntlid"); |
| 2088 |
c->dctype = nvme_get_ctrl_attr(c, "dctype"); |
| 2089 |
c->phy_slot = nvme_ctrl_lookup_phy_slot(r, c->address); |
| 2090 |
nvme_read_sysfs_dhchap(r, c); |
| 2091 |
nvme_read_sysfs_tls(r, c); |
| 2092 |
|
| 2093 |
errno = 0; /* cleanup after nvme_get_ctrl_attr() */ |
| 2094 |
return 0; |
| 2095 |
} |
| 2096 |
|
| 2097 |
int nvme_init_ctrl(nvme_host_t h, nvme_ctrl_t c, int instance) |
| 2098 |
{ |
| 2099 |
_cleanup_free_ char *subsys_name = NULL, *name = NULL, *path = NULL; |
| 2100 |
nvme_subsystem_t s; |
| 2101 |
int ret; |
| 2102 |
|
| 2103 |
ret = asprintf(&name, "nvme%d", instance); |
| 2104 |
if (ret < 0) { |
| 2105 |
errno = ENOMEM; |
| 2106 |
return -1; |
| 2107 |
} |
| 2108 |
ret = asprintf(&path, "%s/%s", nvme_ctrl_sysfs_dir(), name); |
| 2109 |
if (ret < 0) { |
| 2110 |
errno = ENOMEM; |
| 2111 |
return ret; |
| 2112 |
} |
| 2113 |
|
| 2114 |
ret = nvme_reconfigure_ctrl(h->r, c, path, name); |
| 2115 |
if (ret < 0) |
| 2116 |
return ret; |
| 2117 |
|
| 2118 |
c->address = nvme_get_attr(path, "address"); |
| 2119 |
if (!c->address && strcmp(c->transport, "loop")) { |
| 2120 |
errno = ENVME_CONNECT_INVAL_TR; |
| 2121 |
return -1; |
| 2122 |
} |
| 2123 |
|
| 2124 |
subsys_name = nvme_ctrl_lookup_subsystem_name(h->r, name); |
| 2125 |
if (!subsys_name) { |
| 2126 |
nvme_msg(h->r, LOG_ERR, |
| 2127 |
"Failed to lookup subsystem name for %s\n", |
| 2128 |
c->name); |
| 2129 |
errno = ENVME_CONNECT_LOOKUP_SUBSYS_NAME; |
| 2130 |
return -1; |
| 2131 |
} |
| 2132 |
s = nvme_lookup_subsystem(h, subsys_name, c->subsysnqn); |
| 2133 |
if (!s) { |
| 2134 |
errno = ENVME_CONNECT_LOOKUP_SUBSYS; |
| 2135 |
return -1; |
| 2136 |
} |
| 2137 |
if (s->subsystype && !strcmp(s->subsystype, "discovery")) |
| 2138 |
c->discovery_ctrl = true; |
| 2139 |
c->s = s; |
| 2140 |
list_add_tail(&s->ctrls, &c->entry); |
| 2141 |
return ret; |
| 2142 |
} |
| 2143 |
|
| 2144 |
static nvme_ctrl_t nvme_ctrl_alloc(nvme_root_t r, nvme_subsystem_t s, |
| 2145 |
const char *path, const char *name) |
| 2146 |
{ |
| 2147 |
_cleanup_free_ char *addr = NULL, *address = NULL, *transport = NULL; |
| 2148 |
char *host_traddr = NULL, *host_iface = NULL; |
| 2149 |
char *traddr = NULL, *trsvcid = NULL; |
| 2150 |
char *a = NULL, *e = NULL; |
| 2151 |
nvme_ctrl_t c, p; |
| 2152 |
int ret; |
| 2153 |
|
| 2154 |
transport = nvme_get_attr(path, "transport"); |
| 2155 |
if (!transport) { |
| 2156 |
errno = ENXIO; |
| 2157 |
return NULL; |
| 2158 |
} |
| 2159 |
/* Parse 'address' string into components */ |
| 2160 |
addr = nvme_get_attr(path, "address"); |
| 2161 |
if (!addr) { |
| 2162 |
_cleanup_free_ char *rpath = NULL; |
| 2163 |
char *p = NULL, *_a = NULL; |
| 2164 |
|
| 2165 |
/* loop transport might not have an address */ |
| 2166 |
if (!strcmp(transport, "loop")) |
| 2167 |
goto skip_address; |
| 2168 |
|
| 2169 |
/* Older kernel don't support pcie transport addresses */ |
| 2170 |
if (strcmp(transport, "pcie") && |
| 2171 |
strcmp(transport, "apple-nvme")) { |
| 2172 |
errno = ENXIO; |
| 2173 |
return NULL; |
| 2174 |
} |
| 2175 |
/* Figure out the PCI address from the attribute path */ |
| 2176 |
rpath = realpath(path, NULL); |
| 2177 |
if (!rpath) { |
| 2178 |
errno = ENOMEM; |
| 2179 |
return NULL; |
| 2180 |
} |
| 2181 |
a = strtok_r(rpath, "/", &e); |
| 2182 |
while(a && strlen(a)) { |
| 2183 |
if (_a) |
| 2184 |
p = _a; |
| 2185 |
_a = a; |
| 2186 |
if (!strncmp(a, "nvme", 4)) |
| 2187 |
break; |
| 2188 |
a = strtok_r(NULL, "/", &e); |
| 2189 |
} |
| 2190 |
if (p) |
| 2191 |
addr = strdup(p); |
| 2192 |
} else if (!strcmp(transport, "pcie") || |
| 2193 |
!strcmp(transport, "apple-nvme")) { |
| 2194 |
/* The 'address' string is the transport address */ |
| 2195 |
traddr = addr; |
| 2196 |
} else { |
| 2197 |
address = strdup(addr); |
| 2198 |
a = strtok_r(address, ",", &e); |
| 2199 |
while (a && strlen(a)) { |
| 2200 |
if (!strncmp(a, "traddr=", 7)) |
| 2201 |
traddr = a + 7; |
| 2202 |
else if (!strncmp(a, "trsvcid=", 8)) |
| 2203 |
trsvcid = a + 8; |
| 2204 |
else if (!strncmp(a, "host_traddr=", 12)) |
| 2205 |
host_traddr = a + 12; |
| 2206 |
else if (!strncmp(a, "host_iface=", 11)) |
| 2207 |
host_iface = a + 11; |
| 2208 |
a = strtok_r(NULL, ",", &e); |
| 2209 |
} |
| 2210 |
} |
| 2211 |
skip_address: |
| 2212 |
p = NULL; |
| 2213 |
do { |
| 2214 |
c = nvme_lookup_ctrl(s, transport, traddr, |
| 2215 |
host_traddr, host_iface, trsvcid, p); |
| 2216 |
if (c) { |
| 2217 |
if (!c->name) |
| 2218 |
break; |
| 2219 |
if (!strcmp(c->name, name)) { |
| 2220 |
nvme_msg(r, LOG_DEBUG, |
| 2221 |
"found existing ctrl %s\n", c->name); |
| 2222 |
break; |
| 2223 |
} |
| 2224 |
nvme_msg(r, LOG_DEBUG, "skipping ctrl %s\n", c->name); |
| 2225 |
p = c; |
| 2226 |
} |
| 2227 |
} while (c); |
| 2228 |
if (!c) |
| 2229 |
c = p; |
| 2230 |
if (!c && !p) { |
| 2231 |
nvme_msg(r, LOG_ERR, "failed to lookup ctrl\n"); |
| 2232 |
errno = ENODEV; |
| 2233 |
return NULL; |
| 2234 |
} |
| 2235 |
FREE_CTRL_ATTR(c->address); |
| 2236 |
c->address = xstrdup(addr); |
| 2237 |
if (s->subsystype && !strcmp(s->subsystype, "discovery")) |
| 2238 |
c->discovery_ctrl = true; |
| 2239 |
ret = nvme_reconfigure_ctrl(r, c, path, name); |
| 2240 |
return (ret < 0) ? NULL : c; |
| 2241 |
} |
| 2242 |
|
| 2243 |
nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name) |
| 2244 |
{ |
| 2245 |
_cleanup_free_ char *subsysnqn = NULL, *subsysname = NULL; |
| 2246 |
_cleanup_free_ char *hostnqn = NULL, *hostid = NULL; |
| 2247 |
_cleanup_free_ char *path = NULL; |
| 2248 |
char *host_key; |
| 2249 |
nvme_host_t h; |
| 2250 |
nvme_subsystem_t s; |
| 2251 |
nvme_ctrl_t c; |
| 2252 |
int ret; |
| 2253 |
|
| 2254 |
nvme_msg(r, LOG_DEBUG, "scan controller %s\n", name); |
| 2255 |
ret = asprintf(&path, "%s/%s", nvme_ctrl_sysfs_dir(), name); |
| 2256 |
if (ret < 0) { |
| 2257 |
errno = ENOMEM; |
| 2258 |
return NULL; |
| 2259 |
} |
| 2260 |
|
| 2261 |
hostnqn = nvme_get_attr(path, "hostnqn"); |
| 2262 |
hostid = nvme_get_attr(path, "hostid"); |
| 2263 |
h = nvme_lookup_host(r, hostnqn, hostid); |
| 2264 |
if (h) { |
| 2265 |
host_key = nvme_get_attr(path, "dhchap_secret"); |
| 2266 |
if (host_key && strcmp(host_key, "none")) { |
| 2267 |
free(h->dhchap_key); |
| 2268 |
h->dhchap_key = host_key; |
| 2269 |
host_key = NULL; |
| 2270 |
} |
| 2271 |
free(host_key); |
| 2272 |
} |
| 2273 |
if (!h) { |
| 2274 |
h = nvme_default_host(r); |
| 2275 |
if (!h) { |
| 2276 |
errno = ENOMEM; |
| 2277 |
return NULL; |
| 2278 |
} |
| 2279 |
} |
| 2280 |
|
| 2281 |
subsysnqn = nvme_get_attr(path, "subsysnqn"); |
| 2282 |
if (!subsysnqn) { |
| 2283 |
errno = ENXIO; |
| 2284 |
return NULL; |
| 2285 |
} |
| 2286 |
subsysname = nvme_ctrl_lookup_subsystem_name(r, name); |
| 2287 |
if (!subsysname) { |
| 2288 |
nvme_msg(r, LOG_DEBUG, |
| 2289 |
"failed to lookup subsystem for controller %s\n", |
| 2290 |
name); |
| 2291 |
errno = ENXIO; |
| 2292 |
return NULL; |
| 2293 |
} |
| 2294 |
s = nvme_lookup_subsystem(h, subsysname, subsysnqn); |
| 2295 |
|
| 2296 |
if (!s) { |
| 2297 |
errno = ENOMEM; |
| 2298 |
return NULL; |
| 2299 |
} |
| 2300 |
|
| 2301 |
c = nvme_ctrl_alloc(r, s, path, name); |
| 2302 |
if (!c) |
| 2303 |
return NULL; |
| 2304 |
|
| 2305 |
nvme_ctrl_scan_paths(r, c); |
| 2306 |
nvme_ctrl_scan_namespaces(r, c); |
| 2307 |
return c; |
| 2308 |
} |
| 2309 |
|
| 2310 |
void nvme_rescan_ctrl(struct nvme_ctrl *c) |
| 2311 |
{ |
| 2312 |
nvme_root_t r = c->s && c->s->h ? c->s->h->r : NULL; |
| 2313 |
if (!c->s) |
| 2314 |
return; |
| 2315 |
nvme_ctrl_scan_namespaces(r, c); |
| 2316 |
nvme_ctrl_scan_paths(r, c); |
| 2317 |
nvme_subsystem_scan_namespaces(r, c->s); |
| 2318 |
} |
| 2319 |
|
| 2320 |
static int nvme_bytes_to_lba(nvme_ns_t n, off_t offset, size_t count, |
| 2321 |
__u64 *lba, __u16 *nlb) |
| 2322 |
{ |
| 2323 |
int bs; |
| 2324 |
|
| 2325 |
bs = nvme_ns_get_lba_size(n); |
| 2326 |
if (!count || offset & (bs - 1) || count & (bs - 1)) { |
| 2327 |
errno = EINVAL; |
| 2328 |
return -1; |
| 2329 |
} |
| 2330 |
|
| 2331 |
*lba = offset >> n->lba_shift; |
| 2332 |
*nlb = (count >> n->lba_shift) - 1; |
| 2333 |
return 0; |
| 2334 |
} |
| 2335 |
|
| 2336 |
int nvme_ns_get_fd(nvme_ns_t n) |
| 2337 |
{ |
| 2338 |
if (n->fd < 0) { |
| 2339 |
n->fd = nvme_open(n->name); |
| 2340 |
if (n->fd < 0) |
| 2341 |
nvme_msg(root_from_ns(n), LOG_ERR, |
| 2342 |
"Failed to open ns %s, errno %d\n", |
| 2343 |
n->name, errno); |
| 2344 |
} |
| 2345 |
|
| 2346 |
return n->fd; |
| 2347 |
} |
| 2348 |
|
| 2349 |
void nvme_ns_release_fd(nvme_ns_t n) |
| 2350 |
{ |
| 2351 |
if (n->fd < 0) |
| 2352 |
return; |
| 2353 |
|
| 2354 |
close(n->fd); |
| 2355 |
n->fd = -1; |
| 2356 |
} |
| 2357 |
|
| 2358 |
nvme_subsystem_t nvme_ns_get_subsystem(nvme_ns_t n) |
| 2359 |
{ |
| 2360 |
return n->s; |
| 2361 |
} |
| 2362 |
|
| 2363 |
nvme_ctrl_t nvme_ns_get_ctrl(nvme_ns_t n) |
| 2364 |
{ |
| 2365 |
return n->c; |
| 2366 |
} |
| 2367 |
|
| 2368 |
int nvme_ns_get_nsid(nvme_ns_t n) |
| 2369 |
{ |
| 2370 |
return n->nsid; |
| 2371 |
} |
| 2372 |
|
| 2373 |
const char *nvme_ns_get_sysfs_dir(nvme_ns_t n) |
| 2374 |
{ |
| 2375 |
return n->sysfs_dir; |
| 2376 |
} |
| 2377 |
|
| 2378 |
const char *nvme_ns_head_get_sysfs_dir(nvme_ns_head_t head) |
| 2379 |
{ |
| 2380 |
return head->sysfs_dir; |
| 2381 |
} |
| 2382 |
|
| 2383 |
const char *nvme_ns_get_name(nvme_ns_t n) |
| 2384 |
{ |
| 2385 |
return n->name; |
| 2386 |
} |
| 2387 |
|
| 2388 |
const char *nvme_ns_get_generic_name(nvme_ns_t n) |
| 2389 |
{ |
| 2390 |
return n->generic_name; |
| 2391 |
} |
| 2392 |
|
| 2393 |
const char *nvme_ns_get_model(nvme_ns_t n) |
| 2394 |
{ |
| 2395 |
return n->c ? n->c->model : n->s->model; |
| 2396 |
} |
| 2397 |
|
| 2398 |
const char *nvme_ns_get_serial(nvme_ns_t n) |
| 2399 |
{ |
| 2400 |
return n->c ? n->c->serial : n->s->serial; |
| 2401 |
} |
| 2402 |
|
| 2403 |
const char *nvme_ns_get_firmware(nvme_ns_t n) |
| 2404 |
{ |
| 2405 |
return n->c ? n->c->firmware : n->s->firmware; |
| 2406 |
} |
| 2407 |
|
| 2408 |
int nvme_ns_get_lba_size(nvme_ns_t n) |
| 2409 |
{ |
| 2410 |
return n->lba_size; |
| 2411 |
} |
| 2412 |
|
| 2413 |
int nvme_ns_get_meta_size(nvme_ns_t n) |
| 2414 |
{ |
| 2415 |
return n->meta_size; |
| 2416 |
} |
| 2417 |
|
| 2418 |
uint64_t nvme_ns_get_lba_count(nvme_ns_t n) |
| 2419 |
{ |
| 2420 |
return n->lba_count; |
| 2421 |
} |
| 2422 |
|
| 2423 |
uint64_t nvme_ns_get_lba_util(nvme_ns_t n) |
| 2424 |
{ |
| 2425 |
return n->lba_util; |
| 2426 |
} |
| 2427 |
|
| 2428 |
enum nvme_csi nvme_ns_get_csi(nvme_ns_t n) |
| 2429 |
{ |
| 2430 |
return n->csi; |
| 2431 |
} |
| 2432 |
|
| 2433 |
const uint8_t *nvme_ns_get_eui64(nvme_ns_t n) |
| 2434 |
{ |
| 2435 |
return n->eui64; |
| 2436 |
} |
| 2437 |
|
| 2438 |
const uint8_t *nvme_ns_get_nguid(nvme_ns_t n) |
| 2439 |
{ |
| 2440 |
return n->nguid; |
| 2441 |
} |
| 2442 |
|
| 2443 |
void nvme_ns_get_uuid(nvme_ns_t n, unsigned char out[NVME_UUID_LEN]) |
| 2444 |
{ |
| 2445 |
memcpy(out, n->uuid, NVME_UUID_LEN); |
| 2446 |
} |
| 2447 |
|
| 2448 |
int nvme_ns_identify(nvme_ns_t n, struct nvme_id_ns *ns) |
| 2449 |
{ |
| 2450 |
return nvme_identify_ns(nvme_ns_get_fd(n), nvme_ns_get_nsid(n), ns); |
| 2451 |
} |
| 2452 |
|
| 2453 |
int nvme_ns_identify_descs(nvme_ns_t n, struct nvme_ns_id_desc *descs) |
| 2454 |
{ |
| 2455 |
return nvme_identify_ns_descs(nvme_ns_get_fd(n), nvme_ns_get_nsid(n), descs); |
| 2456 |
} |
| 2457 |
|
| 2458 |
int nvme_ns_verify(nvme_ns_t n, off_t offset, size_t count) |
| 2459 |
{ |
| 2460 |
struct nvme_io_args args = { |
| 2461 |
.args_size = sizeof(args), |
| 2462 |
.fd = nvme_ns_get_fd(n), |
| 2463 |
.nsid = nvme_ns_get_nsid(n), |
| 2464 |
.control = 0, |
| 2465 |
.dsm = 0, |
| 2466 |
.dspec = 0, |
| 2467 |
.reftag = 0, |
| 2468 |
.apptag = 0, |
| 2469 |
.appmask = 0, |
| 2470 |
.storage_tag = 0, |
| 2471 |
.data_len = 0, |
| 2472 |
.data = NULL, |
| 2473 |
.metadata_len = 0, |
| 2474 |
.metadata = NULL, |
| 2475 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 2476 |
.result = NULL, |
| 2477 |
}; |
| 2478 |
|
| 2479 |
if (nvme_bytes_to_lba(n, offset, count, &args.slba, &args.nlb)) |
| 2480 |
return -1; |
| 2481 |
|
| 2482 |
return nvme_verify(&args); |
| 2483 |
} |
| 2484 |
|
| 2485 |
int nvme_ns_write_uncorrectable(nvme_ns_t n, off_t offset, size_t count) |
| 2486 |
{ |
| 2487 |
struct nvme_io_args args = { |
| 2488 |
.args_size = sizeof(args), |
| 2489 |
.fd = nvme_ns_get_fd(n), |
| 2490 |
.nsid = nvme_ns_get_nsid(n), |
| 2491 |
.control = 0, |
| 2492 |
.dsm = 0, |
| 2493 |
.dspec = 0, |
| 2494 |
.reftag = 0, |
| 2495 |
.apptag = 0, |
| 2496 |
.appmask = 0, |
| 2497 |
.storage_tag = 0, |
| 2498 |
.data_len = 0, |
| 2499 |
.data = NULL, |
| 2500 |
.metadata_len = 0, |
| 2501 |
.metadata = NULL, |
| 2502 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 2503 |
.result = NULL, |
| 2504 |
}; |
| 2505 |
|
| 2506 |
if (nvme_bytes_to_lba(n, offset, count, &args.slba, &args.nlb)) |
| 2507 |
return -1; |
| 2508 |
|
| 2509 |
return nvme_write_uncorrectable(&args); |
| 2510 |
} |
| 2511 |
|
| 2512 |
int nvme_ns_write_zeros(nvme_ns_t n, off_t offset, size_t count) |
| 2513 |
{ |
| 2514 |
struct nvme_io_args args = { |
| 2515 |
.args_size = sizeof(args), |
| 2516 |
.fd = nvme_ns_get_fd(n), |
| 2517 |
.nsid = nvme_ns_get_nsid(n), |
| 2518 |
.control = 0, |
| 2519 |
.dsm = 0, |
| 2520 |
.dspec = 0, |
| 2521 |
.reftag = 0, |
| 2522 |
.apptag = 0, |
| 2523 |
.appmask = 0, |
| 2524 |
.storage_tag = 0, |
| 2525 |
.data_len = 0, |
| 2526 |
.data = NULL, |
| 2527 |
.metadata_len = 0, |
| 2528 |
.metadata = NULL, |
| 2529 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 2530 |
.result = NULL, |
| 2531 |
}; |
| 2532 |
|
| 2533 |
if (nvme_bytes_to_lba(n, offset, count, &args.slba, &args.nlb)) |
| 2534 |
return -1; |
| 2535 |
|
| 2536 |
return nvme_write_zeros(&args); |
| 2537 |
} |
| 2538 |
|
| 2539 |
int nvme_ns_write(nvme_ns_t n, void *buf, off_t offset, size_t count) |
| 2540 |
{ |
| 2541 |
struct nvme_io_args args = { |
| 2542 |
.args_size = sizeof(args), |
| 2543 |
.fd = nvme_ns_get_fd(n), |
| 2544 |
.nsid = nvme_ns_get_nsid(n), |
| 2545 |
.control = 0, |
| 2546 |
.dsm = 0, |
| 2547 |
.dspec = 0, |
| 2548 |
.reftag = 0, |
| 2549 |
.apptag = 0, |
| 2550 |
.appmask = 0, |
| 2551 |
.storage_tag = 0, |
| 2552 |
.data_len = count, |
| 2553 |
.data = buf, |
| 2554 |
.metadata_len = 0, |
| 2555 |
.metadata = NULL, |
| 2556 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 2557 |
.result = NULL, |
| 2558 |
}; |
| 2559 |
|
| 2560 |
if (nvme_bytes_to_lba(n, offset, count, &args.slba, &args.nlb)) |
| 2561 |
return -1; |
| 2562 |
|
| 2563 |
return nvme_write(&args); |
| 2564 |
} |
| 2565 |
|
| 2566 |
int nvme_ns_read(nvme_ns_t n, void *buf, off_t offset, size_t count) |
| 2567 |
{ |
| 2568 |
struct nvme_io_args args = { |
| 2569 |
.args_size = sizeof(args), |
| 2570 |
.fd = nvme_ns_get_fd(n), |
| 2571 |
.nsid = nvme_ns_get_nsid(n), |
| 2572 |
.control = 0, |
| 2573 |
.dsm = 0, |
| 2574 |
.dspec = 0, |
| 2575 |
.reftag = 0, |
| 2576 |
.apptag = 0, |
| 2577 |
.appmask = 0, |
| 2578 |
.storage_tag = 0, |
| 2579 |
.data_len = count, |
| 2580 |
.data = buf, |
| 2581 |
.metadata_len = 0, |
| 2582 |
.metadata = NULL, |
| 2583 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 2584 |
.result = NULL, |
| 2585 |
}; |
| 2586 |
|
| 2587 |
if (nvme_bytes_to_lba(n, offset, count, &args.slba, &args.nlb)) |
| 2588 |
return -1; |
| 2589 |
|
| 2590 |
return nvme_read(&args); |
| 2591 |
} |
| 2592 |
|
| 2593 |
int nvme_ns_compare(nvme_ns_t n, void *buf, off_t offset, size_t count) |
| 2594 |
{ |
| 2595 |
struct nvme_io_args args = { |
| 2596 |
.args_size = sizeof(args), |
| 2597 |
.fd = nvme_ns_get_fd(n), |
| 2598 |
.nsid = nvme_ns_get_nsid(n), |
| 2599 |
.control = 0, |
| 2600 |
.dsm = 0, |
| 2601 |
.dspec = 0, |
| 2602 |
.reftag = 0, |
| 2603 |
.apptag = 0, |
| 2604 |
.appmask = 0, |
| 2605 |
.storage_tag = 0, |
| 2606 |
.data_len = count, |
| 2607 |
.data = buf, |
| 2608 |
.metadata_len = 0, |
| 2609 |
.metadata = NULL, |
| 2610 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 2611 |
.result = NULL, |
| 2612 |
}; |
| 2613 |
|
| 2614 |
if (nvme_bytes_to_lba(n, offset, count, &args.slba, &args.nlb)) |
| 2615 |
return -1; |
| 2616 |
|
| 2617 |
return nvme_compare(&args); |
| 2618 |
} |
| 2619 |
|
| 2620 |
int nvme_ns_flush(nvme_ns_t n) |
| 2621 |
{ |
| 2622 |
return nvme_flush(nvme_ns_get_fd(n), nvme_ns_get_nsid(n)); |
| 2623 |
} |
| 2624 |
|
| 2625 |
static int nvme_strtou64(const char *str, void *res) |
| 2626 |
{ |
| 2627 |
char *endptr; |
| 2628 |
__u64 v; |
| 2629 |
|
| 2630 |
errno = 0; |
| 2631 |
v = strtoull(str, &endptr, 0); |
| 2632 |
|
| 2633 |
if (errno != 0) |
| 2634 |
return -errno; |
| 2635 |
|
| 2636 |
if (endptr == str) { |
| 2637 |
/* no digits found */ |
| 2638 |
return -EINVAL; |
| 2639 |
} |
| 2640 |
|
| 2641 |
*(__u64 *)res = v; |
| 2642 |
return 0; |
| 2643 |
} |
| 2644 |
|
| 2645 |
static int nvme_strtou32(const char *str, void *res) |
| 2646 |
{ |
| 2647 |
char *endptr; |
| 2648 |
__u32 v; |
| 2649 |
|
| 2650 |
errno = 0; |
| 2651 |
v = strtol(str, &endptr, 0); |
| 2652 |
|
| 2653 |
if (errno != 0) |
| 2654 |
return -errno; |
| 2655 |
|
| 2656 |
if (endptr == str) { |
| 2657 |
/* no digits found */ |
| 2658 |
return -EINVAL; |
| 2659 |
} |
| 2660 |
|
| 2661 |
*(__u32 *)res = v; |
| 2662 |
return 0; |
| 2663 |
} |
| 2664 |
|
| 2665 |
static int nvme_strtoi(const char *str, void *res) |
| 2666 |
{ |
| 2667 |
char *endptr; |
| 2668 |
int v; |
| 2669 |
|
| 2670 |
errno = 0; |
| 2671 |
v = strtol(str, &endptr, 0); |
| 2672 |
|
| 2673 |
if (errno != 0) |
| 2674 |
return -errno; |
| 2675 |
|
| 2676 |
if (endptr == str) { |
| 2677 |
/* no digits found */ |
| 2678 |
return -EINVAL; |
| 2679 |
} |
| 2680 |
|
| 2681 |
*(int *)res = v; |
| 2682 |
return 0; |
| 2683 |
} |
| 2684 |
|
| 2685 |
static int nvme_strtoeuid(const char *str, void *res) |
| 2686 |
{ |
| 2687 |
memcpy(res, str, 8); |
| 2688 |
return 0; |
| 2689 |
} |
| 2690 |
|
| 2691 |
static int nvme_strtouuid(const char *str, void *res) |
| 2692 |
{ |
| 2693 |
memcpy(res, str, NVME_UUID_LEN); |
| 2694 |
return 0; |
| 2695 |
} |
| 2696 |
|
| 2697 |
struct sysfs_attr_table { |
| 2698 |
void *var; |
| 2699 |
int (*parse)(const char *str, void *res); |
| 2700 |
bool mandatory; |
| 2701 |
const char *name; |
| 2702 |
}; |
| 2703 |
|
| 2704 |
#define GETSHIFT(x) (__builtin_ffsll(x) - 1) |
| 2705 |
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) |
| 2706 |
|
| 2707 |
static int parse_attrs(const char *path, struct sysfs_attr_table *tbl, int size) |
| 2708 |
{ |
| 2709 |
char *str; |
| 2710 |
int ret, i; |
| 2711 |
|
| 2712 |
for (i = 0; i < size; i++) { |
| 2713 |
struct sysfs_attr_table *e = &tbl[i]; |
| 2714 |
|
| 2715 |
str = nvme_get_attr(path, e->name); |
| 2716 |
if (!str) { |
| 2717 |
if (!e->mandatory) |
| 2718 |
continue; |
| 2719 |
return -ENOENT; |
| 2720 |
} |
| 2721 |
ret = e->parse(str, e->var); |
| 2722 |
free(str); |
| 2723 |
if (ret) |
| 2724 |
return ret; |
| 2725 |
} |
| 2726 |
|
| 2727 |
return 0; |
| 2728 |
} |
| 2729 |
|
| 2730 |
static int nvme_ns_init(const char *path, struct nvme_ns *ns) |
| 2731 |
{ |
| 2732 |
_cleanup_free_ char *attr = NULL; |
| 2733 |
struct stat sb; |
| 2734 |
uint64_t size; |
| 2735 |
int ret; |
| 2736 |
|
| 2737 |
struct sysfs_attr_table base[] = { |
| 2738 |
{ &ns->nsid, nvme_strtou32, true, "nsid" }, |
| 2739 |
{ &size, nvme_strtou64, true, "size" }, |
| 2740 |
{ &ns->lba_size, nvme_strtou32, true, "queue/logical_block_size" }, |
| 2741 |
{ ns->eui64, nvme_strtoeuid, false, "eui" }, |
| 2742 |
{ ns->nguid, nvme_strtouuid, false, "nguid" }, |
| 2743 |
{ ns->uuid, nvme_strtouuid, false, "uuid" } |
| 2744 |
}; |
| 2745 |
|
| 2746 |
ret = parse_attrs(path, base, ARRAY_SIZE(base)); |
| 2747 |
if (ret) |
| 2748 |
return ret; |
| 2749 |
|
| 2750 |
ns->lba_shift = GETSHIFT(ns->lba_size); |
| 2751 |
/* |
| 2752 |
* size is in 512 bytes units and lba_count is in lba_size which are not |
| 2753 |
* necessarily the same. |
| 2754 |
*/ |
| 2755 |
ns->lba_count = size >> (ns->lba_shift - SECTOR_SHIFT); |
| 2756 |
|
| 2757 |
if (asprintf(&attr, "%s/csi", path) < 0) |
| 2758 |
return -errno; |
| 2759 |
ret = stat(attr, &sb); |
| 2760 |
if (ret == 0) { |
| 2761 |
/* only available on kernels >= 6.8 */ |
| 2762 |
struct sysfs_attr_table ext[] = { |
| 2763 |
{ &ns->csi, nvme_strtoi, true, "csi" }, |
| 2764 |
{ &ns->lba_util, nvme_strtou64, true, "nuse" }, |
| 2765 |
{ &ns->meta_size, nvme_strtoi, true, "metadata_bytes"}, |
| 2766 |
|
| 2767 |
}; |
| 2768 |
|
| 2769 |
ret = parse_attrs(path, ext, ARRAY_SIZE(ext)); |
| 2770 |
if (ret) |
| 2771 |
return ret; |
| 2772 |
} else { |
| 2773 |
_cleanup_free_ struct nvme_id_ns *id = NULL; |
| 2774 |
uint8_t flbas; |
| 2775 |
|
| 2776 |
id = __nvme_alloc(sizeof(*ns)); |
| 2777 |
if (!id) |
| 2778 |
return -ENOMEM; |
| 2779 |
|
| 2780 |
ret = nvme_ns_identify(ns, id); |
| 2781 |
if (ret) |
| 2782 |
return ret; |
| 2783 |
|
| 2784 |
nvme_id_ns_flbas_to_lbaf_inuse(id->flbas, &flbas); |
| 2785 |
ns->lba_count = le64_to_cpu(id->nsze); |
| 2786 |
ns->lba_util = le64_to_cpu(id->nuse); |
| 2787 |
ns->meta_size = le16_to_cpu(id->lbaf[flbas].ms); |
| 2788 |
} |
| 2789 |
|
| 2790 |
return 0; |
| 2791 |
} |
| 2792 |
|
| 2793 |
static void nvme_ns_set_generic_name(struct nvme_ns *n, const char *name) |
| 2794 |
{ |
| 2795 |
char generic_name[PATH_MAX]; |
| 2796 |
int instance, head_instance; |
| 2797 |
int ret; |
| 2798 |
|
| 2799 |
ret = sscanf(name, "nvme%dn%d", &instance, &head_instance); |
| 2800 |
if (ret != 2) |
| 2801 |
return; |
| 2802 |
|
| 2803 |
sprintf(generic_name, "ng%dn%d", instance, head_instance); |
| 2804 |
n->generic_name = strdup(generic_name); |
| 2805 |
} |
| 2806 |
|
| 2807 |
static nvme_ns_t nvme_ns_open(const char *sys_path, const char *name) |
| 2808 |
{ |
| 2809 |
int ret; |
| 2810 |
struct nvme_ns *n; |
| 2811 |
struct nvme_ns_head *head; |
| 2812 |
struct stat arg; |
| 2813 |
_cleanup_free_ char *path = NULL; |
| 2814 |
|
| 2815 |
n = calloc(1, sizeof(*n)); |
| 2816 |
if (!n) { |
| 2817 |
errno = ENOMEM; |
| 2818 |
return NULL; |
| 2819 |
} |
| 2820 |
|
| 2821 |
head = calloc(1, sizeof(*head)); |
| 2822 |
if (!head) { |
| 2823 |
errno = ENOMEM; |
| 2824 |
free(n); |
| 2825 |
return NULL; |
| 2826 |
} |
| 2827 |
|
| 2828 |
head->n = n; |
| 2829 |
list_head_init(&head->paths); |
| 2830 |
ret = asprintf(&path, "%s/%s", sys_path, "multipath"); |
| 2831 |
if (ret < 0) { |
| 2832 |
errno = ENOMEM; |
| 2833 |
goto free_ns_head; |
| 2834 |
} |
| 2835 |
/* |
| 2836 |
* The sysfs-dir "multipath" is available only when nvme multipath |
| 2837 |
* is configured and we're running kernel version >= 6.14. |
| 2838 |
*/ |
| 2839 |
ret = stat(path, &arg); |
| 2840 |
if (ret == 0) { |
| 2841 |
head->sysfs_dir = path; |
| 2842 |
path = NULL; |
| 2843 |
} else |
| 2844 |
head->sysfs_dir = NULL; |
| 2845 |
|
| 2846 |
n->head = head; |
| 2847 |
n->fd = -1; |
| 2848 |
n->name = strdup(name); |
| 2849 |
|
| 2850 |
nvme_ns_set_generic_name(n, name); |
| 2851 |
|
| 2852 |
if (nvme_ns_init(sys_path, n) != 0) |
| 2853 |
goto free_ns; |
| 2854 |
|
| 2855 |
list_node_init(&n->entry); |
| 2856 |
|
| 2857 |
nvme_ns_release_fd(n); /* Do not leak fds */ |
| 2858 |
|
| 2859 |
return n; |
| 2860 |
|
| 2861 |
free_ns: |
| 2862 |
free(n->generic_name); |
| 2863 |
free(n->name); |
| 2864 |
free_ns_head: |
| 2865 |
free(head); |
| 2866 |
free(n); |
| 2867 |
return NULL; |
| 2868 |
} |
| 2869 |
|
| 2870 |
static inline bool nvme_ns_is_generic(const char *name) |
| 2871 |
{ |
| 2872 |
int instance, head_instance; |
| 2873 |
|
| 2874 |
if (sscanf(name, "ng%dn%d", &instance, &head_instance) != 2) |
| 2875 |
return false; |
| 2876 |
return true; |
| 2877 |
} |
| 2878 |
|
| 2879 |
static char *nvme_ns_generic_to_blkdev(const char *generic) |
| 2880 |
{ |
| 2881 |
|
| 2882 |
int instance, head_instance; |
| 2883 |
char blkdev[PATH_MAX]; |
| 2884 |
|
| 2885 |
if (!nvme_ns_is_generic(generic)) |
| 2886 |
return strdup(generic); |
| 2887 |
|
| 2888 |
sscanf(generic, "ng%dn%d", &instance, &head_instance); |
| 2889 |
sprintf(blkdev, "nvme%dn%d", instance, head_instance); |
| 2890 |
|
| 2891 |
return strdup(blkdev); |
| 2892 |
} |
| 2893 |
|
| 2894 |
static struct nvme_ns *__nvme_scan_namespace(const char *sysfs_dir, const char *name) |
| 2895 |
{ |
| 2896 |
struct nvme_ns *n; |
| 2897 |
_cleanup_free_ char *path = NULL; |
| 2898 |
int ret; |
| 2899 |
_cleanup_free_ char *blkdev = NULL; |
| 2900 |
|
| 2901 |
blkdev = nvme_ns_generic_to_blkdev(name); |
| 2902 |
if (!blkdev) { |
| 2903 |
errno = ENOMEM; |
| 2904 |
return NULL; |
| 2905 |
} |
| 2906 |
|
| 2907 |
ret = asprintf(&path, "%s/%s", sysfs_dir, blkdev); |
| 2908 |
if (ret < 0) { |
| 2909 |
errno = ENOMEM; |
| 2910 |
return NULL; |
| 2911 |
} |
| 2912 |
|
| 2913 |
n = nvme_ns_open(path, blkdev); |
| 2914 |
if (!n) |
| 2915 |
return NULL; |
| 2916 |
|
| 2917 |
n->sysfs_dir = path; |
| 2918 |
path = NULL; |
| 2919 |
|
| 2920 |
return n; |
| 2921 |
} |
| 2922 |
|
| 2923 |
nvme_ns_t nvme_scan_namespace(const char *name) |
| 2924 |
{ |
| 2925 |
return __nvme_scan_namespace(nvme_ns_sysfs_dir(), name); |
| 2926 |
} |
| 2927 |
|
| 2928 |
|
| 2929 |
static void nvme_ns_head_scan_path(nvme_subsystem_t s, nvme_ns_t n, char *name) |
| 2930 |
{ |
| 2931 |
nvme_ctrl_t c; |
| 2932 |
nvme_path_t p; |
| 2933 |
|
| 2934 |
nvme_subsystem_for_each_ctrl(s, c) { |
| 2935 |
nvme_ctrl_for_each_path(c, p) { |
| 2936 |
if (!strcmp(nvme_path_get_name(p), name)) { |
| 2937 |
list_add_tail(&n->head->paths, &p->nentry); |
| 2938 |
p->n = n; |
| 2939 |
return; |
| 2940 |
} |
| 2941 |
} |
| 2942 |
} |
| 2943 |
} |
| 2944 |
|
| 2945 |
static void nvme_subsystem_set_ns_path(nvme_subsystem_t s, nvme_ns_t n) |
| 2946 |
{ |
| 2947 |
struct nvme_ns_head *head = n->head; |
| 2948 |
|
| 2949 |
if (nvme_ns_head_get_sysfs_dir(head)) { |
| 2950 |
struct dirents paths = {}; |
| 2951 |
int i; |
| 2952 |
|
| 2953 |
/* |
| 2954 |
* When multipath is configured on kernel version >= 6.15, |
| 2955 |
* we use multipath sysfs link to get each path of a namespace. |
| 2956 |
*/ |
| 2957 |
paths.num = nvme_scan_ns_head_paths(head, &paths.ents); |
| 2958 |
|
| 2959 |
for (i = 0; i < paths.num; i++) |
| 2960 |
nvme_ns_head_scan_path(s, n, paths.ents[i]->d_name); |
| 2961 |
} else { |
| 2962 |
nvme_ctrl_t c; |
| 2963 |
nvme_path_t p; |
| 2964 |
int ns_ctrl, ns_nsid, ret; |
| 2965 |
|
| 2966 |
/* |
| 2967 |
* If multipath is not configured or we're running on kernel |
| 2968 |
* version < 6.15, fallback to the old way. |
| 2969 |
*/ |
| 2970 |
ret = sscanf(nvme_ns_get_name(n), "nvme%dn%d", |
| 2971 |
&ns_ctrl, &ns_nsid); |
| 2972 |
if (ret != 2) |
| 2973 |
return; |
| 2974 |
|
| 2975 |
nvme_subsystem_for_each_ctrl(s, c) { |
| 2976 |
nvme_ctrl_for_each_path(c, p) { |
| 2977 |
int p_subsys, p_ctrl, p_nsid; |
| 2978 |
|
| 2979 |
ret = sscanf(nvme_path_get_name(p), |
| 2980 |
"nvme%dc%dn%d", |
| 2981 |
&p_subsys, &p_ctrl, &p_nsid); |
| 2982 |
if (ret != 3) |
| 2983 |
continue; |
| 2984 |
if (ns_ctrl == p_subsys && ns_nsid == p_nsid) { |
| 2985 |
list_add_tail(&head->paths, &p->nentry); |
| 2986 |
p->n = n; |
| 2987 |
} |
| 2988 |
} |
| 2989 |
} |
| 2990 |
} |
| 2991 |
} |
| 2992 |
|
| 2993 |
static int nvme_ctrl_scan_namespace(nvme_root_t r, struct nvme_ctrl *c, |
| 2994 |
char *name) |
| 2995 |
{ |
| 2996 |
struct nvme_ns *n, *_n, *__n; |
| 2997 |
|
| 2998 |
nvme_msg(r, LOG_DEBUG, "scan controller %s namespace %s\n", |
| 2999 |
c->name, name); |
| 3000 |
if (!c->s) { |
| 3001 |
nvme_msg(r, LOG_DEBUG, "no subsystem for %s\n", name); |
| 3002 |
errno = EINVAL; |
| 3003 |
return -1; |
| 3004 |
} |
| 3005 |
n = __nvme_scan_namespace(c->sysfs_dir, name); |
| 3006 |
if (!n) { |
| 3007 |
nvme_msg(r, LOG_DEBUG, "failed to scan namespace %s\n", name); |
| 3008 |
return -1; |
| 3009 |
} |
| 3010 |
nvme_ctrl_for_each_ns_safe(c, _n, __n) { |
| 3011 |
if (strcmp(n->name, _n->name)) |
| 3012 |
continue; |
| 3013 |
__nvme_free_ns(_n); |
| 3014 |
} |
| 3015 |
n->s = c->s; |
| 3016 |
n->c = c; |
| 3017 |
list_add_tail(&c->namespaces, &n->entry); |
| 3018 |
nvme_subsystem_set_ns_path(c->s, n); |
| 3019 |
|
| 3020 |
return 0; |
| 3021 |
} |
| 3022 |
|
| 3023 |
static int nvme_subsystem_scan_namespace(nvme_root_t r, nvme_subsystem_t s, |
| 3024 |
char *name) |
| 3025 |
{ |
| 3026 |
struct nvme_ns *n, *_n, *__n; |
| 3027 |
|
| 3028 |
nvme_msg(r, LOG_DEBUG, "scan subsystem %s namespace %s\n", |
| 3029 |
s->name, name); |
| 3030 |
n = __nvme_scan_namespace(s->sysfs_dir, name); |
| 3031 |
if (!n) { |
| 3032 |
nvme_msg(r, LOG_DEBUG, "failed to scan namespace %s\n", name); |
| 3033 |
return -1; |
| 3034 |
} |
| 3035 |
nvme_subsystem_for_each_ns_safe(s, _n, __n) { |
| 3036 |
struct nvme_path *p, *_p; |
| 3037 |
|
| 3038 |
if (strcmp(n->name, _n->name)) |
| 3039 |
continue; |
| 3040 |
/* Detach paths */ |
| 3041 |
nvme_namespace_for_each_path_safe(_n, p, _p) { |
| 3042 |
list_del_init(&p->nentry); |
| 3043 |
p->n = NULL; |
| 3044 |
} |
| 3045 |
list_head_init(&_n->head->paths); |
| 3046 |
__nvme_free_ns(_n); |
| 3047 |
} |
| 3048 |
n->s = s; |
| 3049 |
list_add_tail(&s->namespaces, &n->entry); |
| 3050 |
nvme_subsystem_set_ns_path(s, n); |
| 3051 |
return 0; |
| 3052 |
} |
| 3053 |
|
| 3054 |
struct nvme_ns *nvme_subsystem_lookup_namespace(struct nvme_subsystem *s, |
| 3055 |
__u32 nsid) |
| 3056 |
{ |
| 3057 |
struct nvme_ns *n; |
| 3058 |
|
| 3059 |
nvme_subsystem_for_each_ns(s, n) { |
| 3060 |
if (nvme_ns_get_nsid(n) == nsid) |
| 3061 |
return n; |
| 3062 |
} |
| 3063 |
return NULL; |
| 3064 |
} |