xref: /spdk/module/bdev/raid/bdev_raid.c (revision 588dfe314bb83d86effdf67ec42837b11c2620bf)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  *   Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #include "bdev_raid.h"
8 #include "spdk/env.h"
9 #include "spdk/thread.h"
10 #include "spdk/log.h"
11 #include "spdk/string.h"
12 #include "spdk/util.h"
13 #include "spdk/json.h"
14 #include "spdk/string.h"
15 
16 static bool g_shutdown_started = false;
17 
18 /* List of all raid bdevs */
19 struct raid_all_tailq g_raid_bdev_list = TAILQ_HEAD_INITIALIZER(g_raid_bdev_list);
20 
21 static TAILQ_HEAD(, raid_bdev_module) g_raid_modules = TAILQ_HEAD_INITIALIZER(g_raid_modules);
22 
23 static struct raid_bdev_module *
24 raid_bdev_module_find(enum raid_level level)
25 {
26 	struct raid_bdev_module *raid_module;
27 
28 	TAILQ_FOREACH(raid_module, &g_raid_modules, link) {
29 		if (raid_module->level == level) {
30 			return raid_module;
31 		}
32 	}
33 
34 	return NULL;
35 }
36 
37 void
38 raid_bdev_module_list_add(struct raid_bdev_module *raid_module)
39 {
40 	if (raid_bdev_module_find(raid_module->level) != NULL) {
41 		SPDK_ERRLOG("module for raid level '%s' already registered.\n",
42 			    raid_bdev_level_to_str(raid_module->level));
43 		assert(false);
44 	} else {
45 		TAILQ_INSERT_TAIL(&g_raid_modules, raid_module, link);
46 	}
47 }
48 
49 /* Function declarations */
50 static void	raid_bdev_examine(struct spdk_bdev *bdev);
51 static int	raid_bdev_init(void);
52 static void	raid_bdev_deconfigure(struct raid_bdev *raid_bdev,
53 				      raid_bdev_destruct_cb cb_fn, void *cb_arg);
54 
55 /*
56  * brief:
57  * raid_bdev_create_cb function is a cb function for raid bdev which creates the
58  * hierarchy from raid bdev to base bdev io channels. It will be called per core
59  * params:
60  * io_device - pointer to raid bdev io device represented by raid_bdev
61  * ctx_buf - pointer to context buffer for raid bdev io channel
62  * returns:
63  * 0 - success
64  * non zero - failure
65  */
66 static int
67 raid_bdev_create_cb(void *io_device, void *ctx_buf)
68 {
69 	struct raid_bdev            *raid_bdev = io_device;
70 	struct raid_bdev_io_channel *raid_ch = ctx_buf;
71 	uint8_t i;
72 	int ret = 0;
73 
74 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_create_cb, %p\n", raid_ch);
75 
76 	assert(raid_bdev != NULL);
77 	assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE);
78 
79 	raid_ch->num_channels = raid_bdev->num_base_bdevs;
80 
81 	raid_ch->base_channel = calloc(raid_ch->num_channels,
82 				       sizeof(struct spdk_io_channel *));
83 	if (!raid_ch->base_channel) {
84 		SPDK_ERRLOG("Unable to allocate base bdevs io channel\n");
85 		return -ENOMEM;
86 	}
87 	for (i = 0; i < raid_ch->num_channels; i++) {
88 		/*
89 		 * Get the spdk_io_channel for all the base bdevs. This is used during
90 		 * split logic to send the respective child bdev ios to respective base
91 		 * bdev io channel.
92 		 */
93 		raid_ch->base_channel[i] = spdk_bdev_get_io_channel(
94 						   raid_bdev->base_bdev_info[i].desc);
95 		if (!raid_ch->base_channel[i]) {
96 			SPDK_ERRLOG("Unable to create io channel for base bdev\n");
97 			ret = -ENOMEM;
98 			break;
99 		}
100 	}
101 
102 	if (!ret && raid_bdev->module->get_io_channel) {
103 		raid_ch->module_channel = raid_bdev->module->get_io_channel(raid_bdev);
104 		if (!raid_ch->module_channel) {
105 			SPDK_ERRLOG("Unable to create io channel for raid module\n");
106 			ret = -ENOMEM;
107 		}
108 	}
109 
110 	if (ret) {
111 		uint8_t j;
112 
113 		for (j = 0; j < i; j++) {
114 			spdk_put_io_channel(raid_ch->base_channel[j]);
115 		}
116 		free(raid_ch->base_channel);
117 		raid_ch->base_channel = NULL;
118 	}
119 	return ret;
120 }
121 
122 /*
123  * brief:
124  * raid_bdev_destroy_cb function is a cb function for raid bdev which deletes the
125  * hierarchy from raid bdev to base bdev io channels. It will be called per core
126  * params:
127  * io_device - pointer to raid bdev io device represented by raid_bdev
128  * ctx_buf - pointer to context buffer for raid bdev io channel
129  * returns:
130  * none
131  */
132 static void
133 raid_bdev_destroy_cb(void *io_device, void *ctx_buf)
134 {
135 	struct raid_bdev_io_channel *raid_ch = ctx_buf;
136 	uint8_t i;
137 
138 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_destroy_cb\n");
139 
140 	assert(raid_ch != NULL);
141 	assert(raid_ch->base_channel);
142 
143 	if (raid_ch->module_channel) {
144 		spdk_put_io_channel(raid_ch->module_channel);
145 	}
146 
147 	for (i = 0; i < raid_ch->num_channels; i++) {
148 		/* Free base bdev channels */
149 		assert(raid_ch->base_channel[i] != NULL);
150 		spdk_put_io_channel(raid_ch->base_channel[i]);
151 	}
152 	free(raid_ch->base_channel);
153 	raid_ch->base_channel = NULL;
154 }
155 
156 /*
157  * brief:
158  * raid_bdev_cleanup is used to cleanup raid_bdev related data
159  * structures.
160  * params:
161  * raid_bdev - pointer to raid_bdev
162  * returns:
163  * none
164  */
165 static void
166 raid_bdev_cleanup(struct raid_bdev *raid_bdev)
167 {
168 	struct raid_base_bdev_info *base_info;
169 
170 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_cleanup, %p name %s, state %s\n",
171 		      raid_bdev, raid_bdev->bdev.name, raid_bdev_state_to_str(raid_bdev->state));
172 	assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE);
173 
174 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
175 		assert(base_info->bdev == NULL);
176 		assert(base_info->desc == NULL);
177 		free(base_info->name);
178 	}
179 
180 	TAILQ_REMOVE(&g_raid_bdev_list, raid_bdev, global_link);
181 	free(raid_bdev->base_bdev_info);
182 }
183 
184 static void
185 raid_bdev_free(struct raid_bdev *raid_bdev)
186 {
187 	free(raid_bdev->bdev.name);
188 	free(raid_bdev);
189 }
190 
191 static void
192 raid_bdev_cleanup_and_free(struct raid_bdev *raid_bdev)
193 {
194 	raid_bdev_cleanup(raid_bdev);
195 	raid_bdev_free(raid_bdev);
196 }
197 
198 /*
199  * brief:
200  * wrapper for the bdev close operation
201  * params:
202  * base_info - raid base bdev info
203  * returns:
204  */
205 static void
206 _raid_bdev_free_base_bdev_resource(void *ctx)
207 {
208 	struct spdk_bdev_desc *desc = ctx;
209 
210 	spdk_bdev_close(desc);
211 }
212 
213 
214 /*
215  * brief:
216  * free resource of base bdev for raid bdev
217  * params:
218  * raid_bdev - pointer to raid bdev
219  * base_info - raid base bdev info
220  * returns:
221  * 0 - success
222  * non zero - failure
223  */
224 static void
225 raid_bdev_free_base_bdev_resource(struct raid_bdev *raid_bdev,
226 				  struct raid_base_bdev_info *base_info)
227 {
228 	free(base_info->name);
229 	base_info->name = NULL;
230 
231 	if (base_info->bdev == NULL) {
232 		return;
233 	}
234 
235 	assert(base_info->desc);
236 	spdk_bdev_module_release_bdev(base_info->bdev);
237 	if (base_info->thread && base_info->thread != spdk_get_thread()) {
238 		spdk_thread_send_msg(base_info->thread, _raid_bdev_free_base_bdev_resource, base_info->desc);
239 	} else {
240 		spdk_bdev_close(base_info->desc);
241 	}
242 	base_info->desc = NULL;
243 	base_info->bdev = NULL;
244 
245 	assert(raid_bdev->num_base_bdevs_discovered);
246 	raid_bdev->num_base_bdevs_discovered--;
247 }
248 
249 static void
250 raid_bdev_io_device_unregister_cb(void *io_device)
251 {
252 	struct raid_bdev *raid_bdev = io_device;
253 
254 	if (raid_bdev->num_base_bdevs_discovered == 0) {
255 		/* Free raid_bdev when there are no base bdevs left */
256 		SPDK_DEBUGLOG(bdev_raid, "raid bdev base bdevs is 0, going to free all in destruct\n");
257 		raid_bdev_cleanup(raid_bdev);
258 		spdk_bdev_destruct_done(&raid_bdev->bdev, 0);
259 		raid_bdev_free(raid_bdev);
260 	} else {
261 		spdk_bdev_destruct_done(&raid_bdev->bdev, 0);
262 	}
263 }
264 
265 void
266 raid_bdev_module_stop_done(struct raid_bdev *raid_bdev)
267 {
268 	if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING) {
269 		spdk_io_device_unregister(raid_bdev, raid_bdev_io_device_unregister_cb);
270 	}
271 }
272 
273 /*
274  * brief:
275  * raid_bdev_destruct is the destruct function table pointer for raid bdev
276  * params:
277  * ctxt - pointer to raid_bdev
278  * returns:
279  * 1 - success (deferred completion)
280  */
281 static int
282 raid_bdev_destruct(void *ctxt)
283 {
284 	struct raid_bdev *raid_bdev = ctxt;
285 	struct raid_base_bdev_info *base_info;
286 
287 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_destruct\n");
288 
289 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
290 		/*
291 		 * Close all base bdev descriptors for which call has come from below
292 		 * layers.  Also close the descriptors if we have started shutdown.
293 		 */
294 		if (g_shutdown_started || base_info->remove_scheduled == true) {
295 			raid_bdev_free_base_bdev_resource(raid_bdev, base_info);
296 		}
297 	}
298 
299 	if (g_shutdown_started) {
300 		raid_bdev->state = RAID_BDEV_STATE_OFFLINE;
301 	}
302 
303 	if (raid_bdev->module->stop != NULL) {
304 		if (raid_bdev->module->stop(raid_bdev) == false) {
305 			return 1;
306 		}
307 	}
308 
309 	raid_bdev_module_stop_done(raid_bdev);
310 
311 	return 1;
312 }
313 
314 void
315 raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status)
316 {
317 	struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io);
318 
319 	spdk_bdev_io_complete(bdev_io, status);
320 }
321 
322 /*
323  * brief:
324  * raid_bdev_io_complete_part - signal the completion of a part of the expected
325  * base bdev IOs and complete the raid_io if this is the final expected IO.
326  * The caller should first set raid_io->base_bdev_io_remaining. This function
327  * will decrement this counter by the value of the 'completed' parameter and
328  * complete the raid_io if the counter reaches 0. The caller is free to
329  * interpret the 'base_bdev_io_remaining' and 'completed' values as needed,
330  * it can represent e.g. blocks or IOs.
331  * params:
332  * raid_io - pointer to raid_bdev_io
333  * completed - the part of the raid_io that has been completed
334  * status - status of the base IO
335  * returns:
336  * true - if the raid_io is completed
337  * false - otherwise
338  */
339 bool
340 raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
341 			   enum spdk_bdev_io_status status)
342 {
343 	assert(raid_io->base_bdev_io_remaining >= completed);
344 	raid_io->base_bdev_io_remaining -= completed;
345 
346 	if (status != SPDK_BDEV_IO_STATUS_SUCCESS) {
347 		raid_io->base_bdev_io_status = status;
348 	}
349 
350 	if (raid_io->base_bdev_io_remaining == 0) {
351 		raid_bdev_io_complete(raid_io, raid_io->base_bdev_io_status);
352 		return true;
353 	} else {
354 		return false;
355 	}
356 }
357 
358 /*
359  * brief:
360  * raid_bdev_queue_io_wait function processes the IO which failed to submit.
361  * It will try to queue the IOs after storing the context to bdev wait queue logic.
362  * params:
363  * raid_io - pointer to raid_bdev_io
364  * bdev - the block device that the IO is submitted to
365  * ch - io channel
366  * cb_fn - callback when the spdk_bdev_io for bdev becomes available
367  * returns:
368  * none
369  */
370 void
371 raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
372 			struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn)
373 {
374 	raid_io->waitq_entry.bdev = bdev;
375 	raid_io->waitq_entry.cb_fn = cb_fn;
376 	raid_io->waitq_entry.cb_arg = raid_io;
377 	spdk_bdev_queue_io_wait(bdev, ch, &raid_io->waitq_entry);
378 }
379 
380 static void
381 raid_base_bdev_reset_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
382 {
383 	struct raid_bdev_io *raid_io = cb_arg;
384 
385 	spdk_bdev_free_io(bdev_io);
386 
387 	raid_bdev_io_complete_part(raid_io, 1, success ?
388 				   SPDK_BDEV_IO_STATUS_SUCCESS :
389 				   SPDK_BDEV_IO_STATUS_FAILED);
390 }
391 
392 static void raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io);
393 
394 static void
395 _raid_bdev_submit_reset_request(void *_raid_io)
396 {
397 	struct raid_bdev_io *raid_io = _raid_io;
398 
399 	raid_bdev_submit_reset_request(raid_io);
400 }
401 
402 /*
403  * brief:
404  * raid_bdev_submit_reset_request function submits reset requests
405  * to member disks; it will submit as many as possible unless a reset fails with -ENOMEM, in
406  * which case it will queue it for later submission
407  * params:
408  * raid_io
409  * returns:
410  * none
411  */
412 static void
413 raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io)
414 {
415 	struct raid_bdev		*raid_bdev;
416 	int				ret;
417 	uint8_t				i;
418 	struct raid_base_bdev_info	*base_info;
419 	struct spdk_io_channel		*base_ch;
420 
421 	raid_bdev = raid_io->raid_bdev;
422 
423 	if (raid_io->base_bdev_io_remaining == 0) {
424 		raid_io->base_bdev_io_remaining = raid_bdev->num_base_bdevs;
425 	}
426 
427 	while (raid_io->base_bdev_io_submitted < raid_bdev->num_base_bdevs) {
428 		i = raid_io->base_bdev_io_submitted;
429 		base_info = &raid_bdev->base_bdev_info[i];
430 		base_ch = raid_io->raid_ch->base_channel[i];
431 		ret = spdk_bdev_reset(base_info->desc, base_ch,
432 				      raid_base_bdev_reset_complete, raid_io);
433 		if (ret == 0) {
434 			raid_io->base_bdev_io_submitted++;
435 		} else if (ret == -ENOMEM) {
436 			raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch,
437 						_raid_bdev_submit_reset_request);
438 			return;
439 		} else {
440 			SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n");
441 			assert(false);
442 			raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
443 			return;
444 		}
445 	}
446 }
447 
448 /*
449  * brief:
450  * Callback function to spdk_bdev_io_get_buf.
451  * params:
452  * ch - pointer to raid bdev io channel
453  * bdev_io - pointer to parent bdev_io on raid bdev device
454  * success - True if buffer is allocated or false otherwise.
455  * returns:
456  * none
457  */
458 static void
459 raid_bdev_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
460 		     bool success)
461 {
462 	struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx;
463 
464 	if (!success) {
465 		raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
466 		return;
467 	}
468 
469 	raid_io->raid_bdev->module->submit_rw_request(raid_io);
470 }
471 
472 /*
473  * brief:
474  * raid_bdev_submit_request function is the submit_request function pointer of
475  * raid bdev function table. This is used to submit the io on raid_bdev to below
476  * layers.
477  * params:
478  * ch - pointer to raid bdev io channel
479  * bdev_io - pointer to parent bdev_io on raid bdev device
480  * returns:
481  * none
482  */
483 static void
484 raid_bdev_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
485 {
486 	struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx;
487 
488 	raid_io->raid_bdev = bdev_io->bdev->ctxt;
489 	raid_io->raid_ch = spdk_io_channel_get_ctx(ch);
490 	raid_io->base_bdev_io_remaining = 0;
491 	raid_io->base_bdev_io_submitted = 0;
492 	raid_io->base_bdev_io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
493 
494 	switch (bdev_io->type) {
495 	case SPDK_BDEV_IO_TYPE_READ:
496 		spdk_bdev_io_get_buf(bdev_io, raid_bdev_get_buf_cb,
497 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
498 		break;
499 	case SPDK_BDEV_IO_TYPE_WRITE:
500 		raid_io->raid_bdev->module->submit_rw_request(raid_io);
501 		break;
502 
503 	case SPDK_BDEV_IO_TYPE_RESET:
504 		raid_bdev_submit_reset_request(raid_io);
505 		break;
506 
507 	case SPDK_BDEV_IO_TYPE_FLUSH:
508 	case SPDK_BDEV_IO_TYPE_UNMAP:
509 		raid_io->raid_bdev->module->submit_null_payload_request(raid_io);
510 		break;
511 
512 	default:
513 		SPDK_ERRLOG("submit request, invalid io type %u\n", bdev_io->type);
514 		raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED);
515 		break;
516 	}
517 }
518 
519 /*
520  * brief:
521  * _raid_bdev_io_type_supported checks whether io_type is supported in
522  * all base bdev modules of raid bdev module. If anyone among the base_bdevs
523  * doesn't support, the raid device doesn't supports.
524  *
525  * params:
526  * raid_bdev - pointer to raid bdev context
527  * io_type - io type
528  * returns:
529  * true - io_type is supported
530  * false - io_type is not supported
531  */
532 inline static bool
533 _raid_bdev_io_type_supported(struct raid_bdev *raid_bdev, enum spdk_bdev_io_type io_type)
534 {
535 	struct raid_base_bdev_info *base_info;
536 
537 	if (io_type == SPDK_BDEV_IO_TYPE_FLUSH ||
538 	    io_type == SPDK_BDEV_IO_TYPE_UNMAP) {
539 		if (raid_bdev->module->submit_null_payload_request == NULL) {
540 			return false;
541 		}
542 	}
543 
544 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
545 		if (base_info->bdev == NULL) {
546 			assert(false);
547 			continue;
548 		}
549 
550 		if (spdk_bdev_io_type_supported(base_info->bdev, io_type) == false) {
551 			return false;
552 		}
553 	}
554 
555 	return true;
556 }
557 
558 /*
559  * brief:
560  * raid_bdev_io_type_supported is the io_supported function for bdev function
561  * table which returns whether the particular io type is supported or not by
562  * raid bdev module
563  * params:
564  * ctx - pointer to raid bdev context
565  * type - io type
566  * returns:
567  * true - io_type is supported
568  * false - io_type is not supported
569  */
570 static bool
571 raid_bdev_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
572 {
573 	switch (io_type) {
574 	case SPDK_BDEV_IO_TYPE_READ:
575 	case SPDK_BDEV_IO_TYPE_WRITE:
576 		return true;
577 
578 	case SPDK_BDEV_IO_TYPE_FLUSH:
579 	case SPDK_BDEV_IO_TYPE_RESET:
580 	case SPDK_BDEV_IO_TYPE_UNMAP:
581 		return _raid_bdev_io_type_supported(ctx, io_type);
582 
583 	default:
584 		return false;
585 	}
586 
587 	return false;
588 }
589 
590 /*
591  * brief:
592  * raid_bdev_get_io_channel is the get_io_channel function table pointer for
593  * raid bdev. This is used to return the io channel for this raid bdev
594  * params:
595  * ctxt - pointer to raid_bdev
596  * returns:
597  * pointer to io channel for raid bdev
598  */
599 static struct spdk_io_channel *
600 raid_bdev_get_io_channel(void *ctxt)
601 {
602 	struct raid_bdev *raid_bdev = ctxt;
603 
604 	return spdk_get_io_channel(raid_bdev);
605 }
606 
607 /*
608  * brief:
609  * raid_bdev_dump_info_json is the function table pointer for raid bdev
610  * params:
611  * ctx - pointer to raid_bdev
612  * w - pointer to json context
613  * returns:
614  * 0 - success
615  * non zero - failure
616  */
617 static int
618 raid_bdev_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
619 {
620 	struct raid_bdev *raid_bdev = ctx;
621 	struct raid_base_bdev_info *base_info;
622 
623 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_dump_config_json\n");
624 	assert(raid_bdev != NULL);
625 
626 	/* Dump the raid bdev configuration related information */
627 	spdk_json_write_named_object_begin(w, "raid");
628 	spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb);
629 	spdk_json_write_named_string(w, "state", raid_bdev_state_to_str(raid_bdev->state));
630 	spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
631 	spdk_json_write_named_uint32(w, "num_base_bdevs", raid_bdev->num_base_bdevs);
632 	spdk_json_write_named_uint32(w, "num_base_bdevs_discovered", raid_bdev->num_base_bdevs_discovered);
633 	spdk_json_write_name(w, "base_bdevs_list");
634 	spdk_json_write_array_begin(w);
635 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
636 		if (base_info->bdev) {
637 			spdk_json_write_string(w, base_info->bdev->name);
638 		} else {
639 			spdk_json_write_null(w);
640 		}
641 	}
642 	spdk_json_write_array_end(w);
643 	spdk_json_write_object_end(w);
644 
645 	return 0;
646 }
647 
648 /*
649  * brief:
650  * raid_bdev_write_config_json is the function table pointer for raid bdev
651  * params:
652  * bdev - pointer to spdk_bdev
653  * w - pointer to json context
654  * returns:
655  * none
656  */
657 static void
658 raid_bdev_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
659 {
660 	struct raid_bdev *raid_bdev = bdev->ctxt;
661 	struct raid_base_bdev_info *base_info;
662 
663 	spdk_json_write_object_begin(w);
664 
665 	spdk_json_write_named_string(w, "method", "bdev_raid_create");
666 
667 	spdk_json_write_named_object_begin(w, "params");
668 	spdk_json_write_named_string(w, "name", bdev->name);
669 	spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb);
670 	spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level));
671 
672 	spdk_json_write_named_array_begin(w, "base_bdevs");
673 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
674 		if (base_info->bdev) {
675 			spdk_json_write_string(w, base_info->bdev->name);
676 		}
677 	}
678 	spdk_json_write_array_end(w);
679 	spdk_json_write_object_end(w);
680 
681 	spdk_json_write_object_end(w);
682 }
683 
684 static int
685 raid_bdev_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size)
686 {
687 	struct raid_bdev *raid_bdev = ctx;
688 	struct spdk_bdev *base_bdev;
689 	uint32_t i;
690 	int domains_count = 0, rc;
691 
692 	/* First loop to get the number of memory domains */
693 	for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
694 		base_bdev = raid_bdev->base_bdev_info[i].bdev;
695 		rc = spdk_bdev_get_memory_domains(base_bdev, NULL, 0);
696 		if (rc < 0) {
697 			return rc;
698 		}
699 		domains_count += rc;
700 	}
701 
702 	if (!domains || array_size < domains_count) {
703 		return domains_count;
704 	}
705 
706 	for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
707 		base_bdev = raid_bdev->base_bdev_info[i].bdev;
708 		rc = spdk_bdev_get_memory_domains(base_bdev, domains, array_size);
709 		if (rc < 0) {
710 			return rc;
711 		}
712 		domains += rc;
713 		array_size -= rc;
714 	}
715 
716 	return domains_count;
717 }
718 
719 /* g_raid_bdev_fn_table is the function table for raid bdev */
720 static const struct spdk_bdev_fn_table g_raid_bdev_fn_table = {
721 	.destruct		= raid_bdev_destruct,
722 	.submit_request		= raid_bdev_submit_request,
723 	.io_type_supported	= raid_bdev_io_type_supported,
724 	.get_io_channel		= raid_bdev_get_io_channel,
725 	.dump_info_json		= raid_bdev_dump_info_json,
726 	.write_config_json	= raid_bdev_write_config_json,
727 	.get_memory_domains	= raid_bdev_get_memory_domains,
728 };
729 
730 struct raid_bdev *
731 raid_bdev_find_by_name(const char *name)
732 {
733 	struct raid_bdev *raid_bdev;
734 
735 	TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
736 		if (strcmp(raid_bdev->bdev.name, name) == 0) {
737 			return raid_bdev;
738 		}
739 	}
740 
741 	return NULL;
742 }
743 
744 static struct {
745 	const char *name;
746 	enum raid_level value;
747 } g_raid_level_names[] = {
748 	{ "raid0", RAID0 },
749 	{ "0", RAID0 },
750 	{ "raid5f", RAID5F },
751 	{ "5f", RAID5F },
752 	{ "concat", CONCAT },
753 	{ }
754 };
755 
756 static struct {
757 	const char *name;
758 	enum raid_bdev_state value;
759 } g_raid_state_names[] = {
760 	{ "online", RAID_BDEV_STATE_ONLINE },
761 	{ "configuring", RAID_BDEV_STATE_CONFIGURING },
762 	{ "offline", RAID_BDEV_STATE_OFFLINE },
763 	{ }
764 };
765 
766 /* We have to use the typedef in the function declaration to appease astyle. */
767 typedef enum raid_level raid_level_t;
768 typedef enum raid_bdev_state raid_bdev_state_t;
769 
770 raid_level_t
771 raid_bdev_str_to_level(const char *str)
772 {
773 	unsigned int i;
774 
775 	assert(str != NULL);
776 
777 	for (i = 0; g_raid_level_names[i].name != NULL; i++) {
778 		if (strcasecmp(g_raid_level_names[i].name, str) == 0) {
779 			return g_raid_level_names[i].value;
780 		}
781 	}
782 
783 	return INVALID_RAID_LEVEL;
784 }
785 
786 const char *
787 raid_bdev_level_to_str(enum raid_level level)
788 {
789 	unsigned int i;
790 
791 	for (i = 0; g_raid_level_names[i].name != NULL; i++) {
792 		if (g_raid_level_names[i].value == level) {
793 			return g_raid_level_names[i].name;
794 		}
795 	}
796 
797 	return "";
798 }
799 
800 raid_bdev_state_t
801 raid_bdev_str_to_state(const char *str)
802 {
803 	unsigned int i;
804 
805 	assert(str != NULL);
806 
807 	for (i = 0; g_raid_state_names[i].name != NULL; i++) {
808 		if (strcasecmp(g_raid_state_names[i].name, str) == 0) {
809 			return g_raid_state_names[i].value;
810 		}
811 	}
812 
813 	return RAID_BDEV_STATE_MAX;
814 }
815 
816 const char *
817 raid_bdev_state_to_str(enum raid_bdev_state state)
818 {
819 	unsigned int i;
820 
821 	for (i = 0; g_raid_state_names[i].name != NULL; i++) {
822 		if (g_raid_state_names[i].value == state) {
823 			return g_raid_state_names[i].name;
824 		}
825 	}
826 
827 	assert(false);
828 	return "";
829 }
830 
831 /*
832  * brief:
833  * raid_bdev_fini_start is called when bdev layer is starting the
834  * shutdown process
835  * params:
836  * none
837  * returns:
838  * none
839  */
840 static void
841 raid_bdev_fini_start(void)
842 {
843 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_fini_start\n");
844 	g_shutdown_started = true;
845 }
846 
847 /*
848  * brief:
849  * raid_bdev_exit is called on raid bdev module exit time by bdev layer
850  * params:
851  * none
852  * returns:
853  * none
854  */
855 static void
856 raid_bdev_exit(void)
857 {
858 	struct raid_bdev *raid_bdev, *tmp;
859 
860 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_exit\n");
861 
862 	TAILQ_FOREACH_SAFE(raid_bdev, &g_raid_bdev_list, global_link, tmp) {
863 		raid_bdev_cleanup_and_free(raid_bdev);
864 	}
865 }
866 
867 /*
868  * brief:
869  * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid
870  * module
871  * params:
872  * none
873  * returns:
874  * size of spdk_bdev_io context for raid
875  */
876 static int
877 raid_bdev_get_ctx_size(void)
878 {
879 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_get_ctx_size\n");
880 	return sizeof(struct raid_bdev_io);
881 }
882 
883 static struct spdk_bdev_module g_raid_if = {
884 	.name = "raid",
885 	.module_init = raid_bdev_init,
886 	.fini_start = raid_bdev_fini_start,
887 	.module_fini = raid_bdev_exit,
888 	.get_ctx_size = raid_bdev_get_ctx_size,
889 	.examine_config = raid_bdev_examine,
890 	.async_init = false,
891 	.async_fini = false,
892 };
893 SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if)
894 
895 /*
896  * brief:
897  * raid_bdev_init is the initialization function for raid bdev module
898  * params:
899  * none
900  * returns:
901  * 0 - success
902  * non zero - failure
903  */
904 static int
905 raid_bdev_init(void)
906 {
907 	return 0;
908 }
909 
910 /*
911  * brief:
912  * raid_bdev_create allocates raid bdev based on passed configuration
913  * params:
914  * name - name for raid bdev
915  * strip_size - strip size in KB
916  * num_base_bdevs - number of base bdevs
917  * level - raid level
918  * raid_bdev_out - the created raid bdev
919  * returns:
920  * 0 - success
921  * non zero - failure
922  */
923 int
924 raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs,
925 		 enum raid_level level, struct raid_bdev **raid_bdev_out)
926 {
927 	struct raid_bdev *raid_bdev;
928 	struct spdk_bdev *raid_bdev_gen;
929 	struct raid_bdev_module *module;
930 
931 	if (raid_bdev_find_by_name(name) != NULL) {
932 		SPDK_ERRLOG("Duplicate raid bdev name found: %s\n", name);
933 		return -EEXIST;
934 	}
935 
936 	if (spdk_u32_is_pow2(strip_size) == false) {
937 		SPDK_ERRLOG("Invalid strip size %" PRIu32 "\n", strip_size);
938 		return -EINVAL;
939 	}
940 
941 	module = raid_bdev_module_find(level);
942 	if (module == NULL) {
943 		SPDK_ERRLOG("Unsupported raid level '%d'\n", level);
944 		return -EINVAL;
945 	}
946 
947 	assert(module->base_bdevs_min != 0);
948 	if (num_base_bdevs < module->base_bdevs_min) {
949 		SPDK_ERRLOG("At least %u base devices required for %s\n",
950 			    module->base_bdevs_min,
951 			    raid_bdev_level_to_str(level));
952 		return -EINVAL;
953 	}
954 
955 	raid_bdev = calloc(1, sizeof(*raid_bdev));
956 	if (!raid_bdev) {
957 		SPDK_ERRLOG("Unable to allocate memory for raid bdev\n");
958 		return -ENOMEM;
959 	}
960 
961 	raid_bdev->module = module;
962 	raid_bdev->num_base_bdevs = num_base_bdevs;
963 	raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs,
964 					   sizeof(struct raid_base_bdev_info));
965 	if (!raid_bdev->base_bdev_info) {
966 		SPDK_ERRLOG("Unable able to allocate base bdev info\n");
967 		free(raid_bdev);
968 		return -ENOMEM;
969 	}
970 
971 	/* strip_size_kb is from the rpc param.  strip_size is in blocks and used
972 	 * internally and set later.
973 	 */
974 	raid_bdev->strip_size = 0;
975 	raid_bdev->strip_size_kb = strip_size;
976 	raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
977 	raid_bdev->level = level;
978 
979 	raid_bdev_gen = &raid_bdev->bdev;
980 
981 	raid_bdev_gen->name = strdup(name);
982 	if (!raid_bdev_gen->name) {
983 		SPDK_ERRLOG("Unable to allocate name for raid\n");
984 		free(raid_bdev->base_bdev_info);
985 		free(raid_bdev);
986 		return -ENOMEM;
987 	}
988 
989 	raid_bdev_gen->product_name = "Raid Volume";
990 	raid_bdev_gen->ctxt = raid_bdev;
991 	raid_bdev_gen->fn_table = &g_raid_bdev_fn_table;
992 	raid_bdev_gen->module = &g_raid_if;
993 	raid_bdev_gen->write_cache = 0;
994 
995 	TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link);
996 
997 	*raid_bdev_out = raid_bdev;
998 
999 	return 0;
1000 }
1001 
1002 /*
1003  * brief:
1004  * If raid bdev config is complete, then only register the raid bdev to
1005  * bdev layer and remove this raid bdev from configuring list and
1006  * insert the raid bdev to configured list
1007  * params:
1008  * raid_bdev - pointer to raid bdev
1009  * returns:
1010  * 0 - success
1011  * non zero - failure
1012  */
1013 static int
1014 raid_bdev_configure(struct raid_bdev *raid_bdev)
1015 {
1016 	uint32_t blocklen = 0;
1017 	struct spdk_bdev *raid_bdev_gen;
1018 	struct raid_base_bdev_info *base_info;
1019 	int rc = 0;
1020 
1021 	assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING);
1022 	assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs);
1023 
1024 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1025 		assert(base_info->bdev != NULL);
1026 		/* Check blocklen for all base bdevs that it should be same */
1027 		if (blocklen == 0) {
1028 			blocklen = base_info->bdev->blocklen;
1029 		} else if (blocklen != base_info->bdev->blocklen) {
1030 			/*
1031 			 * Assumption is that all the base bdevs for any raid bdev should
1032 			 * have same blocklen
1033 			 */
1034 			SPDK_ERRLOG("Blocklen of various bdevs not matching\n");
1035 			return -EINVAL;
1036 		}
1037 	}
1038 	assert(blocklen > 0);
1039 
1040 	/* The strip_size_kb is read in from user in KB. Convert to blocks here for
1041 	 * internal use.
1042 	 */
1043 	raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / blocklen;
1044 	raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size);
1045 	raid_bdev->blocklen_shift = spdk_u32log2(blocklen);
1046 
1047 	raid_bdev_gen = &raid_bdev->bdev;
1048 	raid_bdev_gen->blocklen = blocklen;
1049 
1050 	rc = raid_bdev->module->start(raid_bdev);
1051 	if (rc != 0) {
1052 		SPDK_ERRLOG("raid module startup callback failed\n");
1053 		return rc;
1054 	}
1055 	raid_bdev->state = RAID_BDEV_STATE_ONLINE;
1056 	SPDK_DEBUGLOG(bdev_raid, "io device register %p\n", raid_bdev);
1057 	SPDK_DEBUGLOG(bdev_raid, "blockcnt %" PRIu64 ", blocklen %u\n",
1058 		      raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen);
1059 	spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb,
1060 				sizeof(struct raid_bdev_io_channel),
1061 				raid_bdev->bdev.name);
1062 	rc = spdk_bdev_register(raid_bdev_gen);
1063 	if (rc != 0) {
1064 		SPDK_ERRLOG("Unable to register raid bdev and stay at configuring state\n");
1065 		if (raid_bdev->module->stop != NULL) {
1066 			raid_bdev->module->stop(raid_bdev);
1067 		}
1068 		spdk_io_device_unregister(raid_bdev, NULL);
1069 		raid_bdev->state = RAID_BDEV_STATE_CONFIGURING;
1070 		return rc;
1071 	}
1072 	SPDK_DEBUGLOG(bdev_raid, "raid bdev generic %p\n", raid_bdev_gen);
1073 	SPDK_DEBUGLOG(bdev_raid, "raid bdev is created with name %s, raid_bdev %p\n",
1074 		      raid_bdev_gen->name, raid_bdev);
1075 
1076 	return 0;
1077 }
1078 
1079 /*
1080  * brief:
1081  * If raid bdev is online and registered, change the bdev state to
1082  * configuring and unregister this raid device. Queue this raid device
1083  * in configuring list
1084  * params:
1085  * raid_bdev - pointer to raid bdev
1086  * cb_fn - callback function
1087  * cb_arg - argument to callback function
1088  * returns:
1089  * none
1090  */
1091 static void
1092 raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn,
1093 		      void *cb_arg)
1094 {
1095 	if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) {
1096 		if (cb_fn) {
1097 			cb_fn(cb_arg, 0);
1098 		}
1099 		return;
1100 	}
1101 
1102 	assert(raid_bdev->num_base_bdevs == raid_bdev->num_base_bdevs_discovered);
1103 	raid_bdev->state = RAID_BDEV_STATE_OFFLINE;
1104 	assert(raid_bdev->num_base_bdevs_discovered);
1105 	SPDK_DEBUGLOG(bdev_raid, "raid bdev state changing from online to offline\n");
1106 
1107 	spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg);
1108 }
1109 
1110 /*
1111  * brief:
1112  * raid_bdev_find_by_base_bdev function finds the raid bdev which has
1113  *  claimed the base bdev.
1114  * params:
1115  * base_bdev - pointer to base bdev pointer
1116  * _raid_bdev - Reference to pointer to raid bdev
1117  * _base_info - Reference to the raid base bdev info.
1118  * returns:
1119  * true - if the raid bdev is found.
1120  * false - if the raid bdev is not found.
1121  */
1122 static bool
1123 raid_bdev_find_by_base_bdev(struct spdk_bdev *base_bdev, struct raid_bdev **_raid_bdev,
1124 			    struct raid_base_bdev_info **_base_info)
1125 {
1126 	struct raid_bdev *raid_bdev;
1127 	struct raid_base_bdev_info *base_info;
1128 
1129 	TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
1130 		RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1131 			if (base_info->bdev == base_bdev) {
1132 				*_raid_bdev = raid_bdev;
1133 				*_base_info = base_info;
1134 				return true;
1135 			}
1136 		}
1137 	}
1138 
1139 	return false;
1140 }
1141 
1142 /*
1143  * brief:
1144  * raid_bdev_remove_base_bdev function is called by below layers when base_bdev
1145  * is removed. This function checks if this base bdev is part of any raid bdev
1146  * or not. If yes, it takes necessary action on that particular raid bdev.
1147  * params:
1148  * base_bdev - pointer to base bdev pointer which got removed
1149  * returns:
1150  * none
1151  */
1152 static void
1153 raid_bdev_remove_base_bdev(struct spdk_bdev *base_bdev)
1154 {
1155 	struct raid_bdev	*raid_bdev = NULL;
1156 	struct raid_base_bdev_info *base_info;
1157 
1158 	SPDK_DEBUGLOG(bdev_raid, "raid_bdev_remove_base_bdev\n");
1159 
1160 	/* Find the raid_bdev which has claimed this base_bdev */
1161 	if (!raid_bdev_find_by_base_bdev(base_bdev, &raid_bdev, &base_info)) {
1162 		SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name);
1163 		return;
1164 	}
1165 
1166 	assert(base_info->desc);
1167 	base_info->remove_scheduled = true;
1168 
1169 	if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) {
1170 		/*
1171 		 * As raid bdev is not registered yet or already unregistered,
1172 		 * so cleanup should be done here itself.
1173 		 */
1174 		raid_bdev_free_base_bdev_resource(raid_bdev, base_info);
1175 		if (raid_bdev->num_base_bdevs_discovered == 0) {
1176 			/* There is no base bdev for this raid, so free the raid device. */
1177 			raid_bdev_cleanup_and_free(raid_bdev);
1178 			return;
1179 		}
1180 	}
1181 
1182 	raid_bdev_deconfigure(raid_bdev, NULL, NULL);
1183 }
1184 
1185 /*
1186  * brief:
1187  * raid_bdev_event_base_bdev function is called by below layers when base_bdev
1188  * triggers asynchronous event.
1189  * params:
1190  * type - event details.
1191  * bdev - bdev that triggered event.
1192  * event_ctx - context for event.
1193  * returns:
1194  * none
1195  */
1196 static void
1197 raid_bdev_event_base_bdev(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
1198 			  void *event_ctx)
1199 {
1200 	switch (type) {
1201 	case SPDK_BDEV_EVENT_REMOVE:
1202 		raid_bdev_remove_base_bdev(bdev);
1203 		break;
1204 	default:
1205 		SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
1206 		break;
1207 	}
1208 }
1209 
1210 /*
1211  * brief:
1212  * Deletes the specified raid bdev
1213  * params:
1214  * raid_bdev - pointer to raid bdev
1215  * cb_fn - callback function
1216  * cb_arg - argument to callback function
1217  */
1218 void
1219 raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_arg)
1220 {
1221 	struct raid_base_bdev_info *base_info;
1222 
1223 	SPDK_DEBUGLOG(bdev_raid, "delete raid bdev: %s\n", raid_bdev->bdev.name);
1224 
1225 	if (raid_bdev->destroy_started) {
1226 		SPDK_DEBUGLOG(bdev_raid, "destroying raid bdev %s is already started\n",
1227 			      raid_bdev->bdev.name);
1228 		if (cb_fn) {
1229 			cb_fn(cb_arg, -EALREADY);
1230 		}
1231 		return;
1232 	}
1233 
1234 	raid_bdev->destroy_started = true;
1235 
1236 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1237 		base_info->remove_scheduled = true;
1238 
1239 		if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) {
1240 			/*
1241 			 * As raid bdev is not registered yet or already unregistered,
1242 			 * so cleanup should be done here itself.
1243 			 */
1244 			raid_bdev_free_base_bdev_resource(raid_bdev, base_info);
1245 		}
1246 	}
1247 
1248 	if (raid_bdev->num_base_bdevs_discovered == 0) {
1249 		/* There is no base bdev for this raid, so free the raid device. */
1250 		raid_bdev_cleanup_and_free(raid_bdev);
1251 		if (cb_fn) {
1252 			cb_fn(cb_arg, 0);
1253 		}
1254 	} else {
1255 		raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg);
1256 	}
1257 }
1258 
1259 static int
1260 raid_bdev_configure_base_bdev(struct raid_bdev *raid_bdev, struct raid_base_bdev_info *base_info)
1261 {
1262 	struct spdk_bdev_desc *desc;
1263 	struct spdk_bdev *bdev;
1264 	int rc;
1265 
1266 	assert(base_info->name != NULL);
1267 	assert(base_info->bdev == NULL);
1268 
1269 	rc = spdk_bdev_open_ext(base_info->name, true, raid_bdev_event_base_bdev, NULL, &desc);
1270 	if (rc != 0) {
1271 		if (rc != -ENODEV) {
1272 			SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", base_info->name);
1273 		}
1274 		return rc;
1275 	}
1276 
1277 	bdev = spdk_bdev_desc_get_bdev(desc);
1278 
1279 	rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if);
1280 	if (rc != 0) {
1281 		SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n");
1282 		spdk_bdev_close(desc);
1283 		return rc;
1284 	}
1285 
1286 	SPDK_DEBUGLOG(bdev_raid, "bdev %s is claimed\n", bdev->name);
1287 
1288 	assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE);
1289 
1290 	base_info->thread = spdk_get_thread();
1291 	base_info->bdev = bdev;
1292 	base_info->desc = desc;
1293 	raid_bdev->num_base_bdevs_discovered++;
1294 	assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs);
1295 
1296 	if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs) {
1297 		rc = raid_bdev_configure(raid_bdev);
1298 		if (rc != 0) {
1299 			SPDK_ERRLOG("Failed to configure raid bdev\n");
1300 			return rc;
1301 		}
1302 	}
1303 
1304 	return 0;
1305 }
1306 
1307 /*
1308  * brief:
1309  * raid_bdev_add_base_device function is the actual function which either adds
1310  * the nvme base device to existing raid bdev or create a new raid bdev. It also claims
1311  * the base device and keep the open descriptor.
1312  * params:
1313  * raid_bdev - pointer to raid bdev
1314  * name - name of the base bdev
1315  * slot - position to add base bdev
1316  * returns:
1317  * 0 - success
1318  * non zero - failure
1319  */
1320 int
1321 raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot)
1322 {
1323 	struct raid_base_bdev_info *base_info;
1324 	int rc;
1325 
1326 	if (slot >= raid_bdev->num_base_bdevs) {
1327 		return -EINVAL;
1328 	}
1329 
1330 	base_info = &raid_bdev->base_bdev_info[slot];
1331 
1332 	if (base_info->name != NULL) {
1333 		SPDK_ERRLOG("Slot %u on raid bdev '%s' already assigned to bdev '%s'\n",
1334 			    slot, raid_bdev->bdev.name, base_info->name);
1335 		return -EBUSY;
1336 	}
1337 
1338 	base_info->name = strdup(name);
1339 	if (base_info->name == NULL) {
1340 		return -ENOMEM;
1341 	}
1342 
1343 	rc = raid_bdev_configure_base_bdev(raid_bdev, base_info);
1344 	if (rc != 0) {
1345 		if (rc != -ENODEV) {
1346 			SPDK_ERRLOG("Failed to allocate resource for bdev '%s'\n", name);
1347 		}
1348 		return rc;
1349 	}
1350 
1351 	return 0;
1352 }
1353 
1354 /*
1355  * brief:
1356  * raid_bdev_examine function is the examine function call by the below layers
1357  * like bdev_nvme layer. This function will check if this base bdev can be
1358  * claimed by this raid bdev or not.
1359  * params:
1360  * bdev - pointer to base bdev
1361  * returns:
1362  * none
1363  */
1364 static void
1365 raid_bdev_examine(struct spdk_bdev *bdev)
1366 {
1367 	struct raid_bdev *raid_bdev;
1368 	struct raid_base_bdev_info *base_info;
1369 
1370 	TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) {
1371 		RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
1372 			if (base_info->bdev == NULL && strcmp(bdev->name, base_info->name) == 0) {
1373 				raid_bdev_configure_base_bdev(raid_bdev, base_info);
1374 				break;
1375 			}
1376 		}
1377 	}
1378 
1379 	spdk_bdev_module_examine_done(&g_raid_if);
1380 }
1381 
1382 /* Log component for bdev raid bdev module */
1383 SPDK_LOG_REGISTER_COMPONENT(bdev_raid)
1384