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_aio.h" 35 #include "spdk/rpc.h" 36 #include "spdk/util.h" 37 #include "spdk/string.h" 38 #include "spdk/log.h" 39 40 struct rpc_construct_aio { 41 char *name; 42 char *filename; 43 uint32_t block_size; 44 }; 45 46 struct rpc_construct_aio_ctx { 47 struct rpc_construct_aio req; 48 struct spdk_jsonrpc_request *request; 49 }; 50 51 static void 52 free_rpc_construct_aio(struct rpc_construct_aio_ctx *ctx) 53 { 54 free(ctx->req.name); 55 free(ctx->req.filename); 56 free(ctx); 57 } 58 59 static const struct spdk_json_object_decoder rpc_construct_aio_decoders[] = { 60 {"name", offsetof(struct rpc_construct_aio, name), spdk_json_decode_string}, 61 {"filename", offsetof(struct rpc_construct_aio, filename), spdk_json_decode_string}, 62 {"block_size", offsetof(struct rpc_construct_aio, block_size), spdk_json_decode_uint32, true}, 63 }; 64 65 static void 66 rpc_bdev_aio_create_cb(void *cb_arg) 67 { 68 struct rpc_construct_aio_ctx *ctx = cb_arg; 69 struct spdk_jsonrpc_request *request = ctx->request; 70 struct spdk_json_write_ctx *w; 71 72 w = spdk_jsonrpc_begin_result(request); 73 spdk_json_write_string(w, ctx->req.name); 74 spdk_jsonrpc_end_result(request, w); 75 free_rpc_construct_aio(ctx); 76 } 77 78 static void 79 rpc_bdev_aio_create(struct spdk_jsonrpc_request *request, 80 const struct spdk_json_val *params) 81 { 82 struct rpc_construct_aio_ctx *ctx; 83 int rc; 84 85 ctx = calloc(1, sizeof(*ctx)); 86 if (!ctx) { 87 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 88 return; 89 } 90 91 if (spdk_json_decode_object(params, rpc_construct_aio_decoders, 92 SPDK_COUNTOF(rpc_construct_aio_decoders), 93 &ctx->req)) { 94 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 95 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 96 "spdk_json_decode_object failed"); 97 free_rpc_construct_aio(ctx); 98 return; 99 } 100 101 ctx->request = request; 102 rc = create_aio_bdev(ctx->req.name, ctx->req.filename, ctx->req.block_size); 103 if (rc) { 104 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 105 free_rpc_construct_aio(ctx); 106 return; 107 } 108 109 spdk_bdev_wait_for_examine(rpc_bdev_aio_create_cb, ctx); 110 } 111 SPDK_RPC_REGISTER("bdev_aio_create", rpc_bdev_aio_create, SPDK_RPC_RUNTIME) 112 113 struct rpc_rescan_aio { 114 char *name; 115 }; 116 117 static const struct spdk_json_object_decoder rpc_rescan_aio_decoders[] = { 118 {"name", offsetof(struct rpc_rescan_aio, name), spdk_json_decode_string}, 119 }; 120 121 static void 122 rpc_bdev_aio_rescan(struct spdk_jsonrpc_request *request, 123 const struct spdk_json_val *params) 124 { 125 struct rpc_rescan_aio req = {NULL}; 126 int bdeverrno; 127 128 if (spdk_json_decode_object(params, rpc_rescan_aio_decoders, 129 SPDK_COUNTOF(rpc_rescan_aio_decoders), 130 &req)) { 131 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 132 "spdk_json_decode_object failed"); 133 goto cleanup; 134 } 135 136 bdeverrno = bdev_aio_rescan(req.name); 137 spdk_jsonrpc_send_bool_response(request, bdeverrno); 138 139 cleanup: 140 free(req.name); 141 } 142 SPDK_RPC_REGISTER("bdev_aio_rescan", rpc_bdev_aio_rescan, SPDK_RPC_RUNTIME) 143 144 struct rpc_delete_aio { 145 char *name; 146 }; 147 148 static void 149 free_rpc_delete_aio(struct rpc_delete_aio *r) 150 { 151 free(r->name); 152 } 153 154 static const struct spdk_json_object_decoder rpc_delete_aio_decoders[] = { 155 {"name", offsetof(struct rpc_delete_aio, name), spdk_json_decode_string}, 156 }; 157 158 static void 159 _rpc_bdev_aio_delete_cb(void *cb_arg, int bdeverrno) 160 { 161 struct spdk_jsonrpc_request *request = cb_arg; 162 163 if (bdeverrno == 0) { 164 spdk_jsonrpc_send_bool_response(request, true); 165 } else { 166 spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno)); 167 } 168 } 169 170 static void 171 rpc_bdev_aio_delete(struct spdk_jsonrpc_request *request, 172 const struct spdk_json_val *params) 173 { 174 struct rpc_delete_aio req = {NULL}; 175 176 if (spdk_json_decode_object(params, rpc_delete_aio_decoders, 177 SPDK_COUNTOF(rpc_delete_aio_decoders), 178 &req)) { 179 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 180 "spdk_json_decode_object failed"); 181 goto cleanup; 182 } 183 184 bdev_aio_delete(req.name, _rpc_bdev_aio_delete_cb, request); 185 186 cleanup: 187 free_rpc_delete_aio(&req); 188 } 189 SPDK_RPC_REGISTER("bdev_aio_delete", rpc_bdev_aio_delete, SPDK_RPC_RUNTIME) 190