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