xref: /spdk/module/bdev/aio/bdev_aio_rpc.c (revision 03e3fc4f5835983a4e6602b4e770922e798ce263)
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_internal/log.h"
39 
40 struct rpc_construct_aio {
41 	char *name;
42 	char *filename;
43 	uint32_t block_size;
44 };
45 
46 static void
47 free_rpc_construct_aio(struct rpc_construct_aio *req)
48 {
49 	free(req->name);
50 	free(req->filename);
51 }
52 
53 static const struct spdk_json_object_decoder rpc_construct_aio_decoders[] = {
54 	{"name", offsetof(struct rpc_construct_aio, name), spdk_json_decode_string},
55 	{"filename", offsetof(struct rpc_construct_aio, filename), spdk_json_decode_string},
56 	{"block_size", offsetof(struct rpc_construct_aio, block_size), spdk_json_decode_uint32, true},
57 };
58 
59 static void
60 rpc_bdev_aio_create(struct spdk_jsonrpc_request *request,
61 		    const struct spdk_json_val *params)
62 {
63 	struct rpc_construct_aio req = {};
64 	struct spdk_json_write_ctx *w;
65 	int rc = 0;
66 
67 	if (spdk_json_decode_object(params, rpc_construct_aio_decoders,
68 				    SPDK_COUNTOF(rpc_construct_aio_decoders),
69 				    &req)) {
70 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
71 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
72 						 "spdk_json_decode_object failed");
73 		goto cleanup;
74 	}
75 
76 	rc = create_aio_bdev(req.name, req.filename, req.block_size);
77 	if (rc) {
78 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
79 		goto cleanup;
80 	}
81 
82 
83 	w = spdk_jsonrpc_begin_result(request);
84 	spdk_json_write_string(w, req.name);
85 	spdk_jsonrpc_end_result(request, w);
86 
87 cleanup:
88 	free_rpc_construct_aio(&req);
89 }
90 SPDK_RPC_REGISTER("bdev_aio_create", rpc_bdev_aio_create, SPDK_RPC_RUNTIME)
91 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_aio_create, construct_aio_bdev)
92 
93 struct rpc_delete_aio {
94 	char *name;
95 };
96 
97 static void
98 free_rpc_delete_aio(struct rpc_delete_aio *r)
99 {
100 	free(r->name);
101 }
102 
103 static const struct spdk_json_object_decoder rpc_delete_aio_decoders[] = {
104 	{"name", offsetof(struct rpc_delete_aio, name), spdk_json_decode_string},
105 };
106 
107 static void
108 _rpc_bdev_aio_delete_cb(void *cb_arg, int bdeverrno)
109 {
110 	struct spdk_jsonrpc_request *request = cb_arg;
111 	struct spdk_json_write_ctx *w = spdk_jsonrpc_begin_result(request);
112 
113 	spdk_json_write_bool(w, bdeverrno == 0);
114 	spdk_jsonrpc_end_result(request, w);
115 }
116 
117 static void
118 rpc_bdev_aio_delete(struct spdk_jsonrpc_request *request,
119 		    const struct spdk_json_val *params)
120 {
121 	struct rpc_delete_aio req = {NULL};
122 	struct spdk_bdev *bdev;
123 
124 	if (spdk_json_decode_object(params, rpc_delete_aio_decoders,
125 				    SPDK_COUNTOF(rpc_delete_aio_decoders),
126 				    &req)) {
127 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
128 						 "spdk_json_decode_object failed");
129 		goto cleanup;
130 	}
131 
132 	bdev = spdk_bdev_get_by_name(req.name);
133 	if (bdev == NULL) {
134 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
135 		goto cleanup;
136 	}
137 
138 	bdev_aio_delete(bdev, _rpc_bdev_aio_delete_cb, request);
139 
140 	free_rpc_delete_aio(&req);
141 
142 	return;
143 
144 cleanup:
145 	free_rpc_delete_aio(&req);
146 }
147 SPDK_RPC_REGISTER("bdev_aio_delete", rpc_bdev_aio_delete, SPDK_RPC_RUNTIME)
148 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_aio_delete, delete_aio_bdev)
149