Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2016 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "bdev_aio.h"
7 : : #include "spdk/rpc.h"
8 : : #include "spdk/util.h"
9 : : #include "spdk/string.h"
10 : : #include "spdk/log.h"
11 : :
12 : : struct rpc_construct_aio {
13 : : char *name;
14 : : char *filename;
15 : : uint32_t block_size;
16 : : bool readonly;
17 : : bool fallocate;
18 : : };
19 : :
20 : : struct rpc_construct_aio_ctx {
21 : : struct rpc_construct_aio req;
22 : : struct spdk_jsonrpc_request *request;
23 : : };
24 : :
25 : : static void
26 : 141 : free_rpc_construct_aio(struct rpc_construct_aio_ctx *ctx)
27 : : {
28 : 141 : free(ctx->req.name);
29 : 141 : free(ctx->req.filename);
30 : 141 : free(ctx);
31 : 141 : }
32 : :
33 : : static const struct spdk_json_object_decoder rpc_construct_aio_decoders[] = {
34 : : {"name", offsetof(struct rpc_construct_aio, name), spdk_json_decode_string},
35 : : {"filename", offsetof(struct rpc_construct_aio, filename), spdk_json_decode_string},
36 : : {"block_size", offsetof(struct rpc_construct_aio, block_size), spdk_json_decode_uint32, true},
37 : : {"readonly", offsetof(struct rpc_construct_aio, readonly), spdk_json_decode_bool, true},
38 : : {"fallocate", offsetof(struct rpc_construct_aio, fallocate), spdk_json_decode_bool, true},
39 : : };
40 : :
41 : : static void
42 : 141 : rpc_bdev_aio_create_cb(void *cb_arg)
43 : : {
44 : 141 : struct rpc_construct_aio_ctx *ctx = cb_arg;
45 : 141 : struct spdk_jsonrpc_request *request = ctx->request;
46 : : struct spdk_json_write_ctx *w;
47 : :
48 : 141 : w = spdk_jsonrpc_begin_result(request);
49 : 141 : spdk_json_write_string(w, ctx->req.name);
50 : 141 : spdk_jsonrpc_end_result(request, w);
51 : 141 : free_rpc_construct_aio(ctx);
52 : 141 : }
53 : :
54 : : static void
55 : 141 : rpc_bdev_aio_create(struct spdk_jsonrpc_request *request,
56 : : const struct spdk_json_val *params)
57 : : {
58 : : struct rpc_construct_aio_ctx *ctx;
59 : : int rc;
60 : :
61 : 141 : ctx = calloc(1, sizeof(*ctx));
62 [ - + ]: 141 : if (!ctx) {
63 : 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
64 : 0 : return;
65 : : }
66 : :
67 [ - + ]: 141 : if (spdk_json_decode_object(params, rpc_construct_aio_decoders,
68 : : SPDK_COUNTOF(rpc_construct_aio_decoders),
69 : 141 : &ctx->req)) {
70 : 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
71 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
72 : : "spdk_json_decode_object failed");
73 : 0 : free_rpc_construct_aio(ctx);
74 : 0 : return;
75 : : }
76 : :
77 : 141 : ctx->request = request;
78 : 141 : rc = create_aio_bdev(ctx->req.name, ctx->req.filename, ctx->req.block_size,
79 [ - + - + ]: 141 : ctx->req.readonly, ctx->req.fallocate);
80 [ - + ]: 141 : if (rc) {
81 : 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
82 : 0 : free_rpc_construct_aio(ctx);
83 : 0 : return;
84 : : }
85 : :
86 : 141 : spdk_bdev_wait_for_examine(rpc_bdev_aio_create_cb, ctx);
87 : : }
88 : 2120 : SPDK_RPC_REGISTER("bdev_aio_create", rpc_bdev_aio_create, SPDK_RPC_RUNTIME)
89 : :
90 : : struct rpc_rescan_aio {
91 : : char *name;
92 : : };
93 : :
94 : : static const struct spdk_json_object_decoder rpc_rescan_aio_decoders[] = {
95 : : {"name", offsetof(struct rpc_rescan_aio, name), spdk_json_decode_string},
96 : : };
97 : :
98 : : static void
99 : 0 : rpc_bdev_aio_rescan(struct spdk_jsonrpc_request *request,
100 : : const struct spdk_json_val *params)
101 : : {
102 : 0 : struct rpc_rescan_aio req = {NULL};
103 : : int bdeverrno;
104 : :
105 [ # # ]: 0 : if (spdk_json_decode_object(params, rpc_rescan_aio_decoders,
106 : : SPDK_COUNTOF(rpc_rescan_aio_decoders),
107 : : &req)) {
108 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
109 : : "spdk_json_decode_object failed");
110 : 0 : goto cleanup;
111 : : }
112 : :
113 : 0 : bdeverrno = bdev_aio_rescan(req.name);
114 [ # # ]: 0 : if (bdeverrno) {
115 : 0 : spdk_jsonrpc_send_error_response(request, bdeverrno,
116 : : spdk_strerror(-bdeverrno));
117 : 0 : goto cleanup;
118 : : }
119 : :
120 : 0 : spdk_jsonrpc_send_bool_response(request, true);
121 : 0 : cleanup:
122 : 0 : free(req.name);
123 : 0 : }
124 : 2120 : SPDK_RPC_REGISTER("bdev_aio_rescan", rpc_bdev_aio_rescan, SPDK_RPC_RUNTIME)
125 : :
126 : : struct rpc_delete_aio {
127 : : char *name;
128 : : };
129 : :
130 : : static void
131 : 37 : free_rpc_delete_aio(struct rpc_delete_aio *r)
132 : : {
133 : 37 : free(r->name);
134 : 37 : }
135 : :
136 : : static const struct spdk_json_object_decoder rpc_delete_aio_decoders[] = {
137 : : {"name", offsetof(struct rpc_delete_aio, name), spdk_json_decode_string},
138 : : };
139 : :
140 : : static void
141 : 37 : _rpc_bdev_aio_delete_cb(void *cb_arg, int bdeverrno)
142 : : {
143 : 37 : struct spdk_jsonrpc_request *request = cb_arg;
144 : :
145 [ + - ]: 37 : if (bdeverrno == 0) {
146 : 37 : spdk_jsonrpc_send_bool_response(request, true);
147 : : } else {
148 : 0 : spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
149 : : }
150 : 37 : }
151 : :
152 : : static void
153 : 37 : rpc_bdev_aio_delete(struct spdk_jsonrpc_request *request,
154 : : const struct spdk_json_val *params)
155 : : {
156 : 37 : struct rpc_delete_aio req = {NULL};
157 : :
158 [ - + ]: 37 : if (spdk_json_decode_object(params, rpc_delete_aio_decoders,
159 : : SPDK_COUNTOF(rpc_delete_aio_decoders),
160 : : &req)) {
161 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
162 : : "spdk_json_decode_object failed");
163 : 0 : goto cleanup;
164 : : }
165 : :
166 : 37 : bdev_aio_delete(req.name, _rpc_bdev_aio_delete_cb, request);
167 : :
168 : 37 : cleanup:
169 : 37 : free_rpc_delete_aio(&req);
170 : 37 : }
171 : 2120 : SPDK_RPC_REGISTER("bdev_aio_delete", rpc_bdev_aio_delete, SPDK_RPC_RUNTIME)
|