xref: /spdk/module/blobfs/bdev/blobfs_bdev_rpc.c (revision ceea3088870a3919d6bdfe61d7adba11b9733fb7)
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 "spdk/stdinc.h"
35 #include "spdk/blobfs.h"
36 #include "spdk/bdev.h"
37 #include "spdk/event.h"
38 #include "spdk/blob_bdev.h"
39 #include "spdk/blobfs_bdev.h"
40 #include "spdk/log.h"
41 #include "spdk/string.h"
42 #include "spdk/rpc.h"
43 #include "spdk/util.h"
44 
45 #ifndef PAGE_SIZE
46 #define PAGE_SIZE 4096
47 #endif
48 
49 #define MIN_CLUSTER_SZ (1024 * 1024)
50 
51 struct rpc_blobfs_set_cache_size {
52 	uint64_t size_in_mb;
53 };
54 
55 static const struct spdk_json_object_decoder rpc_blobfs_set_cache_size_decoders[] = {
56 	{"size_in_mb", offsetof(struct rpc_blobfs_set_cache_size, size_in_mb), spdk_json_decode_uint64},
57 };
58 
59 static void
60 rpc_blobfs_set_cache_size(struct spdk_jsonrpc_request *request,
61 			  const struct spdk_json_val *params)
62 {
63 	struct rpc_blobfs_set_cache_size req;
64 	struct spdk_json_write_ctx *w;
65 	int rc;
66 
67 	if (spdk_json_decode_object(params, rpc_blobfs_set_cache_size_decoders,
68 				    SPDK_COUNTOF(rpc_blobfs_set_cache_size_decoders),
69 				    &req)) {
70 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
71 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
72 						 "spdk_json_decode_object failed");
73 
74 		return;
75 	}
76 
77 	if (req.size_in_mb == 0) {
78 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
79 						 "spdk_json_decode_object failed");
80 
81 		return;
82 	}
83 
84 	rc = spdk_fs_set_cache_size(req.size_in_mb);
85 
86 	w = spdk_jsonrpc_begin_result(request);
87 	spdk_json_write_bool(w, rc == 0);
88 	spdk_jsonrpc_end_result(request, w);
89 }
90 
91 SPDK_RPC_REGISTER("blobfs_set_cache_size", rpc_blobfs_set_cache_size,
92 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
93 
94 struct rpc_blobfs_detect {
95 	char *bdev_name;
96 
97 	struct spdk_jsonrpc_request *request;
98 };
99 
100 static void
101 free_rpc_blobfs_detect(struct rpc_blobfs_detect *req)
102 {
103 	free(req->bdev_name);
104 	free(req);
105 }
106 
107 static const struct spdk_json_object_decoder rpc_blobfs_detect_decoders[] = {
108 	{"bdev_name", offsetof(struct rpc_blobfs_detect, bdev_name), spdk_json_decode_string},
109 };
110 
111 static void
112 _rpc_blobfs_detect_done(void *cb_arg, int fserrno)
113 {
114 	struct rpc_blobfs_detect *req = cb_arg;
115 	struct spdk_json_write_ctx *w;
116 	bool existed = true;
117 
118 	if (fserrno == -EILSEQ) {
119 		/* There is no blobfs existing on bdev */
120 		existed = false;
121 	} else if (fserrno != 0) {
122 		spdk_jsonrpc_send_error_response(req->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
123 						 spdk_strerror(-fserrno));
124 
125 		return;
126 	}
127 
128 	w = spdk_jsonrpc_begin_result(req->request);
129 	spdk_json_write_bool(w, existed);
130 	spdk_jsonrpc_end_result(req->request, w);
131 
132 	free_rpc_blobfs_detect(req);
133 }
134 
135 static void
136 rpc_blobfs_detect(struct spdk_jsonrpc_request *request,
137 		  const struct spdk_json_val *params)
138 {
139 	struct rpc_blobfs_detect *req;
140 
141 	req = calloc(1, sizeof(*req));
142 	if (req == NULL) {
143 		SPDK_ERRLOG("could not allocate rpc_blobfs_detect request.\n");
144 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
145 		return;
146 	}
147 
148 	if (spdk_json_decode_object(params, rpc_blobfs_detect_decoders,
149 				    SPDK_COUNTOF(rpc_blobfs_detect_decoders),
150 				    req)) {
151 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
152 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
153 						 "spdk_json_decode_object failed");
154 
155 		free_rpc_blobfs_detect(req);
156 
157 		return;
158 	}
159 
160 	req->request = request;
161 	spdk_blobfs_bdev_detect(req->bdev_name, _rpc_blobfs_detect_done, req);
162 }
163 
164 SPDK_RPC_REGISTER("blobfs_detect", rpc_blobfs_detect, SPDK_RPC_RUNTIME)
165 
166 struct rpc_blobfs_create {
167 	char *bdev_name;
168 	uint64_t cluster_sz;
169 
170 	struct spdk_jsonrpc_request *request;
171 };
172 
173 static void
174 free_rpc_blobfs_create(struct rpc_blobfs_create *req)
175 {
176 	free(req->bdev_name);
177 	free(req);
178 }
179 
180 static int
181 rpc_decode_cluster_sz(const struct spdk_json_val *val, void *out)
182 {
183 	uint64_t *cluster_sz = out;
184 	char *sz_str = NULL;
185 	bool has_prefix;
186 	int rc;
187 
188 	rc = spdk_json_decode_string(val, &sz_str);
189 	if (rc) {
190 		SPDK_NOTICELOG("Invalid parameter value: cluster_sz\n");
191 		return -EINVAL;
192 	}
193 
194 	rc = spdk_parse_capacity(sz_str, cluster_sz, &has_prefix);
195 	free(sz_str);
196 
197 	if (rc || *cluster_sz % PAGE_SIZE != 0 || *cluster_sz < MIN_CLUSTER_SZ) {
198 		SPDK_NOTICELOG("Invalid parameter value: cluster_sz\n");
199 		return -EINVAL;
200 	}
201 
202 	SPDK_DEBUGLOG(blobfs_bdev_rpc, "cluster_sz of blobfs: %ld\n", *cluster_sz);
203 	return 0;
204 }
205 
206 static const struct spdk_json_object_decoder rpc_blobfs_create_decoders[] = {
207 	{"bdev_name", offsetof(struct rpc_blobfs_create, bdev_name), spdk_json_decode_string},
208 	{"cluster_sz", offsetof(struct rpc_blobfs_create, cluster_sz), rpc_decode_cluster_sz, true},
209 };
210 
211 static void
212 _rpc_blobfs_create_done(void *cb_arg, int fserrno)
213 {
214 	struct rpc_blobfs_create *req = cb_arg;
215 	struct spdk_json_write_ctx *w;
216 
217 	if (fserrno != 0) {
218 		spdk_jsonrpc_send_error_response(req->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
219 						 spdk_strerror(-fserrno));
220 
221 		return;
222 	}
223 
224 	w = spdk_jsonrpc_begin_result(req->request);
225 	spdk_json_write_bool(w, true);
226 	spdk_jsonrpc_end_result(req->request, w);
227 
228 	free_rpc_blobfs_create(req);
229 }
230 
231 static void
232 rpc_blobfs_create(struct spdk_jsonrpc_request *request,
233 		  const struct spdk_json_val *params)
234 {
235 	struct rpc_blobfs_create *req;
236 
237 	req = calloc(1, sizeof(*req));
238 	if (req == NULL) {
239 		SPDK_ERRLOG("could not allocate rpc_blobfs_create request.\n");
240 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
241 		return;
242 	}
243 
244 	if (spdk_json_decode_object(params, rpc_blobfs_create_decoders,
245 				    SPDK_COUNTOF(rpc_blobfs_create_decoders),
246 				    req)) {
247 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
248 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
249 						 "spdk_json_decode_object failed");
250 
251 		free_rpc_blobfs_create(req);
252 
253 		return;
254 	}
255 
256 	req->request = request;
257 	spdk_blobfs_bdev_create(req->bdev_name, req->cluster_sz, _rpc_blobfs_create_done, req);
258 }
259 
260 SPDK_RPC_REGISTER("blobfs_create", rpc_blobfs_create, SPDK_RPC_RUNTIME)
261 
262 SPDK_LOG_REGISTER_COMPONENT(blobfs_bdev_rpc)
263 #ifdef SPDK_CONFIG_FUSE
264 
265 struct rpc_blobfs_mount {
266 	char *bdev_name;
267 	char *mountpoint;
268 
269 	struct spdk_jsonrpc_request *request;
270 };
271 
272 static void
273 free_rpc_blobfs_mount(struct rpc_blobfs_mount *req)
274 {
275 	free(req->bdev_name);
276 	free(req->mountpoint);
277 	free(req);
278 }
279 
280 static const struct spdk_json_object_decoder rpc_blobfs_mount_decoders[] = {
281 	{"bdev_name", offsetof(struct rpc_blobfs_mount, bdev_name), spdk_json_decode_string},
282 	{"mountpoint", offsetof(struct rpc_blobfs_mount, mountpoint), spdk_json_decode_string},
283 };
284 
285 static void
286 _rpc_blobfs_mount_done(void *cb_arg, int fserrno)
287 {
288 	struct rpc_blobfs_mount *req = cb_arg;
289 	struct spdk_json_write_ctx *w;
290 
291 	if (fserrno == -EILSEQ) {
292 		/* There is no blobfs existing on bdev */
293 		spdk_jsonrpc_send_error_response(req->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
294 						 "No blobfs detected on given bdev");
295 
296 		return;
297 	} else if (fserrno != 0) {
298 		spdk_jsonrpc_send_error_response(req->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
299 						 spdk_strerror(-fserrno));
300 
301 		return;
302 	}
303 
304 	w = spdk_jsonrpc_begin_result(req->request);
305 	spdk_json_write_bool(w, true);
306 	spdk_jsonrpc_end_result(req->request, w);
307 
308 	free_rpc_blobfs_mount(req);
309 }
310 
311 static void
312 rpc_blobfs_mount(struct spdk_jsonrpc_request *request,
313 		 const struct spdk_json_val *params)
314 {
315 	struct rpc_blobfs_mount *req;
316 
317 	req = calloc(1, sizeof(*req));
318 	if (req == NULL) {
319 		SPDK_ERRLOG("could not allocate rpc_blobfs_mount request.\n");
320 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
321 		return;
322 	}
323 
324 	if (spdk_json_decode_object(params, rpc_blobfs_mount_decoders,
325 				    SPDK_COUNTOF(rpc_blobfs_mount_decoders),
326 				    req)) {
327 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
328 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
329 						 "spdk_json_decode_object failed");
330 
331 		free_rpc_blobfs_mount(req);
332 
333 		return;
334 	}
335 
336 	req->request = request;
337 	spdk_blobfs_bdev_mount(req->bdev_name, req->mountpoint, _rpc_blobfs_mount_done, req);
338 }
339 
340 SPDK_RPC_REGISTER("blobfs_mount", rpc_blobfs_mount, SPDK_RPC_RUNTIME)
341 
342 #endif
343