xref: /spdk/module/bdev/null/bdev_null_rpc.c (revision 1521bf3be7e150730ea8c3877e4410aaafc57f97)
1488570ebSJim Harris /*   SPDX-License-Identifier: BSD-3-Clause
2a6dbe372Spaul luse  *   Copyright (C) 2017 Intel Corporation. All rights reserved.
307fe6a43SSeth Howell  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
407fe6a43SSeth Howell  */
507fe6a43SSeth Howell 
607fe6a43SSeth Howell #include "spdk/rpc.h"
707fe6a43SSeth Howell #include "spdk/util.h"
807fe6a43SSeth Howell #include "spdk/string.h"
907fe6a43SSeth Howell #include "spdk/bdev_module.h"
104e8e97c8STomasz Zawadzki #include "spdk/log.h"
1107fe6a43SSeth Howell 
1207fe6a43SSeth Howell #include "bdev_null.h"
1307fe6a43SSeth Howell 
1407fe6a43SSeth Howell static void
1536943038SShuhei Matsumoto free_rpc_construct_null(struct null_bdev_opts *req)
1607fe6a43SSeth Howell {
1707fe6a43SSeth Howell 	free(req->name);
1807fe6a43SSeth Howell }
1907fe6a43SSeth Howell 
2007fe6a43SSeth Howell static const struct spdk_json_object_decoder rpc_construct_null_decoders[] = {
2136943038SShuhei Matsumoto 	{"name", offsetof(struct null_bdev_opts, name), spdk_json_decode_string},
2236943038SShuhei Matsumoto 	{"uuid", offsetof(struct null_bdev_opts, uuid), spdk_json_decode_uuid, true},
2336943038SShuhei Matsumoto 	{"num_blocks", offsetof(struct null_bdev_opts, num_blocks), spdk_json_decode_uint64},
2436943038SShuhei Matsumoto 	{"block_size", offsetof(struct null_bdev_opts, block_size), spdk_json_decode_uint32},
2536943038SShuhei Matsumoto 	{"physical_block_size", offsetof(struct null_bdev_opts, physical_block_size), spdk_json_decode_uint32, true},
2636943038SShuhei Matsumoto 	{"md_size", offsetof(struct null_bdev_opts, md_size), spdk_json_decode_uint32, true},
2736943038SShuhei Matsumoto 	{"dif_type", offsetof(struct null_bdev_opts, dif_type), spdk_json_decode_int32, true},
2836943038SShuhei Matsumoto 	{"dif_is_head_of_md", offsetof(struct null_bdev_opts, dif_is_head_of_md), spdk_json_decode_bool, true},
29*1521bf3bSShuhei Matsumoto 	{"dif_pi_format", offsetof(struct null_bdev_opts, dif_pi_format), spdk_json_decode_uint32, true},
3007fe6a43SSeth Howell };
3107fe6a43SSeth Howell 
3207fe6a43SSeth Howell static void
33d4f4fd45SSeth Howell rpc_bdev_null_create(struct spdk_jsonrpc_request *request,
3407fe6a43SSeth Howell 		     const struct spdk_json_val *params)
3507fe6a43SSeth Howell {
3636943038SShuhei Matsumoto 	struct null_bdev_opts req = {};
3707fe6a43SSeth Howell 	struct spdk_json_write_ctx *w;
3807fe6a43SSeth Howell 	struct spdk_bdev *bdev;
3907fe6a43SSeth Howell 	int rc = 0;
4007fe6a43SSeth Howell 
4107fe6a43SSeth Howell 	if (spdk_json_decode_object(params, rpc_construct_null_decoders,
4207fe6a43SSeth Howell 				    SPDK_COUNTOF(rpc_construct_null_decoders),
4307fe6a43SSeth Howell 				    &req)) {
442172c432STomasz Zawadzki 		SPDK_DEBUGLOG(bdev_null, "spdk_json_decode_object failed\n");
4507fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
4607fe6a43SSeth Howell 						 "spdk_json_decode_object failed");
4707fe6a43SSeth Howell 		goto cleanup;
4807fe6a43SSeth Howell 	}
4907fe6a43SSeth Howell 
5036943038SShuhei Matsumoto 	rc = bdev_null_create(&bdev, &req);
5107fe6a43SSeth Howell 	if (rc) {
5207fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
5307fe6a43SSeth Howell 		goto cleanup;
5407fe6a43SSeth Howell 	}
5507fe6a43SSeth Howell 
5607fe6a43SSeth Howell 	w = spdk_jsonrpc_begin_result(request);
5707fe6a43SSeth Howell 	spdk_json_write_string(w, bdev->name);
5807fe6a43SSeth Howell 	spdk_jsonrpc_end_result(request, w);
5907fe6a43SSeth Howell 	free_rpc_construct_null(&req);
6007fe6a43SSeth Howell 	return;
6107fe6a43SSeth Howell 
6207fe6a43SSeth Howell cleanup:
6307fe6a43SSeth Howell 	free_rpc_construct_null(&req);
6407fe6a43SSeth Howell }
65d4f4fd45SSeth Howell SPDK_RPC_REGISTER("bdev_null_create", rpc_bdev_null_create, SPDK_RPC_RUNTIME)
6607fe6a43SSeth Howell 
6707fe6a43SSeth Howell struct rpc_delete_null {
6807fe6a43SSeth Howell 	char *name;
6907fe6a43SSeth Howell };
7007fe6a43SSeth Howell 
7107fe6a43SSeth Howell static void
7207fe6a43SSeth Howell free_rpc_delete_null(struct rpc_delete_null *req)
7307fe6a43SSeth Howell {
7407fe6a43SSeth Howell 	free(req->name);
7507fe6a43SSeth Howell }
7607fe6a43SSeth Howell 
7707fe6a43SSeth Howell static const struct spdk_json_object_decoder rpc_delete_null_decoders[] = {
7807fe6a43SSeth Howell 	{"name", offsetof(struct rpc_delete_null, name), spdk_json_decode_string},
7907fe6a43SSeth Howell };
8007fe6a43SSeth Howell 
8107fe6a43SSeth Howell static void
82d4f4fd45SSeth Howell rpc_bdev_null_delete_cb(void *cb_arg, int bdeverrno)
8307fe6a43SSeth Howell {
8407fe6a43SSeth Howell 	struct spdk_jsonrpc_request *request = cb_arg;
8507fe6a43SSeth Howell 
86d3e394aeSShuhei Matsumoto 	if (bdeverrno == 0) {
87d3e394aeSShuhei Matsumoto 		spdk_jsonrpc_send_bool_response(request, true);
88d3e394aeSShuhei Matsumoto 	} else {
89d3e394aeSShuhei Matsumoto 		spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
90d3e394aeSShuhei Matsumoto 	}
9107fe6a43SSeth Howell }
9207fe6a43SSeth Howell 
9307fe6a43SSeth Howell static void
94d4f4fd45SSeth Howell rpc_bdev_null_delete(struct spdk_jsonrpc_request *request,
9507fe6a43SSeth Howell 		     const struct spdk_json_val *params)
9607fe6a43SSeth Howell {
9707fe6a43SSeth Howell 	struct rpc_delete_null req = {NULL};
9807fe6a43SSeth Howell 
9907fe6a43SSeth Howell 	if (spdk_json_decode_object(params, rpc_delete_null_decoders,
10007fe6a43SSeth Howell 				    SPDK_COUNTOF(rpc_delete_null_decoders),
10107fe6a43SSeth Howell 				    &req)) {
10207fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
10307fe6a43SSeth Howell 						 "spdk_json_decode_object failed");
10407fe6a43SSeth Howell 		goto cleanup;
10507fe6a43SSeth Howell 	}
10607fe6a43SSeth Howell 
1074573e4ccSShuhei Matsumoto 	bdev_null_delete(req.name, rpc_bdev_null_delete_cb, request);
10807fe6a43SSeth Howell 
10907fe6a43SSeth Howell 	free_rpc_delete_null(&req);
11007fe6a43SSeth Howell 
11107fe6a43SSeth Howell 	return;
11207fe6a43SSeth Howell 
11307fe6a43SSeth Howell cleanup:
11407fe6a43SSeth Howell 	free_rpc_delete_null(&req);
11507fe6a43SSeth Howell }
116d4f4fd45SSeth Howell SPDK_RPC_REGISTER("bdev_null_delete", rpc_bdev_null_delete, SPDK_RPC_RUNTIME)
117d0657f32SJin Yu 
118d0657f32SJin Yu struct rpc_bdev_null_resize {
119d0657f32SJin Yu 	char *name;
120d0657f32SJin Yu 	uint64_t new_size;
121d0657f32SJin Yu };
122d0657f32SJin Yu 
123d0657f32SJin Yu static const struct spdk_json_object_decoder rpc_bdev_null_resize_decoders[] = {
124d0657f32SJin Yu 	{"name", offsetof(struct rpc_bdev_null_resize, name), spdk_json_decode_string},
125d0657f32SJin Yu 	{"new_size", offsetof(struct rpc_bdev_null_resize, new_size), spdk_json_decode_uint64}
126d0657f32SJin Yu };
127d0657f32SJin Yu 
128d0657f32SJin Yu static void
129d0657f32SJin Yu free_rpc_bdev_null_resize(struct rpc_bdev_null_resize *req)
130d0657f32SJin Yu {
131d0657f32SJin Yu 	free(req->name);
132d0657f32SJin Yu }
133d0657f32SJin Yu 
134d0657f32SJin Yu static void
135d0657f32SJin Yu spdk_rpc_bdev_null_resize(struct spdk_jsonrpc_request *request,
136d0657f32SJin Yu 			  const struct spdk_json_val *params)
137d0657f32SJin Yu {
138d0657f32SJin Yu 	struct rpc_bdev_null_resize req = {};
139d0657f32SJin Yu 	int rc;
140d0657f32SJin Yu 
141d0657f32SJin Yu 	if (spdk_json_decode_object(params, rpc_bdev_null_resize_decoders,
142d0657f32SJin Yu 				    SPDK_COUNTOF(rpc_bdev_null_resize_decoders),
143d0657f32SJin Yu 				    &req)) {
144d0657f32SJin Yu 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
145d0657f32SJin Yu 						 "spdk_json_decode_object failed");
146d0657f32SJin Yu 		goto cleanup;
147d0657f32SJin Yu 	}
148d0657f32SJin Yu 
14902e3c62cSShuhei Matsumoto 	rc = bdev_null_resize(req.name, req.new_size);
150d0657f32SJin Yu 	if (rc) {
151d0657f32SJin Yu 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
152d0657f32SJin Yu 		goto cleanup;
153d0657f32SJin Yu 	}
154d0657f32SJin Yu 
155d0657f32SJin Yu 	spdk_jsonrpc_send_bool_response(request, true);
156d0657f32SJin Yu cleanup:
157d0657f32SJin Yu 	free_rpc_bdev_null_resize(&req);
158d0657f32SJin Yu }
159d0657f32SJin Yu SPDK_RPC_REGISTER("bdev_null_resize", spdk_rpc_bdev_null_resize, SPDK_RPC_RUNTIME)
160