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