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