xref: /spdk/module/fsdev/aio/fsdev_aio_rpc.c (revision 92108e0a2be7a969e8ee761a776a1ea64465759a)
1e21c39aaSAnton Nayshtut /*   SPDX-License-Identifier: BSD-3-Clause
2e21c39aaSAnton Nayshtut  *   Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3e21c39aaSAnton Nayshtut  */
4e21c39aaSAnton Nayshtut 
5e21c39aaSAnton Nayshtut #include "spdk/stdinc.h"
6e21c39aaSAnton Nayshtut #include "spdk/log.h"
7e21c39aaSAnton Nayshtut #include "spdk/string.h"
8e21c39aaSAnton Nayshtut #include "spdk/rpc.h"
9e21c39aaSAnton Nayshtut #include "spdk/util.h"
10e21c39aaSAnton Nayshtut #include "fsdev_aio.h"
11e21c39aaSAnton Nayshtut 
12e21c39aaSAnton Nayshtut struct rpc_aio_create {
13e21c39aaSAnton Nayshtut 	char *name;
14e21c39aaSAnton Nayshtut 	char *root_path;
15e21c39aaSAnton Nayshtut 	struct spdk_fsdev_aio_opts opts;
16e21c39aaSAnton Nayshtut };
17e21c39aaSAnton Nayshtut 
18e21c39aaSAnton Nayshtut static void
19e21c39aaSAnton Nayshtut free_rpc_aio_create(struct rpc_aio_create *req)
20e21c39aaSAnton Nayshtut {
21e21c39aaSAnton Nayshtut 	free(req->name);
22e21c39aaSAnton Nayshtut 	free(req->root_path);
23e21c39aaSAnton Nayshtut }
24e21c39aaSAnton Nayshtut 
25e21c39aaSAnton Nayshtut static const struct spdk_json_object_decoder rpc_aio_create_decoders[] = {
26e21c39aaSAnton Nayshtut 	{"name", offsetof(struct rpc_aio_create, name), spdk_json_decode_string},
27e21c39aaSAnton Nayshtut 	{"root_path", offsetof(struct rpc_aio_create, root_path), spdk_json_decode_string},
28e21c39aaSAnton Nayshtut 	{"enable_xattr", offsetof(struct rpc_aio_create, opts.xattr_enabled), spdk_json_decode_bool, true},
29e21c39aaSAnton Nayshtut 	{"enable_writeback_cache", offsetof(struct rpc_aio_create, opts.writeback_cache_enabled), spdk_json_decode_bool, true},
30e21c39aaSAnton Nayshtut 	{"max_write", offsetof(struct rpc_aio_create, opts.max_write), spdk_json_decode_uint32, true},
31*92108e0aSYoray Zack 	{"skip_rw", offsetof(struct rpc_aio_create, opts.skip_rw), spdk_json_decode_bool, true},
32e21c39aaSAnton Nayshtut };
33e21c39aaSAnton Nayshtut 
34e21c39aaSAnton Nayshtut static void
35e21c39aaSAnton Nayshtut rpc_aio_create(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
36e21c39aaSAnton Nayshtut {
37e21c39aaSAnton Nayshtut 	struct rpc_aio_create req = {};
38e21c39aaSAnton Nayshtut 	struct spdk_json_write_ctx *w;
39e21c39aaSAnton Nayshtut 	struct spdk_fsdev *fsdev;
40e21c39aaSAnton Nayshtut 	int rc;
41e21c39aaSAnton Nayshtut 
42e21c39aaSAnton Nayshtut 	spdk_fsdev_aio_get_default_opts(&req.opts);
43e21c39aaSAnton Nayshtut 
44e21c39aaSAnton Nayshtut 	if (spdk_json_decode_object(params, rpc_aio_create_decoders,
45e21c39aaSAnton Nayshtut 				    SPDK_COUNTOF(rpc_aio_create_decoders),
46e21c39aaSAnton Nayshtut 				    &req)) {
47e21c39aaSAnton Nayshtut 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
48e21c39aaSAnton Nayshtut 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
49e21c39aaSAnton Nayshtut 						 "spdk_json_decode_object failed");
50e21c39aaSAnton Nayshtut 
51e21c39aaSAnton Nayshtut 		free_rpc_aio_create(&req);
52e21c39aaSAnton Nayshtut 		return;
53e21c39aaSAnton Nayshtut 	}
54e21c39aaSAnton Nayshtut 
55e21c39aaSAnton Nayshtut 	rc = spdk_fsdev_aio_create(&fsdev, req.name, req.root_path, &req.opts);
56e21c39aaSAnton Nayshtut 	if (rc) {
57e21c39aaSAnton Nayshtut 		SPDK_ERRLOG("Failed to create aio %s: rc %d\n", req.name, rc);
58e21c39aaSAnton Nayshtut 		spdk_jsonrpc_send_error_response(request,
59e21c39aaSAnton Nayshtut 						 SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
60e21c39aaSAnton Nayshtut 						 spdk_strerror(-rc));
61e21c39aaSAnton Nayshtut 		free_rpc_aio_create(&req);
62e21c39aaSAnton Nayshtut 		return;
63e21c39aaSAnton Nayshtut 	}
64e21c39aaSAnton Nayshtut 
65e21c39aaSAnton Nayshtut 	w = spdk_jsonrpc_begin_result(request);
66e21c39aaSAnton Nayshtut 	spdk_json_write_string(w, fsdev->name);
67e21c39aaSAnton Nayshtut 	spdk_jsonrpc_end_result(request, w);
68e21c39aaSAnton Nayshtut 	free_rpc_aio_create(&req);
69e21c39aaSAnton Nayshtut }
70e21c39aaSAnton Nayshtut SPDK_RPC_REGISTER("fsdev_aio_create", rpc_aio_create, SPDK_RPC_RUNTIME)
71e21c39aaSAnton Nayshtut 
72e21c39aaSAnton Nayshtut struct rpc_aio_delete {
73e21c39aaSAnton Nayshtut 	char *name;
74e21c39aaSAnton Nayshtut };
75e21c39aaSAnton Nayshtut 
76e21c39aaSAnton Nayshtut static const struct spdk_json_object_decoder rpc_aio_delete_decoders[] = {
77e21c39aaSAnton Nayshtut 	{"name", offsetof(struct rpc_aio_delete, name), spdk_json_decode_string},
78e21c39aaSAnton Nayshtut };
79e21c39aaSAnton Nayshtut 
80e21c39aaSAnton Nayshtut static void
81e21c39aaSAnton Nayshtut rpc_aio_delete_cb(void *cb_arg, int fsdeverrno)
82e21c39aaSAnton Nayshtut {
83e21c39aaSAnton Nayshtut 	struct spdk_jsonrpc_request *request = cb_arg;
84e21c39aaSAnton Nayshtut 
85e21c39aaSAnton Nayshtut 	if (fsdeverrno == 0) {
86e21c39aaSAnton Nayshtut 		spdk_jsonrpc_send_bool_response(request, true);
87e21c39aaSAnton Nayshtut 	} else {
88e21c39aaSAnton Nayshtut 		spdk_jsonrpc_send_error_response(request, fsdeverrno, spdk_strerror(-fsdeverrno));
89e21c39aaSAnton Nayshtut 	}
90e21c39aaSAnton Nayshtut }
91e21c39aaSAnton Nayshtut 
92e21c39aaSAnton Nayshtut static void
93e21c39aaSAnton Nayshtut rpc_aio_delete(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
94e21c39aaSAnton Nayshtut {
95e21c39aaSAnton Nayshtut 	struct rpc_aio_delete req = {};
96e21c39aaSAnton Nayshtut 
97e21c39aaSAnton Nayshtut 	if (spdk_json_decode_object(params, rpc_aio_delete_decoders,
98e21c39aaSAnton Nayshtut 				    SPDK_COUNTOF(rpc_aio_delete_decoders),
99e21c39aaSAnton Nayshtut 				    &req)) {
100e21c39aaSAnton Nayshtut 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
101e21c39aaSAnton Nayshtut 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
102e21c39aaSAnton Nayshtut 						 "spdk_json_decode_object failed");
103e21c39aaSAnton Nayshtut 
104e21c39aaSAnton Nayshtut 		free(req.name);
105e21c39aaSAnton Nayshtut 		return;
106e21c39aaSAnton Nayshtut 	}
107e21c39aaSAnton Nayshtut 
108e21c39aaSAnton Nayshtut 	spdk_fsdev_aio_delete(req.name, rpc_aio_delete_cb, request);
109e21c39aaSAnton Nayshtut 	free(req.name);
110e21c39aaSAnton Nayshtut }
111e21c39aaSAnton Nayshtut SPDK_RPC_REGISTER("fsdev_aio_delete", rpc_aio_delete, SPDK_RPC_RUNTIME)
112