Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2016 Intel Corporation. All rights reserved.
3 : : * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
4 : : * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : #include "spdk/stdinc.h"
8 : :
9 : : #include "nvmf_internal.h"
10 : : #include "transport.h"
11 : :
12 : : #include "spdk/assert.h"
13 : : #include "spdk/likely.h"
14 : : #include "spdk/string.h"
15 : : #include "spdk/trace.h"
16 : : #include "spdk/nvmf_spec.h"
17 : : #include "spdk/uuid.h"
18 : : #include "spdk/json.h"
19 : : #include "spdk/file.h"
20 : : #include "spdk/bit_array.h"
21 : : #include "spdk/bdev.h"
22 : :
23 : : #define __SPDK_BDEV_MODULE_ONLY
24 : : #include "spdk/bdev_module.h"
25 : : #include "spdk/log.h"
26 : : #include "spdk_internal/utf.h"
27 : : #include "spdk_internal/usdt.h"
28 : :
29 : : #define MODEL_NUMBER_DEFAULT "SPDK bdev Controller"
30 : : #define NVMF_SUBSYSTEM_DEFAULT_NAMESPACES 32
31 : :
32 : : /*
33 : : * States for parsing valid domains in NQNs according to RFC 1034
34 : : */
35 : : enum spdk_nvmf_nqn_domain_states {
36 : : /* First character of a domain must be a letter */
37 : : SPDK_NVMF_DOMAIN_ACCEPT_LETTER = 0,
38 : :
39 : : /* Subsequent characters can be any of letter, digit, or hyphen */
40 : : SPDK_NVMF_DOMAIN_ACCEPT_LDH = 1,
41 : :
42 : : /* A domain label must end with either a letter or digit */
43 : : SPDK_NVMF_DOMAIN_ACCEPT_ANY = 2
44 : : };
45 : :
46 : : static int _nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem);
47 : :
48 : : /* Returns true if is a valid ASCII string as defined by the NVMe spec */
49 : : static bool
50 : 353 : nvmf_valid_ascii_string(const void *buf, size_t size)
51 : : {
52 : 353 : const uint8_t *str = buf;
53 : : size_t i;
54 : :
55 [ + + ]: 5372 : for (i = 0; i < size; i++) {
56 [ + + - + ]: 5025 : if (str[i] < 0x20 || str[i] > 0x7E) {
57 : 6 : return false;
58 : : }
59 : : }
60 : :
61 : 347 : return true;
62 : : }
63 : :
64 : : bool
65 : 1731 : nvmf_nqn_is_valid(const char *nqn)
66 : : {
67 : : size_t len;
68 : 420 : struct spdk_uuid uuid_value;
69 : : uint32_t i;
70 : : int bytes_consumed;
71 : : uint32_t domain_label_length;
72 : : char *reverse_domain_end;
73 : : uint32_t reverse_domain_end_index;
74 : 1731 : enum spdk_nvmf_nqn_domain_states domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER;
75 : :
76 : : /* Check for length requirements */
77 [ - + ]: 1731 : len = strlen(nqn);
78 [ + + ]: 1731 : if (len > SPDK_NVMF_NQN_MAX_LEN) {
79 : 3 : SPDK_ERRLOG("Invalid NQN \"%s\": length %zu > max %d\n", nqn, len, SPDK_NVMF_NQN_MAX_LEN);
80 : 3 : return false;
81 : : }
82 : :
83 : : /* The nqn must be at least as long as SPDK_NVMF_NQN_MIN_LEN to contain the necessary prefix. */
84 [ + + ]: 1728 : if (len < SPDK_NVMF_NQN_MIN_LEN) {
85 : 6 : SPDK_ERRLOG("Invalid NQN \"%s\": length %zu < min %d\n", nqn, len, SPDK_NVMF_NQN_MIN_LEN);
86 : 6 : return false;
87 : : }
88 : :
89 : : /* Check for discovery controller nqn */
90 [ + + + + ]: 1722 : if (!strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN)) {
91 : 793 : return true;
92 : : }
93 : :
94 : : /* Check for equality with the generic nqn structure of the form "nqn.2014-08.org.nvmexpress:uuid:11111111-2222-3333-4444-555555555555" */
95 [ + + + + ]: 929 : if (!strncmp(nqn, SPDK_NVMF_NQN_UUID_PRE, SPDK_NVMF_NQN_UUID_PRE_LEN)) {
96 [ + + ]: 337 : if (len != SPDK_NVMF_NQN_UUID_PRE_LEN + SPDK_NVMF_UUID_STRING_LEN) {
97 : 6 : SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not the correct length\n", nqn);
98 : 6 : return false;
99 : : }
100 : :
101 [ + + ]: 331 : if (spdk_uuid_parse(&uuid_value, &nqn[SPDK_NVMF_NQN_UUID_PRE_LEN])) {
102 : 6 : SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not formatted correctly\n", nqn);
103 : 6 : return false;
104 : : }
105 : 325 : return true;
106 : : }
107 : :
108 : : /* If the nqn does not match the uuid structure, the next several checks validate the form "nqn.yyyy-mm.reverse.domain:user-string" */
109 : :
110 [ - + - + ]: 592 : if (strncmp(nqn, "nqn.", 4) != 0) {
111 : 0 : SPDK_ERRLOG("Invalid NQN \"%s\": NQN must begin with \"nqn.\".\n", nqn);
112 : 0 : return false;
113 : : }
114 : :
115 : : /* Check for yyyy-mm. */
116 [ + - + - : 592 : if (!(isdigit(nqn[4]) && isdigit(nqn[5]) && isdigit(nqn[6]) && isdigit(nqn[7]) &&
+ - + - ]
117 [ + - + - : 592 : nqn[8] == '-' && isdigit(nqn[9]) && isdigit(nqn[10]) && nqn[11] == '.')) {
+ - - + ]
118 : 0 : SPDK_ERRLOG("Invalid date code in NQN \"%s\"\n", nqn);
119 : 0 : return false;
120 : : }
121 : :
122 [ - + ]: 592 : reverse_domain_end = strchr(nqn, ':');
123 [ + - + + ]: 592 : if (reverse_domain_end != NULL && (reverse_domain_end_index = reverse_domain_end - nqn) < len - 1) {
124 : : } else {
125 : 3 : SPDK_ERRLOG("Invalid NQN \"%s\". NQN must contain user specified name with a ':' as a prefix.\n",
126 : : nqn);
127 : 3 : return false;
128 : : }
129 : :
130 : : /* Check for valid reverse domain */
131 : 589 : domain_label_length = 0;
132 [ + + ]: 4928 : for (i = 12; i < reverse_domain_end_index; i++) {
133 [ + + ]: 4357 : if (domain_label_length > SPDK_DOMAIN_LABEL_MAX_LEN) {
134 : 3 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". At least one Label is too long.\n", nqn);
135 : 3 : return false;
136 : : }
137 : :
138 [ + + + - ]: 4354 : switch (domain_state) {
139 : :
140 : 1181 : case SPDK_NVMF_DOMAIN_ACCEPT_LETTER: {
141 [ + + ]: 1181 : if (isalpha(nqn[i])) {
142 : 1169 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
143 : 1169 : domain_label_length++;
144 : 1169 : break;
145 : : } else {
146 : 12 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must start with a letter.\n", nqn);
147 : 12 : return false;
148 : : }
149 : : }
150 : :
151 : 12 : case SPDK_NVMF_DOMAIN_ACCEPT_LDH: {
152 [ + + - + ]: 12 : if (isalpha(nqn[i]) || isdigit(nqn[i])) {
153 : 9 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
154 : 9 : domain_label_length++;
155 : 9 : break;
156 [ + - ]: 3 : } else if (nqn[i] == '-') {
157 [ - + ]: 3 : if (i == reverse_domain_end_index - 1) {
158 : 0 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
159 : : nqn);
160 : 0 : return false;
161 : : }
162 : 3 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH;
163 : 3 : domain_label_length++;
164 : 3 : break;
165 [ # # ]: 0 : } else if (nqn[i] == '.') {
166 : 0 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
167 : : nqn);
168 : 0 : return false;
169 : : } else {
170 : 0 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n",
171 : : nqn);
172 : 0 : return false;
173 : : }
174 : : }
175 : :
176 : 3161 : case SPDK_NVMF_DOMAIN_ACCEPT_ANY: {
177 [ + + - + ]: 3161 : if (isalpha(nqn[i]) || isdigit(nqn[i])) {
178 : 2557 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY;
179 : 2557 : domain_label_length++;
180 : 2557 : break;
181 [ + + ]: 604 : } else if (nqn[i] == '-') {
182 [ + + ]: 12 : if (i == reverse_domain_end_index - 1) {
183 : 3 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n",
184 : : nqn);
185 : 3 : return false;
186 : : }
187 : 9 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH;
188 : 9 : domain_label_length++;
189 : 9 : break;
190 [ + - ]: 592 : } else if (nqn[i] == '.') {
191 : 592 : domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER;
192 : 592 : domain_label_length = 0;
193 : 592 : break;
194 : : } else {
195 : 0 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n",
196 : : nqn);
197 : 0 : return false;
198 : : }
199 : : }
200 : : }
201 : 304 : }
202 : :
203 : 571 : i = reverse_domain_end_index + 1;
204 [ + + ]: 4703 : while (i < len) {
205 : 4135 : bytes_consumed = utf8_valid(&nqn[i], &nqn[len]);
206 [ + + ]: 4135 : if (bytes_consumed <= 0) {
207 : 3 : SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only valid utf-8.\n", nqn);
208 : 3 : return false;
209 : : }
210 : :
211 : 4132 : i += bytes_consumed;
212 : : }
213 : 568 : return true;
214 : : }
215 : :
216 : : static void subsystem_state_change_on_pg(struct spdk_io_channel_iter *i);
217 : :
218 : : struct spdk_nvmf_subsystem *
219 : 1241 : spdk_nvmf_subsystem_create(struct spdk_nvmf_tgt *tgt,
220 : : const char *nqn,
221 : : enum spdk_nvmf_subtype type,
222 : : uint32_t num_ns)
223 : : {
224 : : struct spdk_nvmf_subsystem *subsystem;
225 : : uint32_t sid;
226 : :
227 [ - + ]: 1241 : if (spdk_nvmf_tgt_find_subsystem(tgt, nqn)) {
228 : 0 : SPDK_ERRLOG("Subsystem NQN '%s' already exists\n", nqn);
229 : 0 : return NULL;
230 : : }
231 : :
232 [ + + ]: 1241 : if (!nvmf_nqn_is_valid(nqn)) {
233 : 33 : SPDK_ERRLOG("Subsystem NQN '%s' is invalid\n", nqn);
234 : 33 : return NULL;
235 : : }
236 : :
237 [ + + - + ]: 1208 : if (type == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT ||
238 : : type == SPDK_NVMF_SUBTYPE_DISCOVERY) {
239 [ - + ]: 772 : if (num_ns != 0) {
240 : 0 : SPDK_ERRLOG("Discovery subsystem cannot have namespaces.\n");
241 : 0 : return NULL;
242 : : }
243 [ + + ]: 436 : } else if (num_ns == 0) {
244 : 367 : num_ns = NVMF_SUBSYSTEM_DEFAULT_NAMESPACES;
245 : : }
246 : :
247 : : /* Find a free subsystem id (sid) */
248 : 1208 : sid = spdk_bit_array_find_first_clear(tgt->subsystem_ids, 0);
249 [ - + ]: 1208 : if (sid == UINT32_MAX) {
250 : 0 : SPDK_ERRLOG("No free subsystem IDs are available for subsystem creation\n");
251 : 0 : return NULL;
252 : : }
253 : 1208 : subsystem = calloc(1, sizeof(struct spdk_nvmf_subsystem));
254 [ - + ]: 1208 : if (subsystem == NULL) {
255 : 0 : SPDK_ERRLOG("Subsystem memory allocation failed\n");
256 : 0 : return NULL;
257 : : }
258 : :
259 : 1208 : subsystem->thread = spdk_get_thread();
260 : 1208 : subsystem->state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
261 : 1208 : subsystem->tgt = tgt;
262 : 1208 : subsystem->id = sid;
263 : 1208 : subsystem->subtype = type;
264 : 1208 : subsystem->max_nsid = num_ns;
265 : 1208 : subsystem->next_cntlid = 0;
266 : 1208 : subsystem->min_cntlid = NVMF_MIN_CNTLID;
267 : 1208 : subsystem->max_cntlid = NVMF_MAX_CNTLID;
268 [ - + ]: 1208 : snprintf(subsystem->subnqn, sizeof(subsystem->subnqn), "%s", nqn);
269 [ - + ]: 1208 : pthread_mutex_init(&subsystem->mutex, NULL);
270 : 1208 : TAILQ_INIT(&subsystem->listeners);
271 : 1208 : TAILQ_INIT(&subsystem->hosts);
272 : 1208 : TAILQ_INIT(&subsystem->ctrlrs);
273 : 1208 : TAILQ_INIT(&subsystem->state_changes);
274 : 1208 : subsystem->used_listener_ids = spdk_bit_array_create(NVMF_MAX_LISTENERS_PER_SUBSYSTEM);
275 [ - + ]: 1208 : if (subsystem->used_listener_ids == NULL) {
276 [ # # ]: 0 : pthread_mutex_destroy(&subsystem->mutex);
277 : 0 : free(subsystem);
278 : 0 : SPDK_ERRLOG("Listener id array memory allocation failed\n");
279 : 0 : return NULL;
280 : : }
281 : :
282 [ + + ]: 1208 : if (num_ns != 0) {
283 : 436 : subsystem->ns = calloc(num_ns, sizeof(struct spdk_nvmf_ns *));
284 [ - + ]: 436 : if (subsystem->ns == NULL) {
285 : 0 : SPDK_ERRLOG("Namespace memory allocation failed\n");
286 [ # # ]: 0 : pthread_mutex_destroy(&subsystem->mutex);
287 : 0 : spdk_bit_array_free(&subsystem->used_listener_ids);
288 : 0 : free(subsystem);
289 : 0 : return NULL;
290 : : }
291 : 436 : subsystem->ana_group = calloc(num_ns, sizeof(uint32_t));
292 [ - + ]: 436 : if (subsystem->ana_group == NULL) {
293 : 0 : SPDK_ERRLOG("ANA group memory allocation failed\n");
294 [ # # ]: 0 : pthread_mutex_destroy(&subsystem->mutex);
295 : 0 : free(subsystem->ns);
296 : 0 : spdk_bit_array_free(&subsystem->used_listener_ids);
297 : 0 : free(subsystem);
298 : 0 : return NULL;
299 : : }
300 : : }
301 : :
302 [ - + ]: 1208 : memset(subsystem->sn, '0', sizeof(subsystem->sn) - 1);
303 : 1208 : subsystem->sn[sizeof(subsystem->sn) - 1] = '\0';
304 : :
305 [ - + ]: 1208 : snprintf(subsystem->mn, sizeof(subsystem->mn), "%s",
306 : : MODEL_NUMBER_DEFAULT);
307 : :
308 : 1208 : spdk_bit_array_set(tgt->subsystem_ids, sid);
309 : 1208 : RB_INSERT(subsystem_tree, &tgt->subsystems, subsystem);
310 : :
311 : 294 : SPDK_DTRACE_PROBE1(nvmf_subsystem_create, subsystem->subnqn);
312 : :
313 : 1208 : return subsystem;
314 : : }
315 : :
316 : : static void
317 : 391 : nvmf_host_free(struct spdk_nvmf_host *host)
318 : : {
319 : 391 : spdk_keyring_put_key(host->dhchap_key);
320 : 391 : spdk_keyring_put_key(host->dhchap_ctrlr_key);
321 : 391 : free(host);
322 : 391 : }
323 : :
324 : : /* Must hold subsystem->mutex while calling this function */
325 : : static void
326 : 391 : nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_host *host)
327 : : {
328 [ + + ]: 391 : TAILQ_REMOVE(&subsystem->hosts, host, link);
329 : 391 : nvmf_host_free(host);
330 : 391 : }
331 : :
332 : : static void
333 : 502 : _nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem,
334 : : struct spdk_nvmf_subsystem_listener *listener,
335 : : bool stop)
336 : : {
337 : : struct spdk_nvmf_transport *transport;
338 : : struct spdk_nvmf_ctrlr *ctrlr;
339 : :
340 [ + + ]: 502 : if (stop) {
341 : 206 : transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, listener->trid->trstring);
342 [ + - ]: 206 : if (transport != NULL) {
343 : 206 : spdk_nvmf_transport_stop_listen(transport, listener->trid);
344 : : }
345 : : }
346 : :
347 [ + + ]: 544 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
348 [ + + ]: 42 : if (ctrlr->listener == listener) {
349 : 35 : ctrlr->listener = NULL;
350 : : }
351 : : }
352 : :
353 [ + + ]: 502 : TAILQ_REMOVE(&subsystem->listeners, listener, link);
354 : 502 : nvmf_update_discovery_log(listener->subsystem->tgt, NULL);
355 : 502 : free(listener->ana_state);
356 : 502 : spdk_bit_array_clear(subsystem->used_listener_ids, listener->id);
357 : 502 : free(listener->opts.sock_impl);
358 : 502 : free(listener);
359 : 502 : }
360 : :
361 : : static void
362 : 14 : _nvmf_subsystem_destroy_msg(void *cb_arg)
363 : : {
364 : 14 : struct spdk_nvmf_subsystem *subsystem = cb_arg;
365 : :
366 : 14 : _nvmf_subsystem_destroy(subsystem);
367 : 14 : }
368 : :
369 : : static int
370 : 1222 : _nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem)
371 : : {
372 : : struct nvmf_subsystem_state_change_ctx *ctx;
373 : : struct spdk_nvmf_ns *ns;
374 : 1222 : nvmf_subsystem_destroy_cb async_destroy_cb = NULL;
375 : 1222 : void *async_destroy_cb_arg = NULL;
376 : : int rc;
377 : :
378 [ + + ]: 1222 : if (!TAILQ_EMPTY(&subsystem->ctrlrs)) {
379 [ - + - + ]: 14 : SPDK_DEBUGLOG(nvmf, "subsystem %p %s has active controllers\n", subsystem, subsystem->subnqn);
380 : 14 : subsystem->async_destroy = true;
381 : 14 : rc = spdk_thread_send_msg(subsystem->thread, _nvmf_subsystem_destroy_msg, subsystem);
382 [ - + ]: 14 : if (rc) {
383 : 0 : SPDK_ERRLOG("Failed to send thread msg, rc %d\n", rc);
384 : 0 : assert(0);
385 : : return rc;
386 : : }
387 : 14 : return -EINPROGRESS;
388 : : }
389 : :
390 : 1208 : ns = spdk_nvmf_subsystem_get_first_ns(subsystem);
391 [ + + ]: 1527 : while (ns != NULL) {
392 : 319 : struct spdk_nvmf_ns *next_ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns);
393 : :
394 : 319 : spdk_nvmf_subsystem_remove_ns(subsystem, ns->opts.nsid);
395 : 319 : ns = next_ns;
396 : : }
397 : :
398 [ - + ]: 1208 : while ((ctx = TAILQ_FIRST(&subsystem->state_changes))) {
399 : 0 : SPDK_WARNLOG("subsystem %s has pending state change requests\n", subsystem->subnqn);
400 [ # # ]: 0 : TAILQ_REMOVE(&subsystem->state_changes, ctx, link);
401 [ # # ]: 0 : if (ctx->cb_fn != NULL) {
402 : 0 : ctx->cb_fn(subsystem, ctx->cb_arg, -ECANCELED);
403 : : }
404 : 0 : free(ctx);
405 : : }
406 : :
407 : 1208 : free(subsystem->ns);
408 : 1208 : free(subsystem->ana_group);
409 : :
410 : 1208 : RB_REMOVE(subsystem_tree, &subsystem->tgt->subsystems, subsystem);
411 [ - + ]: 1208 : assert(spdk_bit_array_get(subsystem->tgt->subsystem_ids, subsystem->id) == true);
412 : 1208 : spdk_bit_array_clear(subsystem->tgt->subsystem_ids, subsystem->id);
413 : :
414 [ - + ]: 1208 : pthread_mutex_destroy(&subsystem->mutex);
415 : :
416 : 1208 : spdk_bit_array_free(&subsystem->used_listener_ids);
417 : :
418 [ - + + + ]: 1208 : if (subsystem->async_destroy) {
419 : 7 : async_destroy_cb = subsystem->async_destroy_cb;
420 : 7 : async_destroy_cb_arg = subsystem->async_destroy_cb_arg;
421 : : }
422 : :
423 : 1208 : free(subsystem);
424 : :
425 [ + + ]: 1208 : if (async_destroy_cb) {
426 : 7 : async_destroy_cb(async_destroy_cb_arg);
427 : : }
428 : :
429 : 1208 : return 0;
430 : : }
431 : :
432 : : static struct spdk_nvmf_ns *
433 : 0 : _nvmf_subsystem_get_first_zoned_ns(struct spdk_nvmf_subsystem *subsystem)
434 : : {
435 : 0 : struct spdk_nvmf_ns *ns = spdk_nvmf_subsystem_get_first_ns(subsystem);
436 [ # # ]: 0 : while (ns != NULL) {
437 [ # # ]: 0 : if (ns->csi == SPDK_NVME_CSI_ZNS) {
438 : 0 : return ns;
439 : : }
440 : 0 : ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns);
441 : : }
442 : 0 : return NULL;
443 : : }
444 : :
445 : : int
446 : 1208 : spdk_nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem, nvmf_subsystem_destroy_cb cpl_cb,
447 : : void *cpl_cb_arg)
448 : : {
449 : : struct spdk_nvmf_host *host, *host_tmp;
450 : : struct spdk_nvmf_transport *transport;
451 : :
452 [ - + ]: 1208 : if (!subsystem) {
453 : 0 : return -EINVAL;
454 : : }
455 : :
456 : 294 : SPDK_DTRACE_PROBE1(nvmf_subsystem_destroy, subsystem->subnqn);
457 : :
458 [ - + ]: 1208 : assert(spdk_get_thread() == subsystem->thread);
459 : :
460 [ - + ]: 1208 : if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
461 : 0 : SPDK_ERRLOG("Subsystem can only be destroyed in inactive state, %s state %d\n",
462 : : subsystem->subnqn, subsystem->state);
463 : 0 : return -EAGAIN;
464 : : }
465 [ - + - + ]: 1208 : if (subsystem->destroying) {
466 : 0 : SPDK_ERRLOG("Subsystem destruction is already started\n");
467 : 0 : assert(0);
468 : : return -EALREADY;
469 : : }
470 : :
471 : 1208 : subsystem->destroying = true;
472 : :
473 [ - + - + ]: 1208 : SPDK_DEBUGLOG(nvmf, "subsystem is %p %s\n", subsystem, subsystem->subnqn);
474 : :
475 : 1208 : nvmf_subsystem_remove_all_listeners(subsystem, false);
476 : :
477 [ - + ]: 1208 : pthread_mutex_lock(&subsystem->mutex);
478 : :
479 [ + + ]: 1259 : TAILQ_FOREACH_SAFE(host, &subsystem->hosts, link, host_tmp) {
480 [ + + ]: 102 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
481 : 51 : transport = spdk_nvmf_transport_get_next(transport)) {
482 [ + + ]: 51 : if (transport->ops->subsystem_remove_host) {
483 : 48 : transport->ops->subsystem_remove_host(transport, subsystem, host->nqn);
484 : : }
485 : : }
486 : 51 : nvmf_subsystem_remove_host(subsystem, host);
487 : : }
488 : :
489 [ - + ]: 1208 : pthread_mutex_unlock(&subsystem->mutex);
490 : :
491 : 1208 : subsystem->async_destroy_cb = cpl_cb;
492 : 1208 : subsystem->async_destroy_cb_arg = cpl_cb_arg;
493 : :
494 : 1208 : return _nvmf_subsystem_destroy(subsystem);
495 : : }
496 : :
497 : : /* we have to use the typedef in the function declaration to appease astyle. */
498 : : typedef enum spdk_nvmf_subsystem_state spdk_nvmf_subsystem_state_t;
499 : :
500 : : static spdk_nvmf_subsystem_state_t
501 : 17633 : nvmf_subsystem_get_intermediate_state(enum spdk_nvmf_subsystem_state current_state,
502 : : enum spdk_nvmf_subsystem_state requested_state)
503 : : {
504 [ + + + - ]: 17633 : switch (requested_state) {
505 : 1148 : case SPDK_NVMF_SUBSYSTEM_INACTIVE:
506 : 1148 : return SPDK_NVMF_SUBSYSTEM_DEACTIVATING;
507 : 8815 : case SPDK_NVMF_SUBSYSTEM_ACTIVE:
508 [ + + ]: 8815 : if (current_state == SPDK_NVMF_SUBSYSTEM_PAUSED) {
509 : 7667 : return SPDK_NVMF_SUBSYSTEM_RESUMING;
510 : : } else {
511 : 1148 : return SPDK_NVMF_SUBSYSTEM_ACTIVATING;
512 : : }
513 : 7670 : case SPDK_NVMF_SUBSYSTEM_PAUSED:
514 : 7670 : return SPDK_NVMF_SUBSYSTEM_PAUSING;
515 : 0 : default:
516 : 0 : assert(false);
517 : : return SPDK_NVMF_SUBSYSTEM_NUM_STATES;
518 : : }
519 : : }
520 : :
521 : : static int
522 : 35266 : nvmf_subsystem_set_state(struct spdk_nvmf_subsystem *subsystem,
523 : : enum spdk_nvmf_subsystem_state state)
524 : : {
525 : 2430 : enum spdk_nvmf_subsystem_state actual_old_state, expected_old_state;
526 : : bool exchanged;
527 : :
528 [ + + + + : 35266 : switch (state) {
+ + + - ]
529 : 1148 : case SPDK_NVMF_SUBSYSTEM_INACTIVE:
530 : 1148 : expected_old_state = SPDK_NVMF_SUBSYSTEM_DEACTIVATING;
531 : 1148 : break;
532 : 1148 : case SPDK_NVMF_SUBSYSTEM_ACTIVATING:
533 : 1148 : expected_old_state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
534 : 1148 : break;
535 : 8815 : case SPDK_NVMF_SUBSYSTEM_ACTIVE:
536 : 8815 : expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING;
537 : 8815 : break;
538 : 7670 : case SPDK_NVMF_SUBSYSTEM_PAUSING:
539 : 7670 : expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
540 : 7670 : break;
541 : 7670 : case SPDK_NVMF_SUBSYSTEM_PAUSED:
542 : 7670 : expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSING;
543 : 7670 : break;
544 : 7667 : case SPDK_NVMF_SUBSYSTEM_RESUMING:
545 : 7667 : expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSED;
546 : 7667 : break;
547 : 1148 : case SPDK_NVMF_SUBSYSTEM_DEACTIVATING:
548 : 1148 : expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
549 : 1148 : break;
550 : 0 : default:
551 : 0 : assert(false);
552 : : return -1;
553 : : }
554 : :
555 : 35266 : actual_old_state = expected_old_state;
556 : 35266 : exchanged = __atomic_compare_exchange_n(&subsystem->state, &actual_old_state, state, false,
557 : : __ATOMIC_RELAXED, __ATOMIC_RELAXED);
558 [ + + ]: 35266 : if (spdk_unlikely(exchanged == false)) {
559 [ + + + - ]: 7670 : if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING &&
560 : : state == SPDK_NVMF_SUBSYSTEM_ACTIVE) {
561 : 7667 : expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING;
562 : : }
563 : : /* This is for the case when activating the subsystem fails. */
564 [ - + - - ]: 7670 : if (actual_old_state == SPDK_NVMF_SUBSYSTEM_ACTIVATING &&
565 : : state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING) {
566 : 0 : expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING;
567 : : }
568 : : /* This is for the case when resuming the subsystem fails. */
569 [ + + - + ]: 7670 : if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING &&
570 : : state == SPDK_NVMF_SUBSYSTEM_PAUSING) {
571 : 0 : expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING;
572 : : }
573 : : /* This is for the case when stopping paused subsystem */
574 [ + + + - ]: 7670 : if (actual_old_state == SPDK_NVMF_SUBSYSTEM_PAUSED &&
575 : : state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING) {
576 : 3 : expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSED;
577 : : }
578 : 7670 : actual_old_state = expected_old_state;
579 : 7670 : __atomic_compare_exchange_n(&subsystem->state, &actual_old_state, state, false,
580 : : __ATOMIC_RELAXED, __ATOMIC_RELAXED);
581 : : }
582 [ - + ]: 35266 : assert(actual_old_state == expected_old_state);
583 : 35266 : return actual_old_state - expected_old_state;
584 : : }
585 : :
586 : : static void nvmf_subsystem_do_state_change(struct nvmf_subsystem_state_change_ctx *ctx);
587 : :
588 : : static void
589 : 17636 : _nvmf_subsystem_state_change_complete(void *_ctx)
590 : : {
591 : 17636 : struct nvmf_subsystem_state_change_ctx *next, *ctx = _ctx;
592 : 17636 : struct spdk_nvmf_subsystem *subsystem = ctx->subsystem;
593 : :
594 [ - + ]: 17636 : pthread_mutex_lock(&subsystem->mutex);
595 [ - + ]: 17636 : assert(TAILQ_FIRST(&subsystem->state_changes) == ctx);
596 [ + + ]: 17636 : TAILQ_REMOVE(&subsystem->state_changes, ctx, link);
597 : 17636 : next = TAILQ_FIRST(&subsystem->state_changes);
598 [ - + ]: 17636 : pthread_mutex_unlock(&subsystem->mutex);
599 : :
600 [ + + ]: 17636 : if (ctx->cb_fn != NULL) {
601 : 17178 : ctx->cb_fn(subsystem, ctx->cb_arg, ctx->status);
602 : : }
603 : 17636 : free(ctx);
604 : :
605 [ + + ]: 17636 : if (next != NULL) {
606 : 3 : nvmf_subsystem_do_state_change(next);
607 : : }
608 : 17636 : }
609 : :
610 : : static void
611 : 17636 : nvmf_subsystem_state_change_complete(struct nvmf_subsystem_state_change_ctx *ctx, int status)
612 : : {
613 : 17636 : ctx->status = status;
614 : 17636 : spdk_thread_exec_msg(ctx->thread, _nvmf_subsystem_state_change_complete, ctx);
615 : 17636 : }
616 : :
617 : : static void
618 : 0 : subsystem_state_change_revert_done(struct spdk_io_channel_iter *i, int status)
619 : : {
620 : 0 : struct nvmf_subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
621 : :
622 : : /* Nothing to be done here if the state setting fails, we are just screwed. */
623 [ # # ]: 0 : if (nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state)) {
624 : 0 : SPDK_ERRLOG("Unable to revert the subsystem state after operation failure.\n");
625 : : }
626 : :
627 : : /* return a failure here. This function only exists in an error path. */
628 : 0 : nvmf_subsystem_state_change_complete(ctx, -1);
629 : 0 : }
630 : :
631 : : static void
632 : 17633 : subsystem_state_change_done(struct spdk_io_channel_iter *i, int status)
633 : : {
634 : 17633 : struct nvmf_subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
635 : : enum spdk_nvmf_subsystem_state intermediate_state;
636 : :
637 : 6532 : SPDK_DTRACE_PROBE4(nvmf_subsystem_change_state_done, ctx->subsystem->subnqn,
638 : : ctx->requested_state, ctx->original_state, status);
639 : :
640 [ + - ]: 17633 : if (status == 0) {
641 : 17633 : status = nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state);
642 [ - + ]: 17633 : if (status) {
643 : 0 : status = -1;
644 : : }
645 : : }
646 : :
647 [ - + ]: 17633 : if (status) {
648 : 0 : intermediate_state = nvmf_subsystem_get_intermediate_state(ctx->requested_state,
649 : : ctx->original_state);
650 [ # # ]: 0 : assert(intermediate_state != SPDK_NVMF_SUBSYSTEM_NUM_STATES);
651 : :
652 [ # # ]: 0 : if (nvmf_subsystem_set_state(ctx->subsystem, intermediate_state)) {
653 : 0 : goto out;
654 : : }
655 : 0 : ctx->requested_state = ctx->original_state;
656 : 0 : spdk_for_each_channel(ctx->subsystem->tgt,
657 : : subsystem_state_change_on_pg,
658 : : ctx,
659 : : subsystem_state_change_revert_done);
660 : 0 : return;
661 : : }
662 : :
663 : 17633 : out:
664 : 17633 : nvmf_subsystem_state_change_complete(ctx, status);
665 : : }
666 : :
667 : : static void
668 : 43394 : subsystem_state_change_continue(void *ctx, int status)
669 : : {
670 : 43394 : struct spdk_io_channel_iter *i = ctx;
671 : : struct nvmf_subsystem_state_change_ctx *_ctx __attribute__((unused));
672 : :
673 : 43394 : _ctx = spdk_io_channel_iter_get_ctx(i);
674 : 14612 : SPDK_DTRACE_PROBE3(nvmf_pg_change_state_done, _ctx->subsystem->subnqn,
675 : : _ctx->requested_state, spdk_thread_get_id(spdk_get_thread()));
676 : :
677 : 43394 : spdk_for_each_channel_continue(i, status);
678 : 43394 : }
679 : :
680 : : static void
681 : 43394 : subsystem_state_change_on_pg(struct spdk_io_channel_iter *i)
682 : : {
683 : : struct nvmf_subsystem_state_change_ctx *ctx;
684 : : struct spdk_io_channel *ch;
685 : : struct spdk_nvmf_poll_group *group;
686 : :
687 : 43394 : ctx = spdk_io_channel_iter_get_ctx(i);
688 : 43394 : ch = spdk_io_channel_iter_get_channel(i);
689 : 43394 : group = spdk_io_channel_get_ctx(ch);
690 : :
691 : 14612 : SPDK_DTRACE_PROBE3(nvmf_pg_change_state, ctx->subsystem->subnqn,
692 : : ctx->requested_state, spdk_thread_get_id(spdk_get_thread()));
693 [ + + + - ]: 43394 : switch (ctx->requested_state) {
694 : 2300 : case SPDK_NVMF_SUBSYSTEM_INACTIVE:
695 : 2300 : nvmf_poll_group_remove_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
696 : 2300 : break;
697 : 21697 : case SPDK_NVMF_SUBSYSTEM_ACTIVE:
698 [ + + ]: 21697 : if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_ACTIVATING) {
699 : 2300 : nvmf_poll_group_add_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
700 [ + - ]: 19397 : } else if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_RESUMING) {
701 : 19397 : nvmf_poll_group_resume_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i);
702 : : }
703 : 21697 : break;
704 : 19397 : case SPDK_NVMF_SUBSYSTEM_PAUSED:
705 : 19397 : nvmf_poll_group_pause_subsystem(group, ctx->subsystem, ctx->nsid, subsystem_state_change_continue,
706 : : i);
707 : 19397 : break;
708 : 0 : default:
709 : 0 : assert(false);
710 : : break;
711 : : }
712 : 43394 : }
713 : :
714 : : static void
715 : 17636 : nvmf_subsystem_do_state_change(struct nvmf_subsystem_state_change_ctx *ctx)
716 : : {
717 : 17636 : struct spdk_nvmf_subsystem *subsystem = ctx->subsystem;
718 : : enum spdk_nvmf_subsystem_state intermediate_state;
719 : : int rc;
720 : :
721 : 6532 : SPDK_DTRACE_PROBE3(nvmf_subsystem_change_state, subsystem->subnqn,
722 : : ctx->requested_state, subsystem->state);
723 : :
724 : : /* If we are already in the requested state, just call the callback immediately. */
725 [ + + ]: 17636 : if (subsystem->state == ctx->requested_state) {
726 : 3 : nvmf_subsystem_state_change_complete(ctx, 0);
727 : 3 : return;
728 : : }
729 : :
730 : 17633 : intermediate_state = nvmf_subsystem_get_intermediate_state(subsystem->state,
731 : : ctx->requested_state);
732 [ - + ]: 17633 : assert(intermediate_state != SPDK_NVMF_SUBSYSTEM_NUM_STATES);
733 : :
734 : 17633 : ctx->original_state = subsystem->state;
735 : 17633 : rc = nvmf_subsystem_set_state(subsystem, intermediate_state);
736 [ - + ]: 17633 : if (rc) {
737 : 0 : nvmf_subsystem_state_change_complete(ctx, -1);
738 : 0 : return;
739 : : }
740 : :
741 : 17633 : spdk_for_each_channel(subsystem->tgt,
742 : : subsystem_state_change_on_pg,
743 : : ctx,
744 : : subsystem_state_change_done);
745 : : }
746 : :
747 : :
748 : : static int
749 : 17636 : nvmf_subsystem_state_change(struct spdk_nvmf_subsystem *subsystem,
750 : : uint32_t nsid,
751 : : enum spdk_nvmf_subsystem_state requested_state,
752 : : spdk_nvmf_subsystem_state_change_done cb_fn,
753 : : void *cb_arg)
754 : : {
755 : : struct nvmf_subsystem_state_change_ctx *ctx;
756 : : struct spdk_thread *thread;
757 : :
758 : 17636 : thread = spdk_get_thread();
759 [ - + ]: 17636 : if (thread == NULL) {
760 : 0 : return -EINVAL;
761 : : }
762 : :
763 : 17636 : ctx = calloc(1, sizeof(*ctx));
764 [ - + ]: 17636 : if (!ctx) {
765 : 0 : return -ENOMEM;
766 : : }
767 : :
768 : 17636 : ctx->subsystem = subsystem;
769 : 17636 : ctx->nsid = nsid;
770 : 17636 : ctx->requested_state = requested_state;
771 : 17636 : ctx->cb_fn = cb_fn;
772 : 17636 : ctx->cb_arg = cb_arg;
773 : 17636 : ctx->thread = thread;
774 : :
775 [ - + ]: 17636 : pthread_mutex_lock(&subsystem->mutex);
776 : 17636 : TAILQ_INSERT_TAIL(&subsystem->state_changes, ctx, link);
777 [ + + ]: 17636 : if (ctx != TAILQ_FIRST(&subsystem->state_changes)) {
778 [ - + ]: 3 : pthread_mutex_unlock(&subsystem->mutex);
779 : 3 : return 0;
780 : : }
781 [ - + ]: 17633 : pthread_mutex_unlock(&subsystem->mutex);
782 : :
783 : 17633 : nvmf_subsystem_do_state_change(ctx);
784 : :
785 : 17633 : return 0;
786 : : }
787 : :
788 : : int
789 : 1148 : spdk_nvmf_subsystem_start(struct spdk_nvmf_subsystem *subsystem,
790 : : spdk_nvmf_subsystem_state_change_done cb_fn,
791 : : void *cb_arg)
792 : : {
793 : 1148 : return nvmf_subsystem_state_change(subsystem, 0, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg);
794 : : }
795 : :
796 : : int
797 : 1151 : spdk_nvmf_subsystem_stop(struct spdk_nvmf_subsystem *subsystem,
798 : : spdk_nvmf_subsystem_state_change_done cb_fn,
799 : : void *cb_arg)
800 : : {
801 : 1151 : return nvmf_subsystem_state_change(subsystem, 0, SPDK_NVMF_SUBSYSTEM_INACTIVE, cb_fn, cb_arg);
802 : : }
803 : :
804 : : int
805 : 7670 : spdk_nvmf_subsystem_pause(struct spdk_nvmf_subsystem *subsystem,
806 : : uint32_t nsid,
807 : : spdk_nvmf_subsystem_state_change_done cb_fn,
808 : : void *cb_arg)
809 : : {
810 : 7670 : return nvmf_subsystem_state_change(subsystem, nsid, SPDK_NVMF_SUBSYSTEM_PAUSED, cb_fn, cb_arg);
811 : : }
812 : :
813 : : int
814 : 7667 : spdk_nvmf_subsystem_resume(struct spdk_nvmf_subsystem *subsystem,
815 : : spdk_nvmf_subsystem_state_change_done cb_fn,
816 : : void *cb_arg)
817 : : {
818 : 7667 : return nvmf_subsystem_state_change(subsystem, 0, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg);
819 : : }
820 : :
821 : : struct spdk_nvmf_subsystem *
822 : 5964 : spdk_nvmf_subsystem_get_first(struct spdk_nvmf_tgt *tgt)
823 : : {
824 : 5964 : return RB_MIN(subsystem_tree, &tgt->subsystems);
825 : : }
826 : :
827 : : struct spdk_nvmf_subsystem *
828 : 7328 : spdk_nvmf_subsystem_get_next(struct spdk_nvmf_subsystem *subsystem)
829 : : {
830 [ + + ]: 7328 : if (!subsystem) {
831 : 775 : return NULL;
832 : : }
833 : :
834 : 6553 : return RB_NEXT(subsystem_tree, &tgt->subsystems, subsystem);
835 : : }
836 : :
837 : : static int
838 : 18 : nvmf_ns_add_host(struct spdk_nvmf_ns *ns, const char *hostnqn)
839 : : {
840 : : struct spdk_nvmf_host *host;
841 : :
842 : 18 : host = calloc(1, sizeof(*host));
843 [ - + ]: 18 : if (!host) {
844 : 0 : return -ENOMEM;
845 : : }
846 [ - + ]: 18 : snprintf(host->nqn, sizeof(host->nqn), "%s", hostnqn);
847 [ - + ]: 18 : TAILQ_INSERT_HEAD(&ns->hosts, host, link);
848 : 18 : return 0;
849 : : }
850 : :
851 : : static void
852 : 18 : nvmf_ns_remove_host(struct spdk_nvmf_ns *ns, struct spdk_nvmf_host *host)
853 : : {
854 [ - + ]: 18 : TAILQ_REMOVE(&ns->hosts, host, link);
855 : 18 : free(host);
856 : 18 : }
857 : :
858 : : static void
859 : 18 : _async_event_ns_notice(void *_ctrlr)
860 : : {
861 : 18 : struct spdk_nvmf_ctrlr *ctrlr = _ctrlr;
862 : :
863 : 18 : nvmf_ctrlr_async_event_ns_notice(ctrlr);
864 : 18 : }
865 : :
866 : : static void
867 : 18 : send_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr)
868 : : {
869 : 18 : spdk_thread_send_msg(ctrlr->thread, _async_event_ns_notice, ctrlr);
870 : 18 : }
871 : :
872 : : static int
873 : 63 : nvmf_ns_visible(struct spdk_nvmf_subsystem *subsystem,
874 : : uint32_t nsid,
875 : : const char *hostnqn,
876 : : bool visible)
877 : : {
878 : : struct spdk_nvmf_ns *ns;
879 : : struct spdk_nvmf_ctrlr *ctrlr;
880 : : struct spdk_nvmf_host *host;
881 : : int rc;
882 : :
883 [ + + ]: 63 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
884 [ - + ]: 21 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
885 : 0 : assert(false);
886 : : return -1;
887 : : }
888 : :
889 [ + + + + ]: 63 : if (hostnqn == NULL || !nvmf_nqn_is_valid(hostnqn)) {
890 : 9 : return -EINVAL;
891 : : }
892 : :
893 [ + + - + ]: 54 : if (nsid == 0 || nsid > subsystem->max_nsid) {
894 : 6 : return -EINVAL;
895 : : }
896 : :
897 : 48 : ns = subsystem->ns[nsid - 1];
898 [ + + ]: 48 : if (!ns) {
899 : 6 : return -ENOENT;
900 : : }
901 : :
902 [ + + + + ]: 42 : if (ns->always_visible) {
903 : : /* No individual host control */
904 : 9 : return -EPERM;
905 : : }
906 : :
907 : : /* Save host info to use for any future controllers. */
908 : 33 : host = nvmf_ns_find_host(ns, hostnqn);
909 [ + + + + ]: 33 : if (visible && host == NULL) {
910 : 18 : rc = nvmf_ns_add_host(ns, hostnqn);
911 [ - + ]: 18 : if (rc) {
912 : 0 : return rc;
913 : : }
914 [ + + + + ]: 15 : } else if (!visible && host != NULL) {
915 : 9 : nvmf_ns_remove_host(ns, host);
916 : : }
917 : :
918 : : /* Also apply to existing controllers. */
919 [ + + ]: 72 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
920 [ + + + + : 63 : if (strcmp(hostnqn, ctrlr->hostnqn) ||
+ + + + ]
921 : 24 : spdk_bit_array_get(ctrlr->visible_ns, nsid - 1) == visible) {
922 : 21 : continue;
923 : : }
924 [ + + ]: 18 : if (visible) {
925 : 9 : spdk_bit_array_set(ctrlr->visible_ns, nsid - 1);
926 : : } else {
927 : 9 : spdk_bit_array_clear(ctrlr->visible_ns, nsid - 1);
928 : : }
929 : 18 : send_async_event_ns_notice(ctrlr);
930 : 18 : nvmf_ctrlr_ns_changed(ctrlr, nsid);
931 : : }
932 : :
933 : 33 : return 0;
934 : : }
935 : :
936 : : int
937 : 36 : spdk_nvmf_ns_add_host(struct spdk_nvmf_subsystem *subsystem,
938 : : uint32_t nsid,
939 : : const char *hostnqn,
940 : : uint32_t flags)
941 : : {
942 : 4 : SPDK_DTRACE_PROBE4(spdk_nvmf_ns_add_host,
943 : : subsystem->subnqn,
944 : : nsid,
945 : : hostnqn,
946 : : flags);
947 : 36 : return nvmf_ns_visible(subsystem, nsid, hostnqn, true);
948 : : }
949 : :
950 : : int
951 : 27 : spdk_nvmf_ns_remove_host(struct spdk_nvmf_subsystem *subsystem,
952 : : uint32_t nsid,
953 : : const char *hostnqn,
954 : : uint32_t flags)
955 : : {
956 : 3 : SPDK_DTRACE_PROBE4(spdk_nvmf_ns_remove_host,
957 : : subsystem->subnqn,
958 : : nsid,
959 : : hostnqn,
960 : : flags);
961 : 27 : return nvmf_ns_visible(subsystem, nsid, hostnqn, false);
962 : : }
963 : :
964 : : /* Must hold subsystem->mutex while calling this function */
965 : : static struct spdk_nvmf_host *
966 : 9949 : nvmf_subsystem_find_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
967 : : {
968 : 9949 : struct spdk_nvmf_host *host = NULL;
969 : :
970 [ + + ]: 9955 : TAILQ_FOREACH(host, &subsystem->hosts, link) {
971 [ + + - + : 4522 : if (strcmp(hostnqn, host->nqn) == 0) {
+ + ]
972 : 4516 : return host;
973 : : }
974 : : }
975 : :
976 : 5433 : return NULL;
977 : : }
978 : :
979 : : int
980 : 394 : spdk_nvmf_subsystem_add_host_ext(struct spdk_nvmf_subsystem *subsystem,
981 : : const char *hostnqn, struct spdk_nvmf_host_opts *opts)
982 : : {
983 : : struct spdk_nvmf_host *host;
984 : : struct spdk_nvmf_transport *transport;
985 : : struct spdk_key *key;
986 : : int rc;
987 : :
988 [ - + ]: 394 : if (!nvmf_nqn_is_valid(hostnqn)) {
989 : 0 : return -EINVAL;
990 : : }
991 : :
992 [ - + ]: 394 : pthread_mutex_lock(&subsystem->mutex);
993 : :
994 [ + + ]: 394 : if (nvmf_subsystem_find_host(subsystem, hostnqn)) {
995 : : /* This subsystem already allows the specified host. */
996 [ - + ]: 3 : pthread_mutex_unlock(&subsystem->mutex);
997 : 3 : return -EINVAL;
998 : : }
999 : :
1000 : 391 : host = calloc(1, sizeof(*host));
1001 [ - + ]: 391 : if (!host) {
1002 [ # # ]: 0 : pthread_mutex_unlock(&subsystem->mutex);
1003 : 0 : return -ENOMEM;
1004 : : }
1005 : :
1006 [ + + ]: 391 : key = SPDK_GET_FIELD(opts, dhchap_key, NULL);
1007 [ + + ]: 391 : if (key != NULL) {
1008 [ - + ]: 312 : if (!nvmf_auth_is_supported()) {
1009 : 0 : SPDK_ERRLOG("NVMe in-band authentication is unsupported\n");
1010 [ # # ]: 0 : pthread_mutex_unlock(&subsystem->mutex);
1011 : 0 : nvmf_host_free(host);
1012 : 0 : return -EINVAL;
1013 : : }
1014 : 312 : host->dhchap_key = spdk_key_dup(key);
1015 [ - + ]: 312 : if (host->dhchap_key == NULL) {
1016 [ # # ]: 0 : pthread_mutex_unlock(&subsystem->mutex);
1017 : 0 : nvmf_host_free(host);
1018 : 0 : return -EINVAL;
1019 : : }
1020 [ + - ]: 312 : key = SPDK_GET_FIELD(opts, dhchap_ctrlr_key, NULL);
1021 [ + + ]: 312 : if (key != NULL) {
1022 : 224 : host->dhchap_ctrlr_key = spdk_key_dup(key);
1023 [ - + ]: 224 : if (host->dhchap_ctrlr_key == NULL) {
1024 [ # # ]: 0 : pthread_mutex_unlock(&subsystem->mutex);
1025 : 0 : nvmf_host_free(host);
1026 : 0 : return -EINVAL;
1027 : : }
1028 : : }
1029 [ + + - + ]: 79 : } else if (SPDK_GET_FIELD(opts, dhchap_ctrlr_key, NULL) != NULL) {
1030 : 0 : SPDK_ERRLOG("DH-HMAC-CHAP controller key requires host key to be set\n");
1031 [ # # ]: 0 : pthread_mutex_unlock(&subsystem->mutex);
1032 : 0 : nvmf_host_free(host);
1033 : 0 : return -EINVAL;
1034 : : }
1035 : :
1036 [ - + ]: 391 : snprintf(host->nqn, sizeof(host->nqn), "%s", hostnqn);
1037 : :
1038 : 195 : SPDK_DTRACE_PROBE2(nvmf_subsystem_add_host, subsystem->subnqn, host->nqn);
1039 : :
1040 [ + + ]: 391 : TAILQ_INSERT_HEAD(&subsystem->hosts, host, link);
1041 : :
1042 [ + + ]: 391 : if (!TAILQ_EMPTY(&subsystem->listeners)) {
1043 : 373 : nvmf_update_discovery_log(subsystem->tgt, hostnqn);
1044 : : }
1045 : :
1046 [ + + ]: 770 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1047 : 379 : transport = spdk_nvmf_transport_get_next(transport)) {
1048 [ + + ]: 385 : if (transport->ops->subsystem_add_host) {
1049 : 597 : rc = transport->ops->subsystem_add_host(transport, subsystem, hostnqn,
1050 [ + - ]: 299 : SPDK_GET_FIELD(opts, params, NULL));
1051 [ + + ]: 299 : if (rc) {
1052 : 6 : SPDK_ERRLOG("Unable to add host to %s transport\n", transport->ops->name);
1053 : : /* Remove this host from all transports we've managed to add it to. */
1054 [ - + ]: 6 : pthread_mutex_unlock(&subsystem->mutex);
1055 : 6 : spdk_nvmf_subsystem_remove_host(subsystem, hostnqn);
1056 : 6 : return rc;
1057 : : }
1058 : : }
1059 : : }
1060 : :
1061 [ - + ]: 385 : pthread_mutex_unlock(&subsystem->mutex);
1062 : :
1063 : 385 : return 0;
1064 : : }
1065 : :
1066 : : int
1067 : 12 : spdk_nvmf_subsystem_add_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn,
1068 : : const struct spdk_json_val *params)
1069 : : {
1070 : 12 : struct spdk_nvmf_host_opts opts = {};
1071 : :
1072 : 12 : opts.size = SPDK_SIZEOF(&opts, params);
1073 : 12 : opts.params = params;
1074 : :
1075 : 12 : return spdk_nvmf_subsystem_add_host_ext(subsystem, hostnqn, &opts);
1076 : : }
1077 : :
1078 : : int
1079 : 343 : spdk_nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
1080 : : {
1081 : : struct spdk_nvmf_host *host;
1082 : : struct spdk_nvmf_transport *transport;
1083 : :
1084 [ - + ]: 343 : pthread_mutex_lock(&subsystem->mutex);
1085 : :
1086 : 343 : host = nvmf_subsystem_find_host(subsystem, hostnqn);
1087 [ + + ]: 343 : if (host == NULL) {
1088 [ - + ]: 3 : pthread_mutex_unlock(&subsystem->mutex);
1089 : 3 : return -ENOENT;
1090 : : }
1091 : :
1092 : 164 : SPDK_DTRACE_PROBE2(nvmf_subsystem_remove_host, subsystem->subnqn, host->nqn);
1093 : :
1094 : 340 : nvmf_subsystem_remove_host(subsystem, host);
1095 : :
1096 [ + + ]: 340 : if (!TAILQ_EMPTY(&subsystem->listeners)) {
1097 : 334 : nvmf_update_discovery_log(subsystem->tgt, hostnqn);
1098 : : }
1099 : :
1100 [ + + ]: 674 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1101 : 334 : transport = spdk_nvmf_transport_get_next(transport)) {
1102 [ + + ]: 334 : if (transport->ops->subsystem_remove_host) {
1103 : 248 : transport->ops->subsystem_remove_host(transport, subsystem, hostnqn);
1104 : : }
1105 : : }
1106 : :
1107 [ - + ]: 340 : pthread_mutex_unlock(&subsystem->mutex);
1108 : :
1109 : 340 : return 0;
1110 : : }
1111 : :
1112 : : struct nvmf_subsystem_disconnect_host_ctx {
1113 : : struct spdk_nvmf_subsystem *subsystem;
1114 : : char *hostnqn;
1115 : : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn;
1116 : : void *cb_arg;
1117 : : };
1118 : :
1119 : : static void
1120 : 328 : nvmf_subsystem_disconnect_host_fini(struct spdk_io_channel_iter *i, int status)
1121 : : {
1122 : : struct nvmf_subsystem_disconnect_host_ctx *ctx;
1123 : :
1124 : 328 : ctx = spdk_io_channel_iter_get_ctx(i);
1125 : :
1126 [ + - ]: 328 : if (ctx->cb_fn) {
1127 : 328 : ctx->cb_fn(ctx->cb_arg, status);
1128 : : }
1129 : 328 : free(ctx->hostnqn);
1130 : 328 : free(ctx);
1131 : 328 : }
1132 : :
1133 : : static void
1134 : 358 : nvmf_subsystem_disconnect_qpairs_by_host(struct spdk_io_channel_iter *i)
1135 : : {
1136 : : struct nvmf_subsystem_disconnect_host_ctx *ctx;
1137 : : struct spdk_nvmf_poll_group *group;
1138 : : struct spdk_io_channel *ch;
1139 : : struct spdk_nvmf_qpair *qpair, *tmp_qpair;
1140 : : struct spdk_nvmf_ctrlr *ctrlr;
1141 : :
1142 : 358 : ctx = spdk_io_channel_iter_get_ctx(i);
1143 : 358 : ch = spdk_io_channel_iter_get_channel(i);
1144 : 358 : group = spdk_io_channel_get_ctx(ch);
1145 : :
1146 [ + + ]: 366 : TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, tmp_qpair) {
1147 : 8 : ctrlr = qpair->ctrlr;
1148 : :
1149 [ + - - + ]: 8 : if (ctrlr == NULL || ctrlr->subsys != ctx->subsystem) {
1150 : 0 : continue;
1151 : : }
1152 : :
1153 [ - + - + : 8 : if (strncmp(ctrlr->hostnqn, ctx->hostnqn, sizeof(ctrlr->hostnqn)) == 0) {
+ - ]
1154 : : /* Right now this does not wait for the queue pairs to actually disconnect. */
1155 : 8 : spdk_nvmf_qpair_disconnect(qpair);
1156 : : }
1157 : : }
1158 : 358 : spdk_for_each_channel_continue(i, 0);
1159 : 358 : }
1160 : :
1161 : : int
1162 : 328 : spdk_nvmf_subsystem_disconnect_host(struct spdk_nvmf_subsystem *subsystem,
1163 : : const char *hostnqn,
1164 : : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
1165 : : void *cb_arg)
1166 : : {
1167 : : struct nvmf_subsystem_disconnect_host_ctx *ctx;
1168 : :
1169 : 328 : ctx = calloc(1, sizeof(struct nvmf_subsystem_disconnect_host_ctx));
1170 [ - + ]: 328 : if (ctx == NULL) {
1171 : 0 : return -ENOMEM;
1172 : : }
1173 : :
1174 [ - + ]: 328 : ctx->hostnqn = strdup(hostnqn);
1175 [ - + ]: 328 : if (ctx->hostnqn == NULL) {
1176 : 0 : free(ctx);
1177 : 0 : return -ENOMEM;
1178 : : }
1179 : :
1180 : 328 : ctx->subsystem = subsystem;
1181 : 328 : ctx->cb_fn = cb_fn;
1182 : 328 : ctx->cb_arg = cb_arg;
1183 : :
1184 : 328 : spdk_for_each_channel(subsystem->tgt, nvmf_subsystem_disconnect_qpairs_by_host, ctx,
1185 : : nvmf_subsystem_disconnect_host_fini);
1186 : :
1187 : 328 : return 0;
1188 : : }
1189 : :
1190 : : int
1191 : 1202 : spdk_nvmf_subsystem_set_allow_any_host(struct spdk_nvmf_subsystem *subsystem, bool allow_any_host)
1192 : : {
1193 [ - + ]: 1202 : pthread_mutex_lock(&subsystem->mutex);
1194 : 1202 : subsystem->allow_any_host = allow_any_host;
1195 [ + + ]: 1202 : if (!TAILQ_EMPTY(&subsystem->listeners)) {
1196 : 42 : nvmf_update_discovery_log(subsystem->tgt, NULL);
1197 : : }
1198 [ - + ]: 1202 : pthread_mutex_unlock(&subsystem->mutex);
1199 : :
1200 : 1202 : return 0;
1201 : : }
1202 : :
1203 : : bool
1204 : 1454 : spdk_nvmf_subsystem_get_allow_any_host(const struct spdk_nvmf_subsystem *subsystem)
1205 : : {
1206 : : bool allow_any_host;
1207 : : struct spdk_nvmf_subsystem *sub;
1208 : :
1209 : : /* Technically, taking the mutex modifies data in the subsystem. But the const
1210 : : * is still important to convey that this doesn't mutate any other data. Cast
1211 : : * it away to work around this. */
1212 : 1454 : sub = (struct spdk_nvmf_subsystem *)subsystem;
1213 : :
1214 [ - + ]: 1454 : pthread_mutex_lock(&sub->mutex);
1215 [ - + ]: 1454 : allow_any_host = sub->allow_any_host;
1216 [ - + ]: 1454 : pthread_mutex_unlock(&sub->mutex);
1217 : :
1218 : 1454 : return allow_any_host;
1219 : : }
1220 : :
1221 : : bool
1222 : 6915 : spdk_nvmf_subsystem_host_allowed(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
1223 : : {
1224 : : bool allowed;
1225 : :
1226 [ - + ]: 6915 : if (!hostnqn) {
1227 : 0 : return false;
1228 : : }
1229 : :
1230 [ - + ]: 6915 : pthread_mutex_lock(&subsystem->mutex);
1231 : :
1232 [ + + + + ]: 6915 : if (subsystem->allow_any_host) {
1233 [ - + ]: 5502 : pthread_mutex_unlock(&subsystem->mutex);
1234 : 5502 : return true;
1235 : : }
1236 : :
1237 : 1413 : allowed = nvmf_subsystem_find_host(subsystem, hostnqn) != NULL;
1238 [ - + ]: 1413 : pthread_mutex_unlock(&subsystem->mutex);
1239 : :
1240 : 1413 : return allowed;
1241 : : }
1242 : :
1243 : : bool
1244 : 6231 : nvmf_subsystem_host_auth_required(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn)
1245 : : {
1246 : : struct spdk_nvmf_host *host;
1247 : : bool status;
1248 : :
1249 [ - + ]: 6231 : pthread_mutex_lock(&subsystem->mutex);
1250 : 6231 : host = nvmf_subsystem_find_host(subsystem, hostnqn);
1251 [ + + + + ]: 6231 : status = host != NULL && host->dhchap_key != NULL;
1252 [ - + ]: 6231 : pthread_mutex_unlock(&subsystem->mutex);
1253 : :
1254 : 6231 : return status;
1255 : : }
1256 : :
1257 : : struct spdk_key *
1258 : 1568 : nvmf_subsystem_get_dhchap_key(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn,
1259 : : enum nvmf_auth_key_type type)
1260 : : {
1261 : : struct spdk_nvmf_host *host;
1262 : 1568 : struct spdk_key *key = NULL;
1263 : :
1264 [ - + ]: 1568 : pthread_mutex_lock(&subsystem->mutex);
1265 : 1568 : host = nvmf_subsystem_find_host(subsystem, hostnqn);
1266 [ + - ]: 1568 : if (host != NULL) {
1267 [ + + - ]: 1568 : switch (type) {
1268 : 900 : case NVMF_AUTH_KEY_HOST:
1269 : 900 : key = host->dhchap_key;
1270 : 900 : break;
1271 : 668 : case NVMF_AUTH_KEY_CTRLR:
1272 : 668 : key = host->dhchap_ctrlr_key;
1273 : 668 : break;
1274 : : }
1275 [ + + ]: 1568 : if (key != NULL) {
1276 : 1564 : key = spdk_key_dup(key);
1277 : : }
1278 : : }
1279 [ - + ]: 1568 : pthread_mutex_unlock(&subsystem->mutex);
1280 : :
1281 : 1568 : return key;
1282 : : }
1283 : :
1284 : : struct spdk_nvmf_host *
1285 : 970 : spdk_nvmf_subsystem_get_first_host(struct spdk_nvmf_subsystem *subsystem)
1286 : : {
1287 : 970 : return TAILQ_FIRST(&subsystem->hosts);
1288 : : }
1289 : :
1290 : :
1291 : : struct spdk_nvmf_host *
1292 : 201 : spdk_nvmf_subsystem_get_next_host(struct spdk_nvmf_subsystem *subsystem,
1293 : : struct spdk_nvmf_host *prev_host)
1294 : : {
1295 : 201 : return TAILQ_NEXT(prev_host, link);
1296 : : }
1297 : :
1298 : : const char *
1299 : 201 : spdk_nvmf_host_get_nqn(const struct spdk_nvmf_host *host)
1300 : : {
1301 : 201 : return host->nqn;
1302 : : }
1303 : :
1304 : : struct spdk_nvmf_subsystem_listener *
1305 : 2753 : nvmf_subsystem_find_listener(struct spdk_nvmf_subsystem *subsystem,
1306 : : const struct spdk_nvme_transport_id *trid)
1307 : : {
1308 : : struct spdk_nvmf_subsystem_listener *listener;
1309 : :
1310 [ + + ]: 3099 : TAILQ_FOREACH(listener, &subsystem->listeners, link) {
1311 [ + + ]: 2113 : if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) {
1312 : 1767 : return listener;
1313 : : }
1314 : : }
1315 : :
1316 : 986 : return NULL;
1317 : : }
1318 : :
1319 : : /**
1320 : : * Function to be called once the target is listening.
1321 : : *
1322 : : * \param ctx Context argument passed to this function.
1323 : : * \param status 0 if it completed successfully, or negative errno if it failed.
1324 : : */
1325 : : static void
1326 : 502 : _nvmf_subsystem_add_listener_done(void *ctx, int status)
1327 : : {
1328 : 502 : struct spdk_nvmf_subsystem_listener *listener = ctx;
1329 : :
1330 [ - + ]: 502 : if (status) {
1331 : 0 : listener->cb_fn(listener->cb_arg, status);
1332 : 0 : free(listener);
1333 : 0 : return;
1334 : : }
1335 : :
1336 [ + + ]: 502 : TAILQ_INSERT_HEAD(&listener->subsystem->listeners, listener, link);
1337 : 502 : nvmf_update_discovery_log(listener->subsystem->tgt, NULL);
1338 : 502 : listener->cb_fn(listener->cb_arg, status);
1339 : : }
1340 : :
1341 : : void
1342 : 986 : spdk_nvmf_subsystem_listener_opts_init(struct spdk_nvmf_listener_opts *opts, size_t size)
1343 : : {
1344 [ - + ]: 986 : if (opts == NULL) {
1345 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
1346 : 0 : assert(false);
1347 : : return;
1348 : : }
1349 [ - + ]: 986 : if (size == 0) {
1350 : 0 : SPDK_ERRLOG("size should not be zero\n");
1351 : 0 : assert(false);
1352 : : return;
1353 : : }
1354 : :
1355 [ - + ]: 986 : memset(opts, 0, size);
1356 : 986 : opts->opts_size = size;
1357 : :
1358 : : #define FIELD_OK(field) \
1359 : : offsetof(struct spdk_nvmf_listener_opts, field) + sizeof(opts->field) <= size
1360 : :
1361 : : #define SET_FIELD(field, value) \
1362 : : if (FIELD_OK(field)) { \
1363 : : opts->field = value; \
1364 : : } \
1365 : :
1366 [ + - ]: 986 : SET_FIELD(secure_channel, false);
1367 [ + - ]: 986 : SET_FIELD(ana_state, SPDK_NVME_ANA_OPTIMIZED_STATE);
1368 [ + - ]: 986 : SET_FIELD(sock_impl, NULL);
1369 : :
1370 : : #undef FIELD_OK
1371 : : #undef SET_FIELD
1372 : : }
1373 : :
1374 : : static int
1375 : 481 : listener_opts_copy(struct spdk_nvmf_listener_opts *src, struct spdk_nvmf_listener_opts *dst)
1376 : : {
1377 [ - + ]: 481 : if (src->opts_size == 0) {
1378 : 0 : SPDK_ERRLOG("source structure size should not be zero\n");
1379 : 0 : assert(false);
1380 : : return -EINVAL;
1381 : : }
1382 : :
1383 [ - + ]: 481 : memset(dst, 0, sizeof(*dst));
1384 : 481 : dst->opts_size = src->opts_size;
1385 : :
1386 : : #define FIELD_OK(field) \
1387 : : offsetof(struct spdk_nvmf_listener_opts, field) + sizeof(src->field) <= src->opts_size
1388 : :
1389 : : #define SET_FIELD(field) \
1390 : : if (FIELD_OK(field)) { \
1391 : : dst->field = src->field; \
1392 : : } \
1393 : :
1394 [ + - - + ]: 481 : SET_FIELD(secure_channel);
1395 [ + - ]: 481 : SET_FIELD(ana_state);
1396 [ + - ]: 481 : SET_FIELD(sock_impl);
1397 : : /* We should not remove this statement, but need to update the assert statement
1398 : : * if we add a new field, and also add a corresponding SET_FIELD statement. */
1399 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_listener_opts) == 24, "Incorrect size");
1400 : :
1401 : : #undef SET_FIELD
1402 : : #undef FIELD_OK
1403 : :
1404 : 481 : return 0;
1405 : : }
1406 : :
1407 : : static void
1408 : 502 : _nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem,
1409 : : struct spdk_nvme_transport_id *trid,
1410 : : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
1411 : : void *cb_arg, struct spdk_nvmf_listener_opts *opts)
1412 : : {
1413 : : struct spdk_nvmf_transport *transport;
1414 : : struct spdk_nvmf_subsystem_listener *listener;
1415 : : struct spdk_nvmf_listener *tr_listener;
1416 : : uint32_t i;
1417 : : uint32_t id;
1418 : 502 : int rc = 0;
1419 : :
1420 [ - + ]: 502 : assert(cb_fn != NULL);
1421 : :
1422 [ + + ]: 502 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1423 [ - + ]: 481 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1424 : 0 : cb_fn(cb_arg, -EAGAIN);
1425 : 0 : return;
1426 : : }
1427 : :
1428 [ - + ]: 502 : if (nvmf_subsystem_find_listener(subsystem, trid)) {
1429 : : /* Listener already exists in this subsystem */
1430 : 0 : cb_fn(cb_arg, 0);
1431 : 0 : return;
1432 : : }
1433 : :
1434 : 502 : transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, trid->trstring);
1435 [ - + ]: 502 : if (!transport) {
1436 : 0 : SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
1437 : : trid->trstring);
1438 : 0 : cb_fn(cb_arg, -EINVAL);
1439 : 0 : return;
1440 : : }
1441 : :
1442 : 502 : tr_listener = nvmf_transport_find_listener(transport, trid);
1443 [ - + ]: 502 : if (!tr_listener) {
1444 : 0 : SPDK_ERRLOG("Cannot find transport listener for %s\n", trid->traddr);
1445 : 0 : cb_fn(cb_arg, -EINVAL);
1446 : 0 : return;
1447 : : }
1448 : :
1449 : 502 : listener = calloc(1, sizeof(*listener));
1450 [ - + ]: 502 : if (!listener) {
1451 : 0 : cb_fn(cb_arg, -ENOMEM);
1452 : 0 : return;
1453 : : }
1454 : :
1455 : 502 : listener->trid = &tr_listener->trid;
1456 : 502 : listener->transport = transport;
1457 : 502 : listener->cb_fn = cb_fn;
1458 : 502 : listener->cb_arg = cb_arg;
1459 : 502 : listener->subsystem = subsystem;
1460 : 502 : listener->ana_state = calloc(subsystem->max_nsid, sizeof(enum spdk_nvme_ana_state));
1461 [ - + ]: 502 : if (!listener->ana_state) {
1462 : 0 : free(listener);
1463 : 0 : cb_fn(cb_arg, -ENOMEM);
1464 : 0 : return;
1465 : : }
1466 : :
1467 : 502 : spdk_nvmf_subsystem_listener_opts_init(&listener->opts, sizeof(listener->opts));
1468 [ + + ]: 502 : if (opts != NULL) {
1469 : 481 : rc = listener_opts_copy(opts, &listener->opts);
1470 [ - + ]: 481 : if (rc) {
1471 : 0 : SPDK_ERRLOG("Unable to copy listener options\n");
1472 : 0 : free(listener->ana_state);
1473 : 0 : free(listener);
1474 : 0 : cb_fn(cb_arg, -EINVAL);
1475 : 0 : return;
1476 : : }
1477 : : }
1478 : :
1479 : 502 : id = spdk_bit_array_find_first_clear(subsystem->used_listener_ids, 0);
1480 [ - + ]: 502 : if (id == UINT32_MAX) {
1481 : 0 : SPDK_ERRLOG("Cannot add any more listeners\n");
1482 : 0 : free(listener->ana_state);
1483 : 0 : free(listener->opts.sock_impl);
1484 : 0 : free(listener);
1485 : 0 : cb_fn(cb_arg, -EINVAL);
1486 : 0 : return;
1487 : : }
1488 : :
1489 : 502 : spdk_bit_array_set(subsystem->used_listener_ids, id);
1490 : 502 : listener->id = id;
1491 : :
1492 [ + + ]: 12952 : for (i = 0; i < subsystem->max_nsid; i++) {
1493 : 12450 : listener->ana_state[i] = listener->opts.ana_state;
1494 : : }
1495 : :
1496 [ + + ]: 502 : if (transport->ops->listen_associate != NULL) {
1497 : 17 : rc = transport->ops->listen_associate(transport, subsystem, trid);
1498 : : }
1499 : :
1500 : 190 : SPDK_DTRACE_PROBE4(nvmf_subsystem_add_listener, subsystem->subnqn, listener->trid->trtype,
1501 : : listener->trid->traddr, listener->trid->trsvcid);
1502 : :
1503 : 502 : _nvmf_subsystem_add_listener_done(listener, rc);
1504 : : }
1505 : :
1506 : : void
1507 : 21 : spdk_nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem,
1508 : : struct spdk_nvme_transport_id *trid,
1509 : : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
1510 : : void *cb_arg)
1511 : : {
1512 : 21 : _nvmf_subsystem_add_listener(subsystem, trid, cb_fn, cb_arg, NULL);
1513 : 21 : }
1514 : :
1515 : : void
1516 : 481 : spdk_nvmf_subsystem_add_listener_ext(struct spdk_nvmf_subsystem *subsystem,
1517 : : struct spdk_nvme_transport_id *trid,
1518 : : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
1519 : : void *cb_arg, struct spdk_nvmf_listener_opts *opts)
1520 : : {
1521 : 481 : _nvmf_subsystem_add_listener(subsystem, trid, cb_fn, cb_arg, opts);
1522 : 481 : }
1523 : :
1524 : : int
1525 : 43 : spdk_nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem,
1526 : : const struct spdk_nvme_transport_id *trid)
1527 : : {
1528 : : struct spdk_nvmf_subsystem_listener *listener;
1529 : :
1530 [ + - ]: 43 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1531 [ - + ]: 43 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1532 : 0 : return -EAGAIN;
1533 : : }
1534 : :
1535 : 43 : listener = nvmf_subsystem_find_listener(subsystem, trid);
1536 [ + + ]: 43 : if (listener == NULL) {
1537 : 3 : return -ENOENT;
1538 : : }
1539 : :
1540 : 23 : SPDK_DTRACE_PROBE4(nvmf_subsystem_remove_listener, subsystem->subnqn, listener->trid->trtype,
1541 : : listener->trid->traddr, listener->trid->trsvcid);
1542 : :
1543 : 40 : _nvmf_subsystem_remove_listener(subsystem, listener, false);
1544 : :
1545 : 40 : return 0;
1546 : : }
1547 : :
1548 : : void
1549 : 1406 : nvmf_subsystem_remove_all_listeners(struct spdk_nvmf_subsystem *subsystem,
1550 : : bool stop)
1551 : : {
1552 : : struct spdk_nvmf_subsystem_listener *listener, *listener_tmp;
1553 : :
1554 [ + + ]: 1868 : TAILQ_FOREACH_SAFE(listener, &subsystem->listeners, link, listener_tmp) {
1555 : 462 : _nvmf_subsystem_remove_listener(subsystem, listener, stop);
1556 : : }
1557 : 1406 : }
1558 : :
1559 : : bool
1560 : 6231 : spdk_nvmf_subsystem_listener_allowed(struct spdk_nvmf_subsystem *subsystem,
1561 : : const struct spdk_nvme_transport_id *trid)
1562 : : {
1563 : : struct spdk_nvmf_subsystem_listener *listener;
1564 : :
1565 [ + + ]: 6439 : TAILQ_FOREACH(listener, &subsystem->listeners, link) {
1566 [ + + ]: 6432 : if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) {
1567 : 6224 : return true;
1568 : : }
1569 : : }
1570 : :
1571 [ - + + - ]: 7 : if (!strcmp(subsystem->subnqn, SPDK_NVMF_DISCOVERY_NQN)) {
1572 : 7 : SPDK_WARNLOG("Allowing connection to discovery subsystem on %s/%s/%s, "
1573 : : "even though this listener was not added to the discovery "
1574 : : "subsystem. This behavior is deprecated and will be removed "
1575 : : "in a future release.\n",
1576 : : spdk_nvme_transport_id_trtype_str(trid->trtype), trid->traddr, trid->trsvcid);
1577 : 7 : return true;
1578 : : }
1579 : :
1580 : 0 : return false;
1581 : : }
1582 : :
1583 : : struct spdk_nvmf_subsystem_listener *
1584 : 2713 : spdk_nvmf_subsystem_get_first_listener(struct spdk_nvmf_subsystem *subsystem)
1585 : : {
1586 : 2713 : return TAILQ_FIRST(&subsystem->listeners);
1587 : : }
1588 : :
1589 : : struct spdk_nvmf_subsystem_listener *
1590 : 2269 : spdk_nvmf_subsystem_get_next_listener(struct spdk_nvmf_subsystem *subsystem,
1591 : : struct spdk_nvmf_subsystem_listener *prev_listener)
1592 : : {
1593 : 2269 : return TAILQ_NEXT(prev_listener, link);
1594 : : }
1595 : :
1596 : : const struct spdk_nvme_transport_id *
1597 : 1073 : spdk_nvmf_subsystem_listener_get_trid(struct spdk_nvmf_subsystem_listener *listener)
1598 : : {
1599 : 1073 : return listener->trid;
1600 : : }
1601 : :
1602 : : void
1603 : 0 : spdk_nvmf_subsystem_allow_any_listener(struct spdk_nvmf_subsystem *subsystem,
1604 : : bool allow_any_listener)
1605 : : {
1606 : 0 : subsystem->flags.allow_any_listener = allow_any_listener;
1607 : 0 : }
1608 : :
1609 : : bool
1610 : 0 : spdk_nvmf_subsystem_any_listener_allowed(struct spdk_nvmf_subsystem *subsystem)
1611 : : {
1612 : 0 : return subsystem->flags.allow_any_listener;
1613 : : }
1614 : :
1615 : : struct subsystem_update_ns_ctx {
1616 : : struct spdk_nvmf_subsystem *subsystem;
1617 : :
1618 : : spdk_nvmf_subsystem_state_change_done cb_fn;
1619 : : void *cb_arg;
1620 : : };
1621 : :
1622 : : static void
1623 : 0 : subsystem_update_ns_done(struct spdk_io_channel_iter *i, int status)
1624 : : {
1625 : 0 : struct subsystem_update_ns_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1626 : :
1627 [ # # ]: 0 : if (ctx->cb_fn) {
1628 : 0 : ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status);
1629 : : }
1630 : 0 : free(ctx);
1631 : 0 : }
1632 : :
1633 : : static void
1634 : 0 : subsystem_update_ns_on_pg(struct spdk_io_channel_iter *i)
1635 : : {
1636 : : int rc;
1637 : : struct subsystem_update_ns_ctx *ctx;
1638 : : struct spdk_nvmf_poll_group *group;
1639 : : struct spdk_nvmf_subsystem *subsystem;
1640 : :
1641 : 0 : ctx = spdk_io_channel_iter_get_ctx(i);
1642 : 0 : group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
1643 : 0 : subsystem = ctx->subsystem;
1644 : :
1645 : 0 : rc = nvmf_poll_group_update_subsystem(group, subsystem);
1646 : 0 : spdk_for_each_channel_continue(i, rc);
1647 : 0 : }
1648 : :
1649 : : static int
1650 : 0 : nvmf_subsystem_update_ns(struct spdk_nvmf_subsystem *subsystem,
1651 : : spdk_nvmf_subsystem_state_change_done cb_fn, void *cb_arg)
1652 : : {
1653 : : struct subsystem_update_ns_ctx *ctx;
1654 : :
1655 : 0 : ctx = calloc(1, sizeof(*ctx));
1656 [ # # ]: 0 : if (ctx == NULL) {
1657 : 0 : SPDK_ERRLOG("Can't alloc subsystem poll group update context\n");
1658 : 0 : return -ENOMEM;
1659 : : }
1660 : 0 : ctx->subsystem = subsystem;
1661 : 0 : ctx->cb_fn = cb_fn;
1662 : 0 : ctx->cb_arg = cb_arg;
1663 : :
1664 : 0 : spdk_for_each_channel(subsystem->tgt,
1665 : : subsystem_update_ns_on_pg,
1666 : : ctx,
1667 : : subsystem_update_ns_done);
1668 : 0 : return 0;
1669 : : }
1670 : :
1671 : : static void
1672 : 5427 : nvmf_subsystem_ns_changed(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1673 : : {
1674 : : struct spdk_nvmf_ctrlr *ctrlr;
1675 : :
1676 [ + + ]: 9087 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1677 [ + + ]: 3660 : if (nvmf_ctrlr_ns_is_visible(ctrlr, nsid)) {
1678 : 3657 : nvmf_ctrlr_ns_changed(ctrlr, nsid);
1679 : : }
1680 : : }
1681 : 5427 : }
1682 : :
1683 : : static uint32_t nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns);
1684 : :
1685 : : int
1686 : 2667 : spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
1687 : : {
1688 : : struct spdk_nvmf_transport *transport;
1689 : : struct spdk_nvmf_ns *ns;
1690 : : struct spdk_nvmf_host *host, *tmp;
1691 : : struct spdk_nvmf_ctrlr *ctrlr;
1692 : :
1693 [ + + ]: 2667 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
1694 [ - + ]: 2336 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
1695 : 0 : assert(false);
1696 : : return -1;
1697 : : }
1698 : :
1699 [ + - - + ]: 2667 : if (nsid == 0 || nsid > subsystem->max_nsid) {
1700 : 0 : return -1;
1701 : : }
1702 : :
1703 : 2667 : ns = subsystem->ns[nsid - 1];
1704 [ - + ]: 2667 : if (!ns) {
1705 : 0 : return -1;
1706 : : }
1707 : :
1708 : 2667 : subsystem->ns[nsid - 1] = NULL;
1709 : :
1710 [ - + ]: 2667 : assert(ns->anagrpid - 1 < subsystem->max_nsid);
1711 [ - + ]: 2667 : assert(subsystem->ana_group[ns->anagrpid - 1] > 0);
1712 : :
1713 : 2667 : subsystem->ana_group[ns->anagrpid - 1]--;
1714 : :
1715 [ + + ]: 2676 : TAILQ_FOREACH_SAFE(host, &ns->hosts, link, tmp) {
1716 : 9 : nvmf_ns_remove_host(ns, host);
1717 : : }
1718 : :
1719 : 2667 : free(ns->ptpl_file);
1720 : 2667 : nvmf_ns_reservation_clear_all_registrants(ns);
1721 : 2667 : spdk_bdev_module_release_bdev(ns->bdev);
1722 : 2667 : spdk_bdev_close(ns->desc);
1723 : 2667 : free(ns);
1724 : :
1725 [ + + + + : 2667 : if (subsystem->fdp_supported && !spdk_nvmf_subsystem_get_first_ns(subsystem)) {
+ - ]
1726 : 3 : subsystem->fdp_supported = false;
1727 [ - + - + ]: 3 : SPDK_DEBUGLOG(nvmf, "Subsystem with id: %u doesn't have FDP capability.\n",
1728 : : subsystem->id);
1729 : : }
1730 : :
1731 [ + + ]: 5319 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
1732 : 2652 : transport = spdk_nvmf_transport_get_next(transport)) {
1733 [ - + ]: 2652 : if (transport->ops->subsystem_remove_ns) {
1734 : 0 : transport->ops->subsystem_remove_ns(transport, subsystem, nsid);
1735 : : }
1736 : : }
1737 : :
1738 : 2667 : nvmf_subsystem_ns_changed(subsystem, nsid);
1739 : :
1740 [ + + ]: 4463 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1741 : 1796 : spdk_bit_array_clear(ctrlr->visible_ns, nsid - 1);
1742 : : }
1743 : :
1744 : 2667 : return 0;
1745 : : }
1746 : :
1747 : : struct subsystem_ns_change_ctx {
1748 : : struct spdk_nvmf_subsystem *subsystem;
1749 : : spdk_nvmf_subsystem_state_change_done cb_fn;
1750 : : uint32_t nsid;
1751 : : };
1752 : :
1753 : : static void
1754 : 28 : _nvmf_ns_hot_remove(struct spdk_nvmf_subsystem *subsystem,
1755 : : void *cb_arg, int status)
1756 : : {
1757 : 28 : struct subsystem_ns_change_ctx *ctx = cb_arg;
1758 : : int rc;
1759 : :
1760 : 28 : rc = spdk_nvmf_subsystem_remove_ns(subsystem, ctx->nsid);
1761 [ - + ]: 28 : if (rc != 0) {
1762 : 0 : SPDK_ERRLOG("Failed to make changes to NVME-oF subsystem with id: %u\n", subsystem->id);
1763 : : }
1764 : :
1765 : 28 : rc = spdk_nvmf_subsystem_resume(subsystem, NULL, NULL);
1766 [ - + ]: 28 : if (rc != 0) {
1767 : 0 : SPDK_ERRLOG("Failed to resume NVME-oF subsystem with id: %u\n", subsystem->id);
1768 : : }
1769 : :
1770 : 28 : free(ctx);
1771 : 28 : }
1772 : :
1773 : : static void
1774 : 0 : nvmf_ns_change_msg(void *ns_ctx)
1775 : : {
1776 : 0 : struct subsystem_ns_change_ctx *ctx = ns_ctx;
1777 : : int rc;
1778 : :
1779 : 0 : SPDK_DTRACE_PROBE2(nvmf_ns_change, ctx->nsid, ctx->subsystem->subnqn);
1780 : :
1781 : 0 : rc = spdk_nvmf_subsystem_pause(ctx->subsystem, ctx->nsid, ctx->cb_fn, ctx);
1782 [ # # ]: 0 : if (rc) {
1783 [ # # ]: 0 : if (rc == -EBUSY) {
1784 : : /* Try again, this is not a permanent situation. */
1785 : 0 : spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ctx);
1786 : : } else {
1787 : 0 : free(ctx);
1788 : 0 : SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n");
1789 : : }
1790 : : }
1791 : 0 : }
1792 : :
1793 : : static void
1794 : 28 : nvmf_ns_hot_remove(void *remove_ctx)
1795 : : {
1796 : 28 : struct spdk_nvmf_ns *ns = remove_ctx;
1797 : : struct subsystem_ns_change_ctx *ns_ctx;
1798 : : int rc;
1799 : :
1800 : : /* We have to allocate a new context because this op
1801 : : * is asynchronous and we could lose the ns in the middle.
1802 : : */
1803 : 28 : ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx));
1804 [ - + ]: 28 : if (!ns_ctx) {
1805 : 0 : SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n");
1806 : 0 : return;
1807 : : }
1808 : :
1809 : 28 : ns_ctx->subsystem = ns->subsystem;
1810 : 28 : ns_ctx->nsid = ns->opts.nsid;
1811 : 28 : ns_ctx->cb_fn = _nvmf_ns_hot_remove;
1812 : :
1813 : 28 : rc = spdk_nvmf_subsystem_pause(ns->subsystem, ns_ctx->nsid, _nvmf_ns_hot_remove, ns_ctx);
1814 [ - + ]: 28 : if (rc) {
1815 [ # # ]: 0 : if (rc == -EBUSY) {
1816 : : /* Try again, this is not a permanent situation. */
1817 : 0 : spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx);
1818 : : } else {
1819 : 0 : SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n");
1820 : 0 : free(ns_ctx);
1821 : : }
1822 : : }
1823 : : }
1824 : :
1825 : : static void
1826 : 96 : _nvmf_ns_resize(struct spdk_nvmf_subsystem *subsystem, void *cb_arg, int status)
1827 : : {
1828 : 96 : struct subsystem_ns_change_ctx *ctx = cb_arg;
1829 : :
1830 : 96 : nvmf_subsystem_ns_changed(subsystem, ctx->nsid);
1831 [ - + ]: 96 : if (spdk_nvmf_subsystem_resume(subsystem, NULL, NULL) != 0) {
1832 : 0 : SPDK_ERRLOG("Failed to resume NVME-oF subsystem with id: %u\n", subsystem->id);
1833 : : }
1834 : :
1835 : 96 : free(ctx);
1836 : 96 : }
1837 : :
1838 : : static void
1839 : 96 : nvmf_ns_resize(void *event_ctx)
1840 : : {
1841 : 96 : struct spdk_nvmf_ns *ns = event_ctx;
1842 : : struct subsystem_ns_change_ctx *ns_ctx;
1843 : : int rc;
1844 : :
1845 : : /* We have to allocate a new context because this op
1846 : : * is asynchronous and we could lose the ns in the middle.
1847 : : */
1848 : 96 : ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx));
1849 [ - + ]: 96 : if (!ns_ctx) {
1850 : 0 : SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n");
1851 : 0 : return;
1852 : : }
1853 : :
1854 : 96 : ns_ctx->subsystem = ns->subsystem;
1855 : 96 : ns_ctx->nsid = ns->opts.nsid;
1856 : 96 : ns_ctx->cb_fn = _nvmf_ns_resize;
1857 : :
1858 : : /* Specify 0 for the nsid here, because we do not need to pause the namespace.
1859 : : * Namespaces can only be resized bigger, so there is no need to quiesce I/O.
1860 : : */
1861 : 96 : rc = spdk_nvmf_subsystem_pause(ns->subsystem, 0, _nvmf_ns_resize, ns_ctx);
1862 [ - + ]: 96 : if (rc) {
1863 [ # # ]: 0 : if (rc == -EBUSY) {
1864 : : /* Try again, this is not a permanent situation. */
1865 : 0 : spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx);
1866 : : } else {
1867 : 0 : SPDK_ERRLOG("Unable to pause subsystem to process namespace resize!\n");
1868 : 0 : free(ns_ctx);
1869 : : }
1870 : : }
1871 : : }
1872 : :
1873 : : static void
1874 : 124 : nvmf_ns_event(enum spdk_bdev_event_type type,
1875 : : struct spdk_bdev *bdev,
1876 : : void *event_ctx)
1877 : : {
1878 [ - + - + ]: 124 : SPDK_DEBUGLOG(nvmf, "Bdev event: type %d, name %s, subsystem_id %d, ns_id %d\n",
1879 : : type,
1880 : : spdk_bdev_get_name(bdev),
1881 : : ((struct spdk_nvmf_ns *)event_ctx)->subsystem->id,
1882 : : ((struct spdk_nvmf_ns *)event_ctx)->nsid);
1883 : :
1884 [ + + - ]: 124 : switch (type) {
1885 : 28 : case SPDK_BDEV_EVENT_REMOVE:
1886 : 28 : nvmf_ns_hot_remove(event_ctx);
1887 : 28 : break;
1888 : 96 : case SPDK_BDEV_EVENT_RESIZE:
1889 : 96 : nvmf_ns_resize(event_ctx);
1890 : 96 : break;
1891 : 0 : default:
1892 : 0 : SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
1893 : 0 : break;
1894 : : }
1895 : 124 : }
1896 : :
1897 : : void
1898 : 8043 : spdk_nvmf_ns_opts_get_defaults(struct spdk_nvmf_ns_opts *opts, size_t opts_size)
1899 : : {
1900 [ - + ]: 8043 : if (!opts) {
1901 : 0 : SPDK_ERRLOG("opts should not be NULL.\n");
1902 : 0 : return;
1903 : : }
1904 : :
1905 [ - + ]: 8043 : if (!opts_size) {
1906 : 0 : SPDK_ERRLOG("opts_size should not be zero.\n");
1907 : 0 : return;
1908 : : }
1909 : :
1910 [ - + ]: 8043 : memset(opts, 0, opts_size);
1911 : 8043 : opts->opts_size = opts_size;
1912 : :
1913 : : #define FIELD_OK(field) \
1914 : : offsetof(struct spdk_nvmf_ns_opts, field) + sizeof(opts->field) <= opts_size
1915 : :
1916 : : #define SET_FIELD(field, value) \
1917 : : if (FIELD_OK(field)) { \
1918 : : opts->field = value; \
1919 : : } \
1920 : :
1921 : : /* All current fields are set to 0 by default. */
1922 [ + - ]: 8043 : SET_FIELD(nsid, 0);
1923 [ + - ]: 8043 : if (FIELD_OK(nguid)) {
1924 [ - + ]: 8043 : memset(opts->nguid, 0, sizeof(opts->nguid));
1925 : : }
1926 [ + - ]: 8043 : if (FIELD_OK(eui64)) {
1927 [ - + ]: 8043 : memset(opts->eui64, 0, sizeof(opts->eui64));
1928 : : }
1929 [ + - ]: 8043 : if (FIELD_OK(uuid)) {
1930 : 8043 : spdk_uuid_set_null(&opts->uuid);
1931 : : }
1932 [ + - ]: 8043 : SET_FIELD(anagrpid, 0);
1933 [ + - ]: 8043 : SET_FIELD(transport_specific, NULL);
1934 : :
1935 : : #undef FIELD_OK
1936 : : #undef SET_FIELD
1937 : : }
1938 : :
1939 : : static void
1940 : 4020 : nvmf_ns_opts_copy(struct spdk_nvmf_ns_opts *opts,
1941 : : const struct spdk_nvmf_ns_opts *user_opts,
1942 : : size_t opts_size)
1943 : : {
1944 : : #define FIELD_OK(field) \
1945 : : offsetof(struct spdk_nvmf_ns_opts, field) + sizeof(opts->field) <= user_opts->opts_size
1946 : :
1947 : : #define SET_FIELD(field) \
1948 : : if (FIELD_OK(field)) { \
1949 : : opts->field = user_opts->field; \
1950 : : } \
1951 : :
1952 [ + - ]: 4020 : SET_FIELD(nsid);
1953 [ + - ]: 4020 : if (FIELD_OK(nguid)) {
1954 : 4020 : memcpy(opts->nguid, user_opts->nguid, sizeof(opts->nguid));
1955 : : }
1956 [ + - ]: 4020 : if (FIELD_OK(eui64)) {
1957 : 4020 : memcpy(opts->eui64, user_opts->eui64, sizeof(opts->eui64));
1958 : : }
1959 [ + - ]: 4020 : if (FIELD_OK(uuid)) {
1960 : 4020 : spdk_uuid_copy(&opts->uuid, &user_opts->uuid);
1961 : : }
1962 [ + - ]: 4020 : SET_FIELD(anagrpid);
1963 [ + - - + ]: 4020 : SET_FIELD(no_auto_visible);
1964 [ + - ]: 4020 : SET_FIELD(transport_specific);
1965 : :
1966 : 4020 : opts->opts_size = user_opts->opts_size;
1967 : :
1968 : : /* We should not remove this statement, but need to update the assert statement
1969 : : * if we add a new field, and also add a corresponding SET_FIELD statement.
1970 : : */
1971 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_ns_opts) == 72, "Incorrect size");
1972 : :
1973 : : #undef FIELD_OK
1974 : : #undef SET_FIELD
1975 : 4020 : }
1976 : :
1977 : : /* Dummy bdev module used to to claim bdevs. */
1978 : : static struct spdk_bdev_module ns_bdev_module = {
1979 : : .name = "NVMe-oF Target",
1980 : : };
1981 : :
1982 : : static int nvmf_ns_reservation_update(const struct spdk_nvmf_ns *ns,
1983 : : const struct spdk_nvmf_reservation_info *info);
1984 : : static int nvmf_ns_reservation_load(const struct spdk_nvmf_ns *ns,
1985 : : struct spdk_nvmf_reservation_info *info);
1986 : : static int nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns,
1987 : : struct spdk_nvmf_reservation_info *info);
1988 : :
1989 : : bool
1990 : 393 : nvmf_subsystem_zone_append_supported(struct spdk_nvmf_subsystem *subsystem)
1991 : : {
1992 : : struct spdk_nvmf_ns *ns;
1993 : :
1994 [ # # ]: 393 : for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem);
1995 [ + + ]: 503 : ns != NULL;
1996 : 110 : ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
1997 [ - + - - ]: 110 : if (spdk_bdev_is_zoned(ns->bdev) &&
1998 : 0 : spdk_bdev_io_type_supported(ns->bdev, SPDK_BDEV_IO_TYPE_ZONE_APPEND)) {
1999 : 0 : return true;
2000 : : }
2001 : : }
2002 : :
2003 : 393 : return false;
2004 : : }
2005 : :
2006 : : uint32_t
2007 : 4023 : spdk_nvmf_subsystem_add_ns_ext(struct spdk_nvmf_subsystem *subsystem, const char *bdev_name,
2008 : : const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size,
2009 : : const char *ptpl_file)
2010 : : {
2011 : : struct spdk_nvmf_transport *transport;
2012 : 54 : struct spdk_nvmf_ns_opts opts;
2013 : : struct spdk_nvmf_ns *ns, *first_ns;
2014 : : struct spdk_nvmf_ctrlr *ctrlr;
2015 : 4023 : struct spdk_nvmf_reservation_info info = {0};
2016 : : int rc;
2017 : : bool zone_append_supported;
2018 : : uint64_t max_zone_append_size_kib;
2019 : :
2020 [ + + ]: 4023 : if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
2021 [ - + ]: 4002 : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) {
2022 : 0 : return 0;
2023 : : }
2024 : :
2025 : 4023 : spdk_nvmf_ns_opts_get_defaults(&opts, sizeof(opts));
2026 [ + + ]: 4023 : if (user_opts) {
2027 : 4020 : nvmf_ns_opts_copy(&opts, user_opts, opts_size);
2028 : : }
2029 : :
2030 [ + + ]: 4023 : if (opts.nsid == SPDK_NVME_GLOBAL_NS_TAG) {
2031 : 3 : SPDK_ERRLOG("Invalid NSID %" PRIu32 "\n", opts.nsid);
2032 : 3 : return 0;
2033 : : }
2034 : :
2035 [ + + ]: 4020 : if (opts.nsid == 0) {
2036 : : /*
2037 : : * NSID not specified - find a free index.
2038 : : *
2039 : : * If no free slots are found, return error.
2040 : : */
2041 [ + - ]: 2402 : for (opts.nsid = 1; opts.nsid <= subsystem->max_nsid; opts.nsid++) {
2042 [ + + ]: 2402 : if (_nvmf_subsystem_get_ns(subsystem, opts.nsid) == NULL) {
2043 : 2339 : break;
2044 : : }
2045 : : }
2046 [ - + ]: 2339 : if (opts.nsid > subsystem->max_nsid) {
2047 : 0 : SPDK_ERRLOG("No free namespace slot available in the subsystem\n");
2048 : 0 : return 0;
2049 : : }
2050 : : }
2051 : :
2052 [ - + ]: 4020 : if (opts.nsid > subsystem->max_nsid) {
2053 : 0 : SPDK_ERRLOG("NSID greater than maximum not allowed\n");
2054 : 0 : return 0;
2055 : : }
2056 : :
2057 [ + + ]: 4020 : if (_nvmf_subsystem_get_ns(subsystem, opts.nsid)) {
2058 : 1349 : SPDK_ERRLOG("Requested NSID %" PRIu32 " already in use\n", opts.nsid);
2059 : 1349 : return 0;
2060 : : }
2061 : :
2062 [ + - ]: 2671 : if (opts.anagrpid == 0) {
2063 : 2671 : opts.anagrpid = opts.nsid;
2064 : : }
2065 : :
2066 [ - + ]: 2671 : if (opts.anagrpid > subsystem->max_nsid) {
2067 : 0 : SPDK_ERRLOG("ANAGRPID greater than maximum NSID not allowed\n");
2068 : 0 : return 0;
2069 : : }
2070 : :
2071 : 2671 : ns = calloc(1, sizeof(*ns));
2072 [ - + ]: 2671 : if (ns == NULL) {
2073 : 0 : SPDK_ERRLOG("Namespace allocation failed\n");
2074 : 0 : return 0;
2075 : : }
2076 : :
2077 : 2671 : TAILQ_INIT(&ns->hosts);
2078 [ - + ]: 2671 : ns->always_visible = !opts.no_auto_visible;
2079 [ + + + + ]: 2671 : if (ns->always_visible) {
2080 [ + + ]: 4433 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
2081 : 1771 : spdk_bit_array_set(ctrlr->visible_ns, opts.nsid - 1);
2082 : : }
2083 : : }
2084 : :
2085 : 2671 : rc = spdk_bdev_open_ext(bdev_name, true, nvmf_ns_event, ns, &ns->desc);
2086 [ + + ]: 2671 : if (rc != 0) {
2087 : 4 : SPDK_ERRLOG("Subsystem %s: bdev %s cannot be opened, error=%d\n",
2088 : : subsystem->subnqn, bdev_name, rc);
2089 : 4 : free(ns);
2090 : 4 : return 0;
2091 : : }
2092 : :
2093 : 2667 : ns->bdev = spdk_bdev_desc_get_bdev(ns->desc);
2094 : :
2095 [ + + ]: 2667 : if (spdk_bdev_get_md_size(ns->bdev) != 0) {
2096 [ - + ]: 30 : if (!spdk_bdev_is_md_interleaved(ns->bdev)) {
2097 : 0 : SPDK_ERRLOG("Can't attach bdev with separate metadata.\n");
2098 : 0 : spdk_bdev_close(ns->desc);
2099 : 0 : free(ns);
2100 : 0 : return 0;
2101 : : }
2102 : :
2103 [ - + ]: 30 : if (spdk_bdev_get_md_size(ns->bdev) > SPDK_BDEV_MAX_INTERLEAVED_MD_SIZE) {
2104 : 0 : SPDK_ERRLOG("Maximum supported interleaved md size %u, current md size %u\n",
2105 : : SPDK_BDEV_MAX_INTERLEAVED_MD_SIZE, spdk_bdev_get_md_size(ns->bdev));
2106 : 0 : spdk_bdev_close(ns->desc);
2107 : 0 : free(ns);
2108 : 0 : return 0;
2109 : : }
2110 : : }
2111 : :
2112 : 2667 : rc = spdk_bdev_module_claim_bdev(ns->bdev, ns->desc, &ns_bdev_module);
2113 [ - + ]: 2667 : if (rc != 0) {
2114 : 0 : spdk_bdev_close(ns->desc);
2115 : 0 : free(ns);
2116 : 0 : return 0;
2117 : : }
2118 : :
2119 : : /* Cache the zcopy capability of the bdev device */
2120 : 2667 : ns->zcopy = spdk_bdev_io_type_supported(ns->bdev, SPDK_BDEV_IO_TYPE_ZCOPY);
2121 : :
2122 [ + + ]: 2667 : if (spdk_uuid_is_null(&opts.uuid)) {
2123 : 2633 : opts.uuid = *spdk_bdev_get_uuid(ns->bdev);
2124 : : }
2125 : :
2126 : : /* if nguid descriptor is supported by bdev module (nvme) then uuid = nguid */
2127 [ + + ]: 2667 : if (spdk_mem_all_zero(opts.nguid, sizeof(opts.nguid))) {
2128 : : SPDK_STATIC_ASSERT(sizeof(opts.nguid) == sizeof(opts.uuid), "size mismatch");
2129 : 2630 : memcpy(opts.nguid, spdk_bdev_get_uuid(ns->bdev), sizeof(opts.nguid));
2130 : : }
2131 : :
2132 [ - + ]: 2667 : if (spdk_bdev_is_zoned(ns->bdev)) {
2133 [ # # # # ]: 0 : SPDK_DEBUGLOG(nvmf, "The added namespace is backed by a zoned block device.\n");
2134 : 0 : ns->csi = SPDK_NVME_CSI_ZNS;
2135 : :
2136 : 0 : zone_append_supported = spdk_bdev_io_type_supported(ns->bdev,
2137 : : SPDK_BDEV_IO_TYPE_ZONE_APPEND);
2138 : 0 : max_zone_append_size_kib = spdk_bdev_get_max_zone_append_size(
2139 : 0 : ns->bdev) * spdk_bdev_get_block_size(ns->bdev);
2140 : :
2141 [ # # # # ]: 0 : if (_nvmf_subsystem_get_first_zoned_ns(subsystem) != NULL &&
2142 : 0 : (nvmf_subsystem_zone_append_supported(subsystem) != zone_append_supported ||
2143 [ # # ]: 0 : subsystem->max_zone_append_size_kib != max_zone_append_size_kib)) {
2144 : 0 : SPDK_ERRLOG("Namespaces with different zone append support or different zone append size are not allowed.\n");
2145 : 0 : goto err;
2146 : : }
2147 : :
2148 : 0 : subsystem->max_zone_append_size_kib = max_zone_append_size_kib;
2149 : : }
2150 : :
2151 : 2667 : first_ns = spdk_nvmf_subsystem_get_first_ns(subsystem);
2152 [ + + ]: 2667 : if (!first_ns) {
2153 [ + + ]: 2300 : if (spdk_bdev_get_nvme_ctratt(ns->bdev).bits.fdps) {
2154 [ - + - + ]: 3 : SPDK_DEBUGLOG(nvmf, "Subsystem with id: %u has FDP capability.\n",
2155 : : subsystem->id);
2156 : 3 : subsystem->fdp_supported = true;
2157 : : }
2158 : : } else {
2159 : 367 : if (spdk_bdev_get_nvme_ctratt(first_ns->bdev).bits.fdps !=
2160 [ + + ]: 367 : spdk_bdev_get_nvme_ctratt(ns->bdev).bits.fdps) {
2161 [ + - ]: 3 : SPDK_ERRLOG("Subsystem with id: %u can%s FDP namespace.\n", subsystem->id,
2162 : : spdk_bdev_get_nvme_ctratt(first_ns->bdev).bits.fdps ? " only add" : "not add");
2163 : 3 : goto err;
2164 : : }
2165 : : }
2166 : :
2167 : 2664 : ns->opts = opts;
2168 : 2664 : ns->subsystem = subsystem;
2169 : 2664 : subsystem->ns[opts.nsid - 1] = ns;
2170 : 2664 : ns->nsid = opts.nsid;
2171 : 2664 : ns->anagrpid = opts.anagrpid;
2172 : 2664 : subsystem->ana_group[ns->anagrpid - 1]++;
2173 : 2664 : TAILQ_INIT(&ns->registrants);
2174 [ - + ]: 2664 : if (ptpl_file) {
2175 [ # # ]: 0 : ns->ptpl_file = strdup(ptpl_file);
2176 [ # # ]: 0 : if (!ns->ptpl_file) {
2177 : 0 : SPDK_ERRLOG("Namespace ns->ptpl_file allocation failed\n");
2178 : 0 : goto err;
2179 : : }
2180 : : }
2181 : :
2182 [ + + ]: 2664 : if (nvmf_ns_is_ptpl_capable(ns)) {
2183 : 3 : rc = nvmf_ns_reservation_load(ns, &info);
2184 [ - + ]: 3 : if (rc) {
2185 : 0 : SPDK_ERRLOG("Subsystem load reservation failed\n");
2186 : 0 : goto err;
2187 : : }
2188 : :
2189 : 3 : rc = nvmf_ns_reservation_restore(ns, &info);
2190 [ - + ]: 3 : if (rc) {
2191 : 0 : SPDK_ERRLOG("Subsystem restore reservation failed\n");
2192 : 0 : goto err;
2193 : : }
2194 : : }
2195 : :
2196 [ + + ]: 5316 : for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport;
2197 : 2652 : transport = spdk_nvmf_transport_get_next(transport)) {
2198 [ - + ]: 2652 : if (transport->ops->subsystem_add_ns) {
2199 : 0 : rc = transport->ops->subsystem_add_ns(transport, subsystem, ns);
2200 [ # # ]: 0 : if (rc) {
2201 : 0 : SPDK_ERRLOG("Namespace attachment is not allowed by %s transport\n", transport->ops->name);
2202 : 0 : nvmf_ns_reservation_clear_all_registrants(ns);
2203 : 0 : goto err;
2204 : : }
2205 : : }
2206 : : }
2207 : :
2208 : : /* JSON value obj is freed before sending the response. Set NULL to prevent usage of dangling pointer. */
2209 : 2664 : ns->opts.transport_specific = NULL;
2210 : :
2211 [ - + - + ]: 2664 : SPDK_DEBUGLOG(nvmf, "Subsystem %s: bdev %s assigned nsid %" PRIu32 "\n",
2212 : : spdk_nvmf_subsystem_get_nqn(subsystem),
2213 : : bdev_name,
2214 : : opts.nsid);
2215 : :
2216 : 2664 : nvmf_subsystem_ns_changed(subsystem, opts.nsid);
2217 : :
2218 : 902 : SPDK_DTRACE_PROBE2(nvmf_subsystem_add_ns, subsystem->subnqn, ns->nsid);
2219 : :
2220 : 2664 : return opts.nsid;
2221 : 3 : err:
2222 : 3 : subsystem->ns[opts.nsid - 1] = NULL;
2223 : 3 : spdk_bdev_module_release_bdev(ns->bdev);
2224 : 3 : spdk_bdev_close(ns->desc);
2225 : 3 : free(ns->ptpl_file);
2226 : 3 : free(ns);
2227 : :
2228 : 3 : return 0;
2229 : : }
2230 : :
2231 : : static uint32_t
2232 : 17004 : nvmf_subsystem_get_next_allocated_nsid(struct spdk_nvmf_subsystem *subsystem,
2233 : : uint32_t prev_nsid)
2234 : : {
2235 : : uint32_t nsid;
2236 : :
2237 [ + + ]: 17004 : if (prev_nsid >= subsystem->max_nsid) {
2238 : 979 : return 0;
2239 : : }
2240 : :
2241 [ + + ]: 268037 : for (nsid = prev_nsid + 1; nsid <= subsystem->max_nsid; nsid++) {
2242 [ + + ]: 257397 : if (subsystem->ns[nsid - 1]) {
2243 : 5385 : return nsid;
2244 : : }
2245 : : }
2246 : :
2247 : 10640 : return 0;
2248 : : }
2249 : :
2250 : : struct spdk_nvmf_ns *
2251 : 12281 : spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem)
2252 : : {
2253 : : uint32_t first_nsid;
2254 : :
2255 : 12281 : first_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, 0);
2256 : 12281 : return _nvmf_subsystem_get_ns(subsystem, first_nsid);
2257 : : }
2258 : :
2259 : : struct spdk_nvmf_ns *
2260 : 4723 : spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem,
2261 : : struct spdk_nvmf_ns *prev_ns)
2262 : : {
2263 : : uint32_t next_nsid;
2264 : :
2265 : 4723 : next_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, prev_ns->opts.nsid);
2266 : 4723 : return _nvmf_subsystem_get_ns(subsystem, next_nsid);
2267 : : }
2268 : :
2269 : : struct spdk_nvmf_ns *
2270 : 0 : spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
2271 : : {
2272 : 0 : return _nvmf_subsystem_get_ns(subsystem, nsid);
2273 : : }
2274 : :
2275 : : uint32_t
2276 : 725 : spdk_nvmf_ns_get_id(const struct spdk_nvmf_ns *ns)
2277 : : {
2278 : 725 : return ns->opts.nsid;
2279 : : }
2280 : :
2281 : : struct spdk_bdev *
2282 : 1411 : spdk_nvmf_ns_get_bdev(struct spdk_nvmf_ns *ns)
2283 : : {
2284 : 1411 : return ns->bdev;
2285 : : }
2286 : :
2287 : : void
2288 : 725 : spdk_nvmf_ns_get_opts(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_ns_opts *opts,
2289 : : size_t opts_size)
2290 : : {
2291 [ - + ]: 725 : memset(opts, 0, opts_size);
2292 [ - + - + ]: 725 : memcpy(opts, &ns->opts, spdk_min(sizeof(ns->opts), opts_size));
2293 : 725 : }
2294 : :
2295 : : const char *
2296 : 2185 : spdk_nvmf_subsystem_get_sn(const struct spdk_nvmf_subsystem *subsystem)
2297 : : {
2298 : 2185 : return subsystem->sn;
2299 : : }
2300 : :
2301 : : int
2302 : 335 : spdk_nvmf_subsystem_set_sn(struct spdk_nvmf_subsystem *subsystem, const char *sn)
2303 : : {
2304 : : size_t len, max_len;
2305 : :
2306 : 335 : max_len = sizeof(subsystem->sn) - 1;
2307 [ - + ]: 335 : len = strlen(sn);
2308 [ + + ]: 335 : if (len > max_len) {
2309 [ - + - + ]: 9 : SPDK_DEBUGLOG(nvmf, "Invalid sn \"%s\": length %zu > max %zu\n",
2310 : : sn, len, max_len);
2311 : 9 : return -1;
2312 : : }
2313 : :
2314 [ + + ]: 326 : if (!nvmf_valid_ascii_string(sn, len)) {
2315 [ - + - + ]: 3 : SPDK_DEBUGLOG(nvmf, "Non-ASCII sn\n");
2316 [ - + - + ]: 3 : SPDK_LOGDUMP(nvmf, "sn", sn, len);
2317 : 3 : return -1;
2318 : : }
2319 : :
2320 : 323 : snprintf(subsystem->sn, sizeof(subsystem->sn), "%s", sn);
2321 : :
2322 : 323 : return 0;
2323 : : }
2324 : :
2325 : : const char *
2326 : 2185 : spdk_nvmf_subsystem_get_mn(const struct spdk_nvmf_subsystem *subsystem)
2327 : : {
2328 : 2185 : return subsystem->mn;
2329 : : }
2330 : :
2331 : : int
2332 : 30 : spdk_nvmf_subsystem_set_mn(struct spdk_nvmf_subsystem *subsystem, const char *mn)
2333 : : {
2334 : : size_t len, max_len;
2335 : :
2336 [ - + ]: 30 : if (mn == NULL) {
2337 : 0 : mn = MODEL_NUMBER_DEFAULT;
2338 : : }
2339 : 30 : max_len = sizeof(subsystem->mn) - 1;
2340 [ - + ]: 30 : len = strlen(mn);
2341 [ + + ]: 30 : if (len > max_len) {
2342 [ - + - + ]: 3 : SPDK_DEBUGLOG(nvmf, "Invalid mn \"%s\": length %zu > max %zu\n",
2343 : : mn, len, max_len);
2344 : 3 : return -1;
2345 : : }
2346 : :
2347 [ + + ]: 27 : if (!nvmf_valid_ascii_string(mn, len)) {
2348 [ - + - + ]: 3 : SPDK_DEBUGLOG(nvmf, "Non-ASCII mn\n");
2349 [ - + - + ]: 3 : SPDK_LOGDUMP(nvmf, "mn", mn, len);
2350 : 3 : return -1;
2351 : : }
2352 : :
2353 : 24 : snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", mn);
2354 : :
2355 : 24 : return 0;
2356 : : }
2357 : :
2358 : : const char *
2359 : 7506 : spdk_nvmf_subsystem_get_nqn(const struct spdk_nvmf_subsystem *subsystem)
2360 : : {
2361 : 7506 : return subsystem->subnqn;
2362 : : }
2363 : :
2364 : : /* We have to use the typedef in the function declaration to appease astyle. */
2365 : : typedef enum spdk_nvmf_subtype spdk_nvmf_subtype_t;
2366 : :
2367 : : spdk_nvmf_subtype_t
2368 : 2045 : spdk_nvmf_subsystem_get_type(struct spdk_nvmf_subsystem *subsystem)
2369 : : {
2370 : 2045 : return subsystem->subtype;
2371 : : }
2372 : :
2373 : : uint32_t
2374 : 4 : spdk_nvmf_subsystem_get_max_nsid(struct spdk_nvmf_subsystem *subsystem)
2375 : : {
2376 : 4 : return subsystem->max_nsid;
2377 : : }
2378 : :
2379 : : int
2380 : 388 : spdk_nvmf_subsystem_set_cntlid_range(struct spdk_nvmf_subsystem *subsystem,
2381 : : uint16_t min_cntlid, uint16_t max_cntlid)
2382 : : {
2383 [ - + ]: 388 : if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
2384 : 0 : return -EAGAIN;
2385 : : }
2386 : :
2387 [ + + ]: 388 : if (min_cntlid > max_cntlid) {
2388 : 9 : return -EINVAL;
2389 : : }
2390 : : /* The spec reserves cntlid values in the range FFF0h to FFFFh. */
2391 [ + + + - : 379 : if (min_cntlid < NVMF_MIN_CNTLID || min_cntlid > NVMF_MAX_CNTLID ||
+ - ]
2392 [ + + ]: 376 : max_cntlid < NVMF_MIN_CNTLID || max_cntlid > NVMF_MAX_CNTLID) {
2393 : 6 : return -EINVAL;
2394 : : }
2395 : 373 : subsystem->min_cntlid = min_cntlid;
2396 : 373 : subsystem->max_cntlid = max_cntlid;
2397 [ - + - - ]: 373 : if (subsystem->next_cntlid < min_cntlid || subsystem->next_cntlid > max_cntlid - 1) {
2398 : 373 : subsystem->next_cntlid = min_cntlid - 1;
2399 : : }
2400 : :
2401 : 373 : return 0;
2402 : : }
2403 : :
2404 : : static uint16_t
2405 : 1615 : nvmf_subsystem_gen_cntlid(struct spdk_nvmf_subsystem *subsystem)
2406 : : {
2407 : : int count;
2408 : :
2409 : : /*
2410 : : * In the worst case, we might have to try all CNTLID values between min_cntlid and max_cntlid
2411 : : * before we find one that is unused (or find that all values are in use).
2412 : : */
2413 [ + - ]: 1615 : for (count = 0; count < subsystem->max_cntlid - subsystem->min_cntlid + 1; count++) {
2414 : 1615 : subsystem->next_cntlid++;
2415 [ - + ]: 1615 : if (subsystem->next_cntlid > subsystem->max_cntlid) {
2416 : 0 : subsystem->next_cntlid = subsystem->min_cntlid;
2417 : : }
2418 : :
2419 : : /* Check if a controller with this cntlid currently exists. */
2420 [ + - ]: 1615 : if (nvmf_subsystem_get_ctrlr(subsystem, subsystem->next_cntlid) == NULL) {
2421 : : /* Found unused cntlid */
2422 : 1615 : return subsystem->next_cntlid;
2423 : : }
2424 : : }
2425 : :
2426 : : /* All valid cntlid values are in use. */
2427 : 0 : return 0xFFFF;
2428 : : }
2429 : :
2430 : : int
2431 : 1662 : nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr)
2432 : : {
2433 : :
2434 [ + + + + ]: 1662 : if (ctrlr->dynamic_ctrlr) {
2435 : 1615 : ctrlr->cntlid = nvmf_subsystem_gen_cntlid(subsystem);
2436 [ - + ]: 1615 : if (ctrlr->cntlid == 0xFFFF) {
2437 : : /* Unable to get a cntlid */
2438 : 0 : SPDK_ERRLOG("Reached max simultaneous ctrlrs\n");
2439 : 0 : return -EBUSY;
2440 : : }
2441 [ - + ]: 47 : } else if (nvmf_subsystem_get_ctrlr(subsystem, ctrlr->cntlid) != NULL) {
2442 : 0 : SPDK_ERRLOG("Ctrlr with cntlid %u already exist\n", ctrlr->cntlid);
2443 : 0 : return -EEXIST;
2444 : : }
2445 : :
2446 : 1662 : TAILQ_INSERT_TAIL(&subsystem->ctrlrs, ctrlr, link);
2447 : :
2448 : 740 : SPDK_DTRACE_PROBE3(nvmf_subsystem_add_ctrlr, subsystem->subnqn, ctrlr, ctrlr->hostnqn);
2449 : :
2450 : 1662 : return 0;
2451 : : }
2452 : :
2453 : : void
2454 : 1662 : nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem,
2455 : : struct spdk_nvmf_ctrlr *ctrlr)
2456 : : {
2457 : 740 : SPDK_DTRACE_PROBE3(nvmf_subsystem_remove_ctrlr, subsystem->subnqn, ctrlr, ctrlr->hostnqn);
2458 : :
2459 [ - + ]: 1662 : assert(spdk_get_thread() == subsystem->thread);
2460 [ - + ]: 1662 : assert(subsystem == ctrlr->subsys);
2461 [ - + - + ]: 1662 : SPDK_DEBUGLOG(nvmf, "remove ctrlr %p id 0x%x from subsys %p %s\n", ctrlr, ctrlr->cntlid, subsystem,
2462 : : subsystem->subnqn);
2463 [ + + ]: 1662 : TAILQ_REMOVE(&subsystem->ctrlrs, ctrlr, link);
2464 : 1662 : }
2465 : :
2466 : : struct spdk_nvmf_ctrlr *
2467 : 6824 : nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, uint16_t cntlid)
2468 : : {
2469 : : struct spdk_nvmf_ctrlr *ctrlr;
2470 : :
2471 [ + + ]: 7060 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
2472 [ + + ]: 5398 : if (ctrlr->cntlid == cntlid) {
2473 : 5162 : return ctrlr;
2474 : : }
2475 : : }
2476 : :
2477 : 1662 : return NULL;
2478 : : }
2479 : :
2480 : : uint32_t
2481 : 654 : spdk_nvmf_subsystem_get_max_namespaces(const struct spdk_nvmf_subsystem *subsystem)
2482 : : {
2483 : 654 : return subsystem->max_nsid;
2484 : : }
2485 : :
2486 : : uint16_t
2487 : 654 : spdk_nvmf_subsystem_get_min_cntlid(const struct spdk_nvmf_subsystem *subsystem)
2488 : : {
2489 : 654 : return subsystem->min_cntlid;
2490 : : }
2491 : :
2492 : : uint16_t
2493 : 654 : spdk_nvmf_subsystem_get_max_cntlid(const struct spdk_nvmf_subsystem *subsystem)
2494 : : {
2495 : 654 : return subsystem->max_cntlid;
2496 : : }
2497 : :
2498 : : struct _nvmf_ns_registrant {
2499 : : uint64_t rkey;
2500 : : char *host_uuid;
2501 : : };
2502 : :
2503 : : struct _nvmf_ns_registrants {
2504 : : size_t num_regs;
2505 : : struct _nvmf_ns_registrant reg[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2506 : : };
2507 : :
2508 : : struct _nvmf_ns_reservation {
2509 : : bool ptpl_activated;
2510 : : enum spdk_nvme_reservation_type rtype;
2511 : : uint64_t crkey;
2512 : : char *bdev_uuid;
2513 : : char *holder_uuid;
2514 : : struct _nvmf_ns_registrants regs;
2515 : : };
2516 : :
2517 : : static const struct spdk_json_object_decoder nvmf_ns_pr_reg_decoders[] = {
2518 : : {"rkey", offsetof(struct _nvmf_ns_registrant, rkey), spdk_json_decode_uint64},
2519 : : {"host_uuid", offsetof(struct _nvmf_ns_registrant, host_uuid), spdk_json_decode_string},
2520 : : };
2521 : :
2522 : : static int
2523 : 12 : nvmf_decode_ns_pr_reg(const struct spdk_json_val *val, void *out)
2524 : : {
2525 : 12 : struct _nvmf_ns_registrant *reg = out;
2526 : :
2527 : 12 : return spdk_json_decode_object(val, nvmf_ns_pr_reg_decoders,
2528 : : SPDK_COUNTOF(nvmf_ns_pr_reg_decoders), reg);
2529 : : }
2530 : :
2531 : : static int
2532 : 12 : nvmf_decode_ns_pr_regs(const struct spdk_json_val *val, void *out)
2533 : : {
2534 : 12 : struct _nvmf_ns_registrants *regs = out;
2535 : :
2536 : 12 : return spdk_json_decode_array(val, nvmf_decode_ns_pr_reg, regs->reg,
2537 : : SPDK_NVMF_MAX_NUM_REGISTRANTS, ®s->num_regs,
2538 : : sizeof(struct _nvmf_ns_registrant));
2539 : : }
2540 : :
2541 : : static const struct spdk_json_object_decoder nvmf_ns_pr_decoders[] = {
2542 : : {"ptpl", offsetof(struct _nvmf_ns_reservation, ptpl_activated), spdk_json_decode_bool, true},
2543 : : {"rtype", offsetof(struct _nvmf_ns_reservation, rtype), spdk_json_decode_uint32, true},
2544 : : {"crkey", offsetof(struct _nvmf_ns_reservation, crkey), spdk_json_decode_uint64, true},
2545 : : {"bdev_uuid", offsetof(struct _nvmf_ns_reservation, bdev_uuid), spdk_json_decode_string},
2546 : : {"holder_uuid", offsetof(struct _nvmf_ns_reservation, holder_uuid), spdk_json_decode_string, true},
2547 : : {"registrants", offsetof(struct _nvmf_ns_reservation, regs), nvmf_decode_ns_pr_regs},
2548 : : };
2549 : :
2550 : : static int
2551 : 15 : nvmf_ns_reservation_load_json(const struct spdk_nvmf_ns *ns,
2552 : : struct spdk_nvmf_reservation_info *info)
2553 : : {
2554 : 15 : size_t json_size;
2555 : : ssize_t values_cnt, rc;
2556 : 15 : void *json = NULL, *end;
2557 : 15 : struct spdk_json_val *values = NULL;
2558 : 15 : struct _nvmf_ns_reservation res = {};
2559 : 15 : const char *file = ns->ptpl_file;
2560 : : uint32_t i;
2561 : :
2562 : : /* It's not an error if the file does not exist */
2563 [ - + - + ]: 15 : if (access(file, F_OK) != 0) {
2564 [ # # # # ]: 0 : SPDK_DEBUGLOG(nvmf, "File %s does not exist\n", file);
2565 : 0 : return 0;
2566 : : }
2567 : :
2568 : : /* Load all persist file contents into a local buffer */
2569 : 15 : json = spdk_posix_file_load_from_name(file, &json_size);
2570 [ - + ]: 15 : if (!json) {
2571 : 0 : SPDK_ERRLOG("Load persist file %s failed\n", file);
2572 : 0 : return -ENOMEM;
2573 : : }
2574 : :
2575 : 15 : rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0);
2576 [ + + ]: 15 : if (rc < 0) {
2577 : 3 : SPDK_NOTICELOG("Parsing JSON configuration failed (%zd)\n", rc);
2578 : 3 : goto exit;
2579 : : }
2580 : :
2581 : 12 : values_cnt = rc;
2582 : 12 : values = calloc(values_cnt, sizeof(struct spdk_json_val));
2583 [ - + ]: 12 : if (values == NULL) {
2584 : 0 : goto exit;
2585 : : }
2586 : :
2587 : 12 : rc = spdk_json_parse(json, json_size, values, values_cnt, &end, 0);
2588 [ - + ]: 12 : if (rc != values_cnt) {
2589 : 0 : SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
2590 : 0 : goto exit;
2591 : : }
2592 : :
2593 : : /* Decode json */
2594 [ - + ]: 12 : if (spdk_json_decode_object(values, nvmf_ns_pr_decoders,
2595 : : SPDK_COUNTOF(nvmf_ns_pr_decoders),
2596 : : &res)) {
2597 : 0 : SPDK_ERRLOG("Invalid objects in the persist file %s\n", file);
2598 : 0 : rc = -EINVAL;
2599 : 0 : goto exit;
2600 : : }
2601 : :
2602 [ - + ]: 12 : if (res.regs.num_regs > SPDK_NVMF_MAX_NUM_REGISTRANTS) {
2603 : 0 : SPDK_ERRLOG("Can only support up to %u registrants\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
2604 : 0 : rc = -ERANGE;
2605 : 0 : goto exit;
2606 : : }
2607 : :
2608 : 12 : rc = 0;
2609 [ - + ]: 12 : info->ptpl_activated = res.ptpl_activated;
2610 : 12 : info->rtype = res.rtype;
2611 : 12 : info->crkey = res.crkey;
2612 [ - + ]: 12 : snprintf(info->bdev_uuid, sizeof(info->bdev_uuid), "%s", res.bdev_uuid);
2613 [ - + ]: 12 : snprintf(info->holder_uuid, sizeof(info->holder_uuid), "%s", res.holder_uuid);
2614 : 12 : info->num_regs = res.regs.num_regs;
2615 [ + + ]: 24 : for (i = 0; i < res.regs.num_regs; i++) {
2616 : 12 : info->registrants[i].rkey = res.regs.reg[i].rkey;
2617 [ - + ]: 12 : snprintf(info->registrants[i].host_uuid, sizeof(info->registrants[i].host_uuid), "%s",
2618 : 8 : res.regs.reg[i].host_uuid);
2619 : : }
2620 : :
2621 : 15 : exit:
2622 : 15 : free(json);
2623 : 15 : free(values);
2624 : 15 : free(res.bdev_uuid);
2625 : 15 : free(res.holder_uuid);
2626 [ + + ]: 27 : for (i = 0; i < res.regs.num_regs; i++) {
2627 : 12 : free(res.regs.reg[i].host_uuid);
2628 : : }
2629 : :
2630 : 15 : return rc;
2631 : : }
2632 : :
2633 : : static bool nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns);
2634 : :
2635 : : static int
2636 : 15 : nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
2637 : : {
2638 : : uint32_t i;
2639 : 15 : struct spdk_nvmf_registrant *reg, *holder = NULL;
2640 : 15 : struct spdk_uuid bdev_uuid, holder_uuid;
2641 : 15 : bool rkey_flag = false;
2642 : :
2643 [ - + - + ]: 15 : SPDK_DEBUGLOG(nvmf, "NSID %u, PTPL %u, Number of registrants %u\n",
2644 : : ns->nsid, info->ptpl_activated, info->num_regs);
2645 : :
2646 : : /* it's not an error */
2647 [ + - - + ]: 15 : if (!info->ptpl_activated || !info->num_regs) {
2648 : 0 : return 0;
2649 : : }
2650 : :
2651 : : /* Check info->crkey exist or not in info->registrants[i].rkey */
2652 [ + + ]: 42 : for (i = 0; i < info->num_regs; i++) {
2653 [ + + ]: 27 : if (info->crkey == info->registrants[i].rkey) {
2654 : 9 : rkey_flag = true;
2655 : : }
2656 : : }
2657 [ + + + + ]: 15 : if (!rkey_flag && info->crkey != 0) {
2658 : 3 : return -EINVAL;
2659 : : }
2660 : :
2661 : 12 : spdk_uuid_parse(&bdev_uuid, info->bdev_uuid);
2662 [ + + ]: 12 : if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) {
2663 : 3 : SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n");
2664 : 3 : return -EINVAL;
2665 : : }
2666 : :
2667 : 9 : ns->crkey = info->crkey;
2668 : 9 : ns->rtype = info->rtype;
2669 : 9 : ns->ptpl_activated = info->ptpl_activated;
2670 : 9 : spdk_uuid_parse(&holder_uuid, info->holder_uuid);
2671 : :
2672 [ - + - + ]: 9 : SPDK_DEBUGLOG(nvmf, "Bdev UUID %s\n", info->bdev_uuid);
2673 [ + + ]: 9 : if (info->rtype) {
2674 [ - + - + ]: 6 : SPDK_DEBUGLOG(nvmf, "Holder UUID %s, RTYPE %u, RKEY 0x%"PRIx64"\n",
2675 : : info->holder_uuid, info->rtype, info->crkey);
2676 : : }
2677 : :
2678 [ + + ]: 24 : for (i = 0; i < info->num_regs; i++) {
2679 : 15 : reg = calloc(1, sizeof(*reg));
2680 [ - + ]: 15 : if (!reg) {
2681 : 0 : return -ENOMEM;
2682 : : }
2683 : 15 : spdk_uuid_parse(®->hostid, info->registrants[i].host_uuid);
2684 : 15 : reg->rkey = info->registrants[i].rkey;
2685 : 15 : TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
2686 [ + + + + ]: 15 : if (info->crkey != 0 && !spdk_uuid_compare(&holder_uuid, ®->hostid)) {
2687 : 6 : holder = reg;
2688 : : }
2689 [ - + - + ]: 15 : SPDK_DEBUGLOG(nvmf, "Registrant RKEY 0x%"PRIx64", Host UUID %s\n",
2690 : : info->registrants[i].rkey, info->registrants[i].host_uuid);
2691 : : }
2692 : :
2693 [ + + ]: 9 : if (nvmf_ns_reservation_all_registrants_type(ns)) {
2694 : 3 : ns->holder = TAILQ_FIRST(&ns->registrants);
2695 : : } else {
2696 : 6 : ns->holder = holder;
2697 : : }
2698 : :
2699 : 9 : return 0;
2700 : : }
2701 : :
2702 : : static int
2703 : 15 : nvmf_ns_json_write_cb(void *cb_ctx, const void *data, size_t size)
2704 : : {
2705 : 15 : char *file = cb_ctx;
2706 : : size_t rc;
2707 : : FILE *fd;
2708 : :
2709 : 15 : fd = fopen(file, "w");
2710 [ - + ]: 15 : if (!fd) {
2711 : 0 : SPDK_ERRLOG("Can't open file %s for write\n", file);
2712 : 0 : return -ENOENT;
2713 : : }
2714 [ - + - + ]: 15 : rc = fwrite(data, 1, size, fd);
2715 : 15 : fclose(fd);
2716 : :
2717 [ + - ]: 15 : return rc == size ? 0 : -1;
2718 : : }
2719 : :
2720 : : static int
2721 : 15 : nvmf_ns_reservation_update_json(const struct spdk_nvmf_ns *ns,
2722 : : const struct spdk_nvmf_reservation_info *info)
2723 : : {
2724 : 15 : const char *file = ns->ptpl_file;
2725 : : struct spdk_json_write_ctx *w;
2726 : : uint32_t i;
2727 : 15 : int rc = 0;
2728 : :
2729 : 15 : w = spdk_json_write_begin(nvmf_ns_json_write_cb, (void *)file, 0);
2730 [ - + ]: 15 : if (w == NULL) {
2731 : 0 : return -ENOMEM;
2732 : : }
2733 : : /* clear the configuration file */
2734 [ + + ]: 15 : if (!info->ptpl_activated) {
2735 : 3 : goto exit;
2736 : : }
2737 : :
2738 : 12 : spdk_json_write_object_begin(w);
2739 : 12 : spdk_json_write_named_bool(w, "ptpl", info->ptpl_activated);
2740 : 12 : spdk_json_write_named_uint32(w, "rtype", info->rtype);
2741 : 12 : spdk_json_write_named_uint64(w, "crkey", info->crkey);
2742 : 12 : spdk_json_write_named_string(w, "bdev_uuid", info->bdev_uuid);
2743 : 12 : spdk_json_write_named_string(w, "holder_uuid", info->holder_uuid);
2744 : :
2745 : 12 : spdk_json_write_named_array_begin(w, "registrants");
2746 [ + + ]: 24 : for (i = 0; i < info->num_regs; i++) {
2747 : 12 : spdk_json_write_object_begin(w);
2748 : 12 : spdk_json_write_named_uint64(w, "rkey", info->registrants[i].rkey);
2749 : 12 : spdk_json_write_named_string(w, "host_uuid", info->registrants[i].host_uuid);
2750 : 12 : spdk_json_write_object_end(w);
2751 : : }
2752 : 12 : spdk_json_write_array_end(w);
2753 : 12 : spdk_json_write_object_end(w);
2754 : :
2755 : 15 : exit:
2756 : 15 : rc = spdk_json_write_end(w);
2757 : 15 : return rc;
2758 : : }
2759 : :
2760 : : static int
2761 : 21 : nvmf_ns_update_reservation_info(struct spdk_nvmf_ns *ns)
2762 : : {
2763 : 21 : struct spdk_nvmf_reservation_info info;
2764 : : struct spdk_nvmf_registrant *reg, *tmp;
2765 : 21 : uint32_t i = 0;
2766 : :
2767 [ - + ]: 21 : assert(ns != NULL);
2768 : :
2769 [ + - - + ]: 21 : if (!ns->bdev || !nvmf_ns_is_ptpl_capable(ns)) {
2770 : 0 : return 0;
2771 : : }
2772 : :
2773 [ # # ]: 21 : memset(&info, 0, sizeof(info));
2774 : 21 : spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), spdk_bdev_get_uuid(ns->bdev));
2775 : :
2776 [ + + ]: 21 : if (ns->rtype) {
2777 : 6 : info.rtype = ns->rtype;
2778 : 6 : info.crkey = ns->crkey;
2779 [ + - ]: 6 : if (!nvmf_ns_reservation_all_registrants_type(ns)) {
2780 [ - + ]: 6 : assert(ns->holder != NULL);
2781 : 6 : spdk_uuid_fmt_lower(info.holder_uuid, sizeof(info.holder_uuid), &ns->holder->hostid);
2782 : : }
2783 : : }
2784 : :
2785 [ + + ]: 42 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2786 : 21 : spdk_uuid_fmt_lower(info.registrants[i].host_uuid, sizeof(info.registrants[i].host_uuid),
2787 : 21 : ®->hostid);
2788 : 21 : info.registrants[i++].rkey = reg->rkey;
2789 : : }
2790 : :
2791 : 21 : info.num_regs = i;
2792 [ - + ]: 21 : info.ptpl_activated = ns->ptpl_activated;
2793 : :
2794 : 21 : return nvmf_ns_reservation_update(ns, &info);
2795 : : }
2796 : :
2797 : : static struct spdk_nvmf_registrant *
2798 : 330 : nvmf_ns_reservation_get_registrant(struct spdk_nvmf_ns *ns,
2799 : : struct spdk_uuid *uuid)
2800 : : {
2801 : : struct spdk_nvmf_registrant *reg, *tmp;
2802 : :
2803 [ + + ]: 573 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2804 [ + + ]: 453 : if (!spdk_uuid_compare(®->hostid, uuid)) {
2805 : 210 : return reg;
2806 : : }
2807 : : }
2808 : :
2809 : 120 : return NULL;
2810 : : }
2811 : :
2812 : : /* Generate reservation notice log to registered HostID controllers */
2813 : : static void
2814 : 30 : nvmf_subsystem_gen_ctrlr_notification(struct spdk_nvmf_subsystem *subsystem,
2815 : : struct spdk_nvmf_ns *ns,
2816 : : struct spdk_uuid *hostid_list,
2817 : : uint32_t num_hostid,
2818 : : enum spdk_nvme_reservation_notification_log_page_type type)
2819 : : {
2820 : : struct spdk_nvmf_ctrlr *ctrlr;
2821 : : uint32_t i;
2822 : :
2823 [ + + ]: 75 : for (i = 0; i < num_hostid; i++) {
2824 [ + + ]: 225 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
2825 [ + + ]: 180 : if (!spdk_uuid_compare(&ctrlr->hostid, &hostid_list[i])) {
2826 : 66 : nvmf_ctrlr_reservation_notice_log(ctrlr, ns, type);
2827 : : }
2828 : : }
2829 : : }
2830 : 30 : }
2831 : :
2832 : : /* Get all registrants' hostid other than the controller who issued the command */
2833 : : static uint32_t
2834 : 48 : nvmf_ns_reservation_get_all_other_hostid(struct spdk_nvmf_ns *ns,
2835 : : struct spdk_uuid *hostid_list,
2836 : : uint32_t max_num_hostid,
2837 : : struct spdk_uuid *current_hostid)
2838 : : {
2839 : : struct spdk_nvmf_registrant *reg, *tmp;
2840 : 48 : uint32_t num_hostid = 0;
2841 : :
2842 [ + + ]: 165 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2843 [ + + ]: 117 : if (spdk_uuid_compare(®->hostid, current_hostid)) {
2844 [ - + ]: 69 : if (num_hostid == max_num_hostid) {
2845 : 0 : assert(false);
2846 : : return max_num_hostid;
2847 : : }
2848 : 69 : hostid_list[num_hostid++] = reg->hostid;
2849 : : }
2850 : : }
2851 : :
2852 : 48 : return num_hostid;
2853 : : }
2854 : :
2855 : : /* Calculate the unregistered HostID list according to list
2856 : : * prior to execute preempt command and list after executing
2857 : : * preempt command.
2858 : : */
2859 : : static uint32_t
2860 : 9 : nvmf_ns_reservation_get_unregistered_hostid(struct spdk_uuid *old_hostid_list,
2861 : : uint32_t old_num_hostid,
2862 : : struct spdk_uuid *remaining_hostid_list,
2863 : : uint32_t remaining_num_hostid)
2864 : : {
2865 : 9 : struct spdk_uuid temp_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
2866 : 9 : uint32_t i, j, num_hostid = 0;
2867 : : bool found;
2868 : :
2869 [ + + ]: 9 : if (!remaining_num_hostid) {
2870 : 3 : return old_num_hostid;
2871 : : }
2872 : :
2873 [ + + ]: 18 : for (i = 0; i < old_num_hostid; i++) {
2874 : 12 : found = false;
2875 [ + + ]: 18 : for (j = 0; j < remaining_num_hostid; j++) {
2876 [ + + ]: 12 : if (!spdk_uuid_compare(&old_hostid_list[i], &remaining_hostid_list[j])) {
2877 : 6 : found = true;
2878 : 6 : break;
2879 : : }
2880 : : }
2881 [ + + ]: 12 : if (!found) {
2882 : 6 : spdk_uuid_copy(&temp_hostid_list[num_hostid++], &old_hostid_list[i]);
2883 : : }
2884 : : }
2885 : :
2886 [ + - ]: 6 : if (num_hostid) {
2887 [ - + - + ]: 6 : memcpy(old_hostid_list, temp_hostid_list, sizeof(struct spdk_uuid) * num_hostid);
2888 : : }
2889 : :
2890 : 6 : return num_hostid;
2891 : : }
2892 : :
2893 : : /* current reservation type is all registrants or not */
2894 : : static bool
2895 : 162 : nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns)
2896 : : {
2897 [ + + ]: 306 : return (ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS ||
2898 [ - + ]: 144 : ns->rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS);
2899 : : }
2900 : :
2901 : : /* current registrant is reservation holder or not */
2902 : : static bool
2903 : 78 : nvmf_ns_reservation_registrant_is_holder(struct spdk_nvmf_ns *ns,
2904 : : struct spdk_nvmf_registrant *reg)
2905 : : {
2906 [ - + ]: 78 : if (!reg) {
2907 : 0 : return false;
2908 : : }
2909 : :
2910 [ + + ]: 78 : if (nvmf_ns_reservation_all_registrants_type(ns)) {
2911 : 6 : return true;
2912 : : }
2913 : :
2914 : 72 : return (ns->holder == reg);
2915 : : }
2916 : :
2917 : : static int
2918 : 87 : nvmf_ns_reservation_add_registrant(struct spdk_nvmf_ns *ns,
2919 : : struct spdk_nvmf_ctrlr *ctrlr,
2920 : : uint64_t nrkey)
2921 : : {
2922 : : struct spdk_nvmf_registrant *reg;
2923 : :
2924 : 87 : reg = calloc(1, sizeof(*reg));
2925 [ - + ]: 87 : if (!reg) {
2926 : 0 : return -ENOMEM;
2927 : : }
2928 : :
2929 : 87 : reg->rkey = nrkey;
2930 : : /* set hostid for the registrant */
2931 : 87 : spdk_uuid_copy(®->hostid, &ctrlr->hostid);
2932 : 87 : TAILQ_INSERT_TAIL(&ns->registrants, reg, link);
2933 : 87 : ns->gen++;
2934 : :
2935 : 87 : return 0;
2936 : : }
2937 : :
2938 : : static void
2939 : 33 : nvmf_ns_reservation_release_reservation(struct spdk_nvmf_ns *ns)
2940 : : {
2941 : 33 : ns->rtype = 0;
2942 : 33 : ns->crkey = 0;
2943 : 33 : ns->holder = NULL;
2944 : 33 : }
2945 : :
2946 : : /* release the reservation if the last registrant was removed */
2947 : : static void
2948 : 57 : nvmf_ns_reservation_check_release_on_remove_registrant(struct spdk_nvmf_ns *ns,
2949 : : struct spdk_nvmf_registrant *reg)
2950 : : {
2951 : : struct spdk_nvmf_registrant *next_reg;
2952 : :
2953 : : /* no reservation holder */
2954 [ + + ]: 57 : if (!ns->holder) {
2955 [ - + ]: 21 : assert(ns->rtype == 0);
2956 : 21 : return;
2957 : : }
2958 : :
2959 : 36 : next_reg = TAILQ_FIRST(&ns->registrants);
2960 [ + + + + ]: 36 : if (next_reg && nvmf_ns_reservation_all_registrants_type(ns)) {
2961 : : /* the next valid registrant is the new holder now */
2962 : 6 : ns->holder = next_reg;
2963 [ + + ]: 30 : } else if (nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
2964 : : /* release the reservation */
2965 : 21 : nvmf_ns_reservation_release_reservation(ns);
2966 : : }
2967 : : }
2968 : :
2969 : : static void
2970 : 57 : nvmf_ns_reservation_remove_registrant(struct spdk_nvmf_ns *ns,
2971 : : struct spdk_nvmf_registrant *reg)
2972 : : {
2973 [ + + ]: 57 : TAILQ_REMOVE(&ns->registrants, reg, link);
2974 : 57 : nvmf_ns_reservation_check_release_on_remove_registrant(ns, reg);
2975 : 57 : free(reg);
2976 : 57 : ns->gen++;
2977 : 57 : return;
2978 : : }
2979 : :
2980 : : static uint32_t
2981 : 0 : nvmf_ns_reservation_remove_registrants_by_key(struct spdk_nvmf_ns *ns,
2982 : : uint64_t rkey)
2983 : : {
2984 : : struct spdk_nvmf_registrant *reg, *tmp;
2985 : 0 : uint32_t count = 0;
2986 : :
2987 [ # # ]: 0 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
2988 [ # # ]: 0 : if (reg->rkey == rkey) {
2989 : 0 : nvmf_ns_reservation_remove_registrant(ns, reg);
2990 : 0 : count++;
2991 : : }
2992 : : }
2993 : 0 : return count;
2994 : : }
2995 : :
2996 : : static uint32_t
2997 : 3 : nvmf_ns_reservation_remove_all_other_registrants(struct spdk_nvmf_ns *ns,
2998 : : struct spdk_nvmf_registrant *reg)
2999 : : {
3000 : : struct spdk_nvmf_registrant *reg_tmp, *reg_tmp2;
3001 : 3 : uint32_t count = 0;
3002 : :
3003 [ + + ]: 9 : TAILQ_FOREACH_SAFE(reg_tmp, &ns->registrants, link, reg_tmp2) {
3004 [ + + ]: 6 : if (reg_tmp != reg) {
3005 : 3 : nvmf_ns_reservation_remove_registrant(ns, reg_tmp);
3006 : 3 : count++;
3007 : : }
3008 : : }
3009 : 3 : return count;
3010 : : }
3011 : :
3012 : : static uint32_t
3013 : 2679 : nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns)
3014 : : {
3015 : : struct spdk_nvmf_registrant *reg, *reg_tmp;
3016 : 2679 : uint32_t count = 0;
3017 : :
3018 [ + + ]: 2712 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) {
3019 : 33 : nvmf_ns_reservation_remove_registrant(ns, reg);
3020 : 33 : count++;
3021 : : }
3022 : 2679 : return count;
3023 : : }
3024 : :
3025 : : static void
3026 : 36 : nvmf_ns_reservation_acquire_reservation(struct spdk_nvmf_ns *ns, uint64_t rkey,
3027 : : enum spdk_nvme_reservation_type rtype,
3028 : : struct spdk_nvmf_registrant *holder)
3029 : : {
3030 : 36 : ns->rtype = rtype;
3031 : 36 : ns->crkey = rkey;
3032 [ - + ]: 36 : assert(ns->holder == NULL);
3033 : 36 : ns->holder = holder;
3034 : 36 : }
3035 : :
3036 : : static bool
3037 : 135 : nvmf_ns_reservation_register(struct spdk_nvmf_ns *ns,
3038 : : struct spdk_nvmf_ctrlr *ctrlr,
3039 : : struct spdk_nvmf_request *req)
3040 : : {
3041 : 135 : struct spdk_nvme_reservation_register_data key = { 0 };
3042 : 135 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3043 : : uint8_t rrega, iekey, cptpl, rtype;
3044 : : struct spdk_nvmf_registrant *reg;
3045 : 135 : uint8_t status = SPDK_NVME_SC_SUCCESS;
3046 : 135 : bool update_sgroup = false;
3047 : 135 : struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
3048 : 135 : uint32_t num_hostid = 0;
3049 : : int rc;
3050 : :
3051 : 135 : rrega = cmd->cdw10_bits.resv_register.rrega;
3052 : 135 : iekey = cmd->cdw10_bits.resv_register.iekey;
3053 : 135 : cptpl = cmd->cdw10_bits.resv_register.cptpl;
3054 : :
3055 [ + - + - ]: 135 : if (req->iovcnt > 0 && req->length >= sizeof(key)) {
3056 : 135 : struct spdk_iov_xfer ix;
3057 : 135 : spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3058 : 135 : spdk_iov_xfer_to_buf(&ix, &key, sizeof(key));
3059 : : } else {
3060 : 0 : SPDK_ERRLOG("No key provided. Failing request.\n");
3061 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3062 : 0 : goto exit;
3063 : : }
3064 : :
3065 [ - + - + ]: 135 : SPDK_DEBUGLOG(nvmf, "REGISTER: RREGA %u, IEKEY %u, CPTPL %u, "
3066 : : "NRKEY 0x%"PRIx64", NRKEY 0x%"PRIx64"\n",
3067 : : rrega, iekey, cptpl, key.crkey, key.nrkey);
3068 : :
3069 [ + + ]: 135 : if (cptpl == SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON) {
3070 : : /* Ture to OFF state, and need to be updated in the configuration file */
3071 [ + + + - ]: 3 : if (ns->ptpl_activated) {
3072 : 3 : ns->ptpl_activated = 0;
3073 : 3 : update_sgroup = true;
3074 : : }
3075 [ + + ]: 132 : } else if (cptpl == SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS) {
3076 [ + + ]: 12 : if (!nvmf_ns_is_ptpl_capable(ns)) {
3077 : 3 : status = SPDK_NVME_SC_INVALID_FIELD;
3078 : 3 : goto exit;
3079 [ + + + - ]: 9 : } else if (ns->ptpl_activated == 0) {
3080 : 9 : ns->ptpl_activated = 1;
3081 : 9 : update_sgroup = true;
3082 : : }
3083 : : }
3084 : :
3085 : : /* current Host Identifier has registrant or not */
3086 : 132 : reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
3087 : :
3088 [ + + + - ]: 132 : switch (rrega) {
3089 : 108 : case SPDK_NVME_RESERVE_REGISTER_KEY:
3090 [ + + ]: 108 : if (!reg) {
3091 : : /* register new controller */
3092 [ - + ]: 81 : if (key.nrkey == 0) {
3093 : 0 : SPDK_ERRLOG("Can't register zeroed new key\n");
3094 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3095 : 0 : goto exit;
3096 : : }
3097 : 81 : rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey);
3098 [ - + ]: 81 : if (rc < 0) {
3099 : 0 : status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3100 : 0 : goto exit;
3101 : : }
3102 : 81 : update_sgroup = true;
3103 : : } else {
3104 : : /* register with same key is not an error */
3105 [ + + ]: 27 : if (reg->rkey != key.nrkey) {
3106 : 24 : SPDK_ERRLOG("The same host already register a "
3107 : : "key with 0x%"PRIx64"\n",
3108 : : reg->rkey);
3109 : 24 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3110 : 24 : goto exit;
3111 : : }
3112 : : }
3113 : 84 : break;
3114 : 12 : case SPDK_NVME_RESERVE_UNREGISTER_KEY:
3115 [ + - + + : 12 : if (!reg || (!iekey && reg->rkey != key.crkey)) {
- + ]
3116 : 0 : SPDK_ERRLOG("No registrant or current key doesn't match "
3117 : : "with existing registrant key\n");
3118 : 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3119 : 0 : goto exit;
3120 : : }
3121 : :
3122 : 12 : rtype = ns->rtype;
3123 : 12 : num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
3124 : : SPDK_NVMF_MAX_NUM_REGISTRANTS,
3125 : : &ctrlr->hostid);
3126 : :
3127 : 12 : nvmf_ns_reservation_remove_registrant(ns, reg);
3128 : :
3129 [ + + + - : 12 : if (!ns->rtype && num_hostid && (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY ||
+ + - + ]
3130 : : rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY)) {
3131 : 3 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3132 : : hostid_list,
3133 : : num_hostid,
3134 : : SPDK_NVME_RESERVATION_RELEASED);
3135 : : }
3136 : 12 : update_sgroup = true;
3137 : 12 : break;
3138 : 12 : case SPDK_NVME_RESERVE_REPLACE_KEY:
3139 [ - + ]: 12 : if (key.nrkey == 0) {
3140 : 0 : SPDK_ERRLOG("Can't register zeroed new key\n");
3141 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3142 : 0 : goto exit;
3143 : : }
3144 : : /* Registrant exists */
3145 [ + + ]: 12 : if (reg) {
3146 [ + + - + ]: 6 : if (!iekey && reg->rkey != key.crkey) {
3147 : 0 : SPDK_ERRLOG("Current key doesn't match "
3148 : : "existing registrant key\n");
3149 : 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3150 : 0 : goto exit;
3151 : : }
3152 [ - + ]: 6 : if (reg->rkey == key.nrkey) {
3153 : 0 : goto exit;
3154 : : }
3155 : 6 : reg->rkey = key.nrkey;
3156 [ + + ]: 6 : } else if (iekey) { /* No registrant but IEKEY is set */
3157 : : /* new registrant */
3158 : 3 : rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey);
3159 [ - + ]: 3 : if (rc < 0) {
3160 : 0 : status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3161 : 0 : goto exit;
3162 : : }
3163 : : } else { /* No registrant */
3164 : 3 : SPDK_ERRLOG("No registrant\n");
3165 : 3 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3166 : 3 : goto exit;
3167 : :
3168 : : }
3169 : 9 : update_sgroup = true;
3170 : 9 : break;
3171 : 0 : default:
3172 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3173 : 0 : goto exit;
3174 : : }
3175 : :
3176 : 135 : exit:
3177 : 135 : req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3178 : 135 : req->rsp->nvme_cpl.status.sc = status;
3179 : 135 : return update_sgroup;
3180 : : }
3181 : :
3182 : : static bool
3183 : 39 : nvmf_ns_reservation_acquire(struct spdk_nvmf_ns *ns,
3184 : : struct spdk_nvmf_ctrlr *ctrlr,
3185 : : struct spdk_nvmf_request *req)
3186 : : {
3187 : 39 : struct spdk_nvme_reservation_acquire_data key = { 0 };
3188 : 39 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3189 : : uint8_t racqa, iekey, rtype;
3190 : : struct spdk_nvmf_registrant *reg;
3191 : 39 : bool all_regs = false;
3192 : 39 : uint32_t count = 0;
3193 : 39 : bool update_sgroup = true;
3194 : 39 : struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
3195 : 39 : uint32_t num_hostid = 0;
3196 : 39 : struct spdk_uuid new_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
3197 : 39 : uint32_t new_num_hostid = 0;
3198 : 39 : bool reservation_released = false;
3199 : 39 : uint8_t status = SPDK_NVME_SC_SUCCESS;
3200 : :
3201 : 39 : racqa = cmd->cdw10_bits.resv_acquire.racqa;
3202 : 39 : iekey = cmd->cdw10_bits.resv_acquire.iekey;
3203 : 39 : rtype = cmd->cdw10_bits.resv_acquire.rtype;
3204 : :
3205 [ + - + - ]: 39 : if (req->iovcnt > 0 && req->length >= sizeof(key)) {
3206 : 39 : struct spdk_iov_xfer ix;
3207 : 39 : spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3208 : 39 : spdk_iov_xfer_to_buf(&ix, &key, sizeof(key));
3209 : : } else {
3210 : 0 : SPDK_ERRLOG("No key provided. Failing request.\n");
3211 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3212 : 0 : goto exit;
3213 : : }
3214 : :
3215 [ - + - + ]: 39 : SPDK_DEBUGLOG(nvmf, "ACQUIRE: RACQA %u, IEKEY %u, RTYPE %u, "
3216 : : "NRKEY 0x%"PRIx64", PRKEY 0x%"PRIx64"\n",
3217 : : racqa, iekey, rtype, key.crkey, key.prkey);
3218 : :
3219 [ + - - + ]: 39 : if (iekey || rtype > SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) {
3220 : 0 : SPDK_ERRLOG("Ignore existing key field set to 1\n");
3221 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3222 : 0 : update_sgroup = false;
3223 : 0 : goto exit;
3224 : : }
3225 : :
3226 : 39 : reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
3227 : : /* must be registrant and CRKEY must match */
3228 [ + - - + ]: 39 : if (!reg || reg->rkey != key.crkey) {
3229 : 0 : SPDK_ERRLOG("No registrant or current key doesn't match "
3230 : : "with existing registrant key\n");
3231 : 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3232 : 0 : update_sgroup = false;
3233 : 0 : goto exit;
3234 : : }
3235 : :
3236 : 39 : all_regs = nvmf_ns_reservation_all_registrants_type(ns);
3237 : :
3238 [ + + - ]: 39 : switch (racqa) {
3239 : 30 : case SPDK_NVME_RESERVE_ACQUIRE:
3240 : : /* it's not an error for the holder to acquire same reservation type again */
3241 [ - + - - ]: 30 : if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && ns->rtype == rtype) {
3242 : : /* do nothing */
3243 : 0 : update_sgroup = false;
3244 [ + - ]: 30 : } else if (ns->holder == NULL) {
3245 : : /* first time to acquire the reservation */
3246 : 30 : nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
3247 : : } else {
3248 : 0 : SPDK_ERRLOG("Invalid rtype or current registrant is not holder\n");
3249 : 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3250 : 0 : update_sgroup = false;
3251 : 0 : goto exit;
3252 : : }
3253 : 30 : break;
3254 : 9 : case SPDK_NVME_RESERVE_PREEMPT:
3255 : : /* no reservation holder */
3256 [ - + ]: 9 : if (!ns->holder) {
3257 : : /* unregister with PRKEY */
3258 : 0 : nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
3259 : 0 : break;
3260 : : }
3261 : 9 : num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
3262 : : SPDK_NVMF_MAX_NUM_REGISTRANTS,
3263 : : &ctrlr->hostid);
3264 : :
3265 : : /* only 1 reservation holder and reservation key is valid */
3266 [ + + ]: 9 : if (!all_regs) {
3267 : : /* preempt itself */
3268 [ - + ]: 6 : if (nvmf_ns_reservation_registrant_is_holder(ns, reg) &&
3269 [ # # ]: 0 : ns->crkey == key.prkey) {
3270 : 0 : ns->rtype = rtype;
3271 : 0 : reservation_released = true;
3272 : 0 : break;
3273 : : }
3274 : :
3275 [ + - ]: 6 : if (ns->crkey == key.prkey) {
3276 : 6 : nvmf_ns_reservation_remove_registrant(ns, ns->holder);
3277 : 6 : nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg);
3278 : 6 : reservation_released = true;
3279 [ # # ]: 0 : } else if (key.prkey != 0) {
3280 : 0 : nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
3281 : : } else {
3282 : : /* PRKEY is zero */
3283 : 0 : SPDK_ERRLOG("Current PRKEY is zero\n");
3284 : 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3285 : 0 : update_sgroup = false;
3286 : 0 : goto exit;
3287 : : }
3288 : : } else {
3289 : : /* release all other registrants except for the current one */
3290 [ + - ]: 3 : if (key.prkey == 0) {
3291 : 3 : nvmf_ns_reservation_remove_all_other_registrants(ns, reg);
3292 [ - + ]: 3 : assert(ns->holder == reg);
3293 : : } else {
3294 : 0 : count = nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey);
3295 [ # # ]: 0 : if (count == 0) {
3296 : 0 : SPDK_ERRLOG("PRKEY doesn't match any registrant\n");
3297 : 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3298 : 0 : update_sgroup = false;
3299 : 0 : goto exit;
3300 : : }
3301 : : }
3302 : : }
3303 : 9 : break;
3304 : 0 : default:
3305 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3306 : 0 : update_sgroup = false;
3307 : 0 : break;
3308 : : }
3309 : :
3310 : 39 : exit:
3311 [ + - + + ]: 39 : if (update_sgroup && racqa == SPDK_NVME_RESERVE_PREEMPT) {
3312 : 9 : new_num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, new_hostid_list,
3313 : : SPDK_NVMF_MAX_NUM_REGISTRANTS,
3314 : : &ctrlr->hostid);
3315 : : /* Preempt notification occurs on the unregistered controllers
3316 : : * other than the controller who issued the command.
3317 : : */
3318 : 9 : num_hostid = nvmf_ns_reservation_get_unregistered_hostid(hostid_list,
3319 : : num_hostid,
3320 : : new_hostid_list,
3321 : : new_num_hostid);
3322 [ + - ]: 9 : if (num_hostid) {
3323 : 9 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3324 : : hostid_list,
3325 : : num_hostid,
3326 : : SPDK_NVME_REGISTRATION_PREEMPTED);
3327 : :
3328 : : }
3329 : : /* Reservation released notification occurs on the
3330 : : * controllers which are the remaining registrants other than
3331 : : * the controller who issued the command.
3332 : : */
3333 [ + + + - ]: 9 : if (reservation_released && new_num_hostid) {
3334 : 6 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3335 : : new_hostid_list,
3336 : : new_num_hostid,
3337 : : SPDK_NVME_RESERVATION_RELEASED);
3338 : :
3339 : : }
3340 : : }
3341 : 39 : req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3342 : 39 : req->rsp->nvme_cpl.status.sc = status;
3343 : 39 : return update_sgroup;
3344 : : }
3345 : :
3346 : : static bool
3347 : 18 : nvmf_ns_reservation_release(struct spdk_nvmf_ns *ns,
3348 : : struct spdk_nvmf_ctrlr *ctrlr,
3349 : : struct spdk_nvmf_request *req)
3350 : : {
3351 : 18 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3352 : : uint8_t rrela, iekey, rtype;
3353 : : struct spdk_nvmf_registrant *reg;
3354 : 18 : uint64_t crkey = 0;
3355 : 18 : uint8_t status = SPDK_NVME_SC_SUCCESS;
3356 : 18 : bool update_sgroup = true;
3357 : 18 : struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS];
3358 : 18 : uint32_t num_hostid = 0;
3359 : :
3360 : 18 : rrela = cmd->cdw10_bits.resv_release.rrela;
3361 : 18 : iekey = cmd->cdw10_bits.resv_release.iekey;
3362 : 18 : rtype = cmd->cdw10_bits.resv_release.rtype;
3363 : :
3364 [ + - + - ]: 18 : if (req->iovcnt > 0 && req->length >= sizeof(crkey)) {
3365 : 18 : struct spdk_iov_xfer ix;
3366 : 18 : spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3367 : 18 : spdk_iov_xfer_to_buf(&ix, &crkey, sizeof(crkey));
3368 : : } else {
3369 : 0 : SPDK_ERRLOG("No key provided. Failing request.\n");
3370 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3371 : 0 : goto exit;
3372 : : }
3373 : :
3374 [ - + - + ]: 18 : SPDK_DEBUGLOG(nvmf, "RELEASE: RRELA %u, IEKEY %u, RTYPE %u, "
3375 : : "CRKEY 0x%"PRIx64"\n", rrela, iekey, rtype, crkey);
3376 : :
3377 [ - + ]: 18 : if (iekey) {
3378 : 0 : SPDK_ERRLOG("Ignore existing key field set to 1\n");
3379 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3380 : 0 : update_sgroup = false;
3381 : 0 : goto exit;
3382 : : }
3383 : :
3384 : 18 : reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid);
3385 [ + - - + ]: 18 : if (!reg || reg->rkey != crkey) {
3386 : 0 : SPDK_ERRLOG("No registrant or current key doesn't match "
3387 : : "with existing registrant key\n");
3388 : 0 : status = SPDK_NVME_SC_RESERVATION_CONFLICT;
3389 : 0 : update_sgroup = false;
3390 : 0 : goto exit;
3391 : : }
3392 : :
3393 : 18 : num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list,
3394 : : SPDK_NVMF_MAX_NUM_REGISTRANTS,
3395 : : &ctrlr->hostid);
3396 : :
3397 [ + + - ]: 18 : switch (rrela) {
3398 : 12 : case SPDK_NVME_RESERVE_RELEASE:
3399 [ - + ]: 12 : if (!ns->holder) {
3400 [ # # # # ]: 0 : SPDK_DEBUGLOG(nvmf, "RELEASE: no holder\n");
3401 : 0 : update_sgroup = false;
3402 : 0 : goto exit;
3403 : : }
3404 [ - + ]: 12 : if (ns->rtype != rtype) {
3405 : 0 : SPDK_ERRLOG("Type doesn't match\n");
3406 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3407 : 0 : update_sgroup = false;
3408 : 0 : goto exit;
3409 : : }
3410 [ - + ]: 12 : if (!nvmf_ns_reservation_registrant_is_holder(ns, reg)) {
3411 : : /* not the reservation holder, this isn't an error */
3412 : 0 : update_sgroup = false;
3413 : 0 : goto exit;
3414 : : }
3415 : :
3416 : 12 : rtype = ns->rtype;
3417 : 12 : nvmf_ns_reservation_release_reservation(ns);
3418 : :
3419 [ + + + + : 12 : if (num_hostid && rtype != SPDK_NVME_RESERVE_WRITE_EXCLUSIVE &&
+ - ]
3420 : : rtype != SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) {
3421 : 6 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3422 : : hostid_list,
3423 : : num_hostid,
3424 : : SPDK_NVME_RESERVATION_RELEASED);
3425 : : }
3426 : 12 : break;
3427 : 6 : case SPDK_NVME_RESERVE_CLEAR:
3428 : 6 : nvmf_ns_reservation_clear_all_registrants(ns);
3429 [ + - ]: 6 : if (num_hostid) {
3430 : 6 : nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns,
3431 : : hostid_list,
3432 : : num_hostid,
3433 : : SPDK_NVME_RESERVATION_PREEMPTED);
3434 : : }
3435 : 6 : break;
3436 : 0 : default:
3437 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3438 : 0 : update_sgroup = false;
3439 : 0 : goto exit;
3440 : : }
3441 : :
3442 : 18 : exit:
3443 : 18 : req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3444 : 18 : req->rsp->nvme_cpl.status.sc = status;
3445 : 18 : return update_sgroup;
3446 : : }
3447 : :
3448 : : static void
3449 : 9 : nvmf_ns_reservation_report(struct spdk_nvmf_ns *ns,
3450 : : struct spdk_nvmf_ctrlr *ctrlr,
3451 : : struct spdk_nvmf_request *req)
3452 : : {
3453 : 9 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3454 : : struct spdk_nvmf_registrant *reg, *tmp;
3455 : 9 : struct spdk_nvme_reservation_status_extended_data status_data = { 0 };
3456 : 9 : struct spdk_iov_xfer ix;
3457 : : uint32_t transfer_len;
3458 : 9 : uint32_t regctl = 0;
3459 : 9 : uint8_t status = SPDK_NVME_SC_SUCCESS;
3460 : :
3461 [ - + ]: 9 : if (req->iovcnt == 0) {
3462 : 0 : SPDK_ERRLOG("No data transfer specified for request. "
3463 : : " Unable to transfer back response.\n");
3464 : 0 : status = SPDK_NVME_SC_INVALID_FIELD;
3465 : 0 : goto exit;
3466 : : }
3467 : :
3468 [ + + ]: 9 : if (!cmd->cdw11_bits.resv_report.eds) {
3469 : 3 : SPDK_ERRLOG("NVMeoF uses extended controller data structure, "
3470 : : "please set EDS bit in cdw11 and try again\n");
3471 : 3 : status = SPDK_NVME_SC_HOSTID_INCONSISTENT_FORMAT;
3472 : 3 : goto exit;
3473 : : }
3474 : :
3475 : : /* Number of Dwords of the Reservation Status data structure to transfer */
3476 : 6 : transfer_len = (cmd->cdw10 + 1) * sizeof(uint32_t);
3477 : :
3478 [ + + ]: 6 : if (transfer_len < sizeof(struct spdk_nvme_reservation_status_extended_data)) {
3479 : 3 : status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3480 : 3 : goto exit;
3481 : : }
3482 : :
3483 : 3 : spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
3484 : :
3485 : 3 : status_data.data.gen = ns->gen;
3486 : 3 : status_data.data.rtype = ns->rtype;
3487 [ - + ]: 3 : status_data.data.ptpls = ns->ptpl_activated;
3488 : :
3489 [ + + ]: 9 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
3490 : 6 : regctl++;
3491 : : }
3492 : :
3493 : : /*
3494 : : * We report the number of registrants as per the spec here, even if
3495 : : * the iov isn't big enough to contain them all. In that case, the
3496 : : * spdk_iov_xfer_from_buf() won't actually copy any of the remaining
3497 : : * data; as it keeps track of the iov cursor itself, it's simplest to
3498 : : * just walk the entire list anyway.
3499 : : */
3500 : 3 : status_data.data.regctl = regctl;
3501 : :
3502 : 3 : spdk_iov_xfer_from_buf(&ix, &status_data, sizeof(status_data));
3503 : :
3504 [ + + ]: 9 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
3505 : 6 : struct spdk_nvme_registered_ctrlr_extended_data ctrlr_data = { 0 };
3506 : :
3507 : : /* Set to 0xffffh for dynamic controller */
3508 : 6 : ctrlr_data.cntlid = 0xffff;
3509 : 6 : ctrlr_data.rcsts.status = (ns->holder == reg) ? true : false;
3510 : 6 : ctrlr_data.rkey = reg->rkey;
3511 : 6 : spdk_uuid_copy((struct spdk_uuid *)ctrlr_data.hostid, ®->hostid);
3512 : :
3513 : 6 : spdk_iov_xfer_from_buf(&ix, &ctrlr_data, sizeof(ctrlr_data));
3514 : : }
3515 : :
3516 : 9 : exit:
3517 : 9 : req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3518 : 9 : req->rsp->nvme_cpl.status.sc = status;
3519 : 9 : return;
3520 : : }
3521 : :
3522 : : static void
3523 : 0 : nvmf_ns_reservation_complete(void *ctx)
3524 : : {
3525 : 0 : struct spdk_nvmf_request *req = ctx;
3526 : :
3527 : 0 : spdk_nvmf_request_complete(req);
3528 : 0 : }
3529 : :
3530 : : static void
3531 : 0 : _nvmf_ns_reservation_update_done(struct spdk_nvmf_subsystem *subsystem,
3532 : : void *cb_arg, int status)
3533 : : {
3534 : 0 : struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)cb_arg;
3535 : 0 : struct spdk_nvmf_poll_group *group = req->qpair->group;
3536 : :
3537 : 0 : spdk_thread_send_msg(group->thread, nvmf_ns_reservation_complete, req);
3538 : 0 : }
3539 : :
3540 : : void
3541 : 0 : nvmf_ns_reservation_request(void *ctx)
3542 : : {
3543 : 0 : struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)ctx;
3544 : 0 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
3545 : 0 : struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
3546 : : uint32_t nsid;
3547 : : struct spdk_nvmf_ns *ns;
3548 : 0 : bool update_sgroup = false;
3549 : 0 : int status = 0;
3550 : :
3551 : 0 : nsid = cmd->nsid;
3552 : 0 : ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
3553 [ # # ]: 0 : assert(ns != NULL);
3554 : :
3555 [ # # # # : 0 : switch (cmd->opc) {
# ]
3556 : 0 : case SPDK_NVME_OPC_RESERVATION_REGISTER:
3557 : 0 : update_sgroup = nvmf_ns_reservation_register(ns, ctrlr, req);
3558 : 0 : break;
3559 : 0 : case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
3560 : 0 : update_sgroup = nvmf_ns_reservation_acquire(ns, ctrlr, req);
3561 : 0 : break;
3562 : 0 : case SPDK_NVME_OPC_RESERVATION_RELEASE:
3563 : 0 : update_sgroup = nvmf_ns_reservation_release(ns, ctrlr, req);
3564 : 0 : break;
3565 : 0 : case SPDK_NVME_OPC_RESERVATION_REPORT:
3566 : 0 : nvmf_ns_reservation_report(ns, ctrlr, req);
3567 : 0 : break;
3568 : 0 : default:
3569 : 0 : break;
3570 : : }
3571 : :
3572 : : /* update reservation information to subsystem's poll group */
3573 [ # # ]: 0 : if (update_sgroup) {
3574 [ # # # # : 0 : if (ns->ptpl_activated || cmd->opc == SPDK_NVME_OPC_RESERVATION_REGISTER) {
# # ]
3575 [ # # ]: 0 : if (nvmf_ns_update_reservation_info(ns) != 0) {
3576 : 0 : req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3577 : : }
3578 : : }
3579 : 0 : status = nvmf_subsystem_update_ns(ctrlr->subsys, _nvmf_ns_reservation_update_done, req);
3580 [ # # ]: 0 : if (status == 0) {
3581 : 0 : return;
3582 : : }
3583 : : }
3584 : :
3585 : 0 : _nvmf_ns_reservation_update_done(ctrlr->subsys, req, status);
3586 : : }
3587 : :
3588 : : static bool
3589 : 3966 : nvmf_ns_is_ptpl_capable_json(const struct spdk_nvmf_ns *ns)
3590 : : {
3591 : 3966 : return ns->ptpl_file != NULL;
3592 : : }
3593 : :
3594 : : static struct spdk_nvmf_ns_reservation_ops g_reservation_ops = {
3595 : : .is_ptpl_capable = nvmf_ns_is_ptpl_capable_json,
3596 : : .update = nvmf_ns_reservation_update_json,
3597 : : .load = nvmf_ns_reservation_load_json,
3598 : : };
3599 : :
3600 : : bool
3601 : 3978 : nvmf_ns_is_ptpl_capable(const struct spdk_nvmf_ns *ns)
3602 : : {
3603 : 3978 : return g_reservation_ops.is_ptpl_capable(ns);
3604 : : }
3605 : :
3606 : : static int
3607 : 21 : nvmf_ns_reservation_update(const struct spdk_nvmf_ns *ns,
3608 : : const struct spdk_nvmf_reservation_info *info)
3609 : : {
3610 : 21 : return g_reservation_ops.update(ns, info);
3611 : : }
3612 : :
3613 : : static int
3614 : 18 : nvmf_ns_reservation_load(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
3615 : : {
3616 : 18 : return g_reservation_ops.load(ns, info);
3617 : : }
3618 : :
3619 : : void
3620 : 3 : spdk_nvmf_set_custom_ns_reservation_ops(const struct spdk_nvmf_ns_reservation_ops *ops)
3621 : : {
3622 : 3 : g_reservation_ops = *ops;
3623 : 3 : }
3624 : :
3625 : : int
3626 : 388 : spdk_nvmf_subsystem_set_ana_reporting(struct spdk_nvmf_subsystem *subsystem,
3627 : : bool ana_reporting)
3628 : : {
3629 [ - + ]: 388 : if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) {
3630 : 0 : return -EAGAIN;
3631 : : }
3632 : :
3633 : 388 : subsystem->flags.ana_reporting = ana_reporting;
3634 : :
3635 : 388 : return 0;
3636 : : }
3637 : :
3638 : : bool
3639 : 776 : spdk_nvmf_subsystem_get_ana_reporting(struct spdk_nvmf_subsystem *subsystem)
3640 : : {
3641 : 776 : return subsystem->flags.ana_reporting;
3642 : : }
3643 : :
3644 : : struct subsystem_listener_update_ctx {
3645 : : struct spdk_nvmf_subsystem_listener *listener;
3646 : :
3647 : : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn;
3648 : : void *cb_arg;
3649 : : };
3650 : :
3651 : : static void
3652 : 122 : subsystem_listener_update_done(struct spdk_io_channel_iter *i, int status)
3653 : : {
3654 : 122 : struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
3655 : :
3656 [ + - ]: 122 : if (ctx->cb_fn) {
3657 : 122 : ctx->cb_fn(ctx->cb_arg, status);
3658 : : }
3659 : 122 : free(ctx);
3660 : 122 : }
3661 : :
3662 : : static void
3663 : 284 : subsystem_listener_update_on_pg(struct spdk_io_channel_iter *i)
3664 : : {
3665 : 284 : struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
3666 : : struct spdk_nvmf_subsystem_listener *listener;
3667 : : struct spdk_nvmf_poll_group *group;
3668 : : struct spdk_nvmf_ctrlr *ctrlr;
3669 : :
3670 : 284 : listener = ctx->listener;
3671 : 284 : group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i));
3672 : :
3673 [ + + ]: 848 : TAILQ_FOREACH(ctrlr, &listener->subsystem->ctrlrs, link) {
3674 [ + + ]: 564 : if (ctrlr->thread != spdk_get_thread()) {
3675 : 322 : continue;
3676 : : }
3677 : :
3678 [ + - + - : 242 : if (ctrlr->admin_qpair && ctrlr->admin_qpair->group == group && ctrlr->listener == listener) {
+ + ]
3679 : 120 : nvmf_ctrlr_async_event_ana_change_notice(ctrlr);
3680 : : }
3681 : : }
3682 : :
3683 : 284 : spdk_for_each_channel_continue(i, 0);
3684 : 284 : }
3685 : :
3686 : : void
3687 : 122 : spdk_nvmf_subsystem_set_ana_state(struct spdk_nvmf_subsystem *subsystem,
3688 : : const struct spdk_nvme_transport_id *trid,
3689 : : enum spdk_nvme_ana_state ana_state, uint32_t anagrpid,
3690 : : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, void *cb_arg)
3691 : : {
3692 : : struct spdk_nvmf_subsystem_listener *listener;
3693 : : struct subsystem_listener_update_ctx *ctx;
3694 : : uint32_t i;
3695 : :
3696 [ - + ]: 122 : assert(cb_fn != NULL);
3697 [ + - - + ]: 122 : assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE ||
3698 : : subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED);
3699 : :
3700 [ - + ]: 122 : if (!subsystem->flags.ana_reporting) {
3701 : 0 : SPDK_ERRLOG("ANA reporting is disabled\n");
3702 : 0 : cb_fn(cb_arg, -EINVAL);
3703 : 0 : return;
3704 : : }
3705 : :
3706 : : /* ANA Change state is not used, ANA Persistent Loss state
3707 : : * is not supported yet.
3708 : : */
3709 [ + + + + : 122 : if (!(ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE ||
- + ]
3710 : : ana_state == SPDK_NVME_ANA_NON_OPTIMIZED_STATE ||
3711 : : ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE)) {
3712 : 0 : SPDK_ERRLOG("ANA state %d is not supported\n", ana_state);
3713 : 0 : cb_fn(cb_arg, -ENOTSUP);
3714 : 0 : return;
3715 : : }
3716 : :
3717 [ - + ]: 122 : if (anagrpid > subsystem->max_nsid) {
3718 : 0 : SPDK_ERRLOG("ANA group ID %" PRIu32 " is more than maximum\n", anagrpid);
3719 : 0 : cb_fn(cb_arg, -EINVAL);
3720 : 0 : return;
3721 : : }
3722 : :
3723 : 122 : listener = nvmf_subsystem_find_listener(subsystem, trid);
3724 [ - + ]: 122 : if (!listener) {
3725 : 0 : SPDK_ERRLOG("Unable to find listener.\n");
3726 : 0 : cb_fn(cb_arg, -EINVAL);
3727 : 0 : return;
3728 : : }
3729 : :
3730 [ - + - - ]: 122 : if (anagrpid != 0 && listener->ana_state[anagrpid - 1] == ana_state) {
3731 : 0 : cb_fn(cb_arg, 0);
3732 : 0 : return;
3733 : : }
3734 : :
3735 : 122 : ctx = calloc(1, sizeof(*ctx));
3736 [ - + ]: 122 : if (!ctx) {
3737 : 0 : SPDK_ERRLOG("Unable to allocate context\n");
3738 : 0 : cb_fn(cb_arg, -ENOMEM);
3739 : 0 : return;
3740 : : }
3741 : :
3742 [ + + ]: 966 : for (i = 1; i <= subsystem->max_nsid; i++) {
3743 [ - + - - ]: 844 : if (anagrpid == 0 || i == anagrpid) {
3744 : 844 : listener->ana_state[i - 1] = ana_state;
3745 : : }
3746 : : }
3747 : 122 : listener->ana_state_change_count++;
3748 : :
3749 : 122 : ctx->listener = listener;
3750 : 122 : ctx->cb_fn = cb_fn;
3751 : 122 : ctx->cb_arg = cb_arg;
3752 : :
3753 : 122 : spdk_for_each_channel(subsystem->tgt,
3754 : : subsystem_listener_update_on_pg,
3755 : : ctx,
3756 : : subsystem_listener_update_done);
3757 : : }
3758 : :
3759 : : int
3760 : 0 : spdk_nvmf_subsystem_get_ana_state(struct spdk_nvmf_subsystem *subsystem,
3761 : : const struct spdk_nvme_transport_id *trid,
3762 : : uint32_t anagrpid,
3763 : : enum spdk_nvme_ana_state *ana_state)
3764 : : {
3765 [ # # ]: 0 : assert(ana_state != NULL);
3766 : :
3767 : : struct spdk_nvmf_subsystem_listener *listener;
3768 : :
3769 [ # # ]: 0 : if (!subsystem->flags.ana_reporting) {
3770 : 0 : SPDK_ERRLOG("ANA reporting is disabled\n");
3771 : 0 : return -EINVAL;
3772 : : }
3773 : :
3774 [ # # # # ]: 0 : if (anagrpid <= 0 || anagrpid > subsystem->max_nsid) {
3775 : 0 : SPDK_ERRLOG("ANA group ID %" PRIu32 " is invalid\n", anagrpid);
3776 : 0 : return -EINVAL;
3777 : : }
3778 : :
3779 : 0 : listener = nvmf_subsystem_find_listener(subsystem, trid);
3780 [ # # ]: 0 : if (!listener) {
3781 : 0 : SPDK_ERRLOG("Unable to find listener.\n");
3782 : 0 : return -EINVAL;
3783 : : }
3784 : :
3785 : 0 : *ana_state = listener->ana_state[anagrpid - 1];
3786 : 0 : return 0;
3787 : : }
3788 : :
3789 : : bool
3790 : 214337 : spdk_nvmf_subsystem_is_discovery(struct spdk_nvmf_subsystem *subsystem)
3791 : : {
3792 [ + + ]: 426454 : return subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT ||
3793 [ - + ]: 212117 : subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY;
3794 : : }
3795 : :
3796 : : bool
3797 : 21 : nvmf_nqn_is_discovery(const char *nqn)
3798 : : {
3799 [ - + ]: 21 : return strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN) == 0;
3800 : : }
|