]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/lib/nbd/nbd_rpc.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / lib / nbd / nbd_rpc.c
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/string.h"
35 #include "spdk/env.h"
36 #include "spdk/rpc.h"
37 #include "spdk/util.h"
38
39 #include <linux/nbd.h>
40
41 #include "nbd_internal.h"
42 #include "spdk_internal/log.h"
43
44 struct rpc_start_nbd_disk {
45 char *bdev_name;
46 char *nbd_device;
47 };
48
49 static void
50 free_rpc_start_nbd_disk(struct rpc_start_nbd_disk *req)
51 {
52 free(req->bdev_name);
53 free(req->nbd_device);
54 }
55
56 static const struct spdk_json_object_decoder rpc_start_nbd_disk_decoders[] = {
57 {"bdev_name", offsetof(struct rpc_start_nbd_disk, bdev_name), spdk_json_decode_string},
58 {"nbd_device", offsetof(struct rpc_start_nbd_disk, nbd_device), spdk_json_decode_string},
59 };
60
61 static void
62 spdk_rpc_start_nbd_disk(struct spdk_jsonrpc_request *request,
63 const struct spdk_json_val *params)
64 {
65 struct rpc_start_nbd_disk req = {};
66 struct spdk_json_write_ctx *w;
67 struct spdk_nbd_disk *nbd;
68
69 if (spdk_json_decode_object(params, rpc_start_nbd_disk_decoders,
70 SPDK_COUNTOF(rpc_start_nbd_disk_decoders),
71 &req)) {
72 SPDK_ERRLOG("spdk_json_decode_object failed\n");
73 goto invalid;
74 }
75
76 if (req.nbd_device == NULL || req.bdev_name == NULL) {
77 goto invalid;
78 }
79
80 /* make sure nbd_device is not registered */
81 nbd = spdk_nbd_disk_find_by_nbd_path(req.nbd_device);
82 if (nbd) {
83 goto invalid;
84 }
85
86 nbd = spdk_nbd_start(req.bdev_name, req.nbd_device);
87 if (!nbd) {
88 goto invalid;
89 }
90
91 w = spdk_jsonrpc_begin_result(request);
92 if (w == NULL) {
93 free_rpc_start_nbd_disk(&req);
94 return;
95 }
96
97 spdk_json_write_string(w, req.nbd_device);
98 spdk_jsonrpc_end_result(request, w);
99 free_rpc_start_nbd_disk(&req);
100 return;
101
102 invalid:
103 free_rpc_start_nbd_disk(&req);
104 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
105 }
106
107 SPDK_RPC_REGISTER("start_nbd_disk", spdk_rpc_start_nbd_disk, SPDK_RPC_RUNTIME)
108
109 struct rpc_stop_nbd_disk {
110 char *nbd_device;
111 };
112
113 static void
114 free_rpc_stop_nbd_disk(struct rpc_stop_nbd_disk *req)
115 {
116 free(req->nbd_device);
117 }
118
119 static const struct spdk_json_object_decoder rpc_stop_nbd_disk_decoders[] = {
120 {"nbd_device", offsetof(struct rpc_stop_nbd_disk, nbd_device), spdk_json_decode_string},
121 };
122
123 struct nbd_disconnect_arg {
124 struct spdk_jsonrpc_request *request;
125 struct spdk_nbd_disk *nbd;
126 };
127
128 static void *
129 nbd_disconnect_thread(void *arg)
130 {
131 struct nbd_disconnect_arg *thd_arg = arg;
132 struct spdk_json_write_ctx *w;
133
134 spdk_unaffinitize_thread();
135
136 nbd_disconnect(thd_arg->nbd);
137
138 w = spdk_jsonrpc_begin_result(thd_arg->request);
139 if (w == NULL) {
140 goto out;
141 }
142
143 spdk_json_write_bool(w, true);
144 spdk_jsonrpc_end_result(thd_arg->request, w);
145
146 out:
147 free(thd_arg);
148 pthread_exit(NULL);
149 }
150
151 static void
152 spdk_rpc_stop_nbd_disk(struct spdk_jsonrpc_request *request,
153 const struct spdk_json_val *params)
154 {
155 struct rpc_stop_nbd_disk req = {};
156 struct spdk_nbd_disk *nbd;
157 pthread_t tid;
158 struct nbd_disconnect_arg *thd_arg = NULL;
159 int rc;
160
161 if (spdk_json_decode_object(params, rpc_stop_nbd_disk_decoders,
162 SPDK_COUNTOF(rpc_stop_nbd_disk_decoders),
163 &req)) {
164 SPDK_ERRLOG("spdk_json_decode_object failed\n");
165 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
166 "Invalid parameters");
167 goto out;
168 }
169
170 if (req.nbd_device == NULL) {
171 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
172 "Invalid parameters");
173 goto out;
174 }
175
176 /* make sure nbd_device is registered */
177 nbd = spdk_nbd_disk_find_by_nbd_path(req.nbd_device);
178 if (!nbd) {
179 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
180 "Invalid parameters");
181 goto out;
182 }
183
184 /*
185 * thd_arg should be freed by created thread
186 * if thread is created successfully.
187 */
188 thd_arg = malloc(sizeof(*thd_arg));
189 if (!thd_arg) {
190 SPDK_ERRLOG("could not allocate nbd disconnect thread arg\n");
191 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
192 goto out;
193 }
194
195 thd_arg->request = request;
196 thd_arg->nbd = nbd;
197
198 /*
199 * NBD ioctl of disconnect will block until data are flushed.
200 * Create separate thread to execute it.
201 */
202 rc = pthread_create(&tid, NULL, nbd_disconnect_thread, (void *)thd_arg);
203 if (rc != 0) {
204 SPDK_ERRLOG("could not create nbd disconnect thread: %s\n", spdk_strerror(rc));
205 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(rc));
206 free(thd_arg);
207 goto out;
208 }
209
210 rc = pthread_detach(tid);
211 if (rc != 0) {
212 SPDK_ERRLOG("could not detach nbd disconnect thread: %s\n", spdk_strerror(rc));
213 goto out;
214 }
215
216 out:
217 free_rpc_stop_nbd_disk(&req);
218 }
219
220 SPDK_RPC_REGISTER("stop_nbd_disk", spdk_rpc_stop_nbd_disk, SPDK_RPC_RUNTIME)
221
222 static void
223 spdk_rpc_dump_nbd_info(struct spdk_json_write_ctx *w,
224 struct spdk_nbd_disk *nbd)
225 {
226 spdk_json_write_object_begin(w);
227
228 spdk_json_write_name(w, "nbd_device");
229 spdk_json_write_string(w, spdk_nbd_disk_get_nbd_path(nbd));
230
231 spdk_json_write_name(w, "bdev_name");
232 spdk_json_write_string(w, spdk_nbd_disk_get_bdev_name(nbd));
233
234 spdk_json_write_object_end(w);
235 }
236
237 struct rpc_get_nbd_disks {
238 char *nbd_device;
239 };
240
241 static void
242 free_rpc_get_nbd_disks(struct rpc_get_nbd_disks *r)
243 {
244 free(r->nbd_device);
245 }
246
247 static const struct spdk_json_object_decoder rpc_get_nbd_disks_decoders[] = {
248 {"nbd_device", offsetof(struct rpc_get_nbd_disks, nbd_device), spdk_json_decode_string, true},
249 };
250
251 static void
252 spdk_rpc_get_nbd_disks(struct spdk_jsonrpc_request *request,
253 const struct spdk_json_val *params)
254 {
255 struct rpc_get_nbd_disks req = {};
256 struct spdk_json_write_ctx *w;
257 struct spdk_nbd_disk *nbd = NULL;
258
259 if (params != NULL) {
260 if (spdk_json_decode_object(params, rpc_get_nbd_disks_decoders,
261 SPDK_COUNTOF(rpc_get_nbd_disks_decoders),
262 &req)) {
263 SPDK_ERRLOG("spdk_json_decode_object failed\n");
264 goto invalid;
265 }
266
267 if (req.nbd_device) {
268 nbd = spdk_nbd_disk_find_by_nbd_path(req.nbd_device);
269 if (nbd == NULL) {
270 SPDK_ERRLOG("nbd device '%s' does not exist\n", req.nbd_device);
271 goto invalid;
272 }
273
274 free_rpc_get_nbd_disks(&req);
275 }
276 }
277
278 w = spdk_jsonrpc_begin_result(request);
279 if (w == NULL) {
280 return;
281 }
282
283 spdk_json_write_array_begin(w);
284
285 if (nbd != NULL) {
286 spdk_rpc_dump_nbd_info(w, nbd);
287 } else {
288 for (nbd = spdk_nbd_disk_first(); nbd != NULL; nbd = spdk_nbd_disk_next(nbd)) {
289 spdk_rpc_dump_nbd_info(w, nbd);
290 }
291 }
292
293 spdk_json_write_array_end(w);
294
295 spdk_jsonrpc_end_result(request, w);
296
297 return;
298
299 invalid:
300 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
301
302 free_rpc_get_nbd_disks(&req);
303 }
304 SPDK_RPC_REGISTER("get_nbd_disks", spdk_rpc_get_nbd_disks, SPDK_RPC_RUNTIME)