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 "spdk/rpc.h" 35 #include "spdk/bdev.h" 36 #include "bdev_raid.h" 37 #include "spdk/util.h" 38 #include "spdk/string.h" 39 #include "spdk_internal/log.h" 40 #include "spdk/env.h" 41 42 #define RPC_MAX_BASE_BDEVS 255 43 44 SPDK_LOG_REGISTER_COMPONENT("raidrpc", SPDK_LOG_RAID_RPC) 45 46 /* 47 * Input structure for get_raid_bdevs RPC 48 */ 49 struct rpc_get_raid_bdevs { 50 /* category - all or online or configuring or offline */ 51 char *category; 52 }; 53 54 /* 55 * brief: 56 * free_rpc_get_raids function frees RPC get_raids related parameters 57 * params: 58 * req - pointer to RPC request 59 * returns: 60 * none 61 */ 62 static void 63 free_rpc_get_raid_bdevs(struct rpc_get_raid_bdevs *req) 64 { 65 free(req->category); 66 } 67 68 /* 69 * Decoder object for RPC get_raids 70 */ 71 static const struct spdk_json_object_decoder rpc_get_raid_bdevs_decoders[] = { 72 {"category", offsetof(struct rpc_get_raid_bdevs, category), spdk_json_decode_string}, 73 }; 74 75 /* 76 * brief: 77 * spdk_rpc_get_raids function is the RPC for get_raids. This is used to list 78 * all the raid bdev names based on the input category requested. Category should be 79 * one of "all", "online", "configuring" or "offline". "all" means all the raids 80 * whether they are online or configuring or offline. "online" is the raid bdev which 81 * is registered with bdev layer. "configuring" is the raid bdev which does not have 82 * full configuration discovered yet. "offline" is the raid bdev which is not 83 * registered with bdev as of now and it has encountered any error or user has 84 * requested to offline the raid. 85 * params: 86 * requuest - pointer to json rpc request 87 * params - pointer to request parameters 88 * returns: 89 * none 90 */ 91 static void 92 spdk_rpc_get_raid_bdevs(struct spdk_jsonrpc_request *request, 93 const struct spdk_json_val *params) 94 { 95 struct rpc_get_raid_bdevs req = {}; 96 struct spdk_json_write_ctx *w; 97 struct raid_bdev *raid_bdev; 98 99 if (spdk_json_decode_object(params, rpc_get_raid_bdevs_decoders, 100 SPDK_COUNTOF(rpc_get_raid_bdevs_decoders), 101 &req)) { 102 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 103 "spdk_json_decode_object failed"); 104 goto cleanup; 105 } 106 107 if (!(strcmp(req.category, "all") == 0 || 108 strcmp(req.category, "online") == 0 || 109 strcmp(req.category, "configuring") == 0 || 110 strcmp(req.category, "offline") == 0)) { 111 spdk_jsonrpc_send_error_response(request, -EINVAL, spdk_strerror(EINVAL)); 112 goto cleanup; 113 } 114 115 w = spdk_jsonrpc_begin_result(request); 116 spdk_json_write_array_begin(w); 117 118 /* Get raid bdev list based on the category requested */ 119 if (strcmp(req.category, "all") == 0) { 120 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 121 spdk_json_write_string(w, raid_bdev->bdev.name); 122 } 123 } else if (strcmp(req.category, "online") == 0) { 124 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_configured_list, state_link) { 125 spdk_json_write_string(w, raid_bdev->bdev.name); 126 } 127 } else if (strcmp(req.category, "configuring") == 0) { 128 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_configuring_list, state_link) { 129 spdk_json_write_string(w, raid_bdev->bdev.name); 130 } 131 } else { 132 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_offline_list, state_link) { 133 spdk_json_write_string(w, raid_bdev->bdev.name); 134 } 135 } 136 spdk_json_write_array_end(w); 137 spdk_jsonrpc_end_result(request, w); 138 139 cleanup: 140 free_rpc_get_raid_bdevs(&req); 141 } 142 SPDK_RPC_REGISTER("get_raid_bdevs", spdk_rpc_get_raid_bdevs, SPDK_RPC_RUNTIME) 143 144 /* 145 * Base bdevs in RPC construct_raid 146 */ 147 struct rpc_construct_raid_base_bdevs { 148 /* Number of base bdevs */ 149 size_t num_base_bdevs; 150 151 /* List of base bdevs names */ 152 char *base_bdevs[RPC_MAX_BASE_BDEVS]; 153 }; 154 155 /* 156 * Input structure for RPC construct_raid 157 */ 158 struct rpc_construct_raid_bdev { 159 /* Raid bdev name */ 160 char *name; 161 162 /* RAID strip size KB, 'strip_size' is deprecated. */ 163 uint32_t strip_size; 164 uint32_t strip_size_kb; 165 166 /* RAID raid level */ 167 uint8_t raid_level; 168 169 /* Base bdevs information */ 170 struct rpc_construct_raid_base_bdevs base_bdevs; 171 }; 172 173 /* 174 * brief: 175 * free_rpc_construct_raid_bdev function is to free RPC construct_raid_bdev related parameters 176 * params: 177 * req - pointer to RPC request 178 * returns: 179 * none 180 */ 181 static void 182 free_rpc_construct_raid_bdev(struct rpc_construct_raid_bdev *req) 183 { 184 free(req->name); 185 for (size_t i = 0; i < req->base_bdevs.num_base_bdevs; i++) { 186 free(req->base_bdevs.base_bdevs[i]); 187 } 188 } 189 190 /* 191 * Decoder function for RPC construct_raid_bdev to decode base bdevs list 192 */ 193 static int 194 decode_base_bdevs(const struct spdk_json_val *val, void *out) 195 { 196 struct rpc_construct_raid_base_bdevs *base_bdevs = out; 197 return spdk_json_decode_array(val, spdk_json_decode_string, base_bdevs->base_bdevs, 198 RPC_MAX_BASE_BDEVS, &base_bdevs->num_base_bdevs, sizeof(char *)); 199 } 200 201 /* 202 * Decoder object for RPC construct_raid 203 */ 204 /* Note: strip_size is deprecated, one of the two options must be specified but not both. */ 205 static const struct spdk_json_object_decoder rpc_construct_raid_bdev_decoders[] = { 206 {"name", offsetof(struct rpc_construct_raid_bdev, name), spdk_json_decode_string}, 207 {"strip_size", offsetof(struct rpc_construct_raid_bdev, strip_size), spdk_json_decode_uint32, true}, 208 {"strip_size_kb", offsetof(struct rpc_construct_raid_bdev, strip_size_kb), spdk_json_decode_uint32, true}, 209 {"raid_level", offsetof(struct rpc_construct_raid_bdev, raid_level), spdk_json_decode_uint32}, 210 {"base_bdevs", offsetof(struct rpc_construct_raid_bdev, base_bdevs), decode_base_bdevs}, 211 }; 212 213 /* 214 * brief: 215 * spdk_rpc_construct_raid_bdev function is the RPC for construct_raids. It takes 216 * input as raid bdev name, raid level, strip size in KB and list of base bdev names. 217 * params: 218 * requuest - pointer to json rpc request 219 * params - pointer to request parameters 220 * returns: 221 * none 222 */ 223 static void 224 spdk_rpc_construct_raid_bdev(struct spdk_jsonrpc_request *request, 225 const struct spdk_json_val *params) 226 { 227 struct rpc_construct_raid_bdev req = {}; 228 struct spdk_json_write_ctx *w; 229 struct raid_bdev_config *raid_cfg; 230 int rc; 231 232 if (spdk_json_decode_object(params, rpc_construct_raid_bdev_decoders, 233 SPDK_COUNTOF(rpc_construct_raid_bdev_decoders), 234 &req)) { 235 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 236 "spdk_json_decode_object failed"); 237 goto cleanup; 238 } 239 240 if (req.strip_size == 0 && req.strip_size_kb == 0) { 241 spdk_jsonrpc_send_error_response(request, EINVAL, "strip size not specified"); 242 goto cleanup; 243 } else if (req.strip_size > 0 && req.strip_size_kb > 0) { 244 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 245 "please use only one strip size option"); 246 goto cleanup; 247 } else if (req.strip_size > 0 && req.strip_size_kb == 0) { 248 SPDK_ERRLOG("the rpc param strip_size is deprecated.\n"); 249 req.strip_size_kb = req.strip_size; 250 } 251 252 rc = raid_bdev_config_add(req.name, req.strip_size_kb, req.base_bdevs.num_base_bdevs, 253 req.raid_level, 254 &raid_cfg); 255 if (rc != 0) { 256 spdk_jsonrpc_send_error_response_fmt(request, rc, 257 "Failed to add RAID bdev config %s: %s", 258 req.name, spdk_strerror(-rc)); 259 goto cleanup; 260 } 261 262 for (size_t i = 0; i < req.base_bdevs.num_base_bdevs; i++) { 263 rc = raid_bdev_config_add_base_bdev(raid_cfg, req.base_bdevs.base_bdevs[i], i); 264 if (rc != 0) { 265 raid_bdev_config_cleanup(raid_cfg); 266 spdk_jsonrpc_send_error_response_fmt(request, rc, 267 "Failed to add base bdev %s to RAID bdev config %s: %s", 268 req.base_bdevs.base_bdevs[i], req.name, 269 spdk_strerror(-rc)); 270 goto cleanup; 271 } 272 } 273 274 rc = raid_bdev_create(raid_cfg); 275 if (rc != 0) { 276 raid_bdev_config_cleanup(raid_cfg); 277 spdk_jsonrpc_send_error_response_fmt(request, rc, 278 "Failed to create RAID bdev %s: %s", 279 req.name, spdk_strerror(-rc)); 280 goto cleanup; 281 } 282 283 rc = raid_bdev_add_base_devices(raid_cfg); 284 if (rc != 0) { 285 spdk_jsonrpc_send_error_response_fmt(request, rc, 286 "Failed to add any base bdev to RAID bdev %s: %s", 287 req.name, spdk_strerror(-rc)); 288 goto cleanup; 289 } 290 291 w = spdk_jsonrpc_begin_result(request); 292 spdk_json_write_bool(w, true); 293 spdk_jsonrpc_end_result(request, w); 294 295 cleanup: 296 free_rpc_construct_raid_bdev(&req); 297 } 298 SPDK_RPC_REGISTER("construct_raid_bdev", spdk_rpc_construct_raid_bdev, SPDK_RPC_RUNTIME) 299 300 /* 301 * Input structure for RPC destroy_raid 302 */ 303 struct rpc_destroy_raid_bdev { 304 /* raid bdev name */ 305 char *name; 306 }; 307 308 /* 309 * brief: 310 * free_rpc_destroy_raid_bdev function is used to free RPC destroy_raid_bdev related parameters 311 * params: 312 * req - pointer to RPC request 313 * params: 314 * none 315 */ 316 static void 317 free_rpc_destroy_raid_bdev(struct rpc_destroy_raid_bdev *req) 318 { 319 free(req->name); 320 } 321 322 /* 323 * Decoder object for RPC destroy_raid 324 */ 325 static const struct spdk_json_object_decoder rpc_destroy_raid_bdev_decoders[] = { 326 {"name", offsetof(struct rpc_destroy_raid_bdev, name), spdk_json_decode_string}, 327 }; 328 329 struct rpc_destroy_raid_bdev_ctx { 330 struct rpc_destroy_raid_bdev req; 331 struct raid_bdev_config *raid_cfg; 332 struct spdk_jsonrpc_request *request; 333 }; 334 335 /* 336 * brief: 337 * params: 338 * cb_arg - pointer to the callback context. 339 * rc - return code of the destruction of the raid bdev. 340 * returns: 341 * none 342 */ 343 static void 344 destroy_raid_bdev_done(void *cb_arg, int rc) 345 { 346 struct rpc_destroy_raid_bdev_ctx *ctx = cb_arg; 347 struct raid_bdev_config *raid_cfg; 348 struct spdk_jsonrpc_request *request = ctx->request; 349 struct spdk_json_write_ctx *w; 350 351 if (rc != 0) { 352 SPDK_ERRLOG("Failed to destroy raid bdev %s (%d): %s\n", 353 ctx->req.name, rc, spdk_strerror(-rc)); 354 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 355 spdk_strerror(-rc)); 356 goto exit; 357 } 358 359 raid_cfg = ctx->raid_cfg; 360 assert(raid_cfg->raid_bdev == NULL); 361 362 raid_bdev_config_cleanup(raid_cfg); 363 364 w = spdk_jsonrpc_begin_result(request); 365 spdk_json_write_bool(w, true); 366 spdk_jsonrpc_end_result(request, w); 367 exit: 368 free_rpc_destroy_raid_bdev(&ctx->req); 369 free(ctx); 370 } 371 372 /* 373 * brief: 374 * spdk_rpc_destroy_raid_bdev function is the RPC for destroy_raid. It takes raid 375 * name as input and destroy that raid bdev including freeing the base bdev 376 * resources. 377 * params: 378 * requuest - pointer to json rpc request 379 * params - pointer to request parameters 380 * returns: 381 * none 382 */ 383 static void 384 spdk_rpc_destroy_raid_bdev(struct spdk_jsonrpc_request *request, 385 const struct spdk_json_val *params) 386 { 387 struct rpc_destroy_raid_bdev_ctx *ctx; 388 389 ctx = calloc(1, sizeof(*ctx)); 390 if (!ctx) { 391 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 392 return; 393 } 394 395 if (spdk_json_decode_object(params, rpc_destroy_raid_bdev_decoders, 396 SPDK_COUNTOF(rpc_destroy_raid_bdev_decoders), 397 &ctx->req)) { 398 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 399 "spdk_json_decode_object failed"); 400 goto cleanup; 401 } 402 403 ctx->raid_cfg = raid_bdev_config_find_by_name(ctx->req.name); 404 if (ctx->raid_cfg == NULL) { 405 spdk_jsonrpc_send_error_response_fmt(request, ENODEV, 406 "raid bdev %s is not found in config", 407 ctx->req.name); 408 goto cleanup; 409 } 410 411 ctx->request = request; 412 413 /* Remove all the base bdevs from this raid bdev before destroying the raid bdev */ 414 raid_bdev_remove_base_devices(ctx->raid_cfg, destroy_raid_bdev_done, ctx); 415 416 return; 417 418 cleanup: 419 free_rpc_destroy_raid_bdev(&ctx->req); 420 free(ctx); 421 } 422 SPDK_RPC_REGISTER("destroy_raid_bdev", spdk_rpc_destroy_raid_bdev, SPDK_RPC_RUNTIME) 423