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