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