1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2016 Intel Corporation. 3 * All rights reserved. 4 * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 5 */ 6 7 #include "bdev_malloc.h" 8 #include "spdk/rpc.h" 9 #include "spdk/string.h" 10 #include "spdk/log.h" 11 12 static void 13 free_rpc_construct_malloc(struct malloc_bdev_opts *r) 14 { 15 free(r->name); 16 } 17 18 static int 19 decode_mdisk_uuid(const struct spdk_json_val *val, void *out) 20 { 21 char *str = NULL; 22 int rc; 23 24 rc = spdk_json_decode_string(val, &str); 25 if (rc == 0) { 26 rc = spdk_uuid_parse(out, str); 27 } 28 29 free(str); 30 return rc; 31 } 32 33 static const struct spdk_json_object_decoder rpc_construct_malloc_decoders[] = { 34 {"name", offsetof(struct malloc_bdev_opts, name), spdk_json_decode_string, true}, 35 {"uuid", offsetof(struct malloc_bdev_opts, uuid), decode_mdisk_uuid, true}, 36 {"num_blocks", offsetof(struct malloc_bdev_opts, num_blocks), spdk_json_decode_uint64}, 37 {"block_size", offsetof(struct malloc_bdev_opts, block_size), spdk_json_decode_uint32}, 38 {"physical_block_size", offsetof(struct malloc_bdev_opts, physical_block_size), spdk_json_decode_uint32, true}, 39 {"optimal_io_boundary", offsetof(struct malloc_bdev_opts, optimal_io_boundary), spdk_json_decode_uint32, true}, 40 {"md_size", offsetof(struct malloc_bdev_opts, md_size), spdk_json_decode_uint32, true}, 41 {"md_interleave", offsetof(struct malloc_bdev_opts, md_interleave), spdk_json_decode_bool, true}, 42 {"dif_type", offsetof(struct malloc_bdev_opts, dif_type), spdk_json_decode_int32, true}, 43 {"dif_is_head_of_md", offsetof(struct malloc_bdev_opts, dif_is_head_of_md), spdk_json_decode_bool, true}, 44 }; 45 46 static void 47 rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request, 48 const struct spdk_json_val *params) 49 { 50 struct malloc_bdev_opts req = {NULL}; 51 struct spdk_json_write_ctx *w; 52 struct spdk_bdev *bdev; 53 int rc = 0; 54 55 if (spdk_json_decode_object(params, rpc_construct_malloc_decoders, 56 SPDK_COUNTOF(rpc_construct_malloc_decoders), 57 &req)) { 58 SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n"); 59 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 60 "spdk_json_decode_object failed"); 61 goto cleanup; 62 } 63 64 rc = create_malloc_disk(&bdev, &req); 65 if (rc) { 66 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 67 goto cleanup; 68 } 69 70 free_rpc_construct_malloc(&req); 71 72 w = spdk_jsonrpc_begin_result(request); 73 spdk_json_write_string(w, spdk_bdev_get_name(bdev)); 74 spdk_jsonrpc_end_result(request, w); 75 return; 76 77 cleanup: 78 free_rpc_construct_malloc(&req); 79 } 80 SPDK_RPC_REGISTER("bdev_malloc_create", rpc_bdev_malloc_create, SPDK_RPC_RUNTIME) 81 82 struct rpc_delete_malloc { 83 char *name; 84 }; 85 86 static void 87 free_rpc_delete_malloc(struct rpc_delete_malloc *r) 88 { 89 free(r->name); 90 } 91 92 static const struct spdk_json_object_decoder rpc_delete_malloc_decoders[] = { 93 {"name", offsetof(struct rpc_delete_malloc, name), spdk_json_decode_string}, 94 }; 95 96 static void 97 rpc_bdev_malloc_delete_cb(void *cb_arg, int bdeverrno) 98 { 99 struct spdk_jsonrpc_request *request = cb_arg; 100 101 if (bdeverrno == 0) { 102 spdk_jsonrpc_send_bool_response(request, true); 103 } else { 104 spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno)); 105 } 106 } 107 108 static void 109 rpc_bdev_malloc_delete(struct spdk_jsonrpc_request *request, 110 const struct spdk_json_val *params) 111 { 112 struct rpc_delete_malloc req = {NULL}; 113 114 if (spdk_json_decode_object(params, rpc_delete_malloc_decoders, 115 SPDK_COUNTOF(rpc_delete_malloc_decoders), 116 &req)) { 117 SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n"); 118 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 119 "spdk_json_decode_object failed"); 120 goto cleanup; 121 } 122 123 delete_malloc_disk(req.name, rpc_bdev_malloc_delete_cb, request); 124 125 cleanup: 126 free_rpc_delete_malloc(&req); 127 } 128 SPDK_RPC_REGISTER("bdev_malloc_delete", rpc_bdev_malloc_delete, SPDK_RPC_RUNTIME) 129