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