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