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