xref: /spdk/module/fsdev/aio/fsdev_aio_rpc.c (revision b69e3ff279affbf4cbc4a09fa1f9c6c2b72397ff)
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 };
32 
33 static void
34 rpc_aio_create(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
35 {
36 	struct rpc_aio_create req = {};
37 	struct spdk_json_write_ctx *w;
38 	struct spdk_fsdev *fsdev;
39 	int rc;
40 
41 	spdk_fsdev_aio_get_default_opts(&req.opts);
42 
43 	if (spdk_json_decode_object(params, rpc_aio_create_decoders,
44 				    SPDK_COUNTOF(rpc_aio_create_decoders),
45 				    &req)) {
46 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
47 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
48 						 "spdk_json_decode_object failed");
49 
50 		free_rpc_aio_create(&req);
51 		return;
52 	}
53 
54 	rc = spdk_fsdev_aio_create(&fsdev, req.name, req.root_path, &req.opts);
55 	if (rc) {
56 		SPDK_ERRLOG("Failed to create aio %s: rc %d\n", req.name, rc);
57 		spdk_jsonrpc_send_error_response(request,
58 						 SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
59 						 spdk_strerror(-rc));
60 		free_rpc_aio_create(&req);
61 		return;
62 	}
63 
64 	w = spdk_jsonrpc_begin_result(request);
65 	spdk_json_write_string(w, fsdev->name);
66 	spdk_jsonrpc_end_result(request, w);
67 	free_rpc_aio_create(&req);
68 }
69 SPDK_RPC_REGISTER("fsdev_aio_create", rpc_aio_create, SPDK_RPC_RUNTIME)
70 
71 struct rpc_aio_delete {
72 	char *name;
73 };
74 
75 static const struct spdk_json_object_decoder rpc_aio_delete_decoders[] = {
76 	{"name", offsetof(struct rpc_aio_delete, name), spdk_json_decode_string},
77 };
78 
79 static void
80 rpc_aio_delete_cb(void *cb_arg, int fsdeverrno)
81 {
82 	struct spdk_jsonrpc_request *request = cb_arg;
83 
84 	if (fsdeverrno == 0) {
85 		spdk_jsonrpc_send_bool_response(request, true);
86 	} else {
87 		spdk_jsonrpc_send_error_response(request, fsdeverrno, spdk_strerror(-fsdeverrno));
88 	}
89 }
90 
91 static void
92 rpc_aio_delete(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
93 {
94 	struct rpc_aio_delete req = {};
95 
96 	if (spdk_json_decode_object(params, rpc_aio_delete_decoders,
97 				    SPDK_COUNTOF(rpc_aio_delete_decoders),
98 				    &req)) {
99 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
100 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
101 						 "spdk_json_decode_object failed");
102 
103 		free(req.name);
104 		return;
105 	}
106 
107 	spdk_fsdev_aio_delete(req.name, rpc_aio_delete_cb, request);
108 	free(req.name);
109 }
110 SPDK_RPC_REGISTER("fsdev_aio_delete", rpc_aio_delete, SPDK_RPC_RUNTIME)
111