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