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