Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2017 Intel Corporation. All rights reserved.
3 : : * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/rpc.h"
7 : : #include "spdk/util.h"
8 : : #include "spdk/string.h"
9 : : #include "spdk/bdev_module.h"
10 : : #include "spdk/log.h"
11 : :
12 : : #include "bdev_null.h"
13 : :
14 : : struct rpc_construct_null {
15 : : char *name;
16 : : struct spdk_uuid uuid;
17 : : uint64_t num_blocks;
18 : : uint32_t block_size;
19 : : uint32_t physical_block_size;
20 : : uint32_t md_size;
21 : : int32_t dif_type;
22 : : bool dif_is_head_of_md;
23 : : };
24 : :
25 : : static void
26 : 152 : free_rpc_construct_null(struct rpc_construct_null *req)
27 : : {
28 : 152 : free(req->name);
29 : 152 : }
30 : :
31 : : static const struct spdk_json_object_decoder rpc_construct_null_decoders[] = {
32 : : {"name", offsetof(struct rpc_construct_null, name), spdk_json_decode_string},
33 : : {"uuid", offsetof(struct rpc_construct_null, uuid), spdk_json_decode_uuid, true},
34 : : {"num_blocks", offsetof(struct rpc_construct_null, num_blocks), spdk_json_decode_uint64},
35 : : {"block_size", offsetof(struct rpc_construct_null, block_size), spdk_json_decode_uint32},
36 : : {"physical_block_size", offsetof(struct rpc_construct_null, physical_block_size), spdk_json_decode_uint32, true},
37 : : {"md_size", offsetof(struct rpc_construct_null, md_size), spdk_json_decode_uint32, true},
38 : : {"dif_type", offsetof(struct rpc_construct_null, dif_type), spdk_json_decode_int32, true},
39 : : {"dif_is_head_of_md", offsetof(struct rpc_construct_null, dif_is_head_of_md), spdk_json_decode_bool, true},
40 : : };
41 : :
42 : : static void
43 : 152 : rpc_bdev_null_create(struct spdk_jsonrpc_request *request,
44 : : const struct spdk_json_val *params)
45 : : {
46 : 152 : struct rpc_construct_null req = {};
47 : : struct spdk_json_write_ctx *w;
48 : 35 : struct spdk_bdev *bdev;
49 : 152 : struct spdk_null_bdev_opts opts = {};
50 : 152 : int rc = 0;
51 : :
52 [ - + ]: 152 : if (spdk_json_decode_object(params, rpc_construct_null_decoders,
53 : : SPDK_COUNTOF(rpc_construct_null_decoders),
54 : : &req)) {
55 [ # # # # ]: 0 : SPDK_DEBUGLOG(bdev_null, "spdk_json_decode_object failed\n");
56 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
57 : : "spdk_json_decode_object failed");
58 : 0 : goto cleanup;
59 : : }
60 : :
61 [ - + ]: 152 : if (req.block_size < req.md_size) {
62 : 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
63 : : "Interleaved metadata size can not be greater than block size");
64 : 0 : goto cleanup;
65 : : }
66 : :
67 [ - + ]: 152 : if (req.block_size % 512 != 0) {
68 : 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
69 : : "Data block size %u is not a multiple of 512", req.block_size);
70 : 0 : goto cleanup;
71 : : }
72 : :
73 [ - + ]: 152 : if (req.physical_block_size % 512 != 0) {
74 : 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
75 : : "Physical block size %u is not a multiple of 512", req.physical_block_size);
76 : : }
77 : :
78 [ - + ]: 152 : if (req.num_blocks == 0) {
79 : 0 : spdk_jsonrpc_send_error_response(request, -EINVAL,
80 : : "Disk num_blocks must be greater than 0");
81 : 0 : goto cleanup;
82 : : }
83 : :
84 [ + - - + ]: 152 : if (req.dif_type < SPDK_DIF_DISABLE || req.dif_type > SPDK_DIF_TYPE3) {
85 : 0 : spdk_jsonrpc_send_error_response(request, -EINVAL, "Invalid protection information type");
86 : 0 : goto cleanup;
87 : : }
88 : :
89 [ + + - + ]: 152 : if (req.dif_type != SPDK_DIF_DISABLE && !req.md_size) {
90 : 0 : spdk_jsonrpc_send_error_response(request, -EINVAL,
91 : : "Interleaved metadata size should be set for DIF");
92 : 0 : goto cleanup;
93 : : }
94 : :
95 : 152 : opts.name = req.name;
96 : 152 : opts.uuid = &req.uuid;
97 : 152 : opts.num_blocks = req.num_blocks;
98 : 152 : opts.block_size = req.block_size + req.md_size;
99 : 152 : opts.physical_block_size = req.physical_block_size;
100 : 152 : opts.md_size = req.md_size;
101 : 152 : opts.md_interleave = true;
102 : 152 : opts.dif_type = req.dif_type;
103 [ - + ]: 152 : opts.dif_is_head_of_md = req.dif_is_head_of_md;
104 : 152 : rc = bdev_null_create(&bdev, &opts);
105 [ - + ]: 152 : if (rc) {
106 : 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
107 : 0 : goto cleanup;
108 : : }
109 : :
110 : 152 : w = spdk_jsonrpc_begin_result(request);
111 : 152 : spdk_json_write_string(w, bdev->name);
112 : 152 : spdk_jsonrpc_end_result(request, w);
113 : 152 : free_rpc_construct_null(&req);
114 : 152 : return;
115 : :
116 : 0 : cleanup:
117 : 0 : free_rpc_construct_null(&req);
118 : : }
119 : 2337 : SPDK_RPC_REGISTER("bdev_null_create", rpc_bdev_null_create, SPDK_RPC_RUNTIME)
120 : :
121 : : struct rpc_delete_null {
122 : : char *name;
123 : : };
124 : :
125 : : static void
126 : 58 : free_rpc_delete_null(struct rpc_delete_null *req)
127 : : {
128 : 58 : free(req->name);
129 : 58 : }
130 : :
131 : : static const struct spdk_json_object_decoder rpc_delete_null_decoders[] = {
132 : : {"name", offsetof(struct rpc_delete_null, name), spdk_json_decode_string},
133 : : };
134 : :
135 : : static void
136 : 58 : rpc_bdev_null_delete_cb(void *cb_arg, int bdeverrno)
137 : : {
138 : 58 : struct spdk_jsonrpc_request *request = cb_arg;
139 : :
140 [ + - ]: 58 : if (bdeverrno == 0) {
141 : 58 : spdk_jsonrpc_send_bool_response(request, true);
142 : : } else {
143 : 0 : spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
144 : : }
145 : 58 : }
146 : :
147 : : static void
148 : 58 : rpc_bdev_null_delete(struct spdk_jsonrpc_request *request,
149 : : const struct spdk_json_val *params)
150 : : {
151 : 58 : struct rpc_delete_null req = {NULL};
152 : :
153 [ - + ]: 58 : if (spdk_json_decode_object(params, rpc_delete_null_decoders,
154 : : SPDK_COUNTOF(rpc_delete_null_decoders),
155 : : &req)) {
156 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
157 : : "spdk_json_decode_object failed");
158 : 0 : goto cleanup;
159 : : }
160 : :
161 : 58 : bdev_null_delete(req.name, rpc_bdev_null_delete_cb, request);
162 : :
163 : 58 : free_rpc_delete_null(&req);
164 : :
165 : 58 : return;
166 : :
167 : 0 : cleanup:
168 : 0 : free_rpc_delete_null(&req);
169 : : }
170 : 2337 : SPDK_RPC_REGISTER("bdev_null_delete", rpc_bdev_null_delete, SPDK_RPC_RUNTIME)
171 : :
172 : : struct rpc_bdev_null_resize {
173 : : char *name;
174 : : uint64_t new_size;
175 : : };
176 : :
177 : : static const struct spdk_json_object_decoder rpc_bdev_null_resize_decoders[] = {
178 : : {"name", offsetof(struct rpc_bdev_null_resize, name), spdk_json_decode_string},
179 : : {"new_size", offsetof(struct rpc_bdev_null_resize, new_size), spdk_json_decode_uint64}
180 : : };
181 : :
182 : : static void
183 : 114 : free_rpc_bdev_null_resize(struct rpc_bdev_null_resize *req)
184 : : {
185 : 114 : free(req->name);
186 : 114 : }
187 : :
188 : : static void
189 : 114 : spdk_rpc_bdev_null_resize(struct spdk_jsonrpc_request *request,
190 : : const struct spdk_json_val *params)
191 : : {
192 : 114 : struct rpc_bdev_null_resize req = {};
193 : : int rc;
194 : :
195 [ - + ]: 114 : if (spdk_json_decode_object(params, rpc_bdev_null_resize_decoders,
196 : : SPDK_COUNTOF(rpc_bdev_null_resize_decoders),
197 : : &req)) {
198 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
199 : : "spdk_json_decode_object failed");
200 : 0 : goto cleanup;
201 : : }
202 : :
203 : 114 : rc = bdev_null_resize(req.name, req.new_size);
204 [ - + ]: 114 : if (rc) {
205 : 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
206 : 0 : goto cleanup;
207 : : }
208 : :
209 : 114 : spdk_jsonrpc_send_bool_response(request, true);
210 : 114 : cleanup:
211 : 114 : free_rpc_bdev_null_resize(&req);
212 : 114 : }
213 : 2337 : SPDK_RPC_REGISTER("bdev_null_resize", spdk_rpc_bdev_null_resize, SPDK_RPC_RUNTIME)
|