1 | // SPDX-License-Identifier: GPL-3.0-or-later |
2 | |
3 | #pragma once |
4 | |
5 | #include "blockdev.h" |
6 | #include "proto/blockdev.services.h" |
7 | |
8 | #include <abi-bits/ino_t.h> |
9 | #include <librpc/rpc_client.h> |
10 | #include <librpc/rpc_server++.hpp> |
11 | #include <librpc/rpc_server.h> |
12 | #include <map> |
13 | #include <pb_decode.h> |
14 | #include <pb_encode.h> |
15 | #include <string> |
16 | #include <variant> |
17 | |
18 | using namespace mosrpc::blockdev; |
19 | |
20 | struct BlockDeviceInfo |
21 | { |
22 | std::string server_name; |
23 | }; |
24 | |
25 | struct BlockLayerInfo |
26 | { |
27 | std::string server_name; |
28 | u32 partid; |
29 | }; |
30 | |
31 | struct BlockInfo |
32 | { |
33 | ino_t ino; // inode number in blockdevfs |
34 | std::string name; |
35 | size_t n_blocks; |
36 | size_t block_size; |
37 | |
38 | enum |
39 | { |
40 | BLOCKDEV_LAYER, |
41 | BLOCKDEV_DEVICE, |
42 | } type; |
43 | |
44 | std::variant<BlockLayerInfo, BlockDeviceInfo> info; |
45 | }; |
46 | |
47 | extern std::map<std::string, BlockInfo> devices; // blockdev name -> blockdev info |
48 | |
49 | class BlockManager : public IBlockdevManagerService |
50 | { |
51 | |
52 | public: |
53 | explicit BlockManager() : IBlockdevManagerService(BLOCKDEV_MANAGER_RPC_SERVER_NAME) {}; |
54 | |
55 | private: |
56 | virtual void on_connect(rpc_context_t *ctx) override; |
57 | virtual void on_disconnect(rpc_context_t *ctx) override; |
58 | virtual rpc_result_code_t register_layer_server(rpc_context_t *, register_layer_server::request *req, register_layer_server::response *resp) override; |
59 | virtual rpc_result_code_t register_device(rpc_context_t *, register_device::request *req, register_device::response *resp) override; |
60 | virtual rpc_result_code_t open_device(rpc_context_t *ctx, open_device::request *req, open_device::response *resp) override; |
61 | virtual rpc_result_code_t read_block(rpc_context_t *, read_block::request *req, read_block::response *resp) override; |
62 | virtual rpc_result_code_t write_block(rpc_context_t *, write_block::request *req, write_block::response *resp) override; |
63 | }; |
64 | |
65 | bool register_blockdevfs(); |
66 | |