xref: /spdk/lib/nvmf/subsystem.c (revision 7ed0ec6832d9a5cf49bdc35f8e9c00fa80a5f67b)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "nvmf_internal.h"
37 #include "transport.h"
38 
39 #include "spdk/event.h"
40 #include "spdk/likely.h"
41 #include "spdk/string.h"
42 #include "spdk/trace.h"
43 #include "spdk/nvmf_spec.h"
44 #include "spdk/uuid.h"
45 #include "spdk/json.h"
46 #include "spdk/file.h"
47 
48 #include "spdk/bdev_module.h"
49 #include "spdk_internal/log.h"
50 #include "spdk_internal/utf.h"
51 
52 #define MODEL_NUMBER_DEFAULT "SPDK bdev Controller"
53 
54 /*
55  * States for parsing valid domains in NQNs according to RFC 1034
56  */
57 enum spdk_nvmf_nqn_domain_states {
58 	/* First character of a domain must be a letter */
59 	SPDK_NVMF_DOMAIN_ACCEPT_LETTER = 0,
60 
61 	/* Subsequent characters can be any of letter, digit, or hyphen */
62 	SPDK_NVMF_DOMAIN_ACCEPT_LDH = 1,
63 
64 	/* A domain label must end with either a letter or digit */
65 	SPDK_NVMF_DOMAIN_ACCEPT_ANY = 2
66 };
67 
68 /* Returns true if is a valid ASCII string as defined by the NVMe spec */
69 static bool
70 spdk_nvmf_valid_ascii_string(const void *buf, size_t size)
71 {
72 	const uint8_t *str = buf;
73 	size_t i;
74 
75 	for (i = 0; i < size; i++) {
76 		if (str[i] < 0x20 || str[i] > 0x7E) {
77 			return false;
78 		}
79 	}
80 
81 	return true;
82 }
83 
84 static bool
85 spdk_nvmf_valid_nqn(const char *nqn)
86 {
87 	size_t len;
88 	struct spdk_uuid uuid_value;
89 	uint32_t i;
90 	int bytes_consumed;
91 	uint32_t domain_label_length;
92 	char *reverse_domain_end;
93 	uint32_t reverse_domain_end_index;
94 	enum spdk_nvmf_nqn_domain_states domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER;
95 
96 	/* Check for length requirements */
97 	len = strlen(nqn);
98 	if (len > SPDK_NVMF_NQN_MAX_LEN) {
99 		SPDK_ERRLOG("Invalid NQN \"%s\": length %zu > max %d\n", nqn, len, SPDK_NVMF_NQN_MAX_LEN);
100 		return false;
101 	}
102 
103 	/* The nqn must be at least as long as SPDK_NVMF_NQN_MIN_LEN to contain the necessary prefix. */
104 	if (len < SPDK_NVMF_NQN_MIN_LEN) {
105 		SPDK_ERRLOG("Invalid NQN \"%s\": length %zu < min %d\n", nqn, len, SPDK_NVMF_NQN_MIN_LEN);
106 		return false;
107 	}
108 
109 	/* Check for discovery controller nqn */
110 	if (!strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN)) {
111 		return true;
112 	}
113 
114 	/* Check for equality with the generic nqn structure of the form "nqn.2014-08.org.nvmexpress:uuid:11111111-2222-3333-4444-555555555555" */
115 	if (!strncmp(nqn, SPDK_NVMF_NQN_UUID_PRE, SPDK_NVMF_NQN_UUID_PRE_LEN)) {
116 		if (len != SPDK_NVMF_NQN_UUID_PRE_LEN + SPDK_NVMF_UUID_STRING_LEN) {
117 			SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not the correct length\n", nqn);
118 			return false;
119 		}
120 
121 		if (spdk_uuid_parse(&uuid_value, &nqn[SPDK_NVMF_NQN_UUID_PRE_LEN])) {
122 			SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not formatted correctly\n", nqn);
123 			return false;
124 		}
125 		return true;
126 	}
127 
128 	/* If the nqn does not match the uuid structure, the next several checks validate the form "nqn.yyyy-mm.reverse.domain:user-string" */
129 
130 	if (strncmp(nqn, "nqn.", 4) != 0) {
131 		SPDK_ERRLOG("Invalid NQN \"%s\": NQN must begin with \"nqn.\".\n", nqn);
132 		return false;
133 	}
134 
135 	/* Check for yyyy-mm. */
136 	if (!(isdigit(nqn[4]) && isdigit(nqn[5]) && isdigit(nqn[6]) && isdigit(nqn[7]) &&
137 	      nqn[8] == '-' && isdigit(nqn[9]) && isdigit(nqn[10]) && nqn[11] == '.')) {
138 		SPDK_ERRLOG("Invalid date code in NQN \"%s\"\n", nqn);
139 		return false;
140 	}
141 
142 	reverse_domain_end = strchr(nqn, ':');
143 	if (reverse_domain_end != NULL && (reverse_domain_end_index = reverse_domain_end - nqn) < len - 1) {
144 	} else {
145 		SPDK_ERRLOG("Invalid NQN \"%s\". NQN must contain user specified name with a ':' as a prefix.\n",
146 			    nqn);
147 		return false;
148 	}
149 
150 	/* Check for valid reverse domain */
151 	domain_label_length = 0;
152 	for (i = 12; i < reverse_domain_end_index; i++) {
153 		if (domain_label_length > SPDK_DOMAIN_LABEL_MAX_LEN) {
154 			SPDK_ERRLOG("Invalid domain name in NQN \"%s\". At least one Label is too long.\n", nqn);
155 			return false;
156 		}
157 
158 		switch (domain_state) {
159 
160 		case SPDK_NVMF_DOMAIN_ACCEPT_LETTER: {
161 			if (isalpha(nqn[i])) {
162 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
163 				domain_label_length++;
164 				break;
165 			} else {
166 				SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must start with a letter.\n", nqn);
167 				return false;
168 			}
169 		}
170 
171 		case SPDK_NVMF_DOMAIN_ACCEPT_LDH: {
172 			if (isalpha(nqn[i]) || isdigit(nqn[i])) {
173 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
174 				domain_label_length++;
175 				break;
176 			} else if (nqn[i] == '-') {
177 				if (i == reverse_domain_end_index - 1) {
178 					SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
179 						    nqn);
180 					return false;
181 				}
182 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH;
183 				domain_label_length++;
184 				break;
185 			} else if (nqn[i] == '.') {
186 				SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
187 					    nqn);
188 				return false;
189 			} else {
190 				SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n",
191 					    nqn);
192 				return false;
193 			}
194 		}
195 
196 		case SPDK_NVMF_DOMAIN_ACCEPT_ANY: {
197 			if (isalpha(nqn[i]) || isdigit(nqn[i])) {
198 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
199 				domain_label_length++;
200 				break;
201 			} else if (nqn[i] == '-') {
202 				if (i == reverse_domain_end_index - 1) {
203 					SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
204 						    nqn);
205 					return false;
206 				}
207 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH;
208 				domain_label_length++;
209 				break;
210 			} else if (nqn[i] == '.') {
211 				domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER;
212 				domain_label_length = 0;
213 				break;
214 			} else {
215 				SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n",
216 					    nqn);
217 				return false;
218 			}
219 		}
220 		}
221 	}
222 
223 	i = reverse_domain_end_index + 1;
224 	while (i < len) {
225 		bytes_consumed = utf8_valid(&nqn[i], &nqn[len]);
226 		if (bytes_consumed <= 0) {
227 			SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only valid utf-8.\n", nqn);
228 			return false;
229 		}
230 
231 		i += bytes_consumed;
232 	}
233 	return true;
234 }
235 
236 struct spdk_nvmf_subsystem *
237 spdk_nvmf_subsystem_create(struct spdk_nvmf_tgt *tgt,
238 			   const char *nqn,
239 			   enum spdk_nvmf_subtype type,
240 			   uint32_t num_ns)
241 {
242 	struct spdk_nvmf_subsystem	*subsystem;
243 	uint32_t			sid;
244 
245 	if (spdk_nvmf_tgt_find_subsystem(tgt, nqn)) {
246 		SPDK_ERRLOG("Subsystem NQN '%s' already exists\n", nqn);
247 		return NULL;
248 	}
249 
250 	if (!spdk_nvmf_valid_nqn(nqn)) {
251 		return NULL;
252 	}
253 
254 	if (type == SPDK_NVMF_SUBTYPE_DISCOVERY && num_ns != 0) {
255 		SPDK_ERRLOG("Discovery subsystem cannot have namespaces.\n");
256 		return NULL;
257 	}
258 
259 	/* Find a free subsystem id (sid) */
260 	for (sid = 0; sid < tgt->max_subsystems; sid++) {
261 		if (tgt->subsystems[sid] == NULL) {
262 			break;
263 		}
264 	}
265 	if (sid >= tgt->max_subsystems) {
266 		return NULL;
267 	}
268 
269 	subsystem = calloc(1, sizeof(struct spdk_nvmf_subsystem));
270 	if (subsystem == NULL) {
271 		return NULL;
272 	}
273 
274 	subsystem->thread = spdk_get_thread();
275 	subsystem->state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
276 	subsystem->tgt = tgt;
277 	subsystem->id = sid;
278 	subsystem->subtype = type;
279 	subsystem->max_nsid = num_ns;
280 	subsystem->max_allowed_nsid = num_ns;
281 	subsystem->next_cntlid = 0;
282 	snprintf(subsystem->subnqn, sizeof(subsystem->subnqn), "%s", nqn);
283 	TAILQ_INIT(&subsystem->listeners);
284 	TAILQ_INIT(&subsystem->hosts);
285 	TAILQ_INIT(&subsystem->ctrlrs);
286 
287 	if (num_ns != 0) {
288 		subsystem->ns = calloc(num_ns, sizeof(struct spdk_nvmf_ns *));
289 		if (subsystem->ns == NULL) {
290 			SPDK_ERRLOG("Namespace memory allocation failed\n");
291 			free(subsystem);
292 			return NULL;
293 		}
294 	}
295 
296 	memset(subsystem->sn, '0', sizeof(subsystem->sn) - 1);
297 	subsystem->sn[sizeof(subsystem->sn) - 1] = '\0';
298 
299 	snprintf(subsystem->mn, sizeof(subsystem->mn), "%s",
300 		 MODEL_NUMBER_DEFAULT);
301 
302 	tgt->subsystems[sid] = subsystem;
303 	tgt->discovery_genctr++;
304 
305 	return subsystem;
306 }
307 
308 static void
309 _spdk_nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_host *host)
310 {
311 	TAILQ_REMOVE(&subsystem->hosts, host, link);
312 	free(host);
313 }
314 
315 static void
316 _nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem,
317 				struct spdk_nvmf_listener *listener)
318 {
319 	struct spdk_nvmf_transport *transport;
320 
321 	transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, listener->trid.trtype);
322 	if (transport != NULL) {
323 		spdk_nvmf_transport_stop_listen(transport, &listener->trid);
324 	}
325 
326 	TAILQ_REMOVE(&subsystem->listeners, listener, link);
327 	free(listener);
328 }
329 
330 void
331 spdk_nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem)
332 {
333 	struct spdk_nvmf_listener	*listener, *listener_tmp;
334 	struct spdk_nvmf_host		*host, *host_tmp;
335 	struct spdk_nvmf_ctrlr		*ctrlr, *ctrlr_tmp;
336 	struct spdk_nvmf_ns		*ns;
337 
338 	if (!subsystem) {
339 		return;
340 	}
341 
342 	assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE);
343 
344 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "subsystem is %p\n", subsystem);
345 
346 	TAILQ_FOREACH_SAFE(listener, &subsystem->listeners, link, listener_tmp) {
347 		_nvmf_subsystem_remove_listener(subsystem, listener);
348 	}
349 
350 	TAILQ_FOREACH_SAFE(host, &subsystem->hosts, link, host_tmp) {
351 		_spdk_nvmf_subsystem_remove_host(subsystem, host);
352 	}
353 
354 	TAILQ_FOREACH_SAFE(ctrlr, &subsystem->ctrlrs, link, ctrlr_tmp) {
355 		spdk_nvmf_ctrlr_destruct(ctrlr);
356 	}
357 
358 	ns = spdk_nvmf_subsystem_get_first_ns(subsystem);
359 	while (ns != NULL) {
360 		struct spdk_nvmf_ns *next_ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns);
361 
362 		spdk_nvmf_subsystem_remove_ns(subsystem, ns->opts.nsid);
363 		ns = next_ns;
364 	}
365 
366 	free(subsystem->ns);
367 
368 	subsystem->tgt->subsystems[subsystem->id] = NULL;
369 	subsystem->tgt->discovery_genctr++;
370 
371 	free(subsystem);
372 }
373 
374 static int
375 spdk_nvmf_subsystem_set_state(struct spdk_nvmf_subsystem *subsystem,
376 			      enum spdk_nvmf_subsystem_state state)
377 {
378 	enum spdk_nvmf_subsystem_state actual_old_state, expected_old_state;
379 
380 	switch (state) {
381 	case SPDK_NVMF_SUBSYSTEM_INACTIVE:
382 		expected_old_state = SPDK_NVMF_SUBSYSTEM_DEACTIVATING;
383 		break;
384 	case SPDK_NVMF_SUBSYSTEM_ACTIVATING:
385 		expected_old_state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
386 		break;
387 	case SPDK_NVMF_SUBSYSTEM_ACTIVE:
388 		expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING;
389 		break;
390 	case SPDK_NVMF_SUBSYSTEM_PAUSING:
391 		expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
392 		break;
393 	case SPDK_NVMF_SUBSYSTEM_PAUSED:
394 		expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSING;
395 		break;
396 	case SPDK_NVMF_SUBSYSTEM_RESUMING:
397 		expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSED;
398 		break;
399 	case SPDK_NVMF_SUBSYSTEM_DEACTIVATING:
400 		expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
401 		break;
402 	default:
403 		assert(false);
404 		return -1;
405 	}
406 
407 	actual_old_state = __sync_val_compare_and_swap(&subsystem->state, expected_old_state, state);
408 	if (actual_old_state != expected_old_state) {
409 		if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING &&
410 		    state == SPDK_NVMF_SUBSYSTEM_ACTIVE) {
411 			expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING;
412 		}
413 		/* This is for the case when activating the subsystem fails. */
414 		if (actual_old_state == SPDK_NVMF_SUBSYSTEM_ACTIVATING &&
415 		    state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING) {
416 			expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING;
417 		}
418 		actual_old_state = __sync_val_compare_and_swap(&subsystem->state, expected_old_state, state);
419 	}
420 	assert(actual_old_state == expected_old_state);
421 	return actual_old_state - expected_old_state;
422 }
423 
424 struct subsystem_state_change_ctx {
425 	struct spdk_nvmf_subsystem *subsystem;
426 
427 	enum spdk_nvmf_subsystem_state requested_state;
428 
429 	spdk_nvmf_subsystem_state_change_done cb_fn;
430 	void *cb_arg;
431 };
432 
433 static void
434 subsystem_state_change_done(struct spdk_io_channel_iter *i, int status)
435 {
436 	struct subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
437 
438 	if (status == 0) {
439 		status = spdk_nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state);
440 		if (status) {
441 			status = -1;
442 		}
443 	}
444 
445 	if (ctx->cb_fn) {
446 		ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status);
447 	}
448 	free(ctx);
449 }
450 
451 static void
452 subsystem_state_change_continue(void *ctx, int status)
453 {
454 	struct spdk_io_channel_iter *i = ctx;
455 	spdk_for_each_channel_continue(i, status);
456 }
457 
458 static void
459 subsystem_state_change_on_pg(struct spdk_io_channel_iter *i)
460 {
461 	struct subsystem_state_change_ctx *ctx;
462 	struct spdk_io_channel *ch;
463 	struct spdk_nvmf_poll_group *group;
464 
465 	ctx = spdk_io_channel_iter_get_ctx(i);
466 	ch = spdk_io_channel_iter_get_channel(i);
467 	group = spdk_io_channel_get_ctx(ch);
468 
469 	switch (ctx->requested_state) {
470 	case SPDK_NVMF_SUBSYSTEM_INACTIVE:
471 		spdk_nvmf_poll_group_remove_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
472 		break;
473 	case SPDK_NVMF_SUBSYSTEM_ACTIVE:
474 		if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_ACTIVATING) {
475 			spdk_nvmf_poll_group_add_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
476 		} else if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_RESUMING) {
477 			spdk_nvmf_poll_group_resume_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
478 		}
479 		break;
480 	case SPDK_NVMF_SUBSYSTEM_PAUSED:
481 		spdk_nvmf_poll_group_pause_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
482 		break;
483 	default:
484 		assert(false);
485 		break;
486 	}
487 }
488 
489 static int
490 spdk_nvmf_subsystem_state_change(struct spdk_nvmf_subsystem *subsystem,
491 				 enum spdk_nvmf_subsystem_state requested_state,
492 				 spdk_nvmf_subsystem_state_change_done cb_fn,
493 				 void *cb_arg)
494 {
495 	struct subsystem_state_change_ctx *ctx;
496 	enum spdk_nvmf_subsystem_state intermediate_state;
497 	int rc;
498 
499 	switch (requested_state) {
500 	case SPDK_NVMF_SUBSYSTEM_INACTIVE:
501 		intermediate_state = SPDK_NVMF_SUBSYSTEM_DEACTIVATING;
502 		break;
503 	case SPDK_NVMF_SUBSYSTEM_ACTIVE:
504 		if (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED) {
505 			intermediate_state = SPDK_NVMF_SUBSYSTEM_RESUMING;
506 		} else {
507 			intermediate_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING;
508 		}
509 		break;
510 	case SPDK_NVMF_SUBSYSTEM_PAUSED:
511 		intermediate_state = SPDK_NVMF_SUBSYSTEM_PAUSING;
512 		break;
513 	default:
514 		assert(false);
515 		return -EINVAL;
516 	}
517 
518 	ctx = calloc(1, sizeof(*ctx));
519 	if (!ctx) {
520 		return -ENOMEM;
521 	}
522 
523 	rc = spdk_nvmf_subsystem_set_state(subsystem, intermediate_state);
524 	if (rc) {
525 		free(ctx);
526 		return rc;
527 	}
528 
529 	ctx->subsystem = subsystem;
530 	ctx->requested_state = requested_state;
531 	ctx->cb_fn = cb_fn;
532 	ctx->cb_arg = cb_arg;
533 
534 	spdk_for_each_channel(subsystem->tgt,
535 			      subsystem_state_change_on_pg,
536 			      ctx,
537 			      subsystem_state_change_done);
538 
539 	return 0;
540 }
541 
542 int
543 spdk_nvmf_subsystem_start(struct spdk_nvmf_subsystem *subsystem,
544 			  spdk_nvmf_subsystem_state_change_done cb_fn,
545 			  void *cb_arg)
546 {
547 	return spdk_nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg);
548 }
549 
550 int
551 spdk_nvmf_subsystem_stop(struct spdk_nvmf_subsystem *subsystem,
552 			 spdk_nvmf_subsystem_state_change_done cb_fn,
553 			 void *cb_arg)
554 {
555 	return spdk_nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_INACTIVE, cb_fn, cb_arg);
556 }
557 
558 int
559 spdk_nvmf_subsystem_pause(struct spdk_nvmf_subsystem *subsystem,
560 			  spdk_nvmf_subsystem_state_change_done cb_fn,
561 			  void *cb_arg)
562 {
563 	return spdk_nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_PAUSED, cb_fn, cb_arg);
564 }
565 
566 int
567 spdk_nvmf_subsystem_resume(struct spdk_nvmf_subsystem *subsystem,
568 			   spdk_nvmf_subsystem_state_change_done cb_fn,
569 			   void *cb_arg)
570 {
571 	return spdk_nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg);
572 }
573 
574 struct spdk_nvmf_subsystem *
575 spdk_nvmf_subsystem_get_first(struct spdk_nvmf_tgt *tgt)
576 {
577 	struct spdk_nvmf_subsystem	*subsystem;
578 	uint32_t sid;
579 
580 	for (sid = 0; sid < tgt->max_subsystems; sid++) {
581 		subsystem = tgt->subsystems[sid];
582 		if (subsystem) {
583 			return subsystem;
584 		}
585 	}
586 
587 	return NULL;
588 }
589 
590 struct spdk_nvmf_subsystem *
591 spdk_nvmf_subsystem_get_next(struct spdk_nvmf_subsystem *subsystem)
592 {
593 	uint32_t sid;
594 	struct spdk_nvmf_tgt *tgt;
595 
596 	if (!subsystem) {
597 		return NULL;
598 	}
599 
600 	tgt = subsystem->tgt;
601 
602 	for (sid = subsystem->id + 1; sid < tgt->max_subsystems; sid++) {
603 		subsystem = tgt->subsystems[sid];
604 		if (subsystem) {
605 			return subsystem;
606 		}
607 	}
608 
609 	return NULL;
610 }
611 
612 static struct spdk_nvmf_host *
613 _spdk_nvmf_subsystem_find_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
614 {
615 	struct spdk_nvmf_host *host = NULL;
616 
617 	TAILQ_FOREACH(host, &subsystem->hosts, link) {
618 		if (strcmp(hostnqn, host->nqn) == 0) {
619 			return host;
620 		}
621 	}
622 
623 	return NULL;
624 }
625 
626 int
627 spdk_nvmf_subsystem_add_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
628 {
629 	struct spdk_nvmf_host *host;
630 
631 	if (!spdk_nvmf_valid_nqn(hostnqn)) {
632 		return -EINVAL;
633 	}
634 
635 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
636 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
637 		return -EAGAIN;
638 	}
639 
640 	if (_spdk_nvmf_subsystem_find_host(subsystem, hostnqn)) {
641 		/* This subsystem already allows the specified host. */
642 		return 0;
643 	}
644 
645 	host = calloc(1, sizeof(*host));
646 	if (!host) {
647 		return -ENOMEM;
648 	}
649 
650 	snprintf(host->nqn, sizeof(host->nqn), "%s", hostnqn);
651 
652 	TAILQ_INSERT_HEAD(&subsystem->hosts, host, link);
653 	subsystem->tgt->discovery_genctr++;
654 
655 	return 0;
656 }
657 
658 int
659 spdk_nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
660 {
661 	struct spdk_nvmf_host *host;
662 
663 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
664 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
665 		return -EAGAIN;
666 	}
667 
668 	host = _spdk_nvmf_subsystem_find_host(subsystem, hostnqn);
669 	if (host == NULL) {
670 		return -ENOENT;
671 	}
672 
673 	_spdk_nvmf_subsystem_remove_host(subsystem, host);
674 	return 0;
675 }
676 
677 int
678 spdk_nvmf_subsystem_set_allow_any_host(struct spdk_nvmf_subsystem *subsystem, bool allow_any_host)
679 {
680 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
681 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
682 		return -EAGAIN;
683 	}
684 
685 	subsystem->allow_any_host = allow_any_host;
686 
687 	return 0;
688 }
689 
690 bool
691 spdk_nvmf_subsystem_get_allow_any_host(const struct spdk_nvmf_subsystem *subsystem)
692 {
693 	return subsystem->allow_any_host;
694 }
695 
696 bool
697 spdk_nvmf_subsystem_host_allowed(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
698 {
699 	if (!hostnqn) {
700 		return false;
701 	}
702 
703 	if (subsystem->allow_any_host) {
704 		return true;
705 	}
706 
707 	return _spdk_nvmf_subsystem_find_host(subsystem, hostnqn) != NULL;
708 }
709 
710 struct spdk_nvmf_host *
711 spdk_nvmf_subsystem_get_first_host(struct spdk_nvmf_subsystem *subsystem)
712 {
713 	return TAILQ_FIRST(&subsystem->hosts);
714 }
715 
716 
717 struct spdk_nvmf_host *
718 spdk_nvmf_subsystem_get_next_host(struct spdk_nvmf_subsystem *subsystem,
719 				  struct spdk_nvmf_host *prev_host)
720 {
721 	return TAILQ_NEXT(prev_host, link);
722 }
723 
724 const char *
725 spdk_nvmf_host_get_nqn(struct spdk_nvmf_host *host)
726 {
727 	return host->nqn;
728 }
729 
730 static struct spdk_nvmf_listener *
731 _spdk_nvmf_subsystem_find_listener(struct spdk_nvmf_subsystem *subsystem,
732 				   const struct spdk_nvme_transport_id *trid)
733 {
734 	struct spdk_nvmf_listener *listener;
735 
736 	TAILQ_FOREACH(listener, &subsystem->listeners, link) {
737 		if (spdk_nvme_transport_id_compare(&listener->trid, trid) == 0) {
738 			return listener;
739 		}
740 	}
741 
742 	return NULL;
743 }
744 
745 int
746 spdk_nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem,
747 				 struct spdk_nvme_transport_id *trid)
748 {
749 	struct spdk_nvmf_transport *transport;
750 	struct spdk_nvmf_listener *listener;
751 
752 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
753 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
754 		return -EAGAIN;
755 	}
756 
757 	if (_spdk_nvmf_subsystem_find_listener(subsystem, trid)) {
758 		/* Listener already exists in this subsystem */
759 		return 0;
760 	}
761 
762 	transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, trid->trtype);
763 	if (transport == NULL) {
764 		SPDK_ERRLOG("Unknown transport type %d\n", trid->trtype);
765 		return -EINVAL;
766 	}
767 
768 	listener = calloc(1, sizeof(*listener));
769 	if (!listener) {
770 		return -ENOMEM;
771 	}
772 
773 	listener->trid = *trid;
774 	listener->transport = transport;
775 
776 	TAILQ_INSERT_HEAD(&subsystem->listeners, listener, link);
777 
778 	return 0;
779 }
780 
781 int
782 spdk_nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem,
783 				    const struct spdk_nvme_transport_id *trid)
784 {
785 	struct spdk_nvmf_listener *listener;
786 
787 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
788 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
789 		return -EAGAIN;
790 	}
791 
792 	listener = _spdk_nvmf_subsystem_find_listener(subsystem, trid);
793 	if (listener == NULL) {
794 		return -ENOENT;
795 	}
796 
797 	_nvmf_subsystem_remove_listener(subsystem, listener);
798 
799 	return 0;
800 }
801 
802 bool
803 spdk_nvmf_subsystem_listener_allowed(struct spdk_nvmf_subsystem *subsystem,
804 				     struct spdk_nvme_transport_id *trid)
805 {
806 	struct spdk_nvmf_listener *listener;
807 
808 	if (!strcmp(subsystem->subnqn, SPDK_NVMF_DISCOVERY_NQN)) {
809 		return true;
810 	}
811 
812 	TAILQ_FOREACH(listener, &subsystem->listeners, link) {
813 		if (spdk_nvme_transport_id_compare(&listener->trid, trid) == 0) {
814 			return true;
815 		}
816 	}
817 
818 	return false;
819 }
820 
821 struct spdk_nvmf_listener *
822 spdk_nvmf_subsystem_get_first_listener(struct spdk_nvmf_subsystem *subsystem)
823 {
824 	return TAILQ_FIRST(&subsystem->listeners);
825 }
826 
827 struct spdk_nvmf_listener *
828 spdk_nvmf_subsystem_get_next_listener(struct spdk_nvmf_subsystem *subsystem,
829 				      struct spdk_nvmf_listener *prev_listener)
830 {
831 	return TAILQ_NEXT(prev_listener, link);
832 }
833 
834 const struct spdk_nvme_transport_id *
835 spdk_nvmf_listener_get_trid(struct spdk_nvmf_listener *listener)
836 {
837 	return &listener->trid;
838 }
839 
840 struct subsystem_update_ns_ctx {
841 	struct spdk_nvmf_subsystem *subsystem;
842 
843 	spdk_nvmf_subsystem_state_change_done cb_fn;
844 	void *cb_arg;
845 };
846 
847 static void
848 subsystem_update_ns_done(struct spdk_io_channel_iter *i, int status)
849 {
850 	struct subsystem_update_ns_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
851 
852 	if (ctx->cb_fn) {
853 		ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status);
854 	}
855 	free(ctx);
856 }
857 
858 static void
859 subsystem_update_ns_on_pg(struct spdk_io_channel_iter *i)
860 {
861 	int rc;
862 	struct subsystem_update_ns_ctx *ctx;
863 	struct spdk_nvmf_poll_group *group;
864 	struct spdk_nvmf_subsystem *subsystem;
865 
866 	ctx = spdk_io_channel_iter_get_ctx(i);
867 	group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
868 	subsystem = ctx->subsystem;
869 
870 	rc = spdk_nvmf_poll_group_update_subsystem(group, subsystem);
871 	spdk_for_each_channel_continue(i, rc);
872 }
873 
874 static int
875 spdk_nvmf_subsystem_update_ns(struct spdk_nvmf_subsystem *subsystem, spdk_channel_for_each_cpl cpl,
876 			      void *ctx)
877 {
878 	spdk_for_each_channel(subsystem->tgt,
879 			      subsystem_update_ns_on_pg,
880 			      ctx,
881 			      cpl);
882 
883 	return 0;
884 }
885 
886 static void
887 spdk_nvmf_subsystem_ns_changed(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
888 {
889 	struct spdk_nvmf_ctrlr *ctrlr;
890 
891 	TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
892 		spdk_nvmf_ctrlr_ns_changed(ctrlr, nsid);
893 	}
894 }
895 
896 int
897 spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
898 {
899 	struct spdk_nvmf_ns *ns;
900 	struct spdk_nvmf_registrant *reg, *reg_tmp;
901 
902 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
903 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
904 		assert(false);
905 		return -1;
906 	}
907 
908 	if (nsid == 0 || nsid > subsystem->max_nsid) {
909 		return -1;
910 	}
911 
912 	ns = subsystem->ns[nsid - 1];
913 	if (!ns) {
914 		return -1;
915 	}
916 
917 	subsystem->ns[nsid - 1] = NULL;
918 
919 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) {
920 		TAILQ_REMOVE(&ns->registrants, reg, link);
921 		free(reg);
922 	}
923 	spdk_bdev_module_release_bdev(ns->bdev);
924 	spdk_bdev_close(ns->desc);
925 	if (ns->ptpl_file) {
926 		free(ns->ptpl_file);
927 	}
928 	free(ns);
929 
930 	spdk_nvmf_subsystem_ns_changed(subsystem, nsid);
931 
932 	return 0;
933 }
934 
935 static void
936 _spdk_nvmf_ns_hot_remove(struct spdk_nvmf_subsystem *subsystem,
937 			 void *cb_arg, int status)
938 {
939 	struct spdk_nvmf_ns *ns = cb_arg;
940 	int rc;
941 
942 	rc = spdk_nvmf_subsystem_remove_ns(subsystem, ns->opts.nsid);
943 	if (rc != 0) {
944 		SPDK_ERRLOG("Failed to make changes to NVME-oF subsystem with id: %u\n", subsystem->id);
945 	}
946 
947 	spdk_nvmf_subsystem_resume(subsystem, NULL, NULL);
948 }
949 
950 static void
951 spdk_nvmf_ns_hot_remove(void *remove_ctx)
952 {
953 	struct spdk_nvmf_ns *ns = remove_ctx;
954 	int rc;
955 
956 	rc = spdk_nvmf_subsystem_pause(ns->subsystem, _spdk_nvmf_ns_hot_remove, ns);
957 	if (rc) {
958 		SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n");
959 	}
960 }
961 
962 static void
963 _spdk_nvmf_ns_resize(struct spdk_nvmf_subsystem *subsystem, void *cb_arg, int status)
964 {
965 	struct spdk_nvmf_ns *ns = cb_arg;
966 
967 	spdk_nvmf_subsystem_ns_changed(subsystem, ns->opts.nsid);
968 	spdk_nvmf_subsystem_resume(subsystem, NULL, NULL);
969 }
970 
971 static void
972 spdk_nvmf_ns_resize(void *event_ctx)
973 {
974 	struct spdk_nvmf_ns *ns = event_ctx;
975 	int rc;
976 
977 	rc = spdk_nvmf_subsystem_pause(ns->subsystem, _spdk_nvmf_ns_resize, ns);
978 	if (rc) {
979 		SPDK_ERRLOG("Unable to pause subsystem to process namespace resize!\n");
980 	}
981 }
982 
983 static void
984 spdk_nvmf_ns_event(enum spdk_bdev_event_type type,
985 		   struct spdk_bdev *bdev,
986 		   void *event_ctx)
987 {
988 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Bdev event: type %d, name %s, subsystem_id %d, ns_id %d\n",
989 		      type,
990 		      bdev->name,
991 		      ((struct spdk_nvmf_ns *)event_ctx)->subsystem->id,
992 		      ((struct spdk_nvmf_ns *)event_ctx)->nsid);
993 
994 	switch (type) {
995 	case SPDK_BDEV_EVENT_REMOVE:
996 		spdk_nvmf_ns_hot_remove(event_ctx);
997 		break;
998 	case SPDK_BDEV_EVENT_RESIZE:
999 		spdk_nvmf_ns_resize(event_ctx);
1000 		break;
1001 	default:
1002 		SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
1003 		break;
1004 	}
1005 }
1006 
1007 void
1008 spdk_nvmf_ns_opts_get_defaults(struct spdk_nvmf_ns_opts *opts, size_t opts_size)
1009 {
1010 	/* All current fields are set to 0 by default. */
1011 	memset(opts, 0, opts_size);
1012 }
1013 
1014 /* Dummy bdev module used to to claim bdevs. */
1015 static struct spdk_bdev_module ns_bdev_module = {
1016 	.name	= "NVMe-oF Target",
1017 };
1018 
1019 static int
1020 spdk_nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info);
1021 static int
1022 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info);
1023 
1024 uint32_t
1025 spdk_nvmf_subsystem_add_ns(struct spdk_nvmf_subsystem *subsystem, struct spdk_bdev *bdev,
1026 			   const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size,
1027 			   const char *ptpl_file)
1028 {
1029 	struct spdk_nvmf_ns_opts opts;
1030 	struct spdk_nvmf_ns *ns;
1031 	struct spdk_nvmf_reservation_info info = {0};
1032 	int rc;
1033 
1034 	if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1035 	      subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1036 		return 0;
1037 	}
1038 
1039 	if (spdk_bdev_get_md_size(bdev) != 0 && !spdk_bdev_is_md_interleaved(bdev)) {
1040 		SPDK_ERRLOG("Can't attach bdev with separate metadata.\n");
1041 		return 0;
1042 	}
1043 
1044 	spdk_nvmf_ns_opts_get_defaults(&opts, sizeof(opts));
1045 	if (user_opts) {
1046 		memcpy(&opts, user_opts, spdk_min(sizeof(opts), opts_size));
1047 	}
1048 
1049 	if (spdk_mem_all_zero(&opts.uuid, sizeof(opts.uuid))) {
1050 		opts.uuid = *spdk_bdev_get_uuid(bdev);
1051 	}
1052 
1053 	if (opts.nsid == SPDK_NVME_GLOBAL_NS_TAG) {
1054 		SPDK_ERRLOG("Invalid NSID %" PRIu32 "\n", opts.nsid);
1055 		return 0;
1056 	}
1057 
1058 	if (opts.nsid == 0) {
1059 		/*
1060 		 * NSID not specified - find a free index.
1061 		 *
1062 		 * If no free slots are found, opts.nsid will be subsystem->max_nsid + 1, which will
1063 		 * expand max_nsid if possible.
1064 		 */
1065 		for (opts.nsid = 1; opts.nsid <= subsystem->max_nsid; opts.nsid++) {
1066 			if (_spdk_nvmf_subsystem_get_ns(subsystem, opts.nsid) == NULL) {
1067 				break;
1068 			}
1069 		}
1070 	}
1071 
1072 	if (_spdk_nvmf_subsystem_get_ns(subsystem, opts.nsid)) {
1073 		SPDK_ERRLOG("Requested NSID %" PRIu32 " already in use\n", opts.nsid);
1074 		return 0;
1075 	}
1076 
1077 	if (opts.nsid > subsystem->max_nsid) {
1078 		struct spdk_nvmf_ns **new_ns_array;
1079 
1080 		/* If MaxNamespaces was specified, we can't extend max_nsid beyond it. */
1081 		if (subsystem->max_allowed_nsid > 0 && opts.nsid > subsystem->max_allowed_nsid) {
1082 			SPDK_ERRLOG("Can't extend NSID range above MaxNamespaces\n");
1083 			return 0;
1084 		}
1085 
1086 		/* If a controller is connected, we can't change NN. */
1087 		if (!TAILQ_EMPTY(&subsystem->ctrlrs)) {
1088 			SPDK_ERRLOG("Can't extend NSID range while controllers are connected\n");
1089 			return 0;
1090 		}
1091 
1092 		new_ns_array = realloc(subsystem->ns, sizeof(struct spdk_nvmf_ns *) * opts.nsid);
1093 		if (new_ns_array == NULL) {
1094 			SPDK_ERRLOG("Memory allocation error while resizing namespace array.\n");
1095 			return 0;
1096 		}
1097 
1098 		memset(new_ns_array + subsystem->max_nsid, 0,
1099 		       sizeof(struct spdk_nvmf_ns *) * (opts.nsid - subsystem->max_nsid));
1100 		subsystem->ns = new_ns_array;
1101 		subsystem->max_nsid = opts.nsid;
1102 	}
1103 
1104 	ns = calloc(1, sizeof(*ns));
1105 	if (ns == NULL) {
1106 		SPDK_ERRLOG("Namespace allocation failed\n");
1107 		return 0;
1108 	}
1109 
1110 	ns->bdev = bdev;
1111 	ns->opts = opts;
1112 	ns->subsystem = subsystem;
1113 	rc = spdk_bdev_open_ext(bdev->name, true, spdk_nvmf_ns_event, ns, &ns->desc);
1114 	if (rc != 0) {
1115 		SPDK_ERRLOG("Subsystem %s: bdev %s cannot be opened, error=%d\n",
1116 			    subsystem->subnqn, spdk_bdev_get_name(bdev), rc);
1117 		free(ns);
1118 		return 0;
1119 	}
1120 	rc = spdk_bdev_module_claim_bdev(bdev, ns->desc, &ns_bdev_module);
1121 	if (rc != 0) {
1122 		spdk_bdev_close(ns->desc);
1123 		free(ns);
1124 		return 0;
1125 	}
1126 	subsystem->ns[opts.nsid - 1] = ns;
1127 	ns->nsid = opts.nsid;
1128 	TAILQ_INIT(&ns->registrants);
1129 
1130 	if (ptpl_file) {
1131 		rc = spdk_nvmf_ns_load_reservation(ptpl_file, &info);
1132 		if (!rc) {
1133 			rc = nvmf_ns_reservation_restore(ns, &info);
1134 			if (rc) {
1135 				SPDK_ERRLOG("Subsystem restore reservation failed\n");
1136 				subsystem->ns[opts.nsid - 1] = NULL;
1137 				spdk_bdev_close(ns->desc);
1138 				free(ns);
1139 				return 0;
1140 			}
1141 		}
1142 		ns->ptpl_file = strdup(ptpl_file);
1143 	}
1144 
1145 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Subsystem %s: bdev %s assigned nsid %" PRIu32 "\n",
1146 		      spdk_nvmf_subsystem_get_nqn(subsystem),
1147 		      spdk_bdev_get_name(bdev),
1148 		      opts.nsid);
1149 
1150 	spdk_nvmf_subsystem_ns_changed(subsystem, opts.nsid);
1151 
1152 	return opts.nsid;
1153 }
1154 
1155 static uint32_t
1156 spdk_nvmf_subsystem_get_next_allocated_nsid(struct spdk_nvmf_subsystem *subsystem,
1157 		uint32_t prev_nsid)
1158 {
1159 	uint32_t nsid;
1160 
1161 	if (prev_nsid >= subsystem->max_nsid) {
1162 		return 0;
1163 	}
1164 
1165 	for (nsid = prev_nsid + 1; nsid <= subsystem->max_nsid; nsid++) {
1166 		if (subsystem->ns[nsid - 1]) {
1167 			return nsid;
1168 		}
1169 	}
1170 
1171 	return 0;
1172 }
1173 
1174 struct spdk_nvmf_ns *
1175 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem)
1176 {
1177 	uint32_t first_nsid;
1178 
1179 	first_nsid = spdk_nvmf_subsystem_get_next_allocated_nsid(subsystem, 0);
1180 	return _spdk_nvmf_subsystem_get_ns(subsystem, first_nsid);
1181 }
1182 
1183 struct spdk_nvmf_ns *
1184 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem,
1185 				struct spdk_nvmf_ns *prev_ns)
1186 {
1187 	uint32_t next_nsid;
1188 
1189 	next_nsid = spdk_nvmf_subsystem_get_next_allocated_nsid(subsystem, prev_ns->opts.nsid);
1190 	return _spdk_nvmf_subsystem_get_ns(subsystem, next_nsid);
1191 }
1192 
1193 struct spdk_nvmf_ns *
1194 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1195 {
1196 	return _spdk_nvmf_subsystem_get_ns(subsystem, nsid);
1197 }
1198 
1199 uint32_t
1200 spdk_nvmf_ns_get_id(const struct spdk_nvmf_ns *ns)
1201 {
1202 	return ns->opts.nsid;
1203 }
1204 
1205 struct spdk_bdev *
1206 spdk_nvmf_ns_get_bdev(struct spdk_nvmf_ns *ns)
1207 {
1208 	return ns->bdev;
1209 }
1210 
1211 void
1212 spdk_nvmf_ns_get_opts(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_ns_opts *opts,
1213 		      size_t opts_size)
1214 {
1215 	memset(opts, 0, opts_size);
1216 	memcpy(opts, &ns->opts, spdk_min(sizeof(ns->opts), opts_size));
1217 }
1218 
1219 const char *
1220 spdk_nvmf_subsystem_get_sn(const struct spdk_nvmf_subsystem *subsystem)
1221 {
1222 	return subsystem->sn;
1223 }
1224 
1225 int
1226 spdk_nvmf_subsystem_set_sn(struct spdk_nvmf_subsystem *subsystem, const char *sn)
1227 {
1228 	size_t len, max_len;
1229 
1230 	max_len = sizeof(subsystem->sn) - 1;
1231 	len = strlen(sn);
1232 	if (len > max_len) {
1233 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Invalid sn \"%s\": length %zu > max %zu\n",
1234 			      sn, len, max_len);
1235 		return -1;
1236 	}
1237 
1238 	if (!spdk_nvmf_valid_ascii_string(sn, len)) {
1239 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Non-ASCII sn\n");
1240 		SPDK_LOGDUMP(SPDK_LOG_NVMF, "sn", sn, len);
1241 		return -1;
1242 	}
1243 
1244 	snprintf(subsystem->sn, sizeof(subsystem->sn), "%s", sn);
1245 
1246 	return 0;
1247 }
1248 
1249 const char *
1250 spdk_nvmf_subsystem_get_mn(const struct spdk_nvmf_subsystem *subsystem)
1251 {
1252 	return subsystem->mn;
1253 }
1254 
1255 int
1256 spdk_nvmf_subsystem_set_mn(struct spdk_nvmf_subsystem *subsystem, const char *mn)
1257 {
1258 	size_t len, max_len;
1259 
1260 	if (mn == NULL) {
1261 		mn = MODEL_NUMBER_DEFAULT;
1262 	}
1263 	max_len = sizeof(subsystem->mn) - 1;
1264 	len = strlen(mn);
1265 	if (len > max_len) {
1266 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Invalid mn \"%s\": length %zu > max %zu\n",
1267 			      mn, len, max_len);
1268 		return -1;
1269 	}
1270 
1271 	if (!spdk_nvmf_valid_ascii_string(mn, len)) {
1272 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Non-ASCII mn\n");
1273 		SPDK_LOGDUMP(SPDK_LOG_NVMF, "mn", mn, len);
1274 		return -1;
1275 	}
1276 
1277 	snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", mn);
1278 
1279 	return 0;
1280 }
1281 
1282 const char *
1283 spdk_nvmf_subsystem_get_nqn(struct spdk_nvmf_subsystem *subsystem)
1284 {
1285 	return subsystem->subnqn;
1286 }
1287 
1288 /* Workaround for astyle formatting bug */
1289 typedef enum spdk_nvmf_subtype nvmf_subtype_t;
1290 
1291 nvmf_subtype_t
1292 spdk_nvmf_subsystem_get_type(struct spdk_nvmf_subsystem *subsystem)
1293 {
1294 	return subsystem->subtype;
1295 }
1296 
1297 static uint16_t
1298 spdk_nvmf_subsystem_gen_cntlid(struct spdk_nvmf_subsystem *subsystem)
1299 {
1300 	int count;
1301 
1302 	/*
1303 	 * In the worst case, we might have to try all CNTLID values between 1 and 0xFFF0 - 1
1304 	 * before we find one that is unused (or find that all values are in use).
1305 	 */
1306 	for (count = 0; count < 0xFFF0 - 1; count++) {
1307 		subsystem->next_cntlid++;
1308 		if (subsystem->next_cntlid >= 0xFFF0) {
1309 			/* The spec reserves cntlid values in the range FFF0h to FFFFh. */
1310 			subsystem->next_cntlid = 1;
1311 		}
1312 
1313 		/* Check if a controller with this cntlid currently exists. */
1314 		if (spdk_nvmf_subsystem_get_ctrlr(subsystem, subsystem->next_cntlid) == NULL) {
1315 			/* Found unused cntlid */
1316 			return subsystem->next_cntlid;
1317 		}
1318 	}
1319 
1320 	/* All valid cntlid values are in use. */
1321 	return 0xFFFF;
1322 }
1323 
1324 int
1325 spdk_nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr)
1326 {
1327 	ctrlr->cntlid = spdk_nvmf_subsystem_gen_cntlid(subsystem);
1328 	if (ctrlr->cntlid == 0xFFFF) {
1329 		/* Unable to get a cntlid */
1330 		SPDK_ERRLOG("Reached max simultaneous ctrlrs\n");
1331 		return -EBUSY;
1332 	}
1333 
1334 	TAILQ_INSERT_TAIL(&subsystem->ctrlrs, ctrlr, link);
1335 
1336 	return 0;
1337 }
1338 
1339 void
1340 spdk_nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem,
1341 				 struct spdk_nvmf_ctrlr *ctrlr)
1342 {
1343 	assert(subsystem == ctrlr->subsys);
1344 	TAILQ_REMOVE(&subsystem->ctrlrs, ctrlr, link);
1345 }
1346 
1347 struct spdk_nvmf_ctrlr *
1348 spdk_nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, uint16_t cntlid)
1349 {
1350 	struct spdk_nvmf_ctrlr *ctrlr;
1351 
1352 	TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1353 		if (ctrlr->cntlid == cntlid) {
1354 			return ctrlr;
1355 		}
1356 	}
1357 
1358 	return NULL;
1359 }
1360 
1361 uint32_t
1362 spdk_nvmf_subsystem_get_max_namespaces(const struct spdk_nvmf_subsystem *subsystem)
1363 {
1364 	return subsystem->max_allowed_nsid;
1365 }
1366 
1367 struct _nvmf_ns_registrant {
1368 	uint64_t		rkey;
1369 	char			*host_uuid;
1370 };
1371 
1372 struct _nvmf_ns_registrants {
1373 	size_t				num_regs;
1374 	struct _nvmf_ns_registrant	reg[SPDK_NVMF_MAX_NUM_REGISTRANTS];
1375 };
1376 
1377 struct _nvmf_ns_reservation {
1378 	bool					ptpl_activated;
1379 	enum spdk_nvme_reservation_type		rtype;
1380 	uint64_t				crkey;
1381 	char					*bdev_uuid;
1382 	char					*holder_uuid;
1383 	struct _nvmf_ns_registrants		regs;
1384 };
1385 
1386 static const struct spdk_json_object_decoder nvmf_ns_pr_reg_decoders[] = {
1387 	{"rkey", offsetof(struct _nvmf_ns_registrant, rkey), spdk_json_decode_uint64},
1388 	{"host_uuid", offsetof(struct _nvmf_ns_registrant, host_uuid), spdk_json_decode_string},
1389 };
1390 
1391 static int
1392 nvmf_decode_ns_pr_reg(const struct spdk_json_val *val, void *out)
1393 {
1394 	struct _nvmf_ns_registrant *reg = out;
1395 
1396 	return spdk_json_decode_object(val, nvmf_ns_pr_reg_decoders,
1397 				       SPDK_COUNTOF(nvmf_ns_pr_reg_decoders), reg);
1398 }
1399 
1400 static int
1401 nvmf_decode_ns_pr_regs(const struct spdk_json_val *val, void *out)
1402 {
1403 	struct _nvmf_ns_registrants *regs = out;
1404 
1405 	return spdk_json_decode_array(val, nvmf_decode_ns_pr_reg, regs->reg,
1406 				      SPDK_NVMF_MAX_NUM_REGISTRANTS, &regs->num_regs,
1407 				      sizeof(struct _nvmf_ns_registrant));
1408 }
1409 
1410 static const struct spdk_json_object_decoder nvmf_ns_pr_decoders[] = {
1411 	{"ptpl", offsetof(struct _nvmf_ns_reservation, ptpl_activated), spdk_json_decode_bool, true},
1412 	{"rtype", offsetof(struct _nvmf_ns_reservation, rtype), spdk_json_decode_uint32, true},
1413 	{"crkey", offsetof(struct _nvmf_ns_reservation, crkey), spdk_json_decode_uint64, true},
1414 	{"bdev_uuid", offsetof(struct _nvmf_ns_reservation, bdev_uuid), spdk_json_decode_string},
1415 	{"holder_uuid", offsetof(struct _nvmf_ns_reservation, holder_uuid), spdk_json_decode_string, true},
1416 	{"registrants", offsetof(struct _nvmf_ns_reservation, regs), nvmf_decode_ns_pr_regs},
1417 };
1418 
1419 static int
1420 spdk_nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info)
1421 {
1422 	FILE *fd;
1423 	size_t json_size;
1424 	ssize_t values_cnt, rc;
1425 	void *json = NULL, *end;
1426 	struct spdk_json_val *values = NULL;
1427 	struct _nvmf_ns_reservation res = {};
1428 	uint32_t i;
1429 
1430 	fd = fopen(file, "r");
1431 	/* It's not an error if the file does not exist */
1432 	if (!fd) {
1433 		SPDK_NOTICELOG("File %s does not exist\n", file);
1434 		return -ENOENT;
1435 	}
1436 
1437 	/* Load all persist file contents into a local buffer */
1438 	json = spdk_posix_file_load(fd, &json_size);
1439 	fclose(fd);
1440 	if (!json) {
1441 		SPDK_ERRLOG("Load persit file %s failed\n", file);
1442 		return -ENOMEM;
1443 	}
1444 
1445 	rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0);
1446 	if (rc < 0) {
1447 		SPDK_NOTICELOG("Parsing JSON configuration failed (%zd)\n", rc);
1448 		goto exit;
1449 	}
1450 
1451 	values_cnt = rc;
1452 	values = calloc(values_cnt, sizeof(struct spdk_json_val));
1453 	if (values == NULL) {
1454 		goto exit;
1455 	}
1456 
1457 	rc = spdk_json_parse(json, json_size, values, values_cnt, &end, 0);
1458 	if (rc != values_cnt) {
1459 		SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
1460 		goto exit;
1461 	}
1462 
1463 	/* Decode json */
1464 	if (spdk_json_decode_object(values, nvmf_ns_pr_decoders,
1465 				    SPDK_COUNTOF(nvmf_ns_pr_decoders),
1466 				    &res)) {
1467 		SPDK_ERRLOG("Invalid objects in the persist file %s\n", file);
1468 		rc = -EINVAL;
1469 		goto exit;
1470 	}
1471 
1472 	if (res.regs.num_regs > SPDK_NVMF_MAX_NUM_REGISTRANTS) {
1473 		SPDK_ERRLOG("Can only support up to %u registrants\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
1474 		rc = -ERANGE;
1475 		goto exit;
1476 	}
1477 
1478 	rc = 0;
1479 	info->ptpl_activated = res.ptpl_activated;
1480 	info->rtype = res.rtype;
1481 	info->crkey = res.crkey;
1482 	snprintf(info->bdev_uuid, sizeof(info->bdev_uuid), "%s", res.bdev_uuid);
1483 	snprintf(info->holder_uuid, sizeof(info->holder_uuid), "%s", res.holder_uuid);
1484 	info->num_regs = res.regs.num_regs;
1485 	for (i = 0; i < res.regs.num_regs; i++) {
1486 		info->registrants[i].rkey = res.regs.reg[i].rkey;
1487 		snprintf(info->registrants[i].host_uuid, sizeof(info->registrants[i].host_uuid), "%s",
1488 			 res.regs.reg[i].host_uuid);
1489 	}
1490 
1491 exit:
1492 	free(json);
1493 	free(values);
1494 	free(res.bdev_uuid);
1495 	free(res.holder_uuid);
1496 	for (i = 0; i < res.regs.num_regs; i++) {
1497 		free(res.regs.reg[i].host_uuid);
1498 	}
1499 
1500 	return rc;
1501 }
1502 
1503 static bool
1504 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns);
1505 
1506 static int
1507 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
1508 {
1509 	uint32_t i;
1510 	struct spdk_nvmf_registrant *reg, *holder = NULL;
1511 	struct spdk_uuid bdev_uuid, holder_uuid;
1512 
1513 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "NSID %u, PTPL %u, Number of registrants %u\n",
1514 		      ns->nsid, info->ptpl_activated, info->num_regs);
1515 
1516 	/* it's not an error */
1517 	if (!info->ptpl_activated || !info->num_regs) {
1518 		return 0;
1519 	}
1520 
1521 	spdk_uuid_parse(&bdev_uuid, info->bdev_uuid);
1522 	if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) {
1523 		SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n");
1524 		return -EINVAL;
1525 	}
1526 
1527 	ns->crkey = info->crkey;
1528 	ns->rtype = info->rtype;
1529 	ns->ptpl_activated = info->ptpl_activated;
1530 	spdk_uuid_parse(&holder_uuid, info->holder_uuid);
1531 
1532 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Bdev UUID %s\n", info->bdev_uuid);
1533 	if (info->rtype) {
1534 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Holder UUID %s, RTYPE %u, RKEY 0x%"PRIx64"\n",
1535 			      info->holder_uuid, info->rtype, info->crkey);
1536 	}
1537 
1538 	for (i = 0; i < info->num_regs; i++) {
1539 		reg = calloc(1, sizeof(*reg));
1540 		if (!reg) {
1541 			return -ENOMEM;
1542 		}
1543 		spdk_uuid_parse(&reg->hostid, info->registrants[i].host_uuid);
1544 		reg->rkey = info->registrants[i].rkey;
1545 		TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
1546 		if (!spdk_uuid_compare(&holder_uuid, &reg->hostid)) {
1547 			holder = reg;
1548 		}
1549 		SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Registrant RKEY 0x%"PRIx64", Host UUID %s\n",
1550 			      info->registrants[i].rkey, info->registrants[i].host_uuid);
1551 	}
1552 
1553 	if (nvmf_ns_reservation_all_registrants_type(ns)) {
1554 		ns->holder = TAILQ_FIRST(&ns->registrants);
1555 	} else {
1556 		ns->holder = holder;
1557 	}
1558 
1559 	return 0;
1560 }
1561 
1562 static int
1563 spdk_nvmf_ns_json_write_cb(void *cb_ctx, const void *data, size_t size)
1564 {
1565 	char *file = cb_ctx;
1566 	size_t rc;
1567 	FILE *fd;
1568 
1569 	fd = fopen(file, "w");
1570 	if (!fd) {
1571 		SPDK_ERRLOG("Can't open file %s for write\n", file);
1572 		return -ENOENT;
1573 	}
1574 	rc = fwrite(data, 1, size, fd);
1575 	fclose(fd);
1576 
1577 	return rc == size ? 0 : -1;
1578 }
1579 
1580 static int
1581 spdk_nvmf_ns_reservation_update(const char *file, struct spdk_nvmf_reservation_info *info)
1582 {
1583 	struct spdk_json_write_ctx *w;
1584 	uint32_t i;
1585 	int rc = 0;
1586 
1587 	w = spdk_json_write_begin(spdk_nvmf_ns_json_write_cb, (void *)file, 0);
1588 	if (w == NULL) {
1589 		return -ENOMEM;
1590 	}
1591 	/* clear the configuration file */
1592 	if (!info->ptpl_activated) {
1593 		goto exit;
1594 	}
1595 
1596 	spdk_json_write_object_begin(w);
1597 	spdk_json_write_named_bool(w, "ptpl", info->ptpl_activated);
1598 	spdk_json_write_named_uint32(w, "rtype", info->rtype);
1599 	spdk_json_write_named_uint64(w, "crkey", info->crkey);
1600 	spdk_json_write_named_string(w, "bdev_uuid", info->bdev_uuid);
1601 	spdk_json_write_named_string(w, "holder_uuid", info->holder_uuid);
1602 
1603 	spdk_json_write_named_array_begin(w, "registrants");
1604 	for (i = 0; i < info->num_regs; i++) {
1605 		spdk_json_write_object_begin(w);
1606 		spdk_json_write_named_uint64(w, "rkey", info->registrants[i].rkey);
1607 		spdk_json_write_named_string(w, "host_uuid", info->registrants[i].host_uuid);
1608 		spdk_json_write_object_end(w);
1609 	}
1610 	spdk_json_write_array_end(w);
1611 	spdk_json_write_object_end(w);
1612 
1613 exit:
1614 	rc = spdk_json_write_end(w);
1615 	return rc;
1616 }
1617 
1618 static int
1619 nvmf_ns_update_reservation_info(struct spdk_nvmf_ns *ns)
1620 {
1621 	struct spdk_nvmf_reservation_info info;
1622 	struct spdk_nvmf_registrant *reg, *tmp;
1623 	uint32_t i = 0;
1624 
1625 	assert(ns != NULL);
1626 
1627 	if (!ns->bdev || !ns->ptpl_file) {
1628 		return 0;
1629 	}
1630 
1631 	memset(&info, 0, sizeof(info));
1632 	spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), spdk_bdev_get_uuid(ns->bdev));
1633 
1634 	if (ns->rtype) {
1635 		info.rtype = ns->rtype;
1636 		info.crkey = ns->crkey;
1637 		if (!nvmf_ns_reservation_all_registrants_type(ns)) {
1638 			assert(ns->holder != NULL);
1639 			spdk_uuid_fmt_lower(info.holder_uuid, sizeof(info.holder_uuid), &ns->holder->hostid);
1640 		}
1641 	}
1642 
1643 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
1644 		spdk_uuid_fmt_lower(info.registrants[i].host_uuid, sizeof(info.registrants[i].host_uuid),
1645 				    &reg->hostid);
1646 		info.registrants[i++].rkey = reg->rkey;
1647 	}
1648 
1649 	info.num_regs = i;
1650 	info.ptpl_activated = ns->ptpl_activated;
1651 
1652 	return spdk_nvmf_ns_reservation_update(ns->ptpl_file, &info);
1653 }
1654 
1655 static struct spdk_nvmf_registrant *
1656 nvmf_ns_reservation_get_registrant(struct spdk_nvmf_ns *ns,
1657 				   struct spdk_uuid *uuid)
1658 {
1659 	struct spdk_nvmf_registrant *reg, *tmp;
1660 
1661 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
1662 		if (!spdk_uuid_compare(&reg->hostid, uuid)) {
1663 			return reg;
1664 		}
1665 	}
1666 
1667 	return NULL;
1668 }
1669 
1670 /* Generate reservation notice log to registered HostID controllers */
1671 static void
1672 nvmf_subsystem_gen_ctrlr_notification(struct spdk_nvmf_subsystem *subsystem,
1673 				      struct spdk_nvmf_ns *ns,
1674 				      struct spdk_uuid *hostid_list,
1675 				      uint32_t num_hostid,
1676 				      enum spdk_nvme_reservation_notification_log_page_type type)
1677 {
1678 	struct spdk_nvmf_ctrlr *ctrlr;
1679 	uint32_t i;
1680 
1681 	for (i = 0; i < num_hostid; i++) {
1682 		TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1683 			if (!spdk_uuid_compare(&ctrlr->hostid, &hostid_list[i])) {
1684 				spdk_nvmf_ctrlr_reservation_notice_log(ctrlr, ns, type);
1685 			}
1686 		}
1687 	}
1688 }
1689 
1690 /* Get all registrants' hostid other than the controller who issued the command */
1691 static uint32_t
1692 nvmf_ns_reservation_get_all_other_hostid(struct spdk_nvmf_ns *ns,
1693 		struct spdk_uuid *hostid_list,
1694 		uint32_t max_num_hostid,
1695 		struct spdk_uuid *current_hostid)
1696 {
1697 	struct spdk_nvmf_registrant *reg, *tmp;
1698 	uint32_t num_hostid = 0;
1699 
1700 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
1701 		if (spdk_uuid_compare(&reg->hostid, current_hostid)) {
1702 			if (num_hostid == max_num_hostid) {
1703 				assert(false);
1704 				return max_num_hostid;
1705 			}
1706 			hostid_list[num_hostid++] = reg->hostid;
1707 		}
1708 	}
1709 
1710 	return num_hostid;
1711 }
1712 
1713 /* Calculate the unregistered HostID list according to list
1714  * prior to execute preempt command and list after executing
1715  * preempt command.
1716  */
1717 static uint32_t
1718 nvmf_ns_reservation_get_unregistered_hostid(struct spdk_uuid *old_hostid_list,
1719 		uint32_t old_num_hostid,
1720 		struct spdk_uuid *remaining_hostid_list,
1721 		uint32_t remaining_num_hostid)
1722 {
1723 	struct spdk_uuid temp_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
1724 	uint32_t i, j, num_hostid = 0;
1725 	bool found;
1726 
1727 	if (!remaining_num_hostid) {
1728 		return old_num_hostid;
1729 	}
1730 
1731 	for (i = 0; i < old_num_hostid; i++) {
1732 		found = false;
1733 		for (j = 0; j < remaining_num_hostid; j++) {
1734 			if (!spdk_uuid_compare(&old_hostid_list[i], &remaining_hostid_list[j])) {
1735 				found = true;
1736 				break;
1737 			}
1738 		}
1739 		if (!found) {
1740 			spdk_uuid_copy(&temp_hostid_list[num_hostid++], &old_hostid_list[i]);
1741 		}
1742 	}
1743 
1744 	if (num_hostid) {
1745 		memcpy(old_hostid_list, temp_hostid_list, sizeof(struct spdk_uuid) * num_hostid);
1746 	}
1747 
1748 	return num_hostid;
1749 }
1750 
1751 /* current reservation type is all registrants or not */
1752 static bool
1753 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns)
1754 {
1755 	return (ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
1756 		ns->rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS);
1757 }
1758 
1759 /* current registrant is reservation holder or not */
1760 static bool
1761 nvmf_ns_reservation_registrant_is_holder(struct spdk_nvmf_ns *ns,
1762 		struct spdk_nvmf_registrant *reg)
1763 {
1764 	if (!reg) {
1765 		return false;
1766 	}
1767 
1768 	if (nvmf_ns_reservation_all_registrants_type(ns)) {
1769 		return true;
1770 	}
1771 
1772 	return (ns->holder == reg);
1773 }
1774 
1775 static int
1776 nvmf_ns_reservation_add_registrant(struct spdk_nvmf_ns *ns,
1777 				   struct spdk_nvmf_ctrlr *ctrlr,
1778 				   uint64_t nrkey)
1779 {
1780 	struct spdk_nvmf_registrant *reg;
1781 
1782 	reg = calloc(1, sizeof(*reg));
1783 	if (!reg) {
1784 		return -ENOMEM;
1785 	}
1786 
1787 	reg->rkey = nrkey;
1788 	/* set hostid for the registrant */
1789 	spdk_uuid_copy(&reg->hostid, &ctrlr->hostid);
1790 	TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
1791 	ns->gen++;
1792 
1793 	return 0;
1794 }
1795 
1796 static void
1797 nvmf_ns_reservation_release_reservation(struct spdk_nvmf_ns *ns)
1798 {
1799 	ns->rtype = 0;
1800 	ns->crkey = 0;
1801 	ns->holder = NULL;
1802 }
1803 
1804 /* release the reservation if the last registrant was removed */
1805 static void
1806 nvmf_ns_reservation_check_release_on_remove_registrant(struct spdk_nvmf_ns *ns,
1807 		struct spdk_nvmf_registrant *reg)
1808 {
1809 	struct spdk_nvmf_registrant *next_reg;
1810 
1811 	/* no reservation holder */
1812 	if (!ns->holder) {
1813 		assert(ns->rtype == 0);
1814 		return;
1815 	}
1816 
1817 	next_reg = TAILQ_FIRST(&ns->registrants);
1818 	if (next_reg && nvmf_ns_reservation_all_registrants_type(ns)) {
1819 		/* the next valid registrant is the new holder now */
1820 		ns->holder = next_reg;
1821 	} else if (nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
1822 		/* release the reservation */
1823 		nvmf_ns_reservation_release_reservation(ns);
1824 	}
1825 }
1826 
1827 static void
1828 nvmf_ns_reservation_remove_registrant(struct spdk_nvmf_ns *ns,
1829 				      struct spdk_nvmf_registrant *reg)
1830 {
1831 	TAILQ_REMOVE(&ns->registrants, reg, link);
1832 	nvmf_ns_reservation_check_release_on_remove_registrant(ns, reg);
1833 	free(reg);
1834 	ns->gen++;
1835 	return;
1836 }
1837 
1838 static uint32_t
1839 nvmf_ns_reservation_remove_registrants_by_key(struct spdk_nvmf_ns *ns,
1840 		uint64_t rkey)
1841 {
1842 	struct spdk_nvmf_registrant *reg, *tmp;
1843 	uint32_t count = 0;
1844 
1845 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
1846 		if (reg->rkey == rkey) {
1847 			nvmf_ns_reservation_remove_registrant(ns, reg);
1848 			count++;
1849 		}
1850 	}
1851 	return count;
1852 }
1853 
1854 static uint32_t
1855 nvmf_ns_reservation_remove_all_other_registrants(struct spdk_nvmf_ns *ns,
1856 		struct spdk_nvmf_registrant *reg)
1857 {
1858 	struct spdk_nvmf_registrant *reg_tmp, *reg_tmp2;
1859 	uint32_t count = 0;
1860 
1861 	TAILQ_FOREACH_SAFE(reg_tmp, &ns->registrants, link, reg_tmp2) {
1862 		if (reg_tmp != reg) {
1863 			nvmf_ns_reservation_remove_registrant(ns, reg_tmp);
1864 			count++;
1865 		}
1866 	}
1867 	return count;
1868 }
1869 
1870 static uint32_t
1871 nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns)
1872 {
1873 	struct spdk_nvmf_registrant *reg, *reg_tmp;
1874 	uint32_t count = 0;
1875 
1876 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) {
1877 		nvmf_ns_reservation_remove_registrant(ns, reg);
1878 		count++;
1879 	}
1880 	return count;
1881 }
1882 
1883 static void
1884 nvmf_ns_reservation_acquire_reservation(struct spdk_nvmf_ns *ns, uint64_t rkey,
1885 					enum spdk_nvme_reservation_type rtype,
1886 					struct spdk_nvmf_registrant *holder)
1887 {
1888 	ns->rtype = rtype;
1889 	ns->crkey = rkey;
1890 	assert(ns->holder == NULL);
1891 	ns->holder = holder;
1892 }
1893 
1894 static bool
1895 nvmf_ns_reservation_register(struct spdk_nvmf_ns *ns,
1896 			     struct spdk_nvmf_ctrlr *ctrlr,
1897 			     struct spdk_nvmf_request *req)
1898 {
1899 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
1900 	uint8_t rrega, iekey, cptpl, rtype;
1901 	struct spdk_nvme_reservation_register_data key;
1902 	struct spdk_nvmf_registrant *reg;
1903 	uint8_t status = SPDK_NVME_SC_SUCCESS;
1904 	bool update_sgroup = false;
1905 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
1906 	uint32_t num_hostid = 0;
1907 	int rc;
1908 
1909 	rrega = cmd->cdw10 & 0x7u;
1910 	iekey = (cmd->cdw10 >> 3) & 0x1u;
1911 	cptpl = (cmd->cdw10 >> 30) & 0x3u;
1912 
1913 	if (req->data && req->length >= sizeof(key)) {
1914 		memcpy(&key, req->data, sizeof(key));
1915 	} else {
1916 		SPDK_ERRLOG("No key provided. Failing request.\n");
1917 		status = SPDK_NVME_SC_INVALID_FIELD;
1918 		goto exit;
1919 	}
1920 
1921 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "REGISTER: RREGA %u, IEKEY %u, CPTPL %u, "
1922 		      "NRKEY 0x%"PRIx64", NRKEY 0x%"PRIx64"\n",
1923 		      rrega, iekey, cptpl, key.crkey, key.nrkey);
1924 
1925 	if (cptpl == SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON) {
1926 		/* Ture to OFF state, and need to be updated in the configuration file */
1927 		if (ns->ptpl_activated) {
1928 			ns->ptpl_activated = 0;
1929 			update_sgroup = true;
1930 		}
1931 	} else if (cptpl == SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS) {
1932 		if (ns->ptpl_file == NULL) {
1933 			status = SPDK_NVME_SC_INVALID_FIELD;
1934 			goto exit;
1935 		} else if (ns->ptpl_activated == 0) {
1936 			ns->ptpl_activated = 1;
1937 			update_sgroup = true;
1938 		}
1939 	}
1940 
1941 	/* current Host Identifier has registrant or not */
1942 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
1943 
1944 	switch (rrega) {
1945 	case SPDK_NVME_RESERVE_REGISTER_KEY:
1946 		if (!reg) {
1947 			/* register new controller */
1948 			if (key.nrkey == 0) {
1949 				SPDK_ERRLOG("Can't register zeroed new key\n");
1950 				status = SPDK_NVME_SC_INVALID_FIELD;
1951 				goto exit;
1952 			}
1953 			rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey);
1954 			if (rc < 0) {
1955 				status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
1956 				goto exit;
1957 			}
1958 			update_sgroup = true;
1959 		} else {
1960 			/* register with same key is not an error */
1961 			if (reg->rkey != key.nrkey) {
1962 				SPDK_ERRLOG("The same host already register a "
1963 					    "key with 0x%"PRIx64"\n",
1964 					    reg->rkey);
1965 				status = SPDK_NVME_SC_RESERVATION_CONFLICT;
1966 				goto exit;
1967 			}
1968 		}
1969 		break;
1970 	case SPDK_NVME_RESERVE_UNREGISTER_KEY:
1971 		if (!reg || (!iekey && reg->rkey != key.crkey)) {
1972 			SPDK_ERRLOG("No registrant or current key doesn't match "
1973 				    "with existing registrant key\n");
1974 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
1975 			goto exit;
1976 		}
1977 
1978 		rtype = ns->rtype;
1979 		num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
1980 				SPDK_NVMF_MAX_NUM_REGISTRANTS,
1981 				&ctrlr->hostid);
1982 
1983 		nvmf_ns_reservation_remove_registrant(ns, reg);
1984 
1985 		if (!ns->rtype && num_hostid && (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY ||
1986 						 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY)) {
1987 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
1988 							      hostid_list,
1989 							      num_hostid,
1990 							      SPDK_NVME_RESERVATION_RELEASED);
1991 		}
1992 		update_sgroup = true;
1993 		break;
1994 	case SPDK_NVME_RESERVE_REPLACE_KEY:
1995 		if (!reg || (!iekey && reg->rkey != key.crkey)) {
1996 			SPDK_ERRLOG("No registrant or current key doesn't match "
1997 				    "with existing registrant key\n");
1998 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
1999 			goto exit;
2000 		}
2001 		if (key.nrkey == 0) {
2002 			SPDK_ERRLOG("Can't register zeroed new key\n");
2003 			status = SPDK_NVME_SC_INVALID_FIELD;
2004 			goto exit;
2005 		}
2006 		reg->rkey = key.nrkey;
2007 		update_sgroup = true;
2008 		break;
2009 	default:
2010 		status = SPDK_NVME_SC_INVALID_FIELD;
2011 		goto exit;
2012 	}
2013 
2014 exit:
2015 	if (update_sgroup) {
2016 		rc = nvmf_ns_update_reservation_info(ns);
2017 		if (rc != 0) {
2018 			status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2019 		}
2020 	}
2021 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2022 	req->rsp->nvme_cpl.status.sc = status;
2023 	return update_sgroup;
2024 }
2025 
2026 static bool
2027 nvmf_ns_reservation_acquire(struct spdk_nvmf_ns *ns,
2028 			    struct spdk_nvmf_ctrlr *ctrlr,
2029 			    struct spdk_nvmf_request *req)
2030 {
2031 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2032 	uint8_t racqa, iekey, rtype;
2033 	struct spdk_nvme_reservation_acquire_data key;
2034 	struct spdk_nvmf_registrant *reg;
2035 	bool all_regs = false;
2036 	uint32_t count = 0;
2037 	bool update_sgroup = true;
2038 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2039 	uint32_t num_hostid = 0;
2040 	struct spdk_uuid new_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2041 	uint32_t new_num_hostid = 0;
2042 	bool reservation_released = false;
2043 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2044 
2045 	racqa = cmd->cdw10 & 0x7u;
2046 	iekey = (cmd->cdw10 >> 3) & 0x1u;
2047 	rtype = (cmd->cdw10 >> 8) & 0xffu;
2048 
2049 	if (req->data && req->length >= sizeof(key)) {
2050 		memcpy(&key, req->data, sizeof(key));
2051 	} else {
2052 		SPDK_ERRLOG("No key provided. Failing request.\n");
2053 		status = SPDK_NVME_SC_INVALID_FIELD;
2054 		goto exit;
2055 	}
2056 
2057 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ACQUIRE: RACQA %u, IEKEY %u, RTYPE %u, "
2058 		      "NRKEY 0x%"PRIx64", PRKEY 0x%"PRIx64"\n",
2059 		      racqa, iekey, rtype, key.crkey, key.prkey);
2060 
2061 	if (iekey || rtype > SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) {
2062 		SPDK_ERRLOG("Ignore existing key field set to 1\n");
2063 		status = SPDK_NVME_SC_INVALID_FIELD;
2064 		update_sgroup = false;
2065 		goto exit;
2066 	}
2067 
2068 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2069 	/* must be registrant and CRKEY must match */
2070 	if (!reg || reg->rkey != key.crkey) {
2071 		SPDK_ERRLOG("No registrant or current key doesn't match "
2072 			    "with existing registrant key\n");
2073 		status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2074 		update_sgroup = false;
2075 		goto exit;
2076 	}
2077 
2078 	all_regs = nvmf_ns_reservation_all_registrants_type(ns);
2079 
2080 	switch (racqa) {
2081 	case SPDK_NVME_RESERVE_ACQUIRE:
2082 		/* it's not an error for the holder to acquire same reservation type again */
2083 		if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && ns->rtype == rtype) {
2084 			/* do nothing */
2085 			update_sgroup = false;
2086 		} else if (ns->holder == NULL) {
2087 			/* fisrt time to acquire the reservation */
2088 			nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
2089 		} else {
2090 			SPDK_ERRLOG("Invalid rtype or current registrant is not holder\n");
2091 			status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2092 			update_sgroup = false;
2093 			goto exit;
2094 		}
2095 		break;
2096 	case SPDK_NVME_RESERVE_PREEMPT:
2097 		/* no reservation holder */
2098 		if (!ns->holder) {
2099 			/* unregister with PRKEY */
2100 			nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2101 			break;
2102 		}
2103 		num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2104 				SPDK_NVMF_MAX_NUM_REGISTRANTS,
2105 				&ctrlr->hostid);
2106 
2107 		/* only 1 reservation holder and reservation key is valid */
2108 		if (!all_regs) {
2109 			/* preempt itself */
2110 			if (nvmf_ns_reservation_registrant_is_holder(ns, reg) &&
2111 			    ns->crkey == key.prkey) {
2112 				ns->rtype = rtype;
2113 				reservation_released = true;
2114 				break;
2115 			}
2116 
2117 			if (ns->crkey == key.prkey) {
2118 				nvmf_ns_reservation_remove_registrant(ns, ns->holder);
2119 				nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
2120 				reservation_released = true;
2121 			} else if (key.prkey != 0) {
2122 				nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2123 			} else {
2124 				/* PRKEY is zero */
2125 				SPDK_ERRLOG("Current PRKEY is zero\n");
2126 				status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2127 				update_sgroup = false;
2128 				goto exit;
2129 			}
2130 		} else {
2131 			/* release all other registrants except for the current one */
2132 			if (key.prkey == 0) {
2133 				nvmf_ns_reservation_remove_all_other_registrants(ns, reg);
2134 				assert(ns->holder == reg);
2135 			} else {
2136 				count = nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
2137 				if (count == 0) {
2138 					SPDK_ERRLOG("PRKEY doesn't match any registrant\n");
2139 					status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2140 					update_sgroup = false;
2141 					goto exit;
2142 				}
2143 			}
2144 		}
2145 		break;
2146 	default:
2147 		status = SPDK_NVME_SC_INVALID_FIELD;
2148 		update_sgroup = false;
2149 		break;
2150 	}
2151 
2152 exit:
2153 	if (update_sgroup && racqa == SPDK_NVME_RESERVE_PREEMPT) {
2154 		new_num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, new_hostid_list,
2155 				 SPDK_NVMF_MAX_NUM_REGISTRANTS,
2156 				 &ctrlr->hostid);
2157 		/* Preempt notification occurs on the unregistered controllers
2158 		 * other than the controller who issued the command.
2159 		 */
2160 		num_hostid = nvmf_ns_reservation_get_unregistered_hostid(hostid_list,
2161 				num_hostid,
2162 				new_hostid_list,
2163 				new_num_hostid);
2164 		if (num_hostid) {
2165 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2166 							      hostid_list,
2167 							      num_hostid,
2168 							      SPDK_NVME_REGISTRATION_PREEMPTED);
2169 
2170 		}
2171 		/* Reservation released notification occurs on the
2172 		 * controllers which are the remaining registrants other than
2173 		 * the controller who issued the command.
2174 		 */
2175 		if (reservation_released && new_num_hostid) {
2176 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2177 							      new_hostid_list,
2178 							      new_num_hostid,
2179 							      SPDK_NVME_RESERVATION_RELEASED);
2180 
2181 		}
2182 	}
2183 	if (update_sgroup && ns->ptpl_activated) {
2184 		if (nvmf_ns_update_reservation_info(ns)) {
2185 			status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2186 		}
2187 	}
2188 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2189 	req->rsp->nvme_cpl.status.sc = status;
2190 	return update_sgroup;
2191 }
2192 
2193 static bool
2194 nvmf_ns_reservation_release(struct spdk_nvmf_ns *ns,
2195 			    struct spdk_nvmf_ctrlr *ctrlr,
2196 			    struct spdk_nvmf_request *req)
2197 {
2198 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2199 	uint8_t rrela, iekey, rtype;
2200 	struct spdk_nvmf_registrant *reg;
2201 	uint64_t crkey;
2202 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2203 	bool update_sgroup = true;
2204 	struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2205 	uint32_t num_hostid = 0;
2206 
2207 	rrela = cmd->cdw10 & 0x7u;
2208 	iekey = (cmd->cdw10 >> 3) & 0x1u;
2209 	rtype = (cmd->cdw10 >> 8) & 0xffu;
2210 
2211 	if (req->data && req->length >= sizeof(crkey)) {
2212 		memcpy(&crkey, req->data, sizeof(crkey));
2213 	} else {
2214 		SPDK_ERRLOG("No key provided. Failing request.\n");
2215 		status = SPDK_NVME_SC_INVALID_FIELD;
2216 		goto exit;
2217 	}
2218 
2219 	SPDK_DEBUGLOG(SPDK_LOG_NVMF, "RELEASE: RRELA %u, IEKEY %u, RTYPE %u, "
2220 		      "CRKEY 0x%"PRIx64"\n",  rrela, iekey, rtype, crkey);
2221 
2222 	if (iekey) {
2223 		SPDK_ERRLOG("Ignore existing key field set to 1\n");
2224 		status = SPDK_NVME_SC_INVALID_FIELD;
2225 		update_sgroup = false;
2226 		goto exit;
2227 	}
2228 
2229 	reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
2230 	if (!reg || reg->rkey != crkey) {
2231 		SPDK_ERRLOG("No registrant or current key doesn't match "
2232 			    "with existing registrant key\n");
2233 		status = SPDK_NVME_SC_RESERVATION_CONFLICT;
2234 		update_sgroup = false;
2235 		goto exit;
2236 	}
2237 
2238 	num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
2239 			SPDK_NVMF_MAX_NUM_REGISTRANTS,
2240 			&ctrlr->hostid);
2241 
2242 	switch (rrela) {
2243 	case SPDK_NVME_RESERVE_RELEASE:
2244 		if (!ns->holder) {
2245 			SPDK_DEBUGLOG(SPDK_LOG_NVMF, "RELEASE: no holder\n");
2246 			update_sgroup = false;
2247 			goto exit;
2248 		}
2249 		if (ns->rtype != rtype) {
2250 			SPDK_ERRLOG("Type doesn't match\n");
2251 			status = SPDK_NVME_SC_INVALID_FIELD;
2252 			update_sgroup = false;
2253 			goto exit;
2254 		}
2255 		if (!nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
2256 			/* not the reservation holder, this isn't an error */
2257 			update_sgroup = false;
2258 			goto exit;
2259 		}
2260 
2261 		rtype = ns->rtype;
2262 		nvmf_ns_reservation_release_reservation(ns);
2263 
2264 		if (num_hostid && rtype != SPDK_NVME_RESERVE_WRITE_EXCLUSIVE &&
2265 		    rtype != SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
2266 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2267 							      hostid_list,
2268 							      num_hostid,
2269 							      SPDK_NVME_RESERVATION_RELEASED);
2270 		}
2271 		break;
2272 	case SPDK_NVME_RESERVE_CLEAR:
2273 		nvmf_ns_reservation_clear_all_registrants(ns);
2274 		if (num_hostid) {
2275 			nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
2276 							      hostid_list,
2277 							      num_hostid,
2278 							      SPDK_NVME_RESERVATION_PREEMPTED);
2279 		}
2280 		break;
2281 	default:
2282 		status = SPDK_NVME_SC_INVALID_FIELD;
2283 		update_sgroup = false;
2284 		goto exit;
2285 	}
2286 
2287 exit:
2288 	if (update_sgroup && ns->ptpl_activated) {
2289 		if (nvmf_ns_update_reservation_info(ns)) {
2290 			status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2291 		}
2292 	}
2293 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2294 	req->rsp->nvme_cpl.status.sc = status;
2295 	return update_sgroup;
2296 }
2297 
2298 static void
2299 nvmf_ns_reservation_report(struct spdk_nvmf_ns *ns,
2300 			   struct spdk_nvmf_ctrlr *ctrlr,
2301 			   struct spdk_nvmf_request *req)
2302 {
2303 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2304 	struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys;
2305 	struct spdk_nvmf_ctrlr *ctrlr_tmp;
2306 	struct spdk_nvmf_registrant *reg, *tmp;
2307 	struct spdk_nvme_reservation_status_extended_data *status_data;
2308 	struct spdk_nvme_registered_ctrlr_extended_data *ctrlr_data;
2309 	uint8_t *payload;
2310 	uint32_t len, count = 0;
2311 	uint32_t regctl = 0;
2312 	uint8_t status = SPDK_NVME_SC_SUCCESS;
2313 
2314 	if (req->data == NULL) {
2315 		SPDK_ERRLOG("No data transfer specified for request. "
2316 			    " Unable to transfer back response.\n");
2317 		status = SPDK_NVME_SC_INVALID_FIELD;
2318 		goto exit;
2319 	}
2320 
2321 	/* NVMeoF uses Extended Data Structure */
2322 	if ((cmd->cdw11 & 0x00000001u) == 0) {
2323 		SPDK_ERRLOG("NVMeoF uses extended controller data structure, "
2324 			    "please set EDS bit in cdw11 and try again\n");
2325 		status = SPDK_NVME_SC_INVALID_FIELD;
2326 		goto exit;
2327 	}
2328 
2329 	/* Get number of registerd controllers, one Host may have more than
2330 	 * one controller based on different ports.
2331 	 */
2332 	TAILQ_FOREACH(ctrlr_tmp, &subsystem->ctrlrs, link) {
2333 		reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr_tmp->hostid);
2334 		if (reg) {
2335 			regctl++;
2336 		}
2337 	}
2338 
2339 	len = sizeof(*status_data) + sizeof(*ctrlr_data) * regctl;
2340 	payload = calloc(1, len);
2341 	if (!payload) {
2342 		status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2343 		goto exit;
2344 	}
2345 
2346 	status_data = (struct spdk_nvme_reservation_status_extended_data *)payload;
2347 	status_data->data.gen = ns->gen;
2348 	status_data->data.rtype = ns->rtype;
2349 	status_data->data.regctl = regctl;
2350 	status_data->data.ptpls = ns->ptpl_activated;
2351 
2352 	TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2353 		assert(count <= regctl);
2354 		ctrlr_data = (struct spdk_nvme_registered_ctrlr_extended_data *)
2355 			     (payload + sizeof(*status_data) + sizeof(*ctrlr_data) * count);
2356 		/* Set to 0xffffh for dynamic controller */
2357 		ctrlr_data->cntlid = 0xffff;
2358 		ctrlr_data->rcsts.status = (ns->holder == reg) ? true : false;
2359 		ctrlr_data->rkey = reg->rkey;
2360 		spdk_uuid_copy((struct spdk_uuid *)ctrlr_data->hostid, &reg->hostid);
2361 		count++;
2362 	}
2363 
2364 	memcpy(req->data, payload, spdk_min(len, (cmd->cdw10 + 1) * sizeof(uint32_t)));
2365 	free(payload);
2366 
2367 exit:
2368 	req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2369 	req->rsp->nvme_cpl.status.sc = status;
2370 	return;
2371 }
2372 
2373 static void
2374 spdk_nvmf_ns_reservation_complete(void *ctx)
2375 {
2376 	struct spdk_nvmf_request *req = ctx;
2377 
2378 	spdk_nvmf_request_complete(req);
2379 }
2380 
2381 static void
2382 _nvmf_ns_reservation_update_done(struct spdk_nvmf_subsystem *subsystem,
2383 				 void *cb_arg, int status)
2384 {
2385 	struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)cb_arg;
2386 	struct spdk_nvmf_poll_group *group = req->qpair->group;
2387 
2388 	spdk_thread_send_msg(group->thread, spdk_nvmf_ns_reservation_complete, req);
2389 }
2390 
2391 void
2392 spdk_nvmf_ns_reservation_request(void *ctx)
2393 {
2394 	struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)ctx;
2395 	struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
2396 	struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
2397 	struct subsystem_update_ns_ctx *update_ctx;
2398 	uint32_t nsid;
2399 	struct spdk_nvmf_ns *ns;
2400 	bool update_sgroup = false;
2401 
2402 	nsid = cmd->nsid;
2403 	ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
2404 	assert(ns != NULL);
2405 
2406 	switch (cmd->opc) {
2407 	case SPDK_NVME_OPC_RESERVATION_REGISTER:
2408 		update_sgroup = nvmf_ns_reservation_register(ns, ctrlr, req);
2409 		break;
2410 	case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
2411 		update_sgroup = nvmf_ns_reservation_acquire(ns, ctrlr, req);
2412 		break;
2413 	case SPDK_NVME_OPC_RESERVATION_RELEASE:
2414 		update_sgroup = nvmf_ns_reservation_release(ns, ctrlr, req);
2415 		break;
2416 	case SPDK_NVME_OPC_RESERVATION_REPORT:
2417 		nvmf_ns_reservation_report(ns, ctrlr, req);
2418 		break;
2419 	default:
2420 		break;
2421 	}
2422 
2423 	/* update reservation information to subsystem's poll group */
2424 	if (update_sgroup) {
2425 		update_ctx = calloc(1, sizeof(*update_ctx));
2426 		if (update_ctx == NULL) {
2427 			SPDK_ERRLOG("Can't alloc subsystem poll group update context\n");
2428 			goto update_done;
2429 		}
2430 		update_ctx->subsystem = ctrlr->subsys;
2431 		update_ctx->cb_fn = _nvmf_ns_reservation_update_done;
2432 		update_ctx->cb_arg = req;
2433 
2434 		spdk_nvmf_subsystem_update_ns(ctrlr->subsys, subsystem_update_ns_done, update_ctx);
2435 		return;
2436 	}
2437 
2438 update_done:
2439 	_nvmf_ns_reservation_update_done(ctrlr->subsys, (void *)req, 0);
2440 }
2441