xref: /spdk/module/bdev/null/bdev_null_rpc.c (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019 Mellanox Technologies LTD. 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 "spdk/rpc.h"
35 #include "spdk/util.h"
36 #include "spdk/string.h"
37 #include "spdk/bdev_module.h"
38 #include "spdk/log.h"
39 
40 #include "bdev_null.h"
41 
42 struct rpc_construct_null {
43 	char *name;
44 	char *uuid;
45 	uint64_t num_blocks;
46 	uint32_t block_size;
47 	uint32_t md_size;
48 	int32_t dif_type;
49 	bool dif_is_head_of_md;
50 };
51 
52 static void
53 free_rpc_construct_null(struct rpc_construct_null *req)
54 {
55 	free(req->name);
56 	free(req->uuid);
57 }
58 
59 static const struct spdk_json_object_decoder rpc_construct_null_decoders[] = {
60 	{"name", offsetof(struct rpc_construct_null, name), spdk_json_decode_string},
61 	{"uuid", offsetof(struct rpc_construct_null, uuid), spdk_json_decode_string, true},
62 	{"num_blocks", offsetof(struct rpc_construct_null, num_blocks), spdk_json_decode_uint64},
63 	{"block_size", offsetof(struct rpc_construct_null, block_size), spdk_json_decode_uint32},
64 	{"md_size", offsetof(struct rpc_construct_null, md_size), spdk_json_decode_uint32, true},
65 	{"dif_type", offsetof(struct rpc_construct_null, dif_type), spdk_json_decode_int32, true},
66 	{"dif_is_head_of_md", offsetof(struct rpc_construct_null, dif_is_head_of_md), spdk_json_decode_bool, true},
67 };
68 
69 static void
70 rpc_bdev_null_create(struct spdk_jsonrpc_request *request,
71 		     const struct spdk_json_val *params)
72 {
73 	struct rpc_construct_null req = {};
74 	struct spdk_json_write_ctx *w;
75 	struct spdk_uuid *uuid = NULL;
76 	struct spdk_uuid decoded_uuid;
77 	struct spdk_bdev *bdev;
78 	struct spdk_null_bdev_opts opts = {};
79 	uint32_t data_block_size;
80 	int rc = 0;
81 
82 	if (spdk_json_decode_object(params, rpc_construct_null_decoders,
83 				    SPDK_COUNTOF(rpc_construct_null_decoders),
84 				    &req)) {
85 		SPDK_DEBUGLOG(bdev_null, "spdk_json_decode_object failed\n");
86 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
87 						 "spdk_json_decode_object failed");
88 		goto cleanup;
89 	}
90 
91 	if (req.block_size < req.md_size) {
92 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
93 						     "Interleaved metadata size can not be greater than block size");
94 		goto cleanup;
95 	}
96 	data_block_size = req.block_size - req.md_size;
97 	if (data_block_size % 512 != 0) {
98 		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
99 						     "Data block size %u is not a multiple of 512", req.block_size);
100 		goto cleanup;
101 	}
102 
103 	if (req.num_blocks == 0) {
104 		spdk_jsonrpc_send_error_response(request, -EINVAL,
105 						 "Disk num_blocks must be greater than 0");
106 		goto cleanup;
107 	}
108 
109 	if (req.uuid) {
110 		if (spdk_uuid_parse(&decoded_uuid, req.uuid)) {
111 			spdk_jsonrpc_send_error_response(request, -EINVAL,
112 							 "Failed to parse bdev UUID");
113 			goto cleanup;
114 		}
115 		uuid = &decoded_uuid;
116 	}
117 
118 	if (req.dif_type < SPDK_DIF_DISABLE || req.dif_type > SPDK_DIF_TYPE3) {
119 		spdk_jsonrpc_send_error_response(request, -EINVAL, "Invalid protection information type");
120 		goto cleanup;
121 	}
122 
123 	if (req.dif_type != SPDK_DIF_DISABLE && !req.md_size) {
124 		spdk_jsonrpc_send_error_response(request, -EINVAL,
125 						 "Interleaved metadata size should be set for DIF");
126 		goto cleanup;
127 	}
128 
129 	opts.name = req.name;
130 	opts.uuid = uuid;
131 	opts.num_blocks = req.num_blocks;
132 	opts.block_size = req.block_size;
133 	opts.md_size = req.md_size;
134 	opts.md_interleave = true;
135 	opts.dif_type = req.dif_type;
136 	opts.dif_is_head_of_md = req.dif_is_head_of_md;
137 	rc = bdev_null_create(&bdev, &opts);
138 	if (rc) {
139 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
140 		goto cleanup;
141 	}
142 
143 	w = spdk_jsonrpc_begin_result(request);
144 	spdk_json_write_string(w, bdev->name);
145 	spdk_jsonrpc_end_result(request, w);
146 	free_rpc_construct_null(&req);
147 	return;
148 
149 cleanup:
150 	free_rpc_construct_null(&req);
151 }
152 SPDK_RPC_REGISTER("bdev_null_create", rpc_bdev_null_create, SPDK_RPC_RUNTIME)
153 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_null_create, construct_null_bdev)
154 
155 struct rpc_delete_null {
156 	char *name;
157 };
158 
159 static void
160 free_rpc_delete_null(struct rpc_delete_null *req)
161 {
162 	free(req->name);
163 }
164 
165 static const struct spdk_json_object_decoder rpc_delete_null_decoders[] = {
166 	{"name", offsetof(struct rpc_delete_null, name), spdk_json_decode_string},
167 };
168 
169 static void
170 rpc_bdev_null_delete_cb(void *cb_arg, int bdeverrno)
171 {
172 	struct spdk_jsonrpc_request *request = cb_arg;
173 
174 	spdk_jsonrpc_send_bool_response(request, bdeverrno == 0);
175 }
176 
177 static void
178 rpc_bdev_null_delete(struct spdk_jsonrpc_request *request,
179 		     const struct spdk_json_val *params)
180 {
181 	struct rpc_delete_null req = {NULL};
182 	struct spdk_bdev *bdev;
183 
184 	if (spdk_json_decode_object(params, rpc_delete_null_decoders,
185 				    SPDK_COUNTOF(rpc_delete_null_decoders),
186 				    &req)) {
187 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
188 						 "spdk_json_decode_object failed");
189 		goto cleanup;
190 	}
191 
192 	bdev = spdk_bdev_get_by_name(req.name);
193 	if (bdev == NULL) {
194 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
195 		goto cleanup;
196 	}
197 
198 	bdev_null_delete(bdev, rpc_bdev_null_delete_cb, request);
199 
200 	free_rpc_delete_null(&req);
201 
202 	return;
203 
204 cleanup:
205 	free_rpc_delete_null(&req);
206 }
207 SPDK_RPC_REGISTER("bdev_null_delete", rpc_bdev_null_delete, SPDK_RPC_RUNTIME)
208 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_null_delete, delete_null_bdev)
209 
210 struct rpc_bdev_null_resize {
211 	char *name;
212 	uint64_t new_size;
213 };
214 
215 static const struct spdk_json_object_decoder rpc_bdev_null_resize_decoders[] = {
216 	{"name", offsetof(struct rpc_bdev_null_resize, name), spdk_json_decode_string},
217 	{"new_size", offsetof(struct rpc_bdev_null_resize, new_size), spdk_json_decode_uint64}
218 };
219 
220 static void
221 free_rpc_bdev_null_resize(struct rpc_bdev_null_resize *req)
222 {
223 	free(req->name);
224 }
225 
226 static void
227 spdk_rpc_bdev_null_resize(struct spdk_jsonrpc_request *request,
228 			  const struct spdk_json_val *params)
229 {
230 	struct rpc_bdev_null_resize req = {};
231 	struct spdk_bdev *bdev;
232 	int rc;
233 
234 	if (spdk_json_decode_object(params, rpc_bdev_null_resize_decoders,
235 				    SPDK_COUNTOF(rpc_bdev_null_resize_decoders),
236 				    &req)) {
237 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
238 						 "spdk_json_decode_object failed");
239 		goto cleanup;
240 	}
241 
242 	bdev = spdk_bdev_get_by_name(req.name);
243 	if (bdev == NULL) {
244 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
245 		goto cleanup;
246 	}
247 
248 	rc = bdev_null_resize(bdev, req.new_size);
249 	if (rc) {
250 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
251 		goto cleanup;
252 	}
253 
254 	spdk_jsonrpc_send_bool_response(request, true);
255 cleanup:
256 	free_rpc_bdev_null_resize(&req);
257 }
258 SPDK_RPC_REGISTER("bdev_null_resize", spdk_rpc_bdev_null_resize, SPDK_RPC_RUNTIME)
259