feat: owloysius - dynamic init function invocator
[lunaix-os.git] / lunaix-os / kernel / fs / fsm.c
1 /**
2  * @file fsm.c File system manager
3  * @author Lunaixsky (zelong56@gmail.com)
4  * @brief
5  * @version 0.1
6  * @date 2022-07-19
7  *
8  * @copyright Copyright (c) 2022
9  *
10  */
11 #include <klibc/string.h>
12 #include <lunaix/ds/hashtable.h>
13 #include <lunaix/fs.h>
14 #include <lunaix/mm/valloc.h>
15
16 #include <lunaix/fs/twimap.h>
17 #include <lunaix/fs/twifs.h>
18
19 #define HASH_BUCKET_BITS 4
20 #define HASH_BUCKET_NUM (1 << HASH_BUCKET_BITS)
21
22 DEFINE_LLIST(fs_flatlist);
23 DECLARE_HASHTABLE(fs_registry, HASH_BUCKET_NUM);
24
25 void
26 fsm_init()
27 {
28     hashtable_init(fs_registry);
29
30     ldga_invoke_fn0(fs);
31 }
32
33 void
34 fsm_register(struct filesystem* fs)
35 {
36     hstr_rehash(&fs->fs_name, HSTR_FULL_HASH);
37     hashtable_hash_in(fs_registry, &fs->fs_list, fs->fs_name.hash);
38     llist_append(&fs_flatlist, &fs->fs_flat);
39 }
40
41 struct filesystem*
42 fsm_get(const char* fs_name)
43 {
44     struct filesystem *pos, *next;
45     struct hstr str = HSTR(fs_name, 0);
46     hstr_rehash(&str, HSTR_FULL_HASH);
47
48     hashtable_hash_foreach(fs_registry, str.hash, pos, next, fs_list)
49     {
50         if (pos->fs_name.hash == str.hash) {
51             return pos;
52         }
53     }
54
55     return NULL;
56 }
57
58 struct filesystem*
59 fsm_new_fs(char* name, size_t name_len)
60 {
61     struct filesystem* fs = vzalloc(sizeof(*fs));
62     if (name_len == (size_t)-1) {
63         name_len = strlen(name);
64     }
65     fs->fs_name = HHSTR(name, name_len, 0);
66     return fs;
67 }
68
69 static void
70 read_fslist(struct twimap *mapping)
71 {
72     struct filesystem *pos, *n;
73     llist_for_each(pos, n, &fs_flatlist, fs_flat)
74     {
75         twimap_printf(mapping, "%s %d\n", pos->fs_name.value, pos->types);
76     }
77 }
78
79 static void
80 fstab_twifs_plugin()
81 {
82     struct twimap* map = twifs_mapping(NULL, NULL, "fstab");
83     map->read = read_fslist;
84 }
85 EXPORT_TWIFS_PLUGIN(fstab, fstab_twifs_plugin);