1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2019 Intel Corporation.
3 * Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4 * All rights reserved.
5 */
6
7 #include "spdk/stdinc.h"
8
9 #include "vbdev_zone_block.h"
10
11 #include "spdk/util.h"
12 #include "spdk/string.h"
13 #include "spdk/rpc.h"
14
15 #include "spdk/log.h"
16
17 struct rpc_construct_zone_block {
18 char *name;
19 char *base_bdev;
20 uint64_t zone_capacity;
21 uint64_t optimal_open_zones;
22 };
23
24 static void
free_rpc_construct_zone_block(struct rpc_construct_zone_block * req)25 free_rpc_construct_zone_block(struct rpc_construct_zone_block *req)
26 {
27 free(req->name);
28 free(req->base_bdev);
29 }
30
31 static const struct spdk_json_object_decoder rpc_construct_zone_block_decoders[] = {
32 {"name", offsetof(struct rpc_construct_zone_block, name), spdk_json_decode_string},
33 {"base_bdev", offsetof(struct rpc_construct_zone_block, base_bdev), spdk_json_decode_string},
34 {"zone_capacity", offsetof(struct rpc_construct_zone_block, zone_capacity), spdk_json_decode_uint64},
35 {"optimal_open_zones", offsetof(struct rpc_construct_zone_block, optimal_open_zones), spdk_json_decode_uint64},
36 };
37
38 static void
rpc_zone_block_create(struct spdk_jsonrpc_request * request,const struct spdk_json_val * params)39 rpc_zone_block_create(struct spdk_jsonrpc_request *request,
40 const struct spdk_json_val *params)
41 {
42 struct rpc_construct_zone_block req = {};
43 struct spdk_json_write_ctx *w;
44 int rc;
45
46 if (spdk_json_decode_object(params, rpc_construct_zone_block_decoders,
47 SPDK_COUNTOF(rpc_construct_zone_block_decoders),
48 &req)) {
49 SPDK_ERRLOG("Failed to decode block create parameters");
50 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
51 "Invalid parameters");
52 goto cleanup;
53 }
54
55 rc = vbdev_zone_block_create(req.base_bdev, req.name, req.zone_capacity,
56 req.optimal_open_zones);
57 if (rc) {
58 SPDK_ERRLOG("Failed to create block zoned vbdev: %s", spdk_strerror(-rc));
59 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
60 "Failed to create block zoned vbdev: %s",
61 spdk_strerror(-rc));
62 goto cleanup;
63 }
64
65 w = spdk_jsonrpc_begin_result(request);
66 spdk_json_write_string(w, req.name);
67 spdk_jsonrpc_end_result(request, w);
68
69 cleanup:
70 free_rpc_construct_zone_block(&req);
71 }
72 SPDK_RPC_REGISTER("bdev_zone_block_create", rpc_zone_block_create, SPDK_RPC_RUNTIME)
73
74 struct rpc_delete_zone_block {
75 char *name;
76 };
77
78 static void
free_rpc_delete_zone_block(struct rpc_delete_zone_block * req)79 free_rpc_delete_zone_block(struct rpc_delete_zone_block *req)
80 {
81 free(req->name);
82 }
83
84 static const struct spdk_json_object_decoder rpc_delete_zone_block_decoders[] = {
85 {"name", offsetof(struct rpc_delete_zone_block, name), spdk_json_decode_string},
86 };
87
88 static void
_rpc_delete_zone_block_cb(void * cb_ctx,int rc)89 _rpc_delete_zone_block_cb(void *cb_ctx, int rc)
90 {
91 struct spdk_jsonrpc_request *request = cb_ctx;
92
93 if (rc == 0) {
94 spdk_jsonrpc_send_bool_response(request, true);
95 } else {
96 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
97 }
98 }
99
100 static void
rpc_zone_block_delete(struct spdk_jsonrpc_request * request,const struct spdk_json_val * params)101 rpc_zone_block_delete(struct spdk_jsonrpc_request *request,
102 const struct spdk_json_val *params)
103 {
104 struct rpc_delete_zone_block attrs = {};
105
106 if (spdk_json_decode_object(params, rpc_delete_zone_block_decoders,
107 SPDK_COUNTOF(rpc_delete_zone_block_decoders),
108 &attrs)) {
109 SPDK_ERRLOG("Failed to decode block delete parameters");
110 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
111 "Invalid parameters");
112 goto cleanup;
113 }
114
115 vbdev_zone_block_delete(attrs.name, _rpc_delete_zone_block_cb, request);
116
117 cleanup:
118 free_rpc_delete_zone_block(&attrs);
119 }
120 SPDK_RPC_REGISTER("bdev_zone_block_delete", rpc_zone_block_delete, SPDK_RPC_RUNTIME)
121