xref: /spdk/module/bdev/compress/vbdev_compress_rpc.c (revision 03e3fc4f5835983a4e6602b4e770922e798ce263)
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_compress.h"
35  #include "spdk/rpc.h"
36  #include "spdk/util.h"
37  #include "spdk/string.h"
38  #include "spdk_internal/log.h"
39  
40  struct rpc_bdev_compress_get_orphans {
41  	char *name;
42  };
43  
44  static void
45  free_rpc_bdev_compress_get_orphans(struct rpc_bdev_compress_get_orphans *r)
46  {
47  	free(r->name);
48  }
49  
50  static const struct spdk_json_object_decoder rpc_bdev_compress_get_orphans_decoders[] = {
51  	{"name", offsetof(struct rpc_bdev_compress_get_orphans, name), spdk_json_decode_string, true},
52  };
53  
54  static void
55  rpc_bdev_compress_get_orphans(struct spdk_jsonrpc_request *request,
56  			      const struct spdk_json_val *params)
57  {
58  	struct rpc_bdev_compress_get_orphans req = {};
59  	struct spdk_json_write_ctx *w;
60  	struct vbdev_compress *comp_bdev;
61  	bool found = false;
62  
63  
64  	if (params && spdk_json_decode_object(params, rpc_bdev_compress_get_orphans_decoders,
65  					      SPDK_COUNTOF(rpc_bdev_compress_get_orphans_decoders),
66  					      &req)) {
67  		SPDK_ERRLOG("spdk_json_decode_object failed\n");
68  		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
69  						 "spdk_json_decode_object failed");
70  		free_rpc_bdev_compress_get_orphans(&req);
71  		return;
72  	}
73  
74  	if (req.name) {
75  		if (compress_has_orphan(req.name) == false) {
76  			spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
77  			free_rpc_bdev_compress_get_orphans(&req);
78  			return;
79  		}
80  		found = true;
81  	}
82  
83  	w = spdk_jsonrpc_begin_result(request);
84  	spdk_json_write_array_begin(w);
85  	if (found) {
86  		spdk_json_write_string(w, req.name);
87  	} else {
88  		for (comp_bdev = compress_bdev_first(); comp_bdev != NULL;
89  		     comp_bdev = compress_bdev_next(comp_bdev)) {
90  			if (compress_has_orphan(compress_get_name(comp_bdev))) {
91  				spdk_json_write_string(w, compress_get_name(comp_bdev));
92  			}
93  		}
94  	}
95  	spdk_json_write_array_end(w);
96  	spdk_jsonrpc_end_result(request, w);
97  	free_rpc_bdev_compress_get_orphans(&req);
98  }
99  SPDK_RPC_REGISTER("bdev_compress_get_orphans", rpc_bdev_compress_get_orphans, SPDK_RPC_RUNTIME)
100  
101  struct rpc_compress_set_pmd {
102  	enum compress_pmd pmd;
103  };
104  
105  static const struct spdk_json_object_decoder rpc_compress_pmd_decoder[] = {
106  	{"pmd", offsetof(struct rpc_compress_set_pmd, pmd), spdk_json_decode_int32},
107  };
108  
109  static void
110  rpc_compress_set_pmd(struct spdk_jsonrpc_request *request,
111  		     const struct spdk_json_val *params)
112  {
113  	struct rpc_compress_set_pmd req;
114  	struct spdk_json_write_ctx *w;
115  	int rc = 0;
116  
117  	if (spdk_json_decode_object(params, rpc_compress_pmd_decoder,
118  				    SPDK_COUNTOF(rpc_compress_pmd_decoder),
119  				    &req)) {
120  		SPDK_ERRLOG("spdk_json_decode_object failed\n");
121  		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
122  						 "spdk_json_decode_object failed");
123  		return;
124  	}
125  
126  	if (req.pmd >= COMPRESS_PMD_MAX) {
127  		spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
128  						     "PMD value %d should be less than %d", req.pmd, COMPRESS_PMD_MAX);
129  		return;
130  	}
131  
132  	rc = compress_set_pmd(&req.pmd);
133  	if (rc) {
134  		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
135  		return;
136  	}
137  
138  	w = spdk_jsonrpc_begin_result(request);
139  	if (w != NULL) {
140  		spdk_json_write_bool(w, true);
141  		spdk_jsonrpc_end_result(request, w);
142  	}
143  }
144  SPDK_RPC_REGISTER("compress_set_pmd", rpc_compress_set_pmd,
145  		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
146  SPDK_RPC_REGISTER_ALIAS_DEPRECATED(compress_set_pmd, set_compress_pmd)
147  
148  /* Structure to hold the parameters for this RPC method. */
149  struct rpc_construct_compress {
150  	char *base_bdev_name;
151  	char *pm_path;
152  };
153  
154  /* Free the allocated memory resource after the RPC handling. */
155  static void
156  free_rpc_construct_compress(struct rpc_construct_compress *r)
157  {
158  	free(r->base_bdev_name);
159  	free(r->pm_path);
160  }
161  
162  /* Structure to decode the input parameters for this RPC method. */
163  static const struct spdk_json_object_decoder rpc_construct_compress_decoders[] = {
164  	{"base_bdev_name", offsetof(struct rpc_construct_compress, base_bdev_name), spdk_json_decode_string},
165  	{"pm_path", offsetof(struct rpc_construct_compress, pm_path), spdk_json_decode_string},
166  };
167  
168  /* Decode the parameters for this RPC method and properly construct the compress
169   * device. Error status returned in the failed cases.
170   */
171  static void
172  rpc_bdev_compress_create(struct spdk_jsonrpc_request *request,
173  			 const struct spdk_json_val *params)
174  {
175  	struct rpc_construct_compress req = {NULL};
176  	struct spdk_json_write_ctx *w;
177  	char *name;
178  	int rc;
179  
180  	if (spdk_json_decode_object(params, rpc_construct_compress_decoders,
181  				    SPDK_COUNTOF(rpc_construct_compress_decoders),
182  				    &req)) {
183  		SPDK_DEBUGLOG(SPDK_LOG_VBDEV_COMPRESS, "spdk_json_decode_object failed\n");
184  		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
185  						 "spdk_json_decode_object failed");
186  		goto cleanup;
187  	}
188  
189  	rc = create_compress_bdev(req.base_bdev_name, req.pm_path);
190  	if (rc != 0) {
191  		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
192  		goto cleanup;
193  	}
194  
195  	w = spdk_jsonrpc_begin_result(request);
196  	name = spdk_sprintf_alloc("COMP_%s", req.base_bdev_name);
197  	spdk_json_write_string(w, name);
198  	spdk_jsonrpc_end_result(request, w);
199  	free(name);
200  
201  cleanup:
202  	free_rpc_construct_compress(&req);
203  }
204  SPDK_RPC_REGISTER("bdev_compress_create", rpc_bdev_compress_create, SPDK_RPC_RUNTIME)
205  SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_compress_create, construct_compress_bdev)
206  
207  struct rpc_delete_compress {
208  	char *name;
209  };
210  
211  static void
212  free_rpc_delete_compress(struct rpc_delete_compress *req)
213  {
214  	free(req->name);
215  }
216  
217  static const struct spdk_json_object_decoder rpc_delete_compress_decoders[] = {
218  	{"name", offsetof(struct rpc_delete_compress, name), spdk_json_decode_string},
219  };
220  
221  static void
222  _rpc_bdev_compress_delete_cb(void *cb_arg, int bdeverrno)
223  {
224  	struct spdk_jsonrpc_request *request = cb_arg;
225  	struct spdk_json_write_ctx *w;
226  
227  	w = spdk_jsonrpc_begin_result(request);
228  	spdk_json_write_bool(w, bdeverrno == 0);
229  	spdk_jsonrpc_end_result(request, w);
230  }
231  
232  static void
233  rpc_bdev_compress_delete(struct spdk_jsonrpc_request *request,
234  			 const struct spdk_json_val *params)
235  {
236  	struct rpc_delete_compress req = {NULL};
237  
238  	if (spdk_json_decode_object(params, rpc_delete_compress_decoders,
239  				    SPDK_COUNTOF(rpc_delete_compress_decoders),
240  				    &req)) {
241  		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
242  						 "spdk_json_decode_object failed");
243  	} else {
244  		bdev_compress_delete(req.name, _rpc_bdev_compress_delete_cb, request);
245  	}
246  
247  	free_rpc_delete_compress(&req);
248  }
249  SPDK_RPC_REGISTER("bdev_compress_delete", rpc_bdev_compress_delete, SPDK_RPC_RUNTIME)
250  SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_compress_delete, delete_compress_bdev)
251