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_uring.h" 35 #include "spdk/rpc.h" 36 #include "spdk/util.h" 37 #include "spdk/string.h" 38 #include "spdk_internal/log.h" 39 40 /* Structure to hold the parameters for this RPC method. */ 41 struct rpc_create_uring { 42 char *name; 43 char *filename; 44 }; 45 46 /* Free the allocated memory resource after the RPC handling. */ 47 static void 48 free_rpc_create_uring(struct rpc_create_uring *r) 49 { 50 free(r->name); 51 free(r->filename); 52 } 53 54 /* Structure to decode the input parameters for this RPC method. */ 55 static const struct spdk_json_object_decoder rpc_create_uring_decoders[] = { 56 {"name", offsetof(struct rpc_create_uring, name), spdk_json_decode_string}, 57 {"filename", offsetof(struct rpc_create_uring, filename), spdk_json_decode_string}, 58 }; 59 60 /* Decode the parameters for this RPC method and properly create the uring 61 * device. Error status returned in the failed cases. 62 */ 63 static void 64 spdk_rpc_bdev_uring_create(struct spdk_jsonrpc_request *request, 65 const struct spdk_json_val *params) 66 { 67 struct rpc_create_uring req = {}; 68 struct spdk_json_write_ctx *w; 69 struct spdk_bdev *bdev; 70 71 if (spdk_json_decode_object(params, rpc_create_uring_decoders, 72 SPDK_COUNTOF(rpc_create_uring_decoders), 73 &req)) { 74 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 75 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 76 "spdk_json_decode_object failed"); 77 goto cleanup; 78 } 79 80 bdev = create_uring_bdev(req.name, req.filename); 81 if (!bdev) { 82 SPDK_ERRLOG("Unable to create URING bdev from file %s\n", req.filename); 83 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 84 "Unable to create URING bdev."); 85 goto cleanup; 86 } 87 88 w = spdk_jsonrpc_begin_result(request); 89 spdk_json_write_string(w, req.name); 90 spdk_jsonrpc_end_result(request, w); 91 92 cleanup: 93 free_rpc_create_uring(&req); 94 } 95 SPDK_RPC_REGISTER("bdev_uring_create", spdk_rpc_bdev_uring_create, SPDK_RPC_RUNTIME) 96 97 struct rpc_delete_uring { 98 char *name; 99 }; 100 101 static void 102 free_rpc_delete_uring(struct rpc_delete_uring *req) 103 { 104 free(req->name); 105 } 106 107 static const struct spdk_json_object_decoder rpc_delete_uring_decoders[] = { 108 {"name", offsetof(struct rpc_delete_uring, name), spdk_json_decode_string}, 109 }; 110 111 static void 112 _spdk_rpc_bdev_uring_delete_cb(void *cb_arg, int bdeverrno) 113 { 114 struct spdk_jsonrpc_request *request = cb_arg; 115 struct spdk_json_write_ctx *w = spdk_jsonrpc_begin_result(request); 116 117 spdk_json_write_bool(w, bdeverrno == 0); 118 spdk_jsonrpc_end_result(request, w); 119 120 } 121 122 static void 123 spdk_rpc_bdev_uring_delete(struct spdk_jsonrpc_request *request, 124 const struct spdk_json_val *params) 125 { 126 struct rpc_delete_uring req = {NULL}; 127 struct spdk_bdev *bdev; 128 129 if (spdk_json_decode_object(params, rpc_delete_uring_decoders, 130 SPDK_COUNTOF(rpc_delete_uring_decoders), 131 &req)) { 132 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 133 "spdk_json_decode_object failed"); 134 goto cleanup; 135 } 136 137 bdev = spdk_bdev_get_by_name(req.name); 138 if (bdev == NULL) { 139 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 140 goto cleanup; 141 } 142 143 delete_uring_bdev(bdev, _spdk_rpc_bdev_uring_delete_cb, request); 144 145 cleanup: 146 free_rpc_delete_uring(&req); 147 } 148 SPDK_RPC_REGISTER("bdev_uring_delete", spdk_rpc_bdev_uring_delete, SPDK_RPC_RUNTIME) 149