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