xref: /spdk/lib/nvmf/subsystem.c (revision 3630473789c359155f05075bea018c32d24032b3)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "nvmf_internal.h"
37 #include "transport.h"
38 
39 #include "spdk/likely.h"
40 #include "spdk/string.h"
41 #include "spdk/trace.h"
42 #include "spdk/nvmf_spec.h"
43 #include "spdk/uuid.h"
44 #include "spdk/json.h"
45 #include "spdk/file.h"
46 
47 #define __SPDK_BDEV_MODULE_ONLY
48 #include "spdk/bdev_module.h"
49 #include "spdk/log.h"
50 #include "spdk_internal/utf.h"
51 #include "spdk_internal/usdt.h"
52 
53 #define MODEL_NUMBER_DEFAULT "SPDK bdev Controller"
54 #define NVMF_SUBSYSTEM_DEFAULT_NAMESPACES 32
55 
56 /*
57  * States for parsing valid domains in NQNs according to RFC 1034
58  */
59 enum spdk_nvmf_nqn_domain_states {
60 	/* First character of a domain must be a letter */
61 	SPDK_NVMF_DOMAIN_ACCEPT_LETTER = 0,
62 
63 	/* Subsequent characters can be any of letter, digit, or hyphen */
64 	SPDK_NVMF_DOMAIN_ACCEPT_LDH = 1,
65 
66 	/* A domain label must end with either a letter or digit */
67 	SPDK_NVMF_DOMAIN_ACCEPT_ANY = 2
68 };
69 
70 /* Returns true if is a valid ASCII string as defined by the NVMe spec */
71 static bool
72 nvmf_valid_ascii_string(const void *buf, size_t size)
73 {
74 	const uint8_t *str = buf;
75 	size_t i;
76 
77 	for (i = 0; i < size; i++) {
78 		if (str[i] < 0x20 || str[i] > 0x7E) {
79 			return false;
80 		}
81 	}
82 
83 	return true;
84 }
85 
86 static bool
87 nvmf_valid_nqn(const char *nqn)
88 {
89 	size_t len;
90 	struct spdk_uuid uuid_value;
91 	uint32_t i;
92 	int bytes_consumed;
93 	uint32_t domain_label_length;
94 	char *reverse_domain_end;
95 	uint32_t reverse_domain_end_index;
96 	enum spdk_nvmf_nqn_domain_states domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER;
97 
98 	/* Check for length requirements */
99 	len = strlen(nqn);
100 	if (len > SPDK_NVMF_NQN_MAX_LEN) {
101 		SPDK_ERRLOG("Invalid NQN \"%s\": length %zu > max %d\n", nqn, len, SPDK_NVMF_NQN_MAX_LEN);
102 		return false;
103 	}
104 
105 	/* The nqn must be at least as long as SPDK_NVMF_NQN_MIN_LEN to contain the necessary prefix. */
106 	if (len < SPDK_NVMF_NQN_MIN_LEN) {
107 		SPDK_ERRLOG("Invalid NQN \"%s\": length %zu < min %d\n", nqn, len, SPDK_NVMF_NQN_MIN_LEN);
108 		return false;
109 	}
110 
111 	/* Check for discovery controller nqn */
112 	if (!strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN)) {
113 		return true;
114 	}
115 
116 	/* Check for equality with the generic nqn structure of the form "nqn.2014-08.org.nvmexpress:uuid:11111111-2222-3333-4444-555555555555" */
117 	if (!strncmp(nqn, SPDK_NVMF_NQN_UUID_PRE, SPDK_NVMF_NQN_UUID_PRE_LEN)) {
118 		if (len != SPDK_NVMF_NQN_UUID_PRE_LEN + SPDK_NVMF_UUID_STRING_LEN) {
119 			SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not the correct length\n", nqn);
120 			return false;
121 		}
122 
123 		if (spdk_uuid_parse(&uuid_value, &nqn[SPDK_NVMF_NQN_UUID_PRE_LEN])) {
124 			SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not formatted correctly\n", nqn);
125 			return false;
126 		}
127 		return true;
128 	}
129 
130 	/* If the nqn does not match the uuid structure, the next several checks validate the form "nqn.yyyy-mm.reverse.domain:user-string" */
131 
132 	if (strncmp(nqn, "nqn.", 4) != 0) {
133 		SPDK_ERRLOG("Invalid NQN \"%s\": NQN must begin with \"nqn.\".\n", nqn);
134 		return false;
135 	}
136 
137 	/* Check for yyyy-mm. */
138 	if (!(isdigit(nqn[4]) && isdigit(nqn[5]) && isdigit(nqn[6]) && isdigit(nqn[7]) &&
139 	      nqn[8] == '-' && isdigit(nqn[9]) && isdigit(nqn[10]) && nqn[11] == '.')) {
140 		SPDK_ERRLOG("Invalid date code in NQN \"%s\"\n", nqn);
141 		return false;
142 	}
143 
144 	reverse_domain_end = strchr(nqn, ':');
145 	if (reverse_domain_end != NULL && (reverse_domain_end_index = reverse_domain_end - nqn) < len - 1) {
146 	} else {
147 		SPDK_ERRLOG("Invalid NQN \"%s\". NQN must contain user specified name with a ':' as a prefix.\n",
148 			    nqn);
149 		return false;
150 	}
151 
152 	/* Check for valid reverse domain */
153 	domain_label_length = 0;
154 	for (i = 12; i < reverse_domain_end_index; i++) {
155 		if (domain_label_length > SPDK_DOMAIN_LABEL_MAX_LEN) {
156 			SPDK_ERRLOG("Invalid domain name in NQN \"%s\". At least one Label is too long.\n", nqn);
157 			return false;
158 		}
159 
160 		switch (domain_state) {
161 
162 		case SPDK_NVMF_DOMAIN_ACCEPT_LETTER: {
163 			if (isalpha(nqn[i])) {
164 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
165 				domain_label_length++;
166 				break;
167 			} else {
168 				SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must start with a letter.\n", nqn);
169 				return false;
170 			}
171 		}
172 
173 		case SPDK_NVMF_DOMAIN_ACCEPT_LDH: {
174 			if (isalpha(nqn[i]) || isdigit(nqn[i])) {
175 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
176 				domain_label_length++;
177 				break;
178 			} else if (nqn[i] == '-') {
179 				if (i == reverse_domain_end_index - 1) {
180 					SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
181 						    nqn);
182 					return false;
183 				}
184 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH;
185 				domain_label_length++;
186 				break;
187 			} else if (nqn[i] == '.') {
188 				SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
189 					    nqn);
190 				return false;
191 			} else {
192 				SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n",
193 					    nqn);
194 				return false;
195 			}
196 		}
197 
198 		case SPDK_NVMF_DOMAIN_ACCEPT_ANY: {
199 			if (isalpha(nqn[i]) || isdigit(nqn[i])) {
200 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
201 				domain_label_length++;
202 				break;
203 			} else if (nqn[i] == '-') {
204 				if (i == reverse_domain_end_index - 1) {
205 					SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
206 						    nqn);
207 					return false;
208 				}
209 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH;
210 				domain_label_length++;
211 				break;
212 			} else if (nqn[i] == '.') {
213 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER;
214 				domain_label_length = 0;
215 				break;
216 			} else {
217 				SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n",
218 					    nqn);
219 				return false;
220 			}
221 		}
222 		}
223 	}
224 
225 	i = reverse_domain_end_index + 1;
226 	while (i < len) {
227 		bytes_consumed = utf8_valid(&nqn[i], &nqn[len]);
228 		if (bytes_consumed <= 0) {
229 			SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only valid utf-8.\n", nqn);
230 			return false;
231 		}
232 
233 		i += bytes_consumed;
234 	}
235 	return true;
236 }
237 
238 static void subsystem_state_change_on_pg(struct spdk_io_channel_iter *i);
239 
240 struct spdk_nvmf_subsystem *
241 spdk_nvmf_subsystem_create(struct spdk_nvmf_tgt *tgt,
242 			   const char *nqn,
243 			   enum spdk_nvmf_subtype type,
244 			   uint32_t num_ns)
245 {
246 	struct spdk_nvmf_subsystem	*subsystem;
247 	uint32_t			sid;
248 
249 	if (spdk_nvmf_tgt_find_subsystem(tgt, nqn)) {
250 		SPDK_ERRLOG("Subsystem NQN '%s' already exists\n", nqn);
251 		return NULL;
252 	}
253 
254 	if (!nvmf_valid_nqn(nqn)) {
255 		return NULL;
256 	}
257 
258 	if (type == SPDK_NVMF_SUBTYPE_DISCOVERY) {
259 		if (num_ns != 0) {
260 			SPDK_ERRLOG("Discovery subsystem cannot have namespaces.\n");
261 			return NULL;
262 		}
263 	} else if (num_ns == 0) {
264 		num_ns = NVMF_SUBSYSTEM_DEFAULT_NAMESPACES;
265 	}
266 
267 	/* Find a free subsystem id (sid) */
268 	for (sid = 0; sid < tgt->max_subsystems; sid++) {
269 		if (tgt->subsystems[sid] == NULL) {
270 			break;
271 		}
272 	}
273 	if (sid >= tgt->max_subsystems) {
274 		return NULL;
275 	}
276 
277 	subsystem = calloc(1, sizeof(struct spdk_nvmf_subsystem));
278 	if (subsystem == NULL) {
279 		return NULL;
280 	}
281 
282 	subsystem->thread = spdk_get_thread();
283 	subsystem->state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
284 	subsystem->tgt = tgt;
285 	subsystem->id = sid;
286 	subsystem->subtype = type;
287 	subsystem->max_nsid = num_ns;
288 	subsystem->next_cntlid = 0;
289 	subsystem->min_cntlid = NVMF_MIN_CNTLID;
290 	subsystem->max_cntlid = NVMF_MAX_CNTLID;
291 	snprintf(subsystem->subnqn, sizeof(subsystem->subnqn), "%s", nqn);
292 	pthread_mutex_init(&subsystem->mutex, NULL);
293 	TAILQ_INIT(&subsystem->listeners);
294 	TAILQ_INIT(&subsystem->hosts);
295 	TAILQ_INIT(&subsystem->ctrlrs);
296 
297 	if (num_ns != 0) {
298 		subsystem->ns = calloc(num_ns, sizeof(struct spdk_nvmf_ns *));
299 		if (subsystem->ns == NULL) {
300 			SPDK_ERRLOG("Namespace memory allocation failed\n");
301 			pthread_mutex_destroy(&subsystem->mutex);
302 			free(subsystem);
303 			return NULL;
304 		}
305 	}
306 
307 	memset(subsystem->sn, '0', sizeof(subsystem->sn) - 1);
308 	subsystem->sn[sizeof(subsystem->sn) - 1] = '\0';
309 
310 	snprintf(subsystem->mn, sizeof(subsystem->mn), "%s",
311 		 MODEL_NUMBER_DEFAULT);
312 
313 	tgt->subsystems[sid] = subsystem;
314 	nvmf_update_discovery_log(tgt, NULL);
315 
316 	return subsystem;
317 }
318 
319 /* Must hold subsystem->mutex while calling this function */
320 static void
321 nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_host *host)
322 {
323 	TAILQ_REMOVE(&subsystem->hosts, host, link);
324 	free(host);
325 }
326 
327 static void
328 _nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem,
329 				struct spdk_nvmf_subsystem_listener *listener,
330 				bool stop)
331 {
332 	struct spdk_nvmf_transport *transport;
333 
334 	if (stop) {
335 		transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, listener->trid->trstring);
336 		if (transport != NULL) {
337 			spdk_nvmf_transport_stop_listen(transport, listener->trid);
338 		}
339 	}
340 
341 	TAILQ_REMOVE(&subsystem->listeners, listener, link);
342 	free(listener);
343 }
344 
345 void
346 spdk_nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem)
347 {
348 	struct spdk_nvmf_host		*host, *host_tmp;
349 	struct spdk_nvmf_ctrlr		*ctrlr, *ctrlr_tmp;
350 	struct spdk_nvmf_ns		*ns;
351 
352 	if (!subsystem) {
353 		return;
354 	}
355 
356 	assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE);
357 
358 	SPDK_DEBUGLOG(nvmf, "subsystem is %p\n", subsystem);
359 
360 	nvmf_subsystem_remove_all_listeners(subsystem, false);
361 
362 	pthread_mutex_lock(&subsystem->mutex);
363 
364 	TAILQ_FOREACH_SAFE(host, &subsystem->hosts, link, host_tmp) {
365 		nvmf_subsystem_remove_host(subsystem, host);
366 	}
367 
368 	pthread_mutex_unlock(&subsystem->mutex);
369 
370 	TAILQ_FOREACH_SAFE(ctrlr, &subsystem->ctrlrs, link, ctrlr_tmp) {
371 		nvmf_ctrlr_destruct(ctrlr);
372 	}
373 
374 	ns = spdk_nvmf_subsystem_get_first_ns(subsystem);
375 	while (ns != NULL) {
376 		struct spdk_nvmf_ns *next_ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns);
377 
378 		spdk_nvmf_subsystem_remove_ns(subsystem, ns->opts.nsid);
379 		ns = next_ns;
380 	}
381 
382 	free(subsystem->ns);
383 
384 	subsystem->tgt->subsystems[subsystem->id] = NULL;
385 	nvmf_update_discovery_log(subsystem->tgt, NULL);
386 
387 	pthread_mutex_destroy(&subsystem->mutex);
388 
389 	free(subsystem);
390 }
391 
392 
393 /* we have to use the typedef in the function declaration to appease astyle. */
394 typedef enum spdk_nvmf_subsystem_state spdk_nvmf_subsystem_state_t;
395 
396 static spdk_nvmf_subsystem_state_t
397 nvmf_subsystem_get_intermediate_state(enum spdk_nvmf_subsystem_state current_state,
398 				      enum spdk_nvmf_subsystem_state requested_state)
399 {
400 	switch (requested_state) {
401 	case SPDK_NVMF_SUBSYSTEM_INACTIVE:
402 		return SPDK_NVMF_SUBSYSTEM_DEACTIVATING;
403 	case SPDK_NVMF_SUBSYSTEM_ACTIVE:
404 		if (current_state == SPDK_NVMF_SUBSYSTEM_PAUSED) {
405 			return SPDK_NVMF_SUBSYSTEM_RESUMING;
406 		} else {
407 			return SPDK_NVMF_SUBSYSTEM_ACTIVATING;
408 		}
409 	case SPDK_NVMF_SUBSYSTEM_PAUSED:
410 		return SPDK_NVMF_SUBSYSTEM_PAUSING;
411 	default:
412 		assert(false);
413 		return SPDK_NVMF_SUBSYSTEM_NUM_STATES;
414 	}
415 }
416 
417 static int
418 nvmf_subsystem_set_state(struct spdk_nvmf_subsystem *subsystem,
419 			 enum spdk_nvmf_subsystem_state state)
420 {
421 	enum spdk_nvmf_subsystem_state actual_old_state, expected_old_state;
422 	bool exchanged;
423 
424 	switch (state) {
425 	case SPDK_NVMF_SUBSYSTEM_INACTIVE:
426 		expected_old_state = SPDK_NVMF_SUBSYSTEM_DEACTIVATING;
427 		break;
428 	case SPDK_NVMF_SUBSYSTEM_ACTIVATING:
429 		expected_old_state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
430 		break;
431 	case SPDK_NVMF_SUBSYSTEM_ACTIVE:
432 		expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING;
433 		break;
434 	case SPDK_NVMF_SUBSYSTEM_PAUSING:
435 		expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
436 		break;
437 	case SPDK_NVMF_SUBSYSTEM_PAUSED:
438 		expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSING;
439 		break;
440 	case SPDK_NVMF_SUBSYSTEM_RESUMING:
441 		expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSED;
442 		break;
443 	case SPDK_NVMF_SUBSYSTEM_DEACTIVATING:
444 		expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
445 		break;
446 	default:
447 		assert(false);
448 		return -1;
449 	}
450 
451 	actual_old_state = expected_old_state;
452 	exchanged = __atomic_compare_exchange_n(&subsystem->state, &actual_old_state, state, false,
453 						__ATOMIC_RELAXED, __ATOMIC_RELAXED);
454 	if (spdk_unlikely(exchanged == false)) {
455 		if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING &&
456 		    state == SPDK_NVMF_SUBSYSTEM_ACTIVE) {
457 			expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING;
458 		}
459 		/* This is for the case when activating the subsystem fails. */
460 		if (actual_old_state == SPDK_NVMF_SUBSYSTEM_ACTIVATING &&
461 		    state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING) {
462 			expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING;
463 		}
464 		/* This is for the case when resuming the subsystem fails. */
465 		if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING &&
466 		    state == SPDK_NVMF_SUBSYSTEM_PAUSING) {
467 			expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING;
468 		}
469 		actual_old_state = expected_old_state;
470 		__atomic_compare_exchange_n(&subsystem->state, &actual_old_state, state, false,
471 					    __ATOMIC_RELAXED, __ATOMIC_RELAXED);
472 	}
473 	assert(actual_old_state == expected_old_state);
474 	return actual_old_state - expected_old_state;
475 }
476 
477 struct subsystem_state_change_ctx {
478 	struct spdk_nvmf_subsystem		*subsystem;
479 	uint16_t				nsid;
480 
481 	enum spdk_nvmf_subsystem_state		original_state;
482 	enum spdk_nvmf_subsystem_state		requested_state;
483 
484 	spdk_nvmf_subsystem_state_change_done	cb_fn;
485 	void					*cb_arg;
486 };
487 
488 static void
489 subsystem_state_change_revert_done(struct spdk_io_channel_iter *i, int status)
490 {
491 	struct subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
492 
493 	/* Nothing to be done here if the state setting fails, we are just screwed. */
494 	if (nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state)) {
495 		SPDK_ERRLOG("Unable to revert the subsystem state after operation failure.\n");
496 	}
497 
498 	ctx->subsystem->changing_state = false;
499 	if (ctx->cb_fn) {
500 		/* return a failure here. This function only exists in an error path. */
501 		ctx->cb_fn(ctx->subsystem, ctx->cb_arg, -1);
502 	}
503 	free(ctx);
504 }
505 
506 static void
507 subsystem_state_change_done(struct spdk_io_channel_iter *i, int status)
508 {
509 	struct subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
510 	enum spdk_nvmf_subsystem_state intermediate_state;
511 
512 	SPDK_DTRACE_PROBE4(nvmf_subsystem_change_state_done, ctx->subsystem->subnqn,
513 			   ctx->requested_state, ctx->original_state, status);
514 
515 	if (status == 0) {
516 		status = nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state);
517 		if (status) {
518 			status = -1;
519 		}
520 	}
521 
522 	if (status) {
523 		intermediate_state = nvmf_subsystem_get_intermediate_state(ctx->requested_state,
524 				     ctx->original_state);
525 		assert(intermediate_state != SPDK_NVMF_SUBSYSTEM_NUM_STATES);
526 
527 		if (nvmf_subsystem_set_state(ctx->subsystem, intermediate_state)) {
528 			goto out;
529 		}
530 		ctx->requested_state = ctx->original_state;
531 		spdk_for_each_channel(ctx->subsystem->tgt,
532 				      subsystem_state_change_on_pg,
533 				      ctx,
534 				      subsystem_state_change_revert_done);
535 		return;
536 	}
537 
538 out:
539 	ctx->subsystem->changing_state = false;
540 	if (ctx->cb_fn) {
541 		ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status);
542 	}
543 	free(ctx);
544 }
545 
546 static void
547 subsystem_state_change_continue(void *ctx, int status)
548 {
549 	struct spdk_io_channel_iter *i = ctx;
550 	struct subsystem_state_change_ctx *_ctx __attribute__((unused));
551 
552 	_ctx = spdk_io_channel_iter_get_ctx(i);
553 	SPDK_DTRACE_PROBE3(nvmf_pg_change_state_done, _ctx->subsystem->subnqn,
554 			   _ctx->requested_state, spdk_thread_get_id(spdk_get_thread()));
555 
556 	spdk_for_each_channel_continue(i, status);
557 }
558 
559 static void
560 subsystem_state_change_on_pg(struct spdk_io_channel_iter *i)
561 {
562 	struct subsystem_state_change_ctx *ctx;
563 	struct spdk_io_channel *ch;
564 	struct spdk_nvmf_poll_group *group;
565 
566 	ctx = spdk_io_channel_iter_get_ctx(i);
567 	ch = spdk_io_channel_iter_get_channel(i);
568 	group = spdk_io_channel_get_ctx(ch);
569 
570 	SPDK_DTRACE_PROBE3(nvmf_pg_change_state, ctx->subsystem->subnqn,
571 			   ctx->requested_state, spdk_thread_get_id(spdk_get_thread()));
572 	switch (ctx->requested_state) {
573 	case SPDK_NVMF_SUBSYSTEM_INACTIVE:
574 		nvmf_poll_group_remove_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
575 		break;
576 	case SPDK_NVMF_SUBSYSTEM_ACTIVE:
577 		if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_ACTIVATING) {
578 			nvmf_poll_group_add_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
579 		} else if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_RESUMING) {
580 			nvmf_poll_group_resume_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
581 		}
582 		break;
583 	case SPDK_NVMF_SUBSYSTEM_PAUSED:
584 		nvmf_poll_group_pause_subsystem(group, ctx->subsystem, ctx->nsid, subsystem_state_change_continue,
585 						i);
586 		break;
587 	default:
588 		assert(false);
589 		break;
590 	}
591 }
592 
593 static int
594 nvmf_subsystem_state_change(struct spdk_nvmf_subsystem *subsystem,
595 			    uint32_t nsid,
596 			    enum spdk_nvmf_subsystem_state requested_state,
597 			    spdk_nvmf_subsystem_state_change_done cb_fn,
598 			    void *cb_arg)
599 {
600 	struct subsystem_state_change_ctx *ctx;
601 	enum spdk_nvmf_subsystem_state intermediate_state;
602 	int rc;
603 
604 	if (__sync_val_compare_and_swap(&subsystem->changing_state, false, true)) {
605 		return -EBUSY;
606 	}
607 
608 	SPDK_DTRACE_PROBE3(nvmf_subsystem_change_state, subsystem->subnqn,
609 			   requested_state, subsystem->state);
610 	/* If we are already in the requested state, just call the callback immediately. */
611 	if (subsystem->state == requested_state) {
612 		subsystem->changing_state = false;
613 		if (cb_fn) {
614 			cb_fn(subsystem, cb_arg, 0);
615 		}
616 		return 0;
617 	}
618 
619 	intermediate_state = nvmf_subsystem_get_intermediate_state(subsystem->state, requested_state);
620 	assert(intermediate_state != SPDK_NVMF_SUBSYSTEM_NUM_STATES);
621 
622 	ctx = calloc(1, sizeof(*ctx));
623 	if (!ctx) {
624 		subsystem->changing_state = false;
625 		return -ENOMEM;
626 	}
627 
628 	ctx->original_state = subsystem->state;
629 	rc = nvmf_subsystem_set_state(subsystem, intermediate_state);
630 	if (rc) {
631 		free(ctx);
632 		subsystem->changing_state = false;
633 		return rc;
634 	}
635 
636 	ctx->subsystem = subsystem;
637 	ctx->nsid = nsid;
638 	ctx->requested_state = requested_state;
639 	ctx->cb_fn = cb_fn;
640 	ctx->cb_arg = cb_arg;
641 
642 	spdk_for_each_channel(subsystem->tgt,
643 			      subsystem_state_change_on_pg,
644 			      ctx,
645 			      subsystem_state_change_done);
646 
647 	return 0;
648 }
649 
650 int
651 spdk_nvmf_subsystem_start(struct spdk_nvmf_subsystem *subsystem,
652 			  spdk_nvmf_subsystem_state_change_done cb_fn,
653 			  void *cb_arg)
654 {
655 	return nvmf_subsystem_state_change(subsystem, 0, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg);
656 }
657 
658 int
659 spdk_nvmf_subsystem_stop(struct spdk_nvmf_subsystem *subsystem,
660 			 spdk_nvmf_subsystem_state_change_done cb_fn,
661 			 void *cb_arg)
662 {
663 	return nvmf_subsystem_state_change(subsystem, 0, SPDK_NVMF_SUBSYSTEM_INACTIVE, cb_fn, cb_arg);
664 }
665 
666 int
667 spdk_nvmf_subsystem_pause(struct spdk_nvmf_subsystem *subsystem,
668 			  uint32_t nsid,
669 			  spdk_nvmf_subsystem_state_change_done cb_fn,
670 			  void *cb_arg)
671 {
672 	return nvmf_subsystem_state_change(subsystem, nsid, SPDK_NVMF_SUBSYSTEM_PAUSED, cb_fn, cb_arg);
673 }
674 
675 int
676 spdk_nvmf_subsystem_resume(struct spdk_nvmf_subsystem *subsystem,
677 			   spdk_nvmf_subsystem_state_change_done cb_fn,
678 			   void *cb_arg)
679 {
680 	return nvmf_subsystem_state_change(subsystem, 0, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg);
681 }
682 
683 struct spdk_nvmf_subsystem *
684 spdk_nvmf_subsystem_get_first(struct spdk_nvmf_tgt *tgt)
685 {
686 	struct spdk_nvmf_subsystem	*subsystem;
687 	uint32_t sid;
688 
689 	for (sid = 0; sid < tgt->max_subsystems; sid++) {
690 		subsystem = tgt->subsystems[sid];
691 		if (subsystem) {
692 			return subsystem;
693 		}
694 	}
695 
696 	return NULL;
697 }
698 
699 struct spdk_nvmf_subsystem *
700 spdk_nvmf_subsystem_get_next(struct spdk_nvmf_subsystem *subsystem)
701 {
702 	uint32_t sid;
703 	struct spdk_nvmf_tgt *tgt;
704 
705 	if (!subsystem) {
706 		return NULL;
707 	}
708 
709 	tgt = subsystem->tgt;
710 
711 	for (sid = subsystem->id + 1; sid < tgt->max_subsystems; sid++) {
712 		subsystem = tgt->subsystems[sid];
713 		if (subsystem) {
714 			return subsystem;
715 		}
716 	}
717 
718 	return NULL;
719 }
720 
721 /* Must hold subsystem->mutex while calling this function */
722 static struct spdk_nvmf_host *
723 nvmf_subsystem_find_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
724 {
725 	struct spdk_nvmf_host *host = NULL;
726 
727 	TAILQ_FOREACH(host, &subsystem->hosts, link) {
728 		if (strcmp(hostnqn, host->nqn) == 0) {
729 			return host;
730 		}
731 	}
732 
733 	return NULL;
734 }
735 
736 int
737 spdk_nvmf_subsystem_add_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
738 {
739 	struct spdk_nvmf_host *host;
740 
741 	if (!nvmf_valid_nqn(hostnqn)) {
742 		return -EINVAL;
743 	}
744 
745 	pthread_mutex_lock(&subsystem->mutex);
746 
747 	if (nvmf_subsystem_find_host(subsystem, hostnqn)) {
748 		/* This subsystem already allows the specified host. */
749 		pthread_mutex_unlock(&subsystem->mutex);
750 		return 0;
751 	}
752 
753 	host = calloc(1, sizeof(*host));
754 	if (!host) {
755 		pthread_mutex_unlock(&subsystem->mutex);
756 		return -ENOMEM;
757 	}
758 
759 	snprintf(host->nqn, sizeof(host->nqn), "%s", hostnqn);
760 
761 	TAILQ_INSERT_HEAD(&subsystem->hosts, host, link);
762 
763 	nvmf_update_discovery_log(subsystem->tgt, hostnqn);
764 
765 	pthread_mutex_unlock(&subsystem->mutex);
766 
767 	return 0;
768 }
769 
770 int
771 spdk_nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
772 {
773 	struct spdk_nvmf_host *host;
774 
775 	pthread_mutex_lock(&subsystem->mutex);
776 
777 	host = nvmf_subsystem_find_host(subsystem, hostnqn);
778 	if (host == NULL) {
779 		pthread_mutex_unlock(&subsystem->mutex);
780 		return -ENOENT;
781 	}
782 
783 	nvmf_subsystem_remove_host(subsystem, host);
784 	pthread_mutex_unlock(&subsystem->mutex);
785 
786 	return 0;
787 }
788 
789 struct nvmf_subsystem_disconnect_host_ctx {
790 	struct spdk_nvmf_subsystem		*subsystem;
791 	char					*hostnqn;
792 	spdk_nvmf_tgt_subsystem_listen_done_fn	cb_fn;
793 	void					*cb_arg;
794 };
795 
796 static void
797 nvmf_subsystem_disconnect_host_fini(struct spdk_io_channel_iter *i, int status)
798 {
799 	struct nvmf_subsystem_disconnect_host_ctx *ctx;
800 
801 	ctx = spdk_io_channel_iter_get_ctx(i);
802 
803 	if (ctx->cb_fn) {
804 		ctx->cb_fn(ctx->cb_arg, status);
805 	}
806 	free(ctx->hostnqn);
807 	free(ctx);
808 }
809 
810 static void
811 nvmf_subsystem_disconnect_qpairs_by_host(struct spdk_io_channel_iter *i)
812 {
813 	struct nvmf_subsystem_disconnect_host_ctx *ctx;
814 	struct spdk_nvmf_poll_group *group;
815 	struct spdk_io_channel *ch;
816 	struct spdk_nvmf_qpair *qpair, *tmp_qpair;
817 	struct spdk_nvmf_ctrlr *ctrlr;
818 
819 	ctx = spdk_io_channel_iter_get_ctx(i);
820 	ch = spdk_io_channel_iter_get_channel(i);
821 	group = spdk_io_channel_get_ctx(ch);
822 
823 	TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, tmp_qpair) {
824 		ctrlr = qpair->ctrlr;
825 
826 		if (ctrlr == NULL || ctrlr->subsys != ctx->subsystem) {
827 			continue;
828 		}
829 
830 		if (strncmp(ctrlr->hostnqn, ctx->hostnqn, sizeof(ctrlr->hostnqn)) == 0) {
831 			/* Right now this does not wait for the queue pairs to actually disconnect. */
832 			spdk_nvmf_qpair_disconnect(qpair, NULL, NULL);
833 		}
834 	}
835 	spdk_for_each_channel_continue(i, 0);
836 }
837 
838 int
839 spdk_nvmf_subsystem_disconnect_host(struct spdk_nvmf_subsystem *subsystem,
840 				    const char *hostnqn,
841 				    spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
842 				    void *cb_arg)
843 {
844 	struct nvmf_subsystem_disconnect_host_ctx *ctx;
845 
846 	ctx = calloc(1, sizeof(struct nvmf_subsystem_disconnect_host_ctx));
847 	if (ctx == NULL) {
848 		return -ENOMEM;
849 	}
850 
851 	ctx->hostnqn = strdup(hostnqn);
852 	if (ctx->hostnqn == NULL) {
853 		free(ctx);
854 		return -ENOMEM;
855 	}
856 
857 	ctx->subsystem = subsystem;
858 	ctx->cb_fn = cb_fn;
859 	ctx->cb_arg = cb_arg;
860 
861 	spdk_for_each_channel(subsystem->tgt, nvmf_subsystem_disconnect_qpairs_by_host, ctx,
862 			      nvmf_subsystem_disconnect_host_fini);
863 
864 	return 0;
865 }
866 
867 int
868 spdk_nvmf_subsystem_set_allow_any_host(struct spdk_nvmf_subsystem *subsystem, bool allow_any_host)
869 {
870 	pthread_mutex_lock(&subsystem->mutex);
871 	subsystem->flags.allow_any_host = allow_any_host;
872 	nvmf_update_discovery_log(subsystem->tgt, NULL);
873 	pthread_mutex_unlock(&subsystem->mutex);
874 
875 	return 0;
876 }
877 
878 bool
879 spdk_nvmf_subsystem_get_allow_any_host(const struct spdk_nvmf_subsystem *subsystem)
880 {
881 	bool allow_any_host;
882 	struct spdk_nvmf_subsystem *sub;
883 
884 	/* Technically, taking the mutex modifies data in the subsystem. But the const
885 	 * is still important to convey that this doesn't mutate any other data. Cast
886 	 * it away to work around this. */
887 	sub = (struct spdk_nvmf_subsystem *)subsystem;
888 
889 	pthread_mutex_lock(&sub->mutex);
890 	allow_any_host = sub->flags.allow_any_host;
891 	pthread_mutex_unlock(&sub->mutex);
892 
893 	return allow_any_host;
894 }
895 
896 bool
897 spdk_nvmf_subsystem_host_allowed(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
898 {
899 	bool allowed;
900 
901 	if (!hostnqn) {
902 		return false;
903 	}
904 
905 	pthread_mutex_lock(&subsystem->mutex);
906 
907 	if (subsystem->flags.allow_any_host) {
908 		pthread_mutex_unlock(&subsystem->mutex);
909 		return true;
910 	}
911 
912 	allowed =  nvmf_subsystem_find_host(subsystem, hostnqn) != NULL;
913 	pthread_mutex_unlock(&subsystem->mutex);
914 
915 	return allowed;
916 }
917 
918 struct spdk_nvmf_host *
919 spdk_nvmf_subsystem_get_first_host(struct spdk_nvmf_subsystem *subsystem)
920 {
921 	return TAILQ_FIRST(&subsystem->hosts);
922 }
923 
924 
925 struct spdk_nvmf_host *
926 spdk_nvmf_subsystem_get_next_host(struct spdk_nvmf_subsystem *subsystem,
927 				  struct spdk_nvmf_host *prev_host)
928 {
929 	return TAILQ_NEXT(prev_host, link);
930 }
931 
932 const char *
933 spdk_nvmf_host_get_nqn(const struct spdk_nvmf_host *host)
934 {
935 	return host->nqn;
936 }
937 
938 struct spdk_nvmf_subsystem_listener *
939 nvmf_subsystem_find_listener(struct spdk_nvmf_subsystem *subsystem,
940 			     const struct spdk_nvme_transport_id *trid)
941 {
942 	struct spdk_nvmf_subsystem_listener *listener;
943 
944 	TAILQ_FOREACH(listener, &subsystem->listeners, link) {
945 		if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) {
946 			return listener;
947 		}
948 	}
949 
950 	return NULL;
951 }
952 
953 /**
954  * Function to be called once the target is listening.
955  *
956  * \param ctx Context argument passed to this function.
957  * \param status 0 if it completed successfully, or negative errno if it failed.
958  */
959 static void
960 _nvmf_subsystem_add_listener_done(void *ctx, int status)
961 {
962 	struct spdk_nvmf_subsystem_listener *listener = ctx;
963 
964 	if (status) {
965 		listener->cb_fn(listener->cb_arg, status);
966 		free(listener);
967 		return;
968 	}
969 
970 	TAILQ_INSERT_HEAD(&listener->subsystem->listeners, listener, link);
971 	nvmf_update_discovery_log(listener->subsystem->tgt, NULL);
972 	listener->cb_fn(listener->cb_arg, status);
973 }
974 
975 void
976 spdk_nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem,
977 				 struct spdk_nvme_transport_id *trid,
978 				 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
979 				 void *cb_arg)
980 {
981 	struct spdk_nvmf_transport *transport;
982 	struct spdk_nvmf_subsystem_listener *listener;
983 	struct spdk_nvmf_listener *tr_listener;
984 	int rc = 0;
985 
986 	assert(cb_fn != NULL);
987 
988 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
989 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
990 		cb_fn(cb_arg, -EAGAIN);
991 		return;
992 	}
993 
994 	if (nvmf_subsystem_find_listener(subsystem, trid)) {
995 		/* Listener already exists in this subsystem */
996 		cb_fn(cb_arg, 0);
997 		return;
998 	}
999 
1000 	transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, trid->trstring);
1001 	if (!transport) {
1002 		SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
1003 			    trid->trstring);
1004 		cb_fn(cb_arg, -EINVAL);
1005 		return;
1006 	}
1007 
1008 	tr_listener = nvmf_transport_find_listener(transport, trid);
1009 	if (!tr_listener) {
1010 		SPDK_ERRLOG("Cannot find transport listener for %s\n", trid->traddr);
1011 		cb_fn(cb_arg, -EINVAL);
1012 		return;
1013 	}
1014 
1015 	listener = calloc(1, sizeof(*listener));
1016 	if (!listener) {
1017 		cb_fn(cb_arg, -ENOMEM);
1018 		return;
1019 	}
1020 
1021 	listener->trid = &tr_listener->trid;
1022 	listener->transport = transport;
1023 	listener->cb_fn = cb_fn;
1024 	listener->cb_arg = cb_arg;
1025 	listener->subsystem = subsystem;
1026 	listener->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
1027 
1028 	if (transport->ops->listen_associate != NULL) {
1029 		rc = transport->ops->listen_associate(transport, subsystem, trid);
1030 	}
1031 
1032 	_nvmf_subsystem_add_listener_done(listener, rc);
1033 }
1034 
1035 int
1036 spdk_nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem,
1037 				    const struct spdk_nvme_transport_id *trid)
1038 {
1039 	struct spdk_nvmf_subsystem_listener *listener;
1040 
1041 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1042 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1043 		return -EAGAIN;
1044 	}
1045 
1046 	listener = nvmf_subsystem_find_listener(subsystem, trid);
1047 	if (listener == NULL) {
1048 		return -ENOENT;
1049 	}
1050 
1051 	_nvmf_subsystem_remove_listener(subsystem, listener, false);
1052 
1053 	return 0;
1054 }
1055 
1056 void
1057 nvmf_subsystem_remove_all_listeners(struct spdk_nvmf_subsystem *subsystem,
1058 				    bool stop)
1059 {
1060 	struct spdk_nvmf_subsystem_listener *listener, *listener_tmp;
1061 
1062 	TAILQ_FOREACH_SAFE(listener, &subsystem->listeners, link, listener_tmp) {
1063 		_nvmf_subsystem_remove_listener(subsystem, listener, stop);
1064 	}
1065 }
1066 
1067 bool
1068 spdk_nvmf_subsystem_listener_allowed(struct spdk_nvmf_subsystem *subsystem,
1069 				     const struct spdk_nvme_transport_id *trid)
1070 {
1071 	struct spdk_nvmf_subsystem_listener *listener;
1072 
1073 	if (!strcmp(subsystem->subnqn, SPDK_NVMF_DISCOVERY_NQN)) {
1074 		return true;
1075 	}
1076 
1077 	TAILQ_FOREACH(listener, &subsystem->listeners, link) {
1078 		if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) {
1079 			return true;
1080 		}
1081 	}
1082 
1083 	return false;
1084 }
1085 
1086 struct spdk_nvmf_subsystem_listener *
1087 spdk_nvmf_subsystem_get_first_listener(struct spdk_nvmf_subsystem *subsystem)
1088 {
1089 	return TAILQ_FIRST(&subsystem->listeners);
1090 }
1091 
1092 struct spdk_nvmf_subsystem_listener *
1093 spdk_nvmf_subsystem_get_next_listener(struct spdk_nvmf_subsystem *subsystem,
1094 				      struct spdk_nvmf_subsystem_listener *prev_listener)
1095 {
1096 	return TAILQ_NEXT(prev_listener, link);
1097 }
1098 
1099 const struct spdk_nvme_transport_id *
1100 spdk_nvmf_subsystem_listener_get_trid(struct spdk_nvmf_subsystem_listener *listener)
1101 {
1102 	return listener->trid;
1103 }
1104 
1105 void
1106 spdk_nvmf_subsystem_allow_any_listener(struct spdk_nvmf_subsystem *subsystem,
1107 				       bool allow_any_listener)
1108 {
1109 	subsystem->flags.allow_any_listener = allow_any_listener;
1110 }
1111 
1112 bool
1113 spdk_nvmf_subsytem_any_listener_allowed(struct spdk_nvmf_subsystem *subsystem)
1114 {
1115 	return subsystem->flags.allow_any_listener;
1116 }
1117 
1118 
1119 struct subsystem_update_ns_ctx {
1120 	struct spdk_nvmf_subsystem *subsystem;
1121 
1122 	spdk_nvmf_subsystem_state_change_done cb_fn;
1123 	void *cb_arg;
1124 };
1125 
1126 static void
1127 subsystem_update_ns_done(struct spdk_io_channel_iter *i, int status)
1128 {
1129 	struct subsystem_update_ns_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1130 
1131 	if (ctx->cb_fn) {
1132 		ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status);
1133 	}
1134 	free(ctx);
1135 }
1136 
1137 static void
1138 subsystem_update_ns_on_pg(struct spdk_io_channel_iter *i)
1139 {
1140 	int rc;
1141 	struct subsystem_update_ns_ctx *ctx;
1142 	struct spdk_nvmf_poll_group *group;
1143 	struct spdk_nvmf_subsystem *subsystem;
1144 
1145 	ctx = spdk_io_channel_iter_get_ctx(i);
1146 	group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
1147 	subsystem = ctx->subsystem;
1148 
1149 	rc = nvmf_poll_group_update_subsystem(group, subsystem);
1150 	spdk_for_each_channel_continue(i, rc);
1151 }
1152 
1153 static int
1154 nvmf_subsystem_update_ns(struct spdk_nvmf_subsystem *subsystem, spdk_channel_for_each_cpl cpl,
1155 			 void *ctx)
1156 {
1157 	spdk_for_each_channel(subsystem->tgt,
1158 			      subsystem_update_ns_on_pg,
1159 			      ctx,
1160 			      cpl);
1161 
1162 	return 0;
1163 }
1164 
1165 static void
1166 nvmf_subsystem_ns_changed(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1167 {
1168 	struct spdk_nvmf_ctrlr *ctrlr;
1169 
1170 	TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1171 		nvmf_ctrlr_ns_changed(ctrlr, nsid);
1172 	}
1173 }
1174 
1175 static uint32_t
1176 nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns);
1177 
1178 int
1179 spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1180 {
1181 	struct spdk_nvmf_transport *transport;
1182 	struct spdk_nvmf_ns *ns;
1183 
1184 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1185 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1186 		assert(false);
1187 		return -1;
1188 	}
1189 
1190 	if (nsid == 0 || nsid > subsystem->max_nsid) {
1191 		return -1;
1192 	}
1193 
1194 	ns = subsystem->ns[nsid - 1];
1195 	if (!ns) {
1196 		return -1;
1197 	}
1198 
1199 	subsystem->ns[nsid - 1] = NULL;
1200 
1201 	free(ns->ptpl_file);
1202 	nvmf_ns_reservation_clear_all_registrants(ns);
1203 	spdk_bdev_module_release_bdev(ns->bdev);
1204 	spdk_bdev_close(ns->desc);
1205 	free(ns);
1206 
1207 	for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1208 	     transport = spdk_nvmf_transport_get_next(transport)) {
1209 		if (transport->ops->subsystem_remove_ns) {
1210 			transport->ops->subsystem_remove_ns(transport, subsystem, nsid);
1211 		}
1212 	}
1213 
1214 	nvmf_subsystem_ns_changed(subsystem, nsid);
1215 
1216 	return 0;
1217 }
1218 
1219 struct subsystem_ns_change_ctx {
1220 	struct spdk_nvmf_subsystem		*subsystem;
1221 	spdk_nvmf_subsystem_state_change_done	cb_fn;
1222 	uint32_t				nsid;
1223 };
1224 
1225 static void
1226 _nvmf_ns_hot_remove(struct spdk_nvmf_subsystem *subsystem,
1227 		    void *cb_arg, int status)
1228 {
1229 	struct subsystem_ns_change_ctx *ctx = cb_arg;
1230 	int rc;
1231 
1232 	rc = spdk_nvmf_subsystem_remove_ns(subsystem, ctx->nsid);
1233 	if (rc != 0) {
1234 		SPDK_ERRLOG("Failed to make changes to NVME-oF subsystem with id: %u\n", subsystem->id);
1235 	}
1236 
1237 	spdk_nvmf_subsystem_resume(subsystem, NULL, NULL);
1238 
1239 	free(ctx);
1240 }
1241 
1242 static void
1243 nvmf_ns_change_msg(void *ns_ctx)
1244 {
1245 	struct subsystem_ns_change_ctx *ctx = ns_ctx;
1246 	int rc;
1247 
1248 	rc = spdk_nvmf_subsystem_pause(ctx->subsystem, ctx->nsid, ctx->cb_fn, ctx);
1249 	if (rc) {
1250 		if (rc == -EBUSY) {
1251 			/* Try again, this is not a permanent situation. */
1252 			spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ctx);
1253 		} else {
1254 			free(ctx);
1255 			SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n");
1256 		}
1257 	}
1258 }
1259 
1260 static void
1261 nvmf_ns_hot_remove(void *remove_ctx)
1262 {
1263 	struct spdk_nvmf_ns *ns = remove_ctx;
1264 	struct subsystem_ns_change_ctx *ns_ctx;
1265 	int rc;
1266 
1267 	/* We have to allocate a new context because this op
1268 	 * is asynchronous and we could lose the ns in the middle.
1269 	 */
1270 	ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx));
1271 	if (!ns_ctx) {
1272 		SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n");
1273 		return;
1274 	}
1275 
1276 	ns_ctx->subsystem = ns->subsystem;
1277 	ns_ctx->nsid = ns->opts.nsid;
1278 	ns_ctx->cb_fn = _nvmf_ns_hot_remove;
1279 
1280 	rc = spdk_nvmf_subsystem_pause(ns->subsystem, ns_ctx->nsid, _nvmf_ns_hot_remove, ns_ctx);
1281 	if (rc) {
1282 		if (rc == -EBUSY) {
1283 			/* Try again, this is not a permanent situation. */
1284 			spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx);
1285 		} else {
1286 			SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n");
1287 			free(ns_ctx);
1288 		}
1289 	}
1290 }
1291 
1292 static void
1293 _nvmf_ns_resize(struct spdk_nvmf_subsystem *subsystem, void *cb_arg, int status)
1294 {
1295 	struct subsystem_ns_change_ctx *ctx = cb_arg;
1296 
1297 	nvmf_subsystem_ns_changed(subsystem, ctx->nsid);
1298 	spdk_nvmf_subsystem_resume(subsystem, NULL, NULL);
1299 
1300 	free(ctx);
1301 }
1302 
1303 static void
1304 nvmf_ns_resize(void *event_ctx)
1305 {
1306 	struct spdk_nvmf_ns *ns = event_ctx;
1307 	struct subsystem_ns_change_ctx *ns_ctx;
1308 	int rc;
1309 
1310 	/* We have to allocate a new context because this op
1311 	 * is asynchronous and we could lose the ns in the middle.
1312 	 */
1313 	ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx));
1314 	if (!ns_ctx) {
1315 		SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n");
1316 		return;
1317 	}
1318 
1319 	ns_ctx->subsystem = ns->subsystem;
1320 	ns_ctx->nsid = ns->opts.nsid;
1321 	ns_ctx->cb_fn = _nvmf_ns_resize;
1322 
1323 	/* Specify 0 for the nsid here, because we do not need to pause the namespace.
1324 	 * Namespaces can only be resized bigger, so there is no need to quiesce I/O.
1325 	 */
1326 	rc = spdk_nvmf_subsystem_pause(ns->subsystem, 0, _nvmf_ns_resize, ns_ctx);
1327 	if (rc) {
1328 		if (rc == -EBUSY) {
1329 			/* Try again, this is not a permanent situation. */
1330 			spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx);
1331 		} else {
1332 			SPDK_ERRLOG("Unable to pause subsystem to process namespace resize!\n");
1333 			free(ns_ctx);
1334 		}
1335 	}
1336 }
1337 
1338 static void
1339 nvmf_ns_event(enum spdk_bdev_event_type type,
1340 	      struct spdk_bdev *bdev,
1341 	      void *event_ctx)
1342 {
1343 	SPDK_DEBUGLOG(nvmf, "Bdev event: type %d, name %s, subsystem_id %d, ns_id %d\n",
1344 		      type,
1345 		      spdk_bdev_get_name(bdev),
1346 		      ((struct spdk_nvmf_ns *)event_ctx)->subsystem->id,
1347 		      ((struct spdk_nvmf_ns *)event_ctx)->nsid);
1348 
1349 	switch (type) {
1350 	case SPDK_BDEV_EVENT_REMOVE:
1351 		nvmf_ns_hot_remove(event_ctx);
1352 		break;
1353 	case SPDK_BDEV_EVENT_RESIZE:
1354 		nvmf_ns_resize(event_ctx);
1355 		break;
1356 	default:
1357 		SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
1358 		break;
1359 	}
1360 }
1361 
1362 void
1363 spdk_nvmf_ns_opts_get_defaults(struct spdk_nvmf_ns_opts *opts, size_t opts_size)
1364 {
1365 	/* All current fields are set to 0 by default. */
1366 	memset(opts, 0, opts_size);
1367 }
1368 
1369 /* Dummy bdev module used to to claim bdevs. */
1370 static struct spdk_bdev_module ns_bdev_module = {
1371 	.name	= "NVMe-oF Target",
1372 };
1373 
1374 static int
1375 nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info);
1376 static int
1377 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info);
1378 
1379 uint32_t
1380 spdk_nvmf_subsystem_add_ns_ext(struct spdk_nvmf_subsystem *subsystem, const char *bdev_name,
1381 			       const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size,
1382 			       const char *ptpl_file)
1383 {
1384 	struct spdk_nvmf_transport *transport;
1385 	struct spdk_nvmf_ns_opts opts;
1386 	struct spdk_nvmf_ns *ns;
1387 	struct spdk_nvmf_reservation_info info = {0};
1388 	int rc;
1389 
1390 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1391 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1392 		return 0;
1393 	}
1394 
1395 	spdk_nvmf_ns_opts_get_defaults(&opts, sizeof(opts));
1396 	if (user_opts) {
1397 		memcpy(&opts, user_opts, spdk_min(sizeof(opts), opts_size));
1398 	}
1399 
1400 	if (opts.nsid == SPDK_NVME_GLOBAL_NS_TAG) {
1401 		SPDK_ERRLOG("Invalid NSID %" PRIu32 "\n", opts.nsid);
1402 		return 0;
1403 	}
1404 
1405 	if (opts.nsid == 0) {
1406 		/*
1407 		 * NSID not specified - find a free index.
1408 		 *
1409 		 * If no free slots are found, opts.nsid will be subsystem->max_nsid + 1, which will
1410 		 * expand max_nsid if possible.
1411 		 */
1412 		for (opts.nsid = 1; opts.nsid <= subsystem->max_nsid; opts.nsid++) {
1413 			if (_nvmf_subsystem_get_ns(subsystem, opts.nsid) == NULL) {
1414 				break;
1415 			}
1416 		}
1417 	}
1418 
1419 	if (_nvmf_subsystem_get_ns(subsystem, opts.nsid)) {
1420 		SPDK_ERRLOG("Requested NSID %" PRIu32 " already in use\n", opts.nsid);
1421 		return 0;
1422 	}
1423 
1424 	if (opts.nsid > subsystem->max_nsid) {
1425 		SPDK_ERRLOG("NSID greater than maximum not allowed\n");
1426 		return 0;
1427 	}
1428 
1429 	ns = calloc(1, sizeof(*ns));
1430 	if (ns == NULL) {
1431 		SPDK_ERRLOG("Namespace allocation failed\n");
1432 		return 0;
1433 	}
1434 
1435 	rc = spdk_bdev_open_ext(bdev_name, true, nvmf_ns_event, ns, &ns->desc);
1436 	if (rc != 0) {
1437 		SPDK_ERRLOG("Subsystem %s: bdev %s cannot be opened, error=%d\n",
1438 			    subsystem->subnqn, bdev_name, rc);
1439 		free(ns);
1440 		return 0;
1441 	}
1442 
1443 	ns->bdev = spdk_bdev_desc_get_bdev(ns->desc);
1444 
1445 	if (spdk_bdev_get_md_size(ns->bdev) != 0 && !spdk_bdev_is_md_interleaved(ns->bdev)) {
1446 		SPDK_ERRLOG("Can't attach bdev with separate metadata.\n");
1447 		spdk_bdev_close(ns->desc);
1448 		free(ns);
1449 		return 0;
1450 	}
1451 
1452 	rc = spdk_bdev_module_claim_bdev(ns->bdev, ns->desc, &ns_bdev_module);
1453 	if (rc != 0) {
1454 		spdk_bdev_close(ns->desc);
1455 		free(ns);
1456 		return 0;
1457 	}
1458 
1459 	if (spdk_mem_all_zero(&opts.uuid, sizeof(opts.uuid))) {
1460 		opts.uuid = *spdk_bdev_get_uuid(ns->bdev);
1461 	}
1462 
1463 	ns->opts = opts;
1464 	ns->subsystem = subsystem;
1465 	subsystem->ns[opts.nsid - 1] = ns;
1466 	ns->nsid = opts.nsid;
1467 	TAILQ_INIT(&ns->registrants);
1468 
1469 	if (ptpl_file) {
1470 		rc = nvmf_ns_load_reservation(ptpl_file, &info);
1471 		if (!rc) {
1472 			rc = nvmf_ns_reservation_restore(ns, &info);
1473 			if (rc) {
1474 				SPDK_ERRLOG("Subsystem restore reservation failed\n");
1475 				goto err_ns_reservation_restore;
1476 			}
1477 		}
1478 		ns->ptpl_file = strdup(ptpl_file);
1479 		if (!ns->ptpl_file) {
1480 			SPDK_ERRLOG("Namespace ns->ptpl_file allocation failed\n");
1481 			goto err_strdup;
1482 		}
1483 	}
1484 
1485 	for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1486 	     transport = spdk_nvmf_transport_get_next(transport)) {
1487 		if (transport->ops->subsystem_add_ns) {
1488 			rc = transport->ops->subsystem_add_ns(transport, subsystem, ns);
1489 			if (rc) {
1490 				SPDK_ERRLOG("Namespace attachment is not allowed by %s transport\n", transport->ops->name);
1491 				goto err_subsystem_add_ns;
1492 			}
1493 		}
1494 	}
1495 
1496 	SPDK_DEBUGLOG(nvmf, "Subsystem %s: bdev %s assigned nsid %" PRIu32 "\n",
1497 		      spdk_nvmf_subsystem_get_nqn(subsystem),
1498 		      bdev_name,
1499 		      opts.nsid);
1500 
1501 	nvmf_subsystem_ns_changed(subsystem, opts.nsid);
1502 
1503 	return opts.nsid;
1504 
1505 err_subsystem_add_ns:
1506 	free(ns->ptpl_file);
1507 err_strdup:
1508 	nvmf_ns_reservation_clear_all_registrants(ns);
1509 err_ns_reservation_restore:
1510 	subsystem->ns[opts.nsid - 1] = NULL;
1511 	spdk_bdev_module_release_bdev(ns->bdev);
1512 	spdk_bdev_close(ns->desc);
1513 	free(ns);
1514 	return 0;
1515 
1516 }
1517 
1518 static uint32_t
1519 nvmf_subsystem_get_next_allocated_nsid(struct spdk_nvmf_subsystem *subsystem,
1520 				       uint32_t prev_nsid)
1521 {
1522 	uint32_t nsid;
1523 
1524 	if (prev_nsid >= subsystem->max_nsid) {
1525 		return 0;
1526 	}
1527 
1528 	for (nsid = prev_nsid + 1; nsid <= subsystem->max_nsid; nsid++) {
1529 		if (subsystem->ns[nsid - 1]) {
1530 			return nsid;
1531 		}
1532 	}
1533 
1534 	return 0;
1535 }
1536 
1537 struct spdk_nvmf_ns *
1538 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem)
1539 {
1540 	uint32_t first_nsid;
1541 
1542 	first_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, 0);
1543 	return _nvmf_subsystem_get_ns(subsystem, first_nsid);
1544 }
1545 
1546 struct spdk_nvmf_ns *
1547 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem,
1548 				struct spdk_nvmf_ns *prev_ns)
1549 {
1550 	uint32_t next_nsid;
1551 
1552 	next_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, prev_ns->opts.nsid);
1553 	return _nvmf_subsystem_get_ns(subsystem, next_nsid);
1554 }
1555 
1556 struct spdk_nvmf_ns *
1557 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1558 {
1559 	return _nvmf_subsystem_get_ns(subsystem, nsid);
1560 }
1561 
1562 uint32_t
1563 spdk_nvmf_ns_get_id(const struct spdk_nvmf_ns *ns)
1564 {
1565 	return ns->opts.nsid;
1566 }
1567 
1568 struct spdk_bdev *
1569 spdk_nvmf_ns_get_bdev(struct spdk_nvmf_ns *ns)
1570 {
1571 	return ns->bdev;
1572 }
1573 
1574 void
1575 spdk_nvmf_ns_get_opts(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_ns_opts *opts,
1576 		      size_t opts_size)
1577 {
1578 	memset(opts, 0, opts_size);
1579 	memcpy(opts, &ns->opts, spdk_min(sizeof(ns->opts), opts_size));
1580 }
1581 
1582 const char *
1583 spdk_nvmf_subsystem_get_sn(const struct spdk_nvmf_subsystem *subsystem)
1584 {
1585 	return subsystem->sn;
1586 }
1587 
1588 int
1589 spdk_nvmf_subsystem_set_sn(struct spdk_nvmf_subsystem *subsystem, const char *sn)
1590 {
1591 	size_t len, max_len;
1592 
1593 	max_len = sizeof(subsystem->sn) - 1;
1594 	len = strlen(sn);
1595 	if (len > max_len) {
1596 		SPDK_DEBUGLOG(nvmf, "Invalid sn \"%s\": length %zu > max %zu\n",
1597 			      sn, len, max_len);
1598 		return -1;
1599 	}
1600 
1601 	if (!nvmf_valid_ascii_string(sn, len)) {
1602 		SPDK_DEBUGLOG(nvmf, "Non-ASCII sn\n");
1603 		SPDK_LOGDUMP(nvmf, "sn", sn, len);
1604 		return -1;
1605 	}
1606 
1607 	snprintf(subsystem->sn, sizeof(subsystem->sn), "%s", sn);
1608 
1609 	return 0;
1610 }
1611 
1612 const char *
1613 spdk_nvmf_subsystem_get_mn(const struct spdk_nvmf_subsystem *subsystem)
1614 {
1615 	return subsystem->mn;
1616 }
1617 
1618 int
1619 spdk_nvmf_subsystem_set_mn(struct spdk_nvmf_subsystem *subsystem, const char *mn)
1620 {
1621 	size_t len, max_len;
1622 
1623 	if (mn == NULL) {
1624 		mn = MODEL_NUMBER_DEFAULT;
1625 	}
1626 	max_len = sizeof(subsystem->mn) - 1;
1627 	len = strlen(mn);
1628 	if (len > max_len) {
1629 		SPDK_DEBUGLOG(nvmf, "Invalid mn \"%s\": length %zu > max %zu\n",
1630 			      mn, len, max_len);
1631 		return -1;
1632 	}
1633 
1634 	if (!nvmf_valid_ascii_string(mn, len)) {
1635 		SPDK_DEBUGLOG(nvmf, "Non-ASCII mn\n");
1636 		SPDK_LOGDUMP(nvmf, "mn", mn, len);
1637 		return -1;
1638 	}
1639 
1640 	snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", mn);
1641 
1642 	return 0;
1643 }
1644 
1645 const char *
1646 spdk_nvmf_subsystem_get_nqn(const struct spdk_nvmf_subsystem *subsystem)
1647 {
1648 	return subsystem->subnqn;
1649 }
1650 
1651 enum spdk_nvmf_subtype spdk_nvmf_subsystem_get_type(struct spdk_nvmf_subsystem *subsystem)
1652 {
1653 	return subsystem->subtype;
1654 }
1655 
1656 uint32_t
1657 spdk_nvmf_subsystem_get_max_nsid(struct spdk_nvmf_subsystem *subsystem)
1658 {
1659 	return subsystem->max_nsid;
1660 }
1661 
1662 int
1663 nvmf_subsystem_set_cntlid_range(struct spdk_nvmf_subsystem *subsystem,
1664 				uint16_t min_cntlid, uint16_t max_cntlid)
1665 {
1666 	if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
1667 		return -EAGAIN;
1668 	}
1669 
1670 	if (min_cntlid > max_cntlid) {
1671 		return -EINVAL;
1672 	}
1673 	/* The spec reserves cntlid values in the range FFF0h to FFFFh. */
1674 	if (min_cntlid < NVMF_MIN_CNTLID || min_cntlid > NVMF_MAX_CNTLID ||
1675 	    max_cntlid < NVMF_MIN_CNTLID || max_cntlid > NVMF_MAX_CNTLID) {
1676 		return -EINVAL;
1677 	}
1678 	subsystem->min_cntlid = min_cntlid;
1679 	subsystem->max_cntlid = max_cntlid;
1680 	if (subsystem->next_cntlid < min_cntlid || subsystem->next_cntlid > max_cntlid - 1) {
1681 		subsystem->next_cntlid = min_cntlid - 1;
1682 	}
1683 
1684 	return 0;
1685 }
1686 
1687 static uint16_t
1688 nvmf_subsystem_gen_cntlid(struct spdk_nvmf_subsystem *subsystem)
1689 {
1690 	int count;
1691 
1692 	/*
1693 	 * In the worst case, we might have to try all CNTLID values between min_cntlid and max_cntlid
1694 	 * before we find one that is unused (or find that all values are in use).
1695 	 */
1696 	for (count = 0; count < subsystem->max_cntlid - subsystem->min_cntlid + 1; count++) {
1697 		subsystem->next_cntlid++;
1698 		if (subsystem->next_cntlid > subsystem->max_cntlid) {
1699 			subsystem->next_cntlid = subsystem->min_cntlid;
1700 		}
1701 
1702 		/* Check if a controller with this cntlid currently exists. */
1703 		if (nvmf_subsystem_get_ctrlr(subsystem, subsystem->next_cntlid) == NULL) {
1704 			/* Found unused cntlid */
1705 			return subsystem->next_cntlid;
1706 		}
1707 	}
1708 
1709 	/* All valid cntlid values are in use. */
1710 	return 0xFFFF;
1711 }
1712 
1713 int
1714 nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr)
1715 {
1716 	ctrlr->cntlid = nvmf_subsystem_gen_cntlid(subsystem);
1717 	if (ctrlr->cntlid == 0xFFFF) {
1718 		/* Unable to get a cntlid */
1719 		SPDK_ERRLOG("Reached max simultaneous ctrlrs\n");
1720 		return -EBUSY;
1721 	}
1722 
1723 	TAILQ_INSERT_TAIL(&subsystem->ctrlrs, ctrlr, link);
1724 
1725 	return 0;
1726 }
1727 
1728 void
1729 nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem,
1730 			    struct spdk_nvmf_ctrlr *ctrlr)
1731 {
1732 	assert(subsystem == ctrlr->subsys);
1733 	TAILQ_REMOVE(&subsystem->ctrlrs, ctrlr, link);
1734 }
1735 
1736 struct spdk_nvmf_ctrlr *
1737 nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, uint16_t cntlid)
1738 {
1739 	struct spdk_nvmf_ctrlr *ctrlr;
1740 
1741 	TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1742 		if (ctrlr->cntlid == cntlid) {
1743 			return ctrlr;
1744 		}
1745 	}
1746 
1747 	return NULL;
1748 }
1749 
1750 uint32_t
1751 spdk_nvmf_subsystem_get_max_namespaces(const struct spdk_nvmf_subsystem *subsystem)
1752 {
1753 	return subsystem->max_nsid;
1754 }
1755 
1756 uint16_t
1757 spdk_nvmf_subsystem_get_min_cntlid(const struct spdk_nvmf_subsystem *subsystem)
1758 {
1759 	return subsystem->min_cntlid;
1760 }
1761 
1762 uint16_t
1763 spdk_nvmf_subsystem_get_max_cntlid(const struct spdk_nvmf_subsystem *subsystem)
1764 {
1765 	return subsystem->max_cntlid;
1766 }
1767 
1768 struct _nvmf_ns_registrant {
1769 	uint64_t		rkey;
1770 	char			*host_uuid;
1771 };
1772 
1773 struct _nvmf_ns_registrants {
1774 	size_t				num_regs;
1775 	struct _nvmf_ns_registrant	reg[SPDK_NVMF_MAX_NUM_REGISTRANTS];
1776 };
1777 
1778 struct _nvmf_ns_reservation {
1779 	bool					ptpl_activated;
1780 	enum spdk_nvme_reservation_type		rtype;
1781 	uint64_t				crkey;
1782 	char					*bdev_uuid;
1783 	char					*holder_uuid;
1784 	struct _nvmf_ns_registrants		regs;
1785 };
1786 
1787 static const struct spdk_json_object_decoder nvmf_ns_pr_reg_decoders[] = {
1788 	{"rkey", offsetof(struct _nvmf_ns_registrant, rkey), spdk_json_decode_uint64},
1789 	{"host_uuid", offsetof(struct _nvmf_ns_registrant, host_uuid), spdk_json_decode_string},
1790 };
1791 
1792 static int
1793 nvmf_decode_ns_pr_reg(const struct spdk_json_val *val, void *out)
1794 {
1795 	struct _nvmf_ns_registrant *reg = out;
1796 
1797 	return spdk_json_decode_object(val, nvmf_ns_pr_reg_decoders,
1798 				       SPDK_COUNTOF(nvmf_ns_pr_reg_decoders), reg);
1799 }
1800 
1801 static int
1802 nvmf_decode_ns_pr_regs(const struct spdk_json_val *val, void *out)
1803 {
1804 	struct _nvmf_ns_registrants *regs = out;
1805 
1806 	return spdk_json_decode_array(val, nvmf_decode_ns_pr_reg, regs->reg,
1807 				      SPDK_NVMF_MAX_NUM_REGISTRANTS, &regs->num_regs,
1808 				      sizeof(struct _nvmf_ns_registrant));
1809 }
1810 
1811 static const struct spdk_json_object_decoder nvmf_ns_pr_decoders[] = {
1812 	{"ptpl", offsetof(struct _nvmf_ns_reservation, ptpl_activated), spdk_json_decode_bool, true},
1813 	{"rtype", offsetof(struct _nvmf_ns_reservation, rtype), spdk_json_decode_uint32, true},
1814 	{"crkey", offsetof(struct _nvmf_ns_reservation, crkey), spdk_json_decode_uint64, true},
1815 	{"bdev_uuid", offsetof(struct _nvmf_ns_reservation, bdev_uuid), spdk_json_decode_string},
1816 	{"holder_uuid", offsetof(struct _nvmf_ns_reservation, holder_uuid), spdk_json_decode_string, true},
1817 	{"registrants", offsetof(struct _nvmf_ns_reservation, regs), nvmf_decode_ns_pr_regs},
1818 };
1819 
1820 static int
1821 nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info)
1822 {
1823 	FILE *fd;
1824 	size_t json_size;
1825 	ssize_t values_cnt, rc;
1826 	void *json = NULL, *end;
1827 	struct spdk_json_val *values = NULL;
1828 	struct _nvmf_ns_reservation res = {};
1829 	uint32_t i;
1830 
1831 	fd = fopen(file, "r");
1832 	/* It's not an error if the file does not exist */
1833 	if (!fd) {
1834 		SPDK_NOTICELOG("File %s does not exist\n", file);
1835 		return -ENOENT;
1836 	}
1837 
1838 	/* Load all persist file contents into a local buffer */
1839 	json = spdk_posix_file_load(fd, &json_size);
1840 	fclose(fd);
1841 	if (!json) {
1842 		SPDK_ERRLOG("Load persit file %s failed\n", file);
1843 		return -ENOMEM;
1844 	}
1845 
1846 	rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0);
1847 	if (rc < 0) {
1848 		SPDK_NOTICELOG("Parsing JSON configuration failed (%zd)\n", rc);
1849 		goto exit;
1850 	}
1851 
1852 	values_cnt = rc;
1853 	values = calloc(values_cnt, sizeof(struct spdk_json_val));
1854 	if (values == NULL) {
1855 		goto exit;
1856 	}
1857 
1858 	rc = spdk_json_parse(json, json_size, values, values_cnt, &end, 0);
1859 	if (rc != values_cnt) {
1860 		SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
1861 		goto exit;
1862 	}
1863 
1864 	/* Decode json */
1865 	if (spdk_json_decode_object(values, nvmf_ns_pr_decoders,
1866 				    SPDK_COUNTOF(nvmf_ns_pr_decoders),
1867 				    &res)) {
1868 		SPDK_ERRLOG("Invalid objects in the persist file %s\n", file);
1869 		rc = -EINVAL;
1870 		goto exit;
1871 	}
1872 
1873 	if (res.regs.num_regs > SPDK_NVMF_MAX_NUM_REGISTRANTS) {
1874 		SPDK_ERRLOG("Can only support up to %u registrants\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
1875 		rc = -ERANGE;
1876 		goto exit;
1877 	}
1878 
1879 	rc = 0;
1880 	info->ptpl_activated = res.ptpl_activated;
1881 	info->rtype = res.rtype;
1882 	info->crkey = res.crkey;
1883 	snprintf(info->bdev_uuid, sizeof(info->bdev_uuid), "%s", res.bdev_uuid);
1884 	snprintf(info->holder_uuid, sizeof(info->holder_uuid), "%s", res.holder_uuid);
1885 	info->num_regs = res.regs.num_regs;
1886 	for (i = 0; i < res.regs.num_regs; i++) {
1887 		info->registrants[i].rkey = res.regs.reg[i].rkey;
1888 		snprintf(info->registrants[i].host_uuid, sizeof(info->registrants[i].host_uuid), "%s",
1889 			 res.regs.reg[i].host_uuid);
1890 	}
1891 
1892 exit:
1893 	free(json);
1894 	free(values);
1895 	free(res.bdev_uuid);
1896 	free(res.holder_uuid);
1897 	for (i = 0; i < res.regs.num_regs; i++) {
1898 		free(res.regs.reg[i].host_uuid);
1899 	}
1900 
1901 	return rc;
1902 }
1903 
1904 static bool
1905 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns);
1906 
1907 static int
1908 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
1909 {
1910 	uint32_t i;
1911 	struct spdk_nvmf_registrant *reg, *holder = NULL;
1912 	struct spdk_uuid bdev_uuid, holder_uuid;
1913 
1914 	SPDK_DEBUGLOG(nvmf, "NSID %u, PTPL %u, Number of registrants %u\n",
1915 		      ns->nsid, info->ptpl_activated, info->num_regs);
1916 
1917 	/* it's not an error */
1918 	if (!info->ptpl_activated || !info->num_regs) {
1919 		return 0;
1920 	}
1921 
1922 	spdk_uuid_parse(&bdev_uuid, info->bdev_uuid);
1923 	if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) {
1924 		SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n");
1925 		return -EINVAL;
1926 	}
1927 
1928 	ns->crkey = info->crkey;
1929 	ns->rtype = info->rtype;
1930 	ns->ptpl_activated = info->ptpl_activated;
1931 	spdk_uuid_parse(&holder_uuid, info->holder_uuid);
1932 
1933 	SPDK_DEBUGLOG(nvmf, "Bdev UUID %s\n", info->bdev_uuid);
1934 	if (info->rtype) {
1935 		SPDK_DEBUGLOG(nvmf, "Holder UUID %s, RTYPE %u, RKEY 0x%"PRIx64"\n",
1936 			      info->holder_uuid, info->rtype, info->crkey);
1937 	}
1938 
1939 	for (i = 0; i < info->num_regs; i++) {
1940 		reg = calloc(1, sizeof(*reg));
1941 		if (!reg) {
1942 			return -ENOMEM;
1943 		}
1944 		spdk_uuid_parse(&reg->hostid, info->registrants[i].host_uuid);
1945 		reg->rkey = info->registrants[i].rkey;
1946 		TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
1947 		if (!spdk_uuid_compare(&holder_uuid, &reg->hostid)) {
1948 			holder = reg;
1949 		}
1950 		SPDK_DEBUGLOG(nvmf, "Registrant RKEY 0x%"PRIx64", Host UUID %s\n",
1951 			      info->registrants[i].rkey, info->registrants[i].host_uuid);
1952 	}
1953 
1954 	if (nvmf_ns_reservation_all_registrants_type(ns)) {
1955 		ns->holder = TAILQ_FIRST(&ns->registrants);
1956 	} else {
1957 		ns->holder = holder;
1958 	}
1959 
1960 	return 0;
1961 }
1962 
1963 static int
1964 nvmf_ns_json_write_cb(void *cb_ctx, const void *data, size_t size)
1965 {
1966 	char *file = cb_ctx;
1967 	size_t rc;
1968 	FILE *fd;
1969 
1970 	fd = fopen(file, "w");
1971 	if (!fd) {
1972 		SPDK_ERRLOG("Can't open file %s for write\n", file);
1973 		return -ENOENT;
1974 	}
1975 	rc = fwrite(data, 1, size, fd);
1976 	fclose(fd);
1977 
1978 	return rc == size ? 0 : -1;
1979 }
1980 
1981 static int
1982 nvmf_ns_reservation_update(const char *file, struct spdk_nvmf_reservation_info *info)
1983 {
1984 	struct spdk_json_write_ctx *w;
1985 	uint32_t i;
1986 	int rc = 0;
1987 
1988 	w = spdk_json_write_begin(nvmf_ns_json_write_cb, (void *)file, 0);
1989 	if (w == NULL) {
1990 		return -ENOMEM;
1991 	}
1992 	/* clear the configuration file */
1993 	if (!info->ptpl_activated) {
1994 		goto exit;
1995 	}
1996 
1997 	spdk_json_write_object_begin(w);
1998 	spdk_json_write_named_bool(w, "ptpl", info->ptpl_activated);
1999 	spdk_json_write_named_uint32(w, "rtype", info->rtype);
2000 	spdk_json_write_named_uint64(w, "crkey", info->crkey);
2001 	spdk_json_write_named_string(w, "bdev_uuid", info->bdev_uuid);
2002 	spdk_json_write_named_string(w, "holder_uuid", info->holder_uuid);
2003 
2004 	spdk_json_write_named_array_begin(w, "registrants");
2005 	for (i = 0; i < info->num_regs; i++) {
2006 		spdk_json_write_object_begin(w);
2007 		spdk_json_write_named_uint64(w, "rkey", info->registrants[i].rkey);
2008 		spdk_json_write_named_string(w, "host_uuid", info->registrants[i].host_uuid);
2009 		spdk_json_write_object_end(w);
2010 	}
2011 	spdk_json_write_array_end(w);
2012 	spdk_json_write_object_end(w);
2013 
2014 exit:
2015 	rc = spdk_json_write_end(w);
2016 	return rc;
2017 }
2018 
2019 static int
2020 nvmf_ns_update_reservation_info(struct spdk_nvmf_ns *ns)
2021 {
2022 	struct spdk_nvmf_reservation_info info;
2023 	struct spdk_nvmf_registrant *reg, *tmp;
2024 	uint32_t i = 0;
2025 
2026 	assert(ns != NULL);
2027 
2028 	if (!ns->bdev || !ns->ptpl_file) {
2029 		return 0;
2030 	}
2031 
2032 	memset(&info, 0, sizeof(info));
2033 	spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), spdk_bdev_get_uuid(ns->bdev));
2034 
2035 	if (ns->rtype) {
2036 		info.rtype = ns->rtype;
2037 		info.crkey = ns->crkey;
2038 		if (!nvmf_ns_reservation_all_registrants_type(ns)) {
2039 			assert(ns->holder != NULL);
2040 			spdk_uuid_fmt_lower(info.holder_uuid, sizeof(info.holder_uuid), &ns->holder->hostid);
2041 		}
2042 	}
2043 
2044 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2045 		spdk_uuid_fmt_lower(info.registrants[i].host_uuid, sizeof(info.registrants[i].host_uuid),
2046 				    &reg->hostid);
2047 		info.registrants[i++].rkey = reg->rkey;
2048 	}
2049 
2050 	info.num_regs = i;
2051 	info.ptpl_activated = ns->ptpl_activated;
2052 
2053 	return nvmf_ns_reservation_update(ns->ptpl_file, &info);
2054 }
2055 
2056 static struct spdk_nvmf_registrant *
2057 nvmf_ns_reservation_get_registrant(struct spdk_nvmf_ns *ns,
2058 				   struct spdk_uuid *uuid)
2059 {
2060 	struct spdk_nvmf_registrant *reg, *tmp;
2061 
2062 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2063 		if (!spdk_uuid_compare(&reg->hostid, uuid)) {
2064 			return reg;
2065 		}
2066 	}
2067 
2068 	return NULL;
2069 }
2070 
2071 /* Generate reservation notice log to registered HostID controllers */
2072 static void
2073 nvmf_subsystem_gen_ctrlr_notification(struct spdk_nvmf_subsystem *subsystem,
2074 				      struct spdk_nvmf_ns *ns,
2075 				      struct spdk_uuid *hostid_list,
2076 				      uint32_t num_hostid,
2077 				      enum spdk_nvme_reservation_notification_log_page_type type)
2078 {
2079 	struct spdk_nvmf_ctrlr *ctrlr;
2080 	uint32_t i;
2081 
2082 	for (i = 0; i < num_hostid; i++) {
2083 		TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
2084 			if (!spdk_uuid_compare(&ctrlr->hostid, &hostid_list[i])) {
2085 				nvmf_ctrlr_reservation_notice_log(ctrlr, ns, type);
2086 			}
2087 		}
2088 	}
2089 }
2090 
2091 /* Get all registrants' hostid other than the controller who issued the command */
2092 static uint32_t
2093 nvmf_ns_reservation_get_all_other_hostid(struct spdk_nvmf_ns *ns,
2094 		struct spdk_uuid *hostid_list,
2095 		uint32_t max_num_hostid,
2096 		struct spdk_uuid *current_hostid)
2097 {
2098 	struct spdk_nvmf_registrant *reg, *tmp;
2099 	uint32_t num_hostid = 0;
2100 
2101 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2102 		if (spdk_uuid_compare(&reg->hostid, current_hostid)) {
2103 			if (num_hostid == max_num_hostid) {
2104 				assert(false);
2105 				return max_num_hostid;
2106 			}
2107 			hostid_list[num_hostid++] = reg->hostid;
2108 		}
2109 	}
2110 
2111 	return num_hostid;
2112 }
2113 
2114 /* Calculate the unregistered HostID list according to list
2115  * prior to execute preempt command and list after executing
2116  * preempt command.
2117  */
2118 static uint32_t
2119 nvmf_ns_reservation_get_unregistered_hostid(struct spdk_uuid *old_hostid_list,
2120 		uint32_t old_num_hostid,
2121 		struct spdk_uuid *remaining_hostid_list,
2122 		uint32_t remaining_num_hostid)
2123 {
2124 	struct spdk_uuid temp_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2125 	uint32_t i, j, num_hostid = 0;
2126 	bool found;
2127 
2128 	if (!remaining_num_hostid) {
2129 		return old_num_hostid;
2130 	}
2131 
2132 	for (i = 0; i < old_num_hostid; i++) {
2133 		found = false;
2134 		for (j = 0; j < remaining_num_hostid; j++) {
2135 			if (!spdk_uuid_compare(&old_hostid_list[i], &remaining_hostid_list[j])) {
2136 				found = true;
2137 				break;
2138 			}
2139 		}
2140 		if (!found) {
2141 			spdk_uuid_copy(&temp_hostid_list[num_hostid++], &old_hostid_list[i]);
2142 		}
2143 	}
2144 
2145 	if (num_hostid) {
2146 		memcpy(old_hostid_list, temp_hostid_list, sizeof(struct spdk_uuid) * num_hostid);
2147 	}
2148 
2149 	return num_hostid;
2150 }
2151 
2152 /* current reservation type is all registrants or not */
2153 static bool
2154 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns)
2155 {
2156 	return (ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
2157 		ns->rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS);
2158 }
2159 
2160 /* current registrant is reservation holder or not */
2161 static bool
2162 nvmf_ns_reservation_registrant_is_holder(struct spdk_nvmf_ns *ns,
2163 		struct spdk_nvmf_registrant *reg)
2164 {
2165 	if (!reg) {
2166 		return false;
2167 	}
2168 
2169 	if (nvmf_ns_reservation_all_registrants_type(ns)) {
2170 		return true;
2171 	}
2172 
2173 	return (ns->holder == reg);
2174 }
2175 
2176 static int
2177 nvmf_ns_reservation_add_registrant(struct spdk_nvmf_ns *ns,
2178 				   struct spdk_nvmf_ctrlr *ctrlr,
2179 				   uint64_t nrkey)
2180 {
2181 	struct spdk_nvmf_registrant *reg;
2182 
2183 	reg = calloc(1, sizeof(*reg));
2184 	if (!reg) {
2185 		return -ENOMEM;
2186 	}
2187 
2188 	reg->rkey = nrkey;
2189 	/* set hostid for the registrant */
2190 	spdk_uuid_copy(&reg->hostid, &ctrlr->hostid);
2191 	TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
2192 	ns->gen++;
2193 
2194 	return 0;
2195 }
2196 
2197 static void
2198 nvmf_ns_reservation_release_reservation(struct spdk_nvmf_ns *ns)
2199 {
2200 	ns->rtype = 0;
2201 	ns->crkey = 0;
2202 	ns->holder = NULL;
2203 }
2204 
2205 /* release the reservation if the last registrant was removed */
2206 static void
2207 nvmf_ns_reservation_check_release_on_remove_registrant(struct spdk_nvmf_ns *ns,
2208 		struct spdk_nvmf_registrant *reg)
2209 {
2210 	struct spdk_nvmf_registrant *next_reg;
2211 
2212 	/* no reservation holder */
2213 	if (!ns->holder) {
2214 		assert(ns->rtype == 0);
2215 		return;
2216 	}
2217 
2218 	next_reg = TAILQ_FIRST(&ns->registrants);
2219 	if (next_reg && nvmf_ns_reservation_all_registrants_type(ns)) {
2220 		/* the next valid registrant is the new holder now */
2221 		ns->holder = next_reg;
2222 	} else if (nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
2223 		/* release the reservation */
2224 		nvmf_ns_reservation_release_reservation(ns);
2225 	}
2226 }
2227 
2228 static void
2229 nvmf_ns_reservation_remove_registrant(struct spdk_nvmf_ns *ns,
2230 				      struct spdk_nvmf_registrant *reg)
2231 {
2232 	TAILQ_REMOVE(&ns->registrants, reg, link);
2233 	nvmf_ns_reservation_check_release_on_remove_registrant(ns, reg);
2234 	free(reg);
2235 	ns->gen++;
2236 	return;
2237 }
2238 
2239 static uint32_t
2240 nvmf_ns_reservation_remove_registrants_by_key(struct spdk_nvmf_ns *ns,
2241 		uint64_t rkey)
2242 {
2243 	struct spdk_nvmf_registrant *reg, *tmp;
2244 	uint32_t count = 0;
2245 
2246 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2247 		if (reg->rkey == rkey) {
2248 			nvmf_ns_reservation_remove_registrant(ns, reg);
2249 			count++;
2250 		}
2251 	}
2252 	return count;
2253 }
2254 
2255 static uint32_t
2256 nvmf_ns_reservation_remove_all_other_registrants(struct spdk_nvmf_ns *ns,
2257 		struct spdk_nvmf_registrant *reg)
2258 {
2259 	struct spdk_nvmf_registrant *reg_tmp, *reg_tmp2;
2260 	uint32_t count = 0;
2261 
2262 	TAILQ_FOREACH_SAFE(reg_tmp, &ns->registrants, link, reg_tmp2) {
2263 		if (reg_tmp != reg) {
2264 			nvmf_ns_reservation_remove_registrant(ns, reg_tmp);
2265 			count++;
2266 		}
2267 	}
2268 	return count;
2269 }
2270 
2271 static uint32_t
2272 nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns)
2273 {
2274 	struct spdk_nvmf_registrant *reg, *reg_tmp;
2275 	uint32_t count = 0;
2276 
2277 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) {
2278 		nvmf_ns_reservation_remove_registrant(ns, reg);
2279 		count++;
2280 	}
2281 	return count;
2282 }
2283 
2284 static void
2285 nvmf_ns_reservation_acquire_reservation(struct spdk_nvmf_ns *ns, uint64_t rkey,
2286 					enum spdk_nvme_reservation_type rtype,
2287 					struct spdk_nvmf_registrant *holder)
2288 {
2289 	ns->rtype = rtype;
2290 	ns->crkey = rkey;
2291 	assert(ns->holder == NULL);
2292 	ns->holder = holder;
2293 }
2294 
2295 static bool
2296 nvmf_ns_reservation_register(struct spdk_nvmf_ns *ns,
2297 			     struct spdk_nvmf_ctrlr *ctrlr,
2298 			     struct spdk_nvmf_request *req)
2299 {
2300 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2301 	uint8_t rrega, iekey, cptpl, rtype;
2302 	struct spdk_nvme_reservation_register_data key;
2303 	struct spdk_nvmf_registrant *reg;
2304 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2305 	bool update_sgroup = false;
2306 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2307 	uint32_t num_hostid = 0;
2308 	int rc;
2309 
2310 	rrega = cmd->cdw10_bits.resv_register.rrega;
2311 	iekey = cmd->cdw10_bits.resv_register.iekey;
2312 	cptpl = cmd->cdw10_bits.resv_register.cptpl;
2313 
2314 	if (req->data && req->length >= sizeof(key)) {
2315 		memcpy(&key, req->data, sizeof(key));
2316 	} else {
2317 		SPDK_ERRLOG("No key provided. Failing request.\n");
2318 		status = SPDK_NVME_SC_INVALID_FIELD;
2319 		goto exit;
2320 	}
2321 
2322 	SPDK_DEBUGLOG(nvmf, "REGISTER: RREGA %u, IEKEY %u, CPTPL %u, "
2323 		      "NRKEY 0x%"PRIx64", NRKEY 0x%"PRIx64"\n",
2324 		      rrega, iekey, cptpl, key.crkey, key.nrkey);
2325 
2326 	if (cptpl == SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON) {
2327 		/* Ture to OFF state, and need to be updated in the configuration file */
2328 		if (ns->ptpl_activated) {
2329 			ns->ptpl_activated = 0;
2330 			update_sgroup = true;
2331 		}
2332 	} else if (cptpl == SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS) {
2333 		if (ns->ptpl_file == NULL) {
2334 			status = SPDK_NVME_SC_INVALID_FIELD;
2335 			goto exit;
2336 		} else if (ns->ptpl_activated == 0) {
2337 			ns->ptpl_activated = 1;
2338 			update_sgroup = true;
2339 		}
2340 	}
2341 
2342 	/* current Host Identifier has registrant or not */
2343 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2344 
2345 	switch (rrega) {
2346 	case SPDK_NVME_RESERVE_REGISTER_KEY:
2347 		if (!reg) {
2348 			/* register new controller */
2349 			if (key.nrkey == 0) {
2350 				SPDK_ERRLOG("Can't register zeroed new key\n");
2351 				status = SPDK_NVME_SC_INVALID_FIELD;
2352 				goto exit;
2353 			}
2354 			rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey);
2355 			if (rc < 0) {
2356 				status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2357 				goto exit;
2358 			}
2359 			update_sgroup = true;
2360 		} else {
2361 			/* register with same key is not an error */
2362 			if (reg->rkey != key.nrkey) {
2363 				SPDK_ERRLOG("The same host already register a "
2364 					    "key with 0x%"PRIx64"\n",
2365 					    reg->rkey);
2366 				status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2367 				goto exit;
2368 			}
2369 		}
2370 		break;
2371 	case SPDK_NVME_RESERVE_UNREGISTER_KEY:
2372 		if (!reg || (!iekey && reg->rkey != key.crkey)) {
2373 			SPDK_ERRLOG("No registrant or current key doesn't match "
2374 				    "with existing registrant key\n");
2375 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2376 			goto exit;
2377 		}
2378 
2379 		rtype = ns->rtype;
2380 		num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2381 				SPDK_NVMF_MAX_NUM_REGISTRANTS,
2382 				&ctrlr->hostid);
2383 
2384 		nvmf_ns_reservation_remove_registrant(ns, reg);
2385 
2386 		if (!ns->rtype && num_hostid && (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY ||
2387 						 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY)) {
2388 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2389 							      hostid_list,
2390 							      num_hostid,
2391 							      SPDK_NVME_RESERVATION_RELEASED);
2392 		}
2393 		update_sgroup = true;
2394 		break;
2395 	case SPDK_NVME_RESERVE_REPLACE_KEY:
2396 		if (!reg || (!iekey && reg->rkey != key.crkey)) {
2397 			SPDK_ERRLOG("No registrant or current key doesn't match "
2398 				    "with existing registrant key\n");
2399 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2400 			goto exit;
2401 		}
2402 		if (key.nrkey == 0) {
2403 			SPDK_ERRLOG("Can't register zeroed new key\n");
2404 			status = SPDK_NVME_SC_INVALID_FIELD;
2405 			goto exit;
2406 		}
2407 		reg->rkey = key.nrkey;
2408 		update_sgroup = true;
2409 		break;
2410 	default:
2411 		status = SPDK_NVME_SC_INVALID_FIELD;
2412 		goto exit;
2413 	}
2414 
2415 exit:
2416 	if (update_sgroup) {
2417 		rc = nvmf_ns_update_reservation_info(ns);
2418 		if (rc != 0) {
2419 			status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2420 		}
2421 	}
2422 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2423 	req->rsp->nvme_cpl.status.sc = status;
2424 	return update_sgroup;
2425 }
2426 
2427 static bool
2428 nvmf_ns_reservation_acquire(struct spdk_nvmf_ns *ns,
2429 			    struct spdk_nvmf_ctrlr *ctrlr,
2430 			    struct spdk_nvmf_request *req)
2431 {
2432 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2433 	uint8_t racqa, iekey, rtype;
2434 	struct spdk_nvme_reservation_acquire_data key;
2435 	struct spdk_nvmf_registrant *reg;
2436 	bool all_regs = false;
2437 	uint32_t count = 0;
2438 	bool update_sgroup = true;
2439 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2440 	uint32_t num_hostid = 0;
2441 	struct spdk_uuid new_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2442 	uint32_t new_num_hostid = 0;
2443 	bool reservation_released = false;
2444 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2445 
2446 	racqa = cmd->cdw10_bits.resv_acquire.racqa;
2447 	iekey = cmd->cdw10_bits.resv_acquire.iekey;
2448 	rtype = cmd->cdw10_bits.resv_acquire.rtype;
2449 
2450 	if (req->data && req->length >= sizeof(key)) {
2451 		memcpy(&key, req->data, sizeof(key));
2452 	} else {
2453 		SPDK_ERRLOG("No key provided. Failing request.\n");
2454 		status = SPDK_NVME_SC_INVALID_FIELD;
2455 		goto exit;
2456 	}
2457 
2458 	SPDK_DEBUGLOG(nvmf, "ACQUIRE: RACQA %u, IEKEY %u, RTYPE %u, "
2459 		      "NRKEY 0x%"PRIx64", PRKEY 0x%"PRIx64"\n",
2460 		      racqa, iekey, rtype, key.crkey, key.prkey);
2461 
2462 	if (iekey || rtype > SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) {
2463 		SPDK_ERRLOG("Ignore existing key field set to 1\n");
2464 		status = SPDK_NVME_SC_INVALID_FIELD;
2465 		update_sgroup = false;
2466 		goto exit;
2467 	}
2468 
2469 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2470 	/* must be registrant and CRKEY must match */
2471 	if (!reg || reg->rkey != key.crkey) {
2472 		SPDK_ERRLOG("No registrant or current key doesn't match "
2473 			    "with existing registrant key\n");
2474 		status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2475 		update_sgroup = false;
2476 		goto exit;
2477 	}
2478 
2479 	all_regs = nvmf_ns_reservation_all_registrants_type(ns);
2480 
2481 	switch (racqa) {
2482 	case SPDK_NVME_RESERVE_ACQUIRE:
2483 		/* it's not an error for the holder to acquire same reservation type again */
2484 		if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && ns->rtype == rtype) {
2485 			/* do nothing */
2486 			update_sgroup = false;
2487 		} else if (ns->holder == NULL) {
2488 			/* fisrt time to acquire the reservation */
2489 			nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
2490 		} else {
2491 			SPDK_ERRLOG("Invalid rtype or current registrant is not holder\n");
2492 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2493 			update_sgroup = false;
2494 			goto exit;
2495 		}
2496 		break;
2497 	case SPDK_NVME_RESERVE_PREEMPT:
2498 		/* no reservation holder */
2499 		if (!ns->holder) {
2500 			/* unregister with PRKEY */
2501 			nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2502 			break;
2503 		}
2504 		num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2505 				SPDK_NVMF_MAX_NUM_REGISTRANTS,
2506 				&ctrlr->hostid);
2507 
2508 		/* only 1 reservation holder and reservation key is valid */
2509 		if (!all_regs) {
2510 			/* preempt itself */
2511 			if (nvmf_ns_reservation_registrant_is_holder(ns, reg) &&
2512 			    ns->crkey == key.prkey) {
2513 				ns->rtype = rtype;
2514 				reservation_released = true;
2515 				break;
2516 			}
2517 
2518 			if (ns->crkey == key.prkey) {
2519 				nvmf_ns_reservation_remove_registrant(ns, ns->holder);
2520 				nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
2521 				reservation_released = true;
2522 			} else if (key.prkey != 0) {
2523 				nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2524 			} else {
2525 				/* PRKEY is zero */
2526 				SPDK_ERRLOG("Current PRKEY is zero\n");
2527 				status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2528 				update_sgroup = false;
2529 				goto exit;
2530 			}
2531 		} else {
2532 			/* release all other registrants except for the current one */
2533 			if (key.prkey == 0) {
2534 				nvmf_ns_reservation_remove_all_other_registrants(ns, reg);
2535 				assert(ns->holder == reg);
2536 			} else {
2537 				count = nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2538 				if (count == 0) {
2539 					SPDK_ERRLOG("PRKEY doesn't match any registrant\n");
2540 					status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2541 					update_sgroup = false;
2542 					goto exit;
2543 				}
2544 			}
2545 		}
2546 		break;
2547 	default:
2548 		status = SPDK_NVME_SC_INVALID_FIELD;
2549 		update_sgroup = false;
2550 		break;
2551 	}
2552 
2553 exit:
2554 	if (update_sgroup && racqa == SPDK_NVME_RESERVE_PREEMPT) {
2555 		new_num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, new_hostid_list,
2556 				 SPDK_NVMF_MAX_NUM_REGISTRANTS,
2557 				 &ctrlr->hostid);
2558 		/* Preempt notification occurs on the unregistered controllers
2559 		 * other than the controller who issued the command.
2560 		 */
2561 		num_hostid = nvmf_ns_reservation_get_unregistered_hostid(hostid_list,
2562 				num_hostid,
2563 				new_hostid_list,
2564 				new_num_hostid);
2565 		if (num_hostid) {
2566 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2567 							      hostid_list,
2568 							      num_hostid,
2569 							      SPDK_NVME_REGISTRATION_PREEMPTED);
2570 
2571 		}
2572 		/* Reservation released notification occurs on the
2573 		 * controllers which are the remaining registrants other than
2574 		 * the controller who issued the command.
2575 		 */
2576 		if (reservation_released && new_num_hostid) {
2577 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2578 							      new_hostid_list,
2579 							      new_num_hostid,
2580 							      SPDK_NVME_RESERVATION_RELEASED);
2581 
2582 		}
2583 	}
2584 	if (update_sgroup && ns->ptpl_activated) {
2585 		if (nvmf_ns_update_reservation_info(ns)) {
2586 			status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2587 		}
2588 	}
2589 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2590 	req->rsp->nvme_cpl.status.sc = status;
2591 	return update_sgroup;
2592 }
2593 
2594 static bool
2595 nvmf_ns_reservation_release(struct spdk_nvmf_ns *ns,
2596 			    struct spdk_nvmf_ctrlr *ctrlr,
2597 			    struct spdk_nvmf_request *req)
2598 {
2599 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2600 	uint8_t rrela, iekey, rtype;
2601 	struct spdk_nvmf_registrant *reg;
2602 	uint64_t crkey;
2603 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2604 	bool update_sgroup = true;
2605 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2606 	uint32_t num_hostid = 0;
2607 
2608 	rrela = cmd->cdw10_bits.resv_release.rrela;
2609 	iekey = cmd->cdw10_bits.resv_release.iekey;
2610 	rtype = cmd->cdw10_bits.resv_release.rtype;
2611 
2612 	if (req->data && req->length >= sizeof(crkey)) {
2613 		memcpy(&crkey, req->data, sizeof(crkey));
2614 	} else {
2615 		SPDK_ERRLOG("No key provided. Failing request.\n");
2616 		status = SPDK_NVME_SC_INVALID_FIELD;
2617 		goto exit;
2618 	}
2619 
2620 	SPDK_DEBUGLOG(nvmf, "RELEASE: RRELA %u, IEKEY %u, RTYPE %u, "
2621 		      "CRKEY 0x%"PRIx64"\n",  rrela, iekey, rtype, crkey);
2622 
2623 	if (iekey) {
2624 		SPDK_ERRLOG("Ignore existing key field set to 1\n");
2625 		status = SPDK_NVME_SC_INVALID_FIELD;
2626 		update_sgroup = false;
2627 		goto exit;
2628 	}
2629 
2630 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2631 	if (!reg || reg->rkey != crkey) {
2632 		SPDK_ERRLOG("No registrant or current key doesn't match "
2633 			    "with existing registrant key\n");
2634 		status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2635 		update_sgroup = false;
2636 		goto exit;
2637 	}
2638 
2639 	num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2640 			SPDK_NVMF_MAX_NUM_REGISTRANTS,
2641 			&ctrlr->hostid);
2642 
2643 	switch (rrela) {
2644 	case SPDK_NVME_RESERVE_RELEASE:
2645 		if (!ns->holder) {
2646 			SPDK_DEBUGLOG(nvmf, "RELEASE: no holder\n");
2647 			update_sgroup = false;
2648 			goto exit;
2649 		}
2650 		if (ns->rtype != rtype) {
2651 			SPDK_ERRLOG("Type doesn't match\n");
2652 			status = SPDK_NVME_SC_INVALID_FIELD;
2653 			update_sgroup = false;
2654 			goto exit;
2655 		}
2656 		if (!nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
2657 			/* not the reservation holder, this isn't an error */
2658 			update_sgroup = false;
2659 			goto exit;
2660 		}
2661 
2662 		rtype = ns->rtype;
2663 		nvmf_ns_reservation_release_reservation(ns);
2664 
2665 		if (num_hostid && rtype != SPDK_NVME_RESERVE_WRITE_EXCLUSIVE &&
2666 		    rtype != SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2667 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2668 							      hostid_list,
2669 							      num_hostid,
2670 							      SPDK_NVME_RESERVATION_RELEASED);
2671 		}
2672 		break;
2673 	case SPDK_NVME_RESERVE_CLEAR:
2674 		nvmf_ns_reservation_clear_all_registrants(ns);
2675 		if (num_hostid) {
2676 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2677 							      hostid_list,
2678 							      num_hostid,
2679 							      SPDK_NVME_RESERVATION_PREEMPTED);
2680 		}
2681 		break;
2682 	default:
2683 		status = SPDK_NVME_SC_INVALID_FIELD;
2684 		update_sgroup = false;
2685 		goto exit;
2686 	}
2687 
2688 exit:
2689 	if (update_sgroup && ns->ptpl_activated) {
2690 		if (nvmf_ns_update_reservation_info(ns)) {
2691 			status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2692 		}
2693 	}
2694 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2695 	req->rsp->nvme_cpl.status.sc = status;
2696 	return update_sgroup;
2697 }
2698 
2699 static void
2700 nvmf_ns_reservation_report(struct spdk_nvmf_ns *ns,
2701 			   struct spdk_nvmf_ctrlr *ctrlr,
2702 			   struct spdk_nvmf_request *req)
2703 {
2704 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2705 	struct spdk_nvmf_registrant *reg, *tmp;
2706 	struct spdk_nvme_reservation_status_extended_data *status_data;
2707 	struct spdk_nvme_registered_ctrlr_extended_data *ctrlr_data;
2708 	uint8_t *payload;
2709 	uint32_t transfer_len, payload_len = 0;
2710 	uint32_t regctl = 0;
2711 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2712 
2713 	if (req->data == NULL) {
2714 		SPDK_ERRLOG("No data transfer specified for request. "
2715 			    " Unable to transfer back response.\n");
2716 		status = SPDK_NVME_SC_INVALID_FIELD;
2717 		goto exit;
2718 	}
2719 
2720 	if (!cmd->cdw11_bits.resv_report.eds) {
2721 		SPDK_ERRLOG("NVMeoF uses extended controller data structure, "
2722 			    "please set EDS bit in cdw11 and try again\n");
2723 		status = SPDK_NVME_SC_HOSTID_INCONSISTENT_FORMAT;
2724 		goto exit;
2725 	}
2726 
2727 	/* Number of Dwords of the Reservation Status data structure to transfer */
2728 	transfer_len = (cmd->cdw10 + 1) * sizeof(uint32_t);
2729 	payload = req->data;
2730 
2731 	if (transfer_len < sizeof(struct spdk_nvme_reservation_status_extended_data)) {
2732 		status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2733 		goto exit;
2734 	}
2735 
2736 	status_data = (struct spdk_nvme_reservation_status_extended_data *)payload;
2737 	status_data->data.gen = ns->gen;
2738 	status_data->data.rtype = ns->rtype;
2739 	status_data->data.ptpls = ns->ptpl_activated;
2740 	payload_len += sizeof(struct spdk_nvme_reservation_status_extended_data);
2741 
2742 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2743 		payload_len += sizeof(struct spdk_nvme_registered_ctrlr_extended_data);
2744 		if (payload_len > transfer_len) {
2745 			break;
2746 		}
2747 
2748 		ctrlr_data = (struct spdk_nvme_registered_ctrlr_extended_data *)
2749 			     (payload + sizeof(*status_data) + sizeof(*ctrlr_data) * regctl);
2750 		/* Set to 0xffffh for dynamic controller */
2751 		ctrlr_data->cntlid = 0xffff;
2752 		ctrlr_data->rcsts.status = (ns->holder == reg) ? true : false;
2753 		ctrlr_data->rkey = reg->rkey;
2754 		spdk_uuid_copy((struct spdk_uuid *)ctrlr_data->hostid, &reg->hostid);
2755 		regctl++;
2756 	}
2757 	status_data->data.regctl = regctl;
2758 
2759 exit:
2760 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2761 	req->rsp->nvme_cpl.status.sc = status;
2762 	return;
2763 }
2764 
2765 static void
2766 nvmf_ns_reservation_complete(void *ctx)
2767 {
2768 	struct spdk_nvmf_request *req = ctx;
2769 
2770 	spdk_nvmf_request_complete(req);
2771 }
2772 
2773 static void
2774 _nvmf_ns_reservation_update_done(struct spdk_nvmf_subsystem *subsystem,
2775 				 void *cb_arg, int status)
2776 {
2777 	struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)cb_arg;
2778 	struct spdk_nvmf_poll_group *group = req->qpair->group;
2779 
2780 	spdk_thread_send_msg(group->thread, nvmf_ns_reservation_complete, req);
2781 }
2782 
2783 void
2784 nvmf_ns_reservation_request(void *ctx)
2785 {
2786 	struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)ctx;
2787 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2788 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2789 	struct subsystem_update_ns_ctx *update_ctx;
2790 	uint32_t nsid;
2791 	struct spdk_nvmf_ns *ns;
2792 	bool update_sgroup = false;
2793 
2794 	nsid = cmd->nsid;
2795 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
2796 	assert(ns != NULL);
2797 
2798 	switch (cmd->opc) {
2799 	case SPDK_NVME_OPC_RESERVATION_REGISTER:
2800 		update_sgroup = nvmf_ns_reservation_register(ns, ctrlr, req);
2801 		break;
2802 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2803 		update_sgroup = nvmf_ns_reservation_acquire(ns, ctrlr, req);
2804 		break;
2805 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2806 		update_sgroup = nvmf_ns_reservation_release(ns, ctrlr, req);
2807 		break;
2808 	case SPDK_NVME_OPC_RESERVATION_REPORT:
2809 		nvmf_ns_reservation_report(ns, ctrlr, req);
2810 		break;
2811 	default:
2812 		break;
2813 	}
2814 
2815 	/* update reservation information to subsystem's poll group */
2816 	if (update_sgroup) {
2817 		update_ctx = calloc(1, sizeof(*update_ctx));
2818 		if (update_ctx == NULL) {
2819 			SPDK_ERRLOG("Can't alloc subsystem poll group update context\n");
2820 			goto update_done;
2821 		}
2822 		update_ctx->subsystem = ctrlr->subsys;
2823 		update_ctx->cb_fn = _nvmf_ns_reservation_update_done;
2824 		update_ctx->cb_arg = req;
2825 
2826 		nvmf_subsystem_update_ns(ctrlr->subsys, subsystem_update_ns_done, update_ctx);
2827 		return;
2828 	}
2829 
2830 update_done:
2831 	_nvmf_ns_reservation_update_done(ctrlr->subsys, (void *)req, 0);
2832 }
2833 
2834 int
2835 spdk_nvmf_subsystem_set_ana_reporting(struct spdk_nvmf_subsystem *subsystem,
2836 				      bool ana_reporting)
2837 {
2838 	if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
2839 		return -EAGAIN;
2840 	}
2841 
2842 	subsystem->flags.ana_reporting = ana_reporting;
2843 
2844 	return 0;
2845 }
2846 
2847 struct subsystem_listener_update_ctx {
2848 	struct spdk_nvmf_subsystem_listener *listener;
2849 
2850 	spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn;
2851 	void *cb_arg;
2852 };
2853 
2854 static void
2855 subsystem_listener_update_done(struct spdk_io_channel_iter *i, int status)
2856 {
2857 	struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2858 
2859 	if (ctx->cb_fn) {
2860 		ctx->cb_fn(ctx->cb_arg, status);
2861 	}
2862 	free(ctx);
2863 }
2864 
2865 static void
2866 subsystem_listener_update_on_pg(struct spdk_io_channel_iter *i)
2867 {
2868 	struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2869 	struct spdk_nvmf_subsystem_listener *listener;
2870 	struct spdk_nvmf_poll_group *group;
2871 	struct spdk_nvmf_ctrlr *ctrlr;
2872 
2873 	listener = ctx->listener;
2874 	group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
2875 
2876 	TAILQ_FOREACH(ctrlr, &listener->subsystem->ctrlrs, link) {
2877 		if (ctrlr->admin_qpair->group == group && ctrlr->listener == listener) {
2878 			nvmf_ctrlr_async_event_ana_change_notice(ctrlr);
2879 		}
2880 	}
2881 
2882 	spdk_for_each_channel_continue(i, 0);
2883 }
2884 
2885 void
2886 nvmf_subsystem_set_ana_state(struct spdk_nvmf_subsystem *subsystem,
2887 			     const struct spdk_nvme_transport_id *trid,
2888 			     enum spdk_nvme_ana_state ana_state,
2889 			     spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, void *cb_arg)
2890 {
2891 	struct spdk_nvmf_subsystem_listener *listener;
2892 	struct subsystem_listener_update_ctx *ctx;
2893 
2894 	assert(cb_fn != NULL);
2895 	assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
2896 	       subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED);
2897 
2898 	if (!subsystem->flags.ana_reporting) {
2899 		SPDK_ERRLOG("ANA reporting is disabled\n");
2900 		cb_fn(cb_arg, -EINVAL);
2901 		return;
2902 	}
2903 
2904 	/* ANA Change state is not used, ANA Persistent Loss state
2905 	 * is not supported yet.
2906 	 */
2907 	if (!(ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE ||
2908 	      ana_state == SPDK_NVME_ANA_NON_OPTIMIZED_STATE ||
2909 	      ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE)) {
2910 		SPDK_ERRLOG("ANA state %d is not supported\n", ana_state);
2911 		cb_fn(cb_arg, -ENOTSUP);
2912 		return;
2913 	}
2914 
2915 	listener = nvmf_subsystem_find_listener(subsystem, trid);
2916 	if (!listener) {
2917 		SPDK_ERRLOG("Unable to find listener.\n");
2918 		cb_fn(cb_arg, -EINVAL);
2919 		return;
2920 	}
2921 
2922 	if (listener->ana_state == ana_state) {
2923 		cb_fn(cb_arg, 0);
2924 		return;
2925 	}
2926 
2927 	ctx = calloc(1, sizeof(*ctx));
2928 	if (!ctx) {
2929 		SPDK_ERRLOG("Unable to allocate context\n");
2930 		cb_fn(cb_arg, -ENOMEM);
2931 		return;
2932 	}
2933 
2934 	listener->ana_state = ana_state;
2935 	listener->ana_state_change_count++;
2936 
2937 	ctx->listener = listener;
2938 	ctx->cb_fn = cb_fn;
2939 	ctx->cb_arg = cb_arg;
2940 
2941 	spdk_for_each_channel(subsystem->tgt,
2942 			      subsystem_listener_update_on_pg,
2943 			      ctx,
2944 			      subsystem_listener_update_done);
2945 }
2946