xref: /spdk/module/bdev/rbd/bdev_rbd_rpc.c (revision 8bb0ded3e55c182cea67af1f6790f8de5f38c05f)
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 "bdev_rbd.h"
35 #include "spdk/rpc.h"
36 #include "spdk/util.h"
37 #include "spdk/string.h"
38 #include "spdk/log.h"
39 
40 struct rpc_create_rbd {
41 	char *name;
42 	char *user_id;
43 	char *pool_name;
44 	char *rbd_name;
45 	uint32_t block_size;
46 	char **config;
47 };
48 
49 static void
50 free_rpc_create_rbd(struct rpc_create_rbd *req)
51 {
52 	free(req->name);
53 	free(req->user_id);
54 	free(req->pool_name);
55 	free(req->rbd_name);
56 	bdev_rbd_free_config(req->config);
57 }
58 
59 static int
60 bdev_rbd_decode_config(const struct spdk_json_val *values, void *out)
61 {
62 	char ***map = out;
63 	char **entry;
64 	uint32_t i;
65 
66 	if (values->type == SPDK_JSON_VAL_NULL) {
67 		/* treated like empty object: empty config */
68 		*map = calloc(1, sizeof(**map));
69 		if (!*map) {
70 			return -1;
71 		}
72 		return 0;
73 	}
74 
75 	if (values->type != SPDK_JSON_VAL_OBJECT_BEGIN) {
76 		return -1;
77 	}
78 
79 	*map = calloc(values->len + 1, sizeof(**map));
80 	if (!*map) {
81 		return -1;
82 	}
83 
84 	for (i = 0, entry = *map; i < values->len;) {
85 		const struct spdk_json_val *name = &values[i + 1];
86 		const struct spdk_json_val *v = &values[i + 2];
87 		/* Here we catch errors like invalid types. */
88 		if (!(entry[0] = spdk_json_strdup(name)) ||
89 		    !(entry[1] = spdk_json_strdup(v))) {
90 			bdev_rbd_free_config(*map);
91 			*map = NULL;
92 			return -1;
93 		}
94 		i += 1 + spdk_json_val_len(v);
95 		entry += 2;
96 	}
97 
98 	return 0;
99 }
100 
101 static const struct spdk_json_object_decoder rpc_create_rbd_decoders[] = {
102 	{"name", offsetof(struct rpc_create_rbd, name), spdk_json_decode_string, true},
103 	{"user_id", offsetof(struct rpc_create_rbd, user_id), spdk_json_decode_string, true},
104 	{"pool_name", offsetof(struct rpc_create_rbd, pool_name), spdk_json_decode_string},
105 	{"rbd_name", offsetof(struct rpc_create_rbd, rbd_name), spdk_json_decode_string},
106 	{"block_size", offsetof(struct rpc_create_rbd, block_size), spdk_json_decode_uint32},
107 	{"config", offsetof(struct rpc_create_rbd, config), bdev_rbd_decode_config, true}
108 };
109 
110 static void
111 rpc_bdev_rbd_create(struct spdk_jsonrpc_request *request,
112 		    const struct spdk_json_val *params)
113 {
114 	struct rpc_create_rbd req = {};
115 	struct spdk_json_write_ctx *w;
116 	struct spdk_bdev *bdev;
117 	int rc = 0;
118 
119 	if (spdk_json_decode_object(params, rpc_create_rbd_decoders,
120 				    SPDK_COUNTOF(rpc_create_rbd_decoders),
121 				    &req)) {
122 		SPDK_DEBUGLOG(bdev_rbd, "spdk_json_decode_object failed\n");
123 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
124 						 "spdk_json_decode_object failed");
125 		goto cleanup;
126 	}
127 
128 	rc = bdev_rbd_create(&bdev, req.name, req.user_id, req.pool_name,
129 			     (const char *const *)req.config,
130 			     req.rbd_name,
131 			     req.block_size);
132 	if (rc) {
133 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
134 		goto cleanup;
135 	}
136 
137 	w = spdk_jsonrpc_begin_result(request);
138 	spdk_json_write_string(w, spdk_bdev_get_name(bdev));
139 	spdk_jsonrpc_end_result(request, w);
140 
141 cleanup:
142 	free_rpc_create_rbd(&req);
143 }
144 SPDK_RPC_REGISTER("bdev_rbd_create", rpc_bdev_rbd_create, SPDK_RPC_RUNTIME)
145 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_rbd_create, construct_rbd_bdev)
146 
147 struct rpc_bdev_rbd_delete {
148 	char *name;
149 };
150 
151 static void
152 free_rpc_bdev_rbd_delete(struct rpc_bdev_rbd_delete *req)
153 {
154 	free(req->name);
155 }
156 
157 static const struct spdk_json_object_decoder rpc_bdev_rbd_delete_decoders[] = {
158 	{"name", offsetof(struct rpc_bdev_rbd_delete, name), spdk_json_decode_string},
159 };
160 
161 static void
162 _rpc_bdev_rbd_delete_cb(void *cb_arg, int bdeverrno)
163 {
164 	struct spdk_jsonrpc_request *request = cb_arg;
165 
166 	spdk_jsonrpc_send_bool_response(request, bdeverrno == 0);
167 }
168 
169 static void
170 rpc_bdev_rbd_delete(struct spdk_jsonrpc_request *request,
171 		    const struct spdk_json_val *params)
172 {
173 	struct rpc_bdev_rbd_delete req = {NULL};
174 	struct spdk_bdev *bdev;
175 
176 	if (spdk_json_decode_object(params, rpc_bdev_rbd_delete_decoders,
177 				    SPDK_COUNTOF(rpc_bdev_rbd_delete_decoders),
178 				    &req)) {
179 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
180 						 "spdk_json_decode_object failed");
181 		goto cleanup;
182 	}
183 
184 	bdev = spdk_bdev_get_by_name(req.name);
185 	if (bdev == NULL) {
186 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
187 		goto cleanup;
188 	}
189 
190 	bdev_rbd_delete(bdev, _rpc_bdev_rbd_delete_cb, request);
191 
192 cleanup:
193 	free_rpc_bdev_rbd_delete(&req);
194 }
195 SPDK_RPC_REGISTER("bdev_rbd_delete", rpc_bdev_rbd_delete, SPDK_RPC_RUNTIME)
196 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_rbd_delete, delete_rbd_bdev)
197 
198 struct rpc_bdev_rbd_resize {
199 	char *name;
200 	uint64_t new_size;
201 };
202 
203 static const struct spdk_json_object_decoder rpc_bdev_rbd_resize_decoders[] = {
204 	{"name", offsetof(struct rpc_bdev_rbd_resize, name), spdk_json_decode_string},
205 	{"new_size", offsetof(struct rpc_bdev_rbd_resize, new_size), spdk_json_decode_uint64}
206 };
207 
208 static void
209 free_rpc_bdev_rbd_resize(struct rpc_bdev_rbd_resize *req)
210 {
211 	free(req->name);
212 }
213 
214 static void
215 rpc_bdev_rbd_resize(struct spdk_jsonrpc_request *request,
216 		    const struct spdk_json_val *params)
217 {
218 	struct rpc_bdev_rbd_resize req = {};
219 	struct spdk_bdev *bdev;
220 	int rc;
221 
222 	if (spdk_json_decode_object(params, rpc_bdev_rbd_resize_decoders,
223 				    SPDK_COUNTOF(rpc_bdev_rbd_resize_decoders),
224 				    &req)) {
225 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
226 						 "spdk_json_decode_object failed");
227 		goto cleanup;
228 	}
229 
230 	bdev = spdk_bdev_get_by_name(req.name);
231 	if (bdev == NULL) {
232 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
233 		goto cleanup;
234 	}
235 
236 	rc = bdev_rbd_resize(bdev, req.new_size);
237 	if (rc) {
238 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
239 		goto cleanup;
240 	}
241 
242 	spdk_jsonrpc_send_bool_response(request, true);
243 cleanup:
244 	free_rpc_bdev_rbd_resize(&req);
245 }
246 SPDK_RPC_REGISTER("bdev_rbd_resize", rpc_bdev_rbd_resize, SPDK_RPC_RUNTIME)
247