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