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/stdinc.h" 35 36 #include "vbdev_zone_block.h" 37 38 #include "spdk/util.h" 39 #include "spdk/string.h" 40 #include "spdk/rpc.h" 41 42 #include "spdk/log.h" 43 44 struct rpc_construct_zone_block { 45 char *name; 46 char *base_bdev; 47 uint64_t zone_capacity; 48 uint64_t optimal_open_zones; 49 }; 50 51 static void 52 free_rpc_construct_zone_block(struct rpc_construct_zone_block *req) 53 { 54 free(req->name); 55 free(req->base_bdev); 56 } 57 58 static const struct spdk_json_object_decoder rpc_construct_zone_block_decoders[] = { 59 {"name", offsetof(struct rpc_construct_zone_block, name), spdk_json_decode_string}, 60 {"base_bdev", offsetof(struct rpc_construct_zone_block, base_bdev), spdk_json_decode_string}, 61 {"zone_capacity", offsetof(struct rpc_construct_zone_block, zone_capacity), spdk_json_decode_uint64}, 62 {"optimal_open_zones", offsetof(struct rpc_construct_zone_block, optimal_open_zones), spdk_json_decode_uint64}, 63 }; 64 65 static void 66 rpc_zone_block_create(struct spdk_jsonrpc_request *request, 67 const struct spdk_json_val *params) 68 { 69 struct rpc_construct_zone_block req = {}; 70 struct spdk_json_write_ctx *w; 71 int rc; 72 73 if (spdk_json_decode_object(params, rpc_construct_zone_block_decoders, 74 SPDK_COUNTOF(rpc_construct_zone_block_decoders), 75 &req)) { 76 SPDK_ERRLOG("Failed to decode block create parameters"); 77 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 78 "Invalid parameters"); 79 goto cleanup; 80 } 81 82 rc = vbdev_zone_block_create(req.base_bdev, req.name, req.zone_capacity, 83 req.optimal_open_zones); 84 if (rc) { 85 SPDK_ERRLOG("Failed to create block zoned vbdev: %s", spdk_strerror(-rc)); 86 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 87 "Failed to create block zoned vbdev: %s", 88 spdk_strerror(-rc)); 89 goto cleanup; 90 } 91 92 w = spdk_jsonrpc_begin_result(request); 93 spdk_json_write_string(w, req.name); 94 spdk_jsonrpc_end_result(request, w); 95 96 cleanup: 97 free_rpc_construct_zone_block(&req); 98 } 99 SPDK_RPC_REGISTER("bdev_zone_block_create", rpc_zone_block_create, SPDK_RPC_RUNTIME) 100 101 struct rpc_delete_zone_block { 102 char *name; 103 }; 104 105 static void 106 free_rpc_delete_zone_block(struct rpc_delete_zone_block *req) 107 { 108 free(req->name); 109 } 110 111 static const struct spdk_json_object_decoder rpc_delete_zone_block_decoders[] = { 112 {"name", offsetof(struct rpc_delete_zone_block, name), spdk_json_decode_string}, 113 }; 114 115 static void 116 _rpc_delete_zone_block_cb(void *cb_ctx, int rc) 117 { 118 struct spdk_jsonrpc_request *request = cb_ctx; 119 120 spdk_jsonrpc_send_bool_response(request, rc == 0); 121 } 122 123 static void 124 rpc_zone_block_delete(struct spdk_jsonrpc_request *request, 125 const struct spdk_json_val *params) 126 { 127 struct rpc_delete_zone_block attrs = {}; 128 129 if (spdk_json_decode_object(params, rpc_delete_zone_block_decoders, 130 SPDK_COUNTOF(rpc_delete_zone_block_decoders), 131 &attrs)) { 132 SPDK_ERRLOG("Failed to decode block delete parameters"); 133 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 134 "Invalid parameters"); 135 goto cleanup; 136 } 137 138 vbdev_zone_block_delete(attrs.name, _rpc_delete_zone_block_cb, request); 139 140 cleanup: 141 free_rpc_delete_zone_block(&attrs); 142 } 143 SPDK_RPC_REGISTER("bdev_zone_block_delete", rpc_zone_block_delete, SPDK_RPC_RUNTIME) 144