xref: /spdk/module/bdev/raid/bdev_raid.c (revision c86778398b880aa6d671c17d4dc852250189f1d1)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *   Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "bdev_raid.h"
36 #include "spdk/env.h"
37 #include "spdk/thread.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_kb", raid_bdev->strip_size_kb);
627 	spdk_json_write_named_uint32(w, "state", raid_bdev->state);
628 	spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
629 	spdk_json_write_named_uint32(w, "destruct_called", raid_bdev->destruct_called);
630 	spdk_json_write_named_uint32(w, "num_base_bdevs", raid_bdev->num_base_bdevs);
631 	spdk_json_write_named_uint32(w, "num_base_bdevs_discovered", raid_bdev->num_base_bdevs_discovered);
632 	spdk_json_write_name(w, "base_bdevs_list");
633 	spdk_json_write_array_begin(w);
634 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
635 		if (base_info->bdev) {
636 			spdk_json_write_string(w, base_info->bdev->name);
637 		} else {
638 			spdk_json_write_null(w);
639 		}
640 	}
641 	spdk_json_write_array_end(w);
642 	spdk_json_write_object_end(w);
643 
644 	return 0;
645 }
646 
647 /*
648  * brief:
649  * raid_bdev_write_config_json is the function table pointer for raid bdev
650  * params:
651  * bdev - pointer to spdk_bdev
652  * w - pointer to json context
653  * returns:
654  * none
655  */
656 static void
657 raid_bdev_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
658 {
659 	struct raid_bdev *raid_bdev = bdev->ctxt;
660 	struct raid_base_bdev_info *base_info;
661 
662 	spdk_json_write_object_begin(w);
663 
664 	spdk_json_write_named_string(w, "method", "bdev_raid_create");
665 
666 	spdk_json_write_named_object_begin(w, "params");
667 	spdk_json_write_named_string(w, "name", bdev->name);
668 	spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb);
669 	spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
670 
671 	spdk_json_write_named_array_begin(w, "base_bdevs");
672 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
673 		if (base_info->bdev) {
674 			spdk_json_write_string(w, base_info->bdev->name);
675 		}
676 	}
677 	spdk_json_write_array_end(w);
678 	spdk_json_write_object_end(w);
679 
680 	spdk_json_write_object_end(w);
681 }
682 
683 static int
684 raid_bdev_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size)
685 {
686 	struct raid_bdev *raid_bdev = ctx;
687 	struct spdk_bdev *base_bdev;
688 	uint32_t i;
689 	int domains_count = 0, rc;
690 
691 	/* First loop to get the number of memory domains */
692 	for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
693 		base_bdev = raid_bdev->base_bdev_info[i].bdev;
694 		rc = spdk_bdev_get_memory_domains(base_bdev, NULL, 0);
695 		if (rc < 0) {
696 			return rc;
697 		}
698 		domains_count += rc;
699 	}
700 
701 	if (!domains || array_size < domains_count) {
702 		return domains_count;
703 	}
704 
705 	for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
706 		base_bdev = raid_bdev->base_bdev_info[i].bdev;
707 		rc = spdk_bdev_get_memory_domains(base_bdev, domains, array_size);
708 		if (rc < 0) {
709 			return rc;
710 		}
711 		domains += rc;
712 		array_size -= rc;
713 	}
714 
715 	return domains_count;
716 }
717 
718 /* g_raid_bdev_fn_table is the function table for raid bdev */
719 static const struct spdk_bdev_fn_table g_raid_bdev_fn_table = {
720 	.destruct		= raid_bdev_destruct,
721 	.submit_request		= raid_bdev_submit_request,
722 	.io_type_supported	= raid_bdev_io_type_supported,
723 	.get_io_channel		= raid_bdev_get_io_channel,
724 	.dump_info_json		= raid_bdev_dump_info_json,
725 	.write_config_json	= raid_bdev_write_config_json,
726 	.get_memory_domains	= raid_bdev_get_memory_domains,
727 };
728 
729 /*
730  * brief:
731  * raid_bdev_config_cleanup function is used to free memory for one raid_bdev in configuration
732  * params:
733  * raid_cfg - pointer to raid_bdev_config structure
734  * returns:
735  * none
736  */
737 void
738 raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg)
739 {
740 	uint8_t i;
741 
742 	TAILQ_REMOVE(&g_raid_config.raid_bdev_config_head, raid_cfg, link);
743 	g_raid_config.total_raid_bdev--;
744 
745 	if (raid_cfg->base_bdev) {
746 		for (i = 0; i < raid_cfg->num_base_bdevs; i++) {
747 			free(raid_cfg->base_bdev[i].name);
748 		}
749 		free(raid_cfg->base_bdev);
750 	}
751 	free(raid_cfg->name);
752 	free(raid_cfg);
753 }
754 
755 /*
756  * brief:
757  * raid_bdev_free is the raid bdev function table function pointer. This is
758  * called on bdev free path
759  * params:
760  * none
761  * returns:
762  * none
763  */
764 static void
765 raid_bdev_free(void)
766 {
767 	struct raid_bdev_config *raid_cfg, *tmp;
768 
769 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_free\n");
770 	TAILQ_FOREACH_SAFE(raid_cfg, &g_raid_config.raid_bdev_config_head, link, tmp) {
771 		raid_bdev_config_cleanup(raid_cfg);
772 	}
773 }
774 
775 /* brief
776  * raid_bdev_config_find_by_name is a helper function to find raid bdev config
777  * by name as key.
778  *
779  * params:
780  * raid_name - name for raid bdev.
781  */
782 struct raid_bdev_config *
783 raid_bdev_config_find_by_name(const char *raid_name)
784 {
785 	struct raid_bdev_config *raid_cfg;
786 
787 	TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) {
788 		if (!strcmp(raid_cfg->name, raid_name)) {
789 			return raid_cfg;
790 		}
791 	}
792 
793 	return raid_cfg;
794 }
795 
796 /*
797  * brief
798  * raid_bdev_config_add function adds config for newly created raid bdev.
799  *
800  * params:
801  * raid_name - name for raid bdev.
802  * strip_size - strip size in KB
803  * num_base_bdevs - number of base bdevs.
804  * level - raid level.
805  * _raid_cfg - Pointer to newly added configuration
806  */
807 int
808 raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs,
809 		     enum raid_level level, struct raid_bdev_config **_raid_cfg)
810 {
811 	struct raid_bdev_config *raid_cfg;
812 
813 	raid_cfg = raid_bdev_config_find_by_name(raid_name);
814 	if (raid_cfg != NULL) {
815 		SPDK_ERRLOG("Duplicate raid bdev name found in config file %s\n",
816 			    raid_name);
817 		return -EEXIST;
818 	}
819 
820 	if (spdk_u32_is_pow2(strip_size) == false) {
821 		SPDK_ERRLOG("Invalid strip size %" PRIu32 "\n", strip_size);
822 		return -EINVAL;
823 	}
824 
825 	if (num_base_bdevs == 0) {
826 		SPDK_ERRLOG("Invalid base device count %u\n", num_base_bdevs);
827 		return -EINVAL;
828 	}
829 
830 	raid_cfg = calloc(1, sizeof(*raid_cfg));
831 	if (raid_cfg == NULL) {
832 		SPDK_ERRLOG("unable to allocate memory\n");
833 		return -ENOMEM;
834 	}
835 
836 	raid_cfg->name = strdup(raid_name);
837 	if (!raid_cfg->name) {
838 		free(raid_cfg);
839 		SPDK_ERRLOG("unable to allocate memory\n");
840 		return -ENOMEM;
841 	}
842 	raid_cfg->strip_size = strip_size;
843 	raid_cfg->num_base_bdevs = num_base_bdevs;
844 	raid_cfg->level = level;
845 
846 	raid_cfg->base_bdev = calloc(num_base_bdevs, sizeof(*raid_cfg->base_bdev));
847 	if (raid_cfg->base_bdev == NULL) {
848 		free(raid_cfg->name);
849 		free(raid_cfg);
850 		SPDK_ERRLOG("unable to allocate memory\n");
851 		return -ENOMEM;
852 	}
853 
854 	TAILQ_INSERT_TAIL(&g_raid_config.raid_bdev_config_head, raid_cfg, link);
855 	g_raid_config.total_raid_bdev++;
856 
857 	*_raid_cfg = raid_cfg;
858 	return 0;
859 }
860 
861 /*
862  * brief:
863  * raid_bdev_config_add_base_bdev function add base bdev to raid bdev config.
864  *
865  * params:
866  * raid_cfg - pointer to raid bdev configuration
867  * base_bdev_name - name of base bdev
868  * slot - Position to add base bdev
869  */
870 int
871 raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg, const char *base_bdev_name,
872 			       uint8_t slot)
873 {
874 	uint8_t i;
875 	struct raid_bdev_config *tmp;
876 
877 	if (slot >= raid_cfg->num_base_bdevs) {
878 		return -EINVAL;
879 	}
880 
881 	TAILQ_FOREACH(tmp, &g_raid_config.raid_bdev_config_head, link) {
882 		for (i = 0; i < tmp->num_base_bdevs; i++) {
883 			if (tmp->base_bdev[i].name != NULL) {
884 				if (!strcmp(tmp->base_bdev[i].name, base_bdev_name)) {
885 					SPDK_ERRLOG("duplicate base bdev name %s mentioned\n",
886 						    base_bdev_name);
887 					return -EEXIST;
888 				}
889 			}
890 		}
891 	}
892 
893 	raid_cfg->base_bdev[slot].name = strdup(base_bdev_name);
894 	if (raid_cfg->base_bdev[slot].name == NULL) {
895 		SPDK_ERRLOG("unable to allocate memory\n");
896 		return -ENOMEM;
897 	}
898 
899 	return 0;
900 }
901 
902 static struct {
903 	const char *name;
904 	enum raid_level value;
905 } g_raid_level_names[] = {
906 	{ "raid0", RAID0 },
907 	{ "0", RAID0 },
908 	{ "raid5", RAID5 },
909 	{ "5", RAID5 },
910 	{ "concat", CONCAT },
911 	{ }
912 };
913 
914 enum raid_level raid_bdev_parse_raid_level(const char *str)
915 {
916 	unsigned int i;
917 
918 	assert(str != NULL);
919 
920 	for (i = 0; g_raid_level_names[i].name != NULL; i++) {
921 		if (strcasecmp(g_raid_level_names[i].name, str) == 0) {
922 			return g_raid_level_names[i].value;
923 		}
924 	}
925 
926 	return INVALID_RAID_LEVEL;
927 }
928 
929 const char *
930 raid_bdev_level_to_str(enum raid_level level)
931 {
932 	unsigned int i;
933 
934 	for (i = 0; g_raid_level_names[i].name != NULL; i++) {
935 		if (g_raid_level_names[i].value == level) {
936 			return g_raid_level_names[i].name;
937 		}
938 	}
939 
940 	return "";
941 }
942 
943 /*
944  * brief:
945  * raid_bdev_fini_start is called when bdev layer is starting the
946  * shutdown process
947  * params:
948  * none
949  * returns:
950  * none
951  */
952 static void
953 raid_bdev_fini_start(void)
954 {
955 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_fini_start\n");
956 	g_shutdown_started = true;
957 }
958 
959 /*
960  * brief:
961  * raid_bdev_exit is called on raid bdev module exit time by bdev layer
962  * params:
963  * none
964  * returns:
965  * none
966  */
967 static void
968 raid_bdev_exit(void)
969 {
970 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_exit\n");
971 	raid_bdev_free();
972 }
973 
974 /*
975  * brief:
976  * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid
977  * module
978  * params:
979  * none
980  * returns:
981  * size of spdk_bdev_io context for raid
982  */
983 static int
984 raid_bdev_get_ctx_size(void)
985 {
986 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_get_ctx_size\n");
987 	return sizeof(struct raid_bdev_io);
988 }
989 
990 /*
991  * brief:
992  * raid_bdev_can_claim_bdev is the function to check if this base_bdev can be
993  * claimed by raid bdev or not.
994  * params:
995  * bdev_name - represents base bdev name
996  * _raid_cfg - pointer to raid bdev config parsed from config file
997  * base_bdev_slot - if bdev can be claimed, it represents the base_bdev correct
998  * slot. This field is only valid if return value of this function is true
999  * returns:
1000  * true - if bdev can be claimed
1001  * false - if bdev can't be claimed
1002  */
1003 static bool
1004 raid_bdev_can_claim_bdev(const char *bdev_name, struct raid_bdev_config **_raid_cfg,
1005 			 uint8_t *base_bdev_slot)
1006 {
1007 	struct raid_bdev_config *raid_cfg;
1008 	uint8_t i;
1009 
1010 	TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) {
1011 		for (i = 0; i < raid_cfg->num_base_bdevs; i++) {
1012 			/*
1013 			 * Check if the base bdev name is part of raid bdev configuration.
1014 			 * If match is found then return true and the slot information where
1015 			 * this base bdev should be inserted in raid bdev
1016 			 */
1017 			if (!strcmp(bdev_name, raid_cfg->base_bdev[i].name)) {
1018 				*_raid_cfg = raid_cfg;
1019 				*base_bdev_slot = i;
1020 				return true;
1021 			}
1022 		}
1023 	}
1024 
1025 	return false;
1026 }
1027 
1028 
1029 static struct spdk_bdev_module g_raid_if = {
1030 	.name = "raid",
1031 	.module_init = raid_bdev_init,
1032 	.fini_start = raid_bdev_fini_start,
1033 	.module_fini = raid_bdev_exit,
1034 	.get_ctx_size = raid_bdev_get_ctx_size,
1035 	.examine_config = raid_bdev_examine,
1036 	.async_init = false,
1037 	.async_fini = false,
1038 };
1039 SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if)
1040 
1041 /*
1042  * brief:
1043  * raid_bdev_init is the initialization function for raid bdev module
1044  * params:
1045  * none
1046  * returns:
1047  * 0 - success
1048  * non zero - failure
1049  */
1050 static int
1051 raid_bdev_init(void)
1052 {
1053 	return 0;
1054 }
1055 
1056 /*
1057  * brief:
1058  * raid_bdev_create allocates raid bdev based on passed configuration
1059  * params:
1060  * raid_cfg - configuration of raid bdev
1061  * returns:
1062  * 0 - success
1063  * non zero - failure
1064  */
1065 int
1066 raid_bdev_create(struct raid_bdev_config *raid_cfg)
1067 {
1068 	struct raid_bdev *raid_bdev;
1069 	struct spdk_bdev *raid_bdev_gen;
1070 	struct raid_bdev_module *module;
1071 
1072 	module = raid_bdev_module_find(raid_cfg->level);
1073 	if (module == NULL) {
1074 		SPDK_ERRLOG("Unsupported raid level '%d'\n", raid_cfg->level);
1075 		return -EINVAL;
1076 	}
1077 
1078 	assert(module->base_bdevs_min != 0);
1079 	if (raid_cfg->num_base_bdevs < module->base_bdevs_min) {
1080 		SPDK_ERRLOG("At least %u base devices required for %s\n",
1081 			    module->base_bdevs_min,
1082 			    raid_bdev_level_to_str(raid_cfg->level));
1083 		return -EINVAL;
1084 	}
1085 
1086 	raid_bdev = calloc(1, sizeof(*raid_bdev));
1087 	if (!raid_bdev) {
1088 		SPDK_ERRLOG("Unable to allocate memory for raid bdev\n");
1089 		return -ENOMEM;
1090 	}
1091 
1092 	raid_bdev->module = module;
1093 	raid_bdev->num_base_bdevs = raid_cfg->num_base_bdevs;
1094 	raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs,
1095 					   sizeof(struct raid_base_bdev_info));
1096 	if (!raid_bdev->base_bdev_info) {
1097 		SPDK_ERRLOG("Unable able to allocate base bdev info\n");
1098 		free(raid_bdev);
1099 		return -ENOMEM;
1100 	}
1101 
1102 	/* strip_size_kb is from the rpc param.  strip_size is in blocks and used
1103 	 * internally and set later.
1104 	 */
1105 	raid_bdev->strip_size = 0;
1106 	raid_bdev->strip_size_kb = raid_cfg->strip_size;
1107 	raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
1108 	raid_bdev->config = raid_cfg;
1109 	raid_bdev->level = raid_cfg->level;
1110 
1111 	raid_bdev_gen = &raid_bdev->bdev;
1112 
1113 	raid_bdev_gen->name = strdup(raid_cfg->name);
1114 	if (!raid_bdev_gen->name) {
1115 		SPDK_ERRLOG("Unable to allocate name for raid\n");
1116 		free(raid_bdev->base_bdev_info);
1117 		free(raid_bdev);
1118 		return -ENOMEM;
1119 	}
1120 
1121 	raid_bdev_gen->product_name = "Raid Volume";
1122 	raid_bdev_gen->ctxt = raid_bdev;
1123 	raid_bdev_gen->fn_table = &g_raid_bdev_fn_table;
1124 	raid_bdev_gen->module = &g_raid_if;
1125 	raid_bdev_gen->write_cache = 0;
1126 
1127 	TAILQ_INSERT_TAIL(&g_raid_bdev_configuring_list, raid_bdev, state_link);
1128 	TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link);
1129 
1130 	raid_cfg->raid_bdev = raid_bdev;
1131 
1132 	return 0;
1133 }
1134 
1135 /*
1136  * brief
1137  * raid_bdev_alloc_base_bdev_resource allocates resource of base bdev.
1138  * params:
1139  * raid_bdev - pointer to raid bdev
1140  * bdev_name - base bdev name
1141  * base_bdev_slot - position to add base bdev
1142  * returns:
1143  * 0 - success
1144  * non zero - failure
1145  */
1146 static int
1147 raid_bdev_alloc_base_bdev_resource(struct raid_bdev *raid_bdev, const char *bdev_name,
1148 				   uint8_t base_bdev_slot)
1149 {
1150 	struct spdk_bdev_desc *desc;
1151 	struct spdk_bdev *bdev;
1152 	int rc;
1153 
1154 	rc = spdk_bdev_open_ext(bdev_name, true, raid_bdev_event_base_bdev, NULL, &desc);
1155 	if (rc != 0) {
1156 		if (rc != -ENODEV) {
1157 			SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", bdev_name);
1158 		}
1159 		return rc;
1160 	}
1161 
1162 	bdev = spdk_bdev_desc_get_bdev(desc);
1163 
1164 	rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if);
1165 	if (rc != 0) {
1166 		SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n");
1167 		spdk_bdev_close(desc);
1168 		return rc;
1169 	}
1170 
1171 	SPDK_DEBUGLOG(bdev_raid, "bdev %s is claimed\n", bdev_name);
1172 
1173 	assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE);
1174 	assert(base_bdev_slot < raid_bdev->num_base_bdevs);
1175 
1176 	raid_bdev->base_bdev_info[base_bdev_slot].thread = spdk_get_thread();
1177 	raid_bdev->base_bdev_info[base_bdev_slot].bdev = bdev;
1178 	raid_bdev->base_bdev_info[base_bdev_slot].desc = desc;
1179 	raid_bdev->num_base_bdevs_discovered++;
1180 	assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs);
1181 
1182 	return 0;
1183 }
1184 
1185 /*
1186  * brief:
1187  * If raid bdev config is complete, then only register the raid bdev to
1188  * bdev layer and remove this raid bdev from configuring list and
1189  * insert the raid bdev to configured list
1190  * params:
1191  * raid_bdev - pointer to raid bdev
1192  * returns:
1193  * 0 - success
1194  * non zero - failure
1195  */
1196 static int
1197 raid_bdev_configure(struct raid_bdev *raid_bdev)
1198 {
1199 	uint32_t blocklen = 0;
1200 	struct spdk_bdev *raid_bdev_gen;
1201 	struct raid_base_bdev_info *base_info;
1202 	int rc = 0;
1203 
1204 	assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING);
1205 	assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs);
1206 
1207 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1208 		/* Check blocklen for all base bdevs that it should be same */
1209 		if (blocklen == 0) {
1210 			blocklen = base_info->bdev->blocklen;
1211 		} else if (blocklen != base_info->bdev->blocklen) {
1212 			/*
1213 			 * Assumption is that all the base bdevs for any raid bdev should
1214 			 * have same blocklen
1215 			 */
1216 			SPDK_ERRLOG("Blocklen of various bdevs not matching\n");
1217 			return -EINVAL;
1218 		}
1219 	}
1220 	assert(blocklen > 0);
1221 
1222 	/* The strip_size_kb is read in from user in KB. Convert to blocks here for
1223 	 * internal use.
1224 	 */
1225 	raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / blocklen;
1226 	raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size);
1227 	raid_bdev->blocklen_shift = spdk_u32log2(blocklen);
1228 
1229 	raid_bdev_gen = &raid_bdev->bdev;
1230 	raid_bdev_gen->blocklen = blocklen;
1231 
1232 	rc = raid_bdev->module->start(raid_bdev);
1233 	if (rc != 0) {
1234 		SPDK_ERRLOG("raid module startup callback failed\n");
1235 		return rc;
1236 	}
1237 	raid_bdev->state = RAID_BDEV_STATE_ONLINE;
1238 	SPDK_DEBUGLOG(bdev_raid, "io device register %p\n", raid_bdev);
1239 	SPDK_DEBUGLOG(bdev_raid, "blockcnt %" PRIu64 ", blocklen %u\n",
1240 		      raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen);
1241 	spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb,
1242 				sizeof(struct raid_bdev_io_channel),
1243 				raid_bdev->bdev.name);
1244 	rc = spdk_bdev_register(raid_bdev_gen);
1245 	if (rc != 0) {
1246 		SPDK_ERRLOG("Unable to register raid bdev and stay at configuring state\n");
1247 		if (raid_bdev->module->stop != NULL) {
1248 			raid_bdev->module->stop(raid_bdev);
1249 		}
1250 		spdk_io_device_unregister(raid_bdev, NULL);
1251 		raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
1252 		return rc;
1253 	}
1254 	SPDK_DEBUGLOG(bdev_raid, "raid bdev generic %p\n", raid_bdev_gen);
1255 	TAILQ_REMOVE(&g_raid_bdev_configuring_list, raid_bdev, state_link);
1256 	TAILQ_INSERT_TAIL(&g_raid_bdev_configured_list, raid_bdev, state_link);
1257 	SPDK_DEBUGLOG(bdev_raid, "raid bdev is created with name %s, raid_bdev %p\n",
1258 		      raid_bdev_gen->name, raid_bdev);
1259 
1260 	return 0;
1261 }
1262 
1263 /*
1264  * brief:
1265  * If raid bdev is online and registered, change the bdev state to
1266  * configuring and unregister this raid device. Queue this raid device
1267  * in configuring list
1268  * params:
1269  * raid_bdev - pointer to raid bdev
1270  * cb_fn - callback function
1271  * cb_arg - argument to callback function
1272  * returns:
1273  * none
1274  */
1275 static void
1276 raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn,
1277 		      void *cb_arg)
1278 {
1279 	if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) {
1280 		if (cb_fn) {
1281 			cb_fn(cb_arg, 0);
1282 		}
1283 		return;
1284 	}
1285 
1286 	assert(raid_bdev->num_base_bdevs == raid_bdev->num_base_bdevs_discovered);
1287 	TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link);
1288 	if (raid_bdev->module->stop != NULL) {
1289 		raid_bdev->module->stop(raid_bdev);
1290 	}
1291 	raid_bdev->state = RAID_BDEV_STATE_OFFLINE;
1292 	assert(raid_bdev->num_base_bdevs_discovered);
1293 	TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link);
1294 	SPDK_DEBUGLOG(bdev_raid, "raid bdev state changing from online to offline\n");
1295 
1296 	spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg);
1297 }
1298 
1299 /*
1300  * brief:
1301  * raid_bdev_find_by_base_bdev function finds the raid bdev which has
1302  *  claimed the base bdev.
1303  * params:
1304  * base_bdev - pointer to base bdev pointer
1305  * _raid_bdev - Reference to pointer to raid bdev
1306  * _base_info - Reference to the raid base bdev info.
1307  * returns:
1308  * true - if the raid bdev is found.
1309  * false - if the raid bdev is not found.
1310  */
1311 static bool
1312 raid_bdev_find_by_base_bdev(struct spdk_bdev *base_bdev, struct raid_bdev **_raid_bdev,
1313 			    struct raid_base_bdev_info **_base_info)
1314 {
1315 	struct raid_bdev *raid_bdev;
1316 	struct raid_base_bdev_info *base_info;
1317 
1318 	TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
1319 		RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1320 			if (base_info->bdev == base_bdev) {
1321 				*_raid_bdev = raid_bdev;
1322 				*_base_info = base_info;
1323 				return true;
1324 			}
1325 		}
1326 	}
1327 
1328 	return false;
1329 }
1330 
1331 /*
1332  * brief:
1333  * raid_bdev_remove_base_bdev function is called by below layers when base_bdev
1334  * is removed. This function checks if this base bdev is part of any raid bdev
1335  * or not. If yes, it takes necessary action on that particular raid bdev.
1336  * params:
1337  * base_bdev - pointer to base bdev pointer which got removed
1338  * returns:
1339  * none
1340  */
1341 static void
1342 raid_bdev_remove_base_bdev(struct spdk_bdev *base_bdev)
1343 {
1344 	struct raid_bdev	*raid_bdev = NULL;
1345 	struct raid_base_bdev_info *base_info;
1346 
1347 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_remove_base_bdev\n");
1348 
1349 	/* Find the raid_bdev which has claimed this base_bdev */
1350 	if (!raid_bdev_find_by_base_bdev(base_bdev, &raid_bdev, &base_info)) {
1351 		SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name);
1352 		return;
1353 	}
1354 
1355 	assert(base_info->desc);
1356 	base_info->remove_scheduled = true;
1357 
1358 	if (raid_bdev->destruct_called == true ||
1359 	    raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) {
1360 		/*
1361 		 * As raid bdev is not registered yet or already unregistered,
1362 		 * so cleanup should be done here itself.
1363 		 */
1364 		raid_bdev_free_base_bdev_resource(raid_bdev, base_info);
1365 		if (raid_bdev->num_base_bdevs_discovered == 0) {
1366 			/* There is no base bdev for this raid, so free the raid device. */
1367 			raid_bdev_cleanup(raid_bdev);
1368 			return;
1369 		}
1370 	}
1371 
1372 	raid_bdev_deconfigure(raid_bdev, NULL, NULL);
1373 }
1374 
1375 /*
1376  * brief:
1377  * raid_bdev_event_base_bdev function is called by below layers when base_bdev
1378  * triggers asynchronous event.
1379  * params:
1380  * type - event details.
1381  * bdev - bdev that triggered event.
1382  * event_ctx - context for event.
1383  * returns:
1384  * none
1385  */
1386 static void
1387 raid_bdev_event_base_bdev(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
1388 			  void *event_ctx)
1389 {
1390 	switch (type) {
1391 	case SPDK_BDEV_EVENT_REMOVE:
1392 		raid_bdev_remove_base_bdev(bdev);
1393 		break;
1394 	default:
1395 		SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
1396 		break;
1397 	}
1398 }
1399 
1400 /*
1401  * brief:
1402  * Remove base bdevs from the raid bdev one by one.  Skip any base bdev which
1403  *  doesn't exist.
1404  * params:
1405  * raid_cfg - pointer to raid bdev config.
1406  * cb_fn - callback function
1407  * cb_ctx - argument to callback function
1408  */
1409 void
1410 raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg,
1411 			      raid_bdev_destruct_cb cb_fn, void *cb_arg)
1412 {
1413 	struct raid_bdev		*raid_bdev;
1414 	struct raid_base_bdev_info	*base_info;
1415 
1416 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_remove_base_devices\n");
1417 
1418 	raid_bdev = raid_cfg->raid_bdev;
1419 	if (raid_bdev == NULL) {
1420 		SPDK_DEBUGLOG(bdev_raid, "raid bdev %s doesn't exist now\n", raid_cfg->name);
1421 		if (cb_fn) {
1422 			cb_fn(cb_arg, 0);
1423 		}
1424 		return;
1425 	}
1426 
1427 	if (raid_bdev->destroy_started) {
1428 		SPDK_DEBUGLOG(bdev_raid, "destroying raid bdev %s is already started\n",
1429 			      raid_cfg->name);
1430 		if (cb_fn) {
1431 			cb_fn(cb_arg, -EALREADY);
1432 		}
1433 		return;
1434 	}
1435 
1436 	raid_bdev->destroy_started = true;
1437 
1438 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1439 		if (base_info->bdev == NULL) {
1440 			continue;
1441 		}
1442 
1443 		assert(base_info->desc);
1444 		base_info->remove_scheduled = true;
1445 
1446 		if (raid_bdev->destruct_called == true ||
1447 		    raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) {
1448 			/*
1449 			 * As raid bdev is not registered yet or already unregistered,
1450 			 * so cleanup should be done here itself.
1451 			 */
1452 			raid_bdev_free_base_bdev_resource(raid_bdev, base_info);
1453 			if (raid_bdev->num_base_bdevs_discovered == 0) {
1454 				/* There is no base bdev for this raid, so free the raid device. */
1455 				raid_bdev_cleanup(raid_bdev);
1456 				if (cb_fn) {
1457 					cb_fn(cb_arg, 0);
1458 				}
1459 				return;
1460 			}
1461 		}
1462 	}
1463 
1464 	raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg);
1465 }
1466 
1467 /*
1468  * brief:
1469  * raid_bdev_add_base_device function is the actual function which either adds
1470  * the nvme base device to existing raid bdev or create a new raid bdev. It also claims
1471  * the base device and keep the open descriptor.
1472  * params:
1473  * raid_cfg - pointer to raid bdev config
1474  * bdev - pointer to base bdev
1475  * base_bdev_slot - position to add base bdev
1476  * returns:
1477  * 0 - success
1478  * non zero - failure
1479  */
1480 static int
1481 raid_bdev_add_base_device(struct raid_bdev_config *raid_cfg, const char *bdev_name,
1482 			  uint8_t base_bdev_slot)
1483 {
1484 	struct raid_bdev	*raid_bdev;
1485 	int			rc;
1486 
1487 	raid_bdev = raid_cfg->raid_bdev;
1488 	if (!raid_bdev) {
1489 		SPDK_ERRLOG("Raid bdev '%s' is not created yet\n", raid_cfg->name);
1490 		return -ENODEV;
1491 	}
1492 
1493 	rc = raid_bdev_alloc_base_bdev_resource(raid_bdev, bdev_name, base_bdev_slot);
1494 	if (rc != 0) {
1495 		if (rc != -ENODEV) {
1496 			SPDK_ERRLOG("Failed to allocate resource for bdev '%s'\n", bdev_name);
1497 		}
1498 		return rc;
1499 	}
1500 
1501 	assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs);
1502 
1503 	if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs) {
1504 		rc = raid_bdev_configure(raid_bdev);
1505 		if (rc != 0) {
1506 			SPDK_ERRLOG("Failed to configure raid bdev\n");
1507 			return rc;
1508 		}
1509 	}
1510 
1511 	return 0;
1512 }
1513 
1514 /*
1515  * brief:
1516  * Add base bdevs to the raid bdev one by one.  Skip any base bdev which doesn't
1517  *  exist or fails to add. If all base bdevs are successfully added, the raid bdev
1518  *  moves to the configured state and becomes available. Otherwise, the raid bdev
1519  *  stays at the configuring state with added base bdevs.
1520  * params:
1521  * raid_cfg - pointer to raid bdev config
1522  * returns:
1523  * 0 - The raid bdev moves to the configured state or stays at the configuring
1524  *     state with added base bdevs due to any nonexistent base bdev.
1525  * non zero - Failed to add any base bdev and stays at the configuring state with
1526  *            added base bdevs.
1527  */
1528 int
1529 raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg)
1530 {
1531 	uint8_t	i;
1532 	int	rc = 0, _rc;
1533 
1534 	for (i = 0; i < raid_cfg->num_base_bdevs; i++) {
1535 		_rc = raid_bdev_add_base_device(raid_cfg, raid_cfg->base_bdev[i].name, i);
1536 		if (_rc == -ENODEV) {
1537 			SPDK_DEBUGLOG(bdev_raid, "base bdev %s doesn't exist now\n",
1538 				      raid_cfg->base_bdev[i].name);
1539 		} else if (_rc != 0) {
1540 			SPDK_ERRLOG("Failed to add base bdev %s to RAID bdev %s: %s\n",
1541 				    raid_cfg->base_bdev[i].name, raid_cfg->name,
1542 				    spdk_strerror(-_rc));
1543 			if (rc == 0) {
1544 				rc = _rc;
1545 			}
1546 		}
1547 	}
1548 
1549 	return rc;
1550 }
1551 
1552 /*
1553  * brief:
1554  * raid_bdev_examine function is the examine function call by the below layers
1555  * like bdev_nvme layer. This function will check if this base bdev can be
1556  * claimed by this raid bdev or not.
1557  * params:
1558  * bdev - pointer to base bdev
1559  * returns:
1560  * none
1561  */
1562 static void
1563 raid_bdev_examine(struct spdk_bdev *bdev)
1564 {
1565 	struct raid_bdev_config	*raid_cfg;
1566 	uint8_t			base_bdev_slot;
1567 
1568 	if (raid_bdev_can_claim_bdev(bdev->name, &raid_cfg, &base_bdev_slot)) {
1569 		raid_bdev_add_base_device(raid_cfg, bdev->name, base_bdev_slot);
1570 	} else {
1571 		SPDK_DEBUGLOG(bdev_raid, "bdev %s can't be claimed\n",
1572 			      bdev->name);
1573 	}
1574 
1575 	spdk_bdev_module_examine_done(&g_raid_if);
1576 }
1577 
1578 /* Log component for bdev raid bdev module */
1579 SPDK_LOG_REGISTER_COMPONENT(bdev_raid)
1580