xref: /spdk/lib/nvmf/subsystem.c (revision 628c230de4780617ec0432bd2427aa91ca837ba6)
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->subsystem = subsystem;
852 	ctx->hostnqn = strdup(hostnqn);
853 	ctx->cb_fn = cb_fn;
854 	ctx->cb_arg = cb_arg;
855 
856 	spdk_for_each_channel(subsystem->tgt, nvmf_subsystem_disconnect_qpairs_by_host, ctx,
857 			      nvmf_subsystem_disconnect_host_fini);
858 
859 	return 0;
860 }
861 
862 int
863 spdk_nvmf_subsystem_set_allow_any_host(struct spdk_nvmf_subsystem *subsystem, bool allow_any_host)
864 {
865 	pthread_mutex_lock(&subsystem->mutex);
866 	subsystem->flags.allow_any_host = allow_any_host;
867 	nvmf_update_discovery_log(subsystem->tgt, NULL);
868 	pthread_mutex_unlock(&subsystem->mutex);
869 
870 	return 0;
871 }
872 
873 bool
874 spdk_nvmf_subsystem_get_allow_any_host(const struct spdk_nvmf_subsystem *subsystem)
875 {
876 	bool allow_any_host;
877 	struct spdk_nvmf_subsystem *sub;
878 
879 	/* Technically, taking the mutex modifies data in the subsystem. But the const
880 	 * is still important to convey that this doesn't mutate any other data. Cast
881 	 * it away to work around this. */
882 	sub = (struct spdk_nvmf_subsystem *)subsystem;
883 
884 	pthread_mutex_lock(&sub->mutex);
885 	allow_any_host = sub->flags.allow_any_host;
886 	pthread_mutex_unlock(&sub->mutex);
887 
888 	return allow_any_host;
889 }
890 
891 bool
892 spdk_nvmf_subsystem_host_allowed(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
893 {
894 	bool allowed;
895 
896 	if (!hostnqn) {
897 		return false;
898 	}
899 
900 	pthread_mutex_lock(&subsystem->mutex);
901 
902 	if (subsystem->flags.allow_any_host) {
903 		pthread_mutex_unlock(&subsystem->mutex);
904 		return true;
905 	}
906 
907 	allowed =  nvmf_subsystem_find_host(subsystem, hostnqn) != NULL;
908 	pthread_mutex_unlock(&subsystem->mutex);
909 
910 	return allowed;
911 }
912 
913 struct spdk_nvmf_host *
914 spdk_nvmf_subsystem_get_first_host(struct spdk_nvmf_subsystem *subsystem)
915 {
916 	return TAILQ_FIRST(&subsystem->hosts);
917 }
918 
919 
920 struct spdk_nvmf_host *
921 spdk_nvmf_subsystem_get_next_host(struct spdk_nvmf_subsystem *subsystem,
922 				  struct spdk_nvmf_host *prev_host)
923 {
924 	return TAILQ_NEXT(prev_host, link);
925 }
926 
927 const char *
928 spdk_nvmf_host_get_nqn(const struct spdk_nvmf_host *host)
929 {
930 	return host->nqn;
931 }
932 
933 struct spdk_nvmf_subsystem_listener *
934 nvmf_subsystem_find_listener(struct spdk_nvmf_subsystem *subsystem,
935 			     const struct spdk_nvme_transport_id *trid)
936 {
937 	struct spdk_nvmf_subsystem_listener *listener;
938 
939 	TAILQ_FOREACH(listener, &subsystem->listeners, link) {
940 		if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) {
941 			return listener;
942 		}
943 	}
944 
945 	return NULL;
946 }
947 
948 /**
949  * Function to be called once the target is listening.
950  *
951  * \param ctx Context argument passed to this function.
952  * \param status 0 if it completed successfully, or negative errno if it failed.
953  */
954 static void
955 _nvmf_subsystem_add_listener_done(void *ctx, int status)
956 {
957 	struct spdk_nvmf_subsystem_listener *listener = ctx;
958 
959 	if (status) {
960 		listener->cb_fn(listener->cb_arg, status);
961 		free(listener);
962 		return;
963 	}
964 
965 	TAILQ_INSERT_HEAD(&listener->subsystem->listeners, listener, link);
966 	nvmf_update_discovery_log(listener->subsystem->tgt, NULL);
967 	listener->cb_fn(listener->cb_arg, status);
968 }
969 
970 void
971 spdk_nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem,
972 				 struct spdk_nvme_transport_id *trid,
973 				 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
974 				 void *cb_arg)
975 {
976 	struct spdk_nvmf_transport *transport;
977 	struct spdk_nvmf_subsystem_listener *listener;
978 	struct spdk_nvmf_listener *tr_listener;
979 	int rc = 0;
980 
981 	assert(cb_fn != NULL);
982 
983 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
984 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
985 		cb_fn(cb_arg, -EAGAIN);
986 		return;
987 	}
988 
989 	if (nvmf_subsystem_find_listener(subsystem, trid)) {
990 		/* Listener already exists in this subsystem */
991 		cb_fn(cb_arg, 0);
992 		return;
993 	}
994 
995 	transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, trid->trstring);
996 	if (!transport) {
997 		SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
998 			    trid->trstring);
999 		cb_fn(cb_arg, -EINVAL);
1000 		return;
1001 	}
1002 
1003 	tr_listener = nvmf_transport_find_listener(transport, trid);
1004 	if (!tr_listener) {
1005 		SPDK_ERRLOG("Cannot find transport listener for %s\n", trid->traddr);
1006 		cb_fn(cb_arg, -EINVAL);
1007 		return;
1008 	}
1009 
1010 	listener = calloc(1, sizeof(*listener));
1011 	if (!listener) {
1012 		cb_fn(cb_arg, -ENOMEM);
1013 		return;
1014 	}
1015 
1016 	listener->trid = &tr_listener->trid;
1017 	listener->transport = transport;
1018 	listener->cb_fn = cb_fn;
1019 	listener->cb_arg = cb_arg;
1020 	listener->subsystem = subsystem;
1021 	listener->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
1022 
1023 	if (transport->ops->listen_associate != NULL) {
1024 		rc = transport->ops->listen_associate(transport, subsystem, trid);
1025 	}
1026 
1027 	_nvmf_subsystem_add_listener_done(listener, rc);
1028 }
1029 
1030 int
1031 spdk_nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem,
1032 				    const struct spdk_nvme_transport_id *trid)
1033 {
1034 	struct spdk_nvmf_subsystem_listener *listener;
1035 
1036 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1037 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1038 		return -EAGAIN;
1039 	}
1040 
1041 	listener = nvmf_subsystem_find_listener(subsystem, trid);
1042 	if (listener == NULL) {
1043 		return -ENOENT;
1044 	}
1045 
1046 	_nvmf_subsystem_remove_listener(subsystem, listener, false);
1047 
1048 	return 0;
1049 }
1050 
1051 void
1052 nvmf_subsystem_remove_all_listeners(struct spdk_nvmf_subsystem *subsystem,
1053 				    bool stop)
1054 {
1055 	struct spdk_nvmf_subsystem_listener *listener, *listener_tmp;
1056 
1057 	TAILQ_FOREACH_SAFE(listener, &subsystem->listeners, link, listener_tmp) {
1058 		_nvmf_subsystem_remove_listener(subsystem, listener, stop);
1059 	}
1060 }
1061 
1062 bool
1063 spdk_nvmf_subsystem_listener_allowed(struct spdk_nvmf_subsystem *subsystem,
1064 				     const struct spdk_nvme_transport_id *trid)
1065 {
1066 	struct spdk_nvmf_subsystem_listener *listener;
1067 
1068 	if (!strcmp(subsystem->subnqn, SPDK_NVMF_DISCOVERY_NQN)) {
1069 		return true;
1070 	}
1071 
1072 	TAILQ_FOREACH(listener, &subsystem->listeners, link) {
1073 		if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) {
1074 			return true;
1075 		}
1076 	}
1077 
1078 	return false;
1079 }
1080 
1081 struct spdk_nvmf_subsystem_listener *
1082 spdk_nvmf_subsystem_get_first_listener(struct spdk_nvmf_subsystem *subsystem)
1083 {
1084 	return TAILQ_FIRST(&subsystem->listeners);
1085 }
1086 
1087 struct spdk_nvmf_subsystem_listener *
1088 spdk_nvmf_subsystem_get_next_listener(struct spdk_nvmf_subsystem *subsystem,
1089 				      struct spdk_nvmf_subsystem_listener *prev_listener)
1090 {
1091 	return TAILQ_NEXT(prev_listener, link);
1092 }
1093 
1094 const struct spdk_nvme_transport_id *
1095 spdk_nvmf_subsystem_listener_get_trid(struct spdk_nvmf_subsystem_listener *listener)
1096 {
1097 	return listener->trid;
1098 }
1099 
1100 void
1101 spdk_nvmf_subsystem_allow_any_listener(struct spdk_nvmf_subsystem *subsystem,
1102 				       bool allow_any_listener)
1103 {
1104 	subsystem->flags.allow_any_listener = allow_any_listener;
1105 }
1106 
1107 bool
1108 spdk_nvmf_subsytem_any_listener_allowed(struct spdk_nvmf_subsystem *subsystem)
1109 {
1110 	return subsystem->flags.allow_any_listener;
1111 }
1112 
1113 
1114 struct subsystem_update_ns_ctx {
1115 	struct spdk_nvmf_subsystem *subsystem;
1116 
1117 	spdk_nvmf_subsystem_state_change_done cb_fn;
1118 	void *cb_arg;
1119 };
1120 
1121 static void
1122 subsystem_update_ns_done(struct spdk_io_channel_iter *i, int status)
1123 {
1124 	struct subsystem_update_ns_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1125 
1126 	if (ctx->cb_fn) {
1127 		ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status);
1128 	}
1129 	free(ctx);
1130 }
1131 
1132 static void
1133 subsystem_update_ns_on_pg(struct spdk_io_channel_iter *i)
1134 {
1135 	int rc;
1136 	struct subsystem_update_ns_ctx *ctx;
1137 	struct spdk_nvmf_poll_group *group;
1138 	struct spdk_nvmf_subsystem *subsystem;
1139 
1140 	ctx = spdk_io_channel_iter_get_ctx(i);
1141 	group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
1142 	subsystem = ctx->subsystem;
1143 
1144 	rc = nvmf_poll_group_update_subsystem(group, subsystem);
1145 	spdk_for_each_channel_continue(i, rc);
1146 }
1147 
1148 static int
1149 nvmf_subsystem_update_ns(struct spdk_nvmf_subsystem *subsystem, spdk_channel_for_each_cpl cpl,
1150 			 void *ctx)
1151 {
1152 	spdk_for_each_channel(subsystem->tgt,
1153 			      subsystem_update_ns_on_pg,
1154 			      ctx,
1155 			      cpl);
1156 
1157 	return 0;
1158 }
1159 
1160 static void
1161 nvmf_subsystem_ns_changed(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1162 {
1163 	struct spdk_nvmf_ctrlr *ctrlr;
1164 
1165 	TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1166 		nvmf_ctrlr_ns_changed(ctrlr, nsid);
1167 	}
1168 }
1169 
1170 static uint32_t
1171 nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns);
1172 
1173 int
1174 spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1175 {
1176 	struct spdk_nvmf_transport *transport;
1177 	struct spdk_nvmf_ns *ns;
1178 
1179 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1180 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1181 		assert(false);
1182 		return -1;
1183 	}
1184 
1185 	if (nsid == 0 || nsid > subsystem->max_nsid) {
1186 		return -1;
1187 	}
1188 
1189 	ns = subsystem->ns[nsid - 1];
1190 	if (!ns) {
1191 		return -1;
1192 	}
1193 
1194 	subsystem->ns[nsid - 1] = NULL;
1195 
1196 	free(ns->ptpl_file);
1197 	nvmf_ns_reservation_clear_all_registrants(ns);
1198 	spdk_bdev_module_release_bdev(ns->bdev);
1199 	spdk_bdev_close(ns->desc);
1200 	free(ns);
1201 
1202 	for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1203 	     transport = spdk_nvmf_transport_get_next(transport)) {
1204 		if (transport->ops->subsystem_remove_ns) {
1205 			transport->ops->subsystem_remove_ns(transport, subsystem, nsid);
1206 		}
1207 	}
1208 
1209 	nvmf_subsystem_ns_changed(subsystem, nsid);
1210 
1211 	return 0;
1212 }
1213 
1214 struct subsystem_ns_change_ctx {
1215 	struct spdk_nvmf_subsystem		*subsystem;
1216 	spdk_nvmf_subsystem_state_change_done	cb_fn;
1217 	uint32_t				nsid;
1218 };
1219 
1220 static void
1221 _nvmf_ns_hot_remove(struct spdk_nvmf_subsystem *subsystem,
1222 		    void *cb_arg, int status)
1223 {
1224 	struct subsystem_ns_change_ctx *ctx = cb_arg;
1225 	int rc;
1226 
1227 	rc = spdk_nvmf_subsystem_remove_ns(subsystem, ctx->nsid);
1228 	if (rc != 0) {
1229 		SPDK_ERRLOG("Failed to make changes to NVME-oF subsystem with id: %u\n", subsystem->id);
1230 	}
1231 
1232 	spdk_nvmf_subsystem_resume(subsystem, NULL, NULL);
1233 
1234 	free(ctx);
1235 }
1236 
1237 static void
1238 nvmf_ns_change_msg(void *ns_ctx)
1239 {
1240 	struct subsystem_ns_change_ctx *ctx = ns_ctx;
1241 	int rc;
1242 
1243 	rc = spdk_nvmf_subsystem_pause(ctx->subsystem, ctx->nsid, ctx->cb_fn, ctx);
1244 	if (rc) {
1245 		if (rc == -EBUSY) {
1246 			/* Try again, this is not a permanent situation. */
1247 			spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ctx);
1248 		} else {
1249 			free(ctx);
1250 			SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n");
1251 		}
1252 	}
1253 }
1254 
1255 static void
1256 nvmf_ns_hot_remove(void *remove_ctx)
1257 {
1258 	struct spdk_nvmf_ns *ns = remove_ctx;
1259 	struct subsystem_ns_change_ctx *ns_ctx;
1260 	int rc;
1261 
1262 	/* We have to allocate a new context because this op
1263 	 * is asynchronous and we could lose the ns in the middle.
1264 	 */
1265 	ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx));
1266 	if (!ns_ctx) {
1267 		SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n");
1268 		return;
1269 	}
1270 
1271 	ns_ctx->subsystem = ns->subsystem;
1272 	ns_ctx->nsid = ns->opts.nsid;
1273 	ns_ctx->cb_fn = _nvmf_ns_hot_remove;
1274 
1275 	rc = spdk_nvmf_subsystem_pause(ns->subsystem, ns_ctx->nsid, _nvmf_ns_hot_remove, ns_ctx);
1276 	if (rc) {
1277 		if (rc == -EBUSY) {
1278 			/* Try again, this is not a permanent situation. */
1279 			spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx);
1280 		} else {
1281 			SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n");
1282 			free(ns_ctx);
1283 		}
1284 	}
1285 }
1286 
1287 static void
1288 _nvmf_ns_resize(struct spdk_nvmf_subsystem *subsystem, void *cb_arg, int status)
1289 {
1290 	struct subsystem_ns_change_ctx *ctx = cb_arg;
1291 
1292 	nvmf_subsystem_ns_changed(subsystem, ctx->nsid);
1293 	spdk_nvmf_subsystem_resume(subsystem, NULL, NULL);
1294 
1295 	free(ctx);
1296 }
1297 
1298 static void
1299 nvmf_ns_resize(void *event_ctx)
1300 {
1301 	struct spdk_nvmf_ns *ns = event_ctx;
1302 	struct subsystem_ns_change_ctx *ns_ctx;
1303 	int rc;
1304 
1305 	/* We have to allocate a new context because this op
1306 	 * is asynchronous and we could lose the ns in the middle.
1307 	 */
1308 	ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx));
1309 	if (!ns_ctx) {
1310 		SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n");
1311 		return;
1312 	}
1313 
1314 	ns_ctx->subsystem = ns->subsystem;
1315 	ns_ctx->nsid = ns->opts.nsid;
1316 	ns_ctx->cb_fn = _nvmf_ns_resize;
1317 
1318 	/* Specify 0 for the nsid here, because we do not need to pause the namespace.
1319 	 * Namespaces can only be resized bigger, so there is no need to quiesce I/O.
1320 	 */
1321 	rc = spdk_nvmf_subsystem_pause(ns->subsystem, 0, _nvmf_ns_resize, ns_ctx);
1322 	if (rc) {
1323 		if (rc == -EBUSY) {
1324 			/* Try again, this is not a permanent situation. */
1325 			spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx);
1326 		} else {
1327 			SPDK_ERRLOG("Unable to pause subsystem to process namespace resize!\n");
1328 			free(ns_ctx);
1329 		}
1330 	}
1331 }
1332 
1333 static void
1334 nvmf_ns_event(enum spdk_bdev_event_type type,
1335 	      struct spdk_bdev *bdev,
1336 	      void *event_ctx)
1337 {
1338 	SPDK_DEBUGLOG(nvmf, "Bdev event: type %d, name %s, subsystem_id %d, ns_id %d\n",
1339 		      type,
1340 		      spdk_bdev_get_name(bdev),
1341 		      ((struct spdk_nvmf_ns *)event_ctx)->subsystem->id,
1342 		      ((struct spdk_nvmf_ns *)event_ctx)->nsid);
1343 
1344 	switch (type) {
1345 	case SPDK_BDEV_EVENT_REMOVE:
1346 		nvmf_ns_hot_remove(event_ctx);
1347 		break;
1348 	case SPDK_BDEV_EVENT_RESIZE:
1349 		nvmf_ns_resize(event_ctx);
1350 		break;
1351 	default:
1352 		SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
1353 		break;
1354 	}
1355 }
1356 
1357 void
1358 spdk_nvmf_ns_opts_get_defaults(struct spdk_nvmf_ns_opts *opts, size_t opts_size)
1359 {
1360 	/* All current fields are set to 0 by default. */
1361 	memset(opts, 0, opts_size);
1362 }
1363 
1364 /* Dummy bdev module used to to claim bdevs. */
1365 static struct spdk_bdev_module ns_bdev_module = {
1366 	.name	= "NVMe-oF Target",
1367 };
1368 
1369 static int
1370 nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info);
1371 static int
1372 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info);
1373 
1374 uint32_t
1375 spdk_nvmf_subsystem_add_ns_ext(struct spdk_nvmf_subsystem *subsystem, const char *bdev_name,
1376 			       const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size,
1377 			       const char *ptpl_file)
1378 {
1379 	struct spdk_nvmf_transport *transport;
1380 	struct spdk_nvmf_ns_opts opts;
1381 	struct spdk_nvmf_ns *ns;
1382 	struct spdk_nvmf_reservation_info info = {0};
1383 	int rc;
1384 
1385 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1386 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1387 		return 0;
1388 	}
1389 
1390 	spdk_nvmf_ns_opts_get_defaults(&opts, sizeof(opts));
1391 	if (user_opts) {
1392 		memcpy(&opts, user_opts, spdk_min(sizeof(opts), opts_size));
1393 	}
1394 
1395 	if (opts.nsid == SPDK_NVME_GLOBAL_NS_TAG) {
1396 		SPDK_ERRLOG("Invalid NSID %" PRIu32 "\n", opts.nsid);
1397 		return 0;
1398 	}
1399 
1400 	if (opts.nsid == 0) {
1401 		/*
1402 		 * NSID not specified - find a free index.
1403 		 *
1404 		 * If no free slots are found, opts.nsid will be subsystem->max_nsid + 1, which will
1405 		 * expand max_nsid if possible.
1406 		 */
1407 		for (opts.nsid = 1; opts.nsid <= subsystem->max_nsid; opts.nsid++) {
1408 			if (_nvmf_subsystem_get_ns(subsystem, opts.nsid) == NULL) {
1409 				break;
1410 			}
1411 		}
1412 	}
1413 
1414 	if (_nvmf_subsystem_get_ns(subsystem, opts.nsid)) {
1415 		SPDK_ERRLOG("Requested NSID %" PRIu32 " already in use\n", opts.nsid);
1416 		return 0;
1417 	}
1418 
1419 	if (opts.nsid > subsystem->max_nsid) {
1420 		SPDK_ERRLOG("NSID greater than maximum not allowed\n");
1421 		return 0;
1422 	}
1423 
1424 	ns = calloc(1, sizeof(*ns));
1425 	if (ns == NULL) {
1426 		SPDK_ERRLOG("Namespace allocation failed\n");
1427 		return 0;
1428 	}
1429 
1430 	rc = spdk_bdev_open_ext(bdev_name, true, nvmf_ns_event, ns, &ns->desc);
1431 	if (rc != 0) {
1432 		SPDK_ERRLOG("Subsystem %s: bdev %s cannot be opened, error=%d\n",
1433 			    subsystem->subnqn, bdev_name, rc);
1434 		free(ns);
1435 		return 0;
1436 	}
1437 
1438 	ns->bdev = spdk_bdev_desc_get_bdev(ns->desc);
1439 
1440 	if (spdk_bdev_get_md_size(ns->bdev) != 0 && !spdk_bdev_is_md_interleaved(ns->bdev)) {
1441 		SPDK_ERRLOG("Can't attach bdev with separate metadata.\n");
1442 		spdk_bdev_close(ns->desc);
1443 		free(ns);
1444 		return 0;
1445 	}
1446 
1447 	rc = spdk_bdev_module_claim_bdev(ns->bdev, ns->desc, &ns_bdev_module);
1448 	if (rc != 0) {
1449 		spdk_bdev_close(ns->desc);
1450 		free(ns);
1451 		return 0;
1452 	}
1453 
1454 	if (spdk_mem_all_zero(&opts.uuid, sizeof(opts.uuid))) {
1455 		opts.uuid = *spdk_bdev_get_uuid(ns->bdev);
1456 	}
1457 
1458 	ns->opts = opts;
1459 	ns->subsystem = subsystem;
1460 	subsystem->ns[opts.nsid - 1] = ns;
1461 	ns->nsid = opts.nsid;
1462 	TAILQ_INIT(&ns->registrants);
1463 
1464 	if (ptpl_file) {
1465 		rc = nvmf_ns_load_reservation(ptpl_file, &info);
1466 		if (!rc) {
1467 			rc = nvmf_ns_reservation_restore(ns, &info);
1468 			if (rc) {
1469 				SPDK_ERRLOG("Subsystem restore reservation failed\n");
1470 				subsystem->ns[opts.nsid - 1] = NULL;
1471 				spdk_bdev_module_release_bdev(ns->bdev);
1472 				spdk_bdev_close(ns->desc);
1473 				free(ns);
1474 				return 0;
1475 			}
1476 		}
1477 		ns->ptpl_file = strdup(ptpl_file);
1478 	}
1479 
1480 	for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1481 	     transport = spdk_nvmf_transport_get_next(transport)) {
1482 		if (transport->ops->subsystem_add_ns) {
1483 			rc = transport->ops->subsystem_add_ns(transport, subsystem, ns);
1484 			if (rc) {
1485 				SPDK_ERRLOG("Namespace attachment is not allowed by %s transport\n", transport->ops->name);
1486 				free(ns->ptpl_file);
1487 				nvmf_ns_reservation_clear_all_registrants(ns);
1488 				subsystem->ns[opts.nsid - 1] = NULL;
1489 				spdk_bdev_module_release_bdev(ns->bdev);
1490 				spdk_bdev_close(ns->desc);
1491 				free(ns);
1492 				return 0;
1493 			}
1494 		}
1495 	}
1496 
1497 	SPDK_DEBUGLOG(nvmf, "Subsystem %s: bdev %s assigned nsid %" PRIu32 "\n",
1498 		      spdk_nvmf_subsystem_get_nqn(subsystem),
1499 		      bdev_name,
1500 		      opts.nsid);
1501 
1502 	nvmf_subsystem_ns_changed(subsystem, opts.nsid);
1503 
1504 	return opts.nsid;
1505 }
1506 
1507 static uint32_t
1508 nvmf_subsystem_get_next_allocated_nsid(struct spdk_nvmf_subsystem *subsystem,
1509 				       uint32_t prev_nsid)
1510 {
1511 	uint32_t nsid;
1512 
1513 	if (prev_nsid >= subsystem->max_nsid) {
1514 		return 0;
1515 	}
1516 
1517 	for (nsid = prev_nsid + 1; nsid <= subsystem->max_nsid; nsid++) {
1518 		if (subsystem->ns[nsid - 1]) {
1519 			return nsid;
1520 		}
1521 	}
1522 
1523 	return 0;
1524 }
1525 
1526 struct spdk_nvmf_ns *
1527 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem)
1528 {
1529 	uint32_t first_nsid;
1530 
1531 	first_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, 0);
1532 	return _nvmf_subsystem_get_ns(subsystem, first_nsid);
1533 }
1534 
1535 struct spdk_nvmf_ns *
1536 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem,
1537 				struct spdk_nvmf_ns *prev_ns)
1538 {
1539 	uint32_t next_nsid;
1540 
1541 	next_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, prev_ns->opts.nsid);
1542 	return _nvmf_subsystem_get_ns(subsystem, next_nsid);
1543 }
1544 
1545 struct spdk_nvmf_ns *
1546 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1547 {
1548 	return _nvmf_subsystem_get_ns(subsystem, nsid);
1549 }
1550 
1551 uint32_t
1552 spdk_nvmf_ns_get_id(const struct spdk_nvmf_ns *ns)
1553 {
1554 	return ns->opts.nsid;
1555 }
1556 
1557 struct spdk_bdev *
1558 spdk_nvmf_ns_get_bdev(struct spdk_nvmf_ns *ns)
1559 {
1560 	return ns->bdev;
1561 }
1562 
1563 void
1564 spdk_nvmf_ns_get_opts(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_ns_opts *opts,
1565 		      size_t opts_size)
1566 {
1567 	memset(opts, 0, opts_size);
1568 	memcpy(opts, &ns->opts, spdk_min(sizeof(ns->opts), opts_size));
1569 }
1570 
1571 const char *
1572 spdk_nvmf_subsystem_get_sn(const struct spdk_nvmf_subsystem *subsystem)
1573 {
1574 	return subsystem->sn;
1575 }
1576 
1577 int
1578 spdk_nvmf_subsystem_set_sn(struct spdk_nvmf_subsystem *subsystem, const char *sn)
1579 {
1580 	size_t len, max_len;
1581 
1582 	max_len = sizeof(subsystem->sn) - 1;
1583 	len = strlen(sn);
1584 	if (len > max_len) {
1585 		SPDK_DEBUGLOG(nvmf, "Invalid sn \"%s\": length %zu > max %zu\n",
1586 			      sn, len, max_len);
1587 		return -1;
1588 	}
1589 
1590 	if (!nvmf_valid_ascii_string(sn, len)) {
1591 		SPDK_DEBUGLOG(nvmf, "Non-ASCII sn\n");
1592 		SPDK_LOGDUMP(nvmf, "sn", sn, len);
1593 		return -1;
1594 	}
1595 
1596 	snprintf(subsystem->sn, sizeof(subsystem->sn), "%s", sn);
1597 
1598 	return 0;
1599 }
1600 
1601 const char *
1602 spdk_nvmf_subsystem_get_mn(const struct spdk_nvmf_subsystem *subsystem)
1603 {
1604 	return subsystem->mn;
1605 }
1606 
1607 int
1608 spdk_nvmf_subsystem_set_mn(struct spdk_nvmf_subsystem *subsystem, const char *mn)
1609 {
1610 	size_t len, max_len;
1611 
1612 	if (mn == NULL) {
1613 		mn = MODEL_NUMBER_DEFAULT;
1614 	}
1615 	max_len = sizeof(subsystem->mn) - 1;
1616 	len = strlen(mn);
1617 	if (len > max_len) {
1618 		SPDK_DEBUGLOG(nvmf, "Invalid mn \"%s\": length %zu > max %zu\n",
1619 			      mn, len, max_len);
1620 		return -1;
1621 	}
1622 
1623 	if (!nvmf_valid_ascii_string(mn, len)) {
1624 		SPDK_DEBUGLOG(nvmf, "Non-ASCII mn\n");
1625 		SPDK_LOGDUMP(nvmf, "mn", mn, len);
1626 		return -1;
1627 	}
1628 
1629 	snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", mn);
1630 
1631 	return 0;
1632 }
1633 
1634 const char *
1635 spdk_nvmf_subsystem_get_nqn(const struct spdk_nvmf_subsystem *subsystem)
1636 {
1637 	return subsystem->subnqn;
1638 }
1639 
1640 enum spdk_nvmf_subtype spdk_nvmf_subsystem_get_type(struct spdk_nvmf_subsystem *subsystem)
1641 {
1642 	return subsystem->subtype;
1643 }
1644 
1645 uint32_t
1646 spdk_nvmf_subsystem_get_max_nsid(struct spdk_nvmf_subsystem *subsystem)
1647 {
1648 	return subsystem->max_nsid;
1649 }
1650 
1651 int
1652 nvmf_subsystem_set_cntlid_range(struct spdk_nvmf_subsystem *subsystem,
1653 				uint16_t min_cntlid, uint16_t max_cntlid)
1654 {
1655 	if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
1656 		return -EAGAIN;
1657 	}
1658 
1659 	if (min_cntlid > max_cntlid) {
1660 		return -EINVAL;
1661 	}
1662 	/* The spec reserves cntlid values in the range FFF0h to FFFFh. */
1663 	if (min_cntlid < NVMF_MIN_CNTLID || min_cntlid > NVMF_MAX_CNTLID ||
1664 	    max_cntlid < NVMF_MIN_CNTLID || max_cntlid > NVMF_MAX_CNTLID) {
1665 		return -EINVAL;
1666 	}
1667 	subsystem->min_cntlid = min_cntlid;
1668 	subsystem->max_cntlid = max_cntlid;
1669 	if (subsystem->next_cntlid < min_cntlid || subsystem->next_cntlid > max_cntlid - 1) {
1670 		subsystem->next_cntlid = min_cntlid - 1;
1671 	}
1672 
1673 	return 0;
1674 }
1675 
1676 static uint16_t
1677 nvmf_subsystem_gen_cntlid(struct spdk_nvmf_subsystem *subsystem)
1678 {
1679 	int count;
1680 
1681 	/*
1682 	 * In the worst case, we might have to try all CNTLID values between min_cntlid and max_cntlid
1683 	 * before we find one that is unused (or find that all values are in use).
1684 	 */
1685 	for (count = 0; count < subsystem->max_cntlid - subsystem->min_cntlid + 1; count++) {
1686 		subsystem->next_cntlid++;
1687 		if (subsystem->next_cntlid > subsystem->max_cntlid) {
1688 			subsystem->next_cntlid = subsystem->min_cntlid;
1689 		}
1690 
1691 		/* Check if a controller with this cntlid currently exists. */
1692 		if (nvmf_subsystem_get_ctrlr(subsystem, subsystem->next_cntlid) == NULL) {
1693 			/* Found unused cntlid */
1694 			return subsystem->next_cntlid;
1695 		}
1696 	}
1697 
1698 	/* All valid cntlid values are in use. */
1699 	return 0xFFFF;
1700 }
1701 
1702 int
1703 nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr)
1704 {
1705 	ctrlr->cntlid = nvmf_subsystem_gen_cntlid(subsystem);
1706 	if (ctrlr->cntlid == 0xFFFF) {
1707 		/* Unable to get a cntlid */
1708 		SPDK_ERRLOG("Reached max simultaneous ctrlrs\n");
1709 		return -EBUSY;
1710 	}
1711 
1712 	TAILQ_INSERT_TAIL(&subsystem->ctrlrs, ctrlr, link);
1713 
1714 	return 0;
1715 }
1716 
1717 void
1718 nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem,
1719 			    struct spdk_nvmf_ctrlr *ctrlr)
1720 {
1721 	assert(subsystem == ctrlr->subsys);
1722 	TAILQ_REMOVE(&subsystem->ctrlrs, ctrlr, link);
1723 }
1724 
1725 struct spdk_nvmf_ctrlr *
1726 nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, uint16_t cntlid)
1727 {
1728 	struct spdk_nvmf_ctrlr *ctrlr;
1729 
1730 	TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1731 		if (ctrlr->cntlid == cntlid) {
1732 			return ctrlr;
1733 		}
1734 	}
1735 
1736 	return NULL;
1737 }
1738 
1739 uint32_t
1740 spdk_nvmf_subsystem_get_max_namespaces(const struct spdk_nvmf_subsystem *subsystem)
1741 {
1742 	return subsystem->max_nsid;
1743 }
1744 
1745 uint16_t
1746 spdk_nvmf_subsystem_get_min_cntlid(const struct spdk_nvmf_subsystem *subsystem)
1747 {
1748 	return subsystem->min_cntlid;
1749 }
1750 
1751 uint16_t
1752 spdk_nvmf_subsystem_get_max_cntlid(const struct spdk_nvmf_subsystem *subsystem)
1753 {
1754 	return subsystem->max_cntlid;
1755 }
1756 
1757 struct _nvmf_ns_registrant {
1758 	uint64_t		rkey;
1759 	char			*host_uuid;
1760 };
1761 
1762 struct _nvmf_ns_registrants {
1763 	size_t				num_regs;
1764 	struct _nvmf_ns_registrant	reg[SPDK_NVMF_MAX_NUM_REGISTRANTS];
1765 };
1766 
1767 struct _nvmf_ns_reservation {
1768 	bool					ptpl_activated;
1769 	enum spdk_nvme_reservation_type		rtype;
1770 	uint64_t				crkey;
1771 	char					*bdev_uuid;
1772 	char					*holder_uuid;
1773 	struct _nvmf_ns_registrants		regs;
1774 };
1775 
1776 static const struct spdk_json_object_decoder nvmf_ns_pr_reg_decoders[] = {
1777 	{"rkey", offsetof(struct _nvmf_ns_registrant, rkey), spdk_json_decode_uint64},
1778 	{"host_uuid", offsetof(struct _nvmf_ns_registrant, host_uuid), spdk_json_decode_string},
1779 };
1780 
1781 static int
1782 nvmf_decode_ns_pr_reg(const struct spdk_json_val *val, void *out)
1783 {
1784 	struct _nvmf_ns_registrant *reg = out;
1785 
1786 	return spdk_json_decode_object(val, nvmf_ns_pr_reg_decoders,
1787 				       SPDK_COUNTOF(nvmf_ns_pr_reg_decoders), reg);
1788 }
1789 
1790 static int
1791 nvmf_decode_ns_pr_regs(const struct spdk_json_val *val, void *out)
1792 {
1793 	struct _nvmf_ns_registrants *regs = out;
1794 
1795 	return spdk_json_decode_array(val, nvmf_decode_ns_pr_reg, regs->reg,
1796 				      SPDK_NVMF_MAX_NUM_REGISTRANTS, &regs->num_regs,
1797 				      sizeof(struct _nvmf_ns_registrant));
1798 }
1799 
1800 static const struct spdk_json_object_decoder nvmf_ns_pr_decoders[] = {
1801 	{"ptpl", offsetof(struct _nvmf_ns_reservation, ptpl_activated), spdk_json_decode_bool, true},
1802 	{"rtype", offsetof(struct _nvmf_ns_reservation, rtype), spdk_json_decode_uint32, true},
1803 	{"crkey", offsetof(struct _nvmf_ns_reservation, crkey), spdk_json_decode_uint64, true},
1804 	{"bdev_uuid", offsetof(struct _nvmf_ns_reservation, bdev_uuid), spdk_json_decode_string},
1805 	{"holder_uuid", offsetof(struct _nvmf_ns_reservation, holder_uuid), spdk_json_decode_string, true},
1806 	{"registrants", offsetof(struct _nvmf_ns_reservation, regs), nvmf_decode_ns_pr_regs},
1807 };
1808 
1809 static int
1810 nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info)
1811 {
1812 	FILE *fd;
1813 	size_t json_size;
1814 	ssize_t values_cnt, rc;
1815 	void *json = NULL, *end;
1816 	struct spdk_json_val *values = NULL;
1817 	struct _nvmf_ns_reservation res = {};
1818 	uint32_t i;
1819 
1820 	fd = fopen(file, "r");
1821 	/* It's not an error if the file does not exist */
1822 	if (!fd) {
1823 		SPDK_NOTICELOG("File %s does not exist\n", file);
1824 		return -ENOENT;
1825 	}
1826 
1827 	/* Load all persist file contents into a local buffer */
1828 	json = spdk_posix_file_load(fd, &json_size);
1829 	fclose(fd);
1830 	if (!json) {
1831 		SPDK_ERRLOG("Load persit file %s failed\n", file);
1832 		return -ENOMEM;
1833 	}
1834 
1835 	rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0);
1836 	if (rc < 0) {
1837 		SPDK_NOTICELOG("Parsing JSON configuration failed (%zd)\n", rc);
1838 		goto exit;
1839 	}
1840 
1841 	values_cnt = rc;
1842 	values = calloc(values_cnt, sizeof(struct spdk_json_val));
1843 	if (values == NULL) {
1844 		goto exit;
1845 	}
1846 
1847 	rc = spdk_json_parse(json, json_size, values, values_cnt, &end, 0);
1848 	if (rc != values_cnt) {
1849 		SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
1850 		goto exit;
1851 	}
1852 
1853 	/* Decode json */
1854 	if (spdk_json_decode_object(values, nvmf_ns_pr_decoders,
1855 				    SPDK_COUNTOF(nvmf_ns_pr_decoders),
1856 				    &res)) {
1857 		SPDK_ERRLOG("Invalid objects in the persist file %s\n", file);
1858 		rc = -EINVAL;
1859 		goto exit;
1860 	}
1861 
1862 	if (res.regs.num_regs > SPDK_NVMF_MAX_NUM_REGISTRANTS) {
1863 		SPDK_ERRLOG("Can only support up to %u registrants\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
1864 		rc = -ERANGE;
1865 		goto exit;
1866 	}
1867 
1868 	rc = 0;
1869 	info->ptpl_activated = res.ptpl_activated;
1870 	info->rtype = res.rtype;
1871 	info->crkey = res.crkey;
1872 	snprintf(info->bdev_uuid, sizeof(info->bdev_uuid), "%s", res.bdev_uuid);
1873 	snprintf(info->holder_uuid, sizeof(info->holder_uuid), "%s", res.holder_uuid);
1874 	info->num_regs = res.regs.num_regs;
1875 	for (i = 0; i < res.regs.num_regs; i++) {
1876 		info->registrants[i].rkey = res.regs.reg[i].rkey;
1877 		snprintf(info->registrants[i].host_uuid, sizeof(info->registrants[i].host_uuid), "%s",
1878 			 res.regs.reg[i].host_uuid);
1879 	}
1880 
1881 exit:
1882 	free(json);
1883 	free(values);
1884 	free(res.bdev_uuid);
1885 	free(res.holder_uuid);
1886 	for (i = 0; i < res.regs.num_regs; i++) {
1887 		free(res.regs.reg[i].host_uuid);
1888 	}
1889 
1890 	return rc;
1891 }
1892 
1893 static bool
1894 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns);
1895 
1896 static int
1897 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
1898 {
1899 	uint32_t i;
1900 	struct spdk_nvmf_registrant *reg, *holder = NULL;
1901 	struct spdk_uuid bdev_uuid, holder_uuid;
1902 
1903 	SPDK_DEBUGLOG(nvmf, "NSID %u, PTPL %u, Number of registrants %u\n",
1904 		      ns->nsid, info->ptpl_activated, info->num_regs);
1905 
1906 	/* it's not an error */
1907 	if (!info->ptpl_activated || !info->num_regs) {
1908 		return 0;
1909 	}
1910 
1911 	spdk_uuid_parse(&bdev_uuid, info->bdev_uuid);
1912 	if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) {
1913 		SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n");
1914 		return -EINVAL;
1915 	}
1916 
1917 	ns->crkey = info->crkey;
1918 	ns->rtype = info->rtype;
1919 	ns->ptpl_activated = info->ptpl_activated;
1920 	spdk_uuid_parse(&holder_uuid, info->holder_uuid);
1921 
1922 	SPDK_DEBUGLOG(nvmf, "Bdev UUID %s\n", info->bdev_uuid);
1923 	if (info->rtype) {
1924 		SPDK_DEBUGLOG(nvmf, "Holder UUID %s, RTYPE %u, RKEY 0x%"PRIx64"\n",
1925 			      info->holder_uuid, info->rtype, info->crkey);
1926 	}
1927 
1928 	for (i = 0; i < info->num_regs; i++) {
1929 		reg = calloc(1, sizeof(*reg));
1930 		if (!reg) {
1931 			return -ENOMEM;
1932 		}
1933 		spdk_uuid_parse(&reg->hostid, info->registrants[i].host_uuid);
1934 		reg->rkey = info->registrants[i].rkey;
1935 		TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
1936 		if (!spdk_uuid_compare(&holder_uuid, &reg->hostid)) {
1937 			holder = reg;
1938 		}
1939 		SPDK_DEBUGLOG(nvmf, "Registrant RKEY 0x%"PRIx64", Host UUID %s\n",
1940 			      info->registrants[i].rkey, info->registrants[i].host_uuid);
1941 	}
1942 
1943 	if (nvmf_ns_reservation_all_registrants_type(ns)) {
1944 		ns->holder = TAILQ_FIRST(&ns->registrants);
1945 	} else {
1946 		ns->holder = holder;
1947 	}
1948 
1949 	return 0;
1950 }
1951 
1952 static int
1953 nvmf_ns_json_write_cb(void *cb_ctx, const void *data, size_t size)
1954 {
1955 	char *file = cb_ctx;
1956 	size_t rc;
1957 	FILE *fd;
1958 
1959 	fd = fopen(file, "w");
1960 	if (!fd) {
1961 		SPDK_ERRLOG("Can't open file %s for write\n", file);
1962 		return -ENOENT;
1963 	}
1964 	rc = fwrite(data, 1, size, fd);
1965 	fclose(fd);
1966 
1967 	return rc == size ? 0 : -1;
1968 }
1969 
1970 static int
1971 nvmf_ns_reservation_update(const char *file, struct spdk_nvmf_reservation_info *info)
1972 {
1973 	struct spdk_json_write_ctx *w;
1974 	uint32_t i;
1975 	int rc = 0;
1976 
1977 	w = spdk_json_write_begin(nvmf_ns_json_write_cb, (void *)file, 0);
1978 	if (w == NULL) {
1979 		return -ENOMEM;
1980 	}
1981 	/* clear the configuration file */
1982 	if (!info->ptpl_activated) {
1983 		goto exit;
1984 	}
1985 
1986 	spdk_json_write_object_begin(w);
1987 	spdk_json_write_named_bool(w, "ptpl", info->ptpl_activated);
1988 	spdk_json_write_named_uint32(w, "rtype", info->rtype);
1989 	spdk_json_write_named_uint64(w, "crkey", info->crkey);
1990 	spdk_json_write_named_string(w, "bdev_uuid", info->bdev_uuid);
1991 	spdk_json_write_named_string(w, "holder_uuid", info->holder_uuid);
1992 
1993 	spdk_json_write_named_array_begin(w, "registrants");
1994 	for (i = 0; i < info->num_regs; i++) {
1995 		spdk_json_write_object_begin(w);
1996 		spdk_json_write_named_uint64(w, "rkey", info->registrants[i].rkey);
1997 		spdk_json_write_named_string(w, "host_uuid", info->registrants[i].host_uuid);
1998 		spdk_json_write_object_end(w);
1999 	}
2000 	spdk_json_write_array_end(w);
2001 	spdk_json_write_object_end(w);
2002 
2003 exit:
2004 	rc = spdk_json_write_end(w);
2005 	return rc;
2006 }
2007 
2008 static int
2009 nvmf_ns_update_reservation_info(struct spdk_nvmf_ns *ns)
2010 {
2011 	struct spdk_nvmf_reservation_info info;
2012 	struct spdk_nvmf_registrant *reg, *tmp;
2013 	uint32_t i = 0;
2014 
2015 	assert(ns != NULL);
2016 
2017 	if (!ns->bdev || !ns->ptpl_file) {
2018 		return 0;
2019 	}
2020 
2021 	memset(&info, 0, sizeof(info));
2022 	spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), spdk_bdev_get_uuid(ns->bdev));
2023 
2024 	if (ns->rtype) {
2025 		info.rtype = ns->rtype;
2026 		info.crkey = ns->crkey;
2027 		if (!nvmf_ns_reservation_all_registrants_type(ns)) {
2028 			assert(ns->holder != NULL);
2029 			spdk_uuid_fmt_lower(info.holder_uuid, sizeof(info.holder_uuid), &ns->holder->hostid);
2030 		}
2031 	}
2032 
2033 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2034 		spdk_uuid_fmt_lower(info.registrants[i].host_uuid, sizeof(info.registrants[i].host_uuid),
2035 				    &reg->hostid);
2036 		info.registrants[i++].rkey = reg->rkey;
2037 	}
2038 
2039 	info.num_regs = i;
2040 	info.ptpl_activated = ns->ptpl_activated;
2041 
2042 	return nvmf_ns_reservation_update(ns->ptpl_file, &info);
2043 }
2044 
2045 static struct spdk_nvmf_registrant *
2046 nvmf_ns_reservation_get_registrant(struct spdk_nvmf_ns *ns,
2047 				   struct spdk_uuid *uuid)
2048 {
2049 	struct spdk_nvmf_registrant *reg, *tmp;
2050 
2051 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2052 		if (!spdk_uuid_compare(&reg->hostid, uuid)) {
2053 			return reg;
2054 		}
2055 	}
2056 
2057 	return NULL;
2058 }
2059 
2060 /* Generate reservation notice log to registered HostID controllers */
2061 static void
2062 nvmf_subsystem_gen_ctrlr_notification(struct spdk_nvmf_subsystem *subsystem,
2063 				      struct spdk_nvmf_ns *ns,
2064 				      struct spdk_uuid *hostid_list,
2065 				      uint32_t num_hostid,
2066 				      enum spdk_nvme_reservation_notification_log_page_type type)
2067 {
2068 	struct spdk_nvmf_ctrlr *ctrlr;
2069 	uint32_t i;
2070 
2071 	for (i = 0; i < num_hostid; i++) {
2072 		TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
2073 			if (!spdk_uuid_compare(&ctrlr->hostid, &hostid_list[i])) {
2074 				nvmf_ctrlr_reservation_notice_log(ctrlr, ns, type);
2075 			}
2076 		}
2077 	}
2078 }
2079 
2080 /* Get all registrants' hostid other than the controller who issued the command */
2081 static uint32_t
2082 nvmf_ns_reservation_get_all_other_hostid(struct spdk_nvmf_ns *ns,
2083 		struct spdk_uuid *hostid_list,
2084 		uint32_t max_num_hostid,
2085 		struct spdk_uuid *current_hostid)
2086 {
2087 	struct spdk_nvmf_registrant *reg, *tmp;
2088 	uint32_t num_hostid = 0;
2089 
2090 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2091 		if (spdk_uuid_compare(&reg->hostid, current_hostid)) {
2092 			if (num_hostid == max_num_hostid) {
2093 				assert(false);
2094 				return max_num_hostid;
2095 			}
2096 			hostid_list[num_hostid++] = reg->hostid;
2097 		}
2098 	}
2099 
2100 	return num_hostid;
2101 }
2102 
2103 /* Calculate the unregistered HostID list according to list
2104  * prior to execute preempt command and list after executing
2105  * preempt command.
2106  */
2107 static uint32_t
2108 nvmf_ns_reservation_get_unregistered_hostid(struct spdk_uuid *old_hostid_list,
2109 		uint32_t old_num_hostid,
2110 		struct spdk_uuid *remaining_hostid_list,
2111 		uint32_t remaining_num_hostid)
2112 {
2113 	struct spdk_uuid temp_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2114 	uint32_t i, j, num_hostid = 0;
2115 	bool found;
2116 
2117 	if (!remaining_num_hostid) {
2118 		return old_num_hostid;
2119 	}
2120 
2121 	for (i = 0; i < old_num_hostid; i++) {
2122 		found = false;
2123 		for (j = 0; j < remaining_num_hostid; j++) {
2124 			if (!spdk_uuid_compare(&old_hostid_list[i], &remaining_hostid_list[j])) {
2125 				found = true;
2126 				break;
2127 			}
2128 		}
2129 		if (!found) {
2130 			spdk_uuid_copy(&temp_hostid_list[num_hostid++], &old_hostid_list[i]);
2131 		}
2132 	}
2133 
2134 	if (num_hostid) {
2135 		memcpy(old_hostid_list, temp_hostid_list, sizeof(struct spdk_uuid) * num_hostid);
2136 	}
2137 
2138 	return num_hostid;
2139 }
2140 
2141 /* current reservation type is all registrants or not */
2142 static bool
2143 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns)
2144 {
2145 	return (ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
2146 		ns->rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS);
2147 }
2148 
2149 /* current registrant is reservation holder or not */
2150 static bool
2151 nvmf_ns_reservation_registrant_is_holder(struct spdk_nvmf_ns *ns,
2152 		struct spdk_nvmf_registrant *reg)
2153 {
2154 	if (!reg) {
2155 		return false;
2156 	}
2157 
2158 	if (nvmf_ns_reservation_all_registrants_type(ns)) {
2159 		return true;
2160 	}
2161 
2162 	return (ns->holder == reg);
2163 }
2164 
2165 static int
2166 nvmf_ns_reservation_add_registrant(struct spdk_nvmf_ns *ns,
2167 				   struct spdk_nvmf_ctrlr *ctrlr,
2168 				   uint64_t nrkey)
2169 {
2170 	struct spdk_nvmf_registrant *reg;
2171 
2172 	reg = calloc(1, sizeof(*reg));
2173 	if (!reg) {
2174 		return -ENOMEM;
2175 	}
2176 
2177 	reg->rkey = nrkey;
2178 	/* set hostid for the registrant */
2179 	spdk_uuid_copy(&reg->hostid, &ctrlr->hostid);
2180 	TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
2181 	ns->gen++;
2182 
2183 	return 0;
2184 }
2185 
2186 static void
2187 nvmf_ns_reservation_release_reservation(struct spdk_nvmf_ns *ns)
2188 {
2189 	ns->rtype = 0;
2190 	ns->crkey = 0;
2191 	ns->holder = NULL;
2192 }
2193 
2194 /* release the reservation if the last registrant was removed */
2195 static void
2196 nvmf_ns_reservation_check_release_on_remove_registrant(struct spdk_nvmf_ns *ns,
2197 		struct spdk_nvmf_registrant *reg)
2198 {
2199 	struct spdk_nvmf_registrant *next_reg;
2200 
2201 	/* no reservation holder */
2202 	if (!ns->holder) {
2203 		assert(ns->rtype == 0);
2204 		return;
2205 	}
2206 
2207 	next_reg = TAILQ_FIRST(&ns->registrants);
2208 	if (next_reg && nvmf_ns_reservation_all_registrants_type(ns)) {
2209 		/* the next valid registrant is the new holder now */
2210 		ns->holder = next_reg;
2211 	} else if (nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
2212 		/* release the reservation */
2213 		nvmf_ns_reservation_release_reservation(ns);
2214 	}
2215 }
2216 
2217 static void
2218 nvmf_ns_reservation_remove_registrant(struct spdk_nvmf_ns *ns,
2219 				      struct spdk_nvmf_registrant *reg)
2220 {
2221 	TAILQ_REMOVE(&ns->registrants, reg, link);
2222 	nvmf_ns_reservation_check_release_on_remove_registrant(ns, reg);
2223 	free(reg);
2224 	ns->gen++;
2225 	return;
2226 }
2227 
2228 static uint32_t
2229 nvmf_ns_reservation_remove_registrants_by_key(struct spdk_nvmf_ns *ns,
2230 		uint64_t rkey)
2231 {
2232 	struct spdk_nvmf_registrant *reg, *tmp;
2233 	uint32_t count = 0;
2234 
2235 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2236 		if (reg->rkey == rkey) {
2237 			nvmf_ns_reservation_remove_registrant(ns, reg);
2238 			count++;
2239 		}
2240 	}
2241 	return count;
2242 }
2243 
2244 static uint32_t
2245 nvmf_ns_reservation_remove_all_other_registrants(struct spdk_nvmf_ns *ns,
2246 		struct spdk_nvmf_registrant *reg)
2247 {
2248 	struct spdk_nvmf_registrant *reg_tmp, *reg_tmp2;
2249 	uint32_t count = 0;
2250 
2251 	TAILQ_FOREACH_SAFE(reg_tmp, &ns->registrants, link, reg_tmp2) {
2252 		if (reg_tmp != reg) {
2253 			nvmf_ns_reservation_remove_registrant(ns, reg_tmp);
2254 			count++;
2255 		}
2256 	}
2257 	return count;
2258 }
2259 
2260 static uint32_t
2261 nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns)
2262 {
2263 	struct spdk_nvmf_registrant *reg, *reg_tmp;
2264 	uint32_t count = 0;
2265 
2266 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) {
2267 		nvmf_ns_reservation_remove_registrant(ns, reg);
2268 		count++;
2269 	}
2270 	return count;
2271 }
2272 
2273 static void
2274 nvmf_ns_reservation_acquire_reservation(struct spdk_nvmf_ns *ns, uint64_t rkey,
2275 					enum spdk_nvme_reservation_type rtype,
2276 					struct spdk_nvmf_registrant *holder)
2277 {
2278 	ns->rtype = rtype;
2279 	ns->crkey = rkey;
2280 	assert(ns->holder == NULL);
2281 	ns->holder = holder;
2282 }
2283 
2284 static bool
2285 nvmf_ns_reservation_register(struct spdk_nvmf_ns *ns,
2286 			     struct spdk_nvmf_ctrlr *ctrlr,
2287 			     struct spdk_nvmf_request *req)
2288 {
2289 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2290 	uint8_t rrega, iekey, cptpl, rtype;
2291 	struct spdk_nvme_reservation_register_data key;
2292 	struct spdk_nvmf_registrant *reg;
2293 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2294 	bool update_sgroup = false;
2295 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2296 	uint32_t num_hostid = 0;
2297 	int rc;
2298 
2299 	rrega = cmd->cdw10_bits.resv_register.rrega;
2300 	iekey = cmd->cdw10_bits.resv_register.iekey;
2301 	cptpl = cmd->cdw10_bits.resv_register.cptpl;
2302 
2303 	if (req->data && req->length >= sizeof(key)) {
2304 		memcpy(&key, req->data, sizeof(key));
2305 	} else {
2306 		SPDK_ERRLOG("No key provided. Failing request.\n");
2307 		status = SPDK_NVME_SC_INVALID_FIELD;
2308 		goto exit;
2309 	}
2310 
2311 	SPDK_DEBUGLOG(nvmf, "REGISTER: RREGA %u, IEKEY %u, CPTPL %u, "
2312 		      "NRKEY 0x%"PRIx64", NRKEY 0x%"PRIx64"\n",
2313 		      rrega, iekey, cptpl, key.crkey, key.nrkey);
2314 
2315 	if (cptpl == SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON) {
2316 		/* Ture to OFF state, and need to be updated in the configuration file */
2317 		if (ns->ptpl_activated) {
2318 			ns->ptpl_activated = 0;
2319 			update_sgroup = true;
2320 		}
2321 	} else if (cptpl == SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS) {
2322 		if (ns->ptpl_file == NULL) {
2323 			status = SPDK_NVME_SC_INVALID_FIELD;
2324 			goto exit;
2325 		} else if (ns->ptpl_activated == 0) {
2326 			ns->ptpl_activated = 1;
2327 			update_sgroup = true;
2328 		}
2329 	}
2330 
2331 	/* current Host Identifier has registrant or not */
2332 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2333 
2334 	switch (rrega) {
2335 	case SPDK_NVME_RESERVE_REGISTER_KEY:
2336 		if (!reg) {
2337 			/* register new controller */
2338 			if (key.nrkey == 0) {
2339 				SPDK_ERRLOG("Can't register zeroed new key\n");
2340 				status = SPDK_NVME_SC_INVALID_FIELD;
2341 				goto exit;
2342 			}
2343 			rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey);
2344 			if (rc < 0) {
2345 				status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2346 				goto exit;
2347 			}
2348 			update_sgroup = true;
2349 		} else {
2350 			/* register with same key is not an error */
2351 			if (reg->rkey != key.nrkey) {
2352 				SPDK_ERRLOG("The same host already register a "
2353 					    "key with 0x%"PRIx64"\n",
2354 					    reg->rkey);
2355 				status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2356 				goto exit;
2357 			}
2358 		}
2359 		break;
2360 	case SPDK_NVME_RESERVE_UNREGISTER_KEY:
2361 		if (!reg || (!iekey && reg->rkey != key.crkey)) {
2362 			SPDK_ERRLOG("No registrant or current key doesn't match "
2363 				    "with existing registrant key\n");
2364 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2365 			goto exit;
2366 		}
2367 
2368 		rtype = ns->rtype;
2369 		num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2370 				SPDK_NVMF_MAX_NUM_REGISTRANTS,
2371 				&ctrlr->hostid);
2372 
2373 		nvmf_ns_reservation_remove_registrant(ns, reg);
2374 
2375 		if (!ns->rtype && num_hostid && (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY ||
2376 						 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY)) {
2377 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2378 							      hostid_list,
2379 							      num_hostid,
2380 							      SPDK_NVME_RESERVATION_RELEASED);
2381 		}
2382 		update_sgroup = true;
2383 		break;
2384 	case SPDK_NVME_RESERVE_REPLACE_KEY:
2385 		if (!reg || (!iekey && reg->rkey != key.crkey)) {
2386 			SPDK_ERRLOG("No registrant or current key doesn't match "
2387 				    "with existing registrant key\n");
2388 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2389 			goto exit;
2390 		}
2391 		if (key.nrkey == 0) {
2392 			SPDK_ERRLOG("Can't register zeroed new key\n");
2393 			status = SPDK_NVME_SC_INVALID_FIELD;
2394 			goto exit;
2395 		}
2396 		reg->rkey = key.nrkey;
2397 		update_sgroup = true;
2398 		break;
2399 	default:
2400 		status = SPDK_NVME_SC_INVALID_FIELD;
2401 		goto exit;
2402 	}
2403 
2404 exit:
2405 	if (update_sgroup) {
2406 		rc = nvmf_ns_update_reservation_info(ns);
2407 		if (rc != 0) {
2408 			status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2409 		}
2410 	}
2411 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2412 	req->rsp->nvme_cpl.status.sc = status;
2413 	return update_sgroup;
2414 }
2415 
2416 static bool
2417 nvmf_ns_reservation_acquire(struct spdk_nvmf_ns *ns,
2418 			    struct spdk_nvmf_ctrlr *ctrlr,
2419 			    struct spdk_nvmf_request *req)
2420 {
2421 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2422 	uint8_t racqa, iekey, rtype;
2423 	struct spdk_nvme_reservation_acquire_data key;
2424 	struct spdk_nvmf_registrant *reg;
2425 	bool all_regs = false;
2426 	uint32_t count = 0;
2427 	bool update_sgroup = true;
2428 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2429 	uint32_t num_hostid = 0;
2430 	struct spdk_uuid new_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2431 	uint32_t new_num_hostid = 0;
2432 	bool reservation_released = false;
2433 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2434 
2435 	racqa = cmd->cdw10_bits.resv_acquire.racqa;
2436 	iekey = cmd->cdw10_bits.resv_acquire.iekey;
2437 	rtype = cmd->cdw10_bits.resv_acquire.rtype;
2438 
2439 	if (req->data && req->length >= sizeof(key)) {
2440 		memcpy(&key, req->data, sizeof(key));
2441 	} else {
2442 		SPDK_ERRLOG("No key provided. Failing request.\n");
2443 		status = SPDK_NVME_SC_INVALID_FIELD;
2444 		goto exit;
2445 	}
2446 
2447 	SPDK_DEBUGLOG(nvmf, "ACQUIRE: RACQA %u, IEKEY %u, RTYPE %u, "
2448 		      "NRKEY 0x%"PRIx64", PRKEY 0x%"PRIx64"\n",
2449 		      racqa, iekey, rtype, key.crkey, key.prkey);
2450 
2451 	if (iekey || rtype > SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) {
2452 		SPDK_ERRLOG("Ignore existing key field set to 1\n");
2453 		status = SPDK_NVME_SC_INVALID_FIELD;
2454 		update_sgroup = false;
2455 		goto exit;
2456 	}
2457 
2458 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2459 	/* must be registrant and CRKEY must match */
2460 	if (!reg || reg->rkey != key.crkey) {
2461 		SPDK_ERRLOG("No registrant or current key doesn't match "
2462 			    "with existing registrant key\n");
2463 		status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2464 		update_sgroup = false;
2465 		goto exit;
2466 	}
2467 
2468 	all_regs = nvmf_ns_reservation_all_registrants_type(ns);
2469 
2470 	switch (racqa) {
2471 	case SPDK_NVME_RESERVE_ACQUIRE:
2472 		/* it's not an error for the holder to acquire same reservation type again */
2473 		if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && ns->rtype == rtype) {
2474 			/* do nothing */
2475 			update_sgroup = false;
2476 		} else if (ns->holder == NULL) {
2477 			/* fisrt time to acquire the reservation */
2478 			nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
2479 		} else {
2480 			SPDK_ERRLOG("Invalid rtype or current registrant is not holder\n");
2481 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2482 			update_sgroup = false;
2483 			goto exit;
2484 		}
2485 		break;
2486 	case SPDK_NVME_RESERVE_PREEMPT:
2487 		/* no reservation holder */
2488 		if (!ns->holder) {
2489 			/* unregister with PRKEY */
2490 			nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2491 			break;
2492 		}
2493 		num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2494 				SPDK_NVMF_MAX_NUM_REGISTRANTS,
2495 				&ctrlr->hostid);
2496 
2497 		/* only 1 reservation holder and reservation key is valid */
2498 		if (!all_regs) {
2499 			/* preempt itself */
2500 			if (nvmf_ns_reservation_registrant_is_holder(ns, reg) &&
2501 			    ns->crkey == key.prkey) {
2502 				ns->rtype = rtype;
2503 				reservation_released = true;
2504 				break;
2505 			}
2506 
2507 			if (ns->crkey == key.prkey) {
2508 				nvmf_ns_reservation_remove_registrant(ns, ns->holder);
2509 				nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
2510 				reservation_released = true;
2511 			} else if (key.prkey != 0) {
2512 				nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2513 			} else {
2514 				/* PRKEY is zero */
2515 				SPDK_ERRLOG("Current PRKEY is zero\n");
2516 				status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2517 				update_sgroup = false;
2518 				goto exit;
2519 			}
2520 		} else {
2521 			/* release all other registrants except for the current one */
2522 			if (key.prkey == 0) {
2523 				nvmf_ns_reservation_remove_all_other_registrants(ns, reg);
2524 				assert(ns->holder == reg);
2525 			} else {
2526 				count = nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2527 				if (count == 0) {
2528 					SPDK_ERRLOG("PRKEY doesn't match any registrant\n");
2529 					status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2530 					update_sgroup = false;
2531 					goto exit;
2532 				}
2533 			}
2534 		}
2535 		break;
2536 	default:
2537 		status = SPDK_NVME_SC_INVALID_FIELD;
2538 		update_sgroup = false;
2539 		break;
2540 	}
2541 
2542 exit:
2543 	if (update_sgroup && racqa == SPDK_NVME_RESERVE_PREEMPT) {
2544 		new_num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, new_hostid_list,
2545 				 SPDK_NVMF_MAX_NUM_REGISTRANTS,
2546 				 &ctrlr->hostid);
2547 		/* Preempt notification occurs on the unregistered controllers
2548 		 * other than the controller who issued the command.
2549 		 */
2550 		num_hostid = nvmf_ns_reservation_get_unregistered_hostid(hostid_list,
2551 				num_hostid,
2552 				new_hostid_list,
2553 				new_num_hostid);
2554 		if (num_hostid) {
2555 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2556 							      hostid_list,
2557 							      num_hostid,
2558 							      SPDK_NVME_REGISTRATION_PREEMPTED);
2559 
2560 		}
2561 		/* Reservation released notification occurs on the
2562 		 * controllers which are the remaining registrants other than
2563 		 * the controller who issued the command.
2564 		 */
2565 		if (reservation_released && new_num_hostid) {
2566 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2567 							      new_hostid_list,
2568 							      new_num_hostid,
2569 							      SPDK_NVME_RESERVATION_RELEASED);
2570 
2571 		}
2572 	}
2573 	if (update_sgroup && ns->ptpl_activated) {
2574 		if (nvmf_ns_update_reservation_info(ns)) {
2575 			status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2576 		}
2577 	}
2578 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2579 	req->rsp->nvme_cpl.status.sc = status;
2580 	return update_sgroup;
2581 }
2582 
2583 static bool
2584 nvmf_ns_reservation_release(struct spdk_nvmf_ns *ns,
2585 			    struct spdk_nvmf_ctrlr *ctrlr,
2586 			    struct spdk_nvmf_request *req)
2587 {
2588 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2589 	uint8_t rrela, iekey, rtype;
2590 	struct spdk_nvmf_registrant *reg;
2591 	uint64_t crkey;
2592 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2593 	bool update_sgroup = true;
2594 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2595 	uint32_t num_hostid = 0;
2596 
2597 	rrela = cmd->cdw10_bits.resv_release.rrela;
2598 	iekey = cmd->cdw10_bits.resv_release.iekey;
2599 	rtype = cmd->cdw10_bits.resv_release.rtype;
2600 
2601 	if (req->data && req->length >= sizeof(crkey)) {
2602 		memcpy(&crkey, req->data, sizeof(crkey));
2603 	} else {
2604 		SPDK_ERRLOG("No key provided. Failing request.\n");
2605 		status = SPDK_NVME_SC_INVALID_FIELD;
2606 		goto exit;
2607 	}
2608 
2609 	SPDK_DEBUGLOG(nvmf, "RELEASE: RRELA %u, IEKEY %u, RTYPE %u, "
2610 		      "CRKEY 0x%"PRIx64"\n",  rrela, iekey, rtype, crkey);
2611 
2612 	if (iekey) {
2613 		SPDK_ERRLOG("Ignore existing key field set to 1\n");
2614 		status = SPDK_NVME_SC_INVALID_FIELD;
2615 		update_sgroup = false;
2616 		goto exit;
2617 	}
2618 
2619 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2620 	if (!reg || reg->rkey != crkey) {
2621 		SPDK_ERRLOG("No registrant or current key doesn't match "
2622 			    "with existing registrant key\n");
2623 		status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2624 		update_sgroup = false;
2625 		goto exit;
2626 	}
2627 
2628 	num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2629 			SPDK_NVMF_MAX_NUM_REGISTRANTS,
2630 			&ctrlr->hostid);
2631 
2632 	switch (rrela) {
2633 	case SPDK_NVME_RESERVE_RELEASE:
2634 		if (!ns->holder) {
2635 			SPDK_DEBUGLOG(nvmf, "RELEASE: no holder\n");
2636 			update_sgroup = false;
2637 			goto exit;
2638 		}
2639 		if (ns->rtype != rtype) {
2640 			SPDK_ERRLOG("Type doesn't match\n");
2641 			status = SPDK_NVME_SC_INVALID_FIELD;
2642 			update_sgroup = false;
2643 			goto exit;
2644 		}
2645 		if (!nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
2646 			/* not the reservation holder, this isn't an error */
2647 			update_sgroup = false;
2648 			goto exit;
2649 		}
2650 
2651 		rtype = ns->rtype;
2652 		nvmf_ns_reservation_release_reservation(ns);
2653 
2654 		if (num_hostid && rtype != SPDK_NVME_RESERVE_WRITE_EXCLUSIVE &&
2655 		    rtype != SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2656 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2657 							      hostid_list,
2658 							      num_hostid,
2659 							      SPDK_NVME_RESERVATION_RELEASED);
2660 		}
2661 		break;
2662 	case SPDK_NVME_RESERVE_CLEAR:
2663 		nvmf_ns_reservation_clear_all_registrants(ns);
2664 		if (num_hostid) {
2665 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2666 							      hostid_list,
2667 							      num_hostid,
2668 							      SPDK_NVME_RESERVATION_PREEMPTED);
2669 		}
2670 		break;
2671 	default:
2672 		status = SPDK_NVME_SC_INVALID_FIELD;
2673 		update_sgroup = false;
2674 		goto exit;
2675 	}
2676 
2677 exit:
2678 	if (update_sgroup && ns->ptpl_activated) {
2679 		if (nvmf_ns_update_reservation_info(ns)) {
2680 			status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2681 		}
2682 	}
2683 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2684 	req->rsp->nvme_cpl.status.sc = status;
2685 	return update_sgroup;
2686 }
2687 
2688 static void
2689 nvmf_ns_reservation_report(struct spdk_nvmf_ns *ns,
2690 			   struct spdk_nvmf_ctrlr *ctrlr,
2691 			   struct spdk_nvmf_request *req)
2692 {
2693 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2694 	struct spdk_nvmf_registrant *reg, *tmp;
2695 	struct spdk_nvme_reservation_status_extended_data *status_data;
2696 	struct spdk_nvme_registered_ctrlr_extended_data *ctrlr_data;
2697 	uint8_t *payload;
2698 	uint32_t transfer_len, payload_len = 0;
2699 	uint32_t regctl = 0;
2700 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2701 
2702 	if (req->data == NULL) {
2703 		SPDK_ERRLOG("No data transfer specified for request. "
2704 			    " Unable to transfer back response.\n");
2705 		status = SPDK_NVME_SC_INVALID_FIELD;
2706 		goto exit;
2707 	}
2708 
2709 	if (!cmd->cdw11_bits.resv_report.eds) {
2710 		SPDK_ERRLOG("NVMeoF uses extended controller data structure, "
2711 			    "please set EDS bit in cdw11 and try again\n");
2712 		status = SPDK_NVME_SC_HOSTID_INCONSISTENT_FORMAT;
2713 		goto exit;
2714 	}
2715 
2716 	/* Number of Dwords of the Reservation Status data structure to transfer */
2717 	transfer_len = (cmd->cdw10 + 1) * sizeof(uint32_t);
2718 	payload = req->data;
2719 
2720 	if (transfer_len < sizeof(struct spdk_nvme_reservation_status_extended_data)) {
2721 		status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2722 		goto exit;
2723 	}
2724 
2725 	status_data = (struct spdk_nvme_reservation_status_extended_data *)payload;
2726 	status_data->data.gen = ns->gen;
2727 	status_data->data.rtype = ns->rtype;
2728 	status_data->data.ptpls = ns->ptpl_activated;
2729 	payload_len += sizeof(struct spdk_nvme_reservation_status_extended_data);
2730 
2731 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2732 		payload_len += sizeof(struct spdk_nvme_registered_ctrlr_extended_data);
2733 		if (payload_len > transfer_len) {
2734 			break;
2735 		}
2736 
2737 		ctrlr_data = (struct spdk_nvme_registered_ctrlr_extended_data *)
2738 			     (payload + sizeof(*status_data) + sizeof(*ctrlr_data) * regctl);
2739 		/* Set to 0xffffh for dynamic controller */
2740 		ctrlr_data->cntlid = 0xffff;
2741 		ctrlr_data->rcsts.status = (ns->holder == reg) ? true : false;
2742 		ctrlr_data->rkey = reg->rkey;
2743 		spdk_uuid_copy((struct spdk_uuid *)ctrlr_data->hostid, &reg->hostid);
2744 		regctl++;
2745 	}
2746 	status_data->data.regctl = regctl;
2747 
2748 exit:
2749 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2750 	req->rsp->nvme_cpl.status.sc = status;
2751 	return;
2752 }
2753 
2754 static void
2755 nvmf_ns_reservation_complete(void *ctx)
2756 {
2757 	struct spdk_nvmf_request *req = ctx;
2758 
2759 	spdk_nvmf_request_complete(req);
2760 }
2761 
2762 static void
2763 _nvmf_ns_reservation_update_done(struct spdk_nvmf_subsystem *subsystem,
2764 				 void *cb_arg, int status)
2765 {
2766 	struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)cb_arg;
2767 	struct spdk_nvmf_poll_group *group = req->qpair->group;
2768 
2769 	spdk_thread_send_msg(group->thread, nvmf_ns_reservation_complete, req);
2770 }
2771 
2772 void
2773 nvmf_ns_reservation_request(void *ctx)
2774 {
2775 	struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)ctx;
2776 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2777 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2778 	struct subsystem_update_ns_ctx *update_ctx;
2779 	uint32_t nsid;
2780 	struct spdk_nvmf_ns *ns;
2781 	bool update_sgroup = false;
2782 
2783 	nsid = cmd->nsid;
2784 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
2785 	assert(ns != NULL);
2786 
2787 	switch (cmd->opc) {
2788 	case SPDK_NVME_OPC_RESERVATION_REGISTER:
2789 		update_sgroup = nvmf_ns_reservation_register(ns, ctrlr, req);
2790 		break;
2791 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2792 		update_sgroup = nvmf_ns_reservation_acquire(ns, ctrlr, req);
2793 		break;
2794 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2795 		update_sgroup = nvmf_ns_reservation_release(ns, ctrlr, req);
2796 		break;
2797 	case SPDK_NVME_OPC_RESERVATION_REPORT:
2798 		nvmf_ns_reservation_report(ns, ctrlr, req);
2799 		break;
2800 	default:
2801 		break;
2802 	}
2803 
2804 	/* update reservation information to subsystem's poll group */
2805 	if (update_sgroup) {
2806 		update_ctx = calloc(1, sizeof(*update_ctx));
2807 		if (update_ctx == NULL) {
2808 			SPDK_ERRLOG("Can't alloc subsystem poll group update context\n");
2809 			goto update_done;
2810 		}
2811 		update_ctx->subsystem = ctrlr->subsys;
2812 		update_ctx->cb_fn = _nvmf_ns_reservation_update_done;
2813 		update_ctx->cb_arg = req;
2814 
2815 		nvmf_subsystem_update_ns(ctrlr->subsys, subsystem_update_ns_done, update_ctx);
2816 		return;
2817 	}
2818 
2819 update_done:
2820 	_nvmf_ns_reservation_update_done(ctrlr->subsys, (void *)req, 0);
2821 }
2822 
2823 int
2824 spdk_nvmf_subsystem_set_ana_reporting(struct spdk_nvmf_subsystem *subsystem,
2825 				      bool ana_reporting)
2826 {
2827 	if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
2828 		return -EAGAIN;
2829 	}
2830 
2831 	subsystem->flags.ana_reporting = ana_reporting;
2832 
2833 	return 0;
2834 }
2835 
2836 struct subsystem_listener_update_ctx {
2837 	struct spdk_nvmf_subsystem_listener *listener;
2838 
2839 	spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn;
2840 	void *cb_arg;
2841 };
2842 
2843 static void
2844 subsystem_listener_update_done(struct spdk_io_channel_iter *i, int status)
2845 {
2846 	struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2847 
2848 	if (ctx->cb_fn) {
2849 		ctx->cb_fn(ctx->cb_arg, status);
2850 	}
2851 	free(ctx);
2852 }
2853 
2854 static void
2855 subsystem_listener_update_on_pg(struct spdk_io_channel_iter *i)
2856 {
2857 	struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2858 	struct spdk_nvmf_subsystem_listener *listener;
2859 	struct spdk_nvmf_poll_group *group;
2860 	struct spdk_nvmf_ctrlr *ctrlr;
2861 
2862 	listener = ctx->listener;
2863 	group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
2864 
2865 	TAILQ_FOREACH(ctrlr, &listener->subsystem->ctrlrs, link) {
2866 		if (ctrlr->admin_qpair->group == group && ctrlr->listener == listener) {
2867 			nvmf_ctrlr_async_event_ana_change_notice(ctrlr);
2868 		}
2869 	}
2870 
2871 	spdk_for_each_channel_continue(i, 0);
2872 }
2873 
2874 void
2875 nvmf_subsystem_set_ana_state(struct spdk_nvmf_subsystem *subsystem,
2876 			     const struct spdk_nvme_transport_id *trid,
2877 			     enum spdk_nvme_ana_state ana_state,
2878 			     spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, void *cb_arg)
2879 {
2880 	struct spdk_nvmf_subsystem_listener *listener;
2881 	struct subsystem_listener_update_ctx *ctx;
2882 
2883 	assert(cb_fn != NULL);
2884 	assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
2885 	       subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED);
2886 
2887 	if (!subsystem->flags.ana_reporting) {
2888 		SPDK_ERRLOG("ANA reporting is disabled\n");
2889 		cb_fn(cb_arg, -EINVAL);
2890 		return;
2891 	}
2892 
2893 	/* ANA Change state is not used, ANA Persistent Loss state
2894 	 * is not supported yet.
2895 	 */
2896 	if (!(ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE ||
2897 	      ana_state == SPDK_NVME_ANA_NON_OPTIMIZED_STATE ||
2898 	      ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE)) {
2899 		SPDK_ERRLOG("ANA state %d is not supported\n", ana_state);
2900 		cb_fn(cb_arg, -ENOTSUP);
2901 		return;
2902 	}
2903 
2904 	listener = nvmf_subsystem_find_listener(subsystem, trid);
2905 	if (!listener) {
2906 		SPDK_ERRLOG("Unable to find listener.\n");
2907 		cb_fn(cb_arg, -EINVAL);
2908 		return;
2909 	}
2910 
2911 	if (listener->ana_state == ana_state) {
2912 		cb_fn(cb_arg, 0);
2913 		return;
2914 	}
2915 
2916 	ctx = calloc(1, sizeof(*ctx));
2917 	if (!ctx) {
2918 		SPDK_ERRLOG("Unable to allocate context\n");
2919 		cb_fn(cb_arg, -ENOMEM);
2920 		return;
2921 	}
2922 
2923 	listener->ana_state = ana_state;
2924 	listener->ana_state_change_count++;
2925 
2926 	ctx->listener = listener;
2927 	ctx->cb_fn = cb_fn;
2928 	ctx->cb_arg = cb_arg;
2929 
2930 	spdk_for_each_channel(subsystem->tgt,
2931 			      subsystem_listener_update_on_pg,
2932 			      ctx,
2933 			      subsystem_listener_update_done);
2934 }
2935