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