diff --git a/lib/bdev/malloc/bdev_malloc.c b/lib/bdev/malloc/bdev_malloc.c index fec86f0df..cbad290a3 100644 --- a/lib/bdev/malloc/bdev_malloc.c +++ b/lib/bdev/malloc/bdev_malloc.c @@ -390,21 +390,22 @@ static const struct spdk_bdev_fn_table malloc_fn_table = { .write_config_json = bdev_malloc_write_json_config, }; -struct spdk_bdev *create_malloc_disk(const char *name, const struct spdk_uuid *uuid, - uint64_t num_blocks, uint32_t block_size) +int +create_malloc_disk(struct spdk_bdev **bdev, const char *name, const struct spdk_uuid *uuid, + uint64_t num_blocks, uint32_t block_size) { struct malloc_disk *mdisk; - int rc; + int rc; if (num_blocks == 0) { - SPDK_ERRLOG("Disk must be more than 0 blocks\n"); - return NULL; + SPDK_ERRLOG("Disk num_blocks must be greater than 0"); + return -EINVAL; } mdisk = calloc(1, sizeof(*mdisk)); if (!mdisk) { SPDK_ERRLOG("mdisk calloc() failed\n"); - return NULL; + return -ENOMEM; } /* @@ -418,7 +419,7 @@ struct spdk_bdev *create_malloc_disk(const char *name, const struct spdk_uuid *u if (!mdisk->malloc_buf) { SPDK_ERRLOG("malloc_buf spdk_zmalloc() failed\n"); malloc_disk_free(mdisk); - return NULL; + return -ENOMEM; } if (name) { @@ -430,7 +431,7 @@ struct spdk_bdev *create_malloc_disk(const char *name, const struct spdk_uuid *u } if (!mdisk->disk.name) { malloc_disk_free(mdisk); - return NULL; + return -ENOMEM; } mdisk->disk.product_name = "Malloc disk"; @@ -450,12 +451,14 @@ struct spdk_bdev *create_malloc_disk(const char *name, const struct spdk_uuid *u rc = spdk_bdev_register(&mdisk->disk); if (rc) { malloc_disk_free(mdisk); - return NULL; + return rc; } + *bdev = &(mdisk->disk); + TAILQ_INSERT_TAIL(&g_malloc_disks, mdisk, link); - return &mdisk->disk; + return rc; } void @@ -490,10 +493,9 @@ static int bdev_malloc_initialize(void) } size = (uint64_t)LunSizeInMB * 1024 * 1024; for (i = 0; i < NumberOfLuns; i++) { - bdev = create_malloc_disk(NULL, NULL, size / BlockSize, BlockSize); - if (bdev == NULL) { + rc = create_malloc_disk(&bdev, NULL, NULL, size / BlockSize, BlockSize); + if (rc) { SPDK_ERRLOG("Could not create malloc disk\n"); - rc = EINVAL; goto end; } } diff --git a/lib/bdev/malloc/bdev_malloc.h b/lib/bdev/malloc/bdev_malloc.h index 8ebdba780..b683b1062 100644 --- a/lib/bdev/malloc/bdev_malloc.h +++ b/lib/bdev/malloc/bdev_malloc.h @@ -40,8 +40,8 @@ typedef void (*spdk_delete_malloc_complete)(void *cb_arg, int bdeverrno); -struct spdk_bdev *create_malloc_disk(const char *name, const struct spdk_uuid *uuid, - uint64_t num_blocks, uint32_t block_size); +int create_malloc_disk(struct spdk_bdev **bdev, const char *name, const struct spdk_uuid *uuid, + uint64_t num_blocks, uint32_t block_size); void delete_malloc_disk(struct spdk_bdev *bdev, spdk_delete_malloc_complete cb_fn, void *cb_arg); diff --git a/lib/bdev/malloc/bdev_malloc_rpc.c b/lib/bdev/malloc/bdev_malloc_rpc.c index 4066cf2fb..c99b6ad49 100644 --- a/lib/bdev/malloc/bdev_malloc_rpc.c +++ b/lib/bdev/malloc/bdev_malloc_rpc.c @@ -68,24 +68,36 @@ spdk_rpc_construct_malloc_bdev(struct spdk_jsonrpc_request *request, struct spdk_uuid *uuid = NULL; struct spdk_uuid decoded_uuid; struct spdk_bdev *bdev; + int rc = 0; if (spdk_json_decode_object(params, rpc_construct_malloc_decoders, SPDK_COUNTOF(rpc_construct_malloc_decoders), &req)) { SPDK_DEBUGLOG(SPDK_LOG_BDEV_MALLOC, "spdk_json_decode_object failed\n"); - goto invalid; + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + "spdk_json_decode_object failed"); + goto cleanup; + } + + if (req.num_blocks == 0) { + spdk_jsonrpc_send_error_response(request, -EINVAL, + "Disk num_blocks must be greater than 0"); + goto cleanup; } if (req.uuid) { if (spdk_uuid_parse(&decoded_uuid, req.uuid)) { - goto invalid; + spdk_jsonrpc_send_error_response(request, -EINVAL, + "Failed to parse bdev UUID"); + goto cleanup; } uuid = &decoded_uuid; } - bdev = create_malloc_disk(req.name, uuid, req.num_blocks, req.block_size); - if (bdev == NULL) { - goto invalid; + rc = create_malloc_disk(&bdev, req.name, uuid, req.num_blocks, req.block_size); + if (rc) { + spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); + goto cleanup; } free_rpc_construct_malloc(&req); @@ -99,9 +111,8 @@ spdk_rpc_construct_malloc_bdev(struct spdk_jsonrpc_request *request, spdk_jsonrpc_end_result(request, w); return; -invalid: +cleanup: free_rpc_construct_malloc(&req); - spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); } SPDK_RPC_REGISTER("construct_malloc_bdev", spdk_rpc_construct_malloc_bdev, SPDK_RPC_RUNTIME) @@ -140,21 +151,21 @@ spdk_rpc_delete_malloc_bdev(struct spdk_jsonrpc_request *request, { struct rpc_delete_malloc req = {NULL}; struct spdk_bdev *bdev; - int rc; if (spdk_json_decode_object(params, rpc_delete_malloc_decoders, SPDK_COUNTOF(rpc_delete_malloc_decoders), &req)) { SPDK_DEBUGLOG(SPDK_LOG_BDEV_MALLOC, "spdk_json_decode_object failed\n"); - rc = -EINVAL; - goto invalid; + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + "spdk_json_decode_object failed"); + goto cleanup; } bdev = spdk_bdev_get_by_name(req.name); if (bdev == NULL) { SPDK_INFOLOG(SPDK_LOG_BDEV_MALLOC, "bdev '%s' does not exist\n", req.name); - rc = -ENODEV; - goto invalid; + spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); + goto cleanup; } delete_malloc_disk(bdev, _spdk_rpc_delete_malloc_bdev_cb, request); @@ -163,8 +174,7 @@ spdk_rpc_delete_malloc_bdev(struct spdk_jsonrpc_request *request, return; -invalid: +cleanup: free_rpc_delete_malloc(&req); - spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc)); } SPDK_RPC_REGISTER("delete_malloc_bdev", spdk_rpc_delete_malloc_bdev, SPDK_RPC_RUNTIME)