diff options
Diffstat (limited to 'src/spdk/lib/bdev/split')
-rw-r--r-- | src/spdk/lib/bdev/split/Makefile | 40 | ||||
-rw-r--r-- | src/spdk/lib/bdev/split/vbdev_split.c | 565 | ||||
-rw-r--r-- | src/spdk/lib/bdev/split/vbdev_split.h | 68 | ||||
-rw-r--r-- | src/spdk/lib/bdev/split/vbdev_split_rpc.c | 151 |
4 files changed, 824 insertions, 0 deletions
diff --git a/src/spdk/lib/bdev/split/Makefile b/src/spdk/lib/bdev/split/Makefile new file mode 100644 index 00000000..46edf89a --- /dev/null +++ b/src/spdk/lib/bdev/split/Makefile @@ -0,0 +1,40 @@ +# +# 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 + +C_SRCS = vbdev_split.c vbdev_split_rpc.c +LIBNAME = vbdev_split + +include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/src/spdk/lib/bdev/split/vbdev_split.c b/src/spdk/lib/bdev/split/vbdev_split.c new file mode 100644 index 00000000..97f11984 --- /dev/null +++ b/src/spdk/lib/bdev/split/vbdev_split.c @@ -0,0 +1,565 @@ +/*- + * 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. + */ + +/* + * This is a simple example of a virtual block device that takes a single + * bdev and slices it into multiple smaller bdevs. + */ + +#include "vbdev_split.h" + +#include "spdk/rpc.h" +#include "spdk/conf.h" +#include "spdk/endian.h" +#include "spdk/string.h" +#include "spdk/thread.h" +#include "spdk/util.h" + +#include "spdk/bdev_module.h" +#include "spdk_internal/log.h" + +struct spdk_vbdev_split_config { + char *base_bdev; + unsigned split_count; + uint64_t split_size_mb; + + struct spdk_bdev_part_base *split_base; + bool removed; + + TAILQ_ENTRY(spdk_vbdev_split_config) tailq; +}; + +static TAILQ_HEAD(, spdk_vbdev_split_config) g_split_config = TAILQ_HEAD_INITIALIZER( + g_split_config); +static SPDK_BDEV_PART_TAILQ g_split_disks = TAILQ_HEAD_INITIALIZER(g_split_disks); + +struct vbdev_split_channel { + struct spdk_bdev_part_channel part_ch; +}; + +struct vbdev_split_bdev_io { + struct spdk_io_channel *ch; + struct spdk_bdev_io *bdev_io; + + /* for bdev_io_wait */ + struct spdk_bdev_io_wait_entry bdev_io_wait; +}; + +static void vbdev_split_del_config(struct spdk_vbdev_split_config *cfg); + +static int vbdev_split_init(void); +static void vbdev_split_fini(void); +static void vbdev_split_examine(struct spdk_bdev *bdev); +static int vbdev_split_config_json(struct spdk_json_write_ctx *w); +static int vbdev_split_get_ctx_size(void); + +static void +vbdev_split_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io); + +static struct spdk_bdev_module split_if = { + .name = "split", + .module_init = vbdev_split_init, + .module_fini = vbdev_split_fini, + .get_ctx_size = vbdev_split_get_ctx_size, + .examine_config = vbdev_split_examine, + .config_json = vbdev_split_config_json, +}; + +SPDK_BDEV_MODULE_REGISTER(&split_if) + +static void +vbdev_split_base_free(void *ctx) +{ + struct spdk_vbdev_split_config *cfg = ctx; + + cfg->split_base = NULL; + if (cfg->removed) { + vbdev_split_del_config(cfg); + } +} + +static int +vbdev_split_destruct(void *ctx) +{ + struct spdk_bdev_part *part = ctx; + + return spdk_bdev_part_free(part); +} + +static void +vbdev_split_base_bdev_hotremove_cb(void *_base_bdev) +{ + spdk_bdev_part_base_hotremove(_base_bdev, &g_split_disks); +} + +static void +vbdev_split_resubmit_io(void *arg) +{ + struct vbdev_split_bdev_io *split_io = (struct vbdev_split_bdev_io *)arg; + + vbdev_split_submit_request(split_io->ch, split_io->bdev_io); +} + +static void +vbdev_split_queue_io(struct vbdev_split_bdev_io *split_io) +{ + int rc; + + split_io->bdev_io_wait.bdev = split_io->bdev_io->bdev; + split_io->bdev_io_wait.cb_fn = vbdev_split_resubmit_io; + split_io->bdev_io_wait.cb_arg = split_io; + + rc = spdk_bdev_queue_io_wait(split_io->bdev_io->bdev, + split_io->ch, &split_io->bdev_io_wait); + if (rc != 0) { + SPDK_ERRLOG("Queue io failed in vbdev_split_queue_io, rc=%d\n", rc); + spdk_bdev_io_complete(split_io->bdev_io, SPDK_BDEV_IO_STATUS_FAILED); + } +} + +static void +vbdev_split_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) +{ + struct vbdev_split_channel *ch = spdk_io_channel_get_ctx(_ch); + struct vbdev_split_bdev_io *io_ctx = (struct vbdev_split_bdev_io *)bdev_io->driver_ctx; + int rc; + + rc = spdk_bdev_part_submit_request(&ch->part_ch, bdev_io); + if (rc) { + if (rc == -ENOMEM) { + SPDK_DEBUGLOG(SPDK_LOG_VBDEV_SPLIT, "split: no memory, queue io.\n"); + io_ctx->ch = _ch; + io_ctx->bdev_io = bdev_io; + vbdev_split_queue_io(io_ctx); + } else { + SPDK_ERRLOG("split: error on io submission, rc=%d.\n", rc); + spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); + } + } +} + +static int +vbdev_split_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) +{ + struct spdk_bdev_part *part = ctx; + struct spdk_bdev *split_base_bdev = spdk_bdev_part_get_base_bdev(part); + uint64_t offset_blocks = spdk_bdev_part_get_offset_blocks(part); + + spdk_json_write_name(w, "split"); + spdk_json_write_object_begin(w); + + spdk_json_write_name(w, "base_bdev"); + spdk_json_write_string(w, spdk_bdev_get_name(split_base_bdev)); + spdk_json_write_name(w, "offset_blocks"); + spdk_json_write_uint64(w, offset_blocks); + + spdk_json_write_object_end(w); + + return 0; +} + +static void +vbdev_split_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) +{ + /* No config per bdev needed */ +} + +static struct spdk_bdev_fn_table vbdev_split_fn_table = { + .destruct = vbdev_split_destruct, + .submit_request = vbdev_split_submit_request, + .dump_info_json = vbdev_split_dump_info_json, + .write_config_json = vbdev_split_write_config_json +}; + +static int +vbdev_split_create(struct spdk_vbdev_split_config *cfg) +{ + uint64_t split_size_blocks, offset_blocks; + uint64_t split_count, max_split_count; + uint64_t mb = 1024 * 1024; + uint64_t i; + int rc; + char *name; + struct spdk_bdev *base_bdev; + struct spdk_bdev *split_base_bdev; + struct bdev_part_tailq *split_base_tailq; + + assert(cfg->split_count > 0); + + base_bdev = spdk_bdev_get_by_name(cfg->base_bdev); + if (!base_bdev) { + return -ENODEV; + } + + if (cfg->split_size_mb) { + if (((cfg->split_size_mb * mb) % base_bdev->blocklen) != 0) { + SPDK_ERRLOG("Split size %" PRIu64 " MB is not possible with block size " + "%" PRIu32 "\n", + cfg->split_size_mb, base_bdev->blocklen); + return -EINVAL; + } + split_size_blocks = (cfg->split_size_mb * mb) / base_bdev->blocklen; + SPDK_DEBUGLOG(SPDK_LOG_VBDEV_SPLIT, "Split size %" PRIu64 " MB specified by user\n", + cfg->split_size_mb); + } else { + split_size_blocks = base_bdev->blockcnt / cfg->split_count; + SPDK_DEBUGLOG(SPDK_LOG_VBDEV_SPLIT, "Split size not specified by user\n"); + } + + max_split_count = base_bdev->blockcnt / split_size_blocks; + split_count = cfg->split_count; + if (split_count > max_split_count) { + SPDK_WARNLOG("Split count %" PRIu64 " is greater than maximum possible split count " + "%" PRIu64 " - clamping\n", split_count, max_split_count); + split_count = max_split_count; + } + + SPDK_DEBUGLOG(SPDK_LOG_VBDEV_SPLIT, "base_bdev: %s split_count: %" PRIu64 + " split_size_blocks: %" PRIu64 "\n", + spdk_bdev_get_name(base_bdev), split_count, split_size_blocks); + + cfg->split_base = spdk_bdev_part_base_construct(base_bdev, + vbdev_split_base_bdev_hotremove_cb, + &split_if, &vbdev_split_fn_table, + &g_split_disks, vbdev_split_base_free, cfg, + sizeof(struct vbdev_split_channel), NULL, NULL); + if (!cfg->split_base) { + SPDK_ERRLOG("Cannot construct bdev part base\n"); + return -ENOMEM; + } + + offset_blocks = 0; + for (i = 0; i < split_count; i++) { + struct spdk_bdev_part *d; + + d = calloc(1, sizeof(*d)); + if (d == NULL) { + SPDK_ERRLOG("could not allocate bdev part\n"); + rc = -ENOMEM; + goto err; + } + + name = spdk_sprintf_alloc("%sp%" PRIu64, cfg->base_bdev, i); + if (!name) { + SPDK_ERRLOG("could not allocate name\n"); + free(d); + rc = -ENOMEM; + goto err; + } + + rc = spdk_bdev_part_construct(d, cfg->split_base, name, offset_blocks, split_size_blocks, + "Split Disk"); + free(name); + if (rc) { + SPDK_ERRLOG("could not construct bdev part\n"); + /* spdk_bdev_part_construct will free name if it fails */ + free(d); + rc = -ENOMEM; + goto err; + } + + offset_blocks += split_size_blocks; + } + + return 0; +err: + split_base_bdev = spdk_bdev_part_base_get_bdev(cfg->split_base); + split_base_tailq = spdk_bdev_part_base_get_tailq(cfg->split_base); + cfg->removed = true; + spdk_bdev_part_base_hotremove(split_base_bdev, split_base_tailq); + return rc; +} + +static void +vbdev_split_del_config(struct spdk_vbdev_split_config *cfg) +{ + TAILQ_REMOVE(&g_split_config, cfg, tailq); + free(cfg->base_bdev); + free(cfg); +} + +static void +vbdev_split_destruct_config(struct spdk_vbdev_split_config *cfg) +{ + struct spdk_bdev *split_base_bdev; + struct bdev_part_tailq *split_base_tailq; + + cfg->removed = true; + if (cfg->split_base != NULL) { + split_base_bdev = spdk_bdev_part_base_get_bdev(cfg->split_base); + split_base_tailq = spdk_bdev_part_base_get_tailq(cfg->split_base); + spdk_bdev_part_base_hotremove(split_base_bdev, split_base_tailq); + } else { + vbdev_split_del_config(cfg); + } +} + +static void +vbdev_split_clear_config(void) +{ + struct spdk_vbdev_split_config *cfg, *tmp_cfg; + + TAILQ_FOREACH_SAFE(cfg, &g_split_config, tailq, tmp_cfg) { + vbdev_split_destruct_config(cfg); + } +} + +static struct spdk_vbdev_split_config * +vbdev_split_config_find_by_base_name(const char *base_bdev_name) +{ + struct spdk_vbdev_split_config *cfg; + + TAILQ_FOREACH(cfg, &g_split_config, tailq) { + if (strcmp(cfg->base_bdev, base_bdev_name) == 0) { + return cfg; + } + } + + return NULL; +} + +static int +vbdev_split_add_config(const char *base_bdev_name, unsigned split_count, uint64_t split_size, + struct spdk_vbdev_split_config **config) +{ + struct spdk_vbdev_split_config *cfg; + assert(base_bdev_name); + + if (base_bdev_name == NULL) { + SPDK_ERRLOG("Split bdev config: no base bdev provided."); + return -EINVAL; + } + + if (split_count == 0) { + SPDK_ERRLOG("Split bdev config: split_count can't be 0."); + return -EINVAL; + } + + /* Check if we already have 'base_bdev_name' registered in config */ + cfg = vbdev_split_config_find_by_base_name(base_bdev_name); + if (cfg) { + SPDK_ERRLOG("Split bdev config for base bdev '%s' already exist.", base_bdev_name); + return -EEXIST; + } + + cfg = calloc(1, sizeof(*cfg)); + if (!cfg) { + SPDK_ERRLOG("calloc(): Out of memory"); + return -ENOMEM; + } + + cfg->base_bdev = strdup(base_bdev_name); + if (!cfg->base_bdev) { + SPDK_ERRLOG("strdup(): Out of memory"); + free(cfg); + return -ENOMEM; + } + + cfg->split_count = split_count; + cfg->split_size_mb = split_size; + TAILQ_INSERT_TAIL(&g_split_config, cfg, tailq); + if (config) { + *config = cfg; + } + + return 0; +} + +static int +vbdev_split_init(void) +{ + + struct spdk_conf_section *sp; + const char *base_bdev_name; + const char *split_count_str; + const char *split_size_str; + int rc, i, split_count, split_size; + + sp = spdk_conf_find_section(NULL, "Split"); + if (sp == NULL) { + return 0; + } + + for (i = 0; ; i++) { + if (!spdk_conf_section_get_nval(sp, "Split", i)) { + break; + } + + base_bdev_name = spdk_conf_section_get_nmval(sp, "Split", i, 0); + if (!base_bdev_name) { + SPDK_ERRLOG("Split configuration missing bdev name\n"); + rc = -EINVAL; + goto err; + } + + split_count_str = spdk_conf_section_get_nmval(sp, "Split", i, 1); + if (!split_count_str) { + SPDK_ERRLOG("Split configuration missing split count\n"); + rc = -EINVAL; + goto err; + } + + split_count = atoi(split_count_str); + if (split_count < 1) { + SPDK_ERRLOG("Invalid Split count %d\n", split_count); + rc = -EINVAL; + goto err; + } + + /* Optional split size in MB */ + split_size = 0; + split_size_str = spdk_conf_section_get_nmval(sp, "Split", i, 2); + if (split_size_str) { + split_size = atoi(split_size_str); + if (split_size <= 0) { + SPDK_ERRLOG("Invalid Split size %d\n", split_size); + rc = -EINVAL; + goto err; + } + } + + rc = vbdev_split_add_config(base_bdev_name, split_count, split_size, NULL); + if (rc != 0) { + goto err; + } + } + + return 0; +err: + vbdev_split_clear_config(); + return rc; +} + +static void +vbdev_split_fini(void) +{ + vbdev_split_clear_config(); +} + +static void +vbdev_split_examine(struct spdk_bdev *bdev) +{ + struct spdk_vbdev_split_config *cfg = vbdev_split_config_find_by_base_name(bdev->name); + + if (cfg != NULL && cfg->removed == false) { + assert(cfg->split_base == NULL); + + if (vbdev_split_create(cfg)) { + SPDK_ERRLOG("could not split bdev %s\n", bdev->name); + } + } + spdk_bdev_module_examine_done(&split_if); +} + +static int +vbdev_split_config_json(struct spdk_json_write_ctx *w) +{ + struct spdk_vbdev_split_config *cfg; + + TAILQ_FOREACH(cfg, &g_split_config, tailq) { + spdk_json_write_object_begin(w); + + spdk_json_write_named_string(w, "method", "construct_split_vbdev"); + + spdk_json_write_named_object_begin(w, "params"); + spdk_json_write_named_string(w, "base_bdev", cfg->base_bdev); + spdk_json_write_named_uint32(w, "split_count", cfg->split_count); + spdk_json_write_named_uint64(w, "split_size_mb", cfg->split_size_mb); + spdk_json_write_object_end(w); + + spdk_json_write_object_end(w); + } + + return 0; +} + +int +create_vbdev_split(const char *base_bdev_name, unsigned split_count, uint64_t split_size_mb) +{ + int rc; + struct spdk_vbdev_split_config *cfg; + + rc = vbdev_split_add_config(base_bdev_name, split_count, split_size_mb, &cfg); + if (rc) { + return rc; + } + + rc = vbdev_split_create(cfg); + if (rc == -ENODEV) { + /* It is ok if base bdev does not exist yet. */ + rc = 0; + } + + return rc; +} + +int +spdk_vbdev_split_destruct(const char *base_bdev_name) +{ + struct spdk_vbdev_split_config *cfg = vbdev_split_config_find_by_base_name(base_bdev_name); + + if (!cfg) { + SPDK_ERRLOG("Split configuration for '%s' not found\n", base_bdev_name); + return -ENOENT; + } + + vbdev_split_destruct_config(cfg); + return 0; +} + +struct spdk_bdev_part_base * +spdk_vbdev_split_get_part_base(struct spdk_bdev *bdev) +{ + struct spdk_vbdev_split_config *cfg; + + cfg = vbdev_split_config_find_by_base_name(spdk_bdev_get_name(bdev)); + + if (cfg == NULL) { + return NULL; + } + + return cfg->split_base; +} + +/* + * During init we'll be asked how much memory we'd like passed to us + * in bev_io structures as context. Here's where we specify how + * much context we want per IO. + */ +static int +vbdev_split_get_ctx_size(void) +{ + return sizeof(struct vbdev_split_bdev_io); +} + +SPDK_LOG_REGISTER_COMPONENT("vbdev_split", SPDK_LOG_VBDEV_SPLIT) diff --git a/src/spdk/lib/bdev/split/vbdev_split.h b/src/spdk/lib/bdev/split/vbdev_split.h new file mode 100644 index 00000000..4231d443 --- /dev/null +++ b/src/spdk/lib/bdev/split/vbdev_split.h @@ -0,0 +1,68 @@ +/*- + * 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_VBDEV_SPLIT_H +#define SPDK_VBDEV_SPLIT_H + +#include "spdk/bdev_module.h" + +/** + * Add given disk name to split config. If bdev with \c base_bdev_name name + * exist the split bdevs will be created right away, if not the split bdevs will + * be created when base bdev became be available (during examination process). + * + * \param base_bdev_name Base bdev name + * \param split_count number of splits to be created. + * \param split_size_mb size of each bdev. If 0 use base bdev size / split_count + * \return value >= 0 - number of splits create. Negative errno code on error. + */ +int create_vbdev_split(const char *base_bdev_name, unsigned split_count, uint64_t split_size_mb); + +/** + * Remove all created split bdevs and split config. + * + * \param base_bdev_name base bdev name + * \return 0 on success or negative errno value. + */ +int spdk_vbdev_split_destruct(const char *base_bdev_name); + +/** + * Get the spdk_bdev_part_base associated with the given split base_bdev. + * + * \param base_bdev Bdev to get the part_base from + * \return pointer to the associated spdk_bdev_part_base + * \return NULL if the base_bdev is not being split by the split module + */ +struct spdk_bdev_part_base *spdk_vbdev_split_get_part_base(struct spdk_bdev *base_bdev); + +#endif // SPDK_VBDEV_SPLIT_H diff --git a/src/spdk/lib/bdev/split/vbdev_split_rpc.c b/src/spdk/lib/bdev/split/vbdev_split_rpc.c new file mode 100644 index 00000000..fe70346f --- /dev/null +++ b/src/spdk/lib/bdev/split/vbdev_split_rpc.c @@ -0,0 +1,151 @@ +/*- + * 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/util.h" +#include "spdk/string.h" + +#include "vbdev_split.h" +#include "spdk_internal/log.h" + +struct rpc_construct_split { + char *base_bdev; + uint32_t split_count; + uint64_t split_size_mb; +}; + +static const struct spdk_json_object_decoder rpc_construct_split_decoders[] = { + {"base_bdev", offsetof(struct rpc_construct_split, base_bdev), spdk_json_decode_string}, + {"split_count", offsetof(struct rpc_construct_split, split_count), spdk_json_decode_uint32}, + {"split_size_mb", offsetof(struct rpc_construct_split, split_size_mb), spdk_json_decode_uint64, true}, +}; + +static void +spdk_rpc_construct_split_vbdev(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct rpc_construct_split req = {}; + struct spdk_json_write_ctx *w; + struct spdk_bdev *base_bdev; + int rc; + + if (spdk_json_decode_object(params, rpc_construct_split_decoders, + SPDK_COUNTOF(rpc_construct_split_decoders), + &req)) { + SPDK_ERRLOG("spdk_json_decode_object failed\n"); + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); + goto out; + } + + rc = create_vbdev_split(req.base_bdev, req.split_count, req.split_size_mb); + if (rc < 0) { + spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, + "Failed to create %"PRIu32" split bdevs from '%s': %s", + req.split_count, req.base_bdev, spdk_strerror(-rc)); + goto out; + } + + w = spdk_jsonrpc_begin_result(request); + if (w == NULL) { + goto out; + } + + spdk_json_write_array_begin(w); + + base_bdev = spdk_bdev_get_by_name(req.base_bdev); + if (base_bdev != NULL) { + struct spdk_bdev_part_base *split_base; + struct bdev_part_tailq *split_base_tailq; + struct spdk_bdev_part *split_part; + struct spdk_bdev *split_bdev; + + split_base = spdk_vbdev_split_get_part_base(base_bdev); + + assert(split_base != NULL); + + split_base_tailq = spdk_bdev_part_base_get_tailq(split_base); + TAILQ_FOREACH(split_part, split_base_tailq, tailq) { + split_bdev = spdk_bdev_part_get_bdev(split_part); + spdk_json_write_string(w, spdk_bdev_get_name(split_bdev)); + } + } + + spdk_json_write_array_end(w); + spdk_jsonrpc_end_result(request, w); + +out: + free(req.base_bdev); +} +SPDK_RPC_REGISTER("construct_split_vbdev", spdk_rpc_construct_split_vbdev, SPDK_RPC_RUNTIME) + +struct rpc_destruct_split { + char *base_bdev; +}; + +static const struct spdk_json_object_decoder rpc_destruct_split_decoders[] = { + {"base_bdev", offsetof(struct rpc_destruct_split, base_bdev), spdk_json_decode_string}, +}; + +static void +spdk_rpc_destruct_split(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct rpc_destruct_split req = {}; + struct spdk_json_write_ctx *w; + int rc; + + if (spdk_json_decode_object(params, rpc_destruct_split_decoders, + SPDK_COUNTOF(rpc_destruct_split_decoders), + &req)) { + SPDK_ERRLOG("spdk_json_decode_object failed\n"); + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); + goto out; + } + + rc = spdk_vbdev_split_destruct(req.base_bdev); + if (rc < 0) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc)); + goto out; + } + + w = spdk_jsonrpc_begin_result(request); + if (w == NULL) { + goto out; + } + + spdk_json_write_bool(w, true); + spdk_jsonrpc_end_result(request, w); +out: + free(req.base_bdev); +} +SPDK_RPC_REGISTER("destruct_split_vbdev", spdk_rpc_destruct_split, SPDK_RPC_RUNTIME) |