xref: /spdk/module/bdev/raid/bdev_raid.c (revision 7961de43413e7f818f7499bf8518909beb59c82f)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "bdev_raid.h"
35 #include "spdk/env.h"
36 #include "spdk/io_channel.h"
37 #include "spdk/conf.h"
38 #include "spdk_internal/log.h"
39 #include "spdk/string.h"
40 #include "spdk/util.h"
41 #include "spdk/json.h"
42 #include "spdk/string.h"
43 
44 static bool g_shutdown_started = false;
45 
46 /* raid bdev config as read from config file */
47 struct raid_config	g_raid_config = {
48 	.raid_bdev_config_head = TAILQ_HEAD_INITIALIZER(g_raid_config.raid_bdev_config_head),
49 };
50 
51 /*
52  * List of raid bdev in configured list, these raid bdevs are registered with
53  * bdev layer
54  */
55 struct raid_configured_tailq	g_raid_bdev_configured_list = TAILQ_HEAD_INITIALIZER(
56 			g_raid_bdev_configured_list);
57 
58 /* List of raid bdev in configuring list */
59 struct raid_configuring_tailq	g_raid_bdev_configuring_list = TAILQ_HEAD_INITIALIZER(
60 			g_raid_bdev_configuring_list);
61 
62 /* List of all raid bdevs */
63 struct raid_all_tailq		g_raid_bdev_list = TAILQ_HEAD_INITIALIZER(g_raid_bdev_list);
64 
65 /* List of all raid bdevs that are offline */
66 struct raid_offline_tailq	g_raid_bdev_offline_list = TAILQ_HEAD_INITIALIZER(
67 			g_raid_bdev_offline_list);
68 
69 static TAILQ_HEAD(, raid_bdev_module) g_raid_modules = TAILQ_HEAD_INITIALIZER(g_raid_modules);
70 
71 static struct raid_bdev_module *raid_bdev_module_find(enum raid_level level)
72 {
73 	struct raid_bdev_module *raid_module;
74 
75 	TAILQ_FOREACH(raid_module, &g_raid_modules, link) {
76 		if (raid_module->level == level) {
77 			return raid_module;
78 		}
79 	}
80 
81 	return NULL;
82 }
83 
84 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module)
85 {
86 	if (raid_bdev_module_find(raid_module->level) != NULL) {
87 		SPDK_ERRLOG("module for raid level '%s' already registered.\n",
88 			    raid_bdev_level_to_str(raid_module->level));
89 		assert(false);
90 	} else {
91 		TAILQ_INSERT_TAIL(&g_raid_modules, raid_module, link);
92 	}
93 }
94 
95 /* Function declarations */
96 static void	raid_bdev_examine(struct spdk_bdev *bdev);
97 static int	raid_bdev_init(void);
98 static void	raid_bdev_deconfigure(struct raid_bdev *raid_bdev,
99 				      raid_bdev_destruct_cb cb_fn, void *cb_arg);
100 static void	raid_bdev_remove_base_bdev(void *ctx);
101 
102 /*
103  * brief:
104  * raid_bdev_create_cb function is a cb function for raid bdev which creates the
105  * hierarchy from raid bdev to base bdev io channels. It will be called per core
106  * params:
107  * io_device - pointer to raid bdev io device represented by raid_bdev
108  * ctx_buf - pointer to context buffer for raid bdev io channel
109  * returns:
110  * 0 - success
111  * non zero - failure
112  */
113 static int
114 raid_bdev_create_cb(void *io_device, void *ctx_buf)
115 {
116 	struct raid_bdev            *raid_bdev = io_device;
117 	struct raid_bdev_io_channel *raid_ch = ctx_buf;
118 	uint8_t i;
119 
120 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_create_cb, %p\n", raid_ch);
121 
122 	assert(raid_bdev != NULL);
123 	assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE);
124 
125 	raid_ch->num_channels = raid_bdev->num_base_bdevs;
126 
127 	raid_ch->base_channel = calloc(raid_ch->num_channels,
128 				       sizeof(struct spdk_io_channel *));
129 	if (!raid_ch->base_channel) {
130 		SPDK_ERRLOG("Unable to allocate base bdevs io channel\n");
131 		return -ENOMEM;
132 	}
133 	for (i = 0; i < raid_ch->num_channels; i++) {
134 		/*
135 		 * Get the spdk_io_channel for all the base bdevs. This is used during
136 		 * split logic to send the respective child bdev ios to respective base
137 		 * bdev io channel.
138 		 */
139 		raid_ch->base_channel[i] = spdk_bdev_get_io_channel(
140 						   raid_bdev->base_bdev_info[i].desc);
141 		if (!raid_ch->base_channel[i]) {
142 			uint8_t j;
143 
144 			for (j = 0; j < i; j++) {
145 				spdk_put_io_channel(raid_ch->base_channel[j]);
146 			}
147 			free(raid_ch->base_channel);
148 			raid_ch->base_channel = NULL;
149 			SPDK_ERRLOG("Unable to create io channel for base bdev\n");
150 			return -ENOMEM;
151 		}
152 	}
153 
154 	return 0;
155 }
156 
157 /*
158  * brief:
159  * raid_bdev_destroy_cb function is a cb function for raid bdev which deletes the
160  * hierarchy from raid bdev to base bdev io channels. It will be called per core
161  * params:
162  * io_device - pointer to raid bdev io device represented by raid_bdev
163  * ctx_buf - pointer to context buffer for raid bdev io channel
164  * returns:
165  * none
166  */
167 static void
168 raid_bdev_destroy_cb(void *io_device, void *ctx_buf)
169 {
170 	struct raid_bdev_io_channel *raid_ch = ctx_buf;
171 	uint8_t i;
172 
173 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_destroy_cb\n");
174 
175 	assert(raid_ch != NULL);
176 	assert(raid_ch->base_channel);
177 	for (i = 0; i < raid_ch->num_channels; i++) {
178 		/* Free base bdev channels */
179 		assert(raid_ch->base_channel[i] != NULL);
180 		spdk_put_io_channel(raid_ch->base_channel[i]);
181 	}
182 	free(raid_ch->base_channel);
183 	raid_ch->base_channel = NULL;
184 }
185 
186 /*
187  * brief:
188  * raid_bdev_cleanup is used to cleanup and free raid_bdev related data
189  * structures.
190  * params:
191  * raid_bdev - pointer to raid_bdev
192  * returns:
193  * none
194  */
195 static void
196 raid_bdev_cleanup(struct raid_bdev *raid_bdev)
197 {
198 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_cleanup, %p name %s, state %u, config %p\n",
199 		      raid_bdev,
200 		      raid_bdev->bdev.name, raid_bdev->state, raid_bdev->config);
201 	if (raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) {
202 		TAILQ_REMOVE(&g_raid_bdev_configuring_list, raid_bdev, state_link);
203 	} else if (raid_bdev->state == RAID_BDEV_STATE_OFFLINE) {
204 		TAILQ_REMOVE(&g_raid_bdev_offline_list, raid_bdev, state_link);
205 	} else {
206 		assert(0);
207 	}
208 	TAILQ_REMOVE(&g_raid_bdev_list, raid_bdev, global_link);
209 	free(raid_bdev->bdev.name);
210 	free(raid_bdev->base_bdev_info);
211 	if (raid_bdev->config) {
212 		raid_bdev->config->raid_bdev = NULL;
213 	}
214 	free(raid_bdev);
215 }
216 
217 /*
218  * brief:
219  * free resource of base bdev for raid bdev
220  * params:
221  * raid_bdev - pointer to raid bdev
222  * base_bdev_slot - position to base bdev in raid bdev
223  * returns:
224  * 0 - success
225  * non zero - failure
226  */
227 static void
228 raid_bdev_free_base_bdev_resource(struct raid_bdev *raid_bdev, uint8_t base_bdev_slot)
229 {
230 	struct raid_base_bdev_info *info;
231 
232 	info = &raid_bdev->base_bdev_info[base_bdev_slot];
233 
234 	spdk_bdev_module_release_bdev(info->bdev);
235 	spdk_bdev_close(info->desc);
236 	info->desc = NULL;
237 	info->bdev = NULL;
238 
239 	assert(raid_bdev->num_base_bdevs_discovered);
240 	raid_bdev->num_base_bdevs_discovered--;
241 }
242 
243 /*
244  * brief:
245  * raid_bdev_destruct is the destruct function table pointer for raid bdev
246  * params:
247  * ctxt - pointer to raid_bdev
248  * returns:
249  * 0 - success
250  * non zero - failure
251  */
252 static int
253 raid_bdev_destruct(void *ctxt)
254 {
255 	struct raid_bdev *raid_bdev = ctxt;
256 	uint8_t i;
257 
258 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_destruct\n");
259 
260 	raid_bdev->destruct_called = true;
261 	for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
262 		/*
263 		 * Close all base bdev descriptors for which call has come from below
264 		 * layers.  Also close the descriptors if we have started shutdown.
265 		 */
266 		if (g_shutdown_started ||
267 		    ((raid_bdev->base_bdev_info[i].remove_scheduled == true) &&
268 		     (raid_bdev->base_bdev_info[i].bdev != NULL))) {
269 			raid_bdev_free_base_bdev_resource(raid_bdev, i);
270 		}
271 	}
272 
273 	if (g_shutdown_started) {
274 		TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link);
275 		if (raid_bdev->module->stop != NULL) {
276 			raid_bdev->module->stop(raid_bdev);
277 		}
278 		raid_bdev->state = RAID_BDEV_STATE_OFFLINE;
279 		TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link);
280 	}
281 
282 	spdk_io_device_unregister(raid_bdev, NULL);
283 
284 	if (raid_bdev->num_base_bdevs_discovered == 0) {
285 		/* Free raid_bdev when there are no base bdevs left */
286 		SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev base bdevs is 0, going to free all in destruct\n");
287 		raid_bdev_cleanup(raid_bdev);
288 	}
289 
290 	return 0;
291 }
292 
293 void
294 raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status)
295 {
296 	struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io);
297 
298 	spdk_bdev_io_complete(bdev_io, status);
299 }
300 
301 /*
302  * brief:
303  * raid_bdev_base_io_completion is the completion callback for member disk requests
304  * params:
305  * bdev_io - pointer to member disk requested bdev_io
306  * success - true if successful, false if unsuccessful
307  * cb_arg - callback argument (parent raid_bdev_io)
308  * returns:
309  * none
310  */
311 void
312 raid_bdev_base_io_completion(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
313 {
314 	struct raid_bdev_io *raid_io = cb_arg;
315 
316 	spdk_bdev_free_io(bdev_io);
317 
318 	if (!success) {
319 		raid_io->base_bdev_io_status = SPDK_BDEV_IO_STATUS_FAILED;
320 	}
321 
322 	raid_io->base_bdev_io_completed++;
323 	if (raid_io->base_bdev_io_completed == raid_io->base_bdev_io_expected) {
324 		raid_bdev_io_complete(raid_io, raid_io->base_bdev_io_status);
325 	}
326 }
327 
328 /*
329  * brief:
330  * raid_bdev_queue_io_wait function processes the IO which failed to submit.
331  * It will try to queue the IOs after storing the context to bdev wait queue logic.
332  * params:
333  * raid_io - pointer to raid_bdev_io
334  * bdev - the block device that the IO is submitted to
335  * ch - io channel
336  * cb_fn - callback when the spdk_bdev_io for bdev becomes available
337  * returns:
338  * none
339  */
340 void
341 raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
342 			struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn)
343 {
344 	raid_io->waitq_entry.bdev = bdev;
345 	raid_io->waitq_entry.cb_fn = cb_fn;
346 	raid_io->waitq_entry.cb_arg = raid_io;
347 	spdk_bdev_queue_io_wait(bdev, ch, &raid_io->waitq_entry);
348 }
349 
350 static void
351 raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io);
352 
353 static void
354 _raid_bdev_submit_reset_request(void *_raid_io)
355 {
356 	struct raid_bdev_io *raid_io = _raid_io;
357 
358 	raid_bdev_submit_reset_request(raid_io);
359 }
360 
361 /*
362  * brief:
363  * raid_bdev_submit_reset_request function submits reset requests
364  * to member disks; it will submit as many as possible unless a reset fails with -ENOMEM, in
365  * which case it will queue it for later submission
366  * params:
367  * raid_io
368  * returns:
369  * none
370  */
371 static void
372 raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io)
373 {
374 	struct raid_bdev		*raid_bdev;
375 	int				ret;
376 	uint8_t				i;
377 	struct raid_base_bdev_info	*base_info;
378 	struct spdk_io_channel		*base_ch;
379 
380 	raid_bdev = raid_io->raid_bdev;
381 
382 	raid_io->base_bdev_io_expected = raid_bdev->num_base_bdevs;
383 
384 	while (raid_io->base_bdev_io_submitted < raid_bdev->num_base_bdevs) {
385 		i = raid_io->base_bdev_io_submitted;
386 		base_info = &raid_bdev->base_bdev_info[i];
387 		base_ch = raid_io->raid_ch->base_channel[i];
388 		ret = spdk_bdev_reset(base_info->desc, base_ch,
389 				      raid_bdev_base_io_completion, raid_io);
390 		if (ret == 0) {
391 			raid_io->base_bdev_io_submitted++;
392 		} else if (ret == -ENOMEM) {
393 			raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch,
394 						_raid_bdev_submit_reset_request);
395 			return;
396 		} else {
397 			SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n");
398 			assert(false);
399 			raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
400 			return;
401 		}
402 	}
403 }
404 
405 /*
406  * brief:
407  * Callback function to spdk_bdev_io_get_buf.
408  * params:
409  * ch - pointer to raid bdev io channel
410  * bdev_io - pointer to parent bdev_io on raid bdev device
411  * success - True if buffer is allocated or false otherwise.
412  * returns:
413  * none
414  */
415 static void
416 raid_bdev_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
417 		     bool success)
418 {
419 	struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx;
420 
421 	if (!success) {
422 		raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
423 		return;
424 	}
425 
426 	raid_io->raid_bdev->module->submit_rw_request(raid_io);
427 }
428 
429 /*
430  * brief:
431  * raid_bdev_submit_request function is the submit_request function pointer of
432  * raid bdev function table. This is used to submit the io on raid_bdev to below
433  * layers.
434  * params:
435  * ch - pointer to raid bdev io channel
436  * bdev_io - pointer to parent bdev_io on raid bdev device
437  * returns:
438  * none
439  */
440 static void
441 raid_bdev_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
442 {
443 	struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx;
444 
445 	raid_io->raid_bdev = bdev_io->bdev->ctxt;
446 	raid_io->raid_ch = spdk_io_channel_get_ctx(ch);
447 	raid_io->base_bdev_io_submitted = 0;
448 	raid_io->base_bdev_io_completed = 0;
449 	raid_io->base_bdev_io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
450 
451 	switch (bdev_io->type) {
452 	case SPDK_BDEV_IO_TYPE_READ:
453 		spdk_bdev_io_get_buf(bdev_io, raid_bdev_get_buf_cb,
454 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
455 		break;
456 	case SPDK_BDEV_IO_TYPE_WRITE:
457 		raid_io->raid_bdev->module->submit_rw_request(raid_io);
458 		break;
459 
460 	case SPDK_BDEV_IO_TYPE_RESET:
461 		raid_bdev_submit_reset_request(raid_io);
462 		break;
463 
464 	case SPDK_BDEV_IO_TYPE_FLUSH:
465 	case SPDK_BDEV_IO_TYPE_UNMAP:
466 		raid_io->raid_bdev->module->submit_null_payload_request(raid_io);
467 		break;
468 
469 	default:
470 		SPDK_ERRLOG("submit request, invalid io type %u\n", bdev_io->type);
471 		raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
472 		break;
473 	}
474 }
475 
476 /*
477  * brief:
478  * _raid_bdev_io_type_supported checks whether io_type is supported in
479  * all base bdev modules of raid bdev module. If anyone among the base_bdevs
480  * doesn't support, the raid device doesn't supports.
481  *
482  * params:
483  * raid_bdev - pointer to raid bdev context
484  * io_type - io type
485  * returns:
486  * true - io_type is supported
487  * false - io_type is not supported
488  */
489 inline static bool
490 _raid_bdev_io_type_supported(struct raid_bdev *raid_bdev, enum spdk_bdev_io_type io_type)
491 {
492 	uint8_t i;
493 
494 	for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
495 		if (raid_bdev->base_bdev_info[i].bdev == NULL) {
496 			assert(false);
497 			continue;
498 		}
499 
500 		if (spdk_bdev_io_type_supported(raid_bdev->base_bdev_info[i].bdev,
501 						io_type) == false) {
502 			return false;
503 		}
504 	}
505 
506 	return true;
507 }
508 
509 /*
510  * brief:
511  * raid_bdev_io_type_supported is the io_supported function for bdev function
512  * table which returns whether the particular io type is supported or not by
513  * raid bdev module
514  * params:
515  * ctx - pointer to raid bdev context
516  * type - io type
517  * returns:
518  * true - io_type is supported
519  * false - io_type is not supported
520  */
521 static bool
522 raid_bdev_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
523 {
524 	switch (io_type) {
525 	case SPDK_BDEV_IO_TYPE_READ:
526 	case SPDK_BDEV_IO_TYPE_WRITE:
527 		return true;
528 
529 	case SPDK_BDEV_IO_TYPE_FLUSH:
530 	case SPDK_BDEV_IO_TYPE_RESET:
531 	case SPDK_BDEV_IO_TYPE_UNMAP:
532 		return _raid_bdev_io_type_supported(ctx, io_type);
533 
534 	default:
535 		return false;
536 	}
537 
538 	return false;
539 }
540 
541 /*
542  * brief:
543  * raid_bdev_get_io_channel is the get_io_channel function table pointer for
544  * raid bdev. This is used to return the io channel for this raid bdev
545  * params:
546  * ctxt - pointer to raid_bdev
547  * returns:
548  * pointer to io channel for raid bdev
549  */
550 static struct spdk_io_channel *
551 raid_bdev_get_io_channel(void *ctxt)
552 {
553 	struct raid_bdev *raid_bdev = ctxt;
554 
555 	return spdk_get_io_channel(raid_bdev);
556 }
557 
558 /*
559  * brief:
560  * raid_bdev_dump_info_json is the function table pointer for raid bdev
561  * params:
562  * ctx - pointer to raid_bdev
563  * w - pointer to json context
564  * returns:
565  * 0 - success
566  * non zero - failure
567  */
568 static int
569 raid_bdev_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
570 {
571 	struct raid_bdev *raid_bdev = ctx;
572 	uint8_t i;
573 
574 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_dump_config_json\n");
575 	assert(raid_bdev != NULL);
576 
577 	/* Dump the raid bdev configuration related information */
578 	spdk_json_write_named_object_begin(w, "raid");
579 	spdk_json_write_named_uint32(w, "strip_size", raid_bdev->strip_size);
580 	spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb);
581 	spdk_json_write_named_uint32(w, "state", raid_bdev->state);
582 	spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
583 	spdk_json_write_named_uint32(w, "destruct_called", raid_bdev->destruct_called);
584 	spdk_json_write_named_uint32(w, "num_base_bdevs", raid_bdev->num_base_bdevs);
585 	spdk_json_write_named_uint32(w, "num_base_bdevs_discovered", raid_bdev->num_base_bdevs_discovered);
586 	spdk_json_write_name(w, "base_bdevs_list");
587 	spdk_json_write_array_begin(w);
588 	for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
589 		if (raid_bdev->base_bdev_info[i].bdev) {
590 			spdk_json_write_string(w, raid_bdev->base_bdev_info[i].bdev->name);
591 		} else {
592 			spdk_json_write_null(w);
593 		}
594 	}
595 	spdk_json_write_array_end(w);
596 	spdk_json_write_object_end(w);
597 
598 	return 0;
599 }
600 
601 /*
602  * brief:
603  * raid_bdev_write_config_json is the function table pointer for raid bdev
604  * params:
605  * bdev - pointer to spdk_bdev
606  * w - pointer to json context
607  * returns:
608  * none
609  */
610 static void
611 raid_bdev_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
612 {
613 	struct raid_bdev *raid_bdev = bdev->ctxt;
614 	struct spdk_bdev *base;
615 	uint8_t i;
616 
617 	spdk_json_write_object_begin(w);
618 
619 	spdk_json_write_named_string(w, "method", "bdev_raid_create");
620 
621 	spdk_json_write_named_object_begin(w, "params");
622 	spdk_json_write_named_string(w, "name", bdev->name);
623 	spdk_json_write_named_uint32(w, "strip_size", raid_bdev->strip_size_kb);
624 	spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
625 
626 	spdk_json_write_named_array_begin(w, "base_bdevs");
627 	for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
628 		base = raid_bdev->base_bdev_info[i].bdev;
629 		if (base) {
630 			spdk_json_write_string(w, base->name);
631 		}
632 	}
633 	spdk_json_write_array_end(w);
634 	spdk_json_write_object_end(w);
635 
636 	spdk_json_write_object_end(w);
637 }
638 
639 /* g_raid_bdev_fn_table is the function table for raid bdev */
640 static const struct spdk_bdev_fn_table g_raid_bdev_fn_table = {
641 	.destruct		= raid_bdev_destruct,
642 	.submit_request		= raid_bdev_submit_request,
643 	.io_type_supported	= raid_bdev_io_type_supported,
644 	.get_io_channel		= raid_bdev_get_io_channel,
645 	.dump_info_json		= raid_bdev_dump_info_json,
646 	.write_config_json	= raid_bdev_write_config_json,
647 };
648 
649 /*
650  * brief:
651  * raid_bdev_config_cleanup function is used to free memory for one raid_bdev in configuration
652  * params:
653  * raid_cfg - pointer to raid_bdev_config structure
654  * returns:
655  * none
656  */
657 void
658 raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg)
659 {
660 	uint8_t i;
661 
662 	TAILQ_REMOVE(&g_raid_config.raid_bdev_config_head, raid_cfg, link);
663 	g_raid_config.total_raid_bdev--;
664 
665 	if (raid_cfg->base_bdev) {
666 		for (i = 0; i < raid_cfg->num_base_bdevs; i++) {
667 			free(raid_cfg->base_bdev[i].name);
668 		}
669 		free(raid_cfg->base_bdev);
670 	}
671 	free(raid_cfg->name);
672 	free(raid_cfg);
673 }
674 
675 /*
676  * brief:
677  * raid_bdev_free is the raid bdev function table function pointer. This is
678  * called on bdev free path
679  * params:
680  * none
681  * returns:
682  * none
683  */
684 static void
685 raid_bdev_free(void)
686 {
687 	struct raid_bdev_config *raid_cfg, *tmp;
688 
689 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_free\n");
690 	TAILQ_FOREACH_SAFE(raid_cfg, &g_raid_config.raid_bdev_config_head, link, tmp) {
691 		raid_bdev_config_cleanup(raid_cfg);
692 	}
693 }
694 
695 /* brief
696  * raid_bdev_config_find_by_name is a helper function to find raid bdev config
697  * by name as key.
698  *
699  * params:
700  * raid_name - name for raid bdev.
701  */
702 struct raid_bdev_config *
703 raid_bdev_config_find_by_name(const char *raid_name)
704 {
705 	struct raid_bdev_config *raid_cfg;
706 
707 	TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) {
708 		if (!strcmp(raid_cfg->name, raid_name)) {
709 			return raid_cfg;
710 		}
711 	}
712 
713 	return raid_cfg;
714 }
715 
716 /*
717  * brief
718  * raid_bdev_config_add function adds config for newly created raid bdev.
719  *
720  * params:
721  * raid_name - name for raid bdev.
722  * strip_size - strip size in KB
723  * num_base_bdevs - number of base bdevs.
724  * level - raid level.
725  * _raid_cfg - Pointer to newly added configuration
726  */
727 int
728 raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs,
729 		     enum raid_level level, struct raid_bdev_config **_raid_cfg)
730 {
731 	struct raid_bdev_config *raid_cfg;
732 
733 	raid_cfg = raid_bdev_config_find_by_name(raid_name);
734 	if (raid_cfg != NULL) {
735 		SPDK_ERRLOG("Duplicate raid bdev name found in config file %s\n",
736 			    raid_name);
737 		return -EEXIST;
738 	}
739 
740 	if (spdk_u32_is_pow2(strip_size) == false) {
741 		SPDK_ERRLOG("Invalid strip size %" PRIu32 "\n", strip_size);
742 		return -EINVAL;
743 	}
744 
745 	if (num_base_bdevs == 0) {
746 		SPDK_ERRLOG("Invalid base device count %u\n", num_base_bdevs);
747 		return -EINVAL;
748 	}
749 
750 	raid_cfg = calloc(1, sizeof(*raid_cfg));
751 	if (raid_cfg == NULL) {
752 		SPDK_ERRLOG("unable to allocate memory\n");
753 		return -ENOMEM;
754 	}
755 
756 	raid_cfg->name = strdup(raid_name);
757 	if (!raid_cfg->name) {
758 		free(raid_cfg);
759 		SPDK_ERRLOG("unable to allocate memory\n");
760 		return -ENOMEM;
761 	}
762 	raid_cfg->strip_size = strip_size;
763 	raid_cfg->num_base_bdevs = num_base_bdevs;
764 	raid_cfg->level = level;
765 
766 	raid_cfg->base_bdev = calloc(num_base_bdevs, sizeof(*raid_cfg->base_bdev));
767 	if (raid_cfg->base_bdev == NULL) {
768 		free(raid_cfg->name);
769 		free(raid_cfg);
770 		SPDK_ERRLOG("unable to allocate memory\n");
771 		return -ENOMEM;
772 	}
773 
774 	TAILQ_INSERT_TAIL(&g_raid_config.raid_bdev_config_head, raid_cfg, link);
775 	g_raid_config.total_raid_bdev++;
776 
777 	*_raid_cfg = raid_cfg;
778 	return 0;
779 }
780 
781 /*
782  * brief:
783  * raid_bdev_config_add_base_bdev function add base bdev to raid bdev config.
784  *
785  * params:
786  * raid_cfg - pointer to raid bdev configuration
787  * base_bdev_name - name of base bdev
788  * slot - Position to add base bdev
789  */
790 int
791 raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg, const char *base_bdev_name,
792 			       uint8_t slot)
793 {
794 	uint8_t i;
795 	struct raid_bdev_config *tmp;
796 
797 	if (slot >= raid_cfg->num_base_bdevs) {
798 		return -EINVAL;
799 	}
800 
801 	TAILQ_FOREACH(tmp, &g_raid_config.raid_bdev_config_head, link) {
802 		for (i = 0; i < tmp->num_base_bdevs; i++) {
803 			if (tmp->base_bdev[i].name != NULL) {
804 				if (!strcmp(tmp->base_bdev[i].name, base_bdev_name)) {
805 					SPDK_ERRLOG("duplicate base bdev name %s mentioned\n",
806 						    base_bdev_name);
807 					return -EEXIST;
808 				}
809 			}
810 		}
811 	}
812 
813 	raid_cfg->base_bdev[slot].name = strdup(base_bdev_name);
814 	if (raid_cfg->base_bdev[slot].name == NULL) {
815 		SPDK_ERRLOG("unable to allocate memory\n");
816 		return -ENOMEM;
817 	}
818 
819 	return 0;
820 }
821 
822 static struct {
823 	const char *name;
824 	enum raid_level value;
825 } g_raid_level_names[] = {
826 	{ "raid0", RAID0 },
827 	{ "0", RAID0 },
828 	{ }
829 };
830 
831 enum raid_level raid_bdev_parse_raid_level(const char *str)
832 {
833 	unsigned int i;
834 
835 	for (i = 0; g_raid_level_names[i].name != NULL; i++) {
836 		if (strcasecmp(g_raid_level_names[i].name, str) == 0) {
837 			return g_raid_level_names[i].value;
838 		}
839 	}
840 
841 	return INVALID_RAID_LEVEL;
842 }
843 
844 const char *
845 raid_bdev_level_to_str(enum raid_level level)
846 {
847 	unsigned int i;
848 
849 	for (i = 0; g_raid_level_names[i].name != NULL; i++) {
850 		if (g_raid_level_names[i].value == level) {
851 			return g_raid_level_names[i].name;
852 		}
853 	}
854 
855 	return "";
856 }
857 
858 /*
859  * brief:
860  * raid_bdev_parse_raid is used to parse the raid bdev from config file based on
861  * pre-defined raid bdev format in config file.
862  * Format of config file:
863  *   [RAID1]
864  *   Name raid1
865  *   StripSize 64
866  *   NumDevices 2
867  *   RaidLevel 0
868  *   Devices Nvme0n1 Nvme1n1
869  *
870  *   [RAID2]
871  *   Name raid2
872  *   StripSize 64
873  *   NumDevices 3
874  *   RaidLevel 0
875  *   Devices Nvme2n1 Nvme3n1 Nvme4n1
876  *
877  * params:
878  * conf_section - pointer to config section
879  * returns:
880  * 0 - success
881  * non zero - failure
882  */
883 static int
884 raid_bdev_parse_raid(struct spdk_conf_section *conf_section)
885 {
886 	const char *raid_name;
887 	uint32_t strip_size;
888 	uint8_t num_base_bdevs;
889 	const char *raid_level_str;
890 	enum raid_level level;
891 	const char *base_bdev_name;
892 	struct raid_bdev_config *raid_cfg;
893 	int rc, i, val;
894 
895 	raid_name = spdk_conf_section_get_val(conf_section, "Name");
896 	if (raid_name == NULL) {
897 		SPDK_ERRLOG("raid_name is null\n");
898 		return -EINVAL;
899 	}
900 
901 	val = spdk_conf_section_get_intval(conf_section, "StripSize");
902 	if (val < 0) {
903 		return -EINVAL;
904 	}
905 	strip_size = val;
906 
907 	val = spdk_conf_section_get_intval(conf_section, "NumDevices");
908 	if (val < 0) {
909 		return -EINVAL;
910 	}
911 	num_base_bdevs = val;
912 
913 	raid_level_str = spdk_conf_section_get_val(conf_section, "RaidLevel");
914 	if (raid_level_str == NULL) {
915 		SPDK_ERRLOG("Missing RaidLevel\n");
916 		return -EINVAL;
917 	}
918 	level = raid_bdev_parse_raid_level(raid_level_str);
919 	if (level == INVALID_RAID_LEVEL) {
920 		SPDK_ERRLOG("Invalid RaidLevel\n");
921 		return -EINVAL;
922 	}
923 
924 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "%s %" PRIu32 " %u %u\n",
925 		      raid_name, strip_size, num_base_bdevs, level);
926 
927 	rc = raid_bdev_config_add(raid_name, strip_size, num_base_bdevs, level,
928 				  &raid_cfg);
929 	if (rc != 0) {
930 		SPDK_ERRLOG("Failed to add raid bdev config\n");
931 		return rc;
932 	}
933 
934 	for (i = 0; true; i++) {
935 		base_bdev_name = spdk_conf_section_get_nmval(conf_section, "Devices", 0, i);
936 		if (base_bdev_name == NULL) {
937 			break;
938 		}
939 		if (i >= num_base_bdevs) {
940 			raid_bdev_config_cleanup(raid_cfg);
941 			SPDK_ERRLOG("Number of devices mentioned is more than count\n");
942 			return -EINVAL;
943 		}
944 
945 		rc = raid_bdev_config_add_base_bdev(raid_cfg, base_bdev_name, i);
946 		if (rc != 0) {
947 			raid_bdev_config_cleanup(raid_cfg);
948 			SPDK_ERRLOG("Failed to add base bdev to raid bdev config\n");
949 			return rc;
950 		}
951 	}
952 
953 	if (i != raid_cfg->num_base_bdevs) {
954 		raid_bdev_config_cleanup(raid_cfg);
955 		SPDK_ERRLOG("Number of devices mentioned is less than count\n");
956 		return -EINVAL;
957 	}
958 
959 	rc = raid_bdev_create(raid_cfg);
960 	if (rc != 0) {
961 		raid_bdev_config_cleanup(raid_cfg);
962 		SPDK_ERRLOG("Failed to create raid bdev\n");
963 		return rc;
964 	}
965 
966 	rc = raid_bdev_add_base_devices(raid_cfg);
967 	if (rc != 0) {
968 		SPDK_ERRLOG("Failed to add any base bdev to raid bdev\n");
969 		/* Config is not removed in this case. */
970 	}
971 
972 	return 0;
973 }
974 
975 /*
976  * brief:
977  * raid_bdev_parse_config is used to find the raid bdev config section and parse it
978  * Format of config file:
979  * params:
980  * none
981  * returns:
982  * 0 - success
983  * non zero - failure
984  */
985 static int
986 raid_bdev_parse_config(void)
987 {
988 	int                      ret;
989 	struct spdk_conf_section *conf_section;
990 
991 	conf_section = spdk_conf_first_section(NULL);
992 	while (conf_section != NULL) {
993 		if (spdk_conf_section_match_prefix(conf_section, "RAID")) {
994 			ret = raid_bdev_parse_raid(conf_section);
995 			if (ret < 0) {
996 				SPDK_ERRLOG("Unable to parse raid bdev section\n");
997 				return ret;
998 			}
999 		}
1000 		conf_section = spdk_conf_next_section(conf_section);
1001 	}
1002 
1003 	return 0;
1004 }
1005 
1006 /*
1007  * brief:
1008  * raid_bdev_fini_start is called when bdev layer is starting the
1009  * shutdown process
1010  * params:
1011  * none
1012  * returns:
1013  * none
1014  */
1015 static void
1016 raid_bdev_fini_start(void)
1017 {
1018 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_fini_start\n");
1019 	g_shutdown_started = true;
1020 }
1021 
1022 /*
1023  * brief:
1024  * raid_bdev_exit is called on raid bdev module exit time by bdev layer
1025  * params:
1026  * none
1027  * returns:
1028  * none
1029  */
1030 static void
1031 raid_bdev_exit(void)
1032 {
1033 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_exit\n");
1034 	raid_bdev_free();
1035 }
1036 
1037 /*
1038  * brief:
1039  * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid
1040  * module
1041  * params:
1042  * none
1043  * returns:
1044  * size of spdk_bdev_io context for raid
1045  */
1046 static int
1047 raid_bdev_get_ctx_size(void)
1048 {
1049 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_get_ctx_size\n");
1050 	return sizeof(struct raid_bdev_io);
1051 }
1052 
1053 /*
1054  * brief:
1055  * raid_bdev_get_running_config is used to get the configuration options.
1056  *
1057  * params:
1058  * fp - The pointer to a file that will be written to the configuration options.
1059  * returns:
1060  * none
1061  */
1062 static void
1063 raid_bdev_get_running_config(FILE *fp)
1064 {
1065 	struct raid_bdev *raid_bdev;
1066 	struct spdk_bdev *base;
1067 	int index = 1;
1068 	uint8_t i;
1069 
1070 	TAILQ_FOREACH(raid_bdev, &g_raid_bdev_configured_list, state_link) {
1071 		fprintf(fp,
1072 			"\n"
1073 			"[RAID%d]\n"
1074 			"  Name %s\n"
1075 			"  StripSize %" PRIu32 "\n"
1076 			"  NumDevices %u\n"
1077 			"  RaidLevel %s\n",
1078 			index, raid_bdev->bdev.name, raid_bdev->strip_size_kb,
1079 			raid_bdev->num_base_bdevs,
1080 			raid_bdev_level_to_str(raid_bdev->level));
1081 		fprintf(fp,
1082 			"  Devices ");
1083 		for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
1084 			base = raid_bdev->base_bdev_info[i].bdev;
1085 			if (base) {
1086 				fprintf(fp,
1087 					"%s ",
1088 					base->name);
1089 			}
1090 		}
1091 		fprintf(fp,
1092 			"\n");
1093 		index++;
1094 	}
1095 }
1096 
1097 /*
1098  * brief:
1099  * raid_bdev_can_claim_bdev is the function to check if this base_bdev can be
1100  * claimed by raid bdev or not.
1101  * params:
1102  * bdev_name - represents base bdev name
1103  * _raid_cfg - pointer to raid bdev config parsed from config file
1104  * base_bdev_slot - if bdev can be claimed, it represents the base_bdev correct
1105  * slot. This field is only valid if return value of this function is true
1106  * returns:
1107  * true - if bdev can be claimed
1108  * false - if bdev can't be claimed
1109  */
1110 static bool
1111 raid_bdev_can_claim_bdev(const char *bdev_name, struct raid_bdev_config **_raid_cfg,
1112 			 uint8_t *base_bdev_slot)
1113 {
1114 	struct raid_bdev_config *raid_cfg;
1115 	uint8_t i;
1116 
1117 	TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) {
1118 		for (i = 0; i < raid_cfg->num_base_bdevs; i++) {
1119 			/*
1120 			 * Check if the base bdev name is part of raid bdev configuration.
1121 			 * If match is found then return true and the slot information where
1122 			 * this base bdev should be inserted in raid bdev
1123 			 */
1124 			if (!strcmp(bdev_name, raid_cfg->base_bdev[i].name)) {
1125 				*_raid_cfg = raid_cfg;
1126 				*base_bdev_slot = i;
1127 				return true;
1128 			}
1129 		}
1130 	}
1131 
1132 	return false;
1133 }
1134 
1135 
1136 static struct spdk_bdev_module g_raid_if = {
1137 	.name = "raid",
1138 	.module_init = raid_bdev_init,
1139 	.fini_start = raid_bdev_fini_start,
1140 	.module_fini = raid_bdev_exit,
1141 	.get_ctx_size = raid_bdev_get_ctx_size,
1142 	.examine_config = raid_bdev_examine,
1143 	.config_text = raid_bdev_get_running_config,
1144 	.async_init = false,
1145 	.async_fini = false,
1146 };
1147 SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if)
1148 
1149 /*
1150  * brief:
1151  * raid_bdev_init is the initialization function for raid bdev module
1152  * params:
1153  * none
1154  * returns:
1155  * 0 - success
1156  * non zero - failure
1157  */
1158 static int
1159 raid_bdev_init(void)
1160 {
1161 	int ret;
1162 
1163 	/* Parse config file for raids */
1164 	ret = raid_bdev_parse_config();
1165 	if (ret < 0) {
1166 		SPDK_ERRLOG("raid bdev init failed parsing\n");
1167 		raid_bdev_free();
1168 		return ret;
1169 	}
1170 
1171 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_init completed successfully\n");
1172 
1173 	return 0;
1174 }
1175 
1176 /*
1177  * brief:
1178  * raid_bdev_create allocates raid bdev based on passed configuration
1179  * params:
1180  * raid_cfg - configuration of raid bdev
1181  * returns:
1182  * 0 - success
1183  * non zero - failure
1184  */
1185 int
1186 raid_bdev_create(struct raid_bdev_config *raid_cfg)
1187 {
1188 	struct raid_bdev *raid_bdev;
1189 	struct spdk_bdev *raid_bdev_gen;
1190 
1191 	raid_bdev = calloc(1, sizeof(*raid_bdev));
1192 	if (!raid_bdev) {
1193 		SPDK_ERRLOG("Unable to allocate memory for raid bdev\n");
1194 		return -ENOMEM;
1195 	}
1196 
1197 	assert(raid_cfg->num_base_bdevs != 0);
1198 	raid_bdev->num_base_bdevs = raid_cfg->num_base_bdevs;
1199 	raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs,
1200 					   sizeof(struct raid_base_bdev_info));
1201 	if (!raid_bdev->base_bdev_info) {
1202 		SPDK_ERRLOG("Unable able to allocate base bdev info\n");
1203 		free(raid_bdev);
1204 		return -ENOMEM;
1205 	}
1206 
1207 	/* strip_size_kb is from the rpc param.  strip_size is in blocks and used
1208 	 * intnerally and set later.
1209 	 */
1210 	raid_bdev->strip_size = 0;
1211 	raid_bdev->strip_size_kb = raid_cfg->strip_size;
1212 	raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
1213 	raid_bdev->config = raid_cfg;
1214 	raid_bdev->level = raid_cfg->level;
1215 
1216 	raid_bdev->module = raid_bdev_module_find(raid_bdev->level);
1217 	if (raid_bdev->module == NULL) {
1218 		SPDK_ERRLOG("Unsupported raid level '%d'\n", raid_bdev->level);
1219 		free(raid_bdev->base_bdev_info);
1220 		free(raid_bdev);
1221 		return -EINVAL;
1222 	}
1223 
1224 	raid_bdev_gen = &raid_bdev->bdev;
1225 
1226 	raid_bdev_gen->name = strdup(raid_cfg->name);
1227 	if (!raid_bdev_gen->name) {
1228 		SPDK_ERRLOG("Unable to allocate name for raid\n");
1229 		free(raid_bdev->base_bdev_info);
1230 		free(raid_bdev);
1231 		return -ENOMEM;
1232 	}
1233 
1234 	raid_bdev_gen->product_name = "Raid Volume";
1235 	raid_bdev_gen->ctxt = raid_bdev;
1236 	raid_bdev_gen->fn_table = &g_raid_bdev_fn_table;
1237 	raid_bdev_gen->module = &g_raid_if;
1238 	raid_bdev_gen->write_cache = 0;
1239 
1240 	TAILQ_INSERT_TAIL(&g_raid_bdev_configuring_list, raid_bdev, state_link);
1241 	TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link);
1242 
1243 	raid_cfg->raid_bdev = raid_bdev;
1244 
1245 	return 0;
1246 }
1247 
1248 /*
1249  * brief
1250  * raid_bdev_alloc_base_bdev_resource allocates resource of base bdev.
1251  * params:
1252  * raid_bdev - pointer to raid bdev
1253  * bdev - pointer to base bdev
1254  * base_bdev_slot - position to add base bdev
1255  * returns:
1256  * 0 - success
1257  * non zero - failure
1258  */
1259 static int
1260 raid_bdev_alloc_base_bdev_resource(struct raid_bdev *raid_bdev, struct spdk_bdev *bdev,
1261 				   uint8_t base_bdev_slot)
1262 {
1263 	struct spdk_bdev_desc *desc;
1264 	int rc;
1265 
1266 	rc = spdk_bdev_open(bdev, true, raid_bdev_remove_base_bdev, bdev, &desc);
1267 	if (rc != 0) {
1268 		SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", bdev->name);
1269 		return rc;
1270 	}
1271 
1272 	rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if);
1273 	if (rc != 0) {
1274 		SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n");
1275 		spdk_bdev_close(desc);
1276 		return rc;
1277 	}
1278 
1279 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "bdev %s is claimed\n", bdev->name);
1280 
1281 	assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE);
1282 	assert(base_bdev_slot < raid_bdev->num_base_bdevs);
1283 
1284 	raid_bdev->base_bdev_info[base_bdev_slot].bdev = bdev;
1285 	raid_bdev->base_bdev_info[base_bdev_slot].desc = desc;
1286 	raid_bdev->num_base_bdevs_discovered++;
1287 	assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs);
1288 
1289 	return 0;
1290 }
1291 
1292 /*
1293  * brief:
1294  * If raid bdev config is complete, then only register the raid bdev to
1295  * bdev layer and remove this raid bdev from configuring list and
1296  * insert the raid bdev to configured list
1297  * params:
1298  * raid_bdev - pointer to raid bdev
1299  * returns:
1300  * 0 - success
1301  * non zero - failure
1302  */
1303 static int
1304 raid_bdev_configure(struct raid_bdev *raid_bdev)
1305 {
1306 	uint32_t		blocklen;
1307 	struct spdk_bdev	*raid_bdev_gen;
1308 	int rc = 0;
1309 	uint8_t i;
1310 
1311 	assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING);
1312 	assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs);
1313 
1314 	blocklen = raid_bdev->base_bdev_info[0].bdev->blocklen;
1315 	for (i = 1; i < raid_bdev->num_base_bdevs; i++) {
1316 		/* Check blocklen for all base bdevs that it should be same */
1317 		if (blocklen != raid_bdev->base_bdev_info[i].bdev->blocklen) {
1318 			/*
1319 			 * Assumption is that all the base bdevs for any raid bdev should
1320 			 * have same blocklen
1321 			 */
1322 			SPDK_ERRLOG("Blocklen of various bdevs not matching\n");
1323 			return -EINVAL;
1324 		}
1325 	}
1326 
1327 	/* The strip_size_kb is read in from user in KB. Convert to blocks here for
1328 	 * internal use.
1329 	 */
1330 	raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / blocklen;
1331 	raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size);
1332 	raid_bdev->blocklen_shift = spdk_u32log2(blocklen);
1333 
1334 	raid_bdev_gen = &raid_bdev->bdev;
1335 	raid_bdev_gen->blocklen = blocklen;
1336 
1337 	rc = raid_bdev->module->start(raid_bdev);
1338 	if (rc != 0) {
1339 		SPDK_ERRLOG("raid module startup callback failed\n");
1340 		return rc;
1341 	}
1342 	raid_bdev->state = RAID_BDEV_STATE_ONLINE;
1343 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "io device register %p\n", raid_bdev);
1344 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "blockcnt %lu, blocklen %u\n",
1345 		      raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen);
1346 	spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb,
1347 				sizeof(struct raid_bdev_io_channel),
1348 				raid_bdev->bdev.name);
1349 	rc = spdk_bdev_register(raid_bdev_gen);
1350 	if (rc != 0) {
1351 		SPDK_ERRLOG("Unable to register raid bdev and stay at configuring state\n");
1352 		if (raid_bdev->module->stop != NULL) {
1353 			raid_bdev->module->stop(raid_bdev);
1354 		}
1355 		spdk_io_device_unregister(raid_bdev, NULL);
1356 		raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
1357 		return rc;
1358 	}
1359 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev generic %p\n", raid_bdev_gen);
1360 	TAILQ_REMOVE(&g_raid_bdev_configuring_list, raid_bdev, state_link);
1361 	TAILQ_INSERT_TAIL(&g_raid_bdev_configured_list, raid_bdev, state_link);
1362 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev is created with name %s, raid_bdev %p\n",
1363 		      raid_bdev_gen->name, raid_bdev);
1364 
1365 	return 0;
1366 }
1367 
1368 /*
1369  * brief:
1370  * If raid bdev is online and registered, change the bdev state to
1371  * configuring and unregister this raid device. Queue this raid device
1372  * in configuring list
1373  * params:
1374  * raid_bdev - pointer to raid bdev
1375  * cb_fn - callback function
1376  * cb_arg - argument to callback function
1377  * returns:
1378  * none
1379  */
1380 static void
1381 raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn,
1382 		      void *cb_arg)
1383 {
1384 	if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) {
1385 		if (cb_fn) {
1386 			cb_fn(cb_arg, 0);
1387 		}
1388 		return;
1389 	}
1390 
1391 	assert(raid_bdev->num_base_bdevs == raid_bdev->num_base_bdevs_discovered);
1392 	TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link);
1393 	if (raid_bdev->module->stop != NULL) {
1394 		raid_bdev->module->stop(raid_bdev);
1395 	}
1396 	raid_bdev->state = RAID_BDEV_STATE_OFFLINE;
1397 	assert(raid_bdev->num_base_bdevs_discovered);
1398 	TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link);
1399 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev state chaning from online to offline\n");
1400 
1401 	spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg);
1402 }
1403 
1404 /*
1405  * brief:
1406  * raid_bdev_find_by_base_bdev function finds the raid bdev which has
1407  *  claimed the base bdev.
1408  * params:
1409  * base_bdev - pointer to base bdev pointer
1410  * _raid_bdev - Referenct to pointer to raid bdev
1411  * _base_bdev_slot - Reference to the slot of the base bdev.
1412  * returns:
1413  * true - if the raid bdev is found.
1414  * false - if the raid bdev is not found.
1415  */
1416 static bool
1417 raid_bdev_find_by_base_bdev(struct spdk_bdev *base_bdev, struct raid_bdev **_raid_bdev,
1418 			    uint8_t *_base_bdev_slot)
1419 {
1420 	struct raid_bdev	*raid_bdev;
1421 	uint8_t			i;
1422 
1423 	TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
1424 		for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
1425 			if (raid_bdev->base_bdev_info[i].bdev == base_bdev) {
1426 				*_raid_bdev = raid_bdev;
1427 				*_base_bdev_slot = i;
1428 				return true;
1429 			}
1430 		}
1431 	}
1432 
1433 	return false;
1434 }
1435 
1436 /*
1437  * brief:
1438  * raid_bdev_remove_base_bdev function is called by below layers when base_bdev
1439  * is removed. This function checks if this base bdev is part of any raid bdev
1440  * or not. If yes, it takes necessary action on that particular raid bdev.
1441  * params:
1442  * ctx - pointer to base bdev pointer which got removed
1443  * returns:
1444  * none
1445  */
1446 static void
1447 raid_bdev_remove_base_bdev(void *ctx)
1448 {
1449 	struct spdk_bdev	*base_bdev = ctx;
1450 	struct raid_bdev	*raid_bdev = NULL;
1451 	uint8_t			base_bdev_slot = 0;
1452 
1453 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_remove_base_bdev\n");
1454 
1455 	/* Find the raid_bdev which has claimed this base_bdev */
1456 	if (!raid_bdev_find_by_base_bdev(base_bdev, &raid_bdev, &base_bdev_slot)) {
1457 		SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name);
1458 		return;
1459 	}
1460 
1461 	assert(raid_bdev->base_bdev_info[base_bdev_slot].desc);
1462 	raid_bdev->base_bdev_info[base_bdev_slot].remove_scheduled = true;
1463 
1464 	if (raid_bdev->destruct_called == true ||
1465 	    raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) {
1466 		/*
1467 		 * As raid bdev is not registered yet or already unregistered,
1468 		 * so cleanup should be done here itself.
1469 		 */
1470 		raid_bdev_free_base_bdev_resource(raid_bdev, base_bdev_slot);
1471 		if (raid_bdev->num_base_bdevs_discovered == 0) {
1472 			/* There is no base bdev for this raid, so free the raid device. */
1473 			raid_bdev_cleanup(raid_bdev);
1474 			return;
1475 		}
1476 	}
1477 
1478 	raid_bdev_deconfigure(raid_bdev, NULL, NULL);
1479 }
1480 
1481 /*
1482  * brief:
1483  * Remove base bdevs from the raid bdev one by one.  Skip any base bdev which
1484  *  doesn't exist.
1485  * params:
1486  * raid_cfg - pointer to raid bdev config.
1487  * cb_fn - callback function
1488  * cb_ctx - argument to callback function
1489  */
1490 void
1491 raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg,
1492 			      raid_bdev_destruct_cb cb_fn, void *cb_arg)
1493 {
1494 	struct raid_bdev		*raid_bdev;
1495 	struct raid_base_bdev_info	*info;
1496 	uint8_t				i;
1497 
1498 	SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_remove_base_devices\n");
1499 
1500 	raid_bdev = raid_cfg->raid_bdev;
1501 	if (raid_bdev == NULL) {
1502 		SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev %s doesn't exist now\n", raid_cfg->name);
1503 		if (cb_fn) {
1504 			cb_fn(cb_arg, 0);
1505 		}
1506 		return;
1507 	}
1508 
1509 	if (raid_bdev->destroy_started) {
1510 		SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "destroying raid bdev %s is already started\n",
1511 			      raid_cfg->name);
1512 		if (cb_fn) {
1513 			cb_fn(cb_arg, -EALREADY);
1514 		}
1515 		return;
1516 	}
1517 
1518 	raid_bdev->destroy_started = true;
1519 
1520 	for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
1521 		info = &raid_bdev->base_bdev_info[i];
1522 
1523 		if (info->bdev == NULL) {
1524 			continue;
1525 		}
1526 
1527 		assert(info->desc);
1528 		info->remove_scheduled = true;
1529 
1530 		if (raid_bdev->destruct_called == true ||
1531 		    raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) {
1532 			/*
1533 			 * As raid bdev is not registered yet or already unregistered,
1534 			 * so cleanup should be done here itself.
1535 			 */
1536 			raid_bdev_free_base_bdev_resource(raid_bdev, i);
1537 			if (raid_bdev->num_base_bdevs_discovered == 0) {
1538 				/* There is no base bdev for this raid, so free the raid device. */
1539 				raid_bdev_cleanup(raid_bdev);
1540 				if (cb_fn) {
1541 					cb_fn(cb_arg, 0);
1542 				}
1543 				return;
1544 			}
1545 		}
1546 	}
1547 
1548 	raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg);
1549 }
1550 
1551 /*
1552  * brief:
1553  * raid_bdev_add_base_device function is the actual function which either adds
1554  * the nvme base device to existing raid bdev or create a new raid bdev. It also claims
1555  * the base device and keep the open descriptor.
1556  * params:
1557  * raid_cfg - pointer to raid bdev config
1558  * bdev - pointer to base bdev
1559  * base_bdev_slot - position to add base bdev
1560  * returns:
1561  * 0 - success
1562  * non zero - failure
1563  */
1564 static int
1565 raid_bdev_add_base_device(struct raid_bdev_config *raid_cfg, struct spdk_bdev *bdev,
1566 			  uint8_t base_bdev_slot)
1567 {
1568 	struct raid_bdev	*raid_bdev;
1569 	int			rc;
1570 
1571 	raid_bdev = raid_cfg->raid_bdev;
1572 	if (!raid_bdev) {
1573 		SPDK_ERRLOG("Raid bdev '%s' is not created yet\n", raid_cfg->name);
1574 		return -ENODEV;
1575 	}
1576 
1577 	rc = raid_bdev_alloc_base_bdev_resource(raid_bdev, bdev, base_bdev_slot);
1578 	if (rc != 0) {
1579 		SPDK_ERRLOG("Failed to allocate resource for bdev '%s'\n", bdev->name);
1580 		return rc;
1581 	}
1582 
1583 	assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs);
1584 
1585 	if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs) {
1586 		rc = raid_bdev_configure(raid_bdev);
1587 		if (rc != 0) {
1588 			SPDK_ERRLOG("Failed to configure raid bdev\n");
1589 			return rc;
1590 		}
1591 	}
1592 
1593 	return 0;
1594 }
1595 
1596 /*
1597  * brief:
1598  * Add base bdevs to the raid bdev one by one.  Skip any base bdev which doesn't
1599  *  exist or fails to add. If all base bdevs are successfully added, the raid bdev
1600  *  moves to the configured state and becomes available. Otherwise, the raid bdev
1601  *  stays at the configuring state with added base bdevs.
1602  * params:
1603  * raid_cfg - pointer to raid bdev config
1604  * returns:
1605  * 0 - The raid bdev moves to the configured state or stays at the configuring
1606  *     state with added base bdevs due to any nonexistent base bdev.
1607  * non zero - Failed to add any base bdev and stays at the configuring state with
1608  *            added base bdevs.
1609  */
1610 int
1611 raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg)
1612 {
1613 	struct spdk_bdev	*base_bdev;
1614 	uint8_t			i;
1615 	int			rc = 0, _rc;
1616 
1617 	for (i = 0; i < raid_cfg->num_base_bdevs; i++) {
1618 		base_bdev = spdk_bdev_get_by_name(raid_cfg->base_bdev[i].name);
1619 		if (base_bdev == NULL) {
1620 			SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "base bdev %s doesn't exist now\n",
1621 				      raid_cfg->base_bdev[i].name);
1622 			continue;
1623 		}
1624 
1625 		_rc = raid_bdev_add_base_device(raid_cfg, base_bdev, i);
1626 		if (_rc != 0) {
1627 			SPDK_ERRLOG("Failed to add base bdev %s to RAID bdev %s: %s\n",
1628 				    raid_cfg->base_bdev[i].name, raid_cfg->name,
1629 				    spdk_strerror(-_rc));
1630 			if (rc == 0) {
1631 				rc = _rc;
1632 			}
1633 		}
1634 	}
1635 
1636 	return rc;
1637 }
1638 
1639 /*
1640  * brief:
1641  * raid_bdev_examine function is the examine function call by the below layers
1642  * like bdev_nvme layer. This function will check if this base bdev can be
1643  * claimed by this raid bdev or not.
1644  * params:
1645  * bdev - pointer to base bdev
1646  * returns:
1647  * none
1648  */
1649 static void
1650 raid_bdev_examine(struct spdk_bdev *bdev)
1651 {
1652 	struct raid_bdev_config	*raid_cfg;
1653 	uint8_t			base_bdev_slot;
1654 
1655 	if (raid_bdev_can_claim_bdev(bdev->name, &raid_cfg, &base_bdev_slot)) {
1656 		raid_bdev_add_base_device(raid_cfg, bdev, base_bdev_slot);
1657 	} else {
1658 		SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "bdev %s can't be claimed\n",
1659 			      bdev->name);
1660 	}
1661 
1662 	spdk_bdev_module_examine_done(&g_raid_if);
1663 }
1664 
1665 /* Log component for bdev raid bdev module */
1666 SPDK_LOG_REGISTER_COMPONENT("bdev_raid", SPDK_LOG_BDEV_RAID)
1667