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