refactor: one more step towards arch-agnostic design
[lunaix-os.git] / lunaix-os / kernel / fs / twifs / twifs.c
1 /**
2  * @file twifs.c
3  * @author Lunaixsky (zelong56@gmail.com)
4  * @brief TwiFS - A pseudo file system for kernel state exposure.
5  * @version 0.1
6  * @date 2022-07-21
7  *
8  * @copyright Copyright (c) 2022
9  *
10  */
11 #include <klibc/stdio.h>
12 #include <klibc/string.h>
13 #include <lunaix/clock.h>
14 #include <lunaix/fs.h>
15 #include <lunaix/fs/twifs.h>
16 #include <lunaix/fs/twimap.h>
17 #include <lunaix/mm/cake.h>
18 #include <lunaix/mm/valloc.h>
19 #include <lunaix/spike.h>
20
21 static struct twifs_node* fs_root;
22
23 static struct cake_pile* twi_pile;
24
25 static volatile u32_t inode_id = 0;
26
27 extern const struct v_file_ops twifs_file_ops;
28 extern const struct v_inode_ops twifs_inode_ops;
29
30 struct twifs_node*
31 __twifs_new_node(struct twifs_node* parent,
32                  const char* name,
33                  int name_len,
34                  u32_t itype)
35 {
36     struct twifs_node* node = cake_grab(twi_pile);
37     memset(node, 0, sizeof(*node));
38
39     strncpy(node->name_val, name, VFS_NAME_MAXLEN);
40
41     node->name = HSTR(node->name_val, MIN(name_len, VFS_NAME_MAXLEN));
42     node->itype = itype;
43     node->ino_id = inode_id++;
44     hstr_rehash(&node->name, HSTR_FULL_HASH);
45     llist_init_head(&node->children);
46
47     if (parent) {
48         llist_append(&parent->children, &node->siblings);
49     }
50
51     return node;
52 }
53
54 void
55 __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
56 {
57     inode->ops = (struct v_inode_ops*)&twifs_inode_ops;
58     inode->default_fops = (struct v_file_ops*)&twifs_file_ops;
59 }
60
61 int
62 __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
63 {
64     vsb->ops.init_inode = __twifs_init_inode;
65
66     struct v_inode* inode = vfs_i_alloc(vsb);
67     if (!inode) {
68         return ENOMEM;
69     }
70
71     inode->id = fs_root->ino_id;
72     inode->itype = fs_root->itype;
73     inode->data = fs_root;
74
75     vfs_i_addhash(inode);
76     vfs_assign_inode(mount_point, inode);
77     return 0;
78 }
79
80 int
81 __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
82 {
83     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
84     if (!twi_node || !twi_node->ops.write) {
85         return ENOTSUP;
86     }
87     return twi_node->ops.write(inode, buffer, len, fpos);
88 }
89
90 int
91 __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
92 {
93     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
94     if (!twi_node || !twi_node->ops.read) {
95         return ENOTSUP;
96     }
97     return twi_node->ops.read(inode, buffer, len, fpos);
98 }
99
100 struct twifs_node*
101 __twifs_get_node(struct twifs_node* parent, struct hstr* name)
102 {
103     if (!parent)
104         return NULL;
105
106     struct twifs_node *pos, *n;
107     llist_for_each(pos, n, &parent->children, siblings)
108     {
109         if (HSTR_EQ(&pos->name, name)) {
110             return pos;
111         }
112     }
113     return NULL;
114 }
115
116 int
117 __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode)
118 {
119     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
120
121     if (!(twi_node->itype & VFS_IFDIR)) {
122         return ENOTDIR;
123     }
124
125     struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name);
126     if (child_node) {
127         struct v_inode* child_inode = vfs_i_find(inode->sb, child_node->ino_id);
128         if (child_inode) {
129             goto done;
130         }
131
132         if (!(child_inode = vfs_i_alloc(inode->sb))) {
133             return ENOENT;
134         }
135
136         child_inode->id = child_node->ino_id;
137         child_inode->itype = child_node->itype;
138         child_inode->data = child_node;
139
140         vfs_i_addhash(child_inode);
141
142     done:
143         dnode->data = child_node->data;
144         vfs_assign_inode(dnode, child_inode);
145         return 0;
146     }
147     return ENOENT;
148 }
149
150 int
151 __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx)
152 {
153     struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data);
154     int counter = 0;
155     struct twifs_node *pos, *n;
156
157     llist_for_each(pos, n, &twi_node->children, siblings)
158     {
159         if (counter++ >= dctx->index) {
160             dctx->index = counter;
161             dctx->read_complete_callback(
162               dctx, pos->name.value, pos->name.len, vfs_get_dtype(pos->itype));
163             return 1;
164         }
165     }
166
167     return 0;
168 }
169
170 int
171 __twifs_openfile(struct v_inode* inode, struct v_file* file)
172 {
173     struct twifs_node* twi_node = (struct twifs_node*)inode->data;
174     if (twi_node) {
175         return 0;
176     }
177     return ENOTSUP;
178 }
179
180 int
181 twifs_rm_node(struct twifs_node* node)
182 {
183     if ((node->itype & VFS_IFDIR) && !llist_empty(&node->children)) {
184         return ENOTEMPTY;
185     }
186     llist_delete(&node->siblings);
187     cake_release(twi_pile, node);
188     return 0;
189 }
190
191 struct twifs_node*
192 twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args)
193 {
194     char buf[VFS_NAME_MAXLEN];
195     size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
196
197     return __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFSEQDEV);
198 }
199
200 struct twifs_node*
201 twifs_file_node(struct twifs_node* parent, const char* fmt, ...)
202 {
203     va_list args;
204     va_start(args, fmt);
205
206     struct twifs_node* twi_node = twifs_file_node_vargs(parent, fmt, args);
207
208     va_end(args);
209
210     return twi_node;
211 }
212
213 struct twifs_node*
214 twifs_dir_node(struct twifs_node* parent, const char* fmt, ...)
215 {
216     va_list args;
217     va_start(args, fmt);
218
219     char buf[VFS_NAME_MAXLEN];
220     size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
221     struct twifs_node* twi_node =
222       __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR);
223
224     va_end(args);
225
226     return twi_node;
227 }
228
229 void
230 twifs_init()
231 {
232     twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0);
233
234     struct filesystem* twifs = vzalloc(sizeof(struct filesystem));
235     twifs->fs_name = HSTR("twifs", 5);
236     twifs->mount = __twifs_mount;
237     twifs->types = FSTYPE_ROFS;
238     twifs->fs_id = 0;
239
240     fsm_register(twifs);
241
242     fs_root = twifs_dir_node(NULL, NULL, 0, 0);
243 }
244
245 void
246 twifs_register_plugins()
247 {
248     int i = 0;
249     ptr_t init_twifs_plugin_fn;
250     ldga_foreach(twiplugin_inits, ptr_t, i, init_twifs_plugin_fn)
251     {
252         ((void (*)())init_twifs_plugin_fn)();
253     }
254 }
255
256 int
257 __twifs_twimap_file_read(struct v_inode* inode,
258                          void* buf,
259                          size_t len,
260                          size_t fpos)
261 {
262     struct twimap* map = twinode_getdata(inode, struct twimap*);
263     return twimap_read(map, buf, len, fpos);
264 }
265
266 struct twimap*
267 twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...)
268 {
269     va_list args;
270     va_start(args, fmt);
271
272     struct twimap* map = twimap_create(data);
273     struct twifs_node* node = twifs_file_node_vargs(parent, fmt, args);
274     node->ops.read = __twifs_twimap_file_read;
275     node->data = map;
276
277     return map;
278 }
279
280 const struct v_file_ops twifs_file_ops = { .close = default_file_close,
281                                            .read = __twifs_fread,
282                                            .read_page = __twifs_fread,
283                                            .write = __twifs_fwrite,
284                                            .write_page = __twifs_fwrite,
285                                            .readdir = __twifs_iterate_dir };
286
287 const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup,
288                                              .mkdir = default_inode_mkdir,
289                                              .rmdir = default_inode_rmdir,
290                                              .open = __twifs_openfile };