1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/filesystem/userfs/userfs.h"
4#include "mos/filesystem/vfs.h"
5#include "mos/mm/slab_autoinit.h"
6#include "mos/syslog/printk.h"
7#include "mos/tasks/kthread.h"
8#include "proto/userfs-manager.services.h"
9
10#include <librpc/rpc.h>
11#include <librpc/rpc_server.h>
12#include <mos/proto/fs_server.h>
13#include <mos_stdio.h>
14#include <pb_decode.h>
15#include <pb_encode.h>
16
17static slab_t *userfs_slab = NULL;
18SLAB_AUTOINIT("userfs", userfs_slab, userfs_t);
19
20MOS_RPC_USERFS_MANAGER_SERVER(userfs_manager)
21
22static rpc_result_code_t userfs_manager_register_filesystem(rpc_context_t *, mosrpc_userfs_register_request *req, mosrpc_userfs_register_response *resp)
23{
24 userfs_t *userfs = kmalloc(userfs_slab);
25 if (!userfs)
26 return RPC_RESULT_SERVER_INTERNAL_ERROR;
27
28 linked_list_init(list_node(&userfs->fs));
29
30 size_t userfs_fsnamelen = strlen(str: "userfs.") + strlen(str: req->fs.name) + 1;
31 userfs->fs.name = kmalloc(userfs_fsnamelen);
32 if (!userfs->fs.name)
33 {
34 kfree(ptr: userfs);
35 return RPC_RESULT_SERVER_INTERNAL_ERROR;
36 }
37
38 snprintf(str: (char *) userfs->fs.name, size: userfs_fsnamelen, format: "userfs.%s", req->fs.name);
39 userfs->rpc_server_name = strdup(src: req->rpc_server_name);
40
41 resp->result.success = true;
42
43 userfs->fs.mount = userfs_fsop_mount;
44 vfs_register_filesystem(fs: &userfs->fs);
45 return RPC_RESULT_OK;
46}
47
48static void userfs_manager_server_exec(void *arg)
49{
50 MOS_UNUSED(arg);
51 rpc_server_t *fs_server = rpc_server_create(USERFS_SERVER_RPC_NAME, NULL);
52 rpc_server_register_functions(server: fs_server, functions: userfs_manager_functions, MOS_ARRAY_SIZE(userfs_manager_functions));
53 rpc_server_exec(server: fs_server);
54 pr_emerg("fs_rpc_execute_server exited");
55}
56
57static void userfs_manager_rpc_init()
58{
59 kthread_create(entry: userfs_manager_server_exec, NULL, name: "fs_rpc_server");
60}
61
62MOS_INIT(KTHREAD, userfs_manager_rpc_init);
63