xref: /spdk/module/bdev/malloc/bdev_malloc_rpc.c (revision 2f5c602574a98ede645991abe279a96e19c50196)
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 "bdev_malloc.h"
35 #include "spdk/rpc.h"
36 #include "spdk/util.h"
37 #include "spdk/uuid.h"
38 #include "spdk/string.h"
39 #include "spdk/log.h"
40 
41 struct rpc_construct_malloc {
42 	char *name;
43 	char *uuid;
44 	uint64_t num_blocks;
45 	uint32_t block_size;
46 };
47 
48 static void
49 free_rpc_construct_malloc(struct rpc_construct_malloc *r)
50 {
51 	free(r->name);
52 	free(r->uuid);
53 }
54 
55 static const struct spdk_json_object_decoder rpc_construct_malloc_decoders[] = {
56 	{"name", offsetof(struct rpc_construct_malloc, name), spdk_json_decode_string, true},
57 	{"uuid", offsetof(struct rpc_construct_malloc, uuid), spdk_json_decode_string, true},
58 	{"num_blocks", offsetof(struct rpc_construct_malloc, num_blocks), spdk_json_decode_uint64},
59 	{"block_size", offsetof(struct rpc_construct_malloc, block_size), spdk_json_decode_uint32},
60 };
61 
62 static void
63 rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request,
64 		       const struct spdk_json_val *params)
65 {
66 	struct rpc_construct_malloc req = {NULL};
67 	struct spdk_json_write_ctx *w;
68 	struct spdk_uuid *uuid = NULL;
69 	struct spdk_uuid decoded_uuid;
70 	struct spdk_bdev *bdev;
71 	int rc = 0;
72 
73 	if (spdk_json_decode_object(params, rpc_construct_malloc_decoders,
74 				    SPDK_COUNTOF(rpc_construct_malloc_decoders),
75 				    &req)) {
76 		SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n");
77 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
78 						 "spdk_json_decode_object failed");
79 		goto cleanup;
80 	}
81 
82 	if (req.num_blocks == 0) {
83 		spdk_jsonrpc_send_error_response(request, -EINVAL,
84 						 "Disk num_blocks must be greater than 0");
85 		goto cleanup;
86 	}
87 
88 	if (req.uuid) {
89 		if (spdk_uuid_parse(&decoded_uuid, req.uuid)) {
90 			spdk_jsonrpc_send_error_response(request, -EINVAL,
91 							 "Failed to parse bdev UUID");
92 			goto cleanup;
93 		}
94 		uuid = &decoded_uuid;
95 	}
96 
97 	rc = create_malloc_disk(&bdev, req.name, uuid, req.num_blocks, req.block_size);
98 	if (rc) {
99 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
100 		goto cleanup;
101 	}
102 
103 	free_rpc_construct_malloc(&req);
104 
105 	w = spdk_jsonrpc_begin_result(request);
106 	spdk_json_write_string(w, spdk_bdev_get_name(bdev));
107 	spdk_jsonrpc_end_result(request, w);
108 	return;
109 
110 cleanup:
111 	free_rpc_construct_malloc(&req);
112 }
113 SPDK_RPC_REGISTER("bdev_malloc_create", rpc_bdev_malloc_create, SPDK_RPC_RUNTIME)
114 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_malloc_create, construct_malloc_bdev)
115 
116 struct rpc_delete_malloc {
117 	char *name;
118 };
119 
120 static void
121 free_rpc_delete_malloc(struct rpc_delete_malloc *r)
122 {
123 	free(r->name);
124 }
125 
126 static const struct spdk_json_object_decoder rpc_delete_malloc_decoders[] = {
127 	{"name", offsetof(struct rpc_delete_malloc, name), spdk_json_decode_string},
128 };
129 
130 static void
131 rpc_bdev_malloc_delete_cb(void *cb_arg, int bdeverrno)
132 {
133 	struct spdk_jsonrpc_request *request = cb_arg;
134 
135 	spdk_jsonrpc_send_bool_response(request, bdeverrno == 0);
136 }
137 
138 static void
139 rpc_bdev_malloc_delete(struct spdk_jsonrpc_request *request,
140 		       const struct spdk_json_val *params)
141 {
142 	struct rpc_delete_malloc req = {NULL};
143 	struct spdk_bdev *bdev;
144 
145 	if (spdk_json_decode_object(params, rpc_delete_malloc_decoders,
146 				    SPDK_COUNTOF(rpc_delete_malloc_decoders),
147 				    &req)) {
148 		SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n");
149 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
150 						 "spdk_json_decode_object failed");
151 		goto cleanup;
152 	}
153 
154 	bdev = spdk_bdev_get_by_name(req.name);
155 	if (bdev == NULL) {
156 		SPDK_INFOLOG(bdev_malloc, "bdev '%s' does not exist\n", req.name);
157 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
158 		goto cleanup;
159 	}
160 
161 	delete_malloc_disk(bdev, rpc_bdev_malloc_delete_cb, request);
162 
163 cleanup:
164 	free_rpc_delete_malloc(&req);
165 }
166 SPDK_RPC_REGISTER("bdev_malloc_delete", rpc_bdev_malloc_delete, SPDK_RPC_RUNTIME)
167 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_malloc_delete, delete_malloc_bdev)
168