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_iscsi.h" 35 #include "spdk/rpc.h" 36 #include "spdk/util.h" 37 #include "spdk/string.h" 38 39 #include "spdk/log.h" 40 41 struct rpc_bdev_iscsi_create { 42 char *name; 43 char *initiator_iqn; 44 char *url; 45 }; 46 47 static const struct spdk_json_object_decoder rpc_bdev_iscsi_create_decoders[] = { 48 {"name", offsetof(struct rpc_bdev_iscsi_create, name), spdk_json_decode_string}, 49 {"initiator_iqn", offsetof(struct rpc_bdev_iscsi_create, initiator_iqn), spdk_json_decode_string}, 50 {"url", offsetof(struct rpc_bdev_iscsi_create, url), spdk_json_decode_string}, 51 }; 52 53 static void 54 free_rpc_bdev_iscsi_create(struct rpc_bdev_iscsi_create *req) 55 { 56 free(req->name); 57 free(req->initiator_iqn); 58 free(req->url); 59 } 60 61 static void 62 bdev_iscsi_create_cb(void *cb_arg, struct spdk_bdev *bdev, int status) 63 { 64 struct spdk_jsonrpc_request *request = cb_arg; 65 struct spdk_json_write_ctx *w; 66 67 if (status > 0) { 68 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 69 "iSCSI error (%d).", status); 70 } else if (status < 0) { 71 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 72 spdk_strerror(-status)); 73 } else { 74 w = spdk_jsonrpc_begin_result(request); 75 spdk_json_write_string(w, spdk_bdev_get_name(bdev)); 76 spdk_jsonrpc_end_result(request, w); 77 } 78 } 79 80 static void 81 rpc_bdev_iscsi_create(struct spdk_jsonrpc_request *request, 82 const struct spdk_json_val *params) 83 { 84 struct rpc_bdev_iscsi_create req = {}; 85 int rc = 0; 86 87 if (spdk_json_decode_object(params, rpc_bdev_iscsi_create_decoders, 88 SPDK_COUNTOF(rpc_bdev_iscsi_create_decoders), 89 &req)) { 90 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 91 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 92 "spdk_json_decode_object failed"); 93 goto cleanup; 94 } 95 96 rc = create_iscsi_disk(req.name, req.url, req.initiator_iqn, bdev_iscsi_create_cb, request); 97 if (rc) { 98 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 99 } 100 101 cleanup: 102 free_rpc_bdev_iscsi_create(&req); 103 } 104 SPDK_RPC_REGISTER("bdev_iscsi_create", rpc_bdev_iscsi_create, SPDK_RPC_RUNTIME) 105 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_iscsi_create, construct_iscsi_bdev) 106 107 struct rpc_delete_iscsi { 108 char *name; 109 }; 110 111 static void 112 free_rpc_delete_iscsi(struct rpc_delete_iscsi *r) 113 { 114 free(r->name); 115 } 116 117 static const struct spdk_json_object_decoder rpc_delete_iscsi_decoders[] = { 118 {"name", offsetof(struct rpc_delete_iscsi, name), spdk_json_decode_string}, 119 }; 120 121 static void 122 rpc_bdev_iscsi_delete_cb(void *cb_arg, int bdeverrno) 123 { 124 struct spdk_jsonrpc_request *request = cb_arg; 125 126 spdk_jsonrpc_send_bool_response(request, bdeverrno == 0); 127 } 128 129 static void 130 rpc_bdev_iscsi_delete(struct spdk_jsonrpc_request *request, 131 const struct spdk_json_val *params) 132 { 133 struct rpc_delete_iscsi req = {NULL}; 134 struct spdk_bdev *bdev; 135 136 if (spdk_json_decode_object(params, rpc_delete_iscsi_decoders, 137 SPDK_COUNTOF(rpc_delete_iscsi_decoders), 138 &req)) { 139 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 140 "spdk_json_decode_object failed"); 141 goto cleanup; 142 } 143 144 bdev = spdk_bdev_get_by_name(req.name); 145 if (bdev == NULL) { 146 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 147 goto cleanup; 148 } 149 150 delete_iscsi_disk(bdev, rpc_bdev_iscsi_delete_cb, request); 151 152 cleanup: 153 free_rpc_delete_iscsi(&req); 154 } 155 SPDK_RPC_REGISTER("bdev_iscsi_delete", rpc_bdev_iscsi_delete, SPDK_RPC_RUNTIME) 156 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_iscsi_delete, delete_iscsi_bdev) 157