bdev/malloc: Use options structure to create a malloc bdev

Define a options structure, malloc_bdev_opts, and use it directly for
the bdev_malloc_create RPC. To do this, bdev_malloc.h includes
bdev_module.h instead of bdev.h to have the definition of the struct
spdk_uuid, and the struct malloc_bdev_opts has a instance of struct
spdk_uuid. Clean up file inclusion together. Furthermore, use
spdk_uuid_copy() to copy uuid from the malloc_bdev_opts to the malloc
disk rather than the = operator, and remove a duplicated size check.

These are helpful to add more parameters for creation.

Signed-off-by: Shuhei Matsumoto <smatsumoto@nvidia.com>
Change-Id: Ief25f12586c21b1666180ce10cfc6256ede8eba9
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14982
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Aleksey Marchuk <alexeymar@nvidia.com>
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
This commit is contained in:
Shuhei Matsumoto 2022-10-23 21:46:36 +09:00 committed by Tomasz Zawadzki
parent 83eff61df6
commit e6b2b9075a
3 changed files with 32 additions and 46 deletions

View File

@ -7,16 +7,11 @@
#include "spdk/stdinc.h" #include "spdk/stdinc.h"
#include "bdev_malloc.h" #include "bdev_malloc.h"
#include "spdk/bdev.h"
#include "spdk/endian.h" #include "spdk/endian.h"
#include "spdk/env.h" #include "spdk/env.h"
#include "spdk/accel.h" #include "spdk/accel.h"
#include "spdk/json.h"
#include "spdk/thread.h"
#include "spdk/queue.h"
#include "spdk/string.h" #include "spdk/string.h"
#include "spdk/bdev_module.h"
#include "spdk/log.h" #include "spdk/log.h"
struct malloc_disk { struct malloc_disk {
@ -362,18 +357,19 @@ static const struct spdk_bdev_fn_table malloc_fn_table = {
}; };
int int
create_malloc_disk(struct spdk_bdev **bdev, const char *name, const struct spdk_uuid *uuid, create_malloc_disk(struct spdk_bdev **bdev, const struct malloc_bdev_opts *opts)
uint64_t num_blocks, uint32_t block_size, uint32_t optimal_io_boundary)
{ {
struct malloc_disk *mdisk; struct malloc_disk *mdisk;
int rc; int rc;
if (num_blocks == 0) { assert(opts != NULL);
if (opts->num_blocks == 0) {
SPDK_ERRLOG("Disk num_blocks must be greater than 0"); SPDK_ERRLOG("Disk num_blocks must be greater than 0");
return -EINVAL; return -EINVAL;
} }
if (block_size % 512) { if (opts->block_size % 512) {
SPDK_ERRLOG("block size must be 512 bytes aligned\n"); SPDK_ERRLOG("block size must be 512 bytes aligned\n");
return -EINVAL; return -EINVAL;
} }
@ -390,7 +386,7 @@ create_malloc_disk(struct spdk_bdev **bdev, const char *name, const struct spdk_
* TODO: need to pass a hint so we know which socket to allocate * TODO: need to pass a hint so we know which socket to allocate
* from on multi-socket systems. * from on multi-socket systems.
*/ */
mdisk->malloc_buf = spdk_zmalloc(num_blocks * block_size, 2 * 1024 * 1024, NULL, mdisk->malloc_buf = spdk_zmalloc(opts->num_blocks * opts->block_size, 2 * 1024 * 1024, NULL,
SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
if (!mdisk->malloc_buf) { if (!mdisk->malloc_buf) {
SPDK_ERRLOG("malloc_buf spdk_zmalloc() failed\n"); SPDK_ERRLOG("malloc_buf spdk_zmalloc() failed\n");
@ -398,8 +394,8 @@ create_malloc_disk(struct spdk_bdev **bdev, const char *name, const struct spdk_
return -ENOMEM; return -ENOMEM;
} }
if (name) { if (opts->name) {
mdisk->disk.name = strdup(name); mdisk->disk.name = strdup(opts->name);
} else { } else {
/* Auto-generate a name */ /* Auto-generate a name */
mdisk->disk.name = spdk_sprintf_alloc("Malloc%d", malloc_disk_count); mdisk->disk.name = spdk_sprintf_alloc("Malloc%d", malloc_disk_count);
@ -412,14 +408,14 @@ create_malloc_disk(struct spdk_bdev **bdev, const char *name, const struct spdk_
mdisk->disk.product_name = "Malloc disk"; mdisk->disk.product_name = "Malloc disk";
mdisk->disk.write_cache = 1; mdisk->disk.write_cache = 1;
mdisk->disk.blocklen = block_size; mdisk->disk.blocklen = opts->block_size;
mdisk->disk.blockcnt = num_blocks; mdisk->disk.blockcnt = opts->num_blocks;
if (optimal_io_boundary) { if (opts->optimal_io_boundary) {
mdisk->disk.optimal_io_boundary = optimal_io_boundary; mdisk->disk.optimal_io_boundary = opts->optimal_io_boundary;
mdisk->disk.split_on_optimal_io_boundary = true; mdisk->disk.split_on_optimal_io_boundary = true;
} }
if (uuid) { if (!spdk_mem_all_zero(&opts->uuid, sizeof(opts->uuid))) {
mdisk->disk.uuid = *uuid; spdk_uuid_copy(&mdisk->disk.uuid, &opts->uuid);
} else { } else {
spdk_uuid_generate(&mdisk->disk.uuid); spdk_uuid_generate(&mdisk->disk.uuid);
} }

View File

@ -9,12 +9,19 @@
#include "spdk/stdinc.h" #include "spdk/stdinc.h"
#include "spdk/bdev.h" #include "spdk/bdev_module.h"
typedef void (*spdk_delete_malloc_complete)(void *cb_arg, int bdeverrno); typedef void (*spdk_delete_malloc_complete)(void *cb_arg, int bdeverrno);
int create_malloc_disk(struct spdk_bdev **bdev, const char *name, const struct spdk_uuid *uuid, struct malloc_bdev_opts {
uint64_t num_blocks, uint32_t block_size, uint32_t optimal_io_boundary); char *name;
struct spdk_uuid uuid;
uint64_t num_blocks;
uint32_t block_size;
uint32_t optimal_io_boundary;
};
int create_malloc_disk(struct spdk_bdev **bdev, const struct malloc_bdev_opts *opts);
void delete_malloc_disk(const char *name, spdk_delete_malloc_complete cb_fn, void *cb_arg); void delete_malloc_disk(const char *name, spdk_delete_malloc_complete cb_fn, void *cb_arg);

View File

@ -6,21 +6,11 @@
#include "bdev_malloc.h" #include "bdev_malloc.h"
#include "spdk/rpc.h" #include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk/uuid.h"
#include "spdk/string.h" #include "spdk/string.h"
#include "spdk/log.h" #include "spdk/log.h"
struct rpc_construct_malloc {
char *name;
struct spdk_uuid uuid;
uint64_t num_blocks;
uint32_t block_size;
uint32_t optimal_io_boundary;
};
static void static void
free_rpc_construct_malloc(struct rpc_construct_malloc *r) free_rpc_construct_malloc(struct malloc_bdev_opts *r)
{ {
free(r->name); free(r->name);
} }
@ -41,18 +31,18 @@ decode_mdisk_uuid(const struct spdk_json_val *val, void *out)
} }
static const struct spdk_json_object_decoder rpc_construct_malloc_decoders[] = { static const struct spdk_json_object_decoder rpc_construct_malloc_decoders[] = {
{"name", offsetof(struct rpc_construct_malloc, name), spdk_json_decode_string, true}, {"name", offsetof(struct malloc_bdev_opts, name), spdk_json_decode_string, true},
{"uuid", offsetof(struct rpc_construct_malloc, uuid), decode_mdisk_uuid, true}, {"uuid", offsetof(struct malloc_bdev_opts, uuid), decode_mdisk_uuid, true},
{"num_blocks", offsetof(struct rpc_construct_malloc, num_blocks), spdk_json_decode_uint64}, {"num_blocks", offsetof(struct malloc_bdev_opts, num_blocks), spdk_json_decode_uint64},
{"block_size", offsetof(struct rpc_construct_malloc, block_size), spdk_json_decode_uint32}, {"block_size", offsetof(struct malloc_bdev_opts, block_size), spdk_json_decode_uint32},
{"optimal_io_boundary", offsetof(struct rpc_construct_malloc, optimal_io_boundary), spdk_json_decode_uint32, true}, {"optimal_io_boundary", offsetof(struct malloc_bdev_opts, optimal_io_boundary), spdk_json_decode_uint32, true},
}; };
static void static void
rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request, rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *params) const struct spdk_json_val *params)
{ {
struct rpc_construct_malloc req = {NULL}; struct malloc_bdev_opts req = {NULL};
struct spdk_json_write_ctx *w; struct spdk_json_write_ctx *w;
struct spdk_bdev *bdev; struct spdk_bdev *bdev;
int rc = 0; int rc = 0;
@ -66,14 +56,7 @@ rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request,
goto cleanup; goto cleanup;
} }
if (req.num_blocks == 0) { rc = create_malloc_disk(&bdev, &req);
spdk_jsonrpc_send_error_response(request, -EINVAL,
"Disk num_blocks must be greater than 0");
goto cleanup;
}
rc = create_malloc_disk(&bdev, req.name, &req.uuid, req.num_blocks, req.block_size,
req.optimal_io_boundary);
if (rc) { if (rc) {
spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
goto cleanup; goto cleanup;