Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2018 Intel Corporation.
3 : : * All rights reserved.
4 : : * Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : #include "bdev_raid.h"
8 : : #include "spdk/env.h"
9 : : #include "spdk/thread.h"
10 : : #include "spdk/log.h"
11 : : #include "spdk/string.h"
12 : : #include "spdk/util.h"
13 : : #include "spdk/json.h"
14 : : #include "spdk/likely.h"
15 : :
16 : : #define RAID_OFFSET_BLOCKS_INVALID UINT64_MAX
17 : : #define RAID_BDEV_PROCESS_MAX_QD 16
18 : :
19 : : #define RAID_BDEV_PROCESS_WINDOW_SIZE_KB_DEFAULT 1024
20 : :
21 : : static bool g_shutdown_started = false;
22 : :
23 : : /* List of all raid bdevs */
24 : : struct raid_all_tailq g_raid_bdev_list = TAILQ_HEAD_INITIALIZER(g_raid_bdev_list);
25 : :
26 : : static TAILQ_HEAD(, raid_bdev_module) g_raid_modules = TAILQ_HEAD_INITIALIZER(g_raid_modules);
27 : :
28 : : /*
29 : : * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It
30 : : * contains the relationship of raid bdev io channel with base bdev io channels.
31 : : */
32 : : struct raid_bdev_io_channel {
33 : : /* Array of IO channels of base bdevs */
34 : : struct spdk_io_channel **base_channel;
35 : :
36 : : /* Private raid module IO channel */
37 : : struct spdk_io_channel *module_channel;
38 : :
39 : : /* Background process data */
40 : : struct {
41 : : uint64_t offset;
42 : : struct spdk_io_channel *target_ch;
43 : : struct raid_bdev_io_channel *ch_processed;
44 : : } process;
45 : : };
46 : :
47 : : enum raid_bdev_process_state {
48 : : RAID_PROCESS_STATE_INIT,
49 : : RAID_PROCESS_STATE_RUNNING,
50 : : RAID_PROCESS_STATE_STOPPING,
51 : : RAID_PROCESS_STATE_STOPPED,
52 : : };
53 : :
54 : : struct raid_bdev_process {
55 : : struct raid_bdev *raid_bdev;
56 : : enum raid_process_type type;
57 : : enum raid_bdev_process_state state;
58 : : struct spdk_thread *thread;
59 : : struct raid_bdev_io_channel *raid_ch;
60 : : TAILQ_HEAD(, raid_bdev_process_request) requests;
61 : : uint64_t max_window_size;
62 : : uint64_t window_size;
63 : : uint64_t window_remaining;
64 : : int window_status;
65 : : uint64_t window_offset;
66 : : bool window_range_locked;
67 : : struct raid_base_bdev_info *target;
68 : : int status;
69 : : TAILQ_HEAD(, raid_process_finish_action) finish_actions;
70 : : };
71 : :
72 : : struct raid_process_finish_action {
73 : : spdk_msg_fn cb;
74 : : void *cb_ctx;
75 : : TAILQ_ENTRY(raid_process_finish_action) link;
76 : : };
77 : :
78 : : static struct spdk_raid_bdev_opts g_opts = {
79 : : .process_window_size_kb = RAID_BDEV_PROCESS_WINDOW_SIZE_KB_DEFAULT,
80 : : };
81 : :
82 : : void
83 : 252 : raid_bdev_get_opts(struct spdk_raid_bdev_opts *opts)
84 : : {
85 : 252 : *opts = g_opts;
86 : 252 : }
87 : :
88 : : int
89 : 252 : raid_bdev_set_opts(const struct spdk_raid_bdev_opts *opts)
90 : : {
91 [ - + ]: 252 : if (opts->process_window_size_kb == 0) {
92 : 0 : return -EINVAL;
93 : : }
94 : :
95 : 252 : g_opts = *opts;
96 : :
97 : 252 : return 0;
98 : : }
99 : :
100 : : static struct raid_bdev_module *
101 : 8612 : raid_bdev_module_find(enum raid_level level)
102 : : {
103 : : struct raid_bdev_module *raid_module;
104 : :
105 [ + + ]: 18273 : TAILQ_FOREACH(raid_module, &g_raid_modules, link) {
106 [ + + ]: 10787 : if (raid_module->level == level) {
107 : 1126 : return raid_module;
108 : : }
109 : : }
110 : :
111 : 7486 : return NULL;
112 : : }
113 : :
114 : : void
115 : 7482 : raid_bdev_module_list_add(struct raid_bdev_module *raid_module)
116 : : {
117 [ - + ]: 7482 : if (raid_bdev_module_find(raid_module->level) != NULL) {
118 : 0 : SPDK_ERRLOG("module for raid level '%s' already registered.\n",
119 : : raid_bdev_level_to_str(raid_module->level));
120 : 0 : assert(false);
121 : : } else {
122 : 7482 : TAILQ_INSERT_TAIL(&g_raid_modules, raid_module, link);
123 : : }
124 : 7482 : }
125 : :
126 : : struct spdk_io_channel *
127 : 24557640 : raid_bdev_channel_get_base_channel(struct raid_bdev_io_channel *raid_ch, uint8_t idx)
128 : : {
129 : 24557640 : return raid_ch->base_channel[idx];
130 : : }
131 : :
132 : : void *
133 : 3727384 : raid_bdev_channel_get_module_ctx(struct raid_bdev_io_channel *raid_ch)
134 : : {
135 [ - + ]: 3727384 : assert(raid_ch->module_channel != NULL);
136 : :
137 : 3727384 : return spdk_io_channel_get_ctx(raid_ch->module_channel);
138 : : }
139 : :
140 : : struct raid_base_bdev_info *
141 : 15 : raid_bdev_channel_get_base_info(struct raid_bdev_io_channel *raid_ch, struct spdk_bdev *base_bdev)
142 : : {
143 : 15 : struct spdk_io_channel *ch = spdk_io_channel_from_ctx(raid_ch);
144 : 15 : struct raid_bdev *raid_bdev = spdk_io_channel_get_io_device(ch);
145 : : uint8_t i;
146 : :
147 [ + - ]: 15 : for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
148 : 15 : struct raid_base_bdev_info *base_info = &raid_bdev->base_bdev_info[i];
149 : :
150 [ + + + - : 30 : if (base_info->is_configured &&
+ - ]
151 : 15 : spdk_bdev_desc_get_bdev(base_info->desc) == base_bdev) {
152 : 15 : return base_info;
153 : : }
154 : : }
155 : :
156 : 0 : return NULL;
157 : : }
158 : :
159 : : /* Function declarations */
160 : : static void raid_bdev_examine(struct spdk_bdev *bdev);
161 : : static int raid_bdev_init(void);
162 : : static void raid_bdev_deconfigure(struct raid_bdev *raid_bdev,
163 : : raid_bdev_destruct_cb cb_fn, void *cb_arg);
164 : :
165 : : static void
166 : 1494 : raid_bdev_ch_process_cleanup(struct raid_bdev_io_channel *raid_ch)
167 : : {
168 : 1494 : raid_ch->process.offset = RAID_OFFSET_BLOCKS_INVALID;
169 : :
170 [ + + ]: 1494 : if (raid_ch->process.target_ch != NULL) {
171 : 139 : spdk_put_io_channel(raid_ch->process.target_ch);
172 : 139 : raid_ch->process.target_ch = NULL;
173 : : }
174 : :
175 [ + + ]: 1494 : if (raid_ch->process.ch_processed != NULL) {
176 : 212 : free(raid_ch->process.ch_processed->base_channel);
177 : 212 : free(raid_ch->process.ch_processed);
178 : 212 : raid_ch->process.ch_processed = NULL;
179 : : }
180 : 1494 : }
181 : :
182 : : static int
183 : 212 : raid_bdev_ch_process_setup(struct raid_bdev_io_channel *raid_ch, struct raid_bdev_process *process)
184 : : {
185 : 212 : struct raid_bdev *raid_bdev = process->raid_bdev;
186 : : struct raid_bdev_io_channel *raid_ch_processed;
187 : : struct raid_base_bdev_info *base_info;
188 : :
189 : 212 : raid_ch->process.offset = process->window_offset;
190 : :
191 : : /* In the future we may have other types of processes which don't use a target bdev,
192 : : * like data scrubbing or strip size migration. Until then, expect that there always is
193 : : * a process target. */
194 [ - + ]: 212 : assert(process->target != NULL);
195 : :
196 : 212 : raid_ch->process.target_ch = spdk_bdev_get_io_channel(process->target->desc);
197 [ - + ]: 212 : if (raid_ch->process.target_ch == NULL) {
198 : 0 : goto err;
199 : : }
200 : :
201 : 212 : raid_ch_processed = calloc(1, sizeof(*raid_ch_processed));
202 [ - + ]: 212 : if (raid_ch_processed == NULL) {
203 : 0 : goto err;
204 : : }
205 : 212 : raid_ch->process.ch_processed = raid_ch_processed;
206 : :
207 : 212 : raid_ch_processed->base_channel = calloc(raid_bdev->num_base_bdevs,
208 : : sizeof(*raid_ch_processed->base_channel));
209 [ - + ]: 212 : if (raid_ch_processed->base_channel == NULL) {
210 : 0 : goto err;
211 : : }
212 : :
213 [ + + ]: 1040 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
214 : 828 : uint8_t slot = raid_bdev_base_bdev_slot(base_info);
215 : :
216 [ + + ]: 828 : if (base_info != process->target) {
217 : 616 : raid_ch_processed->base_channel[slot] = raid_ch->base_channel[slot];
218 : : } else {
219 : 212 : raid_ch_processed->base_channel[slot] = raid_ch->process.target_ch;
220 : : }
221 : : }
222 : :
223 : 212 : raid_ch_processed->module_channel = raid_ch->module_channel;
224 : 212 : raid_ch_processed->process.offset = RAID_OFFSET_BLOCKS_INVALID;
225 : :
226 : 212 : return 0;
227 : 0 : err:
228 : 0 : raid_bdev_ch_process_cleanup(raid_ch);
229 : 0 : return -ENOMEM;
230 : : }
231 : :
232 : : /*
233 : : * brief:
234 : : * raid_bdev_create_cb function is a cb function for raid bdev which creates the
235 : : * hierarchy from raid bdev to base bdev io channels. It will be called per core
236 : : * params:
237 : : * io_device - pointer to raid bdev io device represented by raid_bdev
238 : : * ctx_buf - pointer to context buffer for raid bdev io channel
239 : : * returns:
240 : : * 0 - success
241 : : * non zero - failure
242 : : */
243 : : static int
244 : 1286 : raid_bdev_create_cb(void *io_device, void *ctx_buf)
245 : : {
246 : 1286 : struct raid_bdev *raid_bdev = io_device;
247 : 1286 : struct raid_bdev_io_channel *raid_ch = ctx_buf;
248 : : uint8_t i;
249 : 1286 : int ret = -ENOMEM;
250 : :
251 [ + + + + ]: 1286 : SPDK_DEBUGLOG(bdev_raid, "raid_bdev_create_cb, %p\n", raid_ch);
252 : :
253 [ - + ]: 1286 : assert(raid_bdev != NULL);
254 [ - + ]: 1286 : assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE);
255 : :
256 : 1286 : raid_ch->base_channel = calloc(raid_bdev->num_base_bdevs, sizeof(struct spdk_io_channel *));
257 [ - + ]: 1286 : if (!raid_ch->base_channel) {
258 : 0 : SPDK_ERRLOG("Unable to allocate base bdevs io channel\n");
259 : 0 : return -ENOMEM;
260 : : }
261 : :
262 [ + + ]: 5163 : for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
263 : : /*
264 : : * Get the spdk_io_channel for all the base bdevs. This is used during
265 : : * split logic to send the respective child bdev ios to respective base
266 : : * bdev io channel.
267 : : * Skip missing base bdevs and the process target, which should also be treated as
268 : : * missing until the process completes.
269 : : */
270 [ + + + + ]: 3877 : if (raid_bdev->base_bdev_info[i].is_configured == false ||
271 [ + + + + ]: 3785 : raid_bdev->base_bdev_info[i].is_process_target == true) {
272 : 264 : continue;
273 : : }
274 : 4813 : raid_ch->base_channel[i] = spdk_bdev_get_io_channel(
275 : 3613 : raid_bdev->base_bdev_info[i].desc);
276 [ - + ]: 3613 : if (!raid_ch->base_channel[i]) {
277 : 0 : SPDK_ERRLOG("Unable to create io channel for base bdev\n");
278 : 0 : goto err;
279 : : }
280 : : }
281 : :
282 [ + + ]: 1286 : if (raid_bdev->module->get_io_channel) {
283 : 706 : raid_ch->module_channel = raid_bdev->module->get_io_channel(raid_bdev);
284 [ - + ]: 706 : if (!raid_ch->module_channel) {
285 : 0 : SPDK_ERRLOG("Unable to create io channel for raid module\n");
286 : 0 : goto err;
287 : : }
288 : : }
289 : :
290 [ + + ]: 1286 : if (raid_bdev->process != NULL) {
291 : 180 : ret = raid_bdev_ch_process_setup(raid_ch, raid_bdev->process);
292 [ - + ]: 180 : if (ret != 0) {
293 : 0 : SPDK_ERRLOG("Failed to setup process io channel\n");
294 : 0 : goto err;
295 : : }
296 : : } else {
297 : 1106 : raid_ch->process.offset = RAID_OFFSET_BLOCKS_INVALID;
298 : : }
299 : :
300 : 1286 : return 0;
301 : 0 : err:
302 [ # # ]: 0 : for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
303 [ # # ]: 0 : if (raid_ch->base_channel[i] != NULL) {
304 : 0 : spdk_put_io_channel(raid_ch->base_channel[i]);
305 : : }
306 : : }
307 : 0 : free(raid_ch->base_channel);
308 : :
309 : 0 : raid_bdev_ch_process_cleanup(raid_ch);
310 : :
311 : 0 : return ret;
312 : : }
313 : :
314 : : /*
315 : : * brief:
316 : : * raid_bdev_destroy_cb function is a cb function for raid bdev which deletes the
317 : : * hierarchy from raid bdev to base bdev io channels. It will be called per core
318 : : * params:
319 : : * io_device - pointer to raid bdev io device represented by raid_bdev
320 : : * ctx_buf - pointer to context buffer for raid bdev io channel
321 : : * returns:
322 : : * none
323 : : */
324 : : static void
325 : 1286 : raid_bdev_destroy_cb(void *io_device, void *ctx_buf)
326 : : {
327 : 1286 : struct raid_bdev *raid_bdev = io_device;
328 : 1286 : struct raid_bdev_io_channel *raid_ch = ctx_buf;
329 : : uint8_t i;
330 : :
331 [ + + + + ]: 1286 : SPDK_DEBUGLOG(bdev_raid, "raid_bdev_destroy_cb\n");
332 : :
333 [ - + ]: 1286 : assert(raid_ch != NULL);
334 [ - + ]: 1286 : assert(raid_ch->base_channel);
335 : :
336 [ + + ]: 1286 : if (raid_ch->module_channel) {
337 : 706 : spdk_put_io_channel(raid_ch->module_channel);
338 : : }
339 : :
340 [ + + ]: 5163 : for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
341 : : /* Free base bdev channels */
342 [ + + ]: 3877 : if (raid_ch->base_channel[i] != NULL) {
343 : 3627 : spdk_put_io_channel(raid_ch->base_channel[i]);
344 : : }
345 : : }
346 : 1286 : free(raid_ch->base_channel);
347 : 1286 : raid_ch->base_channel = NULL;
348 : :
349 : 1286 : raid_bdev_ch_process_cleanup(raid_ch);
350 : 1286 : }
351 : :
352 : : /*
353 : : * brief:
354 : : * raid_bdev_cleanup is used to cleanup raid_bdev related data
355 : : * structures.
356 : : * params:
357 : : * raid_bdev - pointer to raid_bdev
358 : : * returns:
359 : : * none
360 : : */
361 : : static void
362 : 1126 : raid_bdev_cleanup(struct raid_bdev *raid_bdev)
363 : : {
364 : : struct raid_base_bdev_info *base_info;
365 : :
366 [ + + + + ]: 1126 : SPDK_DEBUGLOG(bdev_raid, "raid_bdev_cleanup, %p name %s, state %s\n",
367 : : raid_bdev, raid_bdev->bdev.name, raid_bdev_state_to_str(raid_bdev->state));
368 [ - + ]: 1126 : assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE);
369 [ - + ]: 1126 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
370 : :
371 [ + + ]: 6098 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
372 [ - + ]: 4972 : assert(base_info->desc == NULL);
373 : 4972 : free(base_info->name);
374 : : }
375 : :
376 [ + + ]: 1126 : TAILQ_REMOVE(&g_raid_bdev_list, raid_bdev, global_link);
377 : 1126 : }
378 : :
379 : : static void
380 : 1126 : raid_bdev_free(struct raid_bdev *raid_bdev)
381 : : {
382 : 1126 : raid_bdev_free_superblock(raid_bdev);
383 : 1126 : free(raid_bdev->base_bdev_info);
384 : 1126 : free(raid_bdev->bdev.name);
385 : 1126 : free(raid_bdev);
386 : 1126 : }
387 : :
388 : : static void
389 : 404 : raid_bdev_cleanup_and_free(struct raid_bdev *raid_bdev)
390 : : {
391 : 404 : raid_bdev_cleanup(raid_bdev);
392 : 404 : raid_bdev_free(raid_bdev);
393 : 404 : }
394 : :
395 : : static void
396 : 4393 : raid_bdev_deconfigure_base_bdev(struct raid_base_bdev_info *base_info)
397 : : {
398 : 4393 : struct raid_bdev *raid_bdev = base_info->raid_bdev;
399 : :
400 [ - + - + ]: 4393 : assert(base_info->is_configured);
401 [ - + ]: 4393 : assert(raid_bdev->num_base_bdevs_discovered);
402 : 4393 : raid_bdev->num_base_bdevs_discovered--;
403 : 4393 : base_info->is_configured = false;
404 : 4393 : base_info->is_process_target = false;
405 : 4393 : }
406 : :
407 : : /*
408 : : * brief:
409 : : * free resource of base bdev for raid bdev
410 : : * params:
411 : : * base_info - raid base bdev info
412 : : * returns:
413 : : * none
414 : : */
415 : : static void
416 : 5721 : raid_bdev_free_base_bdev_resource(struct raid_base_bdev_info *base_info)
417 : : {
418 : 5721 : struct raid_bdev *raid_bdev = base_info->raid_bdev;
419 : :
420 [ - + ]: 5721 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
421 : :
422 : 5721 : free(base_info->name);
423 : 5721 : base_info->name = NULL;
424 [ + + ]: 5721 : if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING) {
425 : 4000 : spdk_uuid_set_null(&base_info->uuid);
426 : : }
427 : 5721 : base_info->is_failed = false;
428 : :
429 [ + + ]: 5721 : if (base_info->desc == NULL) {
430 : 1083 : return;
431 : : }
432 : :
433 : 4638 : spdk_bdev_module_release_bdev(spdk_bdev_desc_get_bdev(base_info->desc));
434 : 4638 : spdk_bdev_close(base_info->desc);
435 : 4638 : base_info->desc = NULL;
436 : 4638 : spdk_put_io_channel(base_info->app_thread_ch);
437 : 4638 : base_info->app_thread_ch = NULL;
438 : :
439 [ + + + + ]: 4638 : if (base_info->is_configured) {
440 : 3987 : raid_bdev_deconfigure_base_bdev(base_info);
441 : : }
442 : : }
443 : :
444 : : static void
445 : 790 : raid_bdev_io_device_unregister_cb(void *io_device)
446 : : {
447 : 790 : struct raid_bdev *raid_bdev = io_device;
448 : :
449 [ + + ]: 790 : if (raid_bdev->num_base_bdevs_discovered == 0) {
450 : : /* Free raid_bdev when there are no base bdevs left */
451 [ + + + + ]: 722 : SPDK_DEBUGLOG(bdev_raid, "raid bdev base bdevs is 0, going to free all in destruct\n");
452 : 722 : raid_bdev_cleanup(raid_bdev);
453 : 722 : spdk_bdev_destruct_done(&raid_bdev->bdev, 0);
454 : 722 : raid_bdev_free(raid_bdev);
455 : : } else {
456 : 68 : spdk_bdev_destruct_done(&raid_bdev->bdev, 0);
457 : : }
458 : 790 : }
459 : :
460 : : void
461 : 790 : raid_bdev_module_stop_done(struct raid_bdev *raid_bdev)
462 : : {
463 [ + - ]: 790 : if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING) {
464 : 790 : spdk_io_device_unregister(raid_bdev, raid_bdev_io_device_unregister_cb);
465 : : }
466 : 790 : }
467 : :
468 : : static void
469 : 790 : _raid_bdev_destruct(void *ctxt)
470 : : {
471 : 790 : struct raid_bdev *raid_bdev = ctxt;
472 : : struct raid_base_bdev_info *base_info;
473 : :
474 [ + + + + ]: 790 : SPDK_DEBUGLOG(bdev_raid, "raid_bdev_destruct\n");
475 : :
476 [ - + ]: 790 : assert(raid_bdev->process == NULL);
477 : :
478 [ + + ]: 4454 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
479 : : /*
480 : : * Close all base bdev descriptors for which call has come from below
481 : : * layers. Also close the descriptors if we have started shutdown.
482 : : */
483 [ + + + + : 3664 : if (g_shutdown_started || base_info->remove_scheduled == true) {
- + + + ]
484 : 3396 : raid_bdev_free_base_bdev_resource(base_info);
485 : : }
486 : : }
487 : :
488 [ + + + + ]: 790 : if (g_shutdown_started) {
489 : 268 : raid_bdev->state = RAID_BDEV_STATE_OFFLINE;
490 : : }
491 : :
492 [ + + ]: 790 : if (raid_bdev->module->stop != NULL) {
493 [ + + ]: 558 : if (raid_bdev->module->stop(raid_bdev) == false) {
494 : 393 : return;
495 : : }
496 : : }
497 : :
498 : 397 : raid_bdev_module_stop_done(raid_bdev);
499 : : }
500 : :
501 : : static int
502 : 790 : raid_bdev_destruct(void *ctx)
503 : : {
504 : 790 : spdk_thread_exec_msg(spdk_thread_get_app_thread(), _raid_bdev_destruct, ctx);
505 : :
506 : 790 : return 1;
507 : : }
508 : :
509 : : int
510 : 0 : raid_bdev_remap_dix_reftag(void *md_buf, uint64_t num_blocks,
511 : : struct spdk_bdev *bdev, uint32_t remapped_offset)
512 : : {
513 : 0 : struct spdk_dif_ctx dif_ctx;
514 : 0 : struct spdk_dif_error err_blk = {};
515 : : int rc;
516 : 0 : struct spdk_dif_ctx_init_ext_opts dif_opts;
517 : 0 : struct iovec md_iov = {
518 : : .iov_base = md_buf,
519 : 0 : .iov_len = num_blocks * bdev->md_len,
520 : : };
521 : :
522 [ # # ]: 0 : if (md_buf == NULL) {
523 : 0 : return 0;
524 : : }
525 : :
526 : 0 : dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
527 : 0 : dif_opts.dif_pi_format = SPDK_DIF_PI_FORMAT_16;
528 : 0 : rc = spdk_dif_ctx_init(&dif_ctx,
529 [ # # ]: 0 : bdev->blocklen, bdev->md_len, bdev->md_interleave,
530 [ # # ]: 0 : bdev->dif_is_head_of_md, bdev->dif_type,
531 : : SPDK_DIF_FLAGS_REFTAG_CHECK,
532 : : 0, 0, 0, 0, 0, &dif_opts);
533 [ # # ]: 0 : if (rc != 0) {
534 : 0 : SPDK_ERRLOG("Initialization of DIF context failed\n");
535 : 0 : return rc;
536 : : }
537 : :
538 : 0 : spdk_dif_ctx_set_remapped_init_ref_tag(&dif_ctx, remapped_offset);
539 : :
540 : 0 : rc = spdk_dix_remap_ref_tag(&md_iov, num_blocks, &dif_ctx, &err_blk, false);
541 [ # # ]: 0 : if (rc != 0) {
542 : 0 : SPDK_ERRLOG("Remapping reference tag failed. type=%d, offset=%d"
543 : : PRIu32 "\n", err_blk.err_type, err_blk.err_offset);
544 : : }
545 : :
546 : 0 : return rc;
547 : : }
548 : :
549 : : int
550 : 0 : raid_bdev_verify_dix_reftag(struct iovec *iovs, int iovcnt, void *md_buf,
551 : : uint64_t num_blocks, struct spdk_bdev *bdev, uint32_t offset_blocks)
552 : : {
553 : 0 : struct spdk_dif_ctx dif_ctx;
554 : 0 : struct spdk_dif_error err_blk = {};
555 : : int rc;
556 : 0 : struct spdk_dif_ctx_init_ext_opts dif_opts;
557 : 0 : struct iovec md_iov = {
558 : : .iov_base = md_buf,
559 : 0 : .iov_len = num_blocks * bdev->md_len,
560 : : };
561 : :
562 [ # # ]: 0 : if (md_buf == NULL) {
563 : 0 : return 0;
564 : : }
565 : :
566 : 0 : dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
567 : 0 : dif_opts.dif_pi_format = SPDK_DIF_PI_FORMAT_16;
568 : 0 : rc = spdk_dif_ctx_init(&dif_ctx,
569 [ # # ]: 0 : bdev->blocklen, bdev->md_len, bdev->md_interleave,
570 [ # # ]: 0 : bdev->dif_is_head_of_md, bdev->dif_type,
571 : : SPDK_DIF_FLAGS_REFTAG_CHECK,
572 : : offset_blocks, 0, 0, 0, 0, &dif_opts);
573 [ # # ]: 0 : if (rc != 0) {
574 : 0 : SPDK_ERRLOG("Initialization of DIF context failed\n");
575 : 0 : return rc;
576 : : }
577 : :
578 : 0 : rc = spdk_dix_verify(iovs, iovcnt, &md_iov, num_blocks, &dif_ctx, &err_blk);
579 [ # # ]: 0 : if (rc != 0) {
580 : 0 : SPDK_ERRLOG("Reference tag check failed. type=%d, offset=%d"
581 : : PRIu32 "\n", err_blk.err_type, err_blk.err_offset);
582 : : }
583 : :
584 : 0 : return rc;
585 : : }
586 : :
587 : : void
588 : 16645073 : raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status)
589 : : {
590 : 16645073 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io);
591 : : int rc;
592 : :
593 [ + + ]: 16645073 : if (raid_io->split.offset != RAID_OFFSET_BLOCKS_INVALID) {
594 : 872 : struct iovec *split_iov = raid_io->split.iov;
595 : 872 : const struct iovec *split_iov_orig = &raid_io->split.iov_copy;
596 : :
597 : : /*
598 : : * Non-zero offset here means that this is the completion of the first part of the
599 : : * split I/O (the higher LBAs). Then, we submit the second part and set offset to 0.
600 : : */
601 [ + + ]: 872 : if (raid_io->split.offset != 0) {
602 : 436 : raid_io->offset_blocks = bdev_io->u.bdev.offset_blocks;
603 : 436 : raid_io->md_buf = bdev_io->u.bdev.md_buf;
604 : :
605 [ + - ]: 436 : if (status == SPDK_BDEV_IO_STATUS_SUCCESS) {
606 : 436 : raid_io->num_blocks = raid_io->split.offset;
607 : 436 : raid_io->iovcnt = raid_io->iovs - bdev_io->u.bdev.iovs;
608 : 436 : raid_io->iovs = bdev_io->u.bdev.iovs;
609 [ + + ]: 436 : if (split_iov != NULL) {
610 : 432 : raid_io->iovcnt++;
611 : 432 : split_iov->iov_len = split_iov->iov_base - split_iov_orig->iov_base;
612 : 432 : split_iov->iov_base = split_iov_orig->iov_base;
613 : : }
614 : :
615 : 436 : raid_io->split.offset = 0;
616 : 436 : raid_io->base_bdev_io_submitted = 0;
617 : 436 : raid_io->raid_ch = raid_io->raid_ch->process.ch_processed;
618 : :
619 : 436 : raid_io->raid_bdev->module->submit_rw_request(raid_io);
620 : 436 : return;
621 : : }
622 : : }
623 : :
624 : 436 : raid_io->num_blocks = bdev_io->u.bdev.num_blocks;
625 : 436 : raid_io->iovcnt = bdev_io->u.bdev.iovcnt;
626 : 436 : raid_io->iovs = bdev_io->u.bdev.iovs;
627 [ + + ]: 436 : if (split_iov != NULL) {
628 : 432 : *split_iov = *split_iov_orig;
629 : : }
630 : : }
631 : :
632 [ + + ]: 16644637 : if (spdk_unlikely(raid_io->completion_cb != NULL)) {
633 : 8757 : raid_io->completion_cb(raid_io, status);
634 : : } else {
635 [ + + - + : 16635879 : if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_READ &&
- + - - -
+ - - ]
636 : : spdk_bdev_get_dif_type(bdev_io->bdev) != SPDK_DIF_DISABLE &&
637 : : bdev_io->bdev->dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK &&
638 : : status == SPDK_BDEV_IO_STATUS_SUCCESS)) {
639 : :
640 : 0 : rc = raid_bdev_remap_dix_reftag(bdev_io->u.bdev.md_buf,
641 : : bdev_io->u.bdev.num_blocks, bdev_io->bdev,
642 : 0 : bdev_io->u.bdev.offset_blocks);
643 [ # # ]: 0 : if (rc != 0) {
644 : 0 : status = SPDK_BDEV_IO_STATUS_FAILED;
645 : : }
646 : : }
647 : 16635879 : spdk_bdev_io_complete(bdev_io, status);
648 : : }
649 : : }
650 : :
651 : : /*
652 : : * brief:
653 : : * raid_bdev_io_complete_part - signal the completion of a part of the expected
654 : : * base bdev IOs and complete the raid_io if this is the final expected IO.
655 : : * The caller should first set raid_io->base_bdev_io_remaining. This function
656 : : * will decrement this counter by the value of the 'completed' parameter and
657 : : * complete the raid_io if the counter reaches 0. The caller is free to
658 : : * interpret the 'base_bdev_io_remaining' and 'completed' values as needed,
659 : : * it can represent e.g. blocks or IOs.
660 : : * params:
661 : : * raid_io - pointer to raid_bdev_io
662 : : * completed - the part of the raid_io that has been completed
663 : : * status - status of the base IO
664 : : * returns:
665 : : * true - if the raid_io is completed
666 : : * false - otherwise
667 : : */
668 : : bool
669 : 9236459 : raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
670 : : enum spdk_bdev_io_status status)
671 : : {
672 [ - + ]: 9236459 : assert(raid_io->base_bdev_io_remaining >= completed);
673 : 9236459 : raid_io->base_bdev_io_remaining -= completed;
674 : :
675 [ + + ]: 9236459 : if (status != raid_io->base_bdev_io_status_default) {
676 : 5631444 : raid_io->base_bdev_io_status = status;
677 : : }
678 : :
679 [ + + ]: 9236459 : if (raid_io->base_bdev_io_remaining == 0) {
680 : 4304642 : raid_bdev_io_complete(raid_io, raid_io->base_bdev_io_status);
681 : 4304642 : return true;
682 : : } else {
683 : 4931807 : return false;
684 : : }
685 : : }
686 : :
687 : : /*
688 : : * brief:
689 : : * raid_bdev_queue_io_wait function processes the IO which failed to submit.
690 : : * It will try to queue the IOs after storing the context to bdev wait queue logic.
691 : : * params:
692 : : * raid_io - pointer to raid_bdev_io
693 : : * bdev - the block device that the IO is submitted to
694 : : * ch - io channel
695 : : * cb_fn - callback when the spdk_bdev_io for bdev becomes available
696 : : * returns:
697 : : * none
698 : : */
699 : : void
700 : 0 : raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
701 : : struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn)
702 : : {
703 : 0 : raid_io->waitq_entry.bdev = bdev;
704 : 0 : raid_io->waitq_entry.cb_fn = cb_fn;
705 : 0 : raid_io->waitq_entry.cb_arg = raid_io;
706 : 0 : spdk_bdev_queue_io_wait(bdev, ch, &raid_io->waitq_entry);
707 : 0 : }
708 : :
709 : : static void
710 : 164 : raid_base_bdev_reset_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
711 : : {
712 : 164 : struct raid_bdev_io *raid_io = cb_arg;
713 : :
714 : 164 : spdk_bdev_free_io(bdev_io);
715 : :
716 [ + - ]: 164 : raid_bdev_io_complete_part(raid_io, 1, success ?
717 : : SPDK_BDEV_IO_STATUS_SUCCESS :
718 : : SPDK_BDEV_IO_STATUS_FAILED);
719 : 164 : }
720 : :
721 : : static void raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io);
722 : :
723 : : static void
724 : 0 : _raid_bdev_submit_reset_request(void *_raid_io)
725 : : {
726 : 0 : struct raid_bdev_io *raid_io = _raid_io;
727 : :
728 : 0 : raid_bdev_submit_reset_request(raid_io);
729 : 0 : }
730 : :
731 : : /*
732 : : * brief:
733 : : * raid_bdev_submit_reset_request function submits reset requests
734 : : * to member disks; it will submit as many as possible unless a reset fails with -ENOMEM, in
735 : : * which case it will queue it for later submission
736 : : * params:
737 : : * raid_io
738 : : * returns:
739 : : * none
740 : : */
741 : : static void
742 : 21 : raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io)
743 : : {
744 : : struct raid_bdev *raid_bdev;
745 : : int ret;
746 : : uint8_t i;
747 : : struct raid_base_bdev_info *base_info;
748 : : struct spdk_io_channel *base_ch;
749 : :
750 : 21 : raid_bdev = raid_io->raid_bdev;
751 : :
752 [ + - ]: 21 : if (raid_io->base_bdev_io_remaining == 0) {
753 : 21 : raid_io->base_bdev_io_remaining = raid_bdev->num_base_bdevs;
754 : : }
755 : :
756 [ + + ]: 185 : for (i = raid_io->base_bdev_io_submitted; i < raid_bdev->num_base_bdevs; i++) {
757 : 164 : base_info = &raid_bdev->base_bdev_info[i];
758 : 164 : base_ch = raid_io->raid_ch->base_channel[i];
759 [ - + ]: 164 : if (base_ch == NULL) {
760 : 0 : raid_io->base_bdev_io_submitted++;
761 : 0 : raid_bdev_io_complete_part(raid_io, 1, SPDK_BDEV_IO_STATUS_SUCCESS);
762 : 0 : continue;
763 : : }
764 : 164 : ret = spdk_bdev_reset(base_info->desc, base_ch,
765 : : raid_base_bdev_reset_complete, raid_io);
766 [ + - ]: 164 : if (ret == 0) {
767 : 164 : raid_io->base_bdev_io_submitted++;
768 [ # # ]: 0 : } else if (ret == -ENOMEM) {
769 : 0 : raid_bdev_queue_io_wait(raid_io, spdk_bdev_desc_get_bdev(base_info->desc),
770 : : base_ch, _raid_bdev_submit_reset_request);
771 : 0 : return;
772 : : } else {
773 : 0 : SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n");
774 : 0 : assert(false);
775 : : raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
776 : : return;
777 : : }
778 : : }
779 : : }
780 : :
781 : : static void
782 : 436 : raid_bdev_io_split(struct raid_bdev_io *raid_io, uint64_t split_offset)
783 : : {
784 : 436 : struct raid_bdev *raid_bdev = raid_io->raid_bdev;
785 : 436 : size_t iov_offset = split_offset * raid_bdev->bdev.blocklen;
786 : : int i;
787 : :
788 [ - + ]: 436 : assert(split_offset != 0);
789 [ - + ]: 436 : assert(raid_io->split.offset == RAID_OFFSET_BLOCKS_INVALID);
790 : 436 : raid_io->split.offset = split_offset;
791 : :
792 : 436 : raid_io->offset_blocks += split_offset;
793 : 436 : raid_io->num_blocks -= split_offset;
794 [ + + ]: 436 : if (raid_io->md_buf != NULL) {
795 : 20 : raid_io->md_buf += (split_offset * raid_bdev->bdev.md_len);
796 : : }
797 : :
798 [ + - ]: 464 : for (i = 0; i < raid_io->iovcnt; i++) {
799 : 464 : struct iovec *iov = &raid_io->iovs[i];
800 : :
801 [ + + ]: 464 : if (iov_offset < iov->iov_len) {
802 [ + + ]: 436 : if (iov_offset == 0) {
803 : 4 : raid_io->split.iov = NULL;
804 : : } else {
805 : 432 : raid_io->split.iov = iov;
806 : 432 : raid_io->split.iov_copy = *iov;
807 : 432 : iov->iov_base += iov_offset;
808 : 432 : iov->iov_len -= iov_offset;
809 : : }
810 : 436 : raid_io->iovs += i;
811 : 436 : raid_io->iovcnt -= i;
812 : 436 : break;
813 : : }
814 : :
815 : 28 : iov_offset -= iov->iov_len;
816 : : }
817 : 436 : }
818 : :
819 : : static void
820 : 15112553 : raid_bdev_submit_rw_request(struct raid_bdev_io *raid_io)
821 : : {
822 : 15112553 : struct raid_bdev_io_channel *raid_ch = raid_io->raid_ch;
823 : :
824 [ + + ]: 15112553 : if (raid_ch->process.offset != RAID_OFFSET_BLOCKS_INVALID) {
825 : 6691 : uint64_t offset_begin = raid_io->offset_blocks;
826 : 6691 : uint64_t offset_end = offset_begin + raid_io->num_blocks;
827 : :
828 [ + + ]: 6691 : if (offset_end > raid_ch->process.offset) {
829 [ + + ]: 4739 : if (offset_begin < raid_ch->process.offset) {
830 : : /*
831 : : * If the I/O spans both the processed and unprocessed ranges,
832 : : * split it and first handle the unprocessed part. After it
833 : : * completes, the rest will be handled.
834 : : * This situation occurs when the process thread is not active
835 : : * or is waiting for the process window range to be locked
836 : : * (quiesced). When a window is being processed, such I/Os will be
837 : : * deferred by the bdev layer until the window is unlocked.
838 : : */
839 [ + + + + ]: 436 : SPDK_DEBUGLOG(bdev_raid, "split: process_offset: %lu offset_begin: %lu offset_end: %lu\n",
840 : : raid_ch->process.offset, offset_begin, offset_end);
841 : 436 : raid_bdev_io_split(raid_io, raid_ch->process.offset - offset_begin);
842 : : }
843 : : } else {
844 : : /* Use the child channel, which corresponds to the already processed range */
845 : 1952 : raid_io->raid_ch = raid_ch->process.ch_processed;
846 : : }
847 : : }
848 : :
849 : 15112553 : raid_io->raid_bdev->module->submit_rw_request(raid_io);
850 : 15112553 : }
851 : :
852 : : /*
853 : : * brief:
854 : : * Callback function to spdk_bdev_io_get_buf.
855 : : * params:
856 : : * ch - pointer to raid bdev io channel
857 : : * bdev_io - pointer to parent bdev_io on raid bdev device
858 : : * success - True if buffer is allocated or false otherwise.
859 : : * returns:
860 : : * none
861 : : */
862 : : static void
863 : 5910781 : raid_bdev_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
864 : : bool success)
865 : : {
866 : 5910781 : struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx;
867 : :
868 [ - + ]: 5910781 : if (!success) {
869 : 0 : raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
870 : 0 : return;
871 : : }
872 : :
873 : 5910781 : raid_bdev_submit_rw_request(raid_io);
874 : : }
875 : :
876 : : void
877 : 16644615 : raid_bdev_io_init(struct raid_bdev_io *raid_io, struct raid_bdev_io_channel *raid_ch,
878 : : enum spdk_bdev_io_type type, uint64_t offset_blocks,
879 : : uint64_t num_blocks, struct iovec *iovs, int iovcnt, void *md_buf,
880 : : struct spdk_memory_domain *memory_domain, void *memory_domain_ctx)
881 : : {
882 : 16644615 : struct spdk_io_channel *ch = spdk_io_channel_from_ctx(raid_ch);
883 : 16644615 : struct raid_bdev *raid_bdev = spdk_io_channel_get_io_device(ch);
884 : :
885 : 16644615 : raid_io->type = type;
886 : 16644615 : raid_io->offset_blocks = offset_blocks;
887 : 16644615 : raid_io->num_blocks = num_blocks;
888 : 16644615 : raid_io->iovs = iovs;
889 : 16644615 : raid_io->iovcnt = iovcnt;
890 : 16644615 : raid_io->memory_domain = memory_domain;
891 : 16644615 : raid_io->memory_domain_ctx = memory_domain_ctx;
892 : 16644615 : raid_io->md_buf = md_buf;
893 : :
894 : 16644615 : raid_io->raid_bdev = raid_bdev;
895 : 16644615 : raid_io->raid_ch = raid_ch;
896 : 16644615 : raid_io->base_bdev_io_remaining = 0;
897 : 16644615 : raid_io->base_bdev_io_submitted = 0;
898 : 16644615 : raid_io->completion_cb = NULL;
899 : 16644615 : raid_io->split.offset = RAID_OFFSET_BLOCKS_INVALID;
900 : :
901 : 16644615 : raid_bdev_io_set_default_status(raid_io, SPDK_BDEV_IO_STATUS_SUCCESS);
902 : 16644615 : }
903 : :
904 : : /*
905 : : * brief:
906 : : * raid_bdev_submit_request function is the submit_request function pointer of
907 : : * raid bdev function table. This is used to submit the io on raid_bdev to below
908 : : * layers.
909 : : * params:
910 : : * ch - pointer to raid bdev io channel
911 : : * bdev_io - pointer to parent bdev_io on raid bdev device
912 : : * returns:
913 : : * none
914 : : */
915 : : static void
916 : 16635879 : raid_bdev_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
917 : : {
918 : 16635879 : struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx;
919 : :
920 : 16635879 : raid_bdev_io_init(raid_io, spdk_io_channel_get_ctx(ch), bdev_io->type,
921 : : bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
922 : : bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, bdev_io->u.bdev.md_buf,
923 : : bdev_io->u.bdev.memory_domain, bdev_io->u.bdev.memory_domain_ctx);
924 : :
925 [ + + + + : 16635879 : switch (bdev_io->type) {
- ]
926 : 5910781 : case SPDK_BDEV_IO_TYPE_READ:
927 : 5910781 : spdk_bdev_io_get_buf(bdev_io, raid_bdev_get_buf_cb,
928 : 5910781 : bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
929 : 5910781 : break;
930 : 9201762 : case SPDK_BDEV_IO_TYPE_WRITE:
931 : 9201762 : raid_bdev_submit_rw_request(raid_io);
932 : 9201762 : break;
933 : :
934 : 21 : case SPDK_BDEV_IO_TYPE_RESET:
935 : 21 : raid_bdev_submit_reset_request(raid_io);
936 : 21 : break;
937 : :
938 : 1523309 : case SPDK_BDEV_IO_TYPE_FLUSH:
939 : : case SPDK_BDEV_IO_TYPE_UNMAP:
940 [ - + ]: 1523309 : if (raid_io->raid_bdev->process != NULL) {
941 : : /* TODO: rebuild support */
942 : 0 : raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
943 : 0 : return;
944 : : }
945 : 1523309 : raid_io->raid_bdev->module->submit_null_payload_request(raid_io);
946 : 1523309 : break;
947 : :
948 : 0 : default:
949 : 0 : SPDK_ERRLOG("submit request, invalid io type %u\n", bdev_io->type);
950 : 0 : raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
951 : 0 : break;
952 : : }
953 : : }
954 : :
955 : : /*
956 : : * brief:
957 : : * _raid_bdev_io_type_supported checks whether io_type is supported in
958 : : * all base bdev modules of raid bdev module. If anyone among the base_bdevs
959 : : * doesn't support, the raid device doesn't supports.
960 : : *
961 : : * params:
962 : : * raid_bdev - pointer to raid bdev context
963 : : * io_type - io type
964 : : * returns:
965 : : * true - io_type is supported
966 : : * false - io_type is not supported
967 : : */
968 : : inline static bool
969 : 2147 : _raid_bdev_io_type_supported(struct raid_bdev *raid_bdev, enum spdk_bdev_io_type io_type)
970 : : {
971 : : struct raid_base_bdev_info *base_info;
972 : :
973 [ + + + + ]: 2147 : if (io_type == SPDK_BDEV_IO_TYPE_FLUSH ||
974 : : io_type == SPDK_BDEV_IO_TYPE_UNMAP) {
975 [ + + ]: 1501 : if (raid_bdev->module->submit_null_payload_request == NULL) {
976 : 710 : return false;
977 : : }
978 : : }
979 : :
980 [ + + ]: 5355 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
981 [ + + ]: 3938 : if (base_info->desc == NULL) {
982 : 34 : continue;
983 : : }
984 : :
985 [ + + ]: 3904 : if (spdk_bdev_io_type_supported(spdk_bdev_desc_get_bdev(base_info->desc), io_type) == false) {
986 : 20 : return false;
987 : : }
988 : : }
989 : :
990 : 1417 : return true;
991 : : }
992 : :
993 : : /*
994 : : * brief:
995 : : * raid_bdev_io_type_supported is the io_supported function for bdev function
996 : : * table which returns whether the particular io type is supported or not by
997 : : * raid bdev module
998 : : * params:
999 : : * ctx - pointer to raid bdev context
1000 : : * type - io type
1001 : : * returns:
1002 : : * true - io_type is supported
1003 : : * false - io_type is not supported
1004 : : */
1005 : : static bool
1006 : 603942 : raid_bdev_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
1007 : : {
1008 [ + + + ]: 603942 : switch (io_type) {
1009 : 197967 : case SPDK_BDEV_IO_TYPE_READ:
1010 : : case SPDK_BDEV_IO_TYPE_WRITE:
1011 : 197967 : return true;
1012 : :
1013 : 2147 : case SPDK_BDEV_IO_TYPE_FLUSH:
1014 : : case SPDK_BDEV_IO_TYPE_RESET:
1015 : : case SPDK_BDEV_IO_TYPE_UNMAP:
1016 : 2147 : return _raid_bdev_io_type_supported(ctx, io_type);
1017 : :
1018 : 403828 : default:
1019 : 403828 : return false;
1020 : : }
1021 : :
1022 : : return false;
1023 : : }
1024 : :
1025 : : /*
1026 : : * brief:
1027 : : * raid_bdev_get_io_channel is the get_io_channel function table pointer for
1028 : : * raid bdev. This is used to return the io channel for this raid bdev
1029 : : * params:
1030 : : * ctxt - pointer to raid_bdev
1031 : : * returns:
1032 : : * pointer to io channel for raid bdev
1033 : : */
1034 : : static struct spdk_io_channel *
1035 : 1098 : raid_bdev_get_io_channel(void *ctxt)
1036 : : {
1037 : 1098 : struct raid_bdev *raid_bdev = ctxt;
1038 : :
1039 : 1098 : return spdk_get_io_channel(raid_bdev);
1040 : : }
1041 : :
1042 : : void
1043 : 6426 : raid_bdev_write_info_json(struct raid_bdev *raid_bdev, struct spdk_json_write_ctx *w)
1044 : : {
1045 : : struct raid_base_bdev_info *base_info;
1046 : :
1047 [ - + ]: 6426 : assert(raid_bdev != NULL);
1048 [ - + ]: 6426 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
1049 : :
1050 : 6426 : spdk_json_write_named_uuid(w, "uuid", &raid_bdev->bdev.uuid);
1051 : 6426 : spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb);
1052 : 6426 : spdk_json_write_named_string(w, "state", raid_bdev_state_to_str(raid_bdev->state));
1053 : 6426 : spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
1054 [ - + ]: 6426 : spdk_json_write_named_bool(w, "superblock", raid_bdev->superblock_enabled);
1055 : 6426 : spdk_json_write_named_uint32(w, "num_base_bdevs", raid_bdev->num_base_bdevs);
1056 : 6426 : spdk_json_write_named_uint32(w, "num_base_bdevs_discovered", raid_bdev->num_base_bdevs_discovered);
1057 : 6426 : spdk_json_write_named_uint32(w, "num_base_bdevs_operational",
1058 : 6426 : raid_bdev->num_base_bdevs_operational);
1059 [ + + ]: 6426 : if (raid_bdev->process) {
1060 : 321 : struct raid_bdev_process *process = raid_bdev->process;
1061 : 321 : uint64_t offset = process->window_offset;
1062 : :
1063 : 321 : spdk_json_write_named_object_begin(w, "process");
1064 : 321 : spdk_json_write_name(w, "type");
1065 : 321 : spdk_json_write_string(w, raid_bdev_process_to_str(process->type));
1066 : 321 : spdk_json_write_named_string(w, "target", process->target->name);
1067 : 321 : spdk_json_write_named_object_begin(w, "progress");
1068 : 321 : spdk_json_write_named_uint64(w, "blocks", offset);
1069 : 321 : spdk_json_write_named_uint32(w, "percent", offset * 100.0 / raid_bdev->bdev.blockcnt);
1070 : 321 : spdk_json_write_object_end(w);
1071 : 321 : spdk_json_write_object_end(w);
1072 : : }
1073 : 6426 : spdk_json_write_name(w, "base_bdevs_list");
1074 : 6426 : spdk_json_write_array_begin(w);
1075 [ + + ]: 26807 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1076 : 20381 : spdk_json_write_object_begin(w);
1077 : 20381 : spdk_json_write_name(w, "name");
1078 [ + + ]: 20381 : if (base_info->name) {
1079 : 16269 : spdk_json_write_string(w, base_info->name);
1080 : : } else {
1081 : 4112 : spdk_json_write_null(w);
1082 : : }
1083 : 20381 : spdk_json_write_named_uuid(w, "uuid", &base_info->uuid);
1084 [ - + ]: 20381 : spdk_json_write_named_bool(w, "is_configured", base_info->is_configured);
1085 : 20381 : spdk_json_write_named_uint64(w, "data_offset", base_info->data_offset);
1086 : 20381 : spdk_json_write_named_uint64(w, "data_size", base_info->data_size);
1087 : 20381 : spdk_json_write_object_end(w);
1088 : : }
1089 : 6426 : spdk_json_write_array_end(w);
1090 : 6426 : }
1091 : :
1092 : : /*
1093 : : * brief:
1094 : : * raid_bdev_dump_info_json is the function table pointer for raid bdev
1095 : : * params:
1096 : : * ctx - pointer to raid_bdev
1097 : : * w - pointer to json context
1098 : : * returns:
1099 : : * 0 - success
1100 : : * non zero - failure
1101 : : */
1102 : : static int
1103 : 629 : raid_bdev_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
1104 : : {
1105 : 629 : struct raid_bdev *raid_bdev = ctx;
1106 : :
1107 [ + + + + ]: 629 : SPDK_DEBUGLOG(bdev_raid, "raid_bdev_dump_config_json\n");
1108 : :
1109 : : /* Dump the raid bdev configuration related information */
1110 : 629 : spdk_json_write_named_object_begin(w, "raid");
1111 : 629 : raid_bdev_write_info_json(raid_bdev, w);
1112 : 629 : spdk_json_write_object_end(w);
1113 : :
1114 : 629 : return 0;
1115 : : }
1116 : :
1117 : : /*
1118 : : * brief:
1119 : : * raid_bdev_write_config_json is the function table pointer for raid bdev
1120 : : * params:
1121 : : * bdev - pointer to spdk_bdev
1122 : : * w - pointer to json context
1123 : : * returns:
1124 : : * none
1125 : : */
1126 : : static void
1127 : 17 : raid_bdev_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
1128 : : {
1129 : 17 : struct raid_bdev *raid_bdev = bdev->ctxt;
1130 : : struct raid_base_bdev_info *base_info;
1131 : :
1132 [ - + ]: 17 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
1133 : :
1134 [ - + - + ]: 17 : if (raid_bdev->superblock_enabled) {
1135 : : /* raid bdev configuration is stored in the superblock */
1136 : 0 : return;
1137 : : }
1138 : :
1139 : 17 : spdk_json_write_object_begin(w);
1140 : :
1141 : 17 : spdk_json_write_named_string(w, "method", "bdev_raid_create");
1142 : :
1143 : 17 : spdk_json_write_named_object_begin(w, "params");
1144 : 17 : spdk_json_write_named_string(w, "name", bdev->name);
1145 : 17 : spdk_json_write_named_uuid(w, "uuid", &raid_bdev->bdev.uuid);
1146 [ + + ]: 17 : if (raid_bdev->strip_size_kb != 0) {
1147 : 12 : spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb);
1148 : : }
1149 : 17 : spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
1150 : :
1151 : 17 : spdk_json_write_named_array_begin(w, "base_bdevs");
1152 [ + + ]: 53 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1153 [ + - ]: 36 : if (base_info->name) {
1154 : 36 : spdk_json_write_string(w, base_info->name);
1155 : : } else {
1156 : 0 : char str[32];
1157 : :
1158 [ # # ]: 0 : snprintf(str, sizeof(str), "removed_base_bdev_%u", raid_bdev_base_bdev_slot(base_info));
1159 : 0 : spdk_json_write_string(w, str);
1160 : : }
1161 : : }
1162 : 17 : spdk_json_write_array_end(w);
1163 : 17 : spdk_json_write_object_end(w);
1164 : :
1165 : 17 : spdk_json_write_object_end(w);
1166 : : }
1167 : :
1168 : : static int
1169 : 6065 : raid_bdev_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size)
1170 : : {
1171 : 6065 : struct raid_bdev *raid_bdev = ctx;
1172 : : struct raid_base_bdev_info *base_info;
1173 : 6065 : int domains_count = 0, rc = 0;
1174 : :
1175 [ - + + + ]: 6065 : if (raid_bdev->module->memory_domains_supported == false) {
1176 : 434 : return 0;
1177 : : }
1178 : :
1179 : : /* First loop to get the number of memory domains */
1180 [ + + ]: 20546 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1181 [ + + + + ]: 14915 : if (base_info->is_configured == false) {
1182 : 414 : continue;
1183 : : }
1184 : 14501 : rc = spdk_bdev_get_memory_domains(spdk_bdev_desc_get_bdev(base_info->desc), NULL, 0);
1185 [ - + ]: 14501 : if (rc < 0) {
1186 : 0 : return rc;
1187 : : }
1188 : 14501 : domains_count += rc;
1189 : : }
1190 : :
1191 [ + + - + ]: 5631 : if (!domains || array_size < domains_count) {
1192 : 5061 : return domains_count;
1193 : : }
1194 : :
1195 [ + + ]: 2118 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1196 [ + + + + ]: 1548 : if (base_info->is_configured == false) {
1197 : 30 : continue;
1198 : : }
1199 : 1518 : rc = spdk_bdev_get_memory_domains(spdk_bdev_desc_get_bdev(base_info->desc), domains, array_size);
1200 [ - + ]: 1518 : if (rc < 0) {
1201 : 0 : return rc;
1202 : : }
1203 : 1518 : domains += rc;
1204 : 1518 : array_size -= rc;
1205 : : }
1206 : :
1207 : 570 : return domains_count;
1208 : : }
1209 : :
1210 : : /* g_raid_bdev_fn_table is the function table for raid bdev */
1211 : : static const struct spdk_bdev_fn_table g_raid_bdev_fn_table = {
1212 : : .destruct = raid_bdev_destruct,
1213 : : .submit_request = raid_bdev_submit_request,
1214 : : .io_type_supported = raid_bdev_io_type_supported,
1215 : : .get_io_channel = raid_bdev_get_io_channel,
1216 : : .dump_info_json = raid_bdev_dump_info_json,
1217 : : .write_config_json = raid_bdev_write_config_json,
1218 : : .get_memory_domains = raid_bdev_get_memory_domains,
1219 : : };
1220 : :
1221 : : struct raid_bdev *
1222 : 2089 : raid_bdev_find_by_name(const char *name)
1223 : : {
1224 : : struct raid_bdev *raid_bdev;
1225 : :
1226 [ + + ]: 2260 : TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
1227 [ + + - + : 1122 : if (strcmp(raid_bdev->bdev.name, name) == 0) {
+ + ]
1228 : 951 : return raid_bdev;
1229 : : }
1230 : : }
1231 : :
1232 : 1138 : return NULL;
1233 : : }
1234 : :
1235 : : static struct raid_bdev *
1236 : 856 : raid_bdev_find_by_uuid(const struct spdk_uuid *uuid)
1237 : : {
1238 : : struct raid_bdev *raid_bdev;
1239 : :
1240 [ + + ]: 856 : TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
1241 [ + - ]: 691 : if (spdk_uuid_compare(&raid_bdev->bdev.uuid, uuid) == 0) {
1242 : 691 : return raid_bdev;
1243 : : }
1244 : : }
1245 : :
1246 : 165 : return NULL;
1247 : : }
1248 : :
1249 : : static struct {
1250 : : const char *name;
1251 : : enum raid_level value;
1252 : : } g_raid_level_names[] = {
1253 : : { "raid0", RAID0 },
1254 : : { "0", RAID0 },
1255 : : { "raid1", RAID1 },
1256 : : { "1", RAID1 },
1257 : : { "raid5f", RAID5F },
1258 : : { "5f", RAID5F },
1259 : : { "concat", CONCAT },
1260 : : { }
1261 : : };
1262 : :
1263 : : const char *g_raid_state_names[] = {
1264 : : [RAID_BDEV_STATE_ONLINE] = "online",
1265 : : [RAID_BDEV_STATE_CONFIGURING] = "configuring",
1266 : : [RAID_BDEV_STATE_OFFLINE] = "offline",
1267 : : [RAID_BDEV_STATE_MAX] = NULL
1268 : : };
1269 : :
1270 : : static const char *g_raid_process_type_names[] = {
1271 : : [RAID_PROCESS_NONE] = "none",
1272 : : [RAID_PROCESS_REBUILD] = "rebuild",
1273 : : [RAID_PROCESS_MAX] = NULL
1274 : : };
1275 : :
1276 : : /* We have to use the typedef in the function declaration to appease astyle. */
1277 : : typedef enum raid_level raid_level_t;
1278 : : typedef enum raid_bdev_state raid_bdev_state_t;
1279 : :
1280 : : raid_level_t
1281 : 879 : raid_bdev_str_to_level(const char *str)
1282 : : {
1283 : : unsigned int i;
1284 : :
1285 [ - + ]: 879 : assert(str != NULL);
1286 : :
1287 [ + + ]: 3219 : for (i = 0; g_raid_level_names[i].name != NULL; i++) {
1288 [ + + - + : 3215 : if (strcasecmp(g_raid_level_names[i].name, str) == 0) {
+ + ]
1289 : 875 : return g_raid_level_names[i].value;
1290 : : }
1291 : : }
1292 : :
1293 : 4 : return INVALID_RAID_LEVEL;
1294 : : }
1295 : :
1296 : : const char *
1297 : 6459 : raid_bdev_level_to_str(enum raid_level level)
1298 : : {
1299 : : unsigned int i;
1300 : :
1301 [ + + ]: 23213 : for (i = 0; g_raid_level_names[i].name != NULL; i++) {
1302 [ + + ]: 23173 : if (g_raid_level_names[i].value == level) {
1303 : 6419 : return g_raid_level_names[i].name;
1304 : : }
1305 : : }
1306 : :
1307 : 40 : return "";
1308 : : }
1309 : :
1310 : : raid_bdev_state_t
1311 : 6159 : raid_bdev_str_to_state(const char *str)
1312 : : {
1313 : : unsigned int i;
1314 : :
1315 [ - + ]: 6159 : assert(str != NULL);
1316 : :
1317 [ + + ]: 19136 : for (i = 0; i < RAID_BDEV_STATE_MAX; i++) {
1318 [ + + - + : 15209 : if (strcasecmp(g_raid_state_names[i], str) == 0) {
+ + ]
1319 : 2232 : break;
1320 : : }
1321 : : }
1322 : :
1323 : 6159 : return i;
1324 : : }
1325 : :
1326 : : const char *
1327 : 7316 : raid_bdev_state_to_str(enum raid_bdev_state state)
1328 : : {
1329 [ - + ]: 7316 : if (state >= RAID_BDEV_STATE_MAX) {
1330 : 0 : return "";
1331 : : }
1332 : :
1333 : 7316 : return g_raid_state_names[state];
1334 : : }
1335 : :
1336 : : const char *
1337 : 849 : raid_bdev_process_to_str(enum raid_process_type value)
1338 : : {
1339 [ - + ]: 849 : if (value >= RAID_PROCESS_MAX) {
1340 : 0 : return "";
1341 : : }
1342 : :
1343 : 849 : return g_raid_process_type_names[value];
1344 : : }
1345 : :
1346 : : /*
1347 : : * brief:
1348 : : * raid_bdev_fini_start is called when bdev layer is starting the
1349 : : * shutdown process
1350 : : * params:
1351 : : * none
1352 : : * returns:
1353 : : * none
1354 : : */
1355 : : static void
1356 : 2142 : raid_bdev_fini_start(void)
1357 : : {
1358 [ + + + + ]: 2142 : SPDK_DEBUGLOG(bdev_raid, "raid_bdev_fini_start\n");
1359 : 2142 : g_shutdown_started = true;
1360 : 2142 : }
1361 : :
1362 : : /*
1363 : : * brief:
1364 : : * raid_bdev_exit is called on raid bdev module exit time by bdev layer
1365 : : * params:
1366 : : * none
1367 : : * returns:
1368 : : * none
1369 : : */
1370 : : static void
1371 : 2186 : raid_bdev_exit(void)
1372 : : {
1373 : : struct raid_bdev *raid_bdev, *tmp;
1374 : :
1375 [ + + + + ]: 2186 : SPDK_DEBUGLOG(bdev_raid, "raid_bdev_exit\n");
1376 : :
1377 [ - + ]: 2186 : TAILQ_FOREACH_SAFE(raid_bdev, &g_raid_bdev_list, global_link, tmp) {
1378 : 0 : raid_bdev_cleanup_and_free(raid_bdev);
1379 : : }
1380 : 2186 : }
1381 : :
1382 : : static void
1383 : 169 : raid_bdev_opts_config_json(struct spdk_json_write_ctx *w)
1384 : : {
1385 : 169 : spdk_json_write_object_begin(w);
1386 : :
1387 : 169 : spdk_json_write_named_string(w, "method", "bdev_raid_set_options");
1388 : :
1389 : 169 : spdk_json_write_named_object_begin(w, "params");
1390 : 169 : spdk_json_write_named_uint32(w, "process_window_size_kb", g_opts.process_window_size_kb);
1391 : 169 : spdk_json_write_object_end(w);
1392 : :
1393 : 169 : spdk_json_write_object_end(w);
1394 : 169 : }
1395 : :
1396 : : static int
1397 : 169 : raid_bdev_config_json(struct spdk_json_write_ctx *w)
1398 : : {
1399 : 169 : raid_bdev_opts_config_json(w);
1400 : :
1401 : 169 : return 0;
1402 : : }
1403 : :
1404 : : /*
1405 : : * brief:
1406 : : * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid
1407 : : * module
1408 : : * params:
1409 : : * none
1410 : : * returns:
1411 : : * size of spdk_bdev_io context for raid
1412 : : */
1413 : : static int
1414 : 3511 : raid_bdev_get_ctx_size(void)
1415 : : {
1416 [ + + + + ]: 3511 : SPDK_DEBUGLOG(bdev_raid, "raid_bdev_get_ctx_size\n");
1417 : 3511 : return sizeof(struct raid_bdev_io);
1418 : : }
1419 : :
1420 : : static struct spdk_bdev_module g_raid_if = {
1421 : : .name = "raid",
1422 : : .module_init = raid_bdev_init,
1423 : : .fini_start = raid_bdev_fini_start,
1424 : : .module_fini = raid_bdev_exit,
1425 : : .config_json = raid_bdev_config_json,
1426 : : .get_ctx_size = raid_bdev_get_ctx_size,
1427 : : .examine_disk = raid_bdev_examine,
1428 : : .async_init = false,
1429 : : .async_fini = false,
1430 : : };
1431 : 2336 : SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if)
1432 : :
1433 : : /*
1434 : : * brief:
1435 : : * raid_bdev_init is the initialization function for raid bdev module
1436 : : * params:
1437 : : * none
1438 : : * returns:
1439 : : * 0 - success
1440 : : * non zero - failure
1441 : : */
1442 : : static int
1443 : 2186 : raid_bdev_init(void)
1444 : : {
1445 : 2186 : return 0;
1446 : : }
1447 : :
1448 : : static int
1449 : 1138 : _raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs,
1450 : : enum raid_level level, bool superblock_enabled, const struct spdk_uuid *uuid,
1451 : : struct raid_bdev **raid_bdev_out)
1452 : : {
1453 : : struct raid_bdev *raid_bdev;
1454 : : struct spdk_bdev *raid_bdev_gen;
1455 : : struct raid_bdev_module *module;
1456 : : struct raid_base_bdev_info *base_info;
1457 : : uint8_t min_operational;
1458 : :
1459 [ - + - + ]: 1138 : if (strnlen(name, RAID_BDEV_SB_NAME_SIZE) == RAID_BDEV_SB_NAME_SIZE) {
1460 : 0 : SPDK_ERRLOG("Raid bdev name '%s' exceeds %d characters\n", name, RAID_BDEV_SB_NAME_SIZE - 1);
1461 : 0 : return -EINVAL;
1462 : : }
1463 : :
1464 [ + + ]: 1138 : if (raid_bdev_find_by_name(name) != NULL) {
1465 : 4 : SPDK_ERRLOG("Duplicate raid bdev name found: %s\n", name);
1466 : 4 : return -EEXIST;
1467 : : }
1468 : :
1469 [ + + ]: 1134 : if (level == RAID1) {
1470 [ - + ]: 483 : if (strip_size != 0) {
1471 : 0 : SPDK_ERRLOG("Strip size is not supported by raid1\n");
1472 : 0 : return -EINVAL;
1473 : : }
1474 [ + + ]: 651 : } else if (spdk_u32_is_pow2(strip_size) == false) {
1475 : 4 : SPDK_ERRLOG("Invalid strip size %" PRIu32 "\n", strip_size);
1476 : 4 : return -EINVAL;
1477 : : }
1478 : :
1479 : 1130 : module = raid_bdev_module_find(level);
1480 [ + + ]: 1130 : if (module == NULL) {
1481 : 4 : SPDK_ERRLOG("Unsupported raid level '%d'\n", level);
1482 : 4 : return -EINVAL;
1483 : : }
1484 : :
1485 [ - + ]: 1126 : assert(module->base_bdevs_min != 0);
1486 [ - + ]: 1126 : if (num_base_bdevs < module->base_bdevs_min) {
1487 : 0 : SPDK_ERRLOG("At least %u base devices required for %s\n",
1488 : : module->base_bdevs_min,
1489 : : raid_bdev_level_to_str(level));
1490 : 0 : return -EINVAL;
1491 : : }
1492 : :
1493 [ + + + - ]: 1126 : switch (module->base_bdevs_constraint.type) {
1494 : 84 : case CONSTRAINT_MAX_BASE_BDEVS_REMOVED:
1495 : 84 : min_operational = num_base_bdevs - module->base_bdevs_constraint.value;
1496 : 84 : break;
1497 : 483 : case CONSTRAINT_MIN_BASE_BDEVS_OPERATIONAL:
1498 : 483 : min_operational = module->base_bdevs_constraint.value;
1499 : 483 : break;
1500 : 559 : case CONSTRAINT_UNSET:
1501 [ - + ]: 559 : if (module->base_bdevs_constraint.value != 0) {
1502 : 0 : SPDK_ERRLOG("Unexpected constraint value '%u' provided for raid bdev '%s'.\n",
1503 : : (uint8_t)module->base_bdevs_constraint.value, name);
1504 : 0 : return -EINVAL;
1505 : : }
1506 : 559 : min_operational = num_base_bdevs;
1507 : 559 : break;
1508 : 0 : default:
1509 : 0 : SPDK_ERRLOG("Unrecognised constraint type '%u' in module for raid level '%s'.\n",
1510 : : (uint8_t)module->base_bdevs_constraint.type,
1511 : : raid_bdev_level_to_str(module->level));
1512 : 0 : return -EINVAL;
1513 : : };
1514 : :
1515 [ + - - + ]: 1126 : if (min_operational == 0 || min_operational > num_base_bdevs) {
1516 : 0 : SPDK_ERRLOG("Wrong constraint value for raid level '%s'.\n",
1517 : : raid_bdev_level_to_str(module->level));
1518 : 0 : return -EINVAL;
1519 : : }
1520 : :
1521 : 1126 : raid_bdev = calloc(1, sizeof(*raid_bdev));
1522 [ - + ]: 1126 : if (!raid_bdev) {
1523 : 0 : SPDK_ERRLOG("Unable to allocate memory for raid bdev\n");
1524 : 0 : return -ENOMEM;
1525 : : }
1526 : :
1527 : 1126 : raid_bdev->module = module;
1528 : 1126 : raid_bdev->num_base_bdevs = num_base_bdevs;
1529 : 1126 : raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs,
1530 : : sizeof(struct raid_base_bdev_info));
1531 [ - + ]: 1126 : if (!raid_bdev->base_bdev_info) {
1532 : 0 : SPDK_ERRLOG("Unable able to allocate base bdev info\n");
1533 : 0 : raid_bdev_free(raid_bdev);
1534 : 0 : return -ENOMEM;
1535 : : }
1536 : :
1537 [ + + ]: 6098 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1538 : 4972 : base_info->raid_bdev = raid_bdev;
1539 : : }
1540 : :
1541 : : /* strip_size_kb is from the rpc param. strip_size is in blocks and used
1542 : : * internally and set later.
1543 : : */
1544 : 1126 : raid_bdev->strip_size = 0;
1545 : 1126 : raid_bdev->strip_size_kb = strip_size;
1546 : 1126 : raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
1547 : 1126 : raid_bdev->level = level;
1548 : 1126 : raid_bdev->min_base_bdevs_operational = min_operational;
1549 : 1126 : raid_bdev->superblock_enabled = superblock_enabled;
1550 : :
1551 : 1126 : raid_bdev_gen = &raid_bdev->bdev;
1552 : :
1553 [ - + ]: 1126 : raid_bdev_gen->name = strdup(name);
1554 [ - + ]: 1126 : if (!raid_bdev_gen->name) {
1555 : 0 : SPDK_ERRLOG("Unable to allocate name for raid\n");
1556 : 0 : raid_bdev_free(raid_bdev);
1557 : 0 : return -ENOMEM;
1558 : : }
1559 : :
1560 : 1126 : raid_bdev_gen->product_name = "Raid Volume";
1561 : 1126 : raid_bdev_gen->ctxt = raid_bdev;
1562 : 1126 : raid_bdev_gen->fn_table = &g_raid_bdev_fn_table;
1563 : 1126 : raid_bdev_gen->module = &g_raid_if;
1564 : 1126 : raid_bdev_gen->write_cache = 0;
1565 : 1126 : spdk_uuid_copy(&raid_bdev_gen->uuid, uuid);
1566 : :
1567 : 1126 : TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link);
1568 : :
1569 : 1126 : *raid_bdev_out = raid_bdev;
1570 : :
1571 : 1126 : return 0;
1572 : : }
1573 : :
1574 : : /*
1575 : : * brief:
1576 : : * raid_bdev_create allocates raid bdev based on passed configuration
1577 : : * params:
1578 : : * name - name for raid bdev
1579 : : * strip_size - strip size in KB
1580 : : * num_base_bdevs - number of base bdevs
1581 : : * level - raid level
1582 : : * superblock_enabled - true if raid should have superblock
1583 : : * uuid - uuid to set for the bdev
1584 : : * raid_bdev_out - the created raid bdev
1585 : : * returns:
1586 : : * 0 - success
1587 : : * non zero - failure
1588 : : */
1589 : : int
1590 : 939 : raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs,
1591 : : enum raid_level level, bool superblock_enabled, const struct spdk_uuid *uuid,
1592 : : struct raid_bdev **raid_bdev_out)
1593 : : {
1594 : 763 : struct raid_bdev *raid_bdev;
1595 : : int rc;
1596 : :
1597 [ - + ]: 939 : assert(uuid != NULL);
1598 : :
1599 : 939 : rc = _raid_bdev_create(name, strip_size, num_base_bdevs, level, superblock_enabled, uuid,
1600 : : &raid_bdev);
1601 [ + + ]: 939 : if (rc != 0) {
1602 : 12 : return rc;
1603 : : }
1604 : :
1605 [ + + + - ]: 927 : if (superblock_enabled && spdk_uuid_is_null(uuid)) {
1606 : : /* we need to have the uuid to store in the superblock before the bdev is registered */
1607 : 417 : spdk_uuid_generate(&raid_bdev->bdev.uuid);
1608 : : }
1609 : :
1610 : 927 : raid_bdev->num_base_bdevs_operational = num_base_bdevs;
1611 : :
1612 : 927 : *raid_bdev_out = raid_bdev;
1613 : :
1614 : 927 : return 0;
1615 : : }
1616 : :
1617 : : static void
1618 : 738 : _raid_bdev_unregistering_cont(void *ctx)
1619 : : {
1620 : 738 : struct raid_bdev *raid_bdev = ctx;
1621 : :
1622 : 738 : spdk_bdev_close(raid_bdev->self_desc);
1623 : 738 : raid_bdev->self_desc = NULL;
1624 : 738 : }
1625 : :
1626 : : static void
1627 : 738 : raid_bdev_unregistering_cont(void *ctx)
1628 : : {
1629 : 738 : spdk_thread_exec_msg(spdk_thread_get_app_thread(), _raid_bdev_unregistering_cont, ctx);
1630 : 738 : }
1631 : :
1632 : : static int
1633 : 119 : raid_bdev_process_add_finish_action(struct raid_bdev_process *process, spdk_msg_fn cb, void *cb_ctx)
1634 : : {
1635 : : struct raid_process_finish_action *finish_action;
1636 : :
1637 [ - + ]: 119 : assert(spdk_get_thread() == process->thread);
1638 [ - + ]: 119 : assert(process->state < RAID_PROCESS_STATE_STOPPED);
1639 : :
1640 : 119 : finish_action = calloc(1, sizeof(*finish_action));
1641 [ - + ]: 119 : if (finish_action == NULL) {
1642 : 0 : return -ENOMEM;
1643 : : }
1644 : :
1645 : 119 : finish_action->cb = cb;
1646 : 119 : finish_action->cb_ctx = cb_ctx;
1647 : :
1648 : 119 : TAILQ_INSERT_TAIL(&process->finish_actions, finish_action, link);
1649 : :
1650 : 119 : return 0;
1651 : : }
1652 : :
1653 : : static void
1654 : 0 : raid_bdev_unregistering_stop_process(void *ctx)
1655 : : {
1656 : 0 : struct raid_bdev_process *process = ctx;
1657 : 0 : struct raid_bdev *raid_bdev = process->raid_bdev;
1658 : : int rc;
1659 : :
1660 : 0 : process->state = RAID_PROCESS_STATE_STOPPING;
1661 [ # # ]: 0 : if (process->status == 0) {
1662 : 0 : process->status = -ECANCELED;
1663 : : }
1664 : :
1665 : 0 : rc = raid_bdev_process_add_finish_action(process, raid_bdev_unregistering_cont, raid_bdev);
1666 [ # # ]: 0 : if (rc != 0) {
1667 : 0 : SPDK_ERRLOG("Failed to add raid bdev '%s' process finish action: %s\n",
1668 : : raid_bdev->bdev.name, spdk_strerror(-rc));
1669 : : }
1670 : 0 : }
1671 : :
1672 : : static void
1673 : 743 : raid_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx)
1674 : : {
1675 : 743 : struct raid_bdev *raid_bdev = event_ctx;
1676 : :
1677 [ + + ]: 743 : if (type == SPDK_BDEV_EVENT_REMOVE) {
1678 [ - + ]: 738 : if (raid_bdev->process != NULL) {
1679 : 0 : spdk_thread_send_msg(raid_bdev->process->thread, raid_bdev_unregistering_stop_process,
1680 : 0 : raid_bdev->process);
1681 : : } else {
1682 : 738 : raid_bdev_unregistering_cont(raid_bdev);
1683 : : }
1684 : : }
1685 : 743 : }
1686 : :
1687 : : static void
1688 : 790 : raid_bdev_configure_cont(struct raid_bdev *raid_bdev)
1689 : : {
1690 : 790 : struct spdk_bdev *raid_bdev_gen = &raid_bdev->bdev;
1691 : : int rc;
1692 : :
1693 : 790 : raid_bdev->state = RAID_BDEV_STATE_ONLINE;
1694 [ + + + + ]: 790 : SPDK_DEBUGLOG(bdev_raid, "io device register %p\n", raid_bdev);
1695 [ + + + + ]: 790 : SPDK_DEBUGLOG(bdev_raid, "blockcnt %" PRIu64 ", blocklen %u\n",
1696 : : raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen);
1697 : 790 : spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb,
1698 : : sizeof(struct raid_bdev_io_channel),
1699 : 790 : raid_bdev_gen->name);
1700 : 790 : rc = spdk_bdev_register(raid_bdev_gen);
1701 [ - + ]: 790 : if (rc != 0) {
1702 : 0 : SPDK_ERRLOG("Failed to register raid bdev '%s': %s\n",
1703 : : raid_bdev_gen->name, spdk_strerror(-rc));
1704 : 0 : goto err;
1705 : : }
1706 : :
1707 : : /*
1708 : : * Open the bdev internally to delay unregistering if we need to stop a background process
1709 : : * first. The process may still need to unquiesce a range but it will fail because the
1710 : : * bdev's internal.spinlock is destroyed by the time the destruct callback is reached.
1711 : : * During application shutdown, bdevs automatically get unregistered by the bdev layer
1712 : : * so this is the only way currently to do this correctly.
1713 : : * TODO: try to handle this correctly in bdev layer instead.
1714 : : */
1715 : 790 : rc = spdk_bdev_open_ext(raid_bdev_gen->name, false, raid_bdev_event_cb, raid_bdev,
1716 : : &raid_bdev->self_desc);
1717 [ - + ]: 790 : if (rc != 0) {
1718 : 0 : SPDK_ERRLOG("Failed to open raid bdev '%s': %s\n",
1719 : : raid_bdev_gen->name, spdk_strerror(-rc));
1720 : 0 : spdk_bdev_unregister(raid_bdev_gen, NULL, NULL);
1721 : 0 : goto err;
1722 : : }
1723 : :
1724 [ + + + + ]: 790 : SPDK_DEBUGLOG(bdev_raid, "raid bdev generic %p\n", raid_bdev_gen);
1725 [ + + + + ]: 790 : SPDK_DEBUGLOG(bdev_raid, "raid bdev is created with name %s, raid_bdev %p\n",
1726 : : raid_bdev_gen->name, raid_bdev);
1727 : 790 : return;
1728 : 0 : err:
1729 [ # # ]: 0 : if (raid_bdev->module->stop != NULL) {
1730 : 0 : raid_bdev->module->stop(raid_bdev);
1731 : : }
1732 : 0 : spdk_io_device_unregister(raid_bdev, NULL);
1733 : 0 : raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
1734 : : }
1735 : :
1736 : : static void
1737 : 454 : raid_bdev_configure_write_sb_cb(int status, struct raid_bdev *raid_bdev, void *ctx)
1738 : : {
1739 [ + - ]: 454 : if (status == 0) {
1740 : 454 : raid_bdev_configure_cont(raid_bdev);
1741 : : } else {
1742 : 0 : SPDK_ERRLOG("Failed to write raid bdev '%s' superblock: %s\n",
1743 : : raid_bdev->bdev.name, spdk_strerror(-status));
1744 [ # # ]: 0 : if (raid_bdev->module->stop != NULL) {
1745 : 0 : raid_bdev->module->stop(raid_bdev);
1746 : : }
1747 : : }
1748 : 454 : }
1749 : :
1750 : : /*
1751 : : * brief:
1752 : : * If raid bdev config is complete, then only register the raid bdev to
1753 : : * bdev layer and remove this raid bdev from configuring list and
1754 : : * insert the raid bdev to configured list
1755 : : * params:
1756 : : * raid_bdev - pointer to raid bdev
1757 : : * returns:
1758 : : * 0 - success
1759 : : * non zero - failure
1760 : : */
1761 : : static int
1762 : 790 : raid_bdev_configure(struct raid_bdev *raid_bdev)
1763 : : {
1764 : 790 : uint32_t data_block_size = spdk_bdev_get_data_block_size(&raid_bdev->bdev);
1765 : : int rc;
1766 : :
1767 [ - + ]: 790 : assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING);
1768 [ - + ]: 790 : assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs_operational);
1769 [ - + ]: 790 : assert(raid_bdev->bdev.blocklen > 0);
1770 : :
1771 : : /* The strip_size_kb is read in from user in KB. Convert to blocks here for
1772 : : * internal use.
1773 : : */
1774 [ - + ]: 790 : raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / data_block_size;
1775 [ + + - + ]: 790 : if (raid_bdev->strip_size == 0 && raid_bdev->level != RAID1) {
1776 : 0 : SPDK_ERRLOG("Strip size cannot be smaller than the device block size\n");
1777 : 0 : return -EINVAL;
1778 : : }
1779 : 790 : raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size);
1780 : :
1781 : 790 : rc = raid_bdev->module->start(raid_bdev);
1782 [ - + ]: 790 : if (rc != 0) {
1783 : 0 : SPDK_ERRLOG("raid module startup callback failed\n");
1784 : 0 : return rc;
1785 : : }
1786 : :
1787 [ + + + + ]: 790 : if (raid_bdev->superblock_enabled) {
1788 [ + + ]: 454 : if (raid_bdev->sb == NULL) {
1789 : 289 : rc = raid_bdev_alloc_superblock(raid_bdev, data_block_size);
1790 [ + - ]: 289 : if (rc == 0) {
1791 : 289 : raid_bdev_init_superblock(raid_bdev);
1792 : : }
1793 : : } else {
1794 [ - + ]: 165 : assert(spdk_uuid_compare(&raid_bdev->sb->uuid, &raid_bdev->bdev.uuid) == 0);
1795 [ - + ]: 165 : if (raid_bdev->sb->block_size != data_block_size) {
1796 : 0 : SPDK_ERRLOG("blocklen does not match value in superblock\n");
1797 : 0 : rc = -EINVAL;
1798 : : }
1799 [ - + ]: 165 : if (raid_bdev->sb->raid_size != raid_bdev->bdev.blockcnt) {
1800 : 0 : SPDK_ERRLOG("blockcnt does not match value in superblock\n");
1801 : 0 : rc = -EINVAL;
1802 : : }
1803 : : }
1804 : :
1805 [ - + ]: 454 : if (rc != 0) {
1806 [ # # ]: 0 : if (raid_bdev->module->stop != NULL) {
1807 : 0 : raid_bdev->module->stop(raid_bdev);
1808 : : }
1809 : 0 : return rc;
1810 : : }
1811 : :
1812 : 454 : raid_bdev_write_superblock(raid_bdev, raid_bdev_configure_write_sb_cb, NULL);
1813 : : } else {
1814 : 336 : raid_bdev_configure_cont(raid_bdev);
1815 : : }
1816 : :
1817 : 790 : return 0;
1818 : : }
1819 : :
1820 : : /*
1821 : : * brief:
1822 : : * If raid bdev is online and registered, change the bdev state to
1823 : : * configuring and unregister this raid device. Queue this raid device
1824 : : * in configuring list
1825 : : * params:
1826 : : * raid_bdev - pointer to raid bdev
1827 : : * cb_fn - callback function
1828 : : * cb_arg - argument to callback function
1829 : : * returns:
1830 : : * none
1831 : : */
1832 : : static void
1833 : 522 : raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn,
1834 : : void *cb_arg)
1835 : : {
1836 [ - + ]: 522 : if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) {
1837 [ # # ]: 0 : if (cb_fn) {
1838 : 0 : cb_fn(cb_arg, 0);
1839 : : }
1840 : 0 : return;
1841 : : }
1842 : :
1843 : 522 : raid_bdev->state = RAID_BDEV_STATE_OFFLINE;
1844 [ + + + + ]: 522 : SPDK_DEBUGLOG(bdev_raid, "raid bdev state changing from online to offline\n");
1845 : :
1846 : 522 : spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg);
1847 : : }
1848 : :
1849 : : /*
1850 : : * brief:
1851 : : * raid_bdev_find_base_info_by_bdev function finds the base bdev info by bdev.
1852 : : * params:
1853 : : * base_bdev - pointer to base bdev
1854 : : * returns:
1855 : : * base bdev info if found, otherwise NULL.
1856 : : */
1857 : : static struct raid_base_bdev_info *
1858 : 8456 : raid_bdev_find_base_info_by_bdev(struct spdk_bdev *base_bdev)
1859 : : {
1860 : : struct raid_bdev *raid_bdev;
1861 : : struct raid_base_bdev_info *base_info;
1862 : :
1863 [ + + ]: 10245 : TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
1864 [ + + ]: 8202 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1865 [ + + + + ]: 10953 : if (base_info->desc != NULL &&
1866 : 4540 : spdk_bdev_desc_get_bdev(base_info->desc) == base_bdev) {
1867 : 831 : return base_info;
1868 : : }
1869 : : }
1870 : : }
1871 : :
1872 : 7625 : return NULL;
1873 : : }
1874 : :
1875 : : static void
1876 : 406 : raid_bdev_remove_base_bdev_done(struct raid_base_bdev_info *base_info, int status)
1877 : : {
1878 : 406 : struct raid_bdev *raid_bdev = base_info->raid_bdev;
1879 : :
1880 [ - + - + ]: 406 : assert(base_info->remove_scheduled);
1881 : 406 : base_info->remove_scheduled = false;
1882 : :
1883 [ + - ]: 406 : if (status == 0) {
1884 : 406 : raid_bdev->num_base_bdevs_operational--;
1885 [ + + ]: 406 : if (raid_bdev->num_base_bdevs_operational < raid_bdev->min_base_bdevs_operational) {
1886 : : /* There is not enough base bdevs to keep the raid bdev operational. */
1887 : 53 : raid_bdev_deconfigure(raid_bdev, base_info->remove_cb, base_info->remove_cb_ctx);
1888 : 53 : return;
1889 : : }
1890 : : }
1891 : :
1892 [ + + ]: 353 : if (base_info->remove_cb != NULL) {
1893 : 170 : base_info->remove_cb(base_info->remove_cb_ctx, status);
1894 : : }
1895 : : }
1896 : :
1897 : : static void
1898 : 406 : raid_bdev_remove_base_bdev_on_unquiesced(void *ctx, int status)
1899 : : {
1900 : 406 : struct raid_base_bdev_info *base_info = ctx;
1901 : 406 : struct raid_bdev *raid_bdev = base_info->raid_bdev;
1902 : :
1903 [ - + ]: 406 : if (status != 0) {
1904 : 0 : SPDK_ERRLOG("Failed to unquiesce raid bdev %s: %s\n",
1905 : : raid_bdev->bdev.name, spdk_strerror(-status));
1906 : : }
1907 : :
1908 : 406 : raid_bdev_remove_base_bdev_done(base_info, status);
1909 : 406 : }
1910 : :
1911 : : static void
1912 : 71 : raid_bdev_channel_remove_base_bdev(struct spdk_io_channel_iter *i)
1913 : : {
1914 : 71 : struct raid_base_bdev_info *base_info = spdk_io_channel_iter_get_ctx(i);
1915 : 71 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
1916 : 71 : struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch);
1917 : 71 : uint8_t idx = raid_bdev_base_bdev_slot(base_info);
1918 : :
1919 [ + + + - ]: 71 : SPDK_DEBUGLOG(bdev_raid, "slot: %u raid_ch: %p\n", idx, raid_ch);
1920 : :
1921 [ + + ]: 71 : if (raid_ch->base_channel[idx] != NULL) {
1922 : 55 : spdk_put_io_channel(raid_ch->base_channel[idx]);
1923 : 55 : raid_ch->base_channel[idx] = NULL;
1924 : : }
1925 : :
1926 [ + + ]: 71 : if (raid_ch->process.ch_processed != NULL) {
1927 : 24 : raid_ch->process.ch_processed->base_channel[idx] = NULL;
1928 : : }
1929 : :
1930 : 71 : spdk_for_each_channel_continue(i, 0);
1931 : 71 : }
1932 : :
1933 : : static void
1934 : 406 : raid_bdev_channels_remove_base_bdev_done(struct spdk_io_channel_iter *i, int status)
1935 : : {
1936 : 406 : struct raid_base_bdev_info *base_info = spdk_io_channel_iter_get_ctx(i);
1937 : 406 : struct raid_bdev *raid_bdev = base_info->raid_bdev;
1938 : :
1939 : 406 : raid_bdev_free_base_bdev_resource(base_info);
1940 : :
1941 : 406 : spdk_bdev_unquiesce(&raid_bdev->bdev, &g_raid_if, raid_bdev_remove_base_bdev_on_unquiesced,
1942 : : base_info);
1943 : 406 : }
1944 : :
1945 : : static void
1946 : 406 : raid_bdev_remove_base_bdev_cont(struct raid_base_bdev_info *base_info)
1947 : : {
1948 : 406 : raid_bdev_deconfigure_base_bdev(base_info);
1949 : :
1950 : 406 : spdk_for_each_channel(base_info->raid_bdev, raid_bdev_channel_remove_base_bdev, base_info,
1951 : : raid_bdev_channels_remove_base_bdev_done);
1952 : 406 : }
1953 : :
1954 : : static void
1955 : 206 : raid_bdev_remove_base_bdev_write_sb_cb(int status, struct raid_bdev *raid_bdev, void *ctx)
1956 : : {
1957 : 206 : struct raid_base_bdev_info *base_info = ctx;
1958 : :
1959 [ - + ]: 206 : if (status != 0) {
1960 : 0 : SPDK_ERRLOG("Failed to write raid bdev '%s' superblock: %s\n",
1961 : : raid_bdev->bdev.name, spdk_strerror(-status));
1962 : 0 : raid_bdev_remove_base_bdev_done(base_info, status);
1963 : 0 : return;
1964 : : }
1965 : :
1966 : 206 : raid_bdev_remove_base_bdev_cont(base_info);
1967 : : }
1968 : :
1969 : : static void
1970 : 406 : raid_bdev_remove_base_bdev_on_quiesced(void *ctx, int status)
1971 : : {
1972 : 406 : struct raid_base_bdev_info *base_info = ctx;
1973 : 406 : struct raid_bdev *raid_bdev = base_info->raid_bdev;
1974 : :
1975 [ - + ]: 406 : if (status != 0) {
1976 : 0 : SPDK_ERRLOG("Failed to quiesce raid bdev %s: %s\n",
1977 : : raid_bdev->bdev.name, spdk_strerror(-status));
1978 : 0 : raid_bdev_remove_base_bdev_done(base_info, status);
1979 : 0 : return;
1980 : : }
1981 : :
1982 [ + + ]: 406 : if (raid_bdev->sb) {
1983 : 305 : struct raid_bdev_superblock *sb = raid_bdev->sb;
1984 : 305 : uint8_t slot = raid_bdev_base_bdev_slot(base_info);
1985 : : uint8_t i;
1986 : :
1987 [ + + ]: 646 : for (i = 0; i < sb->base_bdevs_size; i++) {
1988 : 547 : struct raid_bdev_sb_base_bdev *sb_base_bdev = &sb->base_bdevs[i];
1989 : :
1990 [ + + ]: 547 : if (sb_base_bdev->state == RAID_SB_BASE_BDEV_CONFIGURED &&
1991 [ + + ]: 355 : sb_base_bdev->slot == slot) {
1992 [ + + + + ]: 206 : if (base_info->is_failed) {
1993 : 15 : sb_base_bdev->state = RAID_SB_BASE_BDEV_FAILED;
1994 : : } else {
1995 : 191 : sb_base_bdev->state = RAID_SB_BASE_BDEV_MISSING;
1996 : : }
1997 : :
1998 : 206 : raid_bdev_write_superblock(raid_bdev, raid_bdev_remove_base_bdev_write_sb_cb, base_info);
1999 : 206 : return;
2000 : : }
2001 : : }
2002 : : }
2003 : :
2004 : 200 : raid_bdev_remove_base_bdev_cont(base_info);
2005 : : }
2006 : :
2007 : : static int
2008 : 406 : raid_bdev_remove_base_bdev_quiesce(struct raid_base_bdev_info *base_info)
2009 : : {
2010 [ - + ]: 406 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
2011 : :
2012 : 406 : return spdk_bdev_quiesce(&base_info->raid_bdev->bdev, &g_raid_if,
2013 : : raid_bdev_remove_base_bdev_on_quiesced, base_info);
2014 : : }
2015 : :
2016 : : struct raid_bdev_process_base_bdev_remove_ctx {
2017 : : struct raid_bdev_process *process;
2018 : : struct raid_base_bdev_info *base_info;
2019 : : uint8_t num_base_bdevs_operational;
2020 : : };
2021 : :
2022 : : static void
2023 : 135 : _raid_bdev_process_base_bdev_remove_cont(void *ctx)
2024 : : {
2025 : 135 : struct raid_base_bdev_info *base_info = ctx;
2026 : : int ret;
2027 : :
2028 : 135 : ret = raid_bdev_remove_base_bdev_quiesce(base_info);
2029 [ - + ]: 135 : if (ret != 0) {
2030 : 0 : raid_bdev_remove_base_bdev_done(base_info, ret);
2031 : : }
2032 : 135 : }
2033 : :
2034 : : static void
2035 : 135 : raid_bdev_process_base_bdev_remove_cont(void *_ctx)
2036 : : {
2037 : 135 : struct raid_bdev_process_base_bdev_remove_ctx *ctx = _ctx;
2038 : 135 : struct raid_base_bdev_info *base_info = ctx->base_info;
2039 : :
2040 : 135 : free(ctx);
2041 : :
2042 : 135 : spdk_thread_send_msg(spdk_thread_get_app_thread(), _raid_bdev_process_base_bdev_remove_cont,
2043 : : base_info);
2044 : 135 : }
2045 : :
2046 : : static void
2047 : 135 : _raid_bdev_process_base_bdev_remove(void *_ctx)
2048 : : {
2049 : 135 : struct raid_bdev_process_base_bdev_remove_ctx *ctx = _ctx;
2050 : 135 : struct raid_bdev_process *process = ctx->process;
2051 : : int ret;
2052 : :
2053 [ + + ]: 135 : if (ctx->base_info != process->target &&
2054 [ + - ]: 16 : ctx->num_base_bdevs_operational > process->raid_bdev->min_base_bdevs_operational) {
2055 : : /* process doesn't need to be stopped */
2056 : 16 : raid_bdev_process_base_bdev_remove_cont(ctx);
2057 : 16 : return;
2058 : : }
2059 : :
2060 [ + - + + ]: 119 : assert(process->state > RAID_PROCESS_STATE_INIT &&
2061 : : process->state < RAID_PROCESS_STATE_STOPPED);
2062 : :
2063 : 119 : ret = raid_bdev_process_add_finish_action(process, raid_bdev_process_base_bdev_remove_cont, ctx);
2064 [ - + ]: 119 : if (ret != 0) {
2065 : 0 : raid_bdev_remove_base_bdev_done(ctx->base_info, ret);
2066 : 0 : free(ctx);
2067 : 0 : return;
2068 : : }
2069 : :
2070 : 119 : process->state = RAID_PROCESS_STATE_STOPPING;
2071 : :
2072 [ + - ]: 119 : if (process->status == 0) {
2073 : 119 : process->status = -ENODEV;
2074 : : }
2075 : : }
2076 : :
2077 : : static int
2078 : 135 : raid_bdev_process_base_bdev_remove(struct raid_bdev_process *process,
2079 : : struct raid_base_bdev_info *base_info)
2080 : : {
2081 : : struct raid_bdev_process_base_bdev_remove_ctx *ctx;
2082 : :
2083 [ - + ]: 135 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
2084 : :
2085 : 135 : ctx = calloc(1, sizeof(*ctx));
2086 [ - + ]: 135 : if (ctx == NULL) {
2087 : 0 : return -ENOMEM;
2088 : : }
2089 : :
2090 : : /*
2091 : : * We have to send the process and num_base_bdevs_operational in the message ctx
2092 : : * because the process thread should not access raid_bdev's properties. Particularly,
2093 : : * raid_bdev->process may be cleared by the time the message is handled, but ctx->process
2094 : : * will still be valid until the process is fully stopped.
2095 : : */
2096 : 135 : ctx->base_info = base_info;
2097 : 135 : ctx->process = process;
2098 : : /*
2099 : : * raid_bdev->num_base_bdevs_operational can't be used here because it is decremented
2100 : : * after the removal and more than one base bdev may be removed at the same time
2101 : : */
2102 [ + + ]: 525 : RAID_FOR_EACH_BASE_BDEV(process->raid_bdev, base_info) {
2103 [ + + + + : 390 : if (base_info->is_configured && !base_info->remove_scheduled) {
- + + + ]
2104 : 239 : ctx->num_base_bdevs_operational++;
2105 : : }
2106 : : }
2107 : :
2108 : 135 : spdk_thread_send_msg(process->thread, _raid_bdev_process_base_bdev_remove, ctx);
2109 : :
2110 : 135 : return 0;
2111 : : }
2112 : :
2113 : : static int
2114 : 955 : _raid_bdev_remove_base_bdev(struct raid_base_bdev_info *base_info,
2115 : : raid_base_bdev_cb cb_fn, void *cb_ctx)
2116 : : {
2117 : 955 : struct raid_bdev *raid_bdev = base_info->raid_bdev;
2118 : 955 : int ret = 0;
2119 : :
2120 [ + + + - ]: 955 : SPDK_DEBUGLOG(bdev_raid, "%s\n", base_info->name);
2121 : :
2122 [ - + ]: 955 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
2123 : :
2124 [ + + + + : 955 : if (base_info->remove_scheduled || !base_info->is_configured) {
- + - + ]
2125 : 119 : return -ENODEV;
2126 : : }
2127 : :
2128 [ - + ]: 836 : assert(base_info->desc);
2129 : 836 : base_info->remove_scheduled = true;
2130 : :
2131 [ + + ]: 836 : if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) {
2132 : : /*
2133 : : * As raid bdev is not registered yet or already unregistered,
2134 : : * so cleanup should be done here itself.
2135 : : *
2136 : : * Removing a base bdev at this stage does not change the number of operational
2137 : : * base bdevs, only the number of discovered base bdevs.
2138 : : */
2139 : 370 : raid_bdev_free_base_bdev_resource(base_info);
2140 : 370 : base_info->remove_scheduled = false;
2141 [ + + ]: 370 : if (raid_bdev->num_base_bdevs_discovered == 0 &&
2142 [ + - ]: 68 : raid_bdev->state == RAID_BDEV_STATE_OFFLINE) {
2143 : : /* There is no base bdev for this raid, so free the raid device. */
2144 : 68 : raid_bdev_cleanup_and_free(raid_bdev);
2145 : : }
2146 [ + + ]: 370 : if (cb_fn != NULL) {
2147 : 136 : cb_fn(cb_ctx, 0);
2148 : : }
2149 [ + + ]: 466 : } else if (raid_bdev->min_base_bdevs_operational == raid_bdev->num_base_bdevs) {
2150 : : /* This raid bdev does not tolerate removing a base bdev. */
2151 : 60 : raid_bdev->num_base_bdevs_operational--;
2152 : 60 : raid_bdev_deconfigure(raid_bdev, cb_fn, cb_ctx);
2153 : : } else {
2154 : 406 : base_info->remove_cb = cb_fn;
2155 : 406 : base_info->remove_cb_ctx = cb_ctx;
2156 : :
2157 [ + + ]: 406 : if (raid_bdev->process != NULL) {
2158 : 135 : ret = raid_bdev_process_base_bdev_remove(raid_bdev->process, base_info);
2159 : : } else {
2160 : 271 : ret = raid_bdev_remove_base_bdev_quiesce(base_info);
2161 : : }
2162 : :
2163 [ - + ]: 406 : if (ret != 0) {
2164 : 0 : base_info->remove_scheduled = false;
2165 : : }
2166 : : }
2167 : :
2168 : 836 : return ret;
2169 : : }
2170 : :
2171 : : /*
2172 : : * brief:
2173 : : * raid_bdev_remove_base_bdev function is called by below layers when base_bdev
2174 : : * is removed. This function checks if this base bdev is part of any raid bdev
2175 : : * or not. If yes, it takes necessary action on that particular raid bdev.
2176 : : * params:
2177 : : * base_bdev - pointer to base bdev which got removed
2178 : : * cb_fn - callback function
2179 : : * cb_arg - argument to callback function
2180 : : * returns:
2181 : : * 0 - success
2182 : : * non zero - failure
2183 : : */
2184 : : int
2185 : 821 : raid_bdev_remove_base_bdev(struct spdk_bdev *base_bdev, raid_base_bdev_cb cb_fn, void *cb_ctx)
2186 : : {
2187 : : struct raid_base_bdev_info *base_info;
2188 : :
2189 : : /* Find the raid_bdev which has claimed this base_bdev */
2190 : 821 : base_info = raid_bdev_find_base_info_by_bdev(base_bdev);
2191 [ - + ]: 821 : if (!base_info) {
2192 : 0 : SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name);
2193 : 0 : return -ENODEV;
2194 : : }
2195 : :
2196 : 821 : return _raid_bdev_remove_base_bdev(base_info, cb_fn, cb_ctx);
2197 : : }
2198 : :
2199 : : static void
2200 : 15 : raid_bdev_fail_base_remove_cb(void *ctx, int status)
2201 : : {
2202 : 15 : struct raid_base_bdev_info *base_info = ctx;
2203 : :
2204 [ - + ]: 15 : if (status != 0) {
2205 : 0 : SPDK_WARNLOG("Failed to remove base bdev %s\n", base_info->name);
2206 : 0 : base_info->is_failed = false;
2207 : : }
2208 : 15 : }
2209 : :
2210 : : static void
2211 : 15 : _raid_bdev_fail_base_bdev(void *ctx)
2212 : : {
2213 : 15 : struct raid_base_bdev_info *base_info = ctx;
2214 : : int rc;
2215 : :
2216 [ - + - + ]: 15 : if (base_info->is_failed) {
2217 : 0 : return;
2218 : : }
2219 : 15 : base_info->is_failed = true;
2220 : :
2221 : 15 : SPDK_NOTICELOG("Failing base bdev in slot %d ('%s') of raid bdev '%s'\n",
2222 : : raid_bdev_base_bdev_slot(base_info), base_info->name, base_info->raid_bdev->bdev.name);
2223 : :
2224 : 15 : rc = _raid_bdev_remove_base_bdev(base_info, raid_bdev_fail_base_remove_cb, base_info);
2225 [ - + ]: 15 : if (rc != 0) {
2226 : 0 : raid_bdev_fail_base_remove_cb(base_info, rc);
2227 : : }
2228 : : }
2229 : :
2230 : : void
2231 : 15 : raid_bdev_fail_base_bdev(struct raid_base_bdev_info *base_info)
2232 : : {
2233 : 15 : spdk_thread_exec_msg(spdk_thread_get_app_thread(), _raid_bdev_fail_base_bdev, base_info);
2234 : 15 : }
2235 : :
2236 : : static void
2237 : 0 : raid_bdev_resize_write_sb_cb(int status, struct raid_bdev *raid_bdev, void *ctx)
2238 : : {
2239 [ # # ]: 0 : if (status != 0) {
2240 : 0 : SPDK_ERRLOG("Failed to write raid bdev '%s' superblock after resizing the bdev: %s\n",
2241 : : raid_bdev->bdev.name, spdk_strerror(-status));
2242 : : }
2243 : 0 : }
2244 : :
2245 : : /*
2246 : : * brief:
2247 : : * raid_bdev_resize_base_bdev function is called by below layers when base_bdev
2248 : : * is resized. This function checks if the smallest size of the base_bdevs is changed.
2249 : : * If yes, call module handler to resize the raid_bdev if implemented.
2250 : : * params:
2251 : : * base_bdev - pointer to base bdev which got resized.
2252 : : * returns:
2253 : : * none
2254 : : */
2255 : : static void
2256 : 10 : raid_bdev_resize_base_bdev(struct spdk_bdev *base_bdev)
2257 : : {
2258 : : struct raid_bdev *raid_bdev;
2259 : : struct raid_base_bdev_info *base_info;
2260 : : uint64_t blockcnt_old;
2261 : :
2262 [ + + + - ]: 10 : SPDK_DEBUGLOG(bdev_raid, "raid_bdev_resize_base_bdev\n");
2263 : :
2264 : 10 : base_info = raid_bdev_find_base_info_by_bdev(base_bdev);
2265 : :
2266 : : /* Find the raid_bdev which has claimed this base_bdev */
2267 [ - + ]: 10 : if (!base_info) {
2268 : 0 : SPDK_ERRLOG("raid_bdev whose base_bdev '%s' not found\n", base_bdev->name);
2269 : 0 : return;
2270 : : }
2271 : 10 : raid_bdev = base_info->raid_bdev;
2272 : :
2273 [ - + ]: 10 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
2274 : :
2275 : 10 : SPDK_NOTICELOG("base_bdev '%s' was resized: old size %" PRIu64 ", new size %" PRIu64 "\n",
2276 : : base_bdev->name, base_info->blockcnt, base_bdev->blockcnt);
2277 : :
2278 : 10 : base_info->blockcnt = base_bdev->blockcnt;
2279 : :
2280 [ - + ]: 10 : if (!raid_bdev->module->resize) {
2281 : 0 : return;
2282 : : }
2283 : :
2284 : 10 : blockcnt_old = raid_bdev->bdev.blockcnt;
2285 [ + + ]: 10 : if (raid_bdev->module->resize(raid_bdev) == false) {
2286 : 5 : return;
2287 : : }
2288 : :
2289 : 5 : SPDK_NOTICELOG("raid bdev '%s': block count was changed from %" PRIu64 " to %" PRIu64 "\n",
2290 : : raid_bdev->bdev.name, blockcnt_old, raid_bdev->bdev.blockcnt);
2291 : :
2292 [ - + - + ]: 5 : if (raid_bdev->superblock_enabled) {
2293 : 0 : struct raid_bdev_superblock *sb = raid_bdev->sb;
2294 : : uint8_t i;
2295 : :
2296 [ # # ]: 0 : for (i = 0; i < sb->base_bdevs_size; i++) {
2297 : 0 : struct raid_bdev_sb_base_bdev *sb_base_bdev = &sb->base_bdevs[i];
2298 : :
2299 [ # # ]: 0 : if (sb_base_bdev->state == RAID_SB_BASE_BDEV_CONFIGURED) {
2300 : 0 : base_info = &raid_bdev->base_bdev_info[sb_base_bdev->slot];
2301 : 0 : sb_base_bdev->data_size = base_info->data_size;
2302 : : }
2303 : : }
2304 : 0 : sb->raid_size = raid_bdev->bdev.blockcnt;
2305 : 0 : raid_bdev_write_superblock(raid_bdev, raid_bdev_resize_write_sb_cb, NULL);
2306 : : }
2307 : : }
2308 : :
2309 : : /*
2310 : : * brief:
2311 : : * raid_bdev_event_base_bdev function is called by below layers when base_bdev
2312 : : * triggers asynchronous event.
2313 : : * params:
2314 : : * type - event details.
2315 : : * bdev - bdev that triggered event.
2316 : : * event_ctx - context for event.
2317 : : * returns:
2318 : : * none
2319 : : */
2320 : : static void
2321 : 540 : raid_bdev_event_base_bdev(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
2322 : : void *event_ctx)
2323 : : {
2324 : : int rc;
2325 : :
2326 [ + + - ]: 540 : switch (type) {
2327 : 530 : case SPDK_BDEV_EVENT_REMOVE:
2328 : 530 : rc = raid_bdev_remove_base_bdev(bdev, NULL, NULL);
2329 [ - + ]: 530 : if (rc != 0) {
2330 : 0 : SPDK_ERRLOG("Failed to remove base bdev %s: %s\n",
2331 : : spdk_bdev_get_name(bdev), spdk_strerror(-rc));
2332 : : }
2333 : 530 : break;
2334 : 10 : case SPDK_BDEV_EVENT_RESIZE:
2335 : 10 : raid_bdev_resize_base_bdev(bdev);
2336 : 10 : break;
2337 : 0 : default:
2338 : 0 : SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
2339 : 0 : break;
2340 : : }
2341 : 540 : }
2342 : :
2343 : : /*
2344 : : * brief:
2345 : : * Deletes the specified raid bdev
2346 : : * params:
2347 : : * raid_bdev - pointer to raid bdev
2348 : : * cb_fn - callback function
2349 : : * cb_arg - argument to callback function
2350 : : */
2351 : : void
2352 : 741 : raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_arg)
2353 : : {
2354 : : struct raid_base_bdev_info *base_info;
2355 : :
2356 [ + + + + ]: 741 : SPDK_DEBUGLOG(bdev_raid, "delete raid bdev: %s\n", raid_bdev->bdev.name);
2357 : :
2358 [ - + - + ]: 741 : if (raid_bdev->destroy_started) {
2359 [ # # # # ]: 0 : SPDK_DEBUGLOG(bdev_raid, "destroying raid bdev %s is already started\n",
2360 : : raid_bdev->bdev.name);
2361 [ # # ]: 0 : if (cb_fn) {
2362 : 0 : cb_fn(cb_arg, -EALREADY);
2363 : : }
2364 : 0 : return;
2365 : : }
2366 : :
2367 : 741 : raid_bdev->destroy_started = true;
2368 : :
2369 [ + + ]: 4631 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
2370 : 3890 : base_info->remove_scheduled = true;
2371 : :
2372 [ + + ]: 3890 : if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) {
2373 : : /*
2374 : : * As raid bdev is not registered yet or already unregistered,
2375 : : * so cleanup should be done here itself.
2376 : : */
2377 : 1180 : raid_bdev_free_base_bdev_resource(base_info);
2378 : : }
2379 : : }
2380 : :
2381 [ + + ]: 741 : if (raid_bdev->num_base_bdevs_discovered == 0) {
2382 : : /* There is no base bdev for this raid, so free the raid device. */
2383 : 332 : raid_bdev_cleanup_and_free(raid_bdev);
2384 [ + + ]: 332 : if (cb_fn) {
2385 : 226 : cb_fn(cb_arg, 0);
2386 : : }
2387 : : } else {
2388 : 409 : raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg);
2389 : : }
2390 : : }
2391 : :
2392 : : static void
2393 : 33 : raid_bdev_process_finish_write_sb_cb(int status, struct raid_bdev *raid_bdev, void *ctx)
2394 : : {
2395 [ - + ]: 33 : if (status != 0) {
2396 : 0 : SPDK_ERRLOG("Failed to write raid bdev '%s' superblock after background process finished: %s\n",
2397 : : raid_bdev->bdev.name, spdk_strerror(-status));
2398 : : }
2399 : 33 : }
2400 : :
2401 : : static void
2402 : 33 : raid_bdev_process_finish_write_sb(void *ctx)
2403 : : {
2404 : 33 : struct raid_bdev *raid_bdev = ctx;
2405 : 33 : struct raid_bdev_superblock *sb = raid_bdev->sb;
2406 : : struct raid_bdev_sb_base_bdev *sb_base_bdev;
2407 : : struct raid_base_bdev_info *base_info;
2408 : : uint8_t i;
2409 : :
2410 [ + + ]: 121 : for (i = 0; i < sb->base_bdevs_size; i++) {
2411 : 88 : sb_base_bdev = &sb->base_bdevs[i];
2412 : :
2413 [ + + ]: 88 : if (sb_base_bdev->state != RAID_SB_BASE_BDEV_CONFIGURED &&
2414 [ + - ]: 41 : sb_base_bdev->slot < raid_bdev->num_base_bdevs) {
2415 : 41 : base_info = &raid_bdev->base_bdev_info[sb_base_bdev->slot];
2416 [ + + + + ]: 41 : if (base_info->is_configured) {
2417 : 33 : sb_base_bdev->state = RAID_SB_BASE_BDEV_CONFIGURED;
2418 : 33 : spdk_uuid_copy(&sb_base_bdev->uuid, &base_info->uuid);
2419 : : }
2420 : : }
2421 : : }
2422 : :
2423 : 33 : raid_bdev_write_superblock(raid_bdev, raid_bdev_process_finish_write_sb_cb, NULL);
2424 : 33 : }
2425 : :
2426 : : static void raid_bdev_process_free(struct raid_bdev_process *process);
2427 : :
2428 : : static void
2429 : 176 : _raid_bdev_process_finish_done(void *ctx)
2430 : : {
2431 : 176 : struct raid_bdev_process *process = ctx;
2432 : : struct raid_process_finish_action *finish_action;
2433 : :
2434 [ + + ]: 295 : while ((finish_action = TAILQ_FIRST(&process->finish_actions)) != NULL) {
2435 [ - + ]: 119 : TAILQ_REMOVE(&process->finish_actions, finish_action, link);
2436 : 119 : finish_action->cb(finish_action->cb_ctx);
2437 : 119 : free(finish_action);
2438 : : }
2439 : :
2440 : 176 : raid_bdev_process_free(process);
2441 : :
2442 : 176 : spdk_thread_exit(spdk_get_thread());
2443 : 176 : }
2444 : :
2445 : : static void
2446 : 119 : raid_bdev_process_finish_target_removed(void *ctx, int status)
2447 : : {
2448 : 119 : struct raid_bdev_process *process = ctx;
2449 : :
2450 [ + - ]: 119 : if (status != 0) {
2451 : 119 : SPDK_ERRLOG("Failed to remove target bdev: %s\n", spdk_strerror(-status));
2452 : : }
2453 : :
2454 : 119 : spdk_thread_send_msg(process->thread, _raid_bdev_process_finish_done, process);
2455 : 119 : }
2456 : :
2457 : : static void
2458 : 176 : raid_bdev_process_finish_unquiesced(void *ctx, int status)
2459 : : {
2460 : 176 : struct raid_bdev_process *process = ctx;
2461 : :
2462 [ - + ]: 176 : if (status != 0) {
2463 : 0 : SPDK_ERRLOG("Failed to unquiesce bdev: %s\n", spdk_strerror(-status));
2464 : : }
2465 : :
2466 [ + + ]: 176 : if (process->status != 0) {
2467 : 119 : status = _raid_bdev_remove_base_bdev(process->target, raid_bdev_process_finish_target_removed,
2468 : : process);
2469 [ + - ]: 119 : if (status != 0) {
2470 : 119 : raid_bdev_process_finish_target_removed(process, status);
2471 : : }
2472 : 119 : return;
2473 : : }
2474 : :
2475 : 57 : spdk_thread_send_msg(process->thread, _raid_bdev_process_finish_done, process);
2476 : : }
2477 : :
2478 : : static void
2479 : 176 : raid_bdev_process_finish_unquiesce(void *ctx)
2480 : : {
2481 : 176 : struct raid_bdev_process *process = ctx;
2482 : : int rc;
2483 : :
2484 : 176 : rc = spdk_bdev_unquiesce(&process->raid_bdev->bdev, &g_raid_if,
2485 : : raid_bdev_process_finish_unquiesced, process);
2486 [ - + ]: 176 : if (rc != 0) {
2487 : 0 : raid_bdev_process_finish_unquiesced(process, rc);
2488 : : }
2489 : 176 : }
2490 : :
2491 : : static void
2492 : 176 : raid_bdev_process_finish_done(void *ctx)
2493 : : {
2494 : 176 : struct raid_bdev_process *process = ctx;
2495 : 176 : struct raid_bdev *raid_bdev = process->raid_bdev;
2496 : :
2497 [ + - ]: 176 : if (process->raid_ch != NULL) {
2498 : 176 : spdk_put_io_channel(spdk_io_channel_from_ctx(process->raid_ch));
2499 : : }
2500 : :
2501 : 176 : process->state = RAID_PROCESS_STATE_STOPPED;
2502 : :
2503 [ + + ]: 176 : if (process->status == 0) {
2504 : 57 : SPDK_NOTICELOG("Finished %s on raid bdev %s\n",
2505 : : raid_bdev_process_to_str(process->type),
2506 : : raid_bdev->bdev.name);
2507 [ + + + + ]: 57 : if (raid_bdev->superblock_enabled) {
2508 : 33 : spdk_thread_send_msg(spdk_thread_get_app_thread(),
2509 : : raid_bdev_process_finish_write_sb,
2510 : : raid_bdev);
2511 : : }
2512 : : } else {
2513 : 119 : SPDK_WARNLOG("Finished %s on raid bdev %s: %s\n",
2514 : : raid_bdev_process_to_str(process->type),
2515 : : raid_bdev->bdev.name,
2516 : : spdk_strerror(-process->status));
2517 : : }
2518 : :
2519 : 176 : spdk_thread_send_msg(spdk_thread_get_app_thread(), raid_bdev_process_finish_unquiesce,
2520 : : process);
2521 : 176 : }
2522 : :
2523 : : static void
2524 : 176 : __raid_bdev_process_finish(struct spdk_io_channel_iter *i, int status)
2525 : : {
2526 : 176 : struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i);
2527 : :
2528 : 176 : spdk_thread_send_msg(process->thread, raid_bdev_process_finish_done, process);
2529 : 176 : }
2530 : :
2531 : : static void
2532 : 208 : raid_bdev_channel_process_finish(struct spdk_io_channel_iter *i)
2533 : : {
2534 : 208 : struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i);
2535 : 208 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2536 : 208 : struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch);
2537 : :
2538 [ + + ]: 208 : if (process->status == 0) {
2539 : 73 : uint8_t slot = raid_bdev_base_bdev_slot(process->target);
2540 : :
2541 : 73 : raid_ch->base_channel[slot] = raid_ch->process.target_ch;
2542 : 73 : raid_ch->process.target_ch = NULL;
2543 : : }
2544 : :
2545 : 208 : raid_bdev_ch_process_cleanup(raid_ch);
2546 : :
2547 : 208 : spdk_for_each_channel_continue(i, 0);
2548 : 208 : }
2549 : :
2550 : : static void
2551 : 176 : raid_bdev_process_finish_quiesced(void *ctx, int status)
2552 : : {
2553 : 176 : struct raid_bdev_process *process = ctx;
2554 : 176 : struct raid_bdev *raid_bdev = process->raid_bdev;
2555 : :
2556 [ - + ]: 176 : if (status != 0) {
2557 : 0 : SPDK_ERRLOG("Failed to quiesce bdev: %s\n", spdk_strerror(-status));
2558 : 0 : return;
2559 : : }
2560 : :
2561 : 176 : raid_bdev->process = NULL;
2562 : 176 : process->target->is_process_target = false;
2563 : :
2564 : 176 : spdk_for_each_channel(process->raid_bdev, raid_bdev_channel_process_finish, process,
2565 : : __raid_bdev_process_finish);
2566 : : }
2567 : :
2568 : : static void
2569 : 176 : _raid_bdev_process_finish(void *ctx)
2570 : : {
2571 : 176 : struct raid_bdev_process *process = ctx;
2572 : : int rc;
2573 : :
2574 : 176 : rc = spdk_bdev_quiesce(&process->raid_bdev->bdev, &g_raid_if,
2575 : : raid_bdev_process_finish_quiesced, process);
2576 [ - + ]: 176 : if (rc != 0) {
2577 : 0 : raid_bdev_process_finish_quiesced(ctx, rc);
2578 : : }
2579 : 176 : }
2580 : :
2581 : : static void
2582 : 176 : raid_bdev_process_do_finish(struct raid_bdev_process *process)
2583 : : {
2584 : 176 : spdk_thread_send_msg(spdk_thread_get_app_thread(), _raid_bdev_process_finish, process);
2585 : 176 : }
2586 : :
2587 : : static void raid_bdev_process_unlock_window_range(struct raid_bdev_process *process);
2588 : : static void raid_bdev_process_thread_run(struct raid_bdev_process *process);
2589 : :
2590 : : static void
2591 : 57 : raid_bdev_process_finish(struct raid_bdev_process *process, int status)
2592 : : {
2593 [ - + ]: 57 : assert(spdk_get_thread() == process->thread);
2594 : :
2595 [ + - ]: 57 : if (process->status == 0) {
2596 : 57 : process->status = status;
2597 : : }
2598 : :
2599 [ - + ]: 57 : if (process->state >= RAID_PROCESS_STATE_STOPPING) {
2600 : 0 : return;
2601 : : }
2602 : :
2603 [ - + ]: 57 : assert(process->state == RAID_PROCESS_STATE_RUNNING);
2604 : 57 : process->state = RAID_PROCESS_STATE_STOPPING;
2605 : :
2606 [ - + - + ]: 57 : if (process->window_range_locked) {
2607 : 0 : raid_bdev_process_unlock_window_range(process);
2608 : : } else {
2609 : 57 : raid_bdev_process_thread_run(process);
2610 : : }
2611 : : }
2612 : :
2613 : : static void
2614 : 3907 : raid_bdev_process_window_range_unlocked(void *ctx, int status)
2615 : : {
2616 : 3907 : struct raid_bdev_process *process = ctx;
2617 : :
2618 [ - + ]: 3907 : if (status != 0) {
2619 : 0 : SPDK_ERRLOG("Failed to unlock LBA range: %s\n", spdk_strerror(-status));
2620 : 0 : raid_bdev_process_finish(process, status);
2621 : 0 : return;
2622 : : }
2623 : :
2624 : 3907 : process->window_range_locked = false;
2625 : 3907 : process->window_offset += process->window_size;
2626 : :
2627 : 3907 : raid_bdev_process_thread_run(process);
2628 : : }
2629 : :
2630 : : static void
2631 : 3907 : raid_bdev_process_unlock_window_range(struct raid_bdev_process *process)
2632 : : {
2633 : : int rc;
2634 : :
2635 [ - + - + ]: 3907 : assert(process->window_range_locked == true);
2636 : :
2637 : 3907 : rc = spdk_bdev_unquiesce_range(&process->raid_bdev->bdev, &g_raid_if,
2638 : : process->window_offset, process->max_window_size,
2639 : : raid_bdev_process_window_range_unlocked, process);
2640 [ - + ]: 3907 : if (rc != 0) {
2641 : 0 : raid_bdev_process_window_range_unlocked(process, rc);
2642 : : }
2643 : 3907 : }
2644 : :
2645 : : static void
2646 : 3904 : raid_bdev_process_channels_update_done(struct spdk_io_channel_iter *i, int status)
2647 : : {
2648 : 3904 : struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i);
2649 : :
2650 : 3904 : raid_bdev_process_unlock_window_range(process);
2651 : 3904 : }
2652 : :
2653 : : static void
2654 : 4577 : raid_bdev_process_channel_update(struct spdk_io_channel_iter *i)
2655 : : {
2656 : 4577 : struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i);
2657 : 4577 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2658 : 4577 : struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch);
2659 : :
2660 : 4577 : raid_ch->process.offset = process->window_offset + process->window_size;
2661 : :
2662 : 4577 : spdk_for_each_channel_continue(i, 0);
2663 : 4577 : }
2664 : :
2665 : : void
2666 : 8737 : raid_bdev_process_request_complete(struct raid_bdev_process_request *process_req, int status)
2667 : : {
2668 : 8737 : struct raid_bdev_process *process = process_req->process;
2669 : :
2670 : 8737 : TAILQ_INSERT_TAIL(&process->requests, process_req, link);
2671 : :
2672 [ - + ]: 8737 : assert(spdk_get_thread() == process->thread);
2673 [ - + ]: 8737 : assert(process->window_remaining >= process_req->num_blocks);
2674 : :
2675 [ - + ]: 8737 : if (status != 0) {
2676 : 0 : process->window_status = status;
2677 : : }
2678 : :
2679 : 8737 : process->window_remaining -= process_req->num_blocks;
2680 [ + + ]: 8737 : if (process->window_remaining == 0) {
2681 [ - + ]: 3904 : if (process->window_status != 0) {
2682 : 0 : raid_bdev_process_finish(process, process->window_status);
2683 : 0 : return;
2684 : : }
2685 : :
2686 : 3904 : spdk_for_each_channel(process->raid_bdev, raid_bdev_process_channel_update, process,
2687 : : raid_bdev_process_channels_update_done);
2688 : : }
2689 : : }
2690 : :
2691 : : static int
2692 : 9271 : raid_bdev_submit_process_request(struct raid_bdev_process *process, uint64_t offset_blocks,
2693 : : uint32_t num_blocks)
2694 : : {
2695 : 9271 : struct raid_bdev *raid_bdev = process->raid_bdev;
2696 : : struct raid_bdev_process_request *process_req;
2697 : : int ret;
2698 : :
2699 : 9271 : process_req = TAILQ_FIRST(&process->requests);
2700 [ - + ]: 9271 : if (process_req == NULL) {
2701 [ # # ]: 0 : assert(process->window_remaining > 0);
2702 : 0 : return 0;
2703 : : }
2704 : :
2705 : 9271 : process_req->target = process->target;
2706 : 9271 : process_req->target_ch = process->raid_ch->process.target_ch;
2707 : 9271 : process_req->offset_blocks = offset_blocks;
2708 : 9271 : process_req->num_blocks = num_blocks;
2709 : 9271 : process_req->iov.iov_len = num_blocks * raid_bdev->bdev.blocklen;
2710 : :
2711 : 9271 : ret = raid_bdev->module->submit_process_request(process_req, process->raid_ch);
2712 [ + + ]: 9271 : if (ret <= 0) {
2713 [ - + ]: 534 : if (ret < 0) {
2714 : 0 : SPDK_ERRLOG("Failed to submit process request on %s: %s\n",
2715 : : raid_bdev->bdev.name, spdk_strerror(-ret));
2716 : 0 : process->window_status = ret;
2717 : : }
2718 : 534 : return ret;
2719 : : }
2720 : :
2721 : 8737 : process_req->num_blocks = ret;
2722 [ + - ]: 8737 : TAILQ_REMOVE(&process->requests, process_req, link);
2723 : :
2724 : 8737 : return ret;
2725 : : }
2726 : :
2727 : : static void
2728 : 3904 : _raid_bdev_process_thread_run(struct raid_bdev_process *process)
2729 : : {
2730 : 3904 : struct raid_bdev *raid_bdev = process->raid_bdev;
2731 : 3904 : uint64_t offset = process->window_offset;
2732 : 3904 : const uint64_t offset_end = spdk_min(offset + process->max_window_size, raid_bdev->bdev.blockcnt);
2733 : : int ret;
2734 : :
2735 [ + + ]: 12641 : while (offset < offset_end) {
2736 : 9271 : ret = raid_bdev_submit_process_request(process, offset, offset_end - offset);
2737 [ + + ]: 9271 : if (ret <= 0) {
2738 : 534 : break;
2739 : : }
2740 : :
2741 : 8737 : process->window_remaining += ret;
2742 : 8737 : offset += ret;
2743 : : }
2744 : :
2745 [ + - ]: 3904 : if (process->window_remaining > 0) {
2746 : 3904 : process->window_size = process->window_remaining;
2747 : : } else {
2748 : 0 : raid_bdev_process_finish(process, process->window_status);
2749 : : }
2750 : 3904 : }
2751 : :
2752 : : static void
2753 : 3907 : raid_bdev_process_window_range_locked(void *ctx, int status)
2754 : : {
2755 : 3907 : struct raid_bdev_process *process = ctx;
2756 : :
2757 [ - + ]: 3907 : if (status != 0) {
2758 : 0 : SPDK_ERRLOG("Failed to lock LBA range: %s\n", spdk_strerror(-status));
2759 : 0 : raid_bdev_process_finish(process, status);
2760 : 0 : return;
2761 : : }
2762 : :
2763 : 3907 : process->window_range_locked = true;
2764 : :
2765 [ + + ]: 3907 : if (process->state == RAID_PROCESS_STATE_STOPPING) {
2766 : 3 : raid_bdev_process_unlock_window_range(process);
2767 : 3 : return;
2768 : : }
2769 : :
2770 : 3904 : _raid_bdev_process_thread_run(process);
2771 : : }
2772 : :
2773 : : static void
2774 : 4140 : raid_bdev_process_thread_run(struct raid_bdev_process *process)
2775 : : {
2776 : 4140 : struct raid_bdev *raid_bdev = process->raid_bdev;
2777 : : int rc;
2778 : :
2779 [ - + ]: 4140 : assert(spdk_get_thread() == process->thread);
2780 [ - + ]: 4140 : assert(process->window_remaining == 0);
2781 [ - + - + ]: 4140 : assert(process->window_range_locked == false);
2782 : :
2783 [ + + ]: 4140 : if (process->state == RAID_PROCESS_STATE_STOPPING) {
2784 : 176 : raid_bdev_process_do_finish(process);
2785 : 176 : return;
2786 : : }
2787 : :
2788 [ + + ]: 3964 : if (process->window_offset == raid_bdev->bdev.blockcnt) {
2789 [ + + + + ]: 57 : SPDK_DEBUGLOG(bdev_raid, "process completed on %s\n", raid_bdev->bdev.name);
2790 : 57 : raid_bdev_process_finish(process, 0);
2791 : 57 : return;
2792 : : }
2793 : :
2794 : 3907 : process->max_window_size = spdk_min(raid_bdev->bdev.blockcnt - process->window_offset,
2795 : : process->max_window_size);
2796 : :
2797 : 3907 : rc = spdk_bdev_quiesce_range(&raid_bdev->bdev, &g_raid_if,
2798 : : process->window_offset, process->max_window_size,
2799 : : raid_bdev_process_window_range_locked, process);
2800 [ - + ]: 3907 : if (rc != 0) {
2801 : 0 : raid_bdev_process_window_range_locked(process, rc);
2802 : : }
2803 : : }
2804 : :
2805 : : static void
2806 : 176 : raid_bdev_process_thread_init(void *ctx)
2807 : : {
2808 : 176 : struct raid_bdev_process *process = ctx;
2809 : 176 : struct raid_bdev *raid_bdev = process->raid_bdev;
2810 : : struct spdk_io_channel *ch;
2811 : :
2812 : 176 : process->thread = spdk_get_thread();
2813 : :
2814 : 176 : ch = spdk_get_io_channel(raid_bdev);
2815 [ - + ]: 176 : if (ch == NULL) {
2816 : 0 : process->status = -ENOMEM;
2817 : 0 : raid_bdev_process_do_finish(process);
2818 : 0 : return;
2819 : : }
2820 : :
2821 : 176 : process->raid_ch = spdk_io_channel_get_ctx(ch);
2822 : 176 : process->state = RAID_PROCESS_STATE_RUNNING;
2823 : :
2824 : 176 : SPDK_NOTICELOG("Started %s on raid bdev %s\n",
2825 : : raid_bdev_process_to_str(process->type), raid_bdev->bdev.name);
2826 : :
2827 : 176 : raid_bdev_process_thread_run(process);
2828 : : }
2829 : :
2830 : : static void
2831 : 0 : raid_bdev_channels_abort_start_process_done(struct spdk_io_channel_iter *i, int status)
2832 : : {
2833 : 0 : struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i);
2834 : :
2835 : 0 : _raid_bdev_remove_base_bdev(process->target, NULL, NULL);
2836 : 0 : raid_bdev_process_free(process);
2837 : :
2838 : : /* TODO: update sb */
2839 : 0 : }
2840 : :
2841 : : static void
2842 : 0 : raid_bdev_channel_abort_start_process(struct spdk_io_channel_iter *i)
2843 : : {
2844 : 0 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2845 : 0 : struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch);
2846 : :
2847 : 0 : raid_bdev_ch_process_cleanup(raid_ch);
2848 : :
2849 : 0 : spdk_for_each_channel_continue(i, 0);
2850 : 0 : }
2851 : :
2852 : : static void
2853 : 176 : raid_bdev_channels_start_process_done(struct spdk_io_channel_iter *i, int status)
2854 : : {
2855 : 176 : struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i);
2856 : 176 : struct raid_bdev *raid_bdev = process->raid_bdev;
2857 : : struct spdk_thread *thread;
2858 : 140 : char thread_name[RAID_BDEV_SB_NAME_SIZE + 16];
2859 : :
2860 [ + - ]: 176 : if (status == 0 &&
2861 [ + + + - : 176 : (process->target->remove_scheduled || !process->target->is_configured ||
- + + - ]
2862 [ - + ]: 176 : raid_bdev->num_base_bdevs_operational <= raid_bdev->min_base_bdevs_operational)) {
2863 : : /* a base bdev was removed before we got here */
2864 : 0 : status = -ENODEV;
2865 : : }
2866 : :
2867 [ - + ]: 176 : if (status != 0) {
2868 : 0 : SPDK_ERRLOG("Failed to start %s on %s: %s\n",
2869 : : raid_bdev_process_to_str(process->type), raid_bdev->bdev.name,
2870 : : spdk_strerror(-status));
2871 : 0 : goto err;
2872 : : }
2873 : :
2874 [ - + ]: 176 : snprintf(thread_name, sizeof(thread_name), "%s_%s",
2875 : : raid_bdev->bdev.name, raid_bdev_process_to_str(process->type));
2876 : :
2877 : 176 : thread = spdk_thread_create(thread_name, NULL);
2878 [ - + ]: 176 : if (thread == NULL) {
2879 : 0 : SPDK_ERRLOG("Failed to create %s thread for %s\n",
2880 : : raid_bdev_process_to_str(process->type), raid_bdev->bdev.name);
2881 : 0 : goto err;
2882 : : }
2883 : :
2884 : 176 : raid_bdev->process = process;
2885 : :
2886 : 176 : spdk_thread_send_msg(thread, raid_bdev_process_thread_init, process);
2887 : :
2888 : 176 : return;
2889 : 0 : err:
2890 : 0 : spdk_for_each_channel(process->raid_bdev, raid_bdev_channel_abort_start_process, process,
2891 : : raid_bdev_channels_abort_start_process_done);
2892 : : }
2893 : :
2894 : : static void
2895 : 32 : raid_bdev_channel_start_process(struct spdk_io_channel_iter *i)
2896 : : {
2897 : 32 : struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i);
2898 : 32 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2899 : 32 : struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch);
2900 : : int rc;
2901 : :
2902 : 32 : rc = raid_bdev_ch_process_setup(raid_ch, process);
2903 : :
2904 : 32 : spdk_for_each_channel_continue(i, rc);
2905 : 32 : }
2906 : :
2907 : : static void
2908 : 176 : raid_bdev_process_start(struct raid_bdev_process *process)
2909 : : {
2910 : 176 : struct raid_bdev *raid_bdev = process->raid_bdev;
2911 : :
2912 [ - + ]: 176 : assert(raid_bdev->module->submit_process_request != NULL);
2913 : :
2914 : 176 : spdk_for_each_channel(raid_bdev, raid_bdev_channel_start_process, process,
2915 : : raid_bdev_channels_start_process_done);
2916 : 176 : }
2917 : :
2918 : : static void
2919 : 2816 : raid_bdev_process_request_free(struct raid_bdev_process_request *process_req)
2920 : : {
2921 : 2816 : spdk_dma_free(process_req->iov.iov_base);
2922 : 2816 : spdk_dma_free(process_req->md_buf);
2923 : 2816 : free(process_req);
2924 : 2816 : }
2925 : :
2926 : : static struct raid_bdev_process_request *
2927 : 2816 : raid_bdev_process_alloc_request(struct raid_bdev_process *process)
2928 : : {
2929 : 2816 : struct raid_bdev *raid_bdev = process->raid_bdev;
2930 : : struct raid_bdev_process_request *process_req;
2931 : :
2932 : 2816 : process_req = calloc(1, sizeof(*process_req));
2933 [ - + ]: 2816 : if (process_req == NULL) {
2934 : 0 : return NULL;
2935 : : }
2936 : :
2937 : 2816 : process_req->process = process;
2938 : 2816 : process_req->iov.iov_len = process->max_window_size * raid_bdev->bdev.blocklen;
2939 : 2816 : process_req->iov.iov_base = spdk_dma_malloc(process_req->iov.iov_len, 4096, 0);
2940 [ - + ]: 2816 : if (process_req->iov.iov_base == NULL) {
2941 : 0 : free(process_req);
2942 : 0 : return NULL;
2943 : : }
2944 [ + + ]: 2816 : if (spdk_bdev_is_md_separate(&raid_bdev->bdev)) {
2945 : 320 : process_req->md_buf = spdk_dma_malloc(process->max_window_size * raid_bdev->bdev.md_len, 4096, 0);
2946 [ - + ]: 320 : if (process_req->md_buf == NULL) {
2947 : 0 : raid_bdev_process_request_free(process_req);
2948 : 0 : return NULL;
2949 : : }
2950 : : }
2951 : :
2952 : 2816 : return process_req;
2953 : : }
2954 : :
2955 : : static void
2956 : 176 : raid_bdev_process_free(struct raid_bdev_process *process)
2957 : : {
2958 : : struct raid_bdev_process_request *process_req;
2959 : :
2960 [ + + ]: 2992 : while ((process_req = TAILQ_FIRST(&process->requests)) != NULL) {
2961 [ + + ]: 2816 : TAILQ_REMOVE(&process->requests, process_req, link);
2962 : 2816 : raid_bdev_process_request_free(process_req);
2963 : : }
2964 : :
2965 : 176 : free(process);
2966 : 176 : }
2967 : :
2968 : : static struct raid_bdev_process *
2969 : 176 : raid_bdev_process_alloc(struct raid_bdev *raid_bdev, enum raid_process_type type,
2970 : : struct raid_base_bdev_info *target)
2971 : : {
2972 : : struct raid_bdev_process *process;
2973 : : struct raid_bdev_process_request *process_req;
2974 : : int i;
2975 : :
2976 : 176 : process = calloc(1, sizeof(*process));
2977 [ - + ]: 176 : if (process == NULL) {
2978 : 0 : return NULL;
2979 : : }
2980 : :
2981 : 176 : process->raid_bdev = raid_bdev;
2982 : 176 : process->type = type;
2983 : 176 : process->target = target;
2984 [ + - ]: 176 : process->max_window_size = spdk_max(spdk_divide_round_up(g_opts.process_window_size_kb * 1024UL,
2985 : : spdk_bdev_get_data_block_size(&raid_bdev->bdev)),
2986 : : raid_bdev->bdev.write_unit_size);
2987 : 176 : TAILQ_INIT(&process->requests);
2988 : 176 : TAILQ_INIT(&process->finish_actions);
2989 : :
2990 [ + + ]: 2992 : for (i = 0; i < RAID_BDEV_PROCESS_MAX_QD; i++) {
2991 : 2816 : process_req = raid_bdev_process_alloc_request(process);
2992 [ - + ]: 2816 : if (process_req == NULL) {
2993 : 0 : raid_bdev_process_free(process);
2994 : 0 : return NULL;
2995 : : }
2996 : :
2997 : 2816 : TAILQ_INSERT_TAIL(&process->requests, process_req, link);
2998 : : }
2999 : :
3000 : 176 : return process;
3001 : : }
3002 : :
3003 : : static int
3004 : 176 : raid_bdev_start_rebuild(struct raid_base_bdev_info *target)
3005 : : {
3006 : : struct raid_bdev_process *process;
3007 : :
3008 [ - + ]: 176 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
3009 : :
3010 : 176 : process = raid_bdev_process_alloc(target->raid_bdev, RAID_PROCESS_REBUILD, target);
3011 [ - + ]: 176 : if (process == NULL) {
3012 : 0 : return -ENOMEM;
3013 : : }
3014 : :
3015 : 176 : raid_bdev_process_start(process);
3016 : :
3017 : 176 : return 0;
3018 : : }
3019 : :
3020 : : static void raid_bdev_configure_base_bdev_cont(struct raid_base_bdev_info *base_info);
3021 : :
3022 : : static void
3023 : 172 : _raid_bdev_configure_base_bdev_cont(struct spdk_io_channel_iter *i, int status)
3024 : : {
3025 : 172 : struct raid_base_bdev_info *base_info = spdk_io_channel_iter_get_ctx(i);
3026 : :
3027 : 172 : raid_bdev_configure_base_bdev_cont(base_info);
3028 : 172 : }
3029 : :
3030 : : static void
3031 : 32 : raid_bdev_ch_sync(struct spdk_io_channel_iter *i)
3032 : : {
3033 : 32 : spdk_for_each_channel_continue(i, 0);
3034 : 32 : }
3035 : :
3036 : : static void
3037 : 4565 : raid_bdev_configure_base_bdev_cont(struct raid_base_bdev_info *base_info)
3038 : : {
3039 : 4565 : struct raid_bdev *raid_bdev = base_info->raid_bdev;
3040 : : int rc;
3041 : :
3042 [ + + ]: 4565 : if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs_operational &&
3043 [ + + + + ]: 344 : base_info->is_process_target == false) {
3044 : : /* TODO: defer if rebuild in progress on another base bdev */
3045 [ - + ]: 172 : assert(raid_bdev->process == NULL);
3046 [ - + ]: 172 : assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE);
3047 : 172 : base_info->is_process_target = true;
3048 : : /* To assure is_process_target is set before is_configured when checked in raid_bdev_create_cb() */
3049 : 172 : spdk_for_each_channel(raid_bdev, raid_bdev_ch_sync, base_info, _raid_bdev_configure_base_bdev_cont);
3050 : 172 : return;
3051 : : }
3052 : :
3053 : 4393 : base_info->is_configured = true;
3054 : :
3055 : 4393 : raid_bdev->num_base_bdevs_discovered++;
3056 [ - + ]: 4393 : assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs);
3057 [ - + ]: 4393 : assert(raid_bdev->num_base_bdevs_operational <= raid_bdev->num_base_bdevs);
3058 [ - + ]: 4393 : assert(raid_bdev->num_base_bdevs_operational >= raid_bdev->min_base_bdevs_operational);
3059 : :
3060 : : /*
3061 : : * Configure the raid bdev when the number of discovered base bdevs reaches the number
3062 : : * of base bdevs we know to be operational members of the array. Usually this is equal
3063 : : * to the total number of base bdevs (num_base_bdevs) but can be less - when the array is
3064 : : * degraded.
3065 : : */
3066 [ + + ]: 4393 : if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs_operational) {
3067 : 790 : rc = raid_bdev_configure(raid_bdev);
3068 [ - + ]: 790 : if (rc != 0) {
3069 : 0 : SPDK_ERRLOG("Failed to configure raid bdev: %s\n", spdk_strerror(-rc));
3070 : : }
3071 [ + + + + ]: 3603 : } else if (base_info->is_process_target) {
3072 : 172 : raid_bdev->num_base_bdevs_operational++;
3073 : 172 : rc = raid_bdev_start_rebuild(base_info);
3074 [ - + ]: 172 : if (rc != 0) {
3075 : 0 : SPDK_ERRLOG("Failed to start rebuild: %s\n", spdk_strerror(-rc));
3076 : 0 : _raid_bdev_remove_base_bdev(base_info, NULL, NULL);
3077 : : }
3078 : : } else {
3079 : 3431 : rc = 0;
3080 : : }
3081 : :
3082 [ + + ]: 4393 : if (base_info->configure_cb != NULL) {
3083 : 3712 : base_info->configure_cb(base_info->configure_cb_ctx, rc);
3084 : : }
3085 : : }
3086 : :
3087 : : static void raid_bdev_examine_sb(const struct raid_bdev_superblock *sb, struct spdk_bdev *bdev,
3088 : : raid_base_bdev_cb cb_fn, void *cb_ctx);
3089 : :
3090 : : static void
3091 : 3671 : raid_bdev_configure_base_bdev_check_sb_cb(const struct raid_bdev_superblock *sb, int status,
3092 : : void *ctx)
3093 : : {
3094 : 3671 : struct raid_base_bdev_info *base_info = ctx;
3095 : :
3096 [ + + - ]: 3671 : switch (status) {
3097 : 245 : case 0:
3098 : : /* valid superblock found */
3099 [ + + ]: 245 : if (spdk_uuid_compare(&base_info->raid_bdev->bdev.uuid, &sb->uuid) == 0) {
3100 : 66 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(base_info->desc);
3101 : :
3102 : 66 : raid_bdev_free_base_bdev_resource(base_info);
3103 : 66 : raid_bdev_examine_sb(sb, bdev, base_info->configure_cb, base_info->configure_cb_ctx);
3104 : 66 : return;
3105 : : }
3106 : 179 : SPDK_ERRLOG("Superblock of a different raid bdev found on bdev %s\n", base_info->name);
3107 : 179 : status = -EEXIST;
3108 : 179 : raid_bdev_free_base_bdev_resource(base_info);
3109 : 179 : break;
3110 : 3426 : case -EINVAL:
3111 : : /* no valid superblock */
3112 : 3426 : raid_bdev_configure_base_bdev_cont(base_info);
3113 : 3426 : return;
3114 : 0 : default:
3115 : 0 : SPDK_ERRLOG("Failed to examine bdev %s: %s\n",
3116 : : base_info->name, spdk_strerror(-status));
3117 : 0 : break;
3118 : : }
3119 : :
3120 [ + - ]: 179 : if (base_info->configure_cb != NULL) {
3121 : 179 : base_info->configure_cb(base_info->configure_cb_ctx, status);
3122 : : }
3123 : : }
3124 : :
3125 : : static int
3126 : 5589 : raid_bdev_configure_base_bdev(struct raid_base_bdev_info *base_info, bool existing,
3127 : : raid_base_bdev_cb cb_fn, void *cb_ctx)
3128 : : {
3129 : 5589 : struct raid_bdev *raid_bdev = base_info->raid_bdev;
3130 : 4880 : struct spdk_bdev_desc *desc;
3131 : : struct spdk_bdev *bdev;
3132 : : const struct spdk_uuid *bdev_uuid;
3133 : : int rc;
3134 : :
3135 [ - + ]: 5589 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
3136 [ - + ]: 5589 : assert(base_info->desc == NULL);
3137 : :
3138 : : /*
3139 : : * Base bdev can be added by name or uuid. Here we assure both properties are set and valid
3140 : : * before claiming the bdev.
3141 : : */
3142 : :
3143 [ + + ]: 5589 : if (!spdk_uuid_is_null(&base_info->uuid)) {
3144 : 578 : char uuid_str[SPDK_UUID_STRING_LEN];
3145 : : const char *bdev_name;
3146 : :
3147 : 707 : spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &base_info->uuid);
3148 : :
3149 : : /* UUID of a bdev is registered as its alias */
3150 : 707 : bdev = spdk_bdev_get_by_name(uuid_str);
3151 [ - + ]: 707 : if (bdev == NULL) {
3152 : 0 : return -ENODEV;
3153 : : }
3154 : :
3155 : 707 : bdev_name = spdk_bdev_get_name(bdev);
3156 : :
3157 [ + + ]: 707 : if (base_info->name == NULL) {
3158 [ - + ]: 571 : assert(existing == true);
3159 [ - + ]: 571 : base_info->name = strdup(bdev_name);
3160 [ - + ]: 571 : if (base_info->name == NULL) {
3161 : 0 : return -ENOMEM;
3162 : : }
3163 [ - + - + : 136 : } else if (strcmp(base_info->name, bdev_name) != 0) {
- + ]
3164 : 0 : SPDK_ERRLOG("Name mismatch for base bdev '%s' - expected '%s'\n",
3165 : : bdev_name, base_info->name);
3166 : 0 : return -EINVAL;
3167 : : }
3168 : : }
3169 : :
3170 [ - + ]: 5589 : assert(base_info->name != NULL);
3171 : :
3172 : 5589 : rc = spdk_bdev_open_ext(base_info->name, true, raid_bdev_event_base_bdev, NULL, &desc);
3173 [ + + ]: 5589 : if (rc != 0) {
3174 [ - + ]: 943 : if (rc != -ENODEV) {
3175 : 0 : SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", base_info->name);
3176 : : }
3177 : 943 : return rc;
3178 : : }
3179 : :
3180 : 4646 : bdev = spdk_bdev_desc_get_bdev(desc);
3181 : 4646 : bdev_uuid = spdk_bdev_get_uuid(bdev);
3182 : :
3183 [ + + ]: 4646 : if (spdk_uuid_is_null(&base_info->uuid)) {
3184 : 3939 : spdk_uuid_copy(&base_info->uuid, bdev_uuid);
3185 [ - + ]: 707 : } else if (spdk_uuid_compare(&base_info->uuid, bdev_uuid) != 0) {
3186 : 0 : SPDK_ERRLOG("UUID mismatch for base bdev '%s'\n", base_info->name);
3187 : 0 : spdk_bdev_close(desc);
3188 : 0 : return -EINVAL;
3189 : : }
3190 : :
3191 : 4646 : rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if);
3192 [ + + ]: 4646 : if (rc != 0) {
3193 : 8 : SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n");
3194 : 8 : spdk_bdev_close(desc);
3195 : 8 : return rc;
3196 : : }
3197 : :
3198 [ + + + + ]: 4638 : SPDK_DEBUGLOG(bdev_raid, "bdev %s is claimed\n", bdev->name);
3199 : :
3200 : 4638 : base_info->app_thread_ch = spdk_bdev_get_io_channel(desc);
3201 [ - + ]: 4638 : if (base_info->app_thread_ch == NULL) {
3202 : 0 : SPDK_ERRLOG("Failed to get io channel\n");
3203 : 0 : spdk_bdev_module_release_bdev(bdev);
3204 : 0 : spdk_bdev_close(desc);
3205 : 0 : return -ENOMEM;
3206 : : }
3207 : :
3208 : 4638 : base_info->desc = desc;
3209 : 4638 : base_info->blockcnt = bdev->blockcnt;
3210 : :
3211 [ + + + + ]: 4638 : if (raid_bdev->superblock_enabled) {
3212 : : uint64_t data_offset;
3213 : :
3214 [ + + ]: 1764 : if (base_info->data_offset == 0) {
3215 [ - + - + ]: 1027 : assert((RAID_BDEV_MIN_DATA_OFFSET_SIZE % spdk_bdev_get_data_block_size(bdev)) == 0);
3216 [ - + ]: 1027 : data_offset = RAID_BDEV_MIN_DATA_OFFSET_SIZE / spdk_bdev_get_data_block_size(bdev);
3217 : : } else {
3218 : 737 : data_offset = base_info->data_offset;
3219 : : }
3220 : :
3221 [ - + ]: 1764 : if (bdev->optimal_io_boundary != 0) {
3222 : 0 : data_offset = spdk_divide_round_up(data_offset,
3223 : 0 : bdev->optimal_io_boundary) * bdev->optimal_io_boundary;
3224 [ # # # # ]: 0 : if (base_info->data_offset != 0 && base_info->data_offset != data_offset) {
3225 : 0 : SPDK_WARNLOG("Data offset %lu on bdev '%s' is different than optimal value %lu\n",
3226 : : base_info->data_offset, base_info->name, data_offset);
3227 : 0 : data_offset = base_info->data_offset;
3228 : : }
3229 : : }
3230 : :
3231 : 1764 : base_info->data_offset = data_offset;
3232 : : }
3233 : :
3234 [ - + ]: 4638 : if (base_info->data_offset >= bdev->blockcnt) {
3235 : 0 : SPDK_ERRLOG("Data offset %lu exceeds base bdev capacity %lu on bdev '%s'\n",
3236 : : base_info->data_offset, bdev->blockcnt, base_info->name);
3237 : 0 : rc = -EINVAL;
3238 : 0 : goto out;
3239 : : }
3240 : :
3241 [ + + ]: 4638 : if (base_info->data_size == 0) {
3242 : 3759 : base_info->data_size = bdev->blockcnt - base_info->data_offset;
3243 [ - + ]: 879 : } else if (base_info->data_offset + base_info->data_size > bdev->blockcnt) {
3244 : 0 : SPDK_ERRLOG("Data offset and size exceeds base bdev capacity %lu on bdev '%s'\n",
3245 : : bdev->blockcnt, base_info->name);
3246 : 0 : rc = -EINVAL;
3247 : 0 : goto out;
3248 : : }
3249 : :
3250 [ + + + + : 4638 : if (!raid_bdev->module->dif_supported && spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) {
- + ]
3251 : 0 : SPDK_ERRLOG("Base bdev '%s' has DIF or DIX enabled - unsupported RAID configuration\n",
3252 : : bdev->name);
3253 : 0 : rc = -EINVAL;
3254 : 0 : goto out;
3255 : : }
3256 : :
3257 : : /*
3258 : : * Set the raid bdev properties if this is the first base bdev configured,
3259 : : * otherwise - verify. Assumption is that all the base bdevs for any raid bdev should
3260 : : * have the same blocklen and metadata format.
3261 : : */
3262 [ + + ]: 4638 : if (raid_bdev->bdev.blocklen == 0) {
3263 : 1009 : raid_bdev->bdev.blocklen = bdev->blocklen;
3264 : 1009 : raid_bdev->bdev.md_len = spdk_bdev_get_md_size(bdev);
3265 : 1009 : raid_bdev->bdev.md_interleave = spdk_bdev_is_md_interleaved(bdev);
3266 : 1009 : raid_bdev->bdev.dif_type = spdk_bdev_get_dif_type(bdev);
3267 : 1009 : raid_bdev->bdev.dif_check_flags = bdev->dif_check_flags;
3268 : 1009 : raid_bdev->bdev.dif_is_head_of_md = spdk_bdev_is_dif_head_of_md(bdev);
3269 : : } else {
3270 [ - + ]: 3629 : if (raid_bdev->bdev.blocklen != bdev->blocklen) {
3271 : 0 : SPDK_ERRLOG("Raid bdev '%s' blocklen %u differs from base bdev '%s' blocklen %u\n",
3272 : : raid_bdev->bdev.name, raid_bdev->bdev.blocklen, bdev->name, bdev->blocklen);
3273 : 0 : rc = -EINVAL;
3274 : 0 : goto out;
3275 : : }
3276 : :
3277 [ + - + - ]: 7258 : if (raid_bdev->bdev.md_len != spdk_bdev_get_md_size(bdev) ||
3278 [ + + + - ]: 7258 : raid_bdev->bdev.md_interleave != spdk_bdev_is_md_interleaved(bdev) ||
3279 : 3629 : raid_bdev->bdev.dif_type != spdk_bdev_get_dif_type(bdev) ||
3280 [ + - - + ]: 7258 : raid_bdev->bdev.dif_check_flags != bdev->dif_check_flags ||
3281 [ - + ]: 3629 : raid_bdev->bdev.dif_is_head_of_md != spdk_bdev_is_dif_head_of_md(bdev)) {
3282 : 0 : SPDK_ERRLOG("Raid bdev '%s' has different metadata format than base bdev '%s'\n",
3283 : : raid_bdev->bdev.name, bdev->name);
3284 : 0 : rc = -EINVAL;
3285 : 0 : goto out;
3286 : : }
3287 : : }
3288 : :
3289 : 4638 : base_info->configure_cb = cb_fn;
3290 : 4638 : base_info->configure_cb_ctx = cb_ctx;
3291 : :
3292 [ + + ]: 4638 : if (existing) {
3293 : 967 : raid_bdev_configure_base_bdev_cont(base_info);
3294 : : } else {
3295 : : /* check for existing superblock when using a new bdev */
3296 : 3671 : rc = raid_bdev_load_base_bdev_superblock(desc, base_info->app_thread_ch,
3297 : : raid_bdev_configure_base_bdev_check_sb_cb, base_info);
3298 [ + - ]: 3671 : if (rc) {
3299 : 0 : SPDK_ERRLOG("Failed to read bdev %s superblock: %s\n",
3300 : : bdev->name, spdk_strerror(-rc));
3301 : : }
3302 : : }
3303 : 4461 : out:
3304 [ - + ]: 4638 : if (rc != 0) {
3305 : 0 : raid_bdev_free_base_bdev_resource(base_info);
3306 : : }
3307 : 4638 : return rc;
3308 : : }
3309 : :
3310 : : int
3311 : 4622 : raid_bdev_add_base_bdev(struct raid_bdev *raid_bdev, const char *name,
3312 : : raid_base_bdev_cb cb_fn, void *cb_ctx)
3313 : : {
3314 : 4622 : struct raid_base_bdev_info *base_info = NULL, *iter;
3315 : : int rc;
3316 : :
3317 [ - + ]: 4622 : assert(name != NULL);
3318 [ - + ]: 4622 : assert(spdk_get_thread() == spdk_thread_get_app_thread());
3319 : :
3320 [ - + ]: 4622 : if (raid_bdev->process != NULL) {
3321 : 0 : SPDK_ERRLOG("raid bdev '%s' is in process\n",
3322 : : raid_bdev->bdev.name);
3323 : 0 : return -EPERM;
3324 : : }
3325 : :
3326 [ + + ]: 4622 : if (raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) {
3327 : 4450 : struct spdk_bdev *bdev = spdk_bdev_get_by_name(name);
3328 : :
3329 [ + + ]: 4450 : if (bdev != NULL) {
3330 [ + + ]: 69365 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, iter) {
3331 [ + + + + ]: 100925 : if (iter->name == NULL &&
3332 : 34931 : spdk_uuid_compare(&bdev->uuid, &iter->uuid) == 0) {
3333 : 136 : base_info = iter;
3334 : 136 : break;
3335 : : }
3336 : : }
3337 : : }
3338 : : }
3339 : :
3340 [ + + - + ]: 4622 : if (base_info == NULL || raid_bdev->state == RAID_BDEV_STATE_ONLINE) {
3341 [ + - ]: 36658 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, iter) {
3342 [ + + + - ]: 36658 : if (iter->name == NULL && spdk_uuid_is_null(&iter->uuid)) {
3343 : 4486 : base_info = iter;
3344 : 4486 : break;
3345 : : }
3346 : : }
3347 : : }
3348 : :
3349 [ - + ]: 4622 : if (base_info == NULL) {
3350 : 0 : SPDK_ERRLOG("no empty slot found in raid bdev '%s' for new base bdev '%s'\n",
3351 : : raid_bdev->bdev.name, name);
3352 : 0 : return -EINVAL;
3353 : : }
3354 : :
3355 [ - + - + ]: 4622 : assert(base_info->is_configured == false);
3356 : :
3357 [ + + ]: 4622 : if (raid_bdev->state == RAID_BDEV_STATE_ONLINE) {
3358 [ - + ]: 172 : assert(base_info->data_size != 0);
3359 [ - + ]: 172 : assert(base_info->desc == NULL);
3360 : : }
3361 : :
3362 [ - + ]: 4622 : base_info->name = strdup(name);
3363 [ - + ]: 4622 : if (base_info->name == NULL) {
3364 : 0 : return -ENOMEM;
3365 : : }
3366 : :
3367 : 4622 : rc = raid_bdev_configure_base_bdev(base_info, false, cb_fn, cb_ctx);
3368 [ + + + + : 4622 : if (rc != 0 && (rc != -ENODEV || raid_bdev->state != RAID_BDEV_STATE_CONFIGURING)) {
- + ]
3369 : 8 : SPDK_ERRLOG("base bdev '%s' configure failed: %s\n", name, spdk_strerror(-rc));
3370 : 8 : free(base_info->name);
3371 : 8 : base_info->name = NULL;
3372 : : }
3373 : :
3374 : 4622 : return rc;
3375 : : }
3376 : :
3377 : : static int
3378 : 199 : raid_bdev_create_from_sb(const struct raid_bdev_superblock *sb, struct raid_bdev **raid_bdev_out)
3379 : : {
3380 : 162 : struct raid_bdev *raid_bdev;
3381 : : uint8_t i;
3382 : : int rc;
3383 : :
3384 : 330 : rc = _raid_bdev_create(sb->name, (sb->strip_size * sb->block_size) / 1024, sb->num_base_bdevs,
3385 : 199 : sb->level, true, &sb->uuid, &raid_bdev);
3386 [ - + ]: 199 : if (rc != 0) {
3387 : 0 : return rc;
3388 : : }
3389 : :
3390 : 199 : rc = raid_bdev_alloc_superblock(raid_bdev, sb->block_size);
3391 [ - + ]: 199 : if (rc != 0) {
3392 : 0 : raid_bdev_free(raid_bdev);
3393 : 0 : return rc;
3394 : : }
3395 : :
3396 [ - + ]: 199 : assert(sb->length <= RAID_BDEV_SB_MAX_LENGTH);
3397 [ - + - + ]: 199 : memcpy(raid_bdev->sb, sb, sb->length);
3398 : :
3399 [ + + ]: 733 : for (i = 0; i < sb->base_bdevs_size; i++) {
3400 : 534 : const struct raid_bdev_sb_base_bdev *sb_base_bdev = &sb->base_bdevs[i];
3401 : 534 : struct raid_base_bdev_info *base_info = &raid_bdev->base_bdev_info[sb_base_bdev->slot];
3402 : :
3403 [ + + ]: 534 : if (sb_base_bdev->state == RAID_SB_BASE_BDEV_CONFIGURED) {
3404 : 458 : spdk_uuid_copy(&base_info->uuid, &sb_base_bdev->uuid);
3405 : 458 : raid_bdev->num_base_bdevs_operational++;
3406 : : }
3407 : :
3408 : 534 : base_info->data_offset = sb_base_bdev->data_offset;
3409 : 534 : base_info->data_size = sb_base_bdev->data_size;
3410 : : }
3411 : :
3412 : 199 : *raid_bdev_out = raid_bdev;
3413 : 199 : return 0;
3414 : : }
3415 : :
3416 : : static void
3417 : 7210 : raid_bdev_examine_no_sb(struct spdk_bdev *bdev)
3418 : : {
3419 : : struct raid_bdev *raid_bdev;
3420 : : struct raid_base_bdev_info *base_info;
3421 : :
3422 [ + + ]: 8749 : TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
3423 [ + + - + ]: 1539 : if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING || raid_bdev->sb != NULL) {
3424 : 1075 : continue;
3425 : : }
3426 [ + - ]: 815 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
3427 [ + + ]: 815 : if (base_info->desc == NULL &&
3428 [ + + - + : 532 : ((base_info->name != NULL && strcmp(bdev->name, base_info->name) == 0) ||
+ + - + +
- ]
3429 : 68 : spdk_uuid_compare(&base_info->uuid, &bdev->uuid) == 0)) {
3430 : 464 : raid_bdev_configure_base_bdev(base_info, true, NULL, NULL);
3431 : 464 : break;
3432 : : }
3433 : : }
3434 : : }
3435 : 7210 : }
3436 : :
3437 : : struct raid_bdev_examine_others_ctx {
3438 : : struct spdk_uuid raid_bdev_uuid;
3439 : : uint8_t current_base_bdev_idx;
3440 : : raid_base_bdev_cb cb_fn;
3441 : : void *cb_ctx;
3442 : : };
3443 : :
3444 : : static void
3445 : 199 : raid_bdev_examine_others_done(void *_ctx, int status)
3446 : : {
3447 : 199 : struct raid_bdev_examine_others_ctx *ctx = _ctx;
3448 : :
3449 [ + + ]: 199 : if (ctx->cb_fn != NULL) {
3450 : 34 : ctx->cb_fn(ctx->cb_ctx, status);
3451 : : }
3452 : 199 : free(ctx);
3453 : 199 : }
3454 : :
3455 : : typedef void (*raid_bdev_examine_load_sb_cb)(struct spdk_bdev *bdev,
3456 : : const struct raid_bdev_superblock *sb, int status, void *ctx);
3457 : : static int raid_bdev_examine_load_sb(const char *bdev_name, raid_bdev_examine_load_sb_cb cb,
3458 : : void *cb_ctx);
3459 : : static void raid_bdev_examine_sb(const struct raid_bdev_superblock *sb, struct spdk_bdev *bdev,
3460 : : raid_base_bdev_cb cb_fn, void *cb_ctx);
3461 : : static void raid_bdev_examine_others(void *_ctx, int status);
3462 : :
3463 : : static void
3464 : 88 : raid_bdev_examine_others_load_cb(struct spdk_bdev *bdev, const struct raid_bdev_superblock *sb,
3465 : : int status, void *_ctx)
3466 : : {
3467 : 88 : struct raid_bdev_examine_others_ctx *ctx = _ctx;
3468 : :
3469 [ - + ]: 88 : if (status != 0) {
3470 : 0 : raid_bdev_examine_others_done(ctx, status);
3471 : 0 : return;
3472 : : }
3473 : :
3474 : 88 : raid_bdev_examine_sb(sb, bdev, raid_bdev_examine_others, ctx);
3475 : : }
3476 : :
3477 : : static void
3478 : 287 : raid_bdev_examine_others(void *_ctx, int status)
3479 : : {
3480 : 287 : struct raid_bdev_examine_others_ctx *ctx = _ctx;
3481 : : struct raid_bdev *raid_bdev;
3482 : : struct raid_base_bdev_info *base_info;
3483 : 234 : char uuid_str[SPDK_UUID_STRING_LEN];
3484 : :
3485 [ - + ]: 287 : if (status != 0) {
3486 : 0 : goto out;
3487 : : }
3488 : :
3489 : 287 : raid_bdev = raid_bdev_find_by_uuid(&ctx->raid_bdev_uuid);
3490 [ - + ]: 287 : if (raid_bdev == NULL) {
3491 : 0 : status = -ENODEV;
3492 : 0 : goto out;
3493 : : }
3494 : :
3495 : 287 : for (base_info = &raid_bdev->base_bdev_info[ctx->current_base_bdev_idx];
3496 [ + + ]: 821 : base_info < &raid_bdev->base_bdev_info[raid_bdev->num_base_bdevs];
3497 : 534 : base_info++) {
3498 [ + + + + : 622 : if (base_info->is_configured || spdk_uuid_is_null(&base_info->uuid)) {
+ + ]
3499 : 370 : continue;
3500 : : }
3501 : :
3502 : 252 : spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &base_info->uuid);
3503 : :
3504 [ + + ]: 252 : if (spdk_bdev_get_by_name(uuid_str) == NULL) {
3505 : 164 : continue;
3506 : : }
3507 : :
3508 : 88 : ctx->current_base_bdev_idx = raid_bdev_base_bdev_slot(base_info);
3509 : :
3510 : 88 : status = raid_bdev_examine_load_sb(uuid_str, raid_bdev_examine_others_load_cb, ctx);
3511 [ - + ]: 88 : if (status != 0) {
3512 : 0 : continue;
3513 : : }
3514 : 88 : return;
3515 : : }
3516 : 199 : out:
3517 : 199 : raid_bdev_examine_others_done(ctx, status);
3518 : : }
3519 : :
3520 : : static void
3521 : 569 : raid_bdev_examine_sb(const struct raid_bdev_superblock *sb, struct spdk_bdev *bdev,
3522 : : raid_base_bdev_cb cb_fn, void *cb_ctx)
3523 : : {
3524 : 569 : const struct raid_bdev_sb_base_bdev *sb_base_bdev = NULL;
3525 : 462 : struct raid_bdev *raid_bdev;
3526 : : struct raid_base_bdev_info *iter, *base_info;
3527 : : uint8_t i;
3528 : : int rc;
3529 : :
3530 [ - + ]: 569 : if (sb->block_size != spdk_bdev_get_data_block_size(bdev)) {
3531 : 0 : SPDK_WARNLOG("Bdev %s block size (%u) does not match the value in superblock (%u)\n",
3532 : : bdev->name, sb->block_size, spdk_bdev_get_data_block_size(bdev));
3533 : 0 : rc = -EINVAL;
3534 : 0 : goto out;
3535 : : }
3536 : :
3537 [ - + ]: 569 : if (spdk_uuid_is_null(&sb->uuid)) {
3538 : 0 : SPDK_WARNLOG("NULL raid bdev UUID in superblock on bdev %s\n", bdev->name);
3539 : 0 : rc = -EINVAL;
3540 : 0 : goto out;
3541 : : }
3542 : :
3543 : 569 : raid_bdev = raid_bdev_find_by_uuid(&sb->uuid);
3544 : :
3545 [ + + ]: 569 : if (raid_bdev) {
3546 [ + + ]: 404 : if (sb->seq_number > raid_bdev->sb->seq_number) {
3547 [ + + + - ]: 34 : SPDK_DEBUGLOG(bdev_raid,
3548 : : "raid superblock seq_number on bdev %s (%lu) greater than existing raid bdev %s (%lu)\n",
3549 : : bdev->name, sb->seq_number, raid_bdev->bdev.name, raid_bdev->sb->seq_number);
3550 : :
3551 [ - + ]: 34 : if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING) {
3552 : 0 : SPDK_WARNLOG("Newer version of raid bdev %s superblock found on bdev %s but raid bdev is not in configuring state.\n",
3553 : : raid_bdev->bdev.name, bdev->name);
3554 : 0 : rc = -EBUSY;
3555 : 0 : goto out;
3556 : : }
3557 : :
3558 : : /* remove and then recreate the raid bdev using the newer superblock */
3559 : 34 : raid_bdev_delete(raid_bdev, NULL, NULL);
3560 : 34 : raid_bdev = NULL;
3561 [ + + ]: 370 : } else if (sb->seq_number < raid_bdev->sb->seq_number) {
3562 [ + + + - ]: 132 : SPDK_DEBUGLOG(bdev_raid,
3563 : : "raid superblock seq_number on bdev %s (%lu) smaller than existing raid bdev %s (%lu)\n",
3564 : : bdev->name, sb->seq_number, raid_bdev->bdev.name, raid_bdev->sb->seq_number);
3565 : : /* use the current raid bdev superblock */
3566 : 132 : sb = raid_bdev->sb;
3567 : : }
3568 : : }
3569 : :
3570 [ + + ]: 1207 : for (i = 0; i < sb->base_bdevs_size; i++) {
3571 : 1141 : sb_base_bdev = &sb->base_bdevs[i];
3572 : :
3573 [ - + ]: 1141 : assert(spdk_uuid_is_null(&sb_base_bdev->uuid) == false);
3574 : :
3575 [ + + ]: 1141 : if (spdk_uuid_compare(&sb_base_bdev->uuid, spdk_bdev_get_uuid(bdev)) == 0) {
3576 : 503 : break;
3577 : : }
3578 : : }
3579 : :
3580 [ + + ]: 569 : if (i == sb->base_bdevs_size) {
3581 [ + + + - ]: 66 : SPDK_DEBUGLOG(bdev_raid, "raid superblock does not contain this bdev's uuid\n");
3582 : 66 : rc = -EINVAL;
3583 : 66 : goto out;
3584 : : }
3585 : :
3586 [ + + ]: 503 : if (!raid_bdev) {
3587 : : struct raid_bdev_examine_others_ctx *ctx;
3588 : :
3589 : 199 : ctx = calloc(1, sizeof(*ctx));
3590 [ - + ]: 199 : if (ctx == NULL) {
3591 : 0 : rc = -ENOMEM;
3592 : 0 : goto out;
3593 : : }
3594 : :
3595 : 199 : rc = raid_bdev_create_from_sb(sb, &raid_bdev);
3596 [ - + ]: 199 : if (rc != 0) {
3597 : 0 : SPDK_ERRLOG("Failed to create raid bdev %s: %s\n",
3598 : : sb->name, spdk_strerror(-rc));
3599 : 0 : free(ctx);
3600 : 0 : goto out;
3601 : : }
3602 : :
3603 : : /* after this base bdev is configured, examine other base bdevs that may be present */
3604 : 199 : spdk_uuid_copy(&ctx->raid_bdev_uuid, &sb->uuid);
3605 : 199 : ctx->cb_fn = cb_fn;
3606 : 199 : ctx->cb_ctx = cb_ctx;
3607 : :
3608 : 199 : cb_fn = raid_bdev_examine_others;
3609 : 199 : cb_ctx = ctx;
3610 : : }
3611 : :
3612 [ + + ]: 503 : if (raid_bdev->state == RAID_BDEV_STATE_ONLINE) {
3613 [ - + ]: 66 : assert(sb_base_bdev->slot < raid_bdev->num_base_bdevs);
3614 : 66 : base_info = &raid_bdev->base_bdev_info[sb_base_bdev->slot];
3615 [ - + - + ]: 66 : assert(base_info->is_configured == false);
3616 [ - + - - ]: 66 : assert(sb_base_bdev->state == RAID_SB_BASE_BDEV_MISSING ||
3617 : : sb_base_bdev->state == RAID_SB_BASE_BDEV_FAILED);
3618 [ - + ]: 66 : assert(spdk_uuid_is_null(&base_info->uuid));
3619 : 66 : spdk_uuid_copy(&base_info->uuid, &sb_base_bdev->uuid);
3620 : 66 : SPDK_NOTICELOG("Re-adding bdev %s to raid bdev %s.\n", bdev->name, raid_bdev->bdev.name);
3621 : 66 : rc = raid_bdev_configure_base_bdev(base_info, true, cb_fn, cb_ctx);
3622 [ - + ]: 66 : if (rc != 0) {
3623 : 0 : SPDK_ERRLOG("Failed to configure bdev %s as base bdev of raid %s: %s\n",
3624 : : bdev->name, raid_bdev->bdev.name, spdk_strerror(-rc));
3625 : : }
3626 : 66 : goto out;
3627 : : }
3628 : :
3629 [ - + ]: 437 : if (sb_base_bdev->state != RAID_SB_BASE_BDEV_CONFIGURED) {
3630 : 0 : SPDK_NOTICELOG("Bdev %s is not an active member of raid bdev %s. Ignoring.\n",
3631 : : bdev->name, raid_bdev->bdev.name);
3632 : 0 : rc = -EINVAL;
3633 : 0 : goto out;
3634 : : }
3635 : :
3636 : 437 : base_info = NULL;
3637 [ + - ]: 899 : RAID_FOR_EACH_BASE_BDEV(raid_bdev, iter) {
3638 [ + + ]: 899 : if (spdk_uuid_compare(&iter->uuid, spdk_bdev_get_uuid(bdev)) == 0) {
3639 : 437 : base_info = iter;
3640 : 437 : break;
3641 : : }
3642 : : }
3643 : :
3644 [ - + ]: 437 : if (base_info == NULL) {
3645 : 0 : SPDK_ERRLOG("Bdev %s is not a member of raid bdev %s\n",
3646 : : bdev->name, raid_bdev->bdev.name);
3647 : 0 : rc = -EINVAL;
3648 : 0 : goto out;
3649 : : }
3650 : :
3651 : 437 : rc = raid_bdev_configure_base_bdev(base_info, true, cb_fn, cb_ctx);
3652 [ + - ]: 437 : if (rc != 0) {
3653 : 0 : SPDK_ERRLOG("Failed to configure bdev %s as base bdev of raid %s: %s\n",
3654 : : bdev->name, raid_bdev->bdev.name, spdk_strerror(-rc));
3655 : : }
3656 : 541 : out:
3657 [ + + + + ]: 569 : if (rc != 0 && cb_fn != 0) {
3658 : 33 : cb_fn(cb_ctx, rc);
3659 : : }
3660 : 569 : }
3661 : :
3662 : : struct raid_bdev_examine_ctx {
3663 : : struct spdk_bdev_desc *desc;
3664 : : struct spdk_io_channel *ch;
3665 : : raid_bdev_examine_load_sb_cb cb;
3666 : : void *cb_ctx;
3667 : : };
3668 : :
3669 : : static void
3670 : 7683 : raid_bdev_examine_ctx_free(struct raid_bdev_examine_ctx *ctx)
3671 : : {
3672 [ - + ]: 7683 : if (!ctx) {
3673 : 0 : return;
3674 : : }
3675 : :
3676 [ + - ]: 7683 : if (ctx->ch) {
3677 : 7683 : spdk_put_io_channel(ctx->ch);
3678 : : }
3679 : :
3680 [ + - ]: 7683 : if (ctx->desc) {
3681 : 7683 : spdk_bdev_close(ctx->desc);
3682 : : }
3683 : :
3684 : 7683 : free(ctx);
3685 : : }
3686 : :
3687 : : static void
3688 : 7683 : raid_bdev_examine_load_sb_done(const struct raid_bdev_superblock *sb, int status, void *_ctx)
3689 : : {
3690 : 7683 : struct raid_bdev_examine_ctx *ctx = _ctx;
3691 : 7683 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(ctx->desc);
3692 : :
3693 : 7683 : ctx->cb(bdev, sb, status, ctx->cb_ctx);
3694 : :
3695 : 7683 : raid_bdev_examine_ctx_free(ctx);
3696 : 7683 : }
3697 : :
3698 : : static void
3699 : 0 : raid_bdev_examine_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx)
3700 : : {
3701 : 0 : }
3702 : :
3703 : : static int
3704 : 7683 : raid_bdev_examine_load_sb(const char *bdev_name, raid_bdev_examine_load_sb_cb cb, void *cb_ctx)
3705 : : {
3706 : : struct raid_bdev_examine_ctx *ctx;
3707 : : int rc;
3708 : :
3709 [ - + ]: 7683 : assert(cb != NULL);
3710 : :
3711 : 7683 : ctx = calloc(1, sizeof(*ctx));
3712 [ - + ]: 7683 : if (!ctx) {
3713 : 0 : return -ENOMEM;
3714 : : }
3715 : :
3716 : 7683 : rc = spdk_bdev_open_ext(bdev_name, false, raid_bdev_examine_event_cb, NULL, &ctx->desc);
3717 [ - + ]: 7683 : if (rc) {
3718 : 0 : SPDK_ERRLOG("Failed to open bdev %s: %s\n", bdev_name, spdk_strerror(-rc));
3719 : 0 : goto err;
3720 : : }
3721 : :
3722 : 7683 : ctx->ch = spdk_bdev_get_io_channel(ctx->desc);
3723 [ - + ]: 7683 : if (!ctx->ch) {
3724 : 0 : SPDK_ERRLOG("Failed to get io channel for bdev %s\n", bdev_name);
3725 : 0 : rc = -ENOMEM;
3726 : 0 : goto err;
3727 : : }
3728 : :
3729 : 7683 : ctx->cb = cb;
3730 : 7683 : ctx->cb_ctx = cb_ctx;
3731 : :
3732 : 7683 : rc = raid_bdev_load_base_bdev_superblock(ctx->desc, ctx->ch, raid_bdev_examine_load_sb_done, ctx);
3733 [ - + ]: 7683 : if (rc) {
3734 : 0 : SPDK_ERRLOG("Failed to read bdev %s superblock: %s\n",
3735 : : bdev_name, spdk_strerror(-rc));
3736 : 0 : goto err;
3737 : : }
3738 : :
3739 : 7683 : return 0;
3740 : 0 : err:
3741 : 0 : raid_bdev_examine_ctx_free(ctx);
3742 : 0 : return rc;
3743 : : }
3744 : :
3745 : : static void
3746 : 7595 : raid_bdev_examine_cont(struct spdk_bdev *bdev, const struct raid_bdev_superblock *sb, int status,
3747 : : void *ctx)
3748 : : {
3749 [ + + - ]: 7595 : switch (status) {
3750 : 415 : case 0:
3751 : : /* valid superblock found */
3752 [ + + + - ]: 415 : SPDK_DEBUGLOG(bdev_raid, "raid superblock found on bdev %s\n", bdev->name);
3753 : 415 : raid_bdev_examine_sb(sb, bdev, NULL, NULL);
3754 : 415 : break;
3755 : 7180 : case -EINVAL:
3756 : : /* no valid superblock, check if it can be claimed anyway */
3757 : 7180 : raid_bdev_examine_no_sb(bdev);
3758 : 7180 : break;
3759 : 0 : default:
3760 : 0 : SPDK_ERRLOG("Failed to examine bdev %s: %s\n",
3761 : : bdev->name, spdk_strerror(-status));
3762 : 0 : break;
3763 : : }
3764 : :
3765 : 7595 : spdk_bdev_module_examine_done(&g_raid_if);
3766 : 7595 : }
3767 : :
3768 : : /*
3769 : : * brief:
3770 : : * raid_bdev_examine function is the examine function call by the below layers
3771 : : * like bdev_nvme layer. This function will check if this base bdev can be
3772 : : * claimed by this raid bdev or not.
3773 : : * params:
3774 : : * bdev - pointer to base bdev
3775 : : * returns:
3776 : : * none
3777 : : */
3778 : : static void
3779 : 7625 : raid_bdev_examine(struct spdk_bdev *bdev)
3780 : : {
3781 : : int rc;
3782 : :
3783 [ - + ]: 7625 : if (raid_bdev_find_base_info_by_bdev(bdev) != NULL) {
3784 : 0 : goto done;
3785 : : }
3786 : :
3787 [ + + ]: 7625 : if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) {
3788 : 30 : raid_bdev_examine_no_sb(bdev);
3789 : 30 : goto done;
3790 : : }
3791 : :
3792 : 7595 : rc = raid_bdev_examine_load_sb(bdev->name, raid_bdev_examine_cont, NULL);
3793 [ - + ]: 7595 : if (rc != 0) {
3794 : 0 : SPDK_ERRLOG("Failed to examine bdev %s: %s\n",
3795 : : bdev->name, spdk_strerror(-rc));
3796 : 0 : goto done;
3797 : : }
3798 : :
3799 : 7595 : return;
3800 : 30 : done:
3801 : 30 : spdk_bdev_module_examine_done(&g_raid_if);
3802 : : }
3803 : :
3804 : : /* Log component for bdev raid bdev module */
3805 : 2336 : SPDK_LOG_REGISTER_COMPONENT(bdev_raid)
|