xref: /spdk/lib/bdev/bdev_rpc.c (revision 0ed85362c8132a2d1927757fbcade66b6660d26a)
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/bdev.h"
35 
36 #include "spdk/rpc.h"
37 #include "spdk/util.h"
38 #include "spdk/string.h"
39 
40 #include "spdk_internal/log.h"
41 
42 struct spdk_rpc_set_bdev_opts {
43 	uint32_t bdev_io_pool_size;
44 	uint32_t bdev_io_cache_size;
45 	bool bdev_auto_examine;
46 };
47 
48 static const struct spdk_json_object_decoder rpc_set_bdev_opts_decoders[] = {
49 	{"bdev_io_pool_size", offsetof(struct spdk_rpc_set_bdev_opts, bdev_io_pool_size), spdk_json_decode_uint32, true},
50 	{"bdev_io_cache_size", offsetof(struct spdk_rpc_set_bdev_opts, bdev_io_cache_size), spdk_json_decode_uint32, true},
51 	{"bdev_auto_examine", offsetof(struct spdk_rpc_set_bdev_opts, bdev_auto_examine), spdk_json_decode_bool, true},
52 };
53 
54 static void
55 rpc_bdev_set_options(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
56 {
57 	struct spdk_rpc_set_bdev_opts rpc_opts;
58 	struct spdk_bdev_opts bdev_opts;
59 	struct spdk_json_write_ctx *w;
60 	int rc;
61 
62 	rpc_opts.bdev_io_pool_size = UINT32_MAX;
63 	rpc_opts.bdev_io_cache_size = UINT32_MAX;
64 	rpc_opts.bdev_auto_examine = true;
65 
66 	if (params != NULL) {
67 		if (spdk_json_decode_object(params, rpc_set_bdev_opts_decoders,
68 					    SPDK_COUNTOF(rpc_set_bdev_opts_decoders), &rpc_opts)) {
69 			SPDK_ERRLOG("spdk_json_decode_object() failed\n");
70 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
71 							 "Invalid parameters");
72 			return;
73 		}
74 	}
75 
76 	spdk_bdev_get_opts(&bdev_opts);
77 	if (rpc_opts.bdev_io_pool_size != UINT32_MAX) {
78 		bdev_opts.bdev_io_pool_size = rpc_opts.bdev_io_pool_size;
79 	}
80 	if (rpc_opts.bdev_io_cache_size != UINT32_MAX) {
81 		bdev_opts.bdev_io_cache_size = rpc_opts.bdev_io_cache_size;
82 	}
83 	bdev_opts.bdev_auto_examine = rpc_opts.bdev_auto_examine;
84 	rc = spdk_bdev_set_opts(&bdev_opts);
85 
86 	if (rc != 0) {
87 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
88 						     "Pool size %" PRIu32 " too small for cache size %" PRIu32,
89 						     bdev_opts.bdev_io_pool_size, bdev_opts.bdev_io_cache_size);
90 		return;
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 SPDK_RPC_REGISTER("bdev_set_options", rpc_bdev_set_options, SPDK_RPC_STARTUP)
98 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_set_options, set_bdev_options)
99 
100 struct rpc_bdev_examine {
101 	char *name;
102 };
103 
104 static void
105 free_rpc_bdev_examine(struct rpc_bdev_examine *r)
106 {
107 	free(r->name);
108 }
109 
110 static const struct spdk_json_object_decoder rpc_examine_bdev_decoders[] = {
111 	{"name", offsetof(struct rpc_bdev_examine, name), spdk_json_decode_string},
112 };
113 
114 static void
115 rpc_bdev_examine_bdev(struct spdk_jsonrpc_request *request,
116 		      const struct spdk_json_val *params)
117 {
118 	struct rpc_bdev_examine req = {NULL};
119 	struct spdk_json_write_ctx *w;
120 	int rc;
121 
122 	if (spdk_json_decode_object(params, rpc_examine_bdev_decoders,
123 				    SPDK_COUNTOF(rpc_examine_bdev_decoders),
124 				    &req)) {
125 		SPDK_ERRLOG("spdk_json_decode_object() failed\n");
126 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
127 						 "spdk_json_decode_object failed");
128 		goto cleanup;
129 	}
130 
131 	rc = spdk_bdev_examine(req.name);
132 	if (rc != 0) {
133 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
134 		goto cleanup;
135 	}
136 	w = spdk_jsonrpc_begin_result(request);
137 	spdk_json_write_bool(w, true);
138 	spdk_jsonrpc_end_result(request, w);
139 
140 cleanup:
141 	free_rpc_bdev_examine(&req);
142 }
143 SPDK_RPC_REGISTER("bdev_examine", rpc_bdev_examine_bdev, SPDK_RPC_RUNTIME)
144