xref: /spdk/module/bdev/passthru/vbdev_passthru_rpc.c (revision 9889ab2dc80e40dae92dcef361d53dcba722043d)
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_internal/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 spdk_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(SPDK_LOG_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", spdk_rpc_bdev_passthru_create, SPDK_RPC_RUNTIME)
94 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_passthru_create, construct_passthru_bdev)
95 
96 struct rpc_bdev_passthru_delete {
97 	char *name;
98 };
99 
100 static void
101 free_rpc_bdev_passthru_delete(struct rpc_bdev_passthru_delete *req)
102 {
103 	free(req->name);
104 }
105 
106 static const struct spdk_json_object_decoder rpc_bdev_passthru_delete_decoders[] = {
107 	{"name", offsetof(struct rpc_bdev_passthru_delete, name), spdk_json_decode_string},
108 };
109 
110 static void
111 _spdk_rpc_bdev_passthru_delete_cb(void *cb_arg, int bdeverrno)
112 {
113 	struct spdk_jsonrpc_request *request = cb_arg;
114 	struct spdk_json_write_ctx *w;
115 
116 	w = spdk_jsonrpc_begin_result(request);
117 	spdk_json_write_bool(w, bdeverrno == 0);
118 	spdk_jsonrpc_end_result(request, w);
119 }
120 
121 static void
122 spdk_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 	struct spdk_bdev *bdev;
127 
128 	if (spdk_json_decode_object(params, rpc_bdev_passthru_delete_decoders,
129 				    SPDK_COUNTOF(rpc_bdev_passthru_delete_decoders),
130 				    &req)) {
131 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
132 						 "spdk_json_decode_object failed");
133 		goto cleanup;
134 	}
135 
136 	bdev = spdk_bdev_get_by_name(req.name);
137 	if (bdev == NULL) {
138 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
139 		goto cleanup;
140 	}
141 
142 	bdev_passthru_delete_disk(bdev, _spdk_rpc_bdev_passthru_delete_cb, request);
143 
144 cleanup:
145 	free_rpc_bdev_passthru_delete(&req);
146 }
147 SPDK_RPC_REGISTER("bdev_passthru_delete", spdk_rpc_bdev_passthru_delete, SPDK_RPC_RUNTIME)
148 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_passthru_delete, delete_passthru_bdev)
149