xref: /spdk/module/bdev/aio/bdev_aio_rpc.c (revision 927f1fd57bd004df581518466ec4c1b8083e5d23)
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 	int bdeverrno;
128 
129 	if (spdk_json_decode_object(params, rpc_rescan_aio_decoders,
130 				    SPDK_COUNTOF(rpc_rescan_aio_decoders),
131 				    &req)) {
132 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
133 						 "spdk_json_decode_object failed");
134 		goto cleanup;
135 	}
136 
137 	bdeverrno = bdev_aio_rescan(req.name);
138 	spdk_jsonrpc_send_bool_response(request, bdeverrno);
139 
140 cleanup:
141 	free(req.name);
142 }
143 SPDK_RPC_REGISTER("bdev_aio_rescan", rpc_bdev_aio_rescan, SPDK_RPC_RUNTIME)
144 
145 struct rpc_delete_aio {
146 	char *name;
147 };
148 
149 static void
150 free_rpc_delete_aio(struct rpc_delete_aio *r)
151 {
152 	free(r->name);
153 }
154 
155 static const struct spdk_json_object_decoder rpc_delete_aio_decoders[] = {
156 	{"name", offsetof(struct rpc_delete_aio, name), spdk_json_decode_string},
157 };
158 
159 static void
160 _rpc_bdev_aio_delete_cb(void *cb_arg, int bdeverrno)
161 {
162 	struct spdk_jsonrpc_request *request = cb_arg;
163 
164 	if (bdeverrno == 0) {
165 		spdk_jsonrpc_send_bool_response(request, true);
166 	} else {
167 		spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
168 	}
169 }
170 
171 static void
172 rpc_bdev_aio_delete(struct spdk_jsonrpc_request *request,
173 		    const struct spdk_json_val *params)
174 {
175 	struct rpc_delete_aio req = {NULL};
176 
177 	if (spdk_json_decode_object(params, rpc_delete_aio_decoders,
178 				    SPDK_COUNTOF(rpc_delete_aio_decoders),
179 				    &req)) {
180 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
181 						 "spdk_json_decode_object failed");
182 		goto cleanup;
183 	}
184 
185 	bdev_aio_delete(req.name, _rpc_bdev_aio_delete_cb, request);
186 
187 cleanup:
188 	free_rpc_delete_aio(&req);
189 }
190 SPDK_RPC_REGISTER("bdev_aio_delete", rpc_bdev_aio_delete, SPDK_RPC_RUNTIME)
191 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_aio_delete, delete_aio_bdev)
192