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