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