]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/lib/nbd/nbd_rpc.c
import 15.2.0 Octopus source
[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 /* Used to search one available nbd device */
48 int nbd_idx;
49 bool nbd_idx_specified;
50 struct spdk_jsonrpc_request *request;
51 };
52
53 static void
54 free_rpc_start_nbd_disk(struct rpc_start_nbd_disk *req)
55 {
56 free(req->bdev_name);
57 free(req->nbd_device);
58 free(req);
59 }
60
61 static const struct spdk_json_object_decoder rpc_start_nbd_disk_decoders[] = {
62 {"bdev_name", offsetof(struct rpc_start_nbd_disk, bdev_name), spdk_json_decode_string},
63 {"nbd_device", offsetof(struct rpc_start_nbd_disk, nbd_device), spdk_json_decode_string, true},
64 };
65
66 /* Return 0 to indicate the nbd_device might be available,
67 * or non-zero to indicate the nbd_device is invalid or in using.
68 */
69 static int
70 check_available_nbd_disk(char *nbd_device)
71 {
72 char nbd_block_path[256];
73 char tail[2];
74 int rc;
75 unsigned int nbd_idx;
76 struct spdk_nbd_disk *nbd;
77
78 /* nbd device path must be in format of /dev/nbd<num>, with no tail. */
79 rc = sscanf(nbd_device, "/dev/nbd%u%1s", &nbd_idx, tail);
80 if (rc != 1) {
81 return -1;
82 }
83
84 /* make sure nbd_device is not registered inside SPDK */
85 nbd = spdk_nbd_disk_find_by_nbd_path(nbd_device);
86 if (nbd) {
87 /* nbd_device is in using */
88 return 1;
89 }
90
91 /* A valid pid file in /sys/block indicates the device is in using */
92 snprintf(nbd_block_path, 256, "/sys/block/nbd%u/pid", nbd_idx);
93
94 rc = open(nbd_block_path, O_RDONLY);
95 if (rc < 0) {
96 if (errno == ENOENT) {
97 /* nbd_device might be available */
98 return 0;
99 } else {
100 SPDK_ERRLOG("Failed to check PID file %s: %s\n", nbd_block_path, spdk_strerror(errno));
101 return -1;
102 }
103 }
104
105 close(rc);
106
107 /* nbd_device is in using */
108 return 1;
109 }
110
111 static char *
112 find_available_nbd_disk(int nbd_idx, int *next_nbd_idx)
113 {
114 int i, rc;
115 char nbd_device[20];
116
117 for (i = nbd_idx; ; i++) {
118 snprintf(nbd_device, 20, "/dev/nbd%d", i);
119 /* Check whether an nbd device exists in order to reach the last one nbd device */
120 rc = access(nbd_device, F_OK);
121 if (rc != 0) {
122 break;
123 }
124
125 rc = check_available_nbd_disk(nbd_device);
126 if (rc == 0) {
127 if (next_nbd_idx != NULL) {
128 *next_nbd_idx = i + 1;
129 }
130
131 return strdup(nbd_device);
132 }
133 }
134
135 return NULL;
136 }
137
138 static void
139 spdk_rpc_start_nbd_done(void *cb_arg, struct spdk_nbd_disk *nbd, int rc)
140 {
141 struct rpc_start_nbd_disk *req = cb_arg;
142 struct spdk_jsonrpc_request *request = req->request;
143 struct spdk_json_write_ctx *w;
144
145 /* Check whether it's automatic nbd-device assignment */
146 if (rc == -EBUSY && req->nbd_idx_specified == false) {
147 free(req->nbd_device);
148
149 req->nbd_device = find_available_nbd_disk(req->nbd_idx, &req->nbd_idx);
150 if (req->nbd_device != NULL) {
151 spdk_nbd_start(req->bdev_name, req->nbd_device,
152 spdk_rpc_start_nbd_done, req);
153 return;
154 }
155
156 SPDK_INFOLOG(SPDK_LOG_NBD, "There is no available nbd device.\n");
157 }
158
159 if (rc) {
160 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
161 return;
162 }
163
164 w = spdk_jsonrpc_begin_result(request);
165 if (w == NULL) {
166 return;
167 }
168
169 spdk_json_write_string(w, spdk_nbd_get_path(nbd));
170 spdk_jsonrpc_end_result(request, w);
171
172 free_rpc_start_nbd_disk(req);
173 }
174
175 static void
176 spdk_rpc_start_nbd_disk(struct spdk_jsonrpc_request *request,
177 const struct spdk_json_val *params)
178 {
179 struct rpc_start_nbd_disk *req;
180 int rc;
181
182 req = calloc(1, sizeof(*req));
183 if (req == NULL) {
184 SPDK_ERRLOG("could not allocate start_nbd_disk request.\n");
185 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
186 return;
187 }
188
189 if (spdk_json_decode_object(params, rpc_start_nbd_disk_decoders,
190 SPDK_COUNTOF(rpc_start_nbd_disk_decoders),
191 req)) {
192 SPDK_ERRLOG("spdk_json_decode_object failed\n");
193 goto invalid;
194 }
195
196 if (req->bdev_name == NULL) {
197 goto invalid;
198 }
199
200 if (req->nbd_device != NULL) {
201 req->nbd_idx_specified = true;
202 rc = check_available_nbd_disk(req->nbd_device);
203 if (rc == 1) {
204 SPDK_DEBUGLOG(SPDK_LOG_NBD, "NBD device %s is in using.\n", req->nbd_device);
205 goto invalid;
206 }
207
208 if (rc != 0) {
209 SPDK_DEBUGLOG(SPDK_LOG_NBD, "Illegal nbd_device %s.\n", req->nbd_device);
210 goto invalid;
211 }
212 } else {
213 req->nbd_idx = 0;
214 req->nbd_device = find_available_nbd_disk(req->nbd_idx, &req->nbd_idx);
215 if (req->nbd_device == NULL) {
216 SPDK_INFOLOG(SPDK_LOG_NBD, "There is no available nbd device.\n");
217 goto invalid;
218 }
219 }
220
221 req->request = request;
222 spdk_nbd_start(req->bdev_name, req->nbd_device,
223 spdk_rpc_start_nbd_done, req);
224
225 return;
226
227 invalid:
228 free_rpc_start_nbd_disk(req);
229 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
230 }
231
232 SPDK_RPC_REGISTER("start_nbd_disk", spdk_rpc_start_nbd_disk, SPDK_RPC_RUNTIME)
233
234 struct rpc_stop_nbd_disk {
235 char *nbd_device;
236 };
237
238 static void
239 free_rpc_stop_nbd_disk(struct rpc_stop_nbd_disk *req)
240 {
241 free(req->nbd_device);
242 }
243
244 static const struct spdk_json_object_decoder rpc_stop_nbd_disk_decoders[] = {
245 {"nbd_device", offsetof(struct rpc_stop_nbd_disk, nbd_device), spdk_json_decode_string},
246 };
247
248 struct nbd_disconnect_arg {
249 struct spdk_jsonrpc_request *request;
250 struct spdk_nbd_disk *nbd;
251 };
252
253 static void *
254 nbd_disconnect_thread(void *arg)
255 {
256 struct nbd_disconnect_arg *thd_arg = arg;
257 struct spdk_json_write_ctx *w;
258
259 spdk_unaffinitize_thread();
260
261 nbd_disconnect(thd_arg->nbd);
262
263 w = spdk_jsonrpc_begin_result(thd_arg->request);
264 if (w == NULL) {
265 goto out;
266 }
267
268 spdk_json_write_bool(w, true);
269 spdk_jsonrpc_end_result(thd_arg->request, w);
270
271 out:
272 free(thd_arg);
273 pthread_exit(NULL);
274 }
275
276 static void
277 spdk_rpc_stop_nbd_disk(struct spdk_jsonrpc_request *request,
278 const struct spdk_json_val *params)
279 {
280 struct rpc_stop_nbd_disk req = {};
281 struct spdk_nbd_disk *nbd;
282 pthread_t tid;
283 struct nbd_disconnect_arg *thd_arg = NULL;
284 int rc;
285
286 if (spdk_json_decode_object(params, rpc_stop_nbd_disk_decoders,
287 SPDK_COUNTOF(rpc_stop_nbd_disk_decoders),
288 &req)) {
289 SPDK_ERRLOG("spdk_json_decode_object failed\n");
290 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
291 "Invalid parameters");
292 goto out;
293 }
294
295 if (req.nbd_device == NULL) {
296 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
297 "Invalid parameters");
298 goto out;
299 }
300
301 /* make sure nbd_device is registered */
302 nbd = spdk_nbd_disk_find_by_nbd_path(req.nbd_device);
303 if (!nbd) {
304 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
305 "Invalid parameters");
306 goto out;
307 }
308
309 /*
310 * thd_arg should be freed by created thread
311 * if thread is created successfully.
312 */
313 thd_arg = malloc(sizeof(*thd_arg));
314 if (!thd_arg) {
315 SPDK_ERRLOG("could not allocate nbd disconnect thread arg\n");
316 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
317 goto out;
318 }
319
320 thd_arg->request = request;
321 thd_arg->nbd = nbd;
322
323 /*
324 * NBD ioctl of disconnect will block until data are flushed.
325 * Create separate thread to execute it.
326 */
327 rc = pthread_create(&tid, NULL, nbd_disconnect_thread, (void *)thd_arg);
328 if (rc != 0) {
329 SPDK_ERRLOG("could not create nbd disconnect thread: %s\n", spdk_strerror(rc));
330 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(rc));
331 free(thd_arg);
332 goto out;
333 }
334
335 rc = pthread_detach(tid);
336 if (rc != 0) {
337 SPDK_ERRLOG("could not detach nbd disconnect thread: %s\n", spdk_strerror(rc));
338 goto out;
339 }
340
341 out:
342 free_rpc_stop_nbd_disk(&req);
343 }
344
345 SPDK_RPC_REGISTER("stop_nbd_disk", spdk_rpc_stop_nbd_disk, SPDK_RPC_RUNTIME)
346
347 static void
348 spdk_rpc_dump_nbd_info(struct spdk_json_write_ctx *w,
349 struct spdk_nbd_disk *nbd)
350 {
351 spdk_json_write_object_begin(w);
352
353 spdk_json_write_named_string(w, "nbd_device", spdk_nbd_disk_get_nbd_path(nbd));
354
355 spdk_json_write_named_string(w, "bdev_name", spdk_nbd_disk_get_bdev_name(nbd));
356
357 spdk_json_write_object_end(w);
358 }
359
360 struct rpc_get_nbd_disks {
361 char *nbd_device;
362 };
363
364 static void
365 free_rpc_get_nbd_disks(struct rpc_get_nbd_disks *r)
366 {
367 free(r->nbd_device);
368 }
369
370 static const struct spdk_json_object_decoder rpc_get_nbd_disks_decoders[] = {
371 {"nbd_device", offsetof(struct rpc_get_nbd_disks, nbd_device), spdk_json_decode_string, true},
372 };
373
374 static void
375 spdk_rpc_get_nbd_disks(struct spdk_jsonrpc_request *request,
376 const struct spdk_json_val *params)
377 {
378 struct rpc_get_nbd_disks req = {};
379 struct spdk_json_write_ctx *w;
380 struct spdk_nbd_disk *nbd = NULL;
381
382 if (params != NULL) {
383 if (spdk_json_decode_object(params, rpc_get_nbd_disks_decoders,
384 SPDK_COUNTOF(rpc_get_nbd_disks_decoders),
385 &req)) {
386 SPDK_ERRLOG("spdk_json_decode_object failed\n");
387 goto invalid;
388 }
389
390 if (req.nbd_device) {
391 nbd = spdk_nbd_disk_find_by_nbd_path(req.nbd_device);
392 if (nbd == NULL) {
393 SPDK_ERRLOG("nbd device '%s' does not exist\n", req.nbd_device);
394 goto invalid;
395 }
396
397 free_rpc_get_nbd_disks(&req);
398 }
399 }
400
401 w = spdk_jsonrpc_begin_result(request);
402 if (w == NULL) {
403 return;
404 }
405
406 spdk_json_write_array_begin(w);
407
408 if (nbd != NULL) {
409 spdk_rpc_dump_nbd_info(w, nbd);
410 } else {
411 for (nbd = spdk_nbd_disk_first(); nbd != NULL; nbd = spdk_nbd_disk_next(nbd)) {
412 spdk_rpc_dump_nbd_info(w, nbd);
413 }
414 }
415
416 spdk_json_write_array_end(w);
417
418 spdk_jsonrpc_end_result(request, w);
419
420 return;
421
422 invalid:
423 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
424
425 free_rpc_get_nbd_disks(&req);
426 }
427 SPDK_RPC_REGISTER("get_nbd_disks", spdk_rpc_get_nbd_disks, SPDK_RPC_RUNTIME)