xref: /spdk/module/bdev/aio/bdev_aio_rpc.c (revision 2f5c602574a98ede645991abe279a96e19c50196)
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_delete_aio {
115 	char *name;
116 };
117 
118 static void
119 free_rpc_delete_aio(struct rpc_delete_aio *r)
120 {
121 	free(r->name);
122 }
123 
124 static const struct spdk_json_object_decoder rpc_delete_aio_decoders[] = {
125 	{"name", offsetof(struct rpc_delete_aio, name), spdk_json_decode_string},
126 };
127 
128 static void
129 _rpc_bdev_aio_delete_cb(void *cb_arg, int bdeverrno)
130 {
131 	struct spdk_jsonrpc_request *request = cb_arg;
132 
133 	spdk_jsonrpc_send_bool_response(request, bdeverrno == 0);
134 }
135 
136 static void
137 rpc_bdev_aio_delete(struct spdk_jsonrpc_request *request,
138 		    const struct spdk_json_val *params)
139 {
140 	struct rpc_delete_aio req = {NULL};
141 	struct spdk_bdev *bdev;
142 
143 	if (spdk_json_decode_object(params, rpc_delete_aio_decoders,
144 				    SPDK_COUNTOF(rpc_delete_aio_decoders),
145 				    &req)) {
146 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
147 						 "spdk_json_decode_object failed");
148 		goto cleanup;
149 	}
150 
151 	bdev = spdk_bdev_get_by_name(req.name);
152 	if (bdev == NULL) {
153 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
154 		goto cleanup;
155 	}
156 
157 	bdev_aio_delete(bdev, _rpc_bdev_aio_delete_cb, request);
158 
159 cleanup:
160 	free_rpc_delete_aio(&req);
161 }
162 SPDK_RPC_REGISTER("bdev_aio_delete", rpc_bdev_aio_delete, SPDK_RPC_RUNTIME)
163 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_aio_delete, delete_aio_bdev)
164