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