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 "bdev_nvme.h" 37 38 #include "spdk/string.h" 39 #include "spdk/rpc.h" 40 #include "spdk/util.h" 41 #include "spdk/nvme.h" 42 43 #include "spdk/log.h" 44 45 struct rpc_nvme_cuse_register { 46 char *name; 47 }; 48 49 static void 50 free_rpc_nvme_cuse_register(struct rpc_nvme_cuse_register *req) 51 { 52 free(req->name); 53 } 54 55 static const struct spdk_json_object_decoder rpc_nvme_cuse_register_decoders[] = { 56 {"name", offsetof(struct rpc_nvme_cuse_register, name), spdk_json_decode_string}, 57 }; 58 59 static void 60 rpc_nvme_cuse_register(struct spdk_jsonrpc_request *request, 61 const struct spdk_json_val *params) 62 { 63 struct rpc_nvme_cuse_register req = {}; 64 struct nvme_bdev_ctrlr *bdev_ctrlr = NULL; 65 int rc; 66 67 if (spdk_json_decode_object(params, rpc_nvme_cuse_register_decoders, 68 SPDK_COUNTOF(rpc_nvme_cuse_register_decoders), 69 &req)) { 70 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 71 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 72 "spdk_json_decode_object failed"); 73 goto cleanup; 74 } 75 76 bdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name); 77 if (!bdev_ctrlr) { 78 SPDK_ERRLOG("No such controller\n"); 79 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 80 goto cleanup; 81 } 82 83 rc = spdk_nvme_cuse_register(bdev_ctrlr->ctrlr); 84 if (rc) { 85 SPDK_ERRLOG("Failed to register CUSE devices: %s\n", spdk_strerror(-rc)); 86 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 87 goto cleanup; 88 } 89 90 spdk_jsonrpc_send_bool_response(request, true); 91 92 cleanup: 93 free_rpc_nvme_cuse_register(&req); 94 } 95 SPDK_RPC_REGISTER("bdev_nvme_cuse_register", rpc_nvme_cuse_register, SPDK_RPC_RUNTIME) 96 97 struct rpc_nvme_cuse_unregister { 98 char *name; 99 }; 100 101 static void 102 free_rpc_nvme_cuse_unregister(struct rpc_nvme_cuse_unregister *req) 103 { 104 free(req->name); 105 } 106 107 static const struct spdk_json_object_decoder rpc_nvme_cuse_unregister_decoders[] = { 108 {"name", offsetof(struct rpc_nvme_cuse_unregister, name), spdk_json_decode_string, true}, 109 }; 110 111 static void 112 rpc_nvme_cuse_unregister(struct spdk_jsonrpc_request *request, 113 const struct spdk_json_val *params) 114 { 115 struct rpc_nvme_cuse_unregister req = {}; 116 struct nvme_bdev_ctrlr *bdev_ctrlr = NULL; 117 int rc; 118 119 if (spdk_json_decode_object(params, rpc_nvme_cuse_unregister_decoders, 120 SPDK_COUNTOF(rpc_nvme_cuse_unregister_decoders), 121 &req)) { 122 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 123 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 124 "spdk_json_decode_object failed"); 125 goto cleanup; 126 } 127 128 bdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name); 129 if (!bdev_ctrlr) { 130 SPDK_ERRLOG("No such controller\n"); 131 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 132 goto cleanup; 133 } 134 135 rc = spdk_nvme_cuse_unregister(bdev_ctrlr->ctrlr); 136 if (rc) { 137 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 138 goto cleanup; 139 } 140 141 spdk_jsonrpc_send_bool_response(request, true); 142 143 cleanup: 144 free_rpc_nvme_cuse_unregister(&req); 145 } 146 SPDK_RPC_REGISTER("bdev_nvme_cuse_unregister", rpc_nvme_cuse_unregister, SPDK_RPC_RUNTIME) 147