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