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