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