xref: /spdk/module/bdev/aio/bdev_aio_rpc.c (revision c39647df83e4be9bcc49025132c48bf2414ef8b1)
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_aio.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_construct_aio {
41 	char *name;
42 	char *filename;
43 	uint32_t block_size;
44 };
45 
46 struct rpc_construct_aio_ctx {
47 	struct rpc_construct_aio req;
48 	struct spdk_jsonrpc_request *request;
49 };
50 
51 static void
52 free_rpc_construct_aio(struct rpc_construct_aio_ctx *ctx)
53 {
54 	free(ctx->req.name);
55 	free(ctx->req.filename);
56 	free(ctx);
57 }
58 
59 static const struct spdk_json_object_decoder rpc_construct_aio_decoders[] = {
60 	{"name", offsetof(struct rpc_construct_aio, name), spdk_json_decode_string},
61 	{"filename", offsetof(struct rpc_construct_aio, filename), spdk_json_decode_string},
62 	{"block_size", offsetof(struct rpc_construct_aio, block_size), spdk_json_decode_uint32, true},
63 };
64 
65 static void
66 rpc_bdev_aio_create_cb(void *cb_arg)
67 {
68 	struct rpc_construct_aio_ctx *ctx = cb_arg;
69 	struct spdk_jsonrpc_request *request = ctx->request;
70 	struct spdk_json_write_ctx *w;
71 
72 	w = spdk_jsonrpc_begin_result(request);
73 	spdk_json_write_string(w, ctx->req.name);
74 	spdk_jsonrpc_end_result(request, w);
75 	free_rpc_construct_aio(ctx);
76 }
77 
78 static void
79 rpc_bdev_aio_create(struct spdk_jsonrpc_request *request,
80 		    const struct spdk_json_val *params)
81 {
82 	struct rpc_construct_aio_ctx *ctx;
83 	int rc;
84 
85 	ctx = calloc(1, sizeof(*ctx));
86 	if (!ctx) {
87 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
88 		return;
89 	}
90 
91 	if (spdk_json_decode_object(params, rpc_construct_aio_decoders,
92 				    SPDK_COUNTOF(rpc_construct_aio_decoders),
93 				    &ctx->req)) {
94 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
95 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
96 						 "spdk_json_decode_object failed");
97 		free_rpc_construct_aio(ctx);
98 		return;
99 	}
100 
101 	ctx->request = request;
102 	rc = create_aio_bdev(ctx->req.name, ctx->req.filename, ctx->req.block_size);
103 	if (rc) {
104 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
105 		free_rpc_construct_aio(ctx);
106 		return;
107 	}
108 
109 	spdk_bdev_wait_for_examine(rpc_bdev_aio_create_cb, ctx);
110 }
111 SPDK_RPC_REGISTER("bdev_aio_create", rpc_bdev_aio_create, SPDK_RPC_RUNTIME)
112 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_aio_create, construct_aio_bdev)
113 
114 struct rpc_rescan_aio {
115 	char *name;
116 };
117 
118 static const struct spdk_json_object_decoder rpc_rescan_aio_decoders[] = {
119 	{"name", offsetof(struct rpc_rescan_aio, name), spdk_json_decode_string},
120 };
121 
122 static void
123 rpc_bdev_aio_rescan(struct spdk_jsonrpc_request *request,
124 		    const struct spdk_json_val *params)
125 {
126 	struct rpc_rescan_aio req = {NULL};
127 	struct spdk_bdev *bdev;
128 	int bdeverrno;
129 
130 	if (spdk_json_decode_object(params, rpc_rescan_aio_decoders,
131 				    SPDK_COUNTOF(rpc_rescan_aio_decoders),
132 				    &req)) {
133 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
134 						 "spdk_json_decode_object failed");
135 		goto cleanup;
136 	}
137 
138 	bdev = spdk_bdev_get_by_name(req.name);
139 	if (bdev == NULL) {
140 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
141 		goto cleanup;
142 	}
143 
144 	bdeverrno = bdev_aio_rescan(bdev);
145 	spdk_jsonrpc_send_bool_response(request, bdeverrno);
146 
147 cleanup:
148 	free(req.name);
149 }
150 SPDK_RPC_REGISTER("bdev_aio_rescan", rpc_bdev_aio_rescan, SPDK_RPC_RUNTIME)
151 
152 struct rpc_delete_aio {
153 	char *name;
154 };
155 
156 static void
157 free_rpc_delete_aio(struct rpc_delete_aio *r)
158 {
159 	free(r->name);
160 }
161 
162 static const struct spdk_json_object_decoder rpc_delete_aio_decoders[] = {
163 	{"name", offsetof(struct rpc_delete_aio, name), spdk_json_decode_string},
164 };
165 
166 static void
167 _rpc_bdev_aio_delete_cb(void *cb_arg, int bdeverrno)
168 {
169 	struct spdk_jsonrpc_request *request = cb_arg;
170 
171 	spdk_jsonrpc_send_bool_response(request, bdeverrno == 0);
172 }
173 
174 static void
175 rpc_bdev_aio_delete(struct spdk_jsonrpc_request *request,
176 		    const struct spdk_json_val *params)
177 {
178 	struct rpc_delete_aio req = {NULL};
179 	struct spdk_bdev *bdev;
180 
181 	if (spdk_json_decode_object(params, rpc_delete_aio_decoders,
182 				    SPDK_COUNTOF(rpc_delete_aio_decoders),
183 				    &req)) {
184 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
185 						 "spdk_json_decode_object failed");
186 		goto cleanup;
187 	}
188 
189 	bdev = spdk_bdev_get_by_name(req.name);
190 	if (bdev == NULL) {
191 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
192 		goto cleanup;
193 	}
194 
195 	bdev_aio_delete(bdev, _rpc_bdev_aio_delete_cb, request);
196 
197 cleanup:
198 	free_rpc_delete_aio(&req);
199 }
200 SPDK_RPC_REGISTER("bdev_aio_delete", rpc_bdev_aio_delete, SPDK_RPC_RUNTIME)
201 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_aio_delete, delete_aio_bdev)
202