diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/spdk/module/bdev/raid | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/spdk/module/bdev/raid')
-rw-r--r-- | src/spdk/module/bdev/raid/Makefile | 51 | ||||
-rw-r--r-- | src/spdk/module/bdev/raid/bdev_raid.c | 1719 | ||||
-rw-r--r-- | src/spdk/module/bdev/raid/bdev_raid.h | 319 | ||||
-rw-r--r-- | src/spdk/module/bdev/raid/bdev_raid_rpc.c | 452 | ||||
-rw-r--r-- | src/spdk/module/bdev/raid/raid0.c | 398 | ||||
-rw-r--r-- | src/spdk/module/bdev/raid/raid5.c | 114 |
6 files changed, 3053 insertions, 0 deletions
diff --git a/src/spdk/module/bdev/raid/Makefile b/src/spdk/module/bdev/raid/Makefile new file mode 100644 index 000000000..452d32e79 --- /dev/null +++ b/src/spdk/module/bdev/raid/Makefile @@ -0,0 +1,51 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../..) +include $(SPDK_ROOT_DIR)/mk/spdk.common.mk + +SO_VER := 2 +SO_MINOR := 0 + +CFLAGS += -I$(SPDK_ROOT_DIR)/lib/bdev/ +C_SRCS = bdev_raid.c bdev_raid_rpc.c raid0.c + +ifeq ($(CONFIG_RAID5),y) +C_SRCS += raid5.c +endif + +LIBNAME = bdev_raid + +SPDK_MAP_FILE = $(SPDK_ROOT_DIR)/mk/spdk_blank.map + +include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/src/spdk/module/bdev/raid/bdev_raid.c b/src/spdk/module/bdev/raid/bdev_raid.c new file mode 100644 index 000000000..10da1a799 --- /dev/null +++ b/src/spdk/module/bdev/raid/bdev_raid.c @@ -0,0 +1,1719 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "bdev_raid.h" +#include "spdk/env.h" +#include "spdk/thread.h" +#include "spdk/conf.h" +#include "spdk_internal/log.h" +#include "spdk/string.h" +#include "spdk/util.h" +#include "spdk/json.h" +#include "spdk/string.h" + +static bool g_shutdown_started = false; + +/* raid bdev config as read from config file */ +struct raid_config g_raid_config = { + .raid_bdev_config_head = TAILQ_HEAD_INITIALIZER(g_raid_config.raid_bdev_config_head), +}; + +/* + * List of raid bdev in configured list, these raid bdevs are registered with + * bdev layer + */ +struct raid_configured_tailq g_raid_bdev_configured_list = TAILQ_HEAD_INITIALIZER( + g_raid_bdev_configured_list); + +/* List of raid bdev in configuring list */ +struct raid_configuring_tailq g_raid_bdev_configuring_list = TAILQ_HEAD_INITIALIZER( + g_raid_bdev_configuring_list); + +/* List of all raid bdevs */ +struct raid_all_tailq g_raid_bdev_list = TAILQ_HEAD_INITIALIZER(g_raid_bdev_list); + +/* List of all raid bdevs that are offline */ +struct raid_offline_tailq g_raid_bdev_offline_list = TAILQ_HEAD_INITIALIZER( + g_raid_bdev_offline_list); + +static TAILQ_HEAD(, raid_bdev_module) g_raid_modules = TAILQ_HEAD_INITIALIZER(g_raid_modules); + +static struct raid_bdev_module *raid_bdev_module_find(enum raid_level level) +{ + struct raid_bdev_module *raid_module; + + TAILQ_FOREACH(raid_module, &g_raid_modules, link) { + if (raid_module->level == level) { + return raid_module; + } + } + + return NULL; +} + +void raid_bdev_module_list_add(struct raid_bdev_module *raid_module) +{ + if (raid_bdev_module_find(raid_module->level) != NULL) { + SPDK_ERRLOG("module for raid level '%s' already registered.\n", + raid_bdev_level_to_str(raid_module->level)); + assert(false); + } else { + TAILQ_INSERT_TAIL(&g_raid_modules, raid_module, link); + } +} + +/* Function declarations */ +static void raid_bdev_examine(struct spdk_bdev *bdev); +static int raid_bdev_init(void); +static void raid_bdev_deconfigure(struct raid_bdev *raid_bdev, + raid_bdev_destruct_cb cb_fn, void *cb_arg); +static void raid_bdev_remove_base_bdev(void *ctx); + +/* + * brief: + * raid_bdev_create_cb function is a cb function for raid bdev which creates the + * hierarchy from raid bdev to base bdev io channels. It will be called per core + * params: + * io_device - pointer to raid bdev io device represented by raid_bdev + * ctx_buf - pointer to context buffer for raid bdev io channel + * returns: + * 0 - success + * non zero - failure + */ +static int +raid_bdev_create_cb(void *io_device, void *ctx_buf) +{ + struct raid_bdev *raid_bdev = io_device; + struct raid_bdev_io_channel *raid_ch = ctx_buf; + uint8_t i; + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_create_cb, %p\n", raid_ch); + + assert(raid_bdev != NULL); + assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE); + + raid_ch->num_channels = raid_bdev->num_base_bdevs; + + raid_ch->base_channel = calloc(raid_ch->num_channels, + sizeof(struct spdk_io_channel *)); + if (!raid_ch->base_channel) { + SPDK_ERRLOG("Unable to allocate base bdevs io channel\n"); + return -ENOMEM; + } + for (i = 0; i < raid_ch->num_channels; i++) { + /* + * Get the spdk_io_channel for all the base bdevs. This is used during + * split logic to send the respective child bdev ios to respective base + * bdev io channel. + */ + raid_ch->base_channel[i] = spdk_bdev_get_io_channel( + raid_bdev->base_bdev_info[i].desc); + if (!raid_ch->base_channel[i]) { + uint8_t j; + + for (j = 0; j < i; j++) { + spdk_put_io_channel(raid_ch->base_channel[j]); + } + free(raid_ch->base_channel); + raid_ch->base_channel = NULL; + SPDK_ERRLOG("Unable to create io channel for base bdev\n"); + return -ENOMEM; + } + } + + return 0; +} + +/* + * brief: + * raid_bdev_destroy_cb function is a cb function for raid bdev which deletes the + * hierarchy from raid bdev to base bdev io channels. It will be called per core + * params: + * io_device - pointer to raid bdev io device represented by raid_bdev + * ctx_buf - pointer to context buffer for raid bdev io channel + * returns: + * none + */ +static void +raid_bdev_destroy_cb(void *io_device, void *ctx_buf) +{ + struct raid_bdev_io_channel *raid_ch = ctx_buf; + uint8_t i; + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_destroy_cb\n"); + + assert(raid_ch != NULL); + assert(raid_ch->base_channel); + for (i = 0; i < raid_ch->num_channels; i++) { + /* Free base bdev channels */ + assert(raid_ch->base_channel[i] != NULL); + spdk_put_io_channel(raid_ch->base_channel[i]); + } + free(raid_ch->base_channel); + raid_ch->base_channel = NULL; +} + +/* + * brief: + * raid_bdev_cleanup is used to cleanup and free raid_bdev related data + * structures. + * params: + * raid_bdev - pointer to raid_bdev + * returns: + * none + */ +static void +raid_bdev_cleanup(struct raid_bdev *raid_bdev) +{ + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_cleanup, %p name %s, state %u, config %p\n", + raid_bdev, + raid_bdev->bdev.name, raid_bdev->state, raid_bdev->config); + if (raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) { + TAILQ_REMOVE(&g_raid_bdev_configuring_list, raid_bdev, state_link); + } else if (raid_bdev->state == RAID_BDEV_STATE_OFFLINE) { + TAILQ_REMOVE(&g_raid_bdev_offline_list, raid_bdev, state_link); + } else { + assert(0); + } + TAILQ_REMOVE(&g_raid_bdev_list, raid_bdev, global_link); + free(raid_bdev->bdev.name); + free(raid_bdev->base_bdev_info); + if (raid_bdev->config) { + raid_bdev->config->raid_bdev = NULL; + } + free(raid_bdev); +} + +/* + * brief: + * wrapper for the bdev close operation + * params: + * base_info - raid base bdev info + * returns: + */ +static void +_raid_bdev_free_base_bdev_resource(void *ctx) +{ + struct spdk_bdev_desc *desc = ctx; + + spdk_bdev_close(desc); +} + + +/* + * brief: + * free resource of base bdev for raid bdev + * params: + * raid_bdev - pointer to raid bdev + * base_info - raid base bdev info + * returns: + * 0 - success + * non zero - failure + */ +static void +raid_bdev_free_base_bdev_resource(struct raid_bdev *raid_bdev, + struct raid_base_bdev_info *base_info) +{ + spdk_bdev_module_release_bdev(base_info->bdev); + if (base_info->thread && base_info->thread != spdk_get_thread()) { + spdk_thread_send_msg(base_info->thread, _raid_bdev_free_base_bdev_resource, base_info->desc); + } else { + spdk_bdev_close(base_info->desc); + } + base_info->desc = NULL; + base_info->bdev = NULL; + + assert(raid_bdev->num_base_bdevs_discovered); + raid_bdev->num_base_bdevs_discovered--; +} + +/* + * brief: + * raid_bdev_destruct is the destruct function table pointer for raid bdev + * params: + * ctxt - pointer to raid_bdev + * returns: + * 0 - success + * non zero - failure + */ +static int +raid_bdev_destruct(void *ctxt) +{ + struct raid_bdev *raid_bdev = ctxt; + struct raid_base_bdev_info *base_info; + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_destruct\n"); + + raid_bdev->destruct_called = true; + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + /* + * Close all base bdev descriptors for which call has come from below + * layers. Also close the descriptors if we have started shutdown. + */ + if (g_shutdown_started || + ((base_info->remove_scheduled == true) && + (base_info->bdev != NULL))) { + raid_bdev_free_base_bdev_resource(raid_bdev, base_info); + } + } + + if (g_shutdown_started) { + TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link); + if (raid_bdev->module->stop != NULL) { + raid_bdev->module->stop(raid_bdev); + } + raid_bdev->state = RAID_BDEV_STATE_OFFLINE; + TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link); + } + + spdk_io_device_unregister(raid_bdev, NULL); + + if (raid_bdev->num_base_bdevs_discovered == 0) { + /* Free raid_bdev when there are no base bdevs left */ + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev base bdevs is 0, going to free all in destruct\n"); + raid_bdev_cleanup(raid_bdev); + } + + return 0; +} + +void +raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status) +{ + struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io); + + spdk_bdev_io_complete(bdev_io, status); +} + +/* + * brief: + * raid_bdev_io_complete_part - signal the completion of a part of the expected + * base bdev IOs and complete the raid_io if this is the final expected IO. + * The caller should first set raid_io->base_bdev_io_remaining. This function + * will decrement this counter by the value of the 'completed' parameter and + * complete the raid_io if the counter reaches 0. The caller is free to + * interpret the 'base_bdev_io_remaining' and 'completed' values as needed, + * it can represent e.g. blocks or IOs. + * params: + * raid_io - pointer to raid_bdev_io + * completed - the part of the raid_io that has been completed + * status - status of the base IO + * returns: + * true - if the raid_io is completed + * false - otherwise + */ +bool +raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed, + enum spdk_bdev_io_status status) +{ + assert(raid_io->base_bdev_io_remaining >= completed); + raid_io->base_bdev_io_remaining -= completed; + + if (status != SPDK_BDEV_IO_STATUS_SUCCESS) { + raid_io->base_bdev_io_status = status; + } + + if (raid_io->base_bdev_io_remaining == 0) { + raid_bdev_io_complete(raid_io, raid_io->base_bdev_io_status); + return true; + } else { + return false; + } +} + +/* + * brief: + * raid_bdev_queue_io_wait function processes the IO which failed to submit. + * It will try to queue the IOs after storing the context to bdev wait queue logic. + * params: + * raid_io - pointer to raid_bdev_io + * bdev - the block device that the IO is submitted to + * ch - io channel + * cb_fn - callback when the spdk_bdev_io for bdev becomes available + * returns: + * none + */ +void +raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev, + struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn) +{ + raid_io->waitq_entry.bdev = bdev; + raid_io->waitq_entry.cb_fn = cb_fn; + raid_io->waitq_entry.cb_arg = raid_io; + spdk_bdev_queue_io_wait(bdev, ch, &raid_io->waitq_entry); +} + +static void +raid_base_bdev_reset_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) +{ + struct raid_bdev_io *raid_io = cb_arg; + + spdk_bdev_free_io(bdev_io); + + raid_bdev_io_complete_part(raid_io, 1, success ? + SPDK_BDEV_IO_STATUS_SUCCESS : + SPDK_BDEV_IO_STATUS_FAILED); +} + +static void +raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io); + +static void +_raid_bdev_submit_reset_request(void *_raid_io) +{ + struct raid_bdev_io *raid_io = _raid_io; + + raid_bdev_submit_reset_request(raid_io); +} + +/* + * brief: + * raid_bdev_submit_reset_request function submits reset requests + * to member disks; it will submit as many as possible unless a reset fails with -ENOMEM, in + * which case it will queue it for later submission + * params: + * raid_io + * returns: + * none + */ +static void +raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io) +{ + struct raid_bdev *raid_bdev; + int ret; + uint8_t i; + struct raid_base_bdev_info *base_info; + struct spdk_io_channel *base_ch; + + raid_bdev = raid_io->raid_bdev; + + if (raid_io->base_bdev_io_remaining == 0) { + raid_io->base_bdev_io_remaining = raid_bdev->num_base_bdevs; + } + + while (raid_io->base_bdev_io_submitted < raid_bdev->num_base_bdevs) { + i = raid_io->base_bdev_io_submitted; + base_info = &raid_bdev->base_bdev_info[i]; + base_ch = raid_io->raid_ch->base_channel[i]; + ret = spdk_bdev_reset(base_info->desc, base_ch, + raid_base_bdev_reset_complete, raid_io); + if (ret == 0) { + raid_io->base_bdev_io_submitted++; + } else if (ret == -ENOMEM) { + raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch, + _raid_bdev_submit_reset_request); + return; + } else { + SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n"); + assert(false); + raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); + return; + } + } +} + +/* + * brief: + * Callback function to spdk_bdev_io_get_buf. + * params: + * ch - pointer to raid bdev io channel + * bdev_io - pointer to parent bdev_io on raid bdev device + * success - True if buffer is allocated or false otherwise. + * returns: + * none + */ +static void +raid_bdev_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, + bool success) +{ + struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx; + + if (!success) { + raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); + return; + } + + raid_io->raid_bdev->module->submit_rw_request(raid_io); +} + +/* + * brief: + * raid_bdev_submit_request function is the submit_request function pointer of + * raid bdev function table. This is used to submit the io on raid_bdev to below + * layers. + * params: + * ch - pointer to raid bdev io channel + * bdev_io - pointer to parent bdev_io on raid bdev device + * returns: + * none + */ +static void +raid_bdev_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) +{ + struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx; + + raid_io->raid_bdev = bdev_io->bdev->ctxt; + raid_io->raid_ch = spdk_io_channel_get_ctx(ch); + raid_io->base_bdev_io_remaining = 0; + raid_io->base_bdev_io_submitted = 0; + raid_io->base_bdev_io_status = SPDK_BDEV_IO_STATUS_SUCCESS; + + switch (bdev_io->type) { + case SPDK_BDEV_IO_TYPE_READ: + spdk_bdev_io_get_buf(bdev_io, raid_bdev_get_buf_cb, + bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); + break; + case SPDK_BDEV_IO_TYPE_WRITE: + raid_io->raid_bdev->module->submit_rw_request(raid_io); + break; + + case SPDK_BDEV_IO_TYPE_RESET: + raid_bdev_submit_reset_request(raid_io); + break; + + case SPDK_BDEV_IO_TYPE_FLUSH: + case SPDK_BDEV_IO_TYPE_UNMAP: + raid_io->raid_bdev->module->submit_null_payload_request(raid_io); + break; + + default: + SPDK_ERRLOG("submit request, invalid io type %u\n", bdev_io->type); + raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); + break; + } +} + +/* + * brief: + * _raid_bdev_io_type_supported checks whether io_type is supported in + * all base bdev modules of raid bdev module. If anyone among the base_bdevs + * doesn't support, the raid device doesn't supports. + * + * params: + * raid_bdev - pointer to raid bdev context + * io_type - io type + * returns: + * true - io_type is supported + * false - io_type is not supported + */ +inline static bool +_raid_bdev_io_type_supported(struct raid_bdev *raid_bdev, enum spdk_bdev_io_type io_type) +{ + struct raid_base_bdev_info *base_info; + + if (io_type == SPDK_BDEV_IO_TYPE_FLUSH || + io_type == SPDK_BDEV_IO_TYPE_UNMAP) { + if (raid_bdev->module->submit_null_payload_request == NULL) { + return false; + } + } + + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + if (base_info->bdev == NULL) { + assert(false); + continue; + } + + if (spdk_bdev_io_type_supported(base_info->bdev, io_type) == false) { + return false; + } + } + + return true; +} + +/* + * brief: + * raid_bdev_io_type_supported is the io_supported function for bdev function + * table which returns whether the particular io type is supported or not by + * raid bdev module + * params: + * ctx - pointer to raid bdev context + * type - io type + * returns: + * true - io_type is supported + * false - io_type is not supported + */ +static bool +raid_bdev_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) +{ + switch (io_type) { + case SPDK_BDEV_IO_TYPE_READ: + case SPDK_BDEV_IO_TYPE_WRITE: + return true; + + case SPDK_BDEV_IO_TYPE_FLUSH: + case SPDK_BDEV_IO_TYPE_RESET: + case SPDK_BDEV_IO_TYPE_UNMAP: + return _raid_bdev_io_type_supported(ctx, io_type); + + default: + return false; + } + + return false; +} + +/* + * brief: + * raid_bdev_get_io_channel is the get_io_channel function table pointer for + * raid bdev. This is used to return the io channel for this raid bdev + * params: + * ctxt - pointer to raid_bdev + * returns: + * pointer to io channel for raid bdev + */ +static struct spdk_io_channel * +raid_bdev_get_io_channel(void *ctxt) +{ + struct raid_bdev *raid_bdev = ctxt; + + return spdk_get_io_channel(raid_bdev); +} + +/* + * brief: + * raid_bdev_dump_info_json is the function table pointer for raid bdev + * params: + * ctx - pointer to raid_bdev + * w - pointer to json context + * returns: + * 0 - success + * non zero - failure + */ +static int +raid_bdev_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) +{ + struct raid_bdev *raid_bdev = ctx; + struct raid_base_bdev_info *base_info; + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_dump_config_json\n"); + assert(raid_bdev != NULL); + + /* Dump the raid bdev configuration related information */ + spdk_json_write_named_object_begin(w, "raid"); + spdk_json_write_named_uint32(w, "strip_size", raid_bdev->strip_size); + spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb); + spdk_json_write_named_uint32(w, "state", raid_bdev->state); + spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level)); + spdk_json_write_named_uint32(w, "destruct_called", raid_bdev->destruct_called); + spdk_json_write_named_uint32(w, "num_base_bdevs", raid_bdev->num_base_bdevs); + spdk_json_write_named_uint32(w, "num_base_bdevs_discovered", raid_bdev->num_base_bdevs_discovered); + spdk_json_write_name(w, "base_bdevs_list"); + spdk_json_write_array_begin(w); + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + if (base_info->bdev) { + spdk_json_write_string(w, base_info->bdev->name); + } else { + spdk_json_write_null(w); + } + } + spdk_json_write_array_end(w); + spdk_json_write_object_end(w); + + return 0; +} + +/* + * brief: + * raid_bdev_write_config_json is the function table pointer for raid bdev + * params: + * bdev - pointer to spdk_bdev + * w - pointer to json context + * returns: + * none + */ +static void +raid_bdev_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) +{ + struct raid_bdev *raid_bdev = bdev->ctxt; + struct raid_base_bdev_info *base_info; + + spdk_json_write_object_begin(w); + + spdk_json_write_named_string(w, "method", "bdev_raid_create"); + + spdk_json_write_named_object_begin(w, "params"); + spdk_json_write_named_string(w, "name", bdev->name); + spdk_json_write_named_uint32(w, "strip_size", raid_bdev->strip_size_kb); + spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level)); + + spdk_json_write_named_array_begin(w, "base_bdevs"); + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + if (base_info->bdev) { + spdk_json_write_string(w, base_info->bdev->name); + } + } + spdk_json_write_array_end(w); + spdk_json_write_object_end(w); + + spdk_json_write_object_end(w); +} + +/* g_raid_bdev_fn_table is the function table for raid bdev */ +static const struct spdk_bdev_fn_table g_raid_bdev_fn_table = { + .destruct = raid_bdev_destruct, + .submit_request = raid_bdev_submit_request, + .io_type_supported = raid_bdev_io_type_supported, + .get_io_channel = raid_bdev_get_io_channel, + .dump_info_json = raid_bdev_dump_info_json, + .write_config_json = raid_bdev_write_config_json, +}; + +/* + * brief: + * raid_bdev_config_cleanup function is used to free memory for one raid_bdev in configuration + * params: + * raid_cfg - pointer to raid_bdev_config structure + * returns: + * none + */ +void +raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg) +{ + uint8_t i; + + TAILQ_REMOVE(&g_raid_config.raid_bdev_config_head, raid_cfg, link); + g_raid_config.total_raid_bdev--; + + if (raid_cfg->base_bdev) { + for (i = 0; i < raid_cfg->num_base_bdevs; i++) { + free(raid_cfg->base_bdev[i].name); + } + free(raid_cfg->base_bdev); + } + free(raid_cfg->name); + free(raid_cfg); +} + +/* + * brief: + * raid_bdev_free is the raid bdev function table function pointer. This is + * called on bdev free path + * params: + * none + * returns: + * none + */ +static void +raid_bdev_free(void) +{ + struct raid_bdev_config *raid_cfg, *tmp; + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_free\n"); + TAILQ_FOREACH_SAFE(raid_cfg, &g_raid_config.raid_bdev_config_head, link, tmp) { + raid_bdev_config_cleanup(raid_cfg); + } +} + +/* brief + * raid_bdev_config_find_by_name is a helper function to find raid bdev config + * by name as key. + * + * params: + * raid_name - name for raid bdev. + */ +struct raid_bdev_config * +raid_bdev_config_find_by_name(const char *raid_name) +{ + struct raid_bdev_config *raid_cfg; + + TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) { + if (!strcmp(raid_cfg->name, raid_name)) { + return raid_cfg; + } + } + + return raid_cfg; +} + +/* + * brief + * raid_bdev_config_add function adds config for newly created raid bdev. + * + * params: + * raid_name - name for raid bdev. + * strip_size - strip size in KB + * num_base_bdevs - number of base bdevs. + * level - raid level. + * _raid_cfg - Pointer to newly added configuration + */ +int +raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs, + enum raid_level level, struct raid_bdev_config **_raid_cfg) +{ + struct raid_bdev_config *raid_cfg; + + raid_cfg = raid_bdev_config_find_by_name(raid_name); + if (raid_cfg != NULL) { + SPDK_ERRLOG("Duplicate raid bdev name found in config file %s\n", + raid_name); + return -EEXIST; + } + + if (spdk_u32_is_pow2(strip_size) == false) { + SPDK_ERRLOG("Invalid strip size %" PRIu32 "\n", strip_size); + return -EINVAL; + } + + if (num_base_bdevs == 0) { + SPDK_ERRLOG("Invalid base device count %u\n", num_base_bdevs); + return -EINVAL; + } + + raid_cfg = calloc(1, sizeof(*raid_cfg)); + if (raid_cfg == NULL) { + SPDK_ERRLOG("unable to allocate memory\n"); + return -ENOMEM; + } + + raid_cfg->name = strdup(raid_name); + if (!raid_cfg->name) { + free(raid_cfg); + SPDK_ERRLOG("unable to allocate memory\n"); + return -ENOMEM; + } + raid_cfg->strip_size = strip_size; + raid_cfg->num_base_bdevs = num_base_bdevs; + raid_cfg->level = level; + + raid_cfg->base_bdev = calloc(num_base_bdevs, sizeof(*raid_cfg->base_bdev)); + if (raid_cfg->base_bdev == NULL) { + free(raid_cfg->name); + free(raid_cfg); + SPDK_ERRLOG("unable to allocate memory\n"); + return -ENOMEM; + } + + TAILQ_INSERT_TAIL(&g_raid_config.raid_bdev_config_head, raid_cfg, link); + g_raid_config.total_raid_bdev++; + + *_raid_cfg = raid_cfg; + return 0; +} + +/* + * brief: + * raid_bdev_config_add_base_bdev function add base bdev to raid bdev config. + * + * params: + * raid_cfg - pointer to raid bdev configuration + * base_bdev_name - name of base bdev + * slot - Position to add base bdev + */ +int +raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg, const char *base_bdev_name, + uint8_t slot) +{ + uint8_t i; + struct raid_bdev_config *tmp; + + if (slot >= raid_cfg->num_base_bdevs) { + return -EINVAL; + } + + TAILQ_FOREACH(tmp, &g_raid_config.raid_bdev_config_head, link) { + for (i = 0; i < tmp->num_base_bdevs; i++) { + if (tmp->base_bdev[i].name != NULL) { + if (!strcmp(tmp->base_bdev[i].name, base_bdev_name)) { + SPDK_ERRLOG("duplicate base bdev name %s mentioned\n", + base_bdev_name); + return -EEXIST; + } + } + } + } + + raid_cfg->base_bdev[slot].name = strdup(base_bdev_name); + if (raid_cfg->base_bdev[slot].name == NULL) { + SPDK_ERRLOG("unable to allocate memory\n"); + return -ENOMEM; + } + + return 0; +} + +static struct { + const char *name; + enum raid_level value; +} g_raid_level_names[] = { + { "raid0", RAID0 }, + { "0", RAID0 }, + { "raid5", RAID5 }, + { "5", RAID5 }, + { } +}; + +enum raid_level raid_bdev_parse_raid_level(const char *str) +{ + unsigned int i; + + assert(str != NULL); + + for (i = 0; g_raid_level_names[i].name != NULL; i++) { + if (strcasecmp(g_raid_level_names[i].name, str) == 0) { + return g_raid_level_names[i].value; + } + } + + return INVALID_RAID_LEVEL; +} + +const char * +raid_bdev_level_to_str(enum raid_level level) +{ + unsigned int i; + + for (i = 0; g_raid_level_names[i].name != NULL; i++) { + if (g_raid_level_names[i].value == level) { + return g_raid_level_names[i].name; + } + } + + return ""; +} + +/* + * brief: + * raid_bdev_parse_raid is used to parse the raid bdev from config file based on + * pre-defined raid bdev format in config file. + * Format of config file: + * [RAID1] + * Name raid1 + * StripSize 64 + * NumDevices 2 + * RaidLevel 0 + * Devices Nvme0n1 Nvme1n1 + * + * [RAID2] + * Name raid2 + * StripSize 64 + * NumDevices 3 + * RaidLevel 0 + * Devices Nvme2n1 Nvme3n1 Nvme4n1 + * + * params: + * conf_section - pointer to config section + * returns: + * 0 - success + * non zero - failure + */ +static int +raid_bdev_parse_raid(struct spdk_conf_section *conf_section) +{ + const char *raid_name; + uint32_t strip_size; + uint8_t num_base_bdevs; + const char *raid_level_str; + enum raid_level level; + const char *base_bdev_name; + struct raid_bdev_config *raid_cfg; + int rc, i, val; + + raid_name = spdk_conf_section_get_val(conf_section, "Name"); + if (raid_name == NULL) { + SPDK_ERRLOG("raid_name is null\n"); + return -EINVAL; + } + + val = spdk_conf_section_get_intval(conf_section, "StripSize"); + if (val < 0) { + return -EINVAL; + } + strip_size = val; + + val = spdk_conf_section_get_intval(conf_section, "NumDevices"); + if (val < 0) { + return -EINVAL; + } + num_base_bdevs = val; + + raid_level_str = spdk_conf_section_get_val(conf_section, "RaidLevel"); + if (raid_level_str == NULL) { + SPDK_ERRLOG("Missing RaidLevel\n"); + return -EINVAL; + } + level = raid_bdev_parse_raid_level(raid_level_str); + if (level == INVALID_RAID_LEVEL) { + SPDK_ERRLOG("Invalid RaidLevel\n"); + return -EINVAL; + } + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "%s %" PRIu32 " %u %u\n", + raid_name, strip_size, num_base_bdevs, level); + + rc = raid_bdev_config_add(raid_name, strip_size, num_base_bdevs, level, + &raid_cfg); + if (rc != 0) { + SPDK_ERRLOG("Failed to add raid bdev config\n"); + return rc; + } + + for (i = 0; true; i++) { + base_bdev_name = spdk_conf_section_get_nmval(conf_section, "Devices", 0, i); + if (base_bdev_name == NULL) { + break; + } + if (i >= num_base_bdevs) { + raid_bdev_config_cleanup(raid_cfg); + SPDK_ERRLOG("Number of devices mentioned is more than count\n"); + return -EINVAL; + } + + rc = raid_bdev_config_add_base_bdev(raid_cfg, base_bdev_name, i); + if (rc != 0) { + raid_bdev_config_cleanup(raid_cfg); + SPDK_ERRLOG("Failed to add base bdev to raid bdev config\n"); + return rc; + } + } + + if (i != raid_cfg->num_base_bdevs) { + raid_bdev_config_cleanup(raid_cfg); + SPDK_ERRLOG("Number of devices mentioned is less than count\n"); + return -EINVAL; + } + + rc = raid_bdev_create(raid_cfg); + if (rc != 0) { + raid_bdev_config_cleanup(raid_cfg); + SPDK_ERRLOG("Failed to create raid bdev\n"); + return rc; + } + + rc = raid_bdev_add_base_devices(raid_cfg); + if (rc != 0) { + SPDK_ERRLOG("Failed to add any base bdev to raid bdev\n"); + /* Config is not removed in this case. */ + } + + return 0; +} + +/* + * brief: + * raid_bdev_parse_config is used to find the raid bdev config section and parse it + * Format of config file: + * params: + * none + * returns: + * 0 - success + * non zero - failure + */ +static int +raid_bdev_parse_config(void) +{ + int ret; + struct spdk_conf_section *conf_section; + + conf_section = spdk_conf_first_section(NULL); + while (conf_section != NULL) { + if (spdk_conf_section_match_prefix(conf_section, "RAID")) { + ret = raid_bdev_parse_raid(conf_section); + if (ret < 0) { + SPDK_ERRLOG("Unable to parse raid bdev section\n"); + return ret; + } + } + conf_section = spdk_conf_next_section(conf_section); + } + + return 0; +} + +/* + * brief: + * raid_bdev_fini_start is called when bdev layer is starting the + * shutdown process + * params: + * none + * returns: + * none + */ +static void +raid_bdev_fini_start(void) +{ + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_fini_start\n"); + g_shutdown_started = true; +} + +/* + * brief: + * raid_bdev_exit is called on raid bdev module exit time by bdev layer + * params: + * none + * returns: + * none + */ +static void +raid_bdev_exit(void) +{ + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_exit\n"); + raid_bdev_free(); +} + +/* + * brief: + * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid + * module + * params: + * none + * returns: + * size of spdk_bdev_io context for raid + */ +static int +raid_bdev_get_ctx_size(void) +{ + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_get_ctx_size\n"); + return sizeof(struct raid_bdev_io); +} + +/* + * brief: + * raid_bdev_get_running_config is used to get the configuration options. + * + * params: + * fp - The pointer to a file that will be written to the configuration options. + * returns: + * none + */ +static void +raid_bdev_get_running_config(FILE *fp) +{ + struct raid_bdev *raid_bdev; + struct raid_base_bdev_info *base_info; + int index = 1; + + TAILQ_FOREACH(raid_bdev, &g_raid_bdev_configured_list, state_link) { + fprintf(fp, + "\n" + "[RAID%d]\n" + " Name %s\n" + " StripSize %" PRIu32 "\n" + " NumDevices %u\n" + " RaidLevel %s\n", + index, raid_bdev->bdev.name, raid_bdev->strip_size_kb, + raid_bdev->num_base_bdevs, + raid_bdev_level_to_str(raid_bdev->level)); + fprintf(fp, + " Devices "); + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + if (base_info->bdev) { + fprintf(fp, + "%s ", + base_info->bdev->name); + } + } + fprintf(fp, + "\n"); + index++; + } +} + +/* + * brief: + * raid_bdev_can_claim_bdev is the function to check if this base_bdev can be + * claimed by raid bdev or not. + * params: + * bdev_name - represents base bdev name + * _raid_cfg - pointer to raid bdev config parsed from config file + * base_bdev_slot - if bdev can be claimed, it represents the base_bdev correct + * slot. This field is only valid if return value of this function is true + * returns: + * true - if bdev can be claimed + * false - if bdev can't be claimed + */ +static bool +raid_bdev_can_claim_bdev(const char *bdev_name, struct raid_bdev_config **_raid_cfg, + uint8_t *base_bdev_slot) +{ + struct raid_bdev_config *raid_cfg; + uint8_t i; + + TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) { + for (i = 0; i < raid_cfg->num_base_bdevs; i++) { + /* + * Check if the base bdev name is part of raid bdev configuration. + * If match is found then return true and the slot information where + * this base bdev should be inserted in raid bdev + */ + if (!strcmp(bdev_name, raid_cfg->base_bdev[i].name)) { + *_raid_cfg = raid_cfg; + *base_bdev_slot = i; + return true; + } + } + } + + return false; +} + + +static struct spdk_bdev_module g_raid_if = { + .name = "raid", + .module_init = raid_bdev_init, + .fini_start = raid_bdev_fini_start, + .module_fini = raid_bdev_exit, + .get_ctx_size = raid_bdev_get_ctx_size, + .examine_config = raid_bdev_examine, + .config_text = raid_bdev_get_running_config, + .async_init = false, + .async_fini = false, +}; +SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if) + +/* + * brief: + * raid_bdev_init is the initialization function for raid bdev module + * params: + * none + * returns: + * 0 - success + * non zero - failure + */ +static int +raid_bdev_init(void) +{ + int ret; + + /* Parse config file for raids */ + ret = raid_bdev_parse_config(); + if (ret < 0) { + SPDK_ERRLOG("raid bdev init failed parsing\n"); + raid_bdev_free(); + return ret; + } + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_init completed successfully\n"); + + return 0; +} + +/* + * brief: + * raid_bdev_create allocates raid bdev based on passed configuration + * params: + * raid_cfg - configuration of raid bdev + * returns: + * 0 - success + * non zero - failure + */ +int +raid_bdev_create(struct raid_bdev_config *raid_cfg) +{ + struct raid_bdev *raid_bdev; + struct spdk_bdev *raid_bdev_gen; + struct raid_bdev_module *module; + + module = raid_bdev_module_find(raid_cfg->level); + if (module == NULL) { + SPDK_ERRLOG("Unsupported raid level '%d'\n", raid_cfg->level); + return -EINVAL; + } + + assert(module->base_bdevs_min != 0); + if (raid_cfg->num_base_bdevs < module->base_bdevs_min) { + SPDK_ERRLOG("At least %u base devices required for %s\n", + module->base_bdevs_min, + raid_bdev_level_to_str(raid_cfg->level)); + return -EINVAL; + } + + raid_bdev = calloc(1, sizeof(*raid_bdev)); + if (!raid_bdev) { + SPDK_ERRLOG("Unable to allocate memory for raid bdev\n"); + return -ENOMEM; + } + + raid_bdev->module = module; + raid_bdev->num_base_bdevs = raid_cfg->num_base_bdevs; + raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs, + sizeof(struct raid_base_bdev_info)); + if (!raid_bdev->base_bdev_info) { + SPDK_ERRLOG("Unable able to allocate base bdev info\n"); + free(raid_bdev); + return -ENOMEM; + } + + /* strip_size_kb is from the rpc param. strip_size is in blocks and used + * internally and set later. + */ + raid_bdev->strip_size = 0; + raid_bdev->strip_size_kb = raid_cfg->strip_size; + raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; + raid_bdev->config = raid_cfg; + raid_bdev->level = raid_cfg->level; + + raid_bdev_gen = &raid_bdev->bdev; + + raid_bdev_gen->name = strdup(raid_cfg->name); + if (!raid_bdev_gen->name) { + SPDK_ERRLOG("Unable to allocate name for raid\n"); + free(raid_bdev->base_bdev_info); + free(raid_bdev); + return -ENOMEM; + } + + raid_bdev_gen->product_name = "Raid Volume"; + raid_bdev_gen->ctxt = raid_bdev; + raid_bdev_gen->fn_table = &g_raid_bdev_fn_table; + raid_bdev_gen->module = &g_raid_if; + raid_bdev_gen->write_cache = 0; + + TAILQ_INSERT_TAIL(&g_raid_bdev_configuring_list, raid_bdev, state_link); + TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link); + + raid_cfg->raid_bdev = raid_bdev; + + return 0; +} + +/* + * brief + * raid_bdev_alloc_base_bdev_resource allocates resource of base bdev. + * params: + * raid_bdev - pointer to raid bdev + * bdev - pointer to base bdev + * base_bdev_slot - position to add base bdev + * returns: + * 0 - success + * non zero - failure + */ +static int +raid_bdev_alloc_base_bdev_resource(struct raid_bdev *raid_bdev, struct spdk_bdev *bdev, + uint8_t base_bdev_slot) +{ + struct spdk_bdev_desc *desc; + int rc; + + rc = spdk_bdev_open(bdev, true, raid_bdev_remove_base_bdev, bdev, &desc); + if (rc != 0) { + SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", bdev->name); + return rc; + } + + rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if); + if (rc != 0) { + SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n"); + spdk_bdev_close(desc); + return rc; + } + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "bdev %s is claimed\n", bdev->name); + + assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE); + assert(base_bdev_slot < raid_bdev->num_base_bdevs); + + raid_bdev->base_bdev_info[base_bdev_slot].thread = spdk_get_thread(); + raid_bdev->base_bdev_info[base_bdev_slot].bdev = bdev; + raid_bdev->base_bdev_info[base_bdev_slot].desc = desc; + raid_bdev->num_base_bdevs_discovered++; + assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs); + + return 0; +} + +/* + * brief: + * If raid bdev config is complete, then only register the raid bdev to + * bdev layer and remove this raid bdev from configuring list and + * insert the raid bdev to configured list + * params: + * raid_bdev - pointer to raid bdev + * returns: + * 0 - success + * non zero - failure + */ +static int +raid_bdev_configure(struct raid_bdev *raid_bdev) +{ + uint32_t blocklen = 0; + struct spdk_bdev *raid_bdev_gen; + struct raid_base_bdev_info *base_info; + int rc = 0; + + assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING); + assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs); + + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + /* Check blocklen for all base bdevs that it should be same */ + if (blocklen == 0) { + blocklen = base_info->bdev->blocklen; + } else if (blocklen != base_info->bdev->blocklen) { + /* + * Assumption is that all the base bdevs for any raid bdev should + * have same blocklen + */ + SPDK_ERRLOG("Blocklen of various bdevs not matching\n"); + return -EINVAL; + } + } + assert(blocklen > 0); + + /* The strip_size_kb is read in from user in KB. Convert to blocks here for + * internal use. + */ + raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / blocklen; + raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size); + raid_bdev->blocklen_shift = spdk_u32log2(blocklen); + + raid_bdev_gen = &raid_bdev->bdev; + raid_bdev_gen->blocklen = blocklen; + + rc = raid_bdev->module->start(raid_bdev); + if (rc != 0) { + SPDK_ERRLOG("raid module startup callback failed\n"); + return rc; + } + raid_bdev->state = RAID_BDEV_STATE_ONLINE; + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "io device register %p\n", raid_bdev); + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "blockcnt %lu, blocklen %u\n", + raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen); + spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb, + sizeof(struct raid_bdev_io_channel), + raid_bdev->bdev.name); + rc = spdk_bdev_register(raid_bdev_gen); + if (rc != 0) { + SPDK_ERRLOG("Unable to register raid bdev and stay at configuring state\n"); + if (raid_bdev->module->stop != NULL) { + raid_bdev->module->stop(raid_bdev); + } + spdk_io_device_unregister(raid_bdev, NULL); + raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; + return rc; + } + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev generic %p\n", raid_bdev_gen); + TAILQ_REMOVE(&g_raid_bdev_configuring_list, raid_bdev, state_link); + TAILQ_INSERT_TAIL(&g_raid_bdev_configured_list, raid_bdev, state_link); + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev is created with name %s, raid_bdev %p\n", + raid_bdev_gen->name, raid_bdev); + + return 0; +} + +/* + * brief: + * If raid bdev is online and registered, change the bdev state to + * configuring and unregister this raid device. Queue this raid device + * in configuring list + * params: + * raid_bdev - pointer to raid bdev + * cb_fn - callback function + * cb_arg - argument to callback function + * returns: + * none + */ +static void +raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, + void *cb_arg) +{ + if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { + if (cb_fn) { + cb_fn(cb_arg, 0); + } + return; + } + + assert(raid_bdev->num_base_bdevs == raid_bdev->num_base_bdevs_discovered); + TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link); + if (raid_bdev->module->stop != NULL) { + raid_bdev->module->stop(raid_bdev); + } + raid_bdev->state = RAID_BDEV_STATE_OFFLINE; + assert(raid_bdev->num_base_bdevs_discovered); + TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link); + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev state chaning from online to offline\n"); + + spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg); +} + +/* + * brief: + * raid_bdev_find_by_base_bdev function finds the raid bdev which has + * claimed the base bdev. + * params: + * base_bdev - pointer to base bdev pointer + * _raid_bdev - Reference to pointer to raid bdev + * _base_info - Reference to the raid base bdev info. + * returns: + * true - if the raid bdev is found. + * false - if the raid bdev is not found. + */ +static bool +raid_bdev_find_by_base_bdev(struct spdk_bdev *base_bdev, struct raid_bdev **_raid_bdev, + struct raid_base_bdev_info **_base_info) +{ + struct raid_bdev *raid_bdev; + struct raid_base_bdev_info *base_info; + + TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + if (base_info->bdev == base_bdev) { + *_raid_bdev = raid_bdev; + *_base_info = base_info; + return true; + } + } + } + + return false; +} + +/* + * brief: + * raid_bdev_remove_base_bdev function is called by below layers when base_bdev + * is removed. This function checks if this base bdev is part of any raid bdev + * or not. If yes, it takes necessary action on that particular raid bdev. + * params: + * ctx - pointer to base bdev pointer which got removed + * returns: + * none + */ +static void +raid_bdev_remove_base_bdev(void *ctx) +{ + struct spdk_bdev *base_bdev = ctx; + struct raid_bdev *raid_bdev = NULL; + struct raid_base_bdev_info *base_info; + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_remove_base_bdev\n"); + + /* Find the raid_bdev which has claimed this base_bdev */ + if (!raid_bdev_find_by_base_bdev(base_bdev, &raid_bdev, &base_info)) { + SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name); + return; + } + + assert(base_info->desc); + base_info->remove_scheduled = true; + + if (raid_bdev->destruct_called == true || + raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) { + /* + * As raid bdev is not registered yet or already unregistered, + * so cleanup should be done here itself. + */ + raid_bdev_free_base_bdev_resource(raid_bdev, base_info); + if (raid_bdev->num_base_bdevs_discovered == 0) { + /* There is no base bdev for this raid, so free the raid device. */ + raid_bdev_cleanup(raid_bdev); + return; + } + } + + raid_bdev_deconfigure(raid_bdev, NULL, NULL); +} + +/* + * brief: + * Remove base bdevs from the raid bdev one by one. Skip any base bdev which + * doesn't exist. + * params: + * raid_cfg - pointer to raid bdev config. + * cb_fn - callback function + * cb_ctx - argument to callback function + */ +void +raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg, + raid_bdev_destruct_cb cb_fn, void *cb_arg) +{ + struct raid_bdev *raid_bdev; + struct raid_base_bdev_info *base_info; + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_remove_base_devices\n"); + + raid_bdev = raid_cfg->raid_bdev; + if (raid_bdev == NULL) { + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev %s doesn't exist now\n", raid_cfg->name); + if (cb_fn) { + cb_fn(cb_arg, 0); + } + return; + } + + if (raid_bdev->destroy_started) { + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "destroying raid bdev %s is already started\n", + raid_cfg->name); + if (cb_fn) { + cb_fn(cb_arg, -EALREADY); + } + return; + } + + raid_bdev->destroy_started = true; + + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + if (base_info->bdev == NULL) { + continue; + } + + assert(base_info->desc); + base_info->remove_scheduled = true; + + if (raid_bdev->destruct_called == true || + raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) { + /* + * As raid bdev is not registered yet or already unregistered, + * so cleanup should be done here itself. + */ + raid_bdev_free_base_bdev_resource(raid_bdev, base_info); + if (raid_bdev->num_base_bdevs_discovered == 0) { + /* There is no base bdev for this raid, so free the raid device. */ + raid_bdev_cleanup(raid_bdev); + if (cb_fn) { + cb_fn(cb_arg, 0); + } + return; + } + } + } + + raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg); +} + +/* + * brief: + * raid_bdev_add_base_device function is the actual function which either adds + * the nvme base device to existing raid bdev or create a new raid bdev. It also claims + * the base device and keep the open descriptor. + * params: + * raid_cfg - pointer to raid bdev config + * bdev - pointer to base bdev + * base_bdev_slot - position to add base bdev + * returns: + * 0 - success + * non zero - failure + */ +static int +raid_bdev_add_base_device(struct raid_bdev_config *raid_cfg, struct spdk_bdev *bdev, + uint8_t base_bdev_slot) +{ + struct raid_bdev *raid_bdev; + int rc; + + raid_bdev = raid_cfg->raid_bdev; + if (!raid_bdev) { + SPDK_ERRLOG("Raid bdev '%s' is not created yet\n", raid_cfg->name); + return -ENODEV; + } + + rc = raid_bdev_alloc_base_bdev_resource(raid_bdev, bdev, base_bdev_slot); + if (rc != 0) { + SPDK_ERRLOG("Failed to allocate resource for bdev '%s'\n", bdev->name); + return rc; + } + + assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs); + + if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs) { + rc = raid_bdev_configure(raid_bdev); + if (rc != 0) { + SPDK_ERRLOG("Failed to configure raid bdev\n"); + return rc; + } + } + + return 0; +} + +/* + * brief: + * Add base bdevs to the raid bdev one by one. Skip any base bdev which doesn't + * exist or fails to add. If all base bdevs are successfully added, the raid bdev + * moves to the configured state and becomes available. Otherwise, the raid bdev + * stays at the configuring state with added base bdevs. + * params: + * raid_cfg - pointer to raid bdev config + * returns: + * 0 - The raid bdev moves to the configured state or stays at the configuring + * state with added base bdevs due to any nonexistent base bdev. + * non zero - Failed to add any base bdev and stays at the configuring state with + * added base bdevs. + */ +int +raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg) +{ + struct spdk_bdev *base_bdev; + uint8_t i; + int rc = 0, _rc; + + for (i = 0; i < raid_cfg->num_base_bdevs; i++) { + base_bdev = spdk_bdev_get_by_name(raid_cfg->base_bdev[i].name); + if (base_bdev == NULL) { + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "base bdev %s doesn't exist now\n", + raid_cfg->base_bdev[i].name); + continue; + } + + _rc = raid_bdev_add_base_device(raid_cfg, base_bdev, i); + if (_rc != 0) { + SPDK_ERRLOG("Failed to add base bdev %s to RAID bdev %s: %s\n", + raid_cfg->base_bdev[i].name, raid_cfg->name, + spdk_strerror(-_rc)); + if (rc == 0) { + rc = _rc; + } + } + } + + return rc; +} + +/* + * brief: + * raid_bdev_examine function is the examine function call by the below layers + * like bdev_nvme layer. This function will check if this base bdev can be + * claimed by this raid bdev or not. + * params: + * bdev - pointer to base bdev + * returns: + * none + */ +static void +raid_bdev_examine(struct spdk_bdev *bdev) +{ + struct raid_bdev_config *raid_cfg; + uint8_t base_bdev_slot; + + if (raid_bdev_can_claim_bdev(bdev->name, &raid_cfg, &base_bdev_slot)) { + raid_bdev_add_base_device(raid_cfg, bdev, base_bdev_slot); + } else { + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "bdev %s can't be claimed\n", + bdev->name); + } + + spdk_bdev_module_examine_done(&g_raid_if); +} + +/* Log component for bdev raid bdev module */ +SPDK_LOG_REGISTER_COMPONENT("bdev_raid", SPDK_LOG_BDEV_RAID) diff --git a/src/spdk/module/bdev/raid/bdev_raid.h b/src/spdk/module/bdev/raid/bdev_raid.h new file mode 100644 index 000000000..4acca1da6 --- /dev/null +++ b/src/spdk/module/bdev/raid/bdev_raid.h @@ -0,0 +1,319 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SPDK_BDEV_RAID_INTERNAL_H +#define SPDK_BDEV_RAID_INTERNAL_H + +#include "spdk/bdev_module.h" + +enum raid_level { + INVALID_RAID_LEVEL = -1, + RAID0 = 0, + RAID5 = 5, +}; + +/* + * Raid state describes the state of the raid. This raid bdev can be either in + * configured list or configuring list + */ +enum raid_bdev_state { + /* raid bdev is ready and is seen by upper layers */ + RAID_BDEV_STATE_ONLINE, + + /* + * raid bdev is configuring, not all underlying bdevs are present. + * And can't be seen by upper layers. + */ + RAID_BDEV_STATE_CONFIGURING, + + /* + * In offline state, raid bdev layer will complete all incoming commands without + * submitting to underlying base nvme bdevs + */ + RAID_BDEV_STATE_OFFLINE, + + /* raid bdev max, new states should be added before this */ + RAID_BDEV_MAX +}; + +/* + * raid_base_bdev_info contains information for the base bdevs which are part of some + * raid. This structure contains the per base bdev information. Whatever is + * required per base device for raid bdev will be kept here + */ +struct raid_base_bdev_info { + /* pointer to base spdk bdev */ + struct spdk_bdev *bdev; + + /* pointer to base bdev descriptor opened by raid bdev */ + struct spdk_bdev_desc *desc; + + /* + * When underlying base device calls the hot plug function on drive removal, + * this flag will be set and later after doing some processing, base device + * descriptor will be closed + */ + bool remove_scheduled; + + /* thread where base device is opened */ + struct spdk_thread *thread; +}; + +/* + * raid_bdev_io is the context part of bdev_io. It contains the information + * related to bdev_io for a raid bdev + */ +struct raid_bdev_io { + /* The raid bdev associated with this IO */ + struct raid_bdev *raid_bdev; + + /* WaitQ entry, used only in waitq logic */ + struct spdk_bdev_io_wait_entry waitq_entry; + + /* Context of the original channel for this IO */ + struct raid_bdev_io_channel *raid_ch; + + /* Used for tracking progress on io requests sent to member disks. */ + uint64_t base_bdev_io_remaining; + uint8_t base_bdev_io_submitted; + uint8_t base_bdev_io_status; +}; + +/* + * raid_bdev is the single entity structure which contains SPDK block device + * and the information related to any raid bdev either configured or + * in configuring list. io device is created on this. + */ +struct raid_bdev { + /* raid bdev device, this will get registered in bdev layer */ + struct spdk_bdev bdev; + + /* link of raid bdev to link it to configured, configuring or offline list */ + TAILQ_ENTRY(raid_bdev) state_link; + + /* link of raid bdev to link it to global raid bdev list */ + TAILQ_ENTRY(raid_bdev) global_link; + + /* pointer to config file entry */ + struct raid_bdev_config *config; + + /* array of base bdev info */ + struct raid_base_bdev_info *base_bdev_info; + + /* strip size of raid bdev in blocks */ + uint32_t strip_size; + + /* strip size of raid bdev in KB */ + uint32_t strip_size_kb; + + /* strip size bit shift for optimized calculation */ + uint32_t strip_size_shift; + + /* block length bit shift for optimized calculation */ + uint32_t blocklen_shift; + + /* state of raid bdev */ + enum raid_bdev_state state; + + /* number of base bdevs comprising raid bdev */ + uint8_t num_base_bdevs; + + /* number of base bdevs discovered */ + uint8_t num_base_bdevs_discovered; + + /* Raid Level of this raid bdev */ + enum raid_level level; + + /* Set to true if destruct is called for this raid bdev */ + bool destruct_called; + + /* Set to true if destroy of this raid bdev is started. */ + bool destroy_started; + + /* Module for RAID-level specific operations */ + struct raid_bdev_module *module; + + /* Private data for the raid module */ + void *module_private; +}; + +#define RAID_FOR_EACH_BASE_BDEV(r, i) \ + for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++) + +/* + * raid_base_bdev_config is the per base bdev data structure which contains + * information w.r.t to per base bdev during parsing config + */ +struct raid_base_bdev_config { + /* base bdev name from config file */ + char *name; +}; + +/* + * raid_bdev_config contains the raid bdev config related information after + * parsing the config file + */ +struct raid_bdev_config { + /* base bdev config per underlying bdev */ + struct raid_base_bdev_config *base_bdev; + + /* Points to already created raid bdev */ + struct raid_bdev *raid_bdev; + + char *name; + + /* strip size of this raid bdev in kilo bytes */ + uint32_t strip_size; + + /* number of base bdevs */ + uint8_t num_base_bdevs; + + /* raid level */ + enum raid_level level; + + TAILQ_ENTRY(raid_bdev_config) link; +}; + +/* + * raid_config is the top level structure representing the raid bdev config as read + * from config file for all raids + */ +struct raid_config { + /* raid bdev context from config file */ + TAILQ_HEAD(, raid_bdev_config) raid_bdev_config_head; + + /* total raid bdev from config file */ + uint8_t total_raid_bdev; +}; + +/* + * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It + * contains the relationship of raid bdev io channel with base bdev io channels. + */ +struct raid_bdev_io_channel { + /* Array of IO channels of base bdevs */ + struct spdk_io_channel **base_channel; + + /* Number of IO channels */ + uint8_t num_channels; +}; + +/* TAIL heads for various raid bdev lists */ +TAILQ_HEAD(raid_configured_tailq, raid_bdev); +TAILQ_HEAD(raid_configuring_tailq, raid_bdev); +TAILQ_HEAD(raid_all_tailq, raid_bdev); +TAILQ_HEAD(raid_offline_tailq, raid_bdev); + +extern struct raid_configured_tailq g_raid_bdev_configured_list; +extern struct raid_configuring_tailq g_raid_bdev_configuring_list; +extern struct raid_all_tailq g_raid_bdev_list; +extern struct raid_offline_tailq g_raid_bdev_offline_list; +extern struct raid_config g_raid_config; + +typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc); + +int raid_bdev_create(struct raid_bdev_config *raid_cfg); +int raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg); +void raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg, + raid_bdev_destruct_cb cb_fn, void *cb_ctx); +int raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs, + enum raid_level level, struct raid_bdev_config **_raid_cfg); +int raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg, + const char *base_bdev_name, uint8_t slot); +void raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg); +struct raid_bdev_config *raid_bdev_config_find_by_name(const char *raid_name); +enum raid_level raid_bdev_parse_raid_level(const char *str); +const char *raid_bdev_level_to_str(enum raid_level level); + +/* + * RAID module descriptor + */ +struct raid_bdev_module { + /* RAID level implemented by this module */ + enum raid_level level; + + /* Minimum required number of base bdevs. Must be > 0. */ + uint8_t base_bdevs_min; + + /* + * Maximum number of base bdevs that can be removed without failing + * the array. + */ + uint8_t base_bdevs_max_degraded; + + /* + * Called when the raid is starting, right before changing the state to + * online and registering the bdev. Parameters of the bdev like blockcnt + * should be set here. + * + * Non-zero return value will abort the startup process. + */ + int (*start)(struct raid_bdev *raid_bdev); + + /* + * Called when the raid is stopping, right before changing the state to + * offline and unregistering the bdev. Optional. + */ + void (*stop)(struct raid_bdev *raid_bdev); + + /* Handler for R/W requests */ + void (*submit_rw_request)(struct raid_bdev_io *raid_io); + + /* Handler for requests without payload (flush, unmap). Optional. */ + void (*submit_null_payload_request)(struct raid_bdev_io *raid_io); + + TAILQ_ENTRY(raid_bdev_module) link; +}; + +void raid_bdev_module_list_add(struct raid_bdev_module *raid_module); + +#define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line) +#define __RAID_MODULE_REGISTER_(line) raid_module_register_##line + +#define RAID_MODULE_REGISTER(_module) \ +__attribute__((constructor)) static void \ +__RAID_MODULE_REGISTER(__LINE__)(void) \ +{ \ + raid_bdev_module_list_add(_module); \ +} + +bool +raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed, + enum spdk_bdev_io_status status); +void +raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev, + struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn); +void +raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status); + +#endif /* SPDK_BDEV_RAID_INTERNAL_H */ diff --git a/src/spdk/module/bdev/raid/bdev_raid_rpc.c b/src/spdk/module/bdev/raid/bdev_raid_rpc.c new file mode 100644 index 000000000..1c2d070c3 --- /dev/null +++ b/src/spdk/module/bdev/raid/bdev_raid_rpc.c @@ -0,0 +1,452 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/rpc.h" +#include "spdk/bdev.h" +#include "bdev_raid.h" +#include "spdk/util.h" +#include "spdk/string.h" +#include "spdk_internal/log.h" +#include "spdk/env.h" + +#define RPC_MAX_BASE_BDEVS 255 + +SPDK_LOG_REGISTER_COMPONENT("raidrpc", SPDK_LOG_RAID_RPC) + +/* + * Input structure for bdev_raid_get_bdevs RPC + */ +struct rpc_bdev_raid_get_bdevs { + /* category - all or online or configuring or offline */ + char *category; +}; + +/* + * brief: + * free_rpc_bdev_raid_get_bdevs function frees RPC bdev_raid_get_bdevs related parameters + * params: + * req - pointer to RPC request + * returns: + * none + */ +static void +free_rpc_bdev_raid_get_bdevs(struct rpc_bdev_raid_get_bdevs *req) +{ + free(req->category); +} + +/* + * Decoder object for RPC get_raids + */ +static const struct spdk_json_object_decoder rpc_bdev_raid_get_bdevs_decoders[] = { + {"category", offsetof(struct rpc_bdev_raid_get_bdevs, category), spdk_json_decode_string}, +}; + +/* + * brief: + * rpc_bdev_raid_get_bdevs function is the RPC for rpc_bdev_raid_get_bdevs. This is used to list + * all the raid bdev names based on the input category requested. Category should be + * one of "all", "online", "configuring" or "offline". "all" means all the raids + * whether they are online or configuring or offline. "online" is the raid bdev which + * is registered with bdev layer. "configuring" is the raid bdev which does not have + * full configuration discovered yet. "offline" is the raid bdev which is not + * registered with bdev as of now and it has encountered any error or user has + * requested to offline the raid. + * params: + * request - pointer to json rpc request + * params - pointer to request parameters + * returns: + * none + */ +static void +rpc_bdev_raid_get_bdevs(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct rpc_bdev_raid_get_bdevs req = {}; + struct spdk_json_write_ctx *w; + struct raid_bdev *raid_bdev; + + if (spdk_json_decode_object(params, rpc_bdev_raid_get_bdevs_decoders, + SPDK_COUNTOF(rpc_bdev_raid_get_bdevs_decoders), + &req)) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + "spdk_json_decode_object failed"); + goto cleanup; + } + + if (!(strcmp(req.category, "all") == 0 || + strcmp(req.category, "online") == 0 || + strcmp(req.category, "configuring") == 0 || + strcmp(req.category, "offline") == 0)) { + spdk_jsonrpc_send_error_response(request, -EINVAL, spdk_strerror(EINVAL)); + goto cleanup; + } + + w = spdk_jsonrpc_begin_result(request); + spdk_json_write_array_begin(w); + + /* Get raid bdev list based on the category requested */ + if (strcmp(req.category, "all") == 0) { + TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { + spdk_json_write_string(w, raid_bdev->bdev.name); + } + } else if (strcmp(req.category, "online") == 0) { + TAILQ_FOREACH(raid_bdev, &g_raid_bdev_configured_list, state_link) { + spdk_json_write_string(w, raid_bdev->bdev.name); + } + } else if (strcmp(req.category, "configuring") == 0) { + TAILQ_FOREACH(raid_bdev, &g_raid_bdev_configuring_list, state_link) { + spdk_json_write_string(w, raid_bdev->bdev.name); + } + } else { + TAILQ_FOREACH(raid_bdev, &g_raid_bdev_offline_list, state_link) { + spdk_json_write_string(w, raid_bdev->bdev.name); + } + } + spdk_json_write_array_end(w); + spdk_jsonrpc_end_result(request, w); + +cleanup: + free_rpc_bdev_raid_get_bdevs(&req); +} +SPDK_RPC_REGISTER("bdev_raid_get_bdevs", rpc_bdev_raid_get_bdevs, SPDK_RPC_RUNTIME) +SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_raid_get_bdevs, get_raid_bdevs) + +/* + * Base bdevs in RPC bdev_raid_create + */ +struct rpc_bdev_raid_create_base_bdevs { + /* Number of base bdevs */ + size_t num_base_bdevs; + + /* List of base bdevs names */ + char *base_bdevs[RPC_MAX_BASE_BDEVS]; +}; + +/* + * Input structure for RPC rpc_bdev_raid_create + */ +struct rpc_bdev_raid_create { + /* Raid bdev name */ + char *name; + + /* RAID strip size KB, 'strip_size' is deprecated. */ + uint32_t strip_size; + uint32_t strip_size_kb; + + /* RAID raid level */ + enum raid_level level; + + /* Base bdevs information */ + struct rpc_bdev_raid_create_base_bdevs base_bdevs; +}; + +/* + * brief: + * free_rpc_bdev_raid_create function is to free RPC bdev_raid_create related parameters + * params: + * req - pointer to RPC request + * returns: + * none + */ +static void +free_rpc_bdev_raid_create(struct rpc_bdev_raid_create *req) +{ + size_t i; + + free(req->name); + for (i = 0; i < req->base_bdevs.num_base_bdevs; i++) { + free(req->base_bdevs.base_bdevs[i]); + } +} + +/* + * Decoder function for RPC bdev_raid_create to decode raid level + */ +static int +decode_raid_level(const struct spdk_json_val *val, void *out) +{ + int ret; + char *str = NULL; + enum raid_level level; + + ret = spdk_json_decode_string(val, &str); + if (ret == 0 && str != NULL) { + level = raid_bdev_parse_raid_level(str); + if (level == INVALID_RAID_LEVEL) { + ret = -EINVAL; + } else { + *(enum raid_level *)out = level; + } + } + + free(str); + return ret; +} + +/* + * Decoder function for RPC bdev_raid_create to decode base bdevs list + */ +static int +decode_base_bdevs(const struct spdk_json_val *val, void *out) +{ + struct rpc_bdev_raid_create_base_bdevs *base_bdevs = out; + return spdk_json_decode_array(val, spdk_json_decode_string, base_bdevs->base_bdevs, + RPC_MAX_BASE_BDEVS, &base_bdevs->num_base_bdevs, sizeof(char *)); +} + +/* + * Decoder object for RPC bdev_raid_create + */ +/* Note: strip_size is deprecated, one of the two options must be specified but not both. */ +static const struct spdk_json_object_decoder rpc_bdev_raid_create_decoders[] = { + {"name", offsetof(struct rpc_bdev_raid_create, name), spdk_json_decode_string}, + {"strip_size", offsetof(struct rpc_bdev_raid_create, strip_size), spdk_json_decode_uint32, true}, + {"strip_size_kb", offsetof(struct rpc_bdev_raid_create, strip_size_kb), spdk_json_decode_uint32, true}, + {"raid_level", offsetof(struct rpc_bdev_raid_create, level), decode_raid_level}, + {"base_bdevs", offsetof(struct rpc_bdev_raid_create, base_bdevs), decode_base_bdevs}, +}; + +/* + * brief: + * rpc_bdev_raid_create function is the RPC for creating RAID bdevs. It takes + * input as raid bdev name, raid level, strip size in KB and list of base bdev names. + * params: + * request - pointer to json rpc request + * params - pointer to request parameters + * returns: + * none + */ +static void +rpc_bdev_raid_create(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct rpc_bdev_raid_create req = {}; + struct spdk_json_write_ctx *w; + struct raid_bdev_config *raid_cfg; + int rc; + size_t i; + + if (spdk_json_decode_object(params, rpc_bdev_raid_create_decoders, + SPDK_COUNTOF(rpc_bdev_raid_create_decoders), + &req)) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + "spdk_json_decode_object failed"); + goto cleanup; + } + + if (req.strip_size == 0 && req.strip_size_kb == 0) { + spdk_jsonrpc_send_error_response(request, EINVAL, "strip size not specified"); + goto cleanup; + } else if (req.strip_size > 0 && req.strip_size_kb > 0) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, + "please use only one strip size option"); + goto cleanup; + } else if (req.strip_size > 0 && req.strip_size_kb == 0) { + SPDK_ERRLOG("the rpc param strip_size is deprecated.\n"); + req.strip_size_kb = req.strip_size; + } + + rc = raid_bdev_config_add(req.name, req.strip_size_kb, req.base_bdevs.num_base_bdevs, + req.level, + &raid_cfg); + if (rc != 0) { + spdk_jsonrpc_send_error_response_fmt(request, rc, + "Failed to add RAID bdev config %s: %s", + req.name, spdk_strerror(-rc)); + goto cleanup; + } + + for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) { + rc = raid_bdev_config_add_base_bdev(raid_cfg, req.base_bdevs.base_bdevs[i], i); + if (rc != 0) { + raid_bdev_config_cleanup(raid_cfg); + spdk_jsonrpc_send_error_response_fmt(request, rc, + "Failed to add base bdev %s to RAID bdev config %s: %s", + req.base_bdevs.base_bdevs[i], req.name, + spdk_strerror(-rc)); + goto cleanup; + } + } + + rc = raid_bdev_create(raid_cfg); + if (rc != 0) { + raid_bdev_config_cleanup(raid_cfg); + spdk_jsonrpc_send_error_response_fmt(request, rc, + "Failed to create RAID bdev %s: %s", + req.name, spdk_strerror(-rc)); + goto cleanup; + } + + rc = raid_bdev_add_base_devices(raid_cfg); + if (rc != 0) { + spdk_jsonrpc_send_error_response_fmt(request, rc, + "Failed to add any base bdev to RAID bdev %s: %s", + req.name, spdk_strerror(-rc)); + goto cleanup; + } + + w = spdk_jsonrpc_begin_result(request); + spdk_json_write_bool(w, true); + spdk_jsonrpc_end_result(request, w); + +cleanup: + free_rpc_bdev_raid_create(&req); +} +SPDK_RPC_REGISTER("bdev_raid_create", rpc_bdev_raid_create, SPDK_RPC_RUNTIME) +SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_raid_create, construct_raid_bdev) + +/* + * Input structure for RPC deleting a raid bdev + */ +struct rpc_bdev_raid_delete { + /* raid bdev name */ + char *name; +}; + +/* + * brief: + * free_rpc_bdev_raid_delete function is used to free RPC bdev_raid_delete related parameters + * params: + * req - pointer to RPC request + * params: + * none + */ +static void +free_rpc_bdev_raid_delete(struct rpc_bdev_raid_delete *req) +{ + free(req->name); +} + +/* + * Decoder object for RPC raid_bdev_delete + */ +static const struct spdk_json_object_decoder rpc_bdev_raid_delete_decoders[] = { + {"name", offsetof(struct rpc_bdev_raid_delete, name), spdk_json_decode_string}, +}; + +struct rpc_bdev_raid_delete_ctx { + struct rpc_bdev_raid_delete req; + struct raid_bdev_config *raid_cfg; + struct spdk_jsonrpc_request *request; +}; + +/* + * brief: + * params: + * cb_arg - pointer to the callback context. + * rc - return code of the deletion of the raid bdev. + * returns: + * none + */ +static void +bdev_raid_delete_done(void *cb_arg, int rc) +{ + struct rpc_bdev_raid_delete_ctx *ctx = cb_arg; + struct raid_bdev_config *raid_cfg; + struct spdk_jsonrpc_request *request = ctx->request; + struct spdk_json_write_ctx *w; + + if (rc != 0) { + SPDK_ERRLOG("Failed to delete raid bdev %s (%d): %s\n", + ctx->req.name, rc, spdk_strerror(-rc)); + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + spdk_strerror(-rc)); + goto exit; + } + + raid_cfg = ctx->raid_cfg; + assert(raid_cfg->raid_bdev == NULL); + + raid_bdev_config_cleanup(raid_cfg); + + w = spdk_jsonrpc_begin_result(request); + spdk_json_write_bool(w, true); + spdk_jsonrpc_end_result(request, w); +exit: + free_rpc_bdev_raid_delete(&ctx->req); + free(ctx); +} + +/* + * brief: + * rpc_bdev_raid_delete function is the RPC for deleting a raid bdev. It takes raid + * name as input and delete that raid bdev including freeing the base bdev + * resources. + * params: + * request - pointer to json rpc request + * params - pointer to request parameters + * returns: + * none + */ +static void +rpc_bdev_raid_delete(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct rpc_bdev_raid_delete_ctx *ctx; + + ctx = calloc(1, sizeof(*ctx)); + if (!ctx) { + spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); + return; + } + + if (spdk_json_decode_object(params, rpc_bdev_raid_delete_decoders, + SPDK_COUNTOF(rpc_bdev_raid_delete_decoders), + &ctx->req)) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + "spdk_json_decode_object failed"); + goto cleanup; + } + + ctx->raid_cfg = raid_bdev_config_find_by_name(ctx->req.name); + if (ctx->raid_cfg == NULL) { + spdk_jsonrpc_send_error_response_fmt(request, ENODEV, + "raid bdev %s is not found in config", + ctx->req.name); + goto cleanup; + } + + ctx->request = request; + + /* Remove all the base bdevs from this raid bdev before deleting the raid bdev */ + raid_bdev_remove_base_devices(ctx->raid_cfg, bdev_raid_delete_done, ctx); + + return; + +cleanup: + free_rpc_bdev_raid_delete(&ctx->req); + free(ctx); +} +SPDK_RPC_REGISTER("bdev_raid_delete", rpc_bdev_raid_delete, SPDK_RPC_RUNTIME) +SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_raid_delete, destroy_raid_bdev) diff --git a/src/spdk/module/bdev/raid/raid0.c b/src/spdk/module/bdev/raid/raid0.c new file mode 100644 index 000000000..5632c5b7c --- /dev/null +++ b/src/spdk/module/bdev/raid/raid0.c @@ -0,0 +1,398 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "bdev_raid.h" + +#include "spdk/env.h" +#include "spdk/thread.h" +#include "spdk/string.h" +#include "spdk/util.h" + +#include "spdk_internal/log.h" + +/* + * brief: + * raid0_bdev_io_completion function is called by lower layers to notify raid + * module that particular bdev_io is completed. + * params: + * bdev_io - pointer to bdev io submitted to lower layers, like child io + * success - bdev_io status + * cb_arg - function callback context (parent raid_bdev_io) + * returns: + * none + */ +static void +raid0_bdev_io_completion(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) +{ + struct raid_bdev_io *raid_io = cb_arg; + + spdk_bdev_free_io(bdev_io); + + if (success) { + raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_SUCCESS); + } else { + raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); + } +} + +static void +raid0_submit_rw_request(struct raid_bdev_io *raid_io); + +static void +_raid0_submit_rw_request(void *_raid_io) +{ + struct raid_bdev_io *raid_io = _raid_io; + + raid0_submit_rw_request(raid_io); +} + +/* + * brief: + * raid0_submit_rw_request function is used to submit I/O to the correct + * member disk for raid0 bdevs. + * params: + * raid_io + * returns: + * none + */ +static void +raid0_submit_rw_request(struct raid_bdev_io *raid_io) +{ + struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io); + struct raid_bdev_io_channel *raid_ch = raid_io->raid_ch; + struct raid_bdev *raid_bdev = raid_io->raid_bdev; + uint64_t pd_strip; + uint32_t offset_in_strip; + uint64_t pd_lba; + uint64_t pd_blocks; + uint8_t pd_idx; + int ret = 0; + uint64_t start_strip; + uint64_t end_strip; + struct raid_base_bdev_info *base_info; + struct spdk_io_channel *base_ch; + + start_strip = bdev_io->u.bdev.offset_blocks >> raid_bdev->strip_size_shift; + end_strip = (bdev_io->u.bdev.offset_blocks + bdev_io->u.bdev.num_blocks - 1) >> + raid_bdev->strip_size_shift; + if (start_strip != end_strip && raid_bdev->num_base_bdevs > 1) { + assert(false); + SPDK_ERRLOG("I/O spans strip boundary!\n"); + raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); + return; + } + + pd_strip = start_strip / raid_bdev->num_base_bdevs; + pd_idx = start_strip % raid_bdev->num_base_bdevs; + offset_in_strip = bdev_io->u.bdev.offset_blocks & (raid_bdev->strip_size - 1); + pd_lba = (pd_strip << raid_bdev->strip_size_shift) + offset_in_strip; + pd_blocks = bdev_io->u.bdev.num_blocks; + base_info = &raid_bdev->base_bdev_info[pd_idx]; + if (base_info->desc == NULL) { + SPDK_ERRLOG("base bdev desc null for pd_idx %u\n", pd_idx); + assert(0); + } + + /* + * Submit child io to bdev layer with using base bdev descriptors, base + * bdev lba, base bdev child io length in blocks, buffer, completion + * function and function callback context + */ + assert(raid_ch != NULL); + assert(raid_ch->base_channel); + base_ch = raid_ch->base_channel[pd_idx]; + if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) { + ret = spdk_bdev_readv_blocks(base_info->desc, base_ch, + bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, + pd_lba, pd_blocks, raid0_bdev_io_completion, + raid_io); + } else if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) { + ret = spdk_bdev_writev_blocks(base_info->desc, base_ch, + bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, + pd_lba, pd_blocks, raid0_bdev_io_completion, + raid_io); + } else { + SPDK_ERRLOG("Recvd not supported io type %u\n", bdev_io->type); + assert(0); + } + + if (ret == -ENOMEM) { + raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch, + _raid0_submit_rw_request); + } else if (ret != 0) { + SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n"); + assert(false); + raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); + } +} + +/* raid0 IO range */ +struct raid_bdev_io_range { + uint64_t strip_size; + uint64_t start_strip_in_disk; + uint64_t end_strip_in_disk; + uint64_t start_offset_in_strip; + uint64_t end_offset_in_strip; + uint8_t start_disk; + uint8_t end_disk; + uint8_t n_disks_involved; +}; + +static inline void +_raid0_get_io_range(struct raid_bdev_io_range *io_range, + uint8_t num_base_bdevs, uint64_t strip_size, uint64_t strip_size_shift, + uint64_t offset_blocks, uint64_t num_blocks) +{ + uint64_t start_strip; + uint64_t end_strip; + + io_range->strip_size = strip_size; + + /* The start and end strip index in raid0 bdev scope */ + start_strip = offset_blocks >> strip_size_shift; + end_strip = (offset_blocks + num_blocks - 1) >> strip_size_shift; + io_range->start_strip_in_disk = start_strip / num_base_bdevs; + io_range->end_strip_in_disk = end_strip / num_base_bdevs; + + /* The first strip may have unaligned start LBA offset. + * The end strip may have unaligned end LBA offset. + * Strips between them certainly have aligned offset and length to boundaries. + */ + io_range->start_offset_in_strip = offset_blocks % strip_size; + io_range->end_offset_in_strip = (offset_blocks + num_blocks - 1) % strip_size; + + /* The base bdev indexes in which start and end strips are located */ + io_range->start_disk = start_strip % num_base_bdevs; + io_range->end_disk = end_strip % num_base_bdevs; + + /* Calculate how many base_bdevs are involved in io operation. + * Number of base bdevs involved is between 1 and num_base_bdevs. + * It will be 1 if the first strip and last strip are the same one. + */ + io_range->n_disks_involved = spdk_min((end_strip - start_strip + 1), num_base_bdevs); +} + +static inline void +_raid0_split_io_range(struct raid_bdev_io_range *io_range, uint8_t disk_idx, + uint64_t *_offset_in_disk, uint64_t *_nblocks_in_disk) +{ + uint64_t n_strips_in_disk; + uint64_t start_offset_in_disk; + uint64_t end_offset_in_disk; + uint64_t offset_in_disk; + uint64_t nblocks_in_disk; + uint64_t start_strip_in_disk; + uint64_t end_strip_in_disk; + + start_strip_in_disk = io_range->start_strip_in_disk; + if (disk_idx < io_range->start_disk) { + start_strip_in_disk += 1; + } + + end_strip_in_disk = io_range->end_strip_in_disk; + if (disk_idx > io_range->end_disk) { + end_strip_in_disk -= 1; + } + + assert(end_strip_in_disk >= start_strip_in_disk); + n_strips_in_disk = end_strip_in_disk - start_strip_in_disk + 1; + + if (disk_idx == io_range->start_disk) { + start_offset_in_disk = io_range->start_offset_in_strip; + } else { + start_offset_in_disk = 0; + } + + if (disk_idx == io_range->end_disk) { + end_offset_in_disk = io_range->end_offset_in_strip; + } else { + end_offset_in_disk = io_range->strip_size - 1; + } + + offset_in_disk = start_offset_in_disk + start_strip_in_disk * io_range->strip_size; + nblocks_in_disk = (n_strips_in_disk - 1) * io_range->strip_size + + end_offset_in_disk - start_offset_in_disk + 1; + + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID0, + "raid_bdev (strip_size 0x%lx) splits IO to base_bdev (%u) at (0x%lx, 0x%lx).\n", + io_range->strip_size, disk_idx, offset_in_disk, nblocks_in_disk); + + *_offset_in_disk = offset_in_disk; + *_nblocks_in_disk = nblocks_in_disk; +} + +static void +raid0_submit_null_payload_request(struct raid_bdev_io *raid_io); + +static void +_raid0_submit_null_payload_request(void *_raid_io) +{ + struct raid_bdev_io *raid_io = _raid_io; + + raid0_submit_null_payload_request(raid_io); +} + +static void +raid0_base_io_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) +{ + struct raid_bdev_io *raid_io = cb_arg; + + raid_bdev_io_complete_part(raid_io, 1, success ? + SPDK_BDEV_IO_STATUS_SUCCESS : + SPDK_BDEV_IO_STATUS_FAILED); + + spdk_bdev_free_io(bdev_io); +} + +/* + * brief: + * raid0_submit_null_payload_request function submits the next batch of + * io requests with range but without payload, like FLUSH and UNMAP, to member disks; + * it will submit as many as possible unless one base io request fails with -ENOMEM, + * in which case it will queue itself for later submission. + * params: + * bdev_io - pointer to parent bdev_io on raid bdev device + * returns: + * none + */ +static void +raid0_submit_null_payload_request(struct raid_bdev_io *raid_io) +{ + struct spdk_bdev_io *bdev_io; + struct raid_bdev *raid_bdev; + struct raid_bdev_io_range io_range; + int ret; + struct raid_base_bdev_info *base_info; + struct spdk_io_channel *base_ch; + + bdev_io = spdk_bdev_io_from_ctx(raid_io); + raid_bdev = raid_io->raid_bdev; + + _raid0_get_io_range(&io_range, raid_bdev->num_base_bdevs, + raid_bdev->strip_size, raid_bdev->strip_size_shift, + bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks); + + if (raid_io->base_bdev_io_remaining == 0) { + raid_io->base_bdev_io_remaining = io_range.n_disks_involved; + } + + while (raid_io->base_bdev_io_submitted < io_range.n_disks_involved) { + uint8_t disk_idx; + uint64_t offset_in_disk; + uint64_t nblocks_in_disk; + + /* base_bdev is started from start_disk to end_disk. + * It is possible that index of start_disk is larger than end_disk's. + */ + disk_idx = (io_range.start_disk + raid_io->base_bdev_io_submitted) % raid_bdev->num_base_bdevs; + base_info = &raid_bdev->base_bdev_info[disk_idx]; + base_ch = raid_io->raid_ch->base_channel[disk_idx]; + + _raid0_split_io_range(&io_range, disk_idx, &offset_in_disk, &nblocks_in_disk); + + switch (bdev_io->type) { + case SPDK_BDEV_IO_TYPE_UNMAP: + ret = spdk_bdev_unmap_blocks(base_info->desc, base_ch, + offset_in_disk, nblocks_in_disk, + raid0_base_io_complete, raid_io); + break; + + case SPDK_BDEV_IO_TYPE_FLUSH: + ret = spdk_bdev_flush_blocks(base_info->desc, base_ch, + offset_in_disk, nblocks_in_disk, + raid0_base_io_complete, raid_io); + break; + + default: + SPDK_ERRLOG("submit request, invalid io type with null payload %u\n", bdev_io->type); + assert(false); + ret = -EIO; + } + + if (ret == 0) { + raid_io->base_bdev_io_submitted++; + } else if (ret == -ENOMEM) { + raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch, + _raid0_submit_null_payload_request); + return; + } else { + SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n"); + assert(false); + raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); + return; + } + } +} + +static int raid0_start(struct raid_bdev *raid_bdev) +{ + uint64_t min_blockcnt = UINT64_MAX; + struct raid_base_bdev_info *base_info; + + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + /* Calculate minimum block count from all base bdevs */ + min_blockcnt = spdk_min(min_blockcnt, base_info->bdev->blockcnt); + } + + /* + * Take the minimum block count based approach where total block count + * of raid bdev is the number of base bdev times the minimum block count + * of any base bdev. + */ + SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID0, "min blockcount %lu, numbasedev %u, strip size shift %u\n", + min_blockcnt, raid_bdev->num_base_bdevs, raid_bdev->strip_size_shift); + raid_bdev->bdev.blockcnt = ((min_blockcnt >> raid_bdev->strip_size_shift) << + raid_bdev->strip_size_shift) * raid_bdev->num_base_bdevs; + + if (raid_bdev->num_base_bdevs > 1) { + raid_bdev->bdev.optimal_io_boundary = raid_bdev->strip_size; + raid_bdev->bdev.split_on_optimal_io_boundary = true; + } else { + /* Do not need to split reads/writes on single bdev RAID modules. */ + raid_bdev->bdev.optimal_io_boundary = 0; + raid_bdev->bdev.split_on_optimal_io_boundary = false; + } + + return 0; +} + +static struct raid_bdev_module g_raid0_module = { + .level = RAID0, + .base_bdevs_min = 1, + .start = raid0_start, + .submit_rw_request = raid0_submit_rw_request, + .submit_null_payload_request = raid0_submit_null_payload_request, +}; +RAID_MODULE_REGISTER(&g_raid0_module) + +SPDK_LOG_REGISTER_COMPONENT("bdev_raid0", SPDK_LOG_BDEV_RAID0) diff --git a/src/spdk/module/bdev/raid/raid5.c b/src/spdk/module/bdev/raid/raid5.c new file mode 100644 index 000000000..1e287c863 --- /dev/null +++ b/src/spdk/module/bdev/raid/raid5.c @@ -0,0 +1,114 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "bdev_raid.h" + +#include "spdk/env.h" +#include "spdk/thread.h" +#include "spdk/string.h" +#include "spdk/util.h" + +#include "spdk_internal/log.h" + +struct raid5_info { + /* The parent raid bdev */ + struct raid_bdev *raid_bdev; + + /* Number of data blocks in a stripe (without parity) */ + uint64_t stripe_blocks; + + /* Number of stripes on this array */ + uint64_t total_stripes; +}; + +static inline uint8_t +raid5_stripe_data_chunks_num(const struct raid_bdev *raid_bdev) +{ + return raid_bdev->num_base_bdevs - raid_bdev->module->base_bdevs_max_degraded; +} + +static void +raid5_submit_rw_request(struct raid_bdev_io *raid_io) +{ + raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); +} + +static int +raid5_start(struct raid_bdev *raid_bdev) +{ + uint64_t min_blockcnt = UINT64_MAX; + struct raid_base_bdev_info *base_info; + struct raid5_info *r5info; + + r5info = calloc(1, sizeof(*r5info)); + if (!r5info) { + SPDK_ERRLOG("Failed to allocate r5info\n"); + return -ENOMEM; + } + r5info->raid_bdev = raid_bdev; + + RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { + min_blockcnt = spdk_min(min_blockcnt, base_info->bdev->blockcnt); + } + + r5info->total_stripes = min_blockcnt / raid_bdev->strip_size; + r5info->stripe_blocks = raid_bdev->strip_size * raid5_stripe_data_chunks_num(raid_bdev); + + raid_bdev->bdev.blockcnt = r5info->stripe_blocks * r5info->total_stripes; + raid_bdev->bdev.optimal_io_boundary = r5info->stripe_blocks; + raid_bdev->bdev.split_on_optimal_io_boundary = true; + + raid_bdev->module_private = r5info; + + return 0; +} + +static void +raid5_stop(struct raid_bdev *raid_bdev) +{ + struct raid5_info *r5info = raid_bdev->module_private; + + free(r5info); +} + +static struct raid_bdev_module g_raid5_module = { + .level = RAID5, + .base_bdevs_min = 3, + .base_bdevs_max_degraded = 1, + .start = raid5_start, + .stop = raid5_stop, + .submit_rw_request = raid5_submit_rw_request, +}; +RAID_MODULE_REGISTER(&g_raid5_module) + +SPDK_LOG_REGISTER_COMPONENT("bdev_raid5", SPDK_LOG_BDEV_RAID5) |