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