1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) Intel Corporation. 3 * All rights reserved. 4 */ 5 6 #include "spdk/stdinc.h" 7 8 #include "vbdev_zone_block.h" 9 10 #include "spdk/util.h" 11 #include "spdk/string.h" 12 #include "spdk/rpc.h" 13 14 #include "spdk/log.h" 15 16 struct rpc_construct_zone_block { 17 char *name; 18 char *base_bdev; 19 uint64_t zone_capacity; 20 uint64_t optimal_open_zones; 21 }; 22 23 static void 24 free_rpc_construct_zone_block(struct rpc_construct_zone_block *req) 25 { 26 free(req->name); 27 free(req->base_bdev); 28 } 29 30 static const struct spdk_json_object_decoder rpc_construct_zone_block_decoders[] = { 31 {"name", offsetof(struct rpc_construct_zone_block, name), spdk_json_decode_string}, 32 {"base_bdev", offsetof(struct rpc_construct_zone_block, base_bdev), spdk_json_decode_string}, 33 {"zone_capacity", offsetof(struct rpc_construct_zone_block, zone_capacity), spdk_json_decode_uint64}, 34 {"optimal_open_zones", offsetof(struct rpc_construct_zone_block, optimal_open_zones), spdk_json_decode_uint64}, 35 }; 36 37 static void 38 rpc_zone_block_create(struct spdk_jsonrpc_request *request, 39 const struct spdk_json_val *params) 40 { 41 struct rpc_construct_zone_block req = {}; 42 struct spdk_json_write_ctx *w; 43 int rc; 44 45 if (spdk_json_decode_object(params, rpc_construct_zone_block_decoders, 46 SPDK_COUNTOF(rpc_construct_zone_block_decoders), 47 &req)) { 48 SPDK_ERRLOG("Failed to decode block create parameters"); 49 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 50 "Invalid parameters"); 51 goto cleanup; 52 } 53 54 rc = vbdev_zone_block_create(req.base_bdev, req.name, req.zone_capacity, 55 req.optimal_open_zones); 56 if (rc) { 57 SPDK_ERRLOG("Failed to create block zoned vbdev: %s", spdk_strerror(-rc)); 58 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 59 "Failed to create block zoned vbdev: %s", 60 spdk_strerror(-rc)); 61 goto cleanup; 62 } 63 64 w = spdk_jsonrpc_begin_result(request); 65 spdk_json_write_string(w, req.name); 66 spdk_jsonrpc_end_result(request, w); 67 68 cleanup: 69 free_rpc_construct_zone_block(&req); 70 } 71 SPDK_RPC_REGISTER("bdev_zone_block_create", rpc_zone_block_create, SPDK_RPC_RUNTIME) 72 73 struct rpc_delete_zone_block { 74 char *name; 75 }; 76 77 static void 78 free_rpc_delete_zone_block(struct rpc_delete_zone_block *req) 79 { 80 free(req->name); 81 } 82 83 static const struct spdk_json_object_decoder rpc_delete_zone_block_decoders[] = { 84 {"name", offsetof(struct rpc_delete_zone_block, name), spdk_json_decode_string}, 85 }; 86 87 static void 88 _rpc_delete_zone_block_cb(void *cb_ctx, int rc) 89 { 90 struct spdk_jsonrpc_request *request = cb_ctx; 91 92 spdk_jsonrpc_send_bool_response(request, rc == 0); 93 } 94 95 static void 96 rpc_zone_block_delete(struct spdk_jsonrpc_request *request, 97 const struct spdk_json_val *params) 98 { 99 struct rpc_delete_zone_block attrs = {}; 100 101 if (spdk_json_decode_object(params, rpc_delete_zone_block_decoders, 102 SPDK_COUNTOF(rpc_delete_zone_block_decoders), 103 &attrs)) { 104 SPDK_ERRLOG("Failed to decode block delete parameters"); 105 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 106 "Invalid parameters"); 107 goto cleanup; 108 } 109 110 vbdev_zone_block_delete(attrs.name, _rpc_delete_zone_block_cb, request); 111 112 cleanup: 113 free_rpc_delete_zone_block(&attrs); 114 } 115 SPDK_RPC_REGISTER("bdev_zone_block_delete", rpc_zone_block_delete, SPDK_RPC_RUNTIME) 116