1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2018 Intel Corporation. 3 * Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 4 * All rights reserved. 5 */ 6 7 #include "vbdev_compress.h" 8 #include "spdk/rpc.h" 9 #include "spdk/util.h" 10 #include "spdk/string.h" 11 #include "spdk/log.h" 12 13 struct rpc_bdev_compress_get_orphans { 14 char *name; 15 }; 16 17 static void 18 free_rpc_bdev_compress_get_orphans(struct rpc_bdev_compress_get_orphans *r) 19 { 20 free(r->name); 21 } 22 23 static const struct spdk_json_object_decoder rpc_bdev_compress_get_orphans_decoders[] = { 24 {"name", offsetof(struct rpc_bdev_compress_get_orphans, name), spdk_json_decode_string, true}, 25 }; 26 27 static void 28 rpc_bdev_compress_get_orphans(struct spdk_jsonrpc_request *request, 29 const struct spdk_json_val *params) 30 { 31 struct rpc_bdev_compress_get_orphans req = {}; 32 struct spdk_json_write_ctx *w; 33 struct vbdev_compress *comp_bdev; 34 bool found = false; 35 36 37 if (params && spdk_json_decode_object(params, rpc_bdev_compress_get_orphans_decoders, 38 SPDK_COUNTOF(rpc_bdev_compress_get_orphans_decoders), 39 &req)) { 40 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 41 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 42 "spdk_json_decode_object failed"); 43 free_rpc_bdev_compress_get_orphans(&req); 44 return; 45 } 46 47 if (req.name) { 48 if (compress_has_orphan(req.name) == false) { 49 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 50 free_rpc_bdev_compress_get_orphans(&req); 51 return; 52 } 53 found = true; 54 } 55 56 w = spdk_jsonrpc_begin_result(request); 57 spdk_json_write_array_begin(w); 58 if (found) { 59 spdk_json_write_string(w, req.name); 60 } else { 61 for (comp_bdev = compress_bdev_first(); comp_bdev != NULL; 62 comp_bdev = compress_bdev_next(comp_bdev)) { 63 if (compress_has_orphan(compress_get_name(comp_bdev))) { 64 spdk_json_write_string(w, compress_get_name(comp_bdev)); 65 } 66 } 67 } 68 spdk_json_write_array_end(w); 69 spdk_jsonrpc_end_result(request, w); 70 free_rpc_bdev_compress_get_orphans(&req); 71 } 72 SPDK_RPC_REGISTER("bdev_compress_get_orphans", rpc_bdev_compress_get_orphans, SPDK_RPC_RUNTIME) 73 74 /* Structure to hold the parameters for this RPC method. */ 75 struct rpc_construct_compress { 76 char *base_bdev_name; 77 char *pm_path; 78 uint32_t lb_size; 79 }; 80 81 struct rpc_bdev_compress_create_ctx { 82 struct rpc_construct_compress req; 83 struct spdk_jsonrpc_request *request; 84 }; 85 86 /* Free the allocated memory resource after the RPC handling. */ 87 static void 88 free_rpc_construct_compress(struct rpc_bdev_compress_create_ctx *ctx) 89 { 90 struct rpc_construct_compress *req; 91 92 assert(ctx != NULL); 93 94 req = &ctx->req; 95 96 free(req->base_bdev_name); 97 free(req->pm_path); 98 99 free(ctx); 100 } 101 102 /* Structure to decode the input parameters for this RPC method. */ 103 static const struct spdk_json_object_decoder rpc_construct_compress_decoders[] = { 104 {"base_bdev_name", offsetof(struct rpc_construct_compress, base_bdev_name), spdk_json_decode_string}, 105 {"pm_path", offsetof(struct rpc_construct_compress, pm_path), spdk_json_decode_string}, 106 {"lb_size", offsetof(struct rpc_construct_compress, lb_size), spdk_json_decode_uint32, true}, 107 }; 108 109 static void 110 rpc_bdev_compress_create_cb(void *_ctx, int status) 111 { 112 struct rpc_bdev_compress_create_ctx *ctx = _ctx; 113 struct rpc_construct_compress *req = &ctx->req; 114 struct spdk_jsonrpc_request *request = ctx->request; 115 struct spdk_json_write_ctx *w; 116 char *name; 117 118 if (status != 0) { 119 spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status)); 120 } else { 121 w = spdk_jsonrpc_begin_result(request); 122 name = spdk_sprintf_alloc("COMP_%s", req->base_bdev_name); 123 spdk_json_write_string(w, name); 124 spdk_jsonrpc_end_result(request, w); 125 free(name); 126 } 127 128 free_rpc_construct_compress(ctx); 129 } 130 131 /* Decode the parameters for this RPC method and properly construct the compress 132 * device. Error status returned in the failed cases. 133 */ 134 static void 135 rpc_bdev_compress_create(struct spdk_jsonrpc_request *request, 136 const struct spdk_json_val *params) 137 { 138 struct rpc_bdev_compress_create_ctx *ctx; 139 struct rpc_construct_compress *req; 140 int rc; 141 142 ctx = calloc(1, sizeof(*ctx)); 143 if (ctx == NULL) { 144 SPDK_ERRLOG("failed to alloc compress bdev creation contexts\n"); 145 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 146 return; 147 } 148 149 req = &ctx->req; 150 151 if (spdk_json_decode_object(params, rpc_construct_compress_decoders, 152 SPDK_COUNTOF(rpc_construct_compress_decoders), 153 req)) { 154 SPDK_DEBUGLOG(vbdev_compress, "spdk_json_decode_object failed\n"); 155 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_PARSE_ERROR, 156 "spdk_json_decode_object failed"); 157 goto cleanup; 158 } 159 160 rc = create_compress_bdev(req->base_bdev_name, req->pm_path, req->lb_size, 161 rpc_bdev_compress_create_cb, ctx); 162 if (rc != 0) { 163 if (rc == -EBUSY) { 164 spdk_jsonrpc_send_error_response(request, rc, "Base bdev already in use for compression."); 165 } else { 166 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 167 } 168 goto cleanup; 169 } 170 171 ctx->request = request; 172 return; 173 174 cleanup: 175 free_rpc_construct_compress(ctx); 176 } 177 SPDK_RPC_REGISTER("bdev_compress_create", rpc_bdev_compress_create, SPDK_RPC_RUNTIME) 178 179 struct rpc_delete_compress { 180 char *name; 181 }; 182 183 static void 184 free_rpc_delete_compress(struct rpc_delete_compress *req) 185 { 186 free(req->name); 187 } 188 189 static const struct spdk_json_object_decoder rpc_delete_compress_decoders[] = { 190 {"name", offsetof(struct rpc_delete_compress, name), spdk_json_decode_string}, 191 }; 192 193 static void 194 _rpc_bdev_compress_delete_cb(void *cb_arg, int bdeverrno) 195 { 196 struct spdk_jsonrpc_request *request = cb_arg; 197 198 if (bdeverrno == 0) { 199 spdk_jsonrpc_send_bool_response(request, true); 200 } else { 201 spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno)); 202 } 203 } 204 205 static void 206 rpc_bdev_compress_delete(struct spdk_jsonrpc_request *request, 207 const struct spdk_json_val *params) 208 { 209 struct rpc_delete_compress req = {NULL}; 210 211 if (spdk_json_decode_object(params, rpc_delete_compress_decoders, 212 SPDK_COUNTOF(rpc_delete_compress_decoders), 213 &req)) { 214 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 215 "spdk_json_decode_object failed"); 216 } else { 217 bdev_compress_delete(req.name, _rpc_bdev_compress_delete_cb, request); 218 } 219 220 free_rpc_delete_compress(&req); 221 } 222 SPDK_RPC_REGISTER("bdev_compress_delete", rpc_bdev_compress_delete, SPDK_RPC_RUNTIME) 223