xref: /spdk/lib/nvmf/subsystem.c (revision b02581a89058ebaebe03bd0e16e3b58adfe406c1)
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 	FILE *fd;
2229 	size_t json_size;
2230 	ssize_t values_cnt, rc;
2231 	void *json = NULL, *end;
2232 	struct spdk_json_val *values = NULL;
2233 	struct _nvmf_ns_reservation res = {};
2234 	const char *file = ns->ptpl_file;
2235 	uint32_t i;
2236 
2237 	fd = fopen(file, "r");
2238 	/* It's not an error if the file does not exist */
2239 	if (!fd) {
2240 		SPDK_NOTICELOG("File %s does not exist\n", file);
2241 		return 0;
2242 	}
2243 
2244 	/* Load all persist file contents into a local buffer */
2245 	json = spdk_posix_file_load(fd, &json_size);
2246 	fclose(fd);
2247 	if (!json) {
2248 		SPDK_ERRLOG("Load persit file %s failed\n", file);
2249 		return -ENOMEM;
2250 	}
2251 
2252 	rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0);
2253 	if (rc < 0) {
2254 		SPDK_NOTICELOG("Parsing JSON configuration failed (%zd)\n", rc);
2255 		goto exit;
2256 	}
2257 
2258 	values_cnt = rc;
2259 	values = calloc(values_cnt, sizeof(struct spdk_json_val));
2260 	if (values == NULL) {
2261 		goto exit;
2262 	}
2263 
2264 	rc = spdk_json_parse(json, json_size, values, values_cnt, &end, 0);
2265 	if (rc != values_cnt) {
2266 		SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
2267 		goto exit;
2268 	}
2269 
2270 	/* Decode json */
2271 	if (spdk_json_decode_object(values, nvmf_ns_pr_decoders,
2272 				    SPDK_COUNTOF(nvmf_ns_pr_decoders),
2273 				    &res)) {
2274 		SPDK_ERRLOG("Invalid objects in the persist file %s\n", file);
2275 		rc = -EINVAL;
2276 		goto exit;
2277 	}
2278 
2279 	if (res.regs.num_regs > SPDK_NVMF_MAX_NUM_REGISTRANTS) {
2280 		SPDK_ERRLOG("Can only support up to %u registrants\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
2281 		rc = -ERANGE;
2282 		goto exit;
2283 	}
2284 
2285 	rc = 0;
2286 	info->ptpl_activated = res.ptpl_activated;
2287 	info->rtype = res.rtype;
2288 	info->crkey = res.crkey;
2289 	snprintf(info->bdev_uuid, sizeof(info->bdev_uuid), "%s", res.bdev_uuid);
2290 	snprintf(info->holder_uuid, sizeof(info->holder_uuid), "%s", res.holder_uuid);
2291 	info->num_regs = res.regs.num_regs;
2292 	for (i = 0; i < res.regs.num_regs; i++) {
2293 		info->registrants[i].rkey = res.regs.reg[i].rkey;
2294 		snprintf(info->registrants[i].host_uuid, sizeof(info->registrants[i].host_uuid), "%s",
2295 			 res.regs.reg[i].host_uuid);
2296 	}
2297 
2298 exit:
2299 	free(json);
2300 	free(values);
2301 	free(res.bdev_uuid);
2302 	free(res.holder_uuid);
2303 	for (i = 0; i < res.regs.num_regs; i++) {
2304 		free(res.regs.reg[i].host_uuid);
2305 	}
2306 
2307 	return rc;
2308 }
2309 
2310 static bool nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns);
2311 
2312 static int
2313 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
2314 {
2315 	uint32_t i;
2316 	struct spdk_nvmf_registrant *reg, *holder = NULL;
2317 	struct spdk_uuid bdev_uuid, holder_uuid;
2318 	bool rkey_flag = false;
2319 
2320 	SPDK_DEBUGLOG(nvmf, "NSID %u, PTPL %u, Number of registrants %u\n",
2321 		      ns->nsid, info->ptpl_activated, info->num_regs);
2322 
2323 	/* it's not an error */
2324 	if (!info->ptpl_activated || !info->num_regs) {
2325 		return 0;
2326 	}
2327 
2328 	/* Check info->crkey exist or not in info->registrants[i].rkey */
2329 	for (i = 0; i < info->num_regs; i++) {
2330 		if (info->crkey == info->registrants[i].rkey) {
2331 			rkey_flag = true;
2332 		}
2333 	}
2334 	if (!rkey_flag && info->crkey != 0) {
2335 		return -EINVAL;
2336 	}
2337 
2338 	spdk_uuid_parse(&bdev_uuid, info->bdev_uuid);
2339 	if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) {
2340 		SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n");
2341 		return -EINVAL;
2342 	}
2343 
2344 	ns->crkey = info->crkey;
2345 	ns->rtype = info->rtype;
2346 	ns->ptpl_activated = info->ptpl_activated;
2347 	spdk_uuid_parse(&holder_uuid, info->holder_uuid);
2348 
2349 	SPDK_DEBUGLOG(nvmf, "Bdev UUID %s\n", info->bdev_uuid);
2350 	if (info->rtype) {
2351 		SPDK_DEBUGLOG(nvmf, "Holder UUID %s, RTYPE %u, RKEY 0x%"PRIx64"\n",
2352 			      info->holder_uuid, info->rtype, info->crkey);
2353 	}
2354 
2355 	for (i = 0; i < info->num_regs; i++) {
2356 		reg = calloc(1, sizeof(*reg));
2357 		if (!reg) {
2358 			return -ENOMEM;
2359 		}
2360 		spdk_uuid_parse(&reg->hostid, info->registrants[i].host_uuid);
2361 		reg->rkey = info->registrants[i].rkey;
2362 		TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
2363 		if (info->crkey != 0 && !spdk_uuid_compare(&holder_uuid, &reg->hostid)) {
2364 			holder = reg;
2365 		}
2366 		SPDK_DEBUGLOG(nvmf, "Registrant RKEY 0x%"PRIx64", Host UUID %s\n",
2367 			      info->registrants[i].rkey, info->registrants[i].host_uuid);
2368 	}
2369 
2370 	if (nvmf_ns_reservation_all_registrants_type(ns)) {
2371 		ns->holder = TAILQ_FIRST(&ns->registrants);
2372 	} else {
2373 		ns->holder = holder;
2374 	}
2375 
2376 	return 0;
2377 }
2378 
2379 static int
2380 nvmf_ns_json_write_cb(void *cb_ctx, const void *data, size_t size)
2381 {
2382 	char *file = cb_ctx;
2383 	size_t rc;
2384 	FILE *fd;
2385 
2386 	fd = fopen(file, "w");
2387 	if (!fd) {
2388 		SPDK_ERRLOG("Can't open file %s for write\n", file);
2389 		return -ENOENT;
2390 	}
2391 	rc = fwrite(data, 1, size, fd);
2392 	fclose(fd);
2393 
2394 	return rc == size ? 0 : -1;
2395 }
2396 
2397 static int
2398 nvmf_ns_reservation_update_json(const struct spdk_nvmf_ns *ns,
2399 				const struct spdk_nvmf_reservation_info *info)
2400 {
2401 	const char *file = ns->ptpl_file;
2402 	struct spdk_json_write_ctx *w;
2403 	uint32_t i;
2404 	int rc = 0;
2405 
2406 	w = spdk_json_write_begin(nvmf_ns_json_write_cb, (void *)file, 0);
2407 	if (w == NULL) {
2408 		return -ENOMEM;
2409 	}
2410 	/* clear the configuration file */
2411 	if (!info->ptpl_activated) {
2412 		goto exit;
2413 	}
2414 
2415 	spdk_json_write_object_begin(w);
2416 	spdk_json_write_named_bool(w, "ptpl", info->ptpl_activated);
2417 	spdk_json_write_named_uint32(w, "rtype", info->rtype);
2418 	spdk_json_write_named_uint64(w, "crkey", info->crkey);
2419 	spdk_json_write_named_string(w, "bdev_uuid", info->bdev_uuid);
2420 	spdk_json_write_named_string(w, "holder_uuid", info->holder_uuid);
2421 
2422 	spdk_json_write_named_array_begin(w, "registrants");
2423 	for (i = 0; i < info->num_regs; i++) {
2424 		spdk_json_write_object_begin(w);
2425 		spdk_json_write_named_uint64(w, "rkey", info->registrants[i].rkey);
2426 		spdk_json_write_named_string(w, "host_uuid", info->registrants[i].host_uuid);
2427 		spdk_json_write_object_end(w);
2428 	}
2429 	spdk_json_write_array_end(w);
2430 	spdk_json_write_object_end(w);
2431 
2432 exit:
2433 	rc = spdk_json_write_end(w);
2434 	return rc;
2435 }
2436 
2437 static int
2438 nvmf_ns_update_reservation_info(struct spdk_nvmf_ns *ns)
2439 {
2440 	struct spdk_nvmf_reservation_info info;
2441 	struct spdk_nvmf_registrant *reg, *tmp;
2442 	uint32_t i = 0;
2443 
2444 	assert(ns != NULL);
2445 
2446 	if (!ns->bdev || !nvmf_ns_is_ptpl_capable(ns)) {
2447 		return 0;
2448 	}
2449 
2450 	memset(&info, 0, sizeof(info));
2451 	spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), spdk_bdev_get_uuid(ns->bdev));
2452 
2453 	if (ns->rtype) {
2454 		info.rtype = ns->rtype;
2455 		info.crkey = ns->crkey;
2456 		if (!nvmf_ns_reservation_all_registrants_type(ns)) {
2457 			assert(ns->holder != NULL);
2458 			spdk_uuid_fmt_lower(info.holder_uuid, sizeof(info.holder_uuid), &ns->holder->hostid);
2459 		}
2460 	}
2461 
2462 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2463 		spdk_uuid_fmt_lower(info.registrants[i].host_uuid, sizeof(info.registrants[i].host_uuid),
2464 				    &reg->hostid);
2465 		info.registrants[i++].rkey = reg->rkey;
2466 	}
2467 
2468 	info.num_regs = i;
2469 	info.ptpl_activated = ns->ptpl_activated;
2470 
2471 	return nvmf_ns_reservation_update(ns, &info);
2472 }
2473 
2474 static struct spdk_nvmf_registrant *
2475 nvmf_ns_reservation_get_registrant(struct spdk_nvmf_ns *ns,
2476 				   struct spdk_uuid *uuid)
2477 {
2478 	struct spdk_nvmf_registrant *reg, *tmp;
2479 
2480 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2481 		if (!spdk_uuid_compare(&reg->hostid, uuid)) {
2482 			return reg;
2483 		}
2484 	}
2485 
2486 	return NULL;
2487 }
2488 
2489 /* Generate reservation notice log to registered HostID controllers */
2490 static void
2491 nvmf_subsystem_gen_ctrlr_notification(struct spdk_nvmf_subsystem *subsystem,
2492 				      struct spdk_nvmf_ns *ns,
2493 				      struct spdk_uuid *hostid_list,
2494 				      uint32_t num_hostid,
2495 				      enum spdk_nvme_reservation_notification_log_page_type type)
2496 {
2497 	struct spdk_nvmf_ctrlr *ctrlr;
2498 	uint32_t i;
2499 
2500 	for (i = 0; i < num_hostid; i++) {
2501 		TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
2502 			if (!spdk_uuid_compare(&ctrlr->hostid, &hostid_list[i])) {
2503 				nvmf_ctrlr_reservation_notice_log(ctrlr, ns, type);
2504 			}
2505 		}
2506 	}
2507 }
2508 
2509 /* Get all registrants' hostid other than the controller who issued the command */
2510 static uint32_t
2511 nvmf_ns_reservation_get_all_other_hostid(struct spdk_nvmf_ns *ns,
2512 		struct spdk_uuid *hostid_list,
2513 		uint32_t max_num_hostid,
2514 		struct spdk_uuid *current_hostid)
2515 {
2516 	struct spdk_nvmf_registrant *reg, *tmp;
2517 	uint32_t num_hostid = 0;
2518 
2519 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2520 		if (spdk_uuid_compare(&reg->hostid, current_hostid)) {
2521 			if (num_hostid == max_num_hostid) {
2522 				assert(false);
2523 				return max_num_hostid;
2524 			}
2525 			hostid_list[num_hostid++] = reg->hostid;
2526 		}
2527 	}
2528 
2529 	return num_hostid;
2530 }
2531 
2532 /* Calculate the unregistered HostID list according to list
2533  * prior to execute preempt command and list after executing
2534  * preempt command.
2535  */
2536 static uint32_t
2537 nvmf_ns_reservation_get_unregistered_hostid(struct spdk_uuid *old_hostid_list,
2538 		uint32_t old_num_hostid,
2539 		struct spdk_uuid *remaining_hostid_list,
2540 		uint32_t remaining_num_hostid)
2541 {
2542 	struct spdk_uuid temp_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2543 	uint32_t i, j, num_hostid = 0;
2544 	bool found;
2545 
2546 	if (!remaining_num_hostid) {
2547 		return old_num_hostid;
2548 	}
2549 
2550 	for (i = 0; i < old_num_hostid; i++) {
2551 		found = false;
2552 		for (j = 0; j < remaining_num_hostid; j++) {
2553 			if (!spdk_uuid_compare(&old_hostid_list[i], &remaining_hostid_list[j])) {
2554 				found = true;
2555 				break;
2556 			}
2557 		}
2558 		if (!found) {
2559 			spdk_uuid_copy(&temp_hostid_list[num_hostid++], &old_hostid_list[i]);
2560 		}
2561 	}
2562 
2563 	if (num_hostid) {
2564 		memcpy(old_hostid_list, temp_hostid_list, sizeof(struct spdk_uuid) * num_hostid);
2565 	}
2566 
2567 	return num_hostid;
2568 }
2569 
2570 /* current reservation type is all registrants or not */
2571 static bool
2572 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns)
2573 {
2574 	return (ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
2575 		ns->rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS);
2576 }
2577 
2578 /* current registrant is reservation holder or not */
2579 static bool
2580 nvmf_ns_reservation_registrant_is_holder(struct spdk_nvmf_ns *ns,
2581 		struct spdk_nvmf_registrant *reg)
2582 {
2583 	if (!reg) {
2584 		return false;
2585 	}
2586 
2587 	if (nvmf_ns_reservation_all_registrants_type(ns)) {
2588 		return true;
2589 	}
2590 
2591 	return (ns->holder == reg);
2592 }
2593 
2594 static int
2595 nvmf_ns_reservation_add_registrant(struct spdk_nvmf_ns *ns,
2596 				   struct spdk_nvmf_ctrlr *ctrlr,
2597 				   uint64_t nrkey)
2598 {
2599 	struct spdk_nvmf_registrant *reg;
2600 
2601 	reg = calloc(1, sizeof(*reg));
2602 	if (!reg) {
2603 		return -ENOMEM;
2604 	}
2605 
2606 	reg->rkey = nrkey;
2607 	/* set hostid for the registrant */
2608 	spdk_uuid_copy(&reg->hostid, &ctrlr->hostid);
2609 	TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
2610 	ns->gen++;
2611 
2612 	return 0;
2613 }
2614 
2615 static void
2616 nvmf_ns_reservation_release_reservation(struct spdk_nvmf_ns *ns)
2617 {
2618 	ns->rtype = 0;
2619 	ns->crkey = 0;
2620 	ns->holder = NULL;
2621 }
2622 
2623 /* release the reservation if the last registrant was removed */
2624 static void
2625 nvmf_ns_reservation_check_release_on_remove_registrant(struct spdk_nvmf_ns *ns,
2626 		struct spdk_nvmf_registrant *reg)
2627 {
2628 	struct spdk_nvmf_registrant *next_reg;
2629 
2630 	/* no reservation holder */
2631 	if (!ns->holder) {
2632 		assert(ns->rtype == 0);
2633 		return;
2634 	}
2635 
2636 	next_reg = TAILQ_FIRST(&ns->registrants);
2637 	if (next_reg && nvmf_ns_reservation_all_registrants_type(ns)) {
2638 		/* the next valid registrant is the new holder now */
2639 		ns->holder = next_reg;
2640 	} else if (nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
2641 		/* release the reservation */
2642 		nvmf_ns_reservation_release_reservation(ns);
2643 	}
2644 }
2645 
2646 static void
2647 nvmf_ns_reservation_remove_registrant(struct spdk_nvmf_ns *ns,
2648 				      struct spdk_nvmf_registrant *reg)
2649 {
2650 	TAILQ_REMOVE(&ns->registrants, reg, link);
2651 	nvmf_ns_reservation_check_release_on_remove_registrant(ns, reg);
2652 	free(reg);
2653 	ns->gen++;
2654 	return;
2655 }
2656 
2657 static uint32_t
2658 nvmf_ns_reservation_remove_registrants_by_key(struct spdk_nvmf_ns *ns,
2659 		uint64_t rkey)
2660 {
2661 	struct spdk_nvmf_registrant *reg, *tmp;
2662 	uint32_t count = 0;
2663 
2664 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2665 		if (reg->rkey == rkey) {
2666 			nvmf_ns_reservation_remove_registrant(ns, reg);
2667 			count++;
2668 		}
2669 	}
2670 	return count;
2671 }
2672 
2673 static uint32_t
2674 nvmf_ns_reservation_remove_all_other_registrants(struct spdk_nvmf_ns *ns,
2675 		struct spdk_nvmf_registrant *reg)
2676 {
2677 	struct spdk_nvmf_registrant *reg_tmp, *reg_tmp2;
2678 	uint32_t count = 0;
2679 
2680 	TAILQ_FOREACH_SAFE(reg_tmp, &ns->registrants, link, reg_tmp2) {
2681 		if (reg_tmp != reg) {
2682 			nvmf_ns_reservation_remove_registrant(ns, reg_tmp);
2683 			count++;
2684 		}
2685 	}
2686 	return count;
2687 }
2688 
2689 static uint32_t
2690 nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns)
2691 {
2692 	struct spdk_nvmf_registrant *reg, *reg_tmp;
2693 	uint32_t count = 0;
2694 
2695 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) {
2696 		nvmf_ns_reservation_remove_registrant(ns, reg);
2697 		count++;
2698 	}
2699 	return count;
2700 }
2701 
2702 static void
2703 nvmf_ns_reservation_acquire_reservation(struct spdk_nvmf_ns *ns, uint64_t rkey,
2704 					enum spdk_nvme_reservation_type rtype,
2705 					struct spdk_nvmf_registrant *holder)
2706 {
2707 	ns->rtype = rtype;
2708 	ns->crkey = rkey;
2709 	assert(ns->holder == NULL);
2710 	ns->holder = holder;
2711 }
2712 
2713 static bool
2714 nvmf_ns_reservation_register(struct spdk_nvmf_ns *ns,
2715 			     struct spdk_nvmf_ctrlr *ctrlr,
2716 			     struct spdk_nvmf_request *req)
2717 {
2718 	struct spdk_nvme_reservation_register_data key = { 0 };
2719 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2720 	uint8_t rrega, iekey, cptpl, rtype;
2721 	struct spdk_nvmf_registrant *reg;
2722 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2723 	bool update_sgroup = false;
2724 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2725 	uint32_t num_hostid = 0;
2726 	int rc;
2727 
2728 	rrega = cmd->cdw10_bits.resv_register.rrega;
2729 	iekey = cmd->cdw10_bits.resv_register.iekey;
2730 	cptpl = cmd->cdw10_bits.resv_register.cptpl;
2731 
2732 	if (req->iovcnt > 0 && req->length >= sizeof(key)) {
2733 		struct spdk_iov_xfer ix;
2734 		spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
2735 		spdk_iov_xfer_to_buf(&ix, &key, sizeof(key));
2736 	} else {
2737 		SPDK_ERRLOG("No key provided. Failing request.\n");
2738 		status = SPDK_NVME_SC_INVALID_FIELD;
2739 		goto exit;
2740 	}
2741 
2742 	SPDK_DEBUGLOG(nvmf, "REGISTER: RREGA %u, IEKEY %u, CPTPL %u, "
2743 		      "NRKEY 0x%"PRIx64", NRKEY 0x%"PRIx64"\n",
2744 		      rrega, iekey, cptpl, key.crkey, key.nrkey);
2745 
2746 	if (cptpl == SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON) {
2747 		/* Ture to OFF state, and need to be updated in the configuration file */
2748 		if (ns->ptpl_activated) {
2749 			ns->ptpl_activated = 0;
2750 			update_sgroup = true;
2751 		}
2752 	} else if (cptpl == SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS) {
2753 		if (!nvmf_ns_is_ptpl_capable(ns)) {
2754 			status = SPDK_NVME_SC_INVALID_FIELD;
2755 			goto exit;
2756 		} else if (ns->ptpl_activated == 0) {
2757 			ns->ptpl_activated = 1;
2758 			update_sgroup = true;
2759 		}
2760 	}
2761 
2762 	/* current Host Identifier has registrant or not */
2763 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2764 
2765 	switch (rrega) {
2766 	case SPDK_NVME_RESERVE_REGISTER_KEY:
2767 		if (!reg) {
2768 			/* register new controller */
2769 			if (key.nrkey == 0) {
2770 				SPDK_ERRLOG("Can't register zeroed new key\n");
2771 				status = SPDK_NVME_SC_INVALID_FIELD;
2772 				goto exit;
2773 			}
2774 			rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey);
2775 			if (rc < 0) {
2776 				status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2777 				goto exit;
2778 			}
2779 			update_sgroup = true;
2780 		} else {
2781 			/* register with same key is not an error */
2782 			if (reg->rkey != key.nrkey) {
2783 				SPDK_ERRLOG("The same host already register a "
2784 					    "key with 0x%"PRIx64"\n",
2785 					    reg->rkey);
2786 				status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2787 				goto exit;
2788 			}
2789 		}
2790 		break;
2791 	case SPDK_NVME_RESERVE_UNREGISTER_KEY:
2792 		if (!reg || (!iekey && reg->rkey != key.crkey)) {
2793 			SPDK_ERRLOG("No registrant or current key doesn't match "
2794 				    "with existing registrant key\n");
2795 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2796 			goto exit;
2797 		}
2798 
2799 		rtype = ns->rtype;
2800 		num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2801 				SPDK_NVMF_MAX_NUM_REGISTRANTS,
2802 				&ctrlr->hostid);
2803 
2804 		nvmf_ns_reservation_remove_registrant(ns, reg);
2805 
2806 		if (!ns->rtype && num_hostid && (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY ||
2807 						 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY)) {
2808 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2809 							      hostid_list,
2810 							      num_hostid,
2811 							      SPDK_NVME_RESERVATION_RELEASED);
2812 		}
2813 		update_sgroup = true;
2814 		break;
2815 	case SPDK_NVME_RESERVE_REPLACE_KEY:
2816 		if (key.nrkey == 0) {
2817 			SPDK_ERRLOG("Can't register zeroed new key\n");
2818 			status = SPDK_NVME_SC_INVALID_FIELD;
2819 			goto exit;
2820 		}
2821 		/* Registrant exists */
2822 		if (reg) {
2823 			if (!iekey && reg->rkey != key.crkey) {
2824 				SPDK_ERRLOG("Current key doesn't match "
2825 					    "existing registrant key\n");
2826 				status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2827 				goto exit;
2828 			}
2829 			if (reg->rkey == key.nrkey) {
2830 				goto exit;
2831 			}
2832 			reg->rkey = key.nrkey;
2833 		} else if (iekey) { /* No registrant but IEKEY is set */
2834 			/* new registrant */
2835 			rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey);
2836 			if (rc < 0) {
2837 				status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2838 				goto exit;
2839 			}
2840 		} else { /* No registrant */
2841 			SPDK_ERRLOG("No registrant\n");
2842 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2843 			goto exit;
2844 
2845 		}
2846 		update_sgroup = true;
2847 		break;
2848 	default:
2849 		status = SPDK_NVME_SC_INVALID_FIELD;
2850 		goto exit;
2851 	}
2852 
2853 exit:
2854 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2855 	req->rsp->nvme_cpl.status.sc = status;
2856 	return update_sgroup;
2857 }
2858 
2859 static bool
2860 nvmf_ns_reservation_acquire(struct spdk_nvmf_ns *ns,
2861 			    struct spdk_nvmf_ctrlr *ctrlr,
2862 			    struct spdk_nvmf_request *req)
2863 {
2864 	struct spdk_nvme_reservation_acquire_data key = { 0 };
2865 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2866 	uint8_t racqa, iekey, rtype;
2867 	struct spdk_nvmf_registrant *reg;
2868 	bool all_regs = false;
2869 	uint32_t count = 0;
2870 	bool update_sgroup = true;
2871 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2872 	uint32_t num_hostid = 0;
2873 	struct spdk_uuid new_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2874 	uint32_t new_num_hostid = 0;
2875 	bool reservation_released = false;
2876 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2877 
2878 	racqa = cmd->cdw10_bits.resv_acquire.racqa;
2879 	iekey = cmd->cdw10_bits.resv_acquire.iekey;
2880 	rtype = cmd->cdw10_bits.resv_acquire.rtype;
2881 
2882 	if (req->iovcnt > 0 && req->length >= sizeof(key)) {
2883 		struct spdk_iov_xfer ix;
2884 		spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
2885 		spdk_iov_xfer_to_buf(&ix, &key, sizeof(key));
2886 	} else {
2887 		SPDK_ERRLOG("No key provided. Failing request.\n");
2888 		status = SPDK_NVME_SC_INVALID_FIELD;
2889 		goto exit;
2890 	}
2891 
2892 	SPDK_DEBUGLOG(nvmf, "ACQUIRE: RACQA %u, IEKEY %u, RTYPE %u, "
2893 		      "NRKEY 0x%"PRIx64", PRKEY 0x%"PRIx64"\n",
2894 		      racqa, iekey, rtype, key.crkey, key.prkey);
2895 
2896 	if (iekey || rtype > SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) {
2897 		SPDK_ERRLOG("Ignore existing key field set to 1\n");
2898 		status = SPDK_NVME_SC_INVALID_FIELD;
2899 		update_sgroup = false;
2900 		goto exit;
2901 	}
2902 
2903 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2904 	/* must be registrant and CRKEY must match */
2905 	if (!reg || reg->rkey != key.crkey) {
2906 		SPDK_ERRLOG("No registrant or current key doesn't match "
2907 			    "with existing registrant key\n");
2908 		status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2909 		update_sgroup = false;
2910 		goto exit;
2911 	}
2912 
2913 	all_regs = nvmf_ns_reservation_all_registrants_type(ns);
2914 
2915 	switch (racqa) {
2916 	case SPDK_NVME_RESERVE_ACQUIRE:
2917 		/* it's not an error for the holder to acquire same reservation type again */
2918 		if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && ns->rtype == rtype) {
2919 			/* do nothing */
2920 			update_sgroup = false;
2921 		} else if (ns->holder == NULL) {
2922 			/* first time to acquire the reservation */
2923 			nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
2924 		} else {
2925 			SPDK_ERRLOG("Invalid rtype or current registrant is not holder\n");
2926 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2927 			update_sgroup = false;
2928 			goto exit;
2929 		}
2930 		break;
2931 	case SPDK_NVME_RESERVE_PREEMPT:
2932 		/* no reservation holder */
2933 		if (!ns->holder) {
2934 			/* unregister with PRKEY */
2935 			nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2936 			break;
2937 		}
2938 		num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2939 				SPDK_NVMF_MAX_NUM_REGISTRANTS,
2940 				&ctrlr->hostid);
2941 
2942 		/* only 1 reservation holder and reservation key is valid */
2943 		if (!all_regs) {
2944 			/* preempt itself */
2945 			if (nvmf_ns_reservation_registrant_is_holder(ns, reg) &&
2946 			    ns->crkey == key.prkey) {
2947 				ns->rtype = rtype;
2948 				reservation_released = true;
2949 				break;
2950 			}
2951 
2952 			if (ns->crkey == key.prkey) {
2953 				nvmf_ns_reservation_remove_registrant(ns, ns->holder);
2954 				nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
2955 				reservation_released = true;
2956 			} else if (key.prkey != 0) {
2957 				nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2958 			} else {
2959 				/* PRKEY is zero */
2960 				SPDK_ERRLOG("Current PRKEY is zero\n");
2961 				status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2962 				update_sgroup = false;
2963 				goto exit;
2964 			}
2965 		} else {
2966 			/* release all other registrants except for the current one */
2967 			if (key.prkey == 0) {
2968 				nvmf_ns_reservation_remove_all_other_registrants(ns, reg);
2969 				assert(ns->holder == reg);
2970 			} else {
2971 				count = nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2972 				if (count == 0) {
2973 					SPDK_ERRLOG("PRKEY doesn't match any registrant\n");
2974 					status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2975 					update_sgroup = false;
2976 					goto exit;
2977 				}
2978 			}
2979 		}
2980 		break;
2981 	default:
2982 		status = SPDK_NVME_SC_INVALID_FIELD;
2983 		update_sgroup = false;
2984 		break;
2985 	}
2986 
2987 exit:
2988 	if (update_sgroup && racqa == SPDK_NVME_RESERVE_PREEMPT) {
2989 		new_num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, new_hostid_list,
2990 				 SPDK_NVMF_MAX_NUM_REGISTRANTS,
2991 				 &ctrlr->hostid);
2992 		/* Preempt notification occurs on the unregistered controllers
2993 		 * other than the controller who issued the command.
2994 		 */
2995 		num_hostid = nvmf_ns_reservation_get_unregistered_hostid(hostid_list,
2996 				num_hostid,
2997 				new_hostid_list,
2998 				new_num_hostid);
2999 		if (num_hostid) {
3000 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3001 							      hostid_list,
3002 							      num_hostid,
3003 							      SPDK_NVME_REGISTRATION_PREEMPTED);
3004 
3005 		}
3006 		/* Reservation released notification occurs on the
3007 		 * controllers which are the remaining registrants other than
3008 		 * the controller who issued the command.
3009 		 */
3010 		if (reservation_released && new_num_hostid) {
3011 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3012 							      new_hostid_list,
3013 							      new_num_hostid,
3014 							      SPDK_NVME_RESERVATION_RELEASED);
3015 
3016 		}
3017 	}
3018 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3019 	req->rsp->nvme_cpl.status.sc = status;
3020 	return update_sgroup;
3021 }
3022 
3023 static bool
3024 nvmf_ns_reservation_release(struct spdk_nvmf_ns *ns,
3025 			    struct spdk_nvmf_ctrlr *ctrlr,
3026 			    struct spdk_nvmf_request *req)
3027 {
3028 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3029 	uint8_t rrela, iekey, rtype;
3030 	struct spdk_nvmf_registrant *reg;
3031 	uint64_t crkey = 0;
3032 	uint8_t status = SPDK_NVME_SC_SUCCESS;
3033 	bool update_sgroup = true;
3034 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
3035 	uint32_t num_hostid = 0;
3036 
3037 	rrela = cmd->cdw10_bits.resv_release.rrela;
3038 	iekey = cmd->cdw10_bits.resv_release.iekey;
3039 	rtype = cmd->cdw10_bits.resv_release.rtype;
3040 
3041 	if (req->iovcnt > 0 && req->length >= sizeof(crkey)) {
3042 		struct spdk_iov_xfer ix;
3043 		spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3044 		spdk_iov_xfer_to_buf(&ix, &crkey, sizeof(crkey));
3045 	} else {
3046 		SPDK_ERRLOG("No key provided. Failing request.\n");
3047 		status = SPDK_NVME_SC_INVALID_FIELD;
3048 		goto exit;
3049 	}
3050 
3051 	SPDK_DEBUGLOG(nvmf, "RELEASE: RRELA %u, IEKEY %u, RTYPE %u, "
3052 		      "CRKEY 0x%"PRIx64"\n",  rrela, iekey, rtype, crkey);
3053 
3054 	if (iekey) {
3055 		SPDK_ERRLOG("Ignore existing key field set to 1\n");
3056 		status = SPDK_NVME_SC_INVALID_FIELD;
3057 		update_sgroup = false;
3058 		goto exit;
3059 	}
3060 
3061 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
3062 	if (!reg || reg->rkey != crkey) {
3063 		SPDK_ERRLOG("No registrant or current key doesn't match "
3064 			    "with existing registrant key\n");
3065 		status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3066 		update_sgroup = false;
3067 		goto exit;
3068 	}
3069 
3070 	num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
3071 			SPDK_NVMF_MAX_NUM_REGISTRANTS,
3072 			&ctrlr->hostid);
3073 
3074 	switch (rrela) {
3075 	case SPDK_NVME_RESERVE_RELEASE:
3076 		if (!ns->holder) {
3077 			SPDK_DEBUGLOG(nvmf, "RELEASE: no holder\n");
3078 			update_sgroup = false;
3079 			goto exit;
3080 		}
3081 		if (ns->rtype != rtype) {
3082 			SPDK_ERRLOG("Type doesn't match\n");
3083 			status = SPDK_NVME_SC_INVALID_FIELD;
3084 			update_sgroup = false;
3085 			goto exit;
3086 		}
3087 		if (!nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
3088 			/* not the reservation holder, this isn't an error */
3089 			update_sgroup = false;
3090 			goto exit;
3091 		}
3092 
3093 		rtype = ns->rtype;
3094 		nvmf_ns_reservation_release_reservation(ns);
3095 
3096 		if (num_hostid && rtype != SPDK_NVME_RESERVE_WRITE_EXCLUSIVE &&
3097 		    rtype != SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
3098 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3099 							      hostid_list,
3100 							      num_hostid,
3101 							      SPDK_NVME_RESERVATION_RELEASED);
3102 		}
3103 		break;
3104 	case SPDK_NVME_RESERVE_CLEAR:
3105 		nvmf_ns_reservation_clear_all_registrants(ns);
3106 		if (num_hostid) {
3107 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3108 							      hostid_list,
3109 							      num_hostid,
3110 							      SPDK_NVME_RESERVATION_PREEMPTED);
3111 		}
3112 		break;
3113 	default:
3114 		status = SPDK_NVME_SC_INVALID_FIELD;
3115 		update_sgroup = false;
3116 		goto exit;
3117 	}
3118 
3119 exit:
3120 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3121 	req->rsp->nvme_cpl.status.sc = status;
3122 	return update_sgroup;
3123 }
3124 
3125 static void
3126 nvmf_ns_reservation_report(struct spdk_nvmf_ns *ns,
3127 			   struct spdk_nvmf_ctrlr *ctrlr,
3128 			   struct spdk_nvmf_request *req)
3129 {
3130 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3131 	struct spdk_nvmf_registrant *reg, *tmp;
3132 	struct spdk_nvme_reservation_status_extended_data status_data = { 0 };
3133 	struct spdk_iov_xfer ix;
3134 	uint32_t transfer_len;
3135 	uint32_t regctl = 0;
3136 	uint8_t status = SPDK_NVME_SC_SUCCESS;
3137 
3138 	if (req->iovcnt == 0) {
3139 		SPDK_ERRLOG("No data transfer specified for request. "
3140 			    " Unable to transfer back response.\n");
3141 		status = SPDK_NVME_SC_INVALID_FIELD;
3142 		goto exit;
3143 	}
3144 
3145 	if (!cmd->cdw11_bits.resv_report.eds) {
3146 		SPDK_ERRLOG("NVMeoF uses extended controller data structure, "
3147 			    "please set EDS bit in cdw11 and try again\n");
3148 		status = SPDK_NVME_SC_HOSTID_INCONSISTENT_FORMAT;
3149 		goto exit;
3150 	}
3151 
3152 	/* Number of Dwords of the Reservation Status data structure to transfer */
3153 	transfer_len = (cmd->cdw10 + 1) * sizeof(uint32_t);
3154 
3155 	if (transfer_len < sizeof(struct spdk_nvme_reservation_status_extended_data)) {
3156 		status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3157 		goto exit;
3158 	}
3159 
3160 	spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3161 
3162 	status_data.data.gen = ns->gen;
3163 	status_data.data.rtype = ns->rtype;
3164 	status_data.data.ptpls = ns->ptpl_activated;
3165 
3166 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
3167 		regctl++;
3168 	}
3169 
3170 	/*
3171 	 * We report the number of registrants as per the spec here, even if
3172 	 * the iov isn't big enough to contain them all. In that case, the
3173 	 * spdk_iov_xfer_from_buf() won't actually copy any of the remaining
3174 	 * data; as it keeps track of the iov cursor itself, it's simplest to
3175 	 * just walk the entire list anyway.
3176 	 */
3177 	status_data.data.regctl = regctl;
3178 
3179 	spdk_iov_xfer_from_buf(&ix, &status_data, sizeof(status_data));
3180 
3181 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
3182 		struct spdk_nvme_registered_ctrlr_extended_data ctrlr_data = { 0 };
3183 
3184 		/* Set to 0xffffh for dynamic controller */
3185 		ctrlr_data.cntlid = 0xffff;
3186 		ctrlr_data.rcsts.status = (ns->holder == reg) ? true : false;
3187 		ctrlr_data.rkey = reg->rkey;
3188 		spdk_uuid_copy((struct spdk_uuid *)ctrlr_data.hostid, &reg->hostid);
3189 
3190 		spdk_iov_xfer_from_buf(&ix, &ctrlr_data, sizeof(ctrlr_data));
3191 	}
3192 
3193 exit:
3194 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3195 	req->rsp->nvme_cpl.status.sc = status;
3196 	return;
3197 }
3198 
3199 static void
3200 nvmf_ns_reservation_complete(void *ctx)
3201 {
3202 	struct spdk_nvmf_request *req = ctx;
3203 
3204 	spdk_nvmf_request_complete(req);
3205 }
3206 
3207 static void
3208 _nvmf_ns_reservation_update_done(struct spdk_nvmf_subsystem *subsystem,
3209 				 void *cb_arg, int status)
3210 {
3211 	struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)cb_arg;
3212 	struct spdk_nvmf_poll_group *group = req->qpair->group;
3213 
3214 	spdk_thread_send_msg(group->thread, nvmf_ns_reservation_complete, req);
3215 }
3216 
3217 void
3218 nvmf_ns_reservation_request(void *ctx)
3219 {
3220 	struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)ctx;
3221 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3222 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
3223 	uint32_t nsid;
3224 	struct spdk_nvmf_ns *ns;
3225 	bool update_sgroup = false;
3226 	int status = 0;
3227 
3228 	nsid = cmd->nsid;
3229 	ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
3230 	assert(ns != NULL);
3231 
3232 	switch (cmd->opc) {
3233 	case SPDK_NVME_OPC_RESERVATION_REGISTER:
3234 		update_sgroup = nvmf_ns_reservation_register(ns, ctrlr, req);
3235 		break;
3236 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
3237 		update_sgroup = nvmf_ns_reservation_acquire(ns, ctrlr, req);
3238 		break;
3239 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
3240 		update_sgroup = nvmf_ns_reservation_release(ns, ctrlr, req);
3241 		break;
3242 	case SPDK_NVME_OPC_RESERVATION_REPORT:
3243 		nvmf_ns_reservation_report(ns, ctrlr, req);
3244 		break;
3245 	default:
3246 		break;
3247 	}
3248 
3249 	/* update reservation information to subsystem's poll group */
3250 	if (update_sgroup) {
3251 		if (ns->ptpl_activated || cmd->opc == SPDK_NVME_OPC_RESERVATION_REGISTER) {
3252 			if (nvmf_ns_update_reservation_info(ns) != 0) {
3253 				req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3254 			}
3255 		}
3256 		status = nvmf_subsystem_update_ns(ctrlr->subsys, _nvmf_ns_reservation_update_done, req);
3257 		if (status == 0) {
3258 			return;
3259 		}
3260 	}
3261 
3262 	_nvmf_ns_reservation_update_done(ctrlr->subsys, req, status);
3263 }
3264 
3265 static bool
3266 nvmf_ns_is_ptpl_capable_json(const struct spdk_nvmf_ns *ns)
3267 {
3268 	return ns->ptpl_file != NULL;
3269 }
3270 
3271 static struct spdk_nvmf_ns_reservation_ops g_reservation_ops = {
3272 	.is_ptpl_capable = nvmf_ns_is_ptpl_capable_json,
3273 	.update = nvmf_ns_reservation_update_json,
3274 	.load = nvmf_ns_reservation_load_json,
3275 };
3276 
3277 bool
3278 nvmf_ns_is_ptpl_capable(const struct spdk_nvmf_ns *ns)
3279 {
3280 	return g_reservation_ops.is_ptpl_capable(ns);
3281 }
3282 
3283 static int
3284 nvmf_ns_reservation_update(const struct spdk_nvmf_ns *ns,
3285 			   const struct spdk_nvmf_reservation_info *info)
3286 {
3287 	return g_reservation_ops.update(ns, info);
3288 }
3289 
3290 static int
3291 nvmf_ns_reservation_load(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
3292 {
3293 	return g_reservation_ops.load(ns, info);
3294 }
3295 
3296 void
3297 spdk_nvmf_set_custom_ns_reservation_ops(const struct spdk_nvmf_ns_reservation_ops *ops)
3298 {
3299 	g_reservation_ops = *ops;
3300 }
3301 
3302 int
3303 spdk_nvmf_subsystem_set_ana_reporting(struct spdk_nvmf_subsystem *subsystem,
3304 				      bool ana_reporting)
3305 {
3306 	if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
3307 		return -EAGAIN;
3308 	}
3309 
3310 	subsystem->flags.ana_reporting = ana_reporting;
3311 
3312 	return 0;
3313 }
3314 
3315 bool
3316 spdk_nvmf_subsystem_get_ana_reporting(struct spdk_nvmf_subsystem *subsystem)
3317 {
3318 	return subsystem->flags.ana_reporting;
3319 }
3320 
3321 struct subsystem_listener_update_ctx {
3322 	struct spdk_nvmf_subsystem_listener *listener;
3323 
3324 	spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn;
3325 	void *cb_arg;
3326 };
3327 
3328 static void
3329 subsystem_listener_update_done(struct spdk_io_channel_iter *i, int status)
3330 {
3331 	struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
3332 
3333 	if (ctx->cb_fn) {
3334 		ctx->cb_fn(ctx->cb_arg, status);
3335 	}
3336 	free(ctx);
3337 }
3338 
3339 static void
3340 subsystem_listener_update_on_pg(struct spdk_io_channel_iter *i)
3341 {
3342 	struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
3343 	struct spdk_nvmf_subsystem_listener *listener;
3344 	struct spdk_nvmf_poll_group *group;
3345 	struct spdk_nvmf_ctrlr *ctrlr;
3346 
3347 	listener = ctx->listener;
3348 	group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
3349 
3350 	TAILQ_FOREACH(ctrlr, &listener->subsystem->ctrlrs, link) {
3351 		if (ctrlr->thread != spdk_get_thread()) {
3352 			continue;
3353 		}
3354 
3355 		if (ctrlr->admin_qpair && ctrlr->admin_qpair->group == group && ctrlr->listener == listener) {
3356 			nvmf_ctrlr_async_event_ana_change_notice(ctrlr);
3357 		}
3358 	}
3359 
3360 	spdk_for_each_channel_continue(i, 0);
3361 }
3362 
3363 void
3364 spdk_nvmf_subsystem_set_ana_state(struct spdk_nvmf_subsystem *subsystem,
3365 				  const struct spdk_nvme_transport_id *trid,
3366 				  enum spdk_nvme_ana_state ana_state, uint32_t anagrpid,
3367 				  spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, void *cb_arg)
3368 {
3369 	struct spdk_nvmf_subsystem_listener *listener;
3370 	struct subsystem_listener_update_ctx *ctx;
3371 	uint32_t i;
3372 
3373 	assert(cb_fn != NULL);
3374 	assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
3375 	       subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED);
3376 
3377 	if (!subsystem->flags.ana_reporting) {
3378 		SPDK_ERRLOG("ANA reporting is disabled\n");
3379 		cb_fn(cb_arg, -EINVAL);
3380 		return;
3381 	}
3382 
3383 	/* ANA Change state is not used, ANA Persistent Loss state
3384 	 * is not supported yet.
3385 	 */
3386 	if (!(ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE ||
3387 	      ana_state == SPDK_NVME_ANA_NON_OPTIMIZED_STATE ||
3388 	      ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE)) {
3389 		SPDK_ERRLOG("ANA state %d is not supported\n", ana_state);
3390 		cb_fn(cb_arg, -ENOTSUP);
3391 		return;
3392 	}
3393 
3394 	if (anagrpid > subsystem->max_nsid) {
3395 		SPDK_ERRLOG("ANA group ID %" PRIu32 " is more than maximum\n", anagrpid);
3396 		cb_fn(cb_arg, -EINVAL);
3397 		return;
3398 	}
3399 
3400 	listener = nvmf_subsystem_find_listener(subsystem, trid);
3401 	if (!listener) {
3402 		SPDK_ERRLOG("Unable to find listener.\n");
3403 		cb_fn(cb_arg, -EINVAL);
3404 		return;
3405 	}
3406 
3407 	if (anagrpid != 0 && listener->ana_state[anagrpid - 1] == ana_state) {
3408 		cb_fn(cb_arg, 0);
3409 		return;
3410 	}
3411 
3412 	ctx = calloc(1, sizeof(*ctx));
3413 	if (!ctx) {
3414 		SPDK_ERRLOG("Unable to allocate context\n");
3415 		cb_fn(cb_arg, -ENOMEM);
3416 		return;
3417 	}
3418 
3419 	for (i = 1; i <= subsystem->max_nsid; i++) {
3420 		if (anagrpid == 0 || i == anagrpid) {
3421 			listener->ana_state[i - 1] = ana_state;
3422 		}
3423 	}
3424 	listener->ana_state_change_count++;
3425 
3426 	ctx->listener = listener;
3427 	ctx->cb_fn = cb_fn;
3428 	ctx->cb_arg = cb_arg;
3429 
3430 	spdk_for_each_channel(subsystem->tgt,
3431 			      subsystem_listener_update_on_pg,
3432 			      ctx,
3433 			      subsystem_listener_update_done);
3434 }
3435 
3436 bool
3437 spdk_nvmf_subsystem_is_discovery(struct spdk_nvmf_subsystem *subsystem)
3438 {
3439 	return subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT ||
3440 	       subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY;
3441 }
3442 
3443 bool
3444 nvmf_nqn_is_discovery(const char *nqn)
3445 {
3446 	return strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN) == 0;
3447 }
3448