xref: /spdk/module/bdev/split/vbdev_split_rpc.c (revision 0ed85362c8132a2d1927757fbcade66b6660d26a)
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 "spdk/rpc.h"
35 #include "spdk/util.h"
36 #include "spdk/string.h"
37 
38 #include "vbdev_split.h"
39 #include "spdk_internal/log.h"
40 
41 struct rpc_construct_split {
42 	char *base_bdev;
43 	uint32_t split_count;
44 	uint64_t split_size_mb;
45 };
46 
47 static const struct spdk_json_object_decoder rpc_construct_split_decoders[] = {
48 	{"base_bdev", offsetof(struct rpc_construct_split, base_bdev), spdk_json_decode_string},
49 	{"split_count", offsetof(struct rpc_construct_split, split_count), spdk_json_decode_uint32},
50 	{"split_size_mb", offsetof(struct rpc_construct_split, split_size_mb), spdk_json_decode_uint64, true},
51 };
52 
53 static void
54 rpc_bdev_split_create(struct spdk_jsonrpc_request *request,
55 		      const struct spdk_json_val *params)
56 {
57 	struct rpc_construct_split req = {};
58 	struct spdk_json_write_ctx *w;
59 	struct spdk_bdev *base_bdev;
60 	int rc;
61 
62 	if (spdk_json_decode_object(params, rpc_construct_split_decoders,
63 				    SPDK_COUNTOF(rpc_construct_split_decoders),
64 				    &req)) {
65 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
66 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
67 		goto out;
68 	}
69 
70 	rc = create_vbdev_split(req.base_bdev, req.split_count, req.split_size_mb);
71 	if (rc < 0) {
72 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
73 						     "Failed to create %"PRIu32" split bdevs from '%s': %s",
74 						     req.split_count, req.base_bdev, spdk_strerror(-rc));
75 		goto out;
76 	}
77 
78 	w = spdk_jsonrpc_begin_result(request);
79 	spdk_json_write_array_begin(w);
80 
81 	base_bdev = spdk_bdev_get_by_name(req.base_bdev);
82 	if (base_bdev != NULL) {
83 		struct spdk_bdev_part_base *split_base;
84 		struct bdev_part_tailq *split_base_tailq;
85 		struct spdk_bdev_part *split_part;
86 		struct spdk_bdev *split_bdev;
87 
88 		split_base = vbdev_split_get_part_base(base_bdev);
89 
90 		assert(split_base != NULL);
91 
92 		split_base_tailq = spdk_bdev_part_base_get_tailq(split_base);
93 		TAILQ_FOREACH(split_part, split_base_tailq, tailq) {
94 			split_bdev = spdk_bdev_part_get_bdev(split_part);
95 			spdk_json_write_string(w, spdk_bdev_get_name(split_bdev));
96 		}
97 	}
98 
99 	spdk_json_write_array_end(w);
100 	spdk_jsonrpc_end_result(request, w);
101 
102 out:
103 	free(req.base_bdev);
104 }
105 SPDK_RPC_REGISTER("bdev_split_create", rpc_bdev_split_create, SPDK_RPC_RUNTIME)
106 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_split_create, construct_split_vbdev)
107 
108 struct rpc_delete_split {
109 	char *base_bdev;
110 };
111 
112 static const struct spdk_json_object_decoder rpc_delete_split_decoders[] = {
113 	{"base_bdev", offsetof(struct rpc_delete_split, base_bdev), spdk_json_decode_string},
114 };
115 
116 static void
117 rpc_bdev_split_delete(struct spdk_jsonrpc_request *request,
118 		      const struct spdk_json_val *params)
119 {
120 	struct rpc_delete_split req = {};
121 	struct spdk_json_write_ctx *w;
122 	int rc;
123 
124 	if (spdk_json_decode_object(params, rpc_delete_split_decoders,
125 				    SPDK_COUNTOF(rpc_delete_split_decoders),
126 				    &req)) {
127 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
128 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
129 		goto out;
130 	}
131 
132 	rc = vbdev_split_destruct(req.base_bdev);
133 	if (rc < 0) {
134 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc));
135 		goto out;
136 	}
137 
138 	w = spdk_jsonrpc_begin_result(request);
139 	spdk_json_write_bool(w, true);
140 	spdk_jsonrpc_end_result(request, w);
141 out:
142 	free(req.base_bdev);
143 }
144 SPDK_RPC_REGISTER("bdev_split_delete", rpc_bdev_split_delete, SPDK_RPC_RUNTIME)
145 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_split_delete, destruct_split_vbdev)
146