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