xref: /spdk/module/bdev/raid/bdev_raid.c (revision 2f5c602574a98ede645991abe279a96e19c50196)
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/log.h"
38 #include "spdk/string.h"
39 #include "spdk/util.h"
40 #include "spdk/json.h"
41 #include "spdk/string.h"
42 
43 static bool g_shutdown_started = false;
44 
45 /* raid bdev config as read from config file */
46 struct raid_config	g_raid_config = {
47 	.raid_bdev_config_head = TAILQ_HEAD_INITIALIZER(g_raid_config.raid_bdev_config_head),
48 };
49 
50 /*
51  * List of raid bdev in configured list, these raid bdevs are registered with
52  * bdev layer
53  */
54 struct raid_configured_tailq	g_raid_bdev_configured_list = TAILQ_HEAD_INITIALIZER(
55 			g_raid_bdev_configured_list);
56 
57 /* List of raid bdev in configuring list */
58 struct raid_configuring_tailq	g_raid_bdev_configuring_list = TAILQ_HEAD_INITIALIZER(
59 			g_raid_bdev_configuring_list);
60 
61 /* List of all raid bdevs */
62 struct raid_all_tailq		g_raid_bdev_list = TAILQ_HEAD_INITIALIZER(g_raid_bdev_list);
63 
64 /* List of all raid bdevs that are offline */
65 struct raid_offline_tailq	g_raid_bdev_offline_list = TAILQ_HEAD_INITIALIZER(
66 			g_raid_bdev_offline_list);
67 
68 static TAILQ_HEAD(, raid_bdev_module) g_raid_modules = TAILQ_HEAD_INITIALIZER(g_raid_modules);
69 
70 static struct raid_bdev_module *raid_bdev_module_find(enum raid_level level)
71 {
72 	struct raid_bdev_module *raid_module;
73 
74 	TAILQ_FOREACH(raid_module, &g_raid_modules, link) {
75 		if (raid_module->level == level) {
76 			return raid_module;
77 		}
78 	}
79 
80 	return NULL;
81 }
82 
83 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module)
84 {
85 	if (raid_bdev_module_find(raid_module->level) != NULL) {
86 		SPDK_ERRLOG("module for raid level '%s' already registered.\n",
87 			    raid_bdev_level_to_str(raid_module->level));
88 		assert(false);
89 	} else {
90 		TAILQ_INSERT_TAIL(&g_raid_modules, raid_module, link);
91 	}
92 }
93 
94 /* Function declarations */
95 static void	raid_bdev_examine(struct spdk_bdev *bdev);
96 static int	raid_bdev_init(void);
97 static void	raid_bdev_deconfigure(struct raid_bdev *raid_bdev,
98 				      raid_bdev_destruct_cb cb_fn, void *cb_arg);
99 static void	raid_bdev_event_base_bdev(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
100 		void *event_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(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(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(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  * wrapper for the bdev close operation
220  * params:
221  * base_info - raid base bdev info
222  * returns:
223  */
224 static void
225 _raid_bdev_free_base_bdev_resource(void *ctx)
226 {
227 	struct spdk_bdev_desc *desc = ctx;
228 
229 	spdk_bdev_close(desc);
230 }
231 
232 
233 /*
234  * brief:
235  * free resource of base bdev for raid bdev
236  * params:
237  * raid_bdev - pointer to raid bdev
238  * base_info - raid base bdev info
239  * returns:
240  * 0 - success
241  * non zero - failure
242  */
243 static void
244 raid_bdev_free_base_bdev_resource(struct raid_bdev *raid_bdev,
245 				  struct raid_base_bdev_info *base_info)
246 {
247 	spdk_bdev_module_release_bdev(base_info->bdev);
248 	if (base_info->thread && base_info->thread != spdk_get_thread()) {
249 		spdk_thread_send_msg(base_info->thread, _raid_bdev_free_base_bdev_resource, base_info->desc);
250 	} else {
251 		spdk_bdev_close(base_info->desc);
252 	}
253 	base_info->desc = NULL;
254 	base_info->bdev = NULL;
255 
256 	assert(raid_bdev->num_base_bdevs_discovered);
257 	raid_bdev->num_base_bdevs_discovered--;
258 }
259 
260 /*
261  * brief:
262  * raid_bdev_destruct is the destruct function table pointer for raid bdev
263  * params:
264  * ctxt - pointer to raid_bdev
265  * returns:
266  * 0 - success
267  * non zero - failure
268  */
269 static int
270 raid_bdev_destruct(void *ctxt)
271 {
272 	struct raid_bdev *raid_bdev = ctxt;
273 	struct raid_base_bdev_info *base_info;
274 
275 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_destruct\n");
276 
277 	raid_bdev->destruct_called = true;
278 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
279 		/*
280 		 * Close all base bdev descriptors for which call has come from below
281 		 * layers.  Also close the descriptors if we have started shutdown.
282 		 */
283 		if (g_shutdown_started ||
284 		    ((base_info->remove_scheduled == true) &&
285 		     (base_info->bdev != NULL))) {
286 			raid_bdev_free_base_bdev_resource(raid_bdev, base_info);
287 		}
288 	}
289 
290 	if (g_shutdown_started) {
291 		TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link);
292 		if (raid_bdev->module->stop != NULL) {
293 			raid_bdev->module->stop(raid_bdev);
294 		}
295 		raid_bdev->state = RAID_BDEV_STATE_OFFLINE;
296 		TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link);
297 	}
298 
299 	spdk_io_device_unregister(raid_bdev, NULL);
300 
301 	if (raid_bdev->num_base_bdevs_discovered == 0) {
302 		/* Free raid_bdev when there are no base bdevs left */
303 		SPDK_DEBUGLOG(bdev_raid, "raid bdev base bdevs is 0, going to free all in destruct\n");
304 		raid_bdev_cleanup(raid_bdev);
305 	}
306 
307 	return 0;
308 }
309 
310 void
311 raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status)
312 {
313 	struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io);
314 
315 	spdk_bdev_io_complete(bdev_io, status);
316 }
317 
318 /*
319  * brief:
320  * raid_bdev_io_complete_part - signal the completion of a part of the expected
321  * base bdev IOs and complete the raid_io if this is the final expected IO.
322  * The caller should first set raid_io->base_bdev_io_remaining. This function
323  * will decrement this counter by the value of the 'completed' parameter and
324  * complete the raid_io if the counter reaches 0. The caller is free to
325  * interpret the 'base_bdev_io_remaining' and 'completed' values as needed,
326  * it can represent e.g. blocks or IOs.
327  * params:
328  * raid_io - pointer to raid_bdev_io
329  * completed - the part of the raid_io that has been completed
330  * status - status of the base IO
331  * returns:
332  * true - if the raid_io is completed
333  * false - otherwise
334  */
335 bool
336 raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
337 			   enum spdk_bdev_io_status status)
338 {
339 	assert(raid_io->base_bdev_io_remaining >= completed);
340 	raid_io->base_bdev_io_remaining -= completed;
341 
342 	if (status != SPDK_BDEV_IO_STATUS_SUCCESS) {
343 		raid_io->base_bdev_io_status = status;
344 	}
345 
346 	if (raid_io->base_bdev_io_remaining == 0) {
347 		raid_bdev_io_complete(raid_io, raid_io->base_bdev_io_status);
348 		return true;
349 	} else {
350 		return false;
351 	}
352 }
353 
354 /*
355  * brief:
356  * raid_bdev_queue_io_wait function processes the IO which failed to submit.
357  * It will try to queue the IOs after storing the context to bdev wait queue logic.
358  * params:
359  * raid_io - pointer to raid_bdev_io
360  * bdev - the block device that the IO is submitted to
361  * ch - io channel
362  * cb_fn - callback when the spdk_bdev_io for bdev becomes available
363  * returns:
364  * none
365  */
366 void
367 raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
368 			struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn)
369 {
370 	raid_io->waitq_entry.bdev = bdev;
371 	raid_io->waitq_entry.cb_fn = cb_fn;
372 	raid_io->waitq_entry.cb_arg = raid_io;
373 	spdk_bdev_queue_io_wait(bdev, ch, &raid_io->waitq_entry);
374 }
375 
376 static void
377 raid_base_bdev_reset_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
378 {
379 	struct raid_bdev_io *raid_io = cb_arg;
380 
381 	spdk_bdev_free_io(bdev_io);
382 
383 	raid_bdev_io_complete_part(raid_io, 1, success ?
384 				   SPDK_BDEV_IO_STATUS_SUCCESS :
385 				   SPDK_BDEV_IO_STATUS_FAILED);
386 }
387 
388 static void
389 raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io);
390 
391 static void
392 _raid_bdev_submit_reset_request(void *_raid_io)
393 {
394 	struct raid_bdev_io *raid_io = _raid_io;
395 
396 	raid_bdev_submit_reset_request(raid_io);
397 }
398 
399 /*
400  * brief:
401  * raid_bdev_submit_reset_request function submits reset requests
402  * to member disks; it will submit as many as possible unless a reset fails with -ENOMEM, in
403  * which case it will queue it for later submission
404  * params:
405  * raid_io
406  * returns:
407  * none
408  */
409 static void
410 raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io)
411 {
412 	struct raid_bdev		*raid_bdev;
413 	int				ret;
414 	uint8_t				i;
415 	struct raid_base_bdev_info	*base_info;
416 	struct spdk_io_channel		*base_ch;
417 
418 	raid_bdev = raid_io->raid_bdev;
419 
420 	if (raid_io->base_bdev_io_remaining == 0) {
421 		raid_io->base_bdev_io_remaining = raid_bdev->num_base_bdevs;
422 	}
423 
424 	while (raid_io->base_bdev_io_submitted < raid_bdev->num_base_bdevs) {
425 		i = raid_io->base_bdev_io_submitted;
426 		base_info = &raid_bdev->base_bdev_info[i];
427 		base_ch = raid_io->raid_ch->base_channel[i];
428 		ret = spdk_bdev_reset(base_info->desc, base_ch,
429 				      raid_base_bdev_reset_complete, raid_io);
430 		if (ret == 0) {
431 			raid_io->base_bdev_io_submitted++;
432 		} else if (ret == -ENOMEM) {
433 			raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch,
434 						_raid_bdev_submit_reset_request);
435 			return;
436 		} else {
437 			SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n");
438 			assert(false);
439 			raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
440 			return;
441 		}
442 	}
443 }
444 
445 /*
446  * brief:
447  * Callback function to spdk_bdev_io_get_buf.
448  * params:
449  * ch - pointer to raid bdev io channel
450  * bdev_io - pointer to parent bdev_io on raid bdev device
451  * success - True if buffer is allocated or false otherwise.
452  * returns:
453  * none
454  */
455 static void
456 raid_bdev_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
457 		     bool success)
458 {
459 	struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx;
460 
461 	if (!success) {
462 		raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
463 		return;
464 	}
465 
466 	raid_io->raid_bdev->module->submit_rw_request(raid_io);
467 }
468 
469 /*
470  * brief:
471  * raid_bdev_submit_request function is the submit_request function pointer of
472  * raid bdev function table. This is used to submit the io on raid_bdev to below
473  * layers.
474  * params:
475  * ch - pointer to raid bdev io channel
476  * bdev_io - pointer to parent bdev_io on raid bdev device
477  * returns:
478  * none
479  */
480 static void
481 raid_bdev_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
482 {
483 	struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx;
484 
485 	raid_io->raid_bdev = bdev_io->bdev->ctxt;
486 	raid_io->raid_ch = spdk_io_channel_get_ctx(ch);
487 	raid_io->base_bdev_io_remaining = 0;
488 	raid_io->base_bdev_io_submitted = 0;
489 	raid_io->base_bdev_io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
490 
491 	switch (bdev_io->type) {
492 	case SPDK_BDEV_IO_TYPE_READ:
493 		spdk_bdev_io_get_buf(bdev_io, raid_bdev_get_buf_cb,
494 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
495 		break;
496 	case SPDK_BDEV_IO_TYPE_WRITE:
497 		raid_io->raid_bdev->module->submit_rw_request(raid_io);
498 		break;
499 
500 	case SPDK_BDEV_IO_TYPE_RESET:
501 		raid_bdev_submit_reset_request(raid_io);
502 		break;
503 
504 	case SPDK_BDEV_IO_TYPE_FLUSH:
505 	case SPDK_BDEV_IO_TYPE_UNMAP:
506 		raid_io->raid_bdev->module->submit_null_payload_request(raid_io);
507 		break;
508 
509 	default:
510 		SPDK_ERRLOG("submit request, invalid io type %u\n", bdev_io->type);
511 		raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
512 		break;
513 	}
514 }
515 
516 /*
517  * brief:
518  * _raid_bdev_io_type_supported checks whether io_type is supported in
519  * all base bdev modules of raid bdev module. If anyone among the base_bdevs
520  * doesn't support, the raid device doesn't supports.
521  *
522  * params:
523  * raid_bdev - pointer to raid bdev context
524  * io_type - io type
525  * returns:
526  * true - io_type is supported
527  * false - io_type is not supported
528  */
529 inline static bool
530 _raid_bdev_io_type_supported(struct raid_bdev *raid_bdev, enum spdk_bdev_io_type io_type)
531 {
532 	struct raid_base_bdev_info *base_info;
533 
534 	if (io_type == SPDK_BDEV_IO_TYPE_FLUSH ||
535 	    io_type == SPDK_BDEV_IO_TYPE_UNMAP) {
536 		if (raid_bdev->module->submit_null_payload_request == NULL) {
537 			return false;
538 		}
539 	}
540 
541 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
542 		if (base_info->bdev == NULL) {
543 			assert(false);
544 			continue;
545 		}
546 
547 		if (spdk_bdev_io_type_supported(base_info->bdev, io_type) == false) {
548 			return false;
549 		}
550 	}
551 
552 	return true;
553 }
554 
555 /*
556  * brief:
557  * raid_bdev_io_type_supported is the io_supported function for bdev function
558  * table which returns whether the particular io type is supported or not by
559  * raid bdev module
560  * params:
561  * ctx - pointer to raid bdev context
562  * type - io type
563  * returns:
564  * true - io_type is supported
565  * false - io_type is not supported
566  */
567 static bool
568 raid_bdev_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
569 {
570 	switch (io_type) {
571 	case SPDK_BDEV_IO_TYPE_READ:
572 	case SPDK_BDEV_IO_TYPE_WRITE:
573 		return true;
574 
575 	case SPDK_BDEV_IO_TYPE_FLUSH:
576 	case SPDK_BDEV_IO_TYPE_RESET:
577 	case SPDK_BDEV_IO_TYPE_UNMAP:
578 		return _raid_bdev_io_type_supported(ctx, io_type);
579 
580 	default:
581 		return false;
582 	}
583 
584 	return false;
585 }
586 
587 /*
588  * brief:
589  * raid_bdev_get_io_channel is the get_io_channel function table pointer for
590  * raid bdev. This is used to return the io channel for this raid bdev
591  * params:
592  * ctxt - pointer to raid_bdev
593  * returns:
594  * pointer to io channel for raid bdev
595  */
596 static struct spdk_io_channel *
597 raid_bdev_get_io_channel(void *ctxt)
598 {
599 	struct raid_bdev *raid_bdev = ctxt;
600 
601 	return spdk_get_io_channel(raid_bdev);
602 }
603 
604 /*
605  * brief:
606  * raid_bdev_dump_info_json is the function table pointer for raid bdev
607  * params:
608  * ctx - pointer to raid_bdev
609  * w - pointer to json context
610  * returns:
611  * 0 - success
612  * non zero - failure
613  */
614 static int
615 raid_bdev_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
616 {
617 	struct raid_bdev *raid_bdev = ctx;
618 	struct raid_base_bdev_info *base_info;
619 
620 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_dump_config_json\n");
621 	assert(raid_bdev != NULL);
622 
623 	/* Dump the raid bdev configuration related information */
624 	spdk_json_write_named_object_begin(w, "raid");
625 	spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb);
626 	spdk_json_write_named_uint32(w, "state", raid_bdev->state);
627 	spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
628 	spdk_json_write_named_uint32(w, "destruct_called", raid_bdev->destruct_called);
629 	spdk_json_write_named_uint32(w, "num_base_bdevs", raid_bdev->num_base_bdevs);
630 	spdk_json_write_named_uint32(w, "num_base_bdevs_discovered", raid_bdev->num_base_bdevs_discovered);
631 	spdk_json_write_name(w, "base_bdevs_list");
632 	spdk_json_write_array_begin(w);
633 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
634 		if (base_info->bdev) {
635 			spdk_json_write_string(w, base_info->bdev->name);
636 		} else {
637 			spdk_json_write_null(w);
638 		}
639 	}
640 	spdk_json_write_array_end(w);
641 	spdk_json_write_object_end(w);
642 
643 	return 0;
644 }
645 
646 /*
647  * brief:
648  * raid_bdev_write_config_json is the function table pointer for raid bdev
649  * params:
650  * bdev - pointer to spdk_bdev
651  * w - pointer to json context
652  * returns:
653  * none
654  */
655 static void
656 raid_bdev_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
657 {
658 	struct raid_bdev *raid_bdev = bdev->ctxt;
659 	struct raid_base_bdev_info *base_info;
660 
661 	spdk_json_write_object_begin(w);
662 
663 	spdk_json_write_named_string(w, "method", "bdev_raid_create");
664 
665 	spdk_json_write_named_object_begin(w, "params");
666 	spdk_json_write_named_string(w, "name", bdev->name);
667 	spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb);
668 	spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
669 
670 	spdk_json_write_named_array_begin(w, "base_bdevs");
671 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
672 		if (base_info->bdev) {
673 			spdk_json_write_string(w, base_info->bdev->name);
674 		}
675 	}
676 	spdk_json_write_array_end(w);
677 	spdk_json_write_object_end(w);
678 
679 	spdk_json_write_object_end(w);
680 }
681 
682 /* g_raid_bdev_fn_table is the function table for raid bdev */
683 static const struct spdk_bdev_fn_table g_raid_bdev_fn_table = {
684 	.destruct		= raid_bdev_destruct,
685 	.submit_request		= raid_bdev_submit_request,
686 	.io_type_supported	= raid_bdev_io_type_supported,
687 	.get_io_channel		= raid_bdev_get_io_channel,
688 	.dump_info_json		= raid_bdev_dump_info_json,
689 	.write_config_json	= raid_bdev_write_config_json,
690 };
691 
692 /*
693  * brief:
694  * raid_bdev_config_cleanup function is used to free memory for one raid_bdev in configuration
695  * params:
696  * raid_cfg - pointer to raid_bdev_config structure
697  * returns:
698  * none
699  */
700 void
701 raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg)
702 {
703 	uint8_t i;
704 
705 	TAILQ_REMOVE(&g_raid_config.raid_bdev_config_head, raid_cfg, link);
706 	g_raid_config.total_raid_bdev--;
707 
708 	if (raid_cfg->base_bdev) {
709 		for (i = 0; i < raid_cfg->num_base_bdevs; i++) {
710 			free(raid_cfg->base_bdev[i].name);
711 		}
712 		free(raid_cfg->base_bdev);
713 	}
714 	free(raid_cfg->name);
715 	free(raid_cfg);
716 }
717 
718 /*
719  * brief:
720  * raid_bdev_free is the raid bdev function table function pointer. This is
721  * called on bdev free path
722  * params:
723  * none
724  * returns:
725  * none
726  */
727 static void
728 raid_bdev_free(void)
729 {
730 	struct raid_bdev_config *raid_cfg, *tmp;
731 
732 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_free\n");
733 	TAILQ_FOREACH_SAFE(raid_cfg, &g_raid_config.raid_bdev_config_head, link, tmp) {
734 		raid_bdev_config_cleanup(raid_cfg);
735 	}
736 }
737 
738 /* brief
739  * raid_bdev_config_find_by_name is a helper function to find raid bdev config
740  * by name as key.
741  *
742  * params:
743  * raid_name - name for raid bdev.
744  */
745 struct raid_bdev_config *
746 raid_bdev_config_find_by_name(const char *raid_name)
747 {
748 	struct raid_bdev_config *raid_cfg;
749 
750 	TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) {
751 		if (!strcmp(raid_cfg->name, raid_name)) {
752 			return raid_cfg;
753 		}
754 	}
755 
756 	return raid_cfg;
757 }
758 
759 /*
760  * brief
761  * raid_bdev_config_add function adds config for newly created raid bdev.
762  *
763  * params:
764  * raid_name - name for raid bdev.
765  * strip_size - strip size in KB
766  * num_base_bdevs - number of base bdevs.
767  * level - raid level.
768  * _raid_cfg - Pointer to newly added configuration
769  */
770 int
771 raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs,
772 		     enum raid_level level, struct raid_bdev_config **_raid_cfg)
773 {
774 	struct raid_bdev_config *raid_cfg;
775 
776 	raid_cfg = raid_bdev_config_find_by_name(raid_name);
777 	if (raid_cfg != NULL) {
778 		SPDK_ERRLOG("Duplicate raid bdev name found in config file %s\n",
779 			    raid_name);
780 		return -EEXIST;
781 	}
782 
783 	if (spdk_u32_is_pow2(strip_size) == false) {
784 		SPDK_ERRLOG("Invalid strip size %" PRIu32 "\n", strip_size);
785 		return -EINVAL;
786 	}
787 
788 	if (num_base_bdevs == 0) {
789 		SPDK_ERRLOG("Invalid base device count %u\n", num_base_bdevs);
790 		return -EINVAL;
791 	}
792 
793 	raid_cfg = calloc(1, sizeof(*raid_cfg));
794 	if (raid_cfg == NULL) {
795 		SPDK_ERRLOG("unable to allocate memory\n");
796 		return -ENOMEM;
797 	}
798 
799 	raid_cfg->name = strdup(raid_name);
800 	if (!raid_cfg->name) {
801 		free(raid_cfg);
802 		SPDK_ERRLOG("unable to allocate memory\n");
803 		return -ENOMEM;
804 	}
805 	raid_cfg->strip_size = strip_size;
806 	raid_cfg->num_base_bdevs = num_base_bdevs;
807 	raid_cfg->level = level;
808 
809 	raid_cfg->base_bdev = calloc(num_base_bdevs, sizeof(*raid_cfg->base_bdev));
810 	if (raid_cfg->base_bdev == NULL) {
811 		free(raid_cfg->name);
812 		free(raid_cfg);
813 		SPDK_ERRLOG("unable to allocate memory\n");
814 		return -ENOMEM;
815 	}
816 
817 	TAILQ_INSERT_TAIL(&g_raid_config.raid_bdev_config_head, raid_cfg, link);
818 	g_raid_config.total_raid_bdev++;
819 
820 	*_raid_cfg = raid_cfg;
821 	return 0;
822 }
823 
824 /*
825  * brief:
826  * raid_bdev_config_add_base_bdev function add base bdev to raid bdev config.
827  *
828  * params:
829  * raid_cfg - pointer to raid bdev configuration
830  * base_bdev_name - name of base bdev
831  * slot - Position to add base bdev
832  */
833 int
834 raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg, const char *base_bdev_name,
835 			       uint8_t slot)
836 {
837 	uint8_t i;
838 	struct raid_bdev_config *tmp;
839 
840 	if (slot >= raid_cfg->num_base_bdevs) {
841 		return -EINVAL;
842 	}
843 
844 	TAILQ_FOREACH(tmp, &g_raid_config.raid_bdev_config_head, link) {
845 		for (i = 0; i < tmp->num_base_bdevs; i++) {
846 			if (tmp->base_bdev[i].name != NULL) {
847 				if (!strcmp(tmp->base_bdev[i].name, base_bdev_name)) {
848 					SPDK_ERRLOG("duplicate base bdev name %s mentioned\n",
849 						    base_bdev_name);
850 					return -EEXIST;
851 				}
852 			}
853 		}
854 	}
855 
856 	raid_cfg->base_bdev[slot].name = strdup(base_bdev_name);
857 	if (raid_cfg->base_bdev[slot].name == NULL) {
858 		SPDK_ERRLOG("unable to allocate memory\n");
859 		return -ENOMEM;
860 	}
861 
862 	return 0;
863 }
864 
865 static struct {
866 	const char *name;
867 	enum raid_level value;
868 } g_raid_level_names[] = {
869 	{ "raid0", RAID0 },
870 	{ "0", RAID0 },
871 	{ "raid5", RAID5 },
872 	{ "5", RAID5 },
873 	{ }
874 };
875 
876 enum raid_level raid_bdev_parse_raid_level(const char *str)
877 {
878 	unsigned int i;
879 
880 	assert(str != NULL);
881 
882 	for (i = 0; g_raid_level_names[i].name != NULL; i++) {
883 		if (strcasecmp(g_raid_level_names[i].name, str) == 0) {
884 			return g_raid_level_names[i].value;
885 		}
886 	}
887 
888 	return INVALID_RAID_LEVEL;
889 }
890 
891 const char *
892 raid_bdev_level_to_str(enum raid_level level)
893 {
894 	unsigned int i;
895 
896 	for (i = 0; g_raid_level_names[i].name != NULL; i++) {
897 		if (g_raid_level_names[i].value == level) {
898 			return g_raid_level_names[i].name;
899 		}
900 	}
901 
902 	return "";
903 }
904 
905 /*
906  * brief:
907  * raid_bdev_fini_start is called when bdev layer is starting the
908  * shutdown process
909  * params:
910  * none
911  * returns:
912  * none
913  */
914 static void
915 raid_bdev_fini_start(void)
916 {
917 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_fini_start\n");
918 	g_shutdown_started = true;
919 }
920 
921 /*
922  * brief:
923  * raid_bdev_exit is called on raid bdev module exit time by bdev layer
924  * params:
925  * none
926  * returns:
927  * none
928  */
929 static void
930 raid_bdev_exit(void)
931 {
932 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_exit\n");
933 	raid_bdev_free();
934 }
935 
936 /*
937  * brief:
938  * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid
939  * module
940  * params:
941  * none
942  * returns:
943  * size of spdk_bdev_io context for raid
944  */
945 static int
946 raid_bdev_get_ctx_size(void)
947 {
948 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_get_ctx_size\n");
949 	return sizeof(struct raid_bdev_io);
950 }
951 
952 /*
953  * brief:
954  * raid_bdev_can_claim_bdev is the function to check if this base_bdev can be
955  * claimed by raid bdev or not.
956  * params:
957  * bdev_name - represents base bdev name
958  * _raid_cfg - pointer to raid bdev config parsed from config file
959  * base_bdev_slot - if bdev can be claimed, it represents the base_bdev correct
960  * slot. This field is only valid if return value of this function is true
961  * returns:
962  * true - if bdev can be claimed
963  * false - if bdev can't be claimed
964  */
965 static bool
966 raid_bdev_can_claim_bdev(const char *bdev_name, struct raid_bdev_config **_raid_cfg,
967 			 uint8_t *base_bdev_slot)
968 {
969 	struct raid_bdev_config *raid_cfg;
970 	uint8_t i;
971 
972 	TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) {
973 		for (i = 0; i < raid_cfg->num_base_bdevs; i++) {
974 			/*
975 			 * Check if the base bdev name is part of raid bdev configuration.
976 			 * If match is found then return true and the slot information where
977 			 * this base bdev should be inserted in raid bdev
978 			 */
979 			if (!strcmp(bdev_name, raid_cfg->base_bdev[i].name)) {
980 				*_raid_cfg = raid_cfg;
981 				*base_bdev_slot = i;
982 				return true;
983 			}
984 		}
985 	}
986 
987 	return false;
988 }
989 
990 
991 static struct spdk_bdev_module g_raid_if = {
992 	.name = "raid",
993 	.module_init = raid_bdev_init,
994 	.fini_start = raid_bdev_fini_start,
995 	.module_fini = raid_bdev_exit,
996 	.get_ctx_size = raid_bdev_get_ctx_size,
997 	.examine_config = raid_bdev_examine,
998 	.async_init = false,
999 	.async_fini = false,
1000 };
1001 SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if)
1002 
1003 /*
1004  * brief:
1005  * raid_bdev_init is the initialization function for raid bdev module
1006  * params:
1007  * none
1008  * returns:
1009  * 0 - success
1010  * non zero - failure
1011  */
1012 static int
1013 raid_bdev_init(void)
1014 {
1015 	return 0;
1016 }
1017 
1018 /*
1019  * brief:
1020  * raid_bdev_create allocates raid bdev based on passed configuration
1021  * params:
1022  * raid_cfg - configuration of raid bdev
1023  * returns:
1024  * 0 - success
1025  * non zero - failure
1026  */
1027 int
1028 raid_bdev_create(struct raid_bdev_config *raid_cfg)
1029 {
1030 	struct raid_bdev *raid_bdev;
1031 	struct spdk_bdev *raid_bdev_gen;
1032 	struct raid_bdev_module *module;
1033 
1034 	module = raid_bdev_module_find(raid_cfg->level);
1035 	if (module == NULL) {
1036 		SPDK_ERRLOG("Unsupported raid level '%d'\n", raid_cfg->level);
1037 		return -EINVAL;
1038 	}
1039 
1040 	assert(module->base_bdevs_min != 0);
1041 	if (raid_cfg->num_base_bdevs < module->base_bdevs_min) {
1042 		SPDK_ERRLOG("At least %u base devices required for %s\n",
1043 			    module->base_bdevs_min,
1044 			    raid_bdev_level_to_str(raid_cfg->level));
1045 		return -EINVAL;
1046 	}
1047 
1048 	raid_bdev = calloc(1, sizeof(*raid_bdev));
1049 	if (!raid_bdev) {
1050 		SPDK_ERRLOG("Unable to allocate memory for raid bdev\n");
1051 		return -ENOMEM;
1052 	}
1053 
1054 	raid_bdev->module = module;
1055 	raid_bdev->num_base_bdevs = raid_cfg->num_base_bdevs;
1056 	raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs,
1057 					   sizeof(struct raid_base_bdev_info));
1058 	if (!raid_bdev->base_bdev_info) {
1059 		SPDK_ERRLOG("Unable able to allocate base bdev info\n");
1060 		free(raid_bdev);
1061 		return -ENOMEM;
1062 	}
1063 
1064 	/* strip_size_kb is from the rpc param.  strip_size is in blocks and used
1065 	 * internally and set later.
1066 	 */
1067 	raid_bdev->strip_size = 0;
1068 	raid_bdev->strip_size_kb = raid_cfg->strip_size;
1069 	raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
1070 	raid_bdev->config = raid_cfg;
1071 	raid_bdev->level = raid_cfg->level;
1072 
1073 	raid_bdev_gen = &raid_bdev->bdev;
1074 
1075 	raid_bdev_gen->name = strdup(raid_cfg->name);
1076 	if (!raid_bdev_gen->name) {
1077 		SPDK_ERRLOG("Unable to allocate name for raid\n");
1078 		free(raid_bdev->base_bdev_info);
1079 		free(raid_bdev);
1080 		return -ENOMEM;
1081 	}
1082 
1083 	raid_bdev_gen->product_name = "Raid Volume";
1084 	raid_bdev_gen->ctxt = raid_bdev;
1085 	raid_bdev_gen->fn_table = &g_raid_bdev_fn_table;
1086 	raid_bdev_gen->module = &g_raid_if;
1087 	raid_bdev_gen->write_cache = 0;
1088 
1089 	TAILQ_INSERT_TAIL(&g_raid_bdev_configuring_list, raid_bdev, state_link);
1090 	TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link);
1091 
1092 	raid_cfg->raid_bdev = raid_bdev;
1093 
1094 	return 0;
1095 }
1096 
1097 /*
1098  * brief
1099  * raid_bdev_alloc_base_bdev_resource allocates resource of base bdev.
1100  * params:
1101  * raid_bdev - pointer to raid bdev
1102  * bdev_name - base bdev name
1103  * base_bdev_slot - position to add base bdev
1104  * returns:
1105  * 0 - success
1106  * non zero - failure
1107  */
1108 static int
1109 raid_bdev_alloc_base_bdev_resource(struct raid_bdev *raid_bdev, const char *bdev_name,
1110 				   uint8_t base_bdev_slot)
1111 {
1112 	struct spdk_bdev_desc *desc;
1113 	struct spdk_bdev *bdev;
1114 	int rc;
1115 
1116 	rc = spdk_bdev_open_ext(bdev_name, true, raid_bdev_event_base_bdev, NULL, &desc);
1117 	if (rc != 0) {
1118 		if (rc != -ENODEV) {
1119 			SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", bdev_name);
1120 		}
1121 		return rc;
1122 	}
1123 
1124 	bdev = spdk_bdev_desc_get_bdev(desc);
1125 
1126 	rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if);
1127 	if (rc != 0) {
1128 		SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n");
1129 		spdk_bdev_close(desc);
1130 		return rc;
1131 	}
1132 
1133 	SPDK_DEBUGLOG(bdev_raid, "bdev %s is claimed\n", bdev_name);
1134 
1135 	assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE);
1136 	assert(base_bdev_slot < raid_bdev->num_base_bdevs);
1137 
1138 	raid_bdev->base_bdev_info[base_bdev_slot].thread = spdk_get_thread();
1139 	raid_bdev->base_bdev_info[base_bdev_slot].bdev = bdev;
1140 	raid_bdev->base_bdev_info[base_bdev_slot].desc = desc;
1141 	raid_bdev->num_base_bdevs_discovered++;
1142 	assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs);
1143 
1144 	return 0;
1145 }
1146 
1147 /*
1148  * brief:
1149  * If raid bdev config is complete, then only register the raid bdev to
1150  * bdev layer and remove this raid bdev from configuring list and
1151  * insert the raid bdev to configured list
1152  * params:
1153  * raid_bdev - pointer to raid bdev
1154  * returns:
1155  * 0 - success
1156  * non zero - failure
1157  */
1158 static int
1159 raid_bdev_configure(struct raid_bdev *raid_bdev)
1160 {
1161 	uint32_t blocklen = 0;
1162 	struct spdk_bdev *raid_bdev_gen;
1163 	struct raid_base_bdev_info *base_info;
1164 	int rc = 0;
1165 
1166 	assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING);
1167 	assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs);
1168 
1169 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1170 		/* Check blocklen for all base bdevs that it should be same */
1171 		if (blocklen == 0) {
1172 			blocklen = base_info->bdev->blocklen;
1173 		} else if (blocklen != base_info->bdev->blocklen) {
1174 			/*
1175 			 * Assumption is that all the base bdevs for any raid bdev should
1176 			 * have same blocklen
1177 			 */
1178 			SPDK_ERRLOG("Blocklen of various bdevs not matching\n");
1179 			return -EINVAL;
1180 		}
1181 	}
1182 	assert(blocklen > 0);
1183 
1184 	/* The strip_size_kb is read in from user in KB. Convert to blocks here for
1185 	 * internal use.
1186 	 */
1187 	raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / blocklen;
1188 	raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size);
1189 	raid_bdev->blocklen_shift = spdk_u32log2(blocklen);
1190 
1191 	raid_bdev_gen = &raid_bdev->bdev;
1192 	raid_bdev_gen->blocklen = blocklen;
1193 
1194 	rc = raid_bdev->module->start(raid_bdev);
1195 	if (rc != 0) {
1196 		SPDK_ERRLOG("raid module startup callback failed\n");
1197 		return rc;
1198 	}
1199 	raid_bdev->state = RAID_BDEV_STATE_ONLINE;
1200 	SPDK_DEBUGLOG(bdev_raid, "io device register %p\n", raid_bdev);
1201 	SPDK_DEBUGLOG(bdev_raid, "blockcnt %" PRIu64 ", blocklen %u\n",
1202 		      raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen);
1203 	spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb,
1204 				sizeof(struct raid_bdev_io_channel),
1205 				raid_bdev->bdev.name);
1206 	rc = spdk_bdev_register(raid_bdev_gen);
1207 	if (rc != 0) {
1208 		SPDK_ERRLOG("Unable to register raid bdev and stay at configuring state\n");
1209 		if (raid_bdev->module->stop != NULL) {
1210 			raid_bdev->module->stop(raid_bdev);
1211 		}
1212 		spdk_io_device_unregister(raid_bdev, NULL);
1213 		raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
1214 		return rc;
1215 	}
1216 	SPDK_DEBUGLOG(bdev_raid, "raid bdev generic %p\n", raid_bdev_gen);
1217 	TAILQ_REMOVE(&g_raid_bdev_configuring_list, raid_bdev, state_link);
1218 	TAILQ_INSERT_TAIL(&g_raid_bdev_configured_list, raid_bdev, state_link);
1219 	SPDK_DEBUGLOG(bdev_raid, "raid bdev is created with name %s, raid_bdev %p\n",
1220 		      raid_bdev_gen->name, raid_bdev);
1221 
1222 	return 0;
1223 }
1224 
1225 /*
1226  * brief:
1227  * If raid bdev is online and registered, change the bdev state to
1228  * configuring and unregister this raid device. Queue this raid device
1229  * in configuring list
1230  * params:
1231  * raid_bdev - pointer to raid bdev
1232  * cb_fn - callback function
1233  * cb_arg - argument to callback function
1234  * returns:
1235  * none
1236  */
1237 static void
1238 raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn,
1239 		      void *cb_arg)
1240 {
1241 	if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) {
1242 		if (cb_fn) {
1243 			cb_fn(cb_arg, 0);
1244 		}
1245 		return;
1246 	}
1247 
1248 	assert(raid_bdev->num_base_bdevs == raid_bdev->num_base_bdevs_discovered);
1249 	TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link);
1250 	if (raid_bdev->module->stop != NULL) {
1251 		raid_bdev->module->stop(raid_bdev);
1252 	}
1253 	raid_bdev->state = RAID_BDEV_STATE_OFFLINE;
1254 	assert(raid_bdev->num_base_bdevs_discovered);
1255 	TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link);
1256 	SPDK_DEBUGLOG(bdev_raid, "raid bdev state chaning from online to offline\n");
1257 
1258 	spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg);
1259 }
1260 
1261 /*
1262  * brief:
1263  * raid_bdev_find_by_base_bdev function finds the raid bdev which has
1264  *  claimed the base bdev.
1265  * params:
1266  * base_bdev - pointer to base bdev pointer
1267  * _raid_bdev - Reference to pointer to raid bdev
1268  * _base_info - Reference to the raid base bdev info.
1269  * returns:
1270  * true - if the raid bdev is found.
1271  * false - if the raid bdev is not found.
1272  */
1273 static bool
1274 raid_bdev_find_by_base_bdev(struct spdk_bdev *base_bdev, struct raid_bdev **_raid_bdev,
1275 			    struct raid_base_bdev_info **_base_info)
1276 {
1277 	struct raid_bdev *raid_bdev;
1278 	struct raid_base_bdev_info *base_info;
1279 
1280 	TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
1281 		RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1282 			if (base_info->bdev == base_bdev) {
1283 				*_raid_bdev = raid_bdev;
1284 				*_base_info = base_info;
1285 				return true;
1286 			}
1287 		}
1288 	}
1289 
1290 	return false;
1291 }
1292 
1293 /*
1294  * brief:
1295  * raid_bdev_remove_base_bdev function is called by below layers when base_bdev
1296  * is removed. This function checks if this base bdev is part of any raid bdev
1297  * or not. If yes, it takes necessary action on that particular raid bdev.
1298  * params:
1299  * base_bdev - pointer to base bdev pointer which got removed
1300  * returns:
1301  * none
1302  */
1303 static void
1304 raid_bdev_remove_base_bdev(struct spdk_bdev *base_bdev)
1305 {
1306 	struct raid_bdev	*raid_bdev = NULL;
1307 	struct raid_base_bdev_info *base_info;
1308 
1309 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_remove_base_bdev\n");
1310 
1311 	/* Find the raid_bdev which has claimed this base_bdev */
1312 	if (!raid_bdev_find_by_base_bdev(base_bdev, &raid_bdev, &base_info)) {
1313 		SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name);
1314 		return;
1315 	}
1316 
1317 	assert(base_info->desc);
1318 	base_info->remove_scheduled = true;
1319 
1320 	if (raid_bdev->destruct_called == true ||
1321 	    raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) {
1322 		/*
1323 		 * As raid bdev is not registered yet or already unregistered,
1324 		 * so cleanup should be done here itself.
1325 		 */
1326 		raid_bdev_free_base_bdev_resource(raid_bdev, base_info);
1327 		if (raid_bdev->num_base_bdevs_discovered == 0) {
1328 			/* There is no base bdev for this raid, so free the raid device. */
1329 			raid_bdev_cleanup(raid_bdev);
1330 			return;
1331 		}
1332 	}
1333 
1334 	raid_bdev_deconfigure(raid_bdev, NULL, NULL);
1335 }
1336 
1337 /*
1338  * brief:
1339  * raid_bdev_event_base_bdev function is called by below layers when base_bdev
1340  * triggers asynchronous event.
1341  * params:
1342  * type - event details.
1343  * bdev - bdev that triggered event.
1344  * event_ctx - context for event.
1345  * returns:
1346  * none
1347  */
1348 static void
1349 raid_bdev_event_base_bdev(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
1350 			  void *event_ctx)
1351 {
1352 	switch (type) {
1353 	case SPDK_BDEV_EVENT_REMOVE:
1354 		raid_bdev_remove_base_bdev(bdev);
1355 		break;
1356 	default:
1357 		SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
1358 		break;
1359 	}
1360 }
1361 
1362 /*
1363  * brief:
1364  * Remove base bdevs from the raid bdev one by one.  Skip any base bdev which
1365  *  doesn't exist.
1366  * params:
1367  * raid_cfg - pointer to raid bdev config.
1368  * cb_fn - callback function
1369  * cb_ctx - argument to callback function
1370  */
1371 void
1372 raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg,
1373 			      raid_bdev_destruct_cb cb_fn, void *cb_arg)
1374 {
1375 	struct raid_bdev		*raid_bdev;
1376 	struct raid_base_bdev_info	*base_info;
1377 
1378 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_remove_base_devices\n");
1379 
1380 	raid_bdev = raid_cfg->raid_bdev;
1381 	if (raid_bdev == NULL) {
1382 		SPDK_DEBUGLOG(bdev_raid, "raid bdev %s doesn't exist now\n", raid_cfg->name);
1383 		if (cb_fn) {
1384 			cb_fn(cb_arg, 0);
1385 		}
1386 		return;
1387 	}
1388 
1389 	if (raid_bdev->destroy_started) {
1390 		SPDK_DEBUGLOG(bdev_raid, "destroying raid bdev %s is already started\n",
1391 			      raid_cfg->name);
1392 		if (cb_fn) {
1393 			cb_fn(cb_arg, -EALREADY);
1394 		}
1395 		return;
1396 	}
1397 
1398 	raid_bdev->destroy_started = true;
1399 
1400 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1401 		if (base_info->bdev == NULL) {
1402 			continue;
1403 		}
1404 
1405 		assert(base_info->desc);
1406 		base_info->remove_scheduled = true;
1407 
1408 		if (raid_bdev->destruct_called == true ||
1409 		    raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) {
1410 			/*
1411 			 * As raid bdev is not registered yet or already unregistered,
1412 			 * so cleanup should be done here itself.
1413 			 */
1414 			raid_bdev_free_base_bdev_resource(raid_bdev, base_info);
1415 			if (raid_bdev->num_base_bdevs_discovered == 0) {
1416 				/* There is no base bdev for this raid, so free the raid device. */
1417 				raid_bdev_cleanup(raid_bdev);
1418 				if (cb_fn) {
1419 					cb_fn(cb_arg, 0);
1420 				}
1421 				return;
1422 			}
1423 		}
1424 	}
1425 
1426 	raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg);
1427 }
1428 
1429 /*
1430  * brief:
1431  * raid_bdev_add_base_device function is the actual function which either adds
1432  * the nvme base device to existing raid bdev or create a new raid bdev. It also claims
1433  * the base device and keep the open descriptor.
1434  * params:
1435  * raid_cfg - pointer to raid bdev config
1436  * bdev - pointer to base bdev
1437  * base_bdev_slot - position to add base bdev
1438  * returns:
1439  * 0 - success
1440  * non zero - failure
1441  */
1442 static int
1443 raid_bdev_add_base_device(struct raid_bdev_config *raid_cfg, const char *bdev_name,
1444 			  uint8_t base_bdev_slot)
1445 {
1446 	struct raid_bdev	*raid_bdev;
1447 	int			rc;
1448 
1449 	raid_bdev = raid_cfg->raid_bdev;
1450 	if (!raid_bdev) {
1451 		SPDK_ERRLOG("Raid bdev '%s' is not created yet\n", raid_cfg->name);
1452 		return -ENODEV;
1453 	}
1454 
1455 	rc = raid_bdev_alloc_base_bdev_resource(raid_bdev, bdev_name, base_bdev_slot);
1456 	if (rc != 0) {
1457 		if (rc != -ENODEV) {
1458 			SPDK_ERRLOG("Failed to allocate resource for bdev '%s'\n", bdev_name);
1459 		}
1460 		return rc;
1461 	}
1462 
1463 	assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs);
1464 
1465 	if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs) {
1466 		rc = raid_bdev_configure(raid_bdev);
1467 		if (rc != 0) {
1468 			SPDK_ERRLOG("Failed to configure raid bdev\n");
1469 			return rc;
1470 		}
1471 	}
1472 
1473 	return 0;
1474 }
1475 
1476 /*
1477  * brief:
1478  * Add base bdevs to the raid bdev one by one.  Skip any base bdev which doesn't
1479  *  exist or fails to add. If all base bdevs are successfully added, the raid bdev
1480  *  moves to the configured state and becomes available. Otherwise, the raid bdev
1481  *  stays at the configuring state with added base bdevs.
1482  * params:
1483  * raid_cfg - pointer to raid bdev config
1484  * returns:
1485  * 0 - The raid bdev moves to the configured state or stays at the configuring
1486  *     state with added base bdevs due to any nonexistent base bdev.
1487  * non zero - Failed to add any base bdev and stays at the configuring state with
1488  *            added base bdevs.
1489  */
1490 int
1491 raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg)
1492 {
1493 	uint8_t	i;
1494 	int	rc = 0, _rc;
1495 
1496 	for (i = 0; i < raid_cfg->num_base_bdevs; i++) {
1497 		_rc = raid_bdev_add_base_device(raid_cfg, raid_cfg->base_bdev[i].name, i);
1498 		if (_rc == -ENODEV) {
1499 			SPDK_DEBUGLOG(bdev_raid, "base bdev %s doesn't exist now\n",
1500 				      raid_cfg->base_bdev[i].name);
1501 		} else if (_rc != 0) {
1502 			SPDK_ERRLOG("Failed to add base bdev %s to RAID bdev %s: %s\n",
1503 				    raid_cfg->base_bdev[i].name, raid_cfg->name,
1504 				    spdk_strerror(-_rc));
1505 			if (rc == 0) {
1506 				rc = _rc;
1507 			}
1508 		}
1509 	}
1510 
1511 	return rc;
1512 }
1513 
1514 /*
1515  * brief:
1516  * raid_bdev_examine function is the examine function call by the below layers
1517  * like bdev_nvme layer. This function will check if this base bdev can be
1518  * claimed by this raid bdev or not.
1519  * params:
1520  * bdev - pointer to base bdev
1521  * returns:
1522  * none
1523  */
1524 static void
1525 raid_bdev_examine(struct spdk_bdev *bdev)
1526 {
1527 	struct raid_bdev_config	*raid_cfg;
1528 	uint8_t			base_bdev_slot;
1529 
1530 	if (raid_bdev_can_claim_bdev(bdev->name, &raid_cfg, &base_bdev_slot)) {
1531 		raid_bdev_add_base_device(raid_cfg, bdev->name, base_bdev_slot);
1532 	} else {
1533 		SPDK_DEBUGLOG(bdev_raid, "bdev %s can't be claimed\n",
1534 			      bdev->name);
1535 	}
1536 
1537 	spdk_bdev_module_examine_done(&g_raid_if);
1538 }
1539 
1540 /* Log component for bdev raid bdev module */
1541 SPDK_LOG_REGISTER_COMPONENT(bdev_raid)
1542