1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "bdev_rbd.h" 35 #include "spdk/rpc.h" 36 #include "spdk/util.h" 37 #include "spdk/string.h" 38 #include "spdk_internal/log.h" 39 40 struct rpc_create_rbd { 41 char *name; 42 char *user_id; 43 char *pool_name; 44 char *rbd_name; 45 uint32_t block_size; 46 char **config; 47 }; 48 49 static void 50 free_rpc_create_rbd(struct rpc_create_rbd *req) 51 { 52 free(req->name); 53 free(req->user_id); 54 free(req->pool_name); 55 free(req->rbd_name); 56 bdev_rbd_free_config(req->config); 57 } 58 59 static int 60 bdev_rbd_decode_config(const struct spdk_json_val *values, void *out) 61 { 62 char ***map = out; 63 char **entry; 64 uint32_t i; 65 66 if (values->type == SPDK_JSON_VAL_NULL) { 67 /* treated like empty object: empty config */ 68 *map = calloc(1, sizeof(**map)); 69 if (!*map) { 70 return -1; 71 } 72 return 0; 73 } 74 75 if (values->type != SPDK_JSON_VAL_OBJECT_BEGIN) { 76 return -1; 77 } 78 79 *map = calloc(values->len + 1, sizeof(**map)); 80 if (!*map) { 81 return -1; 82 } 83 84 for (i = 0, entry = *map; i < values->len;) { 85 const struct spdk_json_val *name = &values[i + 1]; 86 const struct spdk_json_val *v = &values[i + 2]; 87 /* Here we catch errors like invalid types. */ 88 if (!(entry[0] = spdk_json_strdup(name)) || 89 !(entry[1] = spdk_json_strdup(v))) { 90 bdev_rbd_free_config(*map); 91 *map = NULL; 92 return -1; 93 } 94 i += 1 + spdk_json_val_len(v); 95 entry += 2; 96 } 97 98 return 0; 99 } 100 101 static const struct spdk_json_object_decoder rpc_create_rbd_decoders[] = { 102 {"name", offsetof(struct rpc_create_rbd, name), spdk_json_decode_string, true}, 103 {"user_id", offsetof(struct rpc_create_rbd, user_id), spdk_json_decode_string, true}, 104 {"pool_name", offsetof(struct rpc_create_rbd, pool_name), spdk_json_decode_string}, 105 {"rbd_name", offsetof(struct rpc_create_rbd, rbd_name), spdk_json_decode_string}, 106 {"block_size", offsetof(struct rpc_create_rbd, block_size), spdk_json_decode_uint32}, 107 {"config", offsetof(struct rpc_create_rbd, config), bdev_rbd_decode_config, true} 108 }; 109 110 static void 111 rpc_bdev_rbd_create(struct spdk_jsonrpc_request *request, 112 const struct spdk_json_val *params) 113 { 114 struct rpc_create_rbd req = {}; 115 struct spdk_json_write_ctx *w; 116 struct spdk_bdev *bdev; 117 int rc = 0; 118 119 if (spdk_json_decode_object(params, rpc_create_rbd_decoders, 120 SPDK_COUNTOF(rpc_create_rbd_decoders), 121 &req)) { 122 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RBD, "spdk_json_decode_object failed\n"); 123 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 124 "spdk_json_decode_object failed"); 125 goto cleanup; 126 } 127 128 rc = bdev_rbd_create(&bdev, req.name, req.user_id, req.pool_name, 129 (const char *const *)req.config, 130 req.rbd_name, 131 req.block_size); 132 if (rc) { 133 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 134 goto cleanup; 135 } 136 137 w = spdk_jsonrpc_begin_result(request); 138 spdk_json_write_string(w, spdk_bdev_get_name(bdev)); 139 spdk_jsonrpc_end_result(request, w); 140 141 cleanup: 142 free_rpc_create_rbd(&req); 143 } 144 SPDK_RPC_REGISTER("bdev_rbd_create", rpc_bdev_rbd_create, SPDK_RPC_RUNTIME) 145 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_rbd_create, construct_rbd_bdev) 146 147 struct rpc_bdev_rbd_delete { 148 char *name; 149 }; 150 151 static void 152 free_rpc_bdev_rbd_delete(struct rpc_bdev_rbd_delete *req) 153 { 154 free(req->name); 155 } 156 157 static const struct spdk_json_object_decoder rpc_bdev_rbd_delete_decoders[] = { 158 {"name", offsetof(struct rpc_bdev_rbd_delete, name), spdk_json_decode_string}, 159 }; 160 161 static void 162 _rpc_bdev_rbd_delete_cb(void *cb_arg, int bdeverrno) 163 { 164 struct spdk_jsonrpc_request *request = cb_arg; 165 struct spdk_json_write_ctx *w; 166 167 w = spdk_jsonrpc_begin_result(request); 168 spdk_json_write_bool(w, bdeverrno == 0); 169 spdk_jsonrpc_end_result(request, w); 170 } 171 172 static void 173 rpc_bdev_rbd_delete(struct spdk_jsonrpc_request *request, 174 const struct spdk_json_val *params) 175 { 176 struct rpc_bdev_rbd_delete req = {NULL}; 177 struct spdk_bdev *bdev; 178 179 if (spdk_json_decode_object(params, rpc_bdev_rbd_delete_decoders, 180 SPDK_COUNTOF(rpc_bdev_rbd_delete_decoders), 181 &req)) { 182 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 183 "spdk_json_decode_object failed"); 184 goto cleanup; 185 } 186 187 bdev = spdk_bdev_get_by_name(req.name); 188 if (bdev == NULL) { 189 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 190 goto cleanup; 191 } 192 193 bdev_rbd_delete(bdev, _rpc_bdev_rbd_delete_cb, request); 194 195 cleanup: 196 free_rpc_bdev_rbd_delete(&req); 197 } 198 SPDK_RPC_REGISTER("bdev_rbd_delete", rpc_bdev_rbd_delete, SPDK_RPC_RUNTIME) 199 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_rbd_delete, delete_rbd_bdev) 200 201 struct rpc_bdev_rbd_resize { 202 char *name; 203 uint64_t new_size; 204 }; 205 206 static const struct spdk_json_object_decoder rpc_bdev_rbd_resize_decoders[] = { 207 {"name", offsetof(struct rpc_bdev_rbd_resize, name), spdk_json_decode_string}, 208 {"new_size", offsetof(struct rpc_bdev_rbd_resize, new_size), spdk_json_decode_uint64} 209 }; 210 211 static void 212 free_rpc_bdev_rbd_resize(struct rpc_bdev_rbd_resize *req) 213 { 214 free(req->name); 215 } 216 217 static void 218 rpc_bdev_rbd_resize(struct spdk_jsonrpc_request *request, 219 const struct spdk_json_val *params) 220 { 221 struct rpc_bdev_rbd_resize req = {}; 222 struct spdk_bdev *bdev; 223 struct spdk_json_write_ctx *w; 224 int rc; 225 226 if (spdk_json_decode_object(params, rpc_bdev_rbd_resize_decoders, 227 SPDK_COUNTOF(rpc_bdev_rbd_resize_decoders), 228 &req)) { 229 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 230 "spdk_json_decode_object failed"); 231 goto cleanup; 232 } 233 234 bdev = spdk_bdev_get_by_name(req.name); 235 if (bdev == NULL) { 236 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 237 goto cleanup; 238 } 239 240 rc = bdev_rbd_resize(bdev, req.new_size); 241 if (rc) { 242 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 243 goto cleanup; 244 } 245 246 w = spdk_jsonrpc_begin_result(request); 247 spdk_json_write_bool(w, true); 248 spdk_jsonrpc_end_result(request, w); 249 cleanup: 250 free_rpc_bdev_rbd_resize(&req); 251 } 252 SPDK_RPC_REGISTER("bdev_rbd_resize", rpc_bdev_rbd_resize, SPDK_RPC_RUNTIME) 253