2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2022-11-01 20:26:26 +00:00
|
|
|
* Copyright (C) 2018 Intel Corporation.
|
2018-12-01 15:09:46 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vbdev_compress.h"
|
|
|
|
#include "spdk/rpc.h"
|
|
|
|
#include "spdk/util.h"
|
|
|
|
#include "spdk/string.h"
|
2020-10-06 16:16:26 +00:00
|
|
|
#include "spdk/log.h"
|
2018-12-01 15:09:46 +00:00
|
|
|
|
2019-08-20 17:12:59 +00:00
|
|
|
struct rpc_bdev_compress_get_orphans {
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_rpc_bdev_compress_get_orphans(struct rpc_bdev_compress_get_orphans *r)
|
|
|
|
{
|
|
|
|
free(r->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct spdk_json_object_decoder rpc_bdev_compress_get_orphans_decoders[] = {
|
|
|
|
{"name", offsetof(struct rpc_bdev_compress_get_orphans, name), spdk_json_decode_string, true},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2020-05-10 07:06:52 +00:00
|
|
|
rpc_bdev_compress_get_orphans(struct spdk_jsonrpc_request *request,
|
|
|
|
const struct spdk_json_val *params)
|
2019-08-20 17:12:59 +00:00
|
|
|
{
|
|
|
|
struct rpc_bdev_compress_get_orphans req = {};
|
|
|
|
struct spdk_json_write_ctx *w;
|
|
|
|
struct vbdev_compress *comp_bdev;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (params && spdk_json_decode_object(params, rpc_bdev_compress_get_orphans_decoders,
|
|
|
|
SPDK_COUNTOF(rpc_bdev_compress_get_orphans_decoders),
|
|
|
|
&req)) {
|
|
|
|
SPDK_ERRLOG("spdk_json_decode_object failed\n");
|
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"spdk_json_decode_object failed");
|
|
|
|
free_rpc_bdev_compress_get_orphans(&req);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.name) {
|
|
|
|
if (compress_has_orphan(req.name) == false) {
|
|
|
|
spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
|
|
|
|
free_rpc_bdev_compress_get_orphans(&req);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
w = spdk_jsonrpc_begin_result(request);
|
|
|
|
spdk_json_write_array_begin(w);
|
|
|
|
if (found) {
|
|
|
|
spdk_json_write_string(w, req.name);
|
|
|
|
} else {
|
|
|
|
for (comp_bdev = compress_bdev_first(); comp_bdev != NULL;
|
|
|
|
comp_bdev = compress_bdev_next(comp_bdev)) {
|
|
|
|
if (compress_has_orphan(compress_get_name(comp_bdev))) {
|
|
|
|
spdk_json_write_string(w, compress_get_name(comp_bdev));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spdk_json_write_array_end(w);
|
|
|
|
spdk_jsonrpc_end_result(request, w);
|
|
|
|
free_rpc_bdev_compress_get_orphans(&req);
|
|
|
|
}
|
2020-05-10 07:06:52 +00:00
|
|
|
SPDK_RPC_REGISTER("bdev_compress_get_orphans", rpc_bdev_compress_get_orphans, SPDK_RPC_RUNTIME)
|
2019-08-20 17:12:59 +00:00
|
|
|
|
2018-12-01 15:09:46 +00:00
|
|
|
/* Structure to hold the parameters for this RPC method. */
|
|
|
|
struct rpc_construct_compress {
|
|
|
|
char *base_bdev_name;
|
2019-04-26 15:27:45 +00:00
|
|
|
char *pm_path;
|
2020-07-02 16:43:41 +00:00
|
|
|
uint32_t lb_size;
|
2018-12-01 15:09:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Free the allocated memory resource after the RPC handling. */
|
|
|
|
static void
|
|
|
|
free_rpc_construct_compress(struct rpc_construct_compress *r)
|
|
|
|
{
|
|
|
|
free(r->base_bdev_name);
|
2019-04-26 15:27:45 +00:00
|
|
|
free(r->pm_path);
|
2018-12-01 15:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Structure to decode the input parameters for this RPC method. */
|
|
|
|
static const struct spdk_json_object_decoder rpc_construct_compress_decoders[] = {
|
|
|
|
{"base_bdev_name", offsetof(struct rpc_construct_compress, base_bdev_name), spdk_json_decode_string},
|
2019-04-26 15:27:45 +00:00
|
|
|
{"pm_path", offsetof(struct rpc_construct_compress, pm_path), spdk_json_decode_string},
|
2022-07-01 07:32:09 +00:00
|
|
|
{"lb_size", offsetof(struct rpc_construct_compress, lb_size), spdk_json_decode_uint32, true},
|
2018-12-01 15:09:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Decode the parameters for this RPC method and properly construct the compress
|
|
|
|
* device. Error status returned in the failed cases.
|
|
|
|
*/
|
|
|
|
static void
|
2020-05-10 07:06:52 +00:00
|
|
|
rpc_bdev_compress_create(struct spdk_jsonrpc_request *request,
|
|
|
|
const struct spdk_json_val *params)
|
2018-12-01 15:09:46 +00:00
|
|
|
{
|
|
|
|
struct rpc_construct_compress req = {NULL};
|
|
|
|
struct spdk_json_write_ctx *w;
|
2019-04-26 15:27:45 +00:00
|
|
|
char *name;
|
2018-12-01 15:09:46 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (spdk_json_decode_object(params, rpc_construct_compress_decoders,
|
|
|
|
SPDK_COUNTOF(rpc_construct_compress_decoders),
|
|
|
|
&req)) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vbdev_compress, "spdk_json_decode_object failed\n");
|
2020-07-02 16:43:41 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_PARSE_ERROR,
|
2019-07-12 07:39:37 +00:00
|
|
|
"spdk_json_decode_object failed");
|
|
|
|
goto cleanup;
|
2018-12-01 15:09:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 16:43:41 +00:00
|
|
|
rc = create_compress_bdev(req.base_bdev_name, req.pm_path, req.lb_size);
|
2018-12-01 15:09:46 +00:00
|
|
|
if (rc != 0) {
|
2022-03-03 23:44:08 +00:00
|
|
|
if (rc == -EBUSY) {
|
|
|
|
spdk_jsonrpc_send_error_response(request, rc, "Base bdev already in use for compression.");
|
|
|
|
} else {
|
|
|
|
spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
|
|
|
|
}
|
2019-07-12 07:39:37 +00:00
|
|
|
goto cleanup;
|
2018-12-01 15:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w = spdk_jsonrpc_begin_result(request);
|
2019-04-26 15:27:45 +00:00
|
|
|
name = spdk_sprintf_alloc("COMP_%s", req.base_bdev_name);
|
|
|
|
spdk_json_write_string(w, name);
|
2018-12-01 15:09:46 +00:00
|
|
|
spdk_jsonrpc_end_result(request, w);
|
2019-04-26 15:27:45 +00:00
|
|
|
free(name);
|
2018-12-01 15:09:46 +00:00
|
|
|
|
2019-07-12 07:39:37 +00:00
|
|
|
cleanup:
|
2018-12-01 15:09:46 +00:00
|
|
|
free_rpc_construct_compress(&req);
|
|
|
|
}
|
2020-05-10 07:06:52 +00:00
|
|
|
SPDK_RPC_REGISTER("bdev_compress_create", rpc_bdev_compress_create, SPDK_RPC_RUNTIME)
|
2018-12-01 15:09:46 +00:00
|
|
|
|
|
|
|
struct rpc_delete_compress {
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_rpc_delete_compress(struct rpc_delete_compress *req)
|
|
|
|
{
|
|
|
|
free(req->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct spdk_json_object_decoder rpc_delete_compress_decoders[] = {
|
|
|
|
{"name", offsetof(struct rpc_delete_compress, name), spdk_json_decode_string},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2020-05-10 07:06:52 +00:00
|
|
|
_rpc_bdev_compress_delete_cb(void *cb_arg, int bdeverrno)
|
2018-12-01 15:09:46 +00:00
|
|
|
{
|
|
|
|
struct spdk_jsonrpc_request *request = cb_arg;
|
|
|
|
|
2020-11-06 16:58:30 +00:00
|
|
|
spdk_jsonrpc_send_bool_response(request, bdeverrno == 0);
|
2018-12-01 15:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-10 07:06:52 +00:00
|
|
|
rpc_bdev_compress_delete(struct spdk_jsonrpc_request *request,
|
|
|
|
const struct spdk_json_val *params)
|
2018-12-01 15:09:46 +00:00
|
|
|
{
|
|
|
|
struct rpc_delete_compress req = {NULL};
|
|
|
|
|
|
|
|
if (spdk_json_decode_object(params, rpc_delete_compress_decoders,
|
|
|
|
SPDK_COUNTOF(rpc_delete_compress_decoders),
|
|
|
|
&req)) {
|
2019-07-12 07:39:37 +00:00
|
|
|
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
|
|
|
"spdk_json_decode_object failed");
|
2019-08-20 21:39:03 +00:00
|
|
|
} else {
|
2020-05-10 07:06:52 +00:00
|
|
|
bdev_compress_delete(req.name, _rpc_bdev_compress_delete_cb, request);
|
2018-12-01 15:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free_rpc_delete_compress(&req);
|
|
|
|
}
|
2020-05-10 07:06:52 +00:00
|
|
|
SPDK_RPC_REGISTER("bdev_compress_delete", rpc_bdev_compress_delete, SPDK_RPC_RUNTIME)
|