xref: /spdk/module/bdev/passthru/vbdev_passthru_rpc.c (revision 31513614a7e064bb0cda0667ce3916f4140d68e2)
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 "vbdev_passthru.h"
35 #include "spdk/rpc.h"
36 #include "spdk/util.h"
37 #include "spdk/string.h"
38 #include "spdk/log.h"
39 
40 /* Structure to hold the parameters for this RPC method. */
41 struct rpc_bdev_passthru_create {
42 	char *base_bdev_name;
43 	char *name;
44 };
45 
46 /* Free the allocated memory resource after the RPC handling. */
47 static void
48 free_rpc_bdev_passthru_create(struct rpc_bdev_passthru_create *r)
49 {
50 	free(r->base_bdev_name);
51 	free(r->name);
52 }
53 
54 /* Structure to decode the input parameters for this RPC method. */
55 static const struct spdk_json_object_decoder rpc_bdev_passthru_create_decoders[] = {
56 	{"base_bdev_name", offsetof(struct rpc_bdev_passthru_create, base_bdev_name), spdk_json_decode_string},
57 	{"name", offsetof(struct rpc_bdev_passthru_create, name), spdk_json_decode_string},
58 };
59 
60 /* Decode the parameters for this RPC method and properly construct the passthru
61  * device. Error status returned in the failed cases.
62  */
63 static void
64 rpc_bdev_passthru_create(struct spdk_jsonrpc_request *request,
65 			 const struct spdk_json_val *params)
66 {
67 	struct rpc_bdev_passthru_create req = {NULL};
68 	struct spdk_json_write_ctx *w;
69 	int rc;
70 
71 	if (spdk_json_decode_object(params, rpc_bdev_passthru_create_decoders,
72 				    SPDK_COUNTOF(rpc_bdev_passthru_create_decoders),
73 				    &req)) {
74 		SPDK_DEBUGLOG(vbdev_passthru, "spdk_json_decode_object failed\n");
75 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
76 						 "spdk_json_decode_object failed");
77 		goto cleanup;
78 	}
79 
80 	rc = bdev_passthru_create_disk(req.base_bdev_name, req.name);
81 	if (rc != 0) {
82 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
83 		goto cleanup;
84 	}
85 
86 	w = spdk_jsonrpc_begin_result(request);
87 	spdk_json_write_string(w, req.name);
88 	spdk_jsonrpc_end_result(request, w);
89 
90 cleanup:
91 	free_rpc_bdev_passthru_create(&req);
92 }
93 SPDK_RPC_REGISTER("bdev_passthru_create", rpc_bdev_passthru_create, SPDK_RPC_RUNTIME)
94 
95 struct rpc_bdev_passthru_delete {
96 	char *name;
97 };
98 
99 static void
100 free_rpc_bdev_passthru_delete(struct rpc_bdev_passthru_delete *req)
101 {
102 	free(req->name);
103 }
104 
105 static const struct spdk_json_object_decoder rpc_bdev_passthru_delete_decoders[] = {
106 	{"name", offsetof(struct rpc_bdev_passthru_delete, name), spdk_json_decode_string},
107 };
108 
109 static void
110 rpc_bdev_passthru_delete_cb(void *cb_arg, int bdeverrno)
111 {
112 	struct spdk_jsonrpc_request *request = cb_arg;
113 
114 	if (bdeverrno == 0) {
115 		spdk_jsonrpc_send_bool_response(request, true);
116 	} else {
117 		spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
118 	}
119 }
120 
121 static void
122 rpc_bdev_passthru_delete(struct spdk_jsonrpc_request *request,
123 			 const struct spdk_json_val *params)
124 {
125 	struct rpc_bdev_passthru_delete req = {NULL};
126 
127 	if (spdk_json_decode_object(params, rpc_bdev_passthru_delete_decoders,
128 				    SPDK_COUNTOF(rpc_bdev_passthru_delete_decoders),
129 				    &req)) {
130 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
131 						 "spdk_json_decode_object failed");
132 		goto cleanup;
133 	}
134 
135 	bdev_passthru_delete_disk(req.name, rpc_bdev_passthru_delete_cb, request);
136 
137 cleanup:
138 	free_rpc_bdev_passthru_delete(&req);
139 }
140 SPDK_RPC_REGISTER("bdev_passthru_delete", rpc_bdev_passthru_delete, SPDK_RPC_RUNTIME)
141