diff --git a/lib/bdev/split/Makefile b/lib/bdev/split/Makefile index a9b4dc6f7..46edf89a6 100644 --- a/lib/bdev/split/Makefile +++ b/lib/bdev/split/Makefile @@ -34,7 +34,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk -C_SRCS = vbdev_split.c +C_SRCS = vbdev_split.c vbdev_split_rpc.c LIBNAME = vbdev_split include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/lib/bdev/split/vbdev_split.c b/lib/bdev/split/vbdev_split.c index e083bd8e2..45fc28529 100644 --- a/lib/bdev/split/vbdev_split.c +++ b/lib/bdev/split/vbdev_split.c @@ -36,7 +36,7 @@ * bdev and slices it into multiple smaller bdevs. */ -#include "spdk/stdinc.h" +#include "vbdev_split.h" #include "spdk/rpc.h" #include "spdk/conf.h" @@ -53,6 +53,9 @@ struct spdk_vbdev_split_config { unsigned split_count; uint64_t split_size_mb; + struct spdk_bdev_part_base split_base; + bool removed; + TAILQ_ENTRY(spdk_vbdev_split_config) tailq; }; @@ -64,6 +67,8 @@ struct vbdev_split_channel { struct spdk_bdev_part_channel part_ch; }; +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); @@ -80,7 +85,12 @@ SPDK_BDEV_MODULE_REGISTER(&split_if) static void vbdev_split_base_free(struct spdk_bdev_part_base *base) { - free(base); + struct spdk_vbdev_split_config *cfg = SPDK_CONTAINEROF(base, struct spdk_vbdev_split_config, + split_base); + + if (cfg->removed) { + vbdev_split_del_config(cfg); + } } static int @@ -131,34 +141,40 @@ static struct spdk_bdev_fn_table vbdev_split_fn_table = { }; static int -vbdev_split_create(struct spdk_bdev *base_bdev, uint64_t split_count, uint64_t split_size_mb) +vbdev_split_create(struct spdk_vbdev_split_config *cfg) { uint64_t split_size_blocks, offset_blocks; - uint64_t max_split_count; + uint64_t split_count, max_split_count; uint64_t mb = 1024 * 1024; uint64_t i; int rc; char *name; - struct spdk_bdev_part_base *split_base; + struct spdk_bdev *base_bdev; - assert(split_count > 0); + assert(cfg->split_count > 0); - if (split_size_mb) { - if (((split_size_mb * mb) % base_bdev->blocklen) != 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", - split_size_mb, base_bdev->blocklen); - return -1; + cfg->split_size_mb, base_bdev->blocklen); + return -EINVAL; } - split_size_blocks = (split_size_mb * mb) / base_bdev->blocklen; + 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", - split_size_mb); + cfg->split_size_mb); } else { - split_size_blocks = base_bdev->blockcnt / split_count; + 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); @@ -169,20 +185,14 @@ vbdev_split_create(struct spdk_bdev *base_bdev, uint64_t split_count, uint64_t s " split_size_blocks: %" PRIu64 "\n", spdk_bdev_get_name(base_bdev), split_count, split_size_blocks); - split_base = calloc(1, sizeof(*split_base)); - if (!split_base) { - SPDK_ERRLOG("Cannot allocate bdev part base\n"); - return -1; - } - - rc = spdk_bdev_part_base_construct(split_base, base_bdev, + rc = spdk_bdev_part_base_construct(&cfg->split_base, base_bdev, vbdev_split_base_bdev_hotremove_cb, &split_if, &vbdev_split_fn_table, &g_split_disks, vbdev_split_base_free, sizeof(struct vbdev_split_channel), NULL, NULL); if (rc) { SPDK_ERRLOG("Cannot construct bdev part base\n"); - return -1; + return rc; } offset_blocks = 0; @@ -192,40 +202,64 @@ vbdev_split_create(struct spdk_bdev *base_bdev, uint64_t split_count, uint64_t s d = calloc(1, sizeof(*d)); if (d == NULL) { SPDK_ERRLOG("could not allocate bdev part\n"); - return -ENOMEM; + rc = -ENOMEM; + goto err; } - name = spdk_sprintf_alloc("%sp%" PRIu64, spdk_bdev_get_name(base_bdev), i); + name = spdk_sprintf_alloc("%sp%" PRIu64, cfg->base_bdev, i); if (!name) { SPDK_ERRLOG("could not allocate name\n"); free(d); - return -ENOMEM; + rc = -ENOMEM; + goto err; } - rc = spdk_bdev_part_construct(d, split_base, name, offset_blocks, split_size_blocks, + rc = spdk_bdev_part_construct(d, &cfg->split_base, name, offset_blocks, split_size_blocks, "Split Disk"); if (rc) { SPDK_ERRLOG("could not construct bdev part\n"); /* spdk_bdev_part_construct will free name if it fails */ free(d); - return rc; + rc = -ENOMEM; + goto err; } offset_blocks += split_size_blocks; } return 0; +err: + cfg->removed = true; + spdk_bdev_part_base_hotremove(cfg->split_base.bdev, cfg->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) +{ + cfg->removed = true; + if (cfg->split_base.ref) { + spdk_bdev_part_base_hotremove(cfg->split_base.bdev, cfg->split_base.tailq); + } else { + vbdev_split_del_config(cfg); + } } static void vbdev_split_clear_config(void) { - struct spdk_vbdev_split_config *cfg; + struct spdk_vbdev_split_config *cfg, *tmp_cfg; - while ((cfg = TAILQ_FIRST(&g_split_config))) { - TAILQ_REMOVE(&g_split_config, cfg, tailq); - free(cfg->base_bdev); - free(cfg); + TAILQ_FOREACH_SAFE(cfg, &g_split_config, tailq, tmp_cfg) { + vbdev_split_destruct_config(cfg); } } @@ -244,15 +278,26 @@ vbdev_split_config_find_by_base_name(const char *base_bdev_name) } static int -vbdev_split_add_config(const char *base_bdev_name, unsigned split_count, uint64_t split_size) +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 config for '%s' already exist.", base_bdev_name); + SPDK_ERRLOG("Split bdev config for base bdev '%s' already exist.", base_bdev_name); return -EEXIST; } @@ -272,6 +317,10 @@ vbdev_split_add_config(const char *base_bdev_name, unsigned split_count, uint64_ 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; } @@ -328,7 +377,7 @@ vbdev_split_init(void) } } - rc = vbdev_split_add_config(base_bdev_name, split_count, split_size); + rc = vbdev_split_add_config(base_bdev_name, split_count, split_size, NULL); if (rc != 0) { goto err; } @@ -351,11 +400,49 @@ vbdev_split_examine(struct spdk_bdev *bdev) { struct spdk_vbdev_split_config *cfg = vbdev_split_config_find_by_base_name(bdev->name); - if (cfg && vbdev_split_create(bdev, cfg->split_count, cfg->split_size_mb)) { - SPDK_ERRLOG("could not split bdev %s\n", bdev->name); + if (cfg != NULL && cfg->removed == false) { + assert(cfg->split_base.ref == 0); + + if (vbdev_split_create(cfg)) { + SPDK_ERRLOG("could not split bdev %s\n", bdev->name); + } } spdk_bdev_module_examine_done(&split_if); } +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; +} + SPDK_LOG_REGISTER_COMPONENT("vbdev_split", SPDK_LOG_VBDEV_SPLIT) diff --git a/lib/bdev/split/vbdev_split.h b/lib/bdev/split/vbdev_split.h new file mode 100644 index 000000000..b57700f25 --- /dev/null +++ b/lib/bdev/split/vbdev_split.h @@ -0,0 +1,59 @@ +/*- + * 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_internal/bdev.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 succes or negative errno value. + */ +int spdk_vbdev_split_destruct(const char *base_bdev_name); + +#endif // SPDK_VBDEV_SPLIT_H diff --git a/lib/bdev/split/vbdev_split_rpc.c b/lib/bdev/split/vbdev_split_rpc.c new file mode 100644 index 000000000..fb1307945 --- /dev/null +++ b/lib/bdev/split/vbdev_split_rpc.c @@ -0,0 +1,143 @@ +/*- + * 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; + size_t i; + 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) { + for (i = 0; i < base_bdev->vbdevs_cnt; i++) { + spdk_json_write_string(w, spdk_bdev_get_name(base_bdev->vbdevs[i])); + } + } + + 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) + +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) { + return; + } + + spdk_json_write_bool(w, true); + spdk_jsonrpc_end_result(request, w); + return; + +out: + free(req.base_bdev); +} +SPDK_RPC_REGISTER("destruct_split_vbdev", spdk_rpc_destruct_split) diff --git a/scripts/rpc.py b/scripts/rpc.py index 6e3ba9c9b..87b297b38 100755 --- a/scripts/rpc.py +++ b/scripts/rpc.py @@ -546,6 +546,26 @@ if __name__ == "__main__": p.add_argument('-l', '--lvs_name', help='lvol store name', required=False) p.set_defaults(func=get_lvol_stores) + # split + def construct_split_vbdev(args): + print_dict(rpc.bdev.construct_split_vbdev(args.client, args)) + + p = subparsers.add_parser('construct_split_vbdev', help="""Add given disk name to split config. If bdev with base_name + name exist the split bdevs will be created right away, if not split bdevs will be created when base bdev became + available (during examination process).""") + p.add_argument('base_bdev', help='base bdev name') + p.add_argument('-s', '--split_size_mb', help='size in MiB for each bdev', type=int, default=0) + p.add_argument('split_count', help="""Optional - number of split bdevs to create. Total size * split_count must not + exceed the base bdev size.""", type=int) + p.set_defaults(func=construct_split_vbdev) + + def destruct_split_vbdev(args): + rpc.destruct_split_vbdev(args.client, args) + + p = subparsers.add_parser('destruct_split_vbdev', help="""Delete split config with all created splits.""") + p.add_argument('base_bdev', help='base bdev name') + p.set_defaults(func=destruct_split_vbdev) + # nbd @call_cmd def start_nbd_disk(args): diff --git a/scripts/rpc/bdev.py b/scripts/rpc/bdev.py index 7e41bcf86..56aa9bfec 100755 --- a/scripts/rpc/bdev.py +++ b/scripts/rpc/bdev.py @@ -70,6 +70,25 @@ def construct_pmem_bdev(client, args): return client.call('construct_pmem_bdev', params) +def construct_split_vbdev(client, args): + params = { + 'base_bdev': args.base_bdev, + 'split_count': args.split_count, + } + if args.split_size_mb: + params['split_size_mb'] = args.split_size_mb + + return client.call('construct_split_vbdev', params) + + +def destruct_split_vbdev(client, args): + params = { + 'base_bdev': args.base_bdev, + } + + return client.call('destruct_split_vbdev', params) + + def get_bdevs(client, args): params = {} if args.name: