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