Line data Source code
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
25 16 : free_rpc_construct_zone_block(struct rpc_construct_zone_block *req)
26 : {
27 16 : free(req->name);
28 16 : free(req->base_bdev);
29 16 : }
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
39 16 : rpc_zone_block_create(struct spdk_jsonrpc_request *request,
40 : const struct spdk_json_val *params)
41 : {
42 16 : struct rpc_construct_zone_block req = {};
43 : struct spdk_json_write_ctx *w;
44 : int rc;
45 :
46 16 : if (spdk_json_decode_object(params, rpc_construct_zone_block_decoders,
47 : SPDK_COUNTOF(rpc_construct_zone_block_decoders),
48 : &req)) {
49 0 : SPDK_ERRLOG("Failed to decode block create parameters");
50 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
51 : "Invalid parameters");
52 0 : goto cleanup;
53 : }
54 :
55 16 : rc = vbdev_zone_block_create(req.base_bdev, req.name, req.zone_capacity,
56 : req.optimal_open_zones);
57 16 : if (rc) {
58 4 : SPDK_ERRLOG("Failed to create block zoned vbdev: %s", spdk_strerror(-rc));
59 4 : 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 4 : goto cleanup;
63 : }
64 :
65 12 : w = spdk_jsonrpc_begin_result(request);
66 12 : spdk_json_write_string(w, req.name);
67 12 : spdk_jsonrpc_end_result(request, w);
68 :
69 16 : cleanup:
70 16 : free_rpc_construct_zone_block(&req);
71 16 : }
72 1 : 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
79 12 : free_rpc_delete_zone_block(struct rpc_delete_zone_block *req)
80 : {
81 12 : free(req->name);
82 12 : }
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
89 12 : _rpc_delete_zone_block_cb(void *cb_ctx, int rc)
90 : {
91 12 : struct spdk_jsonrpc_request *request = cb_ctx;
92 :
93 12 : if (rc == 0) {
94 12 : spdk_jsonrpc_send_bool_response(request, true);
95 : } else {
96 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
97 : }
98 12 : }
99 :
100 : static void
101 12 : rpc_zone_block_delete(struct spdk_jsonrpc_request *request,
102 : const struct spdk_json_val *params)
103 : {
104 12 : struct rpc_delete_zone_block attrs = {};
105 :
106 12 : if (spdk_json_decode_object(params, rpc_delete_zone_block_decoders,
107 : SPDK_COUNTOF(rpc_delete_zone_block_decoders),
108 : &attrs)) {
109 0 : SPDK_ERRLOG("Failed to decode block delete parameters");
110 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
111 : "Invalid parameters");
112 0 : goto cleanup;
113 : }
114 :
115 12 : vbdev_zone_block_delete(attrs.name, _rpc_delete_zone_block_cb, request);
116 :
117 12 : cleanup:
118 12 : free_rpc_delete_zone_block(&attrs);
119 12 : }
120 1 : SPDK_RPC_REGISTER("bdev_zone_block_delete", rpc_zone_block_delete, SPDK_RPC_RUNTIME)
|