xref: /spdk/lib/bdev/bdev_rpc.c (revision 712a3f69d32632bf6c862f00200f7f437d3f7529)
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 };
46 
47 static const struct spdk_json_object_decoder rpc_set_bdev_opts_decoders[] = {
48 	{"bdev_io_pool_size", offsetof(struct spdk_rpc_set_bdev_opts, bdev_io_pool_size), spdk_json_decode_uint32, true},
49 	{"bdev_io_cache_size", offsetof(struct spdk_rpc_set_bdev_opts, bdev_io_cache_size), spdk_json_decode_uint32, true},
50 };
51 
52 static void
53 spdk_rpc_bdev_set_options(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
54 {
55 	struct spdk_rpc_set_bdev_opts rpc_opts;
56 	struct spdk_bdev_opts bdev_opts;
57 	struct spdk_json_write_ctx *w;
58 	int rc;
59 
60 	rpc_opts.bdev_io_pool_size = UINT32_MAX;
61 	rpc_opts.bdev_io_cache_size = UINT32_MAX;
62 
63 	if (params != NULL) {
64 		if (spdk_json_decode_object(params, rpc_set_bdev_opts_decoders,
65 					    SPDK_COUNTOF(rpc_set_bdev_opts_decoders), &rpc_opts)) {
66 			SPDK_ERRLOG("spdk_json_decode_object() failed\n");
67 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
68 							 "Invalid parameters");
69 			return;
70 		}
71 	}
72 
73 	spdk_bdev_get_opts(&bdev_opts);
74 	if (rpc_opts.bdev_io_pool_size != UINT32_MAX) {
75 		bdev_opts.bdev_io_pool_size = rpc_opts.bdev_io_pool_size;
76 	}
77 	if (rpc_opts.bdev_io_cache_size != UINT32_MAX) {
78 		bdev_opts.bdev_io_cache_size = rpc_opts.bdev_io_cache_size;
79 	}
80 	rc = spdk_bdev_set_opts(&bdev_opts);
81 
82 	if (rc != 0) {
83 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
84 						     "Pool size %" PRIu32 " too small for cache size %" PRIu32,
85 						     bdev_opts.bdev_io_pool_size, bdev_opts.bdev_io_cache_size);
86 		return;
87 	}
88 
89 	w = spdk_jsonrpc_begin_result(request);
90 	spdk_json_write_bool(w, true);
91 	spdk_jsonrpc_end_result(request, w);
92 }
93 SPDK_RPC_REGISTER("bdev_set_options", spdk_rpc_bdev_set_options, SPDK_RPC_STARTUP)
94 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_set_options, set_bdev_options)
95