Commit e6b2b907 authored by Shuhei Matsumoto's avatar Shuhei Matsumoto Committed by Tomasz Zawadzki
Browse files

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: default avatarShuhei Matsumoto <smatsumoto@nvidia.com>
Change-Id: Ief25f12586c21b1666180ce10cfc6256ede8eba9
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14982


Reviewed-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Community-CI: Mellanox Build Bot
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
parent 83eff61d
Loading
Loading
Loading
Loading
+14 −18
Original line number Diff line number Diff line
@@ -7,16 +7,11 @@
#include "spdk/stdinc.h"

#include "bdev_malloc.h"
#include "spdk/bdev.h"
#include "spdk/endian.h"
#include "spdk/env.h"
#include "spdk/accel.h"
#include "spdk/json.h"
#include "spdk/thread.h"
#include "spdk/queue.h"
#include "spdk/string.h"

#include "spdk/bdev_module.h"
#include "spdk/log.h"

struct malloc_disk {
@@ -362,18 +357,19 @@ static const struct spdk_bdev_fn_table malloc_fn_table = {
};

int
create_malloc_disk(struct spdk_bdev **bdev, const char *name, const struct spdk_uuid *uuid,
		   uint64_t num_blocks, uint32_t block_size, uint32_t optimal_io_boundary)
create_malloc_disk(struct spdk_bdev **bdev, const struct malloc_bdev_opts *opts)
{
	struct malloc_disk	*mdisk;
	int rc;

	if (num_blocks == 0) {
	assert(opts != NULL);

	if (opts->num_blocks == 0) {
		SPDK_ERRLOG("Disk num_blocks must be greater than 0");
		return -EINVAL;
	}

	if (block_size % 512) {
	if (opts->block_size % 512) {
		SPDK_ERRLOG("block size must be 512 bytes aligned\n");
		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
	 *  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);
	if (!mdisk->malloc_buf) {
		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;
	}

	if (name) {
		mdisk->disk.name = strdup(name);
	if (opts->name) {
		mdisk->disk.name = strdup(opts->name);
	} else {
		/* Auto-generate a name */
		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.write_cache = 1;
	mdisk->disk.blocklen = block_size;
	mdisk->disk.blockcnt = num_blocks;
	if (optimal_io_boundary) {
		mdisk->disk.optimal_io_boundary = optimal_io_boundary;
	mdisk->disk.blocklen = opts->block_size;
	mdisk->disk.blockcnt = opts->num_blocks;
	if (opts->optimal_io_boundary) {
		mdisk->disk.optimal_io_boundary = opts->optimal_io_boundary;
		mdisk->disk.split_on_optimal_io_boundary = true;
	}
	if (uuid) {
		mdisk->disk.uuid = *uuid;
	if (!spdk_mem_all_zero(&opts->uuid, sizeof(opts->uuid))) {
		spdk_uuid_copy(&mdisk->disk.uuid, &opts->uuid);
	} else {
		spdk_uuid_generate(&mdisk->disk.uuid);
	}
+10 −3
Original line number Diff line number Diff line
@@ -9,12 +9,19 @@

#include "spdk/stdinc.h"

#include "spdk/bdev.h"
#include "spdk/bdev_module.h"

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,
		       uint64_t num_blocks, uint32_t block_size, uint32_t optimal_io_boundary);
struct malloc_bdev_opts {
	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);

+8 −25
Original line number Diff line number Diff line
@@ -6,21 +6,11 @@

#include "bdev_malloc.h"
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk/uuid.h"
#include "spdk/string.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
free_rpc_construct_malloc(struct rpc_construct_malloc *r)
free_rpc_construct_malloc(struct malloc_bdev_opts *r)
{
	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[] = {
	{"name", offsetof(struct rpc_construct_malloc, name), spdk_json_decode_string, true},
	{"uuid", offsetof(struct rpc_construct_malloc, uuid), decode_mdisk_uuid, true},
	{"num_blocks", offsetof(struct rpc_construct_malloc, num_blocks), spdk_json_decode_uint64},
	{"block_size", offsetof(struct rpc_construct_malloc, block_size), spdk_json_decode_uint32},
	{"optimal_io_boundary", offsetof(struct rpc_construct_malloc, optimal_io_boundary), spdk_json_decode_uint32, true},
	{"name", offsetof(struct malloc_bdev_opts, name), spdk_json_decode_string, true},
	{"uuid", offsetof(struct malloc_bdev_opts, uuid), decode_mdisk_uuid, true},
	{"num_blocks", offsetof(struct malloc_bdev_opts, num_blocks), spdk_json_decode_uint64},
	{"block_size", offsetof(struct malloc_bdev_opts, block_size), spdk_json_decode_uint32},
	{"optimal_io_boundary", offsetof(struct malloc_bdev_opts, optimal_io_boundary), spdk_json_decode_uint32, true},
};

static void
rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request,
		       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_bdev *bdev;
	int rc = 0;
@@ -66,14 +56,7 @@ rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request,
		goto cleanup;
	}

	if (req.num_blocks == 0) {
		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);
	rc = create_malloc_disk(&bdev, &req);
	if (rc) {
		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
		goto cleanup;