xref: /spdk/module/bdev/nvme/bdev_nvme_cuse_rpc.c (revision c41c4c2babbc68d778e7f8bd055cfd18ca5c7209)
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/stdinc.h"
35 
36 #include "bdev_nvme.h"
37 
38 #include "spdk/string.h"
39 #include "spdk/rpc.h"
40 #include "spdk/util.h"
41 #include "spdk/nvme.h"
42 
43 #include "spdk_internal/log.h"
44 
45 struct rpc_nvme_cuse_register {
46 	char *name;
47 };
48 
49 static void
50 free_rpc_nvme_cuse_register(struct rpc_nvme_cuse_register *req)
51 {
52 	free(req->name);
53 }
54 
55 static const struct spdk_json_object_decoder rpc_nvme_cuse_register_decoders[] = {
56 	{"name", offsetof(struct rpc_nvme_cuse_register, name), spdk_json_decode_string},
57 };
58 
59 static void
60 spdk_rpc_nvme_cuse_register(struct spdk_jsonrpc_request *request,
61 			    const struct spdk_json_val *params)
62 {
63 	struct rpc_nvme_cuse_register req = {};
64 	struct spdk_json_write_ctx *w;
65 	struct nvme_bdev_ctrlr *bdev_ctrlr = NULL;
66 	int rc;
67 
68 	if (spdk_json_decode_object(params, rpc_nvme_cuse_register_decoders,
69 				    SPDK_COUNTOF(rpc_nvme_cuse_register_decoders),
70 				    &req)) {
71 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
72 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
73 						 "spdk_json_decode_object failed");
74 		goto cleanup;
75 	}
76 
77 	bdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name);
78 	if (!bdev_ctrlr) {
79 		SPDK_ERRLOG("No such controller\n");
80 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
81 		goto cleanup;
82 	}
83 
84 	rc = spdk_nvme_cuse_register(bdev_ctrlr->ctrlr);
85 	if (rc) {
86 		SPDK_ERRLOG("Failed to register CUSE devices: %s\n", spdk_strerror(-rc));
87 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
88 		goto cleanup;
89 	}
90 
91 	w = spdk_jsonrpc_begin_result(request);
92 	spdk_json_write_bool(w, true);
93 	spdk_jsonrpc_end_result(request, w);
94 
95 cleanup:
96 	free_rpc_nvme_cuse_register(&req);
97 }
98 SPDK_RPC_REGISTER("bdev_nvme_cuse_register", spdk_rpc_nvme_cuse_register, SPDK_RPC_RUNTIME)
99 
100 struct rpc_nvme_cuse_unregister {
101 	char *name;
102 };
103 
104 static void
105 free_rpc_nvme_cuse_unregister(struct rpc_nvme_cuse_unregister *req)
106 {
107 	free(req->name);
108 }
109 
110 static const struct spdk_json_object_decoder rpc_nvme_cuse_unregister_decoders[] = {
111 	{"name", offsetof(struct rpc_nvme_cuse_unregister, name), spdk_json_decode_string, true},
112 };
113 
114 static void
115 spdk_rpc_nvme_cuse_unregister(struct spdk_jsonrpc_request *request,
116 			      const struct spdk_json_val *params)
117 {
118 	struct rpc_nvme_cuse_unregister req = {};
119 	struct spdk_json_write_ctx *w;
120 	struct nvme_bdev_ctrlr *bdev_ctrlr = NULL;
121 	int rc;
122 
123 	if (spdk_json_decode_object(params, rpc_nvme_cuse_unregister_decoders,
124 				    SPDK_COUNTOF(rpc_nvme_cuse_unregister_decoders),
125 				    &req)) {
126 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
127 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
128 						 "spdk_json_decode_object failed");
129 		goto cleanup;
130 	}
131 
132 	bdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name);
133 	if (!bdev_ctrlr) {
134 		SPDK_ERRLOG("No such controller\n");
135 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
136 		goto cleanup;
137 	}
138 
139 	rc = spdk_nvme_cuse_unregister(bdev_ctrlr->ctrlr);
140 	if (rc) {
141 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
142 		goto cleanup;
143 	}
144 
145 	w = spdk_jsonrpc_begin_result(request);
146 	spdk_json_write_bool(w, true);
147 	spdk_jsonrpc_end_result(request, w);
148 
149 cleanup:
150 	free_rpc_nvme_cuse_unregister(&req);
151 }
152 SPDK_RPC_REGISTER("bdev_nvme_cuse_unregister", spdk_rpc_nvme_cuse_unregister, SPDK_RPC_RUNTIME)
153