xref: /spdk/module/bdev/compress/vbdev_compress_rpc.c (revision 588dfe314bb83d86effdf67ec42837b11c2620bf)
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 struct rpc_compress_set_pmd {
74 	enum compress_pmd pmd;
75 };
76 
77 static const struct spdk_json_object_decoder rpc_compress_pmd_decoder[] = {
78 	{"pmd", offsetof(struct rpc_compress_set_pmd, pmd), spdk_json_decode_int32},
79 };
80 
81 static void
82 rpc_bdev_compress_set_pmd(struct spdk_jsonrpc_request *request,
83 			  const struct spdk_json_val *params)
84 {
85 	struct rpc_compress_set_pmd req;
86 	int rc = 0;
87 
88 	if (spdk_json_decode_object(params, rpc_compress_pmd_decoder,
89 				    SPDK_COUNTOF(rpc_compress_pmd_decoder),
90 				    &req)) {
91 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
92 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
93 						 "spdk_json_decode_object failed");
94 		return;
95 	}
96 
97 	if (req.pmd >= COMPRESS_PMD_MAX) {
98 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
99 						     "PMD value %d should be less than %d", req.pmd, COMPRESS_PMD_MAX);
100 		return;
101 	}
102 
103 	rc = compress_set_pmd(&req.pmd);
104 	if (rc) {
105 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
106 		return;
107 	}
108 
109 	spdk_jsonrpc_send_bool_response(request, true);
110 }
111 SPDK_RPC_REGISTER("bdev_compress_set_pmd", rpc_bdev_compress_set_pmd,
112 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
113 
114 /* Structure to hold the parameters for this RPC method. */
115 struct rpc_construct_compress {
116 	char *base_bdev_name;
117 	char *pm_path;
118 	uint32_t lb_size;
119 };
120 
121 /* Free the allocated memory resource after the RPC handling. */
122 static void
123 free_rpc_construct_compress(struct rpc_construct_compress *r)
124 {
125 	free(r->base_bdev_name);
126 	free(r->pm_path);
127 }
128 
129 /* Structure to decode the input parameters for this RPC method. */
130 static const struct spdk_json_object_decoder rpc_construct_compress_decoders[] = {
131 	{"base_bdev_name", offsetof(struct rpc_construct_compress, base_bdev_name), spdk_json_decode_string},
132 	{"pm_path", offsetof(struct rpc_construct_compress, pm_path), spdk_json_decode_string},
133 	{"lb_size", offsetof(struct rpc_construct_compress, lb_size), spdk_json_decode_uint32, true},
134 };
135 
136 /* Decode the parameters for this RPC method and properly construct the compress
137  * device. Error status returned in the failed cases.
138  */
139 static void
140 rpc_bdev_compress_create(struct spdk_jsonrpc_request *request,
141 			 const struct spdk_json_val *params)
142 {
143 	struct rpc_construct_compress req = {NULL};
144 	struct spdk_json_write_ctx *w;
145 	char *name;
146 	int rc;
147 
148 	if (spdk_json_decode_object(params, rpc_construct_compress_decoders,
149 				    SPDK_COUNTOF(rpc_construct_compress_decoders),
150 				    &req)) {
151 		SPDK_DEBUGLOG(vbdev_compress, "spdk_json_decode_object failed\n");
152 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_PARSE_ERROR,
153 						 "spdk_json_decode_object failed");
154 		goto cleanup;
155 	}
156 
157 	rc = create_compress_bdev(req.base_bdev_name, req.pm_path, req.lb_size);
158 	if (rc != 0) {
159 		if (rc == -EBUSY) {
160 			spdk_jsonrpc_send_error_response(request, rc, "Base bdev already in use for compression.");
161 		} else {
162 			spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
163 		}
164 		goto cleanup;
165 	}
166 
167 	w = spdk_jsonrpc_begin_result(request);
168 	name = spdk_sprintf_alloc("COMP_%s", req.base_bdev_name);
169 	spdk_json_write_string(w, name);
170 	spdk_jsonrpc_end_result(request, w);
171 	free(name);
172 
173 cleanup:
174 	free_rpc_construct_compress(&req);
175 }
176 SPDK_RPC_REGISTER("bdev_compress_create", rpc_bdev_compress_create, SPDK_RPC_RUNTIME)
177 
178 struct rpc_delete_compress {
179 	char *name;
180 };
181 
182 static void
183 free_rpc_delete_compress(struct rpc_delete_compress *req)
184 {
185 	free(req->name);
186 }
187 
188 static const struct spdk_json_object_decoder rpc_delete_compress_decoders[] = {
189 	{"name", offsetof(struct rpc_delete_compress, name), spdk_json_decode_string},
190 };
191 
192 static void
193 _rpc_bdev_compress_delete_cb(void *cb_arg, int bdeverrno)
194 {
195 	struct spdk_jsonrpc_request *request = cb_arg;
196 
197 	spdk_jsonrpc_send_bool_response(request, bdeverrno == 0);
198 }
199 
200 static void
201 rpc_bdev_compress_delete(struct spdk_jsonrpc_request *request,
202 			 const struct spdk_json_val *params)
203 {
204 	struct rpc_delete_compress req = {NULL};
205 
206 	if (spdk_json_decode_object(params, rpc_delete_compress_decoders,
207 				    SPDK_COUNTOF(rpc_delete_compress_decoders),
208 				    &req)) {
209 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
210 						 "spdk_json_decode_object failed");
211 	} else {
212 		bdev_compress_delete(req.name, _rpc_bdev_compress_delete_cb, request);
213 	}
214 
215 	free_rpc_delete_compress(&req);
216 }
217 SPDK_RPC_REGISTER("bdev_compress_delete", rpc_bdev_compress_delete, SPDK_RPC_RUNTIME)
218