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