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