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