Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2016 Intel Corporation. All rights reserved.
3 : * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
4 : * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : */
6 :
7 : #include "spdk/stdinc.h"
8 :
9 : #include "nvmf_internal.h"
10 : #include "transport.h"
11 :
12 : #include "spdk/assert.h"
13 : #include "spdk/likely.h"
14 : #include "spdk/string.h"
15 : #include "spdk/trace.h"
16 : #include "spdk/nvmf_spec.h"
17 : #include "spdk/uuid.h"
18 : #include "spdk/json.h"
19 : #include "spdk/file.h"
20 : #include "spdk/bit_array.h"
21 : #include "spdk/bdev.h"
22 :
23 : #define __SPDK_BDEV_MODULE_ONLY
24 : #include "spdk/bdev_module.h"
25 : #include "spdk/log.h"
26 : #include "spdk_internal/utf.h"
27 : #include "spdk_internal/usdt.h"
28 :
29 : #define MODEL_NUMBER_DEFAULT "SPDK bdev Controller"
30 : #define NVMF_SUBSYSTEM_DEFAULT_NAMESPACES 32
31 :
32 : /*
33 : * States for parsing valid domains in NQNs according to RFC 1034
34 : */
35 : enum spdk_nvmf_nqn_domain_states {
36 : /* First character of a domain must be a letter */
37 : SPDK_NVMF_DOMAIN_ACCEPT_LETTER = 0,
38 :
39 : /* Subsequent characters can be any of letter, digit, or hyphen */
40 : SPDK_NVMF_DOMAIN_ACCEPT_LDH = 1,
41 :
42 : /* A domain label must end with either a letter or digit */
43 : SPDK_NVMF_DOMAIN_ACCEPT_ANY = 2
44 : };
45 :
46 : static int _nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem);
47 :
48 : /* Returns true if is a valid ASCII string as defined by the NVMe spec */
49 : static bool
50 3 : nvmf_valid_ascii_string(const void *buf, size_t size)
51 : {
52 3 : const uint8_t *str = buf;
53 : size_t i;
54 :
55 35 : for (i = 0; i < size; i++) {
56 33 : if (str[i] < 0x20 || str[i] > 0x7E) {
57 1 : return false;
58 : }
59 : }
60 :
61 2 : return true;
62 : }
63 :
64 : bool
65 46 : nvmf_nqn_is_valid(const char *nqn)
66 : {
67 : size_t len;
68 46 : struct spdk_uuid uuid_value;
69 : uint32_t i;
70 : int bytes_consumed;
71 : uint32_t domain_label_length;
72 : char *reverse_domain_end;
73 : uint32_t reverse_domain_end_index;
74 46 : enum spdk_nvmf_nqn_domain_states domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER;
75 :
76 : /* Check for length requirements */
77 46 : len = strlen(nqn);
78 46 : if (len > SPDK_NVMF_NQN_MAX_LEN) {
79 1 : SPDK_ERRLOG("Invalid NQN \"%s\": length %zu > max %d\n", nqn, len, SPDK_NVMF_NQN_MAX_LEN);
80 1 : return false;
81 : }
82 :
83 : /* The nqn must be at least as long as SPDK_NVMF_NQN_MIN_LEN to contain the necessary prefix. */
84 45 : if (len < SPDK_NVMF_NQN_MIN_LEN) {
85 2 : SPDK_ERRLOG("Invalid NQN \"%s\": length %zu < min %d\n", nqn, len, SPDK_NVMF_NQN_MIN_LEN);
86 2 : return false;
87 : }
88 :
89 : /* Check for discovery controller nqn */
90 43 : if (!strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN)) {
91 2 : return true;
92 : }
93 :
94 : /* Check for equality with the generic nqn structure of the form "nqn.2014-08.org.nvmexpress:uuid:11111111-2222-3333-4444-555555555555" */
95 41 : if (!strncmp(nqn, SPDK_NVMF_NQN_UUID_PRE, SPDK_NVMF_NQN_UUID_PRE_LEN)) {
96 6 : if (len != SPDK_NVMF_NQN_UUID_PRE_LEN + SPDK_NVMF_UUID_STRING_LEN) {
97 2 : SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not the correct length\n", nqn);
98 2 : return false;
99 : }
100 :
101 4 : if (spdk_uuid_parse(&uuid_value, &nqn[SPDK_NVMF_NQN_UUID_PRE_LEN])) {
102 2 : SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not formatted correctly\n", nqn);
103 2 : return false;
104 : }
105 2 : return true;
106 : }
107 :
108 : /* If the nqn does not match the uuid structure, the next several checks validate the form "nqn.yyyy-mm.reverse.domain:user-string" */
109 :
110 35 : if (strncmp(nqn, "nqn.", 4) != 0) {
111 0 : SPDK_ERRLOG("Invalid NQN \"%s\": NQN must begin with \"nqn.\".\n", nqn);
112 0 : return false;
113 : }
114 :
115 : /* Check for yyyy-mm. */
116 35 : if (!(isdigit(nqn[4]) && isdigit(nqn[5]) && isdigit(nqn[6]) && isdigit(nqn[7]) &&
117 35 : nqn[8] == '-' && isdigit(nqn[9]) && isdigit(nqn[10]) && nqn[11] == '.')) {
118 0 : SPDK_ERRLOG("Invalid date code in NQN \"%s\"\n", nqn);
119 0 : return false;
120 : }
121 :
122 35 : reverse_domain_end = strchr(nqn, ':');
123 35 : if (reverse_domain_end != NULL && (reverse_domain_end_index = reverse_domain_end - nqn) < len - 1) {
124 : } else {
125 1 : SPDK_ERRLOG("Invalid NQN \"%s\". NQN must contain user specified name with a ':' as a prefix.\n",
126 : nqn);
127 1 : return false;
128 : }
129 :
130 : /* Check for valid reverse domain */
131 34 : domain_label_length = 0;
132 338 : for (i = 12; i < reverse_domain_end_index; i++) {
133 310 : if (domain_label_length > SPDK_DOMAIN_LABEL_MAX_LEN) {
134 1 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". At least one Label is too long.\n", nqn);
135 1 : return false;
136 : }
137 :
138 309 : switch (domain_state) {
139 :
140 69 : case SPDK_NVMF_DOMAIN_ACCEPT_LETTER: {
141 69 : if (isalpha(nqn[i])) {
142 65 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
143 65 : domain_label_length++;
144 65 : break;
145 : } else {
146 4 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must start with a letter.\n", nqn);
147 4 : return false;
148 : }
149 : }
150 :
151 4 : case SPDK_NVMF_DOMAIN_ACCEPT_LDH: {
152 4 : if (isalpha(nqn[i]) || isdigit(nqn[i])) {
153 3 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
154 3 : domain_label_length++;
155 3 : break;
156 1 : } else if (nqn[i] == '-') {
157 1 : if (i == reverse_domain_end_index - 1) {
158 0 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
159 : nqn);
160 0 : return false;
161 : }
162 1 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH;
163 1 : domain_label_length++;
164 1 : break;
165 0 : } else if (nqn[i] == '.') {
166 0 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
167 : nqn);
168 0 : return false;
169 : } else {
170 0 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n",
171 : nqn);
172 0 : return false;
173 : }
174 : }
175 :
176 236 : case SPDK_NVMF_DOMAIN_ACCEPT_ANY: {
177 236 : if (isalpha(nqn[i]) || isdigit(nqn[i])) {
178 197 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
179 197 : domain_label_length++;
180 197 : break;
181 39 : } else if (nqn[i] == '-') {
182 4 : if (i == reverse_domain_end_index - 1) {
183 1 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
184 : nqn);
185 1 : return false;
186 : }
187 3 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH;
188 3 : domain_label_length++;
189 3 : break;
190 35 : } else if (nqn[i] == '.') {
191 35 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER;
192 35 : domain_label_length = 0;
193 35 : break;
194 : } else {
195 0 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n",
196 : nqn);
197 0 : return false;
198 : }
199 : }
200 : }
201 304 : }
202 :
203 28 : i = reverse_domain_end_index + 1;
204 414 : while (i < len) {
205 387 : bytes_consumed = utf8_valid(&nqn[i], &nqn[len]);
206 387 : if (bytes_consumed <= 0) {
207 1 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only valid utf-8.\n", nqn);
208 1 : return false;
209 : }
210 :
211 386 : i += bytes_consumed;
212 : }
213 27 : return true;
214 : }
215 :
216 : static void subsystem_state_change_on_pg(struct spdk_io_channel_iter *i);
217 :
218 : struct spdk_nvmf_subsystem *
219 24 : spdk_nvmf_subsystem_create(struct spdk_nvmf_tgt *tgt,
220 : const char *nqn,
221 : enum spdk_nvmf_subtype type,
222 : uint32_t num_ns)
223 : {
224 : struct spdk_nvmf_subsystem *subsystem;
225 : uint32_t sid;
226 :
227 24 : if (spdk_nvmf_tgt_find_subsystem(tgt, nqn)) {
228 0 : SPDK_ERRLOG("Subsystem NQN '%s' already exists\n", nqn);
229 0 : return NULL;
230 : }
231 :
232 24 : if (!nvmf_nqn_is_valid(nqn)) {
233 11 : SPDK_ERRLOG("Subsystem NQN '%s' is invalid\n", nqn);
234 11 : return NULL;
235 : }
236 :
237 13 : if (type == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT ||
238 : type == SPDK_NVMF_SUBTYPE_DISCOVERY) {
239 1 : if (num_ns != 0) {
240 0 : SPDK_ERRLOG("Discovery subsystem cannot have namespaces.\n");
241 0 : return NULL;
242 : }
243 12 : } else if (num_ns == 0) {
244 12 : num_ns = NVMF_SUBSYSTEM_DEFAULT_NAMESPACES;
245 : }
246 :
247 : /* Find a free subsystem id (sid) */
248 13 : sid = spdk_bit_array_find_first_clear(tgt->subsystem_ids, 0);
249 13 : if (sid == UINT32_MAX) {
250 0 : SPDK_ERRLOG("No free subsystem IDs are available for subsystem creation\n");
251 0 : return NULL;
252 : }
253 13 : subsystem = calloc(1, sizeof(struct spdk_nvmf_subsystem));
254 13 : if (subsystem == NULL) {
255 0 : SPDK_ERRLOG("Subsystem memory allocation failed\n");
256 0 : return NULL;
257 : }
258 :
259 13 : subsystem->thread = spdk_get_thread();
260 13 : subsystem->state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
261 13 : subsystem->tgt = tgt;
262 13 : subsystem->id = sid;
263 13 : subsystem->subtype = type;
264 13 : subsystem->max_nsid = num_ns;
265 13 : subsystem->next_cntlid = 0;
266 13 : subsystem->min_cntlid = NVMF_MIN_CNTLID;
267 13 : subsystem->max_cntlid = NVMF_MAX_CNTLID;
268 13 : snprintf(subsystem->subnqn, sizeof(subsystem->subnqn), "%s", nqn);
269 13 : pthread_mutex_init(&subsystem->mutex, NULL);
270 13 : TAILQ_INIT(&subsystem->listeners);
271 13 : TAILQ_INIT(&subsystem->hosts);
272 13 : TAILQ_INIT(&subsystem->ctrlrs);
273 13 : TAILQ_INIT(&subsystem->state_changes);
274 13 : subsystem->used_listener_ids = spdk_bit_array_create(NVMF_MAX_LISTENERS_PER_SUBSYSTEM);
275 13 : if (subsystem->used_listener_ids == NULL) {
276 0 : pthread_mutex_destroy(&subsystem->mutex);
277 0 : free(subsystem);
278 0 : SPDK_ERRLOG("Listener id array memory allocation failed\n");
279 0 : return NULL;
280 : }
281 :
282 13 : if (num_ns != 0) {
283 12 : subsystem->ns = calloc(num_ns, sizeof(struct spdk_nvmf_ns *));
284 12 : if (subsystem->ns == NULL) {
285 0 : SPDK_ERRLOG("Namespace memory allocation failed\n");
286 0 : pthread_mutex_destroy(&subsystem->mutex);
287 0 : spdk_bit_array_free(&subsystem->used_listener_ids);
288 0 : free(subsystem);
289 0 : return NULL;
290 : }
291 12 : subsystem->ana_group = calloc(num_ns, sizeof(uint32_t));
292 12 : if (subsystem->ana_group == NULL) {
293 0 : SPDK_ERRLOG("ANA group memory allocation failed\n");
294 0 : pthread_mutex_destroy(&subsystem->mutex);
295 0 : free(subsystem->ns);
296 0 : spdk_bit_array_free(&subsystem->used_listener_ids);
297 0 : free(subsystem);
298 0 : return NULL;
299 : }
300 : }
301 :
302 13 : memset(subsystem->sn, '0', sizeof(subsystem->sn) - 1);
303 13 : subsystem->sn[sizeof(subsystem->sn) - 1] = '\0';
304 :
305 13 : snprintf(subsystem->mn, sizeof(subsystem->mn), "%s",
306 : MODEL_NUMBER_DEFAULT);
307 :
308 13 : spdk_bit_array_set(tgt->subsystem_ids, sid);
309 13 : RB_INSERT(subsystem_tree, &tgt->subsystems, subsystem);
310 :
311 : SPDK_DTRACE_PROBE1(nvmf_subsystem_create, subsystem->subnqn);
312 :
313 13 : return subsystem;
314 : }
315 :
316 : static void
317 3 : nvmf_host_free(struct spdk_nvmf_host *host)
318 : {
319 3 : spdk_keyring_put_key(host->dhchap_key);
320 3 : spdk_keyring_put_key(host->dhchap_ctrlr_key);
321 3 : free(host);
322 3 : }
323 :
324 : /* Must hold subsystem->mutex while calling this function */
325 : static void
326 3 : nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_host *host)
327 : {
328 3 : TAILQ_REMOVE(&subsystem->hosts, host, link);
329 3 : nvmf_host_free(host);
330 3 : }
331 :
332 : static void
333 7 : _nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem,
334 : struct spdk_nvmf_subsystem_listener *listener,
335 : bool stop)
336 : {
337 : struct spdk_nvmf_transport *transport;
338 : struct spdk_nvmf_ctrlr *ctrlr;
339 :
340 7 : if (stop) {
341 0 : transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, listener->trid->trstring);
342 0 : if (transport != NULL) {
343 0 : spdk_nvmf_transport_stop_listen(transport, listener->trid);
344 : }
345 : }
346 :
347 7 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
348 0 : if (ctrlr->listener == listener) {
349 0 : ctrlr->listener = NULL;
350 : }
351 : }
352 :
353 7 : TAILQ_REMOVE(&subsystem->listeners, listener, link);
354 7 : nvmf_update_discovery_log(listener->subsystem->tgt, NULL);
355 7 : free(listener->ana_state);
356 7 : spdk_bit_array_clear(subsystem->used_listener_ids, listener->id);
357 7 : free(listener);
358 7 : }
359 :
360 : static void
361 0 : _nvmf_subsystem_destroy_msg(void *cb_arg)
362 : {
363 0 : struct spdk_nvmf_subsystem *subsystem = cb_arg;
364 :
365 0 : _nvmf_subsystem_destroy(subsystem);
366 0 : }
367 :
368 : static int
369 13 : _nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem)
370 : {
371 : struct nvmf_subsystem_state_change_ctx *ctx;
372 : struct spdk_nvmf_ns *ns;
373 13 : nvmf_subsystem_destroy_cb async_destroy_cb = NULL;
374 13 : void *async_destroy_cb_arg = NULL;
375 : int rc;
376 :
377 13 : if (!TAILQ_EMPTY(&subsystem->ctrlrs)) {
378 0 : SPDK_DEBUGLOG(nvmf, "subsystem %p %s has active controllers\n", subsystem, subsystem->subnqn);
379 0 : subsystem->async_destroy = true;
380 0 : rc = spdk_thread_send_msg(subsystem->thread, _nvmf_subsystem_destroy_msg, subsystem);
381 0 : if (rc) {
382 0 : SPDK_ERRLOG("Failed to send thread msg, rc %d\n", rc);
383 0 : assert(0);
384 : return rc;
385 : }
386 0 : return -EINPROGRESS;
387 : }
388 :
389 13 : ns = spdk_nvmf_subsystem_get_first_ns(subsystem);
390 13 : while (ns != NULL) {
391 0 : struct spdk_nvmf_ns *next_ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns);
392 :
393 0 : spdk_nvmf_subsystem_remove_ns(subsystem, ns->opts.nsid);
394 0 : ns = next_ns;
395 : }
396 :
397 13 : while ((ctx = TAILQ_FIRST(&subsystem->state_changes))) {
398 0 : SPDK_WARNLOG("subsystem %s has pending state change requests\n", subsystem->subnqn);
399 0 : TAILQ_REMOVE(&subsystem->state_changes, ctx, link);
400 0 : if (ctx->cb_fn != NULL) {
401 0 : ctx->cb_fn(subsystem, ctx->cb_arg, -ECANCELED);
402 : }
403 0 : free(ctx);
404 : }
405 :
406 13 : free(subsystem->ns);
407 13 : free(subsystem->ana_group);
408 :
409 13 : RB_REMOVE(subsystem_tree, &subsystem->tgt->subsystems, subsystem);
410 13 : assert(spdk_bit_array_get(subsystem->tgt->subsystem_ids, subsystem->id) == true);
411 13 : spdk_bit_array_clear(subsystem->tgt->subsystem_ids, subsystem->id);
412 :
413 13 : pthread_mutex_destroy(&subsystem->mutex);
414 :
415 13 : spdk_bit_array_free(&subsystem->used_listener_ids);
416 :
417 13 : if (subsystem->async_destroy) {
418 0 : async_destroy_cb = subsystem->async_destroy_cb;
419 0 : async_destroy_cb_arg = subsystem->async_destroy_cb_arg;
420 : }
421 :
422 13 : free(subsystem);
423 :
424 13 : if (async_destroy_cb) {
425 0 : async_destroy_cb(async_destroy_cb_arg);
426 : }
427 :
428 13 : return 0;
429 : }
430 :
431 : static struct spdk_nvmf_ns *
432 0 : _nvmf_subsystem_get_first_zoned_ns(struct spdk_nvmf_subsystem *subsystem)
433 : {
434 0 : struct spdk_nvmf_ns *ns = spdk_nvmf_subsystem_get_first_ns(subsystem);
435 0 : while (ns != NULL) {
436 0 : if (ns->csi == SPDK_NVME_CSI_ZNS) {
437 0 : return ns;
438 : }
439 0 : ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns);
440 : }
441 0 : return NULL;
442 : }
443 :
444 : int
445 13 : spdk_nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem, nvmf_subsystem_destroy_cb cpl_cb,
446 : void *cpl_cb_arg)
447 : {
448 : struct spdk_nvmf_host *host, *host_tmp;
449 : struct spdk_nvmf_transport *transport;
450 :
451 13 : if (!subsystem) {
452 0 : return -EINVAL;
453 : }
454 :
455 : SPDK_DTRACE_PROBE1(nvmf_subsystem_destroy, subsystem->subnqn);
456 :
457 13 : assert(spdk_get_thread() == subsystem->thread);
458 :
459 13 : if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
460 0 : SPDK_ERRLOG("Subsystem can only be destroyed in inactive state, %s state %d\n",
461 : subsystem->subnqn, subsystem->state);
462 0 : return -EAGAIN;
463 : }
464 13 : if (subsystem->destroying) {
465 0 : SPDK_ERRLOG("Subsystem destruction is already started\n");
466 0 : assert(0);
467 : return -EALREADY;
468 : }
469 :
470 13 : subsystem->destroying = true;
471 :
472 13 : SPDK_DEBUGLOG(nvmf, "subsystem is %p %s\n", subsystem, subsystem->subnqn);
473 :
474 13 : nvmf_subsystem_remove_all_listeners(subsystem, false);
475 :
476 13 : pthread_mutex_lock(&subsystem->mutex);
477 :
478 13 : TAILQ_FOREACH_SAFE(host, &subsystem->hosts, link, host_tmp) {
479 0 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
480 0 : transport = spdk_nvmf_transport_get_next(transport)) {
481 0 : if (transport->ops->subsystem_remove_host) {
482 0 : transport->ops->subsystem_remove_host(transport, subsystem, host->nqn);
483 : }
484 : }
485 0 : nvmf_subsystem_remove_host(subsystem, host);
486 : }
487 :
488 13 : pthread_mutex_unlock(&subsystem->mutex);
489 :
490 13 : subsystem->async_destroy_cb = cpl_cb;
491 13 : subsystem->async_destroy_cb_arg = cpl_cb_arg;
492 :
493 13 : return _nvmf_subsystem_destroy(subsystem);
494 : }
495 :
496 : /* we have to use the typedef in the function declaration to appease astyle. */
497 : typedef enum spdk_nvmf_subsystem_state spdk_nvmf_subsystem_state_t;
498 :
499 : static spdk_nvmf_subsystem_state_t
500 9 : nvmf_subsystem_get_intermediate_state(enum spdk_nvmf_subsystem_state current_state,
501 : enum spdk_nvmf_subsystem_state requested_state)
502 : {
503 9 : switch (requested_state) {
504 2 : case SPDK_NVMF_SUBSYSTEM_INACTIVE:
505 2 : return SPDK_NVMF_SUBSYSTEM_DEACTIVATING;
506 4 : case SPDK_NVMF_SUBSYSTEM_ACTIVE:
507 4 : if (current_state == SPDK_NVMF_SUBSYSTEM_PAUSED) {
508 2 : return SPDK_NVMF_SUBSYSTEM_RESUMING;
509 : } else {
510 2 : return SPDK_NVMF_SUBSYSTEM_ACTIVATING;
511 : }
512 3 : case SPDK_NVMF_SUBSYSTEM_PAUSED:
513 3 : return SPDK_NVMF_SUBSYSTEM_PAUSING;
514 0 : default:
515 0 : assert(false);
516 : return SPDK_NVMF_SUBSYSTEM_NUM_STATES;
517 : }
518 : }
519 :
520 : static int
521 18 : nvmf_subsystem_set_state(struct spdk_nvmf_subsystem *subsystem,
522 : enum spdk_nvmf_subsystem_state state)
523 : {
524 18 : enum spdk_nvmf_subsystem_state actual_old_state, expected_old_state;
525 : bool exchanged;
526 :
527 18 : switch (state) {
528 2 : case SPDK_NVMF_SUBSYSTEM_INACTIVE:
529 2 : expected_old_state = SPDK_NVMF_SUBSYSTEM_DEACTIVATING;
530 2 : break;
531 2 : case SPDK_NVMF_SUBSYSTEM_ACTIVATING:
532 2 : expected_old_state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
533 2 : break;
534 4 : case SPDK_NVMF_SUBSYSTEM_ACTIVE:
535 4 : expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING;
536 4 : break;
537 3 : case SPDK_NVMF_SUBSYSTEM_PAUSING:
538 3 : expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
539 3 : break;
540 3 : case SPDK_NVMF_SUBSYSTEM_PAUSED:
541 3 : expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSING;
542 3 : break;
543 2 : case SPDK_NVMF_SUBSYSTEM_RESUMING:
544 2 : expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSED;
545 2 : break;
546 2 : case SPDK_NVMF_SUBSYSTEM_DEACTIVATING:
547 2 : expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
548 2 : break;
549 0 : default:
550 0 : assert(false);
551 : return -1;
552 : }
553 :
554 18 : actual_old_state = expected_old_state;
555 18 : exchanged = __atomic_compare_exchange_n(&subsystem->state, &actual_old_state, state, false,
556 : __ATOMIC_RELAXED, __ATOMIC_RELAXED);
557 18 : if (spdk_unlikely(exchanged == false)) {
558 3 : if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING &&
559 : state == SPDK_NVMF_SUBSYSTEM_ACTIVE) {
560 2 : expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING;
561 : }
562 : /* This is for the case when activating the subsystem fails. */
563 3 : if (actual_old_state == SPDK_NVMF_SUBSYSTEM_ACTIVATING &&
564 : state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING) {
565 0 : expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING;
566 : }
567 : /* This is for the case when resuming the subsystem fails. */
568 3 : if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING &&
569 : state == SPDK_NVMF_SUBSYSTEM_PAUSING) {
570 0 : expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING;
571 : }
572 : /* This is for the case when stopping paused subsystem */
573 3 : if (actual_old_state == SPDK_NVMF_SUBSYSTEM_PAUSED &&
574 : state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING) {
575 1 : expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSED;
576 : }
577 3 : actual_old_state = expected_old_state;
578 3 : __atomic_compare_exchange_n(&subsystem->state, &actual_old_state, state, false,
579 : __ATOMIC_RELAXED, __ATOMIC_RELAXED);
580 : }
581 18 : assert(actual_old_state == expected_old_state);
582 18 : return actual_old_state - expected_old_state;
583 : }
584 :
585 : static void nvmf_subsystem_do_state_change(struct nvmf_subsystem_state_change_ctx *ctx);
586 :
587 : static void
588 10 : _nvmf_subsystem_state_change_complete(void *_ctx)
589 : {
590 10 : struct nvmf_subsystem_state_change_ctx *next, *ctx = _ctx;
591 10 : struct spdk_nvmf_subsystem *subsystem = ctx->subsystem;
592 :
593 10 : pthread_mutex_lock(&subsystem->mutex);
594 10 : assert(TAILQ_FIRST(&subsystem->state_changes) == ctx);
595 10 : TAILQ_REMOVE(&subsystem->state_changes, ctx, link);
596 10 : next = TAILQ_FIRST(&subsystem->state_changes);
597 10 : pthread_mutex_unlock(&subsystem->mutex);
598 :
599 10 : if (ctx->cb_fn != NULL) {
600 3 : ctx->cb_fn(subsystem, ctx->cb_arg, ctx->status);
601 : }
602 10 : free(ctx);
603 :
604 10 : if (next != NULL) {
605 1 : nvmf_subsystem_do_state_change(next);
606 : }
607 10 : }
608 :
609 : static void
610 10 : nvmf_subsystem_state_change_complete(struct nvmf_subsystem_state_change_ctx *ctx, int status)
611 : {
612 10 : ctx->status = status;
613 10 : spdk_thread_exec_msg(ctx->thread, _nvmf_subsystem_state_change_complete, ctx);
614 10 : }
615 :
616 : static void
617 0 : subsystem_state_change_revert_done(struct spdk_io_channel_iter *i, int status)
618 : {
619 0 : struct nvmf_subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
620 :
621 : /* Nothing to be done here if the state setting fails, we are just screwed. */
622 0 : if (nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state)) {
623 0 : SPDK_ERRLOG("Unable to revert the subsystem state after operation failure.\n");
624 : }
625 :
626 : /* return a failure here. This function only exists in an error path. */
627 0 : nvmf_subsystem_state_change_complete(ctx, -1);
628 0 : }
629 :
630 : static void
631 9 : subsystem_state_change_done(struct spdk_io_channel_iter *i, int status)
632 : {
633 9 : struct nvmf_subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
634 : enum spdk_nvmf_subsystem_state intermediate_state;
635 :
636 : SPDK_DTRACE_PROBE4(nvmf_subsystem_change_state_done, ctx->subsystem->subnqn,
637 : ctx->requested_state, ctx->original_state, status);
638 :
639 9 : if (status == 0) {
640 9 : status = nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state);
641 9 : if (status) {
642 0 : status = -1;
643 : }
644 : }
645 :
646 9 : if (status) {
647 0 : intermediate_state = nvmf_subsystem_get_intermediate_state(ctx->requested_state,
648 : ctx->original_state);
649 0 : assert(intermediate_state != SPDK_NVMF_SUBSYSTEM_NUM_STATES);
650 :
651 0 : if (nvmf_subsystem_set_state(ctx->subsystem, intermediate_state)) {
652 0 : goto out;
653 : }
654 0 : ctx->requested_state = ctx->original_state;
655 0 : spdk_for_each_channel(ctx->subsystem->tgt,
656 : subsystem_state_change_on_pg,
657 : ctx,
658 : subsystem_state_change_revert_done);
659 0 : return;
660 : }
661 :
662 9 : out:
663 9 : nvmf_subsystem_state_change_complete(ctx, status);
664 : }
665 :
666 : static void
667 0 : subsystem_state_change_continue(void *ctx, int status)
668 : {
669 0 : struct spdk_io_channel_iter *i = ctx;
670 : struct nvmf_subsystem_state_change_ctx *_ctx __attribute__((unused));
671 :
672 0 : _ctx = spdk_io_channel_iter_get_ctx(i);
673 : SPDK_DTRACE_PROBE3(nvmf_pg_change_state_done, _ctx->subsystem->subnqn,
674 : _ctx->requested_state, spdk_thread_get_id(spdk_get_thread()));
675 :
676 0 : spdk_for_each_channel_continue(i, status);
677 0 : }
678 :
679 : static void
680 0 : subsystem_state_change_on_pg(struct spdk_io_channel_iter *i)
681 : {
682 : struct nvmf_subsystem_state_change_ctx *ctx;
683 : struct spdk_io_channel *ch;
684 : struct spdk_nvmf_poll_group *group;
685 :
686 0 : ctx = spdk_io_channel_iter_get_ctx(i);
687 0 : ch = spdk_io_channel_iter_get_channel(i);
688 0 : group = spdk_io_channel_get_ctx(ch);
689 :
690 : SPDK_DTRACE_PROBE3(nvmf_pg_change_state, ctx->subsystem->subnqn,
691 : ctx->requested_state, spdk_thread_get_id(spdk_get_thread()));
692 0 : switch (ctx->requested_state) {
693 0 : case SPDK_NVMF_SUBSYSTEM_INACTIVE:
694 0 : nvmf_poll_group_remove_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
695 0 : break;
696 0 : case SPDK_NVMF_SUBSYSTEM_ACTIVE:
697 0 : if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_ACTIVATING) {
698 0 : nvmf_poll_group_add_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
699 0 : } else if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_RESUMING) {
700 0 : nvmf_poll_group_resume_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
701 : }
702 0 : break;
703 0 : case SPDK_NVMF_SUBSYSTEM_PAUSED:
704 0 : nvmf_poll_group_pause_subsystem(group, ctx->subsystem, ctx->nsid, subsystem_state_change_continue,
705 : i);
706 0 : break;
707 0 : default:
708 0 : assert(false);
709 : break;
710 : }
711 0 : }
712 :
713 : static void
714 10 : nvmf_subsystem_do_state_change(struct nvmf_subsystem_state_change_ctx *ctx)
715 : {
716 10 : struct spdk_nvmf_subsystem *subsystem = ctx->subsystem;
717 : enum spdk_nvmf_subsystem_state intermediate_state;
718 : int rc;
719 :
720 : SPDK_DTRACE_PROBE3(nvmf_subsystem_change_state, subsystem->subnqn,
721 : ctx->requested_state, subsystem->state);
722 :
723 : /* If we are already in the requested state, just call the callback immediately. */
724 10 : if (subsystem->state == ctx->requested_state) {
725 1 : nvmf_subsystem_state_change_complete(ctx, 0);
726 1 : return;
727 : }
728 :
729 9 : intermediate_state = nvmf_subsystem_get_intermediate_state(subsystem->state,
730 : ctx->requested_state);
731 9 : assert(intermediate_state != SPDK_NVMF_SUBSYSTEM_NUM_STATES);
732 :
733 9 : ctx->original_state = subsystem->state;
734 9 : rc = nvmf_subsystem_set_state(subsystem, intermediate_state);
735 9 : if (rc) {
736 0 : nvmf_subsystem_state_change_complete(ctx, -1);
737 0 : return;
738 : }
739 :
740 9 : spdk_for_each_channel(subsystem->tgt,
741 : subsystem_state_change_on_pg,
742 : ctx,
743 : subsystem_state_change_done);
744 : }
745 :
746 :
747 : static int
748 10 : nvmf_subsystem_state_change(struct spdk_nvmf_subsystem *subsystem,
749 : uint32_t nsid,
750 : enum spdk_nvmf_subsystem_state requested_state,
751 : spdk_nvmf_subsystem_state_change_done cb_fn,
752 : void *cb_arg)
753 : {
754 : struct nvmf_subsystem_state_change_ctx *ctx;
755 : struct spdk_thread *thread;
756 :
757 10 : thread = spdk_get_thread();
758 10 : if (thread == NULL) {
759 0 : return -EINVAL;
760 : }
761 :
762 10 : ctx = calloc(1, sizeof(*ctx));
763 10 : if (!ctx) {
764 0 : return -ENOMEM;
765 : }
766 :
767 10 : ctx->subsystem = subsystem;
768 10 : ctx->nsid = nsid;
769 10 : ctx->requested_state = requested_state;
770 10 : ctx->cb_fn = cb_fn;
771 10 : ctx->cb_arg = cb_arg;
772 10 : ctx->thread = thread;
773 :
774 10 : pthread_mutex_lock(&subsystem->mutex);
775 10 : TAILQ_INSERT_TAIL(&subsystem->state_changes, ctx, link);
776 10 : if (ctx != TAILQ_FIRST(&subsystem->state_changes)) {
777 1 : pthread_mutex_unlock(&subsystem->mutex);
778 1 : return 0;
779 : }
780 9 : pthread_mutex_unlock(&subsystem->mutex);
781 :
782 9 : nvmf_subsystem_do_state_change(ctx);
783 :
784 9 : return 0;
785 : }
786 :
787 : int
788 2 : spdk_nvmf_subsystem_start(struct spdk_nvmf_subsystem *subsystem,
789 : spdk_nvmf_subsystem_state_change_done cb_fn,
790 : void *cb_arg)
791 : {
792 2 : return nvmf_subsystem_state_change(subsystem, 0, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg);
793 : }
794 :
795 : int
796 3 : spdk_nvmf_subsystem_stop(struct spdk_nvmf_subsystem *subsystem,
797 : spdk_nvmf_subsystem_state_change_done cb_fn,
798 : void *cb_arg)
799 : {
800 3 : return nvmf_subsystem_state_change(subsystem, 0, SPDK_NVMF_SUBSYSTEM_INACTIVE, cb_fn, cb_arg);
801 : }
802 :
803 : int
804 3 : spdk_nvmf_subsystem_pause(struct spdk_nvmf_subsystem *subsystem,
805 : uint32_t nsid,
806 : spdk_nvmf_subsystem_state_change_done cb_fn,
807 : void *cb_arg)
808 : {
809 3 : return nvmf_subsystem_state_change(subsystem, nsid, SPDK_NVMF_SUBSYSTEM_PAUSED, cb_fn, cb_arg);
810 : }
811 :
812 : int
813 2 : spdk_nvmf_subsystem_resume(struct spdk_nvmf_subsystem *subsystem,
814 : spdk_nvmf_subsystem_state_change_done cb_fn,
815 : void *cb_arg)
816 : {
817 2 : return nvmf_subsystem_state_change(subsystem, 0, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg);
818 : }
819 :
820 : struct spdk_nvmf_subsystem *
821 33 : spdk_nvmf_subsystem_get_first(struct spdk_nvmf_tgt *tgt)
822 : {
823 33 : return RB_MIN(subsystem_tree, &tgt->subsystems);
824 : }
825 :
826 : struct spdk_nvmf_subsystem *
827 32 : spdk_nvmf_subsystem_get_next(struct spdk_nvmf_subsystem *subsystem)
828 : {
829 32 : if (!subsystem) {
830 0 : return NULL;
831 : }
832 :
833 32 : return RB_NEXT(subsystem_tree, &tgt->subsystems, subsystem);
834 : }
835 :
836 : static int
837 2 : nvmf_ns_add_host(struct spdk_nvmf_ns *ns, const char *hostnqn)
838 : {
839 : struct spdk_nvmf_host *host;
840 :
841 2 : host = calloc(1, sizeof(*host));
842 2 : if (!host) {
843 0 : return -ENOMEM;
844 : }
845 2 : snprintf(host->nqn, sizeof(host->nqn), "%s", hostnqn);
846 2 : TAILQ_INSERT_HEAD(&ns->hosts, host, link);
847 2 : return 0;
848 : }
849 :
850 : static void
851 2 : nvmf_ns_remove_host(struct spdk_nvmf_ns *ns, struct spdk_nvmf_host *host)
852 : {
853 2 : TAILQ_REMOVE(&ns->hosts, host, link);
854 2 : free(host);
855 2 : }
856 :
857 : static void
858 3 : _async_event_ns_notice(void *_ctrlr)
859 : {
860 3 : struct spdk_nvmf_ctrlr *ctrlr = _ctrlr;
861 :
862 3 : nvmf_ctrlr_async_event_ns_notice(ctrlr);
863 3 : }
864 :
865 : static void
866 3 : send_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr)
867 : {
868 3 : spdk_thread_send_msg(ctrlr->thread, _async_event_ns_notice, ctrlr);
869 3 : }
870 :
871 : static int
872 14 : nvmf_ns_visible(struct spdk_nvmf_subsystem *subsystem,
873 : uint32_t nsid,
874 : const char *hostnqn,
875 : bool visible)
876 : {
877 : struct spdk_nvmf_ns *ns;
878 : struct spdk_nvmf_ctrlr *ctrlr;
879 : struct spdk_nvmf_host *host;
880 : int rc;
881 :
882 14 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
883 0 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
884 0 : assert(false);
885 : return -1;
886 : }
887 :
888 14 : if (hostnqn == NULL || !nvmf_nqn_is_valid(hostnqn)) {
889 3 : return -EINVAL;
890 : }
891 :
892 11 : if (nsid == 0 || nsid > subsystem->max_nsid) {
893 2 : return -EINVAL;
894 : }
895 :
896 9 : ns = subsystem->ns[nsid - 1];
897 9 : if (!ns) {
898 2 : return -ENOENT;
899 : }
900 :
901 7 : if (ns->always_visible) {
902 : /* No individual host control */
903 2 : return -EPERM;
904 : }
905 :
906 : /* Save host info to use for any future controllers. */
907 5 : host = nvmf_ns_find_host(ns, hostnqn);
908 5 : if (visible && host == NULL) {
909 2 : rc = nvmf_ns_add_host(ns, hostnqn);
910 2 : if (rc) {
911 0 : return rc;
912 : }
913 3 : } else if (!visible && host != NULL) {
914 1 : nvmf_ns_remove_host(ns, host);
915 : }
916 :
917 : /* Also apply to existing controllers. */
918 15 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
919 15 : if (strcmp(hostnqn, ctrlr->hostnqn) ||
920 5 : spdk_bit_array_get(ctrlr->visible_ns, nsid - 1) == visible) {
921 7 : continue;
922 : }
923 3 : if (visible) {
924 2 : spdk_bit_array_set(ctrlr->visible_ns, nsid - 1);
925 : } else {
926 1 : spdk_bit_array_clear(ctrlr->visible_ns, nsid - 1);
927 : }
928 3 : send_async_event_ns_notice(ctrlr);
929 3 : nvmf_ctrlr_ns_changed(ctrlr, nsid);
930 : }
931 :
932 5 : return 0;
933 : }
934 :
935 : int
936 8 : spdk_nvmf_ns_add_host(struct spdk_nvmf_subsystem *subsystem,
937 : uint32_t nsid,
938 : const char *hostnqn,
939 : uint32_t flags)
940 : {
941 : SPDK_DTRACE_PROBE4(spdk_nvmf_ns_add_host,
942 : subsystem->subnqn,
943 : nsid,
944 : hostnqn,
945 : flags);
946 8 : return nvmf_ns_visible(subsystem, nsid, hostnqn, true);
947 : }
948 :
949 : int
950 6 : spdk_nvmf_ns_remove_host(struct spdk_nvmf_subsystem *subsystem,
951 : uint32_t nsid,
952 : const char *hostnqn,
953 : uint32_t flags)
954 : {
955 : SPDK_DTRACE_PROBE4(spdk_nvmf_ns_remove_host,
956 : subsystem->subnqn,
957 : nsid,
958 : hostnqn,
959 : flags);
960 6 : return nvmf_ns_visible(subsystem, nsid, hostnqn, false);
961 : }
962 :
963 : /* Must hold subsystem->mutex while calling this function */
964 : static struct spdk_nvmf_host *
965 14 : nvmf_subsystem_find_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
966 : {
967 14 : struct spdk_nvmf_host *host = NULL;
968 :
969 14 : TAILQ_FOREACH(host, &subsystem->hosts, link) {
970 9 : if (strcmp(hostnqn, host->nqn) == 0) {
971 9 : return host;
972 : }
973 : }
974 :
975 5 : return NULL;
976 : }
977 :
978 : int
979 4 : spdk_nvmf_subsystem_add_host_ext(struct spdk_nvmf_subsystem *subsystem,
980 : const char *hostnqn, struct spdk_nvmf_host_opts *opts)
981 : {
982 : struct spdk_nvmf_host *host;
983 : struct spdk_nvmf_transport *transport;
984 : struct spdk_key *key;
985 : int rc;
986 :
987 4 : if (!nvmf_nqn_is_valid(hostnqn)) {
988 0 : return -EINVAL;
989 : }
990 :
991 4 : pthread_mutex_lock(&subsystem->mutex);
992 :
993 4 : if (nvmf_subsystem_find_host(subsystem, hostnqn)) {
994 : /* This subsystem already allows the specified host. */
995 1 : pthread_mutex_unlock(&subsystem->mutex);
996 1 : return 0;
997 : }
998 :
999 3 : host = calloc(1, sizeof(*host));
1000 3 : if (!host) {
1001 0 : pthread_mutex_unlock(&subsystem->mutex);
1002 0 : return -ENOMEM;
1003 : }
1004 :
1005 3 : key = SPDK_GET_FIELD(opts, dhchap_key, NULL);
1006 3 : if (key != NULL) {
1007 0 : if (!nvmf_auth_is_supported()) {
1008 0 : SPDK_ERRLOG("NVMe in-band authentication is unsupported\n");
1009 0 : pthread_mutex_unlock(&subsystem->mutex);
1010 0 : nvmf_host_free(host);
1011 0 : return -EINVAL;
1012 : }
1013 0 : host->dhchap_key = spdk_key_dup(key);
1014 0 : if (host->dhchap_key == NULL) {
1015 0 : pthread_mutex_unlock(&subsystem->mutex);
1016 0 : nvmf_host_free(host);
1017 0 : return -EINVAL;
1018 : }
1019 0 : key = SPDK_GET_FIELD(opts, dhchap_ctrlr_key, NULL);
1020 0 : if (key != NULL) {
1021 0 : host->dhchap_ctrlr_key = spdk_key_dup(key);
1022 0 : if (host->dhchap_ctrlr_key == NULL) {
1023 0 : pthread_mutex_unlock(&subsystem->mutex);
1024 0 : nvmf_host_free(host);
1025 0 : return -EINVAL;
1026 : }
1027 : }
1028 3 : } else if (SPDK_GET_FIELD(opts, dhchap_ctrlr_key, NULL) != NULL) {
1029 0 : SPDK_ERRLOG("DH-HMAC-CHAP controller key requires host key to be set\n");
1030 0 : pthread_mutex_unlock(&subsystem->mutex);
1031 0 : nvmf_host_free(host);
1032 0 : return -EINVAL;
1033 : }
1034 :
1035 3 : snprintf(host->nqn, sizeof(host->nqn), "%s", hostnqn);
1036 :
1037 : SPDK_DTRACE_PROBE2(nvmf_subsystem_add_host, subsystem->subnqn, host->nqn);
1038 :
1039 3 : TAILQ_INSERT_HEAD(&subsystem->hosts, host, link);
1040 :
1041 3 : if (!TAILQ_EMPTY(&subsystem->listeners)) {
1042 0 : nvmf_update_discovery_log(subsystem->tgt, hostnqn);
1043 : }
1044 :
1045 3 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1046 0 : transport = spdk_nvmf_transport_get_next(transport)) {
1047 1 : if (transport->ops->subsystem_add_host) {
1048 1 : rc = transport->ops->subsystem_add_host(transport, subsystem, hostnqn,
1049 1 : SPDK_GET_FIELD(opts, params, NULL));
1050 1 : if (rc) {
1051 1 : SPDK_ERRLOG("Unable to add host to %s transport\n", transport->ops->name);
1052 : /* Remove this host from all transports we've managed to add it to. */
1053 1 : pthread_mutex_unlock(&subsystem->mutex);
1054 1 : spdk_nvmf_subsystem_remove_host(subsystem, hostnqn);
1055 1 : return rc;
1056 : }
1057 : }
1058 : }
1059 :
1060 2 : pthread_mutex_unlock(&subsystem->mutex);
1061 :
1062 2 : return 0;
1063 : }
1064 :
1065 : int
1066 4 : spdk_nvmf_subsystem_add_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn,
1067 : const struct spdk_json_val *params)
1068 : {
1069 4 : struct spdk_nvmf_host_opts opts = {};
1070 :
1071 4 : opts.size = SPDK_SIZEOF(&opts, params);
1072 4 : opts.params = params;
1073 :
1074 4 : return spdk_nvmf_subsystem_add_host_ext(subsystem, hostnqn, &opts);
1075 : }
1076 :
1077 : int
1078 4 : spdk_nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
1079 : {
1080 : struct spdk_nvmf_host *host;
1081 : struct spdk_nvmf_transport *transport;
1082 :
1083 4 : pthread_mutex_lock(&subsystem->mutex);
1084 :
1085 4 : host = nvmf_subsystem_find_host(subsystem, hostnqn);
1086 4 : if (host == NULL) {
1087 1 : pthread_mutex_unlock(&subsystem->mutex);
1088 1 : return -ENOENT;
1089 : }
1090 :
1091 : SPDK_DTRACE_PROBE2(nvmf_subsystem_remove_host, subsystem->subnqn, host->nqn);
1092 :
1093 3 : nvmf_subsystem_remove_host(subsystem, host);
1094 :
1095 3 : if (!TAILQ_EMPTY(&subsystem->listeners)) {
1096 1 : nvmf_update_discovery_log(subsystem->tgt, hostnqn);
1097 : }
1098 :
1099 4 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1100 1 : transport = spdk_nvmf_transport_get_next(transport)) {
1101 1 : if (transport->ops->subsystem_remove_host) {
1102 0 : transport->ops->subsystem_remove_host(transport, subsystem, hostnqn);
1103 : }
1104 : }
1105 :
1106 3 : pthread_mutex_unlock(&subsystem->mutex);
1107 :
1108 3 : return 0;
1109 : }
1110 :
1111 : struct nvmf_subsystem_disconnect_host_ctx {
1112 : struct spdk_nvmf_subsystem *subsystem;
1113 : char *hostnqn;
1114 : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn;
1115 : void *cb_arg;
1116 : };
1117 :
1118 : static void
1119 0 : nvmf_subsystem_disconnect_host_fini(struct spdk_io_channel_iter *i, int status)
1120 : {
1121 : struct nvmf_subsystem_disconnect_host_ctx *ctx;
1122 :
1123 0 : ctx = spdk_io_channel_iter_get_ctx(i);
1124 :
1125 0 : if (ctx->cb_fn) {
1126 0 : ctx->cb_fn(ctx->cb_arg, status);
1127 : }
1128 0 : free(ctx->hostnqn);
1129 0 : free(ctx);
1130 0 : }
1131 :
1132 : static void
1133 0 : nvmf_subsystem_disconnect_qpairs_by_host(struct spdk_io_channel_iter *i)
1134 : {
1135 : struct nvmf_subsystem_disconnect_host_ctx *ctx;
1136 : struct spdk_nvmf_poll_group *group;
1137 : struct spdk_io_channel *ch;
1138 : struct spdk_nvmf_qpair *qpair, *tmp_qpair;
1139 : struct spdk_nvmf_ctrlr *ctrlr;
1140 :
1141 0 : ctx = spdk_io_channel_iter_get_ctx(i);
1142 0 : ch = spdk_io_channel_iter_get_channel(i);
1143 0 : group = spdk_io_channel_get_ctx(ch);
1144 :
1145 0 : TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, tmp_qpair) {
1146 0 : ctrlr = qpair->ctrlr;
1147 :
1148 0 : if (ctrlr == NULL || ctrlr->subsys != ctx->subsystem) {
1149 0 : continue;
1150 : }
1151 :
1152 0 : if (strncmp(ctrlr->hostnqn, ctx->hostnqn, sizeof(ctrlr->hostnqn)) == 0) {
1153 : /* Right now this does not wait for the queue pairs to actually disconnect. */
1154 0 : spdk_nvmf_qpair_disconnect(qpair);
1155 : }
1156 : }
1157 0 : spdk_for_each_channel_continue(i, 0);
1158 0 : }
1159 :
1160 : int
1161 0 : spdk_nvmf_subsystem_disconnect_host(struct spdk_nvmf_subsystem *subsystem,
1162 : const char *hostnqn,
1163 : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
1164 : void *cb_arg)
1165 : {
1166 : struct nvmf_subsystem_disconnect_host_ctx *ctx;
1167 :
1168 0 : ctx = calloc(1, sizeof(struct nvmf_subsystem_disconnect_host_ctx));
1169 0 : if (ctx == NULL) {
1170 0 : return -ENOMEM;
1171 : }
1172 :
1173 0 : ctx->hostnqn = strdup(hostnqn);
1174 0 : if (ctx->hostnqn == NULL) {
1175 0 : free(ctx);
1176 0 : return -ENOMEM;
1177 : }
1178 :
1179 0 : ctx->subsystem = subsystem;
1180 0 : ctx->cb_fn = cb_fn;
1181 0 : ctx->cb_arg = cb_arg;
1182 :
1183 0 : spdk_for_each_channel(subsystem->tgt, nvmf_subsystem_disconnect_qpairs_by_host, ctx,
1184 : nvmf_subsystem_disconnect_host_fini);
1185 :
1186 0 : return 0;
1187 : }
1188 :
1189 : int
1190 0 : spdk_nvmf_subsystem_set_allow_any_host(struct spdk_nvmf_subsystem *subsystem, bool allow_any_host)
1191 : {
1192 0 : pthread_mutex_lock(&subsystem->mutex);
1193 0 : subsystem->allow_any_host = allow_any_host;
1194 0 : if (!TAILQ_EMPTY(&subsystem->listeners)) {
1195 0 : nvmf_update_discovery_log(subsystem->tgt, NULL);
1196 : }
1197 0 : pthread_mutex_unlock(&subsystem->mutex);
1198 :
1199 0 : return 0;
1200 : }
1201 :
1202 : bool
1203 0 : spdk_nvmf_subsystem_get_allow_any_host(const struct spdk_nvmf_subsystem *subsystem)
1204 : {
1205 : bool allow_any_host;
1206 : struct spdk_nvmf_subsystem *sub;
1207 :
1208 : /* Technically, taking the mutex modifies data in the subsystem. But the const
1209 : * is still important to convey that this doesn't mutate any other data. Cast
1210 : * it away to work around this. */
1211 0 : sub = (struct spdk_nvmf_subsystem *)subsystem;
1212 :
1213 0 : pthread_mutex_lock(&sub->mutex);
1214 0 : allow_any_host = sub->allow_any_host;
1215 0 : pthread_mutex_unlock(&sub->mutex);
1216 :
1217 0 : return allow_any_host;
1218 : }
1219 :
1220 : bool
1221 31 : spdk_nvmf_subsystem_host_allowed(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
1222 : {
1223 : bool allowed;
1224 :
1225 31 : if (!hostnqn) {
1226 0 : return false;
1227 : }
1228 :
1229 31 : pthread_mutex_lock(&subsystem->mutex);
1230 :
1231 31 : if (subsystem->allow_any_host) {
1232 25 : pthread_mutex_unlock(&subsystem->mutex);
1233 25 : return true;
1234 : }
1235 :
1236 6 : allowed = nvmf_subsystem_find_host(subsystem, hostnqn) != NULL;
1237 6 : pthread_mutex_unlock(&subsystem->mutex);
1238 :
1239 6 : return allowed;
1240 : }
1241 :
1242 : bool
1243 0 : nvmf_subsystem_host_auth_required(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
1244 : {
1245 : struct spdk_nvmf_host *host;
1246 : bool status;
1247 :
1248 0 : pthread_mutex_lock(&subsystem->mutex);
1249 0 : host = nvmf_subsystem_find_host(subsystem, hostnqn);
1250 0 : status = host != NULL && host->dhchap_key != NULL;
1251 0 : pthread_mutex_unlock(&subsystem->mutex);
1252 :
1253 0 : return status;
1254 : }
1255 :
1256 : struct spdk_key *
1257 0 : nvmf_subsystem_get_dhchap_key(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn,
1258 : enum nvmf_auth_key_type type)
1259 : {
1260 : struct spdk_nvmf_host *host;
1261 0 : struct spdk_key *key = NULL;
1262 :
1263 0 : pthread_mutex_lock(&subsystem->mutex);
1264 0 : host = nvmf_subsystem_find_host(subsystem, hostnqn);
1265 0 : if (host != NULL) {
1266 0 : switch (type) {
1267 0 : case NVMF_AUTH_KEY_HOST:
1268 0 : key = host->dhchap_key;
1269 0 : break;
1270 0 : case NVMF_AUTH_KEY_CTRLR:
1271 0 : key = host->dhchap_ctrlr_key;
1272 0 : break;
1273 : }
1274 0 : if (key != NULL) {
1275 0 : key = spdk_key_dup(key);
1276 : }
1277 : }
1278 0 : pthread_mutex_unlock(&subsystem->mutex);
1279 :
1280 0 : return key;
1281 : }
1282 :
1283 : struct spdk_nvmf_host *
1284 0 : spdk_nvmf_subsystem_get_first_host(struct spdk_nvmf_subsystem *subsystem)
1285 : {
1286 0 : return TAILQ_FIRST(&subsystem->hosts);
1287 : }
1288 :
1289 :
1290 : struct spdk_nvmf_host *
1291 0 : spdk_nvmf_subsystem_get_next_host(struct spdk_nvmf_subsystem *subsystem,
1292 : struct spdk_nvmf_host *prev_host)
1293 : {
1294 0 : return TAILQ_NEXT(prev_host, link);
1295 : }
1296 :
1297 : const char *
1298 0 : spdk_nvmf_host_get_nqn(const struct spdk_nvmf_host *host)
1299 : {
1300 0 : return host->nqn;
1301 : }
1302 :
1303 : struct spdk_nvmf_subsystem_listener *
1304 7 : nvmf_subsystem_find_listener(struct spdk_nvmf_subsystem *subsystem,
1305 : const struct spdk_nvme_transport_id *trid)
1306 : {
1307 : struct spdk_nvmf_subsystem_listener *listener;
1308 :
1309 22 : TAILQ_FOREACH(listener, &subsystem->listeners, link) {
1310 15 : if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) {
1311 0 : return listener;
1312 : }
1313 : }
1314 :
1315 7 : return NULL;
1316 : }
1317 :
1318 : /**
1319 : * Function to be called once the target is listening.
1320 : *
1321 : * \param ctx Context argument passed to this function.
1322 : * \param status 0 if it completed successfully, or negative errno if it failed.
1323 : */
1324 : static void
1325 7 : _nvmf_subsystem_add_listener_done(void *ctx, int status)
1326 : {
1327 7 : struct spdk_nvmf_subsystem_listener *listener = ctx;
1328 :
1329 7 : if (status) {
1330 0 : listener->cb_fn(listener->cb_arg, status);
1331 0 : free(listener);
1332 0 : return;
1333 : }
1334 :
1335 7 : TAILQ_INSERT_HEAD(&listener->subsystem->listeners, listener, link);
1336 7 : nvmf_update_discovery_log(listener->subsystem->tgt, NULL);
1337 7 : listener->cb_fn(listener->cb_arg, status);
1338 : }
1339 :
1340 : void
1341 7 : spdk_nvmf_subsystem_listener_opts_init(struct spdk_nvmf_listener_opts *opts, size_t size)
1342 : {
1343 7 : if (opts == NULL) {
1344 0 : SPDK_ERRLOG("opts should not be NULL\n");
1345 0 : assert(false);
1346 : return;
1347 : }
1348 7 : if (size == 0) {
1349 0 : SPDK_ERRLOG("size should not be zero\n");
1350 0 : assert(false);
1351 : return;
1352 : }
1353 :
1354 7 : memset(opts, 0, size);
1355 7 : opts->opts_size = size;
1356 :
1357 : #define FIELD_OK(field) \
1358 : offsetof(struct spdk_nvmf_listener_opts, field) + sizeof(opts->field) <= size
1359 :
1360 : #define SET_FIELD(field, value) \
1361 : if (FIELD_OK(field)) { \
1362 : opts->field = value; \
1363 : } \
1364 :
1365 7 : SET_FIELD(secure_channel, false);
1366 7 : SET_FIELD(ana_state, SPDK_NVME_ANA_OPTIMIZED_STATE);
1367 :
1368 : #undef FIELD_OK
1369 : #undef SET_FIELD
1370 : }
1371 :
1372 : static int
1373 0 : listener_opts_copy(struct spdk_nvmf_listener_opts *src, struct spdk_nvmf_listener_opts *dst)
1374 : {
1375 0 : if (src->opts_size == 0) {
1376 0 : SPDK_ERRLOG("source structure size should not be zero\n");
1377 0 : assert(false);
1378 : return -EINVAL;
1379 : }
1380 :
1381 0 : memset(dst, 0, sizeof(*dst));
1382 0 : dst->opts_size = src->opts_size;
1383 :
1384 : #define FIELD_OK(field) \
1385 : offsetof(struct spdk_nvmf_listener_opts, field) + sizeof(src->field) <= src->opts_size
1386 :
1387 : #define SET_FIELD(field) \
1388 : if (FIELD_OK(field)) { \
1389 : dst->field = src->field; \
1390 : } \
1391 :
1392 0 : SET_FIELD(secure_channel);
1393 0 : SET_FIELD(ana_state);
1394 : /* We should not remove this statement, but need to update the assert statement
1395 : * if we add a new field, and also add a corresponding SET_FIELD statement. */
1396 : SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_listener_opts) == 16, "Incorrect size");
1397 :
1398 : #undef SET_FIELD
1399 : #undef FIELD_OK
1400 :
1401 0 : return 0;
1402 : }
1403 :
1404 : static void
1405 7 : _nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem,
1406 : struct spdk_nvme_transport_id *trid,
1407 : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
1408 : void *cb_arg, struct spdk_nvmf_listener_opts *opts)
1409 : {
1410 : struct spdk_nvmf_transport *transport;
1411 : struct spdk_nvmf_subsystem_listener *listener;
1412 : struct spdk_nvmf_listener *tr_listener;
1413 : uint32_t i;
1414 : uint32_t id;
1415 7 : int rc = 0;
1416 :
1417 7 : assert(cb_fn != NULL);
1418 :
1419 7 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1420 0 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1421 0 : cb_fn(cb_arg, -EAGAIN);
1422 0 : return;
1423 : }
1424 :
1425 7 : if (nvmf_subsystem_find_listener(subsystem, trid)) {
1426 : /* Listener already exists in this subsystem */
1427 0 : cb_fn(cb_arg, 0);
1428 0 : return;
1429 : }
1430 :
1431 7 : transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, trid->trstring);
1432 7 : if (!transport) {
1433 0 : SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
1434 : trid->trstring);
1435 0 : cb_fn(cb_arg, -EINVAL);
1436 0 : return;
1437 : }
1438 :
1439 7 : tr_listener = nvmf_transport_find_listener(transport, trid);
1440 7 : if (!tr_listener) {
1441 0 : SPDK_ERRLOG("Cannot find transport listener for %s\n", trid->traddr);
1442 0 : cb_fn(cb_arg, -EINVAL);
1443 0 : return;
1444 : }
1445 :
1446 7 : listener = calloc(1, sizeof(*listener));
1447 7 : if (!listener) {
1448 0 : cb_fn(cb_arg, -ENOMEM);
1449 0 : return;
1450 : }
1451 :
1452 7 : listener->trid = &tr_listener->trid;
1453 7 : listener->transport = transport;
1454 7 : listener->cb_fn = cb_fn;
1455 7 : listener->cb_arg = cb_arg;
1456 7 : listener->subsystem = subsystem;
1457 7 : listener->ana_state = calloc(subsystem->max_nsid, sizeof(enum spdk_nvme_ana_state));
1458 7 : if (!listener->ana_state) {
1459 0 : free(listener);
1460 0 : cb_fn(cb_arg, -ENOMEM);
1461 0 : return;
1462 : }
1463 :
1464 7 : spdk_nvmf_subsystem_listener_opts_init(&listener->opts, sizeof(listener->opts));
1465 7 : if (opts != NULL) {
1466 0 : rc = listener_opts_copy(opts, &listener->opts);
1467 0 : if (rc) {
1468 0 : SPDK_ERRLOG("Unable to copy listener options\n");
1469 0 : free(listener->ana_state);
1470 0 : free(listener);
1471 0 : cb_fn(cb_arg, -EINVAL);
1472 0 : return;
1473 : }
1474 : }
1475 :
1476 7 : id = spdk_bit_array_find_first_clear(subsystem->used_listener_ids, 0);
1477 7 : if (id == UINT32_MAX) {
1478 0 : SPDK_ERRLOG("Cannot add any more listeners\n");
1479 0 : free(listener->ana_state);
1480 0 : free(listener);
1481 0 : cb_fn(cb_arg, -EINVAL);
1482 0 : return;
1483 : }
1484 :
1485 7 : spdk_bit_array_set(subsystem->used_listener_ids, id);
1486 7 : listener->id = id;
1487 :
1488 231 : for (i = 0; i < subsystem->max_nsid; i++) {
1489 224 : listener->ana_state[i] = listener->opts.ana_state;
1490 : }
1491 :
1492 7 : if (transport->ops->listen_associate != NULL) {
1493 0 : rc = transport->ops->listen_associate(transport, subsystem, trid);
1494 : }
1495 :
1496 : SPDK_DTRACE_PROBE4(nvmf_subsystem_add_listener, subsystem->subnqn, listener->trid->trtype,
1497 : listener->trid->traddr, listener->trid->trsvcid);
1498 :
1499 7 : _nvmf_subsystem_add_listener_done(listener, rc);
1500 : }
1501 :
1502 : void
1503 7 : spdk_nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem,
1504 : struct spdk_nvme_transport_id *trid,
1505 : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
1506 : void *cb_arg)
1507 : {
1508 7 : _nvmf_subsystem_add_listener(subsystem, trid, cb_fn, cb_arg, NULL);
1509 7 : }
1510 :
1511 : void
1512 0 : spdk_nvmf_subsystem_add_listener_ext(struct spdk_nvmf_subsystem *subsystem,
1513 : struct spdk_nvme_transport_id *trid,
1514 : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
1515 : void *cb_arg, struct spdk_nvmf_listener_opts *opts)
1516 : {
1517 0 : _nvmf_subsystem_add_listener(subsystem, trid, cb_fn, cb_arg, opts);
1518 0 : }
1519 :
1520 : int
1521 0 : spdk_nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem,
1522 : const struct spdk_nvme_transport_id *trid)
1523 : {
1524 : struct spdk_nvmf_subsystem_listener *listener;
1525 :
1526 0 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1527 0 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1528 0 : return -EAGAIN;
1529 : }
1530 :
1531 0 : listener = nvmf_subsystem_find_listener(subsystem, trid);
1532 0 : if (listener == NULL) {
1533 0 : return -ENOENT;
1534 : }
1535 :
1536 : SPDK_DTRACE_PROBE4(nvmf_subsystem_remove_listener, subsystem->subnqn, listener->trid->trtype,
1537 : listener->trid->traddr, listener->trid->trsvcid);
1538 :
1539 0 : _nvmf_subsystem_remove_listener(subsystem, listener, false);
1540 :
1541 0 : return 0;
1542 : }
1543 :
1544 : void
1545 13 : nvmf_subsystem_remove_all_listeners(struct spdk_nvmf_subsystem *subsystem,
1546 : bool stop)
1547 : {
1548 : struct spdk_nvmf_subsystem_listener *listener, *listener_tmp;
1549 :
1550 20 : TAILQ_FOREACH_SAFE(listener, &subsystem->listeners, link, listener_tmp) {
1551 7 : _nvmf_subsystem_remove_listener(subsystem, listener, stop);
1552 : }
1553 13 : }
1554 :
1555 : bool
1556 0 : spdk_nvmf_subsystem_listener_allowed(struct spdk_nvmf_subsystem *subsystem,
1557 : const struct spdk_nvme_transport_id *trid)
1558 : {
1559 : struct spdk_nvmf_subsystem_listener *listener;
1560 :
1561 0 : TAILQ_FOREACH(listener, &subsystem->listeners, link) {
1562 0 : if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) {
1563 0 : return true;
1564 : }
1565 : }
1566 :
1567 0 : if (!strcmp(subsystem->subnqn, SPDK_NVMF_DISCOVERY_NQN)) {
1568 0 : SPDK_WARNLOG("Allowing connection to discovery subsystem on %s/%s/%s, "
1569 : "even though this listener was not added to the discovery "
1570 : "subsystem. This behavior is deprecated and will be removed "
1571 : "in a future release.\n",
1572 : spdk_nvme_transport_id_trtype_str(trid->trtype), trid->traddr, trid->trsvcid);
1573 0 : return true;
1574 : }
1575 :
1576 0 : return false;
1577 : }
1578 :
1579 : struct spdk_nvmf_subsystem_listener *
1580 30 : spdk_nvmf_subsystem_get_first_listener(struct spdk_nvmf_subsystem *subsystem)
1581 : {
1582 30 : return TAILQ_FIRST(&subsystem->listeners);
1583 : }
1584 :
1585 : struct spdk_nvmf_subsystem_listener *
1586 155 : spdk_nvmf_subsystem_get_next_listener(struct spdk_nvmf_subsystem *subsystem,
1587 : struct spdk_nvmf_subsystem_listener *prev_listener)
1588 : {
1589 155 : return TAILQ_NEXT(prev_listener, link);
1590 : }
1591 :
1592 : const struct spdk_nvme_transport_id *
1593 0 : spdk_nvmf_subsystem_listener_get_trid(struct spdk_nvmf_subsystem_listener *listener)
1594 : {
1595 0 : return listener->trid;
1596 : }
1597 :
1598 : void
1599 0 : spdk_nvmf_subsystem_allow_any_listener(struct spdk_nvmf_subsystem *subsystem,
1600 : bool allow_any_listener)
1601 : {
1602 0 : subsystem->flags.allow_any_listener = allow_any_listener;
1603 0 : }
1604 :
1605 : bool
1606 0 : spdk_nvmf_subsystem_any_listener_allowed(struct spdk_nvmf_subsystem *subsystem)
1607 : {
1608 0 : return subsystem->flags.allow_any_listener;
1609 : }
1610 :
1611 : struct subsystem_update_ns_ctx {
1612 : struct spdk_nvmf_subsystem *subsystem;
1613 :
1614 : spdk_nvmf_subsystem_state_change_done cb_fn;
1615 : void *cb_arg;
1616 : };
1617 :
1618 : static void
1619 0 : subsystem_update_ns_done(struct spdk_io_channel_iter *i, int status)
1620 : {
1621 0 : struct subsystem_update_ns_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1622 :
1623 0 : if (ctx->cb_fn) {
1624 0 : ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status);
1625 : }
1626 0 : free(ctx);
1627 0 : }
1628 :
1629 : static void
1630 0 : subsystem_update_ns_on_pg(struct spdk_io_channel_iter *i)
1631 : {
1632 : int rc;
1633 : struct subsystem_update_ns_ctx *ctx;
1634 : struct spdk_nvmf_poll_group *group;
1635 : struct spdk_nvmf_subsystem *subsystem;
1636 :
1637 0 : ctx = spdk_io_channel_iter_get_ctx(i);
1638 0 : group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
1639 0 : subsystem = ctx->subsystem;
1640 :
1641 0 : rc = nvmf_poll_group_update_subsystem(group, subsystem);
1642 0 : spdk_for_each_channel_continue(i, rc);
1643 0 : }
1644 :
1645 : static int
1646 0 : nvmf_subsystem_update_ns(struct spdk_nvmf_subsystem *subsystem,
1647 : spdk_nvmf_subsystem_state_change_done cb_fn, void *cb_arg)
1648 : {
1649 : struct subsystem_update_ns_ctx *ctx;
1650 :
1651 0 : ctx = calloc(1, sizeof(*ctx));
1652 0 : if (ctx == NULL) {
1653 0 : SPDK_ERRLOG("Can't alloc subsystem poll group update context\n");
1654 0 : return -ENOMEM;
1655 : }
1656 0 : ctx->subsystem = subsystem;
1657 0 : ctx->cb_fn = cb_fn;
1658 0 : ctx->cb_arg = cb_arg;
1659 :
1660 0 : spdk_for_each_channel(subsystem->tgt,
1661 : subsystem_update_ns_on_pg,
1662 : ctx,
1663 : subsystem_update_ns_done);
1664 0 : return 0;
1665 : }
1666 :
1667 : static void
1668 10 : nvmf_subsystem_ns_changed(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1669 : {
1670 : struct spdk_nvmf_ctrlr *ctrlr;
1671 :
1672 14 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1673 4 : if (nvmf_ctrlr_ns_is_visible(ctrlr, nsid)) {
1674 3 : nvmf_ctrlr_ns_changed(ctrlr, nsid);
1675 : }
1676 : }
1677 10 : }
1678 :
1679 : static uint32_t nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns);
1680 :
1681 : int
1682 5 : spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1683 : {
1684 : struct spdk_nvmf_transport *transport;
1685 : struct spdk_nvmf_ns *ns;
1686 : struct spdk_nvmf_host *host, *tmp;
1687 : struct spdk_nvmf_ctrlr *ctrlr;
1688 :
1689 5 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1690 1 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1691 0 : assert(false);
1692 : return -1;
1693 : }
1694 :
1695 5 : if (nsid == 0 || nsid > subsystem->max_nsid) {
1696 0 : return -1;
1697 : }
1698 :
1699 5 : ns = subsystem->ns[nsid - 1];
1700 5 : if (!ns) {
1701 0 : return -1;
1702 : }
1703 :
1704 5 : subsystem->ns[nsid - 1] = NULL;
1705 :
1706 5 : assert(ns->anagrpid - 1 < subsystem->max_nsid);
1707 5 : assert(subsystem->ana_group[ns->anagrpid - 1] > 0);
1708 :
1709 5 : subsystem->ana_group[ns->anagrpid - 1]--;
1710 :
1711 6 : TAILQ_FOREACH_SAFE(host, &ns->hosts, link, tmp) {
1712 1 : nvmf_ns_remove_host(ns, host);
1713 : }
1714 :
1715 5 : free(ns->ptpl_file);
1716 5 : nvmf_ns_reservation_clear_all_registrants(ns);
1717 5 : spdk_bdev_module_release_bdev(ns->bdev);
1718 5 : spdk_bdev_close(ns->desc);
1719 5 : free(ns);
1720 :
1721 5 : if (subsystem->fdp_supported && !spdk_nvmf_subsystem_get_first_ns(subsystem)) {
1722 1 : subsystem->fdp_supported = false;
1723 1 : SPDK_DEBUGLOG(nvmf, "Subsystem with id: %u doesn't have FDP capability.\n",
1724 : subsystem->id);
1725 : }
1726 :
1727 5 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1728 0 : transport = spdk_nvmf_transport_get_next(transport)) {
1729 0 : if (transport->ops->subsystem_remove_ns) {
1730 0 : transport->ops->subsystem_remove_ns(transport, subsystem, nsid);
1731 : }
1732 : }
1733 :
1734 5 : nvmf_subsystem_ns_changed(subsystem, nsid);
1735 :
1736 8 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1737 3 : spdk_bit_array_clear(ctrlr->visible_ns, nsid - 1);
1738 : }
1739 :
1740 5 : return 0;
1741 : }
1742 :
1743 : struct subsystem_ns_change_ctx {
1744 : struct spdk_nvmf_subsystem *subsystem;
1745 : spdk_nvmf_subsystem_state_change_done cb_fn;
1746 : uint32_t nsid;
1747 : };
1748 :
1749 : static void
1750 1 : _nvmf_ns_hot_remove(struct spdk_nvmf_subsystem *subsystem,
1751 : void *cb_arg, int status)
1752 : {
1753 1 : struct subsystem_ns_change_ctx *ctx = cb_arg;
1754 : int rc;
1755 :
1756 1 : rc = spdk_nvmf_subsystem_remove_ns(subsystem, ctx->nsid);
1757 1 : if (rc != 0) {
1758 0 : SPDK_ERRLOG("Failed to make changes to NVME-oF subsystem with id: %u\n", subsystem->id);
1759 : }
1760 :
1761 1 : rc = spdk_nvmf_subsystem_resume(subsystem, NULL, NULL);
1762 1 : if (rc != 0) {
1763 0 : SPDK_ERRLOG("Failed to resume NVME-oF subsystem with id: %u\n", subsystem->id);
1764 : }
1765 :
1766 1 : free(ctx);
1767 1 : }
1768 :
1769 : static void
1770 0 : nvmf_ns_change_msg(void *ns_ctx)
1771 : {
1772 0 : struct subsystem_ns_change_ctx *ctx = ns_ctx;
1773 : int rc;
1774 :
1775 : SPDK_DTRACE_PROBE2(nvmf_ns_change, ctx->nsid, ctx->subsystem->subnqn);
1776 :
1777 0 : rc = spdk_nvmf_subsystem_pause(ctx->subsystem, ctx->nsid, ctx->cb_fn, ctx);
1778 0 : if (rc) {
1779 0 : if (rc == -EBUSY) {
1780 : /* Try again, this is not a permanent situation. */
1781 0 : spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ctx);
1782 : } else {
1783 0 : free(ctx);
1784 0 : SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n");
1785 : }
1786 : }
1787 0 : }
1788 :
1789 : static void
1790 1 : nvmf_ns_hot_remove(void *remove_ctx)
1791 : {
1792 1 : struct spdk_nvmf_ns *ns = remove_ctx;
1793 : struct subsystem_ns_change_ctx *ns_ctx;
1794 : int rc;
1795 :
1796 : /* We have to allocate a new context because this op
1797 : * is asynchronous and we could lose the ns in the middle.
1798 : */
1799 1 : ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx));
1800 1 : if (!ns_ctx) {
1801 0 : SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n");
1802 0 : return;
1803 : }
1804 :
1805 1 : ns_ctx->subsystem = ns->subsystem;
1806 1 : ns_ctx->nsid = ns->opts.nsid;
1807 1 : ns_ctx->cb_fn = _nvmf_ns_hot_remove;
1808 :
1809 1 : rc = spdk_nvmf_subsystem_pause(ns->subsystem, ns_ctx->nsid, _nvmf_ns_hot_remove, ns_ctx);
1810 1 : if (rc) {
1811 0 : if (rc == -EBUSY) {
1812 : /* Try again, this is not a permanent situation. */
1813 0 : spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx);
1814 : } else {
1815 0 : SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n");
1816 0 : free(ns_ctx);
1817 : }
1818 : }
1819 : }
1820 :
1821 : static void
1822 1 : _nvmf_ns_resize(struct spdk_nvmf_subsystem *subsystem, void *cb_arg, int status)
1823 : {
1824 1 : struct subsystem_ns_change_ctx *ctx = cb_arg;
1825 :
1826 1 : nvmf_subsystem_ns_changed(subsystem, ctx->nsid);
1827 1 : if (spdk_nvmf_subsystem_resume(subsystem, NULL, NULL) != 0) {
1828 0 : SPDK_ERRLOG("Failed to resume NVME-oF subsystem with id: %u\n", subsystem->id);
1829 : }
1830 :
1831 1 : free(ctx);
1832 1 : }
1833 :
1834 : static void
1835 1 : nvmf_ns_resize(void *event_ctx)
1836 : {
1837 1 : struct spdk_nvmf_ns *ns = event_ctx;
1838 : struct subsystem_ns_change_ctx *ns_ctx;
1839 : int rc;
1840 :
1841 : /* We have to allocate a new context because this op
1842 : * is asynchronous and we could lose the ns in the middle.
1843 : */
1844 1 : ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx));
1845 1 : if (!ns_ctx) {
1846 0 : SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n");
1847 0 : return;
1848 : }
1849 :
1850 1 : ns_ctx->subsystem = ns->subsystem;
1851 1 : ns_ctx->nsid = ns->opts.nsid;
1852 1 : ns_ctx->cb_fn = _nvmf_ns_resize;
1853 :
1854 : /* Specify 0 for the nsid here, because we do not need to pause the namespace.
1855 : * Namespaces can only be resized bigger, so there is no need to quiesce I/O.
1856 : */
1857 1 : rc = spdk_nvmf_subsystem_pause(ns->subsystem, 0, _nvmf_ns_resize, ns_ctx);
1858 1 : if (rc) {
1859 0 : if (rc == -EBUSY) {
1860 : /* Try again, this is not a permanent situation. */
1861 0 : spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx);
1862 : } else {
1863 0 : SPDK_ERRLOG("Unable to pause subsystem to process namespace resize!\n");
1864 0 : free(ns_ctx);
1865 : }
1866 : }
1867 : }
1868 :
1869 : static void
1870 2 : nvmf_ns_event(enum spdk_bdev_event_type type,
1871 : struct spdk_bdev *bdev,
1872 : void *event_ctx)
1873 : {
1874 2 : SPDK_DEBUGLOG(nvmf, "Bdev event: type %d, name %s, subsystem_id %d, ns_id %d\n",
1875 : type,
1876 : spdk_bdev_get_name(bdev),
1877 : ((struct spdk_nvmf_ns *)event_ctx)->subsystem->id,
1878 : ((struct spdk_nvmf_ns *)event_ctx)->nsid);
1879 :
1880 2 : switch (type) {
1881 1 : case SPDK_BDEV_EVENT_REMOVE:
1882 1 : nvmf_ns_hot_remove(event_ctx);
1883 1 : break;
1884 1 : case SPDK_BDEV_EVENT_RESIZE:
1885 1 : nvmf_ns_resize(event_ctx);
1886 1 : break;
1887 0 : default:
1888 0 : SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
1889 0 : break;
1890 : }
1891 2 : }
1892 :
1893 : void
1894 13 : spdk_nvmf_ns_opts_get_defaults(struct spdk_nvmf_ns_opts *opts, size_t opts_size)
1895 : {
1896 13 : if (!opts) {
1897 0 : SPDK_ERRLOG("opts should not be NULL.\n");
1898 0 : return;
1899 : }
1900 :
1901 13 : if (!opts_size) {
1902 0 : SPDK_ERRLOG("opts_size should not be zero.\n");
1903 0 : return;
1904 : }
1905 :
1906 13 : memset(opts, 0, opts_size);
1907 13 : opts->opts_size = opts_size;
1908 :
1909 : #define FIELD_OK(field) \
1910 : offsetof(struct spdk_nvmf_ns_opts, field) + sizeof(opts->field) <= opts_size
1911 :
1912 : #define SET_FIELD(field, value) \
1913 : if (FIELD_OK(field)) { \
1914 : opts->field = value; \
1915 : } \
1916 :
1917 : /* All current fields are set to 0 by default. */
1918 13 : SET_FIELD(nsid, 0);
1919 13 : if (FIELD_OK(nguid)) {
1920 13 : memset(opts->nguid, 0, sizeof(opts->nguid));
1921 : }
1922 13 : if (FIELD_OK(eui64)) {
1923 13 : memset(opts->eui64, 0, sizeof(opts->eui64));
1924 : }
1925 13 : if (FIELD_OK(uuid)) {
1926 13 : spdk_uuid_set_null(&opts->uuid);
1927 : }
1928 13 : SET_FIELD(anagrpid, 0);
1929 13 : SET_FIELD(transport_specific, NULL);
1930 :
1931 : #undef FIELD_OK
1932 : #undef SET_FIELD
1933 : }
1934 :
1935 : static void
1936 6 : nvmf_ns_opts_copy(struct spdk_nvmf_ns_opts *opts,
1937 : const struct spdk_nvmf_ns_opts *user_opts,
1938 : size_t opts_size)
1939 : {
1940 : #define FIELD_OK(field) \
1941 : offsetof(struct spdk_nvmf_ns_opts, field) + sizeof(opts->field) <= user_opts->opts_size
1942 :
1943 : #define SET_FIELD(field) \
1944 : if (FIELD_OK(field)) { \
1945 : opts->field = user_opts->field; \
1946 : } \
1947 :
1948 6 : SET_FIELD(nsid);
1949 6 : if (FIELD_OK(nguid)) {
1950 6 : memcpy(opts->nguid, user_opts->nguid, sizeof(opts->nguid));
1951 : }
1952 6 : if (FIELD_OK(eui64)) {
1953 6 : memcpy(opts->eui64, user_opts->eui64, sizeof(opts->eui64));
1954 : }
1955 6 : if (FIELD_OK(uuid)) {
1956 6 : spdk_uuid_copy(&opts->uuid, &user_opts->uuid);
1957 : }
1958 6 : SET_FIELD(anagrpid);
1959 6 : SET_FIELD(no_auto_visible);
1960 6 : SET_FIELD(transport_specific);
1961 :
1962 6 : opts->opts_size = user_opts->opts_size;
1963 :
1964 : /* We should not remove this statement, but need to update the assert statement
1965 : * if we add a new field, and also add a corresponding SET_FIELD statement.
1966 : */
1967 : SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_ns_opts) == 72, "Incorrect size");
1968 :
1969 : #undef FIELD_OK
1970 : #undef SET_FIELD
1971 6 : }
1972 :
1973 : /* Dummy bdev module used to to claim bdevs. */
1974 : static struct spdk_bdev_module ns_bdev_module = {
1975 : .name = "NVMe-oF Target",
1976 : };
1977 :
1978 : static int nvmf_ns_reservation_update(const struct spdk_nvmf_ns *ns,
1979 : const struct spdk_nvmf_reservation_info *info);
1980 : static int nvmf_ns_reservation_load(const struct spdk_nvmf_ns *ns,
1981 : struct spdk_nvmf_reservation_info *info);
1982 : static int nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns,
1983 : struct spdk_nvmf_reservation_info *info);
1984 :
1985 : uint32_t
1986 7 : spdk_nvmf_subsystem_add_ns_ext(struct spdk_nvmf_subsystem *subsystem, const char *bdev_name,
1987 : const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size,
1988 : const char *ptpl_file)
1989 : {
1990 : struct spdk_nvmf_transport *transport;
1991 7 : struct spdk_nvmf_ns_opts opts;
1992 : struct spdk_nvmf_ns *ns, *first_ns;
1993 : struct spdk_nvmf_ctrlr *ctrlr;
1994 7 : struct spdk_nvmf_reservation_info info = {0};
1995 : int rc;
1996 : bool zone_append_supported;
1997 : uint64_t max_zone_append_size_kib;
1998 :
1999 7 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
2000 0 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
2001 0 : return 0;
2002 : }
2003 :
2004 7 : spdk_nvmf_ns_opts_get_defaults(&opts, sizeof(opts));
2005 7 : if (user_opts) {
2006 6 : nvmf_ns_opts_copy(&opts, user_opts, opts_size);
2007 : }
2008 :
2009 7 : if (opts.nsid == SPDK_NVME_GLOBAL_NS_TAG) {
2010 1 : SPDK_ERRLOG("Invalid NSID %" PRIu32 "\n", opts.nsid);
2011 1 : return 0;
2012 : }
2013 :
2014 6 : if (opts.nsid == 0) {
2015 : /*
2016 : * NSID not specified - find a free index.
2017 : *
2018 : * If no free slots are found, opts.nsid will be subsystem->max_nsid + 1, which will
2019 : * expand max_nsid if possible.
2020 : */
2021 2 : for (opts.nsid = 1; opts.nsid <= subsystem->max_nsid; opts.nsid++) {
2022 2 : if (_nvmf_subsystem_get_ns(subsystem, opts.nsid) == NULL) {
2023 2 : break;
2024 : }
2025 : }
2026 : }
2027 :
2028 6 : if (_nvmf_subsystem_get_ns(subsystem, opts.nsid)) {
2029 1 : SPDK_ERRLOG("Requested NSID %" PRIu32 " already in use\n", opts.nsid);
2030 1 : return 0;
2031 : }
2032 :
2033 5 : if (opts.nsid > subsystem->max_nsid) {
2034 0 : SPDK_ERRLOG("NSID greater than maximum not allowed\n");
2035 0 : return 0;
2036 : }
2037 :
2038 5 : if (opts.anagrpid == 0) {
2039 5 : opts.anagrpid = opts.nsid;
2040 : }
2041 :
2042 5 : if (opts.anagrpid > subsystem->max_nsid) {
2043 0 : SPDK_ERRLOG("ANAGRPID greater than maximum NSID not allowed\n");
2044 0 : return 0;
2045 : }
2046 :
2047 5 : ns = calloc(1, sizeof(*ns));
2048 5 : if (ns == NULL) {
2049 0 : SPDK_ERRLOG("Namespace allocation failed\n");
2050 0 : return 0;
2051 : }
2052 :
2053 5 : TAILQ_INIT(&ns->hosts);
2054 5 : ns->always_visible = !opts.no_auto_visible;
2055 5 : if (ns->always_visible) {
2056 5 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
2057 0 : spdk_bit_array_set(ctrlr->visible_ns, opts.nsid - 1);
2058 : }
2059 : }
2060 :
2061 5 : rc = spdk_bdev_open_ext(bdev_name, true, nvmf_ns_event, ns, &ns->desc);
2062 5 : if (rc != 0) {
2063 0 : SPDK_ERRLOG("Subsystem %s: bdev %s cannot be opened, error=%d\n",
2064 : subsystem->subnqn, bdev_name, rc);
2065 0 : free(ns);
2066 0 : return 0;
2067 : }
2068 :
2069 5 : ns->bdev = spdk_bdev_desc_get_bdev(ns->desc);
2070 :
2071 5 : if (spdk_bdev_get_md_size(ns->bdev) != 0) {
2072 0 : if (!spdk_bdev_is_md_interleaved(ns->bdev)) {
2073 0 : SPDK_ERRLOG("Can't attach bdev with separate metadata.\n");
2074 0 : spdk_bdev_close(ns->desc);
2075 0 : free(ns);
2076 0 : return 0;
2077 : }
2078 :
2079 0 : if (spdk_bdev_get_md_size(ns->bdev) > SPDK_BDEV_MAX_INTERLEAVED_MD_SIZE) {
2080 0 : SPDK_ERRLOG("Maximum supported interleaved md size %u, current md size %u\n",
2081 : SPDK_BDEV_MAX_INTERLEAVED_MD_SIZE, spdk_bdev_get_md_size(ns->bdev));
2082 0 : spdk_bdev_close(ns->desc);
2083 0 : free(ns);
2084 0 : return 0;
2085 : }
2086 : }
2087 :
2088 5 : rc = spdk_bdev_module_claim_bdev(ns->bdev, ns->desc, &ns_bdev_module);
2089 5 : if (rc != 0) {
2090 0 : spdk_bdev_close(ns->desc);
2091 0 : free(ns);
2092 0 : return 0;
2093 : }
2094 :
2095 : /* Cache the zcopy capability of the bdev device */
2096 5 : ns->zcopy = spdk_bdev_io_type_supported(ns->bdev, SPDK_BDEV_IO_TYPE_ZCOPY);
2097 :
2098 5 : if (spdk_uuid_is_null(&opts.uuid)) {
2099 5 : opts.uuid = *spdk_bdev_get_uuid(ns->bdev);
2100 : }
2101 :
2102 : /* if nguid descriptor is supported by bdev module (nvme) then uuid = nguid */
2103 5 : if (spdk_mem_all_zero(opts.nguid, sizeof(opts.nguid))) {
2104 : SPDK_STATIC_ASSERT(sizeof(opts.nguid) == sizeof(opts.uuid), "size mismatch");
2105 5 : memcpy(opts.nguid, spdk_bdev_get_uuid(ns->bdev), sizeof(opts.nguid));
2106 : }
2107 :
2108 5 : if (spdk_bdev_is_zoned(ns->bdev)) {
2109 0 : SPDK_DEBUGLOG(nvmf, "The added namespace is backed by a zoned block device.\n");
2110 0 : ns->csi = SPDK_NVME_CSI_ZNS;
2111 :
2112 0 : zone_append_supported = spdk_bdev_io_type_supported(ns->bdev,
2113 : SPDK_BDEV_IO_TYPE_ZONE_APPEND);
2114 0 : max_zone_append_size_kib = spdk_bdev_get_max_zone_append_size(
2115 0 : ns->bdev) * spdk_bdev_get_block_size(ns->bdev);
2116 :
2117 0 : if (_nvmf_subsystem_get_first_zoned_ns(subsystem) != NULL &&
2118 0 : (subsystem->zone_append_supported != zone_append_supported ||
2119 0 : subsystem->max_zone_append_size_kib != max_zone_append_size_kib)) {
2120 0 : SPDK_ERRLOG("Namespaces with different zone append support or different zone append size are not allowed.\n");
2121 0 : goto err;
2122 : }
2123 :
2124 0 : subsystem->zone_append_supported = zone_append_supported;
2125 0 : subsystem->max_zone_append_size_kib = max_zone_append_size_kib;
2126 : }
2127 :
2128 5 : first_ns = spdk_nvmf_subsystem_get_first_ns(subsystem);
2129 5 : if (!first_ns) {
2130 4 : if (spdk_bdev_get_nvme_ctratt(ns->bdev).bits.fdps) {
2131 1 : SPDK_DEBUGLOG(nvmf, "Subsystem with id: %u has FDP capability.\n",
2132 : subsystem->id);
2133 1 : subsystem->fdp_supported = true;
2134 : }
2135 : } else {
2136 1 : if (spdk_bdev_get_nvme_ctratt(first_ns->bdev).bits.fdps !=
2137 1 : spdk_bdev_get_nvme_ctratt(ns->bdev).bits.fdps) {
2138 1 : SPDK_ERRLOG("Subsystem with id: %u can%s FDP namespace.\n", subsystem->id,
2139 : spdk_bdev_get_nvme_ctratt(first_ns->bdev).bits.fdps ? " only add" : "not add");
2140 1 : goto err;
2141 : }
2142 : }
2143 :
2144 4 : ns->opts = opts;
2145 4 : ns->subsystem = subsystem;
2146 4 : subsystem->ns[opts.nsid - 1] = ns;
2147 4 : ns->nsid = opts.nsid;
2148 4 : ns->anagrpid = opts.anagrpid;
2149 4 : subsystem->ana_group[ns->anagrpid - 1]++;
2150 4 : TAILQ_INIT(&ns->registrants);
2151 4 : if (ptpl_file) {
2152 0 : ns->ptpl_file = strdup(ptpl_file);
2153 0 : if (!ns->ptpl_file) {
2154 0 : SPDK_ERRLOG("Namespace ns->ptpl_file allocation failed\n");
2155 0 : goto err;
2156 : }
2157 : }
2158 :
2159 4 : if (nvmf_ns_is_ptpl_capable(ns)) {
2160 1 : rc = nvmf_ns_reservation_load(ns, &info);
2161 1 : if (rc) {
2162 0 : SPDK_ERRLOG("Subsystem load reservation failed\n");
2163 0 : goto err;
2164 : }
2165 :
2166 1 : rc = nvmf_ns_reservation_restore(ns, &info);
2167 1 : if (rc) {
2168 0 : SPDK_ERRLOG("Subsystem restore reservation failed\n");
2169 0 : goto err;
2170 : }
2171 : }
2172 :
2173 4 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
2174 0 : transport = spdk_nvmf_transport_get_next(transport)) {
2175 0 : if (transport->ops->subsystem_add_ns) {
2176 0 : rc = transport->ops->subsystem_add_ns(transport, subsystem, ns);
2177 0 : if (rc) {
2178 0 : SPDK_ERRLOG("Namespace attachment is not allowed by %s transport\n", transport->ops->name);
2179 0 : nvmf_ns_reservation_clear_all_registrants(ns);
2180 0 : goto err;
2181 : }
2182 : }
2183 : }
2184 :
2185 : /* JSON value obj is freed before sending the response. Set NULL to prevent usage of dangling pointer. */
2186 4 : ns->opts.transport_specific = NULL;
2187 :
2188 4 : SPDK_DEBUGLOG(nvmf, "Subsystem %s: bdev %s assigned nsid %" PRIu32 "\n",
2189 : spdk_nvmf_subsystem_get_nqn(subsystem),
2190 : bdev_name,
2191 : opts.nsid);
2192 :
2193 4 : nvmf_subsystem_ns_changed(subsystem, opts.nsid);
2194 :
2195 : SPDK_DTRACE_PROBE2(nvmf_subsystem_add_ns, subsystem->subnqn, ns->nsid);
2196 :
2197 4 : return opts.nsid;
2198 1 : err:
2199 1 : subsystem->ns[opts.nsid - 1] = NULL;
2200 1 : spdk_bdev_module_release_bdev(ns->bdev);
2201 1 : spdk_bdev_close(ns->desc);
2202 1 : free(ns->ptpl_file);
2203 1 : free(ns);
2204 :
2205 1 : return 0;
2206 : }
2207 :
2208 : static uint32_t
2209 19 : nvmf_subsystem_get_next_allocated_nsid(struct spdk_nvmf_subsystem *subsystem,
2210 : uint32_t prev_nsid)
2211 : {
2212 : uint32_t nsid;
2213 :
2214 19 : if (prev_nsid >= subsystem->max_nsid) {
2215 1 : return 0;
2216 : }
2217 :
2218 4504 : for (nsid = prev_nsid + 1; nsid <= subsystem->max_nsid; nsid++) {
2219 4487 : if (subsystem->ns[nsid - 1]) {
2220 1 : return nsid;
2221 : }
2222 : }
2223 :
2224 17 : return 0;
2225 : }
2226 :
2227 : struct spdk_nvmf_ns *
2228 19 : spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem)
2229 : {
2230 : uint32_t first_nsid;
2231 :
2232 19 : first_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, 0);
2233 19 : return _nvmf_subsystem_get_ns(subsystem, first_nsid);
2234 : }
2235 :
2236 : struct spdk_nvmf_ns *
2237 0 : spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem,
2238 : struct spdk_nvmf_ns *prev_ns)
2239 : {
2240 : uint32_t next_nsid;
2241 :
2242 0 : next_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, prev_ns->opts.nsid);
2243 0 : return _nvmf_subsystem_get_ns(subsystem, next_nsid);
2244 : }
2245 :
2246 : struct spdk_nvmf_ns *
2247 0 : spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
2248 : {
2249 0 : return _nvmf_subsystem_get_ns(subsystem, nsid);
2250 : }
2251 :
2252 : uint32_t
2253 0 : spdk_nvmf_ns_get_id(const struct spdk_nvmf_ns *ns)
2254 : {
2255 0 : return ns->opts.nsid;
2256 : }
2257 :
2258 : struct spdk_bdev *
2259 0 : spdk_nvmf_ns_get_bdev(struct spdk_nvmf_ns *ns)
2260 : {
2261 0 : return ns->bdev;
2262 : }
2263 :
2264 : void
2265 0 : spdk_nvmf_ns_get_opts(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_ns_opts *opts,
2266 : size_t opts_size)
2267 : {
2268 0 : memset(opts, 0, opts_size);
2269 0 : memcpy(opts, &ns->opts, spdk_min(sizeof(ns->opts), opts_size));
2270 0 : }
2271 :
2272 : const char *
2273 0 : spdk_nvmf_subsystem_get_sn(const struct spdk_nvmf_subsystem *subsystem)
2274 : {
2275 0 : return subsystem->sn;
2276 : }
2277 :
2278 : int
2279 4 : spdk_nvmf_subsystem_set_sn(struct spdk_nvmf_subsystem *subsystem, const char *sn)
2280 : {
2281 : size_t len, max_len;
2282 :
2283 4 : max_len = sizeof(subsystem->sn) - 1;
2284 4 : len = strlen(sn);
2285 4 : if (len > max_len) {
2286 1 : SPDK_DEBUGLOG(nvmf, "Invalid sn \"%s\": length %zu > max %zu\n",
2287 : sn, len, max_len);
2288 1 : return -1;
2289 : }
2290 :
2291 3 : if (!nvmf_valid_ascii_string(sn, len)) {
2292 1 : SPDK_DEBUGLOG(nvmf, "Non-ASCII sn\n");
2293 1 : SPDK_LOGDUMP(nvmf, "sn", sn, len);
2294 1 : return -1;
2295 : }
2296 :
2297 2 : snprintf(subsystem->sn, sizeof(subsystem->sn), "%s", sn);
2298 :
2299 2 : return 0;
2300 : }
2301 :
2302 : const char *
2303 0 : spdk_nvmf_subsystem_get_mn(const struct spdk_nvmf_subsystem *subsystem)
2304 : {
2305 0 : return subsystem->mn;
2306 : }
2307 :
2308 : int
2309 0 : spdk_nvmf_subsystem_set_mn(struct spdk_nvmf_subsystem *subsystem, const char *mn)
2310 : {
2311 : size_t len, max_len;
2312 :
2313 0 : if (mn == NULL) {
2314 0 : mn = MODEL_NUMBER_DEFAULT;
2315 : }
2316 0 : max_len = sizeof(subsystem->mn) - 1;
2317 0 : len = strlen(mn);
2318 0 : if (len > max_len) {
2319 0 : SPDK_DEBUGLOG(nvmf, "Invalid mn \"%s\": length %zu > max %zu\n",
2320 : mn, len, max_len);
2321 0 : return -1;
2322 : }
2323 :
2324 0 : if (!nvmf_valid_ascii_string(mn, len)) {
2325 0 : SPDK_DEBUGLOG(nvmf, "Non-ASCII mn\n");
2326 0 : SPDK_LOGDUMP(nvmf, "mn", mn, len);
2327 0 : return -1;
2328 : }
2329 :
2330 0 : snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", mn);
2331 :
2332 0 : return 0;
2333 : }
2334 :
2335 : const char *
2336 0 : spdk_nvmf_subsystem_get_nqn(const struct spdk_nvmf_subsystem *subsystem)
2337 : {
2338 0 : return subsystem->subnqn;
2339 : }
2340 :
2341 : /* We have to use the typedef in the function declaration to appease astyle. */
2342 : typedef enum spdk_nvmf_subtype spdk_nvmf_subtype_t;
2343 :
2344 : spdk_nvmf_subtype_t
2345 0 : spdk_nvmf_subsystem_get_type(struct spdk_nvmf_subsystem *subsystem)
2346 : {
2347 0 : return subsystem->subtype;
2348 : }
2349 :
2350 : uint32_t
2351 0 : spdk_nvmf_subsystem_get_max_nsid(struct spdk_nvmf_subsystem *subsystem)
2352 : {
2353 0 : return subsystem->max_nsid;
2354 : }
2355 :
2356 : int
2357 0 : spdk_nvmf_subsystem_set_cntlid_range(struct spdk_nvmf_subsystem *subsystem,
2358 : uint16_t min_cntlid, uint16_t max_cntlid)
2359 : {
2360 0 : if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
2361 0 : return -EAGAIN;
2362 : }
2363 :
2364 0 : if (min_cntlid > max_cntlid) {
2365 0 : return -EINVAL;
2366 : }
2367 : /* The spec reserves cntlid values in the range FFF0h to FFFFh. */
2368 0 : if (min_cntlid < NVMF_MIN_CNTLID || min_cntlid > NVMF_MAX_CNTLID ||
2369 0 : max_cntlid < NVMF_MIN_CNTLID || max_cntlid > NVMF_MAX_CNTLID) {
2370 0 : return -EINVAL;
2371 : }
2372 0 : subsystem->min_cntlid = min_cntlid;
2373 0 : subsystem->max_cntlid = max_cntlid;
2374 0 : if (subsystem->next_cntlid < min_cntlid || subsystem->next_cntlid > max_cntlid - 1) {
2375 0 : subsystem->next_cntlid = min_cntlid - 1;
2376 : }
2377 :
2378 0 : return 0;
2379 : }
2380 :
2381 : static uint16_t
2382 1 : nvmf_subsystem_gen_cntlid(struct spdk_nvmf_subsystem *subsystem)
2383 : {
2384 : int count;
2385 :
2386 : /*
2387 : * In the worst case, we might have to try all CNTLID values between min_cntlid and max_cntlid
2388 : * before we find one that is unused (or find that all values are in use).
2389 : */
2390 1 : for (count = 0; count < subsystem->max_cntlid - subsystem->min_cntlid + 1; count++) {
2391 1 : subsystem->next_cntlid++;
2392 1 : if (subsystem->next_cntlid > subsystem->max_cntlid) {
2393 0 : subsystem->next_cntlid = subsystem->min_cntlid;
2394 : }
2395 :
2396 : /* Check if a controller with this cntlid currently exists. */
2397 1 : if (nvmf_subsystem_get_ctrlr(subsystem, subsystem->next_cntlid) == NULL) {
2398 : /* Found unused cntlid */
2399 1 : return subsystem->next_cntlid;
2400 : }
2401 : }
2402 :
2403 : /* All valid cntlid values are in use. */
2404 0 : return 0xFFFF;
2405 : }
2406 :
2407 : int
2408 1 : nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr)
2409 : {
2410 :
2411 1 : if (ctrlr->dynamic_ctrlr) {
2412 1 : ctrlr->cntlid = nvmf_subsystem_gen_cntlid(subsystem);
2413 1 : if (ctrlr->cntlid == 0xFFFF) {
2414 : /* Unable to get a cntlid */
2415 0 : SPDK_ERRLOG("Reached max simultaneous ctrlrs\n");
2416 0 : return -EBUSY;
2417 : }
2418 0 : } else if (nvmf_subsystem_get_ctrlr(subsystem, ctrlr->cntlid) != NULL) {
2419 0 : SPDK_ERRLOG("Ctrlr with cntlid %u already exist\n", ctrlr->cntlid);
2420 0 : return -EEXIST;
2421 : }
2422 :
2423 1 : TAILQ_INSERT_TAIL(&subsystem->ctrlrs, ctrlr, link);
2424 :
2425 : SPDK_DTRACE_PROBE3(nvmf_subsystem_add_ctrlr, subsystem->subnqn, ctrlr, ctrlr->hostnqn);
2426 :
2427 1 : return 0;
2428 : }
2429 :
2430 : void
2431 1 : nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem,
2432 : struct spdk_nvmf_ctrlr *ctrlr)
2433 : {
2434 : SPDK_DTRACE_PROBE3(nvmf_subsystem_remove_ctrlr, subsystem->subnqn, ctrlr, ctrlr->hostnqn);
2435 :
2436 1 : assert(spdk_get_thread() == subsystem->thread);
2437 1 : assert(subsystem == ctrlr->subsys);
2438 1 : SPDK_DEBUGLOG(nvmf, "remove ctrlr %p id 0x%x from subsys %p %s\n", ctrlr, ctrlr->cntlid, subsystem,
2439 : subsystem->subnqn);
2440 1 : TAILQ_REMOVE(&subsystem->ctrlrs, ctrlr, link);
2441 1 : }
2442 :
2443 : struct spdk_nvmf_ctrlr *
2444 2 : nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, uint16_t cntlid)
2445 : {
2446 : struct spdk_nvmf_ctrlr *ctrlr;
2447 :
2448 2 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
2449 1 : if (ctrlr->cntlid == cntlid) {
2450 1 : return ctrlr;
2451 : }
2452 : }
2453 :
2454 1 : return NULL;
2455 : }
2456 :
2457 : uint32_t
2458 0 : spdk_nvmf_subsystem_get_max_namespaces(const struct spdk_nvmf_subsystem *subsystem)
2459 : {
2460 0 : return subsystem->max_nsid;
2461 : }
2462 :
2463 : uint16_t
2464 0 : spdk_nvmf_subsystem_get_min_cntlid(const struct spdk_nvmf_subsystem *subsystem)
2465 : {
2466 0 : return subsystem->min_cntlid;
2467 : }
2468 :
2469 : uint16_t
2470 0 : spdk_nvmf_subsystem_get_max_cntlid(const struct spdk_nvmf_subsystem *subsystem)
2471 : {
2472 0 : return subsystem->max_cntlid;
2473 : }
2474 :
2475 : struct _nvmf_ns_registrant {
2476 : uint64_t rkey;
2477 : char *host_uuid;
2478 : };
2479 :
2480 : struct _nvmf_ns_registrants {
2481 : size_t num_regs;
2482 : struct _nvmf_ns_registrant reg[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2483 : };
2484 :
2485 : struct _nvmf_ns_reservation {
2486 : bool ptpl_activated;
2487 : enum spdk_nvme_reservation_type rtype;
2488 : uint64_t crkey;
2489 : char *bdev_uuid;
2490 : char *holder_uuid;
2491 : struct _nvmf_ns_registrants regs;
2492 : };
2493 :
2494 : static const struct spdk_json_object_decoder nvmf_ns_pr_reg_decoders[] = {
2495 : {"rkey", offsetof(struct _nvmf_ns_registrant, rkey), spdk_json_decode_uint64},
2496 : {"host_uuid", offsetof(struct _nvmf_ns_registrant, host_uuid), spdk_json_decode_string},
2497 : };
2498 :
2499 : static int
2500 4 : nvmf_decode_ns_pr_reg(const struct spdk_json_val *val, void *out)
2501 : {
2502 4 : struct _nvmf_ns_registrant *reg = out;
2503 :
2504 4 : return spdk_json_decode_object(val, nvmf_ns_pr_reg_decoders,
2505 : SPDK_COUNTOF(nvmf_ns_pr_reg_decoders), reg);
2506 : }
2507 :
2508 : static int
2509 4 : nvmf_decode_ns_pr_regs(const struct spdk_json_val *val, void *out)
2510 : {
2511 4 : struct _nvmf_ns_registrants *regs = out;
2512 :
2513 4 : return spdk_json_decode_array(val, nvmf_decode_ns_pr_reg, regs->reg,
2514 : SPDK_NVMF_MAX_NUM_REGISTRANTS, ®s->num_regs,
2515 : sizeof(struct _nvmf_ns_registrant));
2516 : }
2517 :
2518 : static const struct spdk_json_object_decoder nvmf_ns_pr_decoders[] = {
2519 : {"ptpl", offsetof(struct _nvmf_ns_reservation, ptpl_activated), spdk_json_decode_bool, true},
2520 : {"rtype", offsetof(struct _nvmf_ns_reservation, rtype), spdk_json_decode_uint32, true},
2521 : {"crkey", offsetof(struct _nvmf_ns_reservation, crkey), spdk_json_decode_uint64, true},
2522 : {"bdev_uuid", offsetof(struct _nvmf_ns_reservation, bdev_uuid), spdk_json_decode_string},
2523 : {"holder_uuid", offsetof(struct _nvmf_ns_reservation, holder_uuid), spdk_json_decode_string, true},
2524 : {"registrants", offsetof(struct _nvmf_ns_reservation, regs), nvmf_decode_ns_pr_regs},
2525 : };
2526 :
2527 : static int
2528 5 : nvmf_ns_reservation_load_json(const struct spdk_nvmf_ns *ns,
2529 : struct spdk_nvmf_reservation_info *info)
2530 : {
2531 5 : size_t json_size;
2532 : ssize_t values_cnt, rc;
2533 5 : void *json = NULL, *end;
2534 5 : struct spdk_json_val *values = NULL;
2535 5 : struct _nvmf_ns_reservation res = {};
2536 5 : const char *file = ns->ptpl_file;
2537 : uint32_t i;
2538 :
2539 : /* Load all persist file contents into a local buffer */
2540 5 : json = spdk_posix_file_load_from_name(file, &json_size);
2541 5 : if (!json) {
2542 0 : SPDK_ERRLOG("Load persit file %s failed\n", file);
2543 0 : return -ENOMEM;
2544 : }
2545 :
2546 5 : rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0);
2547 5 : if (rc < 0) {
2548 1 : SPDK_NOTICELOG("Parsing JSON configuration failed (%zd)\n", rc);
2549 1 : goto exit;
2550 : }
2551 :
2552 4 : values_cnt = rc;
2553 4 : values = calloc(values_cnt, sizeof(struct spdk_json_val));
2554 4 : if (values == NULL) {
2555 0 : goto exit;
2556 : }
2557 :
2558 4 : rc = spdk_json_parse(json, json_size, values, values_cnt, &end, 0);
2559 4 : if (rc != values_cnt) {
2560 0 : SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
2561 0 : goto exit;
2562 : }
2563 :
2564 : /* Decode json */
2565 4 : if (spdk_json_decode_object(values, nvmf_ns_pr_decoders,
2566 : SPDK_COUNTOF(nvmf_ns_pr_decoders),
2567 : &res)) {
2568 0 : SPDK_ERRLOG("Invalid objects in the persist file %s\n", file);
2569 0 : rc = -EINVAL;
2570 0 : goto exit;
2571 : }
2572 :
2573 4 : if (res.regs.num_regs > SPDK_NVMF_MAX_NUM_REGISTRANTS) {
2574 0 : SPDK_ERRLOG("Can only support up to %u registrants\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
2575 0 : rc = -ERANGE;
2576 0 : goto exit;
2577 : }
2578 :
2579 4 : rc = 0;
2580 4 : info->ptpl_activated = res.ptpl_activated;
2581 4 : info->rtype = res.rtype;
2582 4 : info->crkey = res.crkey;
2583 4 : snprintf(info->bdev_uuid, sizeof(info->bdev_uuid), "%s", res.bdev_uuid);
2584 4 : snprintf(info->holder_uuid, sizeof(info->holder_uuid), "%s", res.holder_uuid);
2585 4 : info->num_regs = res.regs.num_regs;
2586 8 : for (i = 0; i < res.regs.num_regs; i++) {
2587 4 : info->registrants[i].rkey = res.regs.reg[i].rkey;
2588 4 : snprintf(info->registrants[i].host_uuid, sizeof(info->registrants[i].host_uuid), "%s",
2589 : res.regs.reg[i].host_uuid);
2590 : }
2591 :
2592 5 : exit:
2593 5 : free(json);
2594 5 : free(values);
2595 5 : free(res.bdev_uuid);
2596 5 : free(res.holder_uuid);
2597 9 : for (i = 0; i < res.regs.num_regs; i++) {
2598 4 : free(res.regs.reg[i].host_uuid);
2599 : }
2600 :
2601 5 : return rc;
2602 : }
2603 :
2604 : static bool nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns);
2605 :
2606 : static int
2607 5 : nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
2608 : {
2609 : uint32_t i;
2610 5 : struct spdk_nvmf_registrant *reg, *holder = NULL;
2611 5 : struct spdk_uuid bdev_uuid, holder_uuid;
2612 5 : bool rkey_flag = false;
2613 :
2614 5 : SPDK_DEBUGLOG(nvmf, "NSID %u, PTPL %u, Number of registrants %u\n",
2615 : ns->nsid, info->ptpl_activated, info->num_regs);
2616 :
2617 : /* it's not an error */
2618 5 : if (!info->ptpl_activated || !info->num_regs) {
2619 0 : return 0;
2620 : }
2621 :
2622 : /* Check info->crkey exist or not in info->registrants[i].rkey */
2623 14 : for (i = 0; i < info->num_regs; i++) {
2624 9 : if (info->crkey == info->registrants[i].rkey) {
2625 3 : rkey_flag = true;
2626 : }
2627 : }
2628 5 : if (!rkey_flag && info->crkey != 0) {
2629 1 : return -EINVAL;
2630 : }
2631 :
2632 4 : spdk_uuid_parse(&bdev_uuid, info->bdev_uuid);
2633 4 : if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) {
2634 1 : SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n");
2635 1 : return -EINVAL;
2636 : }
2637 :
2638 3 : ns->crkey = info->crkey;
2639 3 : ns->rtype = info->rtype;
2640 3 : ns->ptpl_activated = info->ptpl_activated;
2641 3 : spdk_uuid_parse(&holder_uuid, info->holder_uuid);
2642 :
2643 3 : SPDK_DEBUGLOG(nvmf, "Bdev UUID %s\n", info->bdev_uuid);
2644 3 : if (info->rtype) {
2645 2 : SPDK_DEBUGLOG(nvmf, "Holder UUID %s, RTYPE %u, RKEY 0x%"PRIx64"\n",
2646 : info->holder_uuid, info->rtype, info->crkey);
2647 : }
2648 :
2649 8 : for (i = 0; i < info->num_regs; i++) {
2650 5 : reg = calloc(1, sizeof(*reg));
2651 5 : if (!reg) {
2652 0 : return -ENOMEM;
2653 : }
2654 5 : spdk_uuid_parse(®->hostid, info->registrants[i].host_uuid);
2655 5 : reg->rkey = info->registrants[i].rkey;
2656 5 : TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
2657 5 : if (info->crkey != 0 && !spdk_uuid_compare(&holder_uuid, ®->hostid)) {
2658 2 : holder = reg;
2659 : }
2660 5 : SPDK_DEBUGLOG(nvmf, "Registrant RKEY 0x%"PRIx64", Host UUID %s\n",
2661 : info->registrants[i].rkey, info->registrants[i].host_uuid);
2662 : }
2663 :
2664 3 : if (nvmf_ns_reservation_all_registrants_type(ns)) {
2665 1 : ns->holder = TAILQ_FIRST(&ns->registrants);
2666 : } else {
2667 2 : ns->holder = holder;
2668 : }
2669 :
2670 3 : return 0;
2671 : }
2672 :
2673 : static int
2674 5 : nvmf_ns_json_write_cb(void *cb_ctx, const void *data, size_t size)
2675 : {
2676 5 : char *file = cb_ctx;
2677 : size_t rc;
2678 : FILE *fd;
2679 :
2680 5 : fd = fopen(file, "w");
2681 5 : if (!fd) {
2682 0 : SPDK_ERRLOG("Can't open file %s for write\n", file);
2683 0 : return -ENOENT;
2684 : }
2685 5 : rc = fwrite(data, 1, size, fd);
2686 5 : fclose(fd);
2687 :
2688 5 : return rc == size ? 0 : -1;
2689 : }
2690 :
2691 : static int
2692 5 : nvmf_ns_reservation_update_json(const struct spdk_nvmf_ns *ns,
2693 : const struct spdk_nvmf_reservation_info *info)
2694 : {
2695 5 : const char *file = ns->ptpl_file;
2696 : struct spdk_json_write_ctx *w;
2697 : uint32_t i;
2698 5 : int rc = 0;
2699 :
2700 5 : w = spdk_json_write_begin(nvmf_ns_json_write_cb, (void *)file, 0);
2701 5 : if (w == NULL) {
2702 0 : return -ENOMEM;
2703 : }
2704 : /* clear the configuration file */
2705 5 : if (!info->ptpl_activated) {
2706 1 : goto exit;
2707 : }
2708 :
2709 4 : spdk_json_write_object_begin(w);
2710 4 : spdk_json_write_named_bool(w, "ptpl", info->ptpl_activated);
2711 4 : spdk_json_write_named_uint32(w, "rtype", info->rtype);
2712 4 : spdk_json_write_named_uint64(w, "crkey", info->crkey);
2713 4 : spdk_json_write_named_string(w, "bdev_uuid", info->bdev_uuid);
2714 4 : spdk_json_write_named_string(w, "holder_uuid", info->holder_uuid);
2715 :
2716 4 : spdk_json_write_named_array_begin(w, "registrants");
2717 8 : for (i = 0; i < info->num_regs; i++) {
2718 4 : spdk_json_write_object_begin(w);
2719 4 : spdk_json_write_named_uint64(w, "rkey", info->registrants[i].rkey);
2720 4 : spdk_json_write_named_string(w, "host_uuid", info->registrants[i].host_uuid);
2721 4 : spdk_json_write_object_end(w);
2722 : }
2723 4 : spdk_json_write_array_end(w);
2724 4 : spdk_json_write_object_end(w);
2725 :
2726 5 : exit:
2727 5 : rc = spdk_json_write_end(w);
2728 5 : return rc;
2729 : }
2730 :
2731 : static int
2732 7 : nvmf_ns_update_reservation_info(struct spdk_nvmf_ns *ns)
2733 : {
2734 7 : struct spdk_nvmf_reservation_info info;
2735 : struct spdk_nvmf_registrant *reg, *tmp;
2736 7 : uint32_t i = 0;
2737 :
2738 7 : assert(ns != NULL);
2739 :
2740 7 : if (!ns->bdev || !nvmf_ns_is_ptpl_capable(ns)) {
2741 0 : return 0;
2742 : }
2743 :
2744 7 : memset(&info, 0, sizeof(info));
2745 7 : spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), spdk_bdev_get_uuid(ns->bdev));
2746 :
2747 7 : if (ns->rtype) {
2748 2 : info.rtype = ns->rtype;
2749 2 : info.crkey = ns->crkey;
2750 2 : if (!nvmf_ns_reservation_all_registrants_type(ns)) {
2751 2 : assert(ns->holder != NULL);
2752 2 : spdk_uuid_fmt_lower(info.holder_uuid, sizeof(info.holder_uuid), &ns->holder->hostid);
2753 : }
2754 : }
2755 :
2756 14 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2757 7 : spdk_uuid_fmt_lower(info.registrants[i].host_uuid, sizeof(info.registrants[i].host_uuid),
2758 7 : ®->hostid);
2759 7 : info.registrants[i++].rkey = reg->rkey;
2760 : }
2761 :
2762 7 : info.num_regs = i;
2763 7 : info.ptpl_activated = ns->ptpl_activated;
2764 :
2765 7 : return nvmf_ns_reservation_update(ns, &info);
2766 : }
2767 :
2768 : static struct spdk_nvmf_registrant *
2769 110 : nvmf_ns_reservation_get_registrant(struct spdk_nvmf_ns *ns,
2770 : struct spdk_uuid *uuid)
2771 : {
2772 : struct spdk_nvmf_registrant *reg, *tmp;
2773 :
2774 191 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2775 151 : if (!spdk_uuid_compare(®->hostid, uuid)) {
2776 70 : return reg;
2777 : }
2778 : }
2779 :
2780 40 : return NULL;
2781 : }
2782 :
2783 : /* Generate reservation notice log to registered HostID controllers */
2784 : static void
2785 10 : nvmf_subsystem_gen_ctrlr_notification(struct spdk_nvmf_subsystem *subsystem,
2786 : struct spdk_nvmf_ns *ns,
2787 : struct spdk_uuid *hostid_list,
2788 : uint32_t num_hostid,
2789 : enum spdk_nvme_reservation_notification_log_page_type type)
2790 : {
2791 : struct spdk_nvmf_ctrlr *ctrlr;
2792 : uint32_t i;
2793 :
2794 25 : for (i = 0; i < num_hostid; i++) {
2795 75 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
2796 60 : if (!spdk_uuid_compare(&ctrlr->hostid, &hostid_list[i])) {
2797 22 : nvmf_ctrlr_reservation_notice_log(ctrlr, ns, type);
2798 : }
2799 : }
2800 : }
2801 10 : }
2802 :
2803 : /* Get all registrants' hostid other than the controller who issued the command */
2804 : static uint32_t
2805 16 : nvmf_ns_reservation_get_all_other_hostid(struct spdk_nvmf_ns *ns,
2806 : struct spdk_uuid *hostid_list,
2807 : uint32_t max_num_hostid,
2808 : struct spdk_uuid *current_hostid)
2809 : {
2810 : struct spdk_nvmf_registrant *reg, *tmp;
2811 16 : uint32_t num_hostid = 0;
2812 :
2813 55 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2814 39 : if (spdk_uuid_compare(®->hostid, current_hostid)) {
2815 23 : if (num_hostid == max_num_hostid) {
2816 0 : assert(false);
2817 : return max_num_hostid;
2818 : }
2819 23 : hostid_list[num_hostid++] = reg->hostid;
2820 : }
2821 : }
2822 :
2823 16 : return num_hostid;
2824 : }
2825 :
2826 : /* Calculate the unregistered HostID list according to list
2827 : * prior to execute preempt command and list after executing
2828 : * preempt command.
2829 : */
2830 : static uint32_t
2831 3 : nvmf_ns_reservation_get_unregistered_hostid(struct spdk_uuid *old_hostid_list,
2832 : uint32_t old_num_hostid,
2833 : struct spdk_uuid *remaining_hostid_list,
2834 : uint32_t remaining_num_hostid)
2835 : {
2836 3 : struct spdk_uuid temp_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2837 3 : uint32_t i, j, num_hostid = 0;
2838 : bool found;
2839 :
2840 3 : if (!remaining_num_hostid) {
2841 1 : return old_num_hostid;
2842 : }
2843 :
2844 6 : for (i = 0; i < old_num_hostid; i++) {
2845 4 : found = false;
2846 6 : for (j = 0; j < remaining_num_hostid; j++) {
2847 4 : if (!spdk_uuid_compare(&old_hostid_list[i], &remaining_hostid_list[j])) {
2848 2 : found = true;
2849 2 : break;
2850 : }
2851 : }
2852 4 : if (!found) {
2853 2 : spdk_uuid_copy(&temp_hostid_list[num_hostid++], &old_hostid_list[i]);
2854 : }
2855 : }
2856 :
2857 2 : if (num_hostid) {
2858 2 : memcpy(old_hostid_list, temp_hostid_list, sizeof(struct spdk_uuid) * num_hostid);
2859 : }
2860 :
2861 2 : return num_hostid;
2862 : }
2863 :
2864 : /* current reservation type is all registrants or not */
2865 : static bool
2866 54 : nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns)
2867 : {
2868 102 : return (ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
2869 48 : ns->rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS);
2870 : }
2871 :
2872 : /* current registrant is reservation holder or not */
2873 : static bool
2874 26 : nvmf_ns_reservation_registrant_is_holder(struct spdk_nvmf_ns *ns,
2875 : struct spdk_nvmf_registrant *reg)
2876 : {
2877 26 : if (!reg) {
2878 0 : return false;
2879 : }
2880 :
2881 26 : if (nvmf_ns_reservation_all_registrants_type(ns)) {
2882 2 : return true;
2883 : }
2884 :
2885 24 : return (ns->holder == reg);
2886 : }
2887 :
2888 : static int
2889 29 : nvmf_ns_reservation_add_registrant(struct spdk_nvmf_ns *ns,
2890 : struct spdk_nvmf_ctrlr *ctrlr,
2891 : uint64_t nrkey)
2892 : {
2893 : struct spdk_nvmf_registrant *reg;
2894 :
2895 29 : reg = calloc(1, sizeof(*reg));
2896 29 : if (!reg) {
2897 0 : return -ENOMEM;
2898 : }
2899 :
2900 29 : reg->rkey = nrkey;
2901 : /* set hostid for the registrant */
2902 29 : spdk_uuid_copy(®->hostid, &ctrlr->hostid);
2903 29 : TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
2904 29 : ns->gen++;
2905 :
2906 29 : return 0;
2907 : }
2908 :
2909 : static void
2910 11 : nvmf_ns_reservation_release_reservation(struct spdk_nvmf_ns *ns)
2911 : {
2912 11 : ns->rtype = 0;
2913 11 : ns->crkey = 0;
2914 11 : ns->holder = NULL;
2915 11 : }
2916 :
2917 : /* release the reservation if the last registrant was removed */
2918 : static void
2919 19 : nvmf_ns_reservation_check_release_on_remove_registrant(struct spdk_nvmf_ns *ns,
2920 : struct spdk_nvmf_registrant *reg)
2921 : {
2922 : struct spdk_nvmf_registrant *next_reg;
2923 :
2924 : /* no reservation holder */
2925 19 : if (!ns->holder) {
2926 7 : assert(ns->rtype == 0);
2927 7 : return;
2928 : }
2929 :
2930 12 : next_reg = TAILQ_FIRST(&ns->registrants);
2931 12 : if (next_reg && nvmf_ns_reservation_all_registrants_type(ns)) {
2932 : /* the next valid registrant is the new holder now */
2933 2 : ns->holder = next_reg;
2934 10 : } else if (nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
2935 : /* release the reservation */
2936 7 : nvmf_ns_reservation_release_reservation(ns);
2937 : }
2938 : }
2939 :
2940 : static void
2941 19 : nvmf_ns_reservation_remove_registrant(struct spdk_nvmf_ns *ns,
2942 : struct spdk_nvmf_registrant *reg)
2943 : {
2944 19 : TAILQ_REMOVE(&ns->registrants, reg, link);
2945 19 : nvmf_ns_reservation_check_release_on_remove_registrant(ns, reg);
2946 19 : free(reg);
2947 19 : ns->gen++;
2948 19 : return;
2949 : }
2950 :
2951 : static uint32_t
2952 0 : nvmf_ns_reservation_remove_registrants_by_key(struct spdk_nvmf_ns *ns,
2953 : uint64_t rkey)
2954 : {
2955 : struct spdk_nvmf_registrant *reg, *tmp;
2956 0 : uint32_t count = 0;
2957 :
2958 0 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2959 0 : if (reg->rkey == rkey) {
2960 0 : nvmf_ns_reservation_remove_registrant(ns, reg);
2961 0 : count++;
2962 : }
2963 : }
2964 0 : return count;
2965 : }
2966 :
2967 : static uint32_t
2968 1 : nvmf_ns_reservation_remove_all_other_registrants(struct spdk_nvmf_ns *ns,
2969 : struct spdk_nvmf_registrant *reg)
2970 : {
2971 : struct spdk_nvmf_registrant *reg_tmp, *reg_tmp2;
2972 1 : uint32_t count = 0;
2973 :
2974 3 : TAILQ_FOREACH_SAFE(reg_tmp, &ns->registrants, link, reg_tmp2) {
2975 2 : if (reg_tmp != reg) {
2976 1 : nvmf_ns_reservation_remove_registrant(ns, reg_tmp);
2977 1 : count++;
2978 : }
2979 : }
2980 1 : return count;
2981 : }
2982 :
2983 : static uint32_t
2984 9 : nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns)
2985 : {
2986 : struct spdk_nvmf_registrant *reg, *reg_tmp;
2987 9 : uint32_t count = 0;
2988 :
2989 20 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) {
2990 11 : nvmf_ns_reservation_remove_registrant(ns, reg);
2991 11 : count++;
2992 : }
2993 9 : return count;
2994 : }
2995 :
2996 : static void
2997 12 : nvmf_ns_reservation_acquire_reservation(struct spdk_nvmf_ns *ns, uint64_t rkey,
2998 : enum spdk_nvme_reservation_type rtype,
2999 : struct spdk_nvmf_registrant *holder)
3000 : {
3001 12 : ns->rtype = rtype;
3002 12 : ns->crkey = rkey;
3003 12 : assert(ns->holder == NULL);
3004 12 : ns->holder = holder;
3005 12 : }
3006 :
3007 : static bool
3008 45 : nvmf_ns_reservation_register(struct spdk_nvmf_ns *ns,
3009 : struct spdk_nvmf_ctrlr *ctrlr,
3010 : struct spdk_nvmf_request *req)
3011 : {
3012 45 : struct spdk_nvme_reservation_register_data key = { 0 };
3013 45 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3014 : uint8_t rrega, iekey, cptpl, rtype;
3015 : struct spdk_nvmf_registrant *reg;
3016 45 : uint8_t status = SPDK_NVME_SC_SUCCESS;
3017 45 : bool update_sgroup = false;
3018 45 : struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
3019 45 : uint32_t num_hostid = 0;
3020 : int rc;
3021 :
3022 45 : rrega = cmd->cdw10_bits.resv_register.rrega;
3023 45 : iekey = cmd->cdw10_bits.resv_register.iekey;
3024 45 : cptpl = cmd->cdw10_bits.resv_register.cptpl;
3025 :
3026 45 : if (req->iovcnt > 0 && req->length >= sizeof(key)) {
3027 45 : struct spdk_iov_xfer ix;
3028 45 : spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3029 45 : spdk_iov_xfer_to_buf(&ix, &key, sizeof(key));
3030 : } else {
3031 0 : SPDK_ERRLOG("No key provided. Failing request.\n");
3032 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3033 0 : goto exit;
3034 : }
3035 :
3036 45 : SPDK_DEBUGLOG(nvmf, "REGISTER: RREGA %u, IEKEY %u, CPTPL %u, "
3037 : "NRKEY 0x%"PRIx64", NRKEY 0x%"PRIx64"\n",
3038 : rrega, iekey, cptpl, key.crkey, key.nrkey);
3039 :
3040 45 : if (cptpl == SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON) {
3041 : /* Ture to OFF state, and need to be updated in the configuration file */
3042 1 : if (ns->ptpl_activated) {
3043 1 : ns->ptpl_activated = 0;
3044 1 : update_sgroup = true;
3045 : }
3046 44 : } else if (cptpl == SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS) {
3047 4 : if (!nvmf_ns_is_ptpl_capable(ns)) {
3048 1 : status = SPDK_NVME_SC_INVALID_FIELD;
3049 1 : goto exit;
3050 3 : } else if (ns->ptpl_activated == 0) {
3051 3 : ns->ptpl_activated = 1;
3052 3 : update_sgroup = true;
3053 : }
3054 : }
3055 :
3056 : /* current Host Identifier has registrant or not */
3057 44 : reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
3058 :
3059 44 : switch (rrega) {
3060 36 : case SPDK_NVME_RESERVE_REGISTER_KEY:
3061 36 : if (!reg) {
3062 : /* register new controller */
3063 27 : if (key.nrkey == 0) {
3064 0 : SPDK_ERRLOG("Can't register zeroed new key\n");
3065 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3066 0 : goto exit;
3067 : }
3068 27 : rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey);
3069 27 : if (rc < 0) {
3070 0 : status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3071 0 : goto exit;
3072 : }
3073 27 : update_sgroup = true;
3074 : } else {
3075 : /* register with same key is not an error */
3076 9 : if (reg->rkey != key.nrkey) {
3077 8 : SPDK_ERRLOG("The same host already register a "
3078 : "key with 0x%"PRIx64"\n",
3079 : reg->rkey);
3080 8 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3081 8 : goto exit;
3082 : }
3083 : }
3084 28 : break;
3085 4 : case SPDK_NVME_RESERVE_UNREGISTER_KEY:
3086 4 : if (!reg || (!iekey && reg->rkey != key.crkey)) {
3087 0 : SPDK_ERRLOG("No registrant or current key doesn't match "
3088 : "with existing registrant key\n");
3089 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3090 0 : goto exit;
3091 : }
3092 :
3093 4 : rtype = ns->rtype;
3094 4 : num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
3095 : SPDK_NVMF_MAX_NUM_REGISTRANTS,
3096 : &ctrlr->hostid);
3097 :
3098 4 : nvmf_ns_reservation_remove_registrant(ns, reg);
3099 :
3100 4 : if (!ns->rtype && num_hostid && (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY ||
3101 : rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY)) {
3102 1 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3103 : hostid_list,
3104 : num_hostid,
3105 : SPDK_NVME_RESERVATION_RELEASED);
3106 : }
3107 4 : update_sgroup = true;
3108 4 : break;
3109 4 : case SPDK_NVME_RESERVE_REPLACE_KEY:
3110 4 : if (key.nrkey == 0) {
3111 0 : SPDK_ERRLOG("Can't register zeroed new key\n");
3112 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3113 0 : goto exit;
3114 : }
3115 : /* Registrant exists */
3116 4 : if (reg) {
3117 2 : if (!iekey && reg->rkey != key.crkey) {
3118 0 : SPDK_ERRLOG("Current key doesn't match "
3119 : "existing registrant key\n");
3120 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3121 0 : goto exit;
3122 : }
3123 2 : if (reg->rkey == key.nrkey) {
3124 0 : goto exit;
3125 : }
3126 2 : reg->rkey = key.nrkey;
3127 2 : } else if (iekey) { /* No registrant but IEKEY is set */
3128 : /* new registrant */
3129 1 : rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey);
3130 1 : if (rc < 0) {
3131 0 : status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3132 0 : goto exit;
3133 : }
3134 : } else { /* No registrant */
3135 1 : SPDK_ERRLOG("No registrant\n");
3136 1 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3137 1 : goto exit;
3138 :
3139 : }
3140 3 : update_sgroup = true;
3141 3 : break;
3142 0 : default:
3143 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3144 0 : goto exit;
3145 : }
3146 :
3147 45 : exit:
3148 45 : req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3149 45 : req->rsp->nvme_cpl.status.sc = status;
3150 45 : return update_sgroup;
3151 : }
3152 :
3153 : static bool
3154 13 : nvmf_ns_reservation_acquire(struct spdk_nvmf_ns *ns,
3155 : struct spdk_nvmf_ctrlr *ctrlr,
3156 : struct spdk_nvmf_request *req)
3157 : {
3158 13 : struct spdk_nvme_reservation_acquire_data key = { 0 };
3159 13 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3160 : uint8_t racqa, iekey, rtype;
3161 : struct spdk_nvmf_registrant *reg;
3162 13 : bool all_regs = false;
3163 13 : uint32_t count = 0;
3164 13 : bool update_sgroup = true;
3165 13 : struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
3166 13 : uint32_t num_hostid = 0;
3167 13 : struct spdk_uuid new_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
3168 13 : uint32_t new_num_hostid = 0;
3169 13 : bool reservation_released = false;
3170 13 : uint8_t status = SPDK_NVME_SC_SUCCESS;
3171 :
3172 13 : racqa = cmd->cdw10_bits.resv_acquire.racqa;
3173 13 : iekey = cmd->cdw10_bits.resv_acquire.iekey;
3174 13 : rtype = cmd->cdw10_bits.resv_acquire.rtype;
3175 :
3176 13 : if (req->iovcnt > 0 && req->length >= sizeof(key)) {
3177 13 : struct spdk_iov_xfer ix;
3178 13 : spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3179 13 : spdk_iov_xfer_to_buf(&ix, &key, sizeof(key));
3180 : } else {
3181 0 : SPDK_ERRLOG("No key provided. Failing request.\n");
3182 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3183 0 : goto exit;
3184 : }
3185 :
3186 13 : SPDK_DEBUGLOG(nvmf, "ACQUIRE: RACQA %u, IEKEY %u, RTYPE %u, "
3187 : "NRKEY 0x%"PRIx64", PRKEY 0x%"PRIx64"\n",
3188 : racqa, iekey, rtype, key.crkey, key.prkey);
3189 :
3190 13 : if (iekey || rtype > SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) {
3191 0 : SPDK_ERRLOG("Ignore existing key field set to 1\n");
3192 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3193 0 : update_sgroup = false;
3194 0 : goto exit;
3195 : }
3196 :
3197 13 : reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
3198 : /* must be registrant and CRKEY must match */
3199 13 : if (!reg || reg->rkey != key.crkey) {
3200 0 : SPDK_ERRLOG("No registrant or current key doesn't match "
3201 : "with existing registrant key\n");
3202 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3203 0 : update_sgroup = false;
3204 0 : goto exit;
3205 : }
3206 :
3207 13 : all_regs = nvmf_ns_reservation_all_registrants_type(ns);
3208 :
3209 13 : switch (racqa) {
3210 10 : case SPDK_NVME_RESERVE_ACQUIRE:
3211 : /* it's not an error for the holder to acquire same reservation type again */
3212 10 : if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && ns->rtype == rtype) {
3213 : /* do nothing */
3214 0 : update_sgroup = false;
3215 10 : } else if (ns->holder == NULL) {
3216 : /* first time to acquire the reservation */
3217 10 : nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
3218 : } else {
3219 0 : SPDK_ERRLOG("Invalid rtype or current registrant is not holder\n");
3220 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3221 0 : update_sgroup = false;
3222 0 : goto exit;
3223 : }
3224 10 : break;
3225 3 : case SPDK_NVME_RESERVE_PREEMPT:
3226 : /* no reservation holder */
3227 3 : if (!ns->holder) {
3228 : /* unregister with PRKEY */
3229 0 : nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
3230 0 : break;
3231 : }
3232 3 : num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
3233 : SPDK_NVMF_MAX_NUM_REGISTRANTS,
3234 : &ctrlr->hostid);
3235 :
3236 : /* only 1 reservation holder and reservation key is valid */
3237 3 : if (!all_regs) {
3238 : /* preempt itself */
3239 2 : if (nvmf_ns_reservation_registrant_is_holder(ns, reg) &&
3240 0 : ns->crkey == key.prkey) {
3241 0 : ns->rtype = rtype;
3242 0 : reservation_released = true;
3243 0 : break;
3244 : }
3245 :
3246 2 : if (ns->crkey == key.prkey) {
3247 2 : nvmf_ns_reservation_remove_registrant(ns, ns->holder);
3248 2 : nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
3249 2 : reservation_released = true;
3250 0 : } else if (key.prkey != 0) {
3251 0 : nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
3252 : } else {
3253 : /* PRKEY is zero */
3254 0 : SPDK_ERRLOG("Current PRKEY is zero\n");
3255 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3256 0 : update_sgroup = false;
3257 0 : goto exit;
3258 : }
3259 : } else {
3260 : /* release all other registrants except for the current one */
3261 1 : if (key.prkey == 0) {
3262 1 : nvmf_ns_reservation_remove_all_other_registrants(ns, reg);
3263 1 : assert(ns->holder == reg);
3264 : } else {
3265 0 : count = nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
3266 0 : if (count == 0) {
3267 0 : SPDK_ERRLOG("PRKEY doesn't match any registrant\n");
3268 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3269 0 : update_sgroup = false;
3270 0 : goto exit;
3271 : }
3272 : }
3273 : }
3274 3 : break;
3275 0 : default:
3276 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3277 0 : update_sgroup = false;
3278 0 : break;
3279 : }
3280 :
3281 13 : exit:
3282 13 : if (update_sgroup && racqa == SPDK_NVME_RESERVE_PREEMPT) {
3283 3 : new_num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, new_hostid_list,
3284 : SPDK_NVMF_MAX_NUM_REGISTRANTS,
3285 : &ctrlr->hostid);
3286 : /* Preempt notification occurs on the unregistered controllers
3287 : * other than the controller who issued the command.
3288 : */
3289 3 : num_hostid = nvmf_ns_reservation_get_unregistered_hostid(hostid_list,
3290 : num_hostid,
3291 : new_hostid_list,
3292 : new_num_hostid);
3293 3 : if (num_hostid) {
3294 3 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3295 : hostid_list,
3296 : num_hostid,
3297 : SPDK_NVME_REGISTRATION_PREEMPTED);
3298 :
3299 : }
3300 : /* Reservation released notification occurs on the
3301 : * controllers which are the remaining registrants other than
3302 : * the controller who issued the command.
3303 : */
3304 3 : if (reservation_released && new_num_hostid) {
3305 2 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3306 : new_hostid_list,
3307 : new_num_hostid,
3308 : SPDK_NVME_RESERVATION_RELEASED);
3309 :
3310 : }
3311 : }
3312 13 : req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3313 13 : req->rsp->nvme_cpl.status.sc = status;
3314 13 : return update_sgroup;
3315 : }
3316 :
3317 : static bool
3318 6 : nvmf_ns_reservation_release(struct spdk_nvmf_ns *ns,
3319 : struct spdk_nvmf_ctrlr *ctrlr,
3320 : struct spdk_nvmf_request *req)
3321 : {
3322 6 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3323 : uint8_t rrela, iekey, rtype;
3324 : struct spdk_nvmf_registrant *reg;
3325 6 : uint64_t crkey = 0;
3326 6 : uint8_t status = SPDK_NVME_SC_SUCCESS;
3327 6 : bool update_sgroup = true;
3328 6 : struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
3329 6 : uint32_t num_hostid = 0;
3330 :
3331 6 : rrela = cmd->cdw10_bits.resv_release.rrela;
3332 6 : iekey = cmd->cdw10_bits.resv_release.iekey;
3333 6 : rtype = cmd->cdw10_bits.resv_release.rtype;
3334 :
3335 6 : if (req->iovcnt > 0 && req->length >= sizeof(crkey)) {
3336 6 : struct spdk_iov_xfer ix;
3337 6 : spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3338 6 : spdk_iov_xfer_to_buf(&ix, &crkey, sizeof(crkey));
3339 : } else {
3340 0 : SPDK_ERRLOG("No key provided. Failing request.\n");
3341 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3342 0 : goto exit;
3343 : }
3344 :
3345 6 : SPDK_DEBUGLOG(nvmf, "RELEASE: RRELA %u, IEKEY %u, RTYPE %u, "
3346 : "CRKEY 0x%"PRIx64"\n", rrela, iekey, rtype, crkey);
3347 :
3348 6 : if (iekey) {
3349 0 : SPDK_ERRLOG("Ignore existing key field set to 1\n");
3350 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3351 0 : update_sgroup = false;
3352 0 : goto exit;
3353 : }
3354 :
3355 6 : reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
3356 6 : if (!reg || reg->rkey != crkey) {
3357 0 : SPDK_ERRLOG("No registrant or current key doesn't match "
3358 : "with existing registrant key\n");
3359 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3360 0 : update_sgroup = false;
3361 0 : goto exit;
3362 : }
3363 :
3364 6 : num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
3365 : SPDK_NVMF_MAX_NUM_REGISTRANTS,
3366 : &ctrlr->hostid);
3367 :
3368 6 : switch (rrela) {
3369 4 : case SPDK_NVME_RESERVE_RELEASE:
3370 4 : if (!ns->holder) {
3371 0 : SPDK_DEBUGLOG(nvmf, "RELEASE: no holder\n");
3372 0 : update_sgroup = false;
3373 0 : goto exit;
3374 : }
3375 4 : if (ns->rtype != rtype) {
3376 0 : SPDK_ERRLOG("Type doesn't match\n");
3377 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3378 0 : update_sgroup = false;
3379 0 : goto exit;
3380 : }
3381 4 : if (!nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
3382 : /* not the reservation holder, this isn't an error */
3383 0 : update_sgroup = false;
3384 0 : goto exit;
3385 : }
3386 :
3387 4 : rtype = ns->rtype;
3388 4 : nvmf_ns_reservation_release_reservation(ns);
3389 :
3390 4 : if (num_hostid && rtype != SPDK_NVME_RESERVE_WRITE_EXCLUSIVE &&
3391 : rtype != SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
3392 2 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3393 : hostid_list,
3394 : num_hostid,
3395 : SPDK_NVME_RESERVATION_RELEASED);
3396 : }
3397 4 : break;
3398 2 : case SPDK_NVME_RESERVE_CLEAR:
3399 2 : nvmf_ns_reservation_clear_all_registrants(ns);
3400 2 : if (num_hostid) {
3401 2 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3402 : hostid_list,
3403 : num_hostid,
3404 : SPDK_NVME_RESERVATION_PREEMPTED);
3405 : }
3406 2 : break;
3407 0 : default:
3408 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3409 0 : update_sgroup = false;
3410 0 : goto exit;
3411 : }
3412 :
3413 6 : exit:
3414 6 : req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3415 6 : req->rsp->nvme_cpl.status.sc = status;
3416 6 : return update_sgroup;
3417 : }
3418 :
3419 : static void
3420 3 : nvmf_ns_reservation_report(struct spdk_nvmf_ns *ns,
3421 : struct spdk_nvmf_ctrlr *ctrlr,
3422 : struct spdk_nvmf_request *req)
3423 : {
3424 3 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3425 : struct spdk_nvmf_registrant *reg, *tmp;
3426 3 : struct spdk_nvme_reservation_status_extended_data status_data = { 0 };
3427 3 : struct spdk_iov_xfer ix;
3428 : uint32_t transfer_len;
3429 3 : uint32_t regctl = 0;
3430 3 : uint8_t status = SPDK_NVME_SC_SUCCESS;
3431 :
3432 3 : if (req->iovcnt == 0) {
3433 0 : SPDK_ERRLOG("No data transfer specified for request. "
3434 : " Unable to transfer back response.\n");
3435 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3436 0 : goto exit;
3437 : }
3438 :
3439 3 : if (!cmd->cdw11_bits.resv_report.eds) {
3440 1 : SPDK_ERRLOG("NVMeoF uses extended controller data structure, "
3441 : "please set EDS bit in cdw11 and try again\n");
3442 1 : status = SPDK_NVME_SC_HOSTID_INCONSISTENT_FORMAT;
3443 1 : goto exit;
3444 : }
3445 :
3446 : /* Number of Dwords of the Reservation Status data structure to transfer */
3447 2 : transfer_len = (cmd->cdw10 + 1) * sizeof(uint32_t);
3448 :
3449 2 : if (transfer_len < sizeof(struct spdk_nvme_reservation_status_extended_data)) {
3450 1 : status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3451 1 : goto exit;
3452 : }
3453 :
3454 1 : spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3455 :
3456 1 : status_data.data.gen = ns->gen;
3457 1 : status_data.data.rtype = ns->rtype;
3458 1 : status_data.data.ptpls = ns->ptpl_activated;
3459 :
3460 3 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
3461 2 : regctl++;
3462 : }
3463 :
3464 : /*
3465 : * We report the number of registrants as per the spec here, even if
3466 : * the iov isn't big enough to contain them all. In that case, the
3467 : * spdk_iov_xfer_from_buf() won't actually copy any of the remaining
3468 : * data; as it keeps track of the iov cursor itself, it's simplest to
3469 : * just walk the entire list anyway.
3470 : */
3471 1 : status_data.data.regctl = regctl;
3472 :
3473 1 : spdk_iov_xfer_from_buf(&ix, &status_data, sizeof(status_data));
3474 :
3475 3 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
3476 2 : struct spdk_nvme_registered_ctrlr_extended_data ctrlr_data = { 0 };
3477 :
3478 : /* Set to 0xffffh for dynamic controller */
3479 2 : ctrlr_data.cntlid = 0xffff;
3480 2 : ctrlr_data.rcsts.status = (ns->holder == reg) ? true : false;
3481 2 : ctrlr_data.rkey = reg->rkey;
3482 2 : spdk_uuid_copy((struct spdk_uuid *)ctrlr_data.hostid, ®->hostid);
3483 :
3484 2 : spdk_iov_xfer_from_buf(&ix, &ctrlr_data, sizeof(ctrlr_data));
3485 : }
3486 :
3487 3 : exit:
3488 3 : req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3489 3 : req->rsp->nvme_cpl.status.sc = status;
3490 3 : return;
3491 : }
3492 :
3493 : static void
3494 0 : nvmf_ns_reservation_complete(void *ctx)
3495 : {
3496 0 : struct spdk_nvmf_request *req = ctx;
3497 :
3498 0 : spdk_nvmf_request_complete(req);
3499 0 : }
3500 :
3501 : static void
3502 0 : _nvmf_ns_reservation_update_done(struct spdk_nvmf_subsystem *subsystem,
3503 : void *cb_arg, int status)
3504 : {
3505 0 : struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)cb_arg;
3506 0 : struct spdk_nvmf_poll_group *group = req->qpair->group;
3507 :
3508 0 : spdk_thread_send_msg(group->thread, nvmf_ns_reservation_complete, req);
3509 0 : }
3510 :
3511 : void
3512 0 : nvmf_ns_reservation_request(void *ctx)
3513 : {
3514 0 : struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)ctx;
3515 0 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3516 0 : struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
3517 : uint32_t nsid;
3518 : struct spdk_nvmf_ns *ns;
3519 0 : bool update_sgroup = false;
3520 0 : int status = 0;
3521 :
3522 0 : nsid = cmd->nsid;
3523 0 : ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
3524 0 : assert(ns != NULL);
3525 :
3526 0 : switch (cmd->opc) {
3527 0 : case SPDK_NVME_OPC_RESERVATION_REGISTER:
3528 0 : update_sgroup = nvmf_ns_reservation_register(ns, ctrlr, req);
3529 0 : break;
3530 0 : case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
3531 0 : update_sgroup = nvmf_ns_reservation_acquire(ns, ctrlr, req);
3532 0 : break;
3533 0 : case SPDK_NVME_OPC_RESERVATION_RELEASE:
3534 0 : update_sgroup = nvmf_ns_reservation_release(ns, ctrlr, req);
3535 0 : break;
3536 0 : case SPDK_NVME_OPC_RESERVATION_REPORT:
3537 0 : nvmf_ns_reservation_report(ns, ctrlr, req);
3538 0 : break;
3539 0 : default:
3540 0 : break;
3541 : }
3542 :
3543 : /* update reservation information to subsystem's poll group */
3544 0 : if (update_sgroup) {
3545 0 : if (ns->ptpl_activated || cmd->opc == SPDK_NVME_OPC_RESERVATION_REGISTER) {
3546 0 : if (nvmf_ns_update_reservation_info(ns) != 0) {
3547 0 : req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3548 : }
3549 : }
3550 0 : status = nvmf_subsystem_update_ns(ctrlr->subsys, _nvmf_ns_reservation_update_done, req);
3551 0 : if (status == 0) {
3552 0 : return;
3553 : }
3554 : }
3555 :
3556 0 : _nvmf_ns_reservation_update_done(ctrlr->subsys, req, status);
3557 : }
3558 :
3559 : static bool
3560 11 : nvmf_ns_is_ptpl_capable_json(const struct spdk_nvmf_ns *ns)
3561 : {
3562 11 : return ns->ptpl_file != NULL;
3563 : }
3564 :
3565 : static struct spdk_nvmf_ns_reservation_ops g_reservation_ops = {
3566 : .is_ptpl_capable = nvmf_ns_is_ptpl_capable_json,
3567 : .update = nvmf_ns_reservation_update_json,
3568 : .load = nvmf_ns_reservation_load_json,
3569 : };
3570 :
3571 : bool
3572 15 : nvmf_ns_is_ptpl_capable(const struct spdk_nvmf_ns *ns)
3573 : {
3574 15 : return g_reservation_ops.is_ptpl_capable(ns);
3575 : }
3576 :
3577 : static int
3578 7 : nvmf_ns_reservation_update(const struct spdk_nvmf_ns *ns,
3579 : const struct spdk_nvmf_reservation_info *info)
3580 : {
3581 7 : return g_reservation_ops.update(ns, info);
3582 : }
3583 :
3584 : static int
3585 6 : nvmf_ns_reservation_load(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
3586 : {
3587 6 : return g_reservation_ops.load(ns, info);
3588 : }
3589 :
3590 : void
3591 1 : spdk_nvmf_set_custom_ns_reservation_ops(const struct spdk_nvmf_ns_reservation_ops *ops)
3592 : {
3593 1 : g_reservation_ops = *ops;
3594 1 : }
3595 :
3596 : int
3597 0 : spdk_nvmf_subsystem_set_ana_reporting(struct spdk_nvmf_subsystem *subsystem,
3598 : bool ana_reporting)
3599 : {
3600 0 : if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
3601 0 : return -EAGAIN;
3602 : }
3603 :
3604 0 : subsystem->flags.ana_reporting = ana_reporting;
3605 :
3606 0 : return 0;
3607 : }
3608 :
3609 : bool
3610 0 : spdk_nvmf_subsystem_get_ana_reporting(struct spdk_nvmf_subsystem *subsystem)
3611 : {
3612 0 : return subsystem->flags.ana_reporting;
3613 : }
3614 :
3615 : struct subsystem_listener_update_ctx {
3616 : struct spdk_nvmf_subsystem_listener *listener;
3617 :
3618 : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn;
3619 : void *cb_arg;
3620 : };
3621 :
3622 : static void
3623 0 : subsystem_listener_update_done(struct spdk_io_channel_iter *i, int status)
3624 : {
3625 0 : struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
3626 :
3627 0 : if (ctx->cb_fn) {
3628 0 : ctx->cb_fn(ctx->cb_arg, status);
3629 : }
3630 0 : free(ctx);
3631 0 : }
3632 :
3633 : static void
3634 0 : subsystem_listener_update_on_pg(struct spdk_io_channel_iter *i)
3635 : {
3636 0 : struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
3637 : struct spdk_nvmf_subsystem_listener *listener;
3638 : struct spdk_nvmf_poll_group *group;
3639 : struct spdk_nvmf_ctrlr *ctrlr;
3640 :
3641 0 : listener = ctx->listener;
3642 0 : group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
3643 :
3644 0 : TAILQ_FOREACH(ctrlr, &listener->subsystem->ctrlrs, link) {
3645 0 : if (ctrlr->thread != spdk_get_thread()) {
3646 0 : continue;
3647 : }
3648 :
3649 0 : if (ctrlr->admin_qpair && ctrlr->admin_qpair->group == group && ctrlr->listener == listener) {
3650 0 : nvmf_ctrlr_async_event_ana_change_notice(ctrlr);
3651 : }
3652 : }
3653 :
3654 0 : spdk_for_each_channel_continue(i, 0);
3655 0 : }
3656 :
3657 : void
3658 0 : spdk_nvmf_subsystem_set_ana_state(struct spdk_nvmf_subsystem *subsystem,
3659 : const struct spdk_nvme_transport_id *trid,
3660 : enum spdk_nvme_ana_state ana_state, uint32_t anagrpid,
3661 : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, void *cb_arg)
3662 : {
3663 : struct spdk_nvmf_subsystem_listener *listener;
3664 : struct subsystem_listener_update_ctx *ctx;
3665 : uint32_t i;
3666 :
3667 0 : assert(cb_fn != NULL);
3668 0 : assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
3669 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED);
3670 :
3671 0 : if (!subsystem->flags.ana_reporting) {
3672 0 : SPDK_ERRLOG("ANA reporting is disabled\n");
3673 0 : cb_fn(cb_arg, -EINVAL);
3674 0 : return;
3675 : }
3676 :
3677 : /* ANA Change state is not used, ANA Persistent Loss state
3678 : * is not supported yet.
3679 : */
3680 0 : if (!(ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE ||
3681 : ana_state == SPDK_NVME_ANA_NON_OPTIMIZED_STATE ||
3682 : ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE)) {
3683 0 : SPDK_ERRLOG("ANA state %d is not supported\n", ana_state);
3684 0 : cb_fn(cb_arg, -ENOTSUP);
3685 0 : return;
3686 : }
3687 :
3688 0 : if (anagrpid > subsystem->max_nsid) {
3689 0 : SPDK_ERRLOG("ANA group ID %" PRIu32 " is more than maximum\n", anagrpid);
3690 0 : cb_fn(cb_arg, -EINVAL);
3691 0 : return;
3692 : }
3693 :
3694 0 : listener = nvmf_subsystem_find_listener(subsystem, trid);
3695 0 : if (!listener) {
3696 0 : SPDK_ERRLOG("Unable to find listener.\n");
3697 0 : cb_fn(cb_arg, -EINVAL);
3698 0 : return;
3699 : }
3700 :
3701 0 : if (anagrpid != 0 && listener->ana_state[anagrpid - 1] == ana_state) {
3702 0 : cb_fn(cb_arg, 0);
3703 0 : return;
3704 : }
3705 :
3706 0 : ctx = calloc(1, sizeof(*ctx));
3707 0 : if (!ctx) {
3708 0 : SPDK_ERRLOG("Unable to allocate context\n");
3709 0 : cb_fn(cb_arg, -ENOMEM);
3710 0 : return;
3711 : }
3712 :
3713 0 : for (i = 1; i <= subsystem->max_nsid; i++) {
3714 0 : if (anagrpid == 0 || i == anagrpid) {
3715 0 : listener->ana_state[i - 1] = ana_state;
3716 : }
3717 : }
3718 0 : listener->ana_state_change_count++;
3719 :
3720 0 : ctx->listener = listener;
3721 0 : ctx->cb_fn = cb_fn;
3722 0 : ctx->cb_arg = cb_arg;
3723 :
3724 0 : spdk_for_each_channel(subsystem->tgt,
3725 : subsystem_listener_update_on_pg,
3726 : ctx,
3727 : subsystem_listener_update_done);
3728 : }
3729 :
3730 : int
3731 0 : spdk_nvmf_subsystem_get_ana_state(struct spdk_nvmf_subsystem *subsystem,
3732 : const struct spdk_nvme_transport_id *trid,
3733 : uint32_t anagrpid,
3734 : enum spdk_nvme_ana_state *ana_state)
3735 : {
3736 0 : assert(ana_state != NULL);
3737 :
3738 : struct spdk_nvmf_subsystem_listener *listener;
3739 :
3740 0 : if (!subsystem->flags.ana_reporting) {
3741 0 : SPDK_ERRLOG("ANA reporting is disabled\n");
3742 0 : return -EINVAL;
3743 : }
3744 :
3745 0 : if (anagrpid <= 0 || anagrpid > subsystem->max_nsid) {
3746 0 : SPDK_ERRLOG("ANA group ID %" PRIu32 " is invalid\n", anagrpid);
3747 0 : return -EINVAL;
3748 : }
3749 :
3750 0 : listener = nvmf_subsystem_find_listener(subsystem, trid);
3751 0 : if (!listener) {
3752 0 : SPDK_ERRLOG("Unable to find listener.\n");
3753 0 : return -EINVAL;
3754 : }
3755 :
3756 0 : *ana_state = listener->ana_state[anagrpid - 1];
3757 0 : return 0;
3758 : }
3759 :
3760 : bool
3761 0 : spdk_nvmf_subsystem_is_discovery(struct spdk_nvmf_subsystem *subsystem)
3762 : {
3763 0 : return subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT ||
3764 0 : subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY;
3765 : }
3766 :
3767 : bool
3768 0 : nvmf_nqn_is_discovery(const char *nqn)
3769 : {
3770 0 : return strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN) == 0;
3771 : }
|