1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "bdev_malloc.h" 36 #include "spdk/rpc.h" 37 #include "spdk/util.h" 38 #include "spdk/uuid.h" 39 #include "spdk/string.h" 40 #include "spdk/log.h" 41 42 struct rpc_construct_malloc { 43 char *name; 44 char *uuid; 45 uint64_t num_blocks; 46 uint32_t block_size; 47 uint32_t optimal_io_boundary; 48 }; 49 50 static void 51 free_rpc_construct_malloc(struct rpc_construct_malloc *r) 52 { 53 free(r->name); 54 free(r->uuid); 55 } 56 57 static const struct spdk_json_object_decoder rpc_construct_malloc_decoders[] = { 58 {"name", offsetof(struct rpc_construct_malloc, name), spdk_json_decode_string, true}, 59 {"uuid", offsetof(struct rpc_construct_malloc, uuid), spdk_json_decode_string, true}, 60 {"num_blocks", offsetof(struct rpc_construct_malloc, num_blocks), spdk_json_decode_uint64}, 61 {"block_size", offsetof(struct rpc_construct_malloc, block_size), spdk_json_decode_uint32}, 62 {"optimal_io_boundary", offsetof(struct rpc_construct_malloc, optimal_io_boundary), spdk_json_decode_uint32, true}, 63 }; 64 65 static void 66 rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request, 67 const struct spdk_json_val *params) 68 { 69 struct rpc_construct_malloc req = {NULL}; 70 struct spdk_json_write_ctx *w; 71 struct spdk_uuid *uuid = NULL; 72 struct spdk_uuid decoded_uuid; 73 struct spdk_bdev *bdev; 74 int rc = 0; 75 76 if (spdk_json_decode_object(params, rpc_construct_malloc_decoders, 77 SPDK_COUNTOF(rpc_construct_malloc_decoders), 78 &req)) { 79 SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n"); 80 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 81 "spdk_json_decode_object failed"); 82 goto cleanup; 83 } 84 85 if (req.num_blocks == 0) { 86 spdk_jsonrpc_send_error_response(request, -EINVAL, 87 "Disk num_blocks must be greater than 0"); 88 goto cleanup; 89 } 90 91 if (req.uuid) { 92 if (spdk_uuid_parse(&decoded_uuid, req.uuid)) { 93 spdk_jsonrpc_send_error_response(request, -EINVAL, 94 "Failed to parse bdev UUID"); 95 goto cleanup; 96 } 97 uuid = &decoded_uuid; 98 } 99 100 rc = create_malloc_disk(&bdev, req.name, uuid, req.num_blocks, req.block_size, 101 req.optimal_io_boundary); 102 if (rc) { 103 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 104 goto cleanup; 105 } 106 107 free_rpc_construct_malloc(&req); 108 109 w = spdk_jsonrpc_begin_result(request); 110 spdk_json_write_string(w, spdk_bdev_get_name(bdev)); 111 spdk_jsonrpc_end_result(request, w); 112 return; 113 114 cleanup: 115 free_rpc_construct_malloc(&req); 116 } 117 SPDK_RPC_REGISTER("bdev_malloc_create", rpc_bdev_malloc_create, SPDK_RPC_RUNTIME) 118 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_malloc_create, construct_malloc_bdev) 119 120 struct rpc_delete_malloc { 121 char *name; 122 }; 123 124 static void 125 free_rpc_delete_malloc(struct rpc_delete_malloc *r) 126 { 127 free(r->name); 128 } 129 130 static const struct spdk_json_object_decoder rpc_delete_malloc_decoders[] = { 131 {"name", offsetof(struct rpc_delete_malloc, name), spdk_json_decode_string}, 132 }; 133 134 static void 135 rpc_bdev_malloc_delete_cb(void *cb_arg, int bdeverrno) 136 { 137 struct spdk_jsonrpc_request *request = cb_arg; 138 139 spdk_jsonrpc_send_bool_response(request, bdeverrno == 0); 140 } 141 142 static void 143 rpc_bdev_malloc_delete(struct spdk_jsonrpc_request *request, 144 const struct spdk_json_val *params) 145 { 146 struct rpc_delete_malloc req = {NULL}; 147 struct spdk_bdev *bdev; 148 149 if (spdk_json_decode_object(params, rpc_delete_malloc_decoders, 150 SPDK_COUNTOF(rpc_delete_malloc_decoders), 151 &req)) { 152 SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n"); 153 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 154 "spdk_json_decode_object failed"); 155 goto cleanup; 156 } 157 158 bdev = spdk_bdev_get_by_name(req.name); 159 if (bdev == NULL) { 160 SPDK_INFOLOG(bdev_malloc, "bdev '%s' does not exist\n", req.name); 161 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 162 goto cleanup; 163 } 164 165 delete_malloc_disk(bdev, rpc_bdev_malloc_delete_cb, request); 166 167 cleanup: 168 free_rpc_delete_malloc(&req); 169 } 170 SPDK_RPC_REGISTER("bdev_malloc_delete", rpc_bdev_malloc_delete, SPDK_RPC_RUNTIME) 171 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_malloc_delete, delete_malloc_bdev) 172